TypeScript
fix(mobile): agent-spawned threads can be mentioned from the @ picker (#15841)
Lars Nieuwenhuis38d9760ccaea
diff --git a/apps/mobile/src/lib/composerContext.test.ts b/apps/mobile/src/lib/composerContext.test.tsindex 4547efaa332..99117899a5e 100644--- a/apps/mobile/src/lib/composerContext.test.ts+++ b/apps/mobile/src/lib/composerContext.test.ts@@ -4,6 +4,8 @@ import {ProjectId,ProviderInstanceId,ComposerContextId,+ EnvironmentId,+ ThreadId,type OrchestrationMessageContext,} from "@t3tools/contracts";import { collectComposerInlineTokens } from "@t3tools/shared/composerInlineTokens";@@ -24,6 +26,7 @@ import {uploadedComposerContext,serializeComposerMessageForServer,pullRequestComposerContext,+ threadComposerContext,} from "./composerContext";const terminal = {@@ -318,3 +321,20 @@ describe("host context compatibility", () => {expect(context.records).toEqual([terminal, review, pr]);});});++describe("threadComposerContext", () => {+ it("mints a schema-valid, stable id for agent-spawned thread ids", () => {+ const environmentId = EnvironmentId.make("env-1");+ const ref = {+ environmentId,+ threadId: ThreadId.make(`thread:mcp:${encodeURIComponent("3f2b:delegate")}:review:0`),+ };+ const contextId = threadComposerContext(ref, "Review").contextId;++ expect(() => ComposerContextId.make(contextId)).not.toThrow();+ expect(threadComposerContext(ref, "Renamed").contextId).toBe(contextId);+ expect(+ threadComposerContext({ environmentId, threadId: ThreadId.make("abc-123") }, "t").contextId,+ ).toBe("thread_abc-123");+ });+});diff --git a/apps/mobile/src/lib/composerContext.ts b/apps/mobile/src/lib/composerContext.tsindex 45b033dc948..16a790d65e4 100644--- a/apps/mobile/src/lib/composerContext.ts+++ b/apps/mobile/src/lib/composerContext.ts@@ -20,6 +20,7 @@ import {isImageReferencesOnly,replaceComposerContextReferences,sanitizeComposerContextLabel,+ toKindScopedComposerContextId,} from "@t3tools/shared/composerContextReferences";import {collectComposerInlineTokens,@@ -110,7 +111,7 @@ export function threadComposerContext(ref: ScopedThreadRef, title: string): Threreturn {version: 1,kind: "thread",- contextId: ComposerContextId.make(`thread_${ref.threadId}`),+ contextId: toKindScopedComposerContextId("thread", ref.threadId),label,environmentId: ref.environmentId,threadId: ref.threadId,diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsxindex 5f775123d49..822e79da8db 100644--- a/apps/web/src/components/chat/ChatComposer.tsx+++ b/apps/web/src/components/chat/ChatComposer.tsx@@ -199,7 +199,10 @@ import {type TerminalContextSelection,} from "../../lib/terminalContext";import { useComposerPathSearch } from "../../lib/composerPathSearchState";-import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences";+import {+ replaceComposerContextReferences,+ toKindScopedComposerContextId,+} from "@t3tools/shared/composerContextReferences";import {getRestingComposerImagePreviewCounts,resolveRestingComposerControlsLayout,@@ -228,7 +231,6 @@ import {formatInlineContextReference,insertInlineContextReference,inlineContextReferenceReplacement,- toKindScopedComposerContextId,} from "~/lib/composerContextReferences";import {asKnownContextRecord,diff --git a/apps/web/src/composerDraftStore.ts b/apps/web/src/composerDraftStore.tsindex f5663094da6..1dfecfc4d07 100644--- a/apps/web/src/composerDraftStore.ts+++ b/apps/web/src/composerDraftStore.ts@@ -60,8 +60,6 @@ import {ensureInlineContextReferences,formatInlineContextReference,removeInlineContextReference,- toComposerContextId,- toKindScopedComposerContextId,} from "./lib/composerContextReferences";import {fileContextReference,@@ -77,7 +75,11 @@ import { persist, type PersistStorage, type StorageValue } from "zustand/middlewimport { useShallow } from "zustand/react/shallow";import { createDeferredStorage, createMemoryStorage } from "./lib/storage";import { getDefaultServerModel } from "./providerModels";-import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences";+import {+ replaceComposerContextReferences,+ toComposerContextId,+ toKindScopedComposerContextId,+} from "@t3tools/shared/composerContextReferences";import { UnifiedSettings } from "@t3tools/contracts/settings";import { ReviewCommentContextSchema, type ReviewCommentContext } from "./reviewCommentContext";const isRuntimeMode = Schema.is(RuntimeMode);diff --git a/apps/web/src/lib/composerContextRecords.ts b/apps/web/src/lib/composerContextRecords.tsindex e3091de9d76..af743dd1b91 100644--- a/apps/web/src/lib/composerContextRecords.ts+++ b/apps/web/src/lib/composerContextRecords.ts@@ -24,12 +24,12 @@ import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClimport {collectComposerContextReferences,sanitizeComposerContextLabel,+ toKindScopedComposerContextId,} from "@t3tools/shared/composerContextReferences";import {type ComposerContextReference,producerIdFromComposerContextId,- toKindScopedComposerContextId,} from "./composerContextReferences";import type { ComposerFileAttachment, ComposerImageAttachment } from "~/composerDraftStore";import type { AttachmentUploadState } from "./attachmentUploadState";diff --git a/apps/web/src/lib/composerContextReferences.test.ts b/apps/web/src/lib/composerContextReferences.test.tsindex 2d70b6bfa91..53965d6c697 100644--- a/apps/web/src/lib/composerContextReferences.test.ts+++ b/apps/web/src/lib/composerContextReferences.test.ts@@ -9,8 +9,6 @@ import {inlineContextReferenceReplacement,removeInlineContextReference,stripInlineContextReferences,- toComposerContextId,- toKindScopedComposerContextId,} from "./composerContextReferences";const review = { kind: "review-comment", contextId: "rc-1", label: "a.ts L4" };@@ -63,27 +61,3 @@ describe("composerContextReferences", () => {expect(ensureInlineContextReferences(reviewLink, [review])).toBe(reviewLink);});});--describe("toComposerContextId", () => {- it("keeps same-kind raw IDs distinct when one contains the namespace prefix", () => {- expect(toKindScopedComposerContextId("image", "x")).toBe("image_x");- expect(toKindScopedComposerContextId("image", "image_x")).toBe("image_image_x");- });- it("keeps ids that already fit the grammar and folds the rest deterministically", () => {- expect(toComposerContextId("file-comment-1700-1")).toBe("file-comment-1700-1");- const folded = toComposerContextId("pull-request-selection:src/a.ts:4-9");- expect(folded).toMatch(/^pull-request-selection-src-a-ts-4-9-[0-9a-f]{16}$/);- expect(toComposerContextId("pull-request-selection:src/a.ts:4-9")).toBe(folded);- expect(toComposerContextId("pull-request-selection:src/b.ts:4-9")).not.toBe(folded);- expect(toComposerContextId("::")).toMatch(/^ctx-[0-9a-f]{16}$/);- });- it("tells apart producer ids that agree past the slug's truncation point", () => {- // The slug keeps 48 characters, so only the digest distinguishes these two.- const shared = `pull-request-finding:${"a".repeat(60)}`;- const first = toComposerContextId(`${shared}:1`);- const second = toComposerContextId(`${shared}:2`);-- expect(first).not.toBe(second);- expect(first.length).toBeLessThanOrEqual(128);- });-});diff --git a/apps/web/src/lib/composerContextReferences.ts b/apps/web/src/lib/composerContextReferences.tsindex b80e021a3ca..68197bef6e4 100644--- a/apps/web/src/lib/composerContextReferences.ts+++ b/apps/web/src/lib/composerContextReferences.ts@@ -17,49 +17,6 @@ export interface ComposerContextReference {label: string;}-const CONTEXT_ID_PATTERN = /^[a-z0-9_-]{1,128}$/i;--/**- * Two independent FNV-1a passes, one forward and one with a different offset basis over the- * reversed input. 64 bits of digest, because the slug in front of it is truncated: two long- * producer ids that agree on their first 48 characters are told apart by this alone.- */-function fnv1a64(value: string): string {- let forward = 0x811c9dc5;- let reverse = 0x9dc5811c;- for (let index = 0; index < value.length; index += 1) {- forward ^= value.charCodeAt(index);- forward = Math.imul(forward, 0x01000193) >>> 0;- reverse ^= value.charCodeAt(value.length - 1 - index);- reverse = Math.imul(reverse, 0x01000193) >>> 0;- }- return `${forward.toString(16).padStart(8, "0")}${reverse.toString(16).padStart(8, "0")}`;-}--/**- * Producers mint ids in their own grammars (`pull-request-finding:42`,- * `file-comment-<ms>-<n>`). A context id must survive a Markdown link and the wire- * schema, so anything outside `[a-z0-9_-]` is folded into a readable slug plus a hash of- * the original. Deterministic, so the same producer id always maps to the same context id.- */-export function toComposerContextId(producerId: string): ComposerContextId {- if (CONTEXT_ID_PATTERN.test(producerId)) return producerId as ComposerContextId;- const slug = producerId- .replace(/[^a-z0-9_-]+/gi, "-")- .replace(/^-+|-+$/g, "")- .slice(0, 48);- return `${slug || "ctx"}-${fnv1a64(producerId)}` as ComposerContextId;-}--/** Raw producer IDs are always scoped, even when they already start with the kind name. */-export function toKindScopedComposerContextId(- kind: ComposerContextKind,- producerId: string,-): ComposerContextId {- const prefix = `${kind}_`;- return toComposerContextId(`${prefix}${producerId}`);-}-/** Only for importing canonical records: undo one namespace before rebuilding a draft. */export function producerIdFromComposerContextId(kind: ComposerContextKind,diff --git a/apps/web/src/lib/terminalContext.ts b/apps/web/src/lib/terminalContext.tsindex 553a3550581..92e5222fcb8 100644--- a/apps/web/src/lib/terminalContext.ts+++ b/apps/web/src/lib/terminalContext.ts@@ -1,6 +1,8 @@import type { ThreadId } from "@t3tools/contracts";-import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";-import { toKindScopedComposerContextId } from "./composerContextReferences";+import {+ formatComposerContextReference,+ toKindScopedComposerContextId,+} from "@t3tools/shared/composerContextReferences";export interface TerminalContextSelection {terminalId: string;diff --git a/apps/web/src/lib/threadContextAttachment.ts b/apps/web/src/lib/threadContextAttachment.tsindex 474ccac3819..996b444d7f7 100644--- a/apps/web/src/lib/threadContextAttachment.ts+++ b/apps/web/src/lib/threadContextAttachment.ts@@ -1,9 +1,8 @@import type { EnvironmentId, OrchestrationV2ThreadTranscript } from "@t3tools/contracts";+import { toKindScopedComposerContextId } from "@t3tools/shared/composerContextReferences";import { threadTranscriptHeader } from "@t3tools/shared/threadTranscript";import * as DateTime from "effect/DateTime";-import { toKindScopedComposerContextId } from "./composerContextReferences";-export function threadContextAttachment(environmentId: EnvironmentId,transcript: OrchestrationV2ThreadTranscript,diff --git a/packages/shared/src/composerContextReferences.test.ts b/packages/shared/src/composerContextReferences.test.tsindex 8b59a6c261b..f79cd8b2bc5 100644--- a/packages/shared/src/composerContextReferences.test.ts+++ b/packages/shared/src/composerContextReferences.test.ts@@ -11,6 +11,8 @@ import {projectComposerContextForProvider,replaceComposerContextReferences,sanitizeComposerContextLabel,+ toComposerContextId,+ toKindScopedComposerContextId,} from "./composerContextReferences.ts";const ctx = (value: string) => value as ComposerContextId;@@ -298,3 +300,27 @@ describe("provider projection", () => {expect(projected).not.toContain("boom");});});++describe("toComposerContextId", () => {+ it("keeps same-kind raw IDs distinct when one contains the namespace prefix", () => {+ expect(toKindScopedComposerContextId("image", "x")).toBe("image_x");+ expect(toKindScopedComposerContextId("image", "image_x")).toBe("image_image_x");+ });+ it("keeps ids that already fit the grammar and folds the rest deterministically", () => {+ expect(toComposerContextId("file-comment-1700-1")).toBe("file-comment-1700-1");+ const folded = toComposerContextId("pull-request-selection:src/a.ts:4-9");+ expect(folded).toMatch(/^pull-request-selection-src-a-ts-4-9-[0-9a-f]{16}$/);+ expect(toComposerContextId("pull-request-selection:src/a.ts:4-9")).toBe(folded);+ expect(toComposerContextId("pull-request-selection:src/b.ts:4-9")).not.toBe(folded);+ expect(toComposerContextId("::")).toMatch(/^ctx-[0-9a-f]{16}$/);+ });+ it("tells apart producer ids that agree past the slug's truncation point", () => {+ // The slug keeps 48 characters, so only the digest distinguishes these two.+ const shared = `pull-request-finding:${"a".repeat(60)}`;+ const first = toComposerContextId(`${shared}:1`);+ const second = toComposerContextId(`${shared}:2`);++ expect(first).not.toBe(second);+ expect(first.length).toBeLessThanOrEqual(128);+ });+});diff --git a/packages/shared/src/composerContextReferences.ts b/packages/shared/src/composerContextReferences.tsindex ae586489908..ef83dac8c9f 100644--- a/packages/shared/src/composerContextReferences.ts+++ b/packages/shared/src/composerContextReferences.ts@@ -23,6 +23,47 @@ const CONTEXT_LINK = new RegExp("g",);+/**+ * Two independent FNV-1a passes, one forward and one with a different offset basis over the+ * reversed input. 64 bits of digest, because the slug in front of it is truncated: two long+ * producer ids that agree on their first 48 characters are told apart by this alone.+ */+function fnv1a64(value: string): string {+ let forward = 0x811c9dc5;+ let reverse = 0x9dc5811c;+ for (let index = 0; index < value.length; index += 1) {+ forward ^= value.charCodeAt(index);+ forward = Math.imul(forward, 0x01000193) >>> 0;+ reverse ^= value.charCodeAt(value.length - 1 - index);+ reverse = Math.imul(reverse, 0x01000193) >>> 0;+ }+ return `${forward.toString(16).padStart(8, "0")}${reverse.toString(16).padStart(8, "0")}`;+}++/**+ * Producers mint ids in their own grammars (`pull-request-finding:42`,+ * `file-comment-<ms>-<n>`). A context id must survive a Markdown link and the wire+ * schema, so anything outside `[a-z0-9_-]` is folded into a readable slug plus a hash of+ * the original. Deterministic, so the same producer id always maps to the same context id.+ */+export function toComposerContextId(producerId: string): ComposerContextId {+ if (CONTEXT_ID_PATTERN.test(producerId)) return producerId as ComposerContextId;+ const slug = producerId+ .replace(/[^a-z0-9_-]+/gi, "-")+ .replace(/^-+|-+$/g, "")+ .slice(0, 48);+ return `${slug || "ctx"}-${fnv1a64(producerId)}` as ComposerContextId;+}++/** Raw producer IDs are always scoped, even when they already start with the kind name. */+export function toKindScopedComposerContextId(+ kind: ComposerContextKind,+ producerId: string,+): ComposerContextId {+ const prefix = `${kind}_`;+ return toComposerContextId(`${prefix}${producerId}`);+}+export function formatComposerContextHref(kind: ComposerContextKind, contextId: ComposerContextId) {return `${COMPOSER_CONTEXT_HREF_PREFIX}${kind}/${contextId}`;}