Skip to content
126 changes: 126 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
decodeThreadHistoryCursor,
encodeThreadHistoryCursor,
selectHistoryPageFromCursor,
THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT,
THREAD_HISTORY_PAGE_POLICY,
THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
} from "./threadHistoryPaging.ts";
Expand Down Expand Up @@ -2941,6 +2942,131 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
}),
);

it.effect("stores completed inline-output flags that page like the payload predicate", () =>
Effect.gen(function* () {
const store = yield* ProjectionStore.ProjectionStoreV2;
const sql = yield* SqlClient.SqlClient;
const threadId = yield* addRolledBackRecoveryCandidate("inline-output-flag");
const run = (yield* store.getThreadProjection(threadId)).runs[0]!;
const now = yield* DateTime.now;
yield* store.apply({
id: EventId.make("event:inline-output-flag:run-completed"),
type: "run.updated",
threadId,
runId: run.id,
providerInstanceId,
occurredAt: now,
payload: { ...run, status: "completed", completedAt: now },
});
const base = {
threadId,
runId: run.id,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
title: null,
startedAt: now,
completedAt: now,
updatedAt: now,
};
const tool = (input: unknown, toolName = "lookup") => ({
...base,
type: "dynamic_tool" as const,
toolName,
input,
});
const cases = [
["plain", tool({ q: "x" }), 0],
["html", tool({ htmlRender: true }), 1],
["html-case", tool({ HTMLRENDER: true }), 1],
["app", tool({ t3McpApp: {} }), 1],
["image", { ...tool({}), viewedImagePath: "/tmp/a.png" }, 1],
["delegate", tool({}, "mcp__t3_code__delegate_task"), 1],
["delegate-like", tool({}, "t3-code.delegate-task"), 1],
["running", { ...tool({ htmlRender: true }), status: "running" as const }, null],
["command", { ...base, type: "command_execution" as const, input: "htmlRender" }, 0],
] as const;
const write = (name: string, item: (typeof cases)[number][1], ordinal: number) =>
store.apply({
id: EventId.make(`event:inline-output-flag:${name}:${ordinal}`),
type: "turn-item.updated",
threadId,
runId: run.id,
occurredAt: now,
payload: {
status: "completed",
...item,
id: TurnItemId.make(`item:inline-output-flag:${name}`),
ordinal,
},
});
for (const [index, [name, item]] of cases.entries()) yield* write(name, item, index + 2);
const flags = () =>
sql<{ readonly id: string; readonly flag: number | null }>`
SELECT turn_item_id AS id, persistent_output AS flag
FROM orchestration_v2_projection_turn_items
WHERE thread_id = ${threadId} AND ordinal > 1
ORDER BY ordinal`;
assert.deepEqual(
yield* flags(),
cases.map(([name, , flag]) => ({ id: `item:inline-output-flag:${name}`, flag })),
);

yield* write("html", tool({ q: "x" }), 3);
yield* write("plain", { ...tool({ htmlRender: true }), status: "running" }, 2);
yield* write("running", tool({ htmlRender: true }), 9);
const updated = new Map((yield* flags()).map((row) => [row.id, row.flag]));
assert.strictEqual(updated.get("item:inline-output-flag:html"), 0);
assert.isNull(updated.get("item:inline-output-flag:plain"));
assert.strictEqual(updated.get("item:inline-output-flag:running"), 1);
for (let index = 0; index < THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT; index += 1)
yield* write(`bulk-${index}`, tool({ index }), 20 + index);

// The raw SQL edits below stand in for committed writes, which always append a thread event.
let revision = 0;
const page = () =>
sql`INSERT INTO orchestration_events (
event_id, aggregate_kind, stream_id, stream_version, event_type,
occurred_at, actor_kind, payload_json, metadata_json, application_event_version
) VALUES (
${`event:inline-output-flag:revision:${++revision}`}, 'thread', ${threadId}, ${revision},
'turn-item.updated', ${DateTime.formatIso(now)}, 'user', '{}', '{}', 2
)`.pipe(
Effect.andThen(
store.getThreadHistoryPage(
threadId,
encodeThreadHistoryCursor({
snapshotSequence: 0,
sourceThreadId: threadId,
sourceItemId: "item:inline-output-flag:end",
position: 1_000,
}),
undefined,
false,
"deferred",
),
),
);
const stored = yield* page();
assert.isTrue(stored.items.some((row) => row.item.deferredWork !== undefined));
assert.isTrue(
stored.items.some((row) => String(row.sourceItemId) === "item:inline-output-flag:app"),
);
yield* sql`UPDATE orchestration_v2_projection_turn_items SET persistent_output = 0
WHERE thread_id = ${threadId} AND turn_item_id = 'item:inline-output-flag:app'`;
assert.isFalse(
(yield* page()).items.some(
(row) => String(row.sourceItemId) === "item:inline-output-flag:app",
),
);
yield* sql`UPDATE orchestration_v2_projection_turn_items SET persistent_output = NULL
WHERE thread_id = ${threadId}`;
assert.deepEqual({ ...(yield* page()), snapshotSequence: stored.snapshotSequence }, stored);
}),
);

