Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(search): oversized thread queries no longer crash clients (#6633)
Simone421088c27367
diff --git a/packages/client-runtime/src/state/threadSearch.test.ts b/packages/client-runtime/src/state/threadSearch.test.tsindex 9e6c682e7..ef4b401d4 100644--- a/packages/client-runtime/src/state/threadSearch.test.ts+++ b/packages/client-runtime/src/state/threadSearch.test.ts@@ -49,6 +49,51 @@ it("encodes scoped thread keys without delimiter collisions", () => {expect(first).not.toBe(second);});+it("accepts search keys at the maximum decoded query length", () => {+ const queries: string[] = [];+ const searchAtom = createThreadSearchResultsAtomFamily<Error>({+ getSearchAtom: (_environmentId, query) => {+ queries.push(query);+ return Atom.make(AsyncResult.success({ matches: [] }));+ },+ labelPrefix: "test:thread-search",+ });+ const registry = AtomRegistry.make();+ const query = "a".repeat(200);++ try {+ registry.get(searchAtom(makeThreadSearchKey([envA], query)));+ registry.get(searchAtom(makeThreadSearchKey([envA], ` ${query} `)));++ expect(queries).toEqual([query, query]);+ } finally {+ registry.dispose();+ }+});++it("ignores invalid search keys", () => {+ let searchCount = 0;+ const searchAtom = createThreadSearchResultsAtomFamily<Error>({+ getSearchAtom: () => {+ searchCount += 1;+ return Atom.make(AsyncResult.success({ matches: [] }));+ },+ labelPrefix: "test:thread-search",+ });+ const registry = AtomRegistry.make();++ try {+ const state = registry.get(searchAtom(makeThreadSearchKey([envA], "a".repeat(201))));+ const malformedState = registry.get(searchAtom("not json{{"));++ expect(state).toEqual({ matches: [], isLoading: false });+ expect(malformedState).toEqual({ matches: [], isLoading: false });+ expect(searchCount).toBe(0);+ } finally {+ registry.dispose();+ }+});+it("merges successful environments and silently ignores failures", () => {const result: OrchestrationSearchThreadsResult = {matches: [diff --git a/packages/client-runtime/src/state/threadSearch.ts b/packages/client-runtime/src/state/threadSearch.tsindex b71186421..6dae630aa 100644--- a/packages/client-runtime/src/state/threadSearch.ts+++ b/packages/client-runtime/src/state/threadSearch.ts@@ -17,11 +17,10 @@ export interface ThreadSearchResultsState {readonly isLoading: boolean;}-const ThreadSearchKey = Schema.Tuple([- Schema.Array(EnvironmentId),- OrchestrationSearchThreadsInput.fields.query,-]);-const decodeThreadSearchKey = Schema.decodeUnknownSync(ThreadSearchKey);+const ThreadSearchKey = Schema.fromJsonString(+ Schema.Tuple([Schema.Array(EnvironmentId), OrchestrationSearchThreadsInput.fields.query]),+);+const decodeThreadSearchKey = Schema.decodeUnknownOption(ThreadSearchKey);export function makeThreadSearchKey(environmentIds: ReadonlyArray<EnvironmentId>,@@ -34,7 +33,7 @@ export function makeThreadSearchKey(}function parseThreadSearchKey(key: string) {- return decodeThreadSearchKey(JSON.parse(key));+ return decodeThreadSearchKey(key);}export function threadSearchMatchKey(@@ -44,9 +43,9 @@ export function threadSearchMatchKey(}/**- * Combines one search query atom per environment. Failed and disconnected- * environments contribute no content matches, preserving local title search- * as the compatibility fallback.+ * Combines one search query atom per environment. Invalid search keys, failed+ * requests, and disconnected environments contribute no content matches,+ * preserving local title search as the compatibility fallback.*/export function createThreadSearchResultsAtomFamily<E>(options: {readonly getSearchAtom: (@@ -57,7 +56,12 @@ export function createThreadSearchResultsAtomFamily<E>(options: {}) {return Atom.family((key: string) =>Atom.make((get): ThreadSearchResultsState => {- const [environmentIds, query] = parseThreadSearchKey(key);+ const parsedKey = parseThreadSearchKey(key);+ if (Option.isNone(parsedKey)) {+ return { matches: [], isLoading: false };+ }++ const [environmentIds, query] = parsedKey.value;const matches: EnvironmentThreadSearchMatch[] = [];let isLoading = false;