Skip to content
Merged
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
4 changes: 4 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7198,6 +7198,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
if (/^SEARCH (r|run) /.test(detail)) {
assert.include(detail, "COVERING INDEX", detail);
}
// A thread's item count reads one count row per hidden run, not one per run it had.
if (detail.startsWith("SEARCH counts ")) {
assert.include(detail, "run_key=?", detail);
}
if (detail.includes("orchestration_v2_projection_turn_items")) {
// Only held input is read from the sparse index; transcript history stays unread.
assert.include(
Expand Down
50 changes: 26 additions & 24 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8650,30 +8650,32 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =

const selectShellItemCounts = (threadIds: ReadonlyArray<ThreadId>) =>
sql<ShellItemCountRow>`
SELECT counts.thread_id,
SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back'
THEN counts.item_count - CASE
WHEN r.status IN ('queued', 'cancelled') THEN (
SELECT COUNT(*)
FROM orchestration_v2_projection_turn_items held
INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx
WHERE held.run_id = counts.run_key
AND held.type = 'user_message'
AND json_valid(held.payload_json)
AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn'
)
ELSE 0
END
ELSE 0 END) AS item_count,
SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END)
AS runless_item_count
FROM orchestration_v2_projection_turn_item_counts counts
-- The primary key would load each run's whole row just for its status.
LEFT JOIN orchestration_v2_projection_runs r
INDEXED BY orchestration_v2_projection_runs_run_status_idx
ON r.run_id = counts.run_key
WHERE counts.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))
GROUP BY counts.thread_id
-- Only the thread's hidden runs are visited, so settled history costs nothing here.
SELECT totals.thread_id,
totals.item_count - COALESCE((
SELECT SUM(CASE WHEN r.status = 'rolled_back' THEN counts.item_count ELSE (
SELECT COUNT(*)
FROM orchestration_v2_projection_turn_items held
INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx
WHERE held.run_id = counts.run_key
AND held.type = 'user_message'
AND json_valid(held.payload_json)
AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn'
) END)
FROM orchestration_v2_projection_runs r
INDEXED BY orchestration_v2_projection_runs_hidden_items_idx
JOIN orchestration_v2_projection_turn_item_counts counts
ON counts.thread_id = r.thread_id AND counts.run_key = r.run_id
WHERE r.thread_id = totals.thread_id
AND r.status IN ('rolled_back', 'queued', 'cancelled')
), 0) AS item_count,
COALESCE((
SELECT runless.item_count
FROM orchestration_v2_projection_turn_item_counts runless
WHERE runless.thread_id = totals.thread_id AND runless.run_key = ''
), 0) AS runless_item_count
FROM orchestration_v2_projection_thread_item_counts totals
WHERE totals.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))
`;

const selectShellProviderThreadRows = (threadIds?: ReadonlyArray<ThreadId>) =>
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 @@ -114,6 +114,7 @@ import Migration0103 from "./Migrations/103_ActiveShellIndexIndexes.ts";
import Migration0104 from "./Migrations/104_BackgroundRecoveryIndexes.ts";
import Migration0105 from "./Migrations/105_TurnStartReadIndexes.ts";
import Migration0106 from "./Migrations/106_DelegatedTaskReadIndexes.ts";
import Migration0107 from "./Migrations/107_ThreadItemCounts.ts";

const migrationEntries = [
[1, "OrchestrationEvents", Migration0001],
Expand Down Expand Up @@ -223,6 +224,7 @@ const migrationEntries = [
[104, "BackgroundRecoveryIndexes", Migration0104],
[105, "TurnStartReadIndexes", Migration0105],
[106, "DelegatedTaskReadIndexes", Migration0106],
[107, "ThreadItemCounts", Migration0107],
] 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,89 @@
import { assert, it } from "@effect/vitest";
import * as NodeSqliteClient from "@onicode/shared/nodeSqliteClient";
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

import { runMigrations } from "../Migrations.ts";

const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" }));

layer("107_ThreadItemCounts", (it) => {
it.effect("matches a recount of items without a run or with an existing run", () =>
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* runMigrations({ toMigrationInclusive: 106 });
const upsertItem = (id: string, threadId: string, runId: string | null) => sql`
INSERT INTO orchestration_v2_projection_turn_items (
turn_item_id, thread_id, run_id, ordinal, type, status, updated_at, payload_json
)
VALUES (
${id}, ${threadId}, ${runId}, 1, 'assistant_message', 'completed',
'2026-11-19T00:00:00.000Z', '{}'
)
ON CONFLICT(turn_item_id) DO UPDATE SET thread_id = excluded.thread_id, run_id = excluded.run_id
`;
const insertRun = (runId: string, threadId: string) => sql`
INSERT INTO orchestration_v2_projection_runs (
run_id, thread_id, ordinal, provider, status, requested_at, payload_json
)
VALUES (
${runId}, ${threadId},
(SELECT COUNT(*) FROM orchestration_v2_projection_runs WHERE thread_id = ${threadId}),
'codex', 'completed', '2026-11-19T00:00:00.000Z', '{}'
)
`;
const totals = () => sql<{ readonly thread_id: string; readonly item_count: number }>`
SELECT thread_id, item_count FROM orchestration_v2_projection_thread_item_counts
ORDER BY thread_id
`;
const recount = () => sql<{ readonly thread_id: string; readonly item_count: number }>`
SELECT item.thread_id, COUNT(*) AS item_count
FROM orchestration_v2_projection_turn_items item
WHERE item.run_id IS NULL
OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs run WHERE run.run_id = item.run_id)
GROUP BY item.thread_id
ORDER BY item.thread_id
`;
const assertTotals = (expected: ReadonlyArray<readonly [string, number]>) =>
Effect.gen(function* () {
const actual = yield* totals();
assert.deepEqual(actual, yield* recount());
assert.deepEqual(
actual.map((row) => [row.thread_id, row.item_count]),
expected.map(([threadId, count]) => [threadId, count]),
);
});

yield* insertRun("run-1", "thread-1");
yield* upsertItem("runless", "thread-1", null);
yield* upsertItem("run-1:a", "thread-1", "run-1");
yield* upsertItem("late:a", "thread-1", "run-late");
yield* runMigrations();
yield* assertTotals([["thread-1", 2]]);

yield* upsertItem("run-1:b", "thread-1", "run-1");
yield* upsertItem("late:b", "thread-2", "run-late");
yield* assertTotals([["thread-1", 3]]);

yield* insertRun("run-late", "thread-1");
yield* assertTotals([
["thread-1", 4],
["thread-2", 1],
]);

yield* upsertItem("run-1:b", "thread-2", null);
yield* upsertItem("late:a", "thread-1", "run-missing");
yield* assertTotals([
["thread-1", 2],
["thread-2", 2],
]);

yield* sql`DELETE FROM orchestration_v2_projection_runs WHERE run_id = 'run-late'`;
yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = 'run-1:b'`;
yield* assertTotals([["thread-1", 2]]);

yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE thread_id = 'thread-1'`;
yield* assertTotals([]);
}),
);
});
112 changes: 112 additions & 0 deletions apps/server/src/persistence/Migrations/107_ThreadItemCounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

/**
* One total per thread beside 091's per-run counts, so a shell read does not sum every run the
* thread has had. It holds the items without a run or with an existing run, the rows the shell
* read used to sum; the read still applies the run status rules. Triggers keep it exact.
*/
export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;

yield* sql`
CREATE TABLE orchestration_v2_projection_thread_item_counts (
thread_id TEXT PRIMARY KEY,
item_count INTEGER NOT NULL
) WITHOUT ROWID
`;
yield* sql`
INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count)
SELECT counts.thread_id, SUM(counts.item_count)
FROM orchestration_v2_projection_turn_item_counts counts
WHERE counts.run_key = ''
OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = counts.run_key)
GROUP BY counts.thread_id
`;

// The shell read subtracts only these runs. ProjectionStore.selectShellItemCounts must spell
// the same predicate for SQLite to use this partial index.
yield* sql`
CREATE INDEX IF NOT EXISTS orchestration_v2_projection_runs_hidden_items_idx
ON orchestration_v2_projection_runs(thread_id, status, run_id)
WHERE status IN ('rolled_back', 'queued', 'cancelled')
`;

// 091's triggers upsert per-run counts, so an item write fires the insert or the update here.
yield* sql`
CREATE TRIGGER orchestration_v2_thread_item_counts_count_insert
AFTER INSERT ON orchestration_v2_projection_turn_item_counts
WHEN new.run_key = ''
OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = new.run_key)
BEGIN
INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count)
VALUES (new.thread_id, new.item_count)
ON CONFLICT (thread_id) DO UPDATE SET item_count = item_count + excluded.item_count;
END
`;
yield* sql`
CREATE TRIGGER orchestration_v2_thread_item_counts_count_update
AFTER UPDATE ON orchestration_v2_projection_turn_item_counts
BEGIN
UPDATE orchestration_v2_projection_thread_item_counts
SET item_count = item_count - old.item_count
WHERE thread_id = old.thread_id
AND (old.run_key = ''
OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = old.run_key));
INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count)
SELECT new.thread_id, new.item_count
WHERE new.run_key = ''
OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = new.run_key)
ON CONFLICT (thread_id) DO UPDATE SET item_count = item_count + excluded.item_count;
DELETE FROM orchestration_v2_projection_thread_item_counts
WHERE thread_id = old.thread_id AND item_count <= 0;
END
`;
yield* sql`
CREATE TRIGGER orchestration_v2_thread_item_counts_count_delete
AFTER DELETE ON orchestration_v2_projection_turn_item_counts
WHEN old.run_key = ''
OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = old.run_key)
BEGIN
UPDATE orchestration_v2_projection_thread_item_counts
SET item_count = item_count - old.item_count
WHERE thread_id = old.thread_id;
DELETE FROM orchestration_v2_projection_thread_item_counts
WHERE thread_id = old.thread_id AND item_count <= 0;
END
`;

// Items can be written before their run; they count from when it exists.
yield* sql`
CREATE TRIGGER orchestration_v2_thread_item_counts_run_insert
AFTER INSERT ON orchestration_v2_projection_runs
BEGIN
INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count)
SELECT thread_id, COUNT(*)
FROM orchestration_v2_projection_turn_items
WHERE run_id = new.run_id
GROUP BY thread_id
ON CONFLICT (thread_id) DO UPDATE SET item_count = item_count + excluded.item_count;
END
`;
yield* sql`
CREATE TRIGGER orchestration_v2_thread_item_counts_run_delete
AFTER DELETE ON orchestration_v2_projection_runs
BEGIN
UPDATE orchestration_v2_projection_thread_item_counts
SET item_count = item_count - (
SELECT COUNT(*)
FROM orchestration_v2_projection_turn_items item
WHERE item.run_id = old.run_id
AND item.thread_id = orchestration_v2_projection_thread_item_counts.thread_id
)
WHERE thread_id IN (
SELECT thread_id FROM orchestration_v2_projection_turn_items WHERE run_id = old.run_id
);
DELETE FROM orchestration_v2_projection_thread_item_counts
WHERE item_count <= 0 AND thread_id IN (
SELECT thread_id FROM orchestration_v2_projection_turn_items WHERE run_id = old.run_id
);
END
`;
});
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe("V2 preview upgrade", () => {
[104, "BackgroundRecoveryIndexes"],
[105, "TurnStartReadIndexes"],
[106, "DelegatedTaskReadIndexes"],
[107, "ThreadItemCounts"],
]);
assert.deepStrictEqual(yield* runMigrations(), []);
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
Expand Down Expand Up @@ -215,6 +216,7 @@ describe("V2 preview upgrade", () => {
[104, "BackgroundRecoveryIndexes"],
[105, "TurnStartReadIndexes"],
[106, "DelegatedTaskReadIndexes"],
[107, "ThreadItemCounts"],
]);
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
);
Expand Down
5 changes: 3 additions & 2 deletions docs/internals/performance-regressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ sessions. The orchestrator still validates the current thread before applying se

