fix(server): gate migrated thread commands on hydration
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
6a2a78526b
commit
a7495ac4ba
4 files changed
+400
-21
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { expect, it } from "@effect/vitest";
|
||||
import {
|
||||
CommandId,
|
||||
NodeId,
|
||||
type OrchestrationV2Command,
|
||||
ProjectId,
|
||||
ProviderInstanceId,
|
||||
@@ -8,7 +9,7 @@ import {
|
||||
ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import { withCreationProvenance } from "./ThreadManagementService.ts";
|
||||
import { existingThreadIdsForCommand, withCreationProvenance } from "./ThreadManagementService.ts";
|
||||
|
||||
it("stamps authoritative provenance on commands that create threads or messages", () => {
|
||||
const command: OrchestrationV2Command = {
|
||||
@@ -55,3 +56,98 @@ it("leaves commands that do not create durable authored content unchanged", () =
|
||||
}),
|
||||
).toBe(command);
|
||||
});
|
||||
|
||||
it("identifies every existing thread that must be hydrated before dispatch", () => {
|
||||
const sourceThreadId = ThreadId.make("thread:thread-management:source");
|
||||
const targetThreadId = ThreadId.make("thread:thread-management:target");
|
||||
const parentThreadId = ThreadId.make("thread:thread-management:parent");
|
||||
|
||||
expect(
|
||||
existingThreadIdsForCommand({
|
||||
type: "thread.create",
|
||||
createdBy: "user",
|
||||
creationSource: "web",
|
||||
commandId: CommandId.make("command:thread-management:create"),
|
||||
threadId: targetThreadId,
|
||||
projectId: ProjectId.make("project:thread-management"),
|
||||
title: "Created thread",
|
||||
modelSelection: {
|
||||
instanceId: ProviderInstanceId.make("codex"),
|
||||
model: "gpt-5-codex",
|
||||
},
|
||||
runtimeMode: "full-access",
|
||||
interactionMode: "default",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
}),
|
||||
).toEqual([]);
|
||||
|
||||
expect(
|
||||
existingThreadIdsForCommand({
|
||||
type: "thread.archive",
|
||||
commandId: CommandId.make("command:thread-management:archive"),
|
||||
threadId: targetThreadId,
|
||||
}),
|
||||
).toEqual([targetThreadId]);
|
||||
|
||||
expect(
|
||||
existingThreadIdsForCommand({
|
||||
type: "thread.fork",
|
||||
createdBy: "user",
|
||||
creationSource: "web",
|
||||
commandId: CommandId.make("command:thread-management:fork"),
|
||||
sourceThreadId,
|
||||
targetThreadId,
|
||||
sourcePoint: {
|
||||
type: "run",
|
||||
runId: RunId.make("run:thread-management:source"),
|
||||
},
|
||||
}),
|
||||
).toEqual([sourceThreadId]);
|
||||
|
||||
expect(
|
||||
existingThreadIdsForCommand({
|
||||
type: "thread.merge_back",
|
||||
createdBy: "user",
|
||||
creationSource: "web",
|
||||
commandId: CommandId.make("command:thread-management:merge"),
|
||||
sourceThreadId,
|
||||
targetThreadId,
|
||||
sourcePoint: {
|
||||
type: "run",
|
||||
runId: RunId.make("run:thread-management:source"),
|
||||
},
|
||||
}),
|
||||
).toEqual([sourceThreadId, targetThreadId]);
|
||||
|
||||
expect(
|
||||
existingThreadIdsForCommand({
|
||||
type: "delegated_task.request",
|
||||
createdBy: "agent",
|
||||
creationSource: "provider",
|
||||
commandId: CommandId.make("command:thread-management:delegate"),
|
||||
parentThreadId,
|
||||
parentRunId: RunId.make("run:thread-management:parent"),
|
||||
parentNodeId: NodeId.make("node:thread-management:parent"),
|
||||
task: "Inspect the migration",
|
||||
modelSelection: {
|
||||
instanceId: ProviderInstanceId.make("codex"),
|
||||
model: "gpt-5-codex",
|
||||
},
|
||||
runtimeMode: "full-access",
|
||||
interactionMode: "default",
|
||||
}),
|
||||
).toEqual([parentThreadId]);
|
||||
|
||||
expect(
|
||||
existingThreadIdsForCommand({
|
||||
type: "thread.created.record",
|
||||
commandId: CommandId.make("command:thread-management:record"),
|
||||
parentThreadId,
|
||||
parentRunId: RunId.make("run:thread-management:parent"),
|
||||
parentNodeId: NodeId.make("node:thread-management:parent"),
|
||||
targetThreadId,
|
||||
targetRunId: null,
|
||||
}),
|
||||
).toEqual([parentThreadId, targetThreadId]);
|
||||
});
|
||||
@@ -25,11 +25,16 @@ import * as Option from "effect/Option";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import {
|
||||
OrchestratorDispatchError,
|
||||
OrchestratorProjectionError,
|
||||
OrchestratorV2,
|
||||
type OrchestratorV2DispatchResult,
|
||||
type OrchestratorV2Error,
|
||||
} from "./Orchestrator.ts";
|
||||
import { LegacyV1ThreadImporter } from "./LegacyV1ThreadImporter.ts";
|
||||
import {
|
||||
LegacyV1ThreadImporter,
|
||||
type LegacyV1ThreadImportError,
|
||||
} from "./LegacyV1ThreadImporter.ts";
|
||||
|
||||
export type ThreadManagementSendMode = "auto" | "queue" | "steer" | "restart";
|
||||
|
||||
@@ -54,6 +59,29 @@ export function withCreationProvenance(
|
||||
}
|
||||
}
|
||||
|
||||
export function existingThreadIdsForCommand(
|
||||
command: OrchestrationV2Command,
|
||||
): ReadonlyArray<ThreadId> {
|
||||
switch (command.type) {
|
||||
case "thread.create":
|
||||
return [];
|
||||
case "thread.fork":
|
||||
return [command.sourceThreadId];
|
||||
case "thread.merge_back":
|
||||
return command.sourceThreadId === command.targetThreadId
|
||||
? [command.sourceThreadId]
|
||||
: [command.sourceThreadId, command.targetThreadId];
|
||||
case "delegated_task.request":
|
||||
return [command.parentThreadId];
|
||||
case "thread.created.record":
|
||||
return command.parentThreadId === command.targetThreadId
|
||||
? [command.parentThreadId]
|
||||
: [command.parentThreadId, command.targetThreadId];
|
||||
default:
|
||||
return [command.threadId];
|
||||
}
|
||||
}
|
||||
|
||||
export type ThreadManagementTerminalRunStatus = Extract<
|
||||
OrchestrationV2Run["status"],
|
||||
"completed" | "failed" | "cancelled" | "interrupted" | "rolled_back"
|
||||
@@ -133,7 +161,9 @@ export class ThreadManagementError extends Schema.TaggedErrorClass<ThreadManagem
|
||||
type ThreadManagementFailure = ThreadManagementError | OrchestratorV2Error;
|
||||
|
||||
export interface ThreadManagementServiceShape {
|
||||
readonly ensureLegacyTranscript: (threadId: ThreadId) => Effect.Effect<void>;
|
||||
readonly ensureLegacyTranscript: (
|
||||
threadId: ThreadId,
|
||||
) => Effect.Effect<void, LegacyV1ThreadImportError>;
|
||||
readonly dispatch: (
|
||||
command: OrchestrationV2Command,
|
||||
) => Effect.Effect<OrchestratorV2DispatchResult, OrchestratorV2Error>;
|
||||
@@ -239,36 +269,61 @@ const make = Effect.gen(function* () {
|
||||
const orchestrator = yield* OrchestratorV2;
|
||||
const legacyImporter = yield* LegacyV1ThreadImporter;
|
||||
|
||||
const ensureLegacyTranscript = (threadId: ThreadId) =>
|
||||
legacyImporter.ensureTranscript(threadId).pipe(
|
||||
const ensureLegacyTranscript = Effect.fn(
|
||||
"orchestrationV2.threadManagement.ensureLegacyTranscript",
|
||||
)(function* (threadId: ThreadId) {
|
||||
yield* legacyImporter.ensureTranscript(threadId).pipe(
|
||||
Effect.tapError((cause) =>
|
||||
Effect.logWarning("Unable to hydrate migrated v1 thread transcript", {
|
||||
threadId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.ignore,
|
||||
);
|
||||
});
|
||||
|
||||
const ensureProjectionTranscript = (threadId: ThreadId) =>
|
||||
ensureLegacyTranscript(threadId).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new OrchestratorProjectionError({
|
||||
threadId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const ensureCommandTranscripts = Effect.fn(
|
||||
"orchestrationV2.threadManagement.ensureCommandTranscripts",
|
||||
)(function* (command: OrchestrationV2Command) {
|
||||
yield* Effect.forEach(
|
||||
existingThreadIdsForCommand(command),
|
||||
(threadId) => ensureLegacyTranscript(threadId),
|
||||
{ discard: true },
|
||||
).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new OrchestratorDispatchError({
|
||||
commandId: command.commandId,
|
||||
commandType: command.type,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
const getThreadProjection: ThreadManagementServiceShape["getThreadProjection"] = (threadId) =>
|
||||
ensureLegacyTranscript(threadId).pipe(
|
||||
ensureProjectionTranscript(threadId).pipe(
|
||||
Effect.andThen(orchestrator.getThreadProjection(threadId)),
|
||||
);
|
||||
|
||||
const getThreadSnapshot: ThreadManagementServiceShape["getThreadSnapshot"] = (threadId) =>
|
||||
ensureLegacyTranscript(threadId).pipe(Effect.andThen(orchestrator.getThreadSnapshot(threadId)));
|
||||
ensureProjectionTranscript(threadId).pipe(
|
||||
Effect.andThen(orchestrator.getThreadSnapshot(threadId)),
|
||||
);
|
||||
|
||||
const dispatch: ThreadManagementServiceShape["dispatch"] = (command) => {
|
||||
const threadId =
|
||||
command.type === "message.dispatch"
|
||||
? command.threadId
|
||||
: command.type === "thread.fork" || command.type === "thread.merge_back"
|
||||
? command.sourceThreadId
|
||||
: undefined;
|
||||
return threadId === undefined
|
||||
? orchestrator.dispatch(command)
|
||||
: ensureLegacyTranscript(threadId).pipe(Effect.andThen(orchestrator.dispatch(command)));
|
||||
};
|
||||
const dispatch: ThreadManagementServiceShape["dispatch"] = (command) =>
|
||||
ensureCommandTranscripts(command).pipe(Effect.andThen(orchestrator.dispatch(command)));
|
||||
|
||||
const getProjectThread: ThreadManagementServiceShape["getProjectThread"] = (input) =>
|
||||
getThreadProjection(input.threadId).pipe(
|
||||
|
||||
@@ -31,8 +31,14 @@ import { ProviderInstanceRegistry } from "../provider/Services/ProviderInstanceR
|
||||
import type { ProviderInstance } from "../provider/ProviderDriver.ts";
|
||||
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
|
||||
import * as VcsProcess from "../vcs/VcsProcess.ts";
|
||||
import { OrchestratorV2 } from "./Orchestrator.ts";
|
||||
import { LegacyV1ThreadImporter, LegacyV1ThreadImportError } from "./LegacyV1ThreadImporter.ts";
|
||||
import {
|
||||
OrchestratorDispatchError,
|
||||
OrchestratorProjectionError,
|
||||
OrchestratorV2,
|
||||
} from "./Orchestrator.ts";
|
||||
import { OrchestrationEffectWorkerV2 } from "./EffectWorker.ts";
|
||||
import { ProjectionMaintenanceV2 } from "./ProjectionMaintenance.ts";
|
||||
import type { ProviderAdapterV2Shape } from "./ProviderAdapter.ts";
|
||||
import { OrchestrationV2LayerLive } from "./runtimeLayer.ts";
|
||||
import { shellStreamItemFromSnapshot } from "./ShellStream.ts";
|
||||
@@ -99,6 +105,16 @@ const TestLayer = OrchestrationV2LayerLive.pipe(
|
||||
Layer.provide(NodeServices.layer),
|
||||
);
|
||||
|
||||
const LegacyImportTestLayer = OrchestrationV2LayerLive.pipe(
|
||||
Layer.provide(mcpSessionRegistryTestLayer),
|
||||
Layer.provideMerge(SqlitePersistenceMemory),
|
||||
Layer.provide(CheckpointStoreTestLayer),
|
||||
Layer.provide(ServerConfigLayer),
|
||||
Layer.provide(ServerSettingsService.layerTest()),
|
||||
Layer.provide(TestProviderInstanceRegistry),
|
||||
Layer.provide(NodeServices.layer),
|
||||
);
|
||||
|
||||
const SharedApplicationDataPlaneTestLayer = Layer.merge(
|
||||
OrchestrationLayerLive,
|
||||
OrchestrationV2LayerLive,
|
||||
@@ -162,7 +178,210 @@ it.layer(TestLayer)("OrchestrationV2LayerLive", (it) => {
|
||||
assert.deepEqual(projection.runs, []);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it.layer(LegacyImportTestLayer)("OrchestrationV2 legacy import", (it) => {
|
||||
it.effect("hydrates imported transcripts before commands and propagates hydration failures", () =>
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
const importer = yield* LegacyV1ThreadImporter;
|
||||
const maintenance = yield* ProjectionMaintenanceV2;
|
||||
const orchestrator = yield* OrchestratorV2;
|
||||
const threadManagement = yield* ThreadManagementService;
|
||||
const metadataThreadId = ThreadId.make("runtime-layer-legacy-metadata-thread");
|
||||
const failureThreadId = ThreadId.make("runtime-layer-legacy-failure-thread");
|
||||
const projectId = ProjectId.make("runtime-layer-legacy-project");
|
||||
|
||||
yield* sql`
|
||||
INSERT INTO projection_projects (
|
||||
project_id,
|
||||
title,
|
||||
workspace_root,
|
||||
default_model_selection_json,
|
||||
scripts_json,
|
||||
created_at,
|
||||
updated_at,
|
||||
deleted_at
|
||||
) VALUES (
|
||||
${projectId},
|
||||
'Legacy project',
|
||||
'/tmp/runtime-layer-legacy-project',
|
||||
'{"instanceId":"codex","model":"gpt-5.4"}',
|
||||
'[]',
|
||||
'2026-01-01T00:00:00.000Z',
|
||||
'2026-01-04T00:00:00.000Z',
|
||||
NULL
|
||||
)
|
||||
`;
|
||||
yield* sql`
|
||||
INSERT INTO projection_threads (
|
||||
thread_id,
|
||||
project_id,
|
||||
title,
|
||||
model_selection_json,
|
||||
runtime_mode,
|
||||
interaction_mode,
|
||||
branch,
|
||||
worktree_path,
|
||||
latest_turn_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
archived_at,
|
||||
settled_override,
|
||||
settled_at,
|
||||
deleted_at
|
||||
) VALUES
|
||||
(
|
||||
${metadataThreadId},
|
||||
${projectId},
|
||||
'Legacy metadata title',
|
||||
'{"instanceId":"codex","model":"gpt-5.4"}',
|
||||
'full-access',
|
||||
'default',
|
||||
'main',
|
||||
'/tmp/runtime-layer-legacy-project',
|
||||
NULL,
|
||||
'2026-01-01T00:00:00.000Z',
|
||||
'2026-01-04T00:00:00.000Z',
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
),
|
||||
(
|
||||
${failureThreadId},
|
||||
${projectId},
|
||||
'Legacy failure title',
|
||||
'{"instanceId":"codex","model":"gpt-5.4"}',
|
||||
'full-access',
|
||||
'default',
|
||||
'main',
|
||||
'/tmp/runtime-layer-legacy-project',
|
||||
NULL,
|
||||
'2026-01-01T00:00:00.000Z',
|
||||
'2026-01-04T00:00:00.000Z',
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
)
|
||||
`;
|
||||
yield* sql`
|
||||
INSERT INTO projection_thread_messages (
|
||||
message_id,
|
||||
thread_id,
|
||||
turn_id,
|
||||
role,
|
||||
text,
|
||||
attachments_json,
|
||||
is_streaming,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES
|
||||
(
|
||||
'message:runtime-layer-legacy:1',
|
||||
${metadataThreadId},
|
||||
NULL,
|
||||
'user',
|
||||
'First imported question',
|
||||
'[]',
|
||||
0,
|
||||
'2026-01-01T01:00:00.000Z',
|
||||
'2026-01-01T01:00:00.000Z'
|
||||
),
|
||||
(
|
||||
'message:runtime-layer-legacy:2',
|
||||
${metadataThreadId},
|
||||
NULL,
|
||||
'assistant',
|
||||
'First imported answer',
|
||||
'[]',
|
||||
0,
|
||||
'2026-01-02T01:00:00.000Z',
|
||||
'2026-01-02T01:00:00.000Z'
|
||||
),
|
||||
(
|
||||
'message:runtime-layer-legacy:3',
|
||||
${metadataThreadId},
|
||||
NULL,
|
||||
'user',
|
||||
'Latest imported question',
|
||||
'[]',
|
||||
0,
|
||||
'2026-01-03T01:00:00.000Z',
|
||||
'2026-01-03T01:00:00.000Z'
|
||||
),
|
||||
(
|
||||
'message:runtime-layer-legacy:failure',
|
||||
${failureThreadId},
|
||||
NULL,
|
||||
'user',
|
||||
'Imported context must load before archive',
|
||||
'[]',
|
||||
0,
|
||||
'2026-01-03T01:00:00.000Z',
|
||||
'2026-01-03T01:00:00.000Z'
|
||||
)
|
||||
`;
|
||||
|
||||
yield* importer.reconcileShells;
|
||||
const rebuilt = yield* maintenance.rebuild;
|
||||
assert.isTrue(rebuilt.valid);
|
||||
|
||||
yield* threadManagement.dispatch({
|
||||
type: "thread.metadata.update",
|
||||
commandId: CommandId.make("runtime-layer-legacy-metadata-update"),
|
||||
threadId: metadataThreadId,
|
||||
title: "Updated after import",
|
||||
});
|
||||
const updatedProjection = yield* threadManagement.getThreadProjection(metadataThreadId);
|
||||
assert.equal(updatedProjection.thread.title, "Updated after import");
|
||||
assert.deepEqual(
|
||||
updatedProjection.messages.map((message) => message.text),
|
||||
["First imported question", "First imported answer", "Latest imported question"],
|
||||
);
|
||||
|
||||
yield* sql`
|
||||
ALTER TABLE projection_thread_messages
|
||||
RENAME TO projection_thread_messages_unavailable
|
||||
`;
|
||||
const { projectionFailure, hydrationFailure } = yield* Effect.all({
|
||||
projectionFailure: threadManagement.getThreadProjection(failureThreadId).pipe(Effect.flip),
|
||||
hydrationFailure: threadManagement
|
||||
.dispatch({
|
||||
type: "thread.archive",
|
||||
commandId: CommandId.make("runtime-layer-legacy-failed-archive"),
|
||||
threadId: failureThreadId,
|
||||
})
|
||||
.pipe(Effect.flip),
|
||||
}).pipe(
|
||||
Effect.ensuring(
|
||||
sql`
|
||||
ALTER TABLE projection_thread_messages_unavailable
|
||||
RENAME TO projection_thread_messages
|
||||
`.pipe(Effect.orDie),
|
||||
),
|
||||
);
|
||||
assert.instanceOf(projectionFailure, OrchestratorProjectionError);
|
||||
assert.instanceOf(projectionFailure.cause, LegacyV1ThreadImportError);
|
||||
assert.instanceOf(hydrationFailure, OrchestratorDispatchError);
|
||||
assert.instanceOf(hydrationFailure.cause, LegacyV1ThreadImportError);
|
||||
|
||||
const projectionAfterFailure = yield* orchestrator.getThreadProjection(failureThreadId);
|
||||
assert.isNull(projectionAfterFailure.thread.archivedAt);
|
||||
|
||||
yield* threadManagement.dispatch({
|
||||
type: "thread.archive",
|
||||
commandId: CommandId.make("runtime-layer-legacy-retried-archive"),
|
||||
threadId: failureThreadId,
|
||||
});
|
||||
const projectionAfterRetry = yield* threadManagement.getThreadProjection(failureThreadId);
|
||||
assert.isNotNull(projectionAfterRetry.thread.archivedAt);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
|
||||
it.effect("applies lifecycle commands idempotently and emits archive/removal shell deltas", () =>
|
||||
Effect.gen(function* () {
|
||||
const orchestrator = yield* OrchestratorV2;
|
||||
|
||||
+10
-1
@@ -637,7 +637,16 @@ const makeWsRpcLayer = (
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"orchestration_v2.thread_id": input.threadId,
|
||||
});
|
||||
yield* threadManagement.ensureLegacyTranscript(input.threadId);
|
||||
yield* threadManagement.ensureLegacyTranscript(input.threadId).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new OrchestrationV2GetThreadProjectionError({
|
||||
threadId: input.threadId,
|
||||
message: `Failed to hydrate migrated thread ${input.threadId}`,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const eventStreamFrom = (afterSequence: number) =>
|
||||
threadManagement
|
||||
|
||||
Reference in new issue
Block a user