fix(orchestrator): validate replay edge cases
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
7c75be6a3d
commit
f03337a438
6 files changed
+168
-3
No files matched your search
@@ -229,6 +229,37 @@ describe("CodexAdapterV2 assistant message streaming", () => {
|
||||
]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("treats explicit empty final text as authoritative over buffered deltas", () =>
|
||||
Effect.gen(function* () {
|
||||
const updates = yield* Ref.make<ReadonlyArray<CodexAgentMessageDeltaUpdate>>([]);
|
||||
const coalescer = yield* makeCodexAgentMessageDeltaCoalescer({
|
||||
flushIntervalMs: 50,
|
||||
emit: (update) => Ref.update(updates, (current) => [...current, update]),
|
||||
});
|
||||
|
||||
yield* coalescer.append({
|
||||
turnId: "turn-1",
|
||||
itemId: "message-1",
|
||||
delta: "stale buffered text",
|
||||
});
|
||||
const completedText = yield* coalescer.complete({
|
||||
turnId: "turn-1",
|
||||
itemId: "message-1",
|
||||
finalText: "",
|
||||
});
|
||||
|
||||
assert.equal(completedText, "");
|
||||
assert.deepEqual(yield* Ref.get(updates), [
|
||||
{
|
||||
turnId: "turn-1",
|
||||
itemId: "message-1",
|
||||
text: "",
|
||||
completed: true,
|
||||
},
|
||||
]);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("CodexAdapterV2 runtime policy", () => {
|
||||
|
||||
@@ -1022,7 +1022,7 @@ export const makeCodexAgentMessageDeltaCoalescer = Effect.fn(
|
||||
Effect.gen(function* () {
|
||||
const key = codexAgentMessageBufferKey(turnId, itemId);
|
||||
const existing = (yield* Ref.get(buffered)).get(key);
|
||||
const text = finalText && finalText.length > 0 ? finalText : (existing?.text ?? "");
|
||||
const text = finalText !== undefined ? finalText : (existing?.text ?? "");
|
||||
if (emitEmpty || text.length > 0) {
|
||||
yield* input.emit({ turnId, itemId, text, completed: true });
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { assert, it, vi } from "@effect/vitest";
|
||||
import {
|
||||
CheckpointId,
|
||||
CheckpointScopeId,
|
||||
type OrchestrationV2ThreadProjection,
|
||||
ProviderSessionId,
|
||||
ProviderThreadId,
|
||||
ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
|
||||
import { CheckpointServiceV2 } from "./CheckpointService.ts";
|
||||
import {
|
||||
CheckpointRollbackServiceV2,
|
||||
layer as checkpointRollbackServiceLayer,
|
||||
} from "./CheckpointRollbackService.ts";
|
||||
import { EventSinkV2 } from "./EventSink.ts";
|
||||
import { layer as idAllocatorLayer } from "./IdAllocator.ts";
|
||||
import { ProjectionStoreV2 } from "./ProjectionStore.ts";
|
||||
import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
|
||||
import { RuntimePolicyV2 } from "./RuntimePolicy.ts";
|
||||
|
||||
it.effect("rejects a non-ready checkpoint before opening a session or restoring files", () => {
|
||||
const threadId = ThreadId.make("thread:rollback-non-ready");
|
||||
const providerThreadId = ProviderThreadId.make("provider-thread:rollback-non-ready");
|
||||
const providerSessionId = ProviderSessionId.make("provider-session:rollback-non-ready");
|
||||
const checkpointId = CheckpointId.make("checkpoint:rollback-non-ready");
|
||||
const scopeId = CheckpointScopeId.make("checkpoint-scope:rollback-non-ready");
|
||||
const restore = vi.fn(() => Effect.die("checkpoint restore must not run"));
|
||||
const open = vi.fn(() => Effect.die("provider session open must not run"));
|
||||
const resolveRuntimePolicy = vi.fn(() => Effect.die("runtime policy resolution must not run"));
|
||||
const projection = {
|
||||
providerThreads: [{ id: providerThreadId, providerSessionId }],
|
||||
checkpoints: [{ id: checkpointId, scopeId, status: "stale" }],
|
||||
checkpointScopes: [{ id: scopeId }],
|
||||
} as unknown as OrchestrationV2ThreadProjection;
|
||||
const testLayer = checkpointRollbackServiceLayer.pipe(
|
||||
Layer.provide(
|
||||
Layer.mergeAll(
|
||||
Layer.mock(CheckpointServiceV2)({ restore }),
|
||||
Layer.mock(EventSinkV2)({}),
|
||||
idAllocatorLayer,
|
||||
Layer.mock(ProjectionStoreV2)({
|
||||
getThreadProjection: () => Effect.succeed(projection),
|
||||
}),
|
||||
Layer.mock(ProviderSessionManagerV2)({ open }),
|
||||
Layer.mock(RuntimePolicyV2)({ resolve: resolveRuntimePolicy }),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const service = yield* CheckpointRollbackServiceV2;
|
||||
const error = yield* service
|
||||
.execute({
|
||||
threadId,
|
||||
providerThreadId,
|
||||
checkpointId,
|
||||
scopeId,
|
||||
})
|
||||
.pipe(Effect.flip);
|
||||
|
||||
assert.equal(error.cause, "The persisted rollback target is incomplete or no longer valid.");
|
||||
assert.equal(resolveRuntimePolicy.mock.calls.length, 0);
|
||||
assert.equal(open.mock.calls.length, 0);
|
||||
assert.equal(restore.mock.calls.length, 0);
|
||||
}).pipe(Effect.provide(testLayer));
|
||||
});
|
||||
@@ -81,7 +81,8 @@ export const layer: Layer.Layer<
|
||||
providerThread.providerSessionId === null ||
|
||||
checkpoint === undefined ||
|
||||
scope === undefined ||
|
||||
checkpoint.scopeId !== scope.id
|
||||
checkpoint.scopeId !== scope.id ||
|
||||
checkpoint.status !== "ready"
|
||||
) {
|
||||
return yield* new CheckpointRollbackExecutionError({
|
||||
threadId: input.threadId,
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
|
||||
import { runMigrations } from "../Migrations.ts";
|
||||
import * as NodeSqliteClient from "../NodeSqliteClient.ts";
|
||||
|
||||
it.effect("backfills the V2 event driver from payload before the legacy provider column", () =>
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
yield* runMigrations({ toMigrationInclusive: 36 });
|
||||
|
||||
yield* sql`
|
||||
INSERT INTO orchestration_v2_events (
|
||||
event_id,
|
||||
thread_id,
|
||||
provider,
|
||||
event_type,
|
||||
occurred_at,
|
||||
payload_json
|
||||
) VALUES
|
||||
(
|
||||
'event:explicit-driver',
|
||||
'thread:explicit-driver',
|
||||
'codex-work',
|
||||
'thread.created',
|
||||
'2026-07-29T00:00:00.000Z',
|
||||
'{"driver":"codex","providerInstanceId":"codex-work"}'
|
||||
),
|
||||
(
|
||||
'event:legacy-provider',
|
||||
'thread:legacy-provider',
|
||||
'claude-agent',
|
||||
'thread.created',
|
||||
'2026-07-29T00:00:00.000Z',
|
||||
'{}'
|
||||
)
|
||||
`;
|
||||
|
||||
yield* runMigrations({ toMigrationInclusive: 37 });
|
||||
|
||||
const events = yield* sql<{
|
||||
readonly event_id: string;
|
||||
readonly driver: string;
|
||||
readonly provider_instance_id: string;
|
||||
}>`
|
||||
SELECT event_id, driver, provider_instance_id
|
||||
FROM orchestration_v2_events
|
||||
ORDER BY event_id
|
||||
`;
|
||||
assert.deepStrictEqual(events, [
|
||||
{
|
||||
event_id: "event:explicit-driver",
|
||||
driver: "codex",
|
||||
provider_instance_id: "codex-work",
|
||||
},
|
||||
{
|
||||
event_id: "event:legacy-provider",
|
||||
driver: "claude-agent",
|
||||
provider_instance_id: "claude-agent",
|
||||
},
|
||||
]);
|
||||
}).pipe(Effect.provide(NodeSqliteClient.layerMemory())),
|
||||
);
|
||||
@@ -18,7 +18,7 @@ export default Effect.gen(function* () {
|
||||
yield* sql`
|
||||
UPDATE orchestration_v2_events
|
||||
SET
|
||||
driver = provider,
|
||||
driver = COALESCE(json_extract(payload_json, '$.driver'), provider),
|
||||
provider_instance_id = COALESCE(
|
||||
json_extract(payload_json, '$.providerInstanceId'),
|
||||
provider
|
||||
|
||||
Reference in new issue
Block a user