Skip to content

Commit ed6cd61

Browse files
committed
fix(server,web,mobile): idle compaction stays out of thread activity and mark unread
1 parent 09c5999 commit ed6cd61

10 files changed

Lines changed: 173 additions & 16 deletions

File tree

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,16 @@ export function useThreadComposerState() {
368368
const selectedThreadRuntime = useMemo(
369369
() =>
370370
selectedThreadProjection
371-
? deriveThreadRuntime(selectedThreadProjection.projection)
371+
? deriveThreadRuntime(selectedThreadProjection.projection, { includeIdleCompaction: true })
372372
: (selectedThreadShell?.runtime ?? null),
373373
[selectedThreadProjection, selectedThreadShell?.runtime],
374374
);
375375
const selectedThreadActivityRun = useMemo(
376376
() =>
377377
selectedThreadProjection
378-
? deriveThreadActivityRun(selectedThreadProjection.projection)
378+
? deriveThreadActivityRun(selectedThreadProjection.projection, {
379+
includeIdleCompaction: true,
380+
})
379381
: (selectedThreadShell?.latestRun ?? null),
380382
[selectedThreadProjection, selectedThreadShell?.latestRun],
381383
);

‎apps/mobile/src/state/use-thread-selection.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ function firstRouteParam(value: string | string[] | undefined): string | null {
4848
function latestUserMessageAt(
4949
projection: OrchestrationV2ThreadProjection,
5050
): OrchestrationV2ThreadShell["latestUserMessageAt"] {
51+
const idleRunIds = new Set(
52+
projection.runs.filter((run) => run.idleCompactionOfRunId !== undefined).map((run) => run.id),
53+
);
5154
for (let index = projection.messages.length - 1; index >= 0; index -= 1) {
5255
const message = projection.messages[index];
53-
if (message?.role === "user") {
56+
if (message?.role === "user" && (message.runId === null || !idleRunIds.has(message.runId))) {
5457
return message.createdAt;
5558
}
5659
}

‎apps/server/src/orchestration-v2/IdleCompactionService.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ it.effect("keeps the prior activity anchor and hides the system prompt after idl
464464
expect(result.status).toBe("completed");
465465
expect(result.latestRunId).toBe(value.runs[0]!.id);
466466
expect(result.latestRunCompletedAt).toEqual(at());
467+
expect(result.activityRunStatus).toBeNull();
468+
expect(result.activityRunStartedAt).toBeNull();
469+
expect(result.activeRunId).toBeNull();
467470
}
468471
}
469472
}).pipe(Effect.provide(layerProjections)),

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2842,7 +2842,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
28422842
let markUnreadVisitedAt: DateTime.Utc | null = null;
28432843
if (command.type === "thread.mark-unread") {
28442844
const projection = yield* loadProjectionForCommand(command, ["runs"]);
2845-
const latestRunCompletedAt = projection.runs.at(-1)?.completedAt ?? null;
2845+
// Clients compare against the presented run, which never is idle maintenance.
2846+
const latestRunCompletedAt =
2847+
projection.runs.findLast((run) => run.idleCompactionOfRunId === undefined)?.completedAt ??
2848+
null;
28462849
if (latestRunCompletedAt === null) {
28472850
return yield* new OrchestratorDispatchError({
28482851
commandId: command.commandId,

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,6 +2917,57 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
29172917
DateTime.toEpochMillis(now),
29182918
);
29192919
}
2920+
2921+
const maintenance = {
2922+
...run,
2923+
id: RunId.make("run:projection-shell-interruptible:idle-compaction"),
2924+
ordinal: 3,
2925+
idleCompactionOfRunId: runId,
2926+
requestedAt: later,
2927+
startedAt: later,
2928+
};
2929+
yield* projectionStore.apply({
2930+
id: EventId.make("event:projection-shell-interruptible:idle-compaction"),
2931+
type: "run.created",
2932+
threadId,
2933+
occurredAt: later,
2934+
payload: maintenance,
2935+
});
2936+
for (const status of ["running", "waiting", "completed"] as const) {
2937+
yield* projectionStore.apply({
2938+
id: EventId.make(`event:projection-shell-interruptible:maintenance:${status}`),
2939+
type: "run.updated",
2940+
threadId,
2941+
occurredAt: later,
2942+
payload: {
2943+
...wakeProjection.runs[0]!,
2944+
status,
2945+
completedAt: status === "completed" ? later : null,
2946+
},
2947+
});
2948+
const projection = yield* projectionStore.getThreadProjection(threadId);
2949+
const shells = [
2950+
(yield* projectionStore.getShellSnapshot()).threads.find((row) => row.id === threadId)!,
2951+
(yield* projectionStore.getThreadShell(threadId))!,
2952+
ProjectionStore.threadShellFromProjection(projection),
2953+
];
2954+
for (const shell of shells) {
2955+
assert.equal(shell.status, status);
2956+
assert.equal(shell.latestRunId, runId);
2957+
assert.equal(shell.activeRunId, status === "running" ? runId : null);
2958+
assert.equal(shell.activityRunStatus, status === "completed" ? null : status);
2959+
assert.equal(
2960+
shell.activityRunStartedAt == null
2961+
? null
2962+
: DateTime.toEpochMillis(shell.activityRunStartedAt),
2963+
status === "completed" ? null : DateTime.toEpochMillis(now),
2964+
);
2965+
}
2966+
assert.equal(
2967+
projection.runs.find((candidate) => candidate.id === maintenance.id)?.status,
2968+
"running",
2969+
);
2970+
}
29202971
}),
29212972
);
29222973

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,14 +1470,14 @@ export function threadShellFromProjection(
14701470
usageLimitRunPresentedAsLatest(attentionRuns, projection.turnItems, sessionError) ??
14711471
latestUnheldRun(projection.runs);
14721472
const activeRun =
1473-
projection.runs
1473+
attentionRuns
14741474
.filter(isInterruptibleRunForShell)
14751475
.toSorted((left, right) => right.ordinal - left.ordinal)[0] ?? null;
14761476
const activityRun =
1477-
projection.runs
1477+
attentionRuns
14781478
.filter(isActivityRunForShell)
14791479
.toSorted((left, right) => right.ordinal - left.ordinal)[0] ?? null;
1480-
const liveRunIds = new Set(projection.runs.filter(isActivityRunForShell).map((run) => run.id));
1480+
const liveRunIds = new Set(attentionRuns.filter(isActivityRunForShell).map((run) => run.id));
14811481
const pendingRuntimeRequest =
14821482
projection.runtimeRequests
14831483
.filter((request) => request.status === "pending")
@@ -5319,6 +5319,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53195319
FROM orchestration_v2_projection_runs r
53205320
WHERE r.thread_id = t.thread_id
53215321
AND r.status IN ('preparing', 'starting', 'running')
5322+
AND json_extract(r.payload_json, '$.idleCompactionOfRunId') IS NULL
53225323
ORDER BY r.ordinal DESC, r.run_id DESC
53235324
LIMIT 1
53245325
) AS active_run_id,
@@ -5327,6 +5328,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53275328
FROM orchestration_v2_projection_runs r
53285329
WHERE r.thread_id = t.thread_id
53295330
AND r.status IN ('preparing', 'starting', 'running', 'waiting')
5331+
AND json_extract(r.payload_json, '$.idleCompactionOfRunId') IS NULL
53305332
ORDER BY r.ordinal DESC, r.run_id DESC
53315333
LIMIT 1
53325334
) AS activity_run_status,
@@ -5340,6 +5342,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53405342
FROM orchestration_v2_projection_runs r
53415343
WHERE r.thread_id = t.thread_id
53425344
AND r.status IN ('preparing', 'starting', 'running', 'waiting')
5345+
AND json_extract(r.payload_json, '$.idleCompactionOfRunId') IS NULL
53435346
ORDER BY r.ordinal DESC, r.run_id DESC
53445347
LIMIT 1
53455348
) AS activity_run_started_at,
@@ -5404,6 +5407,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
54045407
INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
54055408
WHERE r.thread_id = t.thread_id
54065409
AND r.status IN ('preparing', 'starting', 'running', 'waiting')
5410+
AND json_extract(r.payload_json, '$.idleCompactionOfRunId') IS NULL
54075411
AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id
54085412
AND secret.type = 'secret_request' AND secret.status = 'waiting'
54095413
ORDER BY secret.updated_at DESC, secret.turn_item_id DESC

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,11 +2123,17 @@ export default function ChatView(props: ChatViewProps) {
21232123
[serverProjection],
21242124
);
21252125
const serverActivityRun = useMemo(
2126-
() => (serverProjection === null ? null : deriveThreadActivityRun(serverProjection)),
2126+
() =>
2127+
serverProjection === null
2128+
? null
2129+
: deriveThreadActivityRun(serverProjection, { includeIdleCompaction: true }),
21272130
[serverProjection],
21282131
);
21292132
const serverRuntime = useMemo(
2130-
() => (serverProjection === null ? null : deriveThreadRuntime(serverProjection)),
2133+
() =>
2134+
serverProjection === null
2135+
? null
2136+
: deriveThreadRuntime(serverProjection, { includeIdleCompaction: true }),
21312137
[serverProjection],
21322138
);
21332139
const runlessWorkStartedAt = useMemo(

‎packages/client-runtime/src/operations/commands.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,49 @@ describe("V2 environment commands", () => {
503503
}).pipe(Effect.provide(layerTestCrypto)),
504504
);
505505

