Skip to content

Commit cc23112

Browse files
Andrii Kucheriavyiclaude
andcommitted
fix(server): a chat is told when its background work stops with its agent's session
A chat that ended its turn to wait on background work was never told when that work stopped in three ways the previous fix (a198fd8) left open. Session release. For every reason but shutdown the session manager cleared the roster and recorded nothing. It now records what the released process ran on the run that last ran on the thread, with a notice, in the roster's own write. An expired idle pin or a failed runtime also queues the wake in that write. An idle close does not wake a run that was itself woken for lost work, so quiet work an agent restarts cannot loop unattended. A release someone asked for records the work without a wake. A turn that needs a new session. The orchestrator points the provider thread at the next session before the old one is detached, so the release never saw that roster and the new process cleared it. The release now asks the runtime it closes whose background work it still runs, and records on the run before the one being started, so that turn's prompt carries the note. The note also follows the native conversation when an account move resumes it under another provider thread. The Claude adapter already refuses to replace a process that runs background work for its own chat. A race. A completed run's stream stopped on the adapter's live state before it ingested the roster update that reports the tasks as lost. For a runtime whose tasks end with its process it now keeps reading while the last roster it stored lists tasks. The wake of a finished /compact run now sends the note instead of being dropped. Claude Fable 5.2, reviewed by Claude Fable 5.2 and GPT-6-Astra, in T3.CodeShip (Claude Code harness). Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 7579eeb commit cc23112

17 files changed

Lines changed: 1052 additions & 306 deletions

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export interface EventSinkV2Shape {
128128
readonly writeIfProviderThreadUnchanged: (input: {
129129
readonly expected: OrchestrationV2ProviderThread;
130130
readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
131+
readonly effects?: ReadonlyArray<EffectOutbox.PendingOrchestrationEffectV2>;
131132
}) => Effect.Effect<
132133
{
133134
readonly committed: boolean;
@@ -609,12 +610,17 @@ const layerBase: Layer.Layer<
609610
const normalized = yield* normalizeEvents(input.events);
610611
const storedEvents = yield* eventStore.append({ events: normalized });
611612
const outcomes = yield* applyStoredEvents(storedEvents);
613+
yield* effectOutbox.enqueue(input.effects ?? []);
612614
return { committed: true as const, storedEvents, outcomes };
613615
}),
614616
(result) =>
615-
result.committed
616-
? publishStoredEvents(result.storedEvents, result.outcomes)
617-
: Effect.void,
617+
Effect.gen(function* () {
618+
if (!result.committed) return;
619+
if (input.effects !== undefined && input.effects.length > 0) {
620+
yield* effectOutbox.notifyAvailable(input.effects.length);
621+
}
622+
yield* publishStoredEvents(result.storedEvents, result.outcomes);
623+
}),
618624
);
619625
});
620626

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
} from "../telemetry/ProviderUsageAnalytics.ts";
3535
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
3636
import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
37+
import * as EffectOutbox from "./EffectOutbox.ts";
3738
import * as EventSink from "./EventSink.ts";
3839
import * as ProjectionStore from "./ProjectionStore.ts";
3940
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
@@ -43,10 +44,9 @@ import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
4344
import { stripUnservedToolOutputImageBytes } from "./toolOutputImageBytes.ts";
4445
import { boundFileChangeForStorage } from "./WireProjection.ts";
4546
import {
46-
backgroundWorkStoppedNotice,
4747
cancelledRosterTaskWork,
4848
isAwaitedWork,
49-
mergeRestartCancelledBackgroundWork,
49+
lostBackgroundWorkRecord,
5050
} from "./RestartBackgroundNote.ts";
5151

5252
export class ProviderEventNormalizeError extends Schema.TaggedError<ProviderEventNormalizeError>()(
@@ -479,8 +479,13 @@ export const layer: Layer.Layer<
479479
});
480480
const run = runs[0];
481481
if (run === undefined || run.providerThreadId !== lost.providerThreadId) return [];
482-
const work = lost.tasks.map(cancelledRosterTaskWork);
483482
const now = yield* DateTime.now;
483+
const record = lostBackgroundWorkRecord({
484+
run,
485+
cause: "process-ended",
486+
work: lost.tasks.map(cancelledRosterTaskWork),
487+
now,
488+
});
484489
return [
485490
yield* makeDomainEvent(input, {
486491
type: "run.background-work-cancelled",
@@ -489,10 +494,7 @@ export const layer: Layer.Layer<
489494
occurredAt: now,
490495
payload: {
491496
runId: run.id,
492-
restartCancelledBackgroundWork: mergeRestartCancelledBackgroundWork(
493-
run.restartCancelledBackgroundWork ?? [],
494-
work,
495-
),
497+
restartCancelledBackgroundWork: record.restartCancelledBackgroundWork,
496498
},
497499
}),
498500
yield* makeDomainEvent(input, {
@@ -501,15 +503,7 @@ export const layer: Layer.Layer<
501503
runId: run.id,
502504
nodeId: run.rootNodeId,
503505
occurredAt: now,
504-
payload: backgroundWorkStoppedNotice({
505-
run,
506-
cause: "process-ended",
507-
work,
508-
stoppedAt: now,
509-
now,
510-
// EventSink places the item after the run's existing items.
511-
ordinal: 0,
512-
}),
506+
payload: record.notice,
513507
}),
514508
];
515509
},
@@ -540,12 +534,11 @@ export const layer: Layer.Layer<
540534
commandId,
541535
events: [],
542536
effects: [
543-
{
544-
id: `effect:restart-continuation:${runId}`,
537+
EffectOutbox.restartContinuationEffect({
545538
commandId,
546539
threadId: recorded.event.threadId,
547-
request: { type: "provider-runtime.continue", sourceRunId: runId },
548-
},
540+
sourceRunId: runId,
541+
}),
549542
],
550543
})
551544
.pipe(

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
21
import {
32
CommandId,
43
type OrchestrationV2DomainEvent,
@@ -43,8 +42,8 @@ import {
4342
cancelledRosterTaskWork,
4443
cancelledTurnItemWork,
4544
isAwaitedWork,
45+
latestStartedRun,
4646
mergeRestartCancelledBackgroundWork,
47-
neverStarted,
4847
} from "./RestartBackgroundNote.ts";
4948

5049
export class ProviderRuntimeRecoveryError extends Schema.TaggedError<ProviderRuntimeRecoveryError>()(
@@ -355,23 +354,6 @@ const cancelledItem = (
355354
}
356355
: { ...item, status: "cancelled", completedAt: stoppedAt, updatedAt: now };
357356

358-
function latestStartedRun(
359-
projection: ProjectionStore.ProjectionRuntimeRecoveryState,
360-
providerThreadId: ProviderThreadId,
361-
) {
362-
return projection.runs.reduce<OrchestrationV2ThreadProjection["runs"][number] | undefined>(
363-
(latest, run) =>
364-
run.providerThreadId === providerThreadId &&
365-
run.status !== "queued" &&
366-
run.status !== "rolled_back" &&
367-
!neverStarted(run) &&
368-
(latest === undefined || runRanAfter(run, latest))
369-
? run
370-
: latest,
371-
undefined,
372-
);
373-
}
374-
375357
export const make = Effect.gen(function* () {
376358
const settings = yield* ServerSettings.ServerSettingsService;
377359
const restartResumeRequest = yield* RestartResumeRequest;
@@ -804,7 +786,7 @@ export const make = Effect.gen(function* () {
804786
});
805787
}
806788
for (const [providerThreadId, entries] of cancelledBackgroundWork) {
807-
const noteRun = latestStartedRun(projection, providerThreadId);
789+
const noteRun = latestStartedRun(projection.runs, providerThreadId);
808790
if (noteRun === undefined) continue;
809791
// Use a separate event; stale run snapshots can overwrite intervening lifecycle changes.
810792
events.push({

0 commit comments

Comments
 (0)