- Project retry progress and recovery for Codex and Claude turns - Sync thread completion markers through server-side visit timestamps
1199 lines
37 KiB
TypeScript
1199 lines
37 KiB
TypeScript
import { assert, it } from "@effect/vitest";
|
|
import {
|
|
EventId,
|
|
MessageId,
|
|
type ModelSelection,
|
|
NodeId,
|
|
ProjectId,
|
|
ProviderDriverKind,
|
|
ProviderInstanceId,
|
|
ProviderSessionId,
|
|
ProviderThreadId,
|
|
ProviderTurnId,
|
|
RunAttemptId,
|
|
RunId,
|
|
ThreadId,
|
|
TurnItemId,
|
|
} from "@t3tools/contracts";
|
|
import * as DateTime from "effect/DateTime";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Schema from "effect/Schema";
|
|
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
|
|
|
import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";
|
|
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
|
|
import {
|
|
isTurnItemAtOrBeforeRun,
|
|
ProjectionStoreV2,
|
|
layer as projectionStoreLayer,
|
|
} from "./ProjectionStore.ts";
|
|
|
|
const TestLayer = Layer.mergeAll(
|
|
projectionStoreLayer.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
|
|
SqlitePersistenceMemory,
|
|
);
|
|
const modelSelection = {
|
|
instanceId: ProviderInstanceId.make("codex"),
|
|
model: "gpt-5.4",
|
|
} satisfies ModelSelection;
|
|
const driver = ProviderDriverKind.make("codex");
|
|
const providerInstanceId = modelSelection.instanceId;
|
|
const encodeUnknownJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
|
|
|
|
it("includes imported runless history when selecting fork context through a run", () => {
|
|
const firstRunId = RunId.make("run:projection-imported-fork:1");
|
|
const secondRunId = RunId.make("run:projection-imported-fork:2");
|
|
const runOrdinalById = new Map([
|
|
[firstRunId, 1],
|
|
[secondRunId, 2],
|
|
]);
|
|
|
|
assert.isTrue(
|
|
isTurnItemAtOrBeforeRun({
|
|
historyOrigin: "v1_import",
|
|
itemRunId: null,
|
|
runOrdinalById,
|
|
sourceRunOrdinal: 1,
|
|
}),
|
|
);
|
|
assert.isFalse(
|
|
isTurnItemAtOrBeforeRun({
|
|
historyOrigin: undefined,
|
|
itemRunId: null,
|
|
runOrdinalById,
|
|
sourceRunOrdinal: 1,
|
|
}),
|
|
);
|
|
assert.isTrue(
|
|
isTurnItemAtOrBeforeRun({
|
|
historyOrigin: "v1_import",
|
|
itemRunId: firstRunId,
|
|
runOrdinalById,
|
|
sourceRunOrdinal: 1,
|
|
}),
|
|
);
|
|
assert.isFalse(
|
|
isTurnItemAtOrBeforeRun({
|
|
historyOrigin: "v1_import",
|
|
itemRunId: secondRunId,
|
|
runOrdinalById,
|
|
sourceRunOrdinal: 1,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it.layer(TestLayer)("ProjectionStoreV2", (it) => {
|
|
it.effect("only exposes interruptible runs through the shell activeRunId", () =>
|
|
Effect.gen(function* () {
|
|
const projectionStore = yield* ProjectionStoreV2;
|
|
const now = yield* DateTime.now;
|
|
const threadId = ThreadId.make("thread:projection-shell-interruptible");
|
|
const projectId = ProjectId.make("project:projection-shell-interruptible");
|
|
const runId = RunId.make("run:projection-shell-interruptible");
|
|
const rootNodeId = NodeId.make("node:projection-shell-interruptible");
|
|
const run = {
|
|
id: runId,
|
|
threadId,
|
|
ordinal: 1,
|
|
providerInstanceId,
|
|
modelSelection,
|
|
providerThreadId: null,
|
|
userMessageId: MessageId.make("message:projection-shell-interruptible"),
|
|
rootNodeId,
|
|
activeAttemptId: null,
|
|
status: "running" as const,
|
|
requestedAt: now,
|
|
startedAt: now,
|
|
completedAt: null,
|
|
checkpointId: null,
|
|
contextHandoffId: null,
|
|
};
|
|
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-shell-interruptible:thread"),
|
|
type: "thread.created",
|
|
threadId,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "user",
|
|
creationSource: "web",
|
|
id: threadId,
|
|
projectId,
|
|
title: "Interruptible shell run",
|
|
providerInstanceId,
|
|
modelSelection,
|
|
runtimeMode: "full-access",
|
|
interactionMode: "default",
|
|
branch: null,
|
|
worktreePath: null,
|
|
activeProviderThreadId: null,
|
|
lineage: {
|
|
parentThreadId: null,
|
|
relationshipToParent: null,
|
|
rootThreadId: threadId,
|
|
},
|
|
forkedFrom: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
archivedAt: null,
|
|
settledOverride: null,
|
|
settledAt: null,
|
|
lastVisitedAt: null,
|
|
deletedAt: null,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-shell-interruptible:running"),
|
|
type: "run.created",
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: run,
|
|
});
|
|
|
|
let shell = (yield* projectionStore.getShellSnapshot()).threads.find(
|
|
(thread) => thread.id === threadId,
|
|
);
|
|
assert.equal(shell?.status, "running");
|
|
assert.equal(shell?.activeRunId, runId);
|
|
assert.equal(
|
|
shell?.latestRunRequestedAt && DateTime.toEpochMillis(shell.latestRunRequestedAt),
|
|
DateTime.toEpochMillis(now),
|
|
);
|
|
assert.equal(
|
|
shell?.latestRunStartedAt && DateTime.toEpochMillis(shell.latestRunStartedAt),
|
|
DateTime.toEpochMillis(now),
|
|
);
|
|
assert.isNull(shell?.latestRunCompletedAt);
|
|
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-shell-interruptible:waiting"),
|
|
type: "run.updated",
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: { ...run, status: "waiting" },
|
|
});
|
|
|
|
shell = (yield* projectionStore.getShellSnapshot()).threads.find(
|
|
(thread) => thread.id === threadId,
|
|
);
|
|
assert.equal(shell?.status, "waiting");
|
|
assert.isNull(shell?.activeRunId);
|
|
}),
|
|
);
|
|
|
|
it.effect("projects one shared provider session into multiple thread bindings", () =>
|
|
Effect.gen(function* () {
|
|
const projectionStore = yield* ProjectionStoreV2;
|
|
const now = yield* DateTime.now;
|
|
const projectId = ProjectId.make("project:projection-shared-provider-session");
|
|
const firstThreadId = ThreadId.make("thread:projection-shared-provider-session:first");
|
|
const secondThreadId = ThreadId.make("thread:projection-shared-provider-session:second");
|
|
const providerSessionId = ProviderSessionId.make(
|
|
"provider-session:projection-shared-provider-session",
|
|
);
|
|
const makeThread = (threadId: ThreadId) => ({
|
|
createdBy: "user" as const,
|
|
creationSource: "web" as const,
|
|
id: threadId,
|
|
projectId,
|
|
title: "Shared provider session",
|
|
providerInstanceId,
|
|
modelSelection,
|
|
runtimeMode: "full-access" as const,
|
|
interactionMode: "default" as const,
|
|
branch: null,
|
|
worktreePath: null,
|
|
activeProviderThreadId: null,
|
|
lineage: {
|
|
parentThreadId: null,
|
|
relationshipToParent: null,
|
|
rootThreadId: threadId,
|
|
},
|
|
forkedFrom: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
archivedAt: null,
|
|
settledOverride: null,
|
|
settledAt: null,
|
|
lastVisitedAt: null,
|
|
deletedAt: null,
|
|
});
|
|
const session = {
|
|
id: providerSessionId,
|
|
driver,
|
|
providerInstanceId,
|
|
status: "error" as const,
|
|
cwd: "/workspace",
|
|
model: modelSelection.model,
|
|
capabilities: CodexProviderCapabilitiesV2,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
lastError: "provider process exited",
|
|
};
|
|
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-shared-provider-session:first-thread"),
|
|
type: "thread.created",
|
|
threadId: firstThreadId,
|
|
occurredAt: now,
|
|
payload: makeThread(firstThreadId),
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-shared-provider-session:second-thread"),
|
|
type: "thread.created",
|
|
threadId: secondThreadId,
|
|
occurredAt: now,
|
|
payload: makeThread(secondThreadId),
|
|
});
|
|
for (const [threadId, suffix] of [
|
|
[firstThreadId, "first"],
|
|
[secondThreadId, "second"],
|
|
] as const) {
|
|
yield* projectionStore.apply({
|
|
id: EventId.make(`event:projection-shared-provider-session:${suffix}-binding`),
|
|
type: "provider-session.attached",
|
|
threadId,
|
|
driver,
|
|
providerInstanceId,
|
|
occurredAt: now,
|
|
payload: session,
|
|
});
|
|
}
|
|
|
|
assert.deepEqual(
|
|
(yield* projectionStore.getThreadProjection(firstThreadId)).providerSessions.map(
|
|
(value) => value.id,
|
|
),
|
|
[providerSessionId],
|
|
);
|
|
assert.deepEqual(
|
|
(yield* projectionStore.getThreadProjection(secondThreadId)).providerSessions.map(
|
|
(value) => value.id,
|
|
),
|
|
[providerSessionId],
|
|
);
|
|
assert.deepEqual(
|
|
(yield* projectionStore.getShellSnapshot()).threads
|
|
.filter((thread) => thread.id === firstThreadId || thread.id === secondThreadId)
|
|
.map((thread) => ({
|
|
id: thread.id,
|
|
lastError: thread.lastError,
|
|
})),
|
|
[
|
|
{ id: firstThreadId, lastError: "provider process exited" },
|
|
{ id: secondThreadId, lastError: "provider process exited" },
|
|
],
|
|
);
|
|
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-shared-provider-session:first-detached"),
|
|
type: "provider-session.detached",
|
|
threadId: firstThreadId,
|
|
driver,
|
|
providerInstanceId,
|
|
occurredAt: now,
|
|
payload: { providerSessionId, detachedAt: now },
|
|
});
|
|
|
|
assert.lengthOf(
|
|
(yield* projectionStore.getThreadProjection(firstThreadId)).providerSessions,
|
|
0,
|
|
);
|
|
assert.lengthOf(
|
|
(yield* projectionStore.getThreadProjection(secondThreadId)).providerSessions,
|
|
1,
|
|
);
|
|
}),
|
|
);
|
|
|
|
it.effect("builds shell snapshots without decoding full turn item payloads", () =>
|
|
Effect.gen(function* () {
|
|
const projectionStore = yield* ProjectionStoreV2;
|
|
const sql = yield* SqlClient.SqlClient;
|
|
const now = yield* DateTime.now;
|
|
const nowIso = DateTime.formatIso(now);
|
|
const threadId = ThreadId.make("thread:projection-shell-stale-item");
|
|
const projectId = ProjectId.make("project:projection-shell");
|
|
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-shell-thread-created"),
|
|
type: "thread.created",
|
|
threadId,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "user",
|
|
creationSource: "web",
|
|
id: threadId,
|
|
projectId,
|
|
title: "Projection shell",
|
|
providerInstanceId,
|
|
modelSelection: modelSelection,
|
|
runtimeMode: "full-access",
|
|
interactionMode: "default",
|
|
branch: null,
|
|
worktreePath: null,
|
|
activeProviderThreadId: null,
|
|
lineage: {
|
|
parentThreadId: null,
|
|
relationshipToParent: null,
|
|
rootThreadId: threadId,
|
|
},
|
|
forkedFrom: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
archivedAt: null,
|
|
settledOverride: null,
|
|
settledAt: null,
|
|
lastVisitedAt: null,
|
|
deletedAt: null,
|
|
},
|
|
});
|
|
|
|
yield* sql`
|
|
INSERT INTO orchestration_v2_projection_turn_items (
|
|
turn_item_id,
|
|
thread_id,
|
|
run_id,
|
|
node_id,
|
|
provider_thread_id,
|
|
provider_turn_id,
|
|
parent_item_id,
|
|
ordinal,
|
|
type,
|
|
status,
|
|
updated_at,
|
|
payload_json
|
|
)
|
|
VALUES (
|
|
${"turn-item:stale-user-message"},
|
|
${threadId},
|
|
${null},
|
|
${null},
|
|
${null},
|
|
${null},
|
|
${null},
|
|
${0},
|
|
${"user_message"},
|
|
${"completed"},
|
|
${nowIso},
|
|
${encodeUnknownJsonString({
|
|
id: "turn-item:stale-user-message",
|
|
threadId,
|
|
runId: null,
|
|
nodeId: null,
|
|
providerThreadId: null,
|
|
providerTurnId: null,
|
|
nativeItemRef: null,
|
|
parentItemId: null,
|
|
ordinal: 0,
|
|
status: "completed",
|
|
title: null,
|
|
startedAt: nowIso,
|
|
completedAt: nowIso,
|
|
updatedAt: nowIso,
|
|
type: "user_message",
|
|
messageId: "message:stale-user-message",
|
|
text: "stale user message",
|
|
attachments: [],
|
|
})}
|
|
)
|
|
`;
|
|
|
|
const shell = yield* projectionStore.getShellSnapshot();
|
|
const fullProjectionExit = yield* Effect.exit(projectionStore.getThreadProjection(threadId));
|
|
|
|
assert.deepEqual(
|
|
shell.threads
|
|
.filter((thread) => thread.id === threadId)
|
|
.map((thread) => ({
|
|
id: thread.id,
|
|
itemCount: thread.itemCount,
|
|
visibleItemCount: thread.visibleItemCount,
|
|
status: thread.status,
|
|
})),
|
|
[
|
|
{
|
|
id: threadId,
|
|
itemCount: 1,
|
|
visibleItemCount: 1,
|
|
status: "idle",
|
|
},
|
|
],
|
|
);
|
|
assert.equal(fullProjectionExit._tag, "Failure");
|
|
}),
|
|
);
|
|
|
|
it.effect("counts imported runless history inherited by fork shells", () =>
|
|
Effect.gen(function* () {
|
|
const projectionStore = yield* ProjectionStoreV2;
|
|
const now = yield* DateTime.now;
|
|
const projectId = ProjectId.make("project:projection-imported-fork-shell");
|
|
const sourceThreadId = ThreadId.make("thread:projection-imported-fork-shell:source");
|
|
const targetThreadId = ThreadId.make("thread:projection-imported-fork-shell:target");
|
|
const sourceRunId = RunId.make("run:projection-imported-fork-shell:source");
|
|
const rootNodeId = NodeId.make("node:projection-imported-fork-shell:source");
|
|
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-imported-fork-shell:source-thread"),
|
|
type: "thread.created",
|
|
threadId: sourceThreadId,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "system",
|
|
creationSource: "server",
|
|
id: sourceThreadId,
|
|
projectId,
|
|
title: "Imported fork source",
|
|
providerInstanceId,
|
|
modelSelection,
|
|
runtimeMode: "full-access",
|
|
interactionMode: "default",
|
|
branch: null,
|
|
worktreePath: null,
|
|
activeProviderThreadId: null,
|
|
historyOrigin: "v1_import",
|
|
lineage: {
|
|
parentThreadId: null,
|
|
relationshipToParent: null,
|
|
rootThreadId: sourceThreadId,
|
|
},
|
|
forkedFrom: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
archivedAt: null,
|
|
settledOverride: null,
|
|
settledAt: null,
|
|
lastVisitedAt: null,
|
|
deletedAt: null,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-imported-fork-shell:target-thread"),
|
|
type: "thread.created",
|
|
threadId: targetThreadId,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "user",
|
|
creationSource: "web",
|
|
id: targetThreadId,
|
|
projectId,
|
|
title: "Imported fork target",
|
|
providerInstanceId,
|
|
modelSelection,
|
|
runtimeMode: "full-access",
|
|
interactionMode: "default",
|
|
branch: null,
|
|
worktreePath: null,
|
|
activeProviderThreadId: null,
|
|
lineage: {
|
|
parentThreadId: sourceThreadId,
|
|
relationshipToParent: "fork",
|
|
rootThreadId: sourceThreadId,
|
|
},
|
|
forkedFrom: {
|
|
type: "run",
|
|
threadId: sourceThreadId,
|
|
runId: sourceRunId,
|
|
},
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
archivedAt: null,
|
|
settledOverride: null,
|
|
settledAt: null,
|
|
lastVisitedAt: null,
|
|
deletedAt: null,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-imported-fork-shell:source-run"),
|
|
type: "run.created",
|
|
threadId: sourceThreadId,
|
|
runId: sourceRunId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: sourceRunId,
|
|
threadId: sourceThreadId,
|
|
ordinal: 1,
|
|
providerInstanceId,
|
|
modelSelection,
|
|
providerThreadId: null,
|
|
userMessageId: MessageId.make("message:projection-imported-fork-shell:run"),
|
|
rootNodeId,
|
|
activeAttemptId: null,
|
|
status: "completed",
|
|
requestedAt: now,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
checkpointId: null,
|
|
contextHandoffId: null,
|
|
},
|
|
});
|
|
|
|
const applyAssistantItem = (suffix: string, runId: RunId | null, ordinal: number) =>
|
|
projectionStore.apply({
|
|
id: EventId.make(`event:projection-imported-fork-shell:item:${suffix}`),
|
|
type: "turn-item.updated",
|
|
threadId: sourceThreadId,
|
|
...(runId === null ? {} : { runId }),
|
|
occurredAt: now,
|
|
payload: {
|
|
id: TurnItemId.make(`turn-item:projection-imported-fork-shell:${suffix}`),
|
|
threadId: sourceThreadId,
|
|
runId,
|
|
nodeId: null,
|
|
providerThreadId: null,
|
|
providerTurnId: null,
|
|
nativeItemRef: null,
|
|
parentItemId: null,
|
|
ordinal,
|
|
status: "completed",
|
|
title: null,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
updatedAt: now,
|
|
type: "assistant_message",
|
|
messageId: MessageId.make(`message:projection-imported-fork-shell:${suffix}`),
|
|
text: suffix,
|
|
streaming: false,
|
|
},
|
|
});
|
|
|
|
yield* applyAssistantItem("imported-one", null, 1);
|
|
yield* applyAssistantItem("imported-two", null, 2);
|
|
yield* applyAssistantItem("native-run", sourceRunId, 3);
|
|
|
|
const shell = yield* projectionStore.getShellSnapshot();
|
|
const targetShell = shell.threads.find((thread) => thread.id === targetThreadId);
|
|
const targetProjection = yield* projectionStore.getThreadProjection(targetThreadId);
|
|
|
|
assert.isDefined(targetShell);
|
|
assert.equal(targetShell.itemCount, 0);
|
|
assert.equal(targetShell.visibleItemCount, 4);
|
|
assert.equal(targetProjection.visibleTurnItems.length, 4);
|
|
}),
|
|
);
|
|
|
|
it.effect("removes rolled back runs from the active visible projection", () =>
|
|
Effect.gen(function* () {
|
|
const projectionStore = yield* ProjectionStoreV2;
|
|
const now = yield* DateTime.now;
|
|
const threadId = ThreadId.make("thread:projection-rollback-prune");
|
|
const projectId = ProjectId.make("project:projection-rollback-prune");
|
|
const runId = RunId.make("run:projection-rollback-prune");
|
|
const attemptId = RunAttemptId.make("attempt:projection-rollback-prune");
|
|
const rootNodeId = NodeId.make("node:projection-rollback-prune:root");
|
|
const assistantNodeId = NodeId.make("node:projection-rollback-prune:assistant");
|
|
const providerThreadId = ProviderThreadId.make("provider-thread:projection-rollback-prune");
|
|
const providerTurnId = ProviderTurnId.make("provider-turn:projection-rollback-prune");
|
|
const userMessageId = MessageId.make("message:projection-rollback-prune:user");
|
|
const assistantMessageId = MessageId.make("message:projection-rollback-prune:assistant");
|
|
const userTurnItemId = TurnItemId.make("turn-item:projection-rollback-prune:user");
|
|
const assistantTurnItemId = TurnItemId.make("turn-item:projection-rollback-prune:assistant");
|
|
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:thread-created"),
|
|
type: "thread.created",
|
|
threadId,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "user",
|
|
creationSource: "web",
|
|
id: threadId,
|
|
projectId,
|
|
title: "Projection rollback prune",
|
|
providerInstanceId,
|
|
modelSelection,
|
|
runtimeMode: "full-access",
|
|
interactionMode: "default",
|
|
branch: null,
|
|
worktreePath: null,
|
|
activeProviderThreadId: providerThreadId,
|
|
lineage: {
|
|
parentThreadId: null,
|
|
relationshipToParent: null,
|
|
rootThreadId: threadId,
|
|
},
|
|
forkedFrom: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
archivedAt: null,
|
|
settledOverride: null,
|
|
settledAt: null,
|
|
lastVisitedAt: null,
|
|
deletedAt: null,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:provider-thread"),
|
|
type: "provider-thread.updated",
|
|
threadId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: providerThreadId,
|
|
driver,
|
|
providerInstanceId,
|
|
providerSessionId: null,
|
|
appThreadId: threadId,
|
|
ownerNodeId: null,
|
|
nativeThreadRef: null,
|
|
nativeConversationHeadRef: null,
|
|
status: "active",
|
|
firstRunOrdinal: 1,
|
|
lastRunOrdinal: 1,
|
|
handoffIds: [],
|
|
forkedFrom: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:run-created"),
|
|
type: "run.created",
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: runId,
|
|
threadId,
|
|
ordinal: 1,
|
|
providerInstanceId,
|
|
modelSelection,
|
|
providerThreadId,
|
|
userMessageId,
|
|
rootNodeId,
|
|
activeAttemptId: attemptId,
|
|
status: "completed",
|
|
requestedAt: now,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
checkpointId: null,
|
|
contextHandoffId: null,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:attempt-created"),
|
|
type: "run-attempt.created",
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: attemptId,
|
|
runId,
|
|
attemptOrdinal: 1,
|
|
rootNodeId,
|
|
providerInstanceId,
|
|
providerThreadId,
|
|
providerTurnId,
|
|
reason: "initial",
|
|
status: "completed",
|
|
startedAt: now,
|
|
completedAt: now,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:root-node"),
|
|
type: "node.updated",
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: rootNodeId,
|
|
threadId,
|
|
runId,
|
|
parentNodeId: null,
|
|
rootNodeId,
|
|
kind: "root_turn",
|
|
status: "completed",
|
|
countsForRun: true,
|
|
providerThreadId,
|
|
providerTurnId: null,
|
|
nativeItemRef: null,
|
|
runtimeRequestId: null,
|
|
checkpointScopeId: null,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:assistant-node"),
|
|
type: "node.updated",
|
|
threadId,
|
|
runId,
|
|
nodeId: assistantNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: assistantNodeId,
|
|
threadId,
|
|
runId,
|
|
parentNodeId: rootNodeId,
|
|
rootNodeId,
|
|
kind: "assistant_message",
|
|
status: "completed",
|
|
countsForRun: false,
|
|
providerThreadId,
|
|
providerTurnId,
|
|
nativeItemRef: null,
|
|
runtimeRequestId: null,
|
|
checkpointScopeId: null,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:provider-turn"),
|
|
type: "provider-turn.updated",
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: providerTurnId,
|
|
providerThreadId,
|
|
nodeId: rootNodeId,
|
|
runAttemptId: attemptId,
|
|
nativeTurnRef: null,
|
|
ordinal: 1,
|
|
status: "completed",
|
|
startedAt: now,
|
|
completedAt: now,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:user-message"),
|
|
type: "message.updated",
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "user",
|
|
creationSource: "web",
|
|
id: userMessageId,
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
role: "user",
|
|
text: "rolled back user",
|
|
attachments: [],
|
|
streaming: false,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:assistant-message"),
|
|
type: "message.updated",
|
|
threadId,
|
|
runId,
|
|
nodeId: assistantNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "agent",
|
|
creationSource: "provider",
|
|
id: assistantMessageId,
|
|
threadId,
|
|
runId,
|
|
nodeId: assistantNodeId,
|
|
role: "assistant",
|
|
text: "rolled back assistant",
|
|
attachments: [],
|
|
streaming: false,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:user-item"),
|
|
type: "turn-item.updated",
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "user",
|
|
creationSource: "web",
|
|
id: userTurnItemId,
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
providerThreadId,
|
|
providerTurnId: null,
|
|
nativeItemRef: null,
|
|
parentItemId: null,
|
|
ordinal: 100,
|
|
status: "completed",
|
|
title: null,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
updatedAt: now,
|
|
type: "user_message",
|
|
messageId: userMessageId,
|
|
inputIntent: "turn_start",
|
|
text: "rolled back user",
|
|
attachments: [],
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:assistant-item"),
|
|
type: "turn-item.updated",
|
|
threadId,
|
|
runId,
|
|
nodeId: assistantNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: assistantTurnItemId,
|
|
threadId,
|
|
runId,
|
|
nodeId: assistantNodeId,
|
|
providerThreadId,
|
|
providerTurnId,
|
|
nativeItemRef: null,
|
|
parentItemId: null,
|
|
ordinal: 101,
|
|
status: "completed",
|
|
title: null,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
updatedAt: now,
|
|
type: "assistant_message",
|
|
messageId: assistantMessageId,
|
|
text: "rolled back assistant",
|
|
streaming: false,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:run-rolled-back"),
|
|
type: "run.updated",
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: runId,
|
|
threadId,
|
|
ordinal: 1,
|
|
providerInstanceId,
|
|
modelSelection,
|
|
providerThreadId,
|
|
userMessageId,
|
|
rootNodeId,
|
|
activeAttemptId: attemptId,
|
|
status: "rolled_back",
|
|
requestedAt: now,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
checkpointId: null,
|
|
contextHandoffId: null,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-rollback-prune:root-rolled-back"),
|
|
type: "node.updated",
|
|
threadId,
|
|
runId,
|
|
nodeId: rootNodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: rootNodeId,
|
|
threadId,
|
|
runId,
|
|
parentNodeId: null,
|
|
rootNodeId,
|
|
kind: "root_turn",
|
|
status: "rolled_back",
|
|
countsForRun: true,
|
|
providerThreadId,
|
|
providerTurnId: null,
|
|
nativeItemRef: null,
|
|
runtimeRequestId: null,
|
|
checkpointScopeId: null,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
},
|
|
});
|
|
|
|
const projection = yield* projectionStore.getThreadProjection(threadId);
|
|
|
|
assert.deepEqual(
|
|
projection.runs.map((run) => run.status),
|
|
["rolled_back"],
|
|
);
|
|
assert.deepEqual(
|
|
projection.nodes.map((node) => [node.id, node.status]),
|
|
[
|
|
[assistantNodeId, "completed"],
|
|
[rootNodeId, "rolled_back"],
|
|
],
|
|
);
|
|
assert.lengthOf(projection.providerTurns, 1);
|
|
assert.lengthOf(projection.messages, 2);
|
|
assert.lengthOf(projection.turnItems, 2);
|
|
assert.lengthOf(projection.visibleTurnItems, 0);
|
|
}),
|
|
);
|
|
|
|
it.effect("keeps fork visible items stable after a source run is rolled back", () =>
|
|
Effect.gen(function* () {
|
|
const projectionStore = yield* ProjectionStoreV2;
|
|
const now = yield* DateTime.now;
|
|
const projectId = ProjectId.make("project:projection-fork-source-rollback");
|
|
const sourceThreadId = ThreadId.make("thread:projection-fork-source-rollback:source");
|
|
const targetThreadId = ThreadId.make("thread:projection-fork-source-rollback:target");
|
|
const sourceProviderThreadId = ProviderThreadId.make(
|
|
"provider-thread:projection-fork-source-rollback:source",
|
|
);
|
|
const targetProviderThreadId = ProviderThreadId.make(
|
|
"provider-thread:projection-fork-source-rollback:target",
|
|
);
|
|
const sourceRun1Id = RunId.make("run:projection-fork-source-rollback:source:1");
|
|
const sourceRun2Id = RunId.make("run:projection-fork-source-rollback:source:2");
|
|
const sourceRun1NodeId = NodeId.make("node:projection-fork-source-rollback:source:1");
|
|
const sourceRun2NodeId = NodeId.make("node:projection-fork-source-rollback:source:2");
|
|
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-fork-source-rollback:source-thread"),
|
|
type: "thread.created",
|
|
threadId: sourceThreadId,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "user",
|
|
creationSource: "web",
|
|
id: sourceThreadId,
|
|
projectId,
|
|
title: "Projection fork source rollback source",
|
|
providerInstanceId,
|
|
modelSelection,
|
|
runtimeMode: "full-access",
|
|
interactionMode: "default",
|
|
branch: null,
|
|
worktreePath: null,
|
|
activeProviderThreadId: sourceProviderThreadId,
|
|
lineage: {
|
|
parentThreadId: null,
|
|
relationshipToParent: null,
|
|
rootThreadId: sourceThreadId,
|
|
},
|
|
forkedFrom: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
archivedAt: null,
|
|
settledOverride: null,
|
|
settledAt: null,
|
|
lastVisitedAt: null,
|
|
deletedAt: null,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-fork-source-rollback:target-thread"),
|
|
type: "thread.created",
|
|
threadId: targetThreadId,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "user",
|
|
creationSource: "web",
|
|
id: targetThreadId,
|
|
projectId,
|
|
title: "Projection fork source rollback target",
|
|
providerInstanceId,
|
|
modelSelection,
|
|
runtimeMode: "full-access",
|
|
interactionMode: "default",
|
|
branch: null,
|
|
worktreePath: null,
|
|
activeProviderThreadId: targetProviderThreadId,
|
|
lineage: {
|
|
parentThreadId: sourceThreadId,
|
|
relationshipToParent: "fork",
|
|
rootThreadId: sourceThreadId,
|
|
},
|
|
forkedFrom: {
|
|
type: "run",
|
|
threadId: sourceThreadId,
|
|
runId: sourceRun2Id,
|
|
},
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
archivedAt: null,
|
|
settledOverride: null,
|
|
settledAt: null,
|
|
lastVisitedAt: null,
|
|
deletedAt: null,
|
|
},
|
|
});
|
|
|
|
for (const [ordinal, runId, nodeId, promptText, responseText] of [
|
|
[1, sourceRun1Id, sourceRun1NodeId, "source one", "one"],
|
|
[2, sourceRun2Id, sourceRun2NodeId, "source two", "two"],
|
|
] as const) {
|
|
yield* projectionStore.apply({
|
|
id: EventId.make(`event:projection-fork-source-rollback:run-${ordinal}`),
|
|
type: "run.created",
|
|
threadId: sourceThreadId,
|
|
runId,
|
|
nodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: runId,
|
|
threadId: sourceThreadId,
|
|
ordinal,
|
|
providerInstanceId,
|
|
modelSelection,
|
|
providerThreadId: sourceProviderThreadId,
|
|
userMessageId: MessageId.make(
|
|
`message:projection-fork-source-rollback:user:${ordinal}`,
|
|
),
|
|
rootNodeId: nodeId,
|
|
activeAttemptId: null,
|
|
status: "completed",
|
|
requestedAt: now,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
checkpointId: null,
|
|
contextHandoffId: null,
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make(`event:projection-fork-source-rollback:user-item-${ordinal}`),
|
|
type: "turn-item.updated",
|
|
threadId: sourceThreadId,
|
|
runId,
|
|
nodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
createdBy: "user",
|
|
creationSource: "web",
|
|
id: TurnItemId.make(`turn-item:projection-fork-source-rollback:user:${ordinal}`),
|
|
threadId: sourceThreadId,
|
|
runId,
|
|
nodeId,
|
|
providerThreadId: sourceProviderThreadId,
|
|
providerTurnId: null,
|
|
nativeItemRef: null,
|
|
parentItemId: null,
|
|
ordinal: ordinal * 100,
|
|
status: "completed",
|
|
title: null,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
updatedAt: now,
|
|
type: "user_message",
|
|
messageId: MessageId.make(`message:projection-fork-source-rollback:user:${ordinal}`),
|
|
inputIntent: "turn_start",
|
|
text: promptText,
|
|
attachments: [],
|
|
},
|
|
});
|
|
yield* projectionStore.apply({
|
|
id: EventId.make(`event:projection-fork-source-rollback:assistant-item-${ordinal}`),
|
|
type: "turn-item.updated",
|
|
threadId: sourceThreadId,
|
|
runId,
|
|
nodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: TurnItemId.make(`turn-item:projection-fork-source-rollback:assistant:${ordinal}`),
|
|
threadId: sourceThreadId,
|
|
runId,
|
|
nodeId,
|
|
providerThreadId: sourceProviderThreadId,
|
|
providerTurnId: null,
|
|
nativeItemRef: null,
|
|
parentItemId: null,
|
|
ordinal: ordinal * 100 + 1,
|
|
status: "completed",
|
|
title: null,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
updatedAt: now,
|
|
type: "assistant_message",
|
|
messageId: MessageId.make(
|
|
`message:projection-fork-source-rollback:assistant:${ordinal}`,
|
|
),
|
|
text: responseText,
|
|
streaming: false,
|
|
},
|
|
});
|
|
}
|
|
|
|
const targetBeforeRollback = yield* projectionStore.getThreadProjection(targetThreadId);
|
|
assert.deepEqual(
|
|
targetBeforeRollback.visibleTurnItems.map((row) => row.item.type),
|
|
["user_message", "assistant_message", "user_message", "assistant_message", "fork"],
|
|
);
|
|
|
|
yield* projectionStore.apply({
|
|
id: EventId.make("event:projection-fork-source-rollback:run-2-rolled-back"),
|
|
type: "run.updated",
|
|
threadId: sourceThreadId,
|
|
runId: sourceRun2Id,
|
|
nodeId: sourceRun2NodeId,
|
|
driver,
|
|
occurredAt: now,
|
|
payload: {
|
|
id: sourceRun2Id,
|
|
threadId: sourceThreadId,
|
|
ordinal: 2,
|
|
providerInstanceId,
|
|
modelSelection,
|
|
providerThreadId: sourceProviderThreadId,
|
|
userMessageId: MessageId.make("message:projection-fork-source-rollback:user:2"),
|
|
rootNodeId: sourceRun2NodeId,
|
|
activeAttemptId: null,
|
|
status: "rolled_back",
|
|
requestedAt: now,
|
|
startedAt: now,
|
|
completedAt: now,
|
|
checkpointId: null,
|
|
contextHandoffId: null,
|
|
},
|
|
});
|
|
|
|
const targetAfterRollback = yield* projectionStore.getThreadProjection(targetThreadId);
|
|
assert.deepEqual(
|
|
targetAfterRollback.visibleTurnItems.map((row) => [
|
|
row.visibility,
|
|
row.item.type,
|
|
row.item.type === "user_message" || row.item.type === "assistant_message"
|
|
? row.item.text
|
|
: row.item.title,
|
|
]),
|
|
[
|
|
["inherited", "user_message", "source one"],
|
|
["inherited", "assistant_message", "one"],
|
|
["inherited", "user_message", "source two"],
|
|
["inherited", "assistant_message", "two"],
|
|
["synthetic", "fork", "Forked from conversation"],
|
|
],
|
|
);
|
|
}),
|
|
);
|
|
});
|