Read receipts use `ProjectionStore.getThread` to load only canonical thread metadata. A visit
advances `lastVisitedAt` monotonically without changing `updatedAt` or decoding history. Shell
queries read item counts from a per-thread, per-run table that turn-item triggers keep exact, and
use a partial index for nonterminal background items, including idle items. Latest user-message
queries read item counts from a per-thread total that triggers keep exact, subtract only
rolled-back, queued and cancelled runs, and use a partial index for nonterminal background items,
including idle items. Latest user-message
reads use an index ordered by update time.

A projection read that orders messages by their first user or assistant turn item groups those
Expand Down
1 change: 1 addition & 0 deletions scripts/upstream-sync.ledger.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -2796,3 +2796,4 @@ babc83baf1 take fix(web): PR viewer renders GitHub :emoji: shortcodes (#39315)
6b6950adfe take taken as is (Android-only Kotlin; ReviewDiffGeometryTest passes)
57db750b8f take taken as is (Copilot ACP only)
cb391b5b57 skip relay AgentAwarenessRelay only, removed in Onicode; Onicode's push path retries failed activity sends every 60 s only while agents work, so a failed final completed update has the same gap: queued as a follow-up
2bba2564ca take migration renumbered to Onicode 107; comments name Onicode 091 (upstream 085, TurnItemCounts)