506+
it.effect("Stop resolves a running idle compaction from the full projection", () =>
507+
Effect.gen(function* () {
508+
const runId = RunId.make("idle-compaction");
509+
const projection: OrchestrationV2ThreadProjection = {
510+
...v2Projection,
511+
runs: [
512+
{
513+
id: runId,
514+
threadId: v2ThreadId,
515+
ordinal: 2,
516+
providerInstanceId: v2Projection.thread.providerInstanceId,
517+
modelSelection: v2Projection.thread.modelSelection,
518+
providerThreadId: null,
519+
userMessageId: MessageId.make("idle-compaction-prompt"),
520+
rootNodeId: null,
521+
activeAttemptId: null,
522+
status: "running",
523+
idleCompactionOfRunId: RunId.make("completed-run"),
524+
requestedAt: v2Now,
525+
startedAt: v2Now,
526+
completedAt: null,
527+
checkpointId: null,
528+
contextHandoffId: null,
529+
},
530+
],
531+
};
532+
const commands: OrchestrationV2Command[] = [];
533+
const supervisor = yield* makeSupervisor({ commands, projects: [], projection });
534+
yield* interruptThreadTurn({ threadId: v2ThreadId }).pipe(
535+
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
536+
);
537+
expect(commands).toEqual([
538+
{
539+
type: "run.interrupt",
540+
commandId: expect.any(String),
541+
threadId: v2ThreadId,
542+
runId,
543+
holdQueue: true,
544+
},
545+
]);
546+
}).pipe(Effect.provide(layerTestCrypto)),
547+
);
548+
506549
it.effect("Stop with no run left ends the thread's pull request watches", () =>
507550
Effect.gen(function* () {
508551
const link = {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,34 @@ describe("thread execution presentation", () => {
191191
expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true);
192192
});
193193

194+
it.each(["preparing", "starting", "running", "waiting", "completed", "failed"] as const)(
195+
"keeps %s idle compaction out of thread activity while chat can stop it",
196+
(status) => {
197+
const completed = { ...run("completed", 1, "completed"), completedAt: now };
198+
const maintenance = {
199+
...run("maintenance", 2, status),
200+
idleCompactionOfRunId: completed.id,
201+
};
202+
const projection = { ...v2Projection, runs: [completed, maintenance] };
203+
expect(deriveLatestThreadRun(projection)?.runId).toBe(completed.id);
204+
expect(deriveThreadActivityRun(projection)?.runId).toBe(completed.id);
205+
expect(deriveThreadRuntime(projection)).toMatchObject({
206+
status: "completed",
207+
activeRunId: null,
208+
activityStartedAt: null,
209+
});
210+
const chatRuntime = deriveThreadRuntime(projection, { includeIdleCompaction: true });
211+
const active = ["preparing", "starting", "running", "waiting"].includes(status);
212+
expect(deriveThreadActivityRun(projection, { includeIdleCompaction: true })?.runId).toBe(
213+
active ? maintenance.id : completed.id,
214+
);
215+
expect(chatRuntime?.status).toBe(active ? status : "completed");
216+
const interruptible = active && status !== "waiting";
217+
expect(threadRuntimeHasInterruptibleRun(chatRuntime)).toBe(interruptible);
218+
expect(chatRuntime?.activeRunId).toBe(interruptible ? maintenance.id : null);
219+
},
220+
);
221+
194222
it("presents a held queue as the stopped run instead of queued work", () => {
195223
const interrupted = { ...run("run-interrupted", 1, "interrupted"), completedAt: now };
196224
const held = { ...run("run-held", 2, "queued"), queueHeld: true };

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,18 @@ import { formatSubagentDisplayTitle } from "./subagentDisplay.ts";
3535
const ACTIVITY_RUN_STATUSES = new Set(["preparing", "starting", "running", "waiting"]);
3636
const INTERRUPTIBLE_RUN_STATUSES = new Set(["preparing", "starting", "running"]);
3737

38+
type ThreadActivityOptions = { readonly includeIdleCompaction?: boolean };
39+
3840
function latestMatchingRun(
3941
projection: OrchestrationV2ThreadProjection,
4042
predicate: (run: OrchestrationV2ThreadProjection["runs"][number]) => boolean,
43+
{ includeIdleCompaction = false }: ThreadActivityOptions = {},
4144
): OrchestrationV2ThreadProjection["runs"][number] | null {
4245
return projection.runs.reduce<OrchestrationV2ThreadProjection["runs"][number] | null>(
4346
(latest, candidate) =>
44-
predicate(candidate) && (latest === null || candidate.ordinal > latest.ordinal)
47+
(includeIdleCompaction || candidate.idleCompactionOfRunId === undefined) &&
48+
predicate(candidate) &&
49+
(latest === null || candidate.ordinal > latest.ordinal)
4550
? candidate
4651
: latest,
4752
null,
@@ -104,13 +109,18 @@ export function deriveLatestThreadRun(
104109
* Returns the run that owns live provider work, falling back to the newest run
105110
* once the thread is idle. A newer queued run must not make an older executing
106111
* run look settled in clients that render per-run activity.
112+
* Open chat controls opt into idle compaction so it can still be stopped.
107113
*/
108114
export function deriveThreadActivityRun(
109115
projection: OrchestrationV2ThreadProjection,
116+
options: ThreadActivityOptions = {},
110117
): ThreadRunSummary | null {
111118
const run =
112-
latestMatchingRun(projection, (candidate) => ACTIVITY_RUN_STATUSES.has(candidate.status)) ??
113-
presentedLatestRun(projection);
119+
latestMatchingRun(
120+
projection,
121+
(candidate) => ACTIVITY_RUN_STATUSES.has(candidate.status),
122+
options,
123+
) ?? presentedLatestRun(projection);
114124
return run === null ? null : summarizeThreadRun(projection, run);
115125
}
116126

@@ -230,16 +240,19 @@ export function formatProviderSubagentStatus(
230240

231241
export function deriveThreadRuntime(
232242
projection: OrchestrationV2ThreadProjection,
243+
options: ThreadActivityOptions = {},
233244
): ThreadRuntimeSummary | null {
234245
const latestRun = deriveLatestThreadRun(projection);
235246
const providerSession = projection.providerSessions.findLast(
236247
(session) => session.providerInstanceId === projection.thread.providerInstanceId,
237248
);
238249
const usageLimitedRun = presentedUsageLimitRun(projection);
239250
const latestRunProjection = presentedLatestRun(projection);
240-
const activityRun = deriveThreadActivityRun(projection);
241-
const liveActivityRun = latestMatchingRun(projection, (run) =>
242-
ACTIVITY_RUN_STATUSES.has(run.status),
251+
const activityRun = deriveThreadActivityRun(projection, options);
252+
const liveActivityRun = latestMatchingRun(
253+
projection,
254+
(run) => ACTIVITY_RUN_STATUSES.has(run.status),
255+
options,
243256
);
244257
// Same rule as the shell runtime: only background work that holds the
245258
// completion parks the thread at idle; a dev server left running does not.
@@ -262,7 +275,8 @@ export function deriveThreadRuntime(
262275
return null;
263276
}
264277
const activeRunId =
265-
latestMatchingRun(projection, (run) => INTERRUPTIBLE_RUN_STATUSES.has(run.status))?.id ?? null;
278+
latestMatchingRun(projection, (run) => INTERRUPTIBLE_RUN_STATUSES.has(run.status), options)
279+
?.id ?? null;
266280
return {
267281
status: usageLimitedRun
268282
? "failed"

0 commit comments

Comments
 (0)