Skip to content

Commit 0a84ceb

Browse files
committed
Replay crew-bound start receipts across the staffing upgrade
1 parent a4fcedc commit 0a84ceb

2 files changed

Lines changed: 37 additions & 19 deletions

File tree

‎apps/server/src/j5/playbooks/PlaybookStore.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,27 @@ it.effect("pages beyond 100 runs without losing older active owners", () =>
275275
}).pipe(Effect.scoped, Effect.provide(MemoryLayer)),
276276
);
277277

278+
it.effect("replays a pre-upgrade crew receipt only for the original start", () =>
279+
Effect.gen(function* () {
280+
const { store, workspaceRoot } = yield* makeFixture;
281+
const sql = yield* SqlClient.SqlClient;
282+
const first = yield* store.start(owner, workspaceRoot, "demo", "legacy");
283+
const receipt = JSON.stringify(["start", first.definitionPath, "old-crew"]);
284+
yield* sql`UPDATE j5_playbook_request SET request_json = ${receipt} WHERE run_id = ${first.runId}`;
285+
const retry = yield* store.start(owner, workspaceRoot, "demo.yaml", "legacy");
286+
assert.isTrue(retry.replayed);
287+
assert.equal(retry.runId, first.runId);
288+
assert.equal(
289+
(yield* Effect.flip(store.start(owner, workspaceRoot, "demo", "legacy", "peer"))).code,
290+
"request_conflict",
291+
);
292+
assert.equal(
293+
(yield* Effect.flip(store.start(owner, workspaceRoot, "other", "legacy"))).code,
294+
"request_conflict",
295+
);
296+
}).pipe(Effect.scoped, Effect.provide(MemoryLayer)),
297+
);
298+
278299
it.effect("retains sequential runs and permits a new run after completion or cancellation", () =>
279300
Effect.gen(function* () {
280301
const { store, workspaceRoot } = yield* makeFixture;

‎apps/server/src/j5/playbooks/PlaybookStore.ts‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,6 @@ export const makePlaybookStore = Effect.gen(function* () {
554554
workspaceRoot: string,
555555
name: string,
556556
key: string,
557-
crewInstanceId?: string,
558557
) {
559558
const rows = yield* sql<{
560559
request_json: string;
@@ -565,16 +564,19 @@ export const makePlaybookStore = Effect.gen(function* () {
565564
if (
566565
request[0] !== "start" ||
567566
!recordedPath ||
568-
path.basename(recordedPath, ".yaml") !== name.replace(/\.yaml$/, "") ||
569-
(request[2] ?? null) !== (crewInstanceId ?? null)
567+
path.basename(recordedPath, ".yaml") !== name.replace(/\.yaml$/, "")
570568
)
571569
return yield* playbookError(
572570
"request_conflict",
573571
"This client_request_id belongs to a different operation. Use a new key for a new operation.",
574572
);
575-
return { definitionPath: recordedPath };
573+
// Before runtime assignees, starts could include the originating Crew ID.
574+
// Preserve that legacy shape only for retries of the original unassigned start.
575+
const legacyRequest =
576+
request.length === 3 && typeof request[2] === "string" ? rows[0].request_json : undefined;
577+
return { definitionPath: recordedPath, legacyRequest };
576578
}
577-
return yield* definitionPathFor(workspaceRoot, name);
579+
return { ...(yield* definitionPathFor(workspaceRoot, name)), legacyRequest: undefined };
578580
}, Effect.mapError(storageError));
579581

580582
/** Reads a live definition without touching run state: no permit, no SQL, no revision bump. */
@@ -688,12 +690,16 @@ export const makePlaybookStore = Effect.gen(function* () {
688690
key: string,
689691
assignee?: string,
690692
) {
691-
const { definitionPath } = yield* startDefinitionPathFor(owner, workspaceRoot, name, key);
692-
const request = encodeRequest(
693-
assignee === undefined
694-
? ["start", definitionPath]
695-
: ["start", definitionPath, null, assignee],
693+
const { definitionPath, legacyRequest } = yield* startDefinitionPathFor(
694+
owner,
695+
workspaceRoot,
696+
name,
697+
key,
696698
);
699+
const request =
700+
assignee === undefined
701+
? (legacyRequest ?? encodeRequest(["start", definitionPath]))
702+
: encodeRequest(["start", definitionPath, null, assignee]);
697703
return yield* Effect.gen(function* () {
698704
yield* cancelOrphans();
699705
const previous = yield* replay(owner, key, request);
@@ -1077,14 +1083,6 @@ export const makePlaybookStore = Effect.gen(function* () {
10771083
permit.withPermits(1),
10781084
);
10791085
}, Effect.mapError(storageError));
1080-
/** A Crew's active run with its saved progress, or null. */
1081-
const activeRunForCrew = Effect.fn("PlaybookStore.activeRunForCrew")(function* (
1082-
crewInstanceId: string,
1083-
) {
1084-
const rows = yield* sql<RunRow>`SELECT * FROM j5_playbook_run
1085-
WHERE crew_instance_id = ${crewInstanceId} AND status = 'active' LIMIT 1`;
1086-
return rows[0] ? yield* view(fromRow(rows[0])) : null;
1087-
}, Effect.mapError(storageError));
10881086
const activeRunSummaries = Effect.fn("PlaybookStore.activeRunSummaries")(function* (
10891087
ownerThreadIds: ReadonlyArray<ThreadId>,
10901088
) {
@@ -1156,7 +1154,6 @@ export const makePlaybookStore = Effect.gen(function* () {
11561154
pendingLandings,
11571155
resolveLanding,
11581156
cancelForCrew,
1159-
activeRunForCrew,
11601157
activeRunSummaries,
11611158
};
11621159
});

0 commit comments

Comments
 (0)