diff --git a/.github/workflows/closura-arc-ci.yml b/.github/workflows/closura-arc-ci.yml index d0c2209bf5fda..f1da93204077f 100644 --- a/.github/workflows/closura-arc-ci.yml +++ b/.github/workflows/closura-arc-ci.yml @@ -115,9 +115,10 @@ jobs: 'apps/server/src/speech/CloudSpeech.test.ts': ['AssemblyAI', 'assemblyai'], 'packages/contracts/src/speech.ts': ['assemblyai'], 'apps/web/src/components/settings/CloudVoiceConfiguration.tsx': ['assemblyai'], - 'packages/contracts/src/threadInbox.ts': ['matchesInboxView', 'agent-only'], + 'packages/contracts/src/threadInbox.ts': ['matchesInboxView', 'matchesInboxFilter', 'agent-only'], + 'packages/client-runtime/src/state/threadListFilter.ts': ['matchesPrimaryInbox', 'matchesListedInboxes', 'hiddenEnvironmentIds'], 'apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx': ['Kameron', 'Jeff', 'Agents', 'Environments'], - 'apps/web/src/components/sidebar/threadListFilter.logic.ts': ['matchesInboxFilter', 'hiddenEnvironmentIds'], + 'apps/web/src/components/sidebar/threadListFilter.logic.ts': ['matchesListedInboxes', 'hiddenEnvironmentIds'], }; for (const [file, needles] of Object.entries(required)) { const source = readFileSync(file, 'utf8'); 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/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index b961ef1e0dc34..7d02761ed644e 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,9 @@ const assertShellIndexCoversShells = Effect.fnUntraced(function* () { | "pinnedAt" | "pinOrderKey" | "snoozedUntil" + | "inbox" + | "createdBy" + | "parentFounder" >, ) => ({ id: row.id, @@ -167,6 +174,16 @@ const assertShellIndexCoversShells = Effect.fnUntraced(function* () { pinnedAt: iso(row.pinnedAt), pinOrderKey: row.pinOrderKey ?? null, snoozedUntil: iso(row.snoozedUntil), + // 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) => { @@ -3077,6 +3094,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 +3115,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.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.name), + rootThreadId: id(input.parent ?? (input.subagent === true ? "b" : input.name)), }, forkedFrom: null, createdAt: now, @@ -3127,24 +3152,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 +3175,95 @@ 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 the parent chain. + const agentWork = makeThreadInbox({ + actor: { kind: "agent", id: "lead" }, + routing: inboxRoutingFor("agents"), }); + 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: 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", + 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 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.listing.counts[0]?.groups ?? [])], + [ + { 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 b98ccff45e783..4b92afc30a9a4 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -80,6 +80,9 @@ import { NodeId, ProjectId, ProviderInstanceId, + ThreadInbox, + resolveThreadSenders, + type SenderFounder, } from "@t3tools/contracts"; import { createOrchestrationV2TurnItemVisibility } from "@t3tools/shared/orchestrationV2Timeline"; import { @@ -90,6 +93,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 +102,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 +479,10 @@ export interface ActiveShellIndexEntry extends Pick< | "pinOrderKey" | "snoozedUntil" > { + /** 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. */ @@ -1121,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; @@ -1206,6 +1217,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: []. @@ -2096,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, @@ -7780,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); @@ -7873,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, @@ -8706,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); @@ -8744,6 +8847,7 @@ export const layer: Layer.Layer = ]); return { targetThreadIds, + parentFounders, threadRows, runRows, itemCountRows, @@ -8756,6 +8860,7 @@ export const layer: Layer.Layer = const decodeShellSnapshot = Effect.fn("ProjectionStore.decodeShellSnapshot")(function* ({ targetThreadIds, + parentFounders, threadRows, runRows, itemCountRows, @@ -8792,6 +8897,7 @@ export const layer: Layer.Layer = threadId: state.thread.id, statesByThreadId, }), + parentFounder: parentFounders.get(state.thread.id), }), ); @@ -8956,12 +9062,22 @@ 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 sender_parent_thread_id: string | null; readonly may_be_live: number; readonly has_remote_tasks: number; }>` SELECT t.thread_id, t.project_id, + -- 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, @@ -9018,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), @@ -9030,6 +9149,10 @@ 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), + ...(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, })); @@ -9144,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)}` } `; @@ -9171,21 +9291,40 @@ export const layer: Layer.Layer = ORDER BY older.settled_at DESC, older.thread_id ASC LIMIT ${limit} `; + // Only the older rows; a parent chain is read only for agent work without an owner. const countRows = limit !== 0 ? [] - : yield* sql<{ readonly project_id: string; readonly count: number }>` + : yield* sql<{ + readonly thread_id: string; + readonly project_id: string; + readonly inbox_json: string | null; + readonly created_by: string | null; + readonly sender_parent_thread_id: string | null; + }>` WITH older AS (${older}) - SELECT older.project_id, COUNT(*) AS count FROM 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} - GROUP BY older.project_id `; + const founders = yield* readParentFounders( + countRows.flatMap((row) => row.sender_parent_thread_id ?? []), + ); 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), + ...shellIndexSender(row), + ...(row.sender_parent_thread_id === null + ? {} + : { parentFounder: founders.get(row.sender_parent_thread_id) ?? null }), + })), + ), }; }, Effect.mapError(shellSnapshotReadError)); @@ -9265,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 fbc610235dd01..a65289d578329 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,51 @@ 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, with each stamped parent founder", () => { + 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 }), + // 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("kameron-parent"), + relationshipToParent: null, + rootThreadId: ThreadId.make("kameron-parent"), + }, + }), + row("agent-orphan", 34, { inbox: agentWork, parentFounder: null }), + ]); + 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 +373,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..1f7951432ab33 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,10 @@ 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 @@ -193,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; @@ -219,20 +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); - 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); - } - } - } + let counts: ReadonlyArray | undefined; return { subagentParentIds, subagentThreadIds, remoteTaskParentIds, olderSettledBeforeMs, - olderSettledCounts: Array.from(counts, ([projectId, count]) => ({ projectId, count })), + olderSettledCounts: () => + (counts ??= + olderSettledBeforeMs === undefined + ? [] + : countOlderSettledThreads( + rows.filter((row) => isOlderSettledThreadShell(row, olderSettledBeforeMs)), + )), }; } @@ -292,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/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..6aa8a97d7df1d --- /dev/null +++ b/apps/server/src/orchestration-v2/olderSettledCounts.ts @@ -0,0 +1,58 @@ +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 inbox?: ThreadInbox | undefined; + readonly createdBy?: string | undefined; + readonly parentFounder?: OrchestrationV2ThreadShell["parentFounder"] | undefined; +}; + +/** + * 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( + older: ReadonlyArray, +): ReadonlyArray { + if (older.length === 0) return []; + const senders = resolveThreadSenders( + older.map((row) => ({ + key: row.id, + parentKey: null, + inbox: row.inbox, + createdBy: row.createdBy, + parentFounder: row.parentFounder, + })), + ); + 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..2821cae210dd8 100644 --- a/apps/web/src/components/settings/NewThreadInboxSettings.tsx +++ b/apps/web/src/components/settings/NewThreadInboxSettings.tsx @@ -1,11 +1,16 @@ -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 { Button } from "../ui/button"; import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; import { SettingsRow } from "./settingsLayout"; import { searchableSetting } from "./settingsSearch"; @@ -44,53 +49,103 @@ 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; + /** 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) => { + 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} + {migratable ? ( + + This device chose {OWNER_LABELS[migratable]}. + + + ) : null} +
+
+ +
+
+ ); +} 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..cb63885cc95c1 100644 --- a/docs/internals/connection-runtime.md +++ b/docs/internals/connection-runtime.md @@ -125,6 +125,15 @@ 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. Agent work without an owner takes its sender from +its parent chain; the server resolves that chain and stamps it on the shell as +`parentFounder`, because clients that omit idle subagents never hold those parents. 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; otherwise they read as a lower bound of at least the +Settled rows shown. 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..5dd9895a02f7c 100644 --- a/docs/user/thread-sidebar.md +++ b/docs/user/thread-sidebar.md @@ -210,6 +210,16 @@ 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. After a +machine upgrades, a device that had chosen an owner for it offers **Set on machine** to keep +that choice. + ## 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..3f93fa4aab073 --- /dev/null +++ b/packages/client-runtime/src/state/inboxConvergence.test.ts @@ -0,0 +1,410 @@ +import { + EnvironmentId, + InboxChoice, + inboxRoutingFor, + isOlderSettledThreadShell, + makeThreadInbox, + primaryInbox, + ProjectId, + resolveThreadSenders, + senderNeedsParent, + ThreadId, + type Founder, + type SenderFounder, + 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; + readonly parentFounder?: SenderFounder; + }; +} + +const row = ( + environmentId: EnvironmentId, + id: string, + input: { + readonly inbox?: ThreadInbox; + readonly settledDay?: number; + readonly parent?: string; + readonly subagent?: boolean; + readonly createdBy?: "user" | "agent"; + readonly pinned?: boolean; + } = {}, +): Row => { + const lineage = { + parentThreadId: input.parent === undefined ? null : ThreadId.make(input.parent), + relationshipToParent: input.subagent === true ? "subagent" : 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 }), + }, + }; +}; + +const agentWork = makeThreadInbox({ actor: bot, routing: inboxRoutingFor("agents") }); + +/** What each server sends: rows whose sender comes from a parent carry the chain's founder. */ +function stampParentFounders(rows: ReadonlyArray): ReadonlyArray { + const senders = resolveThreadSenders( + rows.map((thread) => ({ + key: `${thread.environmentId}:${thread.id}`, + parentKey: + thread.lineage.parentThreadId === null + ? null + : `${thread.environmentId}:${thread.lineage.parentThreadId}`, + inbox: thread.source.inbox, + createdBy: thread.source.createdBy, + })), + ); + return rows.map((thread) => + senderNeedsParent(thread.source.inbox) && thread.lineage.parentThreadId !== null + ? { + ...thread, + source: { + ...thread.source, + parentFounder: + senders.get(`${thread.environmentId}:${thread.lineage.parentThreadId}`)?.founder ?? + null, + }, + } + : thread, + ); +} + +// Everything the servers store. Days below 50 settled before each trimming server's cut. +const SERVER_ROWS: ReadonlyArray = stampParentFounders([ + 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: agentWork, settledDay: 14, parent: "jeff-root" }), + // Forks of an idle subagent with no owner: apps that omit idle subagents lack their parent. + row(asus, "subagent", { inbox: agentWork, parent: "jeff-root", subagent: true }), + row(asus, "subagent-fork-old", { inbox: agentWork, settledDay: 16, parent: "subagent" }), + row(asus, "subagent-fork-recent", { inbox: agentWork, settledDay: 94, parent: "subagent" }), + // 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 older = SERVER_ROWS.filter( + (thread) => thread.environmentId === environmentId && isOlder(thread), + ); + // Like the server, each older row's own fields and stamp, without any other row. + const senders = resolveThreadSenders( + older.map((thread) => ({ + key: thread.id, + parentKey: null, + inbox: thread.source.inbox, + createdBy: thread.source.createdBy, + parentFounder: thread.source.parentFounder, + })), + ); + 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)), + settledOnScreen: settled.length, + total, + }; +} + +// Every snapshot row: newer than the cut, pinned, or from a server that trims nothing. Opted-in +// apps also omit idle subagents. +const SNAPSHOT_ROWS = SERVER_ROWS.filter( + (thread) => + (!isOlder(thread) || thread.source.pinnedAt !== null) && + thread.lineage.relationshipToParent !== "subagent", +); +// 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("credits a fork of a subagent alike on an app that lacks the subagent", () => { + const filter = FILTERS[0]![1]; + const key = `${asus}:subagent-fork-recent`; + expect(CLIENT_A.held.some((thread) => thread.id === "subagent")).toBe(false); + expect(CLIENT_B.held.some((thread) => thread.id === "subagent")).toBe(true); + const a = sidebar({ ...CLIENT_A, olderSettled, filter }); + const b = sidebar({ ...CLIENT_B, olderSettled, filter }); + expect(a.attributions.get(key)).toBe("jeff-agents"); + expect(b.attributions.get(key)).toBe("jeff-agents"); + // Without the server's stamp, the app lacking the subagent could only say Unknown. + const unstamped = CLIENT_A.held.map((thread) => { + const { parentFounder: _, ...source } = thread.source; + return { ...thread, source }; + }); + expect( + sidebar({ ...CLIENT_A, held: unstamped, olderSettled, filter }).attributions.get(key), + ).toBe("unknown"); + // Its older sibling, which no app holds, counts under Jeff's agents too. + const fromJeff = FILTERS[5]![1]; + expect(sidebar({ ...CLIENT_A, olderSettled, filter: fromJeff }).total).toEqual({ + count: truthFor(fromJeff).total.count, + exact: true, + }); + }); + + it("shows a lower bound, never below the rows on screen, when a server sends totals only", () => { + const totalsOnly = serverOlderSettled(false); + const kameronInbox = FILTERS[1]![1]; + const truth = truthFor(kameronInbox).total.count; + for (const client of [CLIENT_A, CLIENT_B]) { + const shelf = sidebar({ ...client, olderSettled: totalsOnly, filter: kameronInbox }); + expect(shelf.total.exact).toBe(false); + expect(shelf.total.count).toBeGreaterThanOrEqual(shelf.settledOnScreen); + expect(shelf.total.count).toBeLessThanOrEqual(truth); + } + // The app that loaded every older page shows all of them, not fewer than it lists. + expect( + sidebar({ ...CLIENT_B, olderSettled: totalsOnly, filter: kameronInbox }).total.count, + ).toBe(truth); + // 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..58615eabb8c95 100644 --- a/packages/client-runtime/src/state/olderSettledThreads.test.ts +++ b/packages/client-runtime/src/state/olderSettledThreads.test.ts @@ -1,5 +1,7 @@ import { EnvironmentId, + inboxRoutingFor, + makeThreadInbox, ORCHESTRATION_V2_WS_METHODS, OrchestrationV2GetShellSnapshotError, ProjectId, @@ -38,6 +40,7 @@ import { isListedSettledThread, olderSettledCountKey, settledShelfFrontier, + settledShelfTotal, unloadedOlderSettledCount, wantedOlderSettledPages, type OlderSettled, @@ -52,6 +55,7 @@ const older: OlderSettled = { before, beforeMs: 50 * DAY, counts: new Map([[project, 3]]), + groups: null, }; const row = ( @@ -97,6 +101,56 @@ 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("changes when a parent an older row takes its sender from is rerouted", () => { + const agentWork = makeThreadInbox({ + actor: { kind: "agent", id: "lead" }, + routing: inboxRoutingFor("agents"), + }); + const parent = { ...row("parent", { createdDay: 60 }), inbox: agentWork }; + const grandparent = row("grandparent", { createdDay: 60 }); + const child = { + ...row("child-old", { settledDay: 10 }), + lineage: { + parentThreadId: parent.id, + relationshipToParent: null, + rootThreadId: grandparent.id, + }, + inbox: agentWork, + parentFounder: null, + } as OrchestrationV2ThreadShell; + const withParent = (lineageParent: OrchestrationV2ThreadShell) => [ + child, + { ...lineageParent, lineage: { ...lineageParent.lineage, parentThreadId: grandparent.id } }, + grandparent, + ]; + const start = key(withParent(parent)); + const jeff = inboxRoutingFor("jeff"); + // The parent, and through its ownerless agent work, the grandparent. + expect(key(withParent({ ...parent, inbox: { ...agentWork, ...jeff } }))).not.toBe(start); + expect( + key([ + ...withParent(parent).slice(0, 2), + { ...grandparent, inbox: { ...agentWork, ...jeff } }, + ]), + ).not.toBe(start); + // A new stamp from the server. + expect(key([{ ...child, parentFounder: "jeff" }, ...withParent(parent).slice(1)])).not.toBe( + start, + ); + // A parent of a row with its own owner does not move its group. + const owned = { ...child, inbox: { ...agentWork, ...jeff } }; + expect(key([owned, { ...parent, inbox: makeThreadInbox({ actor: null }) }])).toBe( + key([owned, parent]), + ); }); it("stays put for new threads and subagents", () => { @@ -206,6 +260,33 @@ it("counts older threads the client does not hold, once", () => { ).toBe(18); }); +it("never reads a lower-bound total below the Settled rows on screen", () => { + // A totals-only server and a filter that shows part of it: 3 older counted, 40 loaded and + // listed after several pages. Subtracting them from no total would read "0+". + const loaded = Array.from({ length: 40 }, (_, index) => ({ + source: row(`paged-${index}`, { settledDay: 10 }), + environmentId, + })); + const total = settledShelfTotal({ + olderSettled: () => older, + scopes: [{ environmentId, projectIds: null }], + threads: loaded, + settledCount: loaded.length + 2, + filter: { matches: () => true, coverage: () => "some" }, + }); + expect(total).toEqual({ count: 42, exact: false }); + // An exact total still nets loaded rows against the server's count. + expect( + settledShelfTotal({ + olderSettled: () => older, + scopes: [{ environmentId, projectIds: null }], + threads: loaded.slice(0, 3), + settledCount: 5, + filter: null, + }), + ).toEqual({ count: 5, exact: true }); +}); + describe("wantedOlderSettledPages", () => { const otherEnvironment = EnvironmentId.make("env-b"); const full = (settledAt: string): OlderSettledPages => ({ diff --git a/packages/client-runtime/src/state/olderSettledThreads.ts b/packages/client-runtime/src/state/olderSettledThreads.ts index a5b79beafe533..7b11137262ec9 100644 --- a/packages/client-runtime/src/state/olderSettledThreads.ts +++ b/packages/client-runtime/src/state/olderSettledThreads.ts @@ -2,8 +2,11 @@ import { isListedThreadLineage, isOlderSettledThreadShell, ORCHESTRATION_V2_WS_METHODS, + senderNeedsParent, type EnvironmentId, + type OrchestrationV2OlderSettledCount, type OrchestrationV2OlderSettledCursor, + type OrchestrationV2OlderSettledGroup, type OrchestrationV2OlderSettledThreadsPage, type OrchestrationV2SearchThreadsInput, type OrchestrationV2ThreadFamily, @@ -32,6 +35,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,11 +83,23 @@ 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. - * Archived-thread events arrive as removals too, each costing one count read. + * older row that arrives, changes project or routing (its own or a held parent's it takes its + * sender from), or an old row arriving outside the set (unsettled). Threads created after the cut + * never enter it, as threads settle after creation. */ export function olderSettledCountKey( threads: ReadonlyArray, @@ -91,17 +108,40 @@ export function olderSettledCountKey( ): string { let lastRemoval = 0; for (const sequence of removedAt.values()) lastRemoval = Math.max(lastRemoval, sequence); + const byId = new Map(threads.map((thread) => [thread.id, thread])); const rows: string[] = []; for (const thread of threads) { 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(senderRouting(thread, byId))}` : ""; + rows.push(`${thread.id}:${thread.projectId}:${older ? 1 : 0}${routing}`); } } return `${lastRemoval}|${rows.sort().join(",")}`; } +/** + * What an older row's count group reads: its routing and stamped parent founder, plus the + * routing of each held parent its sender comes from, so a parent's reroute also recounts. + */ +function senderRouting( + thread: OrchestrationV2ThreadShell, + byId: ReadonlyMap, +): ReadonlyArray { + const routing: unknown[] = [thread.inbox ?? null, thread.parentFounder ?? null]; + const seen = new Set([thread.id]); + let current = thread; + while (senderNeedsParent(current.inbox) && current.lineage.parentThreadId !== null) { + const parent = byId.get(current.lineage.parentThreadId); + if (parent === undefined || seen.has(parent.id)) break; + seen.add(parent.id); + routing.push(parent.inbox ?? null); + current = parent; + } + return routing; +} + /** Whether a row sits at or above a page cursor in the server's order (settle time desc, id asc). */ function atOrAboveCursor( thread: Pick, @@ -167,14 +207,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. + * A totals-only server under a partial filter gives a lower bound, at least the loaded rows. + */ +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 +268,9 @@ export function unloadedOlderSettledCount(input: { } } } - return Math.max(0, unloaded); + const total = Math.max(0, input.settledCount + unloaded); + // A lower bound never reads below the rows on screen. + return { count: exact ? total : Math.max(input.settledCount, total), exact }; } /** @@ -268,10 +352,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 +370,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 +395,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..180d4874c3ff0 100644 --- a/packages/client-runtime/src/state/threadAttribution.ts +++ b/packages/client-runtime/src/state/threadAttribution.ts @@ -1,4 +1,12 @@ -import type { EnvironmentId, Founder, ThreadId, ThreadInbox } from "@t3tools/contracts"; +import { + resolveThreadSenders, + type EnvironmentId, + type Founder, + type SenderFounder, + type ThreadId, + type ThreadInbox, + type ThreadSender, +} from "@t3tools/contracts"; import { ThreadAttribution } from "@t3tools/contracts/settings"; export const THREAD_ATTRIBUTIONS = ThreadAttribution.literals; @@ -18,70 +26,84 @@ export interface AttributableThread { readonly source: { readonly inbox?: ThreadInbox | null | undefined; readonly createdBy?: string | undefined; + /** The server's resolution of the parent chain; wins over the parents this client holds. */ + readonly parentFounder?: SenderFounder | undefined; }; } -/** 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, + parentFounder: thread.source.parentFounder, + }; + }), + ); 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..f6b22a0f690be 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 { + InboxActor, + InboxChoice, + SenderFounder, + ThreadInbox, + ThreadInboxRoutingPatch, +} from "./threadInbox.ts"; import * as SchemaAST from "effect/SchemaAST"; import * as SchemaGetter from "effect/SchemaGetter"; import * as SchemaTransformation from "effect/SchemaTransformation"; @@ -2214,6 +2220,11 @@ export type OrchestrationV2LatestVisibleMessageSummary = export const OrchestrationV2ThreadShell = Schema.Struct({ ...OrchestrationV2CreationFields, inbox: Schema.optionalKey(ThreadInbox), + /** + * For agent work with no owner or human requester, the founder its parent chain resolves to, + * so every client labels it alike whichever parents it holds. Older servers omit it. + */ + parentFounder: Schema.optionalKey(SenderFounder), id: ThreadId, projectId: ProjectId, title: Schema.String, @@ -2340,9 +2351,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: SenderFounder, + 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..3e6e05a7f1807 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,82 @@ export function matchesInboxView(inbox: ThreadInbox | null | undefined, view: In const lane = founderInboxLane(inbox, view); return lane === "action" || lane === "FYI"; } + +/** + * A sender's founder before its machine's owner is applied: `"machine"` stands for that owner, + * which clients look up. + */ +export const SenderFounder = Schema.NullOr(Schema.Union([Founder, Schema.Literal("machine")])); +export type SenderFounder = typeof SenderFounder.Type; + +/** Who a thread came from. `byAgent` marks an agent's work for that founder. */ +export interface ThreadSender { + readonly founder: SenderFounder; + 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; + /** The founder the server resolved from the parent chain; used instead of walking parents. */ + readonly parentFounder?: SenderFounder | undefined; +} + +const humanFounder = (actor: InboxActor | null | undefined): Founder | null => + actor?.kind === "human" ? actor.id : null; + +/** + * Whether a thread's sender comes from its parent: agent work with no owner or human requester. + * The server's parent-chain read mirrors this in SQL. + */ +export const senderNeedsParent = (inbox: ThreadInbox | null | undefined): boolean => + inbox?.origin?.kind === "agent" && inbox.owner === null && inbox.requestedBy?.kind !== "human"; + +/** + * 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 stamp `parentFounder` on the threads they send and count by it, so a + * sender never depends on which parents a client holds; parents are walked only without it. + */ +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 fromParent = (): SenderFounder => { + if (thread.parentFounder !== undefined) return thread.parentFounder; + const parent = + thread.parentKey === null || resolving.has(thread.parentKey) + ? undefined + : byKey.get(thread.parentKey); + return parent ? resolve(parent).founder : null; + }; + const human = humanFounder(inbox?.origin); + const founder = + human ?? + humanFounder(inbox?.requestedBy) ?? + (inbox?.origin?.kind === "agent" ? (inbox.owner ?? fromParent()) : 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; +}