From 0391e57db99eef977ff03c41f874a3abd27665ab Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sat, 25 Jul 2026 10:11:48 +0200 Subject: [PATCH 01/11] chore: initialize upstream candidate registry --- .github/upstream-candidates.json | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .github/upstream-candidates.json diff --git a/.github/upstream-candidates.json b/.github/upstream-candidates.json new file mode 100644 index 00000000000..134181c3f2e --- /dev/null +++ b/.github/upstream-candidates.json @@ -0,0 +1,4 @@ +{ + "version": 1, + "candidates": [] +} From a5c36b1a4629b63bc5fedac0a06ee754ed369690 Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sat, 25 Jul 2026 10:38:42 +0200 Subject: [PATCH 02/11] perf: import bounded web thread history (upstream #4018) (#29) Source: pingdotgg/t3code#4018 Source SHA: de8fd65934768173819b93adcd6b92af3e8c7fc3 Imported: bounded server activity snapshots, cursor pagination, lazy web history loading, reconnect-safe reset/dedup, and disabled eager browser sidebar hydration. Adapted: preserved Tim thread lifecycle handling and Omega composer/minimap behavior while resolving current-stack conflicts. Excluded: none of the source PR behavior; native mobile pagination remains separate because #4018 intentionally excludes it. --- .github/upstream-candidates.json | 9 +- .../checkpointing/CheckpointDiffQuery.test.ts | 10 + apps/server/src/cli/project.ts | 4 +- .../Layers/OrchestrationEngine.test.ts | 1 + .../Layers/ProjectionSnapshotQuery.test.ts | 251 ++++++++++++++++++ .../Layers/ProjectionSnapshotQuery.ts | 209 +++++++++++++-- .../Services/ProjectionSnapshotQuery.ts | 12 + apps/server/src/orchestration/http.ts | 6 +- .../project/ProjectSetupScriptRunner.test.ts | 1 + .../Layers/ProviderSessionReaper.test.ts | 1 + apps/server/src/serverRuntimeStartup.test.ts | 4 + apps/server/src/ws.ts | 16 ++ apps/web/src/components/ChatView.tsx | 180 ++++++++++++- apps/web/src/components/Sidebar.tsx | 6 +- .../chat/MessagesTimeline.logic.test.ts | 88 ++++++ .../components/chat/MessagesTimeline.logic.ts | 45 ++++ .../components/chat/MessagesTimeline.test.tsx | 33 +++ .../src/components/chat/MessagesTimeline.tsx | 96 ++++++- packages/client-runtime/package.json | 4 + .../client-runtime/src/state/orchestration.ts | 7 +- .../src/state/threadReducer.test.ts | 77 +++++- .../client-runtime/src/state/threadReducer.ts | 39 +++ packages/contracts/src/orchestration.ts | 48 ++++ packages/contracts/src/rpc.ts | 12 + 24 files changed, 1114 insertions(+), 45 deletions(-) diff --git a/.github/upstream-candidates.json b/.github/upstream-candidates.json index 134181c3f2e..b68afc54b75 100644 --- a/.github/upstream-candidates.json +++ b/.github/upstream-candidates.json @@ -1,4 +1,11 @@ { "version": 1, - "candidates": [] + "candidates": [ + { + "upstreamPr": 4018, + "sourceSha": "de8fd65934768173819b93adcd6b92af3e8c7fc3", + "status": "active", + "purpose": "Bound server thread history and lazily page older web activity" + } + ] } diff --git a/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts b/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts index 42e3b7d1624..907796be53d 100644 --- a/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts +++ b/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts @@ -77,6 +77,8 @@ describe("CheckpointDiffQuery.layer", () => { Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, { getCommandReadModel: () => Effect.die("CheckpointDiffQuery should not request the command read model"), + getThreadActivitiesPage: () => + Effect.die("CheckpointDiffQuery should not request thread activities"), getSnapshot: () => Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"), getShellSnapshot: () => @@ -187,6 +189,8 @@ describe("CheckpointDiffQuery.layer", () => { Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, { getCommandReadModel: () => Effect.die("CheckpointDiffQuery should not request the command read model"), + getThreadActivitiesPage: () => + Effect.die("CheckpointDiffQuery should not request thread activities"), getSnapshot: () => Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"), getShellSnapshot: () => @@ -272,6 +276,8 @@ describe("CheckpointDiffQuery.layer", () => { Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, { getCommandReadModel: () => Effect.die("CheckpointDiffQuery should not request the command read model"), + getThreadActivitiesPage: () => + Effect.die("CheckpointDiffQuery should not request thread activities"), getSnapshot: () => Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"), getShellSnapshot: () => @@ -342,6 +348,8 @@ describe("CheckpointDiffQuery.layer", () => { Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, { getCommandReadModel: () => Effect.die("CheckpointDiffQuery should not request the command read model"), + getThreadActivitiesPage: () => + Effect.die("CheckpointDiffQuery should not request thread activities"), getSnapshot: () => Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"), getShellSnapshot: () => @@ -397,6 +405,8 @@ describe("CheckpointDiffQuery.layer", () => { Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, { getCommandReadModel: () => Effect.die("CheckpointDiffQuery should not request the command read model"), + getThreadActivitiesPage: () => + Effect.die("CheckpointDiffQuery should not request thread activities"), getSnapshot: () => Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"), getShellSnapshot: () => diff --git a/apps/server/src/cli/project.ts b/apps/server/src/cli/project.ts index 25733a5e35b..8b7acdc3103 100644 --- a/apps/server/src/cli/project.ts +++ b/apps/server/src/cli/project.ts @@ -337,7 +337,9 @@ const dispatchLiveOrchestrationCommand = ( const getOfflineSnapshot = Effect.fn("getOfflineSnapshot")(function* () { const projectionSnapshotQuery = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery; - return yield* projectionSnapshotQuery.getSnapshot(); + // Project resolution only reads `.projects`; the command read model returns the + // same shape without loading the heavy per-thread activity/message tables. + return yield* projectionSnapshotQuery.getCommandReadModel(); }); const tryResolveLiveProjectExecutionMode = Effect.fn("tryResolveLiveProjectExecutionMode")( diff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts index 089b4e0e3a0..bee749e80bb 100644 --- a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts +++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts @@ -204,6 +204,7 @@ describe("OrchestrationEngine", () => { getSessionStopContextById: () => Effect.succeed(Option.none()), getThreadDetailById: () => Effect.succeed(Option.none()), getThreadDetailSnapshot: () => Effect.succeed(Option.none()), + getThreadActivitiesPage: () => Effect.die("unused"), getThreadLifecycleById: () => Effect.succeed(Option.none()), }), ), diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts index a05ada69b3d..843ebb3f626 100644 --- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts +++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts @@ -347,6 +347,7 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => { createdAt: "2026-02-24T00:00:06.000Z", }, ], + hasMoreActivities: false, checkpoints: [ { turnId: asTurnId("turn-1"), @@ -1331,6 +1332,8 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => { assert.equal(threadDetail._tag, "Some"); if (threadDetail._tag === "Some") { assert.deepEqual(threadDetail.value.activities, snapshot.threads[0]?.activities ?? []); + // Well under the window — nothing older to lazy-load. + assert.equal(threadDetail.value.hasMoreActivities, false); } assert.deepEqual(snapshot.threads[0]?.activities ?? [], [ @@ -1510,6 +1513,254 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => { }), ); + it.effect( + "windows thread-detail activities to the most recent 500 and pages older on demand", + () => + Effect.gen(function* () { + const snapshotQuery = yield* ProjectionSnapshotQuery; + const sql = yield* SqlClient.SqlClient; + + yield* sql`DELETE FROM projection_projects`; + yield* sql`DELETE FROM projection_threads`; + yield* sql`DELETE FROM projection_thread_activities`; + yield* sql`DELETE FROM projection_state`; + + yield* sql` + INSERT INTO projection_projects ( + project_id, title, workspace_root, default_model_selection_json, + scripts_json, created_at, updated_at, deleted_at + ) + VALUES ( + 'project-1', 'Project 1', '/tmp/project-1', + '{"provider":"codex","model":"gpt-5-codex"}', '[]', + '2026-04-01T00:00:00.000Z', '2026-04-01T00:00:01.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, + latest_user_message_at, pending_approval_count, pending_user_input_count, + has_actionable_proposed_plan, created_at, updated_at, archived_at, deleted_at + ) + VALUES ( + 'thread-1', 'project-1', 'Thread 1', + '{"provider":"codex","model":"gpt-5-codex"}', 'full-access', 'default', + NULL, NULL, NULL, NULL, 0, 0, 0, + '2026-04-01T00:00:02.000Z', '2026-04-01T00:00:03.000Z', NULL, NULL + ) + `; + + // 600 activities (sequence 1..600); the detail load must return only the + // most recent 500 (sequence 101..600), re-sorted ascending for display. + const total = 600; + yield* Effect.forEach( + Array.from({ length: total }, (_unused, index) => index + 1), + (seq) => + sql` + INSERT INTO projection_thread_activities ( + activity_id, thread_id, turn_id, tone, kind, summary, payload_json, + sequence, created_at + ) + VALUES ( + ${`activity-${String(seq).padStart(4, "0")}`}, 'thread-1', NULL, + 'info', 'runtime.note', ${`act-${seq}`}, '{}', ${seq}, + '2026-04-01T00:01:00.000Z' + ) + `, + { discard: true }, + ); + + const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1")); + assert.equal(threadDetail._tag, "Some"); + if (threadDetail._tag === "Some") { + const activities = threadDetail.value.activities; + assert.equal(activities.length, 500); + assert.equal(activities[0]?.summary, "act-101"); + assert.equal(activities[0]?.sequence, 101); + assert.equal(activities.at(-1)?.summary, "act-600"); + // 600 > window, so the client is told older history can be lazy-loaded. + assert.equal(threadDetail.value.hasMoreActivities, true); + } + + // Lazy-load the page immediately older than the windowed view (cursor = + // oldest loaded sequence, 101): sequences 1..100, ascending, no more left. + const olderPage = yield* snapshotQuery.getThreadActivitiesPage({ + threadId: ThreadId.make("thread-1"), + beforeSequence: 101, + limit: 500, + }); + assert.equal(olderPage.activities.length, 100); + assert.equal(olderPage.activities[0]?.summary, "act-1"); + assert.equal(olderPage.activities.at(-1)?.summary, "act-100"); + assert.equal(olderPage.hasMore, false); + + // A bounded page returns the newest `limit` of the older set and reports + // that more remain (sequences 401..600, with 1..400 still older). + const boundedPage = yield* snapshotQuery.getThreadActivitiesPage({ + threadId: ThreadId.make("thread-1"), + beforeSequence: 601, + limit: 200, + }); + assert.equal(boundedPage.activities.length, 200); + assert.equal(boundedPage.activities[0]?.summary, "act-401"); + assert.equal(boundedPage.activities.at(-1)?.summary, "act-600"); + assert.equal(boundedPage.hasMore, true); + + yield* sql`DELETE FROM projection_thread_activities`; + + // Legacy rows may not have a sequence. They are still windowed in the + // detail load and must remain pageable by the deterministic created/id + // ordering used by the snapshot query. + yield* Effect.forEach( + Array.from({ length: total }, (_unused, index) => index + 1), + (seq) => + sql` + INSERT INTO projection_thread_activities ( + activity_id, thread_id, turn_id, tone, kind, summary, payload_json, + sequence, created_at + ) + VALUES ( + ${`unsequenced-${String(seq).padStart(4, "0")}`}, 'thread-1', NULL, + 'info', 'runtime.note', ${`legacy-act-${seq}`}, '{}', NULL, + '2026-04-01T00:01:00.000Z' + ) + `, + { discard: true }, + ); + + const legacyThreadDetail = yield* snapshotQuery.getThreadDetailById( + ThreadId.make("thread-1"), + ); + assert.equal(legacyThreadDetail._tag, "Some"); + if (legacyThreadDetail._tag === "Some") { + const activities = legacyThreadDetail.value.activities; + assert.equal(activities.length, 500); + assert.equal(activities[0]?.summary, "legacy-act-101"); + assert.equal(activities[0]?.sequence, undefined); + assert.equal(activities.at(-1)?.summary, "legacy-act-600"); + + const legacyOlderPage = yield* snapshotQuery.getThreadActivitiesPage({ + threadId: ThreadId.make("thread-1"), + beforeCreatedAt: activities[0]?.createdAt ?? "2026-04-01T00:01:00.000Z", + beforeActivityId: activities[0]?.id ?? asEventId("unsequenced-0101"), + limit: 500, + }); + assert.equal(legacyOlderPage.activities.length, 100); + assert.equal(legacyOlderPage.activities[0]?.summary, "legacy-act-1"); + assert.equal(legacyOlderPage.activities.at(-1)?.summary, "legacy-act-100"); + assert.equal(legacyOlderPage.hasMore, false); + } + + const legacyBoundedPage = yield* snapshotQuery.getThreadActivitiesPage({ + threadId: ThreadId.make("thread-1"), + beforeCreatedAt: "2026-04-01T00:01:00.000Z", + beforeActivityId: asEventId("unsequenced-0601"), + limit: 200, + }); + assert.equal(legacyBoundedPage.activities.length, 200); + assert.equal(legacyBoundedPage.activities[0]?.summary, "legacy-act-401"); + assert.equal(legacyBoundedPage.activities.at(-1)?.summary, "legacy-act-600"); + assert.equal(legacyBoundedPage.hasMore, true); + }), + ); + + it.effect("unsequenced cursor reaches all older rows without stranding sequenced ones", () => + // Regression for the "unsequenced cursor hides sequenced history" concern: + // sequenced rows always sort newer than NULL-sequence (legacy) rows, so when + // the oldest loaded row is unsequenced every sequenced row is already in the + // window — the `sequence IS NULL` cursor can't strand sequenced rows. + Effect.gen(function* () { + const snapshotQuery = yield* ProjectionSnapshotQuery; + const sql = yield* SqlClient.SqlClient; + yield* sql`DELETE FROM projection_projects`; + yield* sql`DELETE FROM projection_threads`; + yield* sql`DELETE FROM projection_thread_activities`; + yield* sql`DELETE FROM projection_state`; + yield* sql` + INSERT INTO projection_projects ( + project_id, title, workspace_root, default_model_selection_json, + scripts_json, created_at, updated_at, deleted_at + ) VALUES ( + 'project-1', 'Project 1', '/tmp/project-1', + '{"provider":"codex","model":"gpt-5-codex"}', '[]', + '2026-04-01T00:00:00.000Z', '2026-04-01T00:00:01.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, + latest_user_message_at, pending_approval_count, pending_user_input_count, + has_actionable_proposed_plan, created_at, updated_at, archived_at, deleted_at + ) VALUES ( + 'thread-1', 'project-1', 'Thread 1', + '{"provider":"codex","model":"gpt-5-codex"}', 'full-access', 'default', + NULL, NULL, NULL, NULL, 0, 0, 0, + '2026-04-01T00:00:02.000Z', '2026-04-01T00:00:03.000Z', NULL, NULL + ) + `; + // 600 legacy unsequenced rows (older) + 3 sequenced rows (newer). The + // window keeps the 3 sequenced + the most-recent 497 unsequenced, so the + // oldest loaded row is unsequenced and 103 older unsequenced remain. + yield* Effect.forEach( + Array.from({ length: 600 }, (_u, index) => index + 1), + (n) => + sql` + INSERT INTO projection_thread_activities ( + activity_id, thread_id, turn_id, tone, kind, summary, payload_json, + sequence, created_at + ) VALUES ( + ${`unseq-${String(n).padStart(4, "0")}`}, 'thread-1', NULL, + 'info', 'runtime.note', ${`unseq-${n}`}, '{}', NULL, + ${`2026-04-01T00:00:01.${String(n).padStart(3, "0")}Z`} + ) + `, + { discard: true }, + ); + yield* Effect.forEach( + [1, 2, 3], + (seq) => + sql` + INSERT INTO projection_thread_activities ( + activity_id, thread_id, turn_id, tone, kind, summary, payload_json, + sequence, created_at + ) VALUES ( + ${`seq-${seq}`}, 'thread-1', NULL, 'info', 'runtime.note', + ${`seq-${seq}`}, '{}', ${seq}, ${`2026-04-01T09:00:0${seq}.000Z`} + ) + `, + { discard: true }, + ); + + const detail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1")); + assert.equal(detail._tag, "Some"); + if (detail._tag !== "Some") return; + const windowed = detail.value.activities; + assert.equal(windowed.length, 500); + // Sequenced rows are the newest (end of the ascending window); the oldest + // loaded row is unsequenced — exactly the case the concern is about. + assert.equal(windowed.at(-1)?.summary, "seq-3"); + assert.equal(windowed[0]?.sequence, undefined); + + // The client pages with the unsequenced cursor of the oldest loaded row. + const oldest = windowed[0]; + assert.ok(oldest); + const olderPage = yield* snapshotQuery.getThreadActivitiesPage({ + threadId: ThreadId.make("thread-1"), + beforeCreatedAt: oldest.createdAt, + beforeActivityId: oldest.id, + limit: 500, + }); + // The 103 older unsequenced rows come back, none are sequenced, and no + // sequenced row was stranded (all 3 are already in the window). + assert.equal(olderPage.activities.length, 103); + assert.equal(olderPage.hasMore, false); + assert.ok(olderPage.activities.every((a) => a.sequence === undefined)); + }), + ); + it.effect("uses projection_threads.latest_turn_id for bulk command and shell snapshots", () => Effect.gen(function* () { const snapshotQuery = yield* ProjectionSnapshotQuery; diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts index 41e52e5bca9..01e8edefa01 100644 --- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts +++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts @@ -1,6 +1,7 @@ import { ChatAttachment, CheckpointRef, + EventId, IsoDateTime, MessageId, NonNegativeInt, @@ -118,6 +119,31 @@ const ProjectIdLookupInput = Schema.Struct({ const ThreadIdLookupInput = Schema.Struct({ threadId: ThreadId, }); + +/** + * Maximum number of most-recent activities loaded into a thread-detail snapshot. + * Bounds peak memory when opening a long-lived thread; older activities are + * fetched on demand (lazy-load, planned) and live ones stream in via events. + */ +const THREAD_DETAIL_ACTIVITY_WINDOW = 500; + +// `beforeSequence`/`limit` are NonNegativeInt (not bare Number) to match the +// contract: the WHERE clause `(sequence < beforeSequence OR sequence IS NULL)` +// is only equivalent to the old `COALESCE(sequence, -1) < beforeSequence` when +// `beforeSequence` is non-negative — a negative cursor would silently match no +// sequenced rows and return only unsequenced ones. Validating here (not just at +// the RPC boundary) keeps any future non-RPC caller honest. +const ThreadActivitiesBeforeSequenceInput = Schema.Struct({ + threadId: ThreadId, + beforeSequence: NonNegativeInt, + limit: NonNegativeInt, +}); +const ThreadActivitiesBeforeActivityInput = Schema.Struct({ + threadId: ThreadId, + beforeCreatedAt: IsoDateTime, + beforeActivityId: EventId, + limit: NonNegativeInt, +}); const ProjectionProjectLookupRowSchema = ProjectionProjectDbRowSchema; const ProjectionThreadIdLookupRowSchema = Schema.Struct({ threadId: ThreadId, @@ -255,6 +281,23 @@ function mapProposedPlanRow( }; } +function mapThreadActivityRow( + row: Schema.Schema.Type, +): OrchestrationThreadActivity { + const activity = { + id: row.activityId, + tone: row.tone, + kind: row.kind, + summary: row.summary, + payload: row.payload, + turnId: row.turnId, + createdAt: row.createdAt, + }; + // `sequence` is the pagination cursor; omit it when the (legacy) row is + // unsequenced so the optional contract field stays absent. + return row.sequence !== null ? Object.assign(activity, { sequence: row.sequence }) : activity; +} + function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) { return (cause: unknown): ProjectionRepositoryError => Schema.isSchemaError(cause) @@ -856,6 +899,11 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { `, }); + // The thread-detail timeline loads only the most recent window of activities so a + // long-lived thread (tens of thousands of activities, hundreds of MB of payload) + // can't blow the server heap. New activities still stream in live via the event + // subscription; older history will be fetched on demand once lazy-load lands. + // Select the most recent N (sequence DESC) then re-sort ascending for display. const listThreadActivityRowsByThread = SqlSchema.findAll({ Request: ThreadIdLookupInput, Result: ProjectionThreadActivityDbRowSchema, @@ -871,8 +919,17 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { payload_json AS "payload", sequence, created_at AS "createdAt" - FROM projection_thread_activities - WHERE thread_id = ${threadId} + FROM ( + SELECT * + FROM projection_thread_activities + WHERE thread_id = ${threadId} + ORDER BY + sequence DESC, + created_at DESC, + activity_id DESC + -- One extra beyond the window so the caller can report hasMoreActivities. + LIMIT ${THREAD_DETAIL_ACTIVITY_WINDOW + 1} + ) ORDER BY sequence ASC, created_at ASC, @@ -880,6 +937,81 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { `, }); + // Older-than-cursor page for lazy-load. Returns rows newest-first (DESC) so a + // simple LIMIT yields the page adjacent to the cursor; the caller reverses to + // ascending. `sequence IS NULL` (legacy unsequenced) rows sort last under + // `sequence DESC` (SQLite orders NULLs last in DESC) — the very oldest — so + // paging eventually reaches them. `beforeSequence` is a NonNegativeInt, so + // `(sequence < beforeSequence OR sequence IS NULL)` is equivalent to the old + // `COALESCE(sequence, -1) < beforeSequence` but lets the + // (thread_id, sequence, created_at, activity_id) index satisfy the ORDER BY + // directly instead of forcing a filesort over the whole thread. + const listThreadActivityRowsBeforeSequence = SqlSchema.findAll({ + Request: ThreadActivitiesBeforeSequenceInput, + Result: ProjectionThreadActivityDbRowSchema, + execute: ({ threadId, beforeSequence, limit }) => + sql` + SELECT + activity_id AS "activityId", + thread_id AS "threadId", + turn_id AS "turnId", + tone, + kind, + summary, + payload_json AS "payload", + sequence, + created_at AS "createdAt" + FROM projection_thread_activities + WHERE thread_id = ${threadId} + AND (sequence < ${beforeSequence} OR sequence IS NULL) + ORDER BY + sequence DESC, + created_at DESC, + activity_id DESC + LIMIT ${limit} + `, + }); + + // Legacy unsequenced (sequence NULL) rows are paged by a (created_at, + // activity_id) cursor. created_at is compared lexicographically as TEXT, which + // equals chronological order only because timestamps are canonical ISO-8601 + // (always UTC `Z`, fixed millisecond precision) — the same invariant every + // `ORDER BY created_at` in this layer (including the detail window above) + // already relies on, so the cursor stays consistent with how rows are + // displayed. activity_id breaks created_at ties; its ordering is arbitrary but + // matches the window's `activity_id` tiebreak, so pages never skip or repeat. + const listUnsequencedThreadActivityRowsBeforeActivity = SqlSchema.findAll({ + Request: ThreadActivitiesBeforeActivityInput, + Result: ProjectionThreadActivityDbRowSchema, + execute: ({ threadId, beforeCreatedAt, beforeActivityId, limit }) => + sql` + SELECT + activity_id AS "activityId", + thread_id AS "threadId", + turn_id AS "turnId", + tone, + kind, + summary, + payload_json AS "payload", + sequence, + created_at AS "createdAt" + FROM projection_thread_activities + WHERE thread_id = ${threadId} + AND sequence IS NULL + AND ( + created_at < ${beforeCreatedAt} + OR ( + created_at = ${beforeCreatedAt} + AND activity_id < ${beforeActivityId} + ) + ) + ORDER BY + created_at DESC, + activity_id DESC + LIMIT ${limit} + `, + }); + const getThreadSessionRowByThread = SqlSchema.findOneOption({ Request: ThreadIdLookupInput, Result: ProjectionThreadSessionDbRowSchema, @@ -1124,16 +1256,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { for (const row of activityRows) { updatedAt = maxIso(updatedAt, row.createdAt); const threadActivities = activitiesByThread.get(row.threadId) ?? []; - threadActivities.push({ - id: row.activityId, - tone: row.tone, - kind: row.kind, - summary: row.summary, - payload: row.payload, - turnId: row.turnId, - ...(row.sequence !== null ? { sequence: row.sequence } : {}), - createdAt: row.createdAt, - }); + threadActivities.push(mapThreadActivityRow(row)); activitiesByThread.set(row.threadId, threadActivities); } @@ -1242,6 +1365,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { messages: messagesByThread.get(row.threadId) ?? [], proposedPlans: proposedPlansByThread.get(row.threadId) ?? [], activities: activitiesByThread.get(row.threadId) ?? [], + // The full snapshot is unwindowed, so there is never more to load. + hasMoreActivities: false, checkpoints: checkpointsByThread.get(row.threadId) ?? [], session: sessionsByThread.get(row.threadId) ?? null, })); @@ -2101,21 +2226,14 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { return message; }), proposedPlans: proposedPlanRows.map(mapProposedPlanRow), - activities: activityRows.map((row) => { - const activity = { - id: row.activityId, - tone: row.tone, - kind: row.kind, - summary: row.summary, - payload: row.payload, - turnId: row.turnId, - createdAt: row.createdAt, - }; - if (row.sequence !== null) { - return Object.assign(activity, { sequence: row.sequence }); - } - return activity; - }), + // The query fetches WINDOW+1 ascending rows; if it returned the extra + // one, older activities exist beyond the window — drop that oldest row + // and flag it so clients can lazy-load older history. + activities: (activityRows.length > THREAD_DETAIL_ACTIVITY_WINDOW + ? activityRows.slice(activityRows.length - THREAD_DETAIL_ACTIVITY_WINDOW) + : activityRows + ).map(mapThreadActivityRow), + hasMoreActivities: activityRows.length > THREAD_DETAIL_ACTIVITY_WINDOW, checkpoints: checkpointRows.map((row) => ({ turnId: row.turnId, checkpointTurnCount: row.checkpointTurnCount, @@ -2177,6 +2295,42 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { ), ), ); + const getThreadActivitiesPage: ProjectionSnapshotQueryShape["getThreadActivitiesPage"] = ( + input, + ) => + Effect.gen(function* () { + const limit = Math.min( + Math.max(1, input.limit ?? THREAD_DETAIL_ACTIVITY_WINDOW), + THREAD_DETAIL_ACTIVITY_WINDOW, + ); + // Fetch one extra to detect whether older activities remain. + const rowsEffect = + "beforeSequence" in input + ? listThreadActivityRowsBeforeSequence({ + threadId: input.threadId, + beforeSequence: input.beforeSequence, + limit: limit + 1, + }) + : listUnsequencedThreadActivityRowsBeforeActivity({ + threadId: input.threadId, + beforeCreatedAt: input.beforeCreatedAt, + beforeActivityId: input.beforeActivityId, + limit: limit + 1, + }); + const rows = yield* rowsEffect.pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getThreadActivitiesPage:query", + "ProjectionSnapshotQuery.getThreadActivitiesPage:decodeRows", + ), + ), + ); + const hasMore = rows.length > limit; + // Rows are newest-first; keep the page closest to the cursor, then reverse + // to ascending for display. + const page = (hasMore ? rows.slice(0, limit) : rows).map(mapThreadActivityRow).toReversed(); + return { activities: page, hasMore }; + }); return { getCommandReadModel, @@ -2195,6 +2349,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { getThreadDetailById, getThreadLifecycleById, getThreadDetailSnapshot, + getThreadActivitiesPage, } satisfies ProjectionSnapshotQueryShape; }); diff --git a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts index 06bef064d33..de0c0deea5d 100644 --- a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts +++ b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts @@ -9,6 +9,8 @@ import type { CheckpointRef, OrchestrationCheckpointSummary, + OrchestrationGetThreadActivitiesInput, + OrchestrationGetThreadActivitiesResult, OrchestrationProject, OrchestrationProjectShell, OrchestrationReadModel, @@ -187,6 +189,16 @@ export interface ProjectionSnapshotQueryShape { threadId: ThreadId, ) => Effect.Effect, ProjectionRepositoryError>; + /** + * Cursor-paginated load of a thread's older activities (lazy-load / infinite + * scroll). Returns the page of activities immediately older than the provided + * sequence or unsequenced activity cursor, ascending, plus whether older ones + * remain. + */ + readonly getThreadActivitiesPage: ( + input: OrchestrationGetThreadActivitiesInput, + ) => Effect.Effect; + /** * Read a thread's lifecycle markers regardless of its deleted/archived * state. Lets callers that got no active row distinguish a thread that was diff --git a/apps/server/src/orchestration/http.ts b/apps/server/src/orchestration/http.ts index 016c3d508ec..f57aa7d5711 100644 --- a/apps/server/src/orchestration/http.ts +++ b/apps/server/src/orchestration/http.ts @@ -31,8 +31,12 @@ export const orchestrationHttpApiLayer = HttpApiBuilder.group( Effect.fn("environment.orchestration.snapshot")(function* (args) { yield* annotateEnvironmentRequest(args.endpoint.name); yield* requireEnvironmentScope(AuthOrchestrationReadScope); + // The only consumer (the `t3` CLI project resolver) reads just + // `.projects`, so use the command read model — it returns the same + // OrchestrationReadModel shape but never materialises the per-thread + // activity/message/checkpoint tables (490MB+ on a busy DB → heap OOM). return yield* projectionSnapshotQuery - .getSnapshot() + .getCommandReadModel() .pipe( Effect.catch((cause) => failEnvironmentInternal("orchestration_snapshot_failed", cause), diff --git a/apps/server/src/project/ProjectSetupScriptRunner.test.ts b/apps/server/src/project/ProjectSetupScriptRunner.test.ts index 1d94f1a0025..7082878c562 100644 --- a/apps/server/src/project/ProjectSetupScriptRunner.test.ts +++ b/apps/server/src/project/ProjectSetupScriptRunner.test.ts @@ -27,6 +27,7 @@ const makeProject = (scripts: OrchestrationProject["scripts"]): OrchestrationPro const makeProjectionSnapshotQueryLayer = (project: OrchestrationProject) => Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, { getCommandReadModel: () => Effect.die("unused"), + getThreadActivitiesPage: () => Effect.die("unused"), getSnapshot: () => Effect.die("unused"), getShellSnapshot: () => Effect.die("unused"), getArchivedShellSnapshot: () => Effect.die("unused"), diff --git a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts index 621503a7e65..e59521df49d 100644 --- a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts +++ b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts @@ -213,6 +213,7 @@ describe("ProviderSessionReaper", () => { ), getThreadDetailById: () => Effect.die("unused"), getThreadDetailSnapshot: () => Effect.die("unused"), + getThreadActivitiesPage: () => Effect.die("unused"), getThreadLifecycleById: () => Effect.die("unused"), }), ), diff --git a/apps/server/src/serverRuntimeStartup.test.ts b/apps/server/src/serverRuntimeStartup.test.ts index 4f50e6cfa21..710dcb228c5 100644 --- a/apps/server/src/serverRuntimeStartup.test.ts +++ b/apps/server/src/serverRuntimeStartup.test.ts @@ -98,6 +98,7 @@ it.effect("launchStartupHeartbeat does not block the caller while counts are loa getThreadShellById: () => Effect.succeed(Option.none()), getThreadDetailById: () => Effect.succeed(Option.none()), getThreadDetailSnapshot: () => Effect.succeed(Option.none()), + getThreadActivitiesPage: () => Effect.die("unused"), getThreadLifecycleById: () => Effect.succeed(Option.none()), }), Effect.provideService(AnalyticsService.AnalyticsService, { @@ -163,6 +164,7 @@ it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and threa getThreadShellById: () => Effect.die("unused"), getThreadDetailById: () => Effect.die("unused"), getThreadDetailSnapshot: () => Effect.die("unused"), + getThreadActivitiesPage: () => Effect.die("unused"), getThreadLifecycleById: () => Effect.die("unused"), }), Effect.provideService(OrchestrationEngine.OrchestrationEngineService, { @@ -209,6 +211,7 @@ it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when getThreadShellById: () => Effect.die("unused"), getThreadDetailById: () => Effect.die("unused"), getThreadDetailSnapshot: () => Effect.die("unused"), + getThreadActivitiesPage: () => Effect.die("unused"), getThreadLifecycleById: () => Effect.die("unused"), }), Effect.provideService(OrchestrationEngine.OrchestrationEngineService, { @@ -261,6 +264,7 @@ it.effect("resolveAutoBootstrapWelcomeTargets preserves typed UUID generation fa getThreadShellById: () => Effect.die("unused"), getThreadDetailById: () => Effect.die("unused"), getThreadDetailSnapshot: () => Effect.die("unused"), + getThreadActivitiesPage: () => Effect.die("unused"), getThreadLifecycleById: () => Effect.die("unused"), }), Effect.provideService(OrchestrationEngine.OrchestrationEngineService, { diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 51b20adbb10..d47ef6f3549 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -34,6 +34,7 @@ import { type OrchestrationThreadStreamItem, OrchestrationGetFullThreadDiffError, OrchestrationGetSnapshotError, + OrchestrationGetThreadActivitiesError, OrchestrationGetTurnDiffError, ORCHESTRATION_WS_METHODS, type ProjectId, @@ -290,6 +291,7 @@ const SHELL_RESUME_MAX_GAP = 1_000; const RPC_REQUIRED_SCOPE = new Map([ [ORCHESTRATION_WS_METHODS.dispatchCommand, AuthOrchestrationOperateScope], [ORCHESTRATION_WS_METHODS.getTurnDiff, AuthOrchestrationReadScope], + [ORCHESTRATION_WS_METHODS.getThreadActivities, AuthOrchestrationReadScope], [ORCHESTRATION_WS_METHODS.getFullThreadDiff, AuthOrchestrationReadScope], [ORCHESTRATION_WS_METHODS.replayEvents, AuthOrchestrationReadScope], [ORCHESTRATION_WS_METHODS.subscribeShell, AuthOrchestrationReadScope], @@ -1231,6 +1233,20 @@ const makeWsRpcLayer = ( ), { "rpc.aggregate": "orchestration" }, ), + [ORCHESTRATION_WS_METHODS.getThreadActivities]: (input) => + observeRpcEffect( + ORCHESTRATION_WS_METHODS.getThreadActivities, + projectionSnapshotQuery.getThreadActivitiesPage(input).pipe( + Effect.mapError( + (cause) => + new OrchestrationGetThreadActivitiesError({ + message: "Failed to load thread activities page", + cause, + }), + ), + ), + { "rpc.aggregate": "orchestration" }, + ), [ORCHESTRATION_WS_METHODS.getFullThreadDiff]: (input) => observeRpcEffect( ORCHESTRATION_WS_METHODS.getFullThreadDiff, diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 2ab2f2ffdbb..57d6b3edd1f 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -64,6 +64,10 @@ import { squashAtomCommandFailure, type AtomCommandResult, } from "@t3tools/client-runtime/state/runtime"; +import { + liveWindowOldestActivityId, + oldestActivityByChronology, +} from "@t3tools/client-runtime/state/thread-reducer"; import * as Cause from "effect/Cause"; import { AsyncResult } from "effect/unstable/reactivity"; import { isElectron } from "../env"; @@ -202,6 +206,7 @@ import { primaryServerSettingsAtom, serverEnvironment, } from "../state/server"; +import { orchestrationEnvironment } from "../state/orchestration"; import { terminalEnvironment } from "../state/terminal"; import { threadEnvironment } from "../state/threads"; import { vcsEnvironment } from "../state/vcs"; @@ -1930,7 +1935,170 @@ function ChatViewContent(props: ChatViewProps) { ); const selectedProvider: ProviderDriverKind = lockedProvider ?? unlockedSelectedProvider; const phase = derivePhase(activeThread?.session ?? null); - const threadActivities = activeThread?.activities ?? EMPTY_ACTIVITIES; + + // ── Older-history lazy-load ──────────────────────────────────────────────── + // The detail snapshot windows activities to the most recent page (the server + // sets `hasMoreActivities` when older ones exist); older pages are fetched on + // demand (infinite scroll-up) and prepended. Messages aren't windowed + // server-side, so this just back-fills the older tool activity. + const [olderActivities, setOlderActivities] = useState< + ReadonlyArray + >([]); + const [olderLoaded, setOlderLoaded] = useState(false); + const [olderHasMore, setOlderHasMore] = useState(false); + const [loadingOlderActivities, setLoadingOlderActivities] = useState(false); + const [olderHistoryCursorVersion, setOlderHistoryCursorVersion] = useState(0); + const loadThreadActivities = useAtomCommand(orchestrationEnvironment.loadThreadActivities, { + reportFailure: false, + }); + const activeThreadActivityRequestKey = activeThread + ? `${activeThread.environmentId}\u0000${activeThread.id}` + : null; + const liveThreadActivities = activeThread?.activities ?? EMPTY_ACTIVITIES; + // Order-independent oldest boundary: `activities[0]` shifts when the reducer + // re-sorts unsequenced rows on the first live append, which would otherwise + // make a plain append look like a window reshape. See helper docs. + const liveOldestActivityId = useMemo( + () => liveWindowOldestActivityId(liveThreadActivities), + [liveThreadActivities], + ); + const liveActivityCount = liveThreadActivities.length; + // Bumps on every lazy-load reset so a late in-flight load can't repopulate the + // freshly-cleared state (the thread key alone doesn't change on a same-thread + // window reshape). + const olderActivitiesGenRef = useRef(0); + // The request key of an in-flight older-history load — coalesces the duplicate + // dispatches a fast scroll-to-top fires before the loading state updates. + const inFlightOlderKeyRef = useRef(null); + // The oldest row we've paged past. Advancing this (not re-deriving from the + // merged set) lets an all-overlap page keep paging when the server still + // reports `hasMore` — without it a page that dedupes to nothing would either + // terminate paging early or re-request the same cursor forever. Reset on reshape. + const olderCursorRef = useRef(null); + // Reset the lazy-loaded older pages when the live window is *reshaped* rather + // than purely appended-to: a different thread or a re-snapshot (reconnect) + // changes its oldest row, and a checkpoint revert removes rows so the count + // shrinks. A pure append (same thread, same oldest, larger count) keeps them. + const olderWindowRef = useRef({ + key: activeThreadActivityRequestKey, + oldest: liveOldestActivityId, + count: liveActivityCount, + }); + // useLayoutEffect (not useEffect) so the cleared state commits before paint: + // otherwise the new thread renders one frame with the previous thread's + // lazy-loaded pages still merged in, flashing stale work-log/approval rows. + useLayoutEffect(() => { + const prev = olderWindowRef.current; + olderWindowRef.current = { + key: activeThreadActivityRequestKey, + oldest: liveOldestActivityId, + count: liveActivityCount, + }; + const reshaped = + activeThreadActivityRequestKey !== prev.key || + liveOldestActivityId !== prev.oldest || + liveActivityCount < prev.count; + if (!reshaped) { + return; + } + olderActivitiesGenRef.current += 1; + inFlightOlderKeyRef.current = null; + olderCursorRef.current = null; + setOlderActivities([]); + setOlderLoaded(false); + setOlderHasMore(false); + setLoadingOlderActivities(false); + setOlderHistoryCursorVersion((version) => version + 1); + }, [activeThreadActivityRequestKey, liveOldestActivityId, liveActivityCount]); + + const threadActivities = useMemo( + () => + olderActivities.length > 0 + ? [...olderActivities, ...liveThreadActivities] + : liveThreadActivities, + [olderActivities, liveThreadActivities], + ); + // Latest merged set, read inside the async load handler so dedup runs against + // the current state, not the snapshot captured when the load was dispatched. + const threadActivitiesRef = useRef(threadActivities); + threadActivitiesRef.current = threadActivities; + // Before any page is loaded, the server tells us whether older history exists + // beyond the windowed snapshot; afterwards the page `hasMore` is authoritative. + const hasMoreOlderActivities = olderLoaded + ? olderHasMore + : (activeThread?.hasMoreActivities ?? false); + const loadOlderActivities = useCallback(() => { + if (!activeThread || !hasMoreOlderActivities) { + return; + } + // Page from the explicit cursor (the oldest row we've already paged past) or, + // before any page, the chronologically-oldest loaded row — not + // `threadActivities[0]`: the reducer sorts unsequenced rows to the end, so + // index 0 can be a newer sequenced row whose `beforeSequence` cursor would + // skip older unsequenced history. This matches the reshape sentinel. + const oldestActivity = olderCursorRef.current ?? oldestActivityByChronology(threadActivities); + if (!oldestActivity || !activeThreadActivityRequestKey) { + return; + } + if (inFlightOlderKeyRef.current === activeThreadActivityRequestKey) { + return; // a load for this thread is already in flight + } + const cursorInput = + oldestActivity.sequence !== undefined + ? { beforeSequence: oldestActivity.sequence } + : { beforeCreatedAt: oldestActivity.createdAt, beforeActivityId: oldestActivity.id }; + const requestKey = activeThreadActivityRequestKey; + const gen = olderActivitiesGenRef.current; + inFlightOlderKeyRef.current = requestKey; + setLoadingOlderActivities(true); + void loadThreadActivities({ + environmentId: activeThread.environmentId, + input: { threadId: activeThread.id, ...cursorInput }, + }) + .then((result) => { + // The window/thread was reset while this was in flight — drop the page + // so it can't repopulate state cleared by the reset. + if (olderActivitiesGenRef.current !== gen) { + return; + } + if (result._tag !== "Success") { + return; + } + const page = result.value; + // Advance the cursor to this page's oldest row (pages are ascending, so + // [0] is oldest) even if every row dedupes away — the server cursor is + // strict, so the cursor strictly decreases and paging can't loop, while + // an all-overlap page no longer terminates paging that the server says + // has more. + const pageOldest = page.activities[0]; + if (pageOldest) { + olderCursorRef.current = pageOldest; + } + // Dedup against the LATEST merged set (via ref) so a live append or a + // prior prepend that settled mid-flight can't leave duplicate ids. + const seen = new Set(threadActivitiesRef.current.map((activity) => activity.id)); + const fresh = page.activities.filter((activity) => !seen.has(activity.id)); + if (fresh.length > 0) { + setOlderActivities((prev) => [...fresh, ...prev]); + } + setOlderLoaded(true); + setOlderHasMore(page.hasMore); + setOlderHistoryCursorVersion((version) => version + 1); + }) + .finally(() => { + if (olderActivitiesGenRef.current === gen) { + inFlightOlderKeyRef.current = null; + setLoadingOlderActivities(false); + } + }); + }, [ + activeThread, + activeThreadActivityRequestKey, + hasMoreOlderActivities, + threadActivities, + loadThreadActivities, + ]); + const workLogEntries = useMemo(() => deriveWorkLogEntries(threadActivities), [threadActivities]); const pendingApprovals = useMemo( () => derivePendingApprovals(threadActivities), @@ -5735,6 +5903,10 @@ function ChatViewContent(props: ChatViewProps) { activeTurnStartedAt={activeWorkStartedAt} listRef={legendListRef} timelineEntries={timelineEntries} + hasMoreOlder={hasMoreOlderActivities} + loadingOlder={loadingOlderActivities} + olderHistoryCursorVersion={olderHistoryCursorVersion} + onLoadOlder={loadOlderActivities} latestTurn={activeLatestTurn} runningTurnId={ activeThread.session?.status === "running" @@ -5879,7 +6051,11 @@ function ChatViewContent(props: ChatViewProps) { activeProject?.defaultModelSelection } activeThreadModelSelection={activeThread?.modelSelection} - activeThreadActivities={activeThread?.activities} + // Merged set (older pages + live window), not the windowed + // live slice: the context meter scans for the latest + // context-window.updated event, which can sit in a + // lazy-loaded page on long threads. + activeThreadActivities={threadActivities} resolvedTheme={resolvedTheme} settings={settings} keybindings={keybindings} diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index d5fc28a533d..4634967fc2d 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -3428,7 +3428,11 @@ export default function Sidebar() { : EMPTY_THREAD_JUMP_LABELS; const orderedSidebarThreadKeys = visibleSidebarThreadKeys; const prewarmedSidebarThreadKeys = useMemo( - () => getSidebarThreadIdsToPrewarm(visibleSidebarThreadKeys), + // Browser clients can sit behind constrained remote links. Prewarming every + // visible thread hydrates several full detail windows before the user opens + // any of them, so keep the eager cache warm-up desktop-only. The active + // route still subscribes to its selected thread normally in either mode. + () => (isElectron ? getSidebarThreadIdsToPrewarm(visibleSidebarThreadKeys) : []), [visibleSidebarThreadKeys], ); const prewarmedSidebarThreadRefs = useMemo( diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts index 6d74204bc1c..350278734cb 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts @@ -4,9 +4,97 @@ import { computeMessageDurationStart, deriveMessagesTimelineRows, normalizeCompactToolLabel, + resolveOlderHistoryAutoLoad, resolveAssistantMessageCopyState, } from "./MessagesTimeline.logic"; +describe("resolveOlderHistoryAutoLoad", () => { + it("does not retry continuously while a failed request leaves the viewport at the start", () => { + const first = resolveOlderHistoryAutoLoad({ + armed: true, + hasMore: true, + isAtStart: true, + loading: false, + observedProgressVersion: 0, + progressVersion: 0, + }); + expect(first).toEqual({ + armed: false, + observedProgressVersion: 0, + shouldLoad: true, + }); + + const afterFailure = resolveOlderHistoryAutoLoad({ + armed: first.armed, + hasMore: true, + isAtStart: true, + loading: false, + observedProgressVersion: first.observedProgressVersion, + progressVersion: 0, + }); + expect(afterFailure).toEqual({ + armed: false, + observedProgressVersion: 0, + shouldLoad: false, + }); + + const afterLeavingStart = resolveOlderHistoryAutoLoad({ + armed: afterFailure.armed, + hasMore: true, + isAtStart: false, + loading: false, + observedProgressVersion: afterFailure.observedProgressVersion, + progressVersion: 0, + }); + expect(afterLeavingStart).toEqual({ + armed: true, + observedProgressVersion: 0, + shouldLoad: false, + }); + + expect( + resolveOlderHistoryAutoLoad({ + armed: afterLeavingStart.armed, + hasMore: true, + isAtStart: true, + loading: false, + observedProgressVersion: afterLeavingStart.observedProgressVersion, + progressVersion: 0, + }), + ).toEqual({ + armed: false, + observedProgressVersion: 0, + shouldLoad: true, + }); + }); + + it("rearms at the start only after a page successfully advances the cursor", () => { + const afterFirstAttempt = resolveOlderHistoryAutoLoad({ + armed: true, + hasMore: true, + isAtStart: true, + loading: false, + observedProgressVersion: 0, + progressVersion: 0, + }); + + expect( + resolveOlderHistoryAutoLoad({ + armed: afterFirstAttempt.armed, + hasMore: true, + isAtStart: true, + loading: false, + observedProgressVersion: afterFirstAttempt.observedProgressVersion, + progressVersion: 1, + }), + ).toEqual({ + armed: false, + observedProgressVersion: 1, + shouldLoad: true, + }); + }); +}); + describe("computeMessageDurationStart", () => { it("returns message createdAt when there is no preceding user message", () => { const result = computeMessageDurationStart([ diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.ts index 3227bac2413..69cd380b577 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts @@ -21,6 +21,51 @@ export interface TimelineEndState { readonly isNearEnd?: boolean; } +export interface OlderHistoryAutoLoadDecision { + readonly armed: boolean; + readonly observedProgressVersion: number; + readonly shouldLoad: boolean; +} + +/** + * Treat reaching the start as an edge, not a continuously-true condition. + * A failed request leaves the viewport at the start, so level-triggered loading + * would immediately retry on every render. Leaving the start OR observing a + * successfully advanced page cursor rearms one future automatic request. The + * visible header control remains available for explicit retries while the edge + * is disarmed. + */ +export function resolveOlderHistoryAutoLoad(input: { + readonly armed: boolean; + readonly hasMore: boolean; + readonly isAtStart: boolean; + readonly loading: boolean; + readonly observedProgressVersion: number; + readonly progressVersion: number; +}): OlderHistoryAutoLoadDecision { + const progressed = input.progressVersion !== input.observedProgressVersion; + const armed = input.armed || progressed; + if (!input.isAtStart) { + return { + armed: true, + observedProgressVersion: input.progressVersion, + shouldLoad: false, + }; + } + if (!armed || !input.hasMore || input.loading) { + return { + armed, + observedProgressVersion: input.progressVersion, + shouldLoad: false, + }; + } + return { + armed: false, + observedProgressVersion: input.progressVersion, + shouldLoad: true, + }; +} + export function resolveTimelineIsAtEnd(state: TimelineEndState | undefined): boolean | undefined { return state?.isNearEnd ?? state?.isAtEnd; } diff --git a/apps/web/src/components/chat/MessagesTimeline.test.tsx b/apps/web/src/components/chat/MessagesTimeline.test.tsx index 83ca7d3e952..fe106f8ba19 100644 --- a/apps/web/src/components/chat/MessagesTimeline.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.test.tsx @@ -655,4 +655,37 @@ describe("MessagesTimeline", () => { expect(markup).toContain("lucide-x"); expect(markup).toContain('aria-label="Tool call failed"'); }); + + it("offers a 'Load older history' control when older activity remains", async () => { + const { MessagesTimeline } = await import("./MessagesTimeline"); + const markup = renderToStaticMarkup( + , + ); + expect(markup).toContain("Load older history"); + }); + + it("shows a loading indicator while older history is being fetched", async () => { + const { MessagesTimeline } = await import("./MessagesTimeline"); + const markup = renderToStaticMarkup( + , + ); + expect(markup).toContain("Loading older history"); + }); + + it("renders no older-history control when none remains", async () => { + const { MessagesTimeline } = await import("./MessagesTimeline"); + const markup = renderToStaticMarkup( + , + ); + expect(markup).not.toContain("older history"); + }); }); diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index a429b54deaf..3a4afcb2b02 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -68,6 +68,7 @@ import { computeStableMessagesTimelineRows, deriveMessagesTimelineRows, normalizeCompactToolLabel, + resolveOlderHistoryAutoLoad, resolveAssistantMessageCopyState, resolveTimelineIsAtEnd, resolveTimelineMinimapHasPersistentGutter, @@ -184,6 +185,12 @@ interface MessagesTimelineProps { onManualNavigation: () => void; hideEmptyPlaceholder?: boolean; topFadeEnabled?: boolean; + /** Older history beyond the live activity window can be lazy-loaded. */ + hasMoreOlder?: boolean; + loadingOlder?: boolean; + /** Increments after the older-history cursor advances or is reset. */ + olderHistoryCursorVersion?: number; + onLoadOlder?: () => void; } // --------------------------------------------------------------------------- @@ -219,6 +226,10 @@ export const MessagesTimeline = memo(function MessagesTimeline({ onManualNavigation, hideEmptyPlaceholder = false, topFadeEnabled = false, + hasMoreOlder = false, + loadingOlder = false, + olderHistoryCursorVersion = 0, + onLoadOlder, }: MessagesTimelineProps) { const [expandedTurnIds, setExpandedTurnIds] = useState>(new Set()); const [expandedWorkGroupIds, setExpandedWorkGroupIds] = useState>(new Set()); @@ -330,6 +341,19 @@ export const MessagesTimeline = memo(function MessagesTimeline({ ); const [minimapHasPersistentGutter, setMinimapHasPersistentGutter] = useState(false); const [minimapHitStripWidth, setMinimapHitStripWidth] = useState(0); + const olderHistoryAutoLoadArmedRef = useRef(true); + const olderHistoryObservedProgressVersionRef = useRef(olderHistoryCursorVersion); + const requestOlderHistory = useCallback(() => { + // Disarm before both automatic and explicit requests. If a request fails, + // prop changes while the viewport remains at the start must not trigger an + // immediate retry loop; the header button still permits a deliberate retry. + olderHistoryAutoLoadArmedRef.current = false; + onLoadOlder?.(); + }, [onLoadOlder]); + useEffect(() => { + olderHistoryAutoLoadArmedRef.current = true; + olderHistoryObservedProgressVersionRef.current = olderHistoryCursorVersion; + }, [routeThreadKey, olderHistoryCursorVersion]); const handleAnchorReady = useCallback( (info: { anchorIndex: number | undefined }) => { if (anchorMessageId !== null && info.anchorIndex !== undefined) { @@ -361,6 +385,21 @@ export const MessagesTimeline = memo(function MessagesTimeline({ if (isAtEnd !== undefined) { onIsAtEndChange(isAtEnd); } + // Reaching the top lazy-loads older history; maintainVisibleContentPosition + // (set on the list) keeps the viewport anchored when rows prepend. + const olderHistoryDecision = resolveOlderHistoryAutoLoad({ + armed: olderHistoryAutoLoadArmedRef.current, + hasMore: hasMoreOlder, + isAtStart: state?.isAtStart ?? false, + loading: loadingOlder, + observedProgressVersion: olderHistoryObservedProgressVersionRef.current, + progressVersion: olderHistoryCursorVersion, + }); + olderHistoryAutoLoadArmedRef.current = olderHistoryDecision.armed; + olderHistoryObservedProgressVersionRef.current = olderHistoryDecision.observedProgressVersion; + if (olderHistoryDecision.shouldLoad) { + requestOlderHistory(); + } if (!state || minimapItems.length === 0) { return; } @@ -383,7 +422,16 @@ export const MessagesTimeline = memo(function MessagesTimeline({ strip.dataset.inView = inView ? "true" : "false"; } - }, [listRef, minimapItems, minimapStripMap, onIsAtEndChange]); + }, [ + listRef, + minimapItems, + minimapStripMap, + onIsAtEndChange, + hasMoreOlder, + loadingOlder, + olderHistoryCursorVersion, + requestOlderHistory, + ]); useEffect(() => { const frame = requestAnimationFrame(handleScroll); @@ -415,6 +463,28 @@ export const MessagesTimeline = memo(function MessagesTimeline({ }; }, [timelineViewportElement, rows.length]); + const listHeader = useMemo(() => { + if (loadingOlder) { + return ( +
+ Loading older history… +
+ ); + } + if (hasMoreOlder) { + return ( + + ); + } + return topFadeEnabled ? TIMELINE_LIST_FADE_HEADER : TIMELINE_LIST_HEADER; + }, [loadingOlder, hasMoreOlder, requestOlderHistory, topFadeEnabled]); + const sharedState = useMemo( () => ({ timestampFormat, @@ -471,13 +541,21 @@ export const MessagesTimeline = memo(function MessagesTimeline({ if (hideEmptyPlaceholder) { return null; } - return ( -
-

- Send a message to start the conversation. -

-
- ); + // Only short-circuit to the empty state when there is genuinely nothing to + // fetch: the window can derive zero VISIBLE rows (e.g. only tool-neutral work + // entries) while older history still exists — the list must render then so + // its "Load older history" header stays reachable. + if (hasMoreOlder || loadingOlder) { + // Keep the list mounted so its older-history control remains reachable. + } else { + return ( +
+

+ Send a message to start the conversation. +

+
+ ); + } } return ( @@ -515,7 +593,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({ "scrollbar-gutter-both h-full min-h-0 overflow-x-hidden overscroll-y-contain px-3 [overflow-anchor:none] sm:px-5", topFadeEnabled && "chat-timeline-scroll-fade", )} - ListHeaderComponent={topFadeEnabled ? TIMELINE_LIST_FADE_HEADER : TIMELINE_LIST_HEADER} + ListHeaderComponent={listHeader} ListFooterComponent={TIMELINE_LIST_FOOTER} /> ( @@ -12,6 +12,11 @@ export function createOrchestrationEnvironmentAtoms( label: "environment-data:orchestration:turn-diff", tag: ORCHESTRATION_WS_METHODS.getTurnDiff, }), + // Imperative lazy-load of older thread activities (infinite scroll-up). + loadThreadActivities: createEnvironmentRpcCommand(runtime, { + label: "environment-data:orchestration:thread-activities", + tag: ORCHESTRATION_WS_METHODS.getThreadActivities, + }), fullThreadDiff: createEnvironmentRpcQueryAtomFamily(runtime, { label: "environment-data:orchestration:full-thread-diff", tag: ORCHESTRATION_WS_METHODS.getFullThreadDiff, diff --git a/packages/client-runtime/src/state/threadReducer.test.ts b/packages/client-runtime/src/state/threadReducer.test.ts index 211f8748f4e..2df8efd4405 100644 --- a/packages/client-runtime/src/state/threadReducer.test.ts +++ b/packages/client-runtime/src/state/threadReducer.test.ts @@ -9,9 +9,25 @@ import { ThreadId, TurnId, } from "@t3tools/contracts"; -import type { OrchestrationThread } from "@t3tools/contracts"; +import type { OrchestrationThread, OrchestrationThreadActivity } from "@t3tools/contracts"; -import { applyThreadDetailEvent } from "./threadReducer.ts"; +import { + applyThreadDetailEvent, + liveWindowOldestActivityId, + oldestActivityByChronology, +} from "./threadReducer.ts"; + +const activity = (id: string, createdAt: string, sequence?: number): OrchestrationThreadActivity => + ({ + id, + tone: "tool", + kind: "command", + summary: id, + payload: {}, + turnId: TurnId.make("turn-1"), + createdAt, + ...(sequence !== undefined ? { sequence } : {}), + }) as unknown as OrchestrationThreadActivity; const baseEventFields = { eventId: EventId.make("event-1"), @@ -755,4 +771,61 @@ describe("applyThreadDetailEvent", () => { expect(result.kind).toBe("unchanged"); }); }); + + describe("liveWindowOldestActivityId", () => { + it("returns null for an empty window", () => { + expect(liveWindowOldestActivityId([])).toBeNull(); + }); + + it("returns the chronologically-oldest id regardless of array position", () => { + // Reducer order places the unsequenced legacy row (oldest) LAST while the + // server snapshot would list it first; the helper picks it either way. + const window = [ + activity("seq-1", "2026-04-01T10:00:01.000Z", 1), + activity("seq-2", "2026-04-01T10:00:02.000Z", 2), + activity("legacy", "2026-04-01T09:00:00.000Z"), + ]; + expect(liveWindowOldestActivityId(window)).toBe("legacy"); + }); + + it("breaks createdAt ties by id", () => { + const window = [ + activity("b", "2026-04-01T10:00:00.000Z", 2), + activity("a", "2026-04-01T10:00:00.000Z", 1), + ]; + expect(liveWindowOldestActivityId(window)).toBe("a"); + }); + + it("is stable when a newer activity is appended (no false reshape)", () => { + const before = [ + activity("legacy", "2026-04-01T09:00:00.000Z"), + activity("seq-1", "2026-04-01T10:00:01.000Z", 1), + ]; + // A live append is the newest activity and is unsequenced in the payload; + // it must not change the detected oldest boundary. + const after = [...before, activity("appended", "2026-04-01T11:00:00.000Z")]; + expect(liveWindowOldestActivityId(after)).toBe(liveWindowOldestActivityId(before)); + expect(liveWindowOldestActivityId(after)).toBe("legacy"); + }); + }); + + describe("oldestActivityByChronology", () => { + it("returns null for an empty set", () => { + expect(oldestActivityByChronology([])).toBeNull(); + }); + + it("returns the unsequenced legacy row so the pagination cursor agrees with the sentinel", () => { + // The reducer placed the legacy (unsequenced, oldest) row at the END; paging + // must cursor from it (createdAt cursor), not from index 0's sequenced row. + const merged = [ + activity("seq-5", "2026-04-01T10:00:05.000Z", 5), + activity("seq-6", "2026-04-01T10:00:06.000Z", 6), + activity("legacy", "2026-04-01T09:00:00.000Z"), + ]; + const oldest = oldestActivityByChronology(merged); + expect(oldest?.id).toBe("legacy"); + expect(oldest?.sequence).toBeUndefined(); // → drives the unsequenced cursor + expect(liveWindowOldestActivityId(merged)).toBe(oldest?.id); // sentinel agrees + }); + }); }); diff --git a/packages/client-runtime/src/state/threadReducer.ts b/packages/client-runtime/src/state/threadReducer.ts index ce0dca52f5a..ee6c580418f 100644 --- a/packages/client-runtime/src/state/threadReducer.ts +++ b/packages/client-runtime/src/state/threadReducer.ts @@ -35,6 +35,45 @@ const activityOrder = O.combineAll([ O.mapInput(O.String, (a) => a.id), ]); +/** + * The oldest activity in a set, by chronology (`createdAt`, then `id`) rather + * than array position. + * + * `activities[0]` is not a stable "oldest": {@link activityOrder} sorts + * unsequenced rows to the end (a missing `sequence` is treated as newest) while + * the server snapshot lists legacy unsequenced rows first, so the first live + * append re-sorts the array and shifts index 0. Both the lazy-load *reshape* + * sentinel ({@link liveWindowOldestActivityId}) and the lazy-load *pagination + * cursor* derive from this so they agree on which row is oldest regardless of + * the reducer's placement of unsequenced rows. Returns `null` when empty. + */ +export function oldestActivityByChronology( + activities: ReadonlyArray, +): OrchestrationThreadActivity | null { + let oldest: OrchestrationThreadActivity | null = null; + for (const activity of activities) { + if ( + oldest === null || + activity.createdAt < oldest.createdAt || + (activity.createdAt === oldest.createdAt && activity.id < oldest.id) + ) { + oldest = activity; + } + } + return oldest; +} + +/** + * The id of {@link oldestActivityByChronology}, used as the lazy-load reshape + * sentinel (a reconnect re-snapshot or checkpoint revert changes it; a plain + * append does not). Returns `null` when empty. + */ +export function liveWindowOldestActivityId( + activities: ReadonlyArray, +): OrchestrationThreadActivity["id"] | null { + return oldestActivityByChronology(activities)?.id ?? null; +} + /** * Apply a single orchestration event to an `OrchestrationThread`, returning * the updated thread, a deletion signal, or an "unchanged" marker when the diff --git a/packages/contracts/src/orchestration.ts b/packages/contracts/src/orchestration.ts index 23e18909f05..31c9ae937eb 100644 --- a/packages/contracts/src/orchestration.ts +++ b/packages/contracts/src/orchestration.ts @@ -25,6 +25,7 @@ import { ProviderInstanceId } from "./providerInstance.ts"; export const ORCHESTRATION_WS_METHODS = { dispatchCommand: "orchestration.dispatchCommand", getTurnDiff: "orchestration.getTurnDiff", + getThreadActivities: "orchestration.getThreadActivities", getFullThreadDiff: "orchestration.getFullThreadDiff", replayEvents: "orchestration.replayEvents", getArchivedShellSnapshot: "orchestration.getArchivedShellSnapshot", @@ -373,6 +374,10 @@ export const OrchestrationThread = Schema.Struct({ Schema.withDecodingDefault(Effect.succeed([])), ), activities: Schema.Array(OrchestrationThreadActivity), + // The detail snapshot windows `activities` to the most recent page; this is + // true when older activities exist beyond the window and can be lazy-loaded + // via the getThreadActivities RPC. Absent on lightweight (shell) threads. + hasMoreActivities: Schema.optional(Schema.Boolean), checkpoints: Schema.Array(OrchestrationCheckpointSummary), session: Schema.NullOr(OrchestrationSession), }); @@ -1356,6 +1361,37 @@ export type OrchestrationGetTurnDiffInput = typeof OrchestrationGetTurnDiffInput export const OrchestrationGetTurnDiffResult = ThreadTurnDiff; export type OrchestrationGetTurnDiffResult = typeof OrchestrationGetTurnDiffResult.Type; +/** + * Cursor-paginated load of a thread's OLDER activities (lazy-load / infinite + * scroll). Sequenced activity uses `beforeSequence`, the `sequence` of the + * oldest activity the client currently holds. Legacy unsequenced activity uses + * the `(beforeCreatedAt, beforeActivityId)` pair from the oldest loaded + * activity. The server returns the page of activities immediately older than the + * cursor (chronological ascending) plus whether any remain beyond that. + */ +export const OrchestrationGetThreadActivitiesInput = Schema.Union([ + Schema.Struct({ + threadId: ThreadId, + beforeSequence: NonNegativeInt, + limit: Schema.optional(NonNegativeInt), + }), + Schema.Struct({ + threadId: ThreadId, + beforeCreatedAt: IsoDateTime, + beforeActivityId: EventId, + limit: Schema.optional(NonNegativeInt), + }), +]); +export type OrchestrationGetThreadActivitiesInput = + typeof OrchestrationGetThreadActivitiesInput.Type; + +export const OrchestrationGetThreadActivitiesResult = Schema.Struct({ + activities: Schema.Array(OrchestrationThreadActivity), + hasMore: Schema.Boolean, +}); +export type OrchestrationGetThreadActivitiesResult = + typeof OrchestrationGetThreadActivitiesResult.Type; + export const OrchestrationGetFullThreadDiffInput = Schema.Struct({ threadId: ThreadId, toTurnCount: NonNegativeInt, @@ -1383,6 +1419,10 @@ export const OrchestrationRpcSchemas = { input: OrchestrationGetTurnDiffInput, output: OrchestrationGetTurnDiffResult, }, + getThreadActivities: { + input: OrchestrationGetThreadActivitiesInput, + output: OrchestrationGetThreadActivitiesResult, + }, getFullThreadDiff: { input: OrchestrationGetFullThreadDiffInput, output: OrchestrationGetFullThreadDiffResult, @@ -1444,6 +1484,14 @@ export class OrchestrationGetTurnDiffError extends Schema.TaggedErrorClass()( + "OrchestrationGetThreadActivitiesError", + { + message: TrimmedNonEmptyString, + cause: Schema.optional(Schema.Defect()), + }, +) {} + export class OrchestrationGetFullThreadDiffError extends Schema.TaggedErrorClass()( "OrchestrationGetFullThreadDiffError", { diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index cbfcb3a9602..e85c9449c3e 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -57,6 +57,8 @@ import { OrchestrationGetFullThreadDiffError, OrchestrationGetFullThreadDiffInput, OrchestrationGetSnapshotError, + OrchestrationGetThreadActivitiesError, + OrchestrationGetThreadActivitiesInput, OrchestrationGetTurnDiffError, OrchestrationGetTurnDiffInput, OrchestrationReplayEventsError, @@ -641,6 +643,15 @@ export const WsOrchestrationGetTurnDiffRpc = Rpc.make(ORCHESTRATION_WS_METHODS.g error: Schema.Union([OrchestrationGetTurnDiffError, EnvironmentAuthorizationError]), }); +export const WsOrchestrationGetThreadActivitiesRpc = Rpc.make( + ORCHESTRATION_WS_METHODS.getThreadActivities, + { + payload: OrchestrationGetThreadActivitiesInput, + success: OrchestrationRpcSchemas.getThreadActivities.output, + error: Schema.Union([OrchestrationGetThreadActivitiesError, EnvironmentAuthorizationError]), + }, +); + export const WsOrchestrationGetFullThreadDiffRpc = Rpc.make( ORCHESTRATION_WS_METHODS.getFullThreadDiff, { @@ -785,6 +796,7 @@ export const WsRpcGroup = RpcGroup.make( WsSubscribeAuthAccessRpc, WsOrchestrationDispatchCommandRpc, WsOrchestrationGetTurnDiffRpc, + WsOrchestrationGetThreadActivitiesRpc, WsOrchestrationGetFullThreadDiffRpc, WsOrchestrationReplayEventsRpc, WsOrchestrationGetArchivedShellSnapshotRpc, From 7db06a35077d8fe6476b86f73eceeabfa915614c Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sat, 25 Jul 2026 10:46:05 +0200 Subject: [PATCH 03/11] perf: import mobile pagination and bounded replay (upstream #3510) (#35) Source: pingdotgg/t3code#3510 Source SHA: 034f4936d7a1435887bb62ac3f2db61f08928cbf Imported: native mobile lazy loading for older thread activity, a 1,000-event subscription catch-up ceiling with snapshot fallback, and synchronized stale snapshot watermarks. Adapted: applied above the refreshed #4018 web/server candidate and preserved Tim lifecycle handling plus our mobile composer changes. Excluded: #3510 server/web pagination duplicated by #4018, the later shared-hook refactor, formatting-only commits, and contract comments. The shared refactor can be revisited independently after production validation. --- .github/upstream-candidates.json | 6 + .../features/threads/ThreadDetailScreen.tsx | 6 + .../src/features/threads/ThreadFeed.tsx | 21 +++- .../features/threads/ThreadRouteScreen.tsx | 3 + .../src/state/use-thread-composer-state.ts | 118 +++++++++++++++++- .../Services/OrchestrationEngine.ts | 5 +- apps/server/src/server.test.ts | 78 ++++++++++++ apps/server/src/ws.ts | 73 ++++++++++- 8 files changed, 298 insertions(+), 12 deletions(-) diff --git a/.github/upstream-candidates.json b/.github/upstream-candidates.json index b68afc54b75..bced8e5cc34 100644 --- a/.github/upstream-candidates.json +++ b/.github/upstream-candidates.json @@ -6,6 +6,12 @@ "sourceSha": "de8fd65934768173819b93adcd6b92af3e8c7fc3", "status": "active", "purpose": "Bound server thread history and lazily page older web activity" + }, + { + "upstreamPr": 3510, + "sourceSha": "034f4936d7a1435887bb62ac3f2db61f08928cbf", + "status": "active", + "purpose": "Page mobile history and bound stale subscription catch-up" } ] } diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx index 8984e3d2ee7..160d11c3529 100644 --- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx @@ -62,6 +62,9 @@ export interface ThreadDetailScreenProps { /** Message sync status for the selected thread (drives the composer status pill). */ readonly threadSyncStatus?: EnvironmentThreadStatus; readonly activeThreadBusy: boolean; + readonly hasMoreOlderActivities: boolean; + readonly loadingOlderActivities: boolean; + readonly onLoadOlderActivities: () => void; readonly environmentId: EnvironmentId; readonly projectWorkspaceRoot: string | null; readonly threadCwd: string | null; @@ -372,6 +375,9 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread usesAutomaticContentInsets={props.usesAutomaticContentInsets} onHeaderMaterialVisibilityChange={props.onHeaderMaterialVisibilityChange} skills={selectedProviderSkills} + hasMoreOlder={props.hasMoreOlderActivities} + loadingOlder={props.loadingOlderActivities} + onLoadOlder={props.onLoadOlderActivities} /> ) : ( diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 37a8639fdbd..2e2e5b3e7ba 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -141,6 +141,10 @@ export interface ThreadFeedProps { readonly usesAutomaticContentInsets?: boolean; readonly onHeaderMaterialVisibilityChange?: (visible: boolean) => void; readonly skills?: ReadonlyArray; + /** Older history beyond the live activity window can be lazy-loaded on scroll-up. */ + readonly hasMoreOlder?: boolean; + readonly loadingOlder?: boolean; + readonly onLoadOlder?: () => void; } function MessageAttachmentImage(props: { @@ -1498,6 +1502,15 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { ? props.latestTurn.turnId : null; + // Reaching the top (oldest) lazy-loads older history. The hook keys an + // in-flight guard by thread, so repeated fires during scroll coalesce. + const { hasMoreOlder, loadingOlder, onLoadOlder } = props; + const onStartReachedOlderHistory = useCallback(() => { + if (hasMoreOlder && !loadingOlder) { + onLoadOlder?.(); + } + }, [hasMoreOlder, loadingOlder, onLoadOlder]); + useEffect(() => { const previous = previousLatestTurnRef.current; previousLatestTurnRef.current = props.latestTurn; @@ -1790,9 +1803,15 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { estimatedItemSize={180} initialScrollAtEnd onScroll={handleScroll} + onStartReached={onStartReachedOlderHistory} + onStartReachedThreshold={0.5} scrollEventThrottle={16} ListHeaderComponent={ - usesNativeAutomaticInsets ? null : + usesNativeAutomaticInsets && !loadingOlder ? null : ( + + {loadingOlder ? : null} + + ) } contentContainerStyle={{ paddingTop: 12, diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx index 7f6ec925380..023f2a1fa38 100644 --- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx @@ -785,6 +785,9 @@ function ThreadRouteContent( connectionStateLabel={routeConnectionState} threadSyncStatus={selectedThreadDetailState.status} activeThreadBusy={composer.activeThreadBusy} + hasMoreOlderActivities={composer.hasMoreOlderActivities} + loadingOlderActivities={composer.loadingOlderActivities} + onLoadOlderActivities={composer.onLoadOlderActivities} environmentId={selectedThread.environmentId} projectWorkspaceRoot={selectedThreadProject?.workspaceRoot ?? null} threadCwd={selectedThreadCwd} diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index 90831f8437a..a9e2b724017 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -1,11 +1,12 @@ import { useAtomValue } from "@effect/atom-react"; -import { useCallback, useEffect, useMemo } from "react"; +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { CommandId, MessageId, type EnvironmentId, type ModelSelection, + type OrchestrationThreadActivity, type ProviderInteractionMode, type RuntimeMode, type ThreadId, @@ -36,11 +37,15 @@ import { useComposerDraft, } from "./use-composer-drafts"; import { setPendingConnectionError } from "../state/use-remote-environment-registry"; +import { orchestrationEnvironment } from "../state/orchestration"; import { useSelectedThreadDetail } from "../state/use-thread-detail"; import { useThreadSelection } from "../state/use-thread-selection"; +import { useAtomCommand } from "./use-atom-command"; import { enqueueThreadOutboxMessage } from "./thread-outbox"; import { useThreadOutboxMessages } from "./use-thread-outbox"; +const EMPTY_ACTIVITIES: ReadonlyArray = []; + export function appendReviewCommentToDraft(input: { readonly environmentId: EnvironmentId; readonly threadId: ThreadId; @@ -89,9 +94,113 @@ export function useThreadComposerState() { () => (selectedThreadKey ? (queuedMessagesByThreadKey[selectedThreadKey] ?? []) : []), [queuedMessagesByThreadKey, selectedThreadKey], ); + + // ── Older-history lazy-load (mirrors web ChatView) ────────────────────────── + // The detail snapshot windows activities to the most recent page (the server + // sets `hasMoreActivities`); older pages are fetched on demand and prepended. + const [olderActivities, setOlderActivities] = useState< + ReadonlyArray + >([]); + const [olderLoaded, setOlderLoaded] = useState(false); + const [olderHasMore, setOlderHasMore] = useState(false); + const [loadingOlderActivities, setLoadingOlderActivities] = useState(false); + const loadThreadActivities = useAtomCommand(orchestrationEnvironment.loadThreadActivities, { + reportFailure: false, + }); + + const activityRequestKey = selectedThreadShell + ? `${selectedThreadShell.environmentId}\u0000${selectedThreadShell.id}` + : null; + const activityRequestKeyRef = useRef(activityRequestKey); + activityRequestKeyRef.current = activityRequestKey; + useEffect(() => { + setOlderActivities([]); + setOlderLoaded(false); + setOlderHasMore(false); + setLoadingOlderActivities(false); + }, [activityRequestKey]); + + const liveActivities = selectedThreadDetail?.activities ?? EMPTY_ACTIVITIES; + const mergedActivities = useMemo( + () => (olderActivities.length > 0 ? [...olderActivities, ...liveActivities] : liveActivities), + [olderActivities, liveActivities], + ); + // Before any page is loaded, the server tells us whether older history exists. + const hasMoreOlderActivities = olderLoaded + ? olderHasMore + : (selectedThreadDetail?.hasMoreActivities ?? false); + + // Synchronous in-flight guard keyed by thread: the list fires onLoadOlder + // repeatedly while pinned at the top, but loading *state* only updates next + // render, so without this a fast scroll dispatches duplicate same-cursor calls. + const inFlightOlderKeyRef = useRef(null); + const onLoadOlderActivities = useCallback(() => { + if (!selectedThreadShell || !hasMoreOlderActivities) { + return; + } + const oldestActivity = mergedActivities[0]; + if (!oldestActivity || !activityRequestKey) { + return; + } + if (inFlightOlderKeyRef.current === activityRequestKey) { + return; + } + const cursorInput = + oldestActivity.sequence !== undefined + ? { beforeSequence: oldestActivity.sequence } + : { beforeCreatedAt: oldestActivity.createdAt, beforeActivityId: oldestActivity.id }; + const requestKey = activityRequestKey; + inFlightOlderKeyRef.current = requestKey; + setLoadingOlderActivities(true); + void loadThreadActivities({ + environmentId: selectedThreadShell.environmentId, + input: { threadId: selectedThreadShell.id, ...cursorInput }, + }) + .then((result) => { + if (activityRequestKeyRef.current !== requestKey) { + return; + } + if (result._tag !== "Success") { + return; + } + const page = result.value; + setOlderActivities((prev) => { + // Dedup against both already-loaded older pages and the live window, + // since mobile merges everything into one array (duplicate ids would + // produce duplicate React keys in the feed). + const seen = new Set(prev.map((activity) => activity.id)); + for (const activity of liveActivities) { + seen.add(activity.id); + } + const fresh = page.activities.filter((activity) => !seen.has(activity.id)); + return [...fresh, ...prev]; + }); + setOlderLoaded(true); + setOlderHasMore(page.hasMore); + }) + .finally(() => { + if (inFlightOlderKeyRef.current === requestKey) { + inFlightOlderKeyRef.current = null; + } + if (activityRequestKeyRef.current === requestKey) { + setLoadingOlderActivities(false); + } + }); + }, [ + selectedThreadShell, + hasMoreOlderActivities, + mergedActivities, + activityRequestKey, + liveActivities, + loadThreadActivities, + ]); + const selectedThreadFeed = useMemo( - () => (selectedThreadDetail ? buildThreadFeed(selectedThreadDetail) : []), - [selectedThreadDetail], + () => + selectedThreadDetail + ? buildThreadFeed({ ...selectedThreadDetail, activities: mergedActivities }) + : [], + [selectedThreadDetail, mergedActivities], ); const selectedDraft = selectedThreadKey ? composerDrafts[selectedThreadKey] : null; @@ -299,6 +408,9 @@ export function useThreadComposerState() { runtimeMode, interactionMode, activeThreadBusy, + hasMoreOlderActivities, + loadingOlderActivities, + onLoadOlderActivities, onChangeDraftMessage, onPickDraftImages, onPasteIntoDraft, diff --git a/apps/server/src/orchestration/Services/OrchestrationEngine.ts b/apps/server/src/orchestration/Services/OrchestrationEngine.ts index f8bcfd76ac0..b224887e465 100644 --- a/apps/server/src/orchestration/Services/OrchestrationEngine.ts +++ b/apps/server/src/orchestration/Services/OrchestrationEngine.ts @@ -27,9 +27,8 @@ export interface OrchestrationEngineShape { * * @param fromSequenceExclusive - Sequence cursor (exclusive). * @param limit - Maximum number of events to read. Defaults to the event - * store's page-bounded default; pass a higher value when the caller must - * read every event after the cursor (e.g. per-thread catch-up that filters - * a small subset out of a potentially larger global range). + * store's page-bounded default. Callers must keep this bounded; use a + * projection snapshot instead of replaying an arbitrarily stale cursor. * @returns Stream containing ordered events. */ readonly readEvents: ( diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index 4db56fb341e..68a8cb3d683 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -5955,6 +5955,84 @@ it.layer(NodeServices.layer)("server router seam", (it) => { }).pipe(Effect.provide(NodeHttpServer.layerTest)), ); + it.effect("subscribeThread replaces a stale cursor with a fresh snapshot", () => + Effect.gen(function* () { + const snapshotSequence = 5_000; + const thread = makeDefaultOrchestrationReadModel().threads[0]!; + const liveEvents = yield* PubSub.unbounded(); + let replayCalls = 0; + const messageEvent = { + sequence: snapshotSequence + 1, + eventId: EventId.make("event-stale-cursor-message"), + aggregateKind: "thread", + aggregateId: defaultThreadId, + occurredAt: "2026-01-01T00:00:01.000Z", + commandId: null, + causationEventId: null, + correlationId: null, + metadata: {}, + type: "thread.message-sent", + payload: { + threadId: defaultThreadId, + messageId: MessageId.make("message-stale-cursor"), + role: "user", + text: "Published while loading the replacement snapshot", + turnId: null, + streaming: false, + createdAt: "2026-01-01T00:00:01.000Z", + updatedAt: "2026-01-01T00:00:01.000Z", + }, + } satisfies Extract; + + yield* buildAppUnderTest({ + layers: { + projectionSnapshotQuery: { + getSnapshotSequence: () => Effect.succeed({ snapshotSequence }), + getThreadDetailSnapshot: () => + Effect.gen(function* () { + yield* Effect.sleep("25 millis"); + yield* PubSub.publish(liveEvents, messageEvent); + return Option.some({ + snapshotSequence, + thread, + }); + }), + }, + orchestrationEngine: { + streamDomainEvents: Stream.fromPubSub(liveEvents), + readEvents: () => { + replayCalls += 1; + return Stream.empty; + }, + }, + }, + }); + + const wsUrl = yield* getWsServerUrl("/ws"); + const result = yield* Effect.scoped( + withWsRpcClient(wsUrl, (client) => + client[ORCHESTRATION_WS_METHODS.subscribeThread]({ + threadId: defaultThreadId, + afterSequence: 1, + requestCompletionMarker: true, + }).pipe(Stream.take(3), Stream.runCollect), + ), + ).pipe(Effect.timeout("2 seconds")); + + assert.equal(replayCalls, 0); + assert.equal(result[0]?.kind, "snapshot"); + if (result[0]?.kind === "snapshot") { + assert.equal(result[0].snapshot.snapshotSequence, snapshotSequence); + assert.equal(result[0].snapshot.thread.id, defaultThreadId); + } + assert.deepEqual(result[1], { kind: "synchronized" }); + assert.equal(result[2]?.kind, "event"); + if (result[2]?.kind === "event") { + assert.equal(result[2].event.sequence, snapshotSequence + 1); + } + }).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive), + ); + it.effect("subscribeShell coalesces a per-thread burst without stalling other threads", () => Effect.gen(function* () { const busyThreadId = ThreadId.make("thread-busy"); diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index d47ef6f3549..241340af35c 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -119,6 +119,23 @@ import * as SessionStore from "./auth/SessionStore.ts"; import { failEnvironmentAuthInvalid, failEnvironmentInternal } from "./auth/http.ts"; import * as RelayClient from "@t3tools/shared/relayClient"; import { deriveLocalBranchNameFromRemoteRef } from "@t3tools/shared/git"; + +const ORCHESTRATION_SUBSCRIPTION_REPLAY_LIMIT = 1_000; + +const subscriptionReplayLimit = ( + afterSequence: number, + snapshotSequence: number, +): number | null => { + const eventCount = snapshotSequence - afterSequence; + if ( + !Number.isSafeInteger(eventCount) || + eventCount < 0 || + eventCount > ORCHESTRATION_SUBSCRIPTION_REPLAY_LIMIT + ) { + return null; + } + return eventCount; +}; const isOrchestrationDispatchCommandError = Schema.is(OrchestrationDispatchCommandError); const nowIso = Effect.map(DateTime.now, DateTime.formatIso); @@ -1446,14 +1463,60 @@ const makeWsRpcLayer = ( // catch-up followed by the buffered/ongoing live events. Overlapping // events are deduped by sequence on the client. // - // Read the full range after the cursor (not the store's default - // page-bounded limit): the range is normally tiny (a fresh HTTP - // snapshot sequence) and the per-thread filter runs after reading, - // so a global cap could otherwise omit this thread's events. + // A recent cursor replays the exact global range through the + // per-thread filter. A stale cursor receives a current thread + // snapshot instead, keeping catch-up bounded even when the global + // event log is very large. if (input.afterSequence !== undefined) { const afterSequence = input.afterSequence; + const { snapshotSequence } = yield* projectionSnapshotQuery + .getSnapshotSequence() + .pipe( + Effect.mapError( + (cause) => + new OrchestrationGetSnapshotError({ + message: "Failed to load orchestration snapshot sequence", + cause, + }), + ), + ); + const replayLimit = subscriptionReplayLimit(afterSequence, snapshotSequence); + + if (replayLimit === null) { + const snapshot = yield* projectionSnapshotQuery + .getThreadDetailSnapshot(input.threadId) + .pipe( + Effect.mapError( + (cause) => + new OrchestrationGetSnapshotError({ + message: `Failed to load thread ${input.threadId}`, + cause, + }), + ), + ); + + if (Option.isNone(snapshot)) { + return yield* new OrchestrationGetSnapshotError({ + message: `Thread ${input.threadId} was not found`, + cause: input.threadId, + }); + } + + const replacementSnapshot = + input.requestCompletionMarker === true + ? Stream.make( + { kind: "snapshot" as const, snapshot: snapshot.value }, + { kind: "synchronized" as const }, + ) + : Stream.make({ + kind: "snapshot" as const, + snapshot: snapshot.value, + }); + return Stream.concat(replacementSnapshot, bufferedLiveStream); + } + const catchUpStream = orchestrationEngine - .readEvents(afterSequence, Number.MAX_SAFE_INTEGER) + .readEvents(afterSequence, replayLimit) .pipe( Stream.filter(isThisThreadDetailEvent), Stream.map((event) => ({ kind: "event" as const, event })), From 173caa8af7f6ea099e4ed3ced0d77728ba1f3718 Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sat, 25 Jul 2026 10:53:00 +0200 Subject: [PATCH 04/11] perf: import bounded long-lived state (upstream #4176) (#34) Source: pingdotgg/t3code#4176 Source SHA: 56b6615afdfe3804a466e33cbab9056b8981f217 Imported: O(1) command read-model maps, deleted-thread eviction, VCS cache cleanup, browser surface cleanup, preview idle TTL, and per-thread UI cleanup. Adapted: preserved our thread settlement, snooze, and sequential worktree deletion actions while wiring upstream cleanup into the current hook. Excluded: none. Co-authored-by: Rusiru Sadathana <27785781+RusiruSadathana@users.noreply.github.com> --- .github/upstream-candidates.json | 6 + .../Layers/OrchestrationEngine.ts | 34 ++-- .../orchestration/commandInvariants.test.ts | 5 +- .../src/orchestration/commandInvariants.ts | 66 ++++---- .../orchestration/commandReadModel.test.ts | 135 ++++++++++++++++ .../src/orchestration/commandReadModel.ts | 119 ++++++++++++++ .../src/orchestration/decider.delete.test.ts | 132 +++++++++++++++ .../src/orchestration/decider.settled.test.ts | 8 +- .../src/orchestration/decider.snoozed.test.ts | 8 +- apps/server/src/orchestration/decider.ts | 12 +- .../orchestration/projector.settled.test.ts | 17 +- .../src/orchestration/projector.test.ts | 37 +++-- apps/server/src/orchestration/projector.ts | 152 ++++++++++-------- .../src/vcs/VcsStatusBroadcaster.test.ts | 15 ++ apps/server/src/vcs/VcsStatusBroadcaster.ts | 20 ++- .../src/browser/browserSurfaceStore.test.ts | 8 +- apps/web/src/browser/browserSurfaceStore.ts | 14 +- apps/web/src/hooks/useThreadActions.ts | 25 +++ apps/web/src/previewStateStore.ts | 13 +- apps/web/src/uiStateStore.test.ts | 22 +++ apps/web/src/uiStateStore.ts | 23 +++ 21 files changed, 708 insertions(+), 163 deletions(-) create mode 100644 apps/server/src/orchestration/commandReadModel.test.ts create mode 100644 apps/server/src/orchestration/commandReadModel.ts diff --git a/.github/upstream-candidates.json b/.github/upstream-candidates.json index bced8e5cc34..c4d320ef6d2 100644 --- a/.github/upstream-candidates.json +++ b/.github/upstream-candidates.json @@ -12,6 +12,12 @@ "sourceSha": "034f4936d7a1435887bb62ac3f2db61f08928cbf", "status": "active", "purpose": "Page mobile history and bound stale subscription catch-up" + }, + { + "upstreamPr": 4176, + "sourceSha": "56b6615afdfe3804a466e33cbab9056b8981f217", + "status": "active", + "purpose": "Bound long-lived orchestration, browser, preview, and VCS in-memory state" } ] } diff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.ts index 19184915ac7..12af4c77b9b 100644 --- a/apps/server/src/orchestration/Layers/OrchestrationEngine.ts +++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.ts @@ -1,9 +1,4 @@ -import type { - OrchestrationEvent, - OrchestrationReadModel, - ProjectId, - ThreadId, -} from "@t3tools/contracts"; +import type { OrchestrationEvent, ProjectId, ThreadId } from "@t3tools/contracts"; import { OrchestrationCommand } from "@t3tools/contracts"; import * as Cause from "effect/Cause"; import * as Clock from "effect/Clock"; @@ -13,6 +8,7 @@ import * as Deferred from "effect/Deferred"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; +import * as HashMap from "effect/HashMap"; import * as Layer from "effect/Layer"; import * as Metric from "effect/Metric"; import * as Option from "effect/Option"; @@ -37,8 +33,13 @@ import { type OrchestrationDispatchError, type OrchestrationProjectorDecodeError, } from "../Errors.ts"; +import { + createEmptyCommandReadModel, + fromWireReadModel, + type CommandReadModel, +} from "../commandReadModel.ts"; import { decideOrchestrationCommand } from "../decider.ts"; -import { createEmptyReadModel, projectEvent } from "../projector.ts"; +import { projectEvent } from "../projector.ts"; import { OrchestrationProjectionPipeline } from "../Services/ProjectionPipeline.ts"; import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts"; import { @@ -85,15 +86,15 @@ const makeOrchestrationEngine = Effect.gen(function* () { const crypto = yield* Crypto.Crypto; const nowIso = Effect.map(DateTime.now, DateTime.formatIso); - let commandReadModel = createEmptyReadModel(yield* nowIso); + let commandReadModel: CommandReadModel = createEmptyCommandReadModel(yield* nowIso); const commandQueue = yield* Queue.unbounded(); const eventPubSub = yield* PubSub.unbounded(); const projectEventsOntoReadModel = ( - baseReadModel: OrchestrationReadModel, + baseReadModel: CommandReadModel, events: ReadonlyArray, - ): Effect.Effect => + ): Effect.Effect => Effect.gen(function* () { let nextReadModel = baseReadModel; for (const event of events) { @@ -298,12 +299,21 @@ const makeOrchestrationEngine = Effect.gen(function* () { }; yield* projectionPipeline.bootstrap; - commandReadModel = yield* projectionSnapshotQuery.getCommandReadModel(); + // Seed the in-memory command model from the DB projection. Deleted threads + // are dropped so the model starts consistent with the projector's eviction + // policy (see commandReadModel.ts / the `thread.deleted` projector branch). + commandReadModel = fromWireReadModel(yield* projectionSnapshotQuery.getCommandReadModel(), { + dropDeletedThreads: true, + }); const worker = Effect.forever(Queue.take(commandQueue).pipe(Effect.flatMap(processEnvelope))); yield* Effect.forkScoped(worker); yield* Effect.logDebug("orchestration engine started").pipe( - Effect.annotateLogs({ sequence: commandReadModel.snapshotSequence }), + Effect.annotateLogs({ + sequence: commandReadModel.snapshotSequence, + threadCount: HashMap.size(commandReadModel.threads), + projectCount: HashMap.size(commandReadModel.projects), + }), ); const readEvents: OrchestrationEngineShape["readEvents"] = (fromSequenceExclusive, limit) => diff --git a/apps/server/src/orchestration/commandInvariants.test.ts b/apps/server/src/orchestration/commandInvariants.test.ts index 9531cd5c3af..05f6e1694f0 100644 --- a/apps/server/src/orchestration/commandInvariants.test.ts +++ b/apps/server/src/orchestration/commandInvariants.test.ts @@ -11,6 +11,7 @@ import { } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import { fromWireReadModel } from "./commandReadModel.ts"; import { findThreadById, listThreadsByProjectId, @@ -21,7 +22,7 @@ import { const now = "2026-01-01T00:00:00.000Z"; -const readModel: OrchestrationReadModel = { +const wireReadModel: OrchestrationReadModel = { snapshotSequence: 2, updatedAt: now, projects: [ @@ -106,6 +107,8 @@ const readModel: OrchestrationReadModel = { ], }; +const readModel = fromWireReadModel(wireReadModel, { dropDeletedThreads: false }); + const messageSendCommand: OrchestrationCommand = { type: "thread.turn.start", commandId: CommandId.make("cmd-1"), diff --git a/apps/server/src/orchestration/commandInvariants.ts b/apps/server/src/orchestration/commandInvariants.ts index b59ded77f4f..e3c532ca1ed 100644 --- a/apps/server/src/orchestration/commandInvariants.ts +++ b/apps/server/src/orchestration/commandInvariants.ts @@ -1,16 +1,25 @@ import type { OrchestrationCommand, OrchestrationProject, - OrchestrationReadModel, OrchestrationThread, ProjectId, ThreadId, } from "@t3tools/contracts"; import { normalizeProjectPathForComparison } from "@t3tools/shared/path"; import * as Effect from "effect/Effect"; +import * as HashMap from "effect/HashMap"; +import { + findProjectById, + findThreadById, + isThreadDeleted, + listThreadsByProjectId, + type CommandReadModel, +} from "./commandReadModel.ts"; import { OrchestrationCommandInvariantError } from "./Errors.ts"; +export { findProjectById, findThreadById, listThreadsByProjectId }; + function invariantError(commandType: string, detail: string): OrchestrationCommandInvariantError { return new OrchestrationCommandInvariantError({ commandType, @@ -18,29 +27,8 @@ function invariantError(commandType: string, detail: string): OrchestrationComma }); } -export function findThreadById( - readModel: OrchestrationReadModel, - threadId: ThreadId, -): OrchestrationThread | undefined { - return readModel.threads.find((thread) => thread.id === threadId); -} - -export function findProjectById( - readModel: OrchestrationReadModel, - projectId: ProjectId, -): OrchestrationProject | undefined { - return readModel.projects.find((project) => project.id === projectId); -} - -export function listThreadsByProjectId( - readModel: OrchestrationReadModel, - projectId: ProjectId, -): ReadonlyArray { - return readModel.threads.filter((thread) => thread.projectId === projectId); -} - export function requireProject(input: { - readonly readModel: OrchestrationReadModel; + readonly readModel: CommandReadModel; readonly command: OrchestrationCommand; readonly projectId: ProjectId; }): Effect.Effect { @@ -57,7 +45,7 @@ export function requireProject(input: { } export function requireProjectAbsent(input: { - readonly readModel: OrchestrationReadModel; + readonly readModel: CommandReadModel; readonly command: OrchestrationCommand; readonly projectId: ProjectId; }): Effect.Effect { @@ -73,18 +61,23 @@ export function requireProjectAbsent(input: { } export function requireActiveProjectWorkspaceRootAbsent(input: { - readonly readModel: OrchestrationReadModel; + readonly readModel: CommandReadModel; readonly command: OrchestrationCommand; readonly workspaceRoot: string; readonly exceptProjectId?: ProjectId; }): Effect.Effect { const normalizedWorkspaceRoot = normalizeProjectPathForComparison(input.workspaceRoot); - const existingProject = input.readModel.projects.find( - (project) => + let existingProject: OrchestrationProject | undefined; + for (const project of HashMap.values(input.readModel.projects)) { + if ( project.deletedAt === null && normalizeProjectPathForComparison(project.workspaceRoot) === normalizedWorkspaceRoot && - project.id !== input.exceptProjectId, - ); + project.id !== input.exceptProjectId + ) { + existingProject = project; + break; + } + } if (existingProject === undefined) { return Effect.void; } @@ -97,7 +90,7 @@ export function requireActiveProjectWorkspaceRootAbsent(input: { } export function requireThread(input: { - readonly readModel: OrchestrationReadModel; + readonly readModel: CommandReadModel; readonly command: OrchestrationCommand; readonly threadId: ThreadId; }): Effect.Effect { @@ -114,7 +107,7 @@ export function requireThread(input: { } export function requireThreadArchived(input: { - readonly readModel: OrchestrationReadModel; + readonly readModel: CommandReadModel; readonly command: OrchestrationCommand; readonly threadId: ThreadId; }): Effect.Effect { @@ -133,7 +126,7 @@ export function requireThreadArchived(input: { } export function requireThreadNotArchived(input: { - readonly readModel: OrchestrationReadModel; + readonly readModel: CommandReadModel; readonly command: OrchestrationCommand; readonly threadId: ThreadId; }): Effect.Effect { @@ -152,11 +145,16 @@ export function requireThreadNotArchived(input: { } export function requireThreadAbsent(input: { - readonly readModel: OrchestrationReadModel; + readonly readModel: CommandReadModel; readonly command: OrchestrationCommand; readonly threadId: ThreadId; }): Effect.Effect { - if (!findThreadById(input.readModel, input.threadId)) { + // A deleted thread is evicted from `threads` but its id is retained in + // `deletedThreadIds`, so reject re-using a live OR previously-deleted id. + if ( + !findThreadById(input.readModel, input.threadId) && + !isThreadDeleted(input.readModel, input.threadId) + ) { return Effect.void; } return Effect.fail( diff --git a/apps/server/src/orchestration/commandReadModel.test.ts b/apps/server/src/orchestration/commandReadModel.test.ts new file mode 100644 index 00000000000..6cc7aad55ee --- /dev/null +++ b/apps/server/src/orchestration/commandReadModel.test.ts @@ -0,0 +1,135 @@ +import { + DEFAULT_PROVIDER_INTERACTION_MODE, + ProjectId, + ProviderInstanceId, + ThreadId, + type OrchestrationReadModel, + type OrchestrationThread, +} from "@t3tools/contracts"; +import * as HashMap from "effect/HashMap"; +import { describe, expect, it } from "vite-plus/test"; + +import { + createEmptyCommandReadModel, + findProjectById, + findThreadById, + fromWireReadModel, + isThreadDeleted, + listThreadsByProjectId, +} from "./commandReadModel.ts"; + +const now = "2026-01-01T00:00:00.000Z"; + +function makeThread( + id: string, + projectId: string, + overrides?: Partial, +): OrchestrationThread { + return { + id: ThreadId.make(id), + projectId: ProjectId.make(projectId), + title: id, + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5-codex", + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "full-access", + branch: null, + worktreePath: null, + createdAt: now, + updatedAt: now, + archivedAt: null, + settledOverride: null, + settledAt: null, + snoozedUntil: null, + snoozedAt: null, + hasMoreActivities: false, + latestTurn: null, + messages: [], + session: null, + activities: [], + proposedPlans: [], + checkpoints: [], + deletedAt: null, + ...overrides, + }; +} + +const wireReadModel: OrchestrationReadModel = { + snapshotSequence: 5, + updatedAt: now, + projects: [ + { + id: ProjectId.make("project-a"), + title: "Project A", + workspaceRoot: "/tmp/project-a", + defaultModelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5-codex", + }, + scripts: [], + createdAt: now, + updatedAt: now, + deletedAt: null, + }, + ], + threads: [ + makeThread("thread-live", "project-a"), + makeThread("thread-archived", "project-a", { archivedAt: now }), + makeThread("thread-deleted", "project-a", { deletedAt: now }), + makeThread("thread-other", "project-b"), + ], +}; + +describe("commandReadModel", () => { + it("creates an empty model", () => { + const model = createEmptyCommandReadModel(now); + expect(model.snapshotSequence).toBe(0); + expect(HashMap.size(model.threads)).toBe(0); + expect(HashMap.size(model.projects)).toBe(0); + expect(model.updatedAt).toBe(now); + }); + + it("seeds from the wire model and drops deleted threads by default", () => { + const model = fromWireReadModel(wireReadModel); + expect(model.snapshotSequence).toBe(5); + // deleted thread is evicted; archived + live + other retained + expect(HashMap.size(model.threads)).toBe(3); + expect(HashMap.has(model.threads, ThreadId.make("thread-deleted"))).toBe(false); + expect(HashMap.has(model.threads, ThreadId.make("thread-archived"))).toBe(true); + expect(HashMap.has(model.threads, ThreadId.make("thread-live"))).toBe(true); + expect(HashMap.size(model.projects)).toBe(1); + // The evicted deleted thread's id is retained so the create-twice invariant + // survives a restart. + expect(isThreadDeleted(model, ThreadId.make("thread-deleted"))).toBe(true); + expect(isThreadDeleted(model, ThreadId.make("thread-live"))).toBe(false); + expect(isThreadDeleted(model, ThreadId.make("thread-archived"))).toBe(false); + }); + + it("retains deleted threads when dropDeletedThreads is false", () => { + const model = fromWireReadModel(wireReadModel, { dropDeletedThreads: false }); + expect(HashMap.size(model.threads)).toBe(4); + expect(HashMap.has(model.threads, ThreadId.make("thread-deleted"))).toBe(true); + }); + + it("finds threads and projects by id with O(1) lookups", () => { + const model = fromWireReadModel(wireReadModel); + expect(findThreadById(model, ThreadId.make("thread-live"))?.projectId).toBe("project-a"); + expect(findThreadById(model, ThreadId.make("thread-deleted"))).toBeUndefined(); + expect(findThreadById(model, ThreadId.make("missing"))).toBeUndefined(); + expect(findProjectById(model, ProjectId.make("project-a"))?.title).toBe("Project A"); + expect(findProjectById(model, ProjectId.make("missing"))).toBeUndefined(); + }); + + it("lists threads by project id", () => { + const model = fromWireReadModel(wireReadModel); + const ids = listThreadsByProjectId(model, ProjectId.make("project-a")) + .map((thread) => thread.id) + .toSorted(); + expect(ids).toEqual([ThreadId.make("thread-archived"), ThreadId.make("thread-live")]); + expect(listThreadsByProjectId(model, ProjectId.make("project-b")).map((t) => t.id)).toEqual([ + ThreadId.make("thread-other"), + ]); + }); +}); diff --git a/apps/server/src/orchestration/commandReadModel.ts b/apps/server/src/orchestration/commandReadModel.ts new file mode 100644 index 00000000000..dce19fc2842 --- /dev/null +++ b/apps/server/src/orchestration/commandReadModel.ts @@ -0,0 +1,119 @@ +import type { + OrchestrationProject, + OrchestrationReadModel, + OrchestrationThread, + ProjectId, + ThreadId, +} from "@t3tools/contracts"; +import * as HashMap from "effect/HashMap"; +import * as HashSet from "effect/HashSet"; +import * as Option from "effect/Option"; + +/** + * Server-internal representation of the orchestration read model. + * + * Unlike the wire {@link OrchestrationReadModel} (which uses arrays and is + * produced by the DB-backed `ProjectionSnapshotQuery`), this model is only ever + * touched by the single serial command-worker fiber in `OrchestrationEngine`. + * It uses persistent `HashMap`s keyed by id so the projector and command + * invariants get O(1)-ish lookups and single-key updates instead of O(N) + * array scans and full-array copies on every event. + * + * The model is never serialized or sent over the wire, so its shape can evolve + * independently of the contract. + */ +export interface CommandReadModel { + readonly snapshotSequence: number; + readonly projects: HashMap.HashMap; + readonly threads: HashMap.HashMap; + /** + * Ids of threads that have been deleted. Deleted threads are evicted from + * `threads` (freeing their message/activity/checkpoint arrays), but their id + * is retained here so `requireThreadAbsent` still rejects re-creating a + * thread with a previously-used id — the "cannot be created twice" invariant + * the DB projection also upholds (its tombstone row is never removed). + */ + readonly deletedThreadIds: HashSet.HashSet; + readonly updatedAt: string; +} + +export function createEmptyCommandReadModel(nowIso: string): CommandReadModel { + return { + snapshotSequence: 0, + projects: HashMap.empty(), + threads: HashMap.empty(), + deletedThreadIds: HashSet.empty(), + updatedAt: nowIso, + }; +} + +/** + * Whether a thread id has been used and deleted. Used by `requireThreadAbsent` + * so an evicted (deleted) thread's id cannot be re-created. + */ +export function isThreadDeleted(model: CommandReadModel, threadId: ThreadId): boolean { + return HashSet.has(model.deletedThreadIds, threadId); +} + +/** + * Seed a {@link CommandReadModel} from the array-based wire read model produced + * by `ProjectionSnapshotQuery.getCommandReadModel()` at engine boot. + * + * Deleted threads are dropped so the in-memory model starts consistent with the + * projector's eviction policy (deleted threads are removed, archived threads are + * retained). The DB projection remains the source of truth for deleted rows. + */ +export function fromWireReadModel( + model: OrchestrationReadModel, + options?: { readonly dropDeletedThreads?: boolean }, +): CommandReadModel { + const dropDeletedThreads = options?.dropDeletedThreads ?? true; + const threads = dropDeletedThreads + ? model.threads.filter((thread) => thread.deletedAt === null) + : model.threads; + // Retain the ids of deleted threads so the create-twice invariant survives + // a restart even though the (evicted) thread bodies are not loaded. + const deletedThreadIds = HashSet.fromIterable( + model.threads.filter((thread) => thread.deletedAt !== null).map((thread) => thread.id), + ); + return { + snapshotSequence: model.snapshotSequence, + updatedAt: model.updatedAt, + projects: HashMap.fromIterable(model.projects.map((project) => [project.id, project] as const)), + threads: HashMap.fromIterable(threads.map((thread) => [thread.id, thread] as const)), + deletedThreadIds, + }; +} + +export function findThreadById( + model: CommandReadModel, + threadId: ThreadId, +): OrchestrationThread | undefined { + return Option.getOrUndefined(HashMap.get(model.threads, threadId)); +} + +export function findProjectById( + model: CommandReadModel, + projectId: ProjectId, +): OrchestrationProject | undefined { + return Option.getOrUndefined(HashMap.get(model.projects, projectId)); +} + +export function listThreadsByProjectId( + model: CommandReadModel, + projectId: ProjectId, +): ReadonlyArray { + const result: OrchestrationThread[] = []; + for (const thread of HashMap.values(model.threads)) { + if (thread.projectId === projectId) { + result.push(thread); + } + } + // HashMap iteration order is not insertion order; sort by creation time (then + // id) so callers observe a stable, deterministic order — matching the prior + // array-backed behavior. Only used by the rare `project.delete` fan-out. + return result.toSorted( + (left, right) => + left.createdAt.localeCompare(right.createdAt) || left.id.localeCompare(right.id), + ); +} diff --git a/apps/server/src/orchestration/decider.delete.test.ts b/apps/server/src/orchestration/decider.delete.test.ts index fea36b5717f..e9f881d1e78 100644 --- a/apps/server/src/orchestration/decider.delete.test.ts +++ b/apps/server/src/orchestration/decider.delete.test.ts @@ -2,6 +2,7 @@ import { CommandId, DEFAULT_PROVIDER_INTERACTION_MODE, EventId, + MessageId, ProjectId, ThreadId, type OrchestrationCommand, @@ -9,6 +10,7 @@ import { ProviderInstanceId, } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import * as HashMap from "effect/HashMap"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { expect, it } from "@effect/vitest"; @@ -214,4 +216,134 @@ it.layer(NodeServices.layer)("decider deletion flows", (it) => { expect(normalizeDeleteEvent(forcedResult)).toEqual(normalizeDeleteEvent(sequentialEvents)); }), ); + + it.effect("rejects commands targeting an already-deleted (evicted) thread", () => + Effect.gen(function* () { + const seeded = yield* seedReadModel; + const now = "2026-01-01T00:00:00.000Z"; + + // Delete thread-delete-1; the projector evicts it from the model. + const afterDelete = yield* projectEvent(seeded, { + sequence: 4, + eventId: asEventId("evt-thread-delete-1"), + aggregateKind: "thread", + aggregateId: asThreadId("thread-delete-1"), + type: "thread.deleted", + occurredAt: now, + commandId: asCommandId("cmd-thread-delete-1"), + causationEventId: null, + correlationId: asCommandId("cmd-thread-delete-1"), + metadata: {}, + payload: { + threadId: asThreadId("thread-delete-1"), + deletedAt: now, + }, + }); + + // A follow-up command to the deleted thread now fails cleanly. + const error = yield* Effect.flip( + decideOrchestrationCommand({ + command: { + type: "thread.turn.start", + commandId: asCommandId("cmd-turn-after-delete"), + threadId: asThreadId("thread-delete-1"), + message: { + messageId: MessageId.make("msg-after-delete"), + role: "user", + text: "hello", + attachments: [], + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "approval-required", + createdAt: now, + }, + readModel: afterDelete, + }), + ); + expect(error.message).toContain("does not exist"); + + // Re-creating a thread with the SAME (deleted) id is rejected: the id is + // retained in deletedThreadIds even though the thread body was evicted, so + // the "cannot be created twice" invariant still holds and the durable DB + // row is not silently overwritten. + const recreateSameId = (threadId: ThreadId) => + decideOrchestrationCommand({ + command: { + type: "thread.create", + commandId: asCommandId(`cmd-recreate-${threadId}`), + threadId, + projectId: asProjectId("project-delete"), + title: "Recreate", + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5-codex", + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "approval-required", + branch: null, + worktreePath: null, + createdAt: now, + }, + readModel: afterDelete, + }); + + const recreateError = yield* Effect.flip(recreateSameId(asThreadId("thread-delete-1"))); + expect(recreateError.message).toContain("cannot be created twice"); + + // A fresh thread with a NEW, never-used id can still be created. + const created = yield* recreateSameId(asThreadId("thread-delete-3")); + const createdEvents = Array.isArray(created) ? created : [created]; + expect(createdEvents.map((event) => event.type)).toEqual(["thread.created"]); + }), + ); + + it.effect("projector evicts deleted threads but retains archived threads", () => + Effect.gen(function* () { + const seeded = yield* seedReadModel; + const now = "2026-01-01T00:00:00.000Z"; + expect(HashMap.size(seeded.threads)).toBe(2); + + // Archiving keeps the thread resident (unarchive/other commands need it). + const afterArchive = yield* projectEvent(seeded, { + sequence: 4, + eventId: asEventId("evt-thread-archive-1"), + aggregateKind: "thread", + aggregateId: asThreadId("thread-delete-1"), + type: "thread.archived", + occurredAt: now, + commandId: asCommandId("cmd-thread-archive-1"), + causationEventId: null, + correlationId: asCommandId("cmd-thread-archive-1"), + metadata: {}, + payload: { + threadId: asThreadId("thread-delete-1"), + archivedAt: now, + updatedAt: now, + }, + }); + expect(HashMap.size(afterArchive.threads)).toBe(2); + expect(HashMap.has(afterArchive.threads, asThreadId("thread-delete-1"))).toBe(true); + + // Deleting evicts the thread from the in-memory model entirely. + const afterDelete = yield* projectEvent(afterArchive, { + sequence: 5, + eventId: asEventId("evt-thread-delete-2"), + aggregateKind: "thread", + aggregateId: asThreadId("thread-delete-2"), + type: "thread.deleted", + occurredAt: now, + commandId: asCommandId("cmd-thread-delete-2"), + causationEventId: null, + correlationId: asCommandId("cmd-thread-delete-2"), + metadata: {}, + payload: { + threadId: asThreadId("thread-delete-2"), + deletedAt: now, + }, + }); + expect(HashMap.size(afterDelete.threads)).toBe(1); + expect(HashMap.has(afterDelete.threads, asThreadId("thread-delete-2"))).toBe(false); + expect(HashMap.has(afterDelete.threads, asThreadId("thread-delete-1"))).toBe(true); + }), + ); }); diff --git a/apps/server/src/orchestration/decider.settled.test.ts b/apps/server/src/orchestration/decider.settled.test.ts index 73f1cbf9127..8debbf5d05c 100644 --- a/apps/server/src/orchestration/decider.settled.test.ts +++ b/apps/server/src/orchestration/decider.settled.test.ts @@ -5,7 +5,6 @@ import { ProjectId, ProviderInstanceId, ThreadId, - type OrchestrationReadModel, type OrchestrationSession, type OrchestrationThread, } from "@t3tools/contracts"; @@ -14,6 +13,7 @@ import { expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import { decideOrchestrationCommand } from "./decider.ts"; +import { fromWireReadModel, type CommandReadModel } from "./commandReadModel.ts"; const NOW = "2026-01-01T00:00:00.000Z"; const SETTLED_AT = "2025-12-30T00:00:00.000Z"; @@ -24,8 +24,8 @@ function makeReadModel( session: OrchestrationSession | null = null, activities: OrchestrationThread["activities"] = [], messages: OrchestrationThread["messages"] = [], -): OrchestrationReadModel { - return { +): CommandReadModel { + return fromWireReadModel({ snapshotSequence: 0, projects: [], threads: [ @@ -53,7 +53,7 @@ function makeReadModel( }, ], updatedAt: NOW, - }; + }); } function makeSession(status: OrchestrationSession["status"]): OrchestrationSession { diff --git a/apps/server/src/orchestration/decider.snoozed.test.ts b/apps/server/src/orchestration/decider.snoozed.test.ts index 1012240b18a..4ca22995e00 100644 --- a/apps/server/src/orchestration/decider.snoozed.test.ts +++ b/apps/server/src/orchestration/decider.snoozed.test.ts @@ -5,7 +5,6 @@ import { ProjectId, ProviderInstanceId, ThreadId, - type OrchestrationReadModel, type OrchestrationThread, } from "@t3tools/contracts"; import * as NodeServices from "@effect/platform-node/NodeServices"; @@ -13,6 +12,7 @@ import { expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import { decideOrchestrationCommand } from "./decider.ts"; +import { fromWireReadModel, type CommandReadModel } from "./commandReadModel.ts"; const NOW = "2026-01-01T00:00:00.000Z"; // The decider's clock is the Effect test clock, pinned to the epoch, so @@ -27,8 +27,8 @@ function makeReadModel(input: { readonly archivedAt?: string | null; readonly activities?: OrchestrationThread["activities"]; readonly messages?: OrchestrationThread["messages"]; -}): OrchestrationReadModel { - return { +}): CommandReadModel { + return fromWireReadModel({ snapshotSequence: 0, projects: [], threads: [ @@ -58,7 +58,7 @@ function makeReadModel(input: { }, ], updatedAt: NOW, - }; + }); } it.layer(NodeServices.layer)("snoozed thread decider", (it) => { diff --git a/apps/server/src/orchestration/decider.ts b/apps/server/src/orchestration/decider.ts index 100369ae6e3..38cc6d63808 100644 --- a/apps/server/src/orchestration/decider.ts +++ b/apps/server/src/orchestration/decider.ts @@ -1,14 +1,10 @@ -import { - EventId, - type OrchestrationCommand, - type OrchestrationEvent, - type OrchestrationReadModel, -} from "@t3tools/contracts"; +import { EventId, type OrchestrationCommand, type OrchestrationEvent } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import * as Crypto from "effect/Crypto"; import * as Effect from "effect/Effect"; import type * as PlatformError from "effect/PlatformError"; +import type { CommandReadModel } from "./commandReadModel.ts"; import { OrchestrationCommandInvariantError } from "./Errors.ts"; import { listThreadsByProjectId, @@ -183,7 +179,7 @@ const decideCommandSequence = Effect.fn("decideCommandSequence")(function* ({ readModel, }: { readonly commands: ReadonlyArray; - readonly readModel: OrchestrationReadModel; + readonly readModel: CommandReadModel; }): Effect.fn.Return< ReadonlyArray, OrchestrationCommandInvariantError | PlatformError.PlatformError, @@ -217,7 +213,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" readModel, }: { readonly command: OrchestrationCommand; - readonly readModel: OrchestrationReadModel; + readonly readModel: CommandReadModel; }): Effect.fn.Return< DecideOrchestrationCommandResult, OrchestrationCommandInvariantError | PlatformError.PlatformError, diff --git a/apps/server/src/orchestration/projector.settled.test.ts b/apps/server/src/orchestration/projector.settled.test.ts index 2070c44418a..c6b04fd16cf 100644 --- a/apps/server/src/orchestration/projector.settled.test.ts +++ b/apps/server/src/orchestration/projector.settled.test.ts @@ -8,6 +8,7 @@ import { import { expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; +import { findThreadById } from "./commandReadModel.ts"; import { createEmptyReadModel, projectEvent } from "./projector.ts"; function makeEvent(input: { @@ -60,8 +61,8 @@ it.effect("projects settled lifecycle events", () => payload: { threadId: ThreadId.make("thread-1"), settledAt: now, updatedAt: now }, }), ); - expect(settled.threads[0]?.settledOverride).toBe("settled"); - expect(settled.threads[0]?.settledAt).toBe(now); + expect(findThreadById(settled, ThreadId.make("thread-1"))?.settledOverride).toBe("settled"); + expect(findThreadById(settled, ThreadId.make("thread-1"))?.settledAt).toBe(now); const userUnsettled = yield* projectEvent( settled, @@ -71,8 +72,10 @@ it.effect("projects settled lifecycle events", () => payload: { threadId: ThreadId.make("thread-1"), reason: "user", updatedAt: now }, }), ); - expect(userUnsettled.threads[0]?.settledOverride).toBe("active"); - expect(userUnsettled.threads[0]?.settledAt).toBeNull(); + expect(findThreadById(userUnsettled, ThreadId.make("thread-1"))?.settledOverride).toBe( + "active", + ); + expect(findThreadById(userUnsettled, ThreadId.make("thread-1"))?.settledAt).toBeNull(); const activityUnsettled = yield* projectEvent( userUnsettled, @@ -82,7 +85,9 @@ it.effect("projects settled lifecycle events", () => payload: { threadId: ThreadId.make("thread-1"), reason: "activity", updatedAt: now }, }), ); - expect(activityUnsettled.threads[0]?.settledOverride).toBeNull(); - expect(activityUnsettled.threads[0]?.settledAt).toBeNull(); + expect( + findThreadById(activityUnsettled, ThreadId.make("thread-1"))?.settledOverride, + ).toBeNull(); + expect(findThreadById(activityUnsettled, ThreadId.make("thread-1"))?.settledAt).toBeNull(); }), ); diff --git a/apps/server/src/orchestration/projector.test.ts b/apps/server/src/orchestration/projector.test.ts index 9c07a312023..81fe043f54f 100644 --- a/apps/server/src/orchestration/projector.test.ts +++ b/apps/server/src/orchestration/projector.test.ts @@ -5,12 +5,25 @@ import { ProviderDriverKind, ThreadId, type OrchestrationEvent, + type OrchestrationThread, } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import * as HashMap from "effect/HashMap"; import { describe, expect, it } from "vite-plus/test"; +import type { CommandReadModel } from "./commandReadModel.ts"; import { createEmptyReadModel, projectEvent } from "./projector.ts"; +/** Thread list from the map, for assertions that previously used an array. */ +function threadsArray(model: CommandReadModel): ReadonlyArray { + return Array.from(HashMap.values(model.threads)); +} + +/** First thread in the map (insertion-order-independent tests use a single thread). */ +function firstThread(model: CommandReadModel): OrchestrationThread | undefined { + return threadsArray(model)[0]; +} + function makeEvent(input: { sequence: number; type: OrchestrationEvent["type"]; @@ -72,7 +85,7 @@ describe("orchestration projector", () => { ); expect(next.snapshotSequence).toBe(1); - expect(next.threads).toEqual([ + expect(threadsArray(next)).toEqual([ { id: "thread-1", projectId: "project-1", @@ -187,7 +200,7 @@ describe("orchestration projector", () => { }), ), ); - expect(archived.threads[0]?.archivedAt).toBe(later); + expect(firstThread(archived)?.archivedAt).toBe(later); const unarchived = await Effect.runPromise( projectEvent( @@ -206,7 +219,7 @@ describe("orchestration projector", () => { }), ), ); - expect(unarchived.threads[0]?.archivedAt).toBeNull(); + expect(firstThread(unarchived)?.archivedAt).toBeNull(); }); it("keeps projector forward-compatible for unhandled event types", async () => { @@ -235,7 +248,7 @@ describe("orchestration projector", () => { expect(next.snapshotSequence).toBe(7); expect(next.updatedAt).toBe("2026-01-01T00:00:00.000Z"); - expect(next.threads).toEqual([]); + expect(threadsArray(next)).toEqual([]); }); it("tracks latest turn id from session lifecycle events", async () => { @@ -331,13 +344,13 @@ describe("orchestration projector", () => { ), ); - const thread = afterRunning.threads[0]; + const thread = firstThread(afterRunning); expect(thread?.latestTurn?.turnId).toBe("turn-1"); expect(thread?.session?.status).toBe("running"); // Leaving the "running" session status settles the running turn with the // session timestamp as the turn end. - const settledThread = afterReady.threads[0]; + const settledThread = firstThread(afterReady); expect(settledThread?.latestTurn?.turnId).toBe("turn-1"); expect(settledThread?.latestTurn?.state).toBe("completed"); expect(settledThread?.latestTurn?.completedAt).toBe(settledAt); @@ -395,8 +408,8 @@ describe("orchestration projector", () => { ), ); - expect(afterUpdate.threads[0]?.runtimeMode).toBe("approval-required"); - expect(afterUpdate.threads[0]?.updatedAt).toBe(updatedAt); + expect(firstThread(afterUpdate)?.runtimeMode).toBe("approval-required"); + expect(firstThread(afterUpdate)?.updatedAt).toBe(updatedAt); }); it("marks assistant messages completed with non-streaming updates", async () => { @@ -481,7 +494,7 @@ describe("orchestration projector", () => { ), ); - const message = afterComplete.threads[0]?.messages[0]; + const message = firstThread(afterComplete)?.messages[0]; expect(message?.id).toBe("assistant:msg-1"); expect(message?.text).toBe("hello"); expect(message?.streaming).toBe(false); @@ -689,7 +702,7 @@ describe("orchestration projector", () => { Promise.resolve(afterCreate), ); - const thread = afterRevert.threads[0]; + const thread = firstThread(afterRevert); expect(thread?.messages.map((message) => ({ role: message.role, text: message.text }))).toEqual( [ { role: "user", text: "First edit" }, @@ -846,7 +859,7 @@ describe("orchestration projector", () => { Promise.resolve(afterCreate), ); - const thread = afterRevert.threads[0]; + const thread = firstThread(afterRevert); expect( thread?.messages.map((message) => ({ id: message.id, @@ -948,7 +961,7 @@ describe("orchestration projector", () => { Promise.resolve(afterMessages), ); - const thread = finalState.threads[0]; + const thread = firstThread(finalState); expect(thread?.messages).toHaveLength(2_000); expect(thread?.messages[0]?.id).toBe("msg-100"); expect(thread?.messages.at(-1)?.id).toBe("msg-2099"); diff --git a/apps/server/src/orchestration/projector.ts b/apps/server/src/orchestration/projector.ts index 0504cb36f9a..d1dfe8ea070 100644 --- a/apps/server/src/orchestration/projector.ts +++ b/apps/server/src/orchestration/projector.ts @@ -1,4 +1,4 @@ -import type { OrchestrationEvent, OrchestrationReadModel, ThreadId } from "@t3tools/contracts"; +import type { OrchestrationEvent, OrchestrationProject, ThreadId } from "@t3tools/contracts"; import { OrchestrationCheckpointSummary, OrchestrationMessage, @@ -6,8 +6,16 @@ import { OrchestrationThread, } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import * as HashMap from "effect/HashMap"; +import * as HashSet from "effect/HashSet"; +import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; +import { + createEmptyCommandReadModel, + findThreadById, + type CommandReadModel, +} from "./commandReadModel.ts"; import { toProjectorDecodeError, type OrchestrationProjectorDecodeError } from "./Errors.ts"; import { MessageSentPayloadSchema, @@ -65,12 +73,40 @@ function settledTurnStateForSessionStatus( } } +/** + * Apply a patch to a single thread, keyed by id. No-op if the thread is absent + * (mirrors the previous array-map behavior, which left the collection unchanged + * when no entry matched). + */ function updateThread( - threads: ReadonlyArray, + threads: HashMap.HashMap, threadId: ThreadId, patch: ThreadPatch, -): OrchestrationThread[] { - return threads.map((thread) => (thread.id === threadId ? { ...thread, ...patch } : thread)); +): HashMap.HashMap { + const existing = HashMap.get(threads, threadId); + if (Option.isNone(existing)) { + return threads; + } + return HashMap.set(threads, threadId, { ...existing.value, ...patch }); +} + +/** + * Apply an update function to a single project in the model, keyed by id. No-op + * if the project is absent. + */ +function updateProject( + model: CommandReadModel, + projectId: OrchestrationProject["id"], + update: (project: OrchestrationProject) => OrchestrationProject, +): CommandReadModel { + const existing = HashMap.get(model.projects, projectId); + if (Option.isNone(existing)) { + return model; + } + return { + ...model, + projects: HashMap.set(model.projects, projectId, update(existing.value)), + }; } function decodeForEvent( @@ -182,20 +218,15 @@ function compareThreadActivities( return left.createdAt.localeCompare(right.createdAt) || left.id.localeCompare(right.id); } -export function createEmptyReadModel(nowIso: string): OrchestrationReadModel { - return { - snapshotSequence: 0, - projects: [], - threads: [], - updatedAt: nowIso, - }; +export function createEmptyReadModel(nowIso: string): CommandReadModel { + return createEmptyCommandReadModel(nowIso); } export function projectEvent( - model: OrchestrationReadModel, + model: CommandReadModel, event: OrchestrationEvent, -): Effect.Effect { - const nextBase: OrchestrationReadModel = { +): Effect.Effect { + const nextBase: CommandReadModel = { ...model, snapshotSequence: event.sequence, updatedAt: event.occurredAt, @@ -205,8 +236,7 @@ export function projectEvent( case "project.created": return decodeForEvent(ProjectCreatedPayload, event.payload, event.type, "payload").pipe( Effect.map((payload) => { - const existing = nextBase.projects.find((entry) => entry.id === payload.projectId); - const nextProject = { + const nextProject: OrchestrationProject = { id: payload.projectId, title: payload.title, workspaceRoot: payload.workspaceRoot, @@ -219,52 +249,38 @@ export function projectEvent( return { ...nextBase, - projects: existing - ? nextBase.projects.map((entry) => - entry.id === payload.projectId ? nextProject : entry, - ) - : [...nextBase.projects, nextProject], + projects: HashMap.set(nextBase.projects, payload.projectId, nextProject), }; }), ); case "project.meta-updated": return decodeForEvent(ProjectMetaUpdatedPayload, event.payload, event.type, "payload").pipe( - Effect.map((payload) => ({ - ...nextBase, - projects: nextBase.projects.map((project) => - project.id === payload.projectId - ? { - ...project, - ...(payload.title !== undefined ? { title: payload.title } : {}), - ...(payload.workspaceRoot !== undefined - ? { workspaceRoot: payload.workspaceRoot } - : {}), - ...(payload.defaultModelSelection !== undefined - ? { defaultModelSelection: payload.defaultModelSelection } - : {}), - ...(payload.scripts !== undefined ? { scripts: payload.scripts } : {}), - updatedAt: payload.updatedAt, - } - : project, - ), - })), + Effect.map((payload) => + updateProject(nextBase, payload.projectId, (project) => ({ + ...project, + ...(payload.title !== undefined ? { title: payload.title } : {}), + ...(payload.workspaceRoot !== undefined + ? { workspaceRoot: payload.workspaceRoot } + : {}), + ...(payload.defaultModelSelection !== undefined + ? { defaultModelSelection: payload.defaultModelSelection } + : {}), + ...(payload.scripts !== undefined ? { scripts: payload.scripts } : {}), + updatedAt: payload.updatedAt, + })), + ), ); case "project.deleted": return decodeForEvent(ProjectDeletedPayload, event.payload, event.type, "payload").pipe( - Effect.map((payload) => ({ - ...nextBase, - projects: nextBase.projects.map((project) => - project.id === payload.projectId - ? { - ...project, - deletedAt: payload.deletedAt, - updatedAt: payload.deletedAt, - } - : project, - ), - })), + Effect.map((payload) => + updateProject(nextBase, payload.projectId, (project) => ({ + ...project, + deletedAt: payload.deletedAt, + updatedAt: payload.deletedAt, + })), + ), ); case "thread.created": @@ -303,23 +319,27 @@ export function projectEvent( event.type, "thread", ); - const existing = nextBase.threads.find((entry) => entry.id === thread.id); return { ...nextBase, - threads: existing - ? nextBase.threads.map((entry) => (entry.id === thread.id ? thread : entry)) - : [...nextBase.threads, thread], + threads: HashMap.set(nextBase.threads, thread.id, thread), }; }); case "thread.deleted": + // Evict deleted threads from the in-memory model entirely rather than + // tombstoning them. The DB projection retains the row (it is the source + // of truth for downstream/HTTP reads and cleanup reactors consume the + // event stream, not this model), and no command legitimately targets an + // already-deleted thread. This is the primary fix for unbounded growth: + // a deleted thread's messages/activities/checkpoints are freed and it no + // longer costs anything on every subsequent event. The id is recorded in + // `deletedThreadIds` so `requireThreadAbsent` still rejects re-creating a + // thread with a previously-used id (the invariant the DB tombstone kept). return decodeForEvent(ThreadDeletedPayload, event.payload, event.type, "payload").pipe( Effect.map((payload) => ({ ...nextBase, - threads: updateThread(nextBase.threads, payload.threadId, { - deletedAt: payload.deletedAt, - updatedAt: payload.deletedAt, - }), + threads: HashMap.remove(nextBase.threads, payload.threadId), + deletedThreadIds: HashSet.add(nextBase.deletedThreadIds, payload.threadId), })), ); @@ -444,7 +464,7 @@ export function projectEvent( event.type, "payload", ); - const thread = nextBase.threads.find((entry) => entry.id === payload.threadId); + const thread = findThreadById(nextBase, payload.threadId); if (!thread) { return nextBase; } @@ -505,7 +525,7 @@ export function projectEvent( event.type, "payload", ); - const thread = nextBase.threads.find((entry) => entry.id === payload.threadId); + const thread = findThreadById(nextBase, payload.threadId); if (!thread) { return nextBase; } @@ -568,7 +588,7 @@ export function projectEvent( event.type, "payload", ); - const thread = nextBase.threads.find((entry) => entry.id === payload.threadId); + const thread = findThreadById(nextBase, payload.threadId); if (!thread) { return nextBase; } @@ -600,7 +620,7 @@ export function projectEvent( event.type, "payload", ); - const thread = nextBase.threads.find((entry) => entry.id === payload.threadId); + const thread = findThreadById(nextBase, payload.threadId); if (!thread) { return nextBase; } @@ -670,7 +690,7 @@ export function projectEvent( case "thread.reverted": return decodeForEvent(ThreadRevertedPayload, event.payload, event.type, "payload").pipe( Effect.map((payload) => { - const thread = nextBase.threads.find((entry) => entry.id === payload.threadId); + const thread = findThreadById(nextBase, payload.threadId); if (!thread) { return nextBase; } @@ -726,7 +746,7 @@ export function projectEvent( "payload", ).pipe( Effect.map((payload) => { - const thread = nextBase.threads.find((entry) => entry.id === payload.threadId); + const thread = findThreadById(nextBase, payload.threadId); if (!thread) { return nextBase; } diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index ee595b1f836..af086b9c8be 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -716,6 +716,7 @@ describe("VcsStatusBroadcaster", () => { yield* Deferred.await(remoteStarted); assert.equal(state.remoteStatusCalls, 1); + assert.equal(state.localStatusCalls, 1); yield* Scope.close(firstScope, Exit.void); assert.isTrue(Option.isNone(yield* Deferred.poll(remoteInterrupted))); @@ -723,6 +724,20 @@ describe("VcsStatusBroadcaster", () => { yield* Scope.close(secondScope, Exit.void).pipe(Effect.forkScoped); yield* Deferred.await(remoteInterrupted); assert.isTrue(Option.isSome(yield* Deferred.poll(remoteInterrupted))); + + const nextSnapshot = yield* Deferred.make(); + const nextScope = yield* Scope.make(); + yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => + event._tag === "snapshot" + ? Deferred.succeed(nextSnapshot, event).pipe(Effect.ignore) + : Effect.void, + ).pipe(Effect.forkIn(nextScope)); + yield* Deferred.await(nextSnapshot); + + // Releasing the final poller also evicts its cwd cache entry, so a later + // subscription reloads local status instead of retaining state forever. + assert.equal(state.localStatusCalls, 2); + yield* Scope.close(nextScope, Exit.void); }).pipe(Effect.provide(testLayer)); }); }); diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index b02ca9deb66..723f393f77c 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -475,10 +475,10 @@ export const make = Effect.gen(function* () { const releaseRemotePoller = Effect.fn("VcsStatusBroadcaster.releaseRemotePoller")(function* ( cwd: string, ) { - const pollerToInterrupt = yield* SynchronizedRef.modify(pollersRef, (activePollers) => { + const pollerToInterrupt = yield* SynchronizedRef.modifyEffect(pollersRef, (activePollers) => { const existing = activePollers.get(cwd); if (!existing) { - return [null, activePollers] as const; + return Effect.succeed([null, activePollers] as const); } if (existing.subscriberCount > 1) { @@ -487,12 +487,24 @@ export const make = Effect.gen(function* () { ...existing, subscriberCount: existing.subscriberCount - 1, }); - return [null, nextPollers] as const; + return Effect.succeed([null, nextPollers] as const); } const nextPollers = new Map(activePollers); nextPollers.delete(cwd); - return [existing.fiber, nextPollers] as const; + // Drop the cached status for this cwd in the same critical section that + // removes the poller, so a concurrent retainRemotePoller (which reloads + // the cache and installs a fresh poller) cannot have its new entry wiped + // by this release. Otherwise the cache grows one entry per cwd for the + // broadcaster's lifetime; a future subscriber re-loads and re-seeds it. + return Ref.update(cacheRef, (cache) => { + if (!cache.has(cwd)) { + return cache; + } + const nextCache = new Map(cache); + nextCache.delete(cwd); + return nextCache; + }).pipe(Effect.as([existing.fiber, nextPollers] as const)); }); if (pollerToInterrupt) { diff --git a/apps/web/src/browser/browserSurfaceStore.test.ts b/apps/web/src/browser/browserSurfaceStore.test.ts index ecfce8cb432..b17467d9bb5 100644 --- a/apps/web/src/browser/browserSurfaceStore.test.ts +++ b/apps/web/src/browser/browserSurfaceStore.test.ts @@ -62,17 +62,15 @@ describe("browserSurfaceStore", () => { }); }); - it("hides a surface when its current lease is released", () => { + it("removes a surface entry when its current lease is released", () => { const tabId = "released-browser-surface"; const lease = acquireBrowserSurface(tabId); lease.present({ x: 10, y: 20, width: 900, height: 640 }, true); lease.release(); + // A stale present() from the released lease must not resurrect the entry. lease.present({ x: 0, y: 0, width: 1, height: 1 }, true); - expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({ - visible: false, - owner: null, - }); + expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toBeUndefined(); }); }); diff --git a/apps/web/src/browser/browserSurfaceStore.ts b/apps/web/src/browser/browserSurfaceStore.ts index 58012a11a30..d023896cd4f 100644 --- a/apps/web/src/browser/browserSurfaceStore.ts +++ b/apps/web/src/browser/browserSurfaceStore.ts @@ -142,12 +142,14 @@ export const useBrowserSurfaceStore = create()((set) = set((state) => { const current = state.byTabId[tabId]; if (current?.owner !== owner) return state; - return { - byTabId: { - ...state.byTabId, - [tabId]: { ...current, visible: false, updatedAt: Date.now(), owner: null }, - }, - }; + // Delete the entry entirely instead of leaving a released tombstone + // ({ visible: false, owner: null }). Readers only consider `visible` + // entries, so removal is behavior-preserving, and it stops byTabId from + // accumulating one dead entry per preview tab ever opened. A later + // re-claim of the same tabId starts fresh, which is correct since release + // means the surface was torn down. + const { [tabId]: _released, ...byTabId } = state.byTabId; + return { byTabId }; }), })); diff --git a/apps/web/src/hooks/useThreadActions.ts b/apps/web/src/hooks/useThreadActions.ts index 724b23b9d45..21c0037a27c 100644 --- a/apps/web/src/hooks/useThreadActions.ts +++ b/apps/web/src/hooks/useThreadActions.ts @@ -20,6 +20,10 @@ import { useCallback, useMemo, useRef } from "react"; import { getFallbackThreadIdAfterDelete } from "../components/Sidebar.logic"; import { useComposerDraftStore } from "../composerDraftStore"; +import { useDiffPanelStore } from "../diffPanelStore"; +import { removePreviewThread } from "../previewStateStore"; +import { useRightPanelStore } from "../rightPanelStore"; +import { useUiStateStore } from "../uiStateStore"; import { terminalEnvironment } from "../state/terminal"; import { threadEnvironment } from "../state/threads"; import { vcsEnvironment } from "../state/vcs"; @@ -138,6 +142,23 @@ export function getWorktreeRemovalAction({ return confirmWorktreeRemoval ? "confirm" : "remove"; } +/** + * Prune per-thread client state that would otherwise accumulate forever. + * + * These stores keep a per-thread entry (preview atom, right-panel surfaces, + * diff-panel selection) that is persisted to localStorage and was never cleaned + * up on thread deletion/archival — a long-lived tab leaked one entry per thread + * ever visited. The cleanup functions already existed but had no callers; this + * wires them into the deletion/archival flow. + */ +function clearPerThreadClientState(ref: ScopedThreadRef): void { + removePreviewThread(ref); + useRightPanelStore.getState().removeThread(ref); + useDiffPanelStore.getState().removeThread(ref); + // uiStateStore is keyed by the scoped thread key (see ChatView.markThreadVisited). + useUiStateStore.getState().removeThread(scopedThreadKey(ref)); +} + export function useThreadActions() { const closeTerminal = useAtomCommand(terminalEnvironment.close); const archiveThreadMutation = useAtomCommand(threadEnvironment.archive, { @@ -359,6 +380,9 @@ export function useThreadActions() { }); if (result._tag === "Success") { refreshArchivedThreadsForEnvironment(target.environmentId); + clearComposerDraftForThread(target); + clearTerminalUiState(target); + clearPerThreadClientState(target); } return result; } @@ -452,6 +476,7 @@ export function useThreadActions() { threadRef, ); clearTerminalUiState(threadRef); + clearPerThreadClientState(threadRef); if (shouldNavigateToFallback) { if (fallbackThreadId) { diff --git a/apps/web/src/previewStateStore.ts b/apps/web/src/previewStateStore.ts index 34553d6f80b..e1d994d2422 100644 --- a/apps/web/src/previewStateStore.ts +++ b/apps/web/src/previewStateStore.ts @@ -52,9 +52,20 @@ const emptyPreviewStateAtom = Atom.make(EMPTY_THREAD_PREVIEW Atom.withLabel("preview:empty-thread"), ); +// Previously `Atom.keepAlive`, which pinned one atom node per thread key ever +// visited in the registry for the process lifetime — an unbounded leak in a +// long-lived tab. Idle-TTL lets the registry evict a thread's preview atom once +// nothing observes it, matching the pattern used for other per-resource atom +// families (see browser/previewWebviewConfigState.ts). Threads with live +// preview sessions stay resident because the observed aggregate +// `activePreviewSessionsAtom` reads them, keeping them out of the idle state; +// only closed/idle threads are collected. An evicted idle thread re-seeds from +// EMPTY and is re-populated by the next server snapshot, so eviction is safe. +const PREVIEW_STATE_IDLE_TTL_MS = 5 * 60_000; + export const previewStateAtom = Atom.family((threadKey: string) => Atom.make(EMPTY_THREAD_PREVIEW_STATE).pipe( - Atom.keepAlive, + Atom.setIdleTTL(PREVIEW_STATE_IDLE_TTL_MS), Atom.withLabel(`preview:thread:${threadKey}`), ), ); diff --git a/apps/web/src/uiStateStore.test.ts b/apps/web/src/uiStateStore.test.ts index 30450287353..5bb273be45a 100644 --- a/apps/web/src/uiStateStore.test.ts +++ b/apps/web/src/uiStateStore.test.ts @@ -9,6 +9,7 @@ import { PERSISTED_STATE_KEY, type PersistedUiState, persistState, + removeThreadUiState, reorderProjects, resolveProjectExpanded, setDefaultAdvertisedEndpointKey, @@ -39,6 +40,27 @@ describe("uiStateStore pure functions", () => { expect(markThreadVisited(visited, threadId, "not-a-date")).toBe(visited); }); + it("removes all per-thread ui state for a deleted thread", () => { + const threadId = ThreadId.make("thread-1"); + const otherId = ThreadId.make("thread-2"); + const state = makeUiState({ + threadLastVisitedAtById: { + [threadId]: "2026-02-25T12:30:00.000Z", + [otherId]: "2026-02-25T12:31:00.000Z", + }, + threadChangedFilesExpandedById: { + [threadId]: { "turn-1": false }, + [otherId]: { "turn-1": false }, + }, + }); + + const next = removeThreadUiState(state, threadId); + expect(next.threadLastVisitedAtById).toEqual({ [otherId]: "2026-02-25T12:31:00.000Z" }); + expect(next.threadChangedFilesExpandedById).toEqual({ [otherId]: { "turn-1": false } }); + // No-op (same reference) when the thread has no state. + expect(removeThreadUiState(next, threadId)).toBe(next); + }); + it("marks a completed thread unread using the server completion timestamp", () => { const threadId = ThreadId.make("thread-1"); const initialState = makeUiState({ diff --git a/apps/web/src/uiStateStore.ts b/apps/web/src/uiStateStore.ts index 5d744d540a5..b0a3bd517b3 100644 --- a/apps/web/src/uiStateStore.ts +++ b/apps/web/src/uiStateStore.ts @@ -270,6 +270,27 @@ export function markThreadUnread( }; } +/** + * Drop all per-thread UI state for a deleted thread. Without this, both + * `threadLastVisitedAtById` and `threadChangedFilesExpandedById` grew one entry + * per thread ever visited and were persisted to localStorage indefinitely. + */ +export function removeThreadUiState(state: UiState, threadId: string): UiState { + const hasVisited = threadId in state.threadLastVisitedAtById; + const hasChangedFiles = threadId in state.threadChangedFilesExpandedById; + if (!hasVisited && !hasChangedFiles) { + return state; + } + const { [threadId]: _visited, ...threadLastVisitedAtById } = state.threadLastVisitedAtById; + const { [threadId]: _changed, ...threadChangedFilesExpandedById } = + state.threadChangedFilesExpandedById; + return { + ...state, + threadLastVisitedAtById, + threadChangedFilesExpandedById, + }; +} + export function setThreadChangedFilesExpanded( state: UiState, threadId: string, @@ -384,6 +405,7 @@ export function reorderProjects( interface UiStateStore extends UiState { markThreadVisited: (threadId: string, visitedAt: string) => void; markThreadUnread: (threadId: string, latestTurnCompletedAt: string | null | undefined) => void; + removeThread: (threadId: string) => void; setThreadChangedFilesExpanded: (threadId: string, turnId: string, expanded: boolean) => void; setDefaultAdvertisedEndpointKey: (key: string | null) => void; setProjectExpanded: (projectIds: string | readonly string[], expanded: boolean) => void; @@ -400,6 +422,7 @@ export const useUiStateStore = create((set) => ({ set((state) => markThreadVisited(state, threadId, visitedAt)), markThreadUnread: (threadId, latestTurnCompletedAt) => set((state) => markThreadUnread(state, threadId, latestTurnCompletedAt)), + removeThread: (threadId) => set((state) => removeThreadUiState(state, threadId)), setThreadChangedFilesExpanded: (threadId, turnId, expanded) => set((state) => setThreadChangedFilesExpanded(state, threadId, turnId, expanded)), setDefaultAdvertisedEndpointKey: (key) => From 407732fb7d199bfb28fd4d7d7c549decd0c96513 Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sat, 25 Jul 2026 14:18:12 +0200 Subject: [PATCH 05/11] feat(web): import answered question timeline (upstream #4506) (#44) Source: https://github.com/pingdotgg/t3code/pull/4506 Source SHA: f7eaa00b99e67a9c1caf09e2fe6ff1f536f66b91 Imported unchanged as one candidate provenance commit. --- .github/upstream-candidates.json | 6 + .../chat/MessagesTimeline.logic.test.ts | 146 +++++++++ .../components/chat/MessagesTimeline.logic.ts | 36 ++- .../components/chat/MessagesTimeline.test.tsx | 83 ++++++ .../src/components/chat/MessagesTimeline.tsx | 112 +++++++ apps/web/src/session-logic.test.ts | 281 ++++++++++++++++++ apps/web/src/session-logic.ts | 236 +++++++++++++++ 7 files changed, 898 insertions(+), 2 deletions(-) diff --git a/.github/upstream-candidates.json b/.github/upstream-candidates.json index c4d320ef6d2..e7c15c52c70 100644 --- a/.github/upstream-candidates.json +++ b/.github/upstream-candidates.json @@ -18,6 +18,12 @@ "sourceSha": "56b6615afdfe3804a466e33cbab9056b8981f217", "status": "active", "purpose": "Bound long-lived orchestration, browser, preview, and VCS in-memory state" + }, + { + "upstreamPr": 4506, + "sourceSha": "f7eaa00b99e67a9c1caf09e2fe6ff1f536f66b91", + "status": "active", + "purpose": "Show answered provider questions in the web thread timeline" } ] } diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts index 350278734cb..28375054178 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts @@ -1099,6 +1099,152 @@ describe("deriveMessagesTimelineRows", () => { expanded: true, }); }); + + it("keeps a clarifying-question exchange out of the collapsed work group", () => { + const userInput = { + requestId: "req-1", + answered: true, + questions: [ + { + id: "Approach?", + header: "Approach", + question: "Approach?", + multiSelect: false, + options: [{ label: "Ship it", description: "Merge as-is" }], + selectedLabels: ["Ship it"], + }, + ], + }; + const rows = deriveMessagesTimelineRows({ + timelineEntries: [ + { + id: "work-entry-1", + kind: "work" as const, + createdAt: "2026-01-01T00:00:01Z", + entry: { + id: "work-1", + createdAt: "2026-01-01T00:00:01Z", + label: "read", + tone: "tool" as const, + }, + }, + { + id: "user-input-entry", + kind: "work" as const, + createdAt: "2026-01-01T00:00:02Z", + entry: { + id: "user-input-1", + createdAt: "2026-01-01T00:00:02Z", + label: "Question: Approach", + tone: "info" as const, + userInput, + }, + }, + { + id: "work-entry-2", + kind: "work" as const, + createdAt: "2026-01-01T00:00:03Z", + entry: { + id: "work-2", + createdAt: "2026-01-01T00:00:03Z", + label: "edit", + tone: "tool" as const, + }, + }, + ], + isWorking: false, + activeTurnStartedAt: null, + turnDiffSummaryByAssistantMessageId: new Map(), + revertTurnCountByUserMessageId: new Map(), + }); + + expect(rows.map((row) => row.id)).toEqual(["work-entry-1", "user-input-entry", "work-entry-2"]); + expect(rows.find((row) => row.kind === "user-input")).toMatchObject({ userInput }); + }); + + it("keeps a clarifying-question exchange visible after its turn folds", () => { + const rows = deriveMessagesTimelineRows({ + timelineEntries: [ + { + id: "user-entry", + kind: "message" as const, + createdAt: "2026-01-01T00:00:00Z", + message: { + id: "user-1" as never, + role: "user" as const, + text: "ship the fix", + turnId: null, + createdAt: "2026-01-01T00:00:00Z", + updatedAt: "2026-01-01T00:00:00Z", + streaming: false, + }, + }, + { + id: "work-entry-1", + kind: "work" as const, + createdAt: "2026-01-01T00:00:05Z", + entry: { + id: "work-1", + createdAt: "2026-01-01T00:00:05Z", + turnId: "turn-1" as never, + label: "Ran command", + tone: "tool" as const, + }, + }, + { + id: "user-input-entry", + kind: "work" as const, + createdAt: "2026-01-01T00:00:08Z", + entry: { + id: "user-input-1", + createdAt: "2026-01-01T00:00:08Z", + turnId: "turn-1" as never, + label: "Question: Approach", + tone: "info" as const, + userInput: { + requestId: "req-1", + answered: true, + questions: [ + { + id: "Approach?", + header: "Approach", + question: "Approach?", + multiSelect: false, + options: [{ label: "Ship it", description: "Merge as-is" }], + selectedLabels: ["Ship it"], + }, + ], + }, + }, + }, + { + id: "assistant-final-entry", + kind: "message" as const, + createdAt: "2026-01-01T00:00:20Z", + message: { + id: "assistant-final" as never, + role: "assistant" as const, + text: "Shipped", + turnId: "turn-1" as never, + createdAt: "2026-01-01T00:00:20Z", + updatedAt: "2026-01-01T00:00:22Z", + streaming: false, + }, + }, + ], + isWorking: false, + activeTurnStartedAt: null, + turnDiffSummaryByAssistantMessageId: new Map(), + revertTurnCountByUserMessageId: new Map(), + }); + + expect(rows.map((row) => row.id)).toEqual([ + "user-entry", + "turn-fold:turn-1", + "user-input-entry", + "assistant-final-entry", + ]); + }); }); describe("computeStableMessagesTimelineRows", () => { diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.ts index 69cd380b577..2b7d062eafb 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts @@ -5,6 +5,7 @@ import { workLogEntryIsToolLike, type TimelineEntry, type WorkLogEntry, + type WorkLogUserInput, } from "../../session-logic"; import { type ChatMessage, type ProposedPlan, type TurnDiffSummary } from "../../types"; import { type MessageId, type OrchestrationLatestTurn, type TurnId } from "@t3tools/contracts"; @@ -220,6 +221,13 @@ export type MessagesTimelineRow = createdAt: string; proposedPlan: ProposedPlan; } + | { + kind: "user-input"; + id: string; + createdAt: string; + entry: WorkLogEntry; + userInput: WorkLogUserInput; + } | { kind: "working"; id: string; createdAt: string | null }; export interface StableMessagesTimelineRowsState { @@ -397,9 +405,15 @@ function deriveTurnFolds(input: { } const hiddenEntryIds = new Set(); for (const entry of group.entries) { - if (entry.id !== group.terminalEntry?.id) { - hiddenEntryIds.add(entry.id); + if (entry.id === group.terminalEntry?.id) { + continue; } + // A clarifying question and its answer record a decision the user made; + // keep them readable once the turn settles instead of folding them away. + if (entry.kind === "work" && entry.entry.userInput !== undefined) { + continue; + } + hiddenEntryIds.add(entry.id); } if (hiddenEntryIds.size === 0) { continue; @@ -505,6 +519,20 @@ export function deriveMessagesTimelineRows(input: { } if (timelineEntry.kind === "work") { + // Clarifying-question exchanges are conversation, not tool noise: they get + // their own row so neither work-group collapsing nor a turn fold hides them. + const userInput = timelineEntry.entry.userInput; + if (userInput) { + nextRows.push({ + kind: "user-input", + id: timelineEntry.id, + createdAt: timelineEntry.createdAt, + entry: timelineEntry.entry, + userInput, + }); + continue; + } + const groupedEntries = [timelineEntry.entry]; let cursor = index + 1; while (cursor < input.timelineEntries.length) { @@ -512,6 +540,7 @@ export function deriveMessagesTimelineRows(input: { if ( !nextEntry || nextEntry.kind !== "work" || + nextEntry.entry.userInput !== undefined || collapsedEntryIds.has(nextEntry.id) || foldsByAnchorEntryId.has(nextEntry.id) ) { @@ -658,6 +687,9 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean case "work": return Equal.equals(a.groupedEntries, (b as typeof a).groupedEntries); + case "user-input": + return Equal.equals(a.userInput, (b as typeof a).userInput); + case "work-toggle": { const bw = b as typeof a; return ( diff --git a/apps/web/src/components/chat/MessagesTimeline.test.tsx b/apps/web/src/components/chat/MessagesTimeline.test.tsx index fe106f8ba19..b0c9f4e9cb2 100644 --- a/apps/web/src/components/chat/MessagesTimeline.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.test.tsx @@ -527,6 +527,89 @@ describe("MessagesTimeline", () => { expect(markup).toContain("Work Log"); }); + it("renders a clarifying question with the answer it received", () => { + const markup = renderToStaticMarkup( + , + ); + + expect(markup).toContain("Approach"); + expect(markup).toContain("How should we proceed?"); + expect(markup).toContain("Iterate"); + expect(markup).toContain("Show options"); + // The chosen answer reads on its own; alternatives stay behind the toggle. + expect(markup).not.toContain("Another review round"); + }); + + it("marks an unanswered clarifying question as awaiting a reply", () => { + const markup = renderToStaticMarkup( + , + ); + + expect(markup).toContain("Awaiting your answer"); + expect(markup).not.toContain("Work Log"); + }); + it("formats changed file paths from the workspace root", () => { const markup = renderToStaticMarkup( [number]; type TimelineMessage = Extract["message"]; type TimelineWorkEntry = Extract["groupedEntries"][number]; type TimelineRow = MessagesTimelineRow; +type TimelineUserInputQuestion = Extract< + MessagesTimelineRow, + { kind: "user-input" } +>["userInput"]["questions"][number]; const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: TimelineRow }) { return ( @@ -939,6 +943,7 @@ const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: Time ) : null} {row.kind === "proposed-plan" ? : null} + {row.kind === "user-input" ? : null} {row.kind === "working" ? : null} ); @@ -1311,6 +1316,113 @@ function WorkGroupToggleTimelineRow({ ); } +/** + * A clarifying-question round trip: what the agent asked and what the user + * picked. The interactive prompt lives in the composer, so this row is the + * thread's only lasting record of the exchange. + */ +const UserInputTimelineRow = memo(function UserInputTimelineRow({ + row, +}: { + row: Extract; +}) { + const [showOptions, setShowOptions] = useState(false); + const { userInput } = row; + const hasUnpickedOptions = userInput.questions.some( + (question) => question.options.length > question.selectedLabels.length, + ); + + return ( +
+ {userInput.questions.map((question, index) => ( +
0 && "mt-3 border-t border-border/40 pt-3")}> +
+ + + {question.header} + +
+

{question.question}

+ + {showOptions && question.options.length > 0 ? ( +
    + {question.options.map((option) => { + const picked = question.selectedLabels.includes(option.label); + return ( +
  • + {option.label} + {option.description && option.description !== option.label ? ( + {option.description} + ) : null} +
  • + ); + })} +
+ ) : null} +
+ ))} + {hasUnpickedOptions ? ( + + ) : null} +
+ ); +}); + +function UserInputAnswer({ + answered, + question, +}: { + answered: boolean; + question: TimelineUserInputQuestion; +}) { + const { selectedLabels, customAnswer } = question; + + if (selectedLabels.length === 0 && !customAnswer) { + return ( +

+ {answered ? "No answer recorded" : "Awaiting your answer"} +

+ ); + } + + return ( +
+ {selectedLabels.map((label) => ( +

+ + {label} +

+ ))} + {customAnswer ? ( +

+ + {customAnswer} +

+ ) : null} +
+ ); +} + /** Subscribes directly to the UI state store for expand/collapse state, * so toggling re-renders only this component — not the entire list. */ const AssistantChangedFilesSection = memo(function AssistantChangedFilesSection({ diff --git a/apps/web/src/session-logic.test.ts b/apps/web/src/session-logic.test.ts index 0f12e672f66..44d98aa0435 100644 --- a/apps/web/src/session-logic.test.ts +++ b/apps/web/src/session-logic.test.ts @@ -1492,6 +1492,287 @@ describe("deriveWorkLogEntries", () => { }); }); +describe("deriveWorkLogEntries clarifying questions", () => { + function makeQuestionActivities(options: { + readonly multiSelect?: boolean; + readonly answers?: Record; + }): OrchestrationThreadActivity[] { + const requested = makeActivity({ + id: "ask", + createdAt: "2026-02-23T00:00:01.000Z", + kind: "user-input.requested", + summary: "User input requested", + tone: "info", + payload: { + requestId: "req-1", + questions: [ + { + id: "How should we proceed?", + header: "Approach", + question: "How should we proceed?", + options: [ + { label: "Ship it", description: "Merge as-is" }, + { label: "Iterate", description: "Another review round" }, + ], + multiSelect: options.multiSelect === true, + }, + ], + }, + }); + if (!options.answers) { + return [requested]; + } + return [ + requested, + makeActivity({ + id: "answer", + createdAt: "2026-02-23T00:00:09.000Z", + kind: "user-input.resolved", + summary: "User input submitted", + tone: "info", + payload: { requestId: "req-1", answers: options.answers }, + }), + ]; + } + + it("merges the request and its answer into one entry carrying the picked option", () => { + const entries = deriveWorkLogEntries( + makeQuestionActivities({ answers: { "How should we proceed?": "Iterate" } }), + ); + + expect(entries).toHaveLength(1); + expect(entries[0]?.label).toBe("Question: Approach"); + expect(entries[0]?.userInput).toEqual({ + requestId: "req-1", + answered: true, + questions: [ + { + id: "How should we proceed?", + header: "Approach", + question: "How should we proceed?", + multiSelect: false, + options: [ + { label: "Ship it", description: "Merge as-is" }, + { label: "Iterate", description: "Another review round" }, + ], + selectedLabels: ["Iterate"], + }, + ], + }); + }); + + it("keeps a free-text answer that matched no option", () => { + const entries = deriveWorkLogEntries( + makeQuestionActivities({ answers: { "How should we proceed?": " Split it in two " } }), + ); + + expect(entries[0]?.userInput?.questions[0]?.selectedLabels).toEqual([]); + expect(entries[0]?.userInput?.questions[0]?.customAnswer).toBe("Split it in two"); + }); + + it("lists multi-select answers in question order", () => { + const entries = deriveWorkLogEntries( + makeQuestionActivities({ + multiSelect: true, + answers: { "How should we proceed?": ["Iterate", "Ship it"] }, + }), + ); + + expect(entries[0]?.userInput?.questions[0]?.selectedLabels).toEqual(["Ship it", "Iterate"]); + }); + + it("classifies comma-joined multi-select answers from OpenCode", () => { + const entries = deriveWorkLogEntries( + makeQuestionActivities({ + multiSelect: true, + answers: { "How should we proceed?": "Iterate, Ship it" }, + }), + ); + + expect(entries[0]?.userInput?.questions[0]).toMatchObject({ + selectedLabels: ["Ship it", "Iterate"], + }); + expect(entries[0]?.userInput?.questions[0]?.customAnswer).toBeUndefined(); + }); + + it("keeps an option label that contains a comma whole", () => { + const entries = deriveWorkLogEntries([ + makeActivity({ + id: "ask", + createdAt: "2026-02-23T00:00:01.000Z", + kind: "user-input.requested", + summary: "User input requested", + tone: "info", + payload: { + requestId: "req-1", + questions: [ + { + id: "Scope?", + header: "Scope", + question: "Scope?", + options: [ + { label: "Small, safe change", description: "Minimal diff" }, + { label: "Tests", description: "Add coverage" }, + ], + multiSelect: true, + }, + ], + }, + }), + makeActivity({ + id: "answer", + createdAt: "2026-02-23T00:00:09.000Z", + kind: "user-input.resolved", + summary: "User input submitted", + tone: "info", + payload: { requestId: "req-1", answers: { "Scope?": "Small, safe change, Tests" } }, + }), + ]); + + expect(entries[0]?.userInput?.questions[0]?.selectedLabels).toEqual([ + "Small, safe change", + "Tests", + ]); + expect(entries[0]?.userInput?.questions[0]?.customAnswer).toBeUndefined(); + }); + + it("keeps a multi-select free-text answer containing a comma intact", () => { + const entries = deriveWorkLogEntries( + makeQuestionActivities({ + multiSelect: true, + answers: { "How should we proceed?": "Ship it, but revert the migration first" }, + }), + ); + + expect(entries[0]?.userInput?.questions[0]?.selectedLabels).toEqual([]); + expect(entries[0]?.userInput?.questions[0]?.customAnswer).toBe( + "Ship it, but revert the migration first", + ); + }); + + it("shows the question while it is still unanswered", () => { + const entries = deriveWorkLogEntries(makeQuestionActivities({})); + + expect(entries).toHaveLength(1); + expect(entries[0]?.userInput?.answered).toBe(false); + expect(entries[0]?.userInput?.questions[0]?.selectedLabels).toEqual([]); + }); + + it("drops the duplicate AskUserQuestion tool rows", () => { + const entries = deriveWorkLogEntries([ + makeActivity({ + id: "tool-started", + createdAt: "2026-02-23T00:00:00.500Z", + kind: "tool.started", + summary: "Tool call started", + payload: { itemType: "dynamic_tool_call", detail: "AskUserQuestion: {}" }, + }), + makeActivity({ + id: "tool-completed", + createdAt: "2026-02-23T00:00:10.000Z", + kind: "tool.completed", + summary: "Tool call", + payload: { + itemType: "dynamic_tool_call", + detail: 'AskUserQuestion: {"questions":[{"question":"How should we proceed?"}]}', + data: { toolName: "AskUserQuestion" }, + }, + }), + ...makeQuestionActivities({ answers: { "How should we proceed?": "Ship it" } }), + ]); + + expect(entries).toHaveLength(1); + expect(entries[0]?.userInput?.answered).toBe(true); + }); + + it("still shows the answer when the request activity is out of view", () => { + const entries = deriveWorkLogEntries([ + makeActivity({ + id: "answer-only", + createdAt: "2026-02-23T00:00:09.000Z", + kind: "user-input.resolved", + summary: "User input submitted", + tone: "info", + payload: { + requestId: "req-1", + answers: { "How should we proceed?": "Ship it" }, + }, + }), + ]); + + expect(entries).toHaveLength(1); + expect(entries[0]?.userInput).toEqual({ + requestId: "req-1", + answered: true, + questions: [ + { + id: "How should we proceed?", + header: "Answer", + question: "How should we proceed?", + multiSelect: false, + options: [], + selectedLabels: [], + customAnswer: "Ship it", + }, + ], + }); + }); + + it("falls back to the generic row when the questions cannot be parsed", () => { + const entries = deriveWorkLogEntries([ + makeActivity({ + id: "ask-broken", + createdAt: "2026-02-23T00:00:01.000Z", + kind: "user-input.requested", + summary: "User input requested", + tone: "info", + payload: { requestId: "req-1", questions: [{ header: "Approach" }] }, + }), + ]); + + expect(entries).toHaveLength(1); + expect(entries[0]?.userInput).toBeUndefined(); + expect(entries[0]?.label).toBe("User input requested"); + }); + + it("replaces an unparsed request row when its answer arrives", () => { + const entries = deriveWorkLogEntries([ + makeActivity({ + id: "ask-broken", + createdAt: "2026-02-23T00:00:01.000Z", + kind: "user-input.requested", + summary: "User input requested", + tone: "info", + payload: { requestId: "req-1", questions: [{ header: "Approach" }] }, + }), + makeActivity({ + id: "answer", + createdAt: "2026-02-23T00:00:09.000Z", + kind: "user-input.resolved", + summary: "User input submitted", + tone: "info", + payload: { + requestId: "req-1", + answers: { "How should we proceed?": "Ship it" }, + }, + }), + ]); + + expect(entries).toHaveLength(1); + expect(entries[0]?.id).toBe("ask-broken"); + expect(entries[0]?.userInput).toMatchObject({ + requestId: "req-1", + answered: true, + questions: [ + { + question: "How should we proceed?", + customAnswer: "Ship it", + }, + ], + }); + }); +}); + describe("deriveTimelineEntries", () => { it("includes proposed plans alongside messages and work entries in chronological order", () => { const entries = deriveTimelineEntries( diff --git a/apps/web/src/session-logic.ts b/apps/web/src/session-logic.ts index 5d5051f748e..af2601ea48a 100644 --- a/apps/web/src/session-logic.ts +++ b/apps/web/src/session-logic.ts @@ -60,6 +60,29 @@ export type WorkLogToolLifecycleStatus = | "declined" | "stopped"; +/** One question from a clarifying-question round trip, with what the user picked. */ +export interface WorkLogUserInputQuestion { + id: string; + header: string; + question: string; + multiSelect: boolean; + options: ReadonlyArray<{ label: string; description: string }>; + /** Chosen option labels, in the order the question listed them. */ + selectedLabels: ReadonlyArray; + /** Free-text answer that matched no option (the "Other" escape hatch). */ + customAnswer?: string; +} + +/** + * A `user-input.requested` / `user-input.resolved` pair merged into one entry so + * the timeline can show the question alongside the answer it received. + */ +export interface WorkLogUserInput { + requestId: string; + answered: boolean; + questions: ReadonlyArray; +} + export interface WorkLogEntry { id: string; createdAt: string; @@ -78,6 +101,8 @@ export interface WorkLogEntry { toolLifecycleStatus?: WorkLogToolLifecycleStatus; /** Originating orchestration activity kind (e.g. `user-input.requested`) for row chrome. */ sourceActivityKind?: OrchestrationThreadActivity["kind"]; + /** Present on clarifying-question entries; rendered as a Q&A card, never folded away. */ + userInput?: WorkLogUserInput; } interface DerivedWorkLogEntry extends WorkLogEntry { @@ -458,6 +483,152 @@ function parseUserInputQuestions( return parsed.length > 0 ? parsed : null; } +function parseUserInputAnswerValues(value: unknown): ReadonlyArray { + const values = typeof value === "string" ? [value] : Array.isArray(value) ? value : []; + return values.flatMap((entry) => { + if (typeof entry !== "string") return []; + const trimmed = entry.trim(); + return trimmed.length > 0 ? [trimmed] : []; + }); +} + +/** + * OpenCode reports a multi-select reply as one `", "`-joined string rather than + * a list. Consume the value label by label, longest first, so a label that + * itself contains a comma survives; anything that is not a run of option labels + * is left alone as free text. + */ +function splitJoinedOptionLabels( + value: string, + optionLabels: ReadonlySet, +): ReadonlyArray | null { + const labelsByLength = [...optionLabels].toSorted((left, right) => right.length - left.length); + const picked: string[] = []; + let rest = value.trim(); + while (rest.length > 0) { + const label = labelsByLength.find( + (candidate) => rest === candidate || rest.startsWith(`${candidate},`), + ); + if (label === undefined) { + return null; + } + picked.push(label); + rest = rest.slice(label.length).replace(/^,\s*/, ""); + } + return picked.length > 1 ? picked : null; +} + +/** Structural shape shared by a freshly parsed question and an already-derived one. */ +interface AskedUserInputQuestion { + readonly id: string; + readonly header: string; + readonly question: string; + readonly multiSelect?: boolean | undefined; + readonly options: ReadonlyArray<{ readonly label: string; readonly description: string }>; +} + +/** Pairs one asked question with the answer recorded for it, when there is one. */ +function toWorkLogUserInputQuestion( + question: AskedUserInputQuestion, + answers: Record | null, +): WorkLogUserInputQuestion { + const optionLabels = new Set(question.options.map((option) => option.label)); + const reported = parseUserInputAnswerValues(answers?.[question.id]); + const [onlyValue] = reported; + const answered = + (question.multiSelect === true && + reported.length === 1 && + onlyValue !== undefined && + !optionLabels.has(onlyValue) + ? splitJoinedOptionLabels(onlyValue, optionLabels) + : null) ?? reported; + // Keep question order rather than answer order so multi-select reads consistently. + const selectedLabels = question.options + .map((option) => option.label) + .filter((label) => answered.includes(label)); + const customAnswer = answered.filter((value) => !optionLabels.has(value)).join("\n"); + return { + id: question.id, + header: question.header, + question: question.question, + multiSelect: question.multiSelect === true, + options: question.options.map((option) => ({ + label: option.label, + description: option.description, + })), + selectedLabels, + ...(customAnswer.length > 0 ? { customAnswer } : {}), + }; +} + +/** + * `user-input.resolved` without its request in view (older activity trimmed + * away) still carries the questions as answer keys — enough to show the answer. + */ +function toOrphanedWorkLogUserInputQuestions( + answers: Record, +): WorkLogUserInputQuestion[] { + return Object.entries(answers).flatMap(([question, value]) => { + const answered = parseUserInputAnswerValues(value); + if (answered.length === 0) return []; + return [ + { + id: question, + header: "Answer", + question, + multiSelect: answered.length > 1, + options: [], + selectedLabels: [], + customAnswer: answered.join("\n"), + }, + ]; + }); +} + +function toWorkLogUserInputEntry( + activity: OrchestrationThreadActivity, + userInput: WorkLogUserInput, +): DerivedWorkLogEntry { + return { + id: activity.id, + createdAt: activity.createdAt, + turnId: activity.turnId, + label: userInputWorkLogLabel(userInput.questions), + tone: "info", + activityKind: activity.kind, + userInput, + }; +} + +function userInputWorkLogLabel(questions: ReadonlyArray): string { + const [first] = questions; + if (questions.length === 1 && first) { + return `Question: ${first.header}`; + } + return `${questions.length} questions`; +} + +/** + * Claude surfaces `AskUserQuestion` through the user-input event channel, so its + * tool row is a duplicate whose only content is the raw question JSON. + */ +function isUserInputToolActivity(activity: OrchestrationThreadActivity): boolean { + if ( + activity.kind !== "tool.started" && + activity.kind !== "tool.updated" && + activity.kind !== "tool.completed" + ) { + return false; + } + const payload = asRecord(activity.payload); + const toolName = asTrimmedString(asRecord(payload?.data)?.toolName); + if (toolName === "AskUserQuestion") { + return true; + } + // `tool.started` lands before any input streams, leaving only the detail prefix. + return asTrimmedString(payload?.detail)?.startsWith("AskUserQuestion:") === true; +} + export function derivePendingUserInputs( activities: ReadonlyArray, ): PendingUserInput[] { @@ -629,12 +800,77 @@ export function deriveWorkLogEntries( ): WorkLogEntry[] { const ordered = [...activities].toSorted(compareActivitiesByOrder); const entries: DerivedWorkLogEntry[] = []; + // Answers arrive in a separate activity from the questions; fold them back + // into the entry that asked, so one round trip renders as one Q&A card. + const userInputEntryIndexByRequestId = new Map(); for (const activity of ordered) { if (activity.kind === "tool.started") continue; if (activity.kind === "task.started") continue; if (activity.kind === "context-window.updated") continue; if (activity.summary === "Checkpoint captured") continue; if (isPlanBoundaryToolActivity(activity)) continue; + if (isUserInputToolActivity(activity)) continue; + + if (activity.kind === "user-input.requested") { + const payload = asRecord(activity.payload); + const requestId = asTrimmedString(payload?.requestId); + const questions = parseUserInputQuestions(payload); + if (requestId) { + userInputEntryIndexByRequestId.set(requestId, entries.length); + if (questions) { + const derived = toWorkLogUserInputEntry(activity, { + requestId, + answered: false, + questions: questions.map((question) => toWorkLogUserInputQuestion(question, null)), + }); + entries.push(derived); + continue; + } + } + } + + if (activity.kind === "user-input.resolved") { + const payload = asRecord(activity.payload); + const requestId = asTrimmedString(payload?.requestId); + const answers = asRecord(payload?.answers); + const askedIndex = requestId ? userInputEntryIndexByRequestId.get(requestId) : undefined; + if (askedIndex !== undefined) { + const asked = entries[askedIndex]; + if (asked?.userInput) { + const questions = asked.userInput.questions.map((question) => + toWorkLogUserInputQuestion(question, answers), + ); + entries[askedIndex] = { + ...asked, + label: userInputWorkLogLabel(questions), + userInput: { ...asked.userInput, answered: true, questions }, + }; + continue; + } + if (asked && requestId && answers) { + const questions = toOrphanedWorkLogUserInputQuestions(answers); + if (questions.length > 0) { + entries[askedIndex] = { + ...asked, + label: userInputWorkLogLabel(questions), + tone: "info", + userInput: { requestId, answered: true, questions }, + }; + } + } + // The matching request already represents this lifecycle. Keep one + // generic row when neither side has enough structure for a Q&A card. + continue; + } + if (requestId && answers) { + const questions = toOrphanedWorkLogUserInputQuestions(answers); + if (questions.length > 0) { + entries.push(toWorkLogUserInputEntry(activity, { requestId, answered: true, questions })); + continue; + } + } + } + entries.push(toDerivedWorkLogEntry(activity)); } return collapseDerivedWorkLogEntries(entries).map((entry) => { From ed1916b135f2df9e15afb75338b635c5091fe922 Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sat, 25 Jul 2026 19:58:05 +0200 Subject: [PATCH 06/11] feat(orchestrator): import upstream follow-up queue (#4245) (#63) --- .github/upstream-candidates.json | 6 + apps/mobile/src/lib/threadActivity.test.ts | 2 + .../Layers/OrchestrationEngine.test.ts | 4 + .../Layers/ProjectionPipeline.ts | 109 +++- .../Layers/ProjectionSnapshotQuery.test.ts | 2 + .../Layers/ProjectionSnapshotQuery.ts | 231 ++++++++- .../Layers/ProviderCommandReactor.test.ts | 58 +++ .../Layers/ProviderRuntimeIngestion.test.ts | 43 +- .../Layers/ProviderRuntimeIngestion.ts | 45 +- apps/server/src/orchestration/Schemas.ts | 4 + .../orchestration/commandInvariants.test.ts | 4 + .../orchestration/commandReadModel.test.ts | 2 + .../src/orchestration/decider.queue.test.ts | 487 ++++++++++++++++++ .../src/orchestration/decider.settled.test.ts | 2 + .../src/orchestration/decider.snoozed.test.ts | 2 + apps/server/src/orchestration/decider.ts | 444 +++++++++++++--- .../src/orchestration/projector.test.ts | 2 + apps/server/src/orchestration/projector.ts | 105 ++++ .../Layers/ProjectionQueuedMessages.ts | 131 +++++ apps/server/src/persistence/Migrations.ts | 2 + .../035_ProjectionQueuedMessages.ts | 26 + .../Services/ProjectionQueuedMessages.ts | 61 +++ .../provider/Layers/CodexSessionRuntime.ts | 46 ++ .../Layers/ProviderSessionReaper.test.ts | 1 + apps/server/src/server.test.ts | 4 + apps/server/src/ws.ts | 4 + .../web/src/components/ChatView.logic.test.ts | 52 ++ apps/web/src/components/ChatView.logic.ts | 30 +- apps/web/src/components/ChatView.tsx | 101 +++- .../components/CommandPalette.logic.test.ts | 2 + apps/web/src/components/Sidebar.logic.test.ts | 9 + .../components/chat/QueuedMessageChips.tsx | 68 +++ apps/web/src/lib/threadSort.test.ts | 7 + apps/web/src/worktreeCleanup.test.ts | 2 + .../client-runtime/src/operations/commands.ts | 26 + .../client-runtime/src/state/entities.test.ts | 4 + .../src/state/threadCommands.ts | 18 + .../src/state/threadReducer.test.ts | 75 +++ .../client-runtime/src/state/threadReducer.ts | 58 +++ .../src/state/threads-sync.test.ts | 2 + packages/contracts/src/orchestration.ts | 101 ++++ 41 files changed, 2273 insertions(+), 109 deletions(-) create mode 100644 apps/server/src/orchestration/decider.queue.test.ts create mode 100644 apps/server/src/persistence/Layers/ProjectionQueuedMessages.ts create mode 100644 apps/server/src/persistence/Migrations/035_ProjectionQueuedMessages.ts create mode 100644 apps/server/src/persistence/Services/ProjectionQueuedMessages.ts create mode 100644 apps/web/src/components/chat/QueuedMessageChips.tsx diff --git a/.github/upstream-candidates.json b/.github/upstream-candidates.json index e7c15c52c70..1ad3b0a1c1a 100644 --- a/.github/upstream-candidates.json +++ b/.github/upstream-candidates.json @@ -24,6 +24,12 @@ "sourceSha": "f7eaa00b99e67a9c1caf09e2fe6ff1f536f66b91", "status": "active", "purpose": "Show answered provider questions in the web thread timeline" + }, + { + "upstreamPr": 4245, + "sourceSha": "6be48eb238cf310a60cc9571240601e774c7d381", + "status": "active", + "purpose": "Queue follow-up messages server-side during active turns with explicit steer controls" } ] } diff --git a/apps/mobile/src/lib/threadActivity.test.ts b/apps/mobile/src/lib/threadActivity.test.ts index b1bdb61b961..80720340796 100644 --- a/apps/mobile/src/lib/threadActivity.test.ts +++ b/apps/mobile/src/lib/threadActivity.test.ts @@ -45,6 +45,8 @@ function makeThread( archivedAt: null, deletedAt: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: [], checkpoints: [], diff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts index bee749e80bb..cd74c3fbe75 100644 --- a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts +++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts @@ -150,6 +150,8 @@ describe("OrchestrationEngine", () => { settledAt: null, deletedAt: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: [], checkpoints: [], @@ -162,6 +164,8 @@ describe("OrchestrationEngine", () => { threads: projectionSnapshot.threads.map((thread) => ({ ...thread, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: [], checkpoints: [], diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts index 1f24a4a0200..2ec1ca1a83d 100644 --- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts +++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts @@ -28,6 +28,7 @@ import { type ProjectionThreadProposedPlan, ProjectionThreadProposedPlanRepository, } from "../../persistence/Services/ProjectionThreadProposedPlans.ts"; +import { ProjectionQueuedMessageRepository } from "../../persistence/Services/ProjectionQueuedMessages.ts"; import { ProjectionThreadSessionRepository } from "../../persistence/Services/ProjectionThreadSessions.ts"; import { type ProjectionTurn, @@ -40,6 +41,7 @@ import { ProjectionStateRepositoryLive } from "../../persistence/Layers/Projecti import { ProjectionThreadActivityRepositoryLive } from "../../persistence/Layers/ProjectionThreadActivities.ts"; import { ProjectionThreadMessageRepositoryLive } from "../../persistence/Layers/ProjectionThreadMessages.ts"; import { ProjectionThreadProposedPlanRepositoryLive } from "../../persistence/Layers/ProjectionThreadProposedPlans.ts"; +import { ProjectionQueuedMessageRepositoryLive } from "../../persistence/Layers/ProjectionQueuedMessages.ts"; import { ProjectionThreadSessionRepositoryLive } from "../../persistence/Layers/ProjectionThreadSessions.ts"; import { ProjectionTurnRepositoryLive } from "../../persistence/Layers/ProjectionTurns.ts"; import { ProjectionThreadRepositoryLive } from "../../persistence/Layers/ProjectionThreads.ts"; @@ -59,6 +61,7 @@ export const ORCHESTRATION_PROJECTOR_NAMES = { projects: "projection.projects", threads: "projection.threads", threadMessages: "projection.thread-messages", + queuedMessages: "projection.queued-messages", threadProposedPlans: "projection.thread-proposed-plans", threadActivities: "projection.thread-activities", threadSessions: "projection.thread-sessions", @@ -329,7 +332,7 @@ function retainProjectionProposedPlansAfterRevert( function collectThreadAttachmentRelativePaths( threadId: string, - messages: ReadonlyArray, + messages: ReadonlyArray>, ): Set { const threadSegment = toSafeThreadAttachmentSegment(threadId); if (!threadSegment) { @@ -476,6 +479,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti const projectionThreadRepository = yield* ProjectionThreadRepository; const projectionThreadMessageRepository = yield* ProjectionThreadMessageRepository; const projectionThreadProposedPlanRepository = yield* ProjectionThreadProposedPlanRepository; + const projectionQueuedMessageRepository = yield* ProjectionQueuedMessageRepository; const projectionThreadActivityRepository = yield* ProjectionThreadActivityRepository; const projectionThreadSessionRepository = yield* ProjectionThreadSessionRepository; const projectionTurnRepository = yield* ProjectionTurnRepository; @@ -782,6 +786,8 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti } case "thread.message-sent": + case "thread.message-queued": + case "thread.queued-message-removed": case "thread.proposed-plan-upserted": case "thread.activity-appended": case "thread.approval-response-requested": @@ -942,9 +948,17 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti yield* Effect.forEach(keptRows, projectionThreadMessageRepository.upsert, { concurrency: 1, }).pipe(Effect.asVoid); + // Queued messages survive a revert (they are not part of the + // timeline), so their attachment files must survive pruning too. + const queuedRows = yield* projectionQueuedMessageRepository.listByThreadId({ + threadId: event.payload.threadId, + }); attachmentSideEffects.prunedThreadRelativePaths.set( event.payload.threadId, - collectThreadAttachmentRelativePaths(event.payload.threadId, keptRows), + collectThreadAttachmentRelativePaths(event.payload.threadId, [ + ...keptRows, + ...queuedRows, + ]), ); return; } @@ -954,6 +968,67 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti } }); + const applyQueuedMessagesProjection: ProjectorDefinition["apply"] = Effect.fn( + "applyQueuedMessagesProjection", + )(function* (event, attachmentSideEffects) { + switch (event.type) { + case "thread.message-queued": + yield* projectionQueuedMessageRepository.upsert({ + messageId: event.payload.messageId, + threadId: event.payload.threadId, + text: event.payload.text, + attachments: event.payload.attachments, + modelSelection: event.payload.modelSelection ?? null, + sourceProposedPlanThreadId: event.payload.sourceProposedPlan?.threadId ?? null, + sourceProposedPlanId: event.payload.sourceProposedPlan?.planId ?? null, + queuedAt: event.payload.queuedAt, + }); + return; + + case "thread.queued-message-removed": { + // A user removal orphans the removed message's attachment files — + // prune to what the timeline and remaining queue still reference. + // Dispatch removals keep everything: the same attachments re-enter + // the timeline via the paired thread.message-sent. + const removedQueuedMessage = + event.payload.reason === "user" + ? (yield* projectionQueuedMessageRepository.listByThreadId({ + threadId: event.payload.threadId, + })).find((entry) => entry.messageId === event.payload.messageId) + : undefined; + yield* projectionQueuedMessageRepository.deleteByMessageId({ + threadId: event.payload.threadId, + messageId: event.payload.messageId, + }); + if (removedQueuedMessage && (removedQueuedMessage.attachments?.length ?? 0) > 0) { + const retainedMessageRows = yield* projectionThreadMessageRepository.listByThreadId({ + threadId: event.payload.threadId, + }); + const retainedQueuedRows = yield* projectionQueuedMessageRepository.listByThreadId({ + threadId: event.payload.threadId, + }); + attachmentSideEffects.prunedThreadRelativePaths.set( + event.payload.threadId, + collectThreadAttachmentRelativePaths(event.payload.threadId, [ + ...retainedMessageRows, + ...retainedQueuedRows, + ]), + ); + } + return; + } + + case "thread.deleted": + yield* projectionQueuedMessageRepository.deleteByThreadId({ + threadId: event.payload.threadId, + }); + return; + + default: + return; + } + }); + const applyThreadProposedPlansProjection: ProjectorDefinition["apply"] = Effect.fn( "applyThreadProposedPlansProjection", )(function* (event, _attachmentSideEffects) { @@ -1093,19 +1168,22 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti case "thread.session-set": { const turnId = event.payload.session.activeTurnId; if (turnId === null || event.payload.session.status !== "running") { - if ( - event.payload.session.status === "error" || - event.payload.session.status === "stopped" || - event.payload.session.status === "interrupted" - ) { - yield* projectionTurnRepository.deletePendingTurnStartByThreadId({ - threadId: event.payload.threadId, - }); - } // Leaving the "running" session status is the turn-end signal: // settle still-running turns so their duration reflects the whole // turn rather than the last assistant message. const settledTurnState = settledTurnStateForSessionStatus(event.payload.session.status); + // Any settled status abandons an unadopted pending turn start — + // including "ready": a mid-turn steer re-arms the pending row + // without a fresh adoption, and a stale row would block queue + // drains (and re-arm the read model's pendingTurnStart flag on + // restart hydration). Ready-with-genuinely-pending starts never + // reach this projection: ingestion maps that shape to + // "starting" before dispatching the session set. + if (settledTurnState !== null) { + yield* projectionTurnRepository.deletePendingTurnStartByThreadId({ + threadId: event.payload.threadId, + }); + } if (settledTurnState === null) { return; } @@ -1541,6 +1619,14 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti name: ORCHESTRATION_PROJECTOR_NAMES.projects, apply: applyProjectsProjection, }, + // queuedMessages must bootstrap before threadMessages: the revert + // handler in threadMessages reads the queued-message projection to + // retain queued attachments, so on replay that table has to be + // populated first or the prune deletes files still referenced. + { + name: ORCHESTRATION_PROJECTOR_NAMES.queuedMessages, + apply: applyQueuedMessagesProjection, + }, { name: ORCHESTRATION_PROJECTOR_NAMES.threadMessages, apply: applyThreadMessagesProjection, @@ -1671,6 +1757,7 @@ export const OrchestrationProjectionPipelineLive = Layer.effect( Layer.provideMerge(ProjectionThreadRepositoryLive), Layer.provideMerge(ProjectionThreadMessageRepositoryLive), Layer.provideMerge(ProjectionThreadProposedPlanRepositoryLive), + Layer.provideMerge(ProjectionQueuedMessageRepositoryLive), Layer.provideMerge(ProjectionThreadActivityRepositoryLive), Layer.provideMerge(ProjectionThreadSessionRepositoryLive), Layer.provideMerge(ProjectionTurnRepositoryLive), diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts index 843ebb3f626..c6bc03b795f 100644 --- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts +++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts @@ -325,6 +325,8 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => { updatedAt: "2026-02-24T00:00:05.000Z", }, ], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [ { id: "plan-1", diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts index 01e8edefa01..8024d7e060c 100644 --- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts +++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts @@ -18,6 +18,7 @@ import { type OrchestrationMessage, type OrchestrationProjectShell, type OrchestrationProposedPlan, + type OrchestrationQueuedMessage, type OrchestrationProject, type OrchestrationSession, type OrchestrationThreadActivity, @@ -48,6 +49,7 @@ import { ProjectionState } from "../../persistence/Services/ProjectionState.ts"; import { ProjectionThreadActivity } from "../../persistence/Services/ProjectionThreadActivities.ts"; import { ProjectionThreadMessage } from "../../persistence/Services/ProjectionThreadMessages.ts"; import { ProjectionThreadProposedPlan } from "../../persistence/Services/ProjectionThreadProposedPlans.ts"; +import { ProjectionQueuedMessage } from "../../persistence/Services/ProjectionQueuedMessages.ts"; import { ProjectionThreadSession } from "../../persistence/Services/ProjectionThreadSessions.ts"; import { ProjectionThread } from "../../persistence/Services/ProjectionThreads.ts"; import * as RepositoryIdentityResolver from "../../project/RepositoryIdentityResolver.ts"; @@ -77,6 +79,12 @@ const ProjectionThreadMessageDbRowSchema = ProjectionThreadMessage.mapFields( }), ); const ProjectionThreadProposedPlanDbRowSchema = ProjectionThreadProposedPlan; +const ProjectionQueuedMessageDbRowSchema = ProjectionQueuedMessage.mapFields( + Struct.assign({ + attachments: Schema.fromJsonString(Schema.Array(ChatAttachment)), + modelSelection: Schema.NullOr(Schema.fromJsonString(ModelSelection)), + }), +); const ProjectionThreadDbRowSchema = ProjectionThread.mapFields( Struct.assign({ modelSelection: Schema.fromJsonString(ModelSelection), @@ -298,6 +306,26 @@ function mapThreadActivityRow( return row.sequence !== null ? Object.assign(activity, { sequence: row.sequence }) : activity; } +function mapQueuedMessageRow( + row: Schema.Schema.Type, +): OrchestrationQueuedMessage { + return { + messageId: row.messageId, + text: row.text, + attachments: row.attachments, + ...(row.modelSelection !== null ? { modelSelection: row.modelSelection } : {}), + ...(row.sourceProposedPlanThreadId !== null && row.sourceProposedPlanId !== null + ? { + sourceProposedPlan: { + threadId: row.sourceProposedPlanThreadId, + planId: row.sourceProposedPlanId, + }, + } + : {}), + queuedAt: row.queuedAt, + }; +} + function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) { return (cause: unknown): ProjectionRepositoryError => Schema.isSchemaError(cause) @@ -499,6 +527,45 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { `, }); + const listQueuedMessageRows = SqlSchema.findAll({ + Request: Schema.Void, + Result: ProjectionQueuedMessageDbRowSchema, + execute: () => + sql` + SELECT + message_id AS "messageId", + thread_id AS "threadId", + text, + attachments_json AS "attachments", + model_selection_json AS "modelSelection", + source_proposed_plan_thread_id AS "sourceProposedPlanThreadId", + source_proposed_plan_id AS "sourceProposedPlanId", + queued_at AS "queuedAt" + FROM projection_queued_messages + ORDER BY thread_id ASC, rowid ASC + `, + }); + + const listQueuedMessageRowsByThread = SqlSchema.findAll({ + Request: ThreadIdLookupInput, + Result: ProjectionQueuedMessageDbRowSchema, + execute: ({ threadId }) => + sql` + SELECT + message_id AS "messageId", + thread_id AS "threadId", + text, + attachments_json AS "attachments", + model_selection_json AS "modelSelection", + source_proposed_plan_thread_id AS "sourceProposedPlanThreadId", + source_proposed_plan_id AS "sourceProposedPlanId", + queued_at AS "queuedAt" + FROM projection_queued_messages + WHERE thread_id = ${threadId} + ORDER BY rowid ASC + `, + }); + const listThreadActivityRows = SqlSchema.findAll({ Request: Schema.Void, Result: ProjectionThreadActivityDbRowSchema, @@ -638,6 +705,50 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { `, }); + const PendingTurnStartDbRowSchema = Schema.Struct({ + threadId: ThreadId, + messageId: MessageId, + requestedAt: IsoDateTime, + }); + + const getPendingTurnStartRowByThread = SqlSchema.findOneOption({ + Request: ThreadIdLookupInput, + Result: PendingTurnStartDbRowSchema, + execute: ({ threadId }) => + sql` + SELECT + thread_id AS "threadId", + pending_message_id AS "messageId", + requested_at AS "requestedAt" + FROM projection_turns + WHERE thread_id = ${threadId} + AND turn_id IS NULL + AND state = 'pending' + AND pending_message_id IS NOT NULL + AND checkpoint_turn_count IS NULL + ORDER BY requested_at DESC + LIMIT 1 + `, + }); + + const listPendingTurnStartRows = SqlSchema.findAll({ + Request: Schema.Void, + Result: PendingTurnStartDbRowSchema, + execute: () => + sql` + SELECT + thread_id AS "threadId", + pending_message_id AS "messageId", + requested_at AS "requestedAt" + FROM projection_turns + WHERE turn_id IS NULL + AND state = 'pending' + AND pending_message_id IS NOT NULL + AND checkpoint_turn_count IS NULL + ORDER BY thread_id ASC, requested_at DESC + `, + }); + const listActiveLatestTurnRows = SqlSchema.findAll({ Request: Schema.Void, Result: ProjectionLatestTurnDbRowSchema, @@ -1147,6 +1258,22 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { ), ), ), + listQueuedMessageRows(undefined).pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getSnapshot:listQueuedMessages:query", + "ProjectionSnapshotQuery.getSnapshot:listQueuedMessages:decodeRows", + ), + ), + ), + listPendingTurnStartRows(undefined).pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getSnapshot:listPendingTurnStarts:query", + "ProjectionSnapshotQuery.getSnapshot:listPendingTurnStarts:decodeRows", + ), + ), + ), listThreadActivityRows(undefined).pipe( Effect.mapError( toPersistenceSqlOrDecodeError( @@ -1196,6 +1323,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { threadRows, messageRows, proposedPlanRows, + queuedMessageRows, + pendingTurnStartRows, activityRows, sessionRows, checkpointRows, @@ -1204,6 +1333,20 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { ]) => Effect.gen(function* () { const messagesByThread = new Map>(); + const queuedMessagesByThread = new Map>(); + // Rows are ordered requested_at DESC per thread; first wins. + const pendingTurnStartByThread = new Map< + string, + { messageId: MessageId; requestedAt: string } + >(); + for (const row of pendingTurnStartRows) { + if (!pendingTurnStartByThread.has(row.threadId)) { + pendingTurnStartByThread.set(row.threadId, { + messageId: row.messageId, + requestedAt: row.requestedAt, + }); + } + } const proposedPlansByThread = new Map>(); const activitiesByThread = new Map>(); const checkpointsByThread = new Map>(); @@ -1253,6 +1396,13 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { proposedPlansByThread.set(row.threadId, threadProposedPlans); } + for (const row of queuedMessageRows) { + updatedAt = maxIso(updatedAt, row.queuedAt); + const threadQueuedMessages = queuedMessagesByThread.get(row.threadId) ?? []; + threadQueuedMessages.push(mapQueuedMessageRow(row)); + queuedMessagesByThread.set(row.threadId, threadQueuedMessages); + } + for (const row of activityRows) { updatedAt = maxIso(updatedAt, row.createdAt); const threadActivities = activitiesByThread.get(row.threadId) ?? []; @@ -1363,6 +1513,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { snoozedAt: row.snoozedAt, deletedAt: row.deletedAt, messages: messagesByThread.get(row.threadId) ?? [], + queuedMessages: queuedMessagesByThread.get(row.threadId) ?? [], + pendingTurnStart: pendingTurnStartByThread.get(row.threadId) ?? null, proposedPlans: proposedPlansByThread.get(row.threadId) ?? [], activities: activitiesByThread.get(row.threadId) ?? [], // The full snapshot is unwindowed, so there is never more to load. @@ -1421,6 +1573,22 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { ), ), ), + listQueuedMessageRows(undefined).pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getCommandReadModel:listQueuedMessages:query", + "ProjectionSnapshotQuery.getCommandReadModel:listQueuedMessages:decodeRows", + ), + ), + ), + listPendingTurnStartRows(undefined).pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getCommandReadModel:listPendingTurnStarts:query", + "ProjectionSnapshotQuery.getCommandReadModel:listPendingTurnStarts:decodeRows", + ), + ), + ), listThreadSessionRows(undefined).pipe( Effect.mapError( toPersistenceSqlOrDecodeError( @@ -1449,7 +1617,16 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { ) .pipe( Effect.flatMap( - ([projectRows, threadRows, proposedPlanRows, sessionRows, latestTurnRows, stateRows]) => + ([ + projectRows, + threadRows, + proposedPlanRows, + queuedMessageRows, + pendingTurnStartRows, + sessionRows, + latestTurnRows, + stateRows, + ]) => Effect.sync(() => { let updatedAt: string | null = null; const projects: OrchestrationProject[] = []; @@ -1523,8 +1700,33 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { latestTurnByThread.set(row.threadId, mapLatestTurn(row)); } const proposedPlansByThread = new Map>(); + const queuedMessagesByThread = new Map>(); + // Rows are ordered requested_at DESC per thread; first wins. + const pendingTurnStartByThread = new Map< + string, + { messageId: MessageId; requestedAt: string } + >(); + for (const row of pendingTurnStartRows) { + if (!pendingTurnStartByThread.has(row.threadId)) { + pendingTurnStartByThread.set(row.threadId, { + messageId: row.messageId, + requestedAt: row.requestedAt, + }); + } + } const sessionByThread = new Map(); + for (let index = 0; index < queuedMessageRows.length; index += 1) { + const row = queuedMessageRows[index]; + if (!row) { + continue; + } + updatedAt = maxIso(updatedAt, row.queuedAt); + const threadQueuedMessages = queuedMessagesByThread.get(row.threadId) ?? []; + threadQueuedMessages.push(mapQueuedMessageRow(row)); + queuedMessagesByThread.set(row.threadId, threadQueuedMessages); + } + for (let index = 0; index < sessionRows.length; index += 1) { const row = sessionRows[index]; if (!row) { @@ -1567,6 +1769,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { snoozedAt: row.snoozedAt, deletedAt: row.deletedAt, messages: [], + queuedMessages: queuedMessagesByThread.get(row.threadId) ?? [], + pendingTurnStart: pendingTurnStartByThread.get(row.threadId) ?? null, proposedPlans: proposedPlansByThread.get(row.threadId) ?? [], activities: [], checkpoints: [], @@ -2125,6 +2329,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { threadRow, messageRows, proposedPlanRows, + queuedMessageRows, + pendingTurnStartRow, activityRows, checkpointRows, latestTurnRow, @@ -2154,6 +2360,22 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { ), ), ), + listQueuedMessageRowsByThread({ threadId }).pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getThreadDetailById:listQueuedMessages:query", + "ProjectionSnapshotQuery.getThreadDetailById:listQueuedMessages:decodeRows", + ), + ), + ), + getPendingTurnStartRowByThread({ threadId }).pipe( + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getThreadDetailById:getPendingTurnStart:query", + "ProjectionSnapshotQuery.getThreadDetailById:getPendingTurnStart:decodeRow", + ), + ), + ), listThreadActivityRowsByThread({ threadId }).pipe( Effect.mapError( toPersistenceSqlOrDecodeError( @@ -2225,6 +2447,13 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { } return message; }), + queuedMessages: queuedMessageRows.map(mapQueuedMessageRow), + pendingTurnStart: Option.isSome(pendingTurnStartRow) + ? { + messageId: pendingTurnStartRow.value.messageId, + requestedAt: pendingTurnStartRow.value.requestedAt, + } + : null, proposedPlans: proposedPlanRows.map(mapProposedPlanRow), // The query fetches WINDOW+1 ascending rows; if it returned the extra // one, older activities exist beyond the window — drop that oldest row diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts index 516df2d85fc..33d6973398e 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts @@ -483,11 +483,61 @@ describe("ProviderCommandReactor", () => { ); } + // Queue-by-default holds turn.start commands while the session is + // starting/running. The mock provider never emits runtime events, so + // tests that send a follow-up on an idle thread must settle the session + // back to ready first — in production, turn completion does this. The + // projected session is preserved apart from status/activeTurnId so + // provider identity fields survive the settle. + const harnessRuntime = runtime; + let settleIndex = 0; + const settleSession = async (threadId: ThreadId = ThreadId.make("thread-1")) => { + settleIndex += 1; + const readModel = await harnessRuntime.runPromise(snapshotQuery.getSnapshot()); + const session = readModel.threads.find((entry) => entry.id === threadId)?.session; + if (!session) { + return; + } + // Mirror the real lifecycle: report the turn running (stamping + // latestTurn so the decider's pending-turn-start guard clears), then + // settle back to ready. A lone ready-set would leave latestTurn null + // and the just-sent message would read as a still-pending start. + await harnessRuntime.runPromise( + engine.dispatch({ + type: "thread.session.set", + commandId: CommandId.make(`cmd-session-run-${settleIndex}`), + threadId, + session: { + ...session, + status: "running", + activeTurnId: asTurnId(`turn-settle-${settleIndex}`), + updatedAt: now, + }, + createdAt: now, + }), + ); + await harnessRuntime.runPromise( + engine.dispatch({ + type: "thread.session.set", + commandId: CommandId.make(`cmd-session-settle-${settleIndex}`), + threadId, + session: { + ...session, + status: "ready", + activeTurnId: null, + updatedAt: now, + }, + createdAt: now, + }), + ); + }; + return { engine, readModel: () => Effect.runPromise(snapshotQuery.getSnapshot()), readTurns: (threadId: ThreadId) => Effect.runPromise(turnRepository.listByThreadId({ threadId })), + settleSession, startSession, sendTurn, interruptTurn, @@ -1633,6 +1683,7 @@ describe("ProviderCommandReactor", () => { await waitFor(() => harness.sendTurn.mock.calls.length === 1); + await harness.settleSession(); await Effect.runPromise( harness.engine.dispatch({ type: "thread.turn.start", @@ -1687,6 +1738,7 @@ describe("ProviderCommandReactor", () => { yield* Effect.promise(() => waitFor(() => harness.sendTurn.mock.calls.length === 1)); + yield* Effect.promise(() => harness.settleSession()); yield* harness.engine.dispatch({ type: "thread.turn.start", commandId: CommandId.make("cmd-turn-start-restricted-2"), @@ -1807,6 +1859,7 @@ describe("ProviderCommandReactor", () => { await waitFor(() => harness.startSession.mock.calls.length === 1); await waitFor(() => harness.sendTurn.mock.calls.length === 1); + await harness.settleSession(); await Effect.runPromise( harness.engine.dispatch({ type: "thread.turn.start", @@ -1856,6 +1909,7 @@ describe("ProviderCommandReactor", () => { await waitFor(() => harness.sendTurn.mock.calls.length === 1); + await harness.settleSession(); await Effect.runPromise( harness.engine.dispatch({ type: "thread.turn.start", @@ -1932,6 +1986,7 @@ describe("ProviderCommandReactor", () => { }), ); + await harness.settleSession(); await Effect.runPromise( harness.engine.dispatch({ type: "thread.turn.start", @@ -1998,6 +2053,7 @@ describe("ProviderCommandReactor", () => { await waitFor(() => harness.startSession.mock.calls.length === 1); await waitFor(() => harness.sendTurn.mock.calls.length === 1); + await harness.settleSession(); await Effect.runPromise( harness.engine.dispatch({ type: "thread.turn.start", @@ -2082,6 +2138,7 @@ describe("ProviderCommandReactor", () => { return thread?.runtimeMode === "approval-required"; }); await waitFor(() => harness.startSession.mock.calls.length === 2); + await harness.settleSession(); await Effect.runPromise( harness.engine.dispatch({ type: "thread.turn.start", @@ -2254,6 +2311,7 @@ describe("ProviderCommandReactor", () => { await waitFor(() => harness.startSession.mock.calls.length === 1); await waitFor(() => harness.sendTurn.mock.calls.length === 1); + await harness.settleSession(); await Effect.runPromise( harness.engine.dispatch({ type: "thread.turn.start", diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts index 74ece50cd31..b057f5afddb 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts @@ -1523,22 +1523,35 @@ describe("ProviderRuntimeIngestion", () => { threadId, ); - // The steer: a user-requested turn start while the old turn still runs. + // The steer: a follow-up sent mid-turn queues by default, then the + // explicit queue.steer command dispatches it into the running turn. await Effect.runPromise( - harness.engine.dispatch({ - type: "thread.turn.start", - commandId: CommandId.make("cmd-turn-start-steer"), - threadId, - message: { - messageId: asMessageId("msg-steer"), - role: "user", - text: "actually, do 15 instead", - attachments: [], - }, - interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, - runtimeMode: "approval-required", - createdAt, - }), + harness.engine + .dispatch({ + type: "thread.turn.start", + commandId: CommandId.make("cmd-turn-start-steer"), + threadId, + message: { + messageId: asMessageId("msg-steer"), + role: "user", + text: "actually, do 15 instead", + attachments: [], + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "approval-required", + createdAt, + }) + .pipe( + Effect.andThen( + harness.engine.dispatch({ + type: "thread.queue.steer", + commandId: CommandId.make("cmd-queue-steer"), + threadId, + messageId: asMessageId("msg-steer"), + createdAt, + }), + ), + ), ); // The provider session tracks the new turn before emitting turn.started diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts index a8a51b30260..d901340e3ac 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts @@ -30,6 +30,8 @@ import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker"; import { ProviderService } from "../../provider/Services/ProviderService.ts"; import { ProjectionTurnRepository } from "../../persistence/Services/ProjectionTurns.ts"; import { ProjectionTurnRepositoryLive } from "../../persistence/Layers/ProjectionTurns.ts"; +import { ProjectionQueuedMessageRepository } from "../../persistence/Services/ProjectionQueuedMessages.ts"; +import { ProjectionQueuedMessageRepositoryLive } from "../../persistence/Layers/ProjectionQueuedMessages.ts"; import { isGitRepository } from "../../git/Utils.ts"; import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts"; import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts"; @@ -692,6 +694,7 @@ const make = Effect.gen(function* () { const projectionSnapshotQuery = yield* ProjectionSnapshotQuery; const providerService = yield* ProviderService; const projectionTurnRepository = yield* ProjectionTurnRepository; + const projectionQueuedMessageRepository = yield* ProjectionQueuedMessageRepository; const serverSettingsService = yield* ServerSettingsService; const providerCommandId = (event: ProviderRuntimeEvent, tag: string) => crypto.randomUUIDv4.pipe( @@ -746,6 +749,29 @@ const make = Effect.gen(function* () { ), ); + const dispatchQueueDrain = Effect.fn("dispatchQueueDrain")(function* ( + threadId: ThreadId, + event: ProviderRuntimeEvent, + now: string, + ) { + const queuedMessages = yield* projectionQueuedMessageRepository.listByThreadId({ threadId }); + if (queuedMessages.length === 0) { + return; + } + yield* orchestrationEngine + .dispatch({ + type: "thread.queue.drain", + commandId: yield* providerCommandId(event, "queue-drain"), + threadId, + createdAt: now, + }) + .pipe( + // A drain losing the race to a fresh user turn (or an already-empty + // queue) is expected; the next natural completion re-attempts. + Effect.catchTag("OrchestrationCommandInvariantError", () => Effect.void), + ); + }); + const resolveThreadDetail = Effect.fn("resolveThreadDetail")(function* (threadId: ThreadId) { return yield* projectionSnapshotQuery .getThreadDetailById(threadId) @@ -1313,6 +1339,7 @@ const make = Effect.gen(function* () { }); const hasPendingTurnStart = Option.isSome(pendingTurnStart) && thread.session?.status === "starting"; + let drainQueueAfterTurnFinalize = false; const conflictsWithActiveTurn = activeTurnId !== null && eventTurnId !== undefined && !sameId(activeTurnId, eventTurnId); @@ -1450,6 +1477,15 @@ const make = Effect.gen(function* () { }, createdAt: now, }); + + // Auto-drain queued follow-ups only on natural completion — + // never after an interrupt (the user stopped the agent to take + // control) or a failure. The dispatch happens after the + // assistant-finalization block below so the drained user message + // sequences after the assistant text it follows up on. + drainQueueAfterTurnFinalize = + event.type === "turn.completed" && + normalizeRuntimeTurnState(event.payload.state) === "completed"; } } @@ -1668,6 +1704,10 @@ const make = Effect.gen(function* () { updatedAt: now, }); } + + if (drainQueueAfterTurnFinalize) { + yield* dispatchQueueDrain(thread.id, event, now); + } } if (event.type === "session.exited") { @@ -1828,4 +1868,7 @@ const make = Effect.gen(function* () { export const ProviderRuntimeIngestionLive = Layer.effect( ProviderRuntimeIngestionService, make, -).pipe(Layer.provide(ProjectionTurnRepositoryLive)); +).pipe( + Layer.provide(ProjectionTurnRepositoryLive), + Layer.provide(ProjectionQueuedMessageRepositoryLive), +); diff --git a/apps/server/src/orchestration/Schemas.ts b/apps/server/src/orchestration/Schemas.ts index 3b558d24739..a3b80fc23c5 100644 --- a/apps/server/src/orchestration/Schemas.ts +++ b/apps/server/src/orchestration/Schemas.ts @@ -14,6 +14,8 @@ import { ThreadSnoozedPayload as ContractsThreadSnoozedPayloadSchema, ThreadUnsnoozedPayload as ContractsThreadUnsnoozedPayloadSchema, ThreadMessageSentPayload as ContractsThreadMessageSentPayloadSchema, + ThreadMessageQueuedPayload as ContractsThreadMessageQueuedPayloadSchema, + ThreadQueuedMessageRemovedPayload as ContractsThreadQueuedMessageRemovedPayloadSchema, ThreadProposedPlanUpsertedPayload as ContractsThreadProposedPlanUpsertedPayloadSchema, ThreadSessionSetPayload as ContractsThreadSessionSetPayloadSchema, ThreadTurnDiffCompletedPayload as ContractsThreadTurnDiffCompletedPayloadSchema, @@ -44,6 +46,8 @@ export const ThreadSnoozedPayload = ContractsThreadSnoozedPayloadSchema; export const ThreadUnsnoozedPayload = ContractsThreadUnsnoozedPayloadSchema; export const MessageSentPayloadSchema = ContractsThreadMessageSentPayloadSchema; +export const ThreadMessageQueuedPayload = ContractsThreadMessageQueuedPayloadSchema; +export const ThreadQueuedMessageRemovedPayload = ContractsThreadQueuedMessageRemovedPayloadSchema; export const ThreadProposedPlanUpsertedPayload = ContractsThreadProposedPlanUpsertedPayloadSchema; export const ThreadSessionSetPayload = ContractsThreadSessionSetPayloadSchema; export const ThreadTurnDiffCompletedPayload = ContractsThreadTurnDiffCompletedPayloadSchema; diff --git a/apps/server/src/orchestration/commandInvariants.test.ts b/apps/server/src/orchestration/commandInvariants.test.ts index 05f6e1694f0..de461a10d65 100644 --- a/apps/server/src/orchestration/commandInvariants.test.ts +++ b/apps/server/src/orchestration/commandInvariants.test.ts @@ -73,6 +73,8 @@ const wireReadModel: OrchestrationReadModel = { settledAt: null, latestTurn: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, session: null, activities: [], proposedPlans: [], @@ -98,6 +100,8 @@ const wireReadModel: OrchestrationReadModel = { settledAt: null, latestTurn: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, session: null, activities: [], proposedPlans: [], diff --git a/apps/server/src/orchestration/commandReadModel.test.ts b/apps/server/src/orchestration/commandReadModel.test.ts index 6cc7aad55ee..575266b51a3 100644 --- a/apps/server/src/orchestration/commandReadModel.test.ts +++ b/apps/server/src/orchestration/commandReadModel.test.ts @@ -47,6 +47,8 @@ function makeThread( hasMoreActivities: false, latestTurn: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, session: null, activities: [], proposedPlans: [], diff --git a/apps/server/src/orchestration/decider.queue.test.ts b/apps/server/src/orchestration/decider.queue.test.ts new file mode 100644 index 00000000000..be110be8c42 --- /dev/null +++ b/apps/server/src/orchestration/decider.queue.test.ts @@ -0,0 +1,487 @@ +import { + CommandId, + DEFAULT_PROVIDER_INTERACTION_MODE, + EventId, + MessageId, + ProjectId, + ProviderInstanceId, + ThreadId, + TurnId, + type OrchestrationEvent, + type OrchestrationSessionStatus, +} from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { expect, it } from "@effect/vitest"; + +import { decideOrchestrationCommand } from "./decider.ts"; +import { findThreadById, type CommandReadModel } from "./commandReadModel.ts"; +import { createEmptyReadModel, projectEvent } from "./projector.ts"; + +const NOW = "2026-01-01T00:00:00.000Z"; +const asCommandId = (value: string): CommandId => CommandId.make(value); +const asEventId = (value: string): EventId => EventId.make(value); +const asMessageId = (value: string): MessageId => MessageId.make(value); +const asProjectId = (value: string): ProjectId => ProjectId.make(value); +const asThreadId = (value: string): ThreadId => ThreadId.make(value); + +const THREAD_ID = asThreadId("thread-queue"); + +const seedReadModel = Effect.gen(function* () { + const initial = createEmptyReadModel(NOW); + const withProject = yield* projectEvent(initial, { + sequence: 1, + eventId: asEventId("evt-project-create"), + aggregateKind: "project", + aggregateId: asProjectId("project-queue"), + type: "project.created", + occurredAt: NOW, + commandId: asCommandId("cmd-project-create"), + causationEventId: null, + correlationId: asCommandId("cmd-project-create"), + metadata: {}, + payload: { + projectId: asProjectId("project-queue"), + title: "Project Queue", + workspaceRoot: "/tmp/project-queue", + defaultModelSelection: null, + scripts: [], + createdAt: NOW, + updatedAt: NOW, + }, + }); + + return yield* projectEvent(withProject, { + sequence: 2, + eventId: asEventId("evt-thread-create"), + aggregateKind: "thread", + aggregateId: THREAD_ID, + type: "thread.created", + occurredAt: NOW, + commandId: asCommandId("cmd-thread-create"), + causationEventId: null, + correlationId: asCommandId("cmd-thread-create"), + metadata: {}, + payload: { + threadId: THREAD_ID, + projectId: asProjectId("project-queue"), + title: "Thread Queue", + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5-codex", + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "full-access", + branch: null, + worktreePath: null, + createdAt: NOW, + updatedAt: NOW, + }, + }); +}); + +const withSessionStatus = ( + readModel: CommandReadModel, + status: OrchestrationSessionStatus, + sequence: number, +) => + projectEvent(readModel, { + sequence, + eventId: asEventId(`evt-session-${status}-${sequence}`), + aggregateKind: "thread", + aggregateId: THREAD_ID, + type: "thread.session-set", + occurredAt: NOW, + commandId: asCommandId(`cmd-session-${status}-${sequence}`), + causationEventId: null, + correlationId: null, + metadata: {}, + payload: { + threadId: THREAD_ID, + session: { + threadId: THREAD_ID, + status, + providerName: "codex", + runtimeMode: "full-access", + activeTurnId: status === "running" ? TurnId.make("turn-active") : null, + lastError: null, + updatedAt: NOW, + }, + }, + }); + +const turnStartCommand = (suffix: string) => + ({ + type: "thread.turn.start", + commandId: asCommandId(`cmd-turn-start-${suffix}`), + threadId: THREAD_ID, + message: { + messageId: asMessageId(`message-${suffix}`), + role: "user", + text: `Follow up ${suffix}`, + attachments: [], + }, + runtimeMode: "full-access", + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + createdAt: NOW, + }) as const; + +const applyPlanned = ( + readModel: CommandReadModel, + planned: + | Omit + | ReadonlyArray>, +) => + Effect.gen(function* () { + let nextReadModel = readModel; + let nextSequence = readModel.snapshotSequence; + for (const event of Array.isArray(planned) ? planned : [planned]) { + nextSequence += 1; + nextReadModel = yield* projectEvent(nextReadModel, { ...event, sequence: nextSequence }); + } + return nextReadModel; + }); + +it.layer(NodeServices.layer)("decider queue flows", (it) => { + it.effect("starts a turn immediately when the thread is idle", () => + Effect.gen(function* () { + const readModel = yield* seedReadModel; + const planned = yield* decideOrchestrationCommand({ + command: turnStartCommand("idle"), + readModel, + }); + const events = Array.isArray(planned) ? planned : [planned]; + expect(events.map((event) => event.type)).toEqual([ + "thread.message-sent", + "thread.turn-start-requested", + ]); + }), + ); + + it.effect("queues a follow-up while a turn is running", () => + Effect.gen(function* () { + const readModel = yield* withSessionStatus(yield* seedReadModel, "running", 3); + const planned = yield* decideOrchestrationCommand({ + command: turnStartCommand("busy"), + readModel, + }); + const events = Array.isArray(planned) ? planned : [planned]; + expect(events.map((event) => event.type)).toEqual(["thread.message-queued"]); + + const projected = yield* applyPlanned(readModel, planned); + const thread = findThreadById(projected, THREAD_ID); + expect(thread?.queuedMessages.map((entry) => entry.messageId)).toEqual([ + asMessageId("message-busy"), + ]); + }), + ); + + it.effect("queues a follow-up racing in behind a just-dispatched turn start", () => + Effect.gen(function* () { + // First send on an idle thread: dispatches message-sent + + // turn-start-requested, but the provider has not reported any + // session status yet — the pending-start window. + let readModel = yield* seedReadModel; + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ command: turnStartCommand("pending-1"), readModel }), + ); + + // Second send in that window must queue, not open a second turn. + const planned = yield* decideOrchestrationCommand({ + command: turnStartCommand("pending-2"), + readModel, + }); + const events = Array.isArray(planned) ? planned : [planned]; + expect(events.map((event) => event.type)).toEqual(["thread.message-queued"]); + + // A drain in the same window is rejected and keeps the queue intact. + readModel = yield* applyPlanned(readModel, planned); + const drainError = yield* Effect.flip( + decideOrchestrationCommand({ + command: { + type: "thread.queue.drain", + commandId: asCommandId("cmd-drain-pending"), + threadId: THREAD_ID, + createdAt: NOW, + }, + readModel, + }), + ); + expect(drainError.message).toContain("busy"); + }), + ); + + it.effect("drains after a mid-turn steer once the turn settles to ready", () => + Effect.gen(function* () { + // Two messages queue while running; the first is steered mid-turn + // (re-arming pendingTurnStart), then the turn completes → ready. + let readModel = yield* withSessionStatus(yield* seedReadModel, "running", 3); + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ command: turnStartCommand("settle-1"), readModel }), + ); + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ command: turnStartCommand("settle-2"), readModel }), + ); + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ + command: { + type: "thread.queue.steer", + commandId: asCommandId("cmd-steer-settle"), + threadId: THREAD_ID, + messageId: asMessageId("message-settle-1"), + createdAt: NOW, + }, + readModel, + }), + ); + // The steer left pendingTurnStart armed with the session still running. + let thread = findThreadById(readModel, THREAD_ID); + expect(thread?.pendingTurnStart?.messageId).toEqual(asMessageId("message-settle-1")); + + // Turn end: session settles to ready, which must release the pending + // flag so the queued follow-up can drain. + readModel = yield* withSessionStatus(readModel, "ready", readModel.snapshotSequence + 1); + thread = findThreadById(readModel, THREAD_ID); + expect(thread?.pendingTurnStart).toBeNull(); + + const planned = yield* decideOrchestrationCommand({ + command: { + type: "thread.queue.drain", + commandId: asCommandId("cmd-drain-settle"), + threadId: THREAD_ID, + createdAt: NOW, + }, + readModel, + }); + const events = Array.isArray(planned) ? planned : [planned]; + expect(events.map((event) => event.type)).toEqual([ + "thread.queued-message-removed", + "thread.message-sent", + "thread.turn-start-requested", + ]); + }), + ); + + it.effect("rejects queueing past the per-thread cap", () => + Effect.gen(function* () { + let readModel = yield* withSessionStatus(yield* seedReadModel, "running", 3); + for (let index = 0; index < 50; index += 1) { + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ + command: turnStartCommand(`cap-${index}`), + readModel, + }), + ); + } + + const error = yield* Effect.flip( + decideOrchestrationCommand({ command: turnStartCommand("cap-overflow"), readModel }), + ); + expect(error.message).toContain("queued messages"); + + const thread = findThreadById(readModel, THREAD_ID); + expect(thread?.queuedMessages).toHaveLength(50); + }), + ); + + it.effect("steer dispatches a queued message even while running", () => + Effect.gen(function* () { + let readModel = yield* withSessionStatus(yield* seedReadModel, "running", 3); + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ command: turnStartCommand("steer"), readModel }), + ); + + const planned = yield* decideOrchestrationCommand({ + command: { + type: "thread.queue.steer", + commandId: asCommandId("cmd-steer"), + threadId: THREAD_ID, + messageId: asMessageId("message-steer"), + createdAt: NOW, + }, + readModel, + }); + const events = Array.isArray(planned) ? planned : [planned]; + expect(events.map((event) => event.type)).toEqual([ + "thread.queued-message-removed", + "thread.message-sent", + "thread.turn-start-requested", + ]); + + const projected = yield* applyPlanned(readModel, planned); + const thread = findThreadById(projected, THREAD_ID); + expect(thread?.queuedMessages).toEqual([]); + expect(thread?.messages.map((entry) => entry.id)).toContain(asMessageId("message-steer")); + }), + ); + + it.effect("steer rejects while the session is still starting", () => + Effect.gen(function* () { + let readModel = yield* withSessionStatus(yield* seedReadModel, "starting", 3); + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ + command: turnStartCommand("steer-starting"), + readModel, + }), + ); + + const error = yield* Effect.flip( + decideOrchestrationCommand({ + command: { + type: "thread.queue.steer", + commandId: asCommandId("cmd-steer-starting"), + threadId: THREAD_ID, + messageId: asMessageId("message-steer-starting"), + createdAt: NOW, + }, + readModel, + }), + ); + expect(error.message).toContain("starting"); + + // The queued message survives the rejected steer. + const thread = findThreadById(readModel, THREAD_ID); + expect(thread?.queuedMessages.map((entry) => entry.messageId)).toEqual([ + asMessageId("message-steer-starting"), + ]); + }), + ); + + it.effect("remove deletes a queued message without dispatching", () => + Effect.gen(function* () { + let readModel = yield* withSessionStatus(yield* seedReadModel, "running", 3); + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ command: turnStartCommand("remove"), readModel }), + ); + + const planned = yield* decideOrchestrationCommand({ + command: { + type: "thread.queue.remove", + commandId: asCommandId("cmd-remove"), + threadId: THREAD_ID, + messageId: asMessageId("message-remove"), + createdAt: NOW, + }, + readModel, + }); + const events = Array.isArray(planned) ? planned : [planned]; + expect(events.map((event) => event.type)).toEqual(["thread.queued-message-removed"]); + + const projected = yield* applyPlanned(readModel, planned); + const thread = findThreadById(projected, THREAD_ID); + expect(thread?.queuedMessages).toEqual([]); + expect(thread?.messages.map((entry) => entry.id)).not.toContain( + asMessageId("message-remove"), + ); + }), + ); + + it.effect("steer and remove reject unknown queued messages", () => + Effect.gen(function* () { + const readModel = yield* seedReadModel; + for (const type of ["thread.queue.steer", "thread.queue.remove"] as const) { + const error = yield* Effect.flip( + decideOrchestrationCommand({ + command: { + type, + commandId: asCommandId(`cmd-${type}-missing`), + threadId: THREAD_ID, + messageId: asMessageId("message-missing"), + createdAt: NOW, + }, + readModel, + }), + ); + expect(error.message).toContain("does not exist"); + } + }), + ); + + it.effect("drain dispatches the queue head once the thread is idle", () => + Effect.gen(function* () { + let readModel = yield* withSessionStatus(yield* seedReadModel, "running", 3); + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ command: turnStartCommand("drain-1"), readModel }), + ); + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ command: turnStartCommand("drain-2"), readModel }), + ); + readModel = yield* withSessionStatus(readModel, "ready", readModel.snapshotSequence + 1); + + const planned = yield* decideOrchestrationCommand({ + command: { + type: "thread.queue.drain", + commandId: asCommandId("cmd-drain"), + threadId: THREAD_ID, + createdAt: NOW, + }, + readModel, + }); + const events = Array.isArray(planned) ? planned : [planned]; + expect(events.map((event) => event.type)).toEqual([ + "thread.queued-message-removed", + "thread.message-sent", + "thread.turn-start-requested", + ]); + + const projected = yield* applyPlanned(readModel, planned); + const thread = findThreadById(projected, THREAD_ID); + // FIFO: the first queued message dispatches, the second stays queued. + expect(thread?.messages.map((entry) => entry.id)).toContain(asMessageId("message-drain-1")); + expect(thread?.queuedMessages.map((entry) => entry.messageId)).toEqual([ + asMessageId("message-drain-2"), + ]); + }), + ); + + it.effect("drain rejects while the thread is busy and keeps the queue intact", () => + Effect.gen(function* () { + let readModel = yield* withSessionStatus(yield* seedReadModel, "running", 3); + readModel = yield* applyPlanned( + readModel, + yield* decideOrchestrationCommand({ command: turnStartCommand("drain-busy"), readModel }), + ); + + const error = yield* Effect.flip( + decideOrchestrationCommand({ + command: { + type: "thread.queue.drain", + commandId: asCommandId("cmd-drain-busy"), + threadId: THREAD_ID, + createdAt: NOW, + }, + readModel, + }), + ); + expect(error.message).toContain("busy"); + }), + ); + + it.effect("drain rejects when the queue is empty", () => + Effect.gen(function* () { + const readModel = yield* seedReadModel; + const error = yield* Effect.flip( + decideOrchestrationCommand({ + command: { + type: "thread.queue.drain", + commandId: asCommandId("cmd-drain-empty"), + threadId: THREAD_ID, + createdAt: NOW, + }, + readModel, + }), + ); + expect(error.message).toContain("no queued messages"); + }), + ); +}); diff --git a/apps/server/src/orchestration/decider.settled.test.ts b/apps/server/src/orchestration/decider.settled.test.ts index 8debbf5d05c..471730a1e34 100644 --- a/apps/server/src/orchestration/decider.settled.test.ts +++ b/apps/server/src/orchestration/decider.settled.test.ts @@ -46,6 +46,8 @@ function makeReadModel( settledAt: settledOverride === "settled" ? SETTLED_AT : null, deletedAt: null, messages, + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities, checkpoints: [], diff --git a/apps/server/src/orchestration/decider.snoozed.test.ts b/apps/server/src/orchestration/decider.snoozed.test.ts index 4ca22995e00..bf682d69f2d 100644 --- a/apps/server/src/orchestration/decider.snoozed.test.ts +++ b/apps/server/src/orchestration/decider.snoozed.test.ts @@ -51,6 +51,8 @@ function makeReadModel(input: { snoozedAt: input.snoozedAt ?? (input.snoozedUntil != null ? SNOOZED_AT : null), deletedAt: null, messages: input.messages ?? [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: input.activities ?? [], checkpoints: [], diff --git a/apps/server/src/orchestration/decider.ts b/apps/server/src/orchestration/decider.ts index 38cc6d63808..ca7422f786e 100644 --- a/apps/server/src/orchestration/decider.ts +++ b/apps/server/src/orchestration/decider.ts @@ -1,10 +1,16 @@ -import { EventId, type OrchestrationCommand, type OrchestrationEvent } from "@t3tools/contracts"; +import { + EventId, + type OrchestrationCommand, + type OrchestrationEvent, + type OrchestrationQueuedMessage, + type OrchestrationThread, +} from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import * as Crypto from "effect/Crypto"; import * as Effect from "effect/Effect"; import type * as PlatformError from "effect/PlatformError"; -import type { CommandReadModel } from "./commandReadModel.ts"; +import { findThreadById, type CommandReadModel } from "./commandReadModel.ts"; import { OrchestrationCommandInvariantError } from "./Errors.ts"; import { listThreadsByProjectId, @@ -174,6 +180,232 @@ type DecideOrchestrationCommandResult = | PlannedOrchestrationEvent | ReadonlyArray; +/** + * A turn is considered active while its session is starting or running. + * Follow-up `thread.turn.start` commands arriving in that window are queued + * instead of dispatched; explicit `thread.queue.steer` bypasses the queue. + */ +function isThreadTurnActive(thread: OrchestrationThread): boolean { + const status = thread.session?.status; + return status === "running" || status === "starting"; +} + +/** + * Settle-guard heuristic: the latest user message is newer than any turn + * activity, i.e. a turn start that no session has picked up yet. Message + * timestamps are client-supplied, so queue/dispatch decisions must NOT + * hang off this (see `pendingTurnStart` on the thread for the event-driven + * signal); settle tolerates the skew — it merely delays settling within + * the grace window. + */ +function hasRecentUnadoptedUserMessage(thread: OrchestrationThread, occurredAt: string): boolean { + const latestUserMessageAtMs = thread.messages.reduce( + (latest, message) => + message.role === "user" ? Math.max(latest, Date.parse(message.createdAt)) : latest, + Number.NEGATIVE_INFINITY, + ); + const latestTurnAtMs = + thread.latestTurn === null + ? Number.NEGATIVE_INFINITY + : Math.max( + ...[ + thread.latestTurn.requestedAt, + thread.latestTurn.startedAt, + thread.latestTurn.completedAt, + ].map((candidate) => + candidate == null ? Number.NEGATIVE_INFINITY : Date.parse(candidate), + ), + ); + const queuedAgeMs = Date.parse(occurredAt) - latestUserMessageAtMs; + return ( + thread.session?.status !== "error" && + Number.isFinite(latestUserMessageAtMs) && + latestUserMessageAtMs > latestTurnAtMs && + Math.abs(queuedAgeMs) <= QUEUED_TURN_START_GRACE_MS + ); +} + +/** + * Enforced here in the decider so a `thread.message-queued` event past the + * cap is never emitted — projections and persistence stay in lockstep with + * the event stream instead of each clamping independently. + */ +const MAX_THREAD_QUEUED_MESSAGES = 50; + +interface TurnStartMessageInput { + readonly messageId: OrchestrationQueuedMessage["messageId"]; + readonly text: string; + readonly attachments: OrchestrationQueuedMessage["attachments"]; + readonly modelSelection?: OrchestrationQueuedMessage["modelSelection"]; + readonly titleSeed?: string; + readonly sourceProposedPlan?: OrchestrationQueuedMessage["sourceProposedPlan"]; +} + +/** + * Plan the `thread.message-sent` + `thread.turn-start-requested` event pair + * shared by immediate turn starts, queued-message steers, and queue drains. + */ +const planTurnStartEvents = Effect.fn("planTurnStartEvents")(function* ({ + commandId, + thread, + message, + occurredAt, +}: { + readonly commandId: OrchestrationCommand["commandId"]; + readonly thread: OrchestrationThread; + readonly message: TurnStartMessageInput; + readonly occurredAt: string; +}): Effect.fn.Return< + ReadonlyArray, + PlatformError.PlatformError, + Crypto.Crypto +> { + const userMessageEvent: PlannedOrchestrationEvent = { + ...(yield* withEventBase({ + aggregateKind: "thread", + aggregateId: thread.id, + occurredAt, + commandId, + })), + type: "thread.message-sent", + payload: { + threadId: thread.id, + messageId: message.messageId, + role: "user", + text: message.text, + attachments: message.attachments, + turnId: null, + streaming: false, + createdAt: occurredAt, + updatedAt: occurredAt, + }, + }; + const turnStartRequestedEvent: PlannedOrchestrationEvent = { + ...(yield* withEventBase({ + aggregateKind: "thread", + aggregateId: thread.id, + occurredAt, + commandId, + })), + causationEventId: userMessageEvent.eventId, + type: "thread.turn-start-requested", + payload: { + threadId: thread.id, + messageId: message.messageId, + ...(message.modelSelection !== undefined ? { modelSelection: message.modelSelection } : {}), + ...(message.titleSeed !== undefined ? { titleSeed: message.titleSeed } : {}), + runtimeMode: thread.runtimeMode, + interactionMode: thread.interactionMode, + ...(message.sourceProposedPlan !== undefined + ? { sourceProposedPlan: message.sourceProposedPlan } + : {}), + createdAt: occurredAt, + }, + }; + // Real activity resets lifecycle overrides. It wakes an explicitly + // settled thread, clears a keep-active pin, and spends a snooze return + // ticket when the user re-engages. + const lifecycleResetEvents: Array = []; + if (thread.settledOverride !== null) { + lifecycleResetEvents.push({ + ...(yield* withEventBase({ + aggregateKind: "thread", + aggregateId: thread.id, + occurredAt, + commandId, + })), + type: "thread.unsettled", + payload: { + threadId: thread.id, + reason: "activity", + updatedAt: occurredAt, + }, + }); + } + if (thread.snoozedUntil !== null) { + lifecycleResetEvents.push({ + ...(yield* withEventBase({ + aggregateKind: "thread", + aggregateId: thread.id, + occurredAt, + commandId, + })), + type: "thread.unsnoozed", + payload: { + threadId: thread.id, + reason: "activity", + updatedAt: occurredAt, + }, + }); + } + return [...lifecycleResetEvents, userMessageEvent, turnStartRequestedEvent]; +}); + +/** + * Plan the dispatch of one queued message: remove it from the queue and + * start (or steer into) a turn with it. `sourceProposedPlan` is dropped + * rather than failed when the referenced plan no longer exists — the + * message itself must still send. + */ +const planQueuedMessageDispatch = Effect.fn("planQueuedMessageDispatch")(function* ({ + commandId, + readModel, + thread, + queuedMessage, + occurredAt, +}: { + readonly commandId: OrchestrationCommand["commandId"]; + readonly readModel: CommandReadModel; + readonly thread: OrchestrationThread; + readonly queuedMessage: OrchestrationQueuedMessage; + readonly occurredAt: string; +}): Effect.fn.Return< + ReadonlyArray, + PlatformError.PlatformError, + Crypto.Crypto +> { + const sourceProposedPlan = queuedMessage.sourceProposedPlan; + const sourceThread = sourceProposedPlan + ? findThreadById(readModel, sourceProposedPlan.threadId) + : undefined; + const sourcePlanStillValid = + sourceProposedPlan !== undefined && + sourceThread !== undefined && + sourceThread.projectId === thread.projectId && + sourceThread.proposedPlans.some((entry) => entry.id === sourceProposedPlan.planId); + + const removedEvent: PlannedOrchestrationEvent = { + ...(yield* withEventBase({ + aggregateKind: "thread", + aggregateId: thread.id, + occurredAt, + commandId, + })), + type: "thread.queued-message-removed", + payload: { + threadId: thread.id, + messageId: queuedMessage.messageId, + reason: "dispatched", + removedAt: occurredAt, + }, + }; + const turnStartEvents = yield* planTurnStartEvents({ + commandId, + thread, + message: { + messageId: queuedMessage.messageId, + text: queuedMessage.text, + attachments: queuedMessage.attachments, + ...(queuedMessage.modelSelection !== undefined + ? { modelSelection: queuedMessage.modelSelection } + : {}), + ...(sourcePlanStillValid ? { sourceProposedPlan } : {}), + }, + occurredAt, + }); + return [removedEvent, ...turnStartEvents]; +}); + const decideCommandSequence = Effect.fn("decideCommandSequence")(function* ({ commands, readModel, @@ -736,87 +968,169 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" detail: `Proposed plan '${sourceProposedPlan?.planId}' belongs to thread '${sourceThread.id}' in a different project.`, }); } - const userMessageEvent: Omit = { - ...(yield* withEventBase({ - aggregateKind: "thread", - aggregateId: command.threadId, - occurredAt: command.createdAt, - commandId: command.commandId, - })), - type: "thread.message-sent", - payload: { - threadId: command.threadId, + // Queue-by-default: a follow-up arriving while a turn is active is + // held server-side and drained on natural turn completion. Explicit + // mid-turn injection goes through `thread.queue.steer`. Bootstrap + // starts are exempt — their thread was created in the same dispatch. + // `pendingTurnStart` covers the window where a turn start was just + // dispatched (by a send or a queue drain) but the provider has not + // reported the session status yet — a send in that gap must queue, + // not open a second concurrent turn ahead of already-queued chips. + if ( + command.bootstrap === undefined && + (isThreadTurnActive(targetThread) || targetThread.pendingTurnStart !== null) + ) { + if (targetThread.queuedMessages.length >= MAX_THREAD_QUEUED_MESSAGES) { + return yield* new OrchestrationCommandInvariantError({ + commandType: command.type, + detail: `Thread '${command.threadId}' already has ${MAX_THREAD_QUEUED_MESSAGES} queued messages.`, + }); + } + return { + ...(yield* withEventBase({ + aggregateKind: "thread", + aggregateId: command.threadId, + occurredAt: command.createdAt, + commandId: command.commandId, + })), + type: "thread.message-queued", + payload: { + threadId: command.threadId, + messageId: command.message.messageId, + text: command.message.text, + attachments: command.message.attachments, + ...(command.modelSelection !== undefined + ? { modelSelection: command.modelSelection } + : {}), + ...(sourceProposedPlan !== undefined ? { sourceProposedPlan } : {}), + queuedAt: command.createdAt, + }, + }; + } + + return yield* planTurnStartEvents({ + commandId: command.commandId, + thread: targetThread, + message: { messageId: command.message.messageId, - role: "user", text: command.message.text, attachments: command.message.attachments, - turnId: null, - streaming: false, - createdAt: command.createdAt, - updatedAt: command.createdAt, + ...(command.modelSelection !== undefined + ? { modelSelection: command.modelSelection } + : {}), + ...(command.titleSeed !== undefined ? { titleSeed: command.titleSeed } : {}), + ...(sourceProposedPlan !== undefined ? { sourceProposedPlan } : {}), }, - }; - const turnStartRequestedEvent: Omit = { + occurredAt: command.createdAt, + }); + } + + case "thread.queue.steer": { + const thread = yield* requireThread({ + readModel, + command, + threadId: command.threadId, + }); + const queuedMessage = thread.queuedMessages.find( + (entry) => entry.messageId === command.messageId, + ); + if (!queuedMessage) { + return yield* new OrchestrationCommandInvariantError({ + commandType: command.type, + detail: `Queued message '${command.messageId}' does not exist on thread '${command.threadId}'.`, + }); + } + // While a turn start is still pending there is no provider turn to + // steer into — dispatching now would race the pending turn/start with + // a second one. The message stays queued; steer again once running. + // A session that already tracks an active turn (running, or a + // provider reconnect mid-turn) is steerable. Rejected shapes: starting + // with no active turn, and any session without an active turn while a + // just-dispatched turn start is still awaiting adoption — including + // "running" with a null activeTurnId (provider waiting). + const steerRacesPendingStart = + (thread.session?.status === "starting" && thread.session.activeTurnId === null) || + (thread.pendingTurnStart !== null && (thread.session?.activeTurnId ?? null) === null); + if (steerRacesPendingStart) { + return yield* new OrchestrationCommandInvariantError({ + commandType: command.type, + detail: `Thread '${command.threadId}' is still starting a turn; steer once it is running.`, + }); + } + // Otherwise dispatch unconditionally: with a running turn the provider + // adapters treat the resulting sendTurn as a steer; on an idle thread + // this degrades to a normal turn start. + return yield* planQueuedMessageDispatch({ + commandId: command.commandId, + readModel, + thread, + queuedMessage, + occurredAt: command.createdAt, + }); + } + + case "thread.queue.remove": { + const thread = yield* requireThread({ + readModel, + command, + threadId: command.threadId, + }); + const queuedMessage = thread.queuedMessages.find( + (entry) => entry.messageId === command.messageId, + ); + if (!queuedMessage) { + return yield* new OrchestrationCommandInvariantError({ + commandType: command.type, + detail: `Queued message '${command.messageId}' does not exist on thread '${command.threadId}'.`, + }); + } + return { ...(yield* withEventBase({ aggregateKind: "thread", aggregateId: command.threadId, occurredAt: command.createdAt, commandId: command.commandId, })), - causationEventId: userMessageEvent.eventId, - type: "thread.turn-start-requested", + type: "thread.queued-message-removed", payload: { threadId: command.threadId, - messageId: command.message.messageId, - ...(command.modelSelection !== undefined - ? { modelSelection: command.modelSelection } - : {}), - ...(command.titleSeed !== undefined ? { titleSeed: command.titleSeed } : {}), - runtimeMode: targetThread.runtimeMode, - interactionMode: targetThread.interactionMode, - ...(sourceProposedPlan !== undefined ? { sourceProposedPlan } : {}), - createdAt: command.createdAt, + messageId: command.messageId, + reason: "user", + removedAt: command.createdAt, }, }; - // Real activity resets ANY override: it wakes an explicitly settled - // thread, and it clears a keep-active pin back to neutral so the - // thread can auto-settle again after this burst of work goes stale. - // A snooze clears the same way — sending a message to a snoozed - // thread is the user re-engaging, so the return ticket is spent. - const lifecycleResetEvents: Array> = []; - if (targetThread.settledOverride !== null) { - lifecycleResetEvents.push({ - ...(yield* withEventBase({ - aggregateKind: "thread", - aggregateId: command.threadId, - occurredAt: command.createdAt, - commandId: command.commandId, - })), - type: "thread.unsettled", - payload: { - threadId: command.threadId, - reason: "activity", - updatedAt: command.createdAt, - }, + } + + case "thread.queue.drain": { + const thread = yield* requireThread({ + readModel, + command, + threadId: command.threadId, + }); + const queuedMessage = thread.queuedMessages.at(0); + if (!queuedMessage) { + return yield* new OrchestrationCommandInvariantError({ + commandType: command.type, + detail: `Thread '${command.threadId}' has no queued messages to drain.`, }); } - if (targetThread.snoozedUntil != null) { - lifecycleResetEvents.push({ - ...(yield* withEventBase({ - aggregateKind: "thread", - aggregateId: command.threadId, - occurredAt: command.createdAt, - commandId: command.commandId, - })), - type: "thread.unsnoozed", - payload: { - threadId: command.threadId, - reason: "activity", - updatedAt: command.createdAt, - }, + // The user may have raced a new message in between turn completion and + // this drain; keep the queue intact and let the next completion retry. + // The pending-start check also stops a duplicate drain from double- + // dispatching while the first drained turn's session is still unset. + if (isThreadTurnActive(thread) || thread.pendingTurnStart !== null) { + return yield* new OrchestrationCommandInvariantError({ + commandType: command.type, + detail: `Thread '${command.threadId}' is busy; queued messages drain on turn completion.`, }); } - return [...lifecycleResetEvents, userMessageEvent, turnStartRequestedEvent]; + return yield* planQueuedMessageDispatch({ + commandId: command.commandId, + readModel, + thread, + queuedMessage, + occurredAt: command.createdAt, + }); } case "thread.turn.interrupt": { diff --git a/apps/server/src/orchestration/projector.test.ts b/apps/server/src/orchestration/projector.test.ts index 81fe043f54f..11c8e21b376 100644 --- a/apps/server/src/orchestration/projector.test.ts +++ b/apps/server/src/orchestration/projector.test.ts @@ -108,6 +108,8 @@ describe("orchestration projector", () => { snoozedAt: null, deletedAt: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: [], checkpoints: [], diff --git a/apps/server/src/orchestration/projector.ts b/apps/server/src/orchestration/projector.ts index d1dfe8ea070..a5a39ee0081 100644 --- a/apps/server/src/orchestration/projector.ts +++ b/apps/server/src/orchestration/projector.ts @@ -19,6 +19,8 @@ import { import { toProjectorDecodeError, type OrchestrationProjectorDecodeError } from "./Errors.ts"; import { MessageSentPayloadSchema, + ThreadMessageQueuedPayload, + ThreadQueuedMessageRemovedPayload, ProjectCreatedPayload, ProjectDeletedPayload, ProjectMetaUpdatedPayload, @@ -38,6 +40,7 @@ import { ThreadRevertedPayload, ThreadSessionSetPayload, ThreadTurnDiffCompletedPayload, + ThreadTurnStartRequestedPayload, } from "./Schemas.ts"; type ThreadPatch = Partial>; @@ -312,6 +315,8 @@ export function projectEvent( snoozedAt: null, deletedAt: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, activities: [], checkpoints: [], session: null, @@ -517,6 +522,93 @@ export function projectEvent( }; }); + case "thread.message-queued": + return decodeForEvent(ThreadMessageQueuedPayload, event.payload, event.type, "payload").pipe( + Effect.map((payload) => { + const thread = findThreadById(nextBase, payload.threadId); + if (!thread) { + return nextBase; + } + + // No cap here: the decider refuses to emit `thread.message-queued` + // past its queue limit, so replaying the event stream stays in + // lockstep with the persisted projection. + const queuedMessages = [ + ...thread.queuedMessages.filter((entry) => entry.messageId !== payload.messageId), + { + messageId: payload.messageId, + text: payload.text, + attachments: payload.attachments, + ...(payload.modelSelection !== undefined + ? { modelSelection: payload.modelSelection } + : {}), + ...(payload.sourceProposedPlan !== undefined + ? { sourceProposedPlan: payload.sourceProposedPlan } + : {}), + queuedAt: payload.queuedAt, + }, + ]; + + return { + ...nextBase, + threads: updateThread(nextBase.threads, payload.threadId, { + queuedMessages, + updatedAt: event.occurredAt, + }), + }; + }), + ); + + case "thread.queued-message-removed": + return decodeForEvent( + ThreadQueuedMessageRemovedPayload, + event.payload, + event.type, + "payload", + ).pipe( + Effect.map((payload) => { + const thread = findThreadById(nextBase, payload.threadId); + if (!thread) { + return nextBase; + } + + return { + ...nextBase, + threads: updateThread(nextBase.threads, payload.threadId, { + queuedMessages: thread.queuedMessages.filter( + (entry) => entry.messageId !== payload.messageId, + ), + updatedAt: event.occurredAt, + }), + }; + }), + ); + + case "thread.turn-start-requested": + return decodeForEvent( + ThreadTurnStartRequestedPayload, + event.payload, + event.type, + "payload", + ).pipe( + Effect.map((payload) => { + const thread = findThreadById(nextBase, payload.threadId); + if (!thread) { + return nextBase; + } + return { + ...nextBase, + threads: updateThread(nextBase.threads, payload.threadId, { + pendingTurnStart: { + messageId: payload.messageId, + requestedAt: payload.createdAt, + }, + updatedAt: event.occurredAt, + }), + }; + }), + ); + case "thread.session-set": return Effect.gen(function* () { const payload = yield* decodeForEvent( @@ -540,10 +632,23 @@ export function projectEvent( // Leaving the "running" session status is the turn-end signal: settle // a still-running latest turn so its duration reflects the whole turn. const settledTurnState = settledTurnStateForSessionStatus(session.status); + // Mirrors the SQL pipeline's pending-turn-start clearing: a running + // session with an active turn adopts the pending start; every settled + // or terminal status (ready/idle/error/stopped/interrupted) clears it + // — a mid-turn steer re-arms the flag without any adopting session + // transition, so the turn-end ready must release it or drains would + // be rejected forever. Only "starting" (and running while waiting on + // a turn id) keeps it pending. + const pendingTurnStart = + (session.status === "running" && session.activeTurnId !== null) || + settledTurnStateForSessionStatus(session.status) !== null + ? null + : thread.pendingTurnStart; return { ...nextBase, threads: updateThread(nextBase.threads, payload.threadId, { session, + pendingTurnStart, latestTurn: session.status === "running" && session.activeTurnId !== null ? { diff --git a/apps/server/src/persistence/Layers/ProjectionQueuedMessages.ts b/apps/server/src/persistence/Layers/ProjectionQueuedMessages.ts new file mode 100644 index 00000000000..0e3d20e948d --- /dev/null +++ b/apps/server/src/persistence/Layers/ProjectionQueuedMessages.ts @@ -0,0 +1,131 @@ +import * as SqlClient from "effect/unstable/sql/SqlClient"; +import * as SqlSchema from "effect/unstable/sql/SqlSchema"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Schema from "effect/Schema"; +import * as Struct from "effect/Struct"; +import { ChatAttachment, ModelSelection } from "@t3tools/contracts"; + +import { toPersistenceSqlError } from "../Errors.ts"; +import { + DeleteProjectionQueuedMessageInput, + DeleteProjectionQueuedMessagesInput, + ListProjectionQueuedMessagesInput, + ProjectionQueuedMessage, + ProjectionQueuedMessageRepository, + type ProjectionQueuedMessageRepositoryShape, +} from "../Services/ProjectionQueuedMessages.ts"; + +const ProjectionQueuedMessageDbRowSchema = ProjectionQueuedMessage.mapFields( + Struct.assign({ + attachments: Schema.fromJsonString(Schema.Array(ChatAttachment)), + modelSelection: Schema.NullOr(Schema.fromJsonString(ModelSelection)), + }), +); + +const makeProjectionQueuedMessageRepository = Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + // Replace-then-insert (not ON CONFLICT UPDATE) so a re-queued messageId + // gets a fresh rowid and moves to the queue tail — the same semantics as + // the in-memory projector. Drain order is rowid order: a monotonic + // insertion sequence that never ties, unlike queued_at timestamps. + const upsertProjectionQueuedMessageRow = SqlSchema.void({ + Request: ProjectionQueuedMessage, + execute: (row) => sql` + INSERT OR REPLACE INTO projection_queued_messages ( + message_id, + thread_id, + text, + attachments_json, + model_selection_json, + source_proposed_plan_thread_id, + source_proposed_plan_id, + queued_at + ) + VALUES ( + ${row.messageId}, + ${row.threadId}, + ${row.text}, + ${JSON.stringify(row.attachments)}, + ${row.modelSelection !== null ? JSON.stringify(row.modelSelection) : null}, + ${row.sourceProposedPlanThreadId}, + ${row.sourceProposedPlanId}, + ${row.queuedAt} + ) + `, + }); + + const listProjectionQueuedMessageRows = SqlSchema.findAll({ + Request: ListProjectionQueuedMessagesInput, + Result: ProjectionQueuedMessageDbRowSchema, + execute: ({ threadId }) => sql` + SELECT + message_id AS "messageId", + thread_id AS "threadId", + text, + attachments_json AS "attachments", + model_selection_json AS "modelSelection", + source_proposed_plan_thread_id AS "sourceProposedPlanThreadId", + source_proposed_plan_id AS "sourceProposedPlanId", + queued_at AS "queuedAt" + FROM projection_queued_messages + WHERE thread_id = ${threadId} + ORDER BY rowid ASC + `, + }); + + const deleteProjectionQueuedMessageRow = SqlSchema.void({ + Request: DeleteProjectionQueuedMessageInput, + execute: ({ threadId, messageId }) => sql` + DELETE FROM projection_queued_messages + WHERE thread_id = ${threadId} AND message_id = ${messageId} + `, + }); + + const deleteProjectionQueuedMessageRows = SqlSchema.void({ + Request: DeleteProjectionQueuedMessagesInput, + execute: ({ threadId }) => sql` + DELETE FROM projection_queued_messages + WHERE thread_id = ${threadId} + `, + }); + + const upsert: ProjectionQueuedMessageRepositoryShape["upsert"] = (row) => + upsertProjectionQueuedMessageRow(row).pipe( + Effect.mapError(toPersistenceSqlError("ProjectionQueuedMessageRepository.upsert:query")), + ); + + const listByThreadId: ProjectionQueuedMessageRepositoryShape["listByThreadId"] = (input) => + listProjectionQueuedMessageRows(input).pipe( + Effect.mapError( + toPersistenceSqlError("ProjectionQueuedMessageRepository.listByThreadId:query"), + ), + ); + + const deleteByMessageId: ProjectionQueuedMessageRepositoryShape["deleteByMessageId"] = (input) => + deleteProjectionQueuedMessageRow(input).pipe( + Effect.mapError( + toPersistenceSqlError("ProjectionQueuedMessageRepository.deleteByMessageId:query"), + ), + ); + + const deleteByThreadId: ProjectionQueuedMessageRepositoryShape["deleteByThreadId"] = (input) => + deleteProjectionQueuedMessageRows(input).pipe( + Effect.mapError( + toPersistenceSqlError("ProjectionQueuedMessageRepository.deleteByThreadId:query"), + ), + ); + + return { + upsert, + listByThreadId, + deleteByMessageId, + deleteByThreadId, + } satisfies ProjectionQueuedMessageRepositoryShape; +}); + +export const ProjectionQueuedMessageRepositoryLive = Layer.effect( + ProjectionQueuedMessageRepository, + makeProjectionQueuedMessageRepository, +); diff --git a/apps/server/src/persistence/Migrations.ts b/apps/server/src/persistence/Migrations.ts index d25895671a9..7fb29b54ea1 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -47,6 +47,7 @@ import Migration0031 from "./Migrations/031_AuthAuthorizationScopes.ts"; import Migration0032 from "./Migrations/032_AuthPairingProofKeyThumbprint.ts"; import Migration0033 from "./Migrations/033_ProjectionThreadsSettled.ts"; import Migration0034 from "./Migrations/034_ProjectionThreadsSnoozed.ts"; +import Migration0035 from "./Migrations/035_ProjectionQueuedMessages.ts"; /** * Migration loader with all migrations defined inline. @@ -93,6 +94,7 @@ export const migrationEntries = [ [32, "AuthPairingProofKeyThumbprint", Migration0032], [33, "ProjectionThreadsSettled", Migration0033], [34, "ProjectionThreadsSnoozed", Migration0034], + [35, "ProjectionQueuedMessages", Migration0035], ] as const; export const makeMigrationLoader = (throughId?: number) => diff --git a/apps/server/src/persistence/Migrations/035_ProjectionQueuedMessages.ts b/apps/server/src/persistence/Migrations/035_ProjectionQueuedMessages.ts new file mode 100644 index 00000000000..33135eab60e --- /dev/null +++ b/apps/server/src/persistence/Migrations/035_ProjectionQueuedMessages.ts @@ -0,0 +1,26 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/unstable/sql/SqlClient"; + +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + yield* sql` + CREATE TABLE IF NOT EXISTS projection_queued_messages ( + message_id TEXT PRIMARY KEY, + thread_id TEXT NOT NULL, + text TEXT NOT NULL, + attachments_json TEXT NOT NULL, + model_selection_json TEXT, + source_proposed_plan_thread_id TEXT, + source_proposed_plan_id TEXT, + queued_at TEXT NOT NULL + ) + `; + + // Drain order is rowid (insertion) order; the index only serves the + // per-thread filter. + yield* sql` + CREATE INDEX IF NOT EXISTS idx_projection_queued_messages_thread + ON projection_queued_messages(thread_id) + `; +}); diff --git a/apps/server/src/persistence/Services/ProjectionQueuedMessages.ts b/apps/server/src/persistence/Services/ProjectionQueuedMessages.ts new file mode 100644 index 00000000000..6a884dd7b57 --- /dev/null +++ b/apps/server/src/persistence/Services/ProjectionQueuedMessages.ts @@ -0,0 +1,61 @@ +import { + ChatAttachment, + IsoDateTime, + MessageId, + ModelSelection, + OrchestrationProposedPlanId, + ThreadId, +} from "@t3tools/contracts"; +import * as Schema from "effect/Schema"; +import * as Context from "effect/Context"; +import type * as Effect from "effect/Effect"; + +import type { ProjectionRepositoryError } from "../Errors.ts"; + +export const ProjectionQueuedMessage = Schema.Struct({ + messageId: MessageId, + threadId: ThreadId, + text: Schema.String, + attachments: Schema.Array(ChatAttachment), + modelSelection: Schema.NullOr(ModelSelection), + sourceProposedPlanThreadId: Schema.NullOr(ThreadId), + sourceProposedPlanId: Schema.NullOr(OrchestrationProposedPlanId), + queuedAt: IsoDateTime, +}); +export type ProjectionQueuedMessage = typeof ProjectionQueuedMessage.Type; + +export const ListProjectionQueuedMessagesInput = Schema.Struct({ + threadId: ThreadId, +}); +export type ListProjectionQueuedMessagesInput = typeof ListProjectionQueuedMessagesInput.Type; + +export const DeleteProjectionQueuedMessageInput = Schema.Struct({ + threadId: ThreadId, + messageId: MessageId, +}); +export type DeleteProjectionQueuedMessageInput = typeof DeleteProjectionQueuedMessageInput.Type; + +export const DeleteProjectionQueuedMessagesInput = Schema.Struct({ + threadId: ThreadId, +}); +export type DeleteProjectionQueuedMessagesInput = typeof DeleteProjectionQueuedMessagesInput.Type; + +export interface ProjectionQueuedMessageRepositoryShape { + readonly upsert: ( + queuedMessage: ProjectionQueuedMessage, + ) => Effect.Effect; + readonly listByThreadId: ( + input: ListProjectionQueuedMessagesInput, + ) => Effect.Effect, ProjectionRepositoryError>; + readonly deleteByMessageId: ( + input: DeleteProjectionQueuedMessageInput, + ) => Effect.Effect; + readonly deleteByThreadId: ( + input: DeleteProjectionQueuedMessagesInput, + ) => Effect.Effect; +} + +export class ProjectionQueuedMessageRepository extends Context.Service< + ProjectionQueuedMessageRepository, + ProjectionQueuedMessageRepositoryShape +>()("t3/persistence/Services/ProjectionQueuedMessages/ProjectionQueuedMessageRepository") {} diff --git a/apps/server/src/provider/Layers/CodexSessionRuntime.ts b/apps/server/src/provider/Layers/CodexSessionRuntime.ts index 67108dd4dbb..fa0403b758d 100644 --- a/apps/server/src/provider/Layers/CodexSessionRuntime.ts +++ b/apps/server/src/provider/Layers/CodexSessionRuntime.ts @@ -1302,6 +1302,52 @@ export const makeCodexSessionRuntime = ( ...(input.effort ? { effort: input.effort } : {}), ...(input.interactionMode ? { interactionMode: input.interactionMode } : {}), }); + + // Steer the active turn via the protocol-native `turn/steer` — + // appending input mid-turn instead of opening a second provider + // turn while the first is still settling. The `expectedTurnId` + // precondition fails when the turn just ended or is not steerable + // (e.g. review/compact); only a server rejection of the request + // itself (operation "receive-response" — the steer was NOT + // applied) falls back to a fresh `turn/start`. Decode failures on + // an accepted steer's response, and transport/protocol failures, + // propagate: the input may already be appended, and retrying via + // turn/start would duplicate it. + const activeSession = yield* Ref.get(sessionRef); + if (activeSession.status === "running" && activeSession.activeTurnId !== undefined) { + const steeredTurnId = yield* client + .request("turn/steer", { + threadId: providerThreadId, + expectedTurnId: activeSession.activeTurnId, + input: params.input, + }) + .pipe( + Effect.map((steerResponse) => TurnId.make(steerResponse.turnId)), + Effect.catchIf( + (cause): cause is CodexErrors.CodexAppServerRequestError => + cause._tag === "CodexAppServerRequestError" && + cause.operation === "receive-response", + (cause) => + Effect.as( + Effect.logDebug("Codex turn/steer rejected; falling back to turn/start.", { + cause, + }), + null, + ), + ), + ); + if (steeredTurnId !== null) { + const steeredProviderThreadId = currentProviderThreadId(yield* Ref.get(sessionRef)); + return { + threadId: options.threadId, + turnId: steeredTurnId, + ...(steeredProviderThreadId + ? { resumeCursor: { threadId: steeredProviderThreadId } } + : {}), + } satisfies ProviderTurnStartResult; + } + } + const rawResponse = yield* client.raw.request("turn/start", params); const response = yield* decodeV2TurnStartResponse(rawResponse).pipe( Effect.mapError((error) => diff --git a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts index e59521df49d..13198541d0f 100644 --- a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts +++ b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts @@ -108,6 +108,7 @@ function makeReadModel( hasActionableProposedPlan: false, latestTurn: null, messages: [], + queuedMessages: [], session: thread.session, activities: [], proposedPlans: [], diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index 68a8cb3d683..64fc03661c2 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -170,6 +170,8 @@ const makeDefaultOrchestrationReadModel = () => { settledAt: null, latestTurn: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, session: null, activities: [], proposedPlans: [], @@ -5528,6 +5530,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => { settledAt: null, latestTurn: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, session: null, activities: [], proposedPlans: [], diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 241340af35c..a5b5c04fd12 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -279,6 +279,8 @@ function isThreadDetailEvent(event: OrchestrationEvent): event is Extract< { type: | "thread.message-sent" + | "thread.message-queued" + | "thread.queued-message-removed" | "thread.proposed-plan-upserted" | "thread.activity-appended" | "thread.turn-diff-completed" @@ -288,6 +290,8 @@ function isThreadDetailEvent(event: OrchestrationEvent): event is Extract< > { return ( event.type === "thread.message-sent" || + event.type === "thread.message-queued" || + event.type === "thread.queued-message-removed" || event.type === "thread.proposed-plan-upserted" || event.type === "thread.activity-appended" || event.type === "thread.turn-diff-completed" || diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 57c12959ffb..acbf93fa613 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -48,6 +48,8 @@ function makeThread(overrides: Partial = {}): Thread { interactionMode: "default", session: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: [], checkpoints: [], @@ -457,6 +459,7 @@ describe("hasServerAcknowledgedLocalDispatch", () => { phase: "ready", latestTurn: completedTurn, latestUserMessageId: localDispatch.latestUserMessageId, + projectedMessageIds: new Set(), session: readySession, hasPendingApproval: false, hasPendingUserInput: false, @@ -483,6 +486,7 @@ describe("hasServerAcknowledgedLocalDispatch", () => { phase: "ready", latestTurn: newerTurn, latestUserMessageId: localDispatch.latestUserMessageId, + projectedMessageIds: new Set(), session: { ...readySession, updatedAt: newerTurn.completedAt }, hasPendingApproval: false, hasPendingUserInput: false, @@ -510,6 +514,7 @@ describe("hasServerAcknowledgedLocalDispatch", () => { phase: "running", latestTurn: runningTurn, latestUserMessageId: localDispatch.latestUserMessageId, + projectedMessageIds: new Set(), session: { ...readySession, status: "running", @@ -526,6 +531,7 @@ describe("hasServerAcknowledgedLocalDispatch", () => { phase: "running", latestTurn: runningTurn, latestUserMessageId: localDispatch.latestUserMessageId, + projectedMessageIds: new Set(), session: { ...readySession, status: "running", @@ -567,18 +573,63 @@ describe("hasServerAcknowledgedLocalDispatch", () => { }), ); + // Dispatch without a known messageId keeps the legacy heuristic: a new + // latest user message acknowledges it. expect( hasServerAcknowledgedLocalDispatch({ localDispatch, phase: "running", latestTurn: runningTurn, latestUserMessageId: MessageId.make("message-steer"), + projectedMessageIds: new Set(), session: runningSession, hasPendingApproval: false, hasPendingUserInput: false, threadError: null, }), ).toBe(true); + + const correlatedDispatch = createLocalDispatchSnapshot( + makeThread({ latestTurn: runningTurn, session: runningSession }), + { messageId: MessageId.make("message-mine") }, + ); + + // A correlated dispatch acknowledges when its exact message appears in + // the timeline or the queue... + for (const projectedMessageIds of [ + new Set(["message-mine"]), + new Set(["message-other", "message-mine"]), + ]) { + expect( + hasServerAcknowledgedLocalDispatch({ + localDispatch: correlatedDispatch, + phase: "running", + latestTurn: runningTurn, + latestUserMessageId: correlatedDispatch.latestUserMessageId, + projectedMessageIds, + session: runningSession, + hasPendingApproval: false, + hasPendingUserInput: false, + threadError: null, + }), + ).toBe(true); + } + + // ...but ignores unrelated queue/timeline changes (another client's + // queued message, or a different queued chip being steered/removed). + expect( + hasServerAcknowledgedLocalDispatch({ + localDispatch: correlatedDispatch, + phase: "running", + latestTurn: runningTurn, + latestUserMessageId: MessageId.make("message-someone-else"), + projectedMessageIds: new Set(["message-someone-else"]), + session: runningSession, + hasPendingApproval: false, + hasPendingUserInput: false, + threadError: null, + }), + ).toBe(false); }); it("acknowledges pending user interaction and errors immediately", () => { @@ -588,6 +639,7 @@ describe("hasServerAcknowledgedLocalDispatch", () => { phase: "ready" as const, latestTurn: null, latestUserMessageId: localDispatch.latestUserMessageId, + projectedMessageIds: new Set(), session: null, hasPendingApproval: false, hasPendingUserInput: false, diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index 466c9b24c87..b46c505ef67 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -69,6 +69,8 @@ export function buildLocalDraftThread( interactionMode: draftThread.interactionMode, session: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, createdAt: draftThread.createdAt, updatedAt: draftThread.createdAt, archivedAt: null, @@ -456,6 +458,13 @@ export async function waitForStartedServerThread( export interface LocalDispatchSnapshot { startedAt: string; preparingWorktree: boolean; + /** + * The messageId of the send this dispatch is waiting on, when the send + * path knows it. Acknowledgment then correlates with this exact message + * being projected (timeline or queue) instead of global tail heuristics + * that other clients' activity could satisfy. + */ + expectedMessageId: ChatMessage["id"] | null; latestUserMessageId: ChatMessage["id"] | null; latestTurnTurnId: TurnId | null; latestTurnRequestedAt: string | null; @@ -467,7 +476,7 @@ export interface LocalDispatchSnapshot { export function createLocalDispatchSnapshot( activeThread: Thread | undefined, - options?: { preparingWorktree?: boolean }, + options?: { preparingWorktree?: boolean; messageId?: ChatMessage["id"] }, ): LocalDispatchSnapshot { const latestTurn = activeThread?.latestTurn ?? null; const session = activeThread?.session ?? null; @@ -475,6 +484,7 @@ export function createLocalDispatchSnapshot( return { startedAt: new Date().toISOString(), preparingWorktree: Boolean(options?.preparingWorktree), + expectedMessageId: options?.messageId ?? null, latestUserMessageId: latestUserMessage?.id ?? null, latestTurnTurnId: latestTurn?.turnId ?? null, latestTurnRequestedAt: latestTurn?.requestedAt ?? null, @@ -490,6 +500,7 @@ export function hasServerAcknowledgedLocalDispatch(input: { phase: SessionPhase; latestTurn: Thread["latestTurn"] | null; latestUserMessageId: ChatMessage["id"] | null; + projectedMessageIds: ReadonlySet; session: Thread["session"] | null; hasPendingApproval: boolean; hasPendingUserInput: boolean; @@ -504,6 +515,7 @@ export function hasServerAcknowledgedLocalDispatch(input: { const latestTurn = input.latestTurn ?? null; const session = input.session ?? null; + const expectedMessageId = input.localDispatch.expectedMessageId; const latestUserMessageChanged = input.localDispatch.latestUserMessageId !== input.latestUserMessageId; const latestTurnChanged = @@ -512,12 +524,18 @@ export function hasServerAcknowledgedLocalDispatch(input: { input.localDispatch.latestTurnStartedAt !== (latestTurn?.startedAt ?? null) || input.localDispatch.latestTurnCompletedAt !== (latestTurn?.completedAt ?? null); + // The dispatched message being projected (timeline or queue) is the + // strongest acknowledgment in every phase: the server also queues sends + // while a session is starting ("connecting" phase), where neither turn + // nor session fields necessarily change. + if (expectedMessageId !== null && input.projectedMessageIds.has(expectedMessageId)) { + return true; + } + if (input.phase === "running") { - // Steering adds a user message to the current running turn without - // necessarily changing any of the turn timestamps. Treat that projected - // message as the server acknowledgment so the composer does not remain - // stuck in its local "Sending" state until the turn settles. - if (latestUserMessageChanged) { + // Dispatches without a known messageId (e.g. plan-implementation flows) + // keep the legacy latest-user-message heuristic. + if (expectedMessageId === null && latestUserMessageChanged) { return true; } if (!latestTurnChanged) { diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 57d6b3edd1f..ba1d926e573 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -238,6 +238,7 @@ import { import { ThreadErrorBanner } from "./chat/ThreadErrorBanner"; import { resolveThreadPr } from "./ThreadStatusIndicators"; import { ComposerBannerStack, type ComposerBannerStackItem } from "./chat/ComposerBannerStack"; +import { QueuedMessageChips } from "./chat/QueuedMessageChips"; import { DRAFT_HERO_TRANSITION_ANIMATION_ID, DRAFT_HERO_TRANSITION_DURATION_MS, @@ -496,6 +497,20 @@ function useLocalDispatchState(input: { const [localDispatch, setLocalDispatch] = useState(null); const latestUserMessageId = input.activeThread?.messages.findLast((message) => message.role === "user")?.id ?? null; + const activeThreadMessages = input.activeThread?.messages; + const activeThreadQueuedMessages = input.activeThread?.queuedMessages; + // Every server-projected id for this thread — timeline messages plus the + // held queue — so acknowledgment can match the exact dispatched message. + const projectedMessageIds = useMemo(() => { + const ids = new Set(); + for (const message of activeThreadMessages ?? []) { + ids.add(message.id); + } + for (const queuedMessage of activeThreadQueuedMessages ?? []) { + ids.add(queuedMessage.messageId); + } + return ids; + }, [activeThreadMessages, activeThreadQueuedMessages]); const resetLocalDispatch = useCallback(() => { setLocalDispatch(null); @@ -508,6 +523,7 @@ function useLocalDispatchState(input: { phase: input.phase, latestTurn: input.activeLatestTurn, latestUserMessageId, + projectedMessageIds, session: input.activeThread?.session ?? null, hasPendingApproval: input.activePendingApproval !== null, hasPendingUserInput: input.activePendingUserInput !== null, @@ -521,12 +537,13 @@ function useLocalDispatchState(input: { input.phase, input.threadError, latestUserMessageId, + projectedMessageIds, localDispatch, ], ); const activeLocalDispatch = serverAcknowledgedLocalDispatch ? null : localDispatch; const beginLocalDispatch = useCallback( - (options?: { preparingWorktree?: boolean }) => { + (options?: { preparingWorktree?: boolean; messageId?: MessageId }) => { const preparingWorktree = Boolean(options?.preparingWorktree); setLocalDispatch((current) => { const active = serverAcknowledgedLocalDispatch ? null : current; @@ -1159,6 +1176,12 @@ function ChatViewContent(props: ChatViewProps) { const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, { reportFailure: false, }); + const steerQueuedThreadMessage = useAtomCommand(threadEnvironment.steerQueuedMessage, { + reportFailure: false, + }); + const removeQueuedThreadMessage = useAtomCommand(threadEnvironment.removeQueuedMessage, { + reportFailure: false, + }); const respondToThreadApproval = useAtomCommand(threadEnvironment.respondToApproval, { reportFailure: false, }); @@ -3926,10 +3949,16 @@ function ChatViewContent(props: ChatViewProps) { useEffect(() => { if (!activeThread?.id) return; - if (activeThread.messages.length === 0) { + if (activeThread.messages.length === 0 && activeThread.queuedMessages.length === 0) { return; } - const serverIds = new Set(activeThread.messages.map((message) => message.id)); + // A queued message is server-acknowledged too — it renders as a chip + // above the composer, so its optimistic timeline copy must go. + const persistedMessageIds = new Set(activeThread.messages.map((message) => message.id)); + const serverIds = new Set([ + ...persistedMessageIds, + ...activeThread.queuedMessages.map((message) => message.messageId), + ]); const removedMessages = optimisticUserMessages.filter((message) => serverIds.has(message.id)); if (removedMessages.length === 0) { return; @@ -3941,7 +3970,10 @@ function ChatViewContent(props: ChatViewProps) { }, 0); for (const removedMessage of removedMessages) { const previewUrls = collectUserMessageBlobPreviewUrls(removedMessage); - if (previewUrls.length > 0) { + // Handoff keeps blob previews alive only for messages entering the + // timeline; a queued-only acknowledgment renders as a text chip, so + // its previews would never be promoted — revoke them instead. + if (previewUrls.length > 0 && persistedMessageIds.has(removedMessage.id)) { handoffAttachmentPreviews(removedMessage.id, previewUrls); continue; } @@ -3950,7 +3982,13 @@ function ChatViewContent(props: ChatViewProps) { return () => { window.clearTimeout(timer); }; - }, [activeThread?.id, activeThread?.messages, handoffAttachmentPreviews, optimisticUserMessages]); + }, [ + activeThread?.id, + activeThread?.messages, + activeThread?.queuedMessages, + handoffAttachmentPreviews, + optimisticUserMessages, + ]); useEffect(() => { setOptimisticUserMessages((existing) => { @@ -4766,7 +4804,11 @@ function ChatViewContent(props: ChatViewProps) { void dockTransition.catch(() => resolveDockStarted?.()); await dockStarted; } - beginLocalDispatch({ preparingWorktree: Boolean(baseBranchForWorktree) }); + const messageIdForSend = newMessageId(); + beginLocalDispatch({ + preparingWorktree: Boolean(baseBranchForWorktree), + messageId: messageIdForSend, + }); const composerImagesSnapshot = [...composerImages]; const composerTerminalContextsSnapshot = [...sendableComposerTerminalContexts]; @@ -4785,7 +4827,6 @@ function ChatViewContent(props: ChatViewProps) { messageTextWithPreviewAnnotations, composerReviewCommentsSnapshot, ); - const messageIdForSend = newMessageId(); const messageCreatedAt = new Date().toISOString(); const outgoingMessageText = formatOutgoingPrompt({ provider: ctxSelectedProvider, @@ -4954,7 +4995,7 @@ function ChatViewContent(props: ChatViewProps) { : {}), } : undefined; - beginLocalDispatch({ preparingWorktree: false }); + beginLocalDispatch({ preparingWorktree: false, messageId: messageIdForSend }); const startResult = await startThreadTurn({ environmentId, input: { @@ -5048,6 +5089,36 @@ function ChatViewContent(props: ChatViewProps) { } }; + const onSteerQueuedMessage = async (messageId: MessageId) => { + if (!activeThread) return; + const result = await steerQueuedThreadMessage({ + environmentId, + input: { threadId: activeThread.id, messageId }, + }); + if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + setThreadError( + activeThread.id, + error instanceof Error ? error.message : "Failed to steer the queued message.", + ); + } + }; + + const onRemoveQueuedMessage = async (messageId: MessageId) => { + if (!activeThread) return; + const result = await removeQueuedThreadMessage({ + environmentId, + input: { threadId: activeThread.id, messageId }, + }); + if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + setThreadError( + activeThread.id, + error instanceof Error ? error.message : "Failed to remove the queued message.", + ); + } + }; + const onRespondToApproval = useCallback( async (requestId: ApprovalRequestId, decision: ProviderApprovalDecision) => { if (!activeThreadId) return; @@ -5257,7 +5328,7 @@ function ChatViewContent(props: ChatViewProps) { }); sendInFlightRef.current = true; - beginLocalDispatch({ preparingWorktree: false }); + beginLocalDispatch({ preparingWorktree: false, messageId: messageIdForSend }); setThreadError(threadIdForSend, null); // Position this sent row once LegendList has measured the anchored tail. @@ -5991,7 +6062,17 @@ function ChatViewContent(props: ChatViewProps) { ) : ( - + <> + {isServerThread && activeThread ? ( + void onSteerQueuedMessage(messageId)} + onRemove={(messageId) => void onRemoveQueuedMessage(messageId)} + /> + ) : null} + + )}
= {}): Thread { interactionMode: "default", session: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], createdAt: "2026-03-01T00:00:00.000Z", archivedAt: null, diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.ts index e256dba36b7..0195453e76b 100644 --- a/apps/web/src/components/Sidebar.logic.test.ts +++ b/apps/web/src/components/Sidebar.logic.test.ts @@ -1177,6 +1177,8 @@ function makeThread(overrides: Partial = {}): Thread { interactionMode: DEFAULT_INTERACTION_MODE, session: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], createdAt: "2026-03-09T10:00:00.000Z", archivedAt: null, @@ -1202,24 +1204,28 @@ describe("getFallbackThreadIdAfterDelete", () => { projectId: ProjectId.make("project-1"), createdAt: "2026-03-09T10:00:00.000Z", messages: [], + queuedMessages: [], }), makeThread({ id: ThreadId.make("thread-active"), projectId: ProjectId.make("project-1"), createdAt: "2026-03-09T10:05:00.000Z", messages: [], + queuedMessages: [], }), makeThread({ id: ThreadId.make("thread-newest"), projectId: ProjectId.make("project-1"), createdAt: "2026-03-09T10:10:00.000Z", messages: [], + queuedMessages: [], }), makeThread({ id: ThreadId.make("thread-other-project"), projectId: ProjectId.make("project-2"), createdAt: "2026-03-09T10:20:00.000Z", messages: [], + queuedMessages: [], }), ], deletedThreadId: ThreadId.make("thread-active"), @@ -1237,18 +1243,21 @@ describe("getFallbackThreadIdAfterDelete", () => { projectId: ProjectId.make("project-1"), createdAt: "2026-03-09T10:05:00.000Z", messages: [], + queuedMessages: [], }), makeThread({ id: ThreadId.make("thread-newest"), projectId: ProjectId.make("project-1"), createdAt: "2026-03-09T10:10:00.000Z", messages: [], + queuedMessages: [], }), makeThread({ id: ThreadId.make("thread-next"), projectId: ProjectId.make("project-1"), createdAt: "2026-03-09T10:07:00.000Z", messages: [], + queuedMessages: [], }), ], deletedThreadId: ThreadId.make("thread-active"), diff --git a/apps/web/src/components/chat/QueuedMessageChips.tsx b/apps/web/src/components/chat/QueuedMessageChips.tsx new file mode 100644 index 00000000000..e158ab5e147 --- /dev/null +++ b/apps/web/src/components/chat/QueuedMessageChips.tsx @@ -0,0 +1,68 @@ +import { memo } from "react"; +import { CornerDownRightIcon, ListEndIcon, Trash2Icon } from "lucide-react"; +import type { MessageId, OrchestrationQueuedMessage } from "@t3tools/contracts"; + +import { Button } from "../ui/button"; + +/** + * Queued follow-up messages held server-side while a turn runs. Each chip + * offers Steer (send now, injecting into the active turn) and delete; the + * queue otherwise auto-drains in order when the turn completes naturally. + */ +export const QueuedMessageChips = memo(function QueuedMessageChips({ + queuedMessages, + disabled, + onSteer, + onRemove, +}: { + readonly queuedMessages: ReadonlyArray; + readonly disabled?: boolean; + readonly onSteer: (messageId: MessageId) => void; + readonly onRemove: (messageId: MessageId) => void; +}) { + if (queuedMessages.length === 0) { + return null; + } + + return ( +
+ {queuedMessages.map((queuedMessage) => ( +
+
+ ))} +
+ ); +}); diff --git a/apps/web/src/lib/threadSort.test.ts b/apps/web/src/lib/threadSort.test.ts index ca9a5986c66..c7f6312c64c 100644 --- a/apps/web/src/lib/threadSort.test.ts +++ b/apps/web/src/lib/threadSort.test.ts @@ -23,6 +23,8 @@ function makeThread(overrides: Partial = {}): Thread { interactionMode: "default", session: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], createdAt: "2026-03-09T10:00:00.000Z", archivedAt: null, @@ -107,6 +109,7 @@ describe("sortThreads", () => { createdAt: "2026-03-09T10:05:00.000Z", updatedAt: "2026-03-09T10:05:00.000Z", messages: [], + queuedMessages: [], }), ], "updated_at", @@ -126,12 +129,14 @@ describe("sortThreads", () => { createdAt: "2026-03-09T10:00:00.000Z", updatedAt: "invalid-date" as never, messages: [], + queuedMessages: [], }), makeThread({ id: ThreadId.make("thread-2"), createdAt: "2026-03-09T09:00:00.000Z", updatedAt: "2026-03-09T09:30:00.000Z", messages: [], + queuedMessages: [], }), ], "updated_at", @@ -151,12 +156,14 @@ describe("sortThreads", () => { createdAt: "invalid-created-at" as never, updatedAt: "invalid-updated-at" as never, messages: [], + queuedMessages: [], }), makeThread({ id: ThreadId.make("thread-2"), createdAt: "invalid-created-at" as never, updatedAt: "invalid-updated-at" as never, messages: [], + queuedMessages: [], }), ], "updated_at", diff --git a/apps/web/src/worktreeCleanup.test.ts b/apps/web/src/worktreeCleanup.test.ts index 89734357889..16b01270da4 100644 --- a/apps/web/src/worktreeCleanup.test.ts +++ b/apps/web/src/worktreeCleanup.test.ts @@ -20,6 +20,8 @@ function makeThread(overrides: Partial = {}): Thread { interactionMode: DEFAULT_INTERACTION_MODE, session: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, checkpoints: [], activities: [], proposedPlans: [], diff --git a/packages/client-runtime/src/operations/commands.ts b/packages/client-runtime/src/operations/commands.ts index ad25d6544dc..45386bcafb8 100644 --- a/packages/client-runtime/src/operations/commands.ts +++ b/packages/client-runtime/src/operations/commands.ts @@ -44,6 +44,8 @@ export type SetThreadRuntimeModeInput = CommandInput<"thread.runtime-mode.set">; export type SetThreadInteractionModeInput = CommandInput<"thread.interaction-mode.set">; export type StartThreadTurnInput = CommandInput<"thread.turn.start">; export type InterruptThreadTurnInput = CommandInput<"thread.turn.interrupt">; +export type SteerQueuedMessageInput = CommandInput<"thread.queue.steer">; +export type RemoveQueuedMessageInput = CommandInput<"thread.queue.remove">; export type RespondToThreadApprovalInput = CommandInput<"thread.approval.respond">; export type RespondToThreadUserInputInput = CommandInput<"thread.user-input.respond">; export type RevertThreadCheckpointInput = CommandInput<"thread.checkpoint.revert">; @@ -254,6 +256,30 @@ export const interruptThreadTurn: (input: InterruptThreadTurnInput) => CommandEf }); }); +export const steerQueuedMessage: (input: SteerQueuedMessageInput) => CommandEffect = Effect.fn( + "EnvironmentCommands.steerQueuedMessage", +)(function* (input) { + const metadata = yield* timestampedCommandMetadata(input); + return yield* dispatch({ + ...input, + type: "thread.queue.steer", + commandId: metadata.commandId, + createdAt: metadata.createdAt, + }); +}); + +export const removeQueuedMessage: (input: RemoveQueuedMessageInput) => CommandEffect = Effect.fn( + "EnvironmentCommands.removeQueuedMessage", +)(function* (input) { + const metadata = yield* timestampedCommandMetadata(input); + return yield* dispatch({ + ...input, + type: "thread.queue.remove", + commandId: metadata.commandId, + createdAt: metadata.createdAt, + }); +}); + export const respondToThreadApproval: (input: RespondToThreadApprovalInput) => CommandEffect = Effect.fn("EnvironmentCommands.respondToThreadApproval")(function* (input) { const metadata = yield* timestampedCommandMetadata(input); diff --git a/packages/client-runtime/src/state/entities.test.ts b/packages/client-runtime/src/state/entities.test.ts index e08fd9e552f..e0179865455 100644 --- a/packages/client-runtime/src/state/entities.test.ts +++ b/packages/client-runtime/src/state/entities.test.ts @@ -207,6 +207,8 @@ describe("environment entity projections", () => { worktreePath: "/repo/stale-worktree", deletedAt: null, messages, + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: [], checkpoints: [], @@ -322,6 +324,8 @@ describe("environment entity projections", () => { ...THREAD_SHELL, deletedAt: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: [], checkpoints: [], diff --git a/packages/client-runtime/src/state/threadCommands.ts b/packages/client-runtime/src/state/threadCommands.ts index 6c128eb01ab..d1444705ba6 100644 --- a/packages/client-runtime/src/state/threadCommands.ts +++ b/packages/client-runtime/src/state/threadCommands.ts @@ -7,6 +7,7 @@ import { type CreateThreadInput, type DeleteThreadInput, type InterruptThreadTurnInput, + type RemoveQueuedMessageInput, type RespondToThreadApprovalInput, type RespondToThreadUserInputInput, type RevertThreadCheckpointInput, @@ -15,6 +16,7 @@ import { type SettleThreadInput, type SnoozeThreadInput, type StartThreadTurnInput, + type SteerQueuedMessageInput, type StopThreadSessionInput, type UnarchiveThreadInput, type UnsettleThreadInput, @@ -24,6 +26,7 @@ import { createThread, deleteThread, interruptThreadTurn, + removeQueuedMessage, respondToThreadApproval, respondToThreadUserInput, revertThreadCheckpoint, @@ -32,6 +35,7 @@ import { settleThread, snoozeThread, startThreadTurn, + steerQueuedMessage, stopThreadSession, unarchiveThread, unsettleThread, @@ -45,6 +49,7 @@ export type { CreateThreadInput, DeleteThreadInput, InterruptThreadTurnInput, + RemoveQueuedMessageInput, RespondToThreadApprovalInput, RespondToThreadUserInputInput, RevertThreadCheckpointInput, @@ -53,6 +58,7 @@ export type { SettleThreadInput, SnoozeThreadInput, StartThreadTurnInput, + SteerQueuedMessageInput, StopThreadSessionInput, UnarchiveThreadInput, UnsettleThreadInput, @@ -148,6 +154,18 @@ export function createThreadEnvironmentAtoms( scheduler, concurrency, }), + steerQueuedMessage: createEnvironmentCommand(runtime, { + label: "environment-data:commands:thread:steer-queued-message", + execute: (input: SteerQueuedMessageInput) => steerQueuedMessage(input), + scheduler, + concurrency, + }), + removeQueuedMessage: createEnvironmentCommand(runtime, { + label: "environment-data:commands:thread:remove-queued-message", + execute: (input: RemoveQueuedMessageInput) => removeQueuedMessage(input), + scheduler, + concurrency, + }), respondToApproval: createEnvironmentCommand(runtime, { label: "environment-data:commands:thread:respond-to-approval", execute: (input: RespondToThreadApprovalInput) => respondToThreadApproval(input), diff --git a/packages/client-runtime/src/state/threadReducer.test.ts b/packages/client-runtime/src/state/threadReducer.test.ts index 2df8efd4405..84a7760e56b 100644 --- a/packages/client-runtime/src/state/threadReducer.test.ts +++ b/packages/client-runtime/src/state/threadReducer.test.ts @@ -54,6 +54,8 @@ const baseThread: OrchestrationThread = { settledAt: null, deletedAt: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: [], checkpoints: [], @@ -412,6 +414,79 @@ describe("applyThreadDetailEvent", () => { }); }); + describe("thread.message-queued / thread.queued-message-removed", () => { + const queuedPayload = { + threadId: ThreadId.make("thread-1"), + messageId: MessageId.make("queued-1"), + text: "Queued follow-up", + attachments: [], + queuedAt: "2026-04-01T06:00:00.000Z", + }; + + it("appends a queued message and replaces an existing messageId", () => { + const queued = applyThreadDetailEvent(baseThread, { + ...baseEventFields, + sequence: 6, + occurredAt: "2026-04-01T06:00:00.000Z", + aggregateKind: "thread", + aggregateId: ThreadId.make("thread-1"), + type: "thread.message-queued", + payload: queuedPayload, + }); + + expect(queued.kind).toBe("updated"); + if (queued.kind !== "updated") return; + expect(queued.thread.queuedMessages).toHaveLength(1); + expect(queued.thread.queuedMessages[0]?.text).toBe("Queued follow-up"); + + const replaced = applyThreadDetailEvent(queued.thread, { + ...baseEventFields, + sequence: 7, + occurredAt: "2026-04-01T06:01:00.000Z", + aggregateKind: "thread", + aggregateId: ThreadId.make("thread-1"), + type: "thread.message-queued", + payload: { ...queuedPayload, text: "Edited follow-up" }, + }); + + expect(replaced.kind).toBe("updated"); + if (replaced.kind !== "updated") return; + expect(replaced.thread.queuedMessages).toHaveLength(1); + expect(replaced.thread.queuedMessages[0]?.text).toBe("Edited follow-up"); + }); + + it("removes a queued message and leaves others intact", () => { + const threadWithQueue: OrchestrationThread = { + ...baseThread, + queuedMessages: [ + { ...queuedPayload, messageId: MessageId.make("queued-1") }, + { ...queuedPayload, messageId: MessageId.make("queued-2") }, + ], + }; + + const result = applyThreadDetailEvent(threadWithQueue, { + ...baseEventFields, + sequence: 8, + occurredAt: "2026-04-01T06:02:00.000Z", + aggregateKind: "thread", + aggregateId: ThreadId.make("thread-1"), + type: "thread.queued-message-removed", + payload: { + threadId: ThreadId.make("thread-1"), + messageId: MessageId.make("queued-1"), + reason: "dispatched", + removedAt: "2026-04-01T06:02:00.000Z", + }, + }); + + expect(result.kind).toBe("updated"); + if (result.kind !== "updated") return; + expect(result.thread.queuedMessages.map((entry) => entry.messageId)).toEqual([ + MessageId.make("queued-2"), + ]); + }); + }); + describe("thread.session-set", () => { it("settles a running latestTurn when the session leaves the running status", () => { const threadWithRunningTurn: OrchestrationThread = { diff --git a/packages/client-runtime/src/state/threadReducer.ts b/packages/client-runtime/src/state/threadReducer.ts index ee6c580418f..d6a543acffb 100644 --- a/packages/client-runtime/src/state/threadReducer.ts +++ b/packages/client-runtime/src/state/threadReducer.ts @@ -117,6 +117,8 @@ export function applyThreadDetailEvent( snoozedAt: null, deletedAt: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: [], checkpoints: [], @@ -236,6 +238,10 @@ export function applyThreadDetailEvent( : {}), runtimeMode: event.payload.runtimeMode, interactionMode: event.payload.interactionMode, + pendingTurnStart: { + messageId: event.payload.messageId, + requestedAt: event.payload.createdAt, + }, updatedAt: event.occurredAt, }, }; @@ -361,6 +367,45 @@ export function applyThreadDetailEvent( }; } + // ── Queued messages ───────────────────────────────────────────── + case "thread.message-queued": { + const queuedMessage = { + messageId: event.payload.messageId, + text: event.payload.text, + attachments: event.payload.attachments, + ...(event.payload.modelSelection !== undefined + ? { modelSelection: event.payload.modelSelection } + : {}), + ...(event.payload.sourceProposedPlan !== undefined + ? { sourceProposedPlan: event.payload.sourceProposedPlan } + : {}), + queuedAt: event.payload.queuedAt, + }; + return { + kind: "updated", + thread: { + ...thread, + queuedMessages: [ + ...thread.queuedMessages.filter((entry) => entry.messageId !== queuedMessage.messageId), + queuedMessage, + ], + updatedAt: event.occurredAt, + }, + }; + } + + case "thread.queued-message-removed": + return { + kind: "updated", + thread: { + ...thread, + queuedMessages: thread.queuedMessages.filter( + (entry) => entry.messageId !== event.payload.messageId, + ), + updatedAt: event.occurredAt, + }, + }; + // ── Session ───────────────────────────────────────────────────── case "thread.session-set": { // Leaving the "running" session status is the turn-end signal: settle a @@ -398,12 +443,25 @@ export function applyThreadDetailEvent( } : thread.latestTurn; + // Mirrors the server projections' pending-turn-start clearing: a + // running session with an active turn adopts it; terminal statuses + // abandon it. + const sessionStatus = event.payload.session.status; + const pendingTurnStart = + (sessionStatus === "running" && event.payload.session.activeTurnId !== null) || + sessionStatus === "error" || + sessionStatus === "stopped" || + sessionStatus === "interrupted" + ? null + : thread.pendingTurnStart; + return { kind: "updated", thread: { ...thread, session: event.payload.session, latestTurn, + pendingTurnStart, updatedAt: event.occurredAt, }, }; diff --git a/packages/client-runtime/src/state/threads-sync.test.ts b/packages/client-runtime/src/state/threads-sync.test.ts index de027fc57d2..66faddc9829 100644 --- a/packages/client-runtime/src/state/threads-sync.test.ts +++ b/packages/client-runtime/src/state/threads-sync.test.ts @@ -74,6 +74,8 @@ const BASE_THREAD: OrchestrationThread = { settledAt: null, deletedAt: null, messages: [], + queuedMessages: [], + pendingTurnStart: null, proposedPlans: [], activities: [], checkpoints: [], diff --git a/packages/contracts/src/orchestration.ts b/packages/contracts/src/orchestration.ts index 31c9ae937eb..33407fb1347 100644 --- a/packages/contracts/src/orchestration.ts +++ b/packages/contracts/src/orchestration.ts @@ -343,6 +343,22 @@ export const OrchestrationLatestTurn = Schema.Struct({ }); export type OrchestrationLatestTurn = typeof OrchestrationLatestTurn.Type; +/** + * A follow-up message held server-side while a turn is running. Queued + * messages are not part of the thread timeline; they enter it via the + * normal `thread.message-sent` event when dispatched (auto-drain on turn + * completion, or an explicit `thread.queue.steer`). + */ +export const OrchestrationQueuedMessage = Schema.Struct({ + messageId: MessageId, + text: Schema.String, + attachments: Schema.Array(ChatAttachment), + modelSelection: Schema.optional(ModelSelection), + sourceProposedPlan: Schema.optional(SourceProposedPlanReference), + queuedAt: IsoDateTime, +}); +export type OrchestrationQueuedMessage = typeof OrchestrationQueuedMessage.Type; + export const OrchestrationThread = Schema.Struct({ id: ThreadId, projectId: ProjectId, @@ -370,6 +386,22 @@ export const OrchestrationThread = Schema.Struct({ snoozedAt: Schema.optional(Schema.NullOr(IsoDateTime)), deletedAt: Schema.NullOr(IsoDateTime), messages: Schema.Array(OrchestrationMessage), + queuedMessages: Schema.Array(OrchestrationQueuedMessage).pipe( + Schema.withDecodingDefault(Effect.succeed([])), + ), + /** + * The turn start that was requested but not yet adopted by a provider + * session. Non-null between `thread.turn-start-requested` and the session + * reporting running (or a terminal status / start failure). While set, + * follow-up sends queue and drains hold — the session status alone cannot + * see this window. Read-model twin of the SQL pending-turn-start row. + */ + pendingTurnStart: Schema.NullOr( + Schema.Struct({ + messageId: MessageId, + requestedAt: IsoDateTime, + }), + ).pipe(Schema.withDecodingDefault(Effect.succeed(null))), proposedPlans: Schema.Array(OrchestrationProposedPlan).pipe( Schema.withDecodingDefault(Effect.succeed([])), ), @@ -721,6 +753,26 @@ const ThreadTurnInterruptCommand = Schema.Struct({ createdAt: IsoDateTime, }); +/** + * Send a queued message immediately, steering the active turn. Degrades to + * a normal turn start when the thread is idle by the time it is processed. + */ +const ThreadQueueSteerCommand = Schema.Struct({ + type: Schema.Literal("thread.queue.steer"), + commandId: CommandId, + threadId: ThreadId, + messageId: MessageId, + createdAt: IsoDateTime, +}); + +const ThreadQueueRemoveCommand = Schema.Struct({ + type: Schema.Literal("thread.queue.remove"), + commandId: CommandId, + threadId: ThreadId, + messageId: MessageId, + createdAt: IsoDateTime, +}); + const ThreadApprovalRespondCommand = Schema.Struct({ type: Schema.Literal("thread.approval.respond"), commandId: CommandId, @@ -771,6 +823,8 @@ const DispatchableClientOrchestrationCommand = Schema.Union([ ThreadInteractionModeSetCommand, ThreadTurnStartCommand, ThreadTurnInterruptCommand, + ThreadQueueSteerCommand, + ThreadQueueRemoveCommand, ThreadApprovalRespondCommand, ThreadUserInputRespondCommand, ThreadCheckpointRevertCommand, @@ -796,6 +850,8 @@ export const ClientOrchestrationCommand = Schema.Union([ ThreadInteractionModeSetCommand, ClientThreadTurnStartCommand, ThreadTurnInterruptCommand, + ThreadQueueSteerCommand, + ThreadQueueRemoveCommand, ThreadApprovalRespondCommand, ThreadUserInputRespondCommand, ThreadCheckpointRevertCommand, @@ -860,6 +916,18 @@ const ThreadActivityAppendCommand = Schema.Struct({ createdAt: IsoDateTime, }); +/** + * Server-internal: dispatch the queued-message head as a turn after a + * natural (non-interrupted) turn completion. Rejected when the queue is + * empty or the thread is busy again; the dispatcher ignores the rejection. + */ +const ThreadQueueDrainCommand = Schema.Struct({ + type: Schema.Literal("thread.queue.drain"), + commandId: CommandId, + threadId: ThreadId, + createdAt: IsoDateTime, +}); + const ThreadRevertCompleteCommand = Schema.Struct({ type: Schema.Literal("thread.revert.complete"), commandId: CommandId, @@ -875,6 +943,7 @@ const InternalOrchestrationCommand = Schema.Union([ ThreadProposedPlanUpsertCommand, ThreadTurnDiffCompleteCommand, ThreadActivityAppendCommand, + ThreadQueueDrainCommand, ThreadRevertCompleteCommand, ]); export type InternalOrchestrationCommand = typeof InternalOrchestrationCommand.Type; @@ -901,6 +970,8 @@ export const OrchestrationEventType = Schema.Literals([ "thread.runtime-mode-set", "thread.interaction-mode-set", "thread.message-sent", + "thread.message-queued", + "thread.queued-message-removed", "thread.turn-start-requested", "thread.turn-interrupt-requested", "thread.approval-response-requested", @@ -1040,6 +1111,26 @@ export const ThreadMessageSentPayload = Schema.Struct({ updatedAt: IsoDateTime, }); +export const ThreadMessageQueuedPayload = Schema.Struct({ + threadId: ThreadId, + messageId: MessageId, + text: Schema.String, + attachments: Schema.Array(ChatAttachment), + modelSelection: Schema.optional(ModelSelection), + sourceProposedPlan: Schema.optional(SourceProposedPlanReference), + queuedAt: IsoDateTime, +}); + +export const ThreadQueuedMessageRemovedReason = Schema.Literals(["user", "dispatched"]); +export type ThreadQueuedMessageRemovedReason = typeof ThreadQueuedMessageRemovedReason.Type; + +export const ThreadQueuedMessageRemovedPayload = Schema.Struct({ + threadId: ThreadId, + messageId: MessageId, + reason: ThreadQueuedMessageRemovedReason, + removedAt: IsoDateTime, +}); + export const ThreadTurnStartRequestedPayload = Schema.Struct({ threadId: ThreadId, messageId: MessageId, @@ -1212,6 +1303,16 @@ export const OrchestrationEvent = Schema.Union([ type: Schema.Literal("thread.message-sent"), payload: ThreadMessageSentPayload, }), + Schema.Struct({ + ...EventBaseFields, + type: Schema.Literal("thread.message-queued"), + payload: ThreadMessageQueuedPayload, + }), + Schema.Struct({ + ...EventBaseFields, + type: Schema.Literal("thread.queued-message-removed"), + payload: ThreadQueuedMessageRemovedPayload, + }), Schema.Struct({ ...EventBaseFields, type: Schema.Literal("thread.turn-start-requested"), From db56e53f0482cfd4f2b082d1f0384e942456c1d4 Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sun, 26 Jul 2026 08:16:15 +0200 Subject: [PATCH 07/11] fix(client-runtime): re-read network status when the app resumes (upstream #4528) Imported from https://github.com/pingdotgg/t3code/pull/4528 --- .../src/connection/connectivity.test.ts | 298 ++++++++++++++++++ .../src/connection/connectivity.ts | 84 ++++- .../src/connection/registry.test.ts | 53 +++- .../client-runtime/src/connection/registry.ts | 12 +- .../src/connection/supervisor.test.ts | 54 +++- .../src/connection/supervisor.ts | 29 +- .../client-runtime/src/relay/discovery.ts | 16 +- 7 files changed, 516 insertions(+), 30 deletions(-) create mode 100644 packages/client-runtime/src/connection/connectivity.test.ts diff --git a/packages/client-runtime/src/connection/connectivity.test.ts b/packages/client-runtime/src/connection/connectivity.test.ts new file mode 100644 index 00000000000..7e607ee30d0 --- /dev/null +++ b/packages/client-runtime/src/connection/connectivity.test.ts @@ -0,0 +1,298 @@ +import { describe, expect, it } from "@effect/vitest"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Ref from "effect/Ref"; +import * as Stream from "effect/Stream"; +import * as SubscriptionRef from "effect/SubscriptionRef"; + +import * as Connectivity from "./connectivity.ts"; +import type { NetworkStatus } from "./model.ts"; +import * as ConnectionWakeups from "./wakeups.ts"; + +const makeHarness = Effect.fn("TestConnectivityHarness.make")(function* (options?: { + readonly status?: Effect.Effect; + readonly initialStatus?: NetworkStatus; +}) { + const liveStatus = yield* Ref.make(options?.initialStatus ?? "online"); + const reported = yield* SubscriptionRef.make<{ + readonly sequence: number; + readonly status: NetworkStatus; + }>({ sequence: 0, status: options?.initialStatus ?? "online" }); + const wakeups = yield* SubscriptionRef.make(0); + const applied = yield* Ref.make>([]); + + const connectivity = Connectivity.Connectivity.of({ + status: options?.status ?? Ref.get(liveStatus), + changes: SubscriptionRef.changes(reported).pipe( + Stream.drop(1), + Stream.map((event) => event.status), + ), + }); + + const wakeupService = ConnectionWakeups.ConnectionWakeups.of({ + changes: SubscriptionRef.changes(wakeups).pipe( + Stream.drop(1), + Stream.map(() => "application-active" as const), + ), + }); + + return { + connectivity, + wakeups: wakeupService, + applied, + setLiveStatus: (status: NetworkStatus) => Ref.set(liveStatus, status), + // Emits a listener event, as a platform connectivity listener would. + report: (status: NetworkStatus) => + Ref.set(liveStatus, status).pipe( + Effect.andThen( + SubscriptionRef.update(reported, (event) => ({ + sequence: event.sequence + 1, + status, + })), + ), + ), + resume: SubscriptionRef.update(wakeups, (count) => count + 1), + apply: (status: NetworkStatus) => Ref.update(applied, (statuses) => [...statuses, status]), + }; +}); + +// The forked listeners subscribe after `followNetworkStatus` returns, so repeat +// the trigger until its effect is observed rather than racing the first one. +const untilApplied = Effect.fn("TestConnectivityHarness.untilApplied")(function* ( + trigger: Effect.Effect, + applied: Ref.Ref>, + expected: number, +) { + for (let attempt = 0; attempt < 100; attempt += 1) { + yield* trigger; + yield* Effect.yieldNow; + if ((yield* Ref.get(applied)).length >= expected) { + return yield* Ref.get(applied); + } + } + return yield* Effect.die(new Error("The expected network status was never applied.")); +}); + +describe("followNetworkStatus", () => { + it.effect("applies statuses reported by the platform listener", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeHarness(); + yield* Connectivity.followNetworkStatus({ + connectivity: harness.connectivity, + wakeups: harness.wakeups, + apply: harness.apply, + }); + + const applied = yield* untilApplied(harness.report("offline"), harness.applied, 1); + expect(applied).toEqual(["offline"]); + }), + ), + ); + + it.effect("applies a resumed read when the listener missed a transition", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeHarness({ initialStatus: "offline" }); + yield* Connectivity.followNetworkStatus({ + connectivity: harness.connectivity, + wakeups: harness.wakeups, + apply: harness.apply, + }); + + // The device came back online while suspended and the listener never + // reported the transition. + yield* harness.setLiveStatus("online"); + const applied = yield* untilApplied(harness.resume, harness.applied, 1); + expect(applied).toEqual(["online"]); + }), + ), + ); + + it.effect("discards a resumed read that a repeated report raced", () => + Effect.scoped( + Effect.gen(function* () { + const readStarted = yield* Deferred.make(); + const releaseRead = yield* Deferred.make(); + const harness = yield* makeHarness({ + initialStatus: "online", + // The resume samples a brief opposite state. + status: Deferred.succeed(readStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseRead)), + Effect.as("offline" as const), + ), + }); + yield* Connectivity.followNetworkStatus({ + connectivity: harness.connectivity, + wakeups: harness.wakeups, + apply: harness.apply, + }); + + // Apply "online" first so the listener's later repeat of it is genuinely + // redundant rather than a new status. + yield* untilApplied(harness.report("online"), harness.applied, 1); + yield* harness.resume.pipe( + Effect.andThen(Effect.yieldNow), + Effect.repeat({ until: () => Deferred.isDone(readStarted) }), + ); + + // The repeat carries no new status, but it proves the listener spoke + // after this read began, so the read is stale even though the status it + // returns differs. Applying it would strand consumers on "offline" while + // the platform reports "online" — the very failure this helper exists to + // prevent. + yield* harness.report("online"); + yield* Effect.yieldNow; + yield* Deferred.succeed(releaseRead, undefined); + yield* Effect.yieldNow; + yield* Effect.yieldNow; + + expect(yield* Ref.get(harness.applied)).toEqual(["online"]); + }), + ), + ); + + it.effect("still deduplicates a repeated report before it reaches consumers", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeHarness({ initialStatus: "online" }); + yield* Connectivity.followNetworkStatus({ + connectivity: harness.connectivity, + wakeups: harness.wakeups, + apply: harness.apply, + }); + + yield* untilApplied(harness.report("offline"), harness.applied, 1); + // Counting the repeat for staleness must not turn it into a redundant + // consumer update. + yield* harness.report("offline"); + yield* Effect.yieldNow; + yield* Effect.yieldNow; + expect(yield* Ref.get(harness.applied)).toEqual(["offline"]); + + // A genuine transition after the repeat still lands. + yield* untilApplied(harness.report("online"), harness.applied, 2); + expect(yield* Ref.get(harness.applied)).toEqual(["offline", "online"]); + }), + ), + ); + + it.effect("does not start a second status read while one is in flight", () => + Effect.scoped( + Effect.gen(function* () { + const firstRead = yield* Deferred.make(); + const readCount = yield* Ref.make(0); + const harness = yield* makeHarness({ + initialStatus: "unknown", + status: Ref.updateAndGet(readCount, (count) => count + 1).pipe( + Effect.andThen(Deferred.await(firstRead)), + ), + }); + yield* Connectivity.followNetworkStatus({ + connectivity: harness.connectivity, + wakeups: harness.wakeups, + apply: harness.apply, + }); + + yield* harness.resume.pipe( + Effect.andThen(Effect.yieldNow), + Effect.repeat({ + until: () => Ref.get(readCount).pipe(Effect.map((count) => count >= 1)), + }), + ); + + // Resumes are consumed sequentially, so further resumes cannot start a + // read that races the one already in flight. Overlapping snapshots + // therefore cannot apply out of order. + yield* harness.resume; + yield* harness.resume; + yield* Effect.yieldNow; + expect(yield* Ref.get(readCount)).toBe(1); + + yield* Deferred.succeed(firstRead, "online"); + yield* Effect.yieldNow; + expect(yield* Ref.get(harness.applied)).toEqual(["online"]); + }), + ), + ); + + it.effect("discards a resumed read that a newer reported change superseded", () => + Effect.scoped( + Effect.gen(function* () { + const readStarted = yield* Deferred.make(); + const releaseRead = yield* Deferred.make(); + const harness = yield* makeHarness({ + initialStatus: "offline", + status: Deferred.succeed(readStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseRead)), + Effect.as("online" as const), + ), + }); + yield* Connectivity.followNetworkStatus({ + connectivity: harness.connectivity, + wakeups: harness.wakeups, + apply: harness.apply, + }); + + // Resume, and hold the status read open so the listener can report a + // newer transition while that read is still in flight. + yield* harness.resume.pipe( + Effect.andThen(Effect.yieldNow), + Effect.repeat({ until: () => Deferred.isDone(readStarted) }), + ); + yield* untilApplied(harness.report("offline"), harness.applied, 1); + yield* Deferred.succeed(releaseRead, undefined); + yield* Effect.yieldNow; + + // The stale "online" snapshot must not land on top of the newer event. + expect(yield* Ref.get(harness.applied)).toEqual(["offline"]); + }), + ), + ); + + it.effect("keeps a reported change from interleaving with a resumed apply", () => + Effect.scoped( + Effect.gen(function* () { + const applyStarted = yield* Deferred.make(); + const releaseApply = yield* Deferred.make(); + const harness = yield* makeHarness({ initialStatus: "offline" }); + // Holds the resumed apply open once it begins, so a reported change has + // a window to interleave between the guard and its apply. + const gatedApply = (status: NetworkStatus) => + status === "online" + ? Deferred.succeed(applyStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseApply)), + Effect.andThen(harness.apply(status)), + ) + : harness.apply(status); + + yield* Connectivity.followNetworkStatus({ + connectivity: harness.connectivity, + wakeups: harness.wakeups, + apply: gatedApply, + }); + + yield* harness.setLiveStatus("online"); + yield* harness.resume.pipe( + Effect.andThen(Effect.yieldNow), + Effect.repeat({ until: () => Deferred.isDone(applyStarted) }), + ); + + // The listener reports a newer transition mid-apply. + yield* harness.report("offline"); + yield* Effect.yieldNow; + yield* Deferred.succeed(releaseApply, undefined); + yield* Effect.yieldNow.pipe( + Effect.repeat({ + until: () => + Ref.get(harness.applied).pipe(Effect.map((statuses) => statuses.length >= 2)), + }), + ); + + // The reported change has to land last; applying it before the resumed + // status finished would leave the stale "online" as the final word. + expect(yield* Ref.get(harness.applied)).toEqual(["online", "offline"]); + }), + ), + ); +}); diff --git a/packages/client-runtime/src/connection/connectivity.ts b/packages/client-runtime/src/connection/connectivity.ts index 6b40680ce35..f3ce89bf553 100644 --- a/packages/client-runtime/src/connection/connectivity.ts +++ b/packages/client-runtime/src/connection/connectivity.ts @@ -1,9 +1,13 @@ import * as Context from "effect/Context"; -import type * as Effect from "effect/Effect"; +import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; -import type * as Stream from "effect/Stream"; +import * as Option from "effect/Option"; +import * as Ref from "effect/Ref"; +import * as Semaphore from "effect/Semaphore"; +import * as Stream from "effect/Stream"; import type { NetworkStatus } from "./model.ts"; +import * as ConnectionWakeups from "./wakeups.ts"; export class Connectivity extends Context.Service< Connectivity, @@ -17,3 +21,79 @@ export const make = (service: Connectivity["Service"]) => Connectivity.of(servic export const layer = (service: Connectivity["Service"]) => Layer.succeed(Connectivity, make(service)); + +/** + * Applies every reported connectivity change, plus a freshly read status each + * time the application resumes. + * + * Platform listeners drop transitions while an app is suspended, so a consumer + * that follows `changes` alone keeps a stale status until the next real + * transition — which left mobile stranded on "offline" until it was restarted. + * Reading the status is asynchronous, so a read that started before a newer + * reported change is discarded instead of applied over it. + */ +export const followNetworkStatus = Effect.fnUntraced(function* (options: { + readonly connectivity: Connectivity["Service"]; + readonly wakeups: ConnectionWakeups.ConnectionWakeups["Service"]; + readonly apply: (status: NetworkStatus) => Effect.Effect; +}) { + // Counts every report the listener delivers, including one that repeats the + // status already in effect. Such a repeat carries no new status, but it does + // prove the listener spoke more recently than a resume read still in flight, + // so it has to invalidate that read: otherwise a snapshot taken during a brief + // opposite state would land afterwards and overwrite the real status. + const reportCount = yield* Ref.make(0); + // Tracks what was last handed to `options.apply` purely to keep repeats from + // reaching consumers. Deduplication is deliberately kept separate from the + // staleness guard above. + const appliedStatus = yield* Ref.make>(Option.none()); + // Counting a report and applying it has to be indivisible with respect to the + // resume branch's guard. Otherwise a change landing between that guard and its + // apply would be overwritten by the older read. + const applyLock = yield* Semaphore.make(1); + + const applyStatus = Effect.fnUntraced(function* (status: NetworkStatus) { + const changed = yield* Ref.modify(appliedStatus, (current) => + Option.isSome(current) && current.value === status + ? ([false, current] as const) + : ([true, Option.some(status)] as const), + ); + if (changed) { + yield* options.apply(status); + } + }); + + yield* options.connectivity.changes.pipe( + Stream.runForEach((status) => + applyLock.withPermits(1)( + Ref.update(reportCount, (count) => count + 1).pipe(Effect.andThen(applyStatus(status))), + ), + ), + // Subscribe before returning so a transition reported while this is still + // being set up is not dropped. + Effect.forkScoped({ startImmediately: true }), + ); + + yield* options.wakeups.changes.pipe( + Stream.runForEach((reason) => + reason === "application-active" + ? Effect.gen(function* () { + // `runForEach` is sequential, so resume reads cannot overlap: a + // second resume does not start a read until this one has applied. + const startedAt = yield* Ref.get(reportCount); + const status = yield* options.connectivity.status; + yield* applyLock.withPermits(1)( + Effect.gen(function* () { + // Re-read under the permit: any report that arrived while the + // read was in flight is newer, so this result is stale. + if ((yield* Ref.get(reportCount)) === startedAt) { + yield* applyStatus(status); + } + }), + ); + }) + : Effect.void, + ), + Effect.forkScoped({ startImmediately: true }), + ); +}); diff --git a/packages/client-runtime/src/connection/registry.test.ts b/packages/client-runtime/src/connection/registry.test.ts index c75cc2fad4b..3e40927aebe 100644 --- a/packages/client-runtime/src/connection/registry.test.ts +++ b/packages/client-runtime/src/connection/registry.test.ts @@ -267,8 +267,12 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* ( Ref.update(ownedDataClears, (environmentIds) => [...environmentIds, environmentId]), }); const networkStatus = yield* SubscriptionRef.make<"unknown" | "offline" | "online">("online"); + // Status reads come from a separate ref so a test can simulate a platform + // listener that missed a transition while the app was suspended. + const liveNetworkStatus = yield* Ref.make<"unknown" | "offline" | "online">("online"); + const wakeups = yield* SubscriptionRef.make(0); const connectivity = Connectivity.Connectivity.of({ - status: SubscriptionRef.get(networkStatus), + status: Ref.get(liveNetworkStatus), changes: SubscriptionRef.changes(networkStatus), }); const profileStore = ConnectionProfileStore.ConnectionProfileStore.of({ @@ -375,7 +379,12 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* ( Layer.succeed(Connectivity.Connectivity, connectivity), Layer.succeed( ConnectionWakeups.ConnectionWakeups, - ConnectionWakeups.ConnectionWakeups.of({ changes: Stream.never }), + ConnectionWakeups.ConnectionWakeups.of({ + changes: SubscriptionRef.changes(wakeups).pipe( + Stream.drop(1), + Stream.map(() => "application-active" as const), + ), + }), ), Layer.succeed(ConnectionDriver.ConnectionDriver, driver), cacheLayer, @@ -398,6 +407,11 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* ( storedRemoteTokens, disconnectedSshTargets, networkStatus, + // Changes the network the device is actually on without emitting a change + // event, mimicking a listener that was suspended while backgrounded. + setLiveNetworkStatus: (status: "unknown" | "offline" | "online") => + Ref.set(liveNetworkStatus, status), + resume: SubscriptionRef.update(wakeups, (count) => count + 1), }; }); @@ -456,6 +470,41 @@ describe("EnvironmentRegistry", () => { }), ); + it.effect("recovers the network status a suspended listener never reported", () => + Effect.gen(function* () { + const harness = yield* makeHarness([]); + + yield* Effect.gen(function* () { + const registry = yield* EnvironmentRegistry.EnvironmentRegistry; + const offline = yield* Effect.forkChild( + SubscriptionRef.changes(registry.networkStatus).pipe( + Stream.filter((status) => status === "offline"), + Stream.runHead, + ), + ); + yield* SubscriptionRef.set(harness.networkStatus, "offline"); + yield* Fiber.join(offline); + + // The device came back online while backgrounded and the listener never + // reported the transition, so only a fresh read can observe it. + yield* harness.setLiveNetworkStatus("online"); + // The forked listener subscribes after `start`, so repeat the resume + // until it is observed rather than racing the first one. + yield* harness.resume.pipe( + Effect.andThen(Effect.yieldNow), + Effect.repeat({ + while: () => + SubscriptionRef.get(registry.networkStatus).pipe( + Effect.map((status) => status !== "online"), + ), + }), + ); + + expect(yield* SubscriptionRef.get(registry.networkStatus)).toBe("online"); + }).pipe(Effect.provide(harness.layer), Effect.scoped); + }), + ); + it.effect("starts persisted environments independently", () => Effect.gen(function* () { const bothLoadsStarted = yield* Deferred.make(); diff --git a/packages/client-runtime/src/connection/registry.ts b/packages/client-runtime/src/connection/registry.ts index a3a36272f32..2bc5076a297 100644 --- a/packages/client-runtime/src/connection/registry.ts +++ b/packages/client-runtime/src/connection/registry.ts @@ -652,10 +652,14 @@ export const make = Effect.gen(function* () { ), ), ); - yield* connectivity.changes.pipe( - Stream.runForEach((status) => SubscriptionRef.set(networkStatus, status)), - Effect.forkScoped, - ); + // `networkStatus` feeds the connection UI and readiness checks, so it has to + // recover from a transition dropped while the app was suspended just like the + // supervisors do. Otherwise a reconnected environment still reads as offline. + yield* Connectivity.followNetworkStatus({ + connectivity, + wakeups, + apply: (status) => SubscriptionRef.set(networkStatus, status), + }); return EnvironmentRegistry.of({ entries, diff --git a/packages/client-runtime/src/connection/supervisor.test.ts b/packages/client-runtime/src/connection/supervisor.test.ts index f3901e42251..95df5de21a6 100644 --- a/packages/client-runtime/src/connection/supervisor.test.ts +++ b/packages/client-runtime/src/connection/supervisor.test.ts @@ -116,9 +116,13 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?: readonly ready?: (attempt: number) => Effect.Effect; readonly probe?: (attempt: number) => Effect.Effect; }) { - const networkStatus = yield* SubscriptionRef.make( + // `reportedNetworkStatus` drives the change stream while `liveNetworkStatus` + // answers status reads, so a test can simulate a platform listener that missed + // a transition while the app was suspended. + const reportedNetworkStatus = yield* SubscriptionRef.make( options?.networkStatus ?? "online", ); + const liveNetworkStatus = yield* Ref.make(options?.networkStatus ?? "online"); const prepareCount = yield* Ref.make(0); const sessionCount = yield* Ref.make(0); const releaseCount = yield* Ref.make(0); @@ -134,8 +138,8 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?: >([]); const connectivity = Connectivity.Connectivity.of({ - status: SubscriptionRef.get(networkStatus), - changes: SubscriptionRef.changes(networkStatus), + status: Ref.get(liveNetworkStatus), + changes: SubscriptionRef.changes(reportedNetworkStatus), }); const prepare = Effect.fn("TestConnectionDriver.prepare")(function* (target: ConnectionTarget) { @@ -197,7 +201,13 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?: prepareCount, sessionCount, releaseCount, - setNetworkStatus: (status: NetworkStatus) => SubscriptionRef.set(networkStatus, status), + setNetworkStatus: (status: NetworkStatus) => + Ref.set(liveNetworkStatus, status).pipe( + Effect.andThen(SubscriptionRef.set(reportedNetworkStatus, status)), + ), + // Changes the network the device is actually on without emitting a change + // event, mimicking a listener that was suspended while backgrounded. + setNetworkStatusWithoutNotifying: (status: NetworkStatus) => Ref.set(liveNetworkStatus, status), wake: (reason: "application-active" | "credentials-changed") => SubscriptionRef.update(wakeups, (event) => ({ sequence: event.sequence + 1, @@ -311,6 +321,42 @@ describe("EnvironmentSupervisor", () => { }), ); + it.effect("recovers from a network change the platform dropped while suspended", () => + Effect.gen(function* () { + const harness = yield* makeHarness({ networkStatus: "offline" }); + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, { + initiallyDesired: true, + }).pipe(Effect.provide(harness.dependencies)); + + yield* awaitState(supervisor.state, (state) => state.phase === "offline"); + + // The device regained connectivity while backgrounded, but the listener + // was suspended and never reported the transition. + yield* harness.setNetworkStatusWithoutNotifying("online"); + + // The supervisor reaches its offline state before the forked wakeup + // listener subscribes, so repeat the resume until it is observed. + yield* harness.wake("application-active").pipe( + Effect.andThen(Effect.yieldNow), + Effect.repeat({ + while: () => + SubscriptionRef.get(supervisor.state).pipe( + Effect.map((state) => state.phase === "offline"), + ), + }), + ); + + const ready = yield* awaitState(supervisor.state, (state) => state.phase === "connected"); + expect(ready).toMatchObject({ + desired: true, + network: "online", + phase: "connected", + lastFailure: null, + }); + expect(yield* Ref.get(harness.prepareCount)).toBe(1); + }), + ); + it.effect("retries forever with exponential backoff capped at sixteen seconds", () => Effect.gen(function* () { const harness = yield* makeHarness({ diff --git a/packages/client-runtime/src/connection/supervisor.ts b/packages/client-runtime/src/connection/supervisor.ts index d9efcd4263a..5d0c63358c3 100644 --- a/packages/client-runtime/src/connection/supervisor.ts +++ b/packages/client-runtime/src/connection/supervisor.ts @@ -667,18 +667,23 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( } }); - yield* connectivity.changes.pipe( - Stream.runForEach((network) => - Ref.modify(intent, (current) => - current.network === network ? [false, current] : ([true, { ...current, network }] as const), - ).pipe( - Effect.flatMap((changed) => - changed ? signal({ _tag: "NetworkChanged", network }) : Effect.void, - ), - ), - ), - Effect.forkScoped, - ); + const applyNetworkStatus = Effect.fnUntraced(function* (network: NetworkStatus) { + const changed = yield* Ref.modify(intent, (current) => + current.network === network ? [false, current] : ([true, { ...current, network }] as const), + ); + if (changed) { + yield* signal({ _tag: "NetworkChanged", network }); + } + }); + + // The offline branch of `run` only waits for signals and re-reads the same + // cached network value, so a transition dropped while the app was suspended + // would otherwise strand this supervisor until the app restarted. + yield* Connectivity.followNetworkStatus({ + connectivity, + wakeups, + apply: applyNetworkStatus, + }); yield* wakeups.changes.pipe( Stream.runForEach((reason) => signal({ _tag: "Wakeup", reason })), Effect.forkScoped, diff --git a/packages/client-runtime/src/relay/discovery.ts b/packages/client-runtime/src/relay/discovery.ts index 855bb2654ed..4c58121742f 100644 --- a/packages/client-runtime/src/relay/discovery.ts +++ b/packages/client-runtime/src/relay/discovery.ts @@ -309,9 +309,15 @@ export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () { ), ); - yield* connectivity.changes.pipe( - Stream.changes, - Stream.runForEach((networkStatus) => + // Follow resumes too: a transition dropped while the app was suspended would + // otherwise leave `offline` set, so the environment list stayed stale until + // another network event or a manual refresh. `followNetworkStatus` only + // reports actual changes, so a resume that reads the current status does not + // trigger a refresh. + yield* Connectivity.followNetworkStatus({ + connectivity, + wakeups, + apply: (networkStatus) => networkStatus === "offline" ? SubscriptionRef.update(state, (current) => ({ ...current, @@ -321,9 +327,7 @@ export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () { : Ref.get(hasRefreshed).pipe( Effect.flatMap((shouldRefresh) => (shouldRefresh ? refresh : Effect.void)), ), - ), - Effect.forkScoped, - ); + }); yield* wakeups.changes.pipe( Stream.runForEach((reason) => reason === "credentials-changed" From b0852f258a611eede4446edaa585838cedc5a8ae Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sun, 26 Jul 2026 08:16:27 +0200 Subject: [PATCH 08/11] perf(server): compress and cache packaged web assets (upstream #4516) Imported from https://github.com/pingdotgg/t3code/pull/4516 --- apps/server/src/http.ts | 86 +++++++- apps/server/src/staticAssetDelivery.test.ts | 223 ++++++++++++++++++++ apps/server/src/staticAssetDelivery.ts | 185 ++++++++++++++++ 3 files changed, 490 insertions(+), 4 deletions(-) create mode 100644 apps/server/src/staticAssetDelivery.test.ts create mode 100644 apps/server/src/staticAssetDelivery.ts diff --git a/apps/server/src/http.ts b/apps/server/src/http.ts index b9bb40f372d..69de791be35 100644 --- a/apps/server/src/http.ts +++ b/apps/server/src/http.ts @@ -37,6 +37,13 @@ import { } from "./auth/http.ts"; import * as ServerEnvironment from "./environment/ServerEnvironment.ts"; import { browserApiCorsAllowedHeaders, browserApiCorsAllowedMethods } from "./httpCors.ts"; +import { + contentCacheKey, + isCompressibleContentType, + makeStaticCompressionCache, + negotiateStaticEncoding, + resolveStaticCacheControl, +} from "./staticAssetDelivery.ts"; const OTLP_TRACES_PROXY_PATH = "/api/observability/v1/traces"; const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]); @@ -204,6 +211,8 @@ export const assetRouteLayer = HttpRouter.add( }), ); +const staticCompressionCache = makeStaticCompressionCache(); + export const staticAndDevRouteLayer = HttpRouter.add( "GET", "*", @@ -273,9 +282,18 @@ export const staticAndDevRouteLayer = HttpRouter.add( if (!indexData) { return HttpServerResponse.text("Not Found", { status: 404 }); } - return HttpServerResponse.uint8Array(indexData, { - status: 200, + return yield* respondWithStaticFile({ + data: indexData, contentType: "text/html; charset=utf-8", + // The SPA fallback always revalidates so a new build is picked up. + cacheControl: resolveStaticCacheControl("index.html"), + // Every deep link lands here, so the document is worth compressing. + // This is the one path whose file keeps a stable name across builds, + // so it is keyed by content: metadata read separately from the bytes + // could describe a different build than the one being served. The + // document is small enough for hashing it to be cheap. + cacheKey: contentCacheKey(indexData), + acceptEncoding: request.headers["accept-encoding"], }); } @@ -285,9 +303,69 @@ export const staticAndDevRouteLayer = HttpRouter.add( return HttpServerResponse.text("Internal Server Error", { status: 500 }); } - return HttpServerResponse.uint8Array(data, { - status: 200, + return yield* respondWithStaticFile({ + data, contentType, + cacheControl: resolveStaticCacheControl(staticRelativePath), + cacheKey: staticCacheKey(filePath, fileInfo), + acceptEncoding: request.headers["accept-encoding"], }); }), ); + +/** + * Identifies a build of a file for the compression cache, without hashing + * payloads that can run to megabytes. The stat is taken before the bytes are + * read, so a rebuild landing between the two can only orphan an entry under + * the superseded key, never publish those bytes under the newer one. Files + * whose contents change without their name are keyed by content instead; the + * rest carry a content hash in their filename already. + */ +function staticCacheKey(filePath: string, info: FileSystem.File.Info): string { + const mtimeMs = info.mtime.pipe( + Option.map((mtime) => mtime.getTime()), + Option.getOrElse(() => 0), + ); + return `${filePath} ${mtimeMs} ${info.size}`; +} + +const respondWithStaticFile = Effect.fn("staticAndDevRoute.respond")(function* (input: { + readonly data: Uint8Array; + readonly contentType: string; + readonly cacheControl: string; + /** Null for the SPA fallback, whose bytes are re-read on every request. */ + readonly cacheKey: string | null; + readonly acceptEncoding: string | undefined; +}) { + const headers: Record = { + "Cache-Control": input.cacheControl, + }; + + const encoding = isCompressibleContentType(input.contentType) + ? negotiateStaticEncoding(input.acceptEncoding) + : null; + if (isCompressibleContentType(input.contentType)) { + // Announce negotiation even when this client took the identity encoding, + // so shared caches do not hand a compressed body to a client that + // cannot read it. + headers["Vary"] = "Accept-Encoding"; + } + + const cacheKey = input.cacheKey; + const compressed = + encoding && cacheKey !== null + ? yield* Effect.tryPromise(() => + staticCompressionCache.get({ cacheKey, data: input.data, encoding }), + ).pipe(Effect.orElseSucceed(() => null)) + : null; + + if (compressed && encoding) { + headers["Content-Encoding"] = encoding; + } + + return HttpServerResponse.uint8Array(compressed ?? input.data, { + status: 200, + contentType: input.contentType, + headers, + }); +}); diff --git a/apps/server/src/staticAssetDelivery.test.ts b/apps/server/src/staticAssetDelivery.test.ts new file mode 100644 index 00000000000..9f3eff34e33 --- /dev/null +++ b/apps/server/src/staticAssetDelivery.test.ts @@ -0,0 +1,223 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { + contentCacheKey, + isCompressibleContentType, + makeStaticCompressionCache, + negotiateStaticEncoding, + resolveStaticCacheControl, +} from "./staticAssetDelivery.ts"; + +describe("contentCacheKey", () => { + const encode = (value: string) => new TextEncoder().encode(value); + + it("keys identical content the same way", () => { + expect(contentCacheKey(encode("a"))).toBe( + contentCacheKey(encode("a")), + ); + }); + + it("separates content of the same length", () => { + // A rebuild can reuse a filename, a size, and even a timestamp, so the + // key has to come from the bytes themselves. + expect(contentCacheKey(encode("a"))).not.toBe( + contentCacheKey(encode("b")), + ); + }); +}); + +describe("resolveStaticCacheControl", () => { + it("marks content-hashed bundle assets immutable", () => { + expect(resolveStaticCacheControl("assets/index-DxV9k2Qp.js")).toBe( + "public, max-age=31536000, immutable", + ); + expect(resolveStaticCacheControl("assets/style-a1b2c3d4.css")).toBe( + "public, max-age=31536000, immutable", + ); + }); + + it("revalidates entry documents and unhashed files", () => { + expect(resolveStaticCacheControl("index.html")).toBe("no-cache"); + expect(resolveStaticCacheControl("/index.html")).toBe("no-cache"); + // No hash means a rebuild reuses the name, so it must not be pinned. + expect(resolveStaticCacheControl("assets/logo.svg")).toBe("no-cache"); + expect(resolveStaticCacheControl("favicon.ico")).toBe("no-cache"); + }); +}); + +describe("negotiateStaticEncoding", () => { + it("prefers brotli when the client accepts both", () => { + expect(negotiateStaticEncoding("gzip, deflate, br")).toBe("br"); + }); + + it("falls back to gzip when brotli is absent", () => { + expect(negotiateStaticEncoding("gzip, deflate")).toBe("gzip"); + }); + + it("returns null when nothing usable is offered", () => { + expect(negotiateStaticEncoding(undefined)).toBeNull(); + expect(negotiateStaticEncoding("")).toBeNull(); + expect(negotiateStaticEncoding("deflate")).toBeNull(); + }); + + it("honors explicit refusals expressed as q=0", () => { + expect(negotiateStaticEncoding("br;q=0, gzip")).toBe("gzip"); + expect(negotiateStaticEncoding("gzip;q=0, br;q=0")).toBeNull(); + }); + + it("accepts a wildcard offer", () => { + expect(negotiateStaticEncoding("*")).toBe("br"); + }); +}); + +describe("isCompressibleContentType", () => { + it("compresses text and structured payloads", () => { + expect(isCompressibleContentType("text/html; charset=utf-8")).toBe(true); + expect(isCompressibleContentType("application/javascript")).toBe(true); + expect(isCompressibleContentType("image/svg+xml")).toBe(true); + }); + + it("leaves already-compressed binaries alone", () => { + expect(isCompressibleContentType("image/png")).toBe(false); + expect(isCompressibleContentType("font/woff2")).toBe(false); + expect(isCompressibleContentType("application/octet-stream")).toBe(false); + }); +}); + +describe("makeStaticCompressionCache", () => { + const bundle = new TextEncoder().encode("export const value = 1;\n".repeat(500)); + + it("compresses a payload once and reuses the result", async () => { + let compressCalls = 0; + const cache = makeStaticCompressionCache(async (data) => { + compressCalls += 1; + return data.subarray(0, 64); + }); + + const first = await cache.get({ cacheKey: "bundle.js 1 100", data: bundle, encoding: "gzip" }); + const second = await cache.get({ cacheKey: "bundle.js 1 100", data: bundle, encoding: "gzip" }); + + expect(first).not.toBeNull(); + expect(second).toBe(first); + expect(compressCalls).toBe(1); + }); + + it("compresses once for a burst of concurrent requests", async () => { + let compressCalls = 0; + let release = () => {}; + const started = new Promise((resolve) => { + release = resolve; + }); + const cache = makeStaticCompressionCache(async (data) => { + compressCalls += 1; + await started; + return data.subarray(0, 64); + }); + + const requests = [ + cache.get({ cacheKey: "bundle.js 1 100", data: bundle, encoding: "br" }), + cache.get({ cacheKey: "bundle.js 1 100", data: bundle, encoding: "br" }), + cache.get({ cacheKey: "bundle.js 1 100", data: bundle, encoding: "br" }), + ]; + release(); + const results = await Promise.all(requests); + + expect(compressCalls).toBe(1); + expect(results[1]).toBe(results[0]); + expect(results[2]).toBe(results[0]); + }); + + it("retries after a failed compression instead of caching the failure", async () => { + let compressCalls = 0; + const cache = makeStaticCompressionCache(async (data) => { + compressCalls += 1; + if (compressCalls === 1) throw new Error("zlib buffer error"); + return data.subarray(0, 64); + }); + + await expect( + cache.get({ cacheKey: "bundle.js 1 100", data: bundle, encoding: "gzip" }), + ).rejects.toThrow("zlib buffer error"); + + const retried = await cache.get({ + cacheKey: "bundle.js 1 100", + data: bundle, + encoding: "gzip", + }); + + expect(compressCalls).toBe(2); + expect(retried).not.toBeNull(); + }); + + it("recompresses when the file changes underneath the same path", async () => { + let compressCalls = 0; + const cache = makeStaticCompressionCache(async (data) => { + compressCalls += 1; + return data.subarray(0, 64); + }); + + await cache.get({ cacheKey: "bundle.js 1 100", data: bundle, encoding: "gzip" }); + await cache.get({ cacheKey: "bundle.js 2 140", data: bundle, encoding: "gzip" }); + + expect(compressCalls).toBe(2); + }); + + it("keeps brotli and gzip results apart", async () => { + const cache = makeStaticCompressionCache(async (data, encoding) => + new TextEncoder().encode(`${encoding}:${data.byteLength}`), + ); + + const brotli = await cache.get({ cacheKey: "bundle.js 1 100", data: bundle, encoding: "br" }); + const gzipped = await cache.get({ + cacheKey: "bundle.js 1 100", + data: bundle, + encoding: "gzip", + }); + + expect(new TextDecoder().decode(brotli ?? new Uint8Array())).toContain("br:"); + expect(new TextDecoder().decode(gzipped ?? new Uint8Array())).toContain("gzip:"); + }); + + it("skips payloads too small to be worth compressing", async () => { + let compressCalls = 0; + const cache = makeStaticCompressionCache(async (data) => { + compressCalls += 1; + return data; + }); + + const result = await cache.get({ + cacheKey: "tiny.js 1 8", + data: new TextEncoder().encode("const a=1;"), + encoding: "gzip", + }); + + expect(result).toBeNull(); + expect(compressCalls).toBe(0); + }); + + it("declines a result that did not get smaller", async () => { + const cache = makeStaticCompressionCache(async (data) => new Uint8Array(data.byteLength + 32)); + + const result = await cache.get({ + cacheKey: "incompressible.bin 1 100", + data: bundle, + encoding: "gzip", + }); + + expect(result).toBeNull(); + expect(cache.retainedByteLength).toBe(0); + }); + + it("really shrinks a realistic bundle with the default compressor", async () => { + const cache = makeStaticCompressionCache(); + + const compressed = await cache.get({ + cacheKey: "real.js 1 100", + data: bundle, + encoding: "br", + }); + + expect(compressed).not.toBeNull(); + expect((compressed as Uint8Array).byteLength).toBeLessThan(bundle.byteLength / 2); + }); +}); diff --git a/apps/server/src/staticAssetDelivery.ts b/apps/server/src/staticAssetDelivery.ts new file mode 100644 index 00000000000..fa7d41ec255 --- /dev/null +++ b/apps/server/src/staticAssetDelivery.ts @@ -0,0 +1,185 @@ +// @effect-diagnostics nodeBuiltinImport:off +import * as NodeCrypto from "node:crypto"; +import * as NodeZlib from "node:zlib"; +import * as NodeUtil from "node:util"; + +/** + * Delivery policy for the packaged web bundle. Vite emits content-hashed + * files under `assets/`, so those are immutable and safe to cache forever, + * while `index.html` and the SPA fallback must revalidate or a new build is + * never picked up. + */ +const IMMUTABLE_CACHE_CONTROL = "public, max-age=31536000, immutable"; +const REVALIDATE_CACHE_CONTROL = "no-cache"; + +/** Vite content hashes are at least 8 url-safe characters before the extension. */ +const HASHED_ASSET_PATTERN = /-[A-Za-z0-9_-]{8,}\.[A-Za-z0-9]+$/; + +/** + * Compressing tiny payloads costs more than it saves, and the header + * overhead can make the response larger than the original. + */ +const MIN_COMPRESSIBLE_BYTES = 1024; + +/** Total size of compressed payloads retained across all static files. */ +const COMPRESSED_CACHE_MAX_BYTES = 64 * 1024 * 1024; + +const COMPRESSIBLE_CONTENT_TYPES = [ + "text/", + "application/javascript", + "application/json", + "application/manifest+json", + "application/wasm", + "image/svg+xml", +]; + +const gzip = NodeUtil.promisify(NodeZlib.gzip); +const brotliCompress = NodeUtil.promisify(NodeZlib.brotliCompress); + +export type StaticContentEncoding = "br" | "gzip"; + +export function resolveStaticCacheControl(relativePath: string): string { + const normalized = relativePath.replaceAll("\\", "/").replace(/^\/+/, ""); + return normalized.startsWith("assets/") && HASHED_ASSET_PATTERN.test(normalized) + ? IMMUTABLE_CACHE_CONTROL + : REVALIDATE_CACHE_CONTROL; +} + +/** + * Cache key derived from the bytes being served rather than from file + * metadata. Used where the served content and the metadata could otherwise + * disagree, which would let one request's compression be reused for + * different content. Only worth it for small payloads, since it hashes the + * whole buffer on every request. + */ +export function contentCacheKey(data: Uint8Array): string { + return NodeCrypto.createHash("sha1").update(data).digest("hex"); +} + +export function isCompressibleContentType(contentType: string): boolean { + const normalized = contentType.toLowerCase(); + return COMPRESSIBLE_CONTENT_TYPES.some((prefix) => normalized.startsWith(prefix)); +} + +/** + * Pick an encoding from `Accept-Encoding`, preferring Brotli. Entries with an + * explicit `q=0` are refusals, and `identity;q=0` plus an unsupported codec + * list leaves nothing usable, so the caller falls back to the raw bytes. + */ +export function negotiateStaticEncoding( + acceptEncoding: string | undefined, +): StaticContentEncoding | null { + if (!acceptEncoding) return null; + + const acceptedQualityByCodec = new Map(); + for (const entry of acceptEncoding.split(",")) { + const [rawCodec = "", ...parameters] = entry.trim().split(";"); + const codec = rawCodec.trim().toLowerCase(); + if (!codec) continue; + const quality = parameters + .map((parameter) => /^\s*q=([0-9.]+)\s*$/i.exec(parameter)) + .find((match) => match !== null)?.[1]; + acceptedQualityByCodec.set(codec, quality === undefined ? 1 : Number.parseFloat(quality)); + } + + const accepts = (codec: StaticContentEncoding) => { + const quality = acceptedQualityByCodec.get(codec) ?? acceptedQualityByCodec.get("*"); + return quality !== undefined && quality > 0; + }; + + if (accepts("br")) return "br"; + if (accepts("gzip")) return "gzip"; + return null; +} + +async function compressBytes( + data: Uint8Array, + encoding: StaticContentEncoding, +): Promise { + if (encoding === "gzip") { + return new Uint8Array(await gzip(data)); + } + return new Uint8Array( + await brotliCompress(data, { + params: { + // Default quality 11 costs seconds on a multi-megabyte bundle. 5 is + // the usual static-asset sweet spot, and every result is cached. + [NodeZlib.constants.BROTLI_PARAM_QUALITY]: 5, + [NodeZlib.constants.BROTLI_PARAM_SIZE_HINT]: data.byteLength, + }, + }), + ); +} + +/** + * Compress once per (file, encoding) and reuse the result. Packaged assets + * never change while the server runs, so recompressing a multi-megabyte + * bundle for every remote request is pure waste. Entries are keyed by mtime + * and size so a dev-server rebuild is not served stale. + */ +export function makeStaticCompressionCache(compress = compressBytes) { + const compressedByKey = new Map(); + /** + * Compressions already running. A cold start requests the bundle, its CSS, + * and its chunks at once, and browsers retry, so without this the same + * multi-megabyte payload is compressed once per concurrent request. + */ + const inFlightByKey = new Map>(); + let retainedBytes = 0; + + const evictOldest = (incomingBytes: number) => { + while (retainedBytes + incomingBytes > COMPRESSED_CACHE_MAX_BYTES) { + const oldestKey = compressedByKey.keys().next().value; + if (oldestKey === undefined) return; + retainedBytes -= compressedByKey.get(oldestKey)?.byteLength ?? 0; + compressedByKey.delete(oldestKey); + } + }; + + const compressAndRetain = async ( + key: string, + data: Uint8Array, + encoding: StaticContentEncoding, + ): Promise => { + const compressed = await compress(data, encoding); + // A payload that grew under compression is not worth serving encoded. + if (compressed.byteLength >= data.byteLength) return null; + + if (compressed.byteLength <= COMPRESSED_CACHE_MAX_BYTES) { + evictOldest(compressed.byteLength); + compressedByKey.set(key, compressed); + retainedBytes += compressed.byteLength; + } + return compressed; + }; + + return { + get(input: { + readonly cacheKey: string; + readonly data: Uint8Array; + readonly encoding: StaticContentEncoding; + }): Promise { + if (input.data.byteLength < MIN_COMPRESSIBLE_BYTES) return Promise.resolve(null); + + const key = `${input.encoding} ${input.cacheKey}`; + const cached = compressedByKey.get(key); + if (cached) return Promise.resolve(cached); + + const pending = inFlightByKey.get(key); + if (pending) return pending; + + // Clear the in-flight entry however this settles, so a failed + // compression is retried rather than remembered as permanently pending. + const compression = compressAndRetain(key, input.data, input.encoding).finally(() => { + inFlightByKey.delete(key); + }); + inFlightByKey.set(key, compression); + return compression; + }, + get retainedByteLength(): number { + return retainedBytes; + }, + }; +} + +export type StaticCompressionCache = ReturnType; From e40af2ca56da3fecd6d7271eb2f05884ee815bfc Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sun, 26 Jul 2026 08:16:33 +0200 Subject: [PATCH 09/11] perf(server): back off repeated checkpoint capture failures (upstream #4517) Imported from https://github.com/pingdotgg/t3code/pull/4517 --- .../src/checkpointing/CaptureBackoff.test.ts | 191 ++++++++++++++++++ .../src/checkpointing/CaptureBackoff.ts | 146 +++++++++++++ .../CheckpointCaptureBackoff.test.ts | 168 +++++++++++++++ .../src/checkpointing/CheckpointStore.ts | 39 +++- 4 files changed, 542 insertions(+), 2 deletions(-) create mode 100644 apps/server/src/checkpointing/CaptureBackoff.test.ts create mode 100644 apps/server/src/checkpointing/CaptureBackoff.ts create mode 100644 apps/server/src/checkpointing/CheckpointCaptureBackoff.test.ts diff --git a/apps/server/src/checkpointing/CaptureBackoff.test.ts b/apps/server/src/checkpointing/CaptureBackoff.test.ts new file mode 100644 index 00000000000..6e9a4c64f3d --- /dev/null +++ b/apps/server/src/checkpointing/CaptureBackoff.test.ts @@ -0,0 +1,191 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { cooldownForFailureCount, makeCaptureBackoff } from "./CaptureBackoff.ts"; + +const MINUTE = 60_000; +const CWD = "/repo/workspace"; + +describe("cooldownForFailureCount", () => { + it("tolerates transient failures before opening a cooldown", () => { + expect(cooldownForFailureCount(1)).toBe(0); + expect(cooldownForFailureCount(2)).toBe(0); + }); + + it("backs off further the longer capture keeps failing", () => { + expect(cooldownForFailureCount(3)).toBe(5 * MINUTE); + expect(cooldownForFailureCount(4)).toBe(10 * MINUTE); + expect(cooldownForFailureCount(5)).toBe(20 * MINUTE); + }); + + it("caps the cooldown so a workspace is retried eventually", () => { + expect(cooldownForFailureCount(20)).toBe(60 * MINUTE); + expect(cooldownForFailureCount(500)).toBe(60 * MINUTE); + }); +}); + +describe("makeCaptureBackoff", () => { + it("does not skip before the failure threshold is reached", () => { + const backoff = makeCaptureBackoff(); + + backoff.recordFailure(CWD, 0, "timeout"); + backoff.recordFailure(CWD, 1_000, "timeout"); + + expect(backoff.beginAttempt(CWD, 2_000).skip).toBe(false); + }); + + it("skips and replays the recorded failure once capture keeps failing", () => { + const backoff = makeCaptureBackoff(); + + for (const at of [0, 1_000, 2_000]) { + backoff.recordFailure(CWD, at, "git add timed out"); + } + + const decision = backoff.beginAttempt(CWD, 3_000); + expect(decision.skip).toBe(true); + expect(decision.lastError).toBe("git add timed out"); + expect(decision.remainingMs).toBeGreaterThan(0); + }); + + it("retries again once the cooldown elapses", () => { + const backoff = makeCaptureBackoff(); + + for (const at of [0, 0, 0]) { + backoff.recordFailure(CWD, at, "timeout"); + } + + expect(backoff.beginAttempt(CWD, 5 * MINUTE - 1).skip).toBe(true); + expect(backoff.beginAttempt(CWD, 5 * MINUTE).skip).toBe(false); + }); + + it("clears the record after a capture succeeds", () => { + const backoff = makeCaptureBackoff(); + + for (const at of [0, 0, 0]) { + backoff.recordFailure(CWD, at, "timeout"); + } + expect(backoff.beginAttempt(CWD, 1_000).skip).toBe(true); + + backoff.recordSuccess(CWD); + + expect(backoff.beginAttempt(CWD, 1_000).skip).toBe(false); + expect(backoff.trackedWorkspaceCount).toBe(0); + }); + + it("tracks each workspace independently", () => { + const backoff = makeCaptureBackoff(); + const healthy = "/repo/other"; + + for (const at of [0, 0, 0]) { + backoff.recordFailure(CWD, at, "timeout"); + } + + expect(backoff.beginAttempt(CWD, 1_000).skip).toBe(true); + expect(backoff.beginAttempt(healthy, 1_000).skip).toBe(false); + }); + + it("bounds how many workspaces it retains", () => { + const backoff = makeCaptureBackoff(); + + for (let index = 0; index < 400; index += 1) { + backoff.recordFailure(`/repo/workspace-${index}`, index, "timeout"); + } + + expect(backoff.trackedWorkspaceCount).toBe(256); + // The oldest entries are the ones dropped: an evicted workspace starts + // counting again from one, a retained one continues. + expect(backoff.recordFailure("/repo/workspace-0", 1_000, "timeout").consecutiveFailures).toBe( + 1, + ); + expect(backoff.recordFailure("/repo/workspace-399", 1_000, "timeout").consecutiveFailures).toBe( + 2, + ); + }); + + it("keeps a repeatedly failing workspace alive through eviction pressure", () => { + const backoff = makeCaptureBackoff(); + + // A workspace in active use fails on every turn while many one-off + // workspaces churn past it. + for (let index = 0; index < 400; index += 1) { + backoff.recordFailure(CWD, index, "timeout"); + backoff.recordFailure(`/repo/other-${index}`, index, "timeout"); + } + + expect(backoff.beginAttempt(CWD, 400).skip).toBe(true); + }); + + it("keeps a workspace that is only being skipped safe from eviction", () => { + const backoff = makeCaptureBackoff(); + + for (const at of [0, 0, 0]) { + backoff.recordFailure(CWD, at, "timeout"); + } + + // A skipped workspace never calls recordFailure again, so only the skip + // itself can keep it ahead of churn from unrelated workspaces. + for (let index = 0; index < 400; index += 1) { + expect(backoff.beginAttempt(CWD, 1_000).skip).toBe(true); + backoff.recordFailure(`/repo/other-${index}`, index, "timeout"); + } + + expect(backoff.beginAttempt(CWD, 1_000).skip).toBe(true); + }); + + it("does not reserve for a workspace that has not reached the threshold", () => { + const backoff = makeCaptureBackoff(); + + backoff.recordFailure(CWD, 0, "timeout"); + + // One transient failure must not suppress a capture running alongside it. + expect(backoff.beginAttempt(CWD, 1_000).skip).toBe(false); + expect(backoff.beginAttempt(CWD, 1_000).skip).toBe(false); + expect(backoff.beginAttempt(CWD, 2_000).skip).toBe(false); + }); + + it("releases only one caller when the cooldown expires", () => { + const backoff = makeCaptureBackoff(); + + for (const at of [0, 0, 0]) { + backoff.recordFailure(CWD, at, "timeout"); + } + + // Threads sharing a workspace can complete turns together, and only the + // first past the cooldown should pay for the capture. + const released = [ + backoff.beginAttempt(CWD, 5 * MINUTE), + backoff.beginAttempt(CWD, 5 * MINUTE), + backoff.beginAttempt(CWD, 5 * MINUTE), + ].filter((decision) => !decision.skip); + + expect(released).toHaveLength(1); + }); + + it("lets the reservation lapse when an attempt never reports back", () => { + const backoff = makeCaptureBackoff(); + + for (const at of [0, 0, 0]) { + backoff.recordFailure(CWD, at, "timeout"); + } + backoff.beginAttempt(CWD, 5 * MINUTE); + + // An interrupted capture reports neither success nor failure, so the + // reservation must expire on its own rather than wedge the workspace. + expect(backoff.beginAttempt(CWD, 5 * MINUTE + 30_000).skip).toBe(true); + expect(backoff.beginAttempt(CWD, 6 * MINUTE + 1).skip).toBe(false); + }); + + it("keeps extending the cooldown while failures continue", () => { + const backoff = makeCaptureBackoff(); + + for (const at of [0, 0, 0]) { + backoff.recordFailure(CWD, at, "timeout"); + } + const firstRemaining = backoff.beginAttempt(CWD, 0).remainingMs; + + // The next attempt after the cooldown fails again, so the wait grows. + backoff.recordFailure(CWD, 5 * MINUTE, "timeout"); + const secondRemaining = backoff.beginAttempt(CWD, 5 * MINUTE).remainingMs; + + expect(secondRemaining).toBeGreaterThan(firstRemaining); + }); +}); diff --git a/apps/server/src/checkpointing/CaptureBackoff.ts b/apps/server/src/checkpointing/CaptureBackoff.ts new file mode 100644 index 00000000000..d53a8278e68 --- /dev/null +++ b/apps/server/src/checkpointing/CaptureBackoff.ts @@ -0,0 +1,146 @@ +/** + * Consecutive-failure backoff for workspace checkpoint capture. + * + * Capture runs a full-tree `git add -A` under a temporary index after every + * completed turn. On a repository large enough to exceed the VCS process + * timeout, that capture can never succeed, so the unguarded retry pegs a CPU + * core for as long as any thread is in use and litters `.git/objects/pack` + * with `tmp_pack_*` files from each killed process. + * + * After a few consecutive failures for a workspace, capture is skipped for a + * growing cooldown instead of being retried every turn. Any success clears + * the record, so a transient failure (a lock held by a concurrent git + * command) costs nothing. + * + * @module CaptureBackoff + */ + +/** Failures tolerated before a workspace enters cooldown. */ +const FAILURE_THRESHOLD = 3; +const BASE_COOLDOWN_MS = 5 * 60_000; +const MAX_COOLDOWN_MS = 60 * 60_000; + +/** + * Only a workspace that later succeeds clears its own record, so a workspace + * that fails once and is then abandoned would otherwise be retained for the + * lifetime of the server. Bound the tracked set and evict least-recently + * touched entries; dropping one only costs a workspace its failure history. + */ +const MAX_TRACKED_WORKSPACES = 256; + +/** + * How long the first caller past an expired cooldown holds the retry to + * itself. Threads sharing a workspace complete turns independently, so + * without this they would all be released at once and launch the same + * expensive capture together. Comfortably longer than the VCS process + * timeout that kills a stuck capture, and it lapses on its own, so an + * attempt that never reports back cannot wedge the workspace. + */ +const ATTEMPT_RESERVATION_MS = 60_000; + +export interface CaptureBackoffDecision { + readonly skip: boolean; + /** Milliseconds left in the cooldown, for logging. Zero when not skipping. */ + readonly remainingMs: number; + /** + * The failure that opened the cooldown. Replayed instead of inventing a new + * error, so callers keep seeing the real reason capture is unavailable and + * the error channel is unchanged. + */ + readonly lastError: E | null; +} + +export function cooldownForFailureCount(consecutiveFailures: number): number { + if (consecutiveFailures < FAILURE_THRESHOLD) return 0; + const doublings = consecutiveFailures - FAILURE_THRESHOLD; + // Clamp the exponent before shifting so a long-lived workspace cannot + // overflow into a negative or infinite cooldown. + const scale = 2 ** Math.min(doublings, 10); + return Math.min(BASE_COOLDOWN_MS * scale, MAX_COOLDOWN_MS); +} + +export interface CaptureFailureOutcome { + readonly consecutiveFailures: number; + /** Zero while the workspace is still under the failure threshold. */ + readonly cooldownMs: number; +} + +interface WorkspaceRecord { + consecutiveFailures: number; + skipUntilMs: number; + lastError: E; +} + +/** + * Tracks capture health per workspace. Callers ask whether to skip, then + * report the outcome of any capture they actually ran. + */ +export function makeCaptureBackoff() { + const recordByCwd = new Map>(); + + /** Move a record to the most-recent position so eviction sees real usage. */ + const touch = (cwd: string, record: WorkspaceRecord) => { + recordByCwd.delete(cwd); + recordByCwd.set(cwd, record); + }; + + return { + /** + * Decide whether this caller should run a capture. Mutating: a caller + * released past an expired cooldown reserves the attempt, and any read + * refreshes eviction recency so a workspace being actively skipped is not + * evicted by churn from unrelated workspaces. + */ + beginAttempt(cwd: string, nowMs: number): CaptureBackoffDecision { + const record = recordByCwd.get(cwd); + if (!record) { + return { skip: false, remainingMs: 0, lastError: null }; + } + + touch(cwd, record); + // Below the threshold a workspace has no cooldown at all, and its zero + // deadline must not read as one that just expired: reserving there + // would let a single transient failure suppress a concurrent capture. + if (record.consecutiveFailures < FAILURE_THRESHOLD) { + return { skip: false, remainingMs: 0, lastError: null }; + } + + if (nowMs < record.skipUntilMs) { + return { + skip: true, + remainingMs: record.skipUntilMs - nowMs, + lastError: record.lastError, + }; + } + + record.skipUntilMs = nowMs + ATTEMPT_RESERVATION_MS; + return { skip: false, remainingMs: 0, lastError: null }; + }, + + recordSuccess(cwd: string): void { + recordByCwd.delete(cwd); + }, + + recordFailure(cwd: string, nowMs: number, error: E): CaptureFailureOutcome { + const consecutiveFailures = (recordByCwd.get(cwd)?.consecutiveFailures ?? 0) + 1; + const cooldownMs = cooldownForFailureCount(consecutiveFailures); + touch(cwd, { + consecutiveFailures, + skipUntilMs: cooldownMs === 0 ? 0 : nowMs + cooldownMs, + lastError: error, + }); + + while (recordByCwd.size > MAX_TRACKED_WORKSPACES) { + const oldestCwd = recordByCwd.keys().next().value; + if (oldestCwd === undefined) break; + recordByCwd.delete(oldestCwd); + } + + return { consecutiveFailures, cooldownMs }; + }, + + get trackedWorkspaceCount(): number { + return recordByCwd.size; + }, + }; +} diff --git a/apps/server/src/checkpointing/CheckpointCaptureBackoff.test.ts b/apps/server/src/checkpointing/CheckpointCaptureBackoff.test.ts new file mode 100644 index 00000000000..ab1b3351a97 --- /dev/null +++ b/apps/server/src/checkpointing/CheckpointCaptureBackoff.test.ts @@ -0,0 +1,168 @@ +import { it } from "@effect/vitest"; +import { ThreadId, VcsProcessTimeoutError } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import { describe, expect } from "vite-plus/test"; + +import * as CheckpointStore from "./CheckpointStore.ts"; +import { checkpointRefForThreadTurn } from "./Utils.ts"; +import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; +import type * as VcsDriver from "../vcs/VcsDriver.ts"; + +const CWD = "/repo/huge-monorepo"; +const THREAD_ID = ThreadId.make("thread-checkpoint-backoff"); + +const captureTimeout = new VcsProcessTimeoutError({ + operation: "GitVcsDriver.checkpoints.captureCheckpoint", + command: "git add", + cwd: CWD, + timeoutMs: 30_000, +}); + +/** + * A driver whose capture always exceeds the process timeout, matching a + * repository too large for a full-tree `git add -A` to finish in time. + */ +function makeAlwaysTimingOutRegistry( + captureAttempts: { count: number }, + options: { readonly succeedOnAttempt?: number } = {}, +) { + const checkpoints = { + captureCheckpoint: () => + Effect.suspend(() => { + captureAttempts.count += 1; + return captureAttempts.count === options.succeedOnAttempt + ? Effect.void + : Effect.fail(captureTimeout); + }), + hasCheckpointRef: () => Effect.succeed(false), + restoreCheckpoint: () => Effect.succeed(false), + diffCheckpoints: () => Effect.succeed(""), + deleteCheckpointRefs: () => Effect.void, + } as unknown as VcsDriver.VcsCheckpointOps; + + const handle = { + kind: "git" as const, + repository: { + kind: "git" as const, + rootPath: CWD, + metadataPath: `${CWD}/.git`, + freshness: { source: "cache" as const, checkedAt: 0 }, + }, + driver: { checkpoints } as unknown as VcsDriver.VcsDriver["Service"], + } as unknown as VcsDriverRegistry.VcsDriverHandle; + + return Layer.succeed( + VcsDriverRegistry.VcsDriverRegistry, + VcsDriverRegistry.VcsDriverRegistry.of({ + get: () => Effect.succeed(handle.driver), + detect: () => Effect.succeed(handle), + resolve: () => Effect.succeed(handle), + }), + ); +} + +describe("checkpoint capture backoff", () => { + it.effect("stops re-running a capture that keeps timing out", () => { + const captureAttempts = { count: 0 }; + + return Effect.gen(function* () { + const store = yield* CheckpointStore.CheckpointStore; + const capture = (turn: number) => + store + .captureCheckpoint({ + cwd: CWD, + checkpointRef: checkpointRefForThreadTurn(THREAD_ID, turn), + }) + .pipe(Effect.flip); + + // The first three turns each pay for a real capture attempt. + for (let turn = 1; turn <= 3; turn += 1) { + const error = yield* capture(turn); + expect(error._tag).toBe("VcsProcessTimeoutError"); + } + expect(captureAttempts.count).toBe(3); + + // Every later turn inside the cooldown replays the recorded failure + // without spawning git again. The cooldown is minutes long, so the rest + // of this test runs well inside it. + for (let turn = 4; turn <= 20; turn += 1) { + const error = yield* capture(turn); + expect(error).toBe(captureTimeout); + } + expect(captureAttempts.count).toBe(3); + }).pipe( + Effect.provide( + CheckpointStore.layer.pipe(Layer.provide(makeAlwaysTimingOutRegistry(captureAttempts))), + ), + ); + }); + + it.effect("does not hold a workspace when the driver cannot be resolved", () => { + const captureAttempts = { count: 0 }; + const registryFailure = new VcsProcessTimeoutError({ + operation: "VcsDriverRegistry.resolve", + command: "git rev-parse", + cwd: CWD, + timeoutMs: 5_000, + }); + const failingRegistry = Layer.succeed( + VcsDriverRegistry.VcsDriverRegistry, + VcsDriverRegistry.VcsDriverRegistry.of({ + get: () => Effect.fail(registryFailure), + detect: () => Effect.fail(registryFailure), + resolve: () => Effect.fail(registryFailure), + }) as never, + ); + + return Effect.gen(function* () { + const store = yield* CheckpointStore.CheckpointStore; + const capture = (turn: number) => + store + .captureCheckpoint({ + cwd: CWD, + checkpointRef: checkpointRefForThreadTurn(THREAD_ID, turn), + }) + .pipe(Effect.flip); + + // Failing before the driver runs still counts as a failure, so the + // first two turns stay under the threshold and keep retrying rather + // than being held by a stale reservation. + for (let turn = 1; turn <= 2; turn += 1) { + const error = yield* capture(turn); + expect(error).toBe(registryFailure); + } + expect(captureAttempts.count).toBe(0); + }).pipe(Effect.provide(CheckpointStore.layer.pipe(Layer.provide(failingRegistry)))); + }); + + it.effect("keeps capturing for a workspace that recovers", () => { + const captureAttempts = { count: 0 }; + + return Effect.gen(function* () { + const store = yield* CheckpointStore.CheckpointStore; + const capture = (turn: number) => + store.captureCheckpoint({ + cwd: CWD, + checkpointRef: checkpointRefForThreadTurn(THREAD_ID, turn), + }); + + // Two failures stay under the threshold, and the success that follows + // clears them, so a later isolated failure does not open a cooldown. + yield* capture(1).pipe(Effect.flip); + yield* capture(2).pipe(Effect.flip); + yield* capture(3); + yield* capture(4).pipe(Effect.flip); + yield* capture(5).pipe(Effect.flip); + yield* capture(6).pipe(Effect.flip); + + expect(captureAttempts.count).toBe(6); + }).pipe( + Effect.provide( + CheckpointStore.layer.pipe( + Layer.provide(makeAlwaysTimingOutRegistry(captureAttempts, { succeedOnAttempt: 3 })), + ), + ), + ); + }); +}); diff --git a/apps/server/src/checkpointing/CheckpointStore.ts b/apps/server/src/checkpointing/CheckpointStore.ts index f13aa4572c1..2dd6ab71dbd 100644 --- a/apps/server/src/checkpointing/CheckpointStore.ts +++ b/apps/server/src/checkpointing/CheckpointStore.ts @@ -14,10 +14,12 @@ * @module CheckpointStore */ import { VcsUnsupportedOperationError, type CheckpointRef } from "@t3tools/contracts"; +import * as Clock from "effect/Clock"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import { makeCaptureBackoff } from "./CaptureBackoff.ts"; import type { CheckpointStoreError } from "./Errors.ts"; import type { VcsCheckpointOps } from "../vcs/VcsDriver.ts"; import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; @@ -98,6 +100,7 @@ export class CheckpointStore extends Context.Service< export const make = Effect.gen(function* () { const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry; + const captureBackoff = makeCaptureBackoff(); const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* ( operation: string, @@ -122,8 +125,40 @@ export const make = Effect.gen(function* () { const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn( "captureCheckpoint", )(function* (input) { - const checkpoints = yield* resolveCheckpoints("CheckpointStore.captureCheckpoint", input.cwd); - return yield* checkpoints.captureCheckpoint(input); + const startedAt = yield* Clock.currentTimeMillis; + const decision = captureBackoff.beginAttempt(input.cwd, startedAt); + if (decision.skip && decision.lastError) { + // Spawning git here would repeat a capture that cannot succeed, so + // replay the failure the caller already handles instead of burning a + // CPU core and leaving another orphaned tmp_pack behind. + yield* Effect.logDebug("Skipping checkpoint capture during failure cooldown", { + cwdLength: input.cwd.length, + remainingMs: decision.remainingMs, + }); + return yield* Effect.fail(decision.lastError); + } + + // Resolving the driver sits inside the reported scope so that a failure + // before the capture runs still replaces this caller's reservation with a + // real outcome, rather than leaving the workspace held until it lapses. + return yield* Effect.gen(function* () { + const checkpoints = yield* resolveCheckpoints("CheckpointStore.captureCheckpoint", input.cwd); + return yield* checkpoints.captureCheckpoint(input); + }).pipe( + Effect.tap(() => Effect.sync(() => captureBackoff.recordSuccess(input.cwd))), + Effect.tapError((error) => + Effect.gen(function* () { + const failedAt = yield* Clock.currentTimeMillis; + const outcome = captureBackoff.recordFailure(input.cwd, failedAt, error); + yield* Effect.logWarning("Checkpoint capture failed", { + cwdLength: input.cwd.length, + consecutiveFailures: outcome.consecutiveFailures, + cooldownMs: outcome.cooldownMs, + error, + }); + }), + ), + ); }); const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn( From 5735c1238b7a8251640c4bc691d68793ea32cc5b Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sun, 26 Jul 2026 08:16:59 +0200 Subject: [PATCH 10/11] fix(server): preserve manually renamed thread titles (upstream #4558) Imported from https://github.com/pingdotgg/t3code/pull/4558\n\nAdapted to retain our provider restart-recovery constants while replacing the local default-title check with the shared policy. --- .../Layers/ProviderCommandReactor.ts | 7 +- .../Layers/ProviderRuntimeIngestion.test.ts | 217 +++++++++++++++++- .../Layers/ProviderRuntimeIngestion.ts | 18 +- apps/web/src/components/CommandPalette.tsx | 13 +- packages/shared/package.json | 4 + packages/shared/src/threadTitle.test.ts | 64 ++++++ packages/shared/src/threadTitle.ts | 17 ++ 7 files changed, 326 insertions(+), 14 deletions(-) create mode 100644 packages/shared/src/threadTitle.test.ts create mode 100644 packages/shared/src/threadTitle.ts diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts index b6588cbea37..1d2290f8914 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts @@ -13,6 +13,7 @@ import { type RuntimeMode, type TurnId, } from "@t3tools/contracts"; +import { isDefaultThreadTitle } from "@t3tools/shared/threadTitle"; import { isTemporaryWorktreeBranch, WORKTREE_BRANCH_PREFIX } from "@t3tools/shared/git"; import * as Cache from "effect/Cache"; import * as Cause from "effect/Cause"; @@ -106,7 +107,6 @@ const turnStartKeyForEvent = (event: ProviderIntentEvent): string => const HANDLED_TURN_START_KEY_MAX = 10_000; const HANDLED_TURN_START_KEY_TTL = Duration.minutes(30); const DEFAULT_RUNTIME_MODE: RuntimeMode = "full-access"; -const DEFAULT_THREAD_TITLE = "New thread"; const STARTUP_RECOVERY_CONCURRENCY = 4; export const RESTART_RECOVERY_CONTINUATION_INSTRUCTION = @@ -128,14 +128,13 @@ export function providerErrorLabelFromInstanceHint(input: { } function canReplaceThreadTitle(currentTitle: string, titleSeed?: string): boolean { - const trimmedCurrentTitle = currentTitle.trim(); - if (trimmedCurrentTitle === DEFAULT_THREAD_TITLE) { + if (isDefaultThreadTitle(currentTitle)) { return true; } const trimmedTitleSeed = titleSeed?.trim(); return trimmedTitleSeed !== undefined && trimmedTitleSeed.length > 0 - ? trimmedCurrentTitle === trimmedTitleSeed + ? currentTitle.trim() === trimmedTitleSeed : false; } diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts index b057f5afddb..c52c6cacf1d 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts @@ -2849,7 +2849,7 @@ describe("ProviderRuntimeIngestion", () => { const thread = await waitForThread( harness.readModel, (entry) => - entry.title === "Renamed by provider" && + entry.title === "Thread" && entry.activities.some( (activity: ProviderRuntimeTestActivity) => activity.kind === "turn.plan.updated", ) && @@ -2864,7 +2864,7 @@ describe("ProviderRuntimeIngestion", () => { ), ); - expect(thread.title).toBe("Renamed by provider"); + expect(thread.title).toBe("Thread"); const planActivity = thread.activities.find( (activity: ProviderRuntimeTestActivity) => activity.id === "evt-turn-plan-updated", @@ -2905,6 +2905,219 @@ describe("ProviderRuntimeIngestion", () => { expect(checkpoint?.checkpointRef).toBe("provider-diff:evt-turn-diff-updated"); }); + effectIt.effect("applies provider thread.metadata.updated when thread title is the default", () => + Effect.gen(function* () { + const harness = yield* Effect.promise(() => createHarness()); + const now = "2026-01-01T00:00:00.000Z"; + + yield* harness.engine.dispatch({ + type: "thread.create", + commandId: CommandId.make("cmd-thread-create-default"), + threadId: ThreadId.make("thread-default"), + projectId: asProjectId("project-1"), + title: "New thread", + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5-codex", + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "approval-required", + branch: null, + worktreePath: null, + createdAt: now, + }); + + harness.emit({ + type: "thread.metadata.updated", + eventId: asEventId("evt-thread-metadata-default"), + provider: ProviderDriverKind.make("codex"), + createdAt: now, + threadId: asThreadId("thread-default"), + payload: { + name: "Provider default title", + metadata: { source: "provider" }, + }, + }); + + yield* Effect.promise(() => harness.drain()); + + const thread = yield* Effect.promise(() => + waitForThread( + harness.readModel, + (entry) => entry.title === "Provider default title", + 2000, + asThreadId("thread-default"), + ), + ); + + expect(thread.title).toBe("Provider default title"); + }), + ); + + effectIt.effect( + "rejects provider thread.metadata.updated when thread title is already customized", + () => + Effect.gen(function* () { + const harness = yield* Effect.promise(() => createHarness()); + const now = "2026-01-01T00:00:00.000Z"; + + yield* harness.engine.dispatch({ + type: "thread.meta.update", + commandId: CommandId.make("cmd-thread-custom-title"), + threadId: ThreadId.make("thread-1"), + title: "My custom title", + }); + + harness.emit({ + type: "thread.metadata.updated", + eventId: asEventId("evt-thread-metadata-custom"), + provider: ProviderDriverKind.make("codex"), + createdAt: now, + threadId: asThreadId("thread-1"), + payload: { + name: "Provider override attempt", + metadata: { source: "provider" }, + }, + }); + + yield* Effect.promise(() => harness.drain()); + + yield* Effect.promise(() => + waitForThread( + harness.readModel, + (entry) => entry.id === "thread-1" && entry.title === "My custom title", + ), + ); + + const readModel = yield* Effect.promise(() => harness.readModel()); + const thread = readModel.threads.find((entry) => entry.id === "thread-1"); + expect(thread?.title).toBe("My custom title"); + }), + ); + + effectIt.effect("skips provider thread.metadata.updated when payload.name is missing", () => + Effect.gen(function* () { + const harness = yield* Effect.promise(() => createHarness()); + const now = "2026-01-01T00:00:00.000Z"; + + yield* harness.engine.dispatch({ + type: "thread.create", + commandId: CommandId.make("cmd-thread-create-no-name"), + threadId: ThreadId.make("thread-no-name"), + projectId: asProjectId("project-1"), + title: "New thread", + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5-codex", + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "approval-required", + branch: null, + worktreePath: null, + createdAt: now, + }); + + harness.emit({ + type: "thread.metadata.updated", + eventId: asEventId("evt-thread-metadata-no-name"), + provider: ProviderDriverKind.make("codex"), + createdAt: now, + threadId: asThreadId("thread-no-name"), + payload: {}, + }); + + yield* Effect.promise(() => harness.drain()); + + const readModel = yield* Effect.promise(() => harness.readModel()); + const thread = readModel.threads.find((entry) => entry.id === "thread-no-name"); + expect(thread?.title).toBe("New thread"); + }), + ); + + effectIt.effect("skips provider thread.metadata.updated when payload.name is empty string", () => + Effect.gen(function* () { + const harness = yield* Effect.promise(() => createHarness()); + const now = "2026-01-01T00:00:00.000Z"; + + yield* harness.engine.dispatch({ + type: "thread.create", + commandId: CommandId.make("cmd-thread-create-empty"), + threadId: ThreadId.make("thread-empty-name"), + projectId: asProjectId("project-1"), + title: "New thread", + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5-codex", + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "approval-required", + branch: null, + worktreePath: null, + createdAt: now, + }); + + harness.emit({ + type: "thread.metadata.updated", + eventId: asEventId("evt-thread-metadata-empty"), + provider: ProviderDriverKind.make("codex"), + createdAt: now, + threadId: asThreadId("thread-empty-name"), + payload: { + name: "", + }, + }); + + yield* Effect.promise(() => harness.drain()); + + const readModel = yield* Effect.promise(() => harness.readModel()); + const thread = readModel.threads.find((entry) => entry.id === "thread-empty-name"); + expect(thread?.title).toBe("New thread"); + }), + ); + + effectIt.effect( + "skips provider thread.metadata.updated when payload.name sanitizes to empty", + () => + Effect.gen(function* () { + const harness = yield* Effect.promise(() => createHarness()); + const now = "2026-01-01T00:00:00.000Z"; + + yield* harness.engine.dispatch({ + type: "thread.create", + commandId: CommandId.make("cmd-thread-create-whitespace"), + threadId: ThreadId.make("thread-whitespace-name"), + projectId: asProjectId("project-1"), + title: "New thread", + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-5-codex", + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "approval-required", + branch: null, + worktreePath: null, + createdAt: now, + }); + + harness.emit({ + type: "thread.metadata.updated", + eventId: asEventId("evt-thread-metadata-whitespace"), + provider: ProviderDriverKind.make("codex"), + createdAt: now, + threadId: asThreadId("thread-whitespace-name"), + payload: { + name: " ", + }, + }); + + yield* Effect.promise(() => harness.drain()); + + const readModel = yield* Effect.promise(() => harness.readModel()); + const thread = readModel.threads.find((entry) => entry.id === "thread-whitespace-name"); + expect(thread?.title).toBe("New thread"); + }), + ); + it("projects context window updates into normalized thread activities", async () => { const harness = await createHarness(); const now = "2026-01-01T00:00:00.000Z"; diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts index d901340e3ac..1e51b30968c 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts @@ -26,6 +26,7 @@ import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Stream from "effect/Stream"; import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker"; +import { isDefaultThreadTitle, sanitizeTitle } from "@t3tools/shared/threadTitle"; import { ProviderService } from "../../provider/Services/ProviderService.ts"; import { ProjectionTurnRepository } from "../../persistence/Services/ProjectionTurns.ts"; @@ -1744,12 +1745,17 @@ const make = Effect.gen(function* () { } if (event.type === "thread.metadata.updated" && event.payload.name) { - yield* orchestrationEngine.dispatch({ - type: "thread.meta.update", - commandId: yield* providerCommandId(event, "thread-meta-update"), - threadId: thread.id, - title: event.payload.name, - }); + if (isDefaultThreadTitle(thread.title)) { + const sanitized = sanitizeTitle(event.payload.name); + if (sanitized.length > 0) { + yield* orchestrationEngine.dispatch({ + type: "thread.meta.update", + commandId: yield* providerCommandId(event, "thread-meta-update"), + threadId: thread.id, + title: sanitized, + }); + } + } } if (event.type === "turn.diff.updated") { diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsx index a1d54e8cfc3..37d3830d016 100644 --- a/apps/web/src/components/CommandPalette.tsx +++ b/apps/web/src/components/CommandPalette.tsx @@ -59,6 +59,7 @@ import { useAtomCommand } from "../state/use-atom-command"; import { useAtomQueryRunner } from "../state/use-atom-query-runner"; import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments"; import { useProjects, useThreadShells } from "../state/entities"; +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models"; import { resolveThreadActionProjectRef, startNewThreadFromContext } from "../lib/chatThreadActions"; import { appendBrowsePathSegment, @@ -468,6 +469,14 @@ function CommandPaletteDialog(props: { ); } +function renderThreadLeadingContent(thread: EnvironmentThreadShell) { + return ; +} + +function renderThreadTrailingContent(thread: EnvironmentThreadShell) { + return ; +} + function OpenCommandPaletteDialog(props: { readonly openIntent: CommandPaletteOpenIntent | null; readonly setOpen: (open: boolean) => void; @@ -853,8 +862,8 @@ function OpenCommandPaletteDialog(props: { projectTitleById, sortOrder: clientSettings.sidebarThreadSortOrder, icon: , - renderLeadingContent: (thread) => , - renderTrailingContent: (thread) => , + renderLeadingContent: renderThreadLeadingContent, + renderTrailingContent: renderThreadTrailingContent, runThread: async (thread) => { await navigate({ to: "/$environmentId/$threadId", diff --git a/packages/shared/package.json b/packages/shared/package.json index 7d0cc5eb820..5757143dca0 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -163,6 +163,10 @@ "types": "./src/terminalLabels.ts", "import": "./src/terminalLabels.ts" }, + "./threadTitle": { + "types": "./src/threadTitle.ts", + "import": "./src/threadTitle.ts" + }, "./relayClient": { "types": "./src/relayClient.ts", "import": "./src/relayClient.ts" diff --git a/packages/shared/src/threadTitle.test.ts b/packages/shared/src/threadTitle.test.ts new file mode 100644 index 00000000000..a211c9796a9 --- /dev/null +++ b/packages/shared/src/threadTitle.test.ts @@ -0,0 +1,64 @@ +import { describe, it, expect } from "vite-plus/test"; +import { isDefaultThreadTitle, sanitizeTitle, DEFAULT_THREAD_TITLE } from "./threadTitle.ts"; + +describe("isDefaultThreadTitle", () => { + it("returns true for the default title", () => { + expect(isDefaultThreadTitle("New thread")).toBe(true); + }); + + it("returns true for the default title with whitespace", () => { + expect(isDefaultThreadTitle(" New thread ")).toBe(true); + }); + + it("returns false for a custom title", () => { + expect(isDefaultThreadTitle("My custom title")).toBe(false); + }); + + it("returns false for empty string", () => { + expect(isDefaultThreadTitle("")).toBe(false); + }); + + it("returns false for null", () => { + expect(isDefaultThreadTitle(null)).toBe(false); + }); + + it("returns false for undefined", () => { + expect(isDefaultThreadTitle(undefined)).toBe(false); + }); +}); + +describe("sanitizeTitle", () => { + it("preserves a normal title", () => { + expect(sanitizeTitle("Hello World")).toBe("Hello World"); + }); + + it("trims whitespace", () => { + expect(sanitizeTitle(" hello ")).toBe("hello"); + }); + + it("strips control characters", () => { + expect(sanitizeTitle("hello\x00world")).toBe("helloworld"); + expect(sanitizeTitle("hello\x1Fworld")).toBe("helloworld"); + }); + + it("returns empty string for whitespace-only input", () => { + expect(sanitizeTitle(" ")).toBe(""); + expect(sanitizeTitle(" \t ")).toBe(""); + }); + + it("returns empty string for control-char-only input", () => { + expect(sanitizeTitle("\x00")).toBe(""); + expect(sanitizeTitle("\x00\x00")).toBe(""); + }); + + it("truncates to MAX_TITLE_LENGTH", () => { + const long = "a".repeat(1000); + expect(sanitizeTitle(long).length).toBe(500); + }); +}); + +describe("DEFAULT_THREAD_TITLE", () => { + it("equals New thread", () => { + expect(DEFAULT_THREAD_TITLE).toBe("New thread"); + }); +}); diff --git a/packages/shared/src/threadTitle.ts b/packages/shared/src/threadTitle.ts new file mode 100644 index 00000000000..ee35dde673f --- /dev/null +++ b/packages/shared/src/threadTitle.ts @@ -0,0 +1,17 @@ +const MAX_TITLE_LENGTH = 500; + +export const DEFAULT_THREAD_TITLE = "New thread"; + +export function isDefaultThreadTitle(title: string | null | undefined): boolean { + return (title ?? "").trim() === DEFAULT_THREAD_TITLE; +} + +export function sanitizeTitle(title: string): string { + return title + .replace(/./g, (c) => { + const code = c.charCodeAt(0); + return code > 0x1f || code === 0x09 || code === 0x0a || code === 0x0d ? c : ""; + }) + .slice(0, MAX_TITLE_LENGTH) + .trim(); +} From 4e562da32fa9ed8364a632eb4958176ac0eeb4ac Mon Sep 17 00:00:00 2001 From: Patrick Roza Date: Sun, 26 Jul 2026 08:17:03 +0200 Subject: [PATCH 11/11] fix(web): avoid woke status overlap after snooze closes (upstream #4539) Imported from https://github.com/pingdotgg/t3code/pull/4539 --- apps/web/src/components/SidebarV2.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/SidebarV2.tsx b/apps/web/src/components/SidebarV2.tsx index 48bae67b553..be4437b5bff 100644 --- a/apps/web/src/components/SidebarV2.tsx +++ b/apps/web/src/components/SidebarV2.tsx @@ -879,10 +879,10 @@ const SidebarV2Row = memo(function SidebarV2Row(props: { ) : ( )} - +