Skip to content

Commit ff19c7c

Browse files
shivamhwpclaude
andcommitted
refactor(server): trim subagent approval push to what the fix needs
Drop same-millisecond tie-breaking, the alert-time advance past earlier alerts, paused-publishing tracking, and the older-server run-status fallback. A child's unchanged request still keeps its first alert time. Keep the core test plus the rename and parent-request regressions. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 68f7603 commit ff19c7c

3 files changed

Lines changed: 38 additions & 371 deletions

File tree

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

Lines changed: 19 additions & 278 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ const makeTestRelay = Effect.fnUntraced(function* (
169169
readonly respond?: (attempt: number) => Response;
170170
readonly failSecretRead?: (name: string) => boolean;
171171
readonly linked?: boolean;
172-
readonly startupThreads?: ReadonlyArray<OrchestrationV2ThreadShell>;
173172
readonly readShell?: (threadId: ThreadId) => Effect.Effect<OrchestrationV2ThreadShell | null>;
174173
readonly domainEvents?: Stream.Stream<OrchestrationV2DomainEvent>;
175174
readonly catchUpThreads?: ReadonlyArray<OrchestrationV2ThreadShell>;
@@ -227,10 +226,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
227226
listActiveSubagentThreadIds: unused,
228227
listAppOwnedChildThreadIds: unused,
229228
countThreads: unused,
230-
listActiveShellIndex: () =>
231-
Effect.succeed(
232-
(options.startupThreads ?? []).map((thread) => ({ ...thread, mayBeLive: true })),
233-
),
229+
listActiveShellIndex: () => Effect.succeed([]),
234230
listOlderSettledThreads: unused,
235231
readShellSnapshot: unused,
236232
getThreadTitles: unused,
@@ -335,64 +331,11 @@ const makeTestRelay = Effect.fnUntraced(function* (
335331
});
336332

337333
describe("AgentAwarenessRelay", () => {
338-
it.effect("publishes a startup approval through a side chat", () =>
339-
Effect.gen(function* () {
340-
const parent = shell({ status: "idle" });
341-
const intermediate = shell({
342-
id: SECOND_THREAD_ID,
343-
lineage: {
344-
rootThreadId: THREAD_ID,
345-
parentThreadId: THREAD_ID,
346-
relationshipToParent: null,
347-
sideChat: true,
348-
},
349-
});
350-
const child = waitingChild({
351-
lineage: {
352-
rootThreadId: THREAD_ID,
353-
parentThreadId: intermediate.id,
354-
relationshipToParent: "subagent",
355-
},
356-
});
357-
const startupThreads = [parent, intermediate, child];
358-
const { relay, publications } = yield* makeTestRelay({
359-
startupThreads,
360-
readShell: (id) =>
361-
Effect.succeed(startupThreads.find((thread) => thread.id === id) ?? null),
362-
});
363-
yield* relay.start();
364-
yield* TestClock.adjust("1 second");
365-
yield* relay.drain;
366-
assert.equal(publications.length, 1);
367-
assert.equal(publications[0]?.state?.threadId, THREAD_ID);
368-
assert.equal(publications[0]?.state?.phase, "waiting_for_approval");
369-
}).pipe(Effect.scoped),
370-
);
371-
372-
it.effect("leaves coordinator-handled child questions out of phone activity", () =>
373-
Effect.gen(function* () {
374-
const child = waitingChild({
375-
pendingRuntimeRequest: {
376-
id: RuntimeRequestId.make("child-question"),
377-
kind: "user_input",
378-
createdAt: DateTime.makeUnsafe(NOW),
379-
},
380-
});
381-
const { relay, publications } = yield* makeTestRelay({
382-
readShell: (id) => Effect.succeed(id === child.id ? child : shell()),
383-
});
384-
yield* relay.publishThread(THREAD_ID);
385-
yield* relay.publishThread(CHILD_THREAD_ID);
386-
assert.equal(publications.length, 1);
387-
assert.equal(publications[0]?.state?.phase, "running");
388-
}),
389-
);
390-
391334
it.effect("publishes the parent as needing approval while its subagent waits", () =>
392335
Effect.gen(function* () {
393336
const shells = yield* Ref.make(
394337
new Map<ThreadId, OrchestrationV2ThreadShell>([
395-
[THREAD_ID, shell({ status: "completed" })],
338+
[THREAD_ID, shell({ status: "running" })],
396339
[CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })],
397340
]),
398341
);
@@ -402,15 +345,13 @@ describe("AgentAwarenessRelay", () => {
402345
readShell: (threadId) =>
403346
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
404347
});
405-
yield* setShell(shell({ status: "running" }));
406348
yield* relay.publishThread(THREAD_ID);
407349
yield* setShell(shell({ status: "completed" }));
408350
yield* setShell(waitingChild());
409351
yield* relay.publishThread(CHILD_THREAD_ID);
410352
assert.equal(publications.length, 2);
411353
assert.isTrue(publications[1]?.url.includes(`/threads/${THREAD_ID}/`) ?? false);
412354
assert.equal(publications[1]?.state?.phase, "waiting_for_approval");
413-
assert.equal(publications[1]?.state?.threadId, THREAD_ID);
414355

415356
// A rename keeps the same request's alert time, so the phone doesn't re-alert.
416357
yield* setShell(
@@ -425,251 +366,51 @@ describe("AgentAwarenessRelay", () => {
425366
assert.equal(publications[2]?.state?.threadTitle, "Renamed thread");
426367
assert.equal(publications[2]?.state?.updatedAt, publications[1]?.state?.updatedAt);
427368

428-
// With two pending requests, answering one shows the next, possibly an
429-
// older one, with no clear between.
430-
yield* setShell(
431-
waitingChild({
432-
updatedAt: DateTime.makeUnsafe("2026-09-04T12:06:00.000Z"),
433-
pendingRuntimeRequest: {
434-
id: RuntimeRequestId.make("child-request-2"),
435-
kind: "command",
436-
createdAt: DateTime.makeUnsafe("2026-09-04T11:59:00.000Z"),
437-
},
438-
}),
439-
);
440-
yield* relay.publishThread(CHILD_THREAD_ID);
441-
assert.equal(publications.length, 4);
442-
assert.equal(publications[3]?.state?.phase, "waiting_for_approval");
443-
assert.equal(publications[3]?.state?.updatedAt, "2026-09-04T12:06:00.000Z");
444-
445369
yield* setShell(waitingChild({ pendingRuntimeRequest: null }));
446370
yield* relay.publishThread(CHILD_THREAD_ID);
447-
assert.equal(publications.length, 4);
448371
yield* TestClock.adjust("5 seconds");
449372
yield* relay.drain;
450-
assert.equal(publications.length, 5);
451-
assert.equal(publications[4]?.state?.phase, "completed");
373+
assert.equal(publications.length, 4);
374+
assert.equal(publications[3]?.state?.phase, "completed");
452375
}),
453376
);
454377

455-
it.effect(
456-
"announces an overlapping child approval from the same millisecond under a newer parent",
457-
() =>
458-
Effect.gen(function* () {
459-
const secondChildId = ThreadId.make("relay-second-child-thread");
460-
const shells = yield* Ref.make(
461-
new Map<ThreadId, OrchestrationV2ThreadShell>([
462-
[THREAD_ID, shell({ updatedAt: DateTime.makeUnsafe("2026-09-04T12:10:00.000Z") })],
463-
[CHILD_THREAD_ID, waitingChild()],
464-
[secondChildId, waitingChild({ id: secondChildId, pendingRuntimeRequest: null })],
465-
]),
466-
);
467-
const { relay, publications } = yield* makeTestRelay({
468-
readShell: (threadId) =>
469-
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
470-
respond: (attempt) =>
471-
attempt === 2
472-
? new Response("unavailable", { status: 503 })
473-
: Response.json({ ok: true, deliveries: [] }),
474-
});
475-
yield* relay.publishThread(CHILD_THREAD_ID);
476-
assert.equal(publications[0]?.state?.phase, "waiting_for_approval");
477-
478-
yield* Ref.update(shells, (current) =>
479-
new Map(current).set(
480-
secondChildId,
481-
waitingChild({
482-
id: secondChildId,
483-
pendingRuntimeRequest: {
484-
id: RuntimeRequestId.make("second-child-request"),
485-
kind: "command",
486-
createdAt: DateTime.makeUnsafe(NOW),
487-
},
488-
}),
489-
),
490-
);
491-
yield* relay.publishThread(secondChildId);
492-
assert.equal(publications.length, 2);
493-
assert.equal(publications[1]?.state?.updatedAt, "2026-09-04T12:10:00.001Z");
494-
assert.notEqual(publications[1]?.state?.updatedAt, publications[0]?.state?.updatedAt);
495-
496-
yield* TestClock.adjust("1 second");
497-
yield* relay.drain;
498-
assert.equal(publications.length, 3);
499-
assert.deepStrictEqual(publications[2]?.state, publications[1]?.state);
500-
yield* relay.publishThread(secondChildId);
501-
assert.equal(publications.length, 3);
502-
}),
503-
);
504-
505-
it.effect("gives a child's next wait a new alert time after the parent republishes", () =>
378+
it.effect("keeps a child's alert time when the parent's own request comes and goes", () =>
506379
Effect.gen(function* () {
507380
const shells = yield* Ref.make(
508381
new Map<ThreadId, OrchestrationV2ThreadShell>([
509-
[THREAD_ID, shell({ updatedAt: DateTime.makeUnsafe("2026-09-04T12:10:00.000Z") })],
510-
[CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })],
382+
[THREAD_ID, shell()],
383+
[CHILD_THREAD_ID, waitingChild()],
511384
]),
512385
);
513-
const setChild = (next: OrchestrationV2ThreadShell) =>
514-
Ref.update(shells, (current) => new Map(current).set(CHILD_THREAD_ID, next));
386+
const setParent = (next: OrchestrationV2ThreadShell) =>
387+
Ref.update(shells, (current) => new Map(current).set(THREAD_ID, next));
515388
const { relay, publications } = yield* makeTestRelay({
516389
readShell: (threadId) =>
517390
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
518391
});
519-
yield* relay.publishThread(THREAD_ID);
520-
yield* setChild(waitingChild());
521-
yield* relay.publishThread(CHILD_THREAD_ID);
522-
yield* setChild(waitingChild({ pendingRuntimeRequest: null }));
523392
yield* relay.publishThread(CHILD_THREAD_ID);
524-
yield* setChild(
525-
waitingChild({
393+
yield* setParent(
394+
shell({
395+
updatedAt: DateTime.makeUnsafe("2026-09-04T12:01:00.000Z"),
526396
pendingRuntimeRequest: {
527-
id: RuntimeRequestId.make("child-request-2"),
397+
id: RuntimeRequestId.make("parent-request"),
528398
kind: "command",
529-
createdAt: DateTime.makeUnsafe(NOW),
399+
createdAt: DateTime.makeUnsafe("2026-09-04T12:01:00.000Z"),
530400
},
531401
}),
532402
);
533-
yield* relay.publishThread(CHILD_THREAD_ID);
403+
yield* relay.publishThread(THREAD_ID);
404+
yield* setParent(shell({ updatedAt: DateTime.makeUnsafe("2026-09-04T12:02:00.000Z") }));
405+
yield* relay.publishThread(THREAD_ID);
534406
assert.deepStrictEqual(
535407
publications.map(({ state }) => state?.phase),
536-
["running", "waiting_for_approval", "running", "waiting_for_approval"],
408+
["waiting_for_approval", "waiting_for_approval", "waiting_for_approval"],
537409
);
538-
assert.notEqual(publications[3]?.state?.updatedAt, publications[1]?.state?.updatedAt);
410+
assert.equal(publications[2]?.state?.updatedAt, publications[0]?.state?.updatedAt);
539411
}),
540412
);
541413

542-
it.effect(
543-
"does not republish a parent waiting on its own request when a child's comes and goes",
544-
() =>
545-
Effect.gen(function* () {
546-
const parentWaiting = shell({
547-
pendingRuntimeRequest: {
548-
id: RuntimeRequestId.make("parent-request"),
549-
kind: "command",
550-
createdAt: DateTime.makeUnsafe(NOW),
551-
},
552-
});
553-
const shells = yield* Ref.make(
554-
new Map<ThreadId, OrchestrationV2ThreadShell>([
555-
[THREAD_ID, parentWaiting],
556-
[CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })],
557-
]),
558-
);
559-
const { relay, publications } = yield* makeTestRelay({
560-
readShell: (threadId) =>
561-
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
562-
});
563-
yield* relay.publishThread(THREAD_ID);
564-
assert.equal(publications[0]?.state?.phase, "waiting_for_approval");
565-
566-
yield* Ref.update(shells, (current) =>
567-
new Map(current).set(CHILD_THREAD_ID, waitingChild()),
568-
);
569-
yield* relay.publishThread(CHILD_THREAD_ID);
570-
yield* Ref.update(shells, (current) =>
571-
new Map(current).set(CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })),
572-
);
573-
yield* relay.publishThread(CHILD_THREAD_ID);
574-
assert.equal(publications.length, 1);
575-
576-
// The parent's own request coming and going doesn't re-alert the child's.
577-
const setParent = (next: OrchestrationV2ThreadShell) =>
578-
Ref.update(shells, (current) => new Map(current).set(THREAD_ID, next));
579-
yield* Ref.update(shells, (current) =>
580-
new Map(current).set(CHILD_THREAD_ID, waitingChild()),
581-
);
582-
yield* relay.publishThread(CHILD_THREAD_ID);
583-
yield* setParent(shell());
584-
yield* relay.publishThread(THREAD_ID);
585-
yield* setParent(parentWaiting);
586-
yield* relay.publishThread(THREAD_ID);
587-
yield* setParent(shell());
588-
yield* relay.publishThread(THREAD_ID);
589-
assert.equal(publications.length, 4);
590-
assert.equal(publications[1]?.state?.phase, "waiting_for_approval");
591-
assert.equal(publications[3]?.state?.updatedAt, publications[1]?.state?.updatedAt);
592-
}),
593-
);
594-
595-
it.effect(
596-
"retries a parent publish for a new subagent request after earlier retries ran out",
597-
() =>
598-
Effect.gen(function* () {
599-
const shells = yield* Ref.make(
600-
new Map<ThreadId, OrchestrationV2ThreadShell>([
601-
[THREAD_ID, shell()],
602-
[CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })],
603-
]),
604-
);
605-
const { relay, publications } = yield* makeTestRelay({
606-
readShell: (threadId) =>
607-
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
608-
respond: (attempt) =>
609-
attempt <= 7
610-
? new Response("unavailable", { status: 503 })
611-
: Response.json({ ok: true, deliveries: [] }),
612-
});
613-
yield* relay.publishThread(THREAD_ID);
614-
for (const delay of [
615-
"1 second",
616-
"2 seconds",
617-
"4 seconds",
618-
"8 seconds",
619-
"16 seconds",
620-
] as const) {
621-
yield* TestClock.adjust(delay);
622-
yield* relay.drain;
623-
}
624-
assert.equal(publications.length, 6);
625-
626-
yield* Ref.update(shells, (current) =>
627-
new Map(current).set(CHILD_THREAD_ID, waitingChild()),
628-
);
629-
yield* relay.publishThread(CHILD_THREAD_ID);
630-
assert.equal(publications.length, 7);
631-
yield* TestClock.adjust("1 second");
632-
yield* relay.drain;
633-
assert.equal(publications.length, 8);
634-
assert.equal(publications[7]?.state?.phase, "waiting_for_approval");
635-
}),
636-
);
637-
638-
it.effect("tracks subagent requests that start and clear while publishing is off", () =>
639-
Effect.gen(function* () {
640-
const shells = yield* Ref.make(
641-
new Map<ThreadId, OrchestrationV2ThreadShell>([
642-
[THREAD_ID, shell()],
643-
[CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })],
644-
]),
645-
);
646-
const { relay, secrets, publications, catchUp } = yield* makeTestRelay({
647-
readShell: (threadId) =>
648-
Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)),
649-
});
650-
yield* relay.start();
651-
yield* TestClock.adjust("1 second");
652-
assert.equal(catchUp.activitySnapshotReads, 1);
653-
assert.equal(publications.length, 0);
654-
655-
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("false"));
656-
yield* Ref.update(shells, (current) => new Map(current).set(CHILD_THREAD_ID, waitingChild()));
657-
yield* relay.publishThread(CHILD_THREAD_ID);
658-
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true"));
659-
yield* relay.publishThread(THREAD_ID);
660-
assert.equal(publications[0]?.state?.phase, "waiting_for_approval");
661-
662-
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("false"));
663-
yield* Ref.update(shells, (current) =>
664-
new Map(current).set(CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })),
665-
);
666-
yield* relay.publishThread(CHILD_THREAD_ID);
667-
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true"));
668-
yield* relay.publishThread(THREAD_ID);
669-
assert.equal(publications[1]?.state?.phase, "running");
670-
}).pipe(Effect.scoped),
671-
);
672-
673414
it.effect("coalesces queued updates and reruns a thread dirtied during publishing", () =>
674415
Effect.gen(function* () {
675416
const started = yield* Deferred.make<void>();

0 commit comments

Comments
 (0)