Skip to content

Commit 7067b99

Browse files
committed
fix(server): preserve child approval awareness after main merge
1 parent 3d8d460 commit 7067b99

3 files changed

Lines changed: 57 additions & 10 deletions

File tree

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
178178
readonly respond?: (attempt: number) => Response;
179179
readonly failSecretRead?: (name: string) => boolean;
180180
readonly linked?: boolean;
181+
readonly startupThreads?: ReadonlyArray<OrchestrationV2ThreadShell>;
181182
readonly readShell?: (threadId: ThreadId) => Effect.Effect<OrchestrationV2ThreadShell | null>;
182183
readonly domainEvents?: Stream.Stream<OrchestrationV2DomainEvent>;
183184
} = {},
@@ -220,14 +221,24 @@ const makeTestRelay = Effect.fnUntraced(function* (
220221
Effect.sync(() => shellReads.push(threadId)).pipe(
221222
Effect.andThen(options.readShell?.(threadId) ?? Ref.get(currentShell)),
222223
),
223-
getShellSnapshot: () =>
224+
getShellSnapshot: (input) =>
224225
Effect.sync(() => {
225226
catchUp.shellSnapshotReads += 1;
226-
return { schemaVersion: 2, snapshotSequence: 1, threads: [], archivedThreads: [] };
227+
return {
228+
schemaVersion: 2,
229+
snapshotSequence: 1,
230+
threads: (options.startupThreads ?? []).filter((thread) =>
231+
input?.threadIds?.includes(thread.id),
232+
),
233+
archivedThreads: [],
234+
};
227235
}),
228236
listActiveSubagentThreadIds: unused,
229237
countThreads: unused,
230-
listActiveShellIndex: () => Effect.succeed([]),
238+
listActiveShellIndex: () =>
239+
Effect.succeed(
240+
(options.startupThreads ?? []).map((thread) => ({ ...thread, mayBeLive: true })),
241+
),
231242
listOlderSettledThreads: unused,
232243
readShellSnapshot: unused,
233244
ensureLegacyTranscript: unused,
@@ -329,6 +340,42 @@ const makeTestRelay = Effect.fnUntraced(function* (
329340
});
330341

331342
describe("AgentAwarenessRelay", () => {
343+
it.effect("publishes an approval already waiting at startup", () =>
344+
Effect.gen(function* () {
345+
const parent = shell({ status: "idle" });
346+
const child = waitingChild();
347+
const { relay, publications } = yield* makeTestRelay({
348+
startupThreads: [parent, child],
349+
readShell: (id) => Effect.succeed(id === child.id ? child : parent),
350+
});
351+
yield* relay.start();
352+
yield* TestClock.adjust("1 second");
353+
yield* relay.drain;
354+
assert.equal(publications.length, 1);
355+
assert.equal(publications[0]?.state?.threadId, THREAD_ID);
356+
assert.equal(publications[0]?.state?.phase, "waiting_for_approval");
357+
}).pipe(Effect.scoped),
358+
);
359+
360+
it.effect("leaves coordinator-handled child questions out of phone activity", () =>
361+
Effect.gen(function* () {
362+
const child = waitingChild({
363+
pendingRuntimeRequest: {
364+
id: RuntimeRequestId.make("child-question"),
365+
kind: "user_input",
366+
createdAt: DateTime.makeUnsafe(NOW),
367+
},
368+
});
369+
const { relay, publications } = yield* makeTestRelay({
370+
readShell: (id) => Effect.succeed(id === child.id ? child : shell()),
371+
});
372+
yield* relay.publishThread(THREAD_ID);
373+
yield* relay.publishThread(CHILD_THREAD_ID);
374+
assert.equal(publications.length, 1);
375+
assert.equal(publications[0]?.state?.phase, "running");
376+
}),
377+
);
378+
332379
it.effect("publishes the parent as needing approval while its subagent waits", () =>
333380
Effect.gen(function* () {
334381
const shells = yield* Ref.make(

‎apps/server/src/relay/AgentAwarenessRelay.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,13 +894,13 @@ export const make = Effect.gen(function* () {
894894
}
895895
const environmentId = yield* serverEnvironment.getEnvironmentId;
896896
const projectSnapshot = yield* projects.snapshot;
897-
// Subagents never publish awareness, and they are most threads.
898-
const topLevelThreadIds = (yield* threads.listActiveShellIndex()).flatMap((thread) =>
899-
thread.lineage.relationshipToParent === "subagent" ? [] : [thread.id],
897+
// Hydrate only live children; their requests can make a top-level thread wait.
898+
const awarenessThreadIds = (yield* threads.listActiveShellIndex()).flatMap((thread) =>
899+
thread.lineage.relationshipToParent === "subagent" && !thread.mayBeLive ? [] : [thread.id],
900900
);
901901
const shellSnapshot = yield* threads.getShellSnapshot({
902902
location: "active",
903-
threadIds: topLevelThreadIds,
903+
threadIds: awarenessThreadIds,
904904
});
905905
const activeThreadIds = resolveAgentAwarenessRelayActiveThreadIds({
906906
environmentId,

‎packages/shared/src/agentAwareness.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ const isChildRunLive = (
4242
thread.status === "running" ||
4343
thread.status === "waiting";
4444

45-
/** The request a subagent thread raises on its parent's behalf. Provider-native
46-
subagents ask on their parent, so they are skipped. */
45+
/** Provider-native subagents ask on their parent; the coordinator answers child questions. */
4746
export function subagentPendingRequest(
4847
thread: Pick<
4948
OrchestrationV2ThreadShell,
@@ -54,13 +53,14 @@ export function subagentPendingRequest(
5453
if (
5554
kind === undefined ||
5655
kind === "auth_refresh" ||
56+
kind === "user_input" ||
5757
thread.lineage.relationshipToParent !== "subagent" ||
5858
isProviderNativeSubagentThread(thread) ||
5959
!isChildRunLive(thread)
6060
) {
6161
return null;
6262
}
63-
return kind === "user_input" ? "user_input" : "approval";
63+
return "approval";
6464
}
6565

6666
export interface AgentAwarenessState {

0 commit comments

Comments
 (0)