it.effect(
"SQL recovery selects unfinished runless native subagents",
() => runlessNativeSubagentNeedsRecovery,
Expand Down
41 changes: 24 additions & 17 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2807,22 +2807,26 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
type,
status,
updated_at,
payload_json
)
VALUES (
${event.payload.id},
${event.payload.threadId},
${event.payload.runId},
${event.payload.nodeId},
${event.payload.providerThreadId},
${event.payload.providerTurnId},
${event.payload.parentItemId},
${event.payload.ordinal},
${event.payload.type},
${event.payload.status},
${updatedAt},
${payloadJson}
payload_json,
persistent_output
)
SELECT item.*, CASE WHEN item.status = 'completed'
THEN CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END END
FROM (SELECT
${event.payload.id} AS turn_item_id,
${event.payload.threadId} AS thread_id,
${event.payload.runId} AS run_id,
${event.payload.nodeId} AS node_id,
${event.payload.providerThreadId} AS provider_thread_id,
${event.payload.providerTurnId} AS provider_turn_id,
${event.payload.parentItemId} AS parent_item_id,
${event.payload.ordinal} AS ordinal,
${event.payload.type} AS type,
${event.payload.status} AS status,
${updatedAt} AS updated_at,
${payloadJson} AS payload_json
) AS item
WHERE true
ON CONFLICT(turn_item_id)
DO UPDATE SET
thread_id = excluded.thread_id,
Expand All @@ -2835,7 +2839,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
type = excluded.type,
status = excluded.status,
updated_at = excluded.updated_at,
payload_json = excluded.payload_json
payload_json = excluded.payload_json,
persistent_output = excluded.persistent_output
`;
break;
}
Expand Down Expand Up @@ -6171,7 +6176,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
${includePersistentOutput ? sql`attempt.attempt_id` : sql`NULL`} AS attempt_id,
${includePersistentOutput ? sql`attempt.status` : sql`NULL`} AS attempt_status,
${
includePersistentOutput ? sql`CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END` : sql`0`
includePersistentOutput
? sql`COALESCE(item.persistent_output, CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END)`
: sql`0`
} AS persistent_output,
CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.inputIntent') END AS input_intent,
CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.createdBy') END AS created_by,
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 @@ -88,6 +88,7 @@ import Migration0082 from "./Migrations/082_TurnItemConversationIndex.ts";
import Migration0083 from "./Migrations/083_CommandReadIndexes.ts";
import Migration0084 from "./Migrations/084_LastExecutedRunIndex.ts";
import Migration0085 from "./Migrations/085_TurnItemCounts.ts";
import Migration0086 from "./Migrations/086_TurnItemPersistentOutput.ts";

const migrationEntries = [
[1, "OrchestrationEvents", Migration0001],
Expand Down Expand Up @@ -176,6 +177,7 @@ const migrationEntries = [
[83, "CommandReadIndexes", Migration0083],
[84, "LastExecutedRunIndex", Migration0084],
[85, "TurnItemCounts", Migration0085],
[86, "TurnItemPersistentOutput", Migration0086],
] 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,8 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

// Null means not yet derived; readers fall back to the payload predicate, so no backfill.
export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`ALTER TABLE orchestration_v2_projection_turn_items ADD COLUMN persistent_output INTEGER`;
});
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe("V2 preview upgrade", () => {
[83, "CommandReadIndexes"],
[84, "LastExecutedRunIndex"],
[85, "TurnItemCounts"],
[86, "TurnItemPersistentOutput"],
]);
assert.deepStrictEqual(yield* runMigrations(), []);
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
Expand Down Expand Up @@ -173,6 +174,7 @@ describe("V2 preview upgrade", () => {
[83, "CommandReadIndexes"],
[84, "LastExecutedRunIndex"],
[85, "TurnItemCounts"],
[86, "TurnItemPersistentOutput"],
]);
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
);
Expand Down
Loading