Repository navigation
fix(child-sessions): scope session_shutdown disposal to that session's children #3204
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
Open
Jarcis-cy
wants to merge
2
commits into
nicobailon:main
Choose a base branch
from
Jarcis-cy:fix/session-scoped-child-dispose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
−22
Open
Changes from 1 commit
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
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
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,120 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { describe, it } from "node:test"; | ||
| import { | ||
| createDefaultChildSessionFactory, | ||
| type ChildSessionLaunch, | ||
| type PiCodingAgentModule, | ||
| } from "../../src/runs/shared/child-session.ts"; | ||
|
|
||
| /** | ||
| * A multi-session host such as pi-web runs several parent sessions in one process. | ||
| * Its idle reaper shuts wrappers down one at a time, each shutdown emitting | ||
| * `session_shutdown` to that session's extension instance. The extension must dispose | ||
| * only that session's children: one session's shutdown must not abort another | ||
| * session's running children. | ||
| */ | ||
|
|
||
| interface SessionLog { | ||
| parent: string | undefined; | ||
| events: string[]; | ||
| } | ||
|
|
||
| function stubPiModule(logs: SessionLog[]): () => PiCodingAgentModule { | ||
| return () => { | ||
| const log: SessionLog = { parent: undefined, events: [] }; | ||
| logs.push(log); | ||
| return { | ||
| ModelRuntime: { create: async () => ({ refresh: async () => {} }) }, | ||
| SettingsManager: { create: () => ({}) }, | ||
| DefaultResourceLoader: class { async reload() {} getExtensions() { return { extensions: [], errors: [], runtime: { pendingProviderRegistrations: [], pendingNativeProviderRegistrations: [] } }; } }, | ||
| SessionManager: { inMemory: () => ({}) }, | ||
| resolveCliModel: () => ({}), | ||
| createAgentSession: async () => ({ | ||
| session: { | ||
| bindExtensions: async () => {}, | ||
| dispose() { log.events.push("dispose"); }, | ||
| extensionRunner: { hasHandlers: () => false }, | ||
| subscribe: () => () => {}, | ||
| prompt: async () => {}, | ||
| abort: async () => { log.events.push("abort"); }, | ||
| steer: async () => {}, | ||
| followUp: async () => {}, | ||
| messages: [], | ||
| sessionId: `child-${logs.length}`, | ||
| }, | ||
| }), | ||
| } as unknown as PiCodingAgentModule; | ||
| }; | ||
| } | ||
|
|
||
| function launchFor(parentSessionFile: string | undefined, cwd: string): ChildSessionLaunch { | ||
| return { | ||
| cwd, | ||
| storage: { kind: "memory" }, | ||
| parentSessionFile, | ||
| extensionPaths: [], | ||
| ambientExtensions: false, | ||
| hooks: [], | ||
| noSkills: true, | ||
| noContextFiles: true, | ||
| runtime: { fanoutChild: false, depth: 1, waitTool: { enabled: false }, fast: false }, | ||
| }; | ||
| } | ||
|
|
||
| describe("disposeForParent scopes child disposal to one session", () => { | ||
| it("disposes only the parent's own children, leaving other sessions' children running", async () => { | ||
| const logs: SessionLog[] = []; | ||
| const factory = createDefaultChildSessionFactory({ loadPiCodingAgent: stubPiModule(logs) }); | ||
| const parentA = "/tmp/sessions/parent-a.jsonl"; | ||
| const parentB = "/tmp/sessions/parent-b.jsonl"; | ||
| const childA1 = await factory.create(launchFor(parentA, process.cwd())); | ||
| await factory.create(launchFor(parentB, process.cwd())); | ||
| const childA1Abort = childA1.abort.bind(childA1); | ||
|
|
||
| await factory.disposeForParent(parentA); | ||
|
|
||
| assert.deepEqual(logs[0]?.events, ["abort", "dispose"], "parent A's child is aborted and disposed"); | ||
| assert.deepEqual(logs[1]?.events, [], "parent B's child is untouched"); | ||
| // The surviving child still works and can be disposed on its own session's shutdown. | ||
| await assert.doesNotReject(childA1Abort); | ||
| }); | ||
|
|
||
| it("disposes only children of file-less parents when the parent session has no file", async () => { | ||
| const logs: SessionLog[] = []; | ||
| const factory = createDefaultChildSessionFactory({ loadPiCodingAgent: stubPiModule(logs) }); | ||
| await factory.create(launchFor("/tmp/sessions/parent-a.jsonl", process.cwd())); | ||
| await factory.create(launchFor(undefined, process.cwd())); | ||
|
|
||
| await factory.disposeForParent(undefined); | ||
|
|
||
| assert.deepEqual(logs[0]?.events, [], "the file-backed parent's child is untouched"); | ||
| assert.deepEqual(logs[1]?.events, ["abort", "dispose"], "the file-less parent's child is disposed"); | ||
| }); | ||
|
|
||
| it("dispose() still disposes every live child", async () => { | ||
| const logs: SessionLog[] = []; | ||
| const factory = createDefaultChildSessionFactory({ loadPiCodingAgent: stubPiModule(logs) }); | ||
| await factory.create(launchFor("/tmp/sessions/parent-a.jsonl", process.cwd())); | ||
| await factory.create(launchFor("/tmp/sessions/parent-b.jsonl", process.cwd())); | ||
| await factory.create(launchFor(undefined, process.cwd())); | ||
|
|
||
| await factory.dispose(); | ||
|
|
||
| for (const log of logs) { | ||
| assert.deepEqual(log.events, ["abort", "dispose"], "every child is aborted and disposed"); | ||
| } | ||
| }); | ||
|
|
||
| it("detached children keep running across both disposal scopes", async () => { | ||
| const logs: SessionLog[] = []; | ||
| const factory = createDefaultChildSessionFactory({ loadPiCodingAgent: stubPiModule(logs) }); | ||
| const parentA = "/tmp/sessions/parent-a.jsonl"; | ||
| const child = await factory.create(launchFor(parentA, process.cwd())); | ||
| child.detached = true; | ||
|
|
||
| await factory.disposeForParent(parentA); | ||
| await factory.dispose(); | ||
|
|
||
| assert.deepEqual(logs[0]?.events, [], "a detached child is never aborted by disposal"); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.