A desktop coding agent with free access to frontier models.
TypeScript
fix(engine2): forget a sub-agent's child session once its call ends
Anurag Pappula597bf013ebea
diff --git a/apps/server/src/engine2/events.ts b/apps/server/src/engine2/events.tsindex 68ee0e51..d7ccd384 100644--- a/apps/server/src/engine2/events.ts+++ b/apps/server/src/engine2/events.ts@@ -77,7 +77,15 @@ function modelRef(value: unknown): ModelRef | undefined {return variant && variant !== "default" ? { providerID, modelID, variant } : { providerID, modelID };}-/** How many sub-agent calls' child sessions the translator remembers. */+/** The child session a sub-agent call's progress or end metadata names. */+function childSessionIn(metadata: unknown): string | undefined {+ return isRecord(metadata) && typeof metadata.sessionID === "string" && metadata.sessionID ? metadata.sessionID : undefined;+}++/**+ * A backstop on the remembered child sessions: an entry normally goes when its+ * call ends, so this only bounds calls that never report an end (engine crash).+ */const MAX_CHILD_SESSIONS = 2000;export class EventTranslator {@@ -102,7 +110,7 @@ export class EventTranslator {}private noteChildSession(sessionID: string, callID: string, metadata: unknown): void {- const child = isRecord(metadata) && typeof metadata.sessionID === "string" && metadata.sessionID ? metadata.sessionID : undefined;+ const child = childSessionIn(metadata);if (!child) return;const key = `${sessionID}\u0000${callID}`;this.childSessions.delete(key);@@ -110,6 +118,20 @@ export class EventTranslator {if (this.childSessions.size > MAX_CHILD_SESSIONS) this.childSessions.delete(this.childSessions.keys().next().value as string);}+ /**+ * Forgets a call's child once its end event names it: the engine's own record+ * of the call carries it from then on. An end that does not name it (a failure+ * the engine may store without it) keeps the entry, so the card stays linked.+ */+ private settleChildSession(sessionID: string, callID: string, metadata: unknown): void {+ if (childSessionIn(metadata)) this.childSessions.delete(`${sessionID}\u0000${callID}`);+ }++ private forgetChildSessionsOf(sessionID: string): void {+ const prefix = `${sessionID}\u0000`;+ for (const key of this.childSessions.keys()) if (key.startsWith(prefix)) this.childSessions.delete(key);+ }+/** The 1.x status of every session the stream saw busy or retrying. */statuses(): Record<string, JsonRecord> {const out: Record<string, JsonRecord> = {};@@ -319,6 +341,7 @@ export class EventTranslator {const info = this.session(sessionID).info ?? { id: sessionID };const out = [this.scoped(sessionID, "session.deleted", { sessionID, info })];this.sessions.delete(sessionID);+ this.forgetChildSessionsOf(sessionID);return out;}case "session.agent.selected": {@@ -561,8 +584,9 @@ export class EventTranslator {}case "session.tool.success":case "session.tool.failed": {- const live = this.liveFor(data);const callID = str(data, "id");+ if (sessionID && callID) this.settleChildSession(sessionID, callID, data.metadata);+ const live = this.liveFor(data);if (!live || !callID) return [];const entry = this.toolEntry(live, callID);const state = record(entry, "state") ?? {};diff --git a/apps/server/src/engine2/shapes.test.ts b/apps/server/src/engine2/shapes.test.tsindex 6a74422e..9f958ed9 100644--- a/apps/server/src/engine2/shapes.test.ts+++ b/apps/server/src/engine2/shapes.test.ts@@ -426,6 +426,31 @@ describe("live events and read-back agree", () => {expect(translator.childSessionOf("ses_other", tasks[0]!.callID)).toBeUndefined();});+ test("a sub-agent call's child is remembered only until the engine's own record names it", async () => {+ const translator = new EventTranslator({ version: "2.0.18" });+ const progress = (sessionID: string, id: string, child: string) =>+ translator.translate({ type: "session.tool.progress", data: { sessionID, id, metadata: { sessionID: child, status: "running" } } });+ await progress("ses_p", "call_ok", "ses_c1");+ await progress("ses_p", "call_bad", "ses_c2");+ await progress("ses_p", "call_bad_named", "ses_c3");+ await progress("ses_q", "call_other", "ses_c4");++ // Success names the child (the engine stores it from then on): forgotten.+ await translator.translate({ type: "session.tool.success", data: { sessionID: "ses_p", id: "call_ok", metadata: { sessionID: "ses_c1", status: "completed" } } });+ expect(translator.childSessionOf("ses_p", "call_ok")).toBeUndefined();+ // A failure that does not name the child keeps it, so its card stays linked.+ await translator.translate({ type: "session.tool.failed", data: { sessionID: "ses_p", id: "call_bad", error: "aborted" } });+ expect(translator.childSessionOf("ses_p", "call_bad")).toBe("ses_c2");+ // A failure that names it is forgotten like a success.+ await translator.translate({ type: "session.tool.failed", data: { sessionID: "ses_p", id: "call_bad_named", error: "boom", metadata: { sessionID: "ses_c3" } } });+ expect(translator.childSessionOf("ses_p", "call_bad_named")).toBeUndefined();++ // Deleting the parent session forgets its calls, and only its calls.+ await translator.translate({ type: "session.deleted", data: { sessionID: "ses_p" } });+ expect(translator.childSessionOf("ses_p", "call_bad")).toBeUndefined();+ expect(translator.childSessionOf("ses_q", "call_other")).toBe("ses_c4");+ });+test("busy and idle come from the execution lifecycle", async () => {const translator = new EventTranslator({ version: "2.0.18" });const started = await translator.translate({ type: "session.execution.started", data: { sessionID: "ses_a" } });