From e01b0ab16928d665db3573cdeeffac47c74909a8 Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:25:58 +0000 Subject: [PATCH 1/3] fix: every app shows the same inbox for the same machines Machine owners were a per-device client setting, so two apps credited the same pre-inbox threads to different founders. The Settled count added the servers' unfiltered older-settled totals to filtered loaded rows, so it matched no inbox and differed from the servers' history. Each machine now reports its owner (`ownerFounder` server setting, advertised by `machineOwner`), and clients resolve attribution with it, falling back to their own map only for servers too old to report one. `olderSettled` counts carry groups by primary inbox and sender, using the same sender rule as clients, so the Settled total applies the list filter to threads no app has loaded. Counts from older servers show as "N+" under a partial filter. Model: claude-opus-5-5 Task: fix Agent: claude-code Co-Authored-By: Claude Opus 5.5 --- .../features/threads/useThreadListV2.test.tsx | 1 + .../src/mcp/toolkits/environment/handlers.ts | 2 + .../src/mcp/toolkits/environment/tools.ts | 3 + .../orchestration-v2/ProjectionStore.test.ts | 90 ++++- .../src/orchestration-v2/ProjectionStore.ts | 65 +++- .../src/orchestration-v2/ShellStream.test.ts | 58 ++- .../src/orchestration-v2/ShellStream.ts | 20 +- apps/server/src/orchestration-v2/http.test.ts | 10 +- .../orchestration-v2/olderSettledCounts.ts | 59 +++ apps/server/src/serverSettings.test.ts | 33 ++ apps/server/src/ws.test.ts | 13 +- apps/server/src/ws.ts | 1 + apps/web/src/components/Sidebar.tsx | 59 ++- .../settings/NewThreadInboxSettings.tsx | 113 +++--- .../components/sidebar/SidebarListMarkers.tsx | 9 +- .../sidebar/threadListFilter.logic.ts | 51 +-- apps/web/src/state/olderSettledShelf.ts | 13 +- docs/internals/connection-runtime.md | 5 + docs/user/thread-sidebar.md | 8 + packages/client-runtime/package.json | 4 + .../src/state/inboxConvergence.test.ts | 335 ++++++++++++++++++ .../src/state/olderSettledThreads.test.ts | 9 + .../src/state/olderSettledThreads.ts | 83 ++++- .../src/state/threadAttribution.ts | 116 +++--- .../src/state/threadListFilter.ts | 88 +++++ packages/contracts/src/orchestrationV2.ts | 24 +- packages/contracts/src/server.ts | 5 + packages/contracts/src/settings.ts | 7 + packages/contracts/src/threadInbox.ts | 76 +++- 29 files changed, 1151 insertions(+), 209 deletions(-) create mode 100644 apps/server/src/orchestration-v2/olderSettledCounts.ts create mode 100644 packages/client-runtime/src/state/inboxConvergence.test.ts create mode 100644 packages/client-runtime/src/state/threadListFilter.ts diff --git a/apps/mobile/src/features/threads/useThreadListV2.test.tsx b/apps/mobile/src/features/threads/useThreadListV2.test.tsx index 7eac8955efa14..b9a3b545e7fbd 100644 --- a/apps/mobile/src/features/threads/useThreadListV2.test.tsx +++ b/apps/mobile/src/features/threads/useThreadListV2.test.tsx @@ -199,6 +199,7 @@ it("reuses a large thread list across tab renders and updates when its inputs ch before, beforeMs: Date.parse(before), counts: new Map([[ProjectId.make("project-test"), 575]]), + groups: null, }; await act(async () => { registry.set( diff --git a/apps/server/src/mcp/toolkits/environment/handlers.ts b/apps/server/src/mcp/toolkits/environment/handlers.ts index fb1716885d6e8..243c5c8a14fb2 100644 --- a/apps/server/src/mcp/toolkits/environment/handlers.ts +++ b/apps/server/src/mcp/toolkits/environment/handlers.ts @@ -15,12 +15,14 @@ export function preferences(settings: ServerSettings) { enableProviderUpdateChecks, backgroundActivity, sourceControlWritingStyle, + ownerFounder, } = settings; const characters = Array.from(sourceControlWritingStyle.customInstructions); return { defaultThreadEnvMode, newWorktreesStartFromOrigin, enableProviderUpdateChecks, + ownerFounder, backgroundActivity: { profile: backgroundActivity.profile }, sourceControlWritingStyle: { ...sourceControlWritingStyle, diff --git a/apps/server/src/mcp/toolkits/environment/tools.ts b/apps/server/src/mcp/toolkits/environment/tools.ts index 9e10380295de2..4f9e152b66d03 100644 --- a/apps/server/src/mcp/toolkits/environment/tools.ts +++ b/apps/server/src/mcp/toolkits/environment/tools.ts @@ -19,6 +19,8 @@ const PreferenceFields = { defaultThreadEnvMode: ServerSettings.fields.defaultThreadEnvMode, newWorktreesStartFromOrigin: ServerSettings.fields.newWorktreesStartFromOrigin, enableProviderUpdateChecks: ServerSettings.fields.enableProviderUpdateChecks, + /** The founder this machine belongs to; threads without sender details count as theirs. */ + ownerFounder: ServerSettings.fields.ownerFounder, backgroundActivity: Schema.Struct({ profile: BackgroundActivityProfileSelection }), sourceControlWritingStyle: Schema.Struct({ mode: Schema.String, @@ -62,6 +64,7 @@ const EnvironmentPreferencesTool = Tool.make("t3_environment_preferences_update" defaultThreadEnvMode: ServerSettingsPatch.fields.defaultThreadEnvMode, newWorktreesStartFromOrigin: ServerSettingsPatch.fields.newWorktreesStartFromOrigin, enableProviderUpdateChecks: ServerSettingsPatch.fields.enableProviderUpdateChecks, + ownerFounder: ServerSettingsPatch.fields.ownerFounder, backgroundActivity: Schema.optionalKey(Schema.Struct({ profile: BackgroundActivityProfile })), sourceControlWritingStyle: ServerSettingsPatch.fields.sourceControlWritingStyle, }), diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index b961ef1e0dc34..b1d8d9ec81459 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -30,8 +30,12 @@ import { ThreadId, TurnItemId, type ThreadPullRequestLink, + type OrchestrationV2OlderSettledCount, + type ThreadInbox, + inboxRoutingFor, isLiveSubagentShell, isListedThreadLineage, + makeThreadInbox, } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; @@ -157,6 +161,8 @@ const assertShellIndexCoversShells = Effect.fnUntraced(function* () { | "pinnedAt" | "pinOrderKey" | "snoozedUntil" + | "inbox" + | "createdBy" >, ) => ({ id: row.id, @@ -167,6 +173,8 @@ const assertShellIndexCoversShells = Effect.fnUntraced(function* () { pinnedAt: iso(row.pinnedAt), pinOrderKey: row.pinOrderKey ?? null, snoozedUntil: iso(row.snoozedUntil), + inbox: row.inbox ?? null, + createdBy: row.createdBy ?? null, }); assert.deepEqual(index.map(trimmed), shells.map(trimmed)); shells.forEach((shell, position) => { @@ -3077,6 +3085,8 @@ it.effect("SQL list older settled threads below a cursor", () => readonly subagent?: boolean; readonly archived?: boolean; readonly snoozedUntilDay?: number; + readonly inbox?: ThreadInbox; + readonly parent?: string; }) => store.apply({ id: EventId.make(`event:older:${input.name}`), @@ -3096,10 +3106,16 @@ it.effect("SQL list older settled threads below a cursor", () => branch: null, worktreePath: null, activeProviderThreadId: null, + ...(input.inbox === undefined ? {} : { inbox: input.inbox }), lineage: { - parentThreadId: input.subagent === true ? id("b") : null, + parentThreadId: + input.subagent === true + ? id("b") + : input.parent === undefined + ? null + : id(input.parent), relationshipToParent: input.subagent === true ? "subagent" : null, - rootThreadId: input.subagent === true ? id("b") : id(input.name), + rootThreadId: input.subagent === true ? id("b") : id(input.parent ?? input.name), }, forkedFrom: null, createdAt: now, @@ -3127,24 +3143,21 @@ it.effect("SQL list older settled threads below a cursor", () => limit: number, projectIds?: ReadonlyArray, ) => store.listOlderSettledThreads({ before, projectIds, limit }); + const totals = (listing: { + readonly counts: ReadonlyArray; + }) => listing.counts.map(({ projectId, count }) => ({ projectId, count })); const before = { settledAt: DateTime.formatIso(at(10)) }; const first = yield* list(before, 2); assert.deepEqual(first, { threadIds: [id("a"), id("b")], counts: [] }); - assert.sameDeepMembers( - [...(yield* list(before, 0)).counts], - [ - { projectId: ProjectId.make("project:a"), count: 4 }, - { projectId: ProjectId.make("project:b"), count: 1 }, - ], - ); + assert.sameDeepMembers(totals(yield* list(before, 0)), [ + { projectId: ProjectId.make("project:a"), count: 4 }, + { projectId: ProjectId.make("project:b"), count: 1 }, + ]); // A cursor without an id is strict, as the snapshot's cut is: the day-4 tie is not counted. - assert.sameDeepMembers( - [...(yield* list({ settledAt: DateTime.formatIso(at(4)) }, 0)).counts], - [ - { projectId: ProjectId.make("project:a"), count: 1 }, - { projectId: ProjectId.make("project:b"), count: 1 }, - ], - ); + assert.sameDeepMembers(totals(yield* list({ settledAt: DateTime.formatIso(at(4)) }, 0)), [ + { projectId: ProjectId.make("project:a"), count: 1 }, + { projectId: ProjectId.make("project:b"), count: 1 }, + ]); // The cursor is the last row: the tie on day 4 continues by id. const next = yield* list({ settledAt: DateTime.formatIso(at(4)), threadId: id("b") }, 10); assert.deepEqual(next.threadIds, [id("c"), id("d"), id("snoozed")]); @@ -3153,8 +3166,51 @@ it.effect("SQL list older settled threads below a cursor", () => const scoped = yield* list(before, 0, [ProjectId.make("project:b")]); assert.deepEqual(scoped, { threadIds: [], - counts: [{ projectId: ProjectId.make("project:b"), count: 1 }], + counts: [ + { + projectId: ProjectId.make("project:b"), + count: 1, + groups: [ + { inbox: null, agentCreated: false, founder: "machine", byAgent: false, count: 1 }, + ], + }, + ], + }); + + // Recounts group like snapshots: by routing, and by sender through an active parent. + yield* createThread({ + name: "jeff-root", + settledDay: null, + project: "project:c", + inbox: makeThreadInbox({ actor: { kind: "human", id: "jeff" } }), + }); + yield* createThread({ + name: "agent-child", + settledDay: 6, + project: "project:c", + parent: "jeff-root", + inbox: makeThreadInbox({ + actor: { kind: "agent", id: "lead" }, + routing: inboxRoutingFor("agents"), + }), }); + yield* createThread({ + name: "kameron", + settledDay: 7, + project: "project:c", + inbox: makeThreadInbox({ actor: { kind: "human", id: "kameron" } }), + }); + // The index carries the routing the snapshot's counts group by, as the shells do. + yield* assertShellIndexCoversShells(); + const routed = yield* list(before, 0, [ProjectId.make("project:c")]); + assert.equal(routed.counts.length, 1); + assert.sameDeepMembers( + [...(routed.counts[0]?.groups ?? [])], + [ + { inbox: "agents", agentCreated: false, founder: "jeff", byAgent: true, count: 1 }, + { inbox: "kameron", agentCreated: false, founder: "kameron", byAgent: false, count: 1 }, + ], + ); }).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index b98ccff45e783..2f398ea0df544 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -80,6 +80,7 @@ import { NodeId, ProjectId, ProviderInstanceId, + ThreadInbox, } from "@t3tools/contracts"; import { createOrchestrationV2TurnItemVisibility } from "@t3tools/shared/orchestrationV2Timeline"; import { @@ -90,6 +91,7 @@ import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; @@ -98,6 +100,7 @@ import type * as Statement from "effect/sql/Statement"; import { MCP_APP_OUTPUT_KEY } from "@t3tools/shared/mcpApp"; import { threadHtmlRenderAttachmentIds } from "../attachmentStore.ts"; import { readableSearchText } from "./messageSearchIndex.ts"; +import { countOlderSettledThreads } from "./olderSettledCounts.ts"; import { isFoldableWorkItem, isThreadHistoryUserTurn, @@ -474,6 +477,9 @@ export interface ActiveShellIndexEntry extends Pick< | "pinOrderKey" | "snoozedUntil" > { + /** Read only to count older settled threads by sender. */ + readonly inbox?: ThreadInbox | undefined; + readonly createdBy?: string | undefined; /** False only when no run, request, work item, or watch can make the shell live. */ readonly mayBeLive: boolean; /** Whether it delegated a task to a linked environment, whose clients nest that task under it. */ @@ -1206,6 +1212,19 @@ const shellIndexLineage = (row: { ...(row.side_chat === null ? {} : { sideChat: row.side_chat === 1 }), }); +const decodeIndexInbox = Schema.decodeUnknownOption(Schema.fromJsonString(ThreadInbox)); +/** The fields older settled counts group an index row by, as they read its shell. */ +const shellIndexSender = (row: { + readonly inbox_json: string | null; + readonly created_by: string | null; +}): Pick => { + const inbox = row.inbox_json === null ? Option.none() : decodeIndexInbox(row.inbox_json); + return { + ...(Option.isSome(inbox) ? { inbox: inbox.value } : {}), + ...(row.created_by === null ? {} : { createdBy: row.created_by }), + }; +}; + const encodeIdList = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String))); // Empty filters still scan SQLite rows; short-circuit them, including turnItemTypes: []. @@ -8956,12 +8975,16 @@ export const layer: Layer.Layer = readonly pinned_at: string | null; readonly pin_order_key: string | null; readonly snoozed_until: string | null; + readonly inbox_json: string | null; + readonly created_by: string | null; readonly may_be_live: number; readonly has_remote_tasks: number; }>` SELECT t.thread_id, t.project_id, + json_extract(t.payload_json, '$.inbox') AS inbox_json, + json_extract(t.payload_json, '$.createdBy') AS created_by, json_extract(t.payload_json, '$.lineage.relationshipToParent') AS relationship_to_parent, json_extract(t.payload_json, '$.lineage.parentThreadId') AS parent_thread_id, json_extract(t.payload_json, '$.lineage.rootThreadId') AS root_thread_id, @@ -9030,6 +9053,7 @@ export const layer: Layer.Layer = pinnedAt: row.pinned_at === null ? null : DateTime.makeUnsafe(row.pinned_at), pinOrderKey: row.pin_order_key, snoozedUntil: row.snoozed_until === null ? null : DateTime.makeUnsafe(row.snoozed_until), + ...shellIndexSender(row), mayBeLive: row.may_be_live === 1, hasRemoteTasks: row.has_remote_tasks === 1, })); @@ -9171,21 +9195,44 @@ export const layer: Layer.Layer = ORDER BY older.settled_at DESC, older.thread_id ASC LIMIT ${limit} `; + // Every active row, so the counts group by sender through parents as snapshots do. const countRows = limit !== 0 ? [] - : yield* sql<{ readonly project_id: string; readonly count: number }>` - WITH older AS (${older}) - SELECT older.project_id, COUNT(*) AS count FROM older - WHERE ${belowCursor} - GROUP BY older.project_id + : yield* sql<{ + readonly thread_id: string; + readonly project_id: string; + readonly parent_thread_id: string | null; + readonly inbox_json: string | null; + readonly created_by: string | null; + readonly is_older: number; + }>` + SELECT t.thread_id, t.project_id, + json_extract(t.payload_json, '$.lineage.parentThreadId') AS parent_thread_id, + json_extract(t.payload_json, '$.inbox') AS inbox_json, + json_extract(t.payload_json, '$.createdBy') AS created_by, + t.thread_id IN ( + SELECT older.thread_id FROM (${older}) older WHERE ${belowCursor} + ) AS is_older + FROM orchestration_v2_projection_threads t + WHERE t.deleted_at IS NULL + AND t.archived_at IS NULL `; return { threadIds: pageRows.map((row) => ThreadId.make(row.thread_id)), - counts: countRows.map((row) => ({ - projectId: ProjectId.make(row.project_id), - count: row.count, - })), + counts: countOlderSettledThreads( + countRows.map((row) => ({ + id: ThreadId.make(row.thread_id), + projectId: ProjectId.make(row.project_id), + lineage: { + parentThreadId: + row.parent_thread_id === null ? null : ThreadId.make(row.parent_thread_id), + }, + ...shellIndexSender(row), + isOlder: row.is_older === 1, + })), + (row) => row.isOlder, + ), }; }, Effect.mapError(shellSnapshotReadError)); diff --git a/apps/server/src/orchestration-v2/ShellStream.test.ts b/apps/server/src/orchestration-v2/ShellStream.test.ts index fbc610235dd01..3be601a23e7a6 100644 --- a/apps/server/src/orchestration-v2/ShellStream.test.ts +++ b/apps/server/src/orchestration-v2/ShellStream.test.ts @@ -5,7 +5,13 @@ import type { OrchestrationV2ShellStreamItem, OrchestrationV2ThreadShell, } from "@t3tools/contracts"; -import { ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"; +import { + inboxRoutingFor, + makeThreadInbox, + ProjectId, + ProviderInstanceId, + ThreadId, +} from "@t3tools/contracts"; import type * as Cause from "effect/Cause"; import * as DateTime from "effect/DateTime"; import * as Deferred from "effect/Deferred"; @@ -245,6 +251,8 @@ describe("omitOlderSettledShells", () => { const newest = Array.from({ length: 25 }, (_, index) => row(`new-${index}`, index / 10)); const omit = (threads: ReadonlyArray) => omitOlderSettledShells({ threads, snapshotSequence: 7, now }); + // History from before inbox routing: its machine's owner's, which each client applies. + const HISTORY = { inbox: null, agentCreated: false, founder: "machine", byAgent: false } as const; it("keeps the 25 newest settled threads and counts the rest per project", () => { const sideChat = row("side-chat", 0, { @@ -271,12 +279,52 @@ describe("omitOlderSettledShells", () => { before: DateTime.formatIso(DateTime.makeUnsafe(100 * DAY - 2.4 * DAY)), asOfSequence: 7, counts: [ - { projectId: "project-a", count: 1 }, - { projectId: "project-b", count: 1 }, + { projectId: "project-a", count: 1, groups: [{ ...HISTORY, count: 1 }] }, + { projectId: "project-b", count: 1, groups: [{ ...HISTORY, count: 1 }] }, ], }); }); + it("groups older counts by what an inbox filter reads, through each parent", () => { + const kameron = makeThreadInbox({ actor: { kind: "human", id: "kameron" } }); + const agentWork = makeThreadInbox({ + actor: { kind: "agent", id: "lead" }, + routing: inboxRoutingFor("agents"), + }); + const result = omit([ + ...newest, + row("history", 30), + row("agent-history", 31, { createdBy: "agent" }), + row("kameron", 32, { inbox: kameron }), + // Unowned agent work is its parent's founder's, even when the parent is not older. + row("jeff-parent", null, { + inbox: makeThreadInbox({ actor: { kind: "human", id: "jeff" } }), + }), + row("agent-child", 33, { + inbox: agentWork, + lineage: { + parentThreadId: ThreadId.make("jeff-parent"), + relationshipToParent: null, + rootThreadId: ThreadId.make("jeff-parent"), + }, + }), + row("agent-orphan", 34, { inbox: agentWork }), + ]); + expect(result.olderSettled.counts).toEqual([ + { + projectId: "project-a", + count: 5, + groups: [ + { ...HISTORY, count: 1 }, + { ...HISTORY, agentCreated: true, byAgent: true, count: 1 }, + { inbox: "kameron", agentCreated: false, founder: "kameron", byAgent: false, count: 1 }, + { inbox: "agents", agentCreated: false, founder: "jeff", byAgent: true, count: 1 }, + { inbox: "agents", agentCreated: false, founder: null, byAgent: true, count: 1 }, + ], + }, + ]); + }); + it("keeps threads tied with the 25th newest", () => { const result = omit([...newest, row("tie", 2.4), row("older", 2.5)]); expect(result.threads.map((thread) => thread.id).slice(25)).toEqual(["tie"]); @@ -326,7 +374,9 @@ describe("omitOlderSettledShells", () => { "side-chat-parent", "side-chat", ]); - expect(result.olderSettled.counts).toEqual([{ projectId: "project-a", count: 5 }]); + expect(result.olderSettled.counts).toEqual([ + { projectId: "project-a", count: 5, groups: [{ ...HISTORY, count: 5 }] }, + ]); }); }); diff --git a/apps/server/src/orchestration-v2/ShellStream.ts b/apps/server/src/orchestration-v2/ShellStream.ts index 5ee1005b16830..24d0dc6bcc9ec 100644 --- a/apps/server/src/orchestration-v2/ShellStream.ts +++ b/apps/server/src/orchestration-v2/ShellStream.ts @@ -8,7 +8,6 @@ import type { OrchestrationV2ThreadShellSnapshot, OrchestrationV2ShellStreamItem, OrchestrationV2StoredEvent, - ProjectId, ThreadId, } from "@t3tools/contracts"; import { @@ -36,6 +35,7 @@ import * as Stream from "effect/Stream"; import type * as OrchestrationEventStore from "../persistence/OrchestrationEventStore.ts"; import { bufferLatestLiveStream } from "./LiveStreamBudget.ts"; +import { countOlderSettledThreads, type OlderSettledCountRow } from "./olderSettledCounts.ts"; import type { ActiveShellIndexEntry } from "./ProjectionStore.ts"; type ShellDelta = Extract; @@ -180,7 +180,8 @@ function buildActiveShellSnapshot(input: { type ShellTrimRow = Pick< OrchestrationV2ThreadShell, "id" | "projectId" | "lineage" | "settledOverride" | "settledAt" -> & { readonly hasRemoteTasks?: boolean }; +> & + Pick & { readonly hasRemoteTasks?: boolean }; /** * What a subscriber's trims read across every active row. Computed from full shells or from the @@ -219,20 +220,17 @@ function shellTrimFacts(rows: ReadonlyArray): ShellTrimFacts { settledTimes.sort((left, right) => right - left); // With fewer than 25, the oldest one is the cut, so nothing is older. const olderSettledBeforeMs = settledTimes[NEWEST_SETTLED_KEPT - 1] ?? settledTimes.at(-1); - const counts = new Map(); - if (olderSettledBeforeMs !== undefined) { - for (const row of rows) { - if (isOlderSettledThreadShell(row, olderSettledBeforeMs)) { - counts.set(row.projectId, (counts.get(row.projectId) ?? 0) + 1); - } - } - } return { subagentParentIds, subagentThreadIds, remoteTaskParentIds, olderSettledBeforeMs, - olderSettledCounts: Array.from(counts, ([projectId, count]) => ({ projectId, count })), + olderSettledCounts: + olderSettledBeforeMs === undefined + ? [] + : countOlderSettledThreads(rows, (row) => + isOlderSettledThreadShell(row, olderSettledBeforeMs), + ), }; } diff --git a/apps/server/src/orchestration-v2/http.test.ts b/apps/server/src/orchestration-v2/http.test.ts index 07385bf134d08..e65c05ea0c6f1 100644 --- a/apps/server/src/orchestration-v2/http.test.ts +++ b/apps/server/src/orchestration-v2/http.test.ts @@ -383,7 +383,15 @@ it.effect("leaves older settled threads out of the HTTP shell only when asked", expect(cut.threads.map((row) => row.id).toSorted()).toEqual( [thread.id, ...Array.from({ length: 25 }, (_, index) => `settled-${index + 1}`)].toSorted(), ); - expect(cut.olderSettled?.counts).toEqual([{ projectId: thread.projectId, count: 1 }]); + expect(cut.olderSettled?.counts).toEqual([ + { + projectId: thread.projectId, + count: 1, + groups: [ + { inbox: null, agentCreated: false, founder: "machine", byAgent: false, count: 1 }, + ], + }, + ]); // A client that holds rows the socket can replay from keeps them; any other cursor reloads. expect(full.projectIdentities).toBeTypeOf("string"); diff --git a/apps/server/src/orchestration-v2/olderSettledCounts.ts b/apps/server/src/orchestration-v2/olderSettledCounts.ts new file mode 100644 index 0000000000000..fd282d251cd13 --- /dev/null +++ b/apps/server/src/orchestration-v2/olderSettledCounts.ts @@ -0,0 +1,59 @@ +import { + primaryInbox, + resolveThreadSenders, + type OrchestrationV2OlderSettledCount, + type OrchestrationV2OlderSettledGroup, + type OrchestrationV2ThreadShell, + type ProjectId, + type ThreadInbox, +} from "@t3tools/contracts"; + +/** What counting reads from a thread, from its shell or the store's index. */ +export type OlderSettledCountRow = Pick & { + readonly lineage: Pick; + readonly inbox?: ThreadInbox | undefined; + readonly createdBy?: string | undefined; +}; + +/** + * Per project, the rows `isOlder` matches, grouped by what an inbox filter reads. `rows` holds + * every active thread, so a sender's parent chain resolves like it does on a client holding them. + */ +export function countOlderSettledThreads( + rows: ReadonlyArray, + isOlder: (row: Row) => boolean, +): ReadonlyArray { + const older = rows.filter(isOlder); + if (older.length === 0) return []; + const senders = resolveThreadSenders( + rows.map((row) => ({ + key: row.id, + parentKey: row.lineage.parentThreadId, + inbox: row.inbox, + createdBy: row.createdBy, + })), + ); + const byProject = new Map< + ProjectId, + { count: number; groups: Map } + >(); + for (const row of older) { + const sender = senders.get(row.id)!; + const group = { + inbox: primaryInbox(row.inbox), + agentCreated: row.createdBy === "agent", + founder: sender.founder, + byAgent: sender.byAgent, + }; + const key = `${group.inbox}|${group.agentCreated}|${group.founder}|${group.byAgent}`; + const project = byProject.get(row.projectId) ?? { count: 0, groups: new Map() }; + byProject.set(row.projectId, project); + project.count += 1; + project.groups.set(key, { ...group, count: (project.groups.get(key)?.count ?? 0) + 1 }); + } + return Array.from(byProject, ([projectId, { count, groups }]) => ({ + projectId, + count, + groups: [...groups.values()], + })); +} diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts index 0ed8033768e8e..53f40196179e1 100644 --- a/apps/server/src/serverSettings.test.ts +++ b/apps/server/src/serverSettings.test.ts @@ -2,8 +2,10 @@ import { deriveProviderInstanceConfigMap } from "./provider/ProviderInstanceRegi import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { + AuthSettingsWriteScope, DEFAULT_SERVER_SETTINGS, globalIdleCompaction, + requiredScopesForServerSettingsPatch, type ManagedPolicyDocument, ModelSelection, ProjectId, @@ -255,6 +257,37 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" }).pipe(Effect.provide(layerServerSettings())), ); + it.effect("stores the machine's owner, sends it to clients, and clears it", () => + Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + const fs = yield* FileSystem.FileSystem; + const service = yield* ServerSettingsModule.ServerSettingsService; + assert.isNull((yield* service.getSettings).ownerFounder); + // An ordinary settings write, not an access change. + assert.deepEqual(requiredScopesForServerSettingsPatch({ ownerFounder: "jeff" }), [ + AuthSettingsWriteScope, + ]); + + yield* service.updateSettings({ ownerFounder: "kameron" }); + const persisted = yield* decodeServerSettingsJson( + yield* fs.readFileString(config.settingsPath), + ); + assert.equal(persisted.ownerFounder, "kameron"); + const client = ServerSettingsModule.redactServerSettingsForClient( + yield* service.getSettings, + { secretReferences: true }, + ); + assert.equal(client.ownerFounder, "kameron"); + + yield* service.updateSettings({ ownerFounder: null }); + assert.isNull((yield* service.getSettings).ownerFounder); + assert.isNull( + (yield* decodeServerSettingsJson(yield* fs.readFileString(config.settingsPath))) + .ownerFounder, + ); + }).pipe(Effect.provide(layerServerSettings())), + ); + it.effect("keeps an override key a newer server saved when a project setting is edited", () => Effect.gen(function* () { const config = yield* ServerConfig.ServerConfig; diff --git a/apps/server/src/ws.test.ts b/apps/server/src/ws.test.ts index d79969d62a980..48cbd71031766 100644 --- a/apps/server/src/ws.test.ts +++ b/apps/server/src/ws.test.ts @@ -805,9 +805,16 @@ it.effect("shell snapshots omit idle children and older settled threads only on withoutOld?.threads.map((thread) => thread.id), ["parent", "live", ...recent, "remote-parent", "remote-child-parent"], ); - assert.deepEqual(withoutOld?.olderSettled?.counts, [ - { projectId: ProjectId.make("project"), count: 2 }, - ]); + const counts = withoutOld?.olderSettled?.counts ?? []; + assert.deepEqual( + counts.map(({ projectId, count }) => ({ projectId, count })), + [{ projectId: ProjectId.make("project"), count: 2 }], + ); + // The index-built snapshot breaks the count down too, for clients' inbox filters. + assert.equal( + counts[0]?.groups?.reduce((total, group) => total + group.count, 0), + counts[0]?.count, + ); }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)), ); diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index feb2ef6ac65a9..290d9667760b2 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -1951,6 +1951,7 @@ export const layerWsRpc = ( shellThreadFamilies: true, threadLinkMetadata: true, shellOlderSettled: true, + machineOwner: true, threadResumeCompletionMarker: true, threadSnapshotPagination: true, threadFind: true, diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index ab7a874106bcc..1e8fd255341b7 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -1,9 +1,14 @@ import { SidebarThreadFilterMenu } from "./sidebar/SidebarThreadFilterMenu"; import { matchesThreadListFilter, threadListFilterOf } from "./sidebar/threadListFilter.logic"; import { + resolveMachineOwners, resolveThreadAttributions, threadAttributionKey, } from "@t3tools/client-runtime/state/thread-attribution"; +import { + matchesOlderSettledGroup, + threadListFilterCoverage, +} from "@t3tools/client-runtime/state/thread-list-filter"; import { threadRoutingUpdateFromMenuAction } from "./threadActionMenu.logic"; import { attachSidebarTree } from "../lib/sidebarTree"; import { type EnvironmentId, type ScopedProjectRef } from "@t3tools/contracts"; @@ -69,6 +74,7 @@ import { } from "@t3tools/contracts"; import { resolveListedAncestorThreadRef, + type OlderSettledFilter, type OlderSettledScope, } from "@t3tools/client-runtime/state/threads"; @@ -337,7 +343,6 @@ const sidebarProviderEntriesAtom = Atom.make((get) => { const EMPTY_PROVIDER_ENTRIES: ReadonlyMap = new Map(); const EMPTY_THREADS: readonly EnvironmentThreadShell[] = []; -const NO_MACHINE_OWNERS = {}; const SETTLED_TAIL_INITIAL_COUNT = 10; const SETTLED_TAIL_PAGE_COUNT = 25; @@ -432,6 +437,8 @@ function useSidebarThreads(input: { readonly connectedEnvironmentIds: readonly EnvironmentId[]; readonly optimisticDrop: SidebarOptimisticDrop | null; readonly threads: readonly EnvironmentThreadShell[]; + /** The list filter `threads` passed, applied to the older settled threads servers count. */ + readonly olderSettledFilter: OlderSettledFilter | null; readonly delegatedUnderParent: ReadonlySet; readonly routeRowThreadKey: string | null; // Set in boring mode: the project groups to fold active threads into, in order. @@ -446,6 +453,7 @@ function useSidebarThreads(input: { connectedEnvironmentIds, optimisticDrop, threads, + olderSettledFilter, delegatedUnderParent, routeRowThreadKey, folderGroups, @@ -511,8 +519,13 @@ function useSidebarThreads(input: { }); const listedSettledThreads = settledThreads.filter(olderSettledShelf.isListed); useOlderSettledShelfGrowth(olderSettledShelf, listedSettledThreads); - const settledCount = - settledThreads.length + olderSettledShelf.unloadedCount(partition.visibleThreads); + // The servers' total for this filter, not just the loaded rows, so every client agrees. + const settledTotal = olderSettledShelf.total({ + threads: partition.visibleThreads, + settledCount: settledThreads.length, + filter: olderSettledFilter, + }); + const settledCount = settledTotal.count; const visibleSettledThreads = (() => { const visible = listedSettledThreads.slice(0, settledVisibleCount); if ( @@ -587,8 +600,13 @@ function useSidebarThreads(input: { ], settledVisibleCount, settledCount, + settledCountExact: settledTotal.exact, olderSettledShelf, - hiddenSettledCount: Math.max(0, settledCount - visibleSettledThreads.length), + // A lower-bound total still offers the pages a server has left. + hiddenSettledCount: Math.max( + settledCount - visibleSettledThreads.length, + !settledTotal.exact && olderSettledShelf.canLoadMore ? 1 : 0, + ), showMoreSettled: () => setSettledTail((current) => ({ scopeKey: current.scopeKey, @@ -1110,19 +1128,25 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve const promotingDraftThreadKeys = usePromotingDraftThreadKeys(); const threadListFilter = useClientSettings(threadListFilterOf); const founderByEnvironmentId = useClientSettings((settings) => settings.founderByEnvironmentId); - const threadAttributions = resolveThreadAttributions( - threads, - founderByEnvironmentId ?? NO_MACHINE_OWNERS, - ); + const machineOwners = resolveMachineOwners(serverConfigs, founderByEnvironmentId); + const threadAttributions = resolveThreadAttributions(threads, machineOwners); const attributionOf = (thread: Pick) => threadAttributions.get(threadAttributionKey(thread)) ?? "unknown"; // Search always looks across every inbox, sender and environment. - const inboxThreads = - threadSearchQuery.trim().length > 0 - ? threads - : threads.filter((thread) => - matchesThreadListFilter(thread, threadListFilter, attributionOf(thread)), - ); + const listFilterApplies = threadSearchQuery.trim().length === 0; + const inboxThreads = listFilterApplies + ? threads.filter((thread) => + matchesThreadListFilter(thread, threadListFilter, attributionOf(thread)), + ) + : threads; + // The same filter over threads servers counted but did not send, for the Settled total. + const olderSettledFilter: OlderSettledFilter | null = listFilterApplies + ? { + matches: (environmentId, group) => + matchesOlderSettledGroup(environmentId, group, threadListFilter, machineOwners), + coverage: (environmentId) => threadListFilterCoverage(environmentId, threadListFilter), + } + : null; const listedThreads = promotingDraftThreadKeys.length === 0 ? inboxThreads @@ -1333,6 +1357,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve searchableThreads, settledVisibleCount, settledCount, + settledCountExact, olderSettledShelf, hiddenSettledCount, showMoreSettled, @@ -1363,6 +1388,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve connectedEnvironmentIds, optimisticDrop, threads: listedThreads, + olderSettledFilter, delegatedUnderParent, routeRowThreadKey, folderGroups: boringSidebarEnabled @@ -3647,6 +3673,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve )} label="Settled" count={settledCount} + countIsLowerBound={!settledCountExact} dragging={from !== null} isDropTarget={dragTargetSection === "settled"} toggle={{ @@ -3698,7 +3725,9 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve ? "Loading" : olderSettledShelf.failed ? "Could not load older threads. Retry" - : `Show ${Math.min(hiddenSettledCount, SETTLED_TAIL_PAGE_COUNT)} more`} + : settledCountExact + ? `Show ${Math.min(hiddenSettledCount, SETTLED_TAIL_PAGE_COUNT)} more` + : "Show more"} ) : null} diff --git a/apps/web/src/components/settings/NewThreadInboxSettings.tsx b/apps/web/src/components/settings/NewThreadInboxSettings.tsx index 06c16ea9b5a0c..9f3bca0de4046 100644 --- a/apps/web/src/components/settings/NewThreadInboxSettings.tsx +++ b/apps/web/src/components/settings/NewThreadInboxSettings.tsx @@ -1,11 +1,15 @@ -import type { EnvironmentId, Founder } from "@t3tools/contracts"; +import { resolveMachineOwners } from "@t3tools/client-runtime/state/thread-attribution"; +import { AuthSettingsWriteScope, type EnvironmentId, type Founder } from "@t3tools/contracts"; import { getClientSettings, updateClientSettings, useClientSettings, + useUpdateEnvironmentSettings, } from "../../hooks/useSettings"; +import { useServerConfigs } from "../../state/entities"; import { useEnvironmentIdentities } from "../../state/environments"; +import { useEnvironmentScope } from "../../state/session"; import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; import { SettingsRow } from "./settingsLayout"; import { searchableSetting } from "./settingsSearch"; @@ -44,53 +48,82 @@ export function NewThreadInboxRow() { const UNSET = "unset"; -/** Which founder each machine belongs to, for threads older than inbox attribution. */ +/** + * Which founder each machine belongs to, for threads older than inbox attribution. The machine + * stores it, so every app agrees; servers too old to store it use this device's own map. + */ export function MachineOwnersRow() { const environments = useEnvironmentIdentities(); - const owners = useClientSettings((settings) => settings.founderByEnvironmentId) ?? {}; - const setOwner = (environmentId: EnvironmentId, founder: Founder | null) => { - // Read at change time, so a second quick edit or another tab's edit is not overwritten. - const next = { ...getClientSettings().founderByEnvironmentId }; - if (founder === null) delete next[environmentId]; - else next[environmentId] = founder; - void updateClientSettings({ founderByEnvironmentId: next }); - }; + const serverConfigs = useServerConfigs(); + const legacyOwners = useClientSettings((settings) => settings.founderByEnvironmentId); + const owners = resolveMachineOwners(serverConfigs, legacyOwners); return (
- {environments.map(({ environmentId, label }) => { - const owner = owners[environmentId]; - return ( -
- {label} -
- -
-
- ); - })} + {environments.map(({ environmentId, label }) => ( + + ))}
); } + +function MachineOwnerRow(props: { + environmentId: EnvironmentId; + label: string; + owner: Founder | undefined; + reportsOwner: boolean; +}) { + const { environmentId, label, owner, reportsOwner } = props; + const canWrite = useEnvironmentScope(environmentId, AuthSettingsWriteScope); + const updateEnvironmentSettings = useUpdateEnvironmentSettings(environmentId); + const setOwner = (founder: Founder | null) => { + if (reportsOwner) updateEnvironmentSettings({ ownerFounder: founder }); + // Kept for servers too old to store an owner. Read at change time, so a second quick edit + // or another tab's edit is not overwritten. + const next = { ...getClientSettings().founderByEnvironmentId }; + if (founder === null) delete next[environmentId]; + else next[environmentId] = founder; + void updateClientSettings({ founderByEnvironmentId: next }); + }; + return ( +
+ {label} +
+ +
+
+ ); +} diff --git a/apps/web/src/components/sidebar/SidebarListMarkers.tsx b/apps/web/src/components/sidebar/SidebarListMarkers.tsx index da9444a5c1b24..60a0173db0bbf 100644 --- a/apps/web/src/components/sidebar/SidebarListMarkers.tsx +++ b/apps/web/src/components/sidebar/SidebarListMarkers.tsx @@ -115,6 +115,8 @@ export function SidebarSectionHeader(props: { label: string; /** A muted trailing count. */ count: number; + /** The count leaves out threads a server could not break down, so it reads "N+". */ + countIsLowerBound?: boolean; className?: string; dragging?: boolean; isDropTarget?: boolean; @@ -138,7 +140,12 @@ export function SidebarSectionHeader(props: { expanded={props.toggle.expanded} tone={props.isDropTarget ? "accent" : props.dragging ? "emphasized" : "muted"} data-testid={`sidebar-${shelf}-shelf-toggle`} - accessory={{props.count}} + accessory={ + + {props.count} + {props.countIsLowerBound ? "+" : ""} + + } > {props.label} diff --git a/apps/web/src/components/sidebar/threadListFilter.logic.ts b/apps/web/src/components/sidebar/threadListFilter.logic.ts index 5699d5b92fd0f..6991999624709 100644 --- a/apps/web/src/components/sidebar/threadListFilter.logic.ts +++ b/apps/web/src/components/sidebar/threadListFilter.logic.ts @@ -1,19 +1,22 @@ +import { + matchesListedInboxes, + type ThreadListFilter, +} from "@t3tools/client-runtime/state/thread-list-filter"; import { effectiveInboxFilter, Founder, InboxChoice, - matchesInboxFilter, primaryInbox, type EnvironmentId, type ThreadInbox, } from "@t3tools/contracts"; -import type { ClientSettings, ThreadAttribution } from "@t3tools/contracts/settings"; +import type { ThreadAttribution } from "@t3tools/contracts/settings"; -export interface ThreadListFilter { - readonly inboxes: ReadonlyArray; - readonly hiddenEnvironmentIds: ReadonlyArray; - readonly hiddenAttributions: ReadonlyArray; -} +export { + matchesThreadListFilter, + threadListFilterOf, + type ThreadListFilter, +} from "@t3tools/client-runtime/state/thread-list-filter"; interface FilterableThread { readonly environmentId: EnvironmentId; @@ -23,36 +26,12 @@ interface FilterableThread { }; } -export const threadListFilterOf = ( - settings: Pick< - ClientSettings, - | "founderInboxFilter" - | "founderInboxOwner" - | "sidebarHiddenEnvironmentIds" - | "sidebarHiddenThreadAttributions" - >, -): ThreadListFilter => ({ - inboxes: effectiveInboxFilter(settings), - hiddenEnvironmentIds: settings.sidebarHiddenEnvironmentIds ?? [], - hiddenAttributions: settings.sidebarHiddenThreadAttributions ?? [], -}); - const matchesInboxes = (thread: FilterableThread, inboxes: ReadonlyArray) => - matchesInboxFilter(thread.source.inbox, inboxes) || - // Agent-created threads from before inbox routing carry no Agents routing. - (inboxes.includes("agents") && - thread.source.createdBy === "agent" && - primaryInbox(thread.source.inbox) === null); - -/** Without an attribution (notifications), the From section does not apply. */ -export const matchesThreadListFilter = ( - thread: FilterableThread, - filter: ThreadListFilter, - attribution?: ThreadAttribution, -) => - !filter.hiddenEnvironmentIds.includes(thread.environmentId) && - (attribution === undefined || !filter.hiddenAttributions.includes(attribution)) && - matchesInboxes(thread, filter.inboxes); + matchesListedInboxes( + primaryInbox(thread.source.inbox), + thread.source.createdBy === "agent", + inboxes, + ); /** The chosen founder's inbox first, then Agents, so the common pair sits together. */ export const inboxMenuOrder = (owner: Founder | undefined): ReadonlyArray => { diff --git a/apps/web/src/state/olderSettledShelf.ts b/apps/web/src/state/olderSettledShelf.ts index e313c805dd3cc..2b97e41958199 100644 --- a/apps/web/src/state/olderSettledShelf.ts +++ b/apps/web/src/state/olderSettledShelf.ts @@ -3,9 +3,11 @@ import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/model import { isListedSettledThread, settledShelfFrontier, + settledShelfTotal, sortOlderSettledScopes, unloadedOlderSettledCount, wantedOlderSettledPages, + type OlderSettledFilter, type OlderSettledScope, } from "@t3tools/client-runtime/state/threads"; import type { EnvironmentId, ProjectId } from "@t3tools/contracts"; @@ -67,9 +69,14 @@ export function useOlderSettledShelf(input: { pages, grow: (next: ReadonlyMap) => setRequested({ scopeKey, pages: next }), isListed: (thread: EnvironmentThreadShell) => isListedSettledThread(thread.source, frontier), - /** Older settled threads in scope that are not local; add it to the local Settled rows. */ - unloadedCount: (threads: ReadonlyArray) => - unloadedOlderSettledCount({ olderSettled: olderOf, scopes, threads }), + /** The shelf's total under the list filter; see `settledShelfTotal`. */ + total: (input: { + readonly threads: ReadonlyArray; + readonly settledCount: number; + readonly filter: OlderSettledFilter | null; + }) => settledShelfTotal({ olderSettled: olderOf, scopes, ...input }), + /** Some environment still has older pages to read. */ + canLoadMore: frontier !== null, loading: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "pending"), failed: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "failed"), retry: () => { diff --git a/docs/internals/connection-runtime.md b/docs/internals/connection-runtime.md index 58eb388a12a13..6967e09230426 100644 --- a/docs/internals/connection-runtime.md +++ b/docs/internals/connection-runtime.md @@ -125,6 +125,11 @@ server first. The Settled count is local Settled rows, minus local rows below the cut, plus the server's counts ([older settled threads](../../packages/client-runtime/src/state/olderSettledThreads.ts)). +Both sides pass through the list's inbox filter: each count is broken into groups by +primary inbox and sender (`resolveThreadSenders`), and a sender's `"machine"` resolves +through the owner the machine reports. A local row below the cut then cancels itself on +both sides, so the total is the same on every client whichever pages it loaded. Counts +without groups, from older servers, are exact only for a filter that shows everything. The counts are re-read when a removal or an old row's arrival or change could move them, so archiving any thread costs one small read. A shelf lists an older row only above the highest page frontier of the environments that can still diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md index 905f3a5bfbe9c..980c768eb1002 100644 --- a/docs/user/thread-sidebar.md +++ b/docs/user/thread-sidebar.md @@ -210,6 +210,14 @@ rules to connected environments that support shared settings. Offline environmen and older servers keep their previous values. Changing a rule does not reopen already settled threads. +The **Settled** count includes older settled threads the shelf has not loaded yet, filtered like +the list, so every app connected to the same machines shows the same number. A count such as +`25+` means an older server reported only its total, so filtered threads it holds are not counted. + +Threads from before inbox routing are credited to their machine's owner. Set the owner once per +machine in **Settings → Threads → Machine owners**. The machine keeps it, so every app connected to +that machine agrees. Machines on older versions use each device's own choice instead. + ## Link a pull request The server finds the PR for each unsettled thread's saved branch, even when your diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.json index 8aa773bb8d076..ac9f7d9e5ccbe 100644 --- a/packages/client-runtime/package.json +++ b/packages/client-runtime/package.json @@ -343,6 +343,10 @@ "types": "./src/state/threadAttribution.ts", "default": "./src/state/threadAttribution.ts" }, + "./state/thread-list-filter": { + "types": "./src/state/threadListFilter.ts", + "default": "./src/state/threadListFilter.ts" + }, "./state/thread-activity-announcement": { "types": "./src/state/threadActivityAnnouncement.ts", "default": "./src/state/threadActivityAnnouncement.ts" diff --git a/packages/client-runtime/src/state/inboxConvergence.test.ts b/packages/client-runtime/src/state/inboxConvergence.test.ts new file mode 100644 index 0000000000000..2d904f67f669c --- /dev/null +++ b/packages/client-runtime/src/state/inboxConvergence.test.ts @@ -0,0 +1,335 @@ +import { + EnvironmentId, + InboxChoice, + inboxRoutingFor, + isOlderSettledThreadShell, + makeThreadInbox, + primaryInbox, + ProjectId, + resolveThreadSenders, + ThreadId, + type Founder, + type OrchestrationV2OlderSettledCount, + type OrchestrationV2ThreadShell, + type ThreadInbox, +} from "@t3tools/contracts"; +import type { ThreadAttribution } from "@t3tools/contracts/settings"; +import * as DateTime from "effect/DateTime"; +import { describe, expect, it } from "vite-plus/test"; + +import { + olderSettledCountsOf, + settledShelfTotal, + type OlderSettled, +} from "./olderSettledThreads.ts"; +import { + resolveMachineOwners, + resolveThreadAttributions, + threadAttributionKey, + type FounderByEnvironmentId, + type MachineOwnerConfig, +} from "./threadAttribution.ts"; +import { + matchesOlderSettledGroup, + matchesThreadListFilter, + threadListFilterCoverage, + type ThreadListFilter, +} from "./threadListFilter.ts"; + +const DAY = 24 * 60 * 60 * 1000; +const CUT_MS = 50 * DAY; +const project = ProjectId.make("project"); +const kameron = { kind: "human", id: "kameron" } as const; +const jeff = { kind: "human", id: "jeff" } as const; +const bot = { kind: "agent", id: "lead" } as const; + +// Kameron's ASUS and Jeff's Air report their owners; a machine whose owner was cleared reports +// null; a 0.1.9 laptop reports nothing and sends every thread. +const asus = EnvironmentId.make("asus"); +const air = EnvironmentId.make("air"); +const cleared = EnvironmentId.make("cleared"); +const legacyLaptop = EnvironmentId.make("laptop-0-1-9"); + +interface Row { + readonly environmentId: EnvironmentId; + readonly id: ThreadId; + readonly lineage: OrchestrationV2ThreadShell["lineage"]; + readonly source: Pick< + OrchestrationV2ThreadShell, + "id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "createdBy" + > & { readonly inbox?: ThreadInbox; readonly pinnedAt: DateTime.Utc | null }; +} + +const row = ( + environmentId: EnvironmentId, + id: string, + input: { + readonly inbox?: ThreadInbox; + readonly settledDay?: number; + readonly parent?: string; + readonly createdBy?: "user" | "agent"; + readonly pinned?: boolean; + } = {}, +): Row => { + const lineage = { + parentThreadId: input.parent === undefined ? null : ThreadId.make(input.parent), + relationshipToParent: null, + rootThreadId: ThreadId.make(input.parent ?? id), + } as const; + return { + environmentId, + id: ThreadId.make(id), + lineage, + source: { + id: ThreadId.make(id), + projectId: project, + lineage, + settledOverride: input.settledDay === undefined ? null : "settled", + settledAt: + input.settledDay === undefined ? null : DateTime.makeUnsafe(input.settledDay * DAY), + createdBy: input.createdBy ?? "user", + pinnedAt: input.pinned === true ? DateTime.makeUnsafe(1) : null, + ...(input.inbox === undefined ? {} : { inbox: input.inbox }), + }, + }; +}; + +// Everything the servers store. Days below 50 settled before each trimming server's cut. +const SERVER_ROWS: ReadonlyArray = [ + row(asus, "k-recent", { inbox: makeThreadInbox({ actor: kameron }), settledDay: 90 }), + row(asus, "history-recent", { settledDay: 91 }), + row(asus, "history-old-1", { settledDay: 10 }), + row(asus, "history-old-2", { settledDay: 11 }), + row(asus, "agent-history-old", { settledDay: 12, createdBy: "agent" }), + row(asus, "jeff-old", { inbox: makeThreadInbox({ actor: jeff }), settledDay: 13 }), + // Agent work with no owner takes its parent's founder, which every client holds (active). + row(asus, "jeff-root", { inbox: makeThreadInbox({ actor: jeff }) }), + row(asus, "agent-old", { + inbox: makeThreadInbox({ actor: bot, routing: inboxRoutingFor("agents") }), + settledDay: 14, + parent: "jeff-root", + }), + // Pinned older threads stay in every snapshot, outside the Settled shelf. + row(asus, "pinned-old", { settledDay: 15, pinned: true }), + row(air, "air-history-old", { settledDay: 20 }), + row(air, "air-history-recent", { settledDay: 92 }), + row(air, "shared-old", { + inbox: makeThreadInbox({ actor: kameron, routing: inboxRoutingFor("shared") }), + settledDay: 21, + }), + row(air, "air-active", {}), + row(cleared, "cleared-history-old", { settledDay: 22 }), + row(cleared, "cleared-history-recent", { settledDay: 93 }), + row(legacyLaptop, "laptop-history", { settledDay: 30 }), + row(legacyLaptop, "laptop-kameron", { + inbox: makeThreadInbox({ actor: kameron }), + settledDay: 31, + }), +]; + +const owner = (ownerFounder: Founder | null): MachineOwnerConfig => ({ + machineOwner: true, + settings: { ownerFounder }, +}); +const SERVER_CONFIGS: ReadonlyMap = new Map([ + [asus, owner("kameron")], + [air, owner("jeff")], + [cleared, owner(null)], + // A 0.1.9 server decodes `ownerFounder` as null without reporting it. + [legacyLaptop, { settings: { ownerFounder: null } }], +]); +const TRIMMING = new Set([asus, air, cleared]); + +const isOlder = (thread: Row) => + TRIMMING.has(thread.environmentId) && isOlderSettledThreadShell(thread.source, CUT_MS); + +/** What each trimming server sends as `olderSettled`, grouped as the server groups it. */ +function serverOlderSettled(withGroups: boolean): ReadonlyMap { + const result = new Map(); + for (const environmentId of TRIMMING) { + const rows = SERVER_ROWS.filter((thread) => thread.environmentId === environmentId); + const senders = resolveThreadSenders( + rows.map((thread) => ({ + key: thread.id, + parentKey: thread.lineage.parentThreadId, + inbox: thread.source.inbox, + createdBy: thread.source.createdBy, + })), + ); + const older = rows.filter(isOlder); + const groups = new Map< + string, + NonNullable[number] + >(); + for (const thread of older) { + const sender = senders.get(thread.id)!; + const group = { + inbox: primaryInbox(thread.source.inbox), + agentCreated: thread.source.createdBy === "agent", + founder: sender.founder, + byAgent: sender.byAgent, + }; + const key = JSON.stringify(group); + groups.set(key, { ...group, count: (groups.get(key)?.count ?? 0) + 1 }); + } + const counts: ReadonlyArray = + older.length === 0 + ? [] + : [ + { + projectId: project, + count: older.length, + ...(withGroups ? { groups: [...groups.values()] } : {}), + }, + ]; + result.set(environmentId, { + before: DateTime.formatIso(DateTime.makeUnsafe(CUT_MS)), + beforeMs: CUT_MS, + ...olderSettledCountsOf(counts), + }); + } + return result; +} + +/** One app's Settled shelf and list over the rows it holds, as the sidebar computes them. */ +function sidebar(input: { + readonly held: ReadonlyArray; + readonly legacy: FounderByEnvironmentId; + readonly olderSettled: ReadonlyMap; + readonly filter: ThreadListFilter; +}) { + const owners = resolveMachineOwners(SERVER_CONFIGS, input.legacy); + const attributions = resolveThreadAttributions(input.held, owners); + const attributionOf = (thread: Row): ThreadAttribution => + attributions.get(threadAttributionKey(thread)) ?? "unknown"; + const listed = input.held.filter((thread) => + matchesThreadListFilter(thread, input.filter, attributionOf(thread)), + ); + const settled = listed.filter( + (thread) => thread.source.settledOverride === "settled" && thread.source.pinnedAt === null, + ); + const total = settledShelfTotal({ + olderSettled: (environmentId) => input.olderSettled.get(environmentId) ?? null, + scopes: [...SERVER_CONFIGS.keys()].map((environmentId) => ({ + environmentId, + projectIds: null, + })), + threads: listed, + settledCount: settled.length, + filter: { + matches: (environmentId, group) => + matchesOlderSettledGroup(environmentId, group, input.filter, owners), + coverage: (environmentId) => threadListFilterCoverage(environmentId, input.filter), + }, + }); + return { + attributions: new Map( + input.held.map((thread) => [threadAttributionKey(thread), attributionOf(thread)]), + ), + listed: new Set(listed.map(threadAttributionKey)), + total, + }; +} + +// Every snapshot row: newer than the cut, pinned, or from a server that trims nothing. +const SNAPSHOT_ROWS = SERVER_ROWS.filter( + (thread) => !isOlder(thread) || thread.source.pinnedAt !== null, +); +// One app saw only the snapshots; the other also loaded every older page, in another order. +const CLIENT_A = { + held: SNAPSHOT_ROWS, + // Wrong for every machine that reports an owner, and must not matter there. + legacy: { [asus]: "jeff", [air]: "kameron", [cleared]: "kameron", [legacyLaptop]: "kameron" }, +} as const; +const CLIENT_B = { + held: [...SERVER_ROWS].reverse(), + legacy: { [legacyLaptop]: "kameron" }, +} as const; + +const FILTERS: ReadonlyArray = [ + ["All", { inboxes: InboxChoice.literals, hiddenEnvironmentIds: [], hiddenAttributions: [] }], + [ + "Kameron + Agents", + { inboxes: ["kameron", "agents"], hiddenEnvironmentIds: [], hiddenAttributions: [] }, + ], + [ + "Jeff + Agents", + { inboxes: ["jeff", "agents"], hiddenEnvironmentIds: [], hiddenAttributions: [] }, + ], + ["Shared", { inboxes: ["shared"], hiddenEnvironmentIds: [], hiddenAttributions: [] }], + [ + "From Kameron only", + { + inboxes: InboxChoice.literals, + hiddenEnvironmentIds: [], + hiddenAttributions: ["jeff", "jeff-agents", "unknown"], + }, + ], + [ + "From Jeff only", + { + inboxes: InboxChoice.literals, + hiddenEnvironmentIds: [], + hiddenAttributions: ["kameron", "kameron-agents", "unknown"], + }, + ], + [ + "Agents, Air hidden", + { inboxes: ["agents"], hiddenEnvironmentIds: [air], hiddenAttributions: [] }, + ], +]; + +describe("one inbox across apps", () => { + const olderSettled = serverOlderSettled(true); + // An app holding every thread with no cut is the ground truth. + const truthFor = (filter: ThreadListFilter) => + sidebar({ held: SERVER_ROWS, legacy: CLIENT_B.legacy, olderSettled: new Map(), filter }); + + it.each(FILTERS)("%s: two apps list and count the same threads", (_name, filter) => { + const a = sidebar({ ...CLIENT_A, olderSettled, filter }); + const b = sidebar({ ...CLIENT_B, olderSettled, filter }); + const truth = truthFor(filter); + + expect(a.total).toEqual(b.total); + expect(a.total).toEqual({ count: truth.total.count, exact: true }); + // Rows both apps hold are listed and credited identically, and as the truth lists them. + for (const thread of CLIENT_A.held) { + const key = threadAttributionKey(thread); + expect(a.listed.has(key)).toBe(b.listed.has(key)); + expect(a.listed.has(key)).toBe(truth.listed.has(key)); + expect(a.attributions.get(key)).toBe(b.attributions.get(key)); + } + }); + + it("credits history to the owner each machine reports, not the app's own map", () => { + const filter = FILTERS[0]![1]; + const a = sidebar({ ...CLIENT_A, olderSettled, filter }).attributions; + expect(a.get(`${asus}:history-recent`)).toBe("kameron"); + expect(a.get(`${air}:air-history-recent`)).toBe("jeff"); + // A cleared owner is reported as null, so the app's stale entry is ignored. + expect(a.get(`${cleared}:cleared-history-recent`)).toBe("unknown"); + // Only a server too old to report an owner uses the app's map. + expect(a.get(`${legacyLaptop}:laptop-history`)).toBe("kameron"); + expect( + sidebar({ ...CLIENT_A, legacy: {}, olderSettled, filter }).attributions.get( + `${legacyLaptop}:laptop-history`, + ), + ).toBe("unknown"); + }); + + it("shows a lower bound, the same on every app, when a server sends totals only", () => { + const totalsOnly = serverOlderSettled(false); + const kameronInbox = FILTERS[1]![1]; + const a = sidebar({ ...CLIENT_A, olderSettled: totalsOnly, filter: kameronInbox }); + const b = sidebar({ ...CLIENT_B, olderSettled: totalsOnly, filter: kameronInbox }); + expect(a.total).toEqual(b.total); + expect(a.total.exact).toBe(false); + expect(a.total.count).toBeLessThanOrEqual(truthFor(kameronInbox).total.count); + // Unfiltered, the totals are the whole answer. + const all = FILTERS[0]![1]; + expect(sidebar({ ...CLIENT_A, olderSettled: totalsOnly, filter: all }).total).toEqual({ + count: truthFor(all).total.count, + exact: true, + }); + }); +}); diff --git a/packages/client-runtime/src/state/olderSettledThreads.test.ts b/packages/client-runtime/src/state/olderSettledThreads.test.ts index 72e23c44d3f9c..3e6248173a4ca 100644 --- a/packages/client-runtime/src/state/olderSettledThreads.test.ts +++ b/packages/client-runtime/src/state/olderSettledThreads.test.ts @@ -1,5 +1,6 @@ import { EnvironmentId, + makeThreadInbox, ORCHESTRATION_V2_WS_METHODS, OrchestrationV2GetShellSnapshotError, ProjectId, @@ -52,6 +53,7 @@ const older: OlderSettled = { before, beforeMs: 50 * DAY, counts: new Map([[project, 3]]), + groups: null, }; const row = ( @@ -97,6 +99,13 @@ describe("olderSettledCountKey", () => { expect(key([{ ...base[0]!, projectId: ProjectId.make("project-b") }, base[1]!])).not.toBe( start, ); + // An older row routed to another inbox, which moves it between the counts' groups. + expect( + key([ + { ...base[0]!, inbox: makeThreadInbox({ actor: { kind: "human", id: "jeff" } }) }, + base[1]!, + ]), + ).not.toBe(start); }); it("stays put for new threads and subagents", () => { diff --git a/packages/client-runtime/src/state/olderSettledThreads.ts b/packages/client-runtime/src/state/olderSettledThreads.ts index a5b79beafe533..ded09110c02da 100644 --- a/packages/client-runtime/src/state/olderSettledThreads.ts +++ b/packages/client-runtime/src/state/olderSettledThreads.ts @@ -3,7 +3,9 @@ import { isOlderSettledThreadShell, ORCHESTRATION_V2_WS_METHODS, type EnvironmentId, + type OrchestrationV2OlderSettledCount, type OrchestrationV2OlderSettledCursor, + type OrchestrationV2OlderSettledGroup, type OrchestrationV2OlderSettledThreadsPage, type OrchestrationV2SearchThreadsInput, type OrchestrationV2ThreadFamily, @@ -32,6 +34,8 @@ export interface OlderSettled { readonly before: string; readonly beforeMs: number; readonly counts: ReadonlyMap; + /** The counts by what an inbox filter reads; null from servers that send totals only. */ + readonly groups: ReadonlyMap> | null; } /** The scope's projects in one environment, or null for all of them. */ @@ -78,10 +82,22 @@ export function sortOlderSettledScopes( .sort((left, right) => left.environmentId.localeCompare(right.environmentId)); } +/** An environment's counts as `OlderSettled` holds them. */ +export function olderSettledCountsOf( + counts: ReadonlyArray, +): Pick { + return { + counts: new Map(counts.map(({ projectId, count }) => [projectId, count] as const)), + groups: counts.every((entry) => entry.groups !== undefined) + ? new Map(counts.map(({ projectId, groups }) => [projectId, groups ?? []] as const)) + : null, + }; +} + /** * Changes whenever the older count can: any removal (by largest sequence, so a repeat counts), an - * older row that arrives, changes or moves project, or an old row arriving outside the set - * (unsettled). Threads created after the cut never enter it, as threads settle after creation. + * older row that arrives, changes project or inbox routing, or an old row arriving outside the + * set (unsettled). Threads created after the cut never enter it, as threads settle after creation. * Archived-thread events arrive as removals too, each costing one count read. */ export function olderSettledCountKey( @@ -96,7 +112,8 @@ export function olderSettledCountKey( if (!isListedThreadLineage(thread.lineage)) continue; const older = isOlderSettledThreadShell(thread, beforeMs); if (older || DateTime.toEpochMillis(thread.createdAt) < beforeMs) { - rows.push(`${thread.id}:${thread.projectId}:${older ? 1 : 0}`); + const routing = older ? `:${JSON.stringify(thread.inbox ?? null)}` : ""; + rows.push(`${thread.id}:${thread.projectId}:${older ? 1 : 0}${routing}`); } } return `${lastRemoval}|${rows.sort().join(",")}`; @@ -167,14 +184,56 @@ export function unloadedOlderSettledCount(input: { readonly source: OlderSettledRow; }>; }): number { + return settledShelfTotal({ ...input, settledCount: 0, filter: null }).count; +} + +/** How the thread list filter applies to threads a server counted but did not send. */ +export interface OlderSettledFilter { + readonly matches: ( + environmentId: EnvironmentId, + group: OrchestrationV2OlderSettledGroup, + ) => boolean; + /** For a server that sends totals only: whether the filter shows all, none or some of it. */ + readonly coverage: (environmentId: EnvironmentId) => "all" | "none" | "some"; +} + +/** + * The Settled shelf's total: its rows plus the server's older counts under the thread list + * filter, minus the older rows the client holds. An older row held in the shelf counts once on + * each side, so every client of the same servers gets the same total whichever pages it loaded. + * Not exact when a server sends totals only and the filter shows part of it; show a lower bound. + */ +export function settledShelfTotal(input: { + readonly olderSettled: (environmentId: EnvironmentId) => OlderSettled | null; + readonly scopes: ReadonlyArray; + /** Every row the client holds in scope that the filter shows, in any section. */ + readonly threads: ReadonlyArray<{ + readonly environmentId: EnvironmentId; + readonly source: OlderSettledRow; + }>; + /** Rows in the Settled section. */ + readonly settledCount: number; + /** Null counts every thread, as a list with no filter. */ + readonly filter: OlderSettledFilter | null; +}): { readonly count: number; readonly exact: boolean } { let unloaded = 0; + let exact = true; for (const scope of input.scopes) { const older = input.olderSettled(scope.environmentId); if (older === null) continue; - if (scope.projectIds === null) { - for (const count of older.counts.values()) unloaded += count; - } else { - for (const projectId of scope.projectIds) unloaded += older.counts.get(projectId) ?? 0; + const projectIds = scope.projectIds ?? [...older.counts.keys()]; + const coverage = input.filter?.coverage(scope.environmentId) ?? "all"; + for (const projectId of projectIds) { + const groups = older.groups?.get(projectId); + if (input.filter !== null && groups !== undefined) { + for (const group of groups) { + if (input.filter.matches(scope.environmentId, group)) unloaded += group.count; + } + } else if (coverage === "all") { + unloaded += older.counts.get(projectId) ?? 0; + } else if (coverage === "some" && (older.counts.get(projectId) ?? 0) > 0) { + exact = false; + } } for (const thread of input.threads) { if ( @@ -186,7 +245,7 @@ export function unloadedOlderSettledCount(input: { } } } - return Math.max(0, unloaded); + return { count: Math.max(0, input.settledCount + unloaded), exact }; } /** @@ -268,10 +327,6 @@ export function createOlderSettledThreadAtoms( }), }); - const countsOf = (source: { - readonly counts: OrchestrationV2OlderSettledThreadsPage["counts"]; - }) => new Map(source.counts.map(({ projectId, count }) => [projectId, count] as const)); - const olderSettledAtom = Atom.family((environmentId: EnvironmentId) => { let generation = -1; // The key of the first state seen this generation, when that state is the snapshot itself. @@ -290,7 +345,7 @@ export function createOlderSettledThreadAtoms( fromSnapshot = { before: cut.before, beforeMs: DateTime.toEpochMillis(DateTime.makeUnsafe(cut.before)), - counts: countsOf(cut), + ...olderSettledCountsOf(cut.counts), }; } // Most shell updates move only the cursor; the key needs new rows or removals. @@ -315,7 +370,7 @@ export function createOlderSettledThreadAtoms( ); if (read !== null && read !== lastReadPage) { lastReadPage = read; - lastRead = { ...fromSnapshot, counts: countsOf(read) }; + lastRead = { ...fromSnapshot, ...olderSettledCountsOf(read.counts) }; } return lastRead ?? fromSnapshot; }).pipe(Atom.withLabel(`environment-older-settled:${environmentId}`)); diff --git a/packages/client-runtime/src/state/threadAttribution.ts b/packages/client-runtime/src/state/threadAttribution.ts index 8cf8740e41261..a01b2d12df0bb 100644 --- a/packages/client-runtime/src/state/threadAttribution.ts +++ b/packages/client-runtime/src/state/threadAttribution.ts @@ -1,4 +1,11 @@ -import type { EnvironmentId, Founder, ThreadId, ThreadInbox } from "@t3tools/contracts"; +import { + resolveThreadSenders, + type EnvironmentId, + type Founder, + type ThreadId, + type ThreadInbox, + type ThreadSender, +} from "@t3tools/contracts"; import { ThreadAttribution } from "@t3tools/contracts/settings"; export const THREAD_ATTRIBUTIONS = ThreadAttribution.literals; @@ -21,67 +28,78 @@ export interface AttributableThread { }; } -/** Machine owners, chosen in Settings; nothing is assumed for an unmapped machine. */ -type FounderByEnvironmentId = Readonly>>; +/** Machine owners; nothing is assumed for an unmapped machine. */ +export type FounderByEnvironmentId = Readonly>>; + +/** The part of a server config that names its machine's owner. */ +export interface MachineOwnerConfig { + /** The server reports its owner, so its value wins even when null. */ + readonly machineOwner?: boolean | undefined; + readonly settings: { readonly ownerFounder: Founder | null }; +} + +/** + * Every machine's owner: the one its server reports, so every client agrees, else this client's + * own map for servers too old to report one. + */ +export function resolveMachineOwners( + serverConfigs: ReadonlyMap, + legacy: FounderByEnvironmentId | undefined, +): FounderByEnvironmentId { + const owners: Partial> = { ...legacy }; + for (const [environmentId, config] of serverConfigs) { + if (config.machineOwner !== true) continue; + if (config.settings.ownerFounder === null) delete owners[environmentId]; + else owners[environmentId] = config.settings.ownerFounder; + } + return owners; +} export const threadAttributionKey = (thread: { readonly environmentId: EnvironmentId; readonly id: ThreadId; }) => `${thread.environmentId}:${thread.id}`; -const attributionOf = (founder: Founder, byAgent: boolean): ThreadAttribution => - byAgent ? `${founder}-agents` : founder; - -const founderOfAttribution = (attribution: ThreadAttribution): Founder | null => - attribution === "unknown" ? null : attribution.startsWith("jeff") ? "jeff" : "kameron"; - -const humanFounder = (actor: ThreadInbox["origin"] | undefined): Founder | null => - actor?.kind === "human" ? actor.id : null; +/** A sender's attribution once its machine's owner is known. */ +export function attributionOfSender( + sender: ThreadSender, + owner: Founder | undefined, +): ThreadAttribution { + const founder = sender.founder === "machine" ? (owner ?? null) : sender.founder; + if (founder === null) return "unknown"; + return sender.byAgent ? `${founder}-agents` : founder; +} /** - * Resolves every thread's attribution, keyed by `threadAttributionKey`. A human origin or - * requester is that founder; agent work takes its requester's or owner's founder, else its - * parent's; only history without inbox fields falls back to the machine's owner. + * Resolves every thread's attribution, keyed by `threadAttributionKey`, with the rule servers + * count older settled threads by (`resolveThreadSenders`). */ export function resolveThreadAttributions( threads: ReadonlyArray, founderByEnvironmentId: FounderByEnvironmentId, ): ReadonlyMap { - const byKey = new Map(threads.map((thread) => [threadAttributionKey(thread), thread])); + const environmentByKey = new Map(); + const senders = resolveThreadSenders( + threads.map((thread) => { + const key = threadAttributionKey(thread); + environmentByKey.set(key, thread.environmentId); + return { + key, + parentKey: + thread.lineage.parentThreadId === null + ? null + : `${thread.environmentId}:${thread.lineage.parentThreadId}`, + inbox: thread.source.inbox, + createdBy: thread.source.createdBy, + }; + }), + ); const resolved = new Map(); - const resolving = new Set(); - const resolve = (key: string, thread: AttributableThread): ThreadAttribution => { - const known = resolved.get(key); - if (known !== undefined) return known; - // A lineage cycle resolves the repeated thread without its parent. - resolving.add(key); - const inbox = thread.source.inbox; - const byAgent = inbox?.origin?.kind === "agent" || thread.source.createdBy === "agent"; - const parentKey = - thread.lineage.parentThreadId === null - ? null - : `${thread.environmentId}:${thread.lineage.parentThreadId}`; - const parent = - parentKey === null || resolving.has(parentKey) ? undefined : byKey.get(parentKey); - const founder = - humanFounder(inbox?.origin) ?? - humanFounder(inbox?.requestedBy) ?? - (inbox?.origin?.kind === "agent" - ? (inbox.owner ?? - (parent && parentKey ? founderOfAttribution(resolve(parentKey, parent)) : null)) - : null) ?? - // Only history from before inbox routing falls back to the machine: a shared machine - // runs both founders' agents, so crediting routed work to its owner would be a guess. - (inbox ? null : founderByEnvironmentId[thread.environmentId]) ?? - null; - const attribution = - founder === null - ? "unknown" - : attributionOf(founder, byAgent && humanFounder(inbox?.origin) === null); - resolving.delete(key); - resolved.set(key, attribution); - return attribution; - }; - for (const [key, thread] of byKey) resolve(key, thread); + for (const [key, sender] of senders) { + resolved.set( + key, + attributionOfSender(sender, founderByEnvironmentId[environmentByKey.get(key)!]), + ); + } return resolved; } diff --git a/packages/client-runtime/src/state/threadListFilter.ts b/packages/client-runtime/src/state/threadListFilter.ts new file mode 100644 index 0000000000000..c06cea1879acf --- /dev/null +++ b/packages/client-runtime/src/state/threadListFilter.ts @@ -0,0 +1,88 @@ +import { + effectiveInboxFilter, + InboxChoice, + matchesPrimaryInbox, + primaryInbox, + type EnvironmentId, + type OrchestrationV2OlderSettledGroup, + type ThreadInbox, +} from "@t3tools/contracts"; +import type { ClientSettings, ThreadAttribution } from "@t3tools/contracts/settings"; + +import { attributionOfSender, type FounderByEnvironmentId } from "./threadAttribution.ts"; + +/** What the thread list shows: chosen inboxes, minus hidden environments and senders. */ +export interface ThreadListFilter { + readonly inboxes: ReadonlyArray; + readonly hiddenEnvironmentIds: ReadonlyArray; + readonly hiddenAttributions: ReadonlyArray; +} + +interface FilterableThread { + readonly environmentId: EnvironmentId; + readonly source: { + readonly inbox?: ThreadInbox | null | undefined; + readonly createdBy?: string | undefined; + }; +} + +export const threadListFilterOf = ( + settings: Pick< + ClientSettings, + | "founderInboxFilter" + | "founderInboxOwner" + | "sidebarHiddenEnvironmentIds" + | "sidebarHiddenThreadAttributions" + >, +): ThreadListFilter => ({ + inboxes: effectiveInboxFilter(settings), + hiddenEnvironmentIds: settings.sidebarHiddenEnvironmentIds ?? [], + hiddenAttributions: settings.sidebarHiddenThreadAttributions ?? [], +}); + +/** Inbox membership from what a thread or an older settled group carries. */ +export const matchesListedInboxes = ( + inbox: InboxChoice | null, + agentCreated: boolean, + inboxes: ReadonlyArray, +) => + matchesPrimaryInbox(inbox, inboxes) || + // Agent-created threads from before inbox routing carry no Agents routing. + (inboxes.includes("agents") && agentCreated && inbox === null); + +/** Without an attribution (notifications), the From section does not apply. */ +export const matchesThreadListFilter = ( + thread: FilterableThread, + filter: ThreadListFilter, + attribution?: ThreadAttribution, +) => + !filter.hiddenEnvironmentIds.includes(thread.environmentId) && + (attribution === undefined || !filter.hiddenAttributions.includes(attribution)) && + matchesListedInboxes( + primaryInbox(thread.source.inbox), + thread.source.createdBy === "agent", + filter.inboxes, + ); + +/** `matchesThreadListFilter` for older settled threads a server counted but did not send. */ +export const matchesOlderSettledGroup = ( + environmentId: EnvironmentId, + group: OrchestrationV2OlderSettledGroup, + filter: ThreadListFilter, + owners: FounderByEnvironmentId, +) => + !filter.hiddenEnvironmentIds.includes(environmentId) && + !filter.hiddenAttributions.includes(attributionOfSender(group, owners[environmentId])) && + matchesListedInboxes(group.inbox, group.agentCreated, filter.inboxes); + +/** How much of an environment the filter shows, for counts that carry no breakdown. */ +export const threadListFilterCoverage = ( + environmentId: EnvironmentId, + filter: ThreadListFilter, +): "all" | "none" | "some" => + filter.hiddenEnvironmentIds.includes(environmentId) + ? "none" + : filter.hiddenAttributions.length > 0 || + !InboxChoice.literals.every((choice) => filter.inboxes.includes(choice)) + ? "some" + : "all"; diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts index 3c4238e09d3f0..fb32ef5f8db37 100644 --- a/packages/contracts/src/orchestrationV2.ts +++ b/packages/contracts/src/orchestrationV2.ts @@ -3,7 +3,13 @@ import { withRecordIdTable } from "./orchestrationV2IdTable.ts"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; -import { ThreadInbox, ThreadInboxRoutingPatch, InboxActor } from "./threadInbox.ts"; +import { + Founder, + InboxActor, + InboxChoice, + ThreadInbox, + ThreadInboxRoutingPatch, +} from "./threadInbox.ts"; import * as SchemaAST from "effect/SchemaAST"; import * as SchemaGetter from "effect/SchemaGetter"; import * as SchemaTransformation from "effect/SchemaTransformation"; @@ -2340,9 +2346,25 @@ export function isOlderSettledThreadShell( ); } +/** + * Older settled threads that share what an inbox filter reads: their `primaryInbox`, whether an + * agent created them, and their `ThreadSender`. Clients apply their filter to these, so a filtered + * Settled count includes threads they have not loaded. + */ +export const OrchestrationV2OlderSettledGroup = Schema.Struct({ + inbox: Schema.NullOr(InboxChoice), + agentCreated: Schema.Boolean, + founder: Schema.NullOr(Schema.Union([Founder, Schema.Literal("machine")])), + byAgent: Schema.Boolean, + count: NonNegativeInt, +}); +export type OrchestrationV2OlderSettledGroup = typeof OrchestrationV2OlderSettledGroup.Type; + export const OrchestrationV2OlderSettledCount = Schema.Struct({ projectId: ProjectId, count: NonNegativeInt, + /** `count` broken down; older servers omit it. */ + groups: Schema.optionalKey(Schema.Array(OrchestrationV2OlderSettledGroup)), }); export type OrchestrationV2OlderSettledCount = typeof OrchestrationV2OlderSettledCount.Type; diff --git a/packages/contracts/src/server.ts b/packages/contracts/src/server.ts index f2e02c6b0a95e..1e909f8444419 100644 --- a/packages/contracts/src/server.ts +++ b/packages/contracts/src/server.ts @@ -718,6 +718,11 @@ export const ServerConfig = Schema.Struct({ * `getOlderSettledThreads` serves them. Only servers that also advertise `shellThreadFamilies`. */ shellOlderSettled: Schema.optionalKey(Schema.Boolean), + /** + * `settings.ownerFounder` is this machine's owner, even when null. Older servers decode it as + * null without meaning it, so clients use their own machine-owner map for them. + */ + machineOwner: Schema.optionalKey(Schema.Boolean), shellRevealInFileManager: Schema.optionalKey(Schema.Boolean), shellRevealInFileManagerKind: Schema.optionalKey(FileManagerRevealKind), threadResumeCompletionMarker: Schema.optionalKey(Schema.Boolean), diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index 5e6917eb28195..bfeb2679ca2f1 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -419,6 +419,7 @@ export const ClientSettingsSchema = Schema.Struct({ founderInboxFilter: Schema.optionalKey(Schema.Array(InboxChoice)), sidebarHiddenEnvironmentIds: Schema.optionalKey(Schema.Array(EnvironmentId)), sidebarHiddenThreadAttributions: Schema.optionalKey(Schema.Array(ThreadAttribution)), + // Machine owners for servers too old to report `ownerFounder`; newer servers' own value wins. founderByEnvironmentId: Schema.optionalKey(Schema.Record(EnvironmentId, Founder)), notificationMode: NotificationMode.pipe( Schema.withDecodingDefault(Effect.succeed("off" as const)), @@ -1409,6 +1410,11 @@ export const ServerSettings = Schema.Struct({ requireConnectDeviceApproval: Schema.Boolean.pipe( Schema.withDecodingDefault(Effect.succeed(false)), ), + /** + * The founder this machine belongs to, which every client credits its threads without sender + * details to. Null credits them to no one. A label only; it grants nothing. + */ + ownerFounder: Schema.NullOr(Founder).pipe(Schema.withDecodingDefault(Effect.succeed(null))), enableAgentBrowserAccess: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))), cloudSpeechCredentials: CloudSpeechCredentials.pipe( @@ -1878,6 +1884,7 @@ export const ServerSettingsPatch = Schema.Struct({ continueThreadsAfterServerUpdate: Schema.optionalKey(Schema.Boolean), keepAwakeWhileAgentsWork: Schema.optionalKey(Schema.Boolean), requireConnectDeviceApproval: Schema.optionalKey(Schema.Boolean), + ownerFounder: Schema.optionalKey(Schema.NullOr(Founder)), enableAgentBrowserAccess: Schema.optionalKey(Schema.Boolean), cloudSpeechCredentials: Schema.optionalKey( Schema.Struct({ diff --git a/packages/contracts/src/threadInbox.ts b/packages/contracts/src/threadInbox.ts index 916023ae65002..6c127a0c105cf 100644 --- a/packages/contracts/src/threadInbox.ts +++ b/packages/contracts/src/threadInbox.ts @@ -160,14 +160,16 @@ export function primaryInbox(inbox: ThreadInbox | null | undefined): InboxChoice return matchesInboxView(inbox, "agents") ? "agents" : null; } +/** `matchesInboxFilter` for a thread already reduced to its `primaryInbox`. */ +export const matchesPrimaryInbox = ( + primary: InboxChoice | null, + filter: ReadonlyArray, +) => isAllInboxes(filter) || (primary !== null && filter.includes(primary)); + export const matchesInboxFilter = ( inbox: ThreadInbox | null | undefined, filter: ReadonlyArray, -) => { - if (isAllInboxes(filter)) return true; - const primary = primaryInbox(inbox); - return primary !== null && filter.includes(primary); -}; +) => matchesPrimaryInbox(primaryInbox(inbox), filter); /** New work is attributed only to a chosen founder; otherwise it stays unclassified (All). */ export const inboxOriginFor = (settings: { @@ -190,3 +192,67 @@ export function matchesInboxView(inbox: ThreadInbox | null | undefined, view: In const lane = founderInboxLane(inbox, view); return lane === "action" || lane === "FYI"; } + +/** + * Who a thread came from, before its machine's owner is applied: `"machine"` stands for that + * owner, which clients look up. `byAgent` marks an agent's work for that founder. + */ +export interface ThreadSender { + readonly founder: Founder | "machine" | null; + readonly byAgent: boolean; +} + +export interface ThreadSenderInput { + readonly key: string; + /** The parent's key; a parent always shares the thread's environment. */ + readonly parentKey: string | null; + readonly inbox?: ThreadInbox | null | undefined; + readonly createdBy?: string | undefined; +} + +const humanFounder = (actor: InboxActor | null | undefined): Founder | null => + actor?.kind === "human" ? actor.id : null; + +/** + * Every thread's sender, keyed by `key`. A human origin or requester is that founder; agent work + * takes its owner, else its parent's founder; only history without inbox fields falls back to the + * machine's owner. Servers count older settled threads by sender and clients label rows by it, so + * the two agree. + */ +export function resolveThreadSenders( + threads: ReadonlyArray, +): ReadonlyMap { + const byKey = new Map(threads.map((thread) => [thread.key, thread])); + const resolved = new Map(); + const resolving = new Set(); + const resolve = (thread: ThreadSenderInput): ThreadSender => { + const known = resolved.get(thread.key); + if (known !== undefined) return known; + // A lineage cycle resolves the repeated thread without its parent. + resolving.add(thread.key); + const inbox = thread.inbox; + const parent = + thread.parentKey === null || resolving.has(thread.parentKey) + ? undefined + : byKey.get(thread.parentKey); + const human = humanFounder(inbox?.origin); + const founder = + human ?? + humanFounder(inbox?.requestedBy) ?? + (inbox?.origin?.kind === "agent" + ? (inbox.owner ?? (parent ? resolve(parent).founder : null)) + : null) ?? + // Only history from before inbox routing falls back to the machine: a shared machine + // runs both founders' agents, so crediting routed work to its owner would be a guess. + (inbox ? null : "machine"); + const sender: ThreadSender = { + founder, + byAgent: human === null && (inbox?.origin?.kind === "agent" || thread.createdBy === "agent"), + }; + resolving.delete(thread.key); + resolved.set(thread.key, sender); + return sender; + }; + for (const thread of byKey.values()) resolve(thread); + return resolved; +} From d6cfb2feea3b329a2fea5f1ab7491fa1b8e52ed8 Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:13:10 +0000 Subject: [PATCH 2/3] fix: one inbox resolves senders on the server and keeps agents out of machine owners Agents can no longer read or set the machine owner: `ownerFounder` is gone from `t3_environment_read` and `t3_environment_preferences_update`, so Settings is the only writer and no model-facing text changes. The server now resolves the sender of agent work without an owner from its parent chain and stamps it on the shell as `parentFounder`. Clients use the stamp, so a fork of an idle subagent is credited the same on an app that never holds the subagent. Older settled counts group by the stamp too, so the `limit: 0` recount reads only the requested projects' older rows, and parent chains are read only for ownerless agent rows. Snapshots group counts once, only when they omit older settled threads, and the shell index decodes inbox fields only for settled listed rows. The recount key also includes the routing of held parents a sender comes from. A lower-bound "N+" Settled header never reads below the rows on screen. Settings offers "Set on machine" when an upgraded machine has no owner but this device's old map chose one. Model: claude-opus-5-5 Task: fix Agent: claude-code Co-Authored-By: Claude Opus 5.5 --- .../src/mcp/toolkits/environment/handlers.ts | 2 - .../src/mcp/toolkits/environment/tools.ts | 3 - .../orchestration-v2/ProjectionStore.test.ts | 87 ++++++++-- .../src/orchestration-v2/ProjectionStore.ts | 151 ++++++++++++++---- .../src/orchestration-v2/ShellStream.test.ts | 15 +- .../src/orchestration-v2/ShellStream.ts | 23 +-- .../orchestration-v2/olderSettledCounts.ts | 17 +- .../settings/NewThreadInboxSettings.tsx | 24 ++- docs/internals/connection-runtime.md | 10 +- docs/user/thread-sidebar.md | 4 +- .../src/state/inboxConvergence.test.ts | 117 +++++++++++--- .../src/state/olderSettledThreads.test.ts | 72 +++++++++ .../src/state/olderSettledThreads.ts | 37 ++++- .../src/state/threadAttribution.ts | 4 + packages/contracts/src/orchestrationV2.ts | 9 +- packages/contracts/src/threadInbox.ts | 39 +++-- 16 files changed, 493 insertions(+), 121 deletions(-) diff --git a/apps/server/src/mcp/toolkits/environment/handlers.ts b/apps/server/src/mcp/toolkits/environment/handlers.ts index 243c5c8a14fb2..fb1716885d6e8 100644 --- a/apps/server/src/mcp/toolkits/environment/handlers.ts +++ b/apps/server/src/mcp/toolkits/environment/handlers.ts @@ -15,14 +15,12 @@ export function preferences(settings: ServerSettings) { enableProviderUpdateChecks, backgroundActivity, sourceControlWritingStyle, - ownerFounder, } = settings; const characters = Array.from(sourceControlWritingStyle.customInstructions); return { defaultThreadEnvMode, newWorktreesStartFromOrigin, enableProviderUpdateChecks, - ownerFounder, backgroundActivity: { profile: backgroundActivity.profile }, sourceControlWritingStyle: { ...sourceControlWritingStyle, diff --git a/apps/server/src/mcp/toolkits/environment/tools.ts b/apps/server/src/mcp/toolkits/environment/tools.ts index 4f9e152b66d03..9e10380295de2 100644 --- a/apps/server/src/mcp/toolkits/environment/tools.ts +++ b/apps/server/src/mcp/toolkits/environment/tools.ts @@ -19,8 +19,6 @@ const PreferenceFields = { defaultThreadEnvMode: ServerSettings.fields.defaultThreadEnvMode, newWorktreesStartFromOrigin: ServerSettings.fields.newWorktreesStartFromOrigin, enableProviderUpdateChecks: ServerSettings.fields.enableProviderUpdateChecks, - /** The founder this machine belongs to; threads without sender details count as theirs. */ - ownerFounder: ServerSettings.fields.ownerFounder, backgroundActivity: Schema.Struct({ profile: BackgroundActivityProfileSelection }), sourceControlWritingStyle: Schema.Struct({ mode: Schema.String, @@ -64,7 +62,6 @@ const EnvironmentPreferencesTool = Tool.make("t3_environment_preferences_update" defaultThreadEnvMode: ServerSettingsPatch.fields.defaultThreadEnvMode, newWorktreesStartFromOrigin: ServerSettingsPatch.fields.newWorktreesStartFromOrigin, enableProviderUpdateChecks: ServerSettingsPatch.fields.enableProviderUpdateChecks, - ownerFounder: ServerSettingsPatch.fields.ownerFounder, backgroundActivity: Schema.optionalKey(Schema.Struct({ profile: BackgroundActivityProfile })), sourceControlWritingStyle: ServerSettingsPatch.fields.sourceControlWritingStyle, }), diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index b1d8d9ec81459..7d02761ed644e 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -163,6 +163,7 @@ const assertShellIndexCoversShells = Effect.fnUntraced(function* () { | "snoozedUntil" | "inbox" | "createdBy" + | "parentFounder" >, ) => ({ id: row.id, @@ -173,8 +174,16 @@ const assertShellIndexCoversShells = Effect.fnUntraced(function* () { pinnedAt: iso(row.pinnedAt), pinOrderKey: row.pinOrderKey ?? null, snoozedUntil: iso(row.snoozedUntil), - inbox: row.inbox ?? null, - createdBy: row.createdBy ?? null, + // The index carries what older settled counts group by only on settled listed rows. + ...(isListedThreadLineage(row.lineage) && + row.settledOverride === "settled" && + row.settledAt !== null + ? { + inbox: row.inbox ?? null, + createdBy: row.createdBy ?? null, + parentFounder: row.parentFounder, + } + : {}), }); assert.deepEqual(index.map(trimmed), shells.map(trimmed)); shells.forEach((shell, position) => { @@ -3109,13 +3118,13 @@ it.effect("SQL list older settled threads below a cursor", () => ...(input.inbox === undefined ? {} : { inbox: input.inbox }), lineage: { parentThreadId: - input.subagent === true - ? id("b") - : input.parent === undefined - ? null - : id(input.parent), + input.parent !== undefined + ? id(input.parent) + : input.subagent === true + ? id("b") + : null, relationshipToParent: input.subagent === true ? "subagent" : null, - rootThreadId: input.subagent === true ? id("b") : id(input.parent ?? input.name), + rootThreadId: id(input.parent ?? (input.subagent === true ? "b" : input.name)), }, forkedFrom: null, createdAt: now, @@ -3177,7 +3186,11 @@ it.effect("SQL list older settled threads below a cursor", () => ], }); - // Recounts group like snapshots: by routing, and by sender through an active parent. + // Recounts group like snapshots: by routing, and by sender through the parent chain. + const agentWork = makeThreadInbox({ + actor: { kind: "agent", id: "lead" }, + routing: inboxRoutingFor("agents"), + }); yield* createThread({ name: "jeff-root", settledDay: null, @@ -3189,10 +3202,23 @@ it.effect("SQL list older settled threads below a cursor", () => settledDay: 6, project: "project:c", parent: "jeff-root", - inbox: makeThreadInbox({ - actor: { kind: "agent", id: "lead" }, - routing: inboxRoutingFor("agents"), - }), + inbox: agentWork, + }); + // A fork of an idle subagent: opted-in clients never hold the subagent it takes Jeff from. + yield* createThread({ + name: "subagent", + settledDay: null, + project: "project:c", + subagent: true, + parent: "jeff-root", + inbox: agentWork, + }); + yield* createThread({ + name: "subagent-fork", + settledDay: 5, + project: "project:c", + parent: "subagent", + inbox: agentWork, }); yield* createThread({ name: "kameron", @@ -3202,15 +3228,42 @@ it.effect("SQL list older settled threads below a cursor", () => }); // The index carries the routing the snapshot's counts group by, as the shells do. yield* assertShellIndexCoversShells(); - const routed = yield* list(before, 0, [ProjectId.make("project:c")]); - assert.equal(routed.counts.length, 1); + const shells = new Map( + (yield* store.getShellSnapshot()).threads.map((shell) => [shell.id, shell] as const), + ); + assert.equal(shells.get(id("subagent-fork"))?.parentFounder, "jeff"); + assert.equal(shells.get(id("agent-child"))?.parentFounder, "jeff"); + assert.isFalse(shells.has(id("kameron")) && "parentFounder" in shells.get(id("kameron"))!); + assert.equal((yield* store.getThreadShell(id("subagent-fork")))?.parentFounder, "jeff"); + + const recount = (projectId: string) => + Effect.gen(function* () { + const { statements, tracer } = traceSqlStatements(); + const listing = yield* list(before, 0, [ProjectId.make(projectId)]).pipe( + Effect.withTracer(tracer), + ); + return { listing, statements }; + }); + const routed = yield* recount("project:c"); + assert.equal(routed.listing.counts.length, 1); assert.sameDeepMembers( - [...(routed.counts[0]?.groups ?? [])], + [...(routed.listing.counts[0]?.groups ?? [])], [ - { inbox: "agents", agentCreated: false, founder: "jeff", byAgent: true, count: 1 }, + { inbox: "agents", agentCreated: false, founder: "jeff", byAgent: true, count: 2 }, { inbox: "kameron", agentCreated: false, founder: "kameron", byAgent: false, count: 1 }, ], ); + // The recount reads the requested project's older rows, then one chain read for the agents. + const chainReads = (statements: ReadonlyArray) => + statements.filter((statement) => statement.includes("WITH RECURSIVE chain")); + assert.lengthOf(chainReads(routed.statements), 1); + assert.isTrue( + routed.statements.some( + (statement) => statement.includes("WITH older AS") && statement.includes("project_id IN"), + ), + ); + // Rows with an owner or no inbox never read a parent. + assert.lengthOf(chainReads((yield* recount("project:b")).statements), 0); }).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 2f398ea0df544..4b92afc30a9a4 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -81,6 +81,8 @@ import { ProjectId, ProviderInstanceId, ThreadInbox, + resolveThreadSenders, + type SenderFounder, } from "@t3tools/contracts"; import { createOrchestrationV2TurnItemVisibility } from "@t3tools/shared/orchestrationV2Timeline"; import { @@ -477,9 +479,10 @@ export interface ActiveShellIndexEntry extends Pick< | "pinOrderKey" | "snoozedUntil" > { - /** Read only to count older settled threads by sender. */ + /** Only on settled listed rows, which older settled counts group by sender. */ readonly inbox?: ThreadInbox | undefined; readonly createdBy?: string | undefined; + readonly parentFounder?: SenderFounder | undefined; /** False only when no run, request, work item, or watch can make the shell live. */ readonly mayBeLive: boolean; /** Whether it delegated a task to a linked environment, whose clients nest that task under it. */ @@ -1127,6 +1130,8 @@ type ShellThreadRow = { readonly thread_id: string; readonly payload_json: string; readonly forked_from_run_source_thread_id: string | null; + /** The parent its sender comes from, which the shell's `parentFounder` resolves. */ + readonly sender_parent_thread_id: string | null; readonly latest_run_id: string | null; readonly latest_run_status: string | null; readonly latest_run_requested_at: string | null; @@ -2115,9 +2120,12 @@ function visibleItemCountForShell(input: { function shellFromState(input: { readonly state: ShellThreadState; readonly visibleItemCount: number; + /** Set when the thread's sender comes from its parent chain. */ + readonly parentFounder: SenderFounder | undefined; }): OrchestrationV2ThreadShell { return { createdBy: input.state.thread.createdBy, + ...(input.parentFounder === undefined ? {} : { parentFounder: input.parentFounder }), creationSource: input.state.thread.creationSource, id: input.state.thread.id, projectId: input.state.thread.projectId, @@ -7799,6 +7807,66 @@ export const layer: Layer.Layer = ) )`; }; + // A listed thread (isListedThreadLineage) that is settled, as isOlderSettledThreadShell reads. + const isSettledListedThread = (alias: string) => { + const thread = sql.literal(alias); + return sql`( + json_extract(${thread}.payload_json, '$.lineage.relationshipToParent') IS NOT 'subagent' + AND json_extract(${thread}.payload_json, '$.lineage.sideChat') IS NOT 1 + AND json_extract(${thread}.payload_json, '$.settledOverride') = 'settled' + AND json_extract(${thread}.payload_json, '$.settledAt') IS NOT NULL + )`; + }; + // Mirrors senderNeedsParent: the parent a thread's sender comes from, else NULL. + const senderParentId = (alias: string) => { + const thread = sql.literal(alias); + return sql`CASE + WHEN json_extract(${thread}.payload_json, '$.inbox.origin.kind') = 'agent' + AND json_extract(${thread}.payload_json, '$.inbox.owner') IS NULL + AND json_extract(${thread}.payload_json, '$.inbox.requestedBy.kind') IS NOT 'human' + THEN json_extract(${thread}.payload_json, '$.lineage.parentThreadId') + END`; + }; + /** + * Each parent's sender founder, read up its chain only while a sender comes from its parent. + * Shells stamp it as `parentFounder` and older counts group by it, so neither a client nor a + * count needs the parents themselves. + */ + const readParentFounders = Effect.fn("ProjectionStore.readParentFounders")(function* ( + parentIds: ReadonlyArray, + ) { + const ids = [...new Set(parentIds)]; + if (ids.length === 0) return new Map(); + const rows = yield* sql<{ + readonly thread_id: string; + readonly parent_thread_id: string | null; + readonly inbox_json: string | null; + readonly created_by: string | null; + }>` + WITH RECURSIVE chain(thread_id, parent_thread_id) AS ( + SELECT t.thread_id, ${senderParentId("t")} + FROM orchestration_v2_projection_threads t + WHERE t.thread_id IN ${sql.in(ids)} + UNION + SELECT t.thread_id, ${senderParentId("t")} + FROM chain + JOIN orchestration_v2_projection_threads t ON t.thread_id = chain.parent_thread_id + ) + SELECT chain.thread_id, chain.parent_thread_id, + json_extract(t.payload_json, '$.inbox') AS inbox_json, + json_extract(t.payload_json, '$.createdBy') AS created_by + FROM chain + JOIN orchestration_v2_projection_threads t ON t.thread_id = chain.thread_id + `; + const senders = resolveThreadSenders( + rows.map((row) => ({ + key: row.thread_id, + parentKey: row.parent_thread_id, + ...shellIndexSender(row), + })), + ); + return new Map(ids.map((id) => [id, senders.get(id)?.founder ?? null] as const)); + }); // Mirrors isSilentRunForShell. const isSilentShellRun = (alias: string) => { const run = sql.literal(alias); @@ -7892,6 +7960,7 @@ export const layer: Layer.Layer = THEN json_extract(t.payload_json, '$.forkedFrom.threadId') ELSE NULL END AS forked_from_run_source_thread_id, + ${senderParentId("t")} AS sender_parent_thread_id, presented.run_id AS latest_run_id, presented.status AS latest_run_status, presented.requested_at AS latest_run_requested_at, @@ -8725,6 +8794,21 @@ export const layer: Layer.Layer = pendingSourceIds.push(ThreadId.make(source.forked_from_run_source_thread_id)); } } + const founders = yield* readParentFounders( + targetThreadRows.flatMap((row) => row.sender_parent_thread_id ?? []), + ); + const parentFounders = new Map( + targetThreadRows.flatMap((row) => + row.sender_parent_thread_id === null + ? [] + : [ + [ + ThreadId.make(row.thread_id), + founders.get(row.sender_parent_thread_id) ?? null, + ] as const, + ], + ), + ); const threadRows = [...rowsByThreadId.values()]; const threadIds = [...rowsByThreadId.keys()]; const forkSourceIds = shellForkSourceIds(threadRows); @@ -8763,6 +8847,7 @@ export const layer: Layer.Layer = ]); return { targetThreadIds, + parentFounders, threadRows, runRows, itemCountRows, @@ -8775,6 +8860,7 @@ export const layer: Layer.Layer = const decodeShellSnapshot = Effect.fn("ProjectionStore.decodeShellSnapshot")(function* ({ targetThreadIds, + parentFounders, threadRows, runRows, itemCountRows, @@ -8811,6 +8897,7 @@ export const layer: Layer.Layer = threadId: state.thread.id, statesByThreadId, }), + parentFounder: parentFounders.get(state.thread.id), }), ); @@ -8977,14 +9064,20 @@ export const layer: Layer.Layer = readonly snoozed_until: string | null; readonly inbox_json: string | null; readonly created_by: string | null; + readonly sender_parent_thread_id: string | null; readonly may_be_live: number; readonly has_remote_tasks: number; }>` SELECT t.thread_id, t.project_id, - json_extract(t.payload_json, '$.inbox') AS inbox_json, - json_extract(t.payload_json, '$.createdBy') AS created_by, + -- Only older settled counts read these, so other rows skip the extract and decode. + CASE WHEN ${isSettledListedThread("t")} + THEN json_extract(t.payload_json, '$.inbox') END AS inbox_json, + CASE WHEN ${isSettledListedThread("t")} + THEN json_extract(t.payload_json, '$.createdBy') END AS created_by, + CASE WHEN ${isSettledListedThread("t")} + THEN ${senderParentId("t")} END AS sender_parent_thread_id, json_extract(t.payload_json, '$.lineage.relationshipToParent') AS relationship_to_parent, json_extract(t.payload_json, '$.lineage.parentThreadId') AS parent_thread_id, json_extract(t.payload_json, '$.lineage.rootThreadId') AS root_thread_id, @@ -9041,6 +9134,9 @@ export const layer: Layer.Layer = AND t.archived_at IS NULL ORDER BY t.updated_at ASC, t.thread_id ASC `; + const founders = yield* readParentFounders( + rows.flatMap((row) => row.sender_parent_thread_id ?? []), + ); return rows.map((row): ActiveShellIndexEntry => ({ id: ThreadId.make(row.thread_id), projectId: ProjectId.make(row.project_id), @@ -9054,6 +9150,9 @@ export const layer: Layer.Layer = pinOrderKey: row.pin_order_key, snoozedUntil: row.snoozed_until === null ? null : DateTime.makeUnsafe(row.snoozed_until), ...shellIndexSender(row), + ...(row.sender_parent_thread_id === null + ? {} + : { parentFounder: founders.get(row.sender_parent_thread_id) ?? null }), mayBeLive: row.may_be_live === 1, hasRemoteTasks: row.has_remote_tasks === 1, })); @@ -9168,15 +9267,12 @@ export const layer: Layer.Layer = )(function* ({ before, projectIds, limit }) { // ISO strings in one format, so string order is time order. const older = sql` - SELECT t.thread_id, t.project_id, + SELECT t.thread_id, t.project_id, t.payload_json, json_extract(t.payload_json, '$.settledAt') AS settled_at FROM orchestration_v2_projection_threads t WHERE t.deleted_at IS NULL AND t.archived_at IS NULL - AND json_extract(t.payload_json, '$.lineage.relationshipToParent') IS NOT 'subagent' - AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 - AND json_extract(t.payload_json, '$.settledOverride') = 'settled' - AND json_extract(t.payload_json, '$.settledAt') IS NOT NULL${ + AND ${isSettledListedThread("t")}${ projectIds === undefined ? sql`` : sql` AND t.project_id IN ${sql.in(projectIds)}` } `; @@ -9195,43 +9291,39 @@ export const layer: Layer.Layer = ORDER BY older.settled_at DESC, older.thread_id ASC LIMIT ${limit} `; - // Every active row, so the counts group by sender through parents as snapshots do. + // Only the older rows; a parent chain is read only for agent work without an owner. const countRows = limit !== 0 ? [] : yield* sql<{ readonly thread_id: string; readonly project_id: string; - readonly parent_thread_id: string | null; readonly inbox_json: string | null; readonly created_by: string | null; - readonly is_older: number; + readonly sender_parent_thread_id: string | null; }>` - SELECT t.thread_id, t.project_id, - json_extract(t.payload_json, '$.lineage.parentThreadId') AS parent_thread_id, - json_extract(t.payload_json, '$.inbox') AS inbox_json, - json_extract(t.payload_json, '$.createdBy') AS created_by, - t.thread_id IN ( - SELECT older.thread_id FROM (${older}) older WHERE ${belowCursor} - ) AS is_older - FROM orchestration_v2_projection_threads t - WHERE t.deleted_at IS NULL - AND t.archived_at IS NULL + WITH older AS (${older}) + SELECT older.thread_id, older.project_id, + json_extract(older.payload_json, '$.inbox') AS inbox_json, + json_extract(older.payload_json, '$.createdBy') AS created_by, + ${senderParentId("older")} AS sender_parent_thread_id + FROM older + WHERE ${belowCursor} `; + const founders = yield* readParentFounders( + countRows.flatMap((row) => row.sender_parent_thread_id ?? []), + ); return { threadIds: pageRows.map((row) => ThreadId.make(row.thread_id)), counts: countOlderSettledThreads( countRows.map((row) => ({ id: ThreadId.make(row.thread_id), projectId: ProjectId.make(row.project_id), - lineage: { - parentThreadId: - row.parent_thread_id === null ? null : ThreadId.make(row.parent_thread_id), - }, ...shellIndexSender(row), - isOlder: row.is_older === 1, + ...(row.sender_parent_thread_id === null + ? {} + : { parentFounder: founders.get(row.sender_parent_thread_id) ?? null }), })), - (row) => row.isOlder, ), }; }, Effect.mapError(shellSnapshotReadError)); @@ -9312,9 +9404,14 @@ export const layer: Layer.Layer = if (state === undefined) { return null; } + const parentId = rowsByThreadId.get(threadId)?.sender_parent_thread_id ?? null; return shellFromState({ state, visibleItemCount: visibleItemCountForShell({ threadId, statesByThreadId }), + parentFounder: + parentId === null + ? undefined + : ((yield* readParentFounders([parentId])).get(parentId) ?? null), }); }), ) diff --git a/apps/server/src/orchestration-v2/ShellStream.test.ts b/apps/server/src/orchestration-v2/ShellStream.test.ts index 3be601a23e7a6..a65289d578329 100644 --- a/apps/server/src/orchestration-v2/ShellStream.test.ts +++ b/apps/server/src/orchestration-v2/ShellStream.test.ts @@ -285,7 +285,7 @@ describe("omitOlderSettledShells", () => { }); }); - it("groups older counts by what an inbox filter reads, through each parent", () => { + it("groups older counts by what an inbox filter reads, with each stamped parent founder", () => { const kameron = makeThreadInbox({ actor: { kind: "human", id: "kameron" } }); const agentWork = makeThreadInbox({ actor: { kind: "agent", id: "lead" }, @@ -296,19 +296,18 @@ describe("omitOlderSettledShells", () => { row("history", 30), row("agent-history", 31, { createdBy: "agent" }), row("kameron", 32, { inbox: kameron }), - // Unowned agent work is its parent's founder's, even when the parent is not older. - row("jeff-parent", null, { - inbox: makeThreadInbox({ actor: { kind: "human", id: "jeff" } }), - }), + // A held parent does not count: the store stamps the founder its chain resolves to. + row("kameron-parent", null, { inbox: kameron }), row("agent-child", 33, { inbox: agentWork, + parentFounder: "jeff", lineage: { - parentThreadId: ThreadId.make("jeff-parent"), + parentThreadId: ThreadId.make("kameron-parent"), relationshipToParent: null, - rootThreadId: ThreadId.make("jeff-parent"), + rootThreadId: ThreadId.make("kameron-parent"), }, }), - row("agent-orphan", 34, { inbox: agentWork }), + row("agent-orphan", 34, { inbox: agentWork, parentFounder: null }), ]); expect(result.olderSettled.counts).toEqual([ { diff --git a/apps/server/src/orchestration-v2/ShellStream.ts b/apps/server/src/orchestration-v2/ShellStream.ts index 24d0dc6bcc9ec..1f7951432ab33 100644 --- a/apps/server/src/orchestration-v2/ShellStream.ts +++ b/apps/server/src/orchestration-v2/ShellStream.ts @@ -181,7 +181,9 @@ type ShellTrimRow = Pick< OrchestrationV2ThreadShell, "id" | "projectId" | "lineage" | "settledOverride" | "settledAt" > & - Pick & { readonly hasRemoteTasks?: boolean }; + Pick & { + readonly hasRemoteTasks?: boolean; + }; /** * What a subscriber's trims read across every active row. Computed from full shells or from the @@ -194,7 +196,8 @@ interface ShellTrimFacts { readonly remoteTaskParentIds: ReadonlySet; /** Undefined when no thread is settled, so none is older. */ readonly olderSettledBeforeMs: number | undefined; - readonly olderSettledCounts: ReadonlyArray; + /** Grouped on first use, so only snapshots that omit older settled threads pay for it. */ + readonly olderSettledCounts: () => ReadonlyArray; } const NEWEST_SETTLED_KEPT = 25; @@ -220,17 +223,19 @@ function shellTrimFacts(rows: ReadonlyArray): ShellTrimFacts { settledTimes.sort((left, right) => right - left); // With fewer than 25, the oldest one is the cut, so nothing is older. const olderSettledBeforeMs = settledTimes[NEWEST_SETTLED_KEPT - 1] ?? settledTimes.at(-1); + let counts: ReadonlyArray | undefined; return { subagentParentIds, subagentThreadIds, remoteTaskParentIds, olderSettledBeforeMs, - olderSettledCounts: - olderSettledBeforeMs === undefined - ? [] - : countOlderSettledThreads(rows, (row) => - isOlderSettledThreadShell(row, olderSettledBeforeMs), - ), + olderSettledCounts: () => + (counts ??= + olderSettledBeforeMs === undefined + ? [] + : countOlderSettledThreads( + rows.filter((row) => isOlderSettledThreadShell(row, olderSettledBeforeMs)), + )), }; } @@ -290,7 +295,7 @@ function omitOlderSettledShellsWith( olderSettled: { before: DateTime.formatIso(DateTime.makeUnsafe(beforeMs)), asOfSequence: input.snapshotSequence, - counts: facts.olderSettledCounts, + counts: facts.olderSettledCounts(), }, }; } diff --git a/apps/server/src/orchestration-v2/olderSettledCounts.ts b/apps/server/src/orchestration-v2/olderSettledCounts.ts index fd282d251cd13..6aa8a97d7df1d 100644 --- a/apps/server/src/orchestration-v2/olderSettledCounts.ts +++ b/apps/server/src/orchestration-v2/olderSettledCounts.ts @@ -10,27 +10,26 @@ import { /** What counting reads from a thread, from its shell or the store's index. */ export type OlderSettledCountRow = Pick & { - readonly lineage: Pick; readonly inbox?: ThreadInbox | undefined; readonly createdBy?: string | undefined; + readonly parentFounder?: OrchestrationV2ThreadShell["parentFounder"] | undefined; }; /** - * Per project, the rows `isOlder` matches, grouped by what an inbox filter reads. `rows` holds - * every active thread, so a sender's parent chain resolves like it does on a client holding them. + * Per project, the older settled rows grouped by what an inbox filter reads. Each row's sender + * uses its stamped `parentFounder`, so no other row is needed. */ -export function countOlderSettledThreads( - rows: ReadonlyArray, - isOlder: (row: Row) => boolean, +export function countOlderSettledThreads( + older: ReadonlyArray, ): ReadonlyArray { - const older = rows.filter(isOlder); if (older.length === 0) return []; const senders = resolveThreadSenders( - rows.map((row) => ({ + older.map((row) => ({ key: row.id, - parentKey: row.lineage.parentThreadId, + parentKey: null, inbox: row.inbox, createdBy: row.createdBy, + parentFounder: row.parentFounder, })), ); const byProject = new Map< diff --git a/apps/web/src/components/settings/NewThreadInboxSettings.tsx b/apps/web/src/components/settings/NewThreadInboxSettings.tsx index 9f3bca0de4046..2821cae210dd8 100644 --- a/apps/web/src/components/settings/NewThreadInboxSettings.tsx +++ b/apps/web/src/components/settings/NewThreadInboxSettings.tsx @@ -10,6 +10,7 @@ import { import { useServerConfigs } from "../../state/entities"; import { useEnvironmentIdentities } from "../../state/environments"; import { useEnvironmentScope } from "../../state/session"; +import { Button } from "../ui/button"; import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; import { SettingsRow } from "./settingsLayout"; import { searchableSetting } from "./settingsSearch"; @@ -70,6 +71,7 @@ export function MachineOwnersRow() { label={label} owner={owners[environmentId]} reportsOwner={serverConfigs.get(environmentId)?.machineOwner === true} + deviceChoice={legacyOwners?.[environmentId]} /> ))} @@ -82,8 +84,13 @@ function MachineOwnerRow(props: { label: string; owner: Founder | undefined; reportsOwner: boolean; + /** This device's own entry from before machines stored their owner. */ + deviceChoice: Founder | undefined; }) { const { environmentId, label, owner, reportsOwner } = props; + // An upgraded machine with no owner yet: offer this device's earlier choice, never push it, + // so devices with different maps do not overwrite each other. + const migratable = reportsOwner && owner === undefined ? props.deviceChoice : undefined; const canWrite = useEnvironmentScope(environmentId, AuthSettingsWriteScope); const updateEnvironmentSettings = useUpdateEnvironmentSettings(environmentId); const setOwner = (founder: Founder | null) => { @@ -97,7 +104,22 @@ function MachineOwnerRow(props: { }; return (
- {label} +
+ {label} + {migratable ? ( + + This device chose {OWNER_LABELS[migratable]}. + + + ) : null} +