Skip to content

Commit 63ccd23

Browse files
Merge branch 'main' into feat/editorconfig-whitespace
2 parents ab4760e + 253dea3 commit 63ccd23

12 files changed

Lines changed: 404 additions & 279 deletions

‎apps/desktop/src/main.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ for (const stream of [process.stdout, process.stderr]) {
88
import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient";
99
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
1010
import * as NodeServices from "@effect/platform-node/NodeServices";
11+
// @effect-diagnostics-next-line nodeBuiltinImport:off - Version output must flush before Electron exits, without acquiring the runtime.
12+
import * as NodeFS from "node:fs";
1113
import * as NodeOS from "node:os";
1214
import * as Effect from "effect/Effect";
1315
import * as Layer from "effect/Layer";
@@ -69,6 +71,15 @@ import * as DesktopWslBackend from "./wsl/DesktopWslBackend.ts";
6971
import * as DesktopWslEnvironment from "./wsl/DesktopWslEnvironment.ts";
7072
import * as DesktopWslServerTree from "./wsl/DesktopWslServerTree.ts";
7173

74+
if (process.argv.includes("--version")) {
75+
try {
76+
NodeFS.writeSync(process.stdout.fd, `${Electron.app.getVersion()}\n`);
77+
} catch (error) {
78+
if (!(error instanceof Error) || !("code" in error) || error.code !== "EPIPE") throw error;
79+
}
80+
Electron.app.exit(0);
81+
}
82+
7283
const desktopEnvironmentLayer = Layer.unwrap(
7384
Effect.gen(function* () {
7485
const metadata = yield* Effect.service(ElectronApp.ElectronApp).pipe(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4382,8 +4382,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
43824382
const source = projection.runs.find((run) => run.id === command.restartContinuationOfRunId);
43834383
if (
43844384
!source ||
4385-
(source.status !== "cancelled" &&
4386-
!isRestartNoteSource(source, projection.providerTurns)) ||
4385+
source.status !== "cancelled" ||
4386+
isRestartNoteSource(source, projection.providerTurns) ||
43874387
projection.thread.archivedAt !== null ||
43884388
projection.thread.deletedAt !== null ||
43894389
projection.thread.providerInstanceId !== source.providerInstanceId ||

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

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -149,33 +149,6 @@ function resolveStaleBackgroundItemProviderInstanceId(
149149
return projection.providerThreads[0]?.providerInstanceId ?? projection.thread.providerInstanceId;
150150
}
151151

152-
/**
153-
* Provider threads with background work reconciliation would cancel and
154-
* record for their next turn.
155-
*/
156-
function providerThreadsWithOpenBackgroundWork(
157-
projection: ProjectionStore.ProjectionRuntimeRecoveryState,
158-
): ReadonlySet<ProviderThreadId> {
159-
const ids = new Set<ProviderThreadId>();
160-
for (const item of projection.turnItems ?? []) {
161-
if (
162-
!isBackgroundCapableTurnItemType(item.type) ||
163-
!isNonterminalTurnItemStatus(item.status) ||
164-
isAppOwnedDelegationItem(item)
165-
)
166-
continue;
167-
const providerThreadId =
168-
item.providerThreadId ??
169-
projection.runs.find((run) => run.id === item.runId)?.providerThreadId;
170-
if (providerThreadId != null) ids.add(providerThreadId);
171-
}
172-
for (const thread of projection.providerThreads ?? []) {
173-
if (thread.ownerNodeId === null && providerThreadHasPendingBackgroundTasks(thread))
174-
ids.add(thread.id);
175-
}
176-
return ids;
177-
}
178-
179152
/**
180153
* A provider thread's latest started run: the last turn that provider saw.
181154
* Restart recovery records the thread's cancelled background work on it, and
@@ -668,7 +641,7 @@ export const make = Effect.gen(function* () {
668641
}
669642
const continuationRun =
670643
continueAfterRestart && trigger === "startup"
671-
? restartContinuationRun(projection, new Set(cancelledBackgroundWork.keys()))
644+
? restartContinuationRun(projection)
672645
: undefined;
673646
const effects: Array<EffectOutbox.PendingOrchestrationEffectV2> = continuationRun
674647
? [
@@ -804,12 +777,7 @@ export const make = Effect.gen(function* () {
804777
.continueThreadsAfterServerUpdate
805778
)
806779
return;
807-
// Shutdown reconciliation cancels the background work below, so a
808-
// settled thread's continuation must be captured while it is still open.
809-
const run = restartContinuationRun(
810-
projection,
811-
providerThreadsWithOpenBackgroundWork(projection),
812-
);
780+
const run = restartContinuationRun(projection);
813781
if (!run) return;
814782
const commandId = CommandId.make(`command:restart-prepare:${run.id}`);
815783
yield* eventSink.writeWithEffects({

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

Lines changed: 144 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const driver = ProviderDriverKind.make("codex");
3232
const providerThreadId = ProviderThreadId.make("provider-thread:restart");
3333
const sessionId = ProviderSessionId.make("session:restart");
3434
const attemptId = RunAttemptId.make("attempt:restart");
35+
// "No project" threads belong to the environment's Scratch project.
36+
const scratchProjectId = ProjectId.make("project:scratch");
3537

3638
function makeProjection() {
3739
return {
@@ -155,76 +157,63 @@ it("recovers an admitted continuation after another crash before provider start"
155157
assert.equal(restartContinuationRun(starting)?.id, runId);
156158
});
157159

158-
it("continues a settled root run only when the restart cancelled its background work", () => {
160+
it("does not continue settled root runs with restart-cancelled background work", () => {
159161
const projection = makeProjection();
160162
const settled = {
161163
...projection,
162-
runs: [{ ...projection.runs[0]!, status: "completed" as const }],
164+
runs: [
165+
{
166+
...projection.runs[0]!,
167+
status: "completed" as const,
168+
restartCancelledBackgroundWork: [{ kind: "shell" as const, label: "sleep 25" }],
169+
},
170+
],
163171
providerThreads: [{ ...projection.providerThreads[0]!, status: "idle" as const }],
164172
providerSessions: [{ ...projection.providerSessions[0]!, status: "stopped" as const }],
165173
providerTurns: [],
166174
};
167-
const lostWork = new Set([providerThreadId]);
168-
assert.isUndefined(restartContinuationRun(settled));
169-
assert.equal(restartContinuationRun(settled, lostWork)?.id, runId);
170-
// Work an older provider thread launched (before a provider switch) is not
171-
// this run's: its provider was never told about it and cannot continue it.
172-
assert.isUndefined(
173-
restartContinuationRun(settled, new Set([ProviderThreadId.make("provider-thread:claude")])),
174-
);
175-
for (const invalid of [
176-
{ ...settled, thread: { ...settled.thread, archivedAt: {} } },
177-
{ ...settled, thread: { ...settled.thread, deletedAt: {} } },
178-
{ ...settled, runs: [{ ...settled.runs[0]!, status: "failed" as const }] },
179-
{
180-
...settled,
181-
providerThreads: [{ ...settled.providerThreads[0]!, nativeThreadRef: null }],
182-
},
183-
])
184-
assert.isUndefined(
185-
restartContinuationRun(invalid as OrchestrationV2ThreadProjection, lostWork),
186-
);
175+
for (const projectId of [scratchProjectId, projection.thread.projectId])
176+
for (const status of ["completed", "waiting"] as const)
177+
assert.isUndefined(
178+
restartContinuationRun({
179+
...settled,
180+
thread: { ...settled.thread, projectId },
181+
runs: [{ ...settled.runs[0]!, status }],
182+
}),
183+
);
187184
});
188185

189-
it.effect("prompts a settled thread's continuation with the note of its lost work", () =>
190-
Effect.gen(function* () {
191-
const base = makeProjection();
192-
const work = [{ kind: "shell" as const, label: "sleep 25 && echo DONE" }];
193-
const projection = {
194-
...base,
195-
runs: [{ ...base.runs[0]!, status: "completed", restartCancelledBackgroundWork: work }],
196-
providerTurns: [{ ...base.providerTurns[0]!, status: "completed" }],
197-
} as unknown as OrchestrationV2ThreadProjection;
198-
const commands: Parameters<
199-
ThreadManagementService.ThreadManagementService["Service"]["dispatch"]
200-
>[0][] = [];
201-
yield* continueRestartedRun({ threadId, sourceRunId: runId }).pipe(
202-
Effect.provide(
203-
Layer.merge(
204-
Layer.mock(ThreadManagementService.ThreadManagementService)({
205-
getThreadRecords: () => Effect.succeed(projection),
206-
recoverDelegatedTask: () => Effect.void,
207-
dispatch: (command) => {
208-
commands.push(command);
209-
return Effect.succeed({} as never);
210-
},
211-
}),
212-
ServerSettings.layerTest({ continueThreadsAfterServerUpdate: true }),
186+
it.effect.each(["completed", "waiting", "cancelled"] as const)(
187+
"ignores a pending restart continuation for a %s run whose provider turn settled",
188+
(status) =>
189+
Effect.gen(function* () {
190+
const base = makeProjection();
191+
const work = [{ kind: "shell" as const, label: "sleep 25 && echo DONE" }];
192+
const projection = {
193+
...base,
194+
runs: [{ ...base.runs[0]!, status, restartCancelledBackgroundWork: work }],
195+
providerTurns: [{ ...base.providerTurns[0]!, status: "completed" }],
196+
} as unknown as OrchestrationV2ThreadProjection;
197+
const commands: Parameters<
198+
ThreadManagementService.ThreadManagementService["Service"]["dispatch"]
199+
>[0][] = [];
200+
yield* continueRestartedRun({ threadId, sourceRunId: runId }).pipe(
201+
Effect.provide(
202+
Layer.merge(
203+
Layer.mock(ThreadManagementService.ThreadManagementService)({
204+
getThreadRecords: () => Effect.succeed(projection),
205+
recoverDelegatedTask: () => Effect.void,
206+
dispatch: (command) => {
207+
commands.push(command);
208+
return Effect.succeed({} as never);
209+
},
210+
}),
211+
ServerSettings.layerTest({ continueThreadsAfterServerUpdate: true }),
212+
),
213213
),
214-
),
215-
);
216-
assert.lengthOf(commands, 1);
217-
const command = commands[0]!;
218-
assert.equal(
219-
command.type === "message.dispatch" ? command.restartContinuationOfRunId : null,
220-
runId,
221-
);
222-
assert.include(
223-
command.type === "message.dispatch" ? command.text : "",
224-
"sleep 25 && echo DONE",
225-
);
226-
assert.notInclude(command.type === "message.dispatch" ? command.text : "", "Continue where");
227-
}),
214+
);
215+
assert.lengthOf(commands, 0);
216+
}),
228217
);
229218

230219
it.effect("does not continue a failed run that lost background work", () =>
@@ -419,7 +408,7 @@ it.effect("prepares no continuation for background work another provider thread
419408
);
420409
yield* recovery.prepareForShutdown;
421410
assert.lengthOf(writes, 0);
422-
// The same work on the Codex run's own provider thread is continued.
411+
// Work on the settled run's own provider thread must not wake it either.
423412
const ownWork = {
424413
...projection,
425414
turnItems: [{ ...projection.turnItems[0]!, providerThreadId }],
@@ -446,13 +435,103 @@ it.effect("prepares no continuation for background work another provider thread
446435
),
447436
);
448437
yield* ownRecovery.prepareForShutdown;
449-
assert.deepEqual(
450-
writes.map((write) => write.effects[0]?.request),
451-
[{ type: "provider-runtime.continue", sourceRunId: runId }],
452-
);
438+
assert.lengthOf(writes, 0);
453439
}),
454440
);
455441

442+
it.effect.each([
443+
["completed", scratchProjectId],
444+
["completed", ProjectId.make("restart-project")],
445+
["waiting", scratchProjectId],
446+
["waiting", ProjectId.make("restart-project")],
447+
] as const)(
448+
"cleans up background work without waking a %s run in project %s",
449+
([status, projectId]) =>
450+
Effect.gen(function* () {
451+
const base = makeProjection();
452+
const projection = {
453+
...base,
454+
thread: { ...base.thread, projectId },
455+
runs: [{ ...base.runs[0]!, status }],
456+
providerTurns: [{ ...base.providerTurns[0]!, status: "completed" }],
457+
turnItems: [
458+
{
459+
id: "turn-item:background-subagent",
460+
runId,
461+
nodeId: null,
462+
providerThreadId,
463+
providerInstanceId: instanceId,
464+
type: "subagent",
465+
subagentId: "subagent:background",
466+
title: "Background reviewer",
467+
status: "running",
468+
},
469+
],
470+
subagents: [
471+
{
472+
id: "subagent:background",
473+
runId,
474+
driver,
475+
providerInstanceId: instanceId,
476+
status: "running",
477+
},
478+
],
479+
} as unknown as OrchestrationV2ThreadProjection;
480+
for (const trigger of ["startup", "shutdown"] as const) {
481+
const commits: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0][] = [];
482+
const writes: Parameters<EventSink.EventSinkV2["Service"]["writeWithEffects"]>[0][] = [];
483+
const recovery = yield* ProviderRuntimeRecovery.make.pipe(
484+
Effect.provide(
485+
Layer.mergeAll(
486+
ServerSettings.layerTest({ continueThreadsAfterServerUpdate: true }),
487+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
488+
getRecoveryThreadIds: () => Effect.succeed([threadId]),
489+
getRuntimeRecoveryProjection: () => Effect.succeed(projection),
490+
}),
491+
Layer.mock(EventSink.EventSinkV2)({
492+
writeWithEffects: (input) =>
493+
Effect.sync(() => {
494+
writes.push(input);
495+
return [];
496+
}),
497+
commitCommand: (input) =>
498+
Effect.sync(() => {
499+
commits.push(input);
500+
return { committed: true, cancelledEffectCount: 0 } as never;
501+
}),
502+
}),
503+
IdAllocator.layer,
504+
Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
505+
runRecoveryOnce: Effect.succeed(false),
506+
}),
507+
Layer.mock(EffectOutbox.EffectOutboxV2)({
508+
listByCommandId: () => Effect.succeed([]),
509+
reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
510+
}),
511+
),
512+
),
513+
);
514+
if (trigger === "shutdown") yield* recovery.prepareForShutdown;
515+
yield* recovery.reconcile(trigger);
516+
assert.lengthOf(writes, 0);
517+
assert.lengthOf(commits, 1);
518+
assert.lengthOf(commits[0]!.effects, 0);
519+
const events = commits[0]!.events;
520+
for (const type of ["turn-item.updated", "subagent.updated"] as const)
521+
assert.isTrue(
522+
events.some((event) => event.type === type && event.payload.status === "cancelled"),
523+
);
524+
const note = events.find((event) => event.type === "run.background-work-cancelled");
525+
assert.equal(note?.runId, runId);
526+
assert.deepEqual(note?.payload.restartCancelledBackgroundWork, [
527+
{ kind: "subagent", label: "Background reviewer", id: "turn-item:background-subagent" },
528+
]);
529+
if (status === "completed")
530+
assert.isFalse(events.some((event) => event.type === "run.updated"));
531+
}
532+
}),
533+
);
534+
456535
it.effect("does not cancel or resume a run that completes while shutdown intent commits", () =>
457536
Effect.gen(function* () {
458537
let projection = makeProjection();

0 commit comments

Comments
 (0)