(
sessionId: string,
input: CompactSessionInput,
execution: PendingExecutionClaim,
)
| 957 | } |
| 958 | |
| 959 | private async *compactSessionClaimed( |
| 960 | sessionId: string, |
| 961 | input: CompactSessionInput, |
| 962 | execution: PendingExecutionClaim, |
| 963 | ): AsyncIterable<SessionEvent> { |
| 964 | await this.enterExecutionClaim(execution); |
| 965 | if ( |
| 966 | input.minRecentTurns !== undefined && |
| 967 | (!Number.isSafeInteger(input.minRecentTurns) || input.minRecentTurns < 0) |
| 968 | ) { |
| 969 | throw new Error('Runtime compaction minRecentTurns must be a non-negative safe integer'); |
| 970 | } |
| 971 | if (!this.deps.runStore || !this.deps.runtimeEventStore) { |
| 972 | throw new RuntimeContextCompactError( |
| 973 | 'operation_unavailable', |
| 974 | 'Runtime compaction requires execution stores', |
| 975 | ); |
| 976 | } |
| 977 | if (this.hasActiveRuns(sessionId)) { |
| 978 | throw new RuntimeContextCompactError( |
| 979 | 'session_busy', |
| 980 | 'Cannot compact while a Turn is running', |
| 981 | ); |
| 982 | } |
| 983 | |
| 984 | const header = await this.deps.store.readHeader(sessionId); |
| 985 | const turnId = input.turnId ?? this.deps.newId(); |
| 986 | const run = new AgentRun({ |
| 987 | sessionId, |
| 988 | header, |
| 989 | userInput: { turnId, text: '' }, |
| 990 | rootExecutionKind: 'context_compact', |
| 991 | ...(input.hostedRoot ? { runId: input.hostedRoot.runId } : {}), |
| 992 | store: this.deps.store, |
| 993 | runStore: this.deps.runStore, |
| 994 | runtimeEventStore: this.deps.runtimeEventStore, |
| 995 | ...(runtimeToolBoundaryProtocol(this.deps, header) |
| 996 | ? { toolBoundaryProtocol: runtimeToolBoundaryProtocol(this.deps, header) } |
| 997 | : {}), |
| 998 | repairRunRuntimeLedger: this.deps.repairRunRuntimeLedger, |
| 999 | newId: this.deps.newId, |
| 1000 | now: this.deps.now, |
| 1001 | effectiveOrchestration: resolveEffectiveOrchestration('default', undefined), |
| 1002 | hooks: { |
| 1003 | reserveRun: async (targetSessionId, nextHeader, activeRun) => { |
| 1004 | const active = await this.reserveParentRun( |
| 1005 | targetSessionId, |
| 1006 | nextHeader, |
| 1007 | activeRun, |
| 1008 | execution, |
| 1009 | ); |
| 1010 | this.reserveExecutionClaim(execution, active, activeRun); |
| 1011 | return active; |
| 1012 | }, |
| 1013 | unregisterRun: (active, activeRun) => this.unregisterParentRun(active, activeRun), |
| 1014 | updateHeader: (targetSessionId, patch) => this.updateHeader(targetSessionId, patch), |
| 1015 | updateStatus: (targetSessionId, status, blockedReason, ts) => |
| 1016 | this.updateStatus(targetSessionId, status, blockedReason, ts), |
no test coverage detected