Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3812,6 +3812,110 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
}),
);

it.effect("flags a stop request only on the latest run, using the interrupt index", () =>
Effect.gen(function* () {
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
const sql = yield* SqlClient.SqlClient;
const suffix = "shell-stop-request";
const threadId = yield* addRolledBackRecoveryCandidate(suffix);
const olderRunId = RunId.make(`run:${suffix}:rolled-back`);
const latestRunId = RunId.make(`run:${suffix}:latest`);
const rootNodeId = NodeId.make(`node:${suffix}:latest`);
const now = yield* DateTime.now;
yield* projectionStore.apply({
id: EventId.make(`event:${suffix}:latest-run`),
type: "run.created",
threadId,
runId: latestRunId,
nodeId: rootNodeId,
driver,
providerInstanceId,
occurredAt: now,
payload: {
id: latestRunId,
threadId,
ordinal: 2,
providerInstanceId,
modelSelection,
providerThreadId: null,
userMessageId: MessageId.make(`message:${suffix}:latest`),
rootNodeId,
activeAttemptId: null,
status: "running",
requestedAt: now,
startedAt: now,
completedAt: null,
checkpointId: null,
contextHandoffId: null,
},
});
const requestStop = (runId: RunId, ordinal: number) =>
projectionStore.apply({
id: EventId.make(`event:${suffix}:stop:${runId}`),
type: "turn-item.updated",
threadId,
runId,
nodeId: rootNodeId,
driver,
occurredAt: now,
payload: {
id: TurnItemId.make(`item:${suffix}:stop:${runId}`),
threadId,
runId,
nodeId: rootNodeId,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal,
status: "completed",
title: "Stop requested",
startedAt: now,
completedAt: now,
updatedAt: now,
type: "run_interrupt_request",
message: "Stopping",
},
});
const expectStopRequested = Effect.fn("expectStopRequested")(function* (expected: boolean) {
const memoryShell = ProjectionStore.threadShellFromProjection(
yield* projectionStore.getThreadProjection(threadId),
);
const sqlShell = (yield* projectionStore.getShellSnapshot()).threads.find(
(thread) => thread.id === threadId,
);
assert.equal(sqlShell?.latestRunId, latestRunId);
assert.equal(memoryShell.latestRunStopRequested, expected);
assert.equal(sqlShell?.latestRunStopRequested, expected);
});

yield* expectStopRequested(false);
yield* requestStop(olderRunId, 2);
yield* expectStopRequested(false);
yield* requestStop(latestRunId, 3);
yield* expectStopRequested(true);

const { statements, tracer } = traceSqlStatements();
yield* projectionStore.getShellSnapshot().pipe(Effect.withTracer(tracer));
const statement = statements.find((query) => query.includes("AS latest_run_stop_requested"));
assert.isDefined(statement);
const stopLookups = (yield* sql.unsafe<{ readonly detail: string }>(
`EXPLAIN QUERY PLAN ${statement}`,
))
.map((row) => row.detail)
.filter((detail) => detail.startsWith("SEARCH stop_request "));
// Latest and blocking runs: each is one probe of the small partial index,
// not a read of every item row in the run.
assert.lengthOf(stopLookups, 2);
for (const lookup of stopLookups) {
assert.include(
lookup,
"COVERING INDEX orchestration_v2_projection_turn_items_run_interrupt_idx (thread_id=? AND run_id=?)",
);
}
}),
);

it.effect("selects the latest waiting secret only from active runs", () =>
Effect.gen(function* () {
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/persistence/Migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import Migration0064 from "./Migrations/064_CanonicalizeThreadPullRequestLinks.t
import Migration0065 from "./Migrations/065_CodexUsageNativeTurnIndex.ts";
import Migration0066 from "./Migrations/066_RepairClientSettlementTimestamps.ts";
import Migration0067 from "./Migrations/067_WebhookDedupe.ts";
import Migration0068 from "./Migrations/068_RunInterruptRequestIndex.ts";

const migrationEntries = [
[1, "OrchestrationEvents", Migration0001],
Expand Down Expand Up @@ -140,6 +141,7 @@ const migrationEntries = [
[65, "CodexUsageNativeTurnIndex", Migration0065],
[66, "RepairClientSettlementTimestamps", Migration0066],
[67, "WebhookDedupe", Migration0067],
[68, "RunInterruptRequestIndex", Migration0068],
] as const;

export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;

// The thread shell checks every thread's latest run for a stop request.
// Without this, that check reads each item row of the run to test its type.
yield* sql`
CREATE INDEX orchestration_v2_projection_turn_items_run_interrupt_idx
ON orchestration_v2_projection_turn_items(thread_id, run_id)
WHERE type = 'run_interrupt_request'
`;
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe("V2 preview upgrade", () => {
[65, "CodexUsageNativeTurnIndex"],
[66, "RepairClientSettlementTimestamps"],
[67, "WebhookDedupe"],
[68, "RunInterruptRequestIndex"],
]);
assert.deepStrictEqual(yield* runMigrations(), []);
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
Expand Down Expand Up @@ -137,6 +138,7 @@ describe("V2 preview upgrade", () => {
[65, "CodexUsageNativeTurnIndex"],
[66, "RepairClientSettlementTimestamps"],
[67, "WebhookDedupe"],
[68, "RunInterruptRequestIndex"],
]);
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
);
Expand Down
Loading