-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(orchestrator): archive and delete owned subagent subtrees #3878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
PixPMusic
wants to merge
1
commit into
pingdotgg:t3code/codex-turn-mapping
from
PixPMusic:pixpmusic/orchestrator-subagent-lifecycle
+2,816
−210
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
apps/server/src/orchestration-v2/CheckpointCleanupService.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { assert, it } from "@effect/vitest"; | ||
| import { | ||
| CheckpointId, | ||
| CheckpointRef, | ||
| CheckpointScopeId, | ||
| ThreadId, | ||
| type OrchestrationV2Checkpoint, | ||
| type OrchestrationV2CheckpointScope, | ||
| type OrchestrationV2ThreadProjection, | ||
| } from "@t3tools/contracts"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Layer from "effect/Layer"; | ||
|
|
||
| import { CheckpointCleanupServiceV2, layer } from "./CheckpointCleanupService.ts"; | ||
| import { CheckpointServiceV2 } from "./CheckpointService.ts"; | ||
| import { ProjectionStoreV2 } from "./ProjectionStore.ts"; | ||
|
|
||
| it.effect("cleans every checkpoint scope through the latest known ordinal", () => { | ||
| const threadId = ThreadId.make("thread_checkpoint_cleanup"); | ||
| const firstScope = { | ||
| id: CheckpointScopeId.make("scope_first"), | ||
| threadId, | ||
| } as OrchestrationV2CheckpointScope; | ||
| const secondScope = { | ||
| id: CheckpointScopeId.make("scope_second"), | ||
| threadId, | ||
| } as OrchestrationV2CheckpointScope; | ||
| const checkpoint = { | ||
| id: CheckpointId.make("checkpoint_first"), | ||
| scopeId: firstScope.id, | ||
| ordinalWithinScope: 2, | ||
| ref: CheckpointRef.make("refs/t3/test/checkpoint-first"), | ||
| } as OrchestrationV2Checkpoint; | ||
| const projection = { | ||
| thread: { id: threadId }, | ||
| runs: [{ ordinal: 4 }], | ||
| checkpointScopes: [firstScope, secondScope], | ||
| checkpoints: [checkpoint], | ||
| } as unknown as OrchestrationV2ThreadProjection; | ||
| const calls: Array<{ | ||
| readonly scopeId: CheckpointScopeId; | ||
| readonly checkpointIds: ReadonlyArray<CheckpointId>; | ||
| readonly throughOrdinal: number; | ||
| }> = []; | ||
| const testLayer = layer.pipe( | ||
| Layer.provide( | ||
| Layer.merge( | ||
| Layer.mock(ProjectionStoreV2)({ | ||
| getThreadProjection: () => Effect.succeed(projection), | ||
| }), | ||
| Layer.mock(CheckpointServiceV2)({ | ||
| deleteScopeRefs: (input) => { | ||
| calls.push({ | ||
| scopeId: input.scope.id, | ||
| checkpointIds: input.checkpoints.map((item) => item.id), | ||
| throughOrdinal: input.throughOrdinal, | ||
| }); | ||
| return Effect.void; | ||
| }, | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| return Effect.gen(function* () { | ||
| const cleanup = yield* CheckpointCleanupServiceV2; | ||
| yield* cleanup.cleanup(threadId); | ||
|
|
||
| assert.deepEqual(calls, [ | ||
| { | ||
| scopeId: firstScope.id, | ||
| checkpointIds: [checkpoint.id], | ||
| throughOrdinal: 4, | ||
| }, | ||
| { | ||
| scopeId: secondScope.id, | ||
| checkpointIds: [], | ||
| throughOrdinal: 4, | ||
| }, | ||
| ]); | ||
| }).pipe(Effect.provide(testLayer)); | ||
| }); |
70 changes: 70 additions & 0 deletions
70
apps/server/src/orchestration-v2/CheckpointCleanupService.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { ThreadId } from "@t3tools/contracts"; | ||
| import * as Context from "effect/Context"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Schema from "effect/Schema"; | ||
|
|
||
| import { CheckpointServiceV2 } from "./CheckpointService.ts"; | ||
| import { ProjectionStoreV2 } from "./ProjectionStore.ts"; | ||
|
|
||
| export class CheckpointCleanupError extends Schema.TaggedErrorClass<CheckpointCleanupError>()( | ||
| "CheckpointCleanupError", | ||
| { | ||
| threadId: ThreadId, | ||
| cause: Schema.Defect(), | ||
| }, | ||
| ) { | ||
| override get message(): string { | ||
| return `Failed to clean checkpoint refs for thread ${this.threadId}.`; | ||
| } | ||
| } | ||
|
|
||
| export class CheckpointCleanupServiceV2 extends Context.Service< | ||
| CheckpointCleanupServiceV2, | ||
| { | ||
| readonly cleanup: (threadId: ThreadId) => Effect.Effect<void, CheckpointCleanupError>; | ||
| } | ||
| >()("t3/orchestration-v2/CheckpointCleanupService/CheckpointCleanupServiceV2") {} | ||
|
|
||
| export const layer: Layer.Layer< | ||
| CheckpointCleanupServiceV2, | ||
| never, | ||
| CheckpointServiceV2 | ProjectionStoreV2 | ||
| > = Layer.effect( | ||
| CheckpointCleanupServiceV2, | ||
| Effect.gen(function* () { | ||
| const checkpoints = yield* CheckpointServiceV2; | ||
| const projections = yield* ProjectionStoreV2; | ||
|
|
||
| const cleanup = Effect.fn("orchestrationV2.checkpointCleanup.cleanup")(function* ( | ||
| threadId: ThreadId, | ||
| ) { | ||
| const projection = yield* projections.getThreadProjection(threadId); | ||
| const throughOrdinal = Math.max( | ||
| 0, | ||
| ...projection.runs.map((run) => run.ordinal), | ||
| ...projection.checkpoints.map((checkpoint) => checkpoint.ordinalWithinScope), | ||
| ); | ||
|
|
||
| yield* Effect.forEach( | ||
| projection.checkpointScopes, | ||
| (scope) => | ||
| checkpoints.deleteScopeRefs({ | ||
| scope, | ||
| checkpoints: projection.checkpoints.filter( | ||
| (checkpoint) => checkpoint.scopeId === scope.id, | ||
| ), | ||
| throughOrdinal, | ||
| }), | ||
| { concurrency: 1, discard: true }, | ||
| ); | ||
| }); | ||
|
|
||
| return CheckpointCleanupServiceV2.of({ | ||
| cleanup: (threadId) => | ||
| cleanup(threadId).pipe( | ||
| Effect.mapError((cause) => new CheckpointCleanupError({ threadId, cause })), | ||
| ), | ||
| }); | ||
| }), | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium
orchestration-v2/CheckpointCleanupService.ts:43cleanupcomputesthroughOrdinalby spreading every run and checkpoint ordinal intoMath.max, so a thread with enough runs and checkpoints exceeds the JavaScript engine's function-argument limit and throwsRangeErrorbefore any scope refs are removed. Compute the maximum iteratively instead of spreading the arrays intoMath.max.🤖 Copy this AI Prompt to have your agent fix this: