Skip to content

Commit d98d3c8

Browse files
committed
fix(background): preserve acknowledgements and isolate shelf navigation
1 parent 2b10657 commit d98d3c8

8 files changed

Lines changed: 140 additions & 9 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,3 +1794,91 @@ it.effect("background failures stay reported when another automated run starts",
17941794
assert.deepStrictEqual((yield* projections.getThreadShell(threadId))!.background, reported);
17951795
}).pipe(Effect.provide(layerTest)),
17961796
);
1797+
1798+
it.effect(
1799+
"acknowledged background failures stay quiet after delegated delivery metadata changes",
1800+
() =>
1801+
Effect.gen(function* () {
1802+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1803+
const projections = yield* ProjectionStore.ProjectionStoreV2;
1804+
const events = yield* EventSink.EventSinkV2;
1805+
const { threadId } = yield* completeAndSnoozeThread("background-metadata", 30);
1806+
yield* orchestrator.dispatch({
1807+
type: "message.dispatch",
1808+
commandId: CommandId.make("background-metadata:start"),
1809+
threadId,
1810+
messageId: MessageId.make("background-metadata:start"),
1811+
text: "Delegate a check",
1812+
attachments: [],
1813+
dispatchMode: { type: "start_immediately" },
1814+
createdBy: "user",
1815+
creationSource: "web",
1816+
});
1817+
const run = (yield* projections.getThreadProjection(threadId)).runs.at(-1)!;
1818+
yield* orchestrator.dispatch({
1819+
type: "delegated_task.request",
1820+
commandId: CommandId.make("background-metadata:delegate"),
1821+
parentThreadId: threadId,
1822+
parentRunId: run.id,
1823+
parentNodeId: run.rootNodeId!,
1824+
task: "Check",
1825+
modelSelection,
1826+
runtimeMode: "full-access",
1827+
interactionMode: "default",
1828+
createdBy: "agent",
1829+
creationSource: "mcp",
1830+
});
1831+
const task = (yield* projections.getThreadProjection(threadId)).subagents[0]!;
1832+
const now = yield* DateTime.now;
1833+
yield* events.write({
1834+
events: [
1835+
{
1836+
id: EventId.make("background-metadata:failed"),
1837+
type: "run.updated",
1838+
threadId,
1839+
runId: run.id,
1840+
occurredAt: now,
1841+
payload: {
1842+
...run,
1843+
status: "failed",
1844+
completedAt: now,
1845+
delegatedCompletion: {
1846+
disposition: "open",
1847+
nextGeneration: 2,
1848+
delivery: {
1849+
generation: 1,
1850+
messageId: MessageId.make("missing-delivery"),
1851+
taskIds: [task.id],
1852+
},
1853+
},
1854+
},
1855+
},
1856+
],
1857+
});
1858+
yield* orchestrator.dispatch({
1859+
type: "thread.metadata.update",
1860+
commandId: CommandId.make("background-metadata:background"),
1861+
threadId,
1862+
background: true,
1863+
});
1864+
yield* orchestrator.dispatch({
1865+
type: "thread.mark-unread",
1866+
commandId: CommandId.make("background-metadata:report"),
1867+
threadId,
1868+
});
1869+
yield* orchestrator.dispatch({
1870+
type: "thread.metadata.update",
1871+
commandId: CommandId.make("background-metadata:ack"),
1872+
threadId,
1873+
acknowledgeBackground: true,
1874+
});
1875+
const result = yield* orchestrator.dispatch({
1876+
type: "delegated_task.completion-delivery.dispose",
1877+
commandId: CommandId.make("background-metadata:dispose"),
1878+
parentThreadId: threadId,
1879+
taskId: task.id,
1880+
});
1881+
assert.isTrue(result.storedEvents.some((stored) => stored.event.type === "run.updated"));
1882+
assert.isNull((yield* projections.getThreadShell(threadId))!.background!.reportedAt);
1883+
}).pipe(Effect.provide(layerTest)),
1884+
);

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,9 +1437,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14371437
) {
14381438
const reports: Array<OrchestrationV2DomainEvent> = [];
14391439
const reported = new Set<ThreadId>();
1440+
const runStatuses = new Map<RunId, string>();
14401441
for (const event of events) {
1442+
if (event.type === "run.created") {
1443+
runStatuses.set(event.payload.id, event.payload.status);
1444+
continue;
1445+
}
1446+
if (event.type !== "run.updated") continue;
1447+
const previousStatus = runStatuses.get(event.payload.id);
1448+
runStatuses.set(event.payload.id, event.payload.status);
14411449
if (
1442-
event.type !== "run.updated" ||
14431450
!["failed", "interrupted", "cancelled"].includes(event.payload.status) ||
14441451
reported.has(event.threadId) ||
14451452
events.some(
@@ -1449,7 +1456,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14491456
)
14501457
)
14511458
continue;
1452-
reported.add(event.threadId);
14531459
const pending = events.findLast(
14541460
(candidate) => candidate.threadId === event.threadId && "lineage" in candidate.payload,
14551461
);
@@ -1458,6 +1464,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14581464
? pending.payload
14591465
: yield* projectionStore.getThread(event.threadId);
14601466
if (thread.background == null || thread.background.reportedAt != null) continue;
1467+
const priorStatus =
1468+
previousStatus ??
1469+
(yield* projectionStore.getThreadRecords(event.threadId, ["runs"], {
1470+
runIds: [event.payload.id],
1471+
})).runs[0]?.status;
1472+
if (priorStatus === event.payload.status) continue;
1473+
reported.add(event.threadId);
14611474
reports.push({
14621475
...event,
14631476
id: yield* idAllocator.allocate.event({ threadId: event.threadId }),

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,6 +3423,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
34233423
navigationParent={
34243424
folderRow ??
34253425
(section === "working" ||
3426+
section === "background" ||
34263427
section === "snoozed" ||
34273428
section === "settled"
34283429
? `shelf:${section}`

‎apps/web/src/components/sidebar/SidebarListMarkers.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ export function SidebarSectionHeader(props: {
123123
const shelf =
124124
props.marker === "working-header"
125125
? "working"
126-
: props.marker === "snoozed-header"
127-
? "snoozed"
128-
: "settled";
126+
: props.marker === "background-header"
127+
? "background"
128+
: props.marker === "snoozed-header"
129+
? "snoozed"
130+
: "settled";
129131
return (
130132
<SortableSidebarMarker
131133
marker={props.marker}

‎packages/client-runtime/src/state/threadSettled.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean
107107
return true;
108108
}
109109
const report = shell.source?.background?.reportedAt;
110-
if (report != null && (shell.snoozedAt == null || report > shell.snoozedAt)) return true;
110+
if (
111+
report != null &&
112+
(shell.snoozedAt == null || Date.parse(report) > Date.parse(shell.snoozedAt))
113+
)
114+
return true;
111115
if (shell.pendingSubagentRequest != null) return true;
112116
if ((shell.pendingGuestMessageCount ?? 0) > 0) return true;
113117
const failedAt = failureEdgeAt(shell);
@@ -439,10 +443,15 @@ export function isQuietBackgroundThread(
439443
return false;
440444
const run = shell.latestRun;
441445
if (run != null && ["failed", "interrupted", "cancelled"].includes(run.status)) {
442-
return run.completedAt != null && run.completedAt <= policy.acknowledgedAt;
446+
return (
447+
run.completedAt != null && Date.parse(run.completedAt) <= Date.parse(policy.acknowledgedAt)
448+
);
443449
}
444450
if (shell.runtime?.status === "failed" || shell.runtime?.status === "interrupted") {
445-
return shell.runtime.updatedAt != null && shell.runtime.updatedAt <= policy.acknowledgedAt;
451+
return (
452+
shell.runtime.updatedAt != null &&
453+
Date.parse(shell.runtime.updatedAt) <= Date.parse(policy.acknowledgedAt)
454+
);
446455
}
447456
return true;
448457
}

‎packages/client-runtime/src/state/threadSnoozed.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,10 @@ it("a new background report wakes a snooze, while an explicit later snooze defer
514514
};
515515
expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
516516
expect(effectiveSnoozed({ ...shell, snoozedAt: NOW }, { now: NOW })).toBe(true);
517+
expect(effectiveSnoozed({ ...shell, snoozedAt: "2026-04-10T11:00:00Z" }, { now: NOW })).toBe(
518+
true,
519+
);
520+
expect(effectiveSnoozed({ ...shell, snoozedAt: "2026-04-10T12:00:00+02:00" }, { now: NOW })).toBe(
521+
false,
522+
);
517523
});

‎packages/shared/src/agentAwareness.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ describe("projectThreadAwarenessV2", () => {
232232
thread: { ...input.thread, background: { ...background, reportedAt: NOW } },
233233
})?.phase,
234234
).toBe("completed");
235+
expect(
236+
projectThreadAwarenessV2({
237+
...input,
238+
thread: {
239+
...input.thread,
240+
status: "failed",
241+
latestRunCompletedAt: DateTime.makeUnsafe(NOW),
242+
background: { acknowledgedAt: NOW.replace(".000Z", "Z"), reportedAt: null },
243+
},
244+
}),
245+
).toBeNull();
235246
const childUpdatedAt = "2026-09-04T12:02:00.000Z";
236247
expect(
237248
projectThreadAwarenessV2({

‎packages/shared/src/agentAwareness.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export function projectThreadAwarenessV2(
113113
(phase === "completed" ||
114114
(phase === "failed" &&
115115
thread.latestRunCompletedAt != null &&
116-
DateTime.formatIso(thread.latestRunCompletedAt) <= thread.background.acknowledgedAt))
116+
DateTime.toEpochMillis(thread.latestRunCompletedAt) <=
117+
Date.parse(thread.background.acknowledgedAt)))
117118
)
118119
return null;
119120
// Resolve the phase first so live or waiting work stays visible after settlement.

0 commit comments

Comments
 (0)