From 6a2d2466673c010a15cb530fa2a8f77b363e046d Mon Sep 17 00:00:00 2001 From: maria Date: Thu, 10 Sep 2026 23:36:40 -0300 Subject: [PATCH 01/63] fix(pr): update labels and reviewers without redundant reloads (#11117) --- .../pullRequest/PullRequestLabelPicker.tsx | 9 +- .../pullRequest/PullRequestReviewerPicker.tsx | 9 +- .../pullRequest/PullRequestSummaryTab.tsx | 2 - .../src/state/pullRequests.test.ts | 242 ++++++++++++++++++ .../client-runtime/src/state/pullRequests.ts | 205 +++++++++++++-- 5 files changed, 427 insertions(+), 40 deletions(-) diff --git a/apps/web/src/components/pullRequest/PullRequestLabelPicker.tsx b/apps/web/src/components/pullRequest/PullRequestLabelPicker.tsx index f1e5fc6a1da44..fe9062f83e559 100644 --- a/apps/web/src/components/pullRequest/PullRequestLabelPicker.tsx +++ b/apps/web/src/components/pullRequest/PullRequestLabelPicker.tsx @@ -33,15 +33,12 @@ export function PullRequestLabelPicker({ environmentId, reference, allowed, - onChanged, }: { environmentId: EnvironmentId; reference: PullRequestRef; /** False where the host would refuse this account's change. Disabled with the reason rather * than hidden, like the reviewer control beside it. */ allowed: boolean; - /** The detail carries the labels, so it is re-read once the host has taken the change. */ - onChanged: () => void; }) { const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); @@ -79,8 +76,6 @@ export function PullRequestLabelPicker({ }); return; } - onChanged(); - candidatesQuery.refresh(); }; return ( @@ -94,8 +89,8 @@ export function PullRequestLabelPicker({ query={query} onQueryChange={setQuery} searchLabel="Search labels" - isPending={candidatesQuery.isPending} - error={candidatesQuery.error} + isPending={candidatesQuery.isPending && candidatesQuery.data === null} + error={candidatesQuery.data === null ? candidatesQuery.error : null} candidates={candidates} emptyLabel="This repository has no labels." noMatchLabel="No label matches that." diff --git a/apps/web/src/components/pullRequest/PullRequestReviewerPicker.tsx b/apps/web/src/components/pullRequest/PullRequestReviewerPicker.tsx index a4da0d99514d0..ac4c83ad714ef 100644 --- a/apps/web/src/components/pullRequest/PullRequestReviewerPicker.tsx +++ b/apps/web/src/components/pullRequest/PullRequestReviewerPicker.tsx @@ -38,15 +38,12 @@ export function PullRequestReviewerPicker({ environmentId, reference, allowed, - onRequested, }: { environmentId: EnvironmentId; reference: PullRequestRef; /** False where the host would refuse this account's request, which is worth saying rather than * hiding: the control disabled with a reason answers the question its absence would raise. */ allowed: boolean; - /** The detail carries who is requested, so it is re-read once the host has taken the change. */ - onRequested: () => void; }) { const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); @@ -96,8 +93,6 @@ export function PullRequestReviewerPicker({ ? `Review request to ${candidate.login} taken back` : `Review requested from ${candidate.login}`, }); - onRequested(); - candidatesQuery.refresh(); }; return ( @@ -111,8 +106,8 @@ export function PullRequestReviewerPicker({ query={query} onQueryChange={setQuery} searchLabel="Search people with access" - isPending={candidatesQuery.isPending} - error={candidatesQuery.error} + isPending={candidatesQuery.isPending && candidatesQuery.data === null} + error={candidatesQuery.data === null ? candidatesQuery.error : null} candidates={candidates} emptyLabel="Nobody else has access to this repository." noMatchLabel="Nobody with access matches that." diff --git a/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx b/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx index fed932d457a58..9501d61a67e25 100644 --- a/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx @@ -683,7 +683,6 @@ export function PullRequestSummaryTab({ environmentId={environmentId} reference={reference} allowed={detail.viewerPermissions.requestReviewers} - onRequested={onRefresh} /> ) : null} @@ -718,7 +717,6 @@ export function PullRequestSummaryTab({ environmentId={environmentId} reference={reference} allowed={detail.viewerPermissions.labels !== false} - onChanged={onRefresh} /> ) : null} diff --git a/packages/client-runtime/src/state/pullRequests.test.ts b/packages/client-runtime/src/state/pullRequests.test.ts index fdd109d6e8c4c..6187cf2e7bd0b 100644 --- a/packages/client-runtime/src/state/pullRequests.test.ts +++ b/packages/client-runtime/src/state/pullRequests.test.ts @@ -1,5 +1,6 @@ import { EnvironmentId, ProjectId, WS_METHODS, type PullRequestStack } from "@t3tools/contracts"; import { expect, it } from "@effect/vitest"; +import * as Data from "effect/Data"; import * as Effect from "effect/Effect"; import * as Latch from "effect/Latch"; import * as Layer from "effect/Layer"; @@ -26,6 +27,8 @@ import { import { PullRequestDiffLoader } from "./pullRequestDiffHttp.ts"; import { executeAtomQuery } from "./runtime.ts"; +class MutationRefused extends Data.TaggedError("MutationRefused") {} + const TARGET = new PrimaryConnectionTarget({ environmentId: EnvironmentId.make("environment-1"), label: "Test environment", @@ -216,6 +219,245 @@ it.effect("refreshes pull request activity after a comment is updated", () => ), ); +it.effect("updates cached labels after successful edits without rereading the host", () => + Effect.scoped( + Effect.gen(function* () { + let detailReads = 0; + let candidateReads = 0; + let refuse = false; + let failDetail = false; + const existing = { name: "existing", color: "111111" }; + const addedLabel = { name: "new", color: "abcdef" }; + const detailRefreshStarted = yield* Latch.make(); + const releaseDetailRefresh = yield* Latch.make(); + const client = { + [WS_METHODS.pullRequestsSubscribeRefreshes]: () => Stream.never, + [WS_METHODS.pullRequestsDetail]: () => + Effect.gen(function* () { + detailReads++; + if (failDetail) { + yield* detailRefreshStarted.open; + yield* releaseDetailRefresh.await; + return yield* Effect.fail(new MutationRefused()); + } + return { title: "keep this title", labels: [existing] }; + }), + [WS_METHODS.pullRequestsLabelCandidates]: () => + Effect.sync(() => { + candidateReads++; + return { + candidates: [ + { ...existing, description: null, isApplied: true }, + { ...addedLabel, description: "description", isApplied: false }, + ], + truncated: false, + }; + }), + [WS_METHODS.pullRequestsSetLabels]: () => + refuse ? Effect.fail(new MutationRefused()) : Effect.void, + } as unknown as WsRpcProtocolClient; + const { atoms, registry } = yield* makeTestRuntime(client); + const target = { + environmentId: TARGET.environmentId, + input: { + projectId: ProjectId.make("project-1"), + repository: "acme/web", + number: 1, + host: "github.example.com", + }, + }; + const detail = atoms.detail(target); + const candidates = atoms.labelCandidates(target); + registry.mount(detail); + const unmountCandidates = registry.mount(candidates); + yield* AtomRegistry.getResult(registry, detail, { suspendOnWaiting: true }); + yield* AtomRegistry.getResult(registry, candidates, { suspendOnWaiting: true }); + + const added = yield* Effect.promise(() => + atoms.setLabels.run(registry, { + ...target, + input: { + host: target.input.host, + projectId: target.input.projectId, + repository: target.input.repository, + number: target.input.number, + labels: ["new"], + applied: true, + }, + }), + ); + expect(AsyncResult.isSuccess(added)).toBe(true); + expect(yield* AtomRegistry.getResult(registry, detail)).toEqual({ + title: "keep this title", + labels: [existing, addedLabel], + }); + unmountCandidates(); + registry.mount(atoms.labelCandidates(target)); + expect((yield* AtomRegistry.getResult(registry, candidates)).candidates[1]).toEqual({ + ...addedLabel, + description: "description", + isApplied: true, + }); + + for (const name of ["existing", "new"]) { + refuse = name === "new"; + const result = yield* Effect.promise(() => + atoms.setLabels.run(registry, { + ...target, + input: { ...target.input, labels: [name], applied: false }, + }), + ); + expect(result._tag).toBe(refuse ? "Failure" : "Success"); + expect((yield* AtomRegistry.getResult(registry, detail)).labels).toEqual([addedLabel]); + expect((yield* AtomRegistry.getResult(registry, candidates)).candidates).toMatchObject([ + { name: "existing", isApplied: false }, + { name: "new", isApplied: true }, + ]); + } + expect(detailReads).toBe(1); + expect(candidateReads).toBe(1); + + failDetail = true; + registry.refresh(detail); + yield* detailRefreshStarted.await; + expect(registry.get(detail).waiting).toBe(true); + expect(Option.getOrThrow(AsyncResult.value(registry.get(detail))).labels).toEqual([ + addedLabel, + ]); + yield* releaseDetailRefresh.open; + yield* Effect.exit(AtomRegistry.getResult(registry, detail, { suspendOnWaiting: true })); + expect(AsyncResult.isFailure(registry.get(detail))).toBe(true); + expect(Option.getOrThrow(AsyncResult.value(registry.get(detail))).labels).toEqual([ + addedLabel, + ]); + failDetail = false; + registry.refresh(detail); + expect( + (yield* AtomRegistry.getResult(registry, detail, { suspendOnWaiting: true })).labels, + ).toEqual([existing]); + expect(detailReads).toBe(3); + }), + ), +); + +it.effect("updates reviewer requests and enriched reviewers without rereading the host", () => + Effect.scoped( + Effect.gen(function* () { + let reads = 0; + let refuse = false; + const actor = { login: "reviewer", name: "Reviewer", avatarUrl: null }; + const hostActor = { ...actor, login: "Reviewer" }; + let hostRequested = false; + let reviewed = false; + let pauseActivity = false; + const activityStarted = yield* Latch.make(); + const client = { + [WS_METHODS.pullRequestsSubscribeRefreshes]: () => Stream.never, + [WS_METHODS.pullRequestsDetail]: () => + Effect.sync(() => { + reads++; + return { reviewers: hostRequested ? [hostActor] : [] }; + }), + [WS_METHODS.pullRequestsActivity]: () => + Effect.gen(function* () { + reads++; + if (pauseActivity) { + pauseActivity = false; + yield* activityStarted.open; + return yield* Effect.never; + } + return { + reviewers: hostRequested ? [hostActor] : [], + comments: reviewed ? [{ kind: "review-comment", author: hostActor }] : [], + }; + }), + [WS_METHODS.pullRequestsReviewerCandidates]: (input: { number: number }) => + input.number === 2 + ? Effect.never + : Effect.sync(() => { + reads++; + return { + candidates: [{ ...actor, id: "12", kind: "user", isRequested: false }], + truncated: false, + }; + }), + [WS_METHODS.pullRequestsRequestReviewers]: (input: { requested: boolean }) => + refuse + ? Effect.fail(new MutationRefused()) + : Effect.sync(() => { + hostRequested = input.requested; + }), + } as unknown as WsRpcProtocolClient; + const { atoms, registry } = yield* makeTestRuntime(client); + const target = { + environmentId: TARGET.environmentId, + input: { + projectId: ProjectId.make("project-1"), + repository: "acme/web", + number: 1, + host: "github.example.com", + }, + }; + const detail = atoms.detail(target); + const activity = atoms.activity(target); + const candidates = atoms.reviewerCandidates(target); + registry.mount(detail); + registry.mount(activity); + registry.mount(candidates); + yield* AtomRegistry.getResult(registry, detail, { suspendOnWaiting: true }); + yield* AtomRegistry.getResult(registry, activity, { suspendOnWaiting: true }); + yield* AtomRegistry.getResult(registry, candidates, { suspendOnWaiting: true }); + const request = (requested: boolean, reference = target) => + Effect.promise(() => + atoms.requestReviewers.run(registry, { + ...reference, + input: { ...reference.input, reviewers: [{ id: "12", kind: "user" }], requested }, + }), + ); + for (const operation of ["request", "refuse", "remove"]) { + refuse = operation === "refuse"; + expect((yield* request(operation === "request"))._tag).toBe(refuse ? "Failure" : "Success"); + const expected = operation === "remove" ? [] : [actor]; + expect((yield* AtomRegistry.getResult(registry, detail)).reviewers).toEqual(expected); + expect((yield* AtomRegistry.getResult(registry, activity)).reviewers).toEqual(expected); + expect((yield* AtomRegistry.getResult(registry, candidates)).candidates).toMatchObject([ + { isRequested: operation !== "remove" }, + ]); + } + expect(reads).toBe(3); + // A slow activity read started before the write must not hide the new request. + pauseActivity = true; + registry.refresh(activity); + yield* activityStarted.await; + expect(AsyncResult.isSuccess(yield* request(true))).toBe(true); + expect( + (yield* AtomRegistry.getResult(registry, activity, { suspendOnWaiting: true })).reviewers, + ).toEqual([hostActor]); + expect(reads).toBe(5); + expect(AsyncResult.isSuccess(yield* request(false))).toBe(true); + expect((yield* AtomRegistry.getResult(registry, activity)).reviewers).toEqual([]); + + reviewed = true; + yield* request(true); + registry.refresh(activity); + yield* AtomRegistry.getResult(registry, activity, { suspendOnWaiting: true }); + yield* request(false); + expect((yield* AtomRegistry.getResult(registry, activity)).reviewers).toEqual([hostActor]); + + // A caller without an open picker still needs authoritative reviewer identities. + const otherTarget = { ...target, input: { ...target.input, number: 2 } }; + const otherDetail = atoms.detail(otherTarget); + registry.mount(otherDetail); + yield* AtomRegistry.getResult(registry, otherDetail, { suspendOnWaiting: true }); + yield* request(true, otherTarget); + expect( + (yield* AtomRegistry.getResult(registry, otherDetail, { suspendOnWaiting: true })) + .reviewers, + ).toEqual([hostActor]); + }), + ), +); + it.effect("refreshes stack state after reopening and head SHAs after a turn", () => Effect.scoped( Effect.gen(function* () { diff --git a/packages/client-runtime/src/state/pullRequests.ts b/packages/client-runtime/src/state/pullRequests.ts index c9d23d02bf553..26a0c08c13edf 100644 --- a/packages/client-runtime/src/state/pullRequests.ts +++ b/packages/client-runtime/src/state/pullRequests.ts @@ -1,7 +1,10 @@ import { WS_METHODS, + type EnvironmentId, + type PullRequestActor, type PullRequestDetail, type PullRequestDiffInput, + type PullRequestRef, type PullRequestSummary, type VcsStatusResult, } from "@t3tools/contracts"; @@ -9,7 +12,7 @@ import * as Data from "effect/Data"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; import * as SubscriptionRef from "effect/SubscriptionRef"; -import { Atom } from "effect/unstable/reactivity"; +import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity"; import { createAtomCommandScheduler, @@ -36,6 +39,52 @@ export class EnvironmentHttpConnectionNotReadyError extends Data.TaggedError( const LINKED_PULL_REQUEST_IDLE_TTL_MS = 5_000; +/** Keep confirmed edits on the same cached reference regardless of input property order. */ +function writableQueryFamily( + family: (target: { + readonly environmentId: EnvironmentId; + readonly input: PullRequestRef; + }) => Atom.Atom>, +) { + const writable = Atom.family((source: Atom.Atom>) => + Atom.writable( + (get) => { + const result = get(source); + if (result._tag === "Success" && !result.waiting) return result; + const previous = get.self>(); + const value = Option.flatMap(previous, AsyncResult.value); + if (Option.isNone(value)) return result; + return result._tag === "Failure" + ? AsyncResult.failureWithPrevious(result.cause, { previous, waiting: result.waiting }) + : AsyncResult.success(value.value, result); + }, + (context, value: AsyncResult.AsyncResult) => context.setSelf(value), + (refresh) => refresh(source), + ).pipe(Atom.setIdleTTL(5 * 60_000)), + ); + return ({ + environmentId, + input: { projectId, host, repository, number }, + }: Parameters[0]) => + writable( + family({ + environmentId, + input: { projectId, ...(host === undefined ? {} : { host }), repository, number }, + }), + ); +} + +/** Restart pre-mutation reads before patching so they cannot restore stale values. */ +function updateCached( + registry: AtomRegistry.AtomRegistry, + atom: Atom.Writable>, + update: (value: A) => A, + refresh = false, +) { + if (refresh || registry.get(atom).waiting) registry.refresh(atom); + registry.update(atom, AsyncResult.map(update)); +} + function createPullRequestRefreshAtomFamily( runtime: Atom.AtomRuntime, ) { @@ -92,7 +141,7 @@ export function pullRequestDetailToVcsStatus( /** * Reopening a PR within a minute reuses detail and activity. Explicit refreshes and * turn notifications still revalidate. Mutations run serially per environment: actions on the same - * pull request are order-sensitive, and the detail view refetches after each one. + * pull request are order-sensitive. Confirmed label and reviewer edits update cached state. */ export function createPullRequestEnvironmentAtoms( runtime: Atom.AtomRuntime, @@ -103,12 +152,36 @@ export function createPullRequestEnvironmentAtoms( mode: "serial", key: ({ environmentId }: { readonly environmentId: string }) => environmentId, } as const; - const activity = createEnvironmentRpcQueryAtomFamily(runtime, { - label: "environment-data:pull-requests:activity", - tag: WS_METHODS.pullRequestsActivity, - staleTimeMs: 60_000, - refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }), - }); + const activity = writableQueryFamily( + createEnvironmentRpcQueryAtomFamily(runtime, { + label: "environment-data:pull-requests:activity", + tag: WS_METHODS.pullRequestsActivity, + staleTimeMs: 60_000, + refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }), + }), + ); + const detail = writableQueryFamily( + createEnvironmentRpcQueryAtomFamily(runtime, { + label: "environment-data:pull-requests:detail", + tag: WS_METHODS.pullRequestsDetail, + staleTimeMs: 60_000, + refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }), + }), + ); + const labelCandidates = writableQueryFamily( + createEnvironmentRpcQueryAtomFamily(runtime, { + label: "environment-data:pull-requests:label-candidates", + tag: WS_METHODS.pullRequestsLabelCandidates, + staleTimeMs: 60_000, + }), + ); + const reviewerCandidates = writableQueryFamily( + createEnvironmentRpcQueryAtomFamily(runtime, { + label: "environment-data:pull-requests:reviewer-candidates", + tag: WS_METHODS.pullRequestsReviewerCandidates, + staleTimeMs: 60_000, + }), + ); return { refreshes, linkedThreads: createEnvironmentRpcQueryAtomFamily(runtime, { @@ -137,12 +210,7 @@ export function createPullRequestEnvironmentAtoms( staleTimeMs: 60_000, refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }), }), - detail: createEnvironmentRpcQueryAtomFamily(runtime, { - label: "environment-data:pull-requests:detail", - tag: WS_METHODS.pullRequestsDetail, - staleTimeMs: 60_000, - refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }), - }), + detail, activity, threadComments: createEnvironmentRpcCommand(runtime, { label: "environment-data:pull-requests:thread-comments", @@ -241,28 +309,117 @@ export function createPullRequestEnvironmentAtoms( * for a minute, because who has access to a repository changes far more slowly than the * change request it is being read for. */ - reviewerCandidates: createEnvironmentRpcQueryAtomFamily(runtime, { - label: "environment-data:pull-requests:reviewer-candidates", - tag: WS_METHODS.pullRequestsReviewerCandidates, - staleTimeMs: 60_000, - }), + reviewerCandidates, requestReviewers: createEnvironmentRpcCommand(runtime, { label: "environment-data:pull-requests:request-reviewers", tag: WS_METHODS.pullRequestsRequestReviewers, scheduler: commandScheduler, concurrency: serialPerEnvironment, + onSuccess: (target, registry) => + Effect.sync(() => { + const { reviewers, requested } = target.input; + const candidatesAtom = reviewerCandidates(target); + const candidates = Option.getOrNull(AsyncResult.value(registry.get(candidatesAtom))); + const selected = + candidates?.candidates.filter((candidate) => + reviewers.some( + (reviewer) => reviewer.id === candidate.id && reviewer.kind === candidate.kind, + ), + ) ?? []; + const missingIdentities = selected.length < reviewers.length; + updateCached(registry, candidatesAtom, (value) => ({ + ...value, + candidates: value.candidates.map((candidate) => + selected.includes(candidate) ? { ...candidate, isRequested: requested } : candidate, + ), + })); + const selectedLogins = new Set( + selected.map((candidate) => candidate.login.toLowerCase()), + ); + const updateReviewers = ( + actors: ReadonlyArray, + keep = (_actor: PullRequestActor) => false, + ) => + requested + ? [ + ...actors, + ...selected + .filter( + (candidate) => + !actors.some( + (actor) => actor.login.toLowerCase() === candidate.login.toLowerCase(), + ), + ) + .map(({ login, name, avatarUrl }) => ({ login, name, avatarUrl })), + ] + : actors.filter( + (actor) => !selectedLogins.has(actor.login.toLowerCase()) || keep(actor), + ); + updateCached( + registry, + detail(target), + (value) => ({ + ...value, + reviewers: updateReviewers(value.reviewers), + }), + missingIdentities, + ); + updateCached( + registry, + activity(target), + (value) => ({ + ...value, + reviewers: + value.reviewers === undefined + ? undefined + : updateReviewers(value.reviewers, (actor) => + value.comments.some( + (comment) => + (comment.kind === "review" || comment.kind === "review-comment") && + comment.author?.login.toLowerCase() === actor.login.toLowerCase(), + ), + ), + }), + missingIdentities, + ); + }), }), /** Read when the label menu opens, and kept for a minute, like the reviewer candidates. */ - labelCandidates: createEnvironmentRpcQueryAtomFamily(runtime, { - label: "environment-data:pull-requests:label-candidates", - tag: WS_METHODS.pullRequestsLabelCandidates, - staleTimeMs: 60_000, - }), + labelCandidates, setLabels: createEnvironmentRpcCommand(runtime, { label: "environment-data:pull-requests:set-labels", tag: WS_METHODS.pullRequestsSetLabels, scheduler: commandScheduler, concurrency: serialPerEnvironment, + onSuccess: (target, registry) => + Effect.sync(() => { + const { labels, applied } = target.input; + const candidatesAtom = labelCandidates(target); + const candidates = Option.getOrNull(AsyncResult.value(registry.get(candidatesAtom))); + const names = new Set(labels); + updateCached(registry, candidatesAtom, (value) => ({ + ...value, + candidates: value.candidates.map((candidate) => + names.has(candidate.name) ? { ...candidate, isApplied: applied } : candidate, + ), + })); + updateCached(registry, detail(target), (value) => ({ + ...value, + labels: applied + ? [ + ...value.labels, + ...labels + .filter((name) => !value.labels.some((label) => label.name === name)) + .map((name) => ({ + name, + color: + candidates?.candidates.find((candidate) => candidate.name === name) + ?.color ?? null, + })), + ] + : value.labels.filter((label) => !names.has(label.name)), + })); + }), }), setThreadResolution: createEnvironmentRpcCommand(runtime, { label: "environment-data:pull-requests:set-thread-resolution", From 27eb79dc719d70a99db495b0cb20bd6e3ac10b8d Mon Sep 17 00:00:00 2001 From: maria Date: Thu, 10 Sep 2026 23:38:42 -0300 Subject: [PATCH 02/63] fix(chat): fold question answers into tool activity (#11014) --- .../threads/QuestionAnswerHistory.tsx | 13 +- .../src/features/threads/thread-work-log.tsx | 21 +- apps/mobile/src/lib/threadActivity.ts | 39 ++-- .../ActivityPayloadProjection.ts | 6 +- .../components/chat/MessagesTimeline.test.tsx | 14 +- .../src/components/chat/MessagesTimeline.tsx | 79 ++++++-- apps/web/src/session-logic.ts | 18 +- packages/client-runtime/package.json | 4 + .../client-runtime/src/work-log/userInput.ts | 186 ++++++++++++++++++ packages/shared/src/toolActivity.ts | 33 ++++ 10 files changed, 368 insertions(+), 45 deletions(-) create mode 100644 packages/client-runtime/src/work-log/userInput.ts diff --git a/apps/mobile/src/features/threads/QuestionAnswerHistory.tsx b/apps/mobile/src/features/threads/QuestionAnswerHistory.tsx index 2ff0a3b370f8a..2a08ee65bb082 100644 --- a/apps/mobile/src/features/threads/QuestionAnswerHistory.tsx +++ b/apps/mobile/src/features/threads/QuestionAnswerHistory.tsx @@ -4,6 +4,7 @@ import type { UserInputAttachments, } from "@t3tools/contracts"; import { Image, Linking, Pressable, View } from "react-native"; +import { getQuestionAnswerText } from "@t3tools/client-runtime/work-log/user-input"; import { AppText as Text } from "../../components/AppText"; import { useAssetUrl } from "../../state/assets"; @@ -41,6 +42,7 @@ export function QuestionAnswerHistory(props: { {[ ...new Set([ + ...Object.keys(props.answer.questionTextById ?? {}), ...Object.keys(props.answer.answers), ...Object.keys(props.answer.attachmentsByQuestionId), ]), @@ -51,12 +53,11 @@ export function QuestionAnswerHistory(props: { {props.answer.questionTextById[questionId]} ) : null} - - {[props.answer.answers[questionId]] - .flat() - .filter((value): value is string => typeof value === "string") - .join(", ")} - + {getQuestionAnswerText(props.answer.answers[questionId]) ? ( + + {getQuestionAnswerText(props.answer.answers[questionId])} + + ) : null} {(props.answer.attachmentsByQuestionId[questionId] ?? []).map((attachment) => ( {displayText} + {answerPreview ? ( + {` ${answerPreview}`} + ) : null} )} diff --git a/apps/mobile/src/lib/threadActivity.ts b/apps/mobile/src/lib/threadActivity.ts index c7d27af11d5ed..b7d5018edd989 100644 --- a/apps/mobile/src/lib/threadActivity.ts +++ b/apps/mobile/src/lib/threadActivity.ts @@ -1,4 +1,5 @@ import * as Option from "effect/Option"; +import { foldUserInputActivities } from "@t3tools/client-runtime/work-log/user-input"; import * as Schema from "effect/Schema"; import { requestKindFromRequestType, @@ -405,7 +406,7 @@ function deriveWorkLogEntries( ): DerivedWorkLogEntry[] { const ordered = Arr.sort(activities, activityOrder); const entries: DerivedWorkLogEntry[] = []; - for (const activity of ordered) { + for (const activity of foldUserInputActivities(ordered)) { if (activity.tone !== "error" && isWorktreeSetupActivity(activity.kind)) continue; if (activity.kind === "tool.started") continue; // Like web: an agent's task.started row anchors its batch. It has a fixed @@ -936,6 +937,7 @@ function workEntryStatus(entry: WorkLogEntry): ThreadFeedActivity["status"] { function workEntryIcon(entry: DerivedWorkLogEntry): ThreadFeedActivity["icon"] { if (entry.agentSpawn) return "agent"; if ( + entry.questionAnswer || entry.sourceActivityKind === "user-input.requested" || entry.sourceActivityKind === "user-input.resolved" ) { @@ -2184,21 +2186,30 @@ export function buildThreadFeed( : loadedMessages; const oldestLoadedMessageCreatedAt = options?.loadedMessages !== undefined ? (loadedMessages[0]?.createdAt ?? null) : null; - const activityEntries = getThreadFeedActivityEntries(thread.activities); + const activityEntries = getThreadFeedActivityEntries(thread.activities).filter( + (entry) => + oldestLoadedMessageCreatedAt === null || entry.createdAt >= oldestLoadedMessageCreatedAt, + ); + const foldedAnswerMessageIds = new Set( + activityEntries.flatMap((entry) => + entry.activity.workEntry.questionAnswer + ? [`async-answer:${entry.activity.workEntry.questionAnswer.requestId}`] + : [], + ), + ); const entries = Arr.sortWith( [ - ...messages.map((message) => { - let entry = messageEntriesCache.get(message); - if (!entry) { - entry = { type: "message", id: message.id, createdAt: message.createdAt, message }; - messageEntriesCache.set(message, entry); - } - return entry; - }), - ...activityEntries.filter( - (entry) => - oldestLoadedMessageCreatedAt === null || entry.createdAt >= oldestLoadedMessageCreatedAt, - ), + ...messages + .filter((message) => message.role !== "user" || !foldedAnswerMessageIds.has(message.id)) + .map((message) => { + let entry = messageEntriesCache.get(message); + if (!entry) { + entry = { type: "message", id: message.id, createdAt: message.createdAt, message }; + messageEntriesCache.set(message, entry); + } + return entry; + }), + ...activityEntries, ], (s) => new Date(s.createdAt), Order.Date, diff --git a/apps/server/src/orchestration/ActivityPayloadProjection.ts b/apps/server/src/orchestration/ActivityPayloadProjection.ts index 98294e63b35c6..43db2cfcc4ccb 100644 --- a/apps/server/src/orchestration/ActivityPayloadProjection.ts +++ b/apps/server/src/orchestration/ActivityPayloadProjection.ts @@ -1,3 +1,4 @@ +import { projectQuestionToolInput } from "@t3tools/shared/toolActivity"; import type { OrchestrationEvent, OrchestrationThreadActivity, @@ -370,18 +371,19 @@ export function projectActivityPayload( payload.status === "completed" && (itemStatus === "failed" || itemStatus === "declined") ? { ...payload, status: itemStatus } : payload; + const questionInput = projectQuestionToolInput(data, payload.title); if (payload.itemType === "mcp_tool_call") { return { ...activity, payload: { ...projectedPayload, - data: projectMcpToolCallData(data), + data: { ...projectMcpToolCallData(data), ...questionInput }, }, }; } - const projectedData: Record = {}; + const projectedData: Record = { ...questionInput }; const item = projectCommandData(data); if (item) { projectedData.item = item; diff --git a/apps/web/src/components/chat/MessagesTimeline.test.tsx b/apps/web/src/components/chat/MessagesTimeline.test.tsx index c47402f4204b0..07252d6ab7fbf 100644 --- a/apps/web/src/components/chat/MessagesTimeline.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.test.tsx @@ -348,12 +348,24 @@ describe("MessagesTimeline", () => { }); const toggle = renderer!.root.findByProps({ "aria-expanded": false }); await act(() => toggle.props.onClick()); + const questionToggle = renderer!.root.find( + (node) => + node.props["aria-label"]?.startsWith("Question answer submitted:") && + node.props["aria-expanded"] === false, + ); + expect(questionToggle.props["aria-label"]).toContain( + Object.values(answers)[0] ?? "spec.txt", + ); + expect(JSON.stringify(renderer!.toJSON())).not.toContain("Provide a spec"); + await act(() => questionToggle.props.onClick()); const markup = JSON.stringify(renderer!.toJSON()); expect(markup.match(/Provide a spec/g)).toHaveLength(1); - expect(markup.match(/spec\.txt/g)).toHaveLength(1); + expect(markup).toContain("spec.txt"); expect(markup).toContain("Provide a screenshot"); expect(markup).toContain("shot.png"); for (const answer of Object.values(answers)) expect(markup).toContain(answer); + await act(() => questionToggle.props.onClick()); + expect(JSON.stringify(renderer!.toJSON())).not.toContain("Provide a spec"); } finally { await act(() => renderer?.unmount()); } diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index 3544e87817bdc..13c1e1dc61b32 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -1,4 +1,9 @@ import { GitPullRequestIcon } from "lucide-react"; +import { + getQuestionAnswerPreview, + getQuestionAnswerText, + hasQuestionAnswer, +} from "@t3tools/client-runtime/work-log/user-input"; import { type AssistantCitation, type EnvironmentId, @@ -2115,7 +2120,7 @@ function LiveActivityRow({ active = false, shimmer = false, }: { - label: string; + label: ReactNode; iconName?: WorkEntryIconName; toolIcon?: ToolActivityIcon | undefined; failed?: boolean; @@ -2155,7 +2160,7 @@ function LiveActivityContent({ active = false, highlighted = false, }: { - label: string; + label: ReactNode; iconName: WorkEntryIconName | undefined; toolIcon?: ToolActivityIcon | undefined; failed?: boolean; @@ -2213,7 +2218,25 @@ function LiveWorkEntryTimelineRow({ row }: { row: Extract ctx.onToggleWorkGroup(row.groupId, row.id)} > + {label} + + {getQuestionAnswerPreview(row.entry.questionAnswer)} + + + ) : ( + label + ) + } iconName={workEntryIconName(row.entry)} toolIcon={row.entry.toolIcon ?? row.entry.toolSource?.icon} failed={failed} @@ -3148,6 +3171,7 @@ const toolCallExpandedBodyClassName = function workEntryIconName(workEntry: TimelineWorkEntry): WorkEntryIconName { if ( + workEntry.questionAnswer || workEntry.sourceActivityKind === "user-input.requested" || workEntry.sourceActivityKind === "user-input.resolved" ) { @@ -3325,9 +3349,10 @@ const PlainWorkEntryRow = memo(function PlainWorkEntryRow(props: { showWarningIndicator || showDestructiveRowStyle ? undefined : (workEntry.toolIcon ?? workEntry.toolSource?.icon); - const previewText = workEntry.questionAnswer - ? "Question answer submitted" - : (displayLabel ?? workEntryDisplayLabel(workEntry, workspaceRoot)); + const previewText = displayLabel ?? workEntryDisplayLabel(workEntry, workspaceRoot); + const answerPreview = workEntry.questionAnswer + ? getQuestionAnswerPreview(workEntry.questionAnswer) + : null; const viewedImagePath = workEntryViewedImagePath(workEntry); const viewedImage = viewedImagePath && threadRef @@ -3337,6 +3362,7 @@ const PlainWorkEntryRow = memo(function PlainWorkEntryRow(props: { }) : null; const canExpand = + Boolean(workEntry.questionAnswer) || (showFailedIndicator && previewText.trim().length > 0) || (workEntry.itemType === "mcp_tool_call" && workEntry.toolData !== undefined) || Boolean( @@ -3374,9 +3400,10 @@ const PlainWorkEntryRow = memo(function PlainWorkEntryRow(props: { : workLogEntryIsToolLike(workEntry) ? "text-secondary-label" : "text-foreground/80"; + const accessiblePreview = [previewText, answerPreview].filter(Boolean).join(": "); const accessibleDisplayText = showFailedIndicator - ? `${previewText}, tool call failed` - : previewText; + ? `${accessiblePreview}, tool call failed` + : accessiblePreview; const rowToggleProps = canExpand ? { role: "button" as const, @@ -3421,7 +3448,7 @@ const PlainWorkEntryRow = memo(function PlainWorkEntryRow(props: {

{previewText} + {answerPreview ? ( + + {answerPreview} + + ) : null}

{showFailedIndicator && @@ -3470,10 +3511,10 @@ const PlainWorkEntryRow = memo(function PlainWorkEntryRow(props: { /> ) : null} - {workEntry.questionAnswer ? ( + {expanded && workEntry.questionAnswer ? ( ) : null} - {expanded && canExpand && expandedBody ? ( + {expanded && canExpand && expandedBody && !workEntry.questionAnswer ? (
{[ ...new Set([ + ...Object.keys(answer.questionTextById ?? {}), ...Object.keys(answer.answers), ...Object.keys(answer.attachmentsByQuestionId), ]), ].map((questionId) => (
{answer.questionTextById?.[questionId] ? ( -

{answer.questionTextById[questionId]}

+

+ {answer.questionTextById[questionId]} +

+ ) : null} + {getQuestionAnswerText(answer.answers[questionId]) ? ( +

+ {getQuestionAnswerText(answer.answers[questionId])} +

) : null} -

- {[answer.answers[questionId]] - .flat() - .filter((value): value is string => typeof value === "string") - .join(", ")} -

{(answer.attachmentsByQuestionId[questionId] ?? []).map((attachment) => { const url = urls[attachments.indexOf(attachment)]; diff --git a/apps/web/src/session-logic.ts b/apps/web/src/session-logic.ts index f84c978a04c83..0ffcc7fdd7c6d 100644 --- a/apps/web/src/session-logic.ts +++ b/apps/web/src/session-logic.ts @@ -3,6 +3,7 @@ import { type PendingApproval, } from "@t3tools/client-runtime/pending-requests"; import { UserInputAttachmentAnswerPayload } from "@t3tools/contracts"; +import { foldUserInputActivities } from "@t3tools/client-runtime/work-log/user-input"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import * as Arr from "effect/Array"; @@ -451,7 +452,7 @@ export function deriveWorkLogEntries( ): WorkLogEntry[] { const ordered = [...activities].toSorted(compareActivitiesByOrder); const entries: DerivedWorkLogEntry[] = []; - for (const activity of ordered) { + for (const activity of foldUserInputActivities(ordered)) { if (activity.tone !== "error" && isWorktreeSetupActivity(activity.kind)) continue; if (activity.kind === "tool.started") continue; // Agent task.started rows are CTA seeds: they carry the true spawn turn, @@ -1622,14 +1623,25 @@ export function deriveTimelineEntriesWithState( const entries = replaceStreamingTimelineMessages(messages, previous); if (entries !== null) return { messages, proposedPlans, workEntries, entries }; } + const foldedAnswerMessageIds = new Set( + workEntries.flatMap((entry) => + entry.questionAnswer ? [`async-answer:${entry.questionAnswer.requestId}`] : [], + ), + ); + const showMessage = (message: ChatMessage) => + message.role !== "user" || !foldedAnswerMessageIds.has(message.id); const canAppend = previous !== null && + !previous.entries.some((entry) => entry.kind === "message" && !showMessage(entry.message)) && hasExactArrayPrefix(previous.messages, messages) && hasExactArrayPrefix(previous.proposedPlans, proposedPlans) && hasExactArrayPrefix(previous.workEntries, workEntries); if (canAppend) { - const messageRows = messages.slice(previous.messages.length).map(timelineEntryFromMessage); + const messageRows = messages + .slice(previous.messages.length) + .filter(showMessage) + .map(timelineEntryFromMessage); const proposedPlanRows = proposedPlans .slice(previous.proposedPlans.length) .map(timelineEntryFromProposedPlan); @@ -1645,7 +1657,7 @@ export function deriveTimelineEntriesWithState( }; } - const messageRows = messages.map(timelineEntryFromMessage); + const messageRows = messages.filter(showMessage).map(timelineEntryFromMessage); const proposedPlanRows = proposedPlans.map(timelineEntryFromProposedPlan); const workRows = workEntries.map(timelineEntryFromWork); return { diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.json index d0bf4aad8e9e6..2a5eb2c7fe932 100644 --- a/packages/client-runtime/package.json +++ b/packages/client-runtime/package.json @@ -223,6 +223,10 @@ "types": "./src/state/vcs.ts", "default": "./src/state/vcs.ts" }, + "./work-log/user-input": { + "types": "./src/work-log/userInput.ts", + "default": "./src/work-log/userInput.ts" + }, "./work-log/presentation": { "types": "./src/work-log/presentation.ts", "default": "./src/work-log/presentation.ts" diff --git a/packages/client-runtime/src/work-log/userInput.ts b/packages/client-runtime/src/work-log/userInput.ts new file mode 100644 index 0000000000000..355083c048361 --- /dev/null +++ b/packages/client-runtime/src/work-log/userInput.ts @@ -0,0 +1,186 @@ +import { projectQuestionToolInput } from "@t3tools/shared/toolActivity"; +import { + type OrchestrationThreadActivity, + UserInputAttachmentAnswerPayload, +} from "@t3tools/contracts"; +import * as Schema from "effect/Schema"; + +function record(value: unknown): Record | undefined { + return value !== null && typeof value === "object" && !Array.isArray(value) + ? (value as Record) + : undefined; +} + +const isQuestionAnswer = Schema.is(UserInputAttachmentAnswerPayload); + +function displayOptionAnswer(value: unknown, labels: ReadonlyMap): unknown { + if (typeof value === "string") return labels.get(value) ?? value; + if (Array.isArray(value)) return value.map((answer) => displayOptionAnswer(answer, labels)); + const nested = record(value); + return nested && "answers" in nested + ? { ...nested, answers: displayOptionAnswer(nested.answers, labels) } + : value; +} + +function questionFingerprint( + turnId: string, + questions: ReadonlyArray, +): string | undefined { + const texts = questions.map((question) => (typeof question === "string" ? question.trim() : "")); + return texts.length > 0 && texts.every(Boolean) + ? JSON.stringify([turnId, texts.toSorted()]) + : undefined; +} + +function withoutDuplicateQuestionTools( + activities: ReadonlyArray, +): ReadonlyArray { + const questions = new Set(); + for (const activity of activities) { + if (activity.kind !== "user-input.answer-submitted" || !activity.turnId) continue; + const payload = record(activity.payload); + const texts = Object.values(record(payload?.questionTextById) ?? {}); + const fingerprint = questionFingerprint(activity.turnId, texts); + if (fingerprint) questions.add(fingerprint); + } + if (questions.size === 0) return activities; + const duplicateToolIds = new Set(); + for (const activity of activities) { + if (!activity.kind.startsWith("tool.") || !activity.turnId) continue; + const payload = record(activity.payload); + if (typeof payload?.toolCallId !== "string") continue; + const input = projectQuestionToolInput(record(payload.data) ?? {}, payload.title).input; + if (!input) continue; + const fingerprint = questionFingerprint( + activity.turnId, + input.questions.map((question) => record(question)?.question), + ); + if (fingerprint && questions.has(fingerprint)) { + duplicateToolIds.add(JSON.stringify([activity.turnId, payload.toolCallId])); + } + } + return activities.filter((activity) => { + const payload = record(activity.payload); + const toolCallId = payload?.toolCallId; + return ( + activity.tone === "error" || + /^(failed|declined|stopped|cancelled)$/.test(String(payload?.status)) || + !activity.kind.startsWith("tool.") || + typeof toolCallId !== "string" || + !duplicateToolIds.has(JSON.stringify([activity.turnId, toolCallId])) + ); + }); +} + +/** Keep a question and its answer at the original tool position in the work log. */ +export function foldUserInputActivities( + activities: ReadonlyArray, +): ReadonlyArray { + const requests = new Map(); + for (const activity of activities) { + if ( + activity.kind !== "user-input.requested" && + activity.kind !== "user-input.resolved" && + activity.kind !== "user-input.answer-submitted" + ) + continue; + const requestId = record(activity.payload)?.requestId; + if (typeof requestId !== "string" || !requestId) continue; + const group = requests.get(requestId) ?? []; + group.push(activity); + requests.set(requestId, group); + } + const replacements = new Map(); + for (const [requestId, group] of requests) { + const payloads = group.map((activity) => record(activity.payload)!); + const questions = new Map>(); + const texts = new Map(); + for (const payload of payloads) { + for (const [id, text] of Object.entries(record(payload.questionTextById) ?? {})) + texts.set(id, text); + for (const value of Array.isArray(payload.questions) ? payload.questions : []) { + const question = record(value); + if (typeof question?.id !== "string") continue; + questions.set(question.id, question); + if (typeof question.question === "string") texts.set(question.id, question.question); + } + } + const questionTextById = Object.fromEntries(texts); + const submitted = group.findLast( + (activity) => + activity.kind === "user-input.answer-submitted" && + record(record(activity.payload)?.answers), + ); + const rawAnswers = + record(record(submitted?.payload)?.answers) ?? + payloads.map((payload) => record(payload.answers)).findLast(Boolean) ?? + {}; + const answers = Object.fromEntries( + Object.entries(rawAnswers).map(([id, value]) => { + const options = questions.get(id)?.options; + const labels = new Map(); + for (const candidate of Array.isArray(options) ? options : []) { + const option = record(candidate); + if (typeof option?.value === "string" && typeof option.label === "string") + labels.set(option.value, option.label); + } + return [id, displayOptionAnswer(value, labels)]; + }), + ); + const attachmentsByQuestionId = Object.fromEntries( + payloads.flatMap((payload) => Object.entries(record(payload.attachmentsByQuestionId) ?? {})), + ); + const answer = { requestId, questionTextById, answers, attachmentsByQuestionId }; + if (!isQuestionAnswer(answer)) continue; + const submittedAnswer = + Object.keys(answers).length > 0 || Object.keys(attachmentsByQuestionId).length > 0; + for (const activity of group) replacements.set(activity, null); + replacements.set(group[0]!, { + ...group[0]!, + kind: "user-input.answer-submitted", + tone: "tool", + summary: submittedAnswer + ? "User input submitted" + : group.some((activity) => activity.kind === "user-input.resolved") + ? "User input dismissed" + : "User input requested", + payload: answer, + }); + } + return withoutDuplicateQuestionTools( + activities.flatMap((activity) => { + const replacement = replacements.get(activity); + return replacement === null ? [] : [replacement ?? activity]; + }), + ); +} + +export function getQuestionAnswerText(value: unknown): string { + if (typeof value === "string") return value; + if (Array.isArray(value)) return value.map(getQuestionAnswerText).filter(Boolean).join(", "); + const nested = record(value); + return nested ? getQuestionAnswerText(nested.answers) : ""; +} + +export function getQuestionAnswerPreview(answer: UserInputAttachmentAnswerPayload): string { + const answers = Object.values(answer.answers).map(getQuestionAnswerText).filter(Boolean); + const attachments = Object.values(answer.attachmentsByQuestionId) + .flat() + .map((attachment) => attachment.name); + return ( + answers.length > 0 + ? answers.join(" · ") + : attachments.length > 0 + ? attachments.join(", ") + : Object.values(answer.questionTextById ?? {}).join(" · ") + ) + .replace(/\s+/g, " ") + .trim(); +} + +export function hasQuestionAnswer(answer: UserInputAttachmentAnswerPayload): boolean { + return ( + Object.values(answer.answers).some(getQuestionAnswerText) || + Object.values(answer.attachmentsByQuestionId).some((attachments) => attachments.length > 0) + ); +} diff --git a/packages/shared/src/toolActivity.ts b/packages/shared/src/toolActivity.ts index 2fd04e76696fb..8c1c65565f516 100644 --- a/packages/shared/src/toolActivity.ts +++ b/packages/shared/src/toolActivity.ts @@ -259,3 +259,36 @@ export function deriveToolActivityPresentation( summary: title ?? fallbackSummary, }; } + +export function projectQuestionToolInput(data: Record, title: unknown) { + const item = asRecord(data.item); + const toolName = data.toolName ?? data.tool ?? item?.tool ?? title; + if (typeof toolName !== "string") return {}; + const name = toolName + .split(/__|[./]/) + .at(-1) + ?.replace(/[_\s]/g, "") + .toLowerCase(); + if (!name || !/^(askuserquestion|requestuserinput(?:async)?|askquestion|question)$/.test(name)) + return {}; + const input = asRecord( + data.input ?? data.rawInput ?? asRecord(data.state)?.input ?? item?.arguments, + ); + const questions = input?.questions ?? asRecord(input?.params)?.questions; + if (!Array.isArray(questions)) return {}; + // Clients match native tools to the canonical question; choices and answers + // already live on the user-input activities and need not cross the wire twice. + return { + toolName, + input: { + questions: questions.map((value) => { + const question = asRecord(value); + return { + question: asTrimmedString( + question?.question ?? question?.question_text ?? question?.prompt ?? question?.title, + ), + }; + }), + }, + }; +} From 48654c1182c41dbd5e99ef0d6620a13ba2400f26 Mon Sep 17 00:00:00 2001 From: maria Date: Thu, 10 Sep 2026 23:39:21 -0300 Subject: [PATCH 03/63] fix(usage): flag unpriced model activity instead of showing $0.00 (#11021) Co-authored-by: Claude Opus 5 (1M context) --- .../src/features/usage/UsageRouteScreen.tsx | 9 +++-- .../src/components/usage/UsagePage.test.tsx | 24 +++++++++++++ apps/web/src/components/usage/UsagePage.tsx | 19 ++++++++--- packages/shared/src/usageMerge.test.ts | 34 ++++++++++++++++++- packages/shared/src/usageMerge.ts | 24 ++++++++++++- 5 files changed, 101 insertions(+), 9 deletions(-) diff --git a/apps/mobile/src/features/usage/UsageRouteScreen.tsx b/apps/mobile/src/features/usage/UsageRouteScreen.tsx index 3e5cd0fc9e3d8..ad54d6e323ee2 100644 --- a/apps/mobile/src/features/usage/UsageRouteScreen.tsx +++ b/apps/mobile/src/features/usage/UsageRouteScreen.tsx @@ -2,6 +2,7 @@ import { EnvironmentId, USAGE_CONTRACT_VERSION } from "@t3tools/contracts"; import { useNavigation } from "@react-navigation/native"; import { isCompatibleUsageContractVersion, + isModelCostUnknown, type DailyTotals, type MergedUsage, } from "@t3tools/shared/usageMerge"; @@ -607,10 +608,14 @@ function ModelsSection(props: { readonly merged: MergedUsage }) { {model.model} - {formatPercent(model.costShare)} of cost · {formatTokens(model.totalTokens)} tokens + {isModelCostUnknown(model) + ? `no known rates · ${formatTokens(model.totalTokens)} tokens` + : `${formatPercent(model.costShare)} of cost · ${formatTokens(model.totalTokens)} tokens`} - {formatUsd(model.costUsd)} + + {isModelCostUnknown(model) ? "Unpriced" : formatUsd(model.costUsd)} + ))} diff --git a/apps/web/src/components/usage/UsagePage.test.tsx b/apps/web/src/components/usage/UsagePage.test.tsx index e41843e6d9cc1..0743b91f6edff 100644 --- a/apps/web/src/components/usage/UsagePage.test.tsx +++ b/apps/web/src/components/usage/UsagePage.test.tsx @@ -87,6 +87,7 @@ const modelTotals = Object.freeze([ costUsd: 10, totalTokens: 100, records: 1, + unpricedRecords: 0, costShare: 10 / 16, }, { @@ -95,6 +96,7 @@ const modelTotals = Object.freeze([ costUsd: 5, totalTokens: 1_000, records: 1, + unpricedRecords: 0, costShare: 5 / 16, }, { @@ -103,8 +105,18 @@ const modelTotals = Object.freeze([ costUsd: 1, totalTokens: 1_000, records: 1, + unpricedRecords: 0, costShare: 1 / 16, }, + { + model: "unpriced-model", + provider: "codex" as const, + costUsd: 0, + totalTokens: 500, + records: 2, + unpricedRecords: 2, + costShare: 0, + }, ]); const environments = [ @@ -190,6 +202,17 @@ describe("UsagePage model breakdown", () => { expect(body).toMatch(/expensive-model.*token-heavy-model.*token-heavy-cheaper-model/); }); + it("flags a model with no known rates instead of showing it as free", () => { + testState.breakdown = "model"; + + const markup = renderToStaticMarkup(); + const body = markup.match(/(.*?)<\/tbody>/)?.[1] ?? ""; + const unpricedRow = body.split(" row.includes("unpriced-model")) ?? ""; + + expect(unpricedRow).toContain("Unpriced"); + expect(unpricedRow).not.toContain("$0.00"); + }); + it("sorts models by token usage when the token metric is selected", () => { testState.metric = "tokens"; testState.breakdown = "model"; @@ -202,6 +225,7 @@ describe("UsagePage model breakdown", () => { "expensive-model", "token-heavy-model", "token-heavy-cheaper-model", + "unpriced-model", ]); }); }); diff --git a/apps/web/src/components/usage/UsagePage.tsx b/apps/web/src/components/usage/UsagePage.tsx index deb05f266b980..21970c6755966 100644 --- a/apps/web/src/components/usage/UsagePage.tsx +++ b/apps/web/src/components/usage/UsagePage.tsx @@ -15,6 +15,7 @@ import { useMemo, useRef, useState } from "react"; import { isCompatibleUsageContractVersion, + isModelCostUnknown, type DailyTotals, type HourlyTotals, } from "@t3tools/shared/usageMerge"; @@ -363,9 +364,13 @@ export function UsagePage() { : formatTokens(merged.totalTokens)} - {metric === "cost" - ? `${formatCount(merged.sessions)} sessions · API estimate` - : `${formatCount(merged.sessions)} sessions`} + {metric !== "cost" + ? `${formatCount(merged.sessions)} sessions` + : merged.costQuality.unpricedShare > 0 + ? `${formatCount(merged.sessions)} sessions · API estimate excludes ${formatPercent( + merged.costQuality.unpricedShare, + )} unpriced records` + : `${formatCount(merged.sessions)} sessions · API estimate`}
@@ -511,10 +516,14 @@ export function UsagePage() { - {formatUsd(model.costUsd)} + {isModelCostUnknown(model) ? ( + Unpriced + ) : ( + formatUsd(model.costUsd) + )} - {formatPercent(model.costShare)} + {isModelCostUnknown(model) ? "—" : formatPercent(model.costShare)} {formatTokens(model.totalTokens)} diff --git a/packages/shared/src/usageMerge.test.ts b/packages/shared/src/usageMerge.test.ts index 6c706395c6ffe..24bbc3b7c2e80 100644 --- a/packages/shared/src/usageMerge.test.ts +++ b/packages/shared/src/usageMerge.test.ts @@ -8,7 +8,7 @@ import { } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; -import { mergeUsage, type EnvironmentUsage } from "./usageMerge.ts"; +import { isModelCostUnknown, mergeUsage, type EnvironmentUsage } from "./usageMerge.ts"; function bucket(overrides: Partial = {}): UsageBucket { return { @@ -221,6 +221,38 @@ describe("mergeUsage", () => { expect(merged.costQuality.cacheSavingsUsd).toBe(4); }); + it("marks a model with no known rates as unpriced rather than free", () => { + const merged = mergeUsage( + [ + environment( + "env-a", + summary( + [ + bucket({ costUsd: 75 }), + bucket({ + provider: "codex", + model: "unknown-model", + costUsd: 0, + costSource: "unpriced", + unpricedRecords: 5, + }), + ], + [ + { provider: "claude", hostId: "mac", homePath: "/a/.claude" }, + { provider: "codex", hostId: "mac", homePath: "/a/.codex" }, + ], + ), + ), + ], + USAGE_CONTRACT_VERSION, + ); + + expect(merged.models.find((model) => model.model === "unknown-model")?.unpricedRecords).toBe(5); + expect(merged.models.filter(isModelCostUnknown).map((model) => model.model)).toEqual([ + "unknown-model", + ]); + }); + it("keeps two machines apart when hostname and home path collide", () => { // Every Mac resolves /Users/theo/.claude, so a hostname clash used to make // one machine's usage vanish. Filesystem identity separates them. diff --git a/packages/shared/src/usageMerge.ts b/packages/shared/src/usageMerge.ts index 95982bf507da0..e0cb0510eb71a 100644 --- a/packages/shared/src/usageMerge.ts +++ b/packages/shared/src/usageMerge.ts @@ -37,9 +37,22 @@ export interface ModelTotals { readonly costUsd: number; readonly totalTokens: number; readonly records: number; + /** + * Records whose tokens are counted here but which contributed nothing to + * `costUsd`. When it equals `records` the cost is unknown, not zero. + */ + readonly unpricedRecords: number; readonly costShare: number; } +/** + * A model whose every record lacked rates has an unknown cost, not a zero one. + * Clients must not present its `costUsd` as a real dollar figure. + */ +export function isModelCostUnknown(model: ModelTotals): boolean { + return model.records > 0 && model.unpricedRecords >= model.records; +} + export interface DailyTotals { readonly day: string; readonly costUsd: number; @@ -249,7 +262,13 @@ export function mergeUsage( >(); const modelAccumulator = new Map< string, - { provider: UsageProviderKind; costUsd: number; totalTokens: number; records: number } + { + provider: UsageProviderKind; + costUsd: number; + totalTokens: number; + records: number; + unpricedRecords: number; + } >(); const dailyAccumulator = new Map< string, @@ -319,10 +338,12 @@ export function mergeUsage( costUsd: 0, totalTokens: 0, records: 0, + unpricedRecords: 0, }; model.costUsd += bucket.costUsd; model.totalTokens += tokens; model.records += bucket.records; + model.unpricedRecords += bucket.unpricedRecords; modelAccumulator.set(modelKey, model); const day = dailyAccumulator.get(bucket.day) ?? { @@ -381,6 +402,7 @@ export function mergeUsage( costUsd: totals.costUsd, totalTokens: totals.totalTokens, records: totals.records, + unpricedRecords: totals.unpricedRecords, costShare: costUsd === 0 ? 0 : totals.costUsd / costUsd, })) .sort((a, b) => b.costUsd - a.costUsd || b.totalTokens - a.totalTokens); From 5735693d4acb949c2b0daf1219b4f62845115435 Mon Sep 17 00:00:00 2001 From: maria Date: Thu, 10 Sep 2026 23:39:31 -0300 Subject: [PATCH 04/63] fix(server): let Claude launch args override the derived permission mode (#11026) Co-authored-by: Claude Opus 5 (1M context) --- .../src/provider/Layers/ClaudeAdapter.test.ts | 23 +++++++++++++++++++ .../src/provider/Layers/ClaudeAdapter.ts | 15 ++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/apps/server/src/provider/Layers/ClaudeAdapter.test.ts b/apps/server/src/provider/Layers/ClaudeAdapter.test.ts index 5422a8730f276..c4136156df104 100644 --- a/apps/server/src/provider/Layers/ClaudeAdapter.test.ts +++ b/apps/server/src/provider/Layers/ClaudeAdapter.test.ts @@ -422,6 +422,29 @@ describe("ClaudeAdapterLive", () => { ); }); + it.effect("lets a launch-arg permission flag win over the thread runtime mode", () => { + const harness = makeHarness({ + claudeConfig: { launchArgs: "--dangerously-skip-permissions --verbose" }, + }); + return Effect.gen(function* () { + const adapter = yield* ClaudeAdapter; + yield* adapter.startSession({ + threadId: THREAD_ID, + provider: ProviderDriverKind.make("claudeAgent"), + runtimeMode: "auto-accept-edits", + }); + + const createInput = harness.getLastCreateQueryInput(); + assert.equal(createInput?.options.permissionMode, "bypassPermissions"); + assert.equal(createInput?.options.allowDangerouslySkipPermissions, true); + // The honored flag is dropped from extraArgs so the CLI sees it once. + assert.deepEqual(createInput?.options.extraArgs, { verbose: null }); + }).pipe( + Effect.provideService(Random.Random, makeDeterministicRandomService()), + Effect.provide(harness.layer), + ); + }); + it.effect("loads Claude filesystem settings sources for SDK sessions", () => { const harness = makeHarness(); return Effect.gen(function* () { diff --git a/apps/server/src/provider/Layers/ClaudeAdapter.ts b/apps/server/src/provider/Layers/ClaudeAdapter.ts index 4af5a06336543..981e183414f13 100644 --- a/apps/server/src/provider/Layers/ClaudeAdapter.ts +++ b/apps/server/src/provider/Layers/ClaudeAdapter.ts @@ -4608,7 +4608,11 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* ( ) => runPromise(handleResumeDialog(request, callbackOptions)); const claudeBinaryPath = claudeSdkExecutablePath; - const extraArgs = parseCliArgs(claudeSettings.launchArgs).flags; + const { + "permission-mode": launchArgPermissionMode, + "dangerously-skip-permissions": launchArgSkipPermissions, + ...extraArgs + } = parseCliArgs(claudeSettings.launchArgs).flags; const selectedModel = input.modelSelection?.instanceId === boundInstanceId ? input.modelSelection : undefined; const modelSelection = selectedModel @@ -4649,7 +4653,14 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* ( auto: "auto", "full-access": "bypassPermissions", }; - const permissionMode = runtimeModeToPermission[input.runtimeMode]; + // A permission launch arg is folded into the mode T3 sends rather than + // passed through: the CLI resolves both inputs together, so argv order + // never let the user's flag win. + const permissionMode = + (launchArgPermissionMode as PermissionMode | null | undefined) ?? + (launchArgSkipPermissions === null || launchArgSkipPermissions === "true" + ? "bypassPermissions" + : runtimeModeToPermission[input.runtimeMode]); const settings = { ...(typeof thinking === "boolean" ? { alwaysThinkingEnabled: thinking } : {}), ...(fastMode ? { fastMode: true } : {}), From 20ef25037f455a570426ac2a93fcf499d1f91167 Mon Sep 17 00:00:00 2001 From: maria Date: Thu, 10 Sep 2026 23:39:41 -0300 Subject: [PATCH 05/63] fix(editors): accept root paths and Windows servers in Zed remote links (#11044) Co-authored-by: Claude Fable 5.1 --- apps/desktop/src/electron/ElectronShell.test.ts | 12 +++++++++--- apps/desktop/src/electron/ElectronShell.ts | 2 +- apps/web/src/remoteOpen.test.ts | 9 +++++++++ packages/contracts/src/editor.ts | 17 ++++++++++++----- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/apps/desktop/src/electron/ElectronShell.test.ts b/apps/desktop/src/electron/ElectronShell.test.ts index 75eea216df21e..8ba8082f0e57c 100644 --- a/apps/desktop/src/electron/ElectronShell.test.ts +++ b/apps/desktop/src/electron/ElectronShell.test.ts @@ -93,10 +93,16 @@ describe("ElectronShell", () => { openExternalMock.mockResolvedValue(undefined); const electronShell = yield* ElectronShell.ElectronShell; - const result = yield* electronShell.openExternal("zed://ssh/example.com/home/user/project"); + const results = yield* Effect.all([ + electronShell.openExternal("zed://ssh/example.com/home/user/project"), + electronShell.openExternal("zed://ssh/example.com/"), + ]); - assert.equal(result, true); - assert.deepEqual(openExternalMock.mock.calls, [["zed://ssh/example.com/home/user/project"]]); + assert.deepEqual(results, [true, true]); + assert.deepEqual(openExternalMock.mock.calls, [ + ["zed://ssh/example.com/home/user/project"], + ["zed://ssh/example.com/"], + ]); }).pipe(Effect.provide(ElectronShell.layer)), ); diff --git a/apps/desktop/src/electron/ElectronShell.ts b/apps/desktop/src/electron/ElectronShell.ts index 2089be58c0dc2..aa97c018bd219 100644 --- a/apps/desktop/src/electron/ElectronShell.ts +++ b/apps/desktop/src/electron/ElectronShell.ts @@ -36,7 +36,7 @@ const REMOTE_EDITOR_PROTOCOLS = new Set( ); // Zed's host sits in the first path segment, so it needs its own userinfo ban. -const ZED_SSH_PATHNAME = /^\/[^/@:]+\/.+$/; +const ZED_SSH_PATHNAME = /^\/[^/@:]+\/.*$/; const isRemoteEditorUrl = (url: URL) => REMOTE_EDITOR_PROTOCOLS.has(url.protocol) && diff --git a/apps/web/src/remoteOpen.test.ts b/apps/web/src/remoteOpen.test.ts index 6f7c17d8177ad..0f636a526cef9 100644 --- a/apps/web/src/remoteOpen.test.ts +++ b/apps/web/src/remoteOpen.test.ts @@ -151,6 +151,15 @@ describe("buildRemoteOpenUrl", () => { ).toBe("zed://ssh/sol.tail1234.ts.net/home/theo/code/my%20repo"); }); + it("drops the Windows drive letter for Zed", () => { + expect( + buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "C:\\Users\\theo" }), + ).toBe("zed://ssh/sol/Users/theo"); + expect(buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "/C:/project" })).toBe( + "zed://ssh/sol/C%3A/project", + ); + }); + it("returns undefined for editors without remote support", () => { expect(buildRemoteOpenUrl({ editor: "idea", host: "sol", absolutePath: "/tmp/x" })).toBe( undefined, diff --git a/packages/contracts/src/editor.ts b/packages/contracts/src/editor.ts index 6331a544d3416..0680a398879ea 100644 --- a/packages/contracts/src/editor.ts +++ b/packages/contracts/src/editor.ts @@ -91,7 +91,7 @@ export type LaunchEditorInput = typeof LaunchEditorInput.Type; const remoteSchemeOf = (editor: EditorDefinition): string | undefined => editor.remoteScheme; -/** Editors that can open a remote workspace via `vscode-remote` deep links. */ +/** Editors that can open a remote workspace via an SSH deep link. */ export const REMOTE_CAPABLE_EDITOR_IDS: ReadonlyArray = EDITORS.flatMap((editor) => remoteSchemeOf(editor) !== undefined ? [editor.id] : [], ); @@ -119,11 +119,18 @@ export const buildRemoteOpenUrl = (input: { // Windows server paths (`C:\...`) appear as `/C:/...` in vscode-remote URIs. const posixPath = input.absolutePath.replaceAll("\\", "/"); const rootedPath = posixPath.startsWith("/") ? posixPath : `/${posixPath}`; - const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/"); const encodedHost = encodeURIComponent(input.host); - return input.editor === "zed" - ? `${scheme}://ssh/${encodedHost}${encodedPath}` - : `${scheme}://vscode-remote/ssh-remote+${encodedHost}${encodedPath}`; + if (input.editor === "zed") { + // Zed's remote server resolves a rooted path on the system drive, so a + // Windows `C:\Users\x` must become `/Users/x` (verified in #8938). Other + // drives are untested and kept as is rather than silently remapped, and a + // POSIX path that happens to start with `/C:` is left alone. + const zedPath = /^[Cc]:[\\/]/.test(input.absolutePath) ? rootedPath.slice(3) : rootedPath; + const encodedZedPath = zedPath.split("/").map(encodeURIComponent).join("/"); + return `${scheme}://ssh/${encodedHost}${encodedZedPath}`; + } + const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/"); + return `${scheme}://vscode-remote/ssh-remote+${encodedHost}${encodedPath}`; }; /** From 4d06156dd7821851007e61489105f33dbb241a7f Mon Sep 17 00:00:00 2001 From: maria Date: Thu, 10 Sep 2026 23:59:10 -0300 Subject: [PATCH 06/63] fix(web): center pull request unavailable states (#11110) --- .../components/pullRequest/PullRequestDetailPanel.tsx | 10 ++++++++-- .../pullRequest/PullRequestsUnavailableState.tsx | 2 +- apps/web/src/routes/_chat.pull-requests.tsx | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx index 236d7cd86ad99..5b86ef5ee3ba8 100644 --- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx +++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx @@ -1447,7 +1447,13 @@ export function PullRequestDetailPanel({ onPickerOpenChange={setThreadPickerOpen} /> ) : null} -
+
{ const scroller = event.target as HTMLElement; scrollerRef.current = scroller; diff --git a/apps/web/src/components/pullRequest/PullRequestsUnavailableState.tsx b/apps/web/src/components/pullRequest/PullRequestsUnavailableState.tsx index 26c80326aec0b..f17ec882c5a0a 100644 --- a/apps/web/src/components/pullRequest/PullRequestsUnavailableState.tsx +++ b/apps/web/src/components/pullRequest/PullRequestsUnavailableState.tsx @@ -25,7 +25,7 @@ export function PullRequestsUnavailableState({ gitHubUrl?: string; }) { return ( - + diff --git a/apps/web/src/routes/_chat.pull-requests.tsx b/apps/web/src/routes/_chat.pull-requests.tsx index 24dcab132b861..5a21a2cc37ff7 100644 --- a/apps/web/src/routes/_chat.pull-requests.tsx +++ b/apps/web/src/routes/_chat.pull-requests.tsx @@ -2362,7 +2362,7 @@ function PullRequestsColumn({ {/* The top padding is the shared fade band's height, the same pairing the settings page makes: at rest the controls sit fully below the mask, and only content actually passing under the chrome fades. */} - +
From 0a37240a87bb2fed8f48cf20dc8312ee3b94eba6 Mon Sep 17 00:00:00 2001 From: maria Date: Fri, 11 Sep 2026 00:13:23 -0300 Subject: [PATCH 07/63] fix(web): remove sidebar pull request link icon (#11179) --- apps/web/src/components/LegacySidebar.tsx | 12 +---- apps/web/src/components/Sidebar.tsx | 21 +------- .../LinkBranchPullRequestButton.tsx | 54 ------------------- 3 files changed, 2 insertions(+), 85 deletions(-) delete mode 100644 apps/web/src/components/pullRequest/LinkBranchPullRequestButton.tsx diff --git a/apps/web/src/components/LegacySidebar.tsx b/apps/web/src/components/LegacySidebar.tsx index 3a39366232757..81fd6047f13e1 100644 --- a/apps/web/src/components/LegacySidebar.tsx +++ b/apps/web/src/components/LegacySidebar.tsx @@ -1,10 +1,6 @@ import { useSupportsMultiplePullRequests } from "~/hooks/useSupportsMultiplePullRequests"; import { GitPullRequestIcon } from "lucide-react"; -import { LinkBranchPullRequestButton } from "./pullRequest/LinkBranchPullRequestButton"; -import { - resolveThreadCurrentPullRequestLink, - visibleThreadPullRequests, -} from "@t3tools/shared/threadPullRequests"; +import { resolveThreadCurrentPullRequestLink } from "@t3tools/shared/threadPullRequests"; import { Spinner } from "~/components/ui/spinner"; import { ArchiveIcon, @@ -764,12 +760,6 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP ) : null} - {pr && - (supportsMultiplePullRequests - ? visibleThreadPullRequests(thread.pullRequests).length === 0 - : thread.linkedPullRequest == null) ? ( - - ) : null} {threadStatus && } {renamingThreadKey === threadKey ? ( - ) : null} {sortable?.isDragging ? ( dragDestination ) : ( @@ -1925,13 +1913,6 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: { )} {terminalStatusIcon} {prBadge} - {prBadge && - pr && - (supportsMultiplePullRequests - ? visibleThreadPullRequests(thread.pullRequests).length === 0 - : thread.linkedPullRequest == null) ? ( - - ) : null} {diff ? ( +{diff.insertions}{" "} diff --git a/apps/web/src/components/pullRequest/LinkBranchPullRequestButton.tsx b/apps/web/src/components/pullRequest/LinkBranchPullRequestButton.tsx deleted file mode 100644 index bbcb9e1441903..0000000000000 --- a/apps/web/src/components/pullRequest/LinkBranchPullRequestButton.tsx +++ /dev/null @@ -1,54 +0,0 @@ -import type { ScopedThreadRef } from "@t3tools/contracts"; -import { Link2 } from "lucide-react"; -import { useState } from "react"; -import { usePullRequestLinking } from "~/hooks/usePullRequestLinking"; -import { Button } from "../ui/button"; -import { toastManager } from "../ui/toast"; -import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; - -/** Adopts a branch discovery as a durable link, even after the thread changes branches. */ -export function LinkBranchPullRequestButton({ - threadRef, - url, -}: { - threadRef: ScopedThreadRef; - url: string; -}) { - const linking = usePullRequestLinking(threadRef.environmentId); - const [pending, setPending] = useState(false); - if (!linking.canLink(url)) return null; - return ( - - event.stopPropagation()} - onClick={async (event) => { - event.preventDefault(); - event.stopPropagation(); - setPending(true); - try { - await linking.changeLink(threadRef, url, true); - } catch (error) { - toastManager.add({ - type: "error", - title: "Could not link pull request", - description: error instanceof Error ? error.message : String(error), - }); - } finally { - setPending(false); - } - }} - > - - - } - /> - Link this PR to keep it with this thread - - ); -} From 02297e3dbd896ef619d5c916938c751e343e76a8 Mon Sep 17 00:00:00 2001 From: maria Date: Fri, 11 Sep 2026 00:58:57 -0300 Subject: [PATCH 08/63] fix(ui): color linked pr counts by aggregate status (#11180) --- .../features/threads/thread-list-items.tsx | 10 +++-- .../features/threads/thread-list-v2-items.tsx | 8 +++- .../src/state/thread-pr-presentation.ts | 23 ++++++++--- apps/mobile/src/state/use-thread-pr.test.ts | 34 ++++++++++++++- .../src/components/ThreadStatusIndicators.tsx | 13 +++--- .../shared/src/threadPullRequests.test.ts | 41 ++++++++++++++++++- packages/shared/src/threadPullRequests.ts | 22 ++++++---- 7 files changed, 123 insertions(+), 28 deletions(-) diff --git a/apps/mobile/src/features/threads/thread-list-items.tsx b/apps/mobile/src/features/threads/thread-list-items.tsx index a4da32ef70185..cad6181e96236 100644 --- a/apps/mobile/src/features/threads/thread-list-items.tsx +++ b/apps/mobile/src/features/threads/thread-list-items.tsx @@ -44,11 +44,11 @@ export const THREAD_LIST_COMPACT_INSET = HOME_HORIZONTAL_INSET; const SIDEBAR_ROW_RADIUS = 12; function pullRequestTintColor( - pr: Pick, + pr: Pick, colorScheme: "light" | "dark", ) { const dark = colorScheme === "dark"; - if (pr.others > 0 || (pr.state === "open" && pr.isDraft === true)) { + if (pr.state === "open" && pr.isDraft === true) { return dark ? "#a1a1aa" : "#71717a"; } switch (pr.state) { @@ -56,8 +56,12 @@ function pullRequestTintColor( return dark ? "#34d399" : "#059669"; case "merged": return dark ? "#a78bfa" : "#7c3aed"; - case null: case "closed": + if (pr.kind === "stack" || pr.others > 0) { + return dark ? "#fb7185" : "#e11d48"; + } + return dark ? "#a1a1aa" : "#71717a"; + case null: return dark ? "#a1a1aa" : "#71717a"; } } diff --git a/apps/mobile/src/features/threads/thread-list-v2-items.tsx b/apps/mobile/src/features/threads/thread-list-v2-items.tsx index 937ca5f909a8f..a234147471c61 100644 --- a/apps/mobile/src/features/threads/thread-list-v2-items.tsx +++ b/apps/mobile/src/features/threads/thread-list-v2-items.tsx @@ -880,7 +880,13 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { ? materialYouStyleLayoutActive ? "accent-thread-selected-foreground" : "accent-user-bubble-foreground" - : "accent-foreground-muted" + : pr.state === null || pr.isDraft + ? "accent-foreground-muted" + : pr.state === "open" + ? "accent-adaptive-emerald-600-400" + : pr.state === "closed" + ? "accent-adaptive-rose-600-400" + : "accent-adaptive-violet-600-400" } /> ) : null} diff --git a/apps/mobile/src/state/thread-pr-presentation.ts b/apps/mobile/src/state/thread-pr-presentation.ts index fc310d070acc7..21234b7ff1503 100644 --- a/apps/mobile/src/state/thread-pr-presentation.ts +++ b/apps/mobile/src/state/thread-pr-presentation.ts @@ -63,9 +63,16 @@ export function presentThreadLinkedPullRequests( const badge = resolveThreadPullRequestBadge(links); if (link === null || badge === null) return null; const snapshot = link.snapshot; - const state = badge.kind === "stack" ? badge.state : (snapshot?.state ?? null); - const isDraft = snapshot?.isDraft === true && state === "open"; const linkedCount = badge.kind === "pull-request" && badge.others > 0 ? badge.others + 1 : null; + const isMultiple = badge.kind === "stack" || linkedCount !== null; + const state = isMultiple + ? badge.state === "draft" + ? "open" + : badge.state + : (snapshot?.state ?? null); + const isDraft = isMultiple + ? badge.state === "draft" + : snapshot?.isDraft === true && state === "open"; const label = badge.kind === "stack" ? String(badge.layers) @@ -83,12 +90,16 @@ export function presentThreadLinkedPullRequests( label, accessibilityLabel: badge.kind === "stack" - ? `${badge.layers} pull requests in stack, ${state ?? "status pending"}` - : `#${link.number} pull request ${state === null ? "status pending" : isDraft ? "draft" : state}${badge.others > 0 ? `, ${badge.others} more linked` : ""}`, + ? `${badge.layers} pull requests in stack, ${isDraft ? "draft" : (state ?? "status pending")}` + : linkedCount !== null + ? `${linkedCount} linked pull requests, overall ${badge.state}` + : `#${link.number} pull request ${state === null ? "status pending" : isDraft ? "draft" : state}`, textClassName: - linkedCount !== null || state === null || isDraft + state === null || isDraft ? "text-foreground-muted" - : PR_STATE_TEXT_CLASS[state], + : isMultiple && state === "closed" + ? "text-adaptive-rose-600-400" + : PR_STATE_TEXT_CLASS[state], }; } diff --git a/apps/mobile/src/state/use-thread-pr.test.ts b/apps/mobile/src/state/use-thread-pr.test.ts index e36bd7d81434f..75ec6743ac59f 100644 --- a/apps/mobile/src/state/use-thread-pr.test.ts +++ b/apps/mobile/src/state/use-thread-pr.test.ts @@ -89,10 +89,42 @@ describe("presentThreadLinkedPullRequests", () => { kind: "pull-request", label: "+2", others: 1, - textClassName: "text-foreground-muted", + state: "open", + isDraft: false, + textClassName: "text-adaptive-emerald-600-400", }); }); + it.each([ + ["closed", false, "closed", false, "closed", false, "text-adaptive-rose-600-400"], + ["open", true, "open", true, "open", true, "text-foreground-muted"], + ["open", true, "open", false, "open", false, "text-adaptive-emerald-600-400"], + ["closed", false, "open", false, "open", false, "text-adaptive-emerald-600-400"], + ["merged", false, "merged", false, "merged", false, "text-adaptive-violet-600-400"], + ["closed", false, "merged", false, "closed", false, "text-adaptive-rose-600-400"], + ] as const)( + "colors linked %s (draft %s) and %s (draft %s) by their aggregate state", + (firstState, firstDraft, secondState, secondDraft, state, isDraft, textClassName) => { + const first = linkedPr(1); + const second = linkedPr(2); + expect( + presentThreadLinkedPullRequests([ + { ...first, snapshot: { ...first.snapshot!, state: firstState, isDraft: firstDraft } }, + { + ...second, + snapshot: { ...second.snapshot!, state: secondState, isDraft: secondDraft }, + }, + ]), + ).toMatchObject({ + label: "+2", + state, + isDraft, + textClassName, + accessibilityLabel: `2 linked pull requests, overall ${isDraft ? "draft" : state}`, + }); + }, + ); + it("uses the top of a derived stack even when its bottom was linked later", () => { const bottom = linkedPr(1, { linkedAt: "2026-09-09T00:00:00.000Z" }); const top = linkedPr(2); diff --git a/apps/web/src/components/ThreadStatusIndicators.tsx b/apps/web/src/components/ThreadStatusIndicators.tsx index e948aa4091ee5..2a69eaeddc9da 100644 --- a/apps/web/src/components/ThreadStatusIndicators.tsx +++ b/apps/web/src/components/ThreadStatusIndicators.tsx @@ -160,7 +160,7 @@ export function ThreadPullRequestBadgeControl({ ? `Stack of ${badge.layers} pull requests, ${badge.state}` : `${status?.tooltip ?? `PR #${number}, status pending`}${ badge?.kind === "pull-request" && badge.others > 0 - ? `, and ${badge.others} more linked` + ? `, and ${badge.others} more linked; overall ${badge.state}` : "" }`; const className = cn( @@ -170,11 +170,9 @@ export function ThreadPullRequestBadgeControl({ "text-xs tabular-nums", variant === "ghost" && "font-normal text-xs! active:scale-100 [--control-icon-color:currentColor]", - linkedCount !== null - ? "text-secondary-label" - : isStack - ? PR_STATE_COLOR_CLASS[badge.state] - : (status?.colorClass ?? "text-muted-foreground"), + badge !== null && (isStack || linkedCount !== null) + ? PR_STATE_COLOR_CLASS[badge.state] + : (status?.colorClass ?? "text-muted-foreground"), ); const content = ( <> @@ -276,10 +274,11 @@ export function ThreadPullRequestsMiniList({ } /** The ink each pull-request state wears in the sidebar, shared by the number and stack badges. */ -const PR_STATE_COLOR_CLASS: Record["state"], string> = { +const PR_STATE_COLOR_CLASS: Record = { open: "text-emerald-600 dark:text-emerald-300/90", merged: "text-violet-600 dark:text-violet-300/90", closed: "text-red-600 dark:text-red-300/90", + draft: "text-zinc-500 dark:text-zinc-400/80", }; export function settledPrHoverColorClass( diff --git a/packages/shared/src/threadPullRequests.test.ts b/packages/shared/src/threadPullRequests.test.ts index 7369b526dad3b..05fc7b12b021e 100644 --- a/packages/shared/src/threadPullRequests.test.ts +++ b/packages/shared/src/threadPullRequests.test.ts @@ -248,6 +248,40 @@ describe("resolveThreadPullRequestChains", () => { }); describe("chain selection and badge state", () => { + it.each([ + ["open", false, "open", false, "open"], + ["closed", false, "closed", false, "closed"], + ["open", true, "open", true, "draft"], + ["open", false, "open", true, "open"], + ["closed", false, "open", true, "open"], + ["merged", false, "merged", false, "merged"], + ["merged", false, "closed", true, "closed"], + ] as const)( + "aggregates %s (draft %s) and %s (draft %s) as %s", + (firstState, firstDraft, secondState, secondDraft, state) => { + for (const stacked of [false, true]) { + const links = [ + link(1, { + snapshot: snapshot({ state: firstState, isDraft: firstDraft, headBranch: "base" }), + }), + link(2, { + snapshot: snapshot({ + state: secondState, + isDraft: secondDraft, + baseBranch: stacked ? "base" : "main", + }), + }), + link(3, { source: "stack-dismissed" }), + ]; + expect(resolveThreadPullRequestBadge(links)).toEqual( + stacked + ? { kind: "stack", layers: 2, state } + : { kind: "pull-request", others: 1, state }, + ); + } + }, + ); + it.each(["open", "merged", "closed"] as const)( "targets the top of a derived %s chain despite a later bottom update and link", (state) => { @@ -309,6 +343,7 @@ describe("chain selection and badge state", () => { expect(resolveThreadPullRequestBadge([bottom, top, link(3)])).toEqual({ kind: "pull-request", others: 2, + state: "open", }); expect(resolveThreadPullRequestBadge([link(3, { source: "stack-dismissed" })])).toBeNull(); }); @@ -340,7 +375,11 @@ describe("chain selection and badge state", () => { kind: "stack", top: { number: 2 }, }); - expect(resolveThreadPullRequestBadge(links)).toEqual({ kind: "pull-request", others: 1 }); + expect(resolveThreadPullRequestBadge(links)).toEqual({ + kind: "pull-request", + others: 1, + state: "open", + }); }); it("does not guess a parent when a head branch was reused", () => { diff --git a/packages/shared/src/threadPullRequests.ts b/packages/shared/src/threadPullRequests.ts index 424433b3457c0..5d91d98d52c99 100644 --- a/packages/shared/src/threadPullRequests.ts +++ b/packages/shared/src/threadPullRequests.ts @@ -243,31 +243,35 @@ export function resolveThreadPullRequestChains( return chains; } -export type ThreadPullRequestBadge = +export type ThreadPullRequestBadge = { + readonly state: "open" | "closed" | "merged" | "draft"; +} & ( | { readonly kind: "stack"; readonly layers: number; - readonly state: "open" | "closed" | "merged"; } - | { readonly kind: "pull-request"; readonly others: number }; + | { readonly kind: "pull-request"; readonly others: number } +); -/** Aggregate a single chain's state; unrelated links show a count beside the current PR. */ +/** Aggregate visible links' state for both stacks and unrelated linked counts. */ export function resolveThreadPullRequestBadge( pullRequests: ReadonlyArray | undefined, ): ThreadPullRequestBadge | null { const visible = visibleThreadPullRequests(pullRequests ?? []); if (visible.length === 0) return null; - const chains = resolveThreadPullRequestChains(visible); - if (visible.length > 1 && chains.length === 1) { - const states = visible.map((link) => link.snapshot?.state ?? "open"); - const state = states.includes("open") + const states = visible.map((link) => link.snapshot?.state ?? "open"); + const state = visible.every((link) => link.snapshot?.state === "open" && link.snapshot.isDraft) + ? "draft" + : states.includes("open") ? "open" : states.every((entry) => entry === "merged") ? "merged" : "closed"; + const chains = resolveThreadPullRequestChains(visible); + if (visible.length > 1 && chains.length === 1) { return { kind: "stack", layers: visible.length, state }; } - return { kind: "pull-request", others: visible.length - 1 }; + return { kind: "pull-request", others: visible.length - 1, state }; } /** Search terms for visible PR links, including the legacy single-link projection. */ From 6c69534a5800a95e1640b0571580561ef71ec461 Mon Sep 17 00:00:00 2001 From: maria Date: Fri, 11 Sep 2026 01:04:14 -0300 Subject: [PATCH 09/63] fix(preview): render website favicons for browser tool activity (#11032) --- apps/server/src/mcp/McpHttpServer.test.ts | 47 +++++--- apps/server/src/mcp/McpHttpServer.ts | 7 ++ .../src/mcp/PreviewAutomationBroker.test.ts | 102 +++++++++++------- .../server/src/mcp/PreviewAutomationBroker.ts | 6 ++ .../src/mcp/toolkits/preview/handlers.ts | 69 +++++++++--- .../src/mcp/toolkits/preview/tools.test.ts | 1 + apps/server/src/mcp/toolkits/preview/tools.ts | 17 ++- .../ActivityPayloadProjection.test.ts | 78 ++++++++++++++ .../ActivityPayloadProjection.ts | 71 +++++++++++- 9 files changed, 320 insertions(+), 78 deletions(-) diff --git a/apps/server/src/mcp/McpHttpServer.test.ts b/apps/server/src/mcp/McpHttpServer.test.ts index 8856299307069..9cbc3fde29772 100644 --- a/apps/server/src/mcp/McpHttpServer.test.ts +++ b/apps/server/src/mcp/McpHttpServer.test.ts @@ -250,7 +250,10 @@ it.effect.each([ const { accessibilityTree: _tree, ...boundedMetadata } = metadata; expect(snapshot.isError).toBe(false); expect(snapshot.structuredContent).toEqual(metadata); - const [text, ...rest] = snapshot.content; + const [identity, text, ...rest] = snapshot.content; + expect(identity?.type === "text" ? decodeJsonText(identity.text) : null).toEqual({ + url: page.url, + }); expect(text?.type === "text" ? decodeJsonText(text.text) : null).toEqual(boundedMetadata); expect(rest).toEqual([ { @@ -279,7 +282,12 @@ it.effect.each([ Effect.provideService(McpInvocationContext.McpInvocationContext, invocation), Effect.provideService(McpSchema.McpServerClient, client), ); - expect(nextDefault.content.map((content) => content.type)).toEqual(["text", "text", "image"]); + expect(nextDefault.content.map((content) => content.type)).toEqual([ + "text", + "text", + "text", + "image", + ]); expect(nextDefault.structuredContent).toEqual({ ...page, title: "Snapshot 7", screenshot }); expect(requests).toBe(7); }), @@ -329,7 +337,7 @@ it.effect("saves the snapshot PNG on request and reports its path", () => /^browser-screenshot-example-test-[0-9a-z]+-[0-9a-f]{8}\.png$/, ); expect(Buffer.from(yield* fileSystem.readFile(screenshotPath!)).toString()).toBe("png"); - const text = snapshot.content.find((content) => content.type === "text"); + const [, text] = snapshot.content; expect(text?.type === "text" ? text.text : "").toContain(screenshotPath); const unsaved = yield* callSnapshot({}); @@ -429,7 +437,10 @@ it.effect("keeps the snapshot text under the agent's output ceiling", () => const snapshot = yield* callSnapshot({ includeImage: false }); expect(snapshot.isError).toBe(false); - const [text, notice] = snapshot.content; + const [identity, text, notice] = snapshot.content; + expect(identity?.type === "text" ? decodeJsonText(identity.text) : null).toEqual({ + url: oversized.url, + }); expect(text?.type).toBe("text"); const body = text?.type === "text" ? text.text : ""; expect(Buffer.byteLength(body, "utf8")).toBeLessThanOrEqual( @@ -471,7 +482,7 @@ it.effect("bounds the snapshot text even when nothing but logs and the title are const snapshot = yield* callSnapshot({ includeImage: false }); - const [text] = snapshot.content; + const [, text] = snapshot.content; const body = text?.type === "text" ? text.text : ""; expect(Buffer.byteLength(body, "utf8")).toBeLessThanOrEqual( McpHttpServer.MAX_SNAPSHOT_TEXT_BYTES, @@ -482,7 +493,7 @@ it.effect("bounds the snapshot text even when nothing but logs and the title are }; expect(parsed.title.length).toBe(2_049); expect(parsed.consoleEntries[0]?.text.length).toBe(501); - const notice = snapshot.content[1]; + const notice = snapshot.content[2]; const noticeText = notice?.type === "text" ? notice.text : ""; expect(noticeText).toContain("url or title after 2048 characters"); expect(noticeText).toContain("console entries text after 500 characters"); @@ -533,7 +544,7 @@ it.effect("sheds log entries before locators when every list is full", () => const snapshot = yield* callSnapshot({ includeImage: false }); - const [text, notice] = snapshot.content; + const [, text, notice] = snapshot.content; const body = text?.type === "text" ? text.text : ""; expect(Buffer.byteLength(body, "utf8")).toBeLessThanOrEqual( McpHttpServer.MAX_SNAPSHOT_TEXT_BYTES, @@ -615,6 +626,10 @@ it.effect("registers annotated tools and preserves authenticated request context Effect.gen(function* () { const server = yield* McpServer.McpServer; const broker = yield* PreviewAutomationBroker.PreviewAutomationBroker; + const toolIcon = { + _tag: "website" as const, + pageUrl: "http://example.test/", + }; const routedRequests: Array<{ readonly operation: string; readonly tabId?: string | undefined; @@ -664,7 +679,7 @@ it.effect("registers annotated tools and preserves authenticated request context expect(clickTool?.tool.annotations?.readOnlyHint).toBe(false); expect(clickTool?.tool.annotations?.destructiveHint).toBe(true); expect(clickTool?.tool.annotations?.openWorldHint).toBe(true); - expect(clickTool?.tool.outputSchema).toEqual({ + expect(clickTool?.tool.outputSchema).toMatchObject({ type: "object", additionalProperties: false, description: "The preview action completed successfully.", @@ -721,10 +736,12 @@ it.effect("registers annotated tools and preserves authenticated request context Effect.provideService(McpSchema.McpServerClient, client), ); expect(evaluated.isError).toBe(false); - expect(evaluated.structuredContent).toEqual({ value: ["Connect", "Continue"] }); - expect(evaluated.content).toEqual([ - { type: "text", text: '{"value":["Connect","Continue"]}' }, - ]); + expect(evaluated.structuredContent).toEqual({ value: ["Connect", "Continue"], toolIcon }); + const evaluatedText = evaluated.content[0]; + expect(evaluatedText?.type === "text" ? decodeJsonText(evaluatedText.text) : null).toEqual({ + toolIcon, + value: ["Connect", "Continue"], + }); const actionRequests = [ { name: "preview_click", arguments: { x: 10, y: 10 } }, @@ -741,8 +758,10 @@ it.effect("registers annotated tools and preserves authenticated request context Effect.provideService(McpSchema.McpServerClient, client), ); expect(result.isError).toBe(false); - expect(result.structuredContent).toEqual({}); - expect(result.content).toEqual([{ type: "text", text: "{}" }]); + expect(result.structuredContent).toEqual({ toolIcon }); + expect(routedRequests.at(-1)?.operation).toBe("status"); + const text = result.content[0]; + expect(text?.type === "text" ? decodeJsonText(text.text) : null).toEqual({ toolIcon }); } }), ).pipe(Effect.provide(TestLayer)), diff --git a/apps/server/src/mcp/McpHttpServer.ts b/apps/server/src/mcp/McpHttpServer.ts index bf7cf0520668d..5a8cb573ad88b 100644 --- a/apps/server/src/mcp/McpHttpServer.ts +++ b/apps/server/src/mcp/McpHttpServer.ts @@ -410,6 +410,13 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot isError: false, structuredContent: metadata, content: [ + // Keep the page identity readable even if a provider truncates the snapshot. + { + type: "text", + text: encodeJsonText({ + url: cutText(snapshot.url, MAX_SNAPSHOT_IDENTIFIER_CHARS), + }), + }, { type: "text", text: bounded.text }, ...(bounded.omitted.length === 0 ? [] diff --git a/apps/server/src/mcp/PreviewAutomationBroker.test.ts b/apps/server/src/mcp/PreviewAutomationBroker.test.ts index 42f849f5edf32..27557d43b7014 100644 --- a/apps/server/src/mcp/PreviewAutomationBroker.test.ts +++ b/apps/server/src/mcp/PreviewAutomationBroker.test.ts @@ -127,50 +127,70 @@ it.effect("targets multiple tabs explicitly while retaining a default tab", () = ), ); -it.effect("does not let an older response replace a newer explicit tab target", () => - Effect.scoped( - Effect.gen(function* () { - const broker = yield* makeBroker; - const olderTabId = PreviewTabId.make("tab-older-request"); - const newerTabId = PreviewTabId.make("tab-newer-request"); - const releaseOlderResponse = yield* Deferred.make(); - const routedRequests: RoutedRequest[] = []; - const requests = requestsFrom(yield* broker.connect(makeHost())); - yield* Stream.runForEach(requests, (request) => { - routedRequests.push(request); - const response = Effect.gen(function* () { - if (request.tabId === olderTabId) { - yield* Deferred.await(releaseOlderResponse); - } - yield* broker.respond({ - clientId: "client-1", - connectionId: request.connectionId, - requestId: request.requestId, - ok: true, - result: { url: "http://localhost:3200" }, +it.effect.each([true, false])( + "keeps an older target stable while a newer explicit tab responds (implicit: %s)", + (implicit) => + Effect.scoped( + Effect.gen(function* () { + const broker = yield* makeBroker; + const olderTabId = PreviewTabId.make("tab-older-request"); + const newerTabId = PreviewTabId.make("tab-newer-request"); + const releaseOlderResponse = yield* Deferred.make(); + const routedRequests: RoutedRequest[] = []; + const requests = requestsFrom(yield* broker.connect(makeHost())); + yield* Stream.runForEach(requests, (request) => { + routedRequests.push(request); + const response = Effect.gen(function* () { + if (request.tabId === olderTabId && request.operation === "snapshot") { + yield* Deferred.await(releaseOlderResponse); + } + yield* broker.respond({ + clientId: "client-1", + connectionId: request.connectionId, + requestId: request.requestId, + ok: true, + result: { url: "http://localhost:3200" }, + }); + if (request.tabId === newerTabId) { + yield* Deferred.succeed(releaseOlderResponse, undefined); + } }); - if (request.tabId === newerTabId) { - yield* Deferred.succeed(releaseOlderResponse, undefined); - } + return response.pipe(Effect.forkScoped, Effect.asVoid); + }).pipe(Effect.forkScoped); + yield* Effect.yieldNow; + + yield* broker.invoke({ scope, operation: "status", input: {}, tabId: olderTabId }); + let capturedTabId: PreviewTabId | undefined; + const older = yield* broker + .invoke({ + scope, + operation: "snapshot", + input: {}, + ...(implicit ? {} : { tabId: olderTabId }), + onTargetTab: (tabId) => { + capturedTabId = tabId; + }, + }) + .pipe(Effect.forkScoped); + yield* Effect.yieldNow; + const newer = yield* broker + .invoke({ scope, operation: "snapshot", input: {}, tabId: newerTabId }) + .pipe(Effect.forkScoped); + yield* Fiber.join(newer); + yield* Fiber.join(older); + yield* broker.invoke({ + scope, + operation: "status", + input: {}, + tabId: olderTabId, + updateCurrentTab: false, }); - return response.pipe(Effect.forkScoped, Effect.asVoid); - }).pipe(Effect.forkScoped); - yield* Effect.yieldNow; - - const older = yield* broker - .invoke({ scope, operation: "snapshot", input: {}, tabId: olderTabId }) - .pipe(Effect.forkScoped); - yield* Effect.yieldNow; - const newer = yield* broker - .invoke({ scope, operation: "snapshot", input: {}, tabId: newerTabId }) - .pipe(Effect.forkScoped); - yield* Fiber.join(newer); - yield* Fiber.join(older); - yield* broker.invoke({ scope, operation: "snapshot", input: {} }); + yield* broker.invoke({ scope, operation: "snapshot", input: {} }); - expect(routedRequests.at(-1)?.tabId).toBe(newerTabId); - }), - ), + expect(routedRequests.at(-1)?.tabId).toBe(newerTabId); + expect(capturedTabId).toBe(olderTabId); + }), + ), ); it.effect("tracks the tab returned by a targeted recording stop", () => diff --git a/apps/server/src/mcp/PreviewAutomationBroker.ts b/apps/server/src/mcp/PreviewAutomationBroker.ts index d8f17973c218a..8d92059bde8a2 100644 --- a/apps/server/src/mcp/PreviewAutomationBroker.ts +++ b/apps/server/src/mcp/PreviewAutomationBroker.ts @@ -44,6 +44,10 @@ export interface PreviewAutomationInvokeInput { readonly input: unknown; readonly tabId?: PreviewTabId; readonly timeoutMs?: number; + /** Background metadata reads must not change the agent's current tab. */ + readonly updateCurrentTab?: boolean; + /** Capture the routed tab before another request changes the current assignment. */ + readonly onTargetTab?: (tabId: PreviewTabId | undefined) => void; } export class PreviewAutomationBroker extends Context.Service< @@ -541,6 +545,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() { }); } const { connection, requestId, requestContext, requestSequence } = route; + input.onTargetTab?.(requestContext.tabId); const removePending = SynchronizedRef.update(state, (next) => { if (!next.pending.has(requestId)) return next; const pending = new Map(next.pending); @@ -575,6 +580,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() { }); }); const result = yield* awaitResponse().pipe(Effect.ensuring(removePending)); + if (input.updateCurrentTab === false) return result; const responseTabId = readResultTabId(result); const resultTabId = responseTabId === undefined ? input.tabId : responseTabId; if (resultTabId === undefined) return result; diff --git a/apps/server/src/mcp/toolkits/preview/handlers.ts b/apps/server/src/mcp/toolkits/preview/handlers.ts index d34c2d3ba3afc..caa4cbd157cf9 100644 --- a/apps/server/src/mcp/toolkits/preview/handlers.ts +++ b/apps/server/src/mcp/toolkits/preview/handlers.ts @@ -7,6 +7,7 @@ import { PreviewAutomationRecordingTransferError, PreviewAutomationRecordingDesktopUpdateRequiredError, PreviewAutomationRecordingArtifact, + type ToolActivityIcon, type ThreadId, type PreviewAutomationOperation, type PreviewAutomationOpenInput, @@ -55,22 +56,47 @@ const invoke = Effect.fn("PreviewToolkit.invoke")(function* ( timeoutMs?: number, tabId?: PreviewTabId, ): Effect.fn.Return< - A, + { result: A; toolIcon?: ToolActivityIcon }, import("@t3tools/contracts").PreviewAutomationError, McpInvocationContext.McpInvocationContext | PreviewAutomationBroker.PreviewAutomationBroker > { const scope = yield* McpInvocationContext.requireMcpCapability("preview"); const broker = yield* PreviewAutomationBroker.PreviewAutomationBroker; - return yield* broker.invoke({ + let targetTabId = tabId; + const result = yield* broker.invoke({ + onTargetTab: (resolvedTabId) => { + targetTabId = resolvedTabId; + }, scope, operation, input, ...(timeoutMs === undefined ? {} : { timeoutMs }), ...(tabId === undefined ? {} : { tabId }), }); + if (["status", "open", "navigate", "snapshot"].includes(operation)) return { result }; + const statusTabId = + (operation !== "evaluate" && typeof result === "object" && result !== null + ? (result as { tabId?: PreviewTabId }).tabId + : undefined) ?? targetTabId; + const page = yield* broker + .invoke({ + scope, + operation: "status", + input: {}, + timeoutMs: 500, + updateCurrentTab: false, + ...(statusTabId === undefined ? {} : { tabId: statusTabId }), + }) + .pipe(Effect.catch(() => Effect.succeed(null))); + return { + result, + ...(page?.url && /^https?:\/\//i.test(page.url) && page.url.length <= 4096 + ? { toolIcon: { _tag: "website" as const, pageUrl: page.url } } + : {}), + }; }); -const invokeTargeted = ( +const invokeTargeted = ( operation: PreviewAutomationOperation, input: { readonly tabId?: PreviewTabId | undefined; @@ -79,7 +105,12 @@ const invokeTargeted = ( timeoutMs?: number, ) => { const { tabId, ...operationInput } = input; - return invoke(operation, operationInput, timeoutMs, tabId); + return invoke(operation, operationInput, timeoutMs, tabId).pipe( + Effect.map(({ result, toolIcon }) => ({ + ...result, + ...(toolIcon ? { toolIcon } : {}), + })), + ); }; const UploadedRecordingArtifact = Schema.Struct({ @@ -170,28 +201,32 @@ const handlers = { const { includeImage: _includeImage, save: _save, ...operationInput } = input ?? {}; return invokeTargeted("snapshot", operationInput); }, - preview_click: (input) => - invokeTargeted("click", input, input.timeoutMs).pipe(Effect.as({})), - preview_type: (input) => invokeTargeted("type", input, input.timeoutMs).pipe(Effect.as({})), - preview_press: (input) => invokeTargeted("press", input).pipe(Effect.as({})), - preview_scroll: (input) => invokeTargeted("scroll", input).pipe(Effect.as({})), - preview_evaluate: (input) => - invokeTargeted("evaluate", input).pipe( - Effect.map((result) => ({ value: result ?? null })), + preview_click: (input) => invokeTargeted("click", input, input.timeoutMs), + preview_type: (input) => invokeTargeted("type", input, input.timeoutMs), + preview_press: (input) => invokeTargeted("press", input), + preview_scroll: (input) => invokeTargeted("scroll", input), + preview_evaluate: ({ tabId, ...input }) => + invoke("evaluate", input, undefined, tabId).pipe( + Effect.map(({ result, toolIcon }) => ({ + value: result ?? null, + ...(toolIcon ? { toolIcon } : {}), + })), ), - preview_wait_for: (input) => - invokeTargeted("waitFor", input, input.timeoutMs).pipe(Effect.as({})), + preview_wait_for: (input) => invokeTargeted("waitFor", input, input.timeoutMs), preview_recording_start: (input) => invokeTargeted("recordingStart", input ?? {}), preview_recording_stop: (input) => Effect.gen(function* () { const scope = yield* McpInvocationContext.requireMcpCapability("preview"); - const response = yield* invokeTargeted( + const { tabId, ...operationInput } = input; + const response = yield* invoke( "recordingStop", - { ...input, transferToEnvironment: true }, + { ...operationInput, transferToEnvironment: true }, PREVIEW_RECORDING_STOP_TIMEOUT_MS, + tabId, ); - return yield* claimPreviewRecording(scope.threadId, response); + const artifact = yield* claimPreviewRecording(scope.threadId, response.result); + return { ...artifact, ...(response.toolIcon ? { toolIcon: response.toolIcon } : {}) }; }), } satisfies Parameters[0]; diff --git a/apps/server/src/mcp/toolkits/preview/tools.test.ts b/apps/server/src/mcp/toolkits/preview/tools.test.ts index 2cdc67ad7d924..3deef11f671e1 100644 --- a/apps/server/src/mcp/toolkits/preview/tools.test.ts +++ b/apps/server/src/mcp/toolkits/preview/tools.test.ts @@ -67,6 +67,7 @@ it("exports exact object result schemas for preview actions", () => { for (const name of actionNames) { expect(Tool.getJsonSchemaFromSchema(PreviewToolkit.tools[name].successSchema)).toEqual({ type: "object", + properties: { toolIcon: expect.any(Object) }, additionalProperties: false, description: "The preview action completed successfully.", }); diff --git a/apps/server/src/mcp/toolkits/preview/tools.ts b/apps/server/src/mcp/toolkits/preview/tools.ts index 28a2b96228b57..3f80e84e9a595 100644 --- a/apps/server/src/mcp/toolkits/preview/tools.ts +++ b/apps/server/src/mcp/toolkits/preview/tools.ts @@ -1,4 +1,5 @@ import { + ToolActivityIcon, PreviewAutomationClickInput, PreviewAutomationError, PreviewAutomationEvaluateInput, @@ -31,7 +32,9 @@ const dependencies = [ PreviewAutomationBroker.PreviewAutomationBroker, ]; -const PreviewActionResult = Schema.Record(Schema.String, Schema.Never).annotate({ +const presentationFields = { toolIcon: Schema.optional(ToolActivityIcon) }; + +const PreviewActionResult = Schema.Struct(presentationFields).annotate({ description: "The preview action completed successfully.", }); @@ -89,7 +92,7 @@ const PreviewResizeTool = safeBrowserTool( description: "Resize a collaborative browser tab, optionally selected by tabId. Use {mode:'fill'}, {mode:'freeform',width:1024,height:768}, or {mode:'preset',preset:'iphone-12-pro',orientation:'portrait'}. This changes CSS layout breakpoints without changing the desktop browser user agent.", parameters: PreviewAutomationResizeInput, - success: PreviewAutomationResizeResult, + success: Schema.Struct({ ...PreviewAutomationResizeResult.fields, ...presentationFields }), failure: PreviewAutomationError, dependencies, }) @@ -102,7 +105,10 @@ const PreviewSetAppearanceTool = safeBrowserTool( description: "Emulate prefers-color-scheme in a collaborative browser tab, optionally selected by tabId. Use {colorScheme:'dark'} or {colorScheme:'light'} to preview the page in that appearance, and {colorScheme:'system'} to clear the override and follow the OS appearance.", parameters: PreviewAutomationSetColorSchemeInput, - success: PreviewAutomationSetColorSchemeResult, + success: Schema.Struct({ + ...PreviewAutomationSetColorSchemeResult.fields, + ...presentationFields, + }), failure: PreviewAutomationError, dependencies, }) @@ -185,6 +191,7 @@ const PreviewScrollTool = safeBrowserTool( * null valid instead of failing only for non-object expressions. */ export const PreviewEvaluateResult = Schema.Struct({ + ...presentationFields, value: Schema.Unknown.annotate({ description: "The JSON-serializable value the expression produced, or null.", }), @@ -217,7 +224,7 @@ const PreviewRecordingStartTool = safeBrowserTool( description: "Start recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted.", parameters: PreviewAutomationTabTargetInput, - success: PreviewAutomationRecordingStatus, + success: Schema.Struct({ ...PreviewAutomationRecordingStatus.fields, ...presentationFields }), failure: PreviewAutomationError, dependencies, }).annotate(Tool.Title, "Start browser recording"), @@ -228,7 +235,7 @@ const PreviewRecordingStopTool = safeBrowserTool( description: "Stop recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted, and transfer the compressed recording once (up to 50 MiB) to an evidence file readable in this agent's environment. Returns its environment-local path after transfer succeeds.", parameters: PreviewAutomationTabTargetInput, - success: PreviewAutomationRecordingArtifact, + success: Schema.Struct({ ...PreviewAutomationRecordingArtifact.fields, ...presentationFields }), failure: PreviewAutomationError, dependencies: [...dependencies, FileSystem.FileSystem, ServerConfig.ServerConfig], }).annotate(Tool.Title, "Stop browser recording"), diff --git a/apps/server/src/orchestration/ActivityPayloadProjection.test.ts b/apps/server/src/orchestration/ActivityPayloadProjection.test.ts index bf09ed959e178..e6468ff8f789d 100644 --- a/apps/server/src/orchestration/ActivityPayloadProjection.test.ts +++ b/apps/server/src/orchestration/ActivityPayloadProjection.test.ts @@ -249,6 +249,84 @@ describe("projectActivityPayload", () => { expect(JSON.stringify(projected.payload).length).toBeLessThan(500); }); + it.each([ + { + item: { + server: "t3-code", + tool: "preview_open", + result: { structuredContent: { url: "https://example.com/" } }, + }, + }, + { + toolName: "mcp__t3-code__preview_navigate", + result: { content: '{"url":"https://example.com/"}' }, + }, + { tool: "t3-code_preview_status", state: { output: '{"url":"https://example.com/"}' } }, + { + toolName: "mcp__t3_code__preview_snapshot", + result: { + content: [ + { type: "text", text: '{"url":"https://example.com/"}' }, + { type: "text", text: "Snapshot text was bounded. Omitted: accessibilityTree." }, + ], + }, + }, + { + toolName: "mcp__t3-code__preview_click", + result: { content: '{"toolIcon":{"_tag":"website","pageUrl":"https://example.com/"}}' }, + }, + { + toolName: "mcp__t3_code__preview_snapshot", + result: { content: '{"url":"https://example.com/"}\n{"accessibilityTree":"truncated' }, + }, + ...[false, true].map((truncated) => ({ + toolName: "mcp__t3_code__preview_snapshot", + result: { + content: JSON.stringify({ + content: [{ type: "text", text: '{"url":"https://example.com/"}' }], + structuredContent: { url: "https://example.com/", visibleText: "page" }, + }).slice(0, truncated ? -5 : undefined), + }, + })), + ...[ + "type", + "press", + "scroll", + "resize", + "set_appearance", + "evaluate", + "wait_for", + "recording_start", + "recording_stop", + ].map((action) => ({ + toolName: `mcp__t3_code__preview_${action}`, + result: { content: '{"toolIcon":{"_tag":"website","pageUrl":"https://example.com/"}}' }, + })), + ])("preserves the preview page favicon through result slimming", (data) => { + const projected = projectActivityPayload(activity({ itemType: "mcp_tool_call", data })); + const icon = { _tag: "website", pageUrl: "https://example.com/" }; + expect(projected.payload).toMatchObject({ toolIcon: icon }); + expect(projectActivityPayload(projected).payload).toMatchObject({ toolIcon: icon }); + }); + + it.each([ + { toolName: "mcp__other__preview_open", result: { content: '{"url":"https://example.com/"}' } }, + { + toolName: "mcp__t3-code__preview_evaluate", + result: { content: '{"url":"https://example.com/"}' }, + }, + { + toolName: "mcp__t3-code__preview_open", + result: { isError: true, content: '{"url":"https://example.com/"}' }, + }, + { toolName: "mcp__t3-code__preview_open", result: { content: "malformed JSON" } }, + { toolName: "mcp__t3-code__preview_open", result: { content: '{"url":"about:blank"}' } }, + ])("keeps the fallback for unrelated tools, failed navigation, and missing page URLs", (data) => { + expect( + projectActivityPayload(activity({ itemType: "mcp_tool_call", data })).payload, + ).not.toHaveProperty("toolIcon"); + }); + it("passes task lifecycle payloads (no data field) through untouched", () => { const source = activity({ taskId: "task-9", diff --git a/apps/server/src/orchestration/ActivityPayloadProjection.ts b/apps/server/src/orchestration/ActivityPayloadProjection.ts index 43db2cfcc4ccb..0525aae7b72b2 100644 --- a/apps/server/src/orchestration/ActivityPayloadProjection.ts +++ b/apps/server/src/orchestration/ActivityPayloadProjection.ts @@ -5,6 +5,7 @@ import type { OrchestrationThreadDetailSnapshot, } from "@t3tools/contracts"; import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview"; +import { extractJsonObject } from "@t3tools/shared/schemaJson"; function asRecord(value: unknown): Record | null { return value !== null && typeof value === "object" && !Array.isArray(value) @@ -241,6 +242,70 @@ function summarizeMcpResult(result: unknown): Record | undefine return summary ? { content: summary } : undefined; } +/** Reuse the page URL already returned by preview tools before slimming their output. */ +function projectPreviewToolMetadata(data: Record, status: unknown) { + const item = asRecord(data.item); + const name = item ? `mcp__${item.server}__${item.tool}` : (data.toolName ?? data.tool); + if ( + typeof name !== "string" || + !/^(?:mcp__)?(?:t3-code|t3_code|t3code)_{1,2}preview_(?:open|navigate|status|snapshot|click|type|press|scroll|resize|set_appearance|evaluate|wait_for|recording_start|recording_stop)$/.test( + name, + ) + ) + return {}; + const state = asRecord(data.state); + const result = item?.result ?? data.result ?? state?.output; + const record = asRecord(result); + if ( + status === "failed" || + status === "declined" || + state?.status === "error" || + item?.error != null || + record?.isError === true || + record?.is_error === true + ) + return {}; + + let page = record; + let output: unknown = result; + for (let depth = 0; depth < 3; depth += 1) { + if (page?.isError === true || page?.is_error === true) return {}; + const structured = asRecord(page?.structuredContent); + if (structured) { + page = structured; + break; + } + const text = extractMcpResultText(output)?.slice(0, 2 * 1024 * 1024); + if (!text) break; + try { + page = asRecord(JSON.parse(extractJsonObject(text))); + } catch { + // A truncated MCP envelope can still contain a complete first text block. + const firstBlock = /^\s*\{\s*"content"\s*:\s*\[\s*/.exec(text); + if (!firstBlock) return {}; + try { + const block = asRecord(JSON.parse(extractJsonObject(text.slice(firstBlock[0].length)))); + page = block?.type === "text" ? { content: [block] } : null; + } catch { + return {}; + } + } + output = page; + } + const rawUrl = asTrimmedString( + asRecord(page?.toolIcon)?.pageUrl ?? + (/preview_(?:open|navigate|status|snapshot)$/.test(name) ? page?.url : undefined), + ); + if (!rawUrl || rawUrl.length > 4096) return {}; + try { + const url = new URL(rawUrl); + if (url.protocol !== "http:" && url.protocol !== "https:") return {}; + return { toolIcon: { _tag: "website", pageUrl: url.href } }; + } catch { + return {}; + } +} + /** * MCP tool calls carry full tool results (`data.item.result` on Codex, * `data.result` on Claude/OpenCode) that used to bypass slimming entirely to @@ -367,10 +432,14 @@ export function projectActivityPayload( } const itemStatus = asRecord(data.item)?.status; - const projectedPayload = + const statusPayload = payload.status === "completed" && (itemStatus === "failed" || itemStatus === "declined") ? { ...payload, status: itemStatus } : payload; + const projectedPayload = { + ...projectPreviewToolMetadata(data, statusPayload.status), + ...statusPayload, + }; const questionInput = projectQuestionToolInput(data, payload.title); if (payload.itemType === "mcp_tool_call") { From 18c5a1d2dfbb02856ec51b54edac82952852a58a Mon Sep 17 00:00:00 2001 From: maria Date: Fri, 11 Sep 2026 01:23:45 -0300 Subject: [PATCH 10/63] fix(web): simplify pull request summary sections (#10612) --- .../pullRequest/PullRequestDetailPanel.tsx | 4 +- .../pullRequest/PullRequestMarkdown.tsx | 8 +- .../components/pullRequest/PullRequestRow.tsx | 52 ++--- .../pullRequest/PullRequestSummaryTab.tsx | 188 +++++++++--------- .../pullRequest/ThreadPullRequestsPanel.tsx | 23 +-- .../pullRequest/pullRequestPresentation.tsx | 18 +- 6 files changed, 152 insertions(+), 141 deletions(-) diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx index 5b86ef5ee3ba8..0284a2e6cdecb 100644 --- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx +++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx @@ -1654,7 +1654,7 @@ export function PullRequestDetailPanel({ render={ + +
+ {(showChecks ? detail.checks : []).map((check, index) => { + const finding = { kind: "check", check } as const; + const failing = check.status === "failure" || check.status === "cancelled"; + return ( +
- - {check.name} - - {pullRequestCheckStatusLabel(check)} - - - {/* Only where there is something to fix. A passing check has no failure to - reproduce, and the button would be an invitation to waste a thread. */} - {onFixFinding && failing ? ( - - ) : null} -
- ); - })} + + {check.name} + + {pullRequestCheckStatusLabel(check)} + + + {/* Only where there is something to fix. A passing check has no failure to + reproduce, and the button would be an invitation to waste a thread. */} + {onFixFinding && failing ? ( + + ) : null} +
+ ); + })} + )} - +
0 ? ( - - ) : null + } > {activityPending ? ( diff --git a/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx b/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx index c9ef8974ef131..35a449a3da79b 100644 --- a/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx +++ b/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx @@ -29,6 +29,7 @@ import { pullRequestListLines, type PullRequestListLine } from "./pullRequestLis import { PullRequestActorAvatar, PullRequestDiffStat, + PullRequestApprovalGlyph, PullRequestStateGlyph, pullRequestChecksStatePresentation, } from "./pullRequestPresentation"; @@ -111,27 +112,23 @@ function LinkRow({ {snapshot?.title ?? link.repository} - {/* Right-aligned signals, in the order a reviewer scans them: are checks green, - has someone ruled, how big is it. Each is absent rather than neutral when the + {/* Match the full PR list: review verdict, checks, then diff counts. + Each is absent rather than neutral when the host said nothing, so a row without them reads as unknown, not as fine. */} - {snapshot?.checksState ? : null} {snapshot?.state === "open" && (snapshot.reviewDecision === "approved" || snapshot.reviewDecision === "changes-requested") ? ( - - {snapshot.reviewDecision === "approved" ? "Approved" : "Changes requested"} - + snapshot.reviewDecision === "approved" ? ( + + ) : ( + Changes requested + ) ) : null} {snapshot?.state === "open" && snapshot.mergeability === "conflicting" ? ( Conflicts ) : null} + {snapshot?.checksState ? : null} {snapshot?.updatedAt ? ( - · {formatRelativeTimeLabel(snapshot.updatedAt)} + {formatRelativeTimeLabel(snapshot.updatedAt)} ) : null} diff --git a/apps/web/src/components/pullRequest/pullRequestPresentation.tsx b/apps/web/src/components/pullRequest/pullRequestPresentation.tsx index 9cd3340832ddf..890efb67cb9bf 100644 --- a/apps/web/src/components/pullRequest/pullRequestPresentation.tsx +++ b/apps/web/src/components/pullRequest/pullRequestPresentation.tsx @@ -17,6 +17,7 @@ import { GitPullRequestDraftIcon, GitPullRequestIcon, TriangleAlertIcon, + UserCheckIcon, } from "lucide-react"; import { Children, isValidElement, type ReactNode } from "react"; @@ -32,6 +33,21 @@ interface StatePresentation { readonly Icon: typeof GitPullRequestIcon; } +export function PullRequestApprovalGlyph() { + return ( + + }> + + Approved + + Approved + + ); +} + /** * How a pull request's state reads on this page. Open, closed, merged, and draft use the same * ink as the thread badge in `ThreadStatusIndicators`, so one pull request cannot look like two @@ -173,7 +189,7 @@ const CHECKS_STATE_PRESENTATION = { passing: { label: "All checks have passed", Icon: CircleCheckIcon, - toneClassName: "text-emerald-600 dark:text-emerald-300/90", + toneClassName: CHECK_STATUS_PRESENTATION.success.toneClassName, }, failing: { label: "Some checks were not successful", From ef6fa118749572da1905eca5d0543d1e98f0b1b2 Mon Sep 17 00:00:00 2001 From: maria Date: Fri, 11 Sep 2026 01:46:33 -0300 Subject: [PATCH 11/63] fix(web): preserve drafts when compacting context (#11103) --- apps/web/src/components/ChatView.tsx | 87 +++++++++++++++++-- apps/web/src/components/chat/ChatComposer.tsx | 33 ++----- 2 files changed, 84 insertions(+), 36 deletions(-) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 283e853656674..7a0e6a719326d 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -5881,15 +5881,13 @@ export default function ChatView(props: ChatViewProps) { pendingApprovals.length > 0 || pendingUserInputs.length > 0 || showPlanFollowUpPrompt; - const compactDisabled = compactThreadUnavailable || composerHasUnsentContent; + const compactDisabled = compactThreadUnavailable; const compactDisabledReason = compactDisabled - ? composerHasUnsentContent - ? "Send or clear your draft before compacting" - : !activeProject - ? "Choose a project before compacting" - : !manualCompactionProviderAvailable - ? "Compaction is unavailable for this provider" - : "Compacting is unavailable right now" + ? !activeProject + ? "Choose a project before compacting" + : !manualCompactionProviderAvailable + ? "Compaction is unavailable for this provider" + : "Compacting is unavailable right now" : null; const resumeCompactionBannerItem = useMemo(() => { if ( @@ -6479,6 +6477,78 @@ export default function ChatView(props: ChatViewProps) { ], ); + const onCompactContext = async () => { + if (compactDisabled || !activeThread || !clientSettingsHydrated || sendInFlightRef.current) { + return; + } + const context = composerRef.current?.getSendContext(); + if (!context?.providerAvailable) return; + + // Compaction is a standalone command; the draft and its attachments stay local. + const threadId = activeThread.id; + const messageId = newMessageId(); + const createdAt = new Date().toISOString(); + sendInFlightRef.current = true; + beginLocalDispatch(); + setThreadError(threadId, null); + setOptimisticUserMessages((messages) => [ + ...messages, + { + id: messageId, + role: "user", + text: "/compact", + turnId: null, + createdAt, + updatedAt: createdAt, + streaming: false, + }, + ]); + scrollToEnd(); + try { + const settingsResult = await persistThreadSettingsForNextTurn({ + threadId, + createdAt, + modelSelection: context.selectedModelSelection, + ...(localCheckoutBranchMismatch + ? { branch: localCheckoutBranchMismatch.currentBranch } + : {}), + runtimeMode, + interactionMode: context.interactionMode, + }); + const result = + settingsResult._tag === "Failure" + ? settingsResult + : await startThreadTurn({ + environmentId, + input: { + threadId, + message: { messageId, role: "user", text: "/compact", attachments: [] }, + modelSelection: context.selectedModelSelection, + runtimeMode, + interactionMode: context.interactionMode, + createdAt, + }, + }); + if (result._tag === "Failure") { + setOptimisticUserMessages((messages) => + messages.filter((message) => message.id !== messageId), + ); + resetLocalDispatch(); + if (!isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + setThreadError( + threadId, + error instanceof Error ? error.message : "Failed to compact context.", + ); + } + } else { + clearUsageLimitsFor(routeThreadKey); + } + } finally { + sendInFlightRef.current = false; + } + }; + const onSend = async ( e?: { preventDefault: () => void }, submissionIntent: ComposerSubmissionIntent = "foreground", @@ -8575,6 +8645,7 @@ export default function ChatView(props: ChatViewProps) { onPageScrollKeyDown={onComposerPageScrollKeyDown} onPageScrollKeyUp={onComposerPageScrollKeyUp} onPageScrollRelease={onComposerPageScrollRelease} + onCompactContext={onCompactContext} onSend={onSend} onInterrupt={onInterrupt} onImplementPlanInNewThread={onImplementPlanInNewThread} diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 7291f8cead22a..7ecb5e2dc6352 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -1354,6 +1354,7 @@ export interface ChatComposerProps { onPageScrollRelease: () => void; // Callbacks + onCompactContext: () => void; onSend: (e?: { preventDefault: () => void }, intent?: ComposerSubmissionIntent) => void; onInterrupt: () => void; onImplementPlanInNewThread: () => void; @@ -1460,6 +1461,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) onPageScrollKeyDown, onPageScrollKeyUp, onPageScrollRelease, + onCompactContext, onSend, onInterrupt, onImplementPlanInNewThread, @@ -1995,7 +1997,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) /** * Count of pasted images still being compressed, per thread. Reserved * against the attachment limit so concurrent pastes can't overshoot it, - * and checked before sending or compacting so an image cannot move into + * and checked before sending so an image cannot move into * the next draft. */ const pendingImageCompressionsRef = useRef>(new Map()); @@ -3097,42 +3099,17 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) ) { return; } - // The compact buttons cannot see the compression counter (it lives in - // a ref), so they render enabled during a paste; toast instead of - // silently ignoring the click. - if ((pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) > 0) { - toastManager.add({ - type: "info", - title: "Still compressing a pasted image.", - description: "Compact again once its thumbnail appears.", - }); - return; - } - - promptRef.current = "/compact"; - setComposerDraftPrompt(composerDraftTarget, "/compact"); - submitComposer(); - // A blocked dispatch (busy send ref, provider preflight rejection) - // would leave the injected "/compact" behind as if the user typed it. - // Clearing here is safe even when the send did dispatch: the send - // snapshots its prompt synchronously and clears the draft itself. - if (promptRef.current === "/compact") { - promptRef.current = ""; - setComposerDraftPrompt(composerDraftTarget, ""); - } + onCompactContext(); }, [ activePendingApproval, activeThreadId, compactDisabled, - composerDraftTarget, isConnecting, isSendBusy, noProviderAvailable, + onCompactContext, pendingUserInputs.length, phase, - promptRef, - setComposerDraftPrompt, - submitComposer, ]); const expandMobileComposer = useCallback(() => { if (composerBlurFrameRef.current !== null) { From 57aee3e19f1910f3323f06384bb2a1d02b79e369 Mon Sep 17 00:00:00 2001 From: maria Date: Fri, 11 Sep 2026 01:46:46 -0300 Subject: [PATCH 12/63] fix(server): queue messages during context compaction (#11107) Co-authored-by: Claude Fable 5.1 --- .../Layers/ProviderCommandReactor.test.ts | 310 +++++++++++++----- .../Layers/ProviderCommandReactor.ts | 157 ++++++++- 2 files changed, 374 insertions(+), 93 deletions(-) diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts index d93fec5a3cf68..c927e72fe737c 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts @@ -175,6 +175,8 @@ describe("ProviderCommandReactor", () => { readonly titleRegenerationBeforeStart?: "one" | "two"; readonly serverActivation?: Effect.Effect; readonly beforeReadySessionDispatch?: () => Effect.Effect; + readonly beforeTurnStartDispatch?: () => Effect.Effect; + readonly afterTurnStartDispatch?: () => Effect.Effect; readonly compactThreadEffect?: () => Effect.Effect; readonly interruptTurnEffect?: () => Effect.Effect; readonly stopSessionEffect?: () => Effect.Effect; @@ -431,11 +433,21 @@ describe("ProviderCommandReactor", () => { return Effect.die(new Error("Injected title regeneration completion failure")); } } - return ( + const isReplay = + command.type === "thread.turn.start" && + command.commandId.startsWith("server:after-compaction:"); + const before = command.type === "thread.session.set" && command.session.status === "ready" - ? (input?.beforeReadySessionDispatch?.() ?? Effect.void) - : Effect.void - ).pipe(Effect.andThen(engine.dispatch(command))); + ? input?.beforeReadySessionDispatch + : isReplay + ? input?.beforeTurnStartDispatch + : undefined; + return (before?.() ?? Effect.void).pipe( + Effect.andThen(engine.dispatch(command)), + Effect.tap(() => + isReplay ? (input?.afterTurnStartDispatch?.() ?? Effect.void) : Effect.void, + ), + ); }, get streamDomainEvents() { return engine.streamDomainEvents; @@ -973,86 +985,203 @@ describe("ProviderCommandReactor", () => { }), ); - effectIt.effect("keeps turns blocked until compaction restores the session", () => - Effect.gen(function* () { - const readyDispatchStarted = yield* Deferred.make(); - const releaseReadyDispatch = yield* Deferred.make(); - let blockReadyDispatch = false; - const harness = yield* Effect.promise(() => - createHarness({ - beforeReadySessionDispatch: () => - blockReadyDispatch - ? Deferred.succeed(readyDispatchStarted, undefined).pipe( - Effect.andThen(Deferred.await(releaseReadyDispatch)), - ) - : Effect.void, - }), - ); - const threadId = ThreadId.make("thread-1"); - const now = "2026-01-01T00:00:00.000Z"; - const dispatchTurn = (id: string, text: string, createdAt: string) => - harness.engine.dispatch({ - type: "thread.turn.start", - commandId: CommandId.make(`cmd-${id}`), + effectIt.effect.each(["resume", "stop before resume", "stop after send"])( + "queues messages until compaction restores the session (%s)", + (scenario) => + Effect.gen(function* () { + const stopBeforeResume = scenario === "stop before resume"; + const readyDispatchStarted = yield* Deferred.make(); + const releaseReadyDispatch = yield* Deferred.make(); + const firstSent = yield* Deferred.make(); + const queuedSent = yield* Deferred.make(); + const resumeStarted = yield* Deferred.make(); + const releaseResume = yield* Deferred.make(); + const resumeDispatched = yield* Deferred.make(); + const queuedSendStarted = yield* Deferred.make(); + const releaseQueuedSend = yield* Deferred.make(); + let blockReadyDispatch = false; + const harness = yield* Effect.promise(() => + createHarness({ + beforeTurnStartDispatch: () => + stopBeforeResume + ? Deferred.succeed(resumeStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseResume)), + ) + : Effect.void, + afterTurnStartDispatch: () => Deferred.succeed(resumeDispatched, undefined), + beforeReadySessionDispatch: () => + blockReadyDispatch + ? Deferred.succeed(readyDispatchStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseReadyDispatch)), + ) + : Effect.void, + }), + ); + const threadId = ThreadId.make("thread-1"); + let sentCount = 0; + harness.sendTurn.mockImplementation(() => + Effect.succeed({ threadId, turnId: asTurnId("turn-1") }).pipe( + Effect.tap(() => { + sentCount++; + return sentCount === 1 + ? Deferred.succeed(firstSent, undefined) + : sentCount === 2 && scenario === "stop after send" + ? Deferred.succeed(queuedSendStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseQueuedSend)), + ) + : sentCount === 3 + ? Deferred.succeed(queuedSent, undefined) + : Effect.void; + }), + ), + ); + const now = "2026-01-01T00:00:00.000Z"; + const dispatchTurn = (id: string, text: string, createdAt: string) => + harness.engine.dispatch({ + type: "thread.turn.start", + commandId: CommandId.make(`cmd-${id}`), + threadId, + message: { + messageId: asMessageId(`user-message-${id}`), + role: "user", + text, + attachments: [], + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "approval-required", + createdAt, + }); + + yield* dispatchTurn("before-blocked-compact", "hello", now); + yield* Deferred.await(firstSent); + yield* harness.engine.dispatch({ + type: "thread.session.set", + commandId: CommandId.make("cmd-session-ready-before-blocked-compact"), threadId, - message: { - messageId: asMessageId(`user-message-${id}`), - role: "user", - text, - attachments: [], + session: { + threadId, + status: "ready", + providerName: "codex", + providerInstanceId: ProviderInstanceId.make("codex"), + runtimeMode: "approval-required", + activeTurnId: null, + lastError: null, + updatedAt: now, }, - interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, - runtimeMode: "approval-required", - createdAt, + createdAt: now, }); - yield* dispatchTurn("before-blocked-compact", "hello", now); - yield* Effect.promise(() => waitFor(() => harness.sendTurn.mock.calls.length === 1)); - yield* harness.engine.dispatch({ - type: "thread.session.set", - commandId: CommandId.make("cmd-session-ready-before-blocked-compact"), - threadId, - session: { - threadId, - status: "ready", - providerName: "codex", - providerInstanceId: ProviderInstanceId.make("codex"), - runtimeMode: "approval-required", - activeTurnId: null, - lastError: null, - updatedAt: now, - }, - createdAt: now, - }); - - blockReadyDispatch = true; - yield* dispatchTurn("blocked-compact", "/compact", "2026-01-01T00:00:01.000Z"); - yield* Deferred.await(readyDispatchStarted); + blockReadyDispatch = true; + yield* dispatchTurn("blocked-compact", "/compact", "2026-01-01T00:00:01.000Z"); + yield* Deferred.await(readyDispatchStarted); - yield* dispatchTurn("during-compact-recovery", "too soon", "2026-01-01T00:00:02.000Z"); - yield* Effect.promise(() => - waitFor(async () => { - const thread = (await harness.readModel()).threads.find((entry) => entry.id === threadId); - return ( - thread?.activities.some( - (activity) => activity.kind === "provider.turn.start.failed", - ) === true + yield* harness.engine.dispatch({ + type: "thread.interaction-mode.set", + commandId: CommandId.make("cmd-queued-mode-plan"), + threadId, + interactionMode: "plan", + createdAt: now, + }); + yield* dispatchTurn("during-compact-recovery", "first queued", "2026-01-01T00:00:02.000Z"); + yield* harness.engine.dispatch({ + type: "thread.interaction-mode.set", + commandId: CommandId.make("cmd-queued-mode-default"), + threadId, + interactionMode: "default", + createdAt: now, + }); + yield* dispatchTurn( + "during-compact-recovery-2", + "second queued", + "2026-01-01T00:00:03.000Z", + ); + yield* Effect.promise(() => harness.drain()); + expect(harness.sendTurn).toHaveBeenCalledTimes(1); + const beforeRestore = (yield* Effect.promise(() => harness.readModel())).threads.find( + (entry) => entry.id === threadId, + ); + expect( + beforeRestore?.activities.filter( + (activity) => activity.kind === "provider.turn.start.failed", + ), + ).toEqual([]); + expect(yield* Effect.promise(() => harness.readPendingTurnStarts())).toEqual([ + { threadId: "thread-1" }, + ]); + + yield* Deferred.succeed(releaseReadyDispatch, undefined); + if (scenario === "stop after send") { + yield* Deferred.await(queuedSendStarted); + yield* harness.engine.dispatch({ + type: "thread.session.stop", + commandId: CommandId.make("cmd-stop-after-queued-send"), + threadId, + createdAt: "2026-01-01T00:00:04.000Z", + }); + yield* Effect.promise(() => harness.drain()); + const stoppedThread = (yield* Effect.promise(() => harness.readModel())).threads.find( + (entry) => entry.id === threadId, ); - }), - ); - expect(harness.sendTurn).toHaveBeenCalledTimes(1); - expect(yield* Effect.promise(() => harness.readPendingTurnStarts())).toEqual([ - { threadId: "thread-1" }, - ]); - - yield* Deferred.succeed(releaseReadyDispatch, undefined); - yield* Effect.promise(() => - waitFor(async () => { - const thread = (await harness.readModel()).threads.find((entry) => entry.id === threadId); - return thread?.session?.status === "ready"; - }), - ); - }), + expect(stoppedThread?.session?.status).toBe("stopped"); + expect( + stoppedThread?.activities.filter( + (activity) => activity.summary === "Queued message was not sent", + ), + ).toEqual([ + expect.objectContaining({ + payload: { + requestId: "user-message-during-compact-recovery-2", + detail: expect.any(String), + }, + }), + ]); + expect(harness.sendTurn).toHaveBeenCalledTimes(2); + yield* Deferred.succeed(releaseQueuedSend, undefined); + return; + } + if (stopBeforeResume) { + yield* Deferred.await(resumeStarted); + yield* dispatchTurn("compact-during-resume", "/compact", "2026-01-01T00:00:04.000Z"); + yield* Effect.promise(() => harness.drain()); + expect(harness.compactThread).toHaveBeenCalledTimes(1); + yield* harness.engine.dispatch({ + type: "thread.session.stop", + commandId: CommandId.make("cmd-stop-before-queued-resume"), + threadId, + createdAt: "2026-01-01T00:00:04.000Z", + }); + yield* Effect.promise(() => harness.drain()); + yield* Deferred.succeed(releaseResume, undefined); + yield* Deferred.await(resumeDispatched); + yield* Effect.promise(() => harness.drain()); + expect(harness.sendTurn).toHaveBeenCalledTimes(1); + const stoppedThread = (yield* Effect.promise(() => harness.readModel())).threads.find( + (entry) => entry.id === threadId, + ); + expect(stoppedThread?.session?.status).toBe("stopped"); + expect(yield* Effect.promise(() => harness.readPendingTurnStarts())).toEqual([]); + expect( + stoppedThread?.activities.filter( + (activity) => activity.summary === "Queued message was not sent", + ), + ).toHaveLength(2); + return; + } + yield* Deferred.await(queuedSent); + expect(harness.sendTurn.mock.calls.slice(1).map(([request]) => request)).toEqual([ + expect.objectContaining({ input: "first queued", interactionMode: "plan" }), + expect.objectContaining({ input: "second queued", interactionMode: "default" }), + ]); + const afterRestore = (yield* Effect.promise(() => harness.readModel())).threads.find( + (entry) => entry.id === threadId, + ); + expect( + afterRestore?.messages.filter((message) => message.text === "first queued"), + ).toHaveLength(1); + expect( + afterRestore?.messages.filter((message) => message.text === "second queued"), + ).toHaveLength(1); + }), ); effectIt.effect("does not overwrite concurrent session state after compaction failure", () => @@ -1136,6 +1265,21 @@ describe("ProviderCommandReactor", () => { (entry) => entry.id === threadId, ); expect(compactingThread?.session?.status).toBe("starting"); + yield* harness.engine.dispatch({ + type: "thread.turn.start", + commandId: CommandId.make("cmd-queued-before-stop"), + threadId, + message: { + messageId: asMessageId("user-message-queued-before-stop"), + role: "user", + text: "do not restart after stopping", + attachments: [], + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "approval-required", + createdAt: now, + }); + yield* Effect.promise(() => harness.drain()); yield* harness.engine.dispatch({ type: "thread.session.stop", commandId: CommandId.make("cmd-stop-during-compact"), @@ -1151,7 +1295,7 @@ describe("ProviderCommandReactor", () => { ); return ( compactingThread?.activities.some( - (activity) => activity.kind === "provider.turn.start.failed", + (activity) => activity.summary === "Context compaction failed", ) === true ); }), @@ -1167,6 +1311,14 @@ describe("ProviderCommandReactor", () => { (entry) => entry.id === threadId, ); expect(recoveredThread?.session?.status).toBe("ready"); + expect(harness.sendTurn).toHaveBeenCalledTimes(1); + expect( + recoveredThread?.activities.find( + (activity) => activity.summary === "Queued message was not sent", + ), + ).toMatchObject({ + payload: { requestId: "user-message-queued-before-stop" }, + }); expect( recoveredThread?.activities.find( (activity) => activity.kind === "provider.session.stop.failed", diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts index 6df08bfadb9c7..9b125922137cb 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts @@ -18,6 +18,7 @@ import * as Cache from "effect/Cache"; import * as Cause from "effect/Cause"; import * as Crypto from "effect/Crypto"; import * as DateTime from "effect/DateTime"; +import * as Deferred from "effect/Deferred"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Equal from "effect/Equal"; @@ -346,6 +347,19 @@ const make = Effect.gen(function* () { const threadModelSelections = new Map(); const compactingThreadIds = new Set(); + type QueuedTurnStart = Extract; + // Turn starts received while a thread compacts, replayed in order once its session is restored. + const turnsAfterCompaction = new Map>(); + // Replay command id → the queued turn start it re-requests. `sent` settles once the replay's + // provider send finishes, which is what lets the next queued turn follow it in order. + const resumedTurnStarts = new Map< + CommandId, + { + readonly event: QueuedTurnStart; + readonly queued: Array; + readonly sent: Deferred.Deferred; + } + >(); const stoppingThreadIds = new Set(); const appendProviderFailureActivity = (input: { @@ -388,6 +402,71 @@ const make = Effect.gen(function* () { ), ); + const cancelTurnsAfterCompaction = Effect.fn("cancelTurnsAfterCompaction")(function* ( + threadId: ThreadId, + detail: string, + ) { + const queued = turnsAfterCompaction.get(threadId) ?? []; + turnsAfterCompaction.delete(threadId); + for (const event of queued) { + yield* appendProviderFailureActivity({ + threadId, + kind: "provider.turn.start.failed", + summary: "Queued message was not sent", + detail, + turnId: null, + createdAt: DateTime.formatIso(yield* DateTime.now), + requestId: event.payload.messageId, + }).pipe(Effect.ignore({ log: true, message: "failed to report canceled queued message" })); + } + }); + + const resumeTurnsAfterCompaction = Effect.fn("resumeTurnsAfterCompaction")(function* ( + threadId: ThreadId, + ) { + const queued = turnsAfterCompaction.get(threadId) ?? []; + while (queued.length > 0 && turnsAfterCompaction.get(threadId) === queued) { + const event = queued[0]!; + const turnStart = yield* projectionSnapshotQuery.getTurnStartMessage({ + threadId, + messageId: event.payload.messageId, + }); + if (turnsAfterCompaction.get(threadId) !== queued) return; + // In flight from here on: a cancellation reports it when the replay runs, not from the queue. + queued.shift(); + if (Option.isNone(turnStart)) continue; + // Reissue the durable request after restoration clears compaction's + // pending slot. Reusing the message id preserves a single user bubble. + const commandId = yield* serverCommandId("after-compaction"); + const sent = yield* Deferred.make(); + resumedTurnStarts.set(commandId, { event, queued, sent }); + const { messageId, ...request } = event.payload; + yield* orchestrationEngine + .dispatch({ + type: "thread.turn.start", + commandId, + ...request, + message: { + messageId, + role: "user", + text: turnStart.value.message.text, + attachments: turnStart.value.message.attachments ?? [], + }, + }) + .pipe( + Effect.onError(() => + Effect.sync(() => { + resumedTurnStarts.delete(commandId); + queued.unshift(event); + }), + ), + ); + yield* Deferred.await(sent); + resumedTurnStarts.delete(commandId); + } + if (turnsAfterCompaction.get(threadId) === queued) turnsAfterCompaction.delete(threadId); + }); + const formatFailureDetail = (cause: Cause.Cause): string => { const failReason = cause.reasons.find(Cause.isFailReason); if (isProviderAdapterRequestError(failReason?.error)) { @@ -1181,8 +1260,11 @@ const make = Effect.gen(function* () { ); const processTurnStartRequested = Effect.fn("processTurnStartRequested")(function* ( - event: Extract, + receivedEvent: Extract, ) { + const resumed = + receivedEvent.commandId !== null ? resumedTurnStarts.get(receivedEvent.commandId) : undefined; + const event = resumed ? { ...receivedEvent, payload: resumed.event.payload } : receivedEvent; const key = turnStartKeyForEvent(event); if (yield* hasHandledTurnStartRecently(key)) { return; @@ -1219,6 +1301,12 @@ const make = Effect.gen(function* () { createdAt: event.payload.createdAt, requestId: event.payload.messageId, }); + if (resumed && turnsAfterCompaction.get(event.payload.threadId) !== resumed.queued) { + return yield* appendTurnStartFailure( + "Queued message was not sent", + "The queued message was canceled before it could resume. Send it again to continue.", + ); + } const handleTurnStartFailure = (cause: Cause.Cause) => { if (Cause.hasInterruptsOnly(cause)) { @@ -1381,6 +1469,7 @@ const make = Effect.gen(function* () { const latestThread = yield* resolveThreadShell(event.payload.threadId); if ( compactingThreadIds.has(event.payload.threadId) || + turnsAfterCompaction.has(event.payload.threadId) || latestThread?.session?.status === "starting" || latestThread?.session?.status === "running" ) { @@ -1391,6 +1480,9 @@ const make = Effect.gen(function* () { return; } compactingThreadIds.add(event.payload.threadId); + const clearCompacting = Effect.sync( + () => void compactingThreadIds.delete(event.payload.threadId), + ); yield* Effect.gen(function* () { yield* ensureSessionForThread( event.payload.threadId, @@ -1410,17 +1502,32 @@ const make = Effect.gen(function* () { ); }).pipe( Effect.andThen(restoreCompaction(event.payload.threadId, true)), - Effect.catchCause(recoverCompactionFailure), - Effect.ensuring(Effect.sync(() => void compactingThreadIds.delete(event.payload.threadId))), + Effect.andThen(clearCompacting), + Effect.andThen(resumeTurnsAfterCompaction(event.payload.threadId)), + Effect.catchCause((cause) => + recoverCompactionFailure(cause).pipe( + Effect.ensuring(clearCompacting), + Effect.andThen( + cancelTurnsAfterCompaction( + event.payload.threadId, + "Context compaction failed. Send this message again to continue.", + ), + ), + ), + ), Effect.forkScoped, ); return; } - if (compactingThreadIds.has(event.payload.threadId)) { - return yield* appendTurnStartFailure( - "Provider turn start failed", - "Wait for context compaction to finish before sending another message.", - ); + if ( + !resumed && + (compactingThreadIds.has(event.payload.threadId) || + turnsAfterCompaction.has(event.payload.threadId)) + ) { + const queued = turnsAfterCompaction.get(event.payload.threadId) ?? []; + queued.push(event); + turnsAfterCompaction.set(event.payload.threadId, queued); + return; } const sendTurnRequest = yield* buildSendTurnRequestForThread({ threadId: event.payload.threadId, @@ -1440,14 +1547,24 @@ const make = Effect.gen(function* () { return; } - yield* providerService + const send = providerService .sendTurn(sendTurnRequest.value) - .pipe(Effect.asVoid, Effect.catchCause(recoverTurnStartFailure), Effect.forkScoped); + .pipe(Effect.asVoid, Effect.catchCause(recoverTurnStartFailure)); + // The forked send settles `sent` from here on, so drop the entry the post-processing hook uses. + if (resumed && event.commandId !== null) resumedTurnStarts.delete(event.commandId); + yield* send.pipe( + Effect.ensuring(resumed ? Deferred.succeed(resumed.sent, undefined) : Effect.void), + Effect.forkScoped, + ); }); const processTurnInterruptRequested = Effect.fn("processTurnInterruptRequested")(function* ( event: Extract, ) { + yield* cancelTurnsAfterCompaction( + event.payload.threadId, + "Context compaction was interrupted. Send this message again to continue.", + ); const thread = yield* resolveThreadShell(event.payload.threadId); if (!thread) { return; @@ -1643,11 +1760,15 @@ const make = Effect.gen(function* () { const wasCompacting = compactingThreadIds.has(thread.id); stoppingThreadIds.add(thread.id); const clearStopping = Effect.sync(() => void stoppingThreadIds.delete(thread.id)); - yield* ( - thread.session && thread.session.status !== "stopped" - ? providerService.stopSession({ threadId: thread.id }) - : Effect.void + yield* cancelTurnsAfterCompaction( + thread.id, + "The session was stopped during context compaction. Send this message again to continue.", ).pipe( + Effect.andThen( + thread.session && thread.session.status !== "stopped" + ? providerService.stopSession({ threadId: thread.id }) + : Effect.void, + ), Effect.matchCauseEffect({ onFailure: (cause) => { if (Cause.hasInterruptsOnly(cause)) { @@ -1761,6 +1882,14 @@ const make = Effect.gen(function* () { const processDomainEventSafely = (event: ProviderIntentEvent) => processDomainEvent(event).pipe( + // A replay that returned before forking its send still holds its entry; settle it so + // the compaction queue moves on. Forked sends drop the entry first and settle it themselves. + Effect.ensuring( + Effect.suspend(() => { + const resumed = event.commandId !== null && resumedTurnStarts.get(event.commandId); + return resumed ? Deferred.succeed(resumed.sent, undefined) : Effect.void; + }), + ), Effect.catchCause((cause) => { if (Cause.hasInterruptsOnly(cause)) { return Effect.interrupt; From 8fc253605e6d203c3654dbb1ce22fa1fd6fa0767 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 10 Sep 2026 23:50:31 -0700 Subject: [PATCH 13/63] perf(web): format minimap previews only when opened (#11181) --- .../src/components/chat/MessagesTimeline.tsx | 65 +++------------ .../chat/timelineMinimapItems.test.ts | 81 +++++++++++++++++++ .../components/chat/timelineMinimapItems.ts | 66 +++++++++++++++ 3 files changed, 159 insertions(+), 53 deletions(-) create mode 100644 apps/web/src/components/chat/timelineMinimapItems.test.ts create mode 100644 apps/web/src/components/chat/timelineMinimapItems.ts diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index 13c1e1dc61b32..41e3a0740a95d 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -4,6 +4,11 @@ import { getQuestionAnswerText, hasQuestionAnswer, } from "@t3tools/client-runtime/work-log/user-input"; +import { + deriveTimelineMinimapItems, + resolveTimelineMinimapPreview, + type TimelineMinimapItem, +} from "./timelineMinimapItems"; import { type AssistantCitation, type EnvironmentId, @@ -918,13 +923,6 @@ function getItemType(item: MessagesTimelineRow) { return item.kind === "message" ? `message:${item.message.role}` : item.kind; } -interface TimelineMinimapItem { - readonly id: string; - readonly rowIndex: number; - readonly userText: string | null; - readonly assistantText: string | null; -} - interface TimelinePositionState { readonly contentLength?: number; readonly scroll?: number; @@ -933,51 +931,6 @@ interface TimelinePositionState { readonly sizeAtIndex?: (index: number) => number | undefined; } -function deriveTimelineMinimapItems( - rows: ReadonlyArray, -): TimelineMinimapItem[] { - const items: TimelineMinimapItem[] = []; - for (let index = 0; index < rows.length; index += 1) { - const row = rows[index]; - if (row?.kind !== "message" || row.message.role !== "user") { - continue; - } - - items.push({ - id: row.id, - rowIndex: index, - userText: compactMinimapPreview(row.message.text), - assistantText: compactMinimapPreview(resolveFinalAssistantTextForTurn(rows, index)), - }); - } - return items; -} - -function resolveFinalAssistantTextForTurn( - rows: ReadonlyArray, - userRowIndex: number, -) { - let finalAssistantText: string | null = null; - for (let index = userRowIndex + 1; index < rows.length; index += 1) { - const row = rows[index]; - if (row?.kind !== "message") { - continue; - } - if (row.message.role === "user") { - break; - } - if (row.message.role === "assistant") { - finalAssistantText = row.message.text ?? null; - } - } - return finalAssistantText; -} - -function compactMinimapPreview(text: string | null | undefined) { - const compact = text?.replace(/\s+/g, " ").trim() ?? ""; - return compact.length > 0 ? compact : null; -} - function resolveTimelineRowTop(state: TimelinePositionState, rowIndex: number) { const top = state.positionAtIndex?.(rowIndex); return typeof top === "number" && Number.isFinite(top) ? top : null; @@ -1011,7 +964,13 @@ function TimelineMinimap({ const resolvedActiveIndex = activeIndex !== null && activeIndex < items.length ? activeIndex : null; - const activeItem = resolvedActiveIndex === null ? null : (items[resolvedActiveIndex] ?? null); + const activeItem = useMemo( + () => + resolveTimelineMinimapPreview( + resolvedActiveIndex === null ? null : (items[resolvedActiveIndex] ?? null), + ), + [items, resolvedActiveIndex], + ); const activeTopPercent = resolvedActiveIndex === null ? 0 diff --git a/apps/web/src/components/chat/timelineMinimapItems.test.ts b/apps/web/src/components/chat/timelineMinimapItems.test.ts new file mode 100644 index 0000000000000..f7c40aa712065 --- /dev/null +++ b/apps/web/src/components/chat/timelineMinimapItems.test.ts @@ -0,0 +1,81 @@ +import { describe, expect, it } from "vite-plus/test"; +import { MessageId } from "@t3tools/contracts"; +import type { MessagesTimelineRow } from "./MessagesTimeline.logic"; +import { deriveTimelineMinimapItems, resolveTimelineMinimapPreview } from "./timelineMinimapItems"; +import type { ChatMessage } from "../../types"; + +function rows( + entries: ReadonlyArray, +): MessagesTimelineRow[] { + const messages: ChatMessage[] = entries.map(([role, text], index) => ({ + id: MessageId.make(`message-${index}`), + role, + text, + streaming: false, + turnId: null, + createdAt: new Date(index * 1000).toISOString(), + updatedAt: new Date(index * 1000).toISOString(), + })); + return messages.map((message) => ({ + kind: "message", + id: message.id, + createdAt: message.createdAt, + message, + durationStart: message.createdAt, + showAssistantMeta: false, + showAssistantCopyButton: false, + assistantCopyStreaming: false, + })); +} + +describe("timeline minimap previews", () => { + it("previews the last assistant response before the next prompt and retains jump targets", () => { + const source = rows([ + ["user", " Inspect\n this "], + ["assistant", "Working"], + ["assistant", " Done\t now "], + ["user", "Next"], + ["assistant", "Second answer"], + ]); + const items = deriveTimelineMinimapItems(source); + expect(items).toHaveLength(2); + expect(resolveTimelineMinimapPreview(items[0]!)).toEqual({ + ...items[0], + userText: "Inspect this", + assistantText: "Done now", + }); + expect(source[items[0]!.rowIndex]!.id).toBe(items[0]!.id); + expect(resolveTimelineMinimapPreview(items[1]!)?.assistantText).toBe("Second answer"); + expect(items[0]?.assistantText).toBe(" Done\t now "); + }); + + it("handles an unanswered prompt, empty responses, and a closed preview", () => { + const items = deriveTimelineMinimapItems( + rows([ + ["user", "First"], + ["assistant", " \n\t"], + ["user", "Next"], + ]), + ); + expect(items.map((item) => resolveTimelineMinimapPreview(item)?.assistantText)).toEqual([ + null, + null, + ]); + expect(resolveTimelineMinimapPreview(null)).toBeNull(); + }); + + it("shows fresh streaming text without changing the jump target", () => { + const first = deriveTimelineMinimapItems( + rows([ + ["user", "Explain"], + ["assistant", "First"], + ]), + )[0]!; + const next = { ...first, assistantText: "First\n second" }; + expect(resolveTimelineMinimapPreview(next)).toEqual({ + ...first, + assistantText: "First second", + }); + expect(resolveTimelineMinimapPreview(first)?.assistantText).toBe("First"); + }); +}); diff --git a/apps/web/src/components/chat/timelineMinimapItems.ts b/apps/web/src/components/chat/timelineMinimapItems.ts new file mode 100644 index 0000000000000..0a37c686e780f --- /dev/null +++ b/apps/web/src/components/chat/timelineMinimapItems.ts @@ -0,0 +1,66 @@ +import type { MessagesTimelineRow } from "./MessagesTimeline.logic"; + +export interface TimelineMinimapItem { + readonly id: string; + readonly rowIndex: number; + readonly userText: string | null; + readonly assistantText: string | null; +} + +/** Keep full source text untouched until a minimap preview is opened. */ +export function deriveTimelineMinimapItems( + rows: ReadonlyArray, +): TimelineMinimapItem[] { + const items: TimelineMinimapItem[] = []; + for (let index = 0; index < rows.length; index += 1) { + const row = rows[index]; + if (row?.kind !== "message" || row.message.role !== "user") { + continue; + } + + items.push({ + id: row.id, + rowIndex: index, + userText: row.message.text, + assistantText: resolveFinalAssistantTextForTurn(rows, index), + }); + } + return items; +} + +function resolveFinalAssistantTextForTurn( + rows: ReadonlyArray, + userRowIndex: number, +) { + let finalAssistantText: string | null = null; + for (let index = userRowIndex + 1; index < rows.length; index += 1) { + const row = rows[index]; + if (row?.kind !== "message") { + continue; + } + if (row.message.role === "user") { + break; + } + if (row.message.role === "assistant") { + finalAssistantText = row.message.text ?? null; + } + } + return finalAssistantText; +} + +function compactMinimapPreview(text: string | null | undefined) { + const compact = text?.replace(/\s+/g, " ").trim() ?? ""; + return compact.length > 0 ? compact : null; +} + +export function resolveTimelineMinimapPreview( + item: TimelineMinimapItem | null, +): TimelineMinimapItem | null { + return item === null + ? null + : { + ...item, + userText: compactMinimapPreview(item.userText), + assistantText: compactMinimapPreview(item.assistantText), + }; +} From a9dabbf100d1f6c0b2ed7b5e879d469ac14fd186 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 10 Sep 2026 23:50:32 -0700 Subject: [PATCH 14/63] perf(web): reuse completed Markdown prefixes while streaming (#11193) --- apps/web/package.json | 2 + apps/web/src/components/ChatMarkdown.tsx | 8 +- apps/web/src/markdown-incremental.test.tsx | 136 +++++++++++++++++++++ apps/web/src/markdown-incremental.ts | 107 ++++++++++++++++ pnpm-lock.yaml | 6 + 5 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 apps/web/src/markdown-incremental.test.tsx create mode 100644 apps/web/src/markdown-incremental.ts diff --git a/apps/web/package.json b/apps/web/package.json index c2ce73b42b116..069a222ca1227 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -59,6 +59,7 @@ "@types/babel__core": "^7.20.5", "@types/compression": "^1.8.1", "@types/culori": "^4.0.1", + "@types/mdast": "^4.0.4", "@types/react": "~19.2.14", "@types/react-dom": "~19.2.3", "@types/react-test-renderer": "19.1.0", @@ -68,6 +69,7 @@ "compression": "^1.8.1", "react-test-renderer": "19.2.6", "tailwindcss": "^4.0.0", + "unified": "^11.0.5", "vite": "catalog:", "vite-plus": "catalog:" } diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 192abaf385f7d..88d34b8d58bcb 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -69,6 +69,7 @@ import React, { } from "react"; import type { Components, Options as ReactMarkdownOptions } from "react-markdown"; import ReactMarkdown from "react-markdown"; +import { createIncrementalMarkdownPlugin } from "../markdown-incremental"; import { defaultUrlTransform } from "react-markdown"; import rehypeRaw from "rehype-raw"; import rehypeSanitize, { defaultSchema } from "rehype-sanitize"; @@ -3104,12 +3105,17 @@ function ChatMarkdown({ localMediaPreview, setLocalMediaPreview, } = useChatMarkdownState({ text, ...props }); + const incrementalParsing = + props.isStreaming === true && + extraRemarkPlugins.length === 0 && + /(?:^|\n) {0,3}(?:`{3}|~{3})/.test(text); const remarkPlugins = useMemo( () => [ ...(lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS), ...extraRemarkPlugins, + ...(incrementalParsing ? [createIncrementalMarkdownPlugin()] : []), ], - [extraRemarkPlugins, lineBreaks], + [extraRemarkPlugins, incrementalParsing, lineBreaks], ); // react-markdown converts unparsed HTML nodes to text when skipHtml is false. diff --git a/apps/web/src/markdown-incremental.test.tsx b/apps/web/src/markdown-incremental.test.tsx new file mode 100644 index 0000000000000..cf06355f3b49e --- /dev/null +++ b/apps/web/src/markdown-incremental.test.tsx @@ -0,0 +1,136 @@ +import type { Root } from "mdast"; +import { renderToStaticMarkup } from "react-dom/server"; +import ReactMarkdown from "react-markdown"; +import rehypeRaw from "rehype-raw"; +import rehypeSanitize from "rehype-sanitize"; +import remarkGfm from "remark-gfm"; +import type { Plugin } from "unified"; +import { describe, expect, it } from "vite-plus/test"; + +import { remarkCodexDirectives } from "@t3tools/client-runtime/codex-markdown-directives"; +import { remarkGithubAlerts } from "./markdown-github-alerts"; +import { createIncrementalMarkdownPlugin } from "./markdown-incremental"; +import { remarkNormalizeListItemIndentation } from "./markdown-list-indentation"; + +function render(source: string, incremental?: Plugin<[], Root>, parsedSources?: string[]) { + let tree: Root | undefined; + const observeParsing: Plugin<[], Root> = function () { + const original = this.parser; + if (original) { + this.parser = (text, file) => { + parsedSources?.push(text); + return original(text, file); + }; + } + }; + const capture: Plugin<[], Root> = () => (root) => { + tree = structuredClone(root); + }; + const html = renderToStaticMarkup( + + {source} + , + ); + return { html, tree }; +} + +const prefix = "# Before\n\n```ts\nconst values = [1, 2];\n```\n\n"; + +describe("incremental Markdown parsing", () => { + it("keeps the document prefix cached when list recovery parses contain fences", () => { + const source = + prefix + + "- first block\n\n ```ts\n const nested = 1;\n ```\n\n tail"; + const incremental = createIncrementalMarkdownPlugin(); + const parsedSources: string[] = []; + expect(render(source, incremental, parsedSources)).toEqual(render(source)); + parsedSources.length = 0; + const next = source + " more"; + expect(render(next, incremental, parsedSources)).toEqual(render(next)); + expect(parsedSources).not.toContain(next); + expect(parsedSources.some((text) => text.startsWith("t3-markdown-inline-prefix:"))).toBe(true); + }); + + it.each([ + "a\n===\n\nb\n---\n", + "- first\n\n continued\n\n- next\n", + "> quoted\n>\n> ```js\n> abc\n> ```\n\nend", + "
\nhello\n\n
\n\nend", + "[ref]\n\n[ref]: /later", + "a[^x]\n\n[^x]: note", + "a | b\n--|--\na | b\n", + "```\na\n```\n\nnext\n\n~~~\nb\n~~~\n\nmore", + "\n\n\tcode\n\nmore", + "text *bold*", + "> [!NOTE]\n> alert\n\n- [ ] task", + "\uFEFFtext after a byte-order mark", + ])("preserves the parse tree, positions, and HTML while streaming %j", (tail) => { + const source = prefix + tail; + const incremental = createIncrementalMarkdownPlugin(); + for (let end = 0; end <= source.length; end++) { + const text = source.slice(0, end); + expect(render(text, incremental), `prefix ${end}`).toEqual(render(text)); + } + }); + + it.each(["\r\n", "\r"])("preserves partial %j line endings", (newline) => { + const source = (prefix + "next\n\n```\nlast\n```\n\nend").replaceAll("\n", newline); + const incremental = createIncrementalMarkdownPlugin(); + for (let end = 0; end <= source.length; end++) { + const text = source.slice(0, end); + expect(render(text, incremental)).toEqual(render(text)); + } + }); + + it("updates earlier references when definitions arrive after the cached prefix", () => { + const before = "[later] and footnote[^note]\n\n" + prefix; + const incremental = createIncrementalMarkdownPlugin(); + for (const tail of ["text", "[later]: /target", "[later]: /target\n\n[^note]: a note"]) { + expect(render(before + tail, incremental)).toEqual(render(before + tail)); + } + }); + + it("handles edits, replacements, and repeated renders without leaking transformed nodes", () => { + const incremental = createIncrementalMarkdownPlugin(); + const documents = [ + prefix + "- first\n - second", + prefix + "> [!NOTE]\n> transformed alert", + prefix + "plain text", + "replacement without fences", + prefix.replace("Before", "Edited") + "edited prefix", + prefix + "plain text", + prefix + "plain text", + ]; + for (const document of documents) { + expect(render(document, incremental)).toEqual(render(document)); + } + }); + + it("does not freeze unclosed, nested, indented, or mismatched fences", () => { + const prefixes = [ + "```\nopen\n\n", + "````\n```\n\n", + "> ```\n> code\n> ```\n\n", + "- ```\n code\n ```\n\n", + " ```\n code\n ```\n\n", + "\n\n", + markdown: "# heading\n\n```ts\nconst a = 1;\n```\n\ntext\n", + rust: 'fn main() {\n let x = r#"multi\nline"#;\n}\n', + tsx: 'const element = \n{value}\n;\n', + json: '{\n "value": [1,\n 2, 3]\n}\n', + yaml: "key: |\n multiline\n value\nnext: true\n", + css: '/* comment\n continued */\np::before {\n content: "text";\n}\n', + sql: "SELECT 'multi\nline'\nFROM table_name;\n", +} as const; + +const highlighterPromise = getSharedHighlighter({ + langs: Object.keys(samples) as Array, + themes: ["pierre-dark", "pierre-light"], + preferredHighlighter: "shiki-wasm", +}); + +describe("incremental code highlighting", () => { + it.each(Object.entries(samples))( + "matches full HTML at every streaming prefix in %s", + async (language, code) => { + const highlighter = await highlighterPromise; + for (const theme of ["pierre-dark", "pierre-light"] as const) { + const highlight = createIncrementalHighlighter(highlighter, language, theme); + for (let end = 0; end <= code.length; end++) { + const text = code.slice(0, end); + expect(highlight(text), `${theme}, prefix ${end}`).toBe( + highlighter.codeToHtml(text, { lang: language, theme }), + ); + } + } + }, + ); + + it("resets after edits and truncation, including edits to a completed line", async () => { + const highlighter = await highlighterPromise; + const highlight = createIncrementalHighlighter(highlighter, "typescript", "pierre-dark"); + const inputs = [ + "/* open\ncomment\n", + "/* open\ncomment\n*/\nconst x = 1;", + "const edited = 2;\nconst x = 1;", + "const edited = 2;\nconst x = 10;", + "const edited = 2;\n", + "", + "\n\n\nconst fresh = true;\n", + ]; + for (const text of inputs) { + expect(highlight(text)).toBe( + highlighter.codeToHtml(text, { lang: "typescript", theme: "pierre-dark" }), + ); + } + }); + + it.each(["text", "plaintext", "plain", "txt", "ansi"])( + "preserves %s without requesting grammar state", + async (language) => { + const highlighter = await highlighterPromise; + const highlight = createIncrementalHighlighter(highlighter, language, "pierre-dark"); + for (const text of ["plain\ntext", "\u001b[31mred\ncontinued", "\n"]) { + expect(highlight(text)).toBe( + highlighter.codeToHtml(text, { lang: language, theme: "pierre-dark" }), + ); + } + }, + ); + + it("preserves partial CRLF and CR line endings", async () => { + const highlighter = await highlighterPromise; + const highlight = createIncrementalHighlighter(highlighter, "typescript", "pierre-dark"); + const code = "/* multi\r\nline */\r\nconst x = 1;\r\n"; + for (let end = 0; end <= code.length; end++) { + const text = code.slice(0, end); + expect(highlight(text)).toBe( + highlighter.codeToHtml(text, { lang: "typescript", theme: "pierre-dark" }), + ); + } + }); +}); diff --git a/apps/web/src/lib/incrementalHighlighting.ts b/apps/web/src/lib/incrementalHighlighting.ts new file mode 100644 index 0000000000000..a2f90b6935c2a --- /dev/null +++ b/apps/web/src/lib/incrementalHighlighting.ts @@ -0,0 +1,74 @@ +import type { DiffsHighlighter } from "@pierre/diffs"; + +import type { DiffThemeName } from "./diffRendering"; + +function codeChildren(root: ReturnType) { + const pre = root.children.find((node) => node.type === "element" && node.tagName === "pre"); + if (pre?.type !== "element") throw new Error("Missing highlighted pre element"); + const code = pre.children.find((node) => node.type === "element" && node.tagName === "code"); + if (code?.type !== "element") throw new Error("Missing highlighted code element"); + return code.children; +} + +/** Resume tokenization after the last completed line. Keep its grammar state so + * multiline strings, comments, and embedded languages continue to highlight as + * they do in a full pass. The current line is always highlighted again. + */ +export function createIncrementalHighlighter( + highlighter: DiffsHighlighter, + language: string, + theme: DiffThemeName, +) { + const options = { lang: language, theme }; + const newline = { type: "text" as const, value: "\n" }; + let cached: + | { + prefix: string; + state: ReturnType; + children: ReturnType; + } + | undefined; + + return (code: string): string => { + // Plain text and ANSI do not have a TextMate grammar state. A CR at the end + // of a chunk can still become a CRLF, so keep that input on the full path. + if ( + !language || + ["text", "plaintext", "plain", "txt", "ansi"].includes(language) || + code.includes("\r") + ) { + return highlighter.codeToHtml(code, options); + } + if (cached && !code.startsWith(cached.prefix)) cached = undefined; + const end = code.lastIndexOf("\n") + 1; + if (end > (cached?.prefix.length ?? 0)) { + // Omit the final newline: Shiki would tokenize an extra empty line and + // advance the grammar state twice before we process the following line. + const root = highlighter.codeToHast(code.slice(cached?.prefix.length ?? 0, end - 1), { + ...options, + ...(cached ? { grammarState: cached.state } : {}), + }); + const state = highlighter.getLastGrammarState(root); + if (!state) { + cached = undefined; + return highlighter.codeToHtml(code, options); + } + cached = { + prefix: code.slice(0, end), + state, + children: [...(cached ? [...cached.children, newline] : []), ...codeChildren(root)], + }; + } + const prefix = cached; + if (!prefix) return highlighter.codeToHtml(code, options); + return highlighter.codeToHtml(code.slice(prefix.prefix.length), { + ...options, + grammarState: prefix.state, + transformers: [ + { + code: (node) => ({ ...node, children: [...prefix.children, newline, ...node.children] }), + }, + ], + }); + }; +} From 8078c532ceeee5cb951325031f216171507f3d5e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 10 Sep 2026 23:50:32 -0700 Subject: [PATCH 16/63] perf(web): preserve completed code-line DOM while streaming (#11198) --- apps/web/package.json | 2 + apps/web/src/components/ChatMarkdown.test.tsx | 31 ++++++++++-- apps/web/src/components/ChatMarkdown.tsx | 43 ++++++++++++----- .../chat/HighlightedCodeLines.test.tsx | 26 ++++++++++ .../components/chat/HighlightedCodeLines.tsx | 48 +++++++++++++++++++ .../src/lib/incrementalHighlighting.test.ts | 27 +++++++---- apps/web/src/lib/incrementalHighlighting.ts | 12 ++--- pnpm-lock.yaml | 6 +++ 8 files changed, 164 insertions(+), 31 deletions(-) create mode 100644 apps/web/src/components/chat/HighlightedCodeLines.test.tsx create mode 100644 apps/web/src/components/chat/HighlightedCodeLines.tsx diff --git a/apps/web/package.json b/apps/web/package.json index 069a222ca1227..16579e730f1bf 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -34,6 +34,8 @@ "class-variance-authority": "^0.7.1", "culori": "^4.0.2", "effect": "catalog:", + "hast-util-to-html": "^9.0.5", + "hast-util-to-jsx-runtime": "^2.3.6", "heic-to": "^1.5.2", "jose": "catalog:", "jsonc-parser": "3.3.1", diff --git a/apps/web/src/components/ChatMarkdown.test.tsx b/apps/web/src/components/ChatMarkdown.test.tsx index d21960a28e341..adc3ddb77444b 100644 --- a/apps/web/src/components/ChatMarkdown.test.tsx +++ b/apps/web/src/components/ChatMarkdown.test.tsx @@ -110,13 +110,36 @@ describe("ChatMarkdown favicon privacy", () => { }); describe("ChatMarkdown streaming", () => { + it("does not retokenize completed lines when streaming finishes", async () => { + const highlighter = await getSyntaxHighlighterPromise("typescript"); + const highlight = vi.spyOn(highlighter, "codeToHast"); + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + let renderer: ReactTestRenderer | undefined; + const text = "```typescript\nconst completed = 1;\nconst current = 2;"; + try { + await act(async () => { + renderer = create(); + }); + expect(highlight).toHaveBeenCalled(); + highlight.mockClear(); + await act(async () => { + renderer!.update(); + }); + expect(highlight.mock.calls.every(([code]) => !code.includes("const completed"))).toBe(true); + } finally { + await act(async () => renderer?.unmount()); + vi.unstubAllGlobals(); + vi.restoreAllMocks(); + } + }); + it("recovers highlighting after a failed fence changes without resetting its controls", async () => { const highlighter = await getSyntaxHighlighterPromise("text"); - const codeToHtml = highlighter.codeToHtml.bind(highlighter); + const codeToHast = highlighter.codeToHast.bind(highlighter); let fail = true; - vi.spyOn(highlighter, "codeToHtml").mockImplementation((...args) => { + vi.spyOn(highlighter, "codeToHast").mockImplementation((...args) => { if (fail) throw new Error("Temporary highlighter failure"); - return codeToHtml(...args); + return codeToHast(...args); }); vi.spyOn(console, "error").mockImplementation(() => {}); vi.spyOn(console, "warn").mockImplementation(() => {}); @@ -156,7 +179,7 @@ describe("ChatMarkdown streaming", () => { it("preserves code controls and details without highlighting an unchanged fence again", async () => { const highlighter = await getSyntaxHighlighterPromise("text"); - const highlight = vi.spyOn(highlighter, "codeToHtml"); + const highlight = vi.spyOn(highlighter, "codeToHast"); const writeText = vi.fn(async (_text: string) => {}); vi.stubGlobal("navigator", { clipboard: { writeText } }); vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 9be8b0a7b393a..0dd85e9147dca 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -69,6 +69,7 @@ import React, { } from "react"; import type { Components, Options as ReactMarkdownOptions } from "react-markdown"; import ReactMarkdown from "react-markdown"; +import { toHtml } from "hast-util-to-html"; import { createIncrementalMarkdownPlugin } from "../markdown-incremental"; import { defaultUrlTransform } from "react-markdown"; import rehypeRaw from "rehype-raw"; @@ -123,7 +124,8 @@ import { fnv1a32 } from "../lib/diffRendering"; import { LRUCache } from "../lib/lruCache"; import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting"; import { GitHubIcon } from "./Icons"; -import { createIncrementalHighlighter } from "../lib/incrementalHighlighting"; +import { createIncrementalHighlightedDocument } from "../lib/incrementalHighlighting"; +import { HighlightedCodeLines } from "./chat/HighlightedCodeLines"; import { RenderErrorBoundary } from "./RenderErrorBoundary"; import { useTheme } from "../hooks/useTheme"; import { getClientSettings, useClientSettings } from "../hooks/useSettings"; @@ -1011,9 +1013,14 @@ function SuspenseShikiCodeBlock({ themeName, isStreaming, }: SuspenseShikiCodeBlockProps) { + const [hasStreamed, setHasStreamed] = useState(isStreaming); + if (isStreaming && !hasStreamed) setHasStreamed(true); const language = extractFenceLanguage(className); const cacheKey = createHighlightCacheKey(code, language, themeName); - const cachedHighlightedHtml = !isStreaming ? highlightedCodeCache.get(cacheKey) : null; + // Once lines are mounted individually, keep that renderer when streaming + // finishes so switching to cached HTML cannot clear an existing selection. + const cachedHighlightedHtml = + !isStreaming && !hasStreamed ? highlightedCodeCache.get(cacheKey) : null; if (cachedHighlightedHtml != null) { return ( @@ -1031,6 +1038,7 @@ function SuspenseShikiCodeBlock({ themeName={themeName} cacheKey={cacheKey} isStreaming={isStreaming} + preserveLines={isStreaming || hasStreamed} /> ); } @@ -1041,6 +1049,7 @@ interface UncachedShikiCodeBlockProps { themeName: DiffThemeName; cacheKey: string; isStreaming: boolean; + preserveLines: boolean; } function UncachedShikiCodeBlock({ @@ -1049,16 +1058,19 @@ function UncachedShikiCodeBlock({ themeName, cacheKey, isStreaming, + preserveLines, }: UncachedShikiCodeBlockProps) { const highlighter = use(getSyntaxHighlighterPromise(language)); const incrementalHighlight = useMemo( - () => (isStreaming ? createIncrementalHighlighter(highlighter, language, themeName) : null), - [highlighter, isStreaming, language, themeName], + () => + preserveLines ? createIncrementalHighlightedDocument(highlighter, language, themeName) : null, + [highlighter, preserveLines, language, themeName], ); - const highlightedHtml = useMemo(() => { + const highlighted = useMemo(() => { try { - return incrementalHighlight - ? incrementalHighlight(code) + if (incrementalHighlight) return incrementalHighlight(code); + return preserveLines + ? highlighter.codeToHast(code, { lang: language, theme: themeName }) : highlighter.codeToHtml(code, { lang: language, theme: themeName }); } catch (error) { // Log highlighting failures for debugging while falling back to plain text @@ -1067,22 +1079,29 @@ function UncachedShikiCodeBlock({ error instanceof Error ? error.message : error, ); // If highlighting fails for this language, render as plain text - return highlighter.codeToHtml(code, { lang: "text", theme: themeName }); + return preserveLines + ? highlighter.codeToHast(code, { lang: "text", theme: themeName }) + : highlighter.codeToHtml(code, { lang: "text", theme: themeName }); } - }, [code, highlighter, incrementalHighlight, language, themeName]); + }, [code, highlighter, incrementalHighlight, language, preserveLines, themeName]); useEffect(() => { if (!isStreaming) { + const highlightedHtml = typeof highlighted === "string" ? highlighted : toHtml(highlighted); highlightedCodeCache.set( cacheKey, highlightedHtml, estimateHighlightedSize(highlightedHtml, code), ); } - }, [cacheKey, code, highlightedHtml, isStreaming]); + }, [cacheKey, code, highlighted, isStreaming]); - return ( -
+ return typeof highlighted === "string" ? ( +
+ ) : ( +
+ +
); } diff --git a/apps/web/src/components/chat/HighlightedCodeLines.test.tsx b/apps/web/src/components/chat/HighlightedCodeLines.test.tsx new file mode 100644 index 0000000000000..66af6dcc5ed0b --- /dev/null +++ b/apps/web/src/components/chat/HighlightedCodeLines.test.tsx @@ -0,0 +1,26 @@ +import { getSharedHighlighter } from "@pierre/diffs"; +import { toHtml } from "hast-util-to-html"; +import { renderToStaticMarkup } from "react-dom/server"; +import { describe, expect, it } from "vite-plus/test"; + +import { createIncrementalHighlightedDocument } from "../../lib/incrementalHighlighting"; +import { HighlightedCodeLines } from "./HighlightedCodeLines"; + +describe("highlighted code lines", () => { + it("preserves Shiki HTML, including colors, escaping, whitespace, and blank lines", async () => { + const highlighter = await getSharedHighlighter({ + langs: ["typescript"], + themes: ["pierre-dark", "pierre-light"], + preferredHighlighter: "shiki-wasm", + }); + for (const theme of ["pierre-dark", "pierre-light"] as const) { + const highlight = createIncrementalHighlightedDocument(highlighter, "typescript", theme); + const code = + 'const html = "";\n\n/* multi\nline */\n\tconst x = 1;\n'; + for (let end = 0; end <= code.length; end++) { + const root = highlight(code.slice(0, end)); + expect(renderToStaticMarkup()).toBe(toHtml(root)); + } + } + }); +}); diff --git a/apps/web/src/components/chat/HighlightedCodeLines.tsx b/apps/web/src/components/chat/HighlightedCodeLines.tsx new file mode 100644 index 0000000000000..f0f971681c109 --- /dev/null +++ b/apps/web/src/components/chat/HighlightedCodeLines.tsx @@ -0,0 +1,48 @@ +import type { DiffsHighlighter } from "@pierre/diffs"; +import { toHtml } from "hast-util-to-html"; +import { toJsxRuntime } from "hast-util-to-jsx-runtime"; +import { cloneElement, isValidElement, memo, type DOMAttributes } from "react"; +import { Fragment, jsx, jsxs } from "react/jsx-runtime"; + +type HighlightedRoot = ReturnType; +type HighlightedNode = HighlightedRoot["children"][number]; +const runtime = { Fragment, jsx, jsxs }; + +function elementShell(node: Extract) { + const element = toJsxRuntime({ ...node, children: [] }, runtime); + if (!isValidElement>(element)) { + throw new Error("Expected a highlighted code element"); + } + return element; +} + +const HighlightedLine = memo(function HighlightedLine({ node }: { node: HighlightedNode }) { + if (node.type !== "element") return toJsxRuntime(node, runtime); + return cloneElement(elementShell(node), { + dangerouslySetInnerHTML: { __html: toHtml({ type: "root", children: node.children }) }, + }); +}); + +/** Completed line nodes retain their identity in the incremental highlighter. + * Keep their DOM mounted too: replacing the entire pre makes the browser parse + * and resolve styles for thousands of unchanged token spans on each update. + */ +export function HighlightedCodeLines({ root }: { root: HighlightedRoot }) { + const pre = root.children[0]; + if (pre?.type !== "element" || pre.tagName !== "pre") return toJsxRuntime(root, runtime); + const code = pre.children[0]; + if (code?.type !== "element" || code.tagName !== "code") return toJsxRuntime(root, runtime); + return cloneElement( + elementShell(pre), + undefined, + cloneElement( + elementShell(code), + undefined, + code.children.map((node, index) => ( + // A line's position is stable as tokens and new lines are appended. + // oxlint-disable-next-line react/no-array-index-key + + )), + ), + ); +} diff --git a/apps/web/src/lib/incrementalHighlighting.test.ts b/apps/web/src/lib/incrementalHighlighting.test.ts index b61cfc3b48a5e..36c27cea14a22 100644 --- a/apps/web/src/lib/incrementalHighlighting.test.ts +++ b/apps/web/src/lib/incrementalHighlighting.test.ts @@ -1,7 +1,8 @@ +import { toHtml } from "hast-util-to-html"; import { getSharedHighlighter } from "@pierre/diffs"; import { describe, expect, it } from "vite-plus/test"; -import { createIncrementalHighlighter } from "./incrementalHighlighting"; +import { createIncrementalHighlightedDocument } from "./incrementalHighlighting"; const samples = { typescript: "/* multi\nline comment */\nconst x = `template\n${1 + 2}`;\nconst re = /abc/;\n", @@ -29,10 +30,10 @@ describe("incremental code highlighting", () => { async (language, code) => { const highlighter = await highlighterPromise; for (const theme of ["pierre-dark", "pierre-light"] as const) { - const highlight = createIncrementalHighlighter(highlighter, language, theme); + const highlight = createIncrementalHighlightedDocument(highlighter, language, theme); for (let end = 0; end <= code.length; end++) { const text = code.slice(0, end); - expect(highlight(text), `${theme}, prefix ${end}`).toBe( + expect(toHtml(highlight(text)), `${theme}, prefix ${end}`).toBe( highlighter.codeToHtml(text, { lang: language, theme }), ); } @@ -42,7 +43,11 @@ describe("incremental code highlighting", () => { it("resets after edits and truncation, including edits to a completed line", async () => { const highlighter = await highlighterPromise; - const highlight = createIncrementalHighlighter(highlighter, "typescript", "pierre-dark"); + const highlight = createIncrementalHighlightedDocument( + highlighter, + "typescript", + "pierre-dark", + ); const inputs = [ "/* open\ncomment\n", "/* open\ncomment\n*/\nconst x = 1;", @@ -53,7 +58,7 @@ describe("incremental code highlighting", () => { "\n\n\nconst fresh = true;\n", ]; for (const text of inputs) { - expect(highlight(text)).toBe( + expect(toHtml(highlight(text))).toBe( highlighter.codeToHtml(text, { lang: "typescript", theme: "pierre-dark" }), ); } @@ -63,9 +68,9 @@ describe("incremental code highlighting", () => { "preserves %s without requesting grammar state", async (language) => { const highlighter = await highlighterPromise; - const highlight = createIncrementalHighlighter(highlighter, language, "pierre-dark"); + const highlight = createIncrementalHighlightedDocument(highlighter, language, "pierre-dark"); for (const text of ["plain\ntext", "\u001b[31mred\ncontinued", "\n"]) { - expect(highlight(text)).toBe( + expect(toHtml(highlight(text))).toBe( highlighter.codeToHtml(text, { lang: language, theme: "pierre-dark" }), ); } @@ -74,11 +79,15 @@ describe("incremental code highlighting", () => { it("preserves partial CRLF and CR line endings", async () => { const highlighter = await highlighterPromise; - const highlight = createIncrementalHighlighter(highlighter, "typescript", "pierre-dark"); + const highlight = createIncrementalHighlightedDocument( + highlighter, + "typescript", + "pierre-dark", + ); const code = "/* multi\r\nline */\r\nconst x = 1;\r\n"; for (let end = 0; end <= code.length; end++) { const text = code.slice(0, end); - expect(highlight(text)).toBe( + expect(toHtml(highlight(text))).toBe( highlighter.codeToHtml(text, { lang: "typescript", theme: "pierre-dark" }), ); } diff --git a/apps/web/src/lib/incrementalHighlighting.ts b/apps/web/src/lib/incrementalHighlighting.ts index a2f90b6935c2a..fef3598a21520 100644 --- a/apps/web/src/lib/incrementalHighlighting.ts +++ b/apps/web/src/lib/incrementalHighlighting.ts @@ -14,7 +14,7 @@ function codeChildren(root: ReturnType) { * multiline strings, comments, and embedded languages continue to highlight as * they do in a full pass. The current line is always highlighted again. */ -export function createIncrementalHighlighter( +export function createIncrementalHighlightedDocument( highlighter: DiffsHighlighter, language: string, theme: DiffThemeName, @@ -29,7 +29,7 @@ export function createIncrementalHighlighter( } | undefined; - return (code: string): string => { + return (code: string) => { // Plain text and ANSI do not have a TextMate grammar state. A CR at the end // of a chunk can still become a CRLF, so keep that input on the full path. if ( @@ -37,7 +37,7 @@ export function createIncrementalHighlighter( ["text", "plaintext", "plain", "txt", "ansi"].includes(language) || code.includes("\r") ) { - return highlighter.codeToHtml(code, options); + return highlighter.codeToHast(code, options); } if (cached && !code.startsWith(cached.prefix)) cached = undefined; const end = code.lastIndexOf("\n") + 1; @@ -51,7 +51,7 @@ export function createIncrementalHighlighter( const state = highlighter.getLastGrammarState(root); if (!state) { cached = undefined; - return highlighter.codeToHtml(code, options); + return highlighter.codeToHast(code, options); } cached = { prefix: code.slice(0, end), @@ -60,8 +60,8 @@ export function createIncrementalHighlighter( }; } const prefix = cached; - if (!prefix) return highlighter.codeToHtml(code, options); - return highlighter.codeToHtml(code.slice(prefix.prefix.length), { + if (!prefix) return highlighter.codeToHast(code, options); + return highlighter.codeToHast(code.slice(prefix.prefix.length), { ...options, grammarState: prefix.state, transformers: [ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 704437983bacc..eb2ff380f6ce0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -637,6 +637,12 @@ importers: effect: specifier: 4.0.0-rc.112 version: 4.0.0-rc.112(patch_hash=8bef799f35729cf3465eb428196f617b434a7c9f658d56acb2874d74b21b98b2) + hast-util-to-html: + specifier: ^9.0.5 + version: 9.0.5 + hast-util-to-jsx-runtime: + specifier: ^2.3.6 + version: 2.3.6 heic-to: specifier: ^1.5.2 version: 1.5.2 From 211618fd9fe39d3dde01171a6856ce9f633571c9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 10 Sep 2026 23:50:33 -0700 Subject: [PATCH 17/63] perf(web): huge-thread switch no longer blanks the chat pane (#11169) Co-authored-by: Cursor Agent Co-authored-by: Julius Marminge --- .../web/src/components/ChatView.logic.test.ts | 181 ++++++++++++++++++ apps/web/src/components/ChatView.logic.ts | 141 ++++++++++++++ apps/web/src/components/ChatView.tsx | 135 ++++++++++--- .../src/components/chat/MessagesTimeline.tsx | 59 ++++-- 4 files changed, 472 insertions(+), 44 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 06fcce22ee3f2..973dd5749027b 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -59,6 +59,14 @@ import { resolveSendEnvMode, threadShellHasStarted, resolveDraftHeroState, + isPaintOnlyThreadTimeline, + peekHeldThreadTimeline, + peekRememberedThreadTimeline, + rememberReadyThreadTimeline, + resetHeldThreadTimeline, + resolveThreadSwitchTimeline, + threadKeysShareEnvironment, + timelineHasEphemeralPreviewUrls, scheduleEnvironmentReconnectWarning, startNewThreadForProject, codexArtifactTemplatePromptToAppend, @@ -565,6 +573,179 @@ describe("draft hero submission transition", () => { }); }); +describe("resolveThreadSwitchTimeline", () => { + afterEach(() => { + resetHeldThreadTimeline(); + }); + + const held = { threadKey: "env-1:thread-a", entries: ["a1", "a2"] }; + + it("keeps the previous thread's entries while the next thread is loading", () => { + expect( + resolveThreadSwitchTimeline({ + loading: true, + activeThreadKey: "env-1:thread-b", + nextEntries: [], + lastReady: held, + }), + ).toEqual({ entries: ["a1", "a2"], displayThreadKey: "env-1:thread-a" }); + }); + + it("shows the new thread once its detail is ready", () => { + expect( + resolveThreadSwitchTimeline({ + loading: false, + activeThreadKey: "env-1:thread-b", + nextEntries: ["b1"], + lastReady: held, + }), + ).toEqual({ entries: ["b1"], displayThreadKey: "env-1:thread-b" }); + }); + + it("does not invent a timeline on the first open of a thread", () => { + expect( + resolveThreadSwitchTimeline({ + loading: true, + activeThreadKey: "env-1:thread-a", + nextEntries: [], + lastReady: null, + }), + ).toEqual({ entries: [], displayThreadKey: "env-1:thread-a" }); + }); + + it("keeps the held thread workspace cwd with the snapshot", () => { + rememberReadyThreadTimeline({ + ...held, + markdownCwd: "/repo/a", + workspaceRoot: "/repo/a", + }); + expect(peekHeldThreadTimeline()).toEqual({ + ...held, + markdownCwd: "/repo/a", + workspaceRoot: "/repo/a", + }); + }); + + it("survives a ChatView remount by remembering the last ready timeline", () => { + rememberReadyThreadTimeline(held); + expect(peekHeldThreadTimeline()).toEqual(held); + expect( + resolveThreadSwitchTimeline({ + loading: true, + activeThreadKey: "env-1:thread-b", + nextEntries: [], + }), + ).toEqual({ entries: ["a1", "a2"], displayThreadKey: "env-1:thread-a" }); + }); + + it("paints a remembered destination instead of the last-viewed thread", () => { + rememberReadyThreadTimeline(held); + rememberReadyThreadTimeline({ threadKey: "env-1:thread-b", entries: ["b1", "b2"] }); + expect(peekRememberedThreadTimeline("env-1:thread-a")).toEqual(["a1", "a2"]); + expect( + resolveThreadSwitchTimeline({ + loading: true, + activeThreadKey: "env-1:thread-a", + nextEntries: [], + }), + ).toEqual({ entries: ["a1", "a2"], displayThreadKey: "env-1:thread-a" }); + }); + + it("prefers live entries over a remembered snapshot", () => { + rememberReadyThreadTimeline({ threadKey: "env-1:thread-b", entries: ["stale-b"] }); + expect( + resolveThreadSwitchTimeline({ + loading: false, + activeThreadKey: "env-1:thread-b", + nextEntries: ["fresh-b"], + }), + ).toEqual({ entries: ["fresh-b"], displayThreadKey: "env-1:thread-b" }); + }); + + it("does not keep a remembered snapshot on a resolved empty thread", () => { + rememberReadyThreadTimeline(held); + expect( + resolveThreadSwitchTimeline({ + loading: false, + activeThreadKey: "env-1:thread-a", + nextEntries: [], + }), + ).toEqual({ entries: [], displayThreadKey: "env-1:thread-a" }); + }); + + it("does not hold another environment's timeline across a jump", () => { + expect(threadKeysShareEnvironment("env-1:thread-a", "env-2:thread-b")).toBe(false); + expect( + resolveThreadSwitchTimeline({ + loading: true, + activeThreadKey: "env-2:thread-b", + nextEntries: [], + lastReady: held, + }), + ).toEqual({ entries: [], displayThreadKey: "env-2:thread-b" }); + }); + + it("treats a foreign held timeline as paint-only", () => { + expect(isPaintOnlyThreadTimeline("env-1:thread-a", "env-1:thread-b")).toBe(true); + expect(isPaintOnlyThreadTimeline("env-1:thread-b", "env-1:thread-b")).toBe(false); + }); + + it("does not remember a timeline that still has handoff blob previews", () => { + expect( + timelineHasEphemeralPreviewUrls([ + { + kind: "message", + message: { + id: MessageId.make("preview-message"), + role: "user", + text: "Preview", + turnId: null, + streaming: false, + createdAt: "2026-09-10T12:00:00.000Z", + updatedAt: "2026-09-10T12:00:00.000Z", + attachments: [ + { + type: "image", + id: "preview", + name: "preview.png", + mimeType: "image/png", + sizeBytes: 1, + previewUrl: "blob:handoff", + }, + ], + }, + }, + ]), + ).toBe(true); + expect( + timelineHasEphemeralPreviewUrls([ + { + kind: "message", + message: { + id: MessageId.make("preview-message"), + role: "user", + text: "Preview", + turnId: null, + streaming: false, + createdAt: "2026-09-10T12:00:00.000Z", + updatedAt: "2026-09-10T12:00:00.000Z", + attachments: [ + { + type: "image", + id: "preview", + name: "preview.png", + mimeType: "image/png", + sizeBytes: 1, + previewUrl: "https://cdn.example/a.png", + }, + ], + }, + }, + ]), + ).toBe(false); + }); +}); + describe("shouldReleaseTimelineAnchorForToolActivity", () => { const activeTurnId = TurnId.make("active-turn"); const anchorMessageId = MessageId.make("anchored-message"); diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index 66214df385e8e..772a0f3cf2fa1 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -18,6 +18,7 @@ import { type ThreadLinkedPullRequest, type TurnId, } from "@t3tools/contracts"; +import { parseScopedThreadKey } from "@t3tools/client-runtime/environment"; import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets"; import { squashAtomCommandFailure, @@ -262,6 +263,135 @@ export function resolveDraftHeroState(input: { ); } +/** + * Keep painted timelines on screen across thread jumps. Remounting LegendList + * (or handing it an empty first paint) punches a hole through the chat pane — + * white in light mode — so cmd+1/2/3 spam flashes even when the destination + * is already cached. + * + * Stored at module scope because ChatView remounts when the thread route + * changes (same pattern as the thread-error banner session dismissals). + * Remember more than the last thread so jumping back to cmd+1 does not show + * cmd+3's messages, and so a cached destination can paint on the first frame. + */ +export type HeldThreadTimeline = { + threadKey: string | null; + entries: T; + markdownCwd?: string | null; + workspaceRoot?: string | null; +}; + +const MAX_REMEMBERED_THREAD_TIMELINES = 16; + +let rememberedThreadTimelines = new Map>(); +let rememberedThreadTimelineOrder: string[] = []; +let lastReadyThreadKey: string | null = null; + +function rememberThreadTimelineEntries(held: HeldThreadTimeline): void { + if (held.threadKey === null) { + return; + } + rememberedThreadTimelines.set(held.threadKey, held); + rememberedThreadTimelineOrder = [ + ...rememberedThreadTimelineOrder.filter((key) => key !== held.threadKey), + held.threadKey, + ]; + while (rememberedThreadTimelineOrder.length > MAX_REMEMBERED_THREAD_TIMELINES) { + const evicted = rememberedThreadTimelineOrder.shift(); + if (evicted !== undefined) { + rememberedThreadTimelines.delete(evicted); + } + } + lastReadyThreadKey = held.threadKey; +} + +export function rememberReadyThreadTimeline( + held: HeldThreadTimeline, +): void { + if (held.threadKey === null || held.entries.length === 0) { + return; + } + rememberThreadTimelineEntries(held); +} + +export function peekRememberedThreadTimeline( + threadKey: string | null, +): T | null { + if (threadKey === null) { + return null; + } + return (rememberedThreadTimelines.get(threadKey)?.entries as T | undefined) ?? null; +} + +export function peekHeldThreadTimeline< + T extends readonly unknown[], +>(): HeldThreadTimeline | null { + if (lastReadyThreadKey === null) { + return null; + } + const held = rememberedThreadTimelines.get(lastReadyThreadKey); + if (held === undefined || held.entries.length === 0) { + return null; + } + return held as HeldThreadTimeline; +} + +export function resetHeldThreadTimeline(): void { + rememberedThreadTimelines = new Map(); + rememberedThreadTimelineOrder = []; + lastReadyThreadKey = null; +} + +export function threadKeysShareEnvironment(left: string | null, right: string | null): boolean { + if (left === null || right === null) { + return false; + } + const leftRef = parseScopedThreadKey(left); + const rightRef = parseScopedThreadKey(right); + return leftRef !== null && rightRef !== null && leftRef.environmentId === rightRef.environmentId; +} + +/** True while we still paint another thread's last snapshot. */ +export function isPaintOnlyThreadTimeline( + displayThreadKey: string | null, + activeThreadKey: string | null, +): boolean { + return ( + displayThreadKey !== null && activeThreadKey !== null && displayThreadKey !== activeThreadKey + ); +} + +export function resolveThreadSwitchTimeline(input: { + loading: boolean; + activeThreadKey: string | null; + nextEntries: T; + rememberedForActive?: T | null; + lastReady?: HeldThreadTimeline | null; +}): { entries: T; displayThreadKey: string | null } { + if (input.nextEntries.length > 0) { + return { entries: input.nextEntries, displayThreadKey: input.activeThreadKey }; + } + + const rememberedForActive = + input.rememberedForActive ?? peekRememberedThreadTimeline(input.activeThreadKey); + if (input.loading && rememberedForActive !== null && rememberedForActive.length > 0) { + return { entries: rememberedForActive, displayThreadKey: input.activeThreadKey }; + } + + const lastReady = input.lastReady ?? peekHeldThreadTimeline(); + if ( + input.loading && + lastReady !== null && + lastReady.threadKey !== null && + lastReady.threadKey !== input.activeThreadKey && + lastReady.entries.length > 0 && + threadKeysShareEnvironment(lastReady.threadKey, input.activeThreadKey) + ) { + return { entries: lastReady.entries, displayThreadKey: lastReady.threadKey }; + } + return { entries: input.nextEntries, displayThreadKey: input.activeThreadKey }; +} + export function resolveDraftPromotionNavigationTarget(input: { serverThreadRef: ScopedThreadRef | null; serverThread: Pick | null | undefined; @@ -612,6 +742,17 @@ export function revokeUserMessagePreviewUrls(message: ChatMessage): void { } } +export function timelineHasEphemeralPreviewUrls( + entries: ReadonlyArray & { message?: ChatMessage }>, +): boolean { + return entries.some( + (entry) => + entry.kind === "message" && + entry.message !== undefined && + collectUserMessageBlobPreviewUrls(entry.message).length > 0, + ); +} + export function collectUserMessageBlobPreviewUrls(message: ChatMessage): string[] { if (message.role !== "user" || !message.attachments) { return []; diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 7a0e6a719326d..51b9c5eabc488 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -415,6 +415,12 @@ import { resolveComposerInteractionMode, resolveComposerProviderSelection, resolveDraftHeroState, + isPaintOnlyThreadTimeline, + peekHeldThreadTimeline, + peekRememberedThreadTimeline, + rememberReadyThreadTimeline, + resolveThreadSwitchTimeline, + timelineHasEphemeralPreviewUrls, observeProactivePanelUserChoice, resolveProactiveTurnDiffAction, resolveThreadMetadataUpdateForNextTurn, @@ -1379,6 +1385,10 @@ function chatActionErrorMessage(error: unknown): string { } const ENVIRONMENT_UNAVAILABLE_SEND_TOAST_TRAIL_SIZE = 3; +const EMPTY_HELD_TURN_DIFF_SUMMARIES: readonly never[] = []; +const noopHeldTurnDiff = (_turnId: TurnId, _filePath?: string) => {}; +const noopHeldRevert = (_targetTurnCount: number) => {}; +const noopHeldAttachment = (_attachment: ChatFileAttachment) => {}; /** * Drops the send-time anchored end space. That space is what holds a sent @@ -3239,6 +3249,18 @@ export default function ChatView(props: ChatViewProps) { timelineMessages, workLogEntries, ]); + const displayedTimeline = resolveThreadSwitchTimeline({ + loading: timelineEntries.length === 0 && threadSyncPhase !== null, + activeThreadKey, + nextEntries: timelineEntries, + rememberedForActive: peekRememberedThreadTimeline(activeThreadKey), + }); + const displayedTimelineKey = displayedTimeline.displayThreadKey ?? routeThreadKey; + const paintOnlyDisplayedTimeline = isPaintOnlyThreadTimeline( + displayedTimeline.displayThreadKey, + activeThreadKey, + ); + const displayedThreadRef = parseScopedThreadKey(displayedTimelineKey); const [dockedDraftHeroThreadKey, setDockedDraftHeroThreadKey] = useState(null); const draftHeroDockRequested = activeThreadKey !== null && dockedDraftHeroThreadKey === activeThreadKey; @@ -3337,6 +3359,24 @@ export default function ChatView(props: ChatViewProps) { const activeProjectCwd = activeProject?.workspaceRoot ?? null; const activeThreadWorktreePath = activeThread?.worktreePath ?? null; const activeWorkspaceRoot = activeThreadWorktreePath ?? activeProjectCwd ?? undefined; + useLayoutEffect(() => { + if ( + threadDetailLoading || + timelineEntries.length === 0 || + timelineHasEphemeralPreviewUrls(timelineEntries) + ) { + return; + } + rememberReadyThreadTimeline({ + threadKey: activeThreadKey, + entries: timelineEntries, + markdownCwd: gitCwd, + workspaceRoot: activeWorkspaceRoot ?? null, + }); + }, [activeThreadKey, activeWorkspaceRoot, gitCwd, threadDetailLoading, timelineEntries]); + const heldPaintContext = paintOnlyDisplayedTimeline + ? peekHeldThreadTimeline() + : null; const activeTerminalLaunchContext = terminalUiLaunchContext?.threadId === activeThreadId ? terminalUiLaunchContext : null; // Git status arrives after the composer paints. A checkout seen earlier in @@ -4910,6 +4950,21 @@ export default function ChatView(props: ChatViewProps) { void legendListRef.current?.scrollToEnd?.({ animated }); }); }, []); + const displayedTimelineKeyRef = useRef(displayedTimeline.displayThreadKey); + useLayoutEffect(() => { + const displayKey = displayedTimeline.displayThreadKey; + if (displayKey === null || displayKey !== activeThreadKey) { + displayedTimelineKeyRef.current = displayKey; + return; + } + if (displayedTimelineKeyRef.current === displayKey) { + return; + } + displayedTimelineKeyRef.current = displayKey; + // Keep the list mounted across jumps; pin the newly displayed thread to + // its end the way a remount used to via initialScrollAtEnd. + scrollToEnd(); + }, [activeThreadKey, displayedTimeline.displayThreadKey, scrollToEnd]); useLayoutEffect(() => { if (timelineScrollModeRef.current !== "anchoring-new-turn") { return; @@ -8435,54 +8490,78 @@ export default function ChatView(props: ChatViewProps) { />
{/* Messages Wrapper */} -
+
{/* Messages — LegendList handles virtualization and scrolling internally */} {/* scroll to end pill — shown when user has scrolled away from the live edge */} diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index 41e3a0740a95d..e2c3b6520de1d 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -331,6 +331,12 @@ interface MessagesTimelineProps { runningTurnId: TurnId | null; turnDiffSummaries: ReadonlyArray; routeThreadKey: string; + /** + * Thread whose entries are currently painted. Differs from `routeThreadKey` + * while a jump is still holding the previous list. Identity for row + * projection and list extraData — do not remount on this value. + */ + displayThreadKey?: string; onOpenTurnDiff: (turnId: TurnId, filePath?: string) => void; supportsConversationRollback: boolean; onRevertToTurnCount: (targetTurnCount: number) => void; @@ -389,6 +395,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({ runningTurnId, turnDiffSummaries, routeThreadKey, + displayThreadKey, onOpenTurnDiff, supportsConversationRollback, onRevertToTurnCount, @@ -416,17 +423,30 @@ export const MessagesTimeline = memo(function MessagesTimeline({ loadEarlier = null, }: MessagesTimelineProps) { const [expandedTurnIds, setExpandedTurnIds] = useState>(new Set()); + const [expandedWorkGroupIds, setExpandedWorkGroupIds] = useState>(new Set()); + const listIdentityKey = displayThreadKey ?? routeThreadKey; + const listIdentityRef = useRef(listIdentityKey); + const previousLatestTurnRef = useRef(latestTurn); + let paintedExpandedTurnIds = expandedTurnIds; + let paintedExpandedWorkGroupIds = expandedWorkGroupIds; + if (listIdentityRef.current !== listIdentityKey) { + listIdentityRef.current = listIdentityKey; + previousLatestTurnRef.current = latestTurn; + paintedExpandedTurnIds = new Set(); + paintedExpandedWorkGroupIds = new Set(); + setExpandedTurnIds(paintedExpandedTurnIds); + setExpandedWorkGroupIds(paintedExpandedWorkGroupIds); + } const citationThreadRef = useMemo(() => parseScopedThreadKey(routeThreadKey), [routeThreadKey]); const expandCitedTurn = useCallback((turnId: TurnId) => { setExpandedTurnIds((current) => current.has(turnId) ? current : new Set([...current, turnId]), ); }, []); - const [expandedWorkGroupIds, setExpandedWorkGroupIds] = useState>(new Set()); // Scroll/disclosure state outlives virtualized rows, but never the current thread. const workGroupViewState = useMemo( () => ({ scrollPositions: new Map(), expandedEntries: new Set() }), - [routeThreadKey], + [listIdentityKey], ); const [disclosureToggleSettling, setDisclosureToggleSettling] = useState(false); const [minimapStripMap] = useState(() => new Map()); @@ -518,7 +538,6 @@ export const MessagesTimeline = memo(function MessagesTimeline({ // An in-session interrupt leaves its turn expanded so the user keeps their // place; the next turn (or a reload, since this is local state) folds it. - const previousLatestTurnRef = useRef(latestTurn); useEffect(() => { const previous = previousLatestTurnRef.current; previousLatestTurnRef.current = latestTurn; @@ -557,34 +576,34 @@ export const MessagesTimeline = memo(function MessagesTimeline({ timelineEntries, latestTurn, runningTurnId, - expandedTurnIds, - expandedWorkGroupIds, + expandedTurnIds: paintedExpandedTurnIds, + expandedWorkGroupIds: paintedExpandedWorkGroupIds, isWorking, activeTurnStartedAt, turnDiffSummaries, supportsConversationRollback, }, - previous?.threadKey === routeThreadKey && previous.workspaceRoot === workspaceRoot + previous?.threadKey === listIdentityKey && previous.workspaceRoot === workspaceRoot ? previous.projection : null, ); - rowsProjectionRef.current = { threadKey: routeThreadKey, workspaceRoot, projection }; + rowsProjectionRef.current = { threadKey: listIdentityKey, workspaceRoot, projection }; return projection.rows; }, [ rowsProjectionRef, - routeThreadKey, + listIdentityKey, workspaceRoot, timelineEntries, latestTurn, runningTurnId, - expandedTurnIds, - expandedWorkGroupIds, + paintedExpandedTurnIds, + paintedExpandedWorkGroupIds, isWorking, activeTurnStartedAt, turnDiffSummaries, supportsConversationRollback, ]); - const rows = useStableRows(rawRows); + const rows = useStableRows(rawRows, listIdentityKey); const minimapItems = useMemo(() => deriveTimelineMinimapItems(rows), [rows]); const [timelineViewportElement, setTimelineViewportElement] = useState( null, @@ -822,7 +841,9 @@ export const MessagesTimeline = memo(function MessagesTimeline({ if (rows.length === 0 && !isWorking) { if (hideEmptyPlaceholder) { - return null; + // Occupy the pane with the theme surface so a thread switch cannot + // punch a hole through to the window chrome (white in light mode). + return
; } return (
@@ -849,7 +870,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({ ref={listRef} data={rows} - extraData={rows.length} + extraData={`${listIdentityKey}:${rows.length}`} keyExtractor={keyExtractor} getItemType={getItemType} renderItem={renderItem} @@ -2742,17 +2763,23 @@ function UserMessageReviewCommentCard({ comment }: { comment: ReviewCommentConte /** Returns a structurally-shared copy of `rows`: for each row whose content * hasn't changed since last call, the previous object reference is reused. */ -function useStableRows(rows: MessagesTimelineRow[]): MessagesTimelineRow[] { +function useStableRows(rows: MessagesTimelineRow[], identity: string): MessagesTimelineRow[] { const prevState = useRef({ byId: new Map(), result: [], }); + const prevIdentity = useRef(identity); return useMemo(() => { - const nextState = computeStableMessagesTimelineRows(rows, prevState.current); + const previous = + prevIdentity.current === identity + ? prevState.current + : { byId: new Map(), result: [] }; + prevIdentity.current = identity; + const nextState = computeStableMessagesTimelineRows(rows, previous); prevState.current = nextState; return nextState.result; - }, [rows]); + }, [identity, rows]); } // --------------------------------------------------------------------------- From fb3d165d33b155038de717851207d25fb52fe65e Mon Sep 17 00:00:00 2001 From: Bilal Bakr <62337003+Bil0000@users.noreply.github.com> Date: Fri, 11 Sep 2026 17:17:59 +0300 Subject: [PATCH 18/63] fix(web): show platform file manager icons in Open menu (#11228) --- apps/web/src/components/Icons.tsx | 28 +++++++++++++++++++ .../src/components/chat/OpenInPicker.test.ts | 26 +++++++++++++++++ apps/web/src/components/chat/OpenInPicker.tsx | 19 +++++++++---- apps/web/src/editorLabels.test.ts | 2 +- apps/web/src/lib/utils.test.ts | 2 +- apps/web/src/lib/utils.ts | 2 +- 6 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 apps/web/src/components/chat/OpenInPicker.test.ts diff --git a/apps/web/src/components/Icons.tsx b/apps/web/src/components/Icons.tsx index b13040152e180..99880a2512ad4 100644 --- a/apps/web/src/components/Icons.tsx +++ b/apps/web/src/components/Icons.tsx @@ -2,6 +2,34 @@ import React, { type SVGProps, useId } from "react"; import { cn } from "~/lib/utils"; export type Icon = React.FC>; +export const FinderIcon: Icon = (props) => ( + + + + + +); + +export const FileExplorerIcon: Icon = (props) => ( + + + + + + +); + // Apple brand mark from Simple Icons (CC0). export const AppleIcon: Icon = (props) => (
Date: Fri, 11 Sep 2026 12:04:44 -0700 Subject: [PATCH 24/63] perf(mobile): reuse completed code lines while streaming (#11211) --- .../src/NativeMarkdownBlock.tsx | 232 +++++------------- .../src/SelectableMarkdownText.types.ts | 15 +- .../src/nativeMarkdownText.ts | 23 +- .../src/pendingCodeHighlight.test.ts | 28 +++ .../src/pendingCodeHighlight.ts | 19 ++ .../src/useHighlightedCode.test.tsx | 189 ++++++++++++++ .../src/useHighlightedCode.ts | 149 +++++++++++ .../review/incrementalSnippet.test.ts | 85 +++++++ .../src/features/review/incrementalSnippet.ts | 68 +++++ .../features/review/shikiReviewHighlighter.ts | 66 ++++- .../mobile/src/lib/nativeMarkdownText.test.ts | 93 ++++++- 11 files changed, 783 insertions(+), 184 deletions(-) create mode 100644 apps/mobile/modules/t3-markdown-text/src/pendingCodeHighlight.test.ts create mode 100644 apps/mobile/modules/t3-markdown-text/src/pendingCodeHighlight.ts create mode 100644 apps/mobile/modules/t3-markdown-text/src/useHighlightedCode.test.tsx create mode 100644 apps/mobile/modules/t3-markdown-text/src/useHighlightedCode.ts create mode 100644 apps/mobile/src/features/review/incrementalSnippet.test.ts create mode 100644 apps/mobile/src/features/review/incrementalSnippet.ts diff --git a/apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.tsx b/apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.tsx index ab75eaf32a2c8..a4485ede47058 100644 --- a/apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.tsx +++ b/apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.tsx @@ -1,10 +1,14 @@ -import { createContext, useContext, useEffect, useState } from "react"; +import { createContext, memo, useContext, useMemo } from "react"; import { Image, Platform, ScrollView, Text, useColorScheme, View } from "react-native"; import type { MarkdownNode } from "react-native-nitro-markdown/headless"; import { CopyTextButton } from "./CopyTextButton"; import { MarkdownTextPrimitive } from "./MarkdownTextPrimitive"; -import { nativeMarkdownDocumentRuns, nativeMarkdownListItemBlocks } from "./nativeMarkdownText"; +import { + nativeMarkdownDocumentRuns, + nativeMarkdownListItemBlocks, + nativeMarkdownNodePosition, +} from "./nativeMarkdownText"; import { NativeMarkdownSelectableText } from "./NativeMarkdownSelectableText"; import type { MarkdownCodeHighlighter, @@ -13,15 +17,11 @@ import type { NativeMarkdownTextStyle, SelectableMarkdownSkill, } from "./SelectableMarkdownText.types"; +import { useHighlightedCode, type HighlightedCode } from "./useHighlightedCode"; /** Set by SelectableMarkdownText so images anywhere in the block tree can use it. */ export const MarkdownImageRendererContext = createContext(null); -type HighlightedCode = ReadonlyArray>; - -const highlightedCodeCache = new Map(); -const highlightedCodePromiseCache = new Map>(); -const HIGHLIGHTED_CODE_CACHE_LIMIT = 64; const MONO_FONT_FAMILY = Platform.select({ ios: "ui-monospace", android: "monospace", @@ -29,7 +29,7 @@ const MONO_FONT_FAMILY = Platform.select({ }); function nodeKey(node: MarkdownNode, index: number): string { - return `${node.type}:${node.beg ?? index}:${node.end ?? index}`; + return `${node.type}:${nativeMarkdownNodePosition(node, index)}`; } /** Code inside markdown scales with the base text size (12pt at the default 15pt body). */ @@ -67,174 +67,74 @@ function SelectableNode(props: { ); } -function codeHighlightCacheKey( - code: string, - language: string | undefined, - theme: "light" | "dark", -): string { - return `${theme}:${language ?? "text"}:${code}`; -} - -function cacheHighlightedCode(key: string, tokens: HighlightedCode): void { - highlightedCodeCache.delete(key); - highlightedCodeCache.set(key, tokens); - - while (highlightedCodeCache.size > HIGHLIGHTED_CODE_CACHE_LIMIT) { - const oldestKey = highlightedCodeCache.keys().next().value; - if (oldestKey === undefined) { - break; - } - highlightedCodeCache.delete(oldestKey); - } -} - -function loadHighlightedCode( - code: string, - language: string | undefined, - theme: "light" | "dark", - highlightCode: MarkdownCodeHighlighter, -): Promise { - const key = codeHighlightCacheKey(code, language, theme); - const cached = highlightedCodeCache.get(key); - if (cached) { - return Promise.resolve(cached); - } - - const pending = highlightedCodePromiseCache.get(key); - if (pending) { - return pending; - } - - const promise = highlightCode({ code, language, theme }) - .then((tokens) => { - cacheHighlightedCode(key, tokens); - highlightedCodePromiseCache.delete(key); - return tokens; - }) - .catch((error) => { - highlightedCodePromiseCache.delete(key); - throw error; - }); - highlightedCodePromiseCache.set(key, promise); - return promise; -} - -function useHighlightedCode( - code: string, - language: string | undefined, - theme: "light" | "dark", - highlightCode: MarkdownCodeHighlighter, -): HighlightedCode | null { - const key = codeHighlightCacheKey(code, language, theme); - const [highlighted, setHighlighted] = useState<{ - readonly key: string; - readonly tokens: HighlightedCode | null; - }>(() => ({ - key, - tokens: highlightedCodeCache.get(key) ?? null, - })); - - useEffect(() => { - let active = true; - const cached = highlightedCodeCache.get(key); - if (cached) { - cacheHighlightedCode(key, cached); - setHighlighted({ key, tokens: cached }); - return () => { - active = false; - }; - } - - void loadHighlightedCode(code, language, theme, highlightCode) - .then((tokens) => { - if (active) { - setHighlighted({ key, tokens }); - } - }) - .catch(() => { - if (active) { - setHighlighted({ key, tokens: null }); - } - }); - return () => { - active = false; - }; - }, [code, highlightCode, key, language, theme]); - - return highlighted.key === key ? highlighted.tokens : null; -} - -function HighlightedCodeText(props: { - readonly content: string; - readonly highlighted: HighlightedCode | null; - readonly textStyle: NativeMarkdownTextStyle; +const HighlightedCodeLine = memo(function HighlightedCodeLine(props: { + readonly tokens: ReadonlyArray; + readonly color: string; + readonly newline: boolean; }) { - if (!props.highlighted) { - return ( + let offset = 0; + const children = []; + for (const token of props.tokens) { + if (!token.content) continue; + children.push( - {props.content} - + {token.content} + , ); + offset += token.content.length; } - const highlighted = props.highlighted; - let sourceOffset = 0; - const keyOccurrences = new Map(); - const keyedLines = highlighted.map((line) => { - const lineStart = sourceOffset; - const tokens = line.map((token) => { - const start = sourceOffset; - sourceOffset += token.content.length; - const signature = `${start}:${token.content}:${token.color ?? ""}:${token.fontStyle ?? ""}`; - const occurrence = keyOccurrences.get(signature) ?? 0; - keyOccurrences.set(signature, occurrence + 1); - return { key: `${signature}:${occurrence}`, token }; - }); - sourceOffset += 1; - return { - key: `line:${lineStart}:${line.map((token) => token.content).join("")}`, - tokens, - }; - }); + return ( + + {children} + {props.newline ? "\n" : ""} + + ); +}); +function HighlightedCodeText(props: { + readonly content: string; + readonly highlighted: HighlightedCode | null; + readonly textStyle: NativeMarkdownTextStyle; +}) { + // The text root provides inherited styles through context. A new style object + // would rerender every token even when its completed line is unchanged. + const fontSize = codeBlockFontSize(props.textStyle); + const lineHeight = codeBlockLineHeight(props.textStyle); + const style = useMemo( + () => ({ + color: props.textStyle.codeColor, + fontFamily: MONO_FONT_FAMILY, + fontSize, + lineHeight, + }), + [props.textStyle.codeColor, fontSize, lineHeight], + ); + let offset = 0; + const lines = []; + if (props.highlighted) { + for (const tokens of props.highlighted) { + lines.push( + , + ); + offset += tokens.reduce((length, token) => length + token.content.length, 0) + 1; + } + } return ( - - {keyedLines.map((line, lineIndex) => ( - - {line.tokens.map(({ key, token }) => ( - - {token.content} - - ))} - {lineIndex + 1 < keyedLines.length ? "\n" : ""} - - ))} + + {props.highlighted ? lines : props.content} ); } diff --git a/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.types.ts b/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.types.ts index 50b1cccb66023..13acb58b1a7dc 100644 --- a/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.types.ts +++ b/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.types.ts @@ -25,11 +25,22 @@ export interface MarkdownHighlightedToken { readonly fontStyle: number | null; } -export type MarkdownCodeHighlighter = (input: { +export interface MarkdownCodeHighlightInput { + /** Identity of the mounted code block, for incremental highlighting. */ + readonly session?: object; readonly code: string; readonly language?: string | null; readonly theme: "light" | "dark"; -}) => Promise>>; +} +export interface MarkdownCodeHighlighter { + ( + input: MarkdownCodeHighlightInput, + ): Promise>>; + /** Optional synchronous result for a small append to an already warm block. */ + read?: ( + input: MarkdownCodeHighlightInput, + ) => ReadonlyArray> | undefined; +} export interface SelectableMarkdownSkill { readonly name: string; diff --git a/apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts b/apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts index 4c8a6c4d7cd5a..51dc85a2e9bfe 100644 --- a/apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts +++ b/apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts @@ -694,21 +694,31 @@ function containsRichBlock(node: MarkdownNode): boolean { return (node.children ?? []).some(containsRichBlock); } +/** + * Sibling identity for React keys. A source offset survives appends while the + * document streams; the child index is the fallback for offset-free nodes. The + * two never share a namespace, so a positioned node cannot collide with an + * offset-free sibling whose index happens to equal its offset. + */ +export function nativeMarkdownNodePosition(node: MarkdownNode, index: number): string { + return node.beg === undefined ? `index:${index}` : `offset:${node.beg}`; +} + export function nativeMarkdownDocumentChunks( document: MarkdownNode, ): ReadonlyArray { const chunks: NativeMarkdownDocumentChunk[] = []; let selectableNodes: MarkdownNode[] = []; + let selectableStart = 0; const flushSelectable = () => { - if (selectableNodes.length === 0) { + const first = selectableNodes[0]; + if (!first) { return; } - const first = selectableNodes[0]; - const last = selectableNodes.at(-1); chunks.push({ kind: "selectable", - key: `selectable:${first?.beg ?? "start"}:${last?.end ?? "end"}`, + key: `selectable:${nativeMarkdownNodePosition(first, selectableStart)}`, node: { type: "document", children: selectableNodes, @@ -719,6 +729,9 @@ export function nativeMarkdownDocumentChunks( for (const [index, child] of (document.children ?? []).entries()) { if (!containsRichBlock(child)) { + if (selectableNodes.length === 0) { + selectableStart = index; + } selectableNodes.push(child); continue; } @@ -726,7 +739,7 @@ export function nativeMarkdownDocumentChunks( flushSelectable(); chunks.push({ kind: "rich", - key: `rich:${child.type}:${child.beg ?? index}:${child.end ?? index}`, + key: `rich:${child.type}:${nativeMarkdownNodePosition(child, index)}`, node: child, }); } diff --git a/apps/mobile/modules/t3-markdown-text/src/pendingCodeHighlight.test.ts b/apps/mobile/modules/t3-markdown-text/src/pendingCodeHighlight.test.ts new file mode 100644 index 0000000000000..19516e7825248 --- /dev/null +++ b/apps/mobile/modules/t3-markdown-text/src/pendingCodeHighlight.test.ts @@ -0,0 +1,28 @@ +import { expect, it } from "vite-plus/test"; +import { pendingCodeHighlight } from "./pendingCodeHighlight"; + +it("keeps completed colors and exact current text without retaining an edited tail", () => { + const colored = [ + [{ content: "const n = 1;", color: "red", fontStyle: 0 }], + [{ content: "partial", color: "blue", fontStyle: 0 }], + ]; + const result = pendingCodeHighlight( + "const n = 1;\npartial", + "const n = 1;\nchanged\nnext", + colored, + )!; + expect(result[0]).toBe(colored[0]); + expect(result.map((line) => line.map((token) => token.content).join("")).join("\n")).toBe( + "const n = 1;\nchanged\nnext", + ); + expect( + result + .slice(1) + .flat() + .every((token) => token.color === null), + ).toBe(true); + expect( + pendingCodeHighlight("const n = 1;\npartial", "const n = 2;\npartial", colored), + ).toBeNull(); + expect(pendingCodeHighlight("partial", "other", colored)).toBeNull(); +}); diff --git a/apps/mobile/modules/t3-markdown-text/src/pendingCodeHighlight.ts b/apps/mobile/modules/t3-markdown-text/src/pendingCodeHighlight.ts new file mode 100644 index 0000000000000..308997fc83a8a --- /dev/null +++ b/apps/mobile/modules/t3-markdown-text/src/pendingCodeHighlight.ts @@ -0,0 +1,19 @@ +import type { MarkdownHighlightedToken } from "./SelectableMarkdownText.types"; + +/** Keep finished lines colored while the current line awaits highlighting. */ +export function pendingCodeHighlight( + previousCode: string, + code: string, + tokens: ReadonlyArray>, +): ReadonlyArray> | null { + const end = previousCode.lastIndexOf("\n") + 1; + if (!end || !code.startsWith(previousCode.slice(0, end))) return null; + const completedLines = previousCode.slice(0, end).split("\n").length - 1; + return [ + ...tokens.slice(0, completedLines), + ...code + .slice(end) + .split("\n") + .map((content) => [{ content, color: null, fontStyle: null }]), + ]; +} diff --git a/apps/mobile/modules/t3-markdown-text/src/useHighlightedCode.test.tsx b/apps/mobile/modules/t3-markdown-text/src/useHighlightedCode.test.tsx new file mode 100644 index 0000000000000..c4fcaa597db55 --- /dev/null +++ b/apps/mobile/modules/t3-markdown-text/src/useHighlightedCode.test.tsx @@ -0,0 +1,189 @@ +import { act, useLayoutEffect } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; + +import type { MarkdownCodeHighlighter } from "./SelectableMarkdownText.types"; +import { useHighlightedCode, type HighlightedCode } from "./useHighlightedCode"; + +type Deferred = { + readonly code: string; + readonly resolve: (tokens: HighlightedCode) => void; +}; + +/** One colored line per newline-separated line, so a line's color proves where it came from. */ +function tokensFor(code: string, color: string): HighlightedCode { + return code.split("\n").map((content) => [{ content, color, fontStyle: 0 }]); +} + +function lineText(tokens: HighlightedCode | null): string | null { + return tokens?.map((line) => line.map((token) => token.content).join("")).join("\n") ?? null; +} + +function lineColors(tokens: HighlightedCode | null): ReadonlyArray { + return tokens?.map((line) => line[0]?.color ?? null) ?? []; +} + +const deferred: Deferred[] = []; +const readResults = new Map(); +let renders = 0; +let result: HighlightedCode | null = null; +let root: Root; + +const highlightCode: MarkdownCodeHighlighter = Object.assign( + vi.fn( + (input: { code: string }) => + new Promise((resolve) => { + deferred.push({ code: input.code, resolve }); + }), + ), + { + read: vi.fn((input: { code: string }) => readResults.get(input.code)), + }, +); + +function Probe(props: { + readonly code: string; + readonly language?: string; + readonly theme?: "light" | "dark"; +}) { + const tokens = useHighlightedCode( + props.code, + props.language ?? "ts", + props.theme ?? "dark", + highlightCode, + ); + useLayoutEffect(() => { + renders += 1; + result = tokens; + }); + return null; +} + +async function render(props: Parameters[0]) { + await act(() => root.render()); +} + +async function resolveAsync(index: number, color: string) { + const pending = deferred[index]; + if (!pending) throw new Error(`no pending highlight at ${index}`); + await act(async () => { + pending.resolve(tokensFor(pending.code, color)); + }); +} + +beforeEach(async () => { + // The probe has no DOM output, but ReactDOM needs an event target. + const document = { + nodeType: 9, + addEventListener() {}, + removeEventListener() {}, + }; + const container = { + nodeType: 1, + tagName: "DIV", + namespaceURI: "http://www.w3.org/1999/xhtml", + ownerDocument: document, + addEventListener() {}, + removeEventListener() {}, + }; + vi.stubGlobal("document", document); + vi.stubGlobal("window", { document, HTMLIFrameElement: EventTarget }); + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + deferred.length = 0; + readResults.clear(); + renders = 0; + result = null; + vi.mocked(highlightCode).mockClear(); + vi.mocked(highlightCode.read!).mockClear(); + root = createRoot(container as unknown as HTMLElement); +}); + +afterEach(async () => { + await act(() => root.unmount()); + vi.unstubAllGlobals(); +}); + +// Every test uses a distinct language so the module-level token cache from an +// earlier test can never satisfy a later one. +describe("useHighlightedCode", () => { + it("keeps colors from the latest synchronous read when a later read misses", async () => { + const language = "sync-baseline"; + await render({ code: "a", language }); + expect(result).toBeNull(); + await resolveAsync(0, "async"); + expect(lineColors(result)).toEqual(["async"]); + + readResults.set("a\nb", tokensFor("a\nb", "sync-1")); + readResults.set("a\nb\nc", tokensFor("a\nb\nc", "sync-2")); + const rendersBeforeAppends = renders; + await render({ code: "a\nb", language }); + await render({ code: "a\nb\nc", language }); + expect(lineColors(result)).toEqual(["sync-2", "sync-2", "sync-2"]); + // A synchronous hit is one render per append: no state update, no highlighter call. + expect(renders - rendersBeforeAppends).toBe(2); + expect(highlightCode).toHaveBeenCalledTimes(1); + + await render({ code: "a\nb\nc\nd", language }); + expect(highlightCode).toHaveBeenCalledTimes(2); + expect(lineText(result)).toBe("a\nb\nc\nd"); + // Lines before the previous code's final newline are the completed ones. + expect(lineColors(result)).toEqual(["sync-2", "sync-2", null, null]); + + await resolveAsync(1, "async-2"); + expect(lineColors(result)).toEqual(["async-2", "async-2", "async-2", "async-2"]); + }); + + it("does not show obsolete text after a non-append edit", async () => { + const language = "non-append"; + await render({ code: "a\nb", language }); + await resolveAsync(0, "async"); + readResults.set("a\nb\nc", tokensFor("a\nb\nc", "sync")); + await render({ code: "a\nb\nc", language }); + expect(lineColors(result)).toEqual(["sync", "sync", "sync"]); + + await render({ code: "x\nb\nc", language }); + expect(result).toBeNull(); + await resolveAsync(1, "async-2"); + expect(lineText(result)).toBe("x\nb\nc"); + }); + + it("never reuses colors across a language or theme change", async () => { + const language = "theme-change"; + await render({ code: "a\nb", language }); + await resolveAsync(0, "async"); + readResults.set("a\nb\nc", tokensFor("a\nb\nc", "sync")); + await render({ code: "a\nb\nc", language }); + expect(lineColors(result)).toEqual(["sync", "sync", "sync"]); + + await render({ code: "a\nb\nc\nd", language, theme: "light" }); + expect(result).toBeNull(); + await render({ code: "a\nb\nc\nd", language: `${language}-other` }); + expect(result).toBeNull(); + }); + + it("ignores an asynchronous completion that is older than the current code", async () => { + const language = "stale-async"; + await render({ code: "a\nb", language }); + await resolveAsync(0, "async"); + + await render({ code: "a\nb\nc", language }); + expect(deferred).toHaveLength(2); + expect(lineColors(result)).toEqual(["async", null, null]); + + readResults.set("a\nb\nc\nd", tokensFor("a\nb\nc\nd", "sync")); + await render({ code: "a\nb\nc\nd", language }); + expect(lineColors(result)).toEqual(["sync", "sync", "sync", "sync"]); + + await render({ code: "a\nb\nc\nd\ne", language }); + expect(deferred).toHaveLength(3); + expect(lineColors(result)).toEqual(["sync", "sync", "sync", null, null]); + + // The older request finishes after newer synchronous results exist. + await resolveAsync(1, "stale"); + expect(lineText(result)).toBe("a\nb\nc\nd\ne"); + expect(lineColors(result)).toEqual(["sync", "sync", "sync", null, null]); + + await resolveAsync(2, "async-3"); + expect(lineColors(result)).toEqual(["async-3", "async-3", "async-3", "async-3", "async-3"]); + }); +}); diff --git a/apps/mobile/modules/t3-markdown-text/src/useHighlightedCode.ts b/apps/mobile/modules/t3-markdown-text/src/useHighlightedCode.ts new file mode 100644 index 0000000000000..dd86dc3af9afa --- /dev/null +++ b/apps/mobile/modules/t3-markdown-text/src/useHighlightedCode.ts @@ -0,0 +1,149 @@ +import { useEffect, useMemo, useRef, useState } from "react"; + +import { pendingCodeHighlight } from "./pendingCodeHighlight"; +import type { + MarkdownCodeHighlighter, + MarkdownHighlightedToken, +} from "./SelectableMarkdownText.types"; + +export type HighlightedCode = ReadonlyArray>; + +interface HighlightedCodeResult { + readonly key: string; + readonly code: string; + readonly language: string | undefined; + readonly theme: "light" | "dark"; + readonly tokens: HighlightedCode | null; +} + +const highlightedCodeCache = new Map(); +const highlightedCodePromiseCache = new Map>(); +const HIGHLIGHTED_CODE_CACHE_LIMIT = 64; + +function codeHighlightCacheKey( + code: string, + language: string | undefined, + theme: "light" | "dark", +): string { + return `${theme}:${language ?? "text"}:${code}`; +} + +function cacheHighlightedCode(key: string, tokens: HighlightedCode): void { + highlightedCodeCache.delete(key); + highlightedCodeCache.set(key, tokens); + + while (highlightedCodeCache.size > HIGHLIGHTED_CODE_CACHE_LIMIT) { + const oldestKey = highlightedCodeCache.keys().next().value; + if (oldestKey === undefined) { + break; + } + highlightedCodeCache.delete(oldestKey); + } +} + +function loadHighlightedCode( + code: string, + language: string | undefined, + theme: "light" | "dark", + highlightCode: MarkdownCodeHighlighter, + session: object, +): Promise { + const key = codeHighlightCacheKey(code, language, theme); + const cached = highlightedCodeCache.get(key); + if (cached) { + return Promise.resolve(cached); + } + + const pending = highlightedCodePromiseCache.get(key); + if (pending) { + return pending; + } + + const promise = highlightCode({ code, language, theme, session }) + .then((tokens) => { + cacheHighlightedCode(key, tokens); + highlightedCodePromiseCache.delete(key); + return tokens; + }) + .catch((error) => { + highlightedCodePromiseCache.delete(key); + throw error; + }); + highlightedCodePromiseCache.set(key, promise); + return promise; +} + +/** + * Tokens for a code block, or null while nothing usable exists yet. A cached or + * synchronous result renders in the same pass. Otherwise the most recent + * completed result seeds `pendingCodeHighlight` so finished lines keep their + * colors while the asynchronous highlighter catches up. + */ +export function useHighlightedCode( + code: string, + language: string | undefined, + theme: "light" | "dark", + highlightCode: MarkdownCodeHighlighter, +): HighlightedCode | null { + const [session] = useState(() => ({})); + const key = codeHighlightCacheKey(code, language, theme); + const ready = useMemo( + () => highlightedCodeCache.get(key) ?? highlightCode.read?.({ code, language, theme, session }), + [code, language, theme, key, highlightCode, session], + ); + const [highlighted, setHighlighted] = useState(() => ({ + code, + language, + theme, + key, + tokens: highlightedCodeCache.get(key) ?? null, + })); + // Synchronous reads never touch state, so each append costs one render. The + // ref remembers the newest of them, and is cleared whenever an asynchronous + // result commits so `highlighted` is the baseline again from then on. + const latestRead = useRef(null); + + useEffect(() => { + if (ready) { + latestRead.current = { code, language, theme, key, tokens: ready }; + return; + } + let active = true; + const commit = (tokens: HighlightedCode | null) => { + latestRead.current = null; + setHighlighted({ code, language, theme, key, tokens }); + }; + const cached = highlightedCodeCache.get(key); + if (cached) { + cacheHighlightedCode(key, cached); + commit(cached); + return () => { + active = false; + }; + } + + void loadHighlightedCode(code, language, theme, highlightCode, session) + .then((tokens) => { + if (active) { + commit(tokens); + } + }) + .catch(() => { + if (active) { + commit(null); + } + }); + return () => { + active = false; + }; + }, [code, highlightCode, key, language, theme, ready, session]); + + if (ready) return ready; + // oxlint-disable-next-line react/refs -- Written only after commit; a discarded render never advances it. + const baseline = latestRead.current ?? highlighted; + if (baseline.key === key) return baseline.tokens; + if (baseline.tokens && baseline.language === language && baseline.theme === theme) { + return pendingCodeHighlight(baseline.code, code, baseline.tokens); + } + return null; +} diff --git a/apps/mobile/src/features/review/incrementalSnippet.test.ts b/apps/mobile/src/features/review/incrementalSnippet.test.ts new file mode 100644 index 0000000000000..3e768bbcd5393 --- /dev/null +++ b/apps/mobile/src/features/review/incrementalSnippet.test.ts @@ -0,0 +1,85 @@ +import { createHighlighterCore } from "@shikijs/core"; +import { createJavaScriptRegexEngine } from "@shikijs/engine-javascript"; +import typescript from "@shikijs/langs/typescript"; +import dark from "@shikijs/themes/github-dark-default"; +import { describe, expect, it } from "vite-plus/test"; + +import { createIncrementalSnippet } from "./incrementalSnippet"; +import { highlightCodeSnippet } from "./shikiReviewHighlighter"; + +const highlighter = await createHighlighterCore({ + langs: [typescript], + themes: [dark], + engine: createJavaScriptRegexEngine(), +}); +const rendered = (lines: ReturnType) => + lines.map((line) => line.map(({ content, color, fontStyle }) => ({ content, color, fontStyle }))); +const options = { lang: "typescript", theme: "github-dark-default" }; + +describe("incremental snippet highlighting", () => { + it("matches full highlighting through partial lines, multiline syntax, edits and truncation", async () => { + const highlight = createIncrementalSnippet(highlighter, options.lang, options.theme); + const source = "/* comment\nstill comment */\nconst text = `first\nsecond ${42}`;\n"; + for (let end = 1; end <= source.length; end++) { + const code = source.slice(0, end); + expect(rendered(highlight.read(code) ?? (await highlight(code)))).toEqual( + rendered(highlighter.codeToTokensBase(code, options)), + ); + } + for (const code of ['const other = "new";\n', "const other", source, source.slice(0, 20)]) { + expect(rendered(highlight.read(code) ?? (await highlight(code)))).toEqual( + rendered(highlighter.codeToTokensBase(code, options)), + ); + } + }); + + it("carries grammar state across batches and overlapping updates", async () => { + const highlight = createIncrementalSnippet(highlighter, options.lang, options.theme); + const code = "/*\n" + "still a comment\n".repeat(410) + "*/\nconst value = 42;"; + const first = highlight(code); + const next = highlight("const changed = true;\n"); + expect(rendered(await first)).toEqual(rendered(highlighter.codeToTokensBase(code, options))); + expect(rendered(await next)).toEqual( + rendered(highlighter.codeToTokensBase("const changed = true;\n", options)), + ); + const last = 'const changed = true;\nconst tail = "ok";'; + expect(rendered(await highlight(last))).toEqual( + rendered(highlighter.codeToTokensBase(last, options)), + ); + }); + + it("reuses completed token rows on warm reads and falls back for large updates", async () => { + const session = {}; + const input = { language: "ts", theme: "dark" as const, session }; + const code = "const a = 1;\nconst b = 2;"; + expect(highlightCodeSnippet.read({ ...input, code })).toBeUndefined(); + const first = await highlightCodeSnippet({ ...input, code }); + const nextCode = code + "\nconst c = 3;"; + const next = highlightCodeSnippet.read({ ...input, code: nextCode })!; + expect(next[0]).toBe(first[0]); + expect(next).toEqual( + await highlightCodeSnippet({ code: nextCode, language: "ts", theme: "dark" }), + ); + expect( + highlightCodeSnippet.read({ ...input, code: nextCode + "\n" + "const x = 1;\n".repeat(210) }), + ).toBeUndefined(); + expect(highlightCodeSnippet.read({ ...input, code: nextCode, theme: "light" })).toBeUndefined(); + }); + + it("resets session on language, theme, CRLF, long-line and empty input changes", async () => { + const session = {}; + const cases = [ + { code: "/* hello\nworld */", language: "ts", theme: "dark" as const }, + { code: "/* hello\nworld */\nconst n = 3;", language: "ts", theme: "light" as const }, + { code: "hello\nworld", language: "text", theme: "light" as const }, + { code: "const n = 2;\r\nconst m = 3;", language: "ts", theme: "dark" as const }, + { code: "a".repeat(1100), language: "ts", theme: "dark" as const }, + { code: "", language: "ts", theme: "dark" as const }, + { code: "const fresh = true;\n", language: "ts", theme: "dark" as const }, + ]; + for (const input of cases) + expect(await highlightCodeSnippet({ ...input, session })).toEqual( + await highlightCodeSnippet(input), + ); + }); +}); diff --git a/apps/mobile/src/features/review/incrementalSnippet.ts b/apps/mobile/src/features/review/incrementalSnippet.ts new file mode 100644 index 0000000000000..ba8acd847b413 --- /dev/null +++ b/apps/mobile/src/features/review/incrementalSnippet.ts @@ -0,0 +1,68 @@ +import type { HighlighterCore } from "@shikijs/core"; + +/** A code block owns this session. Only completed lines survive the next update. */ +export function createIncrementalSnippet( + highlighter: HighlighterCore, + language: string, + theme: string, +) { + const options = { lang: language, theme }; + let cached: + | { + prefix: string; + tokens: ReturnType; + state: ReturnType; + } + | undefined; + let revision = 0; + + function* tokenize(code: string) { + const currentRevision = ++revision; + const previous = cached && code.startsWith(cached.prefix) ? cached : undefined; + const end = code.lastIndexOf("\n") + 1; + let state = previous?.state; + const tokens = [...(previous?.tokens ?? [])]; + const completed = code.slice(previous?.prefix.length ?? 0, end); + if (completed) { + const lines = completed.slice(0, -1).split("\n"); + for (let offset = 0; offset < lines.length; offset += 200) { + const batch = highlighter.codeToTokensBase(lines.slice(offset, offset + 200).join("\n"), { + ...options, + ...(state ? { grammarState: state } : {}), + }); + state = highlighter.getLastGrammarState(batch); + tokens.push(...batch); + if (offset + 200 < lines.length) yield; + } + } + if (currentRevision === revision) { + cached = state ? { prefix: code.slice(0, end), tokens, state } : undefined; + } + return [ + ...tokens, + ...highlighter.codeToTokensBase(code.slice(end), { + ...options, + ...(state ? { grammarState: state } : {}), + }), + ]; + } + const highlight = async (code: string) => { + const work = tokenize(code); + let next = work.next(); + while (!next.done) { + await new Promise((resolve) => setTimeout(resolve, 0)); + next = work.next(); + } + return next.value; + }; + // A small append can finish during render, avoiding a plain-text commit before + // the asynchronous effect supplies colors. Cold/large changes stay asynchronous. + highlight.read = (code: string) => { + if (!cached || !code.startsWith(cached.prefix)) return undefined; + const tail = code.slice(cached.prefix.length); + if (tail.length > 2_000 || tail.split("\n").length > 200) return undefined; + const next = tokenize(code).next(); + return next.done ? next.value : undefined; + }; + return highlight; +} diff --git a/apps/mobile/src/features/review/shikiReviewHighlighter.ts b/apps/mobile/src/features/review/shikiReviewHighlighter.ts index a2e3fbd4eb0c1..52e0b83be34d6 100644 --- a/apps/mobile/src/features/review/shikiReviewHighlighter.ts +++ b/apps/mobile/src/features/review/shikiReviewHighlighter.ts @@ -17,6 +17,7 @@ import { resolveReviewHighlighterEnginePreference, type ReviewHighlighterEngine, } from "./reviewHighlighterEngine"; +import { createIncrementalSnippet } from "./incrementalSnippet"; import type { ReviewRenderableLineRow } from "./reviewModel"; import { applyDiffRangesToTokens, computeWordAltDiffRanges } from "./reviewWordDiffs"; @@ -453,16 +454,23 @@ async function resolveLanguageFromPath( return candidate; } +type RawHighlightedLine = ReadonlyArray<{ content: string; color?: string; fontStyle?: number }>; +const normalizedLines = new WeakMap>(); + function normalizeHighlightedLines( - tokenLines: ReadonlyArray>, + tokenLines: ReadonlyArray, ): ReadonlyArray> { - return tokenLines.map((line) => - line.map((token) => ({ + return tokenLines.map((line) => { + const cached = normalizedLines.get(line); + if (cached) return cached; + const normalized = line.map((token) => ({ content: token.content, color: token.color ?? null, fontStyle: token.fontStyle ?? null, - })), - ); + })); + normalizedLines.set(line, normalized); + return normalized; + }); } function applyWordAltDiffHighlightsToSelectedLines(input: { @@ -594,16 +602,62 @@ async function highlightLines( return highlightedLines; } +const snippetSessions = new WeakMap< + object, + { language: string; theme: string; highlight: ReturnType } +>(); + export async function highlightCodeSnippet(input: { + readonly session?: object; readonly code: string; readonly language?: string | null; readonly theme: ReviewDiffTheme; }): Promise>> { const languageHint = input.language?.trim() || "text"; const language = await resolveLanguageFromPath(`snippet.${languageHint}`, languageHint); - return highlightLines(input.code, language, SHIKI_THEME_NAME_BY_SCHEME[input.theme]); + const theme = SHIKI_THEME_NAME_BY_SCHEME[input.theme]; + // Bound retained text and preserve the existing plain-text/long-line fallback. + if ( + !input.session || + language === "text" || + input.code.length === 0 || + input.code.length > 100_000 || + input.code.includes("\r") || + input.code.split("\n").some((line) => line.length > REVIEW_TOKENIZE_MAX_LINE_LENGTH) + ) { + if (input.session) snippetSessions.delete(input.session); + return highlightLines(input.code, language, theme); + } + const highlighter = await getHighlighter(); + let session = snippetSessions.get(input.session); + if (!session || session.language !== language || session.theme !== theme) { + session = { + language, + theme, + highlight: createIncrementalSnippet(highlighter, language, theme), + }; + snippetSessions.set(input.session, session); + } + return normalizeHighlightedLines(await session.highlight(input.code)); } +highlightCodeSnippet.read = (input: Parameters[0]) => { + if (!input.session || !input.code || input.code.length > 100_000 || input.code.includes("\r")) + return undefined; + const session = snippetSessions.get(input.session); + const hint = input.language?.trim() || "text"; + const language = resolveLoadedLanguageFromPath(`snippet.${hint}`, hint); + if ( + !session || + session.language !== language || + session.theme !== SHIKI_THEME_NAME_BY_SCHEME[input.theme] || + input.code.split("\n").some((line) => line.length > REVIEW_TOKENIZE_MAX_LINE_LENGTH) + ) + return undefined; + const tokens = session.highlight.read(input.code); + return tokens ? normalizeHighlightedLines(tokens) : undefined; +}; + export async function highlightSourceFile(input: { readonly path: string; readonly contents: string; diff --git a/apps/mobile/src/lib/nativeMarkdownText.test.ts b/apps/mobile/src/lib/nativeMarkdownText.test.ts index c3951c8d81f05..bf7e90e419731 100644 --- a/apps/mobile/src/lib/nativeMarkdownText.test.ts +++ b/apps/mobile/src/lib/nativeMarkdownText.test.ts @@ -536,7 +536,7 @@ describe("nativeMarkdownDocumentChunks", () => { ).toEqual([ { kind: "rich", - key: "rich:blockquote:0:120", + key: "rich:blockquote:offset:0", node: blockquote, }, ]); @@ -663,7 +663,7 @@ describe("nativeMarkdownDocumentChunks", () => { expect(chunks[0]).toMatchObject({ kind: "selectable" }); expect(chunks[1]).toEqual({ kind: "rich", - key: "rich:code_block:11:35", + key: "rich:code_block:offset:11", node: document.children?.[1], }); expect(chunks[2]).toMatchObject({ kind: "selectable" }); @@ -700,7 +700,7 @@ describe("nativeMarkdownDocumentChunks", () => { expect(nativeMarkdownDocumentChunks(document)).toEqual([ { kind: "rich", - key: "rich:list:0:45", + key: "rich:list:offset:0", node: document.children?.[0], }, ]); @@ -728,7 +728,7 @@ describe("nativeMarkdownDocumentChunks", () => { expect(chunks[0]).toMatchObject({ kind: "selectable" }); expect(chunks[1]).toEqual({ kind: "rich", - key: "rich:horizontal_rule:1:1", + key: "rich:horizontal_rule:index:1", node: document.children?.[1], }); expect(chunks[2]).toMatchObject({ kind: "selectable" }); @@ -774,7 +774,7 @@ describe("nativeMarkdownDocumentChunks", () => { expect(chunks[0]).toMatchObject({ kind: "selectable" }); expect(chunks[1]).toEqual({ kind: "rich", - key: "rich:list:1:1", + key: "rich:list:index:1", node: document.children?.[1], }); expect(chunks[2]).toMatchObject({ kind: "selectable" }); @@ -824,6 +824,89 @@ describe("nativeMarkdownDocumentChunks", () => { }); }); + it("keys positioned and offset-free siblings in separate namespaces", () => { + const positioned: MarkdownNode = { + type: "blockquote", + beg: 1, + end: 10, + children: [{ type: "paragraph", children: [{ type: "text", content: "Positioned" }] }], + }; + const offsetFree: MarkdownNode = { + type: "blockquote", + children: [{ type: "paragraph", children: [{ type: "text", content: "Generated" }] }], + }; + const chunks = nativeMarkdownDocumentChunks({ + type: "document", + children: [ + { type: "paragraph", beg: 0, end: 0, children: [] }, + offsetFree, + positioned, + { type: "paragraph", children: [{ type: "text", content: "Tail" }] }, + ], + }); + + expect(chunks.map((chunk) => chunk.key)).toEqual([ + "selectable:offset:0", + "rich:blockquote:index:1", + "rich:blockquote:offset:1", + "selectable:index:3", + ]); + }); + + it("gives every offset-free selectable group its own key", () => { + const chunks = nativeMarkdownDocumentChunks({ + type: "document", + children: [ + { type: "paragraph", children: [{ type: "text", content: "One" }] }, + { type: "horizontal_rule" }, + { type: "paragraph", children: [{ type: "text", content: "Two" }] }, + { type: "horizontal_rule" }, + { type: "paragraph", children: [{ type: "text", content: "Three" }] }, + ], + }); + + expect(chunks.map((chunk) => chunk.key)).toEqual([ + "selectable:index:0", + "rich:horizontal_rule:index:1", + "selectable:index:2", + "rich:horizontal_rule:index:3", + "selectable:index:4", + ]); + }); + + it("keeps positioned chunk keys stable while text streams in", () => { + const before: MarkdownNode = { + type: "document", + children: [ + { type: "paragraph", beg: 0, end: 5, children: [{ type: "text", content: "Intro" }] }, + { + type: "code_block", + language: "ts", + beg: 7, + end: 20, + children: [{ type: "text", content: "const a" }], + }, + ], + }; + const after: MarkdownNode = { + type: "document", + children: [ + { type: "paragraph", beg: 0, end: 5, children: [{ type: "text", content: "Intro" }] }, + { + type: "code_block", + language: "ts", + beg: 7, + end: 40, + children: [{ type: "text", content: "const a = 1;\nconst b" }], + }, + ], + }; + + expect(nativeMarkdownDocumentChunks(after).map((chunk) => chunk.key)).toEqual( + nativeMarkdownDocumentChunks(before).map((chunk) => chunk.key), + ); + }); + it("keeps a plain list in one selectable native text container", () => { const list: MarkdownNode = { type: "list", From 6e8931d75615ec020e602e97df2694b0541a6272 Mon Sep 17 00:00:00 2001 From: Bilal Bakr <62337003+Bil0000@users.noreply.github.com> Date: Fri, 11 Sep 2026 23:20:54 +0300 Subject: [PATCH 25/63] perf(client): reduce remote request and message sync overhead (#11029) --- .../src/authorization/remote.ts | 26 +-- .../src/environment/descriptor.ts | 6 +- .../src/remotePerformance.bench.ts | 165 ++++++++++++++++++ packages/client-runtime/src/rpc/http.ts | 14 ++ .../src/state/environmentHttpAuth.test.ts | 11 ++ .../src/state/environmentHttpAuth.ts | 22 ++- .../src/state/pullRequestDiffHttp.ts | 3 +- packages/client-runtime/src/state/session.ts | 3 +- .../src/state/shellSnapshotHttp.ts | 3 +- .../src/state/threadReducer.test.ts | 48 +++++ .../client-runtime/src/state/threadReducer.ts | 39 ++--- .../src/state/threadSnapshotHttp.ts | 3 +- 12 files changed, 296 insertions(+), 47 deletions(-) create mode 100644 packages/client-runtime/src/remotePerformance.bench.ts diff --git a/packages/client-runtime/src/authorization/remote.ts b/packages/client-runtime/src/authorization/remote.ts index 538c0aa114a37..be863e189a9ea 100644 --- a/packages/client-runtime/src/authorization/remote.ts +++ b/packages/client-runtime/src/authorization/remote.ts @@ -11,7 +11,7 @@ import * as Effect from "effect/Effect"; import { environmentEndpointUrl } from "../environment/endpoint.ts"; import { executeEnvironmentHttpRequest, - makeEnvironmentHttpApiClient, + makeEnvironmentHttpApiGroupClient, type RemoteEnvironmentRequestError, } from "../rpc/http.ts"; @@ -93,11 +93,11 @@ export const exchangeRemoteDpopAccessToken = Effect.fn( readonly dpopProof: string; readonly timeoutMs?: number; }) { - const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl); + const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth"); const response = yield* executeEnvironmentHttpRequest( environmentEndpointUrl(input.httpBaseUrl, "/oauth/token"), input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS, - client.auth.token({ + client.token({ headers: { dpop: input.dpopProof }, payload: { grant_type: AuthTokenExchangeGrantType, @@ -121,11 +121,11 @@ export const bootstrapRemoteBearerSession = Effect.fn( readonly clientMetadata?: AuthClientPresentationMetadata; readonly timeoutMs?: number; }) { - const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl); + const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth"); return yield* executeEnvironmentHttpRequest( environmentEndpointUrl(input.httpBaseUrl, "/oauth/token"), input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS, - client.auth.token({ + client.token({ headers: {}, payload: { grant_type: AuthTokenExchangeGrantType, @@ -146,11 +146,11 @@ export const fetchRemoteSessionState = Effect.fn( readonly bearerToken: string; readonly timeoutMs?: number; }) { - const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl); + const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth"); return yield* executeEnvironmentHttpRequest( environmentEndpointUrl(input.httpBaseUrl, "/api/auth/session"), input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS, - client.auth.session({ + client.session({ headers: { authorization: `Bearer ${input.bearerToken}`, }, @@ -166,11 +166,11 @@ export const fetchRemoteDpopSessionState = Effect.fn( readonly dpopProof: string; readonly timeoutMs?: number; }) { - const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl); + const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth"); return yield* executeEnvironmentHttpRequest( environmentEndpointUrl(input.httpBaseUrl, "/api/auth/session"), input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS, - client.auth.session({ + client.session({ headers: { authorization: `DPoP ${input.accessToken}`, dpop: input.dpopProof, @@ -186,11 +186,11 @@ export const issueRemoteWebSocketTicket = Effect.fn( readonly bearerToken: string; readonly timeoutMs?: number; }) { - const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl); + const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth"); return yield* executeEnvironmentHttpRequest( environmentEndpointUrl(input.httpBaseUrl, "/api/auth/websocket-ticket"), input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS, - client.auth.webSocketTicket({ + client.webSocketTicket({ headers: { authorization: `Bearer ${input.bearerToken}`, }, @@ -206,11 +206,11 @@ export const issueRemoteDpopWebSocketTicket = Effect.fn( readonly dpopProof: string; readonly timeoutMs?: number; }) { - const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl); + const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth"); return yield* executeEnvironmentHttpRequest( environmentEndpointUrl(input.httpBaseUrl, "/api/auth/websocket-ticket"), input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS, - client.auth.webSocketTicket({ + client.webSocketTicket({ headers: { authorization: `DPoP ${input.accessToken}`, dpop: input.dpopProof, diff --git a/packages/client-runtime/src/environment/descriptor.ts b/packages/client-runtime/src/environment/descriptor.ts index d49a0d9a89049..1f92b296c5e33 100644 --- a/packages/client-runtime/src/environment/descriptor.ts +++ b/packages/client-runtime/src/environment/descriptor.ts @@ -1,17 +1,17 @@ import * as Effect from "effect/Effect"; import { environmentEndpointUrl } from "./endpoint.ts"; -import { executeEnvironmentHttpRequest, makeEnvironmentHttpApiClient } from "../rpc/http.ts"; +import { executeEnvironmentHttpRequest, makeEnvironmentHttpApiGroupClient } from "../rpc/http.ts"; const DEFAULT_REMOTE_REQUEST_TIMEOUT_MS = 10_000; export const fetchRemoteEnvironmentDescriptor = Effect.fn( "clientRuntime.environment.fetchRemoteEnvironmentDescriptor", )(function* (input: { readonly httpBaseUrl: string; readonly timeoutMs?: number }) { - const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl); + const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "metadata"); return yield* executeEnvironmentHttpRequest( environmentEndpointUrl(input.httpBaseUrl, "/.well-known/t3/environment"), input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS, - client.metadata.descriptor(), + client.descriptor(), ); }); diff --git a/packages/client-runtime/src/remotePerformance.bench.ts b/packages/client-runtime/src/remotePerformance.bench.ts new file mode 100644 index 0000000000000..85b4cf732c42e --- /dev/null +++ b/packages/client-runtime/src/remotePerformance.bench.ts @@ -0,0 +1,165 @@ +import { + EnvironmentId, + EventId, + MessageId, + ProjectId, + ProviderInstanceId, + ThreadId, + type OrchestrationEvent, + type OrchestrationThread, +} from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; +import { HttpClient, HttpClientResponse } from "effect/unstable/http"; +import { bench, describe } from "vite-plus/test"; + +import { issueRemoteWebSocketTicket } from "./authorization/remote.ts"; +import { PrimaryConnectionTarget } from "./connection/model.ts"; +import { fetchRemoteEnvironmentDescriptor } from "./environment/descriptor.ts"; +import type { RemoteEnvironmentRequestError } from "./rpc/http.ts"; +import { fetchEnvironmentThreadSnapshot } from "./state/threadSnapshotHttp.ts"; +import { applyThreadDetailEvent } from "./state/threadReducer.ts"; + +const timestamp = "2026-09-01T00:00:00.000Z"; +const thread: OrchestrationThread = { + id: ThreadId.make("thread-1"), + projectId: ProjectId.make("project-1"), + title: "Remote thread", + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + latestTurn: null, + createdAt: timestamp, + updatedAt: timestamp, + archivedAt: null, + settledOverride: null, + settledAt: null, + deletedAt: null, + pullRequests: [], + messages: Array.from({ length: 100 }, (_, index) => ({ + id: MessageId.make(`message-${index}`), + role: "assistant", + text: "Message text. ".repeat(40), + turnId: null, + streaming: false, + createdAt: timestamp, + updatedAt: timestamp, + })), + proposedPlans: [], + activities: [], + checkpoints: [], + session: null, +}; +const target = new PrimaryConnectionTarget({ + environmentId: EnvironmentId.make("remote-1"), + label: "Remote", + httpBaseUrl: "https://remote.example.test", + wsBaseUrl: "wss://remote.example.test/ws", +}); +const responses = { + "/.well-known/t3/environment": { + environmentId: target.environmentId, + label: target.label, + platform: { os: "linux", arch: "x64" }, + serverVersion: "0.0.0-test", + capabilities: { repositoryIdentity: true }, + }, + "/api/auth/websocket-ticket": { ticket: "test-ticket", expiresAt: timestamp }, + "/api/orchestration/threads/thread-1": { snapshotSequence: 1, thread }, +}; +const httpClient = HttpClient.make((request) => + Effect.sync(() => { + const path = new URL(request.url).pathname as keyof typeof responses; + return HttpClientResponse.fromWeb(request, Response.json(responses[path])); + }), +); +const requests: Record< + string, + Effect.Effect +> = { + "read remote connection descriptor": fetchRemoteEnvironmentDescriptor({ + httpBaseUrl: target.httpBaseUrl, + }), + "issue remote WebSocket ticket": issueRemoteWebSocketTicket({ + httpBaseUrl: target.httpBaseUrl, + bearerToken: "test-token", + }), + "load remote snapshot with 100 messages": fetchEnvironmentThreadSnapshot({ + prepared: { + environmentId: target.environmentId, + label: target.label, + httpBaseUrl: target.httpBaseUrl, + socketUrl: target.wsBaseUrl, + httpAuthorization: null, + target, + }, + threadId: thread.id, + signer: Option.none(), + }), +}; + +describe("remote HTTP processing with an in-memory transport", () => { + for (const [name, request] of Object.entries(requests)) { + bench( + name, + async () => { + await Effect.runPromise( + request.pipe(Effect.provideService(HttpClient.HttpClient, httpClient)), + ); + }, + { warmupTime: 1_000, time: 1_500 }, + ); + } +}); + +const delta: OrchestrationEvent = { + eventId: EventId.make("delta"), + sequence: 2, + aggregateKind: "thread", + aggregateId: thread.id, + occurredAt: timestamp, + commandId: null, + causationEventId: null, + correlationId: null, + metadata: {}, + type: "thread.message-sent", + payload: { + threadId: thread.id, + messageId: MessageId.make("message-99"), + role: "assistant", + text: " next", + turnId: null, + streaming: true, + createdAt: timestamp, + updatedAt: timestamp, + }, +}; + +describe("remote message replay", () => { + for (const count of [100, 1_000]) { + const loaded = { + ...thread, + messages: Array.from({ length: count }, (_, index) => ({ + ...thread.messages[0]!, + id: MessageId.make(`message-${index}`), + })), + }; + const event = { + ...delta, + payload: { ...delta.payload, messageId: loaded.messages.at(-1)!.id }, + }; + bench( + `apply 200 text deltas to ${count} loaded messages`, + () => { + let current: OrchestrationThread = loaded; + for (let index = 0; index < 200; index += 1) { + const result = applyThreadDetailEvent(current, event); + if (result.kind === "updated") current = result.thread; + } + }, + { warmupTime: 1_000, time: 1_500 }, + ); + } +}); diff --git a/packages/client-runtime/src/rpc/http.ts b/packages/client-runtime/src/rpc/http.ts index e52e012954f7f..d1c470d171ffd 100644 --- a/packages/client-runtime/src/rpc/http.ts +++ b/packages/client-runtime/src/rpc/http.ts @@ -99,6 +99,20 @@ export const makeEnvironmentHttpApiClient = (httpBaseUrl: string) => baseUrl: remoteApiBaseUrl(httpBaseUrl), }); +export const makeEnvironmentHttpApiGroupClient = < + Group extends keyof typeof EnvironmentHttpApi.groups, +>( + httpBaseUrl: string, + group: Group, +) => + Effect.flatMap(HttpClient.HttpClient, (httpClient) => + HttpApiClient.group(EnvironmentHttpApi, { + httpClient, + group, + baseUrl: remoteApiBaseUrl(httpBaseUrl), + }), + ); + /** Contract-derived request URLs for authentication proofs, tracing, and structured errors. */ export const makeEnvironmentHttpApiUrlBuilder = (httpBaseUrl: string) => HttpApiClient.urlBuilder(EnvironmentHttpApi, { diff --git a/packages/client-runtime/src/state/environmentHttpAuth.test.ts b/packages/client-runtime/src/state/environmentHttpAuth.test.ts index 2d4152356ad5b..f8fb0429cc5a9 100644 --- a/packages/client-runtime/src/state/environmentHttpAuth.test.ts +++ b/packages/client-runtime/src/state/environmentHttpAuth.test.ts @@ -215,6 +215,17 @@ const LOADERS: ReadonlyArray<{ ]; describe("authenticated environment HTTP requests", () => { + it.effect.each(LOADERS)("rejects an invalid $name response", (loader) => + Effect.gen(function* () { + const harness = makeHarness(() => Response.json({})); + const result = yield* loader + .load(harness.input) + .pipe(Effect.provide(harness.httpLayer), Effect.asVoid, Effect.flip); + expect(result._tag).toBe("RemoteEnvironmentAuthInvalidJsonError"); + expect(harness.calls).toHaveLength(1); + }), + ); + it.effect.each(LOADERS)("uses current relay authorization and endpoint for $name", (loader) => Effect.gen(function* () { const harness = makeHarness(() => Response.json(loader.response)); diff --git a/packages/client-runtime/src/state/environmentHttpAuth.ts b/packages/client-runtime/src/state/environmentHttpAuth.ts index 019b52ddd3597..29dd08f9f1886 100644 --- a/packages/client-runtime/src/state/environmentHttpAuth.ts +++ b/packages/client-runtime/src/state/environmentHttpAuth.ts @@ -1,14 +1,14 @@ import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; import * as Result from "effect/Result"; -import { FetchHttpClient, type HttpClient, type HttpMethod } from "effect/unstable/http"; +import { FetchHttpClient, type HttpMethod } from "effect/unstable/http"; import type { RemoteEnvironmentAuthorization } from "../authorization/service.ts"; import type { PreparedConnection, PreparedHttpAuthorization } from "../connection/model.ts"; import type { ManagedRelayDpopSigner } from "../relay/managedRelay.ts"; import { executeEnvironmentHttpRequest, - makeEnvironmentHttpApiClient, + makeEnvironmentHttpApiGroupClient, RemoteEnvironmentAuthFetchError, RemoteEnvironmentAuthTimeoutError, type RemoteEnvironmentRequestError, @@ -86,20 +86,30 @@ const buildEnvironmentAuthHeaders = ( */ export const executeAuthenticatedEnvironmentHttpRequest = Effect.fn( "clientRuntime.state.executeAuthenticatedEnvironmentHttpRequest", -)(function* (input: { +)(function* < + Group extends Parameters[1], + A, + E, + R, +>(input: { readonly prepared: PreparedConnection; readonly signer: Option.Option; readonly remoteAuthorization?: Option.Option; readonly method: HttpMethod.HttpMethod; readonly url: (httpBaseUrl: string) => string; readonly timeoutMs: number; + readonly group: Group; readonly request: (input: { - readonly client: Effect.Success>; + readonly client: Effect.Success>>; readonly headers: EnvironmentHttpAuthHeaders; }) => Effect.Effect; /** Some endpoints report rejected credentials in a successful response. */ readonly isUnauthorizedResponse?: (response: NoInfer) => boolean; -}): Effect.fn.Return { +}): Effect.fn.Return< + A, + RemoteEnvironmentRequestError, + Effect.Services>> | R +> { let httpBaseUrl = input.prepared.httpBaseUrl; return yield* Effect.gen(function* () { let rejectedAccessToken: string | undefined; @@ -132,7 +142,7 @@ export const executeAuthenticatedEnvironmentHttpRequest = Effect.fn( } const requestUrl = input.url(httpBaseUrl); - const client = yield* makeEnvironmentHttpApiClient(httpBaseUrl); + const client = yield* makeEnvironmentHttpApiGroupClient(httpBaseUrl, input.group); const headers = yield* buildEnvironmentAuthHeaders( authorization, input.method, diff --git a/packages/client-runtime/src/state/pullRequestDiffHttp.ts b/packages/client-runtime/src/state/pullRequestDiffHttp.ts index f1a250e61018e..dd514259d983b 100644 --- a/packages/client-runtime/src/state/pullRequestDiffHttp.ts +++ b/packages/client-runtime/src/state/pullRequestDiffHttp.ts @@ -50,10 +50,11 @@ export const fetchEnvironmentPullRequestDiff = Effect.fn( }) { return yield* executeAuthenticatedEnvironmentHttpRequest({ ...input, + group: "pullRequests", method: "POST", url: (httpBaseUrl) => makeEnvironmentHttpApiUrlBuilder(httpBaseUrl).pullRequests.diff(), timeoutMs: input.timeoutMs ?? DEFAULT_PULL_REQUEST_DIFF_TIMEOUT_MS, - request: ({ client, headers }) => client.pullRequests.diff({ payload: input.diff, headers }), + request: ({ client, headers }) => client.diff({ payload: input.diff, headers }), }).pipe( Effect.mapError((error) => error._tag === "EnvironmentAuthInvalidError" && error.reason === "invalid_credential" diff --git a/packages/client-runtime/src/state/session.ts b/packages/client-runtime/src/state/session.ts index 5ed60afc8a5e8..9a0a9dfe9a5fc 100644 --- a/packages/client-runtime/src/state/session.ts +++ b/packages/client-runtime/src/state/session.ts @@ -49,10 +49,11 @@ export const fetchEnvironmentSessionState = Effect.fn( }) { return yield* executeAuthenticatedEnvironmentHttpRequest({ ...input, + group: "auth", method: "GET", url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, "/api/auth/session"), timeoutMs: input.timeoutMs ?? DEFAULT_SESSION_STATE_TIMEOUT_MS, - request: ({ client, headers }) => client.auth.session({ headers }), + request: ({ client, headers }) => client.session({ headers }), // This endpoint returns 200 with authenticated:false for expired credentials. isUnauthorizedResponse: (response) => !response.authenticated, }); diff --git a/packages/client-runtime/src/state/shellSnapshotHttp.ts b/packages/client-runtime/src/state/shellSnapshotHttp.ts index aa1ad9081e05d..84ab1a3f1d4b0 100644 --- a/packages/client-runtime/src/state/shellSnapshotHttp.ts +++ b/packages/client-runtime/src/state/shellSnapshotHttp.ts @@ -32,10 +32,11 @@ export const fetchEnvironmentShellSnapshot = Effect.fn( }) { return yield* executeAuthenticatedEnvironmentHttpRequest({ ...input, + group: "orchestration", method: "GET", url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, "/api/orchestration/shell"), timeoutMs: input.timeoutMs ?? DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS, - request: ({ client, headers }) => client.orchestration.shellSnapshot({ headers }), + request: ({ client, headers }) => client.shellSnapshot({ headers }), }); }); diff --git a/packages/client-runtime/src/state/threadReducer.test.ts b/packages/client-runtime/src/state/threadReducer.test.ts index 5b70ca9ef0aa7..54e1eb8d1f653 100644 --- a/packages/client-runtime/src/state/threadReducer.test.ts +++ b/packages/client-runtime/src/state/threadReducer.test.ts @@ -526,6 +526,54 @@ describe("applyThreadDetailEvent", () => { }); describe("thread.message-sent", () => { + it.each([ + ["first", ["first+", "middle", "last"]], + ["middle", ["first", "middle+", "last"]], + ["last", ["first", "middle", "last+"]], + ["new", ["first", "middle", "last", "+"]], + ] as const)("applies a delta to %s without changing other messages", (id, texts) => { + const messages = Object.freeze( + ["first", "middle", "last"].map((name) => + Object.freeze({ + id: MessageId.make(name), + role: "assistant" as const, + text: name, + turnId: null, + streaming: false, + createdAt: baseThread.createdAt, + updatedAt: baseThread.updatedAt, + }), + ), + ); + const result = applyThreadDetailEvent( + { ...baseThread, messages }, + { + ...baseEventFields, + sequence: 6, + occurredAt: baseThread.updatedAt, + aggregateKind: "thread", + aggregateId: baseThread.id, + type: "thread.message-sent", + payload: { + threadId: baseThread.id, + messageId: MessageId.make(id), + role: "assistant", + text: "+", + turnId: null, + streaming: true, + createdAt: baseThread.createdAt, + updatedAt: baseThread.updatedAt, + }, + }, + ); + expect(result.kind).toBe("updated"); + if (result.kind !== "updated") return; + expect(result.thread.messages.map((message) => message.text)).toEqual(texts); + for (const [index, message] of messages.entries()) { + if (message.id !== id) expect(result.thread.messages[index]).toBe(message); + } + }); + it("appends a new message", () => { const result = applyThreadDetailEvent(baseThread, { ...baseEventFields, diff --git a/packages/client-runtime/src/state/threadReducer.ts b/packages/client-runtime/src/state/threadReducer.ts index ce9aa94b1f062..c55c1fe64dd82 100644 --- a/packages/client-runtime/src/state/threadReducer.ts +++ b/packages/client-runtime/src/state/threadReducer.ts @@ -386,27 +386,24 @@ export function applyThreadDetailEvent( updatedAt: event.payload.updatedAt, }; - const existingMessage = thread.messages.find((entry) => entry.id === message.id); - const messages = existingMessage - ? Arr.map(thread.messages, (entry) => - entry.id !== message.id - ? entry - : { - ...entry, - text: message.streaming - ? `${entry.text}${message.text}` - : message.text.length > 0 - ? message.text - : entry.text, - streaming: message.streaming, - ...(message.turnId !== undefined ? { turnId: message.turnId } : {}), - ...(message.streaming ? {} : { updatedAt: message.updatedAt }), - ...(message.attachments !== undefined - ? { attachments: message.attachments } - : {}), - }, - ) - : Arr.append(thread.messages, message); + let found = false; + const messages = thread.messages.map((entry) => { + if (entry.id !== message.id) return entry; + found = true; + return { + ...entry, + text: message.streaming + ? `${entry.text}${message.text}` + : message.text.length > 0 + ? message.text + : entry.text, + streaming: message.streaming, + ...(message.turnId !== undefined ? { turnId: message.turnId } : {}), + ...(message.streaming ? {} : { updatedAt: message.updatedAt }), + ...(message.attachments !== undefined ? { attachments: message.attachments } : {}), + }; + }); + if (!found) messages.push(message); // Update latestTurn for assistant messages bound to a turn. A completed // assistant message only settles the turn once the session is no longer // running it — providers may emit several assistant messages per turn diff --git a/packages/client-runtime/src/state/threadSnapshotHttp.ts b/packages/client-runtime/src/state/threadSnapshotHttp.ts index a82d4c1555c10..6af74067faed8 100644 --- a/packages/client-runtime/src/state/threadSnapshotHttp.ts +++ b/packages/client-runtime/src/state/threadSnapshotHttp.ts @@ -45,12 +45,13 @@ export const fetchEnvironmentThreadSnapshot = Effect.fn( }) { return yield* executeAuthenticatedEnvironmentHttpRequest({ ...input, + group: "orchestration", method: "GET", url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, `/api/orchestration/threads/${input.threadId}`), timeoutMs: input.timeoutMs ?? DEFAULT_THREAD_SNAPSHOT_TIMEOUT_MS, request: ({ client, headers }) => - client.orchestration.threadSnapshot({ + client.threadSnapshot({ params: { threadId: input.threadId }, payload: { ...(input.window !== undefined ? { turnLimit: input.window.turnLimit } : {}), From e1c94f703f05b43f2935d09ed1ef882d0ee13ff8 Mon Sep 17 00:00:00 2001 From: "t3-code[bot]" <269035359+t3-code[bot]@users.noreply.github.com> Date: Fri, 11 Sep 2026 13:21:27 -0700 Subject: [PATCH 26/63] fix(web): refresh usage limit countdowns without switching tabs (#11187) Co-authored-by: Exotic <118054752+extoci@users.noreply.github.com> --- apps/web/src/components/usage/UsageLimits.tsx | 8 +- .../usage/UsagePage.refresh.test.tsx | 179 ++++++++++++++++++ apps/web/src/components/usage/UsagePage.tsx | 5 +- 3 files changed, 187 insertions(+), 5 deletions(-) create mode 100644 apps/web/src/components/usage/UsagePage.refresh.test.tsx diff --git a/apps/web/src/components/usage/UsageLimits.tsx b/apps/web/src/components/usage/UsageLimits.tsx index e50547b660081..6d30d49a7e4a7 100644 --- a/apps/web/src/components/usage/UsageLimits.tsx +++ b/apps/web/src/components/usage/UsageLimits.tsx @@ -316,17 +316,17 @@ export function ResetCredits({ /** * Subscription quota across every connected environment's providers and hubs, - * pooled per provider. Countdowns anchor to render time rather than ticking: a - * live clock would repaint the page every minute for no decision-changing gain. + * pooled per provider. The page advances `now` on explicit refresh rather than + * ticking: a live clock would repaint the page for no decision-changing gain. */ export function UsageLimitsSection({ selectedEnvironmentIds, + now, }: { readonly selectedEnvironmentIds: ReadonlySet | null; + readonly now: number; }) { const presentations = useAtomValue(environmentPresentations.presentationsAtom); - // Anchored once per mount on purpose: countdowns must not tick (see above). - const [now] = useState(() => Date.now()); const selected = selectedEnvironmentIds === null ? presentations diff --git a/apps/web/src/components/usage/UsagePage.refresh.test.tsx b/apps/web/src/components/usage/UsagePage.refresh.test.tsx new file mode 100644 index 0000000000000..d6d300ce35b06 --- /dev/null +++ b/apps/web/src/components/usage/UsagePage.refresh.test.tsx @@ -0,0 +1,179 @@ +import { EnvironmentId, ProviderInstanceId, USAGE_CONTRACT_VERSION } from "@t3tools/contracts"; +import { mergeUsage } from "@t3tools/shared/usageMerge"; +import { act } from "react"; +import { create, type ReactTestRenderer } from "react-test-renderer"; +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; + +const state = vi.hoisted(() => ({ + presentations: new Map(), + refreshProviders: vi.fn(async () => undefined), +})); +vi.mock("@effect/atom-react", () => ({ useAtomValue: () => state.presentations })); +vi.mock("../../state/presentation", () => ({ + environmentPresentations: { presentationsAtom: null }, +})); +vi.mock("../../state/server", () => ({ serverEnvironment: { refreshProviders: null } })); +vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => state.refreshProviders })); +vi.mock("../../env", () => ({ isElectron: false })); +vi.mock("../../hooks/useSettings", () => ({ usePrimarySettings: () => "24h" })); +vi.mock("../../state/usage", () => ({ + useUsage: () => ({ + merged: mergeUsage([], USAGE_CONTRACT_VERSION), + environments: [ + { + environmentId: EnvironmentId.make("test"), + label: "Test", + isPending: false, + error: null, + summary: null, + }, + ], + selectedEnvironments: [ + { + environmentId: EnvironmentId.make("test"), + label: "Test", + isPending: false, + error: null, + summary: null, + }, + ], + isPending: false, + isPartial: false, + refresh: async () => undefined, + }), +})); +vi.mock("./usagePagePreferences", () => ({ + readUsagePagePreferences: () => ({ metric: "limits", windowDays: 30 }), + saveUsagePagePreferences: vi.fn(), +})); +vi.mock("../ui/button", () => ({ Button: "button" })); +vi.mock("../ui/scroll-area", () => ({ ScrollArea: "div" })); +vi.mock("../ui/select", () => ({ + Select: "select", + SelectItem: "option", + SelectPopup: "div", + SelectTrigger: "div", + SelectValue: "span", +})); +vi.mock("../ui/sidebar", () => ({ SidebarInset: "div" })); +vi.mock("../ui/toggle-group", () => ({ Toggle: "button", ToggleGroup: "div" })); +vi.mock("../ui/tooltip", () => ({ Tooltip: "div", TooltipPopup: "div", TooltipTrigger: "div" })); +vi.mock("../ui/popover", () => ({ Popover: "div", PopoverPopup: "div", PopoverTrigger: "div" })); +vi.mock("../ui/menu", () => ({ + Menu: "div", + MenuCheckboxItem: "div", + MenuItem: "div", + MenuPopup: "div", + MenuSeparator: "hr", + MenuTrigger: "div", +})); +vi.mock("../WorkspaceBreadcrumb", () => ({ + WorkspaceBreadcrumb: "div", + WorkspaceBreadcrumbItem: "div", + WorkspaceBreadcrumbSeparator: "span", +})); +vi.mock("../WorkspacePageContainer", () => ({ WorkspacePageContainer: "main" })); +vi.mock("../WorkspacePageHeader", () => ({ WorkspacePageHeader: "header" })); +vi.mock("./UsageProviderChart", () => ({ UsageProviderChart: "div" })); +vi.mock("./UsagePriceOverrides", () => ({ UsagePriceOverrides: () => null })); +vi.mock("../chat/ProviderInstanceIcon", () => ({ ProviderInstanceIcon: () => null })); +vi.mock("../settings/RedactedSensitiveText", () => ({ RedactedSensitiveText: "span" })); +vi.mock("../settings/providerDriverMeta", () => ({ getDriverOption: () => ({ label: "Codex" }) })); + +import { UsagePage } from "./UsagePage"; + +let renderer: ReactTestRenderer; +beforeEach(() => { + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + vi.spyOn(Date, "now").mockReturnValue(Date.parse("2026-09-11T12:00:00Z")); + state.refreshProviders.mockClear(); + state.presentations = new Map([ + [ + EnvironmentId.make("test"), + { + entry: { target: { label: "Test" } }, + connection: { phase: "connected" }, + serverConfig: { + providers: [ + { + instanceId: ProviderInstanceId.make("codex"), + driver: "codex", + enabled: true, + installed: true, + version: null, + status: "ready", + auth: { status: "authenticated" }, + checkedAt: "2026-09-11T12:00:00Z", + models: [], + slashCommands: [], + skills: [], + usageLimits: { + checkedAt: "2026-09-11T12:00:00Z", + windows: [ + { + id: "five_hour", + kind: "session", + label: "Session", + usedPercent: 40, + windowDurationMins: 300, + resetsAt: "2026-09-11T14:00:00Z", + }, + ], + }, + }, + ], + }, + }, + ], + ]); +}); +afterEach(async () => { + await act(() => renderer?.unmount()); + vi.restoreAllMocks(); + vi.unstubAllGlobals(); +}); + +it.each([0, 1])( + "refreshes the visible limits countdown with refresh button %i without switching tabs, even when quota is unchanged", + async (buttonIndex) => { + await act(() => { + renderer = create(); + }); + expect( + JSON.stringify(renderer.toJSON(), (key, value) => (key === "props" ? undefined : value)), + ).toContain("in 2h 0m"); + vi.mocked(Date.now).mockReturnValue(Date.parse("2026-09-11T12:30:00Z")); + await act(async () => { + renderer.root + .findAllByProps({ "aria-label": "Refresh limits" }) + .filter((node) => node.type === "button") + .at(buttonIndex)! + .props.onClick(); + }); + expect(state.refreshProviders).toHaveBeenCalledWith({ environmentId: "test", input: {} }); + expect( + JSON.stringify(renderer.toJSON(), (key, value) => (key === "props" ? undefined : value)), + ).toContain("in 1h 30m"); + expect( + JSON.stringify(renderer.toJSON(), (key, value) => (key === "props" ? undefined : value)), + ).not.toContain("in 2h 0m"); + }, +); + +it("uses the current time when returning to limits from tokens", async () => { + await act(() => { + renderer = create(); + }); + const selectMetric = (metric: string) => { + renderer.root + .findAll((node) => node.type === "div" && node.props["aria-label"] === "Usage metric")[0]! + .props.onValueChange([metric]); + }; + await act(() => selectMetric("tokens")); + vi.mocked(Date.now).mockReturnValue(Date.parse("2026-09-11T13:00:00Z")); + await act(() => selectMetric("limits")); + expect( + JSON.stringify(renderer.toJSON(), (key, value) => (key === "props" ? undefined : value)), + ).toContain("in 1h 0m"); + expect(state.refreshProviders).not.toHaveBeenCalled(); +}); diff --git a/apps/web/src/components/usage/UsagePage.tsx b/apps/web/src/components/usage/UsagePage.tsx index 21970c6755966..2da7414d9337e 100644 --- a/apps/web/src/components/usage/UsagePage.tsx +++ b/apps/web/src/components/usage/UsagePage.tsx @@ -104,6 +104,7 @@ export function UsagePage() { const metric = preferences.metric; const showingLimits = metric === "limits"; const [isRefreshing, setIsRefreshing] = useState(false); + const [limitsNow, setLimitsNow] = useState(() => Date.now()); const refreshingRef = useRef(false); const [breakdown, setBreakdown] = useState<"model" | "time">("model"); const [selectedEnvironmentIds, setSelectedEnvironmentIds] = @@ -159,6 +160,7 @@ export function UsagePage() { }); }; const selectMetric = (nextMetric: UsageMetric) => { + if (nextMetric === "limits") setLimitsNow(Date.now()); const nextPreferences = { metric: nextMetric, windowDays }; setPreferences(nextPreferences); saveUsagePagePreferences(nextPreferences); @@ -177,6 +179,7 @@ export function UsagePage() { } }), ).finally(() => { + setLimitsNow(Date.now()); refreshingRef.current = false; setIsRefreshing(false); }); @@ -350,7 +353,7 @@ export function UsagePage() { : `Select an environment to see ${showingLimits ? "limits" : "usage"}.`}

) : showingLimits ? ( - + ) : isPending ? ( ) : ( From fb52d125b78b2ed1638a719a7546fad9c695c620 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 11 Sep 2026 13:53:45 -0700 Subject: [PATCH 27/63] fix(client-runtime): typecheck device hub ticket request on main (#11304) Co-authored-by: Claude Fable 5 --- packages/client-runtime/src/state/deviceHubAccess.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/client-runtime/src/state/deviceHubAccess.ts b/packages/client-runtime/src/state/deviceHubAccess.ts index 328dea83ce7af..4c87aa538ac10 100644 --- a/packages/client-runtime/src/state/deviceHubAccess.ts +++ b/packages/client-runtime/src/state/deviceHubAccess.ts @@ -50,10 +50,11 @@ export const resolveDeviceHubAccess = Effect.fn("clientRuntime.state.resolveDevi prepared: input.prepared, signer, remoteAuthorization, + group: "auth", method: "POST", url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, "/api/auth/websocket-ticket"), timeoutMs: TICKET_TIMEOUT_MS, - request: ({ client, headers }) => client.auth.webSocketTicket({ headers }), + request: ({ client, headers }) => client.webSocketTicket({ headers }), }); return { httpBase, From 2c0e891740f86205784506a974096cf110b64da7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 11 Sep 2026 13:56:39 -0700 Subject: [PATCH 28/63] feat(settings): add per-project overrides for scopable server settings (#11176) --- .../src/environment/ServerEnvironment.ts | 1 + apps/server/src/git/GitManager.ts | 31 ++- .../Layers/ProjectionSnapshotQuery.test.ts | 1 + .../Layers/ProjectionSnapshotQuery.ts | 4 + .../Layers/ProviderCommandReactor.ts | 24 +- .../Layers/ProviderRuntimeIngestion.ts | 11 +- .../Services/ProjectionSnapshotQuery.ts | 2 +- .../ThreadSettlementReactor.test.ts | 82 +++++++ .../orchestration/ThreadSettlementReactor.ts | 61 ++++- .../provider/Layers/ProviderService.test.ts | 47 +++- .../src/provider/Layers/ProviderService.ts | 96 +++++--- apps/server/src/serverRuntimeStartup.test.ts | 70 ++++-- apps/server/src/serverRuntimeStartup.ts | 26 ++- apps/server/src/serverSettings.test.ts | 112 ++++++++++ apps/server/src/serverSettings.ts | 178 ++++++++++++--- apps/server/src/vcs/VcsStatusBroadcaster.ts | 4 +- .../src/state/sharedSettings.test.ts | 12 + packages/contracts/src/environment.ts | 2 + packages/contracts/src/settings.ts | 84 +++++++ packages/shared/package.json | 4 + packages/shared/src/projectScripts.ts | 38 +++- packages/shared/src/projectSettings.test.ts | 208 ++++++++++++++++++ packages/shared/src/projectSettings.ts | 124 +++++++++++ packages/shared/src/serverSettings.test.ts | 18 +- packages/shared/src/serverSettings.ts | 143 +++++++++--- 25 files changed, 1229 insertions(+), 154 deletions(-) create mode 100644 packages/shared/src/projectSettings.test.ts create mode 100644 packages/shared/src/projectSettings.ts diff --git a/apps/server/src/environment/ServerEnvironment.ts b/apps/server/src/environment/ServerEnvironment.ts index 2aab17b27a763..7bf081abc91d2 100644 --- a/apps/server/src/environment/ServerEnvironment.ts +++ b/apps/server/src/environment/ServerEnvironment.ts @@ -222,6 +222,7 @@ export const make = Effect.gen(function* () { threadSettlement: true, threadAutoSettlement: true, threadRestartContinuation: true, + projectSettingsOverrides: true, threadSnooze: true, environmentThemes: true, usageLimitSources: true, diff --git a/apps/server/src/git/GitManager.ts b/apps/server/src/git/GitManager.ts index f60eb2781872b..1a14fb5be5bc0 100644 --- a/apps/server/src/git/GitManager.ts +++ b/apps/server/src/git/GitManager.ts @@ -29,9 +29,16 @@ import { type VcsStatusRemoteResult, VcsStatusResult, ModelSelection, + type ProjectId, SourceControlProviderError, type SourceControlWritingStyleSettings, + type ThreadId, } from "@t3tools/contracts"; +import { + hasProjectSettingsOverrides, + resolveProjectSettings, +} from "@t3tools/shared/projectSettings"; +import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts"; import { detectSourceControlProviderFromGitRemoteUrl, mergeGitStatusParts, @@ -661,6 +668,28 @@ export const make = Effect.gen(function* () { const sourceControlProvider = (cwd: string) => sourceControlProviders.resolve({ cwd }); const serverSettingsService = yield* ServerSettings.ServerSettingsService; + // Optional: git actions also run from the CLI and tests without orchestration. + const projectionQuery = yield* Effect.serviceOption( + ProjectionSnapshotQuery.ProjectionSnapshotQuery, + ); + /** Environment settings with the acting project's overrides applied. */ + const projectSettingsFor = Effect.fnUntraced(function* (input: { + readonly cwd: string; + readonly threadId?: ThreadId | undefined; + }) { + const settings = yield* serverSettingsService.getSettings; + if (!hasProjectSettingsOverrides(settings) || Option.isNone(projectionQuery)) return settings; + const projectId = yield* ( + input.threadId !== undefined + ? projectionQuery.value + .getThreadShellById(input.threadId) + .pipe(Effect.map(Option.map((thread) => thread.projectId))) + : projectionQuery.value + .getActiveProjectByWorkspaceRoot(input.cwd) + .pipe(Effect.map(Option.map((project) => project.id))) + ).pipe(Effect.orElseSucceed(() => Option.none())); + return resolveProjectSettings(settings, Option.getOrNull(projectId)).settings; + }); const readRepositoryInstructions = (cwd: string, fileName: string) => Effect.gen(function* () { const root = yield* fileSystem.realPath(cwd); @@ -2600,7 +2629,7 @@ export const make = Effect.gen(function* () { let commitMessageForStep = input.commitMessage; let preResolvedCommitSuggestion: CommitAndBranchSuggestion | undefined = undefined; - const textGenerationSettings = yield* serverSettingsService.getSettings.pipe( + const textGenerationSettings = yield* projectSettingsFor(input).pipe( Effect.flatMap((settings) => settings.sourceControlWriterModelSelection === null ? Effect.succeed({ diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts index 5849123c55d67..be66f3cf4b43b 100644 --- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts +++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts @@ -682,6 +682,7 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => { if (context._tag === "Some") { assert.deepEqual(context.value, { id: ThreadId.make("thread-1"), + projectId: asProjectId("project-1"), title: "Thread 1", session: snapshot.threads[0]?.session, }); diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts index 066c60760ca52..efb7bba8f15b6 100644 --- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts +++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts @@ -142,6 +142,7 @@ const ProjectionThreadActivityIdRowSchema = Schema.Struct({ const ProjectionThreadSessionDbRowSchema = ProjectionThreadSession; const ProjectionThreadRuntimeContextDbRowSchema = Schema.Struct({ id: ThreadId, + projectId: ProjectId, title: Schema.String, session: Schema.NullOr(ProjectionThreadSessionDbRowSchema), }); @@ -1231,6 +1232,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { sql` SELECT threads.thread_id AS id, + threads.project_id AS "projectId", threads.title, sessions.thread_id AS "threadId", sessions.status, @@ -1251,6 +1253,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { Effect.map((rows) => rows.map((row) => ({ id: row.id, + projectId: row.projectId, title: row.title, session: row.threadId === null ? null : row, })), @@ -3164,6 +3167,7 @@ pending_approval_requests AS ( ); return Option.map(context, (row) => ({ id: row.id, + projectId: row.projectId, title: row.title, session: row.session === null ? null : mapSessionRow(row.session), })); diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts index 9b125922137cb..c5d120106a19c 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts @@ -54,6 +54,7 @@ import { resolveSourceControlWriterModelSelection, ServerSettingsService, } from "../../serverSettings.ts"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts"; import { GitWorkflowService } from "../../git/GitWorkflowService.ts"; const isProviderAdapterRequestError = Schema.is(ProviderAdapterRequestError); @@ -329,6 +330,16 @@ const make = Effect.gen(function* () { const vcsStatusBroadcaster = yield* VcsStatusBroadcaster; const textGeneration = yield* TextGeneration; const serverSettingsService = yield* ServerSettingsService; + /** Environment settings with the thread's project overrides applied. */ + const projectSettingsForThread = Effect.fnUntraced(function* (threadId: ThreadId) { + const settings = yield* serverSettingsService.getSettings; + if (Object.keys(settings.projectSettingsOverrides).length === 0) return settings; + const thread = yield* projectionSnapshotQuery + .getThreadShellById(threadId) + .pipe(Effect.orElseSucceed(() => Option.none())); + return resolveProjectSettings(settings, Option.isSome(thread) ? thread.value.projectId : null) + .settings; + }); const serverCommandId = (tag: string) => crypto.randomUUIDv4.pipe(Effect.map((uuid) => CommandId.make(`server:${tag}:${uuid}`))); const serverEventId = () => crypto.randomUUIDv4.pipe(Effect.map(EventId.make)); @@ -996,7 +1007,7 @@ const make = Effect.gen(function* () { const cwd = input.worktreePath; const attachments = input.attachments ?? []; yield* Effect.gen(function* () { - const settings = yield* serverSettingsService.getSettings; + const settings = yield* projectSettingsForThread(input.threadId); const modelSelection = settings.sourceControlWriterModelSelection === null ? settings.textGenerationModelSelection @@ -1047,8 +1058,9 @@ const make = Effect.gen(function* () { }) { const attachments = input.attachments ?? []; yield* Effect.gen(function* () { - const { textGenerationModelSelection: modelSelection } = - yield* serverSettingsService.getSettings; + const { textGenerationModelSelection: modelSelection } = yield* projectSettingsForThread( + input.threadId, + ); const generated = yield* textGeneration .generateThreadTitle({ @@ -1117,8 +1129,10 @@ const make = Effect.gen(function* () { thread, projects: project ? [project] : [], }) ?? process.cwd(); - const { textGenerationModelSelection: modelSelection } = - yield* serverSettingsService.getSettings; + const { textGenerationModelSelection: modelSelection } = resolveProjectSettings( + yield* serverSettingsService.getSettings, + thread.projectId, + ).settings; const generated = yield* textGeneration.generateThreadTitle({ cwd, message, diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts index 8d34fee4f9817..964f60d3a3064 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts @@ -51,6 +51,7 @@ import { import { projectActivityPayload } from "../ActivityPayloadProjection.ts"; import { forkParked } from "../../serverActivation.ts"; import { ServerSettingsService } from "../../serverSettings.ts"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import { canReplaceThreadTitle } from "../threadTitles.ts"; const providerTurnKey = (threadId: ThreadId, turnId: TurnId) => `${threadId}:${turnId}`; @@ -1668,7 +1669,10 @@ const make = Effect.gen(function* () { const assistantDeliveryMode: AssistantDeliveryMode = yield* Effect.map( serverSettingsService.getSettings, - (settings) => (settings.enableLegacyTokenStreaming ? "streaming" : "buffered"), + (settings) => + resolveProjectSettings(settings, thread.projectId).settings.enableLegacyTokenStreaming + ? "streaming" + : "buffered", ); if (assistantDeliveryMode === "buffered") { const spillChunk = yield* appendBufferedAssistantText(assistantMessageId, assistantDelta); @@ -1709,7 +1713,10 @@ const make = Effect.gen(function* () { }); const assistantDeliveryMode: AssistantDeliveryMode = yield* Effect.map( serverSettingsService.getSettings, - (settings) => (settings.enableLegacyTokenStreaming ? "streaming" : "buffered"), + (settings) => + resolveProjectSettings(settings, thread.projectId).settings.enableLegacyTokenStreaming + ? "streaming" + : "buffered", ); const flushedMessageIds = assistantDeliveryMode === "buffered" diff --git a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts index 35d5bacc239cd..fda0ac04556e1 100644 --- a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts +++ b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts @@ -209,7 +209,7 @@ export interface ProjectionSnapshotQueryShape { readonly getThreadRuntimeContext: ( threadId: ThreadId, ) => Effect.Effect< - Option.Option>, + Option.Option>, ProjectionRepositoryError >; diff --git a/apps/server/src/orchestration/ThreadSettlementReactor.test.ts b/apps/server/src/orchestration/ThreadSettlementReactor.test.ts index 0690eea2d50eb..c443ec75e0d55 100644 --- a/apps/server/src/orchestration/ThreadSettlementReactor.test.ts +++ b/apps/server/src/orchestration/ThreadSettlementReactor.test.ts @@ -302,6 +302,35 @@ const startHarness = Effect.fn("startThreadSettlementHarness")(function* ( }); describe("ThreadSettlementReactor", () => { + it("distinguishes a project that inherits the threshold from one that disables it", () => { + const inherits = ThreadSettlementReactor.autoSettlementSettingsKey({ + ...DEFAULT_SERVER_SETTINGS, + projectSettingsOverrides: { [PROJECT_ID]: { sidebarAutoSettleOnMerge: true } }, + }); + const never = ThreadSettlementReactor.autoSettlementSettingsKey({ + ...DEFAULT_SERVER_SETTINGS, + projectSettingsOverrides: { + [PROJECT_ID]: { sidebarAutoSettleOnMerge: true, sidebarAutoSettleAfterDays: null }, + }, + }); + assert.notStrictEqual(inherits, never); + }); + + it("ignores project overrides that do not touch settlement", () => { + const base = ThreadSettlementReactor.autoSettlementSettingsKey({ + ...DEFAULT_SERVER_SETTINGS, + projectSettingsOverrides: { [PROJECT_ID]: { sidebarAutoSettleOnMerge: false } }, + }); + const unrelated = ThreadSettlementReactor.autoSettlementSettingsKey({ + ...DEFAULT_SERVER_SETTINGS, + projectSettingsOverrides: { + [LINKED_PROJECT_ID]: { defaultThreadEnvMode: "worktree" }, + [PROJECT_ID]: { sidebarAutoSettleOnMerge: false, defaultAutoPull: true }, + }, + }); + assert.strictEqual(base, unrelated); + }); + it.effect( "settles all-terminal links from snapshots and keeps open or unsynced links active", () => @@ -486,6 +515,59 @@ describe("ThreadSettlementReactor", () => { ), ); + it.effect("a project override settles only that project's inactive threads", () => + Effect.scoped( + Effect.gen(function* () { + yield* TestClock.setTime(Date.parse(NOW)); + const overriddenProject = ProjectId.make("overridden-project"); + const fixture = yield* makeHarness({ + snapshot: makeSnapshot( + [ + makeThread("inherits-thread"), + makeThread("overridden-thread", { projectId: overriddenProject }), + ], + [makeProject(), makeProject(overriddenProject, "/workspace/overridden")], + ), + settings: { + ...DEFAULT_SERVER_SETTINGS, + sidebarAutoSettleAfterDays: null, + sidebarAutoSettleOnMerge: false, + projectSettingsOverrides: { + [overriddenProject]: { sidebarAutoSettleAfterDays: 1 }, + }, + }, + }); + + yield* Effect.gen(function* () { + const reactor = yield* ThreadSettlementReactor.ThreadSettlementReactor; + yield* reactor.start(); + yield* Queue.take(fixture.settingsReads); + yield* Deferred.succeed(fixture.activation, undefined); + yield* Queue.take(fixture.snapshotReads); + yield* reactor.drain; + assert.deepStrictEqual( + (yield* Ref.get(fixture.commands)).map((command) => command.threadId), + [ThreadId.make("overridden-thread")], + ); + + // Clearing the override is a settlement change, so the sweep re-arms. + yield* fixture.updateSettings({ + projectSettingsOverrides: { [overriddenProject]: null }, + sidebarAutoSettleAfterDays: 1, + }); + yield* Queue.take(fixture.snapshotReads); + yield* reactor.drain; + // The static snapshot never records the first settlement, so the + // second sweep dispatches for both; the inheriting thread is new. + assert.include( + (yield* Ref.get(fixture.commands)).map((command) => command.threadId), + ThreadId.make("inherits-thread"), + ); + }).pipe(Effect.provide(fixture.layer)); + }), + ), + ); + it.effect("starts without clients and skips protected threads before pull request lookup", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/orchestration/ThreadSettlementReactor.ts b/apps/server/src/orchestration/ThreadSettlementReactor.ts index 61fc5d4ab8635..b9041d2976cac 100644 --- a/apps/server/src/orchestration/ThreadSettlementReactor.ts +++ b/apps/server/src/orchestration/ThreadSettlementReactor.ts @@ -1,4 +1,5 @@ -import { CommandId } from "@t3tools/contracts"; +import { CommandId, type ServerSettings as ServerSettingsValue } from "@t3tools/contracts"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker"; import * as Cause from "effect/Cause"; import * as Context from "effect/Context"; @@ -32,6 +33,45 @@ export class ThreadSettlementReactor extends Context.Service< } >()("t3/orchestration/ThreadSettlementReactor") {} +/** @public Service construction is part of the canonical Effect module API. */ +/** Whether any environment default or project override can settle a thread. */ +function autoSettlementConfigured(settings: ServerSettingsValue): boolean { + if (settings.sidebarAutoSettleOnMerge || settings.sidebarAutoSettleAfterDays !== null) { + return true; + } + return Object.values(settings.projectSettingsOverrides).some( + (entry) => + entry.sidebarAutoSettleOnMerge === true || + (entry.sidebarAutoSettleAfterDays !== undefined && entry.sidebarAutoSettleAfterDays !== null), + ); +} + +/** Identity of every settlement input, so unrelated settings edits do not trigger a sweep. */ +/** @internal Exported for tests. */ +export function autoSettlementSettingsKey(settings: ServerSettingsValue): string { + return JSON.stringify([ + settings.sidebarAutoSettleOnMerge, + settings.sidebarAutoSettleAfterDays, + // Only entries that touch settlement, in a stable order, so a project + // override on an unrelated key does not queue a sweep. JSON drops + // undefined, so inherit (absent) and never (null) need distinct marks. + Object.entries(settings.projectSettingsOverrides) + .filter( + ([, entry]) => + entry.sidebarAutoSettleOnMerge !== undefined || + entry.sidebarAutoSettleAfterDays !== undefined, + ) + .sort(([left], [right]) => left.localeCompare(right)) + .map(([projectId, entry]) => [ + projectId, + entry.sidebarAutoSettleOnMerge ?? "inherit", + entry.sidebarAutoSettleAfterDays === undefined + ? "inherit" + : entry.sidebarAutoSettleAfterDays, + ]), + ]); +} + /** @public Service construction is part of the canonical Effect module API. */ export const make = Effect.gen(function* () { const engine = yield* OrchestrationEngine.OrchestrationEngineService; @@ -46,7 +86,7 @@ export const make = Effect.gen(function* () { mergedPullRequest: PullRequestService.PullRequestMergeEvent | null, ) { const settings = yield* settingsService.getSettings; - if (!settings.sidebarAutoSettleOnMerge && settings.sidebarAutoSettleAfterDays === null) { + if (!autoSettlementConfigured(settings)) { return; } const snapshot = yield* snapshots.getShellSnapshot(); @@ -60,7 +100,10 @@ export const make = Effect.gen(function* () { // dispatch skips it for this snapshot instead of retrying through a lookup. const settleThread = Effect.fn("ThreadSettlementReactor.settleThread")( function* (thread: (typeof candidates)[number], pullRequest: SettlementPullRequest | null) { - const settings = yield* settingsService.getSettings; + const settings = resolveProjectSettings( + yield* settingsService.getSettings, + thread.projectId, + ).settings; const decisionNow = DateTime.formatIso(yield* DateTime.now); const settledAt = resolveAutoSettlementAt({ thread, @@ -254,8 +297,7 @@ export const make = Effect.gen(function* () { const settingsChanges = yield* settingsService.subscribeChanges; const mergedPullRequests = yield* pullRequests.subscribeMerges; const initialSettings = yield* settingsService.getSettings.pipe(Effect.orDie); - let lastAfterDays = initialSettings.sidebarAutoSettleAfterDays; - let lastOnMerge = initialSettings.sidebarAutoSettleOnMerge; + let lastSettlementSettings = autoSettlementSettingsKey(initialSettings); yield* forkParked( Effect.gen(function* () { yield* worker.enqueue(undefined); @@ -264,14 +306,11 @@ export const make = Effect.gen(function* () { ); yield* forkParked( Stream.runForEach(settingsChanges, (settings) => { - if ( - settings.sidebarAutoSettleAfterDays === lastAfterDays && - settings.sidebarAutoSettleOnMerge === lastOnMerge - ) { + const key = autoSettlementSettingsKey(settings); + if (key === lastSettlementSettings) { return Effect.void; } - lastAfterDays = settings.sidebarAutoSettleAfterDays; - lastOnMerge = settings.sidebarAutoSettleOnMerge; + lastSettlementSettings = key; return worker.enqueue(undefined); }), ); diff --git a/apps/server/src/provider/Layers/ProviderService.test.ts b/apps/server/src/provider/Layers/ProviderService.test.ts index 9a3fcb8d65f41..17f49355a904c 100644 --- a/apps/server/src/provider/Layers/ProviderService.test.ts +++ b/apps/server/src/provider/Layers/ProviderService.test.ts @@ -4802,7 +4802,8 @@ describe("agent browser access", () => { const startSessionWith = ( access: boolean | { readonly browser: boolean; readonly device: boolean }, threadId: ThreadId, - projectOverride?: boolean, + projectOverride?: boolean | { readonly browser?: boolean; readonly device?: boolean }, + options?: { readonly withoutOrchestration?: boolean }, ) => Effect.gen(function* () { const enableAgentBrowserAccess = typeof access === "boolean" ? access : access.browser; @@ -4875,13 +4876,26 @@ describe("agent browser access", () => { }).pipe( Layer.provide(providerAdapterLayer), Layer.provide(directoryLayer), - Layer.provide(projectionLayer), + Layer.provide(options?.withoutOrchestration ? Layer.empty : projectionLayer), Layer.provide( ServerSettings.ServerSettingsService.layerTest({ enableAgentBrowserAccess, enableAgentDeviceAccess, - projectAgentBrowserAccessOverrides: - projectOverride === undefined ? {} : { [projectId]: projectOverride }, + projectSettingsOverrides: + projectOverride === undefined + ? {} + : typeof projectOverride === "boolean" + ? { [projectId]: { enableAgentBrowserAccess: projectOverride } } + : { + [projectId]: { + ...(projectOverride.browser !== undefined + ? { enableAgentBrowserAccess: projectOverride.browser } + : {}), + ...(projectOverride.device !== undefined + ? { enableAgentDeviceAccess: projectOverride.device } + : {}), + }, + }, }), ), Layer.provide(serverConfigTestLayer), @@ -4965,4 +4979,29 @@ describe("agent browser access", () => { assert.deepEqual(issued, [{ threadId, capabilities: ["preview", "pull-requests"] }]); }).pipe(Effect.provide(NodeServices.layer)), ); + + it.effect("a project device override grants device access when the environment denies it", () => + Effect.gen(function* () { + const threadId = asThreadId("thread-project-device-on"); + const issued = yield* startSessionWith({ browser: false, device: false }, threadId, { + device: true, + }); + assert.deepEqual(issued, [{ threadId, capabilities: ["device", "pull-requests"] }]); + }).pipe(Effect.provide(NodeServices.layer)), + ); + + // Without orchestration the project cannot be resolved, so an overridden + // capability is withheld; one no project overrides keeps its environment value. + it.effect("withholds only the overridden capability when the project cannot be resolved", () => + Effect.gen(function* () { + const threadId = asThreadId("thread-no-orchestration-device-override"); + const issued = yield* startSessionWith( + { browser: true, device: true }, + threadId, + { device: false }, + { withoutOrchestration: true }, + ); + assert.deepEqual(issued, [{ threadId, capabilities: ["preview", "pull-requests"] }]); + }).pipe(Effect.provide(NodeServices.layer)), + ); }); diff --git a/apps/server/src/provider/Layers/ProviderService.ts b/apps/server/src/provider/Layers/ProviderService.ts index d04dcae7f1266..7ffbb113d549b 100644 --- a/apps/server/src/provider/Layers/ProviderService.ts +++ b/apps/server/src/provider/Layers/ProviderService.ts @@ -28,16 +28,18 @@ import { ProviderUploadFeedbackInput, ThreadId, TurnId, + type ProjectId, type ProviderInstanceId, type ProviderDriverKind, type ProviderRuntimeEvent, type ProviderSession, + type ServerSettings as ServerSettingsValue, } from "@t3tools/contracts"; import { expandAssistantCitationsForProvider } from "@t3tools/shared/assistantCitations"; import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; import { causeErrorTag } from "@t3tools/shared/observability"; import { getModelSelectionStringOptionValue } from "@t3tools/shared/model"; -import { resolveProjectAgentBrowserAccess } from "@t3tools/shared/serverSettings"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import * as DateTime from "effect/DateTime"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; @@ -864,34 +866,40 @@ const makeProviderService = Effect.fn("makeProviderService")(function* ( * "off" silently becoming "on" would violate the user's stated choice, * whereas the reverse costs an agent one toolset and is visible immediately. */ - const agentBrowserAccessEnabled = Effect.fn("ProviderService.agentBrowserAccessEnabled")( + const agentAccessSettings = Effect.fn("ProviderService.agentAccessSettings")( function* (threadId: ThreadId) { const settings = yield* serverSettings.getSettings; - if (Object.keys(settings.projectAgentBrowserAccessOverrides).length === 0) { - return settings.enableAgentBrowserAccess; - } + const entries = Object.values(settings.projectSettingsOverrides); + const browserOverridden = entries.some( + (entry) => entry.enableAgentBrowserAccess !== undefined, + ); + const deviceOverridden = entries.some((entry) => entry.enableAgentDeviceAccess !== undefined); + const environment = { + browser: settings.enableAgentBrowserAccess, + device: settings.enableAgentDeviceAccess, + }; + if (!browserOverridden && !deviceOverridden) return environment; // Provider-only runtimes may omit orchestration. An unresolved project - // must not bypass an explicit browser override. - if (Option.isNone(projectionQuery)) return false; + // must not bypass an explicit project override, but a capability no + // project overrides keeps its environment value. + const denied = { + browser: browserOverridden ? false : environment.browser, + device: deviceOverridden ? false : environment.device, + }; + if (Option.isNone(projectionQuery)) return denied; const thread = yield* projectionQuery.value.getThreadShellById(threadId); - if (Option.isNone(thread)) return false; - return resolveProjectAgentBrowserAccess(settings, thread.value.projectId); + if (Option.isNone(thread)) return denied; + const resolved = resolveProjectSettings(settings, thread.value.projectId).settings; + return { + browser: resolved.enableAgentBrowserAccess, + device: resolved.enableAgentDeviceAccess, + }; }, Effect.catch((cause) => Effect.logWarning( - "Could not read server settings; withholding agent browser access for this session.", + "Could not read server settings; withholding agent browser and device access for this session.", { cause }, - ).pipe(Effect.as(false)), - ), - ); - - const agentDeviceAccessEnabled = serverSettings.getSettings.pipe( - Effect.map((settings) => settings.enableAgentDeviceAccess), - Effect.catch((cause) => - Effect.logWarning( - "Could not read server settings; withholding agent device access for this session.", - { cause }, - ).pipe(Effect.as(false)), + ).pipe(Effect.as({ browser: false, device: false })), ), ); @@ -899,8 +907,9 @@ const makeProviderService = Effect.fn("makeProviderService")(function* ( threadId: ThreadId, ) { const capabilities = new Set(["pull-requests"]); - if (yield* agentBrowserAccessEnabled(threadId)) capabilities.add("preview"); - if (yield* agentDeviceAccessEnabled) capabilities.add("device"); + const access = yield* agentAccessSettings(threadId); + if (access.browser) capabilities.add("preview"); + if (access.device) capabilities.add("device"); return capabilities; }); @@ -2224,10 +2233,30 @@ const makeProviderService = Effect.fn("makeProviderService")(function* ( ); const runStopAll = Effect.fn("runStopAll")(function* () { - const continueAfterRestart = yield* serverSettings.getSettings.pipe( - Effect.map((settings) => settings.continueThreadsAfterServerUpdate), - Effect.orElseSucceed(() => false), + // Continuation is project-scopable, so decide it per session's project; + // without orchestration the environment value is all there is. + const stopSettings = yield* serverSettings.getSettings.pipe( + Effect.map(Option.some), + Effect.orElseSucceed(() => Option.none()), ); + const continueAfterRestartFor = Effect.fn("continueAfterRestartFor")(function* ( + threadId: ThreadId, + ) { + if (Option.isNone(stopSettings)) return false; + const settings = stopSettings.value; + const overridden = Object.values(settings.projectSettingsOverrides).some( + (entry) => entry.continueThreadsAfterServerUpdate !== undefined, + ); + if (!overridden || Option.isNone(projectionQuery)) { + return settings.continueThreadsAfterServerUpdate; + } + const thread = yield* projectionQuery.value + .getThreadShellById(threadId) + .pipe(Effect.orElseSucceed(() => Option.none<{ projectId: ProjectId }>())); + if (Option.isNone(thread)) return settings.continueThreadsAfterServerUpdate; + return resolveProjectSettings(settings, thread.value.projectId).settings + .continueThreadsAfterServerUpdate; + }); const properties = yield* Ref.modify(turnAnalytics, (state) => { const completed: Array>> = []; for (const [sessionKey, session] of state.sessions) { @@ -2253,15 +2282,20 @@ const makeProviderService = Effect.fn("makeProviderService")(function* ( ), ).pipe(Effect.map((sessionsByAdapter) => sessionsByAdapter.flatMap((sessions) => sessions))); yield* Effect.forEach(activeSessions, (session) => - Effect.flatMap(nowIso, (lastRuntimeEventAt) => - upsertSessionBinding(session, session.threadId, { - ...(continueAfterRestart && session.status === "running" && session.activeTurnId + Effect.gen(function* () { + const continueAfterRestart = + session.status === "running" && session.activeTurnId + ? yield* continueAfterRestartFor(session.threadId) + : false; + const lastRuntimeEventAt = yield* nowIso; + yield* upsertSessionBinding(session, session.threadId, { + ...(continueAfterRestart && session.activeTurnId ? { continueAfterServerUpdate: session.activeTurnId } : {}), lastRuntimeEvent: "provider.stopAll", lastRuntimeEventAt, - }), - ), + }); + }), ).pipe(Effect.asVoid); yield* Effect.forEach(currentAdapters, ([, adapter]) => adapter.stopAll()).pipe(Effect.asVoid); yield* McpSessionRegistry.revokeAllActiveMcpCredentials(); diff --git a/apps/server/src/serverRuntimeStartup.test.ts b/apps/server/src/serverRuntimeStartup.test.ts index 0426df44bceac..37dbd7394a00b 100644 --- a/apps/server/src/serverRuntimeStartup.test.ts +++ b/apps/server/src/serverRuntimeStartup.test.ts @@ -1,5 +1,11 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; -import { DEFAULT_MODEL, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"; +import { + DEFAULT_MODEL, + DEFAULT_SERVER_SETTINGS, + ProjectId, + ProviderInstanceId, + ThreadId, +} from "@t3tools/contracts"; import { assert, it } from "@effect/vitest"; import * as Crypto from "effect/Crypto"; import * as Deferred from "effect/Deferred"; @@ -40,27 +46,43 @@ it.effect("automatic pull only updates enabled, behind, clean default-branch che }; }), } as unknown as GitVcsDriver.GitVcsDriver["Service"]; - const project = (workspaceRoot: string, autoPull = true) => - ({ id: ProjectId.make(workspaceRoot), workspaceRoot, autoPull }) as never; - - yield* ServerRuntimeStartup.autoPullProjects([ - project("/clean"), - project("/current"), - project("/dirty"), - project("/ahead"), - project("/feature"), - project("/disabled", false), - ]).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, git)); + const project = (workspaceRoot: string) => + ({ id: ProjectId.make(workspaceRoot), workspaceRoot }) as never; + const overrides = (entries: Record) => ({ + ...DEFAULT_SERVER_SETTINGS, + projectSettingsOverrides: Object.fromEntries( + Object.entries(entries).map(([root, defaultAutoPull]) => [ + ProjectId.make(root), + { defaultAutoPull }, + ]), + ), + }); + + yield* ServerRuntimeStartup.autoPullProjects( + [ + project("/clean"), + project("/current"), + project("/dirty"), + project("/ahead"), + project("/feature"), + project("/disabled"), + ], + overrides({ + "/clean": true, + "/current": true, + "/dirty": true, + "/ahead": true, + "/feature": true, + "/disabled": false, + }), + ).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, git)); assert.deepStrictEqual(pulled, ["/clean"]); pulled.length = 0; yield* ServerRuntimeStartup.autoPullProjects( - [project("/inherited", false), project("/opted-out"), project("/dirty", false)], - { - defaultAutoPull: true, - projectAutoPullOverrides: { [ProjectId.make("/opted-out")]: false }, - }, + [project("/inherited"), project("/opted-out"), project("/dirty")], + { ...overrides({ "/opted-out": false }), defaultAutoPull: true }, ).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, git)); assert.deepStrictEqual(pulled, ["/inherited"]); }), @@ -223,7 +245,17 @@ it.effect.each([ }> >([]); const targets = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe( - Effect.provide(ServerSettings.layerTest({ defaultModelSelection: machineSelection })), + Effect.provide( + ServerSettings.layerTest({ + defaultModelSelection: machineSelection, + projectSettingsOverrides: + existing && projectSelection + ? { + [ProjectId.make("existing-project")]: { defaultModelSelection: projectSelection }, + } + : {}, + }), + ), Effect.provideService(ServerConfig.ServerConfig, { cwd: "/tmp/startup-project", autoBootstrapProjectFromCwd: true, @@ -244,7 +276,7 @@ it.effect.each([ id: ProjectId.make("existing-project"), title: "Startup Project", workspaceRoot: "/tmp/startup-project", - defaultModelSelection: projectSelection, + defaultModelSelection: null, scripts: [], createdAt: "2026-01-01T00:00:00.000Z", updatedAt: "2026-01-01T00:00:00.000Z", diff --git a/apps/server/src/serverRuntimeStartup.ts b/apps/server/src/serverRuntimeStartup.ts index 3d04abaa1914d..90d6c3c576a07 100644 --- a/apps/server/src/serverRuntimeStartup.ts +++ b/apps/server/src/serverRuntimeStartup.ts @@ -3,6 +3,7 @@ import { DEFAULT_MODEL, DEFAULT_PROVIDER_INTERACTION_MODE, DEFAULT_SERVER_SETTINGS, + type ServerSettings as ServerSettingsValue, type ModelSelection, type OrchestrationProjectShell, ProjectId, @@ -10,7 +11,7 @@ import { ThreadId, TurnId, } from "@t3tools/contracts"; -import { resolveProjectAutoPull } from "@t3tools/shared/serverSettings"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import * as Cause from "effect/Cause"; import * as Console from "effect/Console"; import * as Context from "effect/Context"; @@ -229,7 +230,8 @@ export const resolveAutoBootstrapWelcomeTargets = Effect.gen(function* () { nextProjectId = existingProject.value.id; bootstrapProjectId = nextProjectId; nextThreadModelSelection = - existingProject.value.defaultModelSelection ?? defaultModelSelection; + resolveProjectSettings(settings, nextProjectId, existingProject.value).settings + .defaultModelSelection ?? defaultModelSelection; } yield* Effect.gen(function* () { @@ -479,14 +481,19 @@ export const reconcileProviderSessions = Effect.gen(function* () { const providerService = yield* ProviderService.ProviderService; const query = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery; const settings = yield* ServerSettings.ServerSettingsService; - const continueAfterRestart = yield* settings.getSettings.pipe( - Effect.map((value) => value.continueThreadsAfterServerUpdate), + const restartSettings = yield* settings.getSettings.pipe( + Effect.map(Option.some), Effect.catch((cause) => Effect.logWarning("could not read restart continuation preference", { cause }).pipe( - Effect.as(false), + Effect.as(Option.none()), ), ), ); + const continueAfterRestartFor = (projectId: ProjectId) => + Option.isSome(restartSettings) + ? resolveProjectSettings(restartSettings.value, projectId).settings + .continueThreadsAfterServerUpdate + : false; const liveThreadIds = new Set( (yield* providerService.listSessions()).map((session) => session.threadId), @@ -568,7 +575,7 @@ export const reconcileProviderSessions = Effect.gen(function* () { // Runtime events advance the projection's turn, but not the directory's // last admitted turn. Use the projection to identify interrupted work. const interruptedByRestart = - continueAfterRestart && + continueAfterRestartFor(thread.projectId) && session.status === "running" && session.activeTurnId !== null && Option.isSome(binding) && @@ -742,16 +749,13 @@ interface StartupOptions { export const autoPullProjects = Effect.fn("autoPullProjects")(function* ( projects: ReadonlyArray, - settings: Pick< - typeof DEFAULT_SERVER_SETTINGS, - "defaultAutoPull" | "projectAutoPullOverrides" - > = DEFAULT_SERVER_SETTINGS, + settings: ServerSettingsValue = DEFAULT_SERVER_SETTINGS, ) { const git = yield* GitVcsDriver.GitVcsDriver; const workspaceRoots = [ ...new Set( projects - .filter((project) => resolveProjectAutoPull(settings, project.id, project.autoPull)) + .filter((project) => resolveProjectSettings(settings, project.id).settings.defaultAutoPull) .map((project) => project.workspaceRoot), ), ]; diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts index 5d2571e72dc3a..208d75fb65179 100644 --- a/apps/server/src/serverSettings.test.ts +++ b/apps/server/src/serverSettings.test.ts @@ -1,6 +1,9 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { DEFAULT_SERVER_SETTINGS, + ModelSelection, + ProjectId, + ProjectScript, ProviderDriverKind, ProviderInstanceId, resolveProviderInstanceEnabled, @@ -1279,4 +1282,113 @@ it.layer(NodeServices.layer)("server settings", (it) => { assert.include(persisted, '"valueRedacted": true'); }).pipe(Effect.provide(makeServerSettingsLayer())), ); + + it.effect("folds legacy project overrides into projectSettingsOverrides once", () => + Effect.gen(function* () { + const serverConfig = yield* ServerConfig.ServerConfig; + const fileSystem = yield* FileSystem.FileSystem; + const sql = yield* SqlClient.SqlClient; + const serverSettings = yield* ServerSettingsModule.ServerSettingsService; + const legacyProject = ProjectId.make("project-legacy"); + const scriptedProject = ProjectId.make("project-scripted"); + const script: ProjectScript = { + id: "check", + name: "Check", + command: "npm test", + icon: "play", + runOnWorktreeCreate: false, + }; + const model = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.5"); + const modelJson = yield* Schema.encodeEffect(Schema.fromJsonString(ModelSelection))(model); + const scriptsJson = yield* Schema.encodeEffect( + Schema.fromJsonString(Schema.Array(ProjectScript)), + )([script]); + for (const [projectId, modelColumn, envMode, autoPull, scripts] of [ + // The legacy project also carries aggregate scripts, but its stored + // null override reset them; the fold must not bring them back. + [legacyProject, modelJson, "worktree", 1, scriptsJson], + [scriptedProject, null, null, 0, scriptsJson], + ] as const) { + yield* sql` + INSERT INTO projection_projects ( + project_id, title, workspace_root, default_model_selection_json, + default_thread_env_mode, auto_pull, scripts_json, created_at, updated_at + ) + VALUES ( + ${projectId}, ${"Project"}, ${`/tmp/${projectId}`}, ${modelColumn}, + ${envMode}, ${autoPull}, ${scripts}, + ${"2026-08-25T00:00:00.000Z"}, ${"2026-08-25T00:00:00.000Z"} + ) + `; + } + yield* fileSystem.writeFileString( + serverConfig.settingsPath, + `{"projectAgentBrowserAccessOverrides":{"${legacyProject}":false},"projectAutoPullOverrides":{"${scriptedProject}":true},"projectScriptOverrides":{"${legacyProject}":null}}`, + ); + + const settings = yield* serverSettings.getSettings; + assert.isTrue(settings.projectSettingsFolded); + assert.deepEqual( + settings.projectSettingsOverrides, + { + [legacyProject]: { + enableAgentBrowserAccess: false, + defaultModelSelection: model, + defaultThreadEnvMode: "worktree", + defaultAutoPull: true, + }, + [scriptedProject]: { defaultAutoPull: true, defaultProjectScripts: [script] }, + }, + ); + // Derived legacy views keep older clients reading the same values. + assert.deepEqual( + settings.projectAutoPullOverrides, + { + [legacyProject]: true, + [scriptedProject]: true, + }, + ); + assert.deepEqual(settings.projectScriptOverrides, { + [scriptedProject]: [script], + }); + + // A reset survives the next load: the fold does not run again. + yield* serverSettings.updateSettings({ + projectSettingsOverrides: { [legacyProject]: null }, + }); + const raw = yield* fileSystem.readFileString(serverConfig.settingsPath); + const persisted = yield* decodeServerSettings( + // @effect-diagnostics-next-line preferSchemaOverJson:off + JSON.parse(raw), + ); + assert.isTrue(persisted.projectSettingsFolded); + assert.isUndefined(persisted.projectSettingsOverrides[legacyProject]); + }).pipe(Effect.provide(makeServerSettingsLayer())), + ); + + it.effect("leaves an unreadable settings.json untouched instead of folding over it", () => + Effect.gen(function* () { + const serverConfig = yield* ServerConfig.ServerConfig; + const fileSystem = yield* FileSystem.FileSystem; + const sql = yield* SqlClient.SqlClient; + const serverSettings = yield* ServerSettingsModule.ServerSettingsService; + yield* sql` + INSERT INTO projection_projects ( + project_id, title, workspace_root, auto_pull, scripts_json, created_at, updated_at + ) + VALUES ( + ${"project-broken"}, ${"Project"}, ${"/tmp/project-broken"}, ${1}, ${"[]"}, + ${"2026-08-25T00:00:00.000Z"}, ${"2026-08-25T00:00:00.000Z"} + ) + `; + const broken = '{"defaultAutoPull": tru'; + yield* fileSystem.writeFileString(serverConfig.settingsPath, broken); + + const settings = yield* serverSettings.getSettings; + assert.isFalse(settings.projectSettingsFolded); + assert.deepEqual(settings.projectSettingsOverrides, {}); + // The user's file is still there to repair; nothing was written over it. + assert.equal(yield* fileSystem.readFileString(serverConfig.settingsPath), broken); + }).pipe(Effect.provide(makeServerSettingsLayer())), + ); }); diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts index 0b64d445adf83..50f8649eaacb0 100644 --- a/apps/server/src/serverSettings.ts +++ b/apps/server/src/serverSettings.ts @@ -15,7 +15,9 @@ import { DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER, DEFAULT_MODEL_BY_PROVIDER, DEFAULT_SERVER_SETTINGS, - type ModelSelection, + ModelSelection, + ProjectScript, + type ProjectSettingsOverrides, type ProviderInstanceConfig, type ProviderInstanceEnvironmentVariable, type UsageLimitSourceConfig, @@ -51,6 +53,7 @@ import { type DeepPartial, deepMerge } from "@t3tools/shared/Struct"; import { fromJsonStringPretty, fromLenientJson } from "@t3tools/shared/schemaJson"; import { applyServerSettingsPatch, + deriveLegacyProjectOverrides, isModelSelectionProviderEnabled, } from "@t3tools/shared/serverSettings"; import * as ServerSecretStore from "./auth/ServerSecretStore.ts"; @@ -118,6 +121,7 @@ const normalizeServerSettings = ( encodeServerSettings(settings).pipe( Effect.flatMap(decodeServerSettings), Effect.map(foldProviderInstanceEnabledFlags), + Effect.map((next) => ({ ...next, ...deriveLegacyProjectOverrides(next) })), Effect.mapError( (cause) => new ServerSettingsError({ @@ -353,6 +357,7 @@ const ATOMIC_SETTINGS_KEYS: ReadonlySet = new Set([ "providerHealthRefreshInterval", "sourceControlWriterModelSelection", "textGenerationModelSelection", + "pullRequestMergeMethod", ]); // Preserve both enabled states because provider history cannot recover a new opt-in. @@ -400,6 +405,93 @@ function stripDefaultServerSettings(current: unknown, defaults: unknown): unknow return Object.is(current, defaults) ? undefined : current; } +const decodeProjectScriptsJson = Schema.decodeUnknownOption( + Schema.fromJsonString(Schema.Array(ProjectScript)), +); +const decodeModelSelectionJson = Schema.decodeUnknownOption( + Schema.fromJsonString(Schema.NullOr(ModelSelection)), +); + +interface LegacyProjectSettingsRow { + readonly projectId: string; + readonly defaultModelSelection: string | null; + readonly defaultThreadEnvMode: string | null; + readonly autoPull: number; + readonly scripts: string; +} + +/** + * One-time fold of the legacy per-project fields into `projectSettingsOverrides`: + * the three `project*Overrides` maps and the settings columns on the project + * aggregate. Keys already present in the generic record win. Marked with + * `projectSettingsFolded` so a later reset in the UI survives restarts. + */ +function foldLegacyProjectSettings( + settings: ServerSettings, + rows: ReadonlyArray, +): ServerSettings { + if (settings.projectSettingsFolded) return settings; + // Nothing to fold yet (fresh install): leave the marker off so the file + // stays sparse, and check again on the next load. + if ( + rows.length === 0 && + Object.keys(settings.projectAgentBrowserAccessOverrides).length === 0 && + Object.keys(settings.projectAutoPullOverrides).length === 0 && + Object.keys(settings.projectScriptOverrides).length === 0 + ) { + return settings; + } + const entries: Record = { + ...settings.projectSettingsOverrides, + }; + const set = ( + projectId: string, + key: K, + value: ProjectSettingsOverrides[K] | undefined, + ) => { + if (value === undefined) return; + const entry = entries[projectId] ?? {}; + if (Object.hasOwn(entry, key)) return; + entries[projectId] = { ...entry, [key]: value }; + }; + for (const [projectId, value] of Object.entries(settings.projectAgentBrowserAccessOverrides)) { + set(projectId, "enableAgentBrowserAccess", value); + } + for (const [projectId, value] of Object.entries(settings.projectAutoPullOverrides)) { + set(projectId, "defaultAutoPull", value); + } + // A stored null meant "reset to machine defaults", which is now plain + // inheritance; the project's own aggregate scripts must not resurface. + const resetScripts = new Set(); + for (const [projectId, value] of Object.entries(settings.projectScriptOverrides)) { + if (value === null) resetScripts.add(projectId); + else set(projectId, "defaultProjectScripts", value); + } + for (const row of rows) { + const model = decodeModelSelectionJson(row.defaultModelSelection ?? "null"); + if (Option.isSome(model) && model.value !== null) { + set(row.projectId, "defaultModelSelection", model.value); + } + if (row.defaultThreadEnvMode === "local" || row.defaultThreadEnvMode === "worktree") { + set(row.projectId, "defaultThreadEnvMode", row.defaultThreadEnvMode); + } + if (row.autoPull === 1) set(row.projectId, "defaultAutoPull", true); + const scripts = decodeProjectScriptsJson(row.scripts); + if (Option.isSome(scripts) && scripts.value.length > 0 && !resetScripts.has(row.projectId)) { + set(row.projectId, "defaultProjectScripts", scripts.value); + } + } + const projectSettingsOverrides = Object.fromEntries( + Object.entries(entries).filter(([, entry]) => Object.keys(entry).length > 0), + ); + return { + ...settings, + projectSettingsOverrides, + projectSettingsFolded: true, + ...deriveLegacyProjectOverrides({ projectSettingsOverrides }), + }; +} + const make = Effect.gen(function* () { const { settingsPath } = yield* ServerConfig.ServerConfig; const fs = yield* FileSystem.FileSystem; @@ -439,9 +531,36 @@ const make = Effect.gen(function* () { ), ); + const writeSettingsAtomically = Effect.fnUntraced( + function* (settings: ServerSettings) { + const sparseSettingsJson = yield* encodeServerSettingsJson( + stripDefaultServerSettings(settings, PERSISTED_SERVER_SETTINGS_DEFAULTS) ?? {}, + ); + + return yield* writeFileStringAtomically({ + filePath: settingsPath, + contents: `${sparseSettingsJson}\n`, + }).pipe( + Effect.provideService(FileSystem.FileSystem, fs), + Effect.provideService(Path.Path, pathService), + ); + }, + Effect.mapError( + (cause) => + new ServerSettingsError({ + settingsPath, + operation: "write-file", + cause, + }), + ), + ); + const loadSettingsFromDisk = Effect.gen(function* () { let settings = DEFAULT_SERVER_SETTINGS; let persisted: typeof PersistedOptionalProviderSettings.Type = {}; + // A file that failed to decode must stay on disk for the user to repair; + // the fold below only writes when it started from the file's real contents. + let settingsFileTrusted = true; if (yield* readConfigExists) { const raw = yield* readRawConfig; @@ -452,6 +571,7 @@ const make = Effect.gen(function* () { } if (decoded._tag === "Failure" || persistedSettings._tag === "Failure") { const failure = decoded._tag === "Failure" ? decoded : persistedSettings; + settingsFileTrusted = false; if (failure._tag === "Failure") { yield* Effect.logWarning("failed to parse settings.json, using defaults", { path: settingsPath, @@ -490,9 +610,39 @@ const make = Effect.gen(function* () { ), ); - return foldProviderInstanceEnabledFlags( + const legacyProjectRows = + settings.projectSettingsFolded || !settingsFileTrusted + ? [] + : yield* sql` + SELECT + project_id AS "projectId", + default_model_selection_json AS "defaultModelSelection", + default_thread_env_mode AS "defaultThreadEnvMode", + auto_pull AS "autoPull", + scripts_json AS "scripts" + FROM projection_projects + WHERE deleted_at IS NULL + `.pipe( + Effect.mapError( + (cause) => + new ServerSettingsError({ + settingsPath, + operation: "read-project-settings", + cause, + }), + ), + ); + + const loaded = foldProviderInstanceEnabledFlags( restoreUsedProviders(settings, persisted, providerHistory), ); + const folded = settingsFileTrusted + ? foldLegacyProjectSettings(loaded, legacyProjectRows) + : loaded; + if (folded !== loaded) { + yield* writeSettingsAtomically(folded); + } + return folded; }); const settingsCache = yield* Cache.make({ @@ -738,30 +888,6 @@ const make = Effect.gen(function* () { }; }); - const writeSettingsAtomically = Effect.fnUntraced( - function* (settings: ServerSettings) { - const sparseSettingsJson = yield* encodeServerSettingsJson( - stripDefaultServerSettings(settings, PERSISTED_SERVER_SETTINGS_DEFAULTS) ?? {}, - ); - - return yield* writeFileStringAtomically({ - filePath: settingsPath, - contents: `${sparseSettingsJson}\n`, - }).pipe( - Effect.provideService(FileSystem.FileSystem, fs), - Effect.provideService(Path.Path, pathService), - ); - }, - Effect.mapError( - (cause) => - new ServerSettingsError({ - settingsPath, - operation: "write-file", - cause, - }), - ), - ); - const revalidateAndEmit = writeSemaphore.withPermits(1)( Effect.gen(function* () { yield* Cache.invalidate(settingsCache, cacheKey); diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index b9fc9e7ee3ab6..6668cc6a0ff53 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -22,7 +22,7 @@ import type { VcsStatusStreamEvent, } from "@t3tools/contracts"; import { mergeGitStatusParts } from "@t3tools/shared/git"; -import { resolveProjectAutoPull } from "@t3tools/shared/serverSettings"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; import * as GitWorkflowService from "../git/GitWorkflowService.ts"; @@ -160,7 +160,7 @@ export const autoPullPolicyLayer = Layer.effect( const project = yield* snapshots.getActiveProjectByWorkspaceRoot(cwd); if (project._tag === "None") return false; const settings = yield* serverSettings.getSettings; - return resolveProjectAutoPull(settings, project.value.id, project.value.autoPull); + return resolveProjectSettings(settings, project.value.id).settings.defaultAutoPull; }, Effect.orElseSucceed(() => false), ), diff --git a/packages/client-runtime/src/state/sharedSettings.test.ts b/packages/client-runtime/src/state/sharedSettings.test.ts index fc5ffe62ebc43..3e9934f6f7829 100644 --- a/packages/client-runtime/src/state/sharedSettings.test.ts +++ b/packages/client-runtime/src/state/sharedSettings.test.ts @@ -1,6 +1,7 @@ import { DEFAULT_SERVER_SETTINGS, EnvironmentId, + ProjectId, ProviderDriverKind, ProviderInstanceId, } from "@t3tools/contracts"; @@ -43,6 +44,17 @@ describe("supportsSharedSettingsSync", () => { }); describe("splitSharedServerPatch", () => { + it("keeps project overrides local: project ids belong to one environment", () => { + const patch = { + projectSettingsOverrides: { [ProjectId.make("project")]: { defaultAutoPull: true } }, + sidebarAutoSettleOnMerge: false, + }; + expect(splitSharedServerPatch(patch)).toEqual({ + sharedPatch: { sidebarAutoSettleOnMerge: false }, + localPatch: { projectSettingsOverrides: patch.projectSettingsOverrides }, + }); + }); + it.each([ { instanceId: ProviderInstanceId.make("codex"), diff --git a/packages/contracts/src/environment.ts b/packages/contracts/src/environment.ts index 9dcc844e713a0..a57f30f95e556 100644 --- a/packages/contracts/src/environment.ts +++ b/packages/contracts/src/environment.ts @@ -99,6 +99,8 @@ export const ExecutionEnvironmentCapabilities = Schema.Struct({ threadAutoSettlement: Schema.optionalKey(Schema.Boolean), /** Server persists the opt-in for continuing interrupted threads after restarts. */ threadRestartContinuation: Schema.optionalKey(Schema.Boolean), + /** Server resolves `projectSettingsOverrides`; older servers ignore the key. */ + projectSettingsOverrides: Schema.optionalKey(Schema.Boolean), /** Server understands thread.snooze / thread.unsnooze commands. Same version-skew contract as threadSettlement. */ threadSnooze: Schema.optionalKey(Schema.Boolean), diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index dd6136461fc16..90b176c3095a7 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -928,6 +928,55 @@ export const BackgroundActivitySettings = Schema.Struct({ }).pipe(Schema.withDecodingDefault(Effect.succeed({}))); export type BackgroundActivitySettings = typeof BackgroundActivitySettings.Type; +/** + * Server settings a project may override. Every other server setting is + * environment-wide: providers, keybindings, observability, device hosts, + * background activity, theme. UI, search and the write planner derive + * eligibility from this list, so adding a key here is the whole opt-in. + */ +export const PROJECT_SCOPED_SERVER_SETTING_KEYS = [ + "defaultModelSelection", + "defaultThreadEnvMode", + "newWorktreesStartFromOrigin", + "defaultAutoPull", + "defaultProjectScripts", + "enableAgentBrowserAccess", + "enableAgentDeviceAccess", + "textGenerationModelSelection", + "sourceControlWriterModelSelection", + "sourceControlWritingStyle", + "pullRequestMergeMethod", + "sidebarAutoSettleOnMerge", + "sidebarAutoSettleAfterDays", + "continueThreadsAfterServerUpdate", + "enableLegacyTokenStreaming", +] as const; +export type ProjectScopedServerSettingKey = (typeof PROJECT_SCOPED_SERVER_SETTING_KEYS)[number]; + +/** + * One project's overrides. An absent key inherits the environment value; + * `null` is a real value where the environment type is nullable (no default + * model, no dedicated writer model, never auto-settle). + */ +export const ProjectSettingsOverrides = Schema.Struct({ + defaultModelSelection: Schema.optionalKey(Schema.NullOr(ModelSelection)), + defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode), + newWorktreesStartFromOrigin: Schema.optionalKey(Schema.Boolean), + defaultAutoPull: Schema.optionalKey(Schema.Boolean), + defaultProjectScripts: Schema.optionalKey(Schema.Array(ProjectScript)), + enableAgentBrowserAccess: Schema.optionalKey(Schema.Boolean), + enableAgentDeviceAccess: Schema.optionalKey(Schema.Boolean), + textGenerationModelSelection: Schema.optionalKey(ModelSelection), + sourceControlWriterModelSelection: Schema.optionalKey(Schema.NullOr(ModelSelection)), + sourceControlWritingStyle: Schema.optionalKey(SourceControlWritingStyleSettings), + pullRequestMergeMethod: Schema.optionalKey(Schema.NullOr(PullRequestMergeMethod)), + sidebarAutoSettleOnMerge: Schema.optionalKey(Schema.Boolean), + sidebarAutoSettleAfterDays: Schema.optionalKey(Schema.NullOr(SidebarAutoSettleAfterDays)), + continueThreadsAfterServerUpdate: Schema.optionalKey(Schema.Boolean), + enableLegacyTokenStreaming: Schema.optionalKey(Schema.Boolean), +} satisfies Record); +export type ProjectSettingsOverrides = typeof ProjectSettingsOverrides.Type; + export const ServerSettings = Schema.Struct({ // Legacy token-by-token assistant output. Deliberately a fresh key (was // `enableAssistantStreaming`): decoding drops the old key, so everyone, @@ -968,6 +1017,21 @@ export const ServerSettings = Schema.Struct({ defaultModelSelection: Schema.NullOr(ModelSelection).pipe( Schema.withDecodingDefault(Effect.succeed(null)), ), + /** + * Per-project overrides of the keys in `PROJECT_SCOPED_SERVER_SETTING_KEYS`. + * The source of truth for project settings; `projectAgentBrowserAccessOverrides`, + * `projectAutoPullOverrides` and `projectScriptOverrides` are derived views + * kept for one release so older clients keep reading them. + */ + projectSettingsOverrides: Schema.Record(ProjectId, ProjectSettingsOverrides).pipe( + Schema.withDecodingDefault(Effect.succeed({})), + ), + /** + * Whether the legacy per-project fields have been folded into + * `projectSettingsOverrides`. The fold runs once so a later reset in the + * settings UI is not undone by the next server start. + */ + projectSettingsFolded: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), /** * Whether agents may drive simulators and emulators. Gates the `device_*` * MCP tools and the preconfigured `agent-device` CLI the same way @@ -1053,6 +1117,14 @@ export const ServerSettings = Schema.Struct({ sourceControlWriterModelSelection: Schema.NullOr(ModelSelection).pipe( Schema.withDecodingDefault(Effect.succeed(null)), ), + /** + * The merge method pull requests start with; `null` reuses the method + * last chosen on this device. Server-side so a project can override it + * like any other project setting. + */ + pullRequestMergeMethod: Schema.NullOr(PullRequestMergeMethod).pipe( + Schema.withDecodingDefault(Effect.succeed(null)), + ), // Legacy single-instance-per-driver settings. Continues to be the source // of truth until `providerInstances` (below) lands per-driver migration @@ -1140,6 +1212,7 @@ export const ServerSettingsOperation = Schema.Literals([ "check-exists", "read-file", "read-provider-history", + "read-project-settings", "read-secret", "remove-secret", "remove-stale-secret", @@ -1257,6 +1330,16 @@ export const ServerSettingsPatch = Schema.Struct({ Schema.Record(ProjectId, Schema.NullOr(Schema.Boolean)), ), defaultModelSelection: Schema.optionalKey(Schema.NullOr(ModelSelection)), + /** + * Per-project entry replacement: each entry replaces that project's whole + * override set and `null` removes it. Clearing one override means resending + * the entry without that key. Per-key null cannot express "clear" for the + * keys whose value type is itself nullable, and clients always hold the + * current entry from the last settings snapshot. + */ + projectSettingsOverrides: Schema.optionalKey( + Schema.Record(ProjectId, Schema.NullOr(ProjectSettingsOverrides)), + ), enableAgentDeviceAccess: Schema.optionalKey(Schema.Boolean), enableDeviceSupport: Schema.optionalKey(Schema.Boolean), deviceOnboardingCompleted: Schema.optionalKey(Schema.Boolean), @@ -1287,6 +1370,7 @@ export const ServerSettingsPatch = Schema.Struct({ }), ), sourceControlWriterModelSelection: Schema.optionalKey(Schema.NullOr(ModelSelection)), + pullRequestMergeMethod: Schema.optionalKey(Schema.NullOr(PullRequestMergeMethod)), observability: Schema.optionalKey( Schema.Struct({ otlpTracesUrl: Schema.optionalKey(TrimmedString), diff --git a/packages/shared/package.json b/packages/shared/package.json index b502090e2a9dc..bda80d937b920 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -103,6 +103,10 @@ "types": "./src/projectScripts.ts", "import": "./src/projectScripts.ts" }, + "./projectSettings": { + "types": "./src/projectSettings.ts", + "import": "./src/projectSettings.ts" + }, "./threadEnvMode": { "types": "./src/threadEnvMode.ts", "import": "./src/threadEnvMode.ts" diff --git a/packages/shared/src/projectScripts.ts b/packages/shared/src/projectScripts.ts index 4d98e36b4d706..5cb988753a391 100644 --- a/packages/shared/src/projectScripts.ts +++ b/packages/shared/src/projectScripts.ts @@ -1,23 +1,41 @@ import type { ProjectId, ProjectScript, ServerSettings } from "@t3tools/contracts"; -/** Missing entries preserve existing actions; null explicitly resets a checkout to machine defaults. */ +type ProjectScriptSettings = Pick< + ServerSettings, + | "defaultProjectScripts" + | "projectScriptOverrides" + | "projectSettingsOverrides" + | "projectSettingsFolded" +>; + +/** + * The project's override wins, then environment defaults. Until the legacy + * fields have been folded into `projectSettingsOverrides`, the old map (null + * there meant "reset to machine defaults") and the aggregate's own scripts + * still count, so a server that has not run the fold yet behaves as before. + */ export function resolveProjectScripts( - settings: Pick, + settings: ProjectScriptSettings, project: { id: ProjectId; scripts: readonly ProjectScript[] }, ): readonly ProjectScript[] { - const override = settings.projectScriptOverrides[project.id]; - if (override === null) return settings.defaultProjectScripts; - return ( - override ?? (project.scripts.length > 0 ? project.scripts : settings.defaultProjectScripts) - ); + const override = settings.projectSettingsOverrides[project.id]?.defaultProjectScripts; + if (override !== undefined) return override; + if (settings.projectSettingsFolded) return settings.defaultProjectScripts; + const legacy = settings.projectScriptOverrides[project.id]; + if (legacy === null) return settings.defaultProjectScripts; + return legacy ?? (project.scripts.length > 0 ? project.scripts : settings.defaultProjectScripts); } export function projectScriptsInheritDefaults( - settings: Pick, + settings: ProjectScriptSettings, project: { id: ProjectId; scripts: readonly ProjectScript[] }, ): boolean { - const override = settings.projectScriptOverrides[project.id]; - return override === null || (override === undefined && project.scripts.length === 0); + if (settings.projectSettingsOverrides[project.id]?.defaultProjectScripts !== undefined) { + return false; + } + if (settings.projectSettingsFolded) return true; + const legacy = settings.projectScriptOverrides[project.id]; + return legacy === null || (legacy === undefined && project.scripts.length === 0); } interface ProjectScriptRuntimeEnvInput { diff --git a/packages/shared/src/projectSettings.test.ts b/packages/shared/src/projectSettings.test.ts new file mode 100644 index 0000000000000..950867d36dc7c --- /dev/null +++ b/packages/shared/src/projectSettings.test.ts @@ -0,0 +1,208 @@ +import { + DEFAULT_SERVER_SETTINGS, + PROJECT_SCOPED_SERVER_SETTING_KEYS, + ProjectId, + ProviderInstanceId, +} from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; +import { createModelSelection } from "./model.ts"; +import { + clearProjectSettingsOverrides, + hasProjectSettingsOverrides, + resolveProjectSettings, + withProjectSettingsOverrides, +} from "./projectSettings.ts"; +import { applyServerSettingsPatch } from "./serverSettings.ts"; + +const projectId = ProjectId.make("project-a"); +const otherProjectId = ProjectId.make("project-b"); + +describe("resolveProjectSettings", () => { + it("inherits every scopable key when the project has no overrides", () => { + const resolved = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId); + expect(resolved.settings).toBe(DEFAULT_SERVER_SETTINGS); + for (const key of PROJECT_SCOPED_SERVER_SETTING_KEYS) { + expect(resolved.sources[key]).toBe("environment"); + } + expect(resolveProjectSettings(DEFAULT_SERVER_SETTINGS, null).settings).toBe( + DEFAULT_SERVER_SETTINGS, + ); + }); + + it("applies overrides per key and reports their source", () => { + const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + defaultAutoPull: true, + sidebarAutoSettleAfterDays: 3, + projectSettingsOverrides: { + [projectId]: { defaultAutoPull: false, sidebarAutoSettleAfterDays: null }, + }, + }); + const resolved = resolveProjectSettings(settings, projectId); + expect(resolved.settings.defaultAutoPull).toBe(false); + expect(resolved.settings.sidebarAutoSettleAfterDays).toBeNull(); + expect(resolved.settings.defaultThreadEnvMode).toBe(settings.defaultThreadEnvMode); + expect(resolved.sources.defaultAutoPull).toBe("project"); + expect(resolved.sources.sidebarAutoSettleAfterDays).toBe("project"); + expect(resolved.sources.defaultThreadEnvMode).toBe("environment"); + expect(resolveProjectSettings(settings, otherProjectId).settings.defaultAutoPull).toBe(true); + }); + + it("keeps the environment text generation model when the override's provider is disabled", () => { + const disabledSelection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "opus"); + const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + providers: { claudeAgent: { enabled: false } }, + projectSettingsOverrides: { + [projectId]: { textGenerationModelSelection: disabledSelection }, + }, + }); + const resolved = resolveProjectSettings(settings, projectId); + expect(resolved.settings.textGenerationModelSelection).toEqual( + settings.textGenerationModelSelection, + ); + expect(resolved.sources.textGenerationModelSelection).toBe("environment"); + }); + + it("honours the aggregate's own fields only until the server has folded them", () => { + const aggregateModel = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.5"); + const project = { + defaultModelSelection: aggregateModel, + defaultThreadEnvMode: "local" as const, + }; + const unfolded = resolveProjectSettings( + { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: false }, + projectId, + project, + ); + expect(unfolded.settings.defaultModelSelection).toEqual(aggregateModel); + expect(unfolded.settings.defaultThreadEnvMode).toBe("local"); + expect(unfolded.sources.defaultModelSelection).toBe("project"); + // A stored override still beats the aggregate before the fold. + const overridden = resolveProjectSettings( + { + ...DEFAULT_SERVER_SETTINGS, + projectSettingsFolded: false, + projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "worktree" } }, + }, + projectId, + project, + ); + expect(overridden.settings.defaultThreadEnvMode).toBe("worktree"); + // After the fold a reset in the record wins over the stale aggregate. + const folded = resolveProjectSettings( + { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: true }, + projectId, + project, + ); + expect(folded.settings.defaultModelSelection).toBeNull(); + expect(folded.sources.defaultModelSelection).toBe("environment"); + }); + + it("keeps the environment default model when the override's provider is disabled", () => { + const disabledSelection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "opus"); + const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + providers: { claudeAgent: { enabled: false } }, + projectSettingsOverrides: { [projectId]: { defaultModelSelection: disabledSelection } }, + }); + const resolved = resolveProjectSettings(settings, projectId); + expect(resolved.settings.defaultModelSelection).toBeNull(); + expect(resolved.sources.defaultModelSelection).toBe("environment"); + }); +}); + +describe("projectSettingsOverrides patches", () => { + it("replaces a project's entry, removes it with null, and drops empty entries", () => { + const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + projectSettingsOverrides: { + [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false }, + [otherProjectId]: { defaultAutoPull: false }, + }, + }); + expect(hasProjectSettingsOverrides(first)).toBe(true); + const replaced = applyServerSettingsPatch(first, { + projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: false } }, + }); + expect(replaced.projectSettingsOverrides[projectId]).toEqual({ + enableAgentBrowserAccess: false, + }); + expect(replaced.projectSettingsOverrides[otherProjectId]).toEqual({ defaultAutoPull: false }); + const emptied = applyServerSettingsPatch(replaced, { + projectSettingsOverrides: { [projectId]: {} }, + }); + expect(emptied.projectSettingsOverrides[projectId]).toBeUndefined(); + const removed = applyServerSettingsPatch(replaced, { + projectSettingsOverrides: { [projectId]: null }, + }); + expect(removed.projectSettingsOverrides).toEqual({ + [otherProjectId]: { defaultAutoPull: false }, + }); + expect(hasProjectSettingsOverrides(DEFAULT_SERVER_SETTINGS)).toBe(false); + }); + + it("derives the legacy per-key maps from the generic record", () => { + const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + projectSettingsOverrides: { + [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false }, + [otherProjectId]: { defaultProjectScripts: [] }, + }, + }); + expect(settings.projectAutoPullOverrides).toEqual({ [projectId]: true }); + expect(settings.projectAgentBrowserAccessOverrides).toEqual({ [projectId]: false }); + expect(settings.projectScriptOverrides).toEqual({ [otherProjectId]: [] }); + }); + + it("translates legacy per-key patches into the generic record", () => { + const written = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + projectAutoPullOverrides: { [projectId]: true }, + projectAgentBrowserAccessOverrides: { [projectId]: false, [otherProjectId]: true }, + }); + expect(written.projectSettingsOverrides).toEqual({ + [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false }, + [otherProjectId]: { enableAgentBrowserAccess: true }, + }); + const cleared = applyServerSettingsPatch(written, { + projectAgentBrowserAccessOverrides: { [projectId]: null, [otherProjectId]: null }, + }); + expect(cleared.projectSettingsOverrides).toEqual({ [projectId]: { defaultAutoPull: true } }); + }); + + it("lets a canonical entry win over a legacy map for the same project", () => { + const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } }, + }); + // The canonical entry omits defaultAutoPull to clear it; the stale legacy + // map in the same patch must not put it back. + const next = applyServerSettingsPatch(current, { + projectSettingsOverrides: { [projectId]: { defaultThreadEnvMode: "local" } }, + projectAutoPullOverrides: { [projectId]: true, [otherProjectId]: false }, + }); + expect(next.projectSettingsOverrides).toEqual({ + [projectId]: { defaultThreadEnvMode: "local" }, + [otherProjectId]: { defaultAutoPull: false }, + }); + }); + + it("builds replacement entries and clears individual keys", () => { + const settings = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + projectSettingsOverrides: { + [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false }, + }, + }); + expect(clearProjectSettingsOverrides(settings, projectId, ["defaultAutoPull"])).toEqual({ + enableAgentBrowserAccess: false, + }); + expect( + clearProjectSettingsOverrides(settings, projectId, [ + "defaultAutoPull", + "enableAgentBrowserAccess", + ]), + ).toBeNull(); + expect(clearProjectSettingsOverrides(settings, otherProjectId, ["defaultAutoPull"])).toBeNull(); + expect(withProjectSettingsOverrides(settings, projectId, null)).toEqual({}); + expect( + withProjectSettingsOverrides(settings, otherProjectId, { defaultThreadEnvMode: "worktree" }), + ).toEqual({ + ...settings.projectSettingsOverrides, + [otherProjectId]: { defaultThreadEnvMode: "worktree" }, + }); + }); +}); diff --git a/packages/shared/src/projectSettings.ts b/packages/shared/src/projectSettings.ts new file mode 100644 index 0000000000000..742e1cb8d140c --- /dev/null +++ b/packages/shared/src/projectSettings.ts @@ -0,0 +1,124 @@ +import { + type ModelSelection, + PROJECT_SCOPED_SERVER_SETTING_KEYS, + type ProjectId, + type ProjectScopedServerSettingKey, + type ProjectSettingsOverrides, + type ServerSettings, + type ThreadEnvMode, +} from "@t3tools/contracts"; +import { isModelSelectionProviderEnabled } from "./serverSettings.ts"; + +export type ProjectSettingSource = "environment" | "project"; + +export type ProjectSettingSources = Readonly< + Record +>; + +export interface ResolvedProjectSettings { + /** Environment settings with the project's overrides applied. */ + readonly settings: ServerSettings; + /** Where each scopable key's effective value came from. */ + readonly sources: ProjectSettingSources; + /** The project's raw override entry; `{}` when it has none. */ + readonly overrides: ProjectSettingsOverrides; +} + +const EMPTY_OVERRIDES: ProjectSettingsOverrides = {}; + +const ENVIRONMENT_SOURCES: ProjectSettingSources = Object.fromEntries( + PROJECT_SCOPED_SERVER_SETTING_KEYS.map((key) => [key, "environment"]), +) as Record; + +/** Cheap check so hot paths skip the projectId lookup when nothing is overridden. */ +export function hasProjectSettingsOverrides( + settings: Pick, +): boolean { + for (const entry of Object.values(settings.projectSettingsOverrides)) { + if (Object.keys(entry).length > 0) return true; + } + return false; +} + +/** + * The project aggregate's own model and workspace fields. They remain the + * source of truth until the server has folded them into the override record; + * after the fold the record alone decides, so a reset there cannot be undone + * by a stale aggregate value. + */ +export interface LegacyProjectSettingsFields { + readonly defaultModelSelection?: ModelSelection | null | undefined; + readonly defaultThreadEnvMode?: ThreadEnvMode | null | undefined; +} + +/** + * Apply one project's overrides on top of environment settings. A model + * override whose provider is disabled on this environment falls back to the + * environment value, the same guard the environment-level selection gets. + */ +export function resolveProjectSettings( + settings: ServerSettings, + projectId: ProjectId | null, + project?: LegacyProjectSettingsFields, +): ResolvedProjectSettings { + const stored = projectId === null ? undefined : settings.projectSettingsOverrides[projectId]; + const overrides: ProjectSettingsOverrides = + project === undefined || settings.projectSettingsFolded + ? (stored ?? EMPTY_OVERRIDES) + : { + ...(project.defaultModelSelection != null + ? { defaultModelSelection: project.defaultModelSelection } + : {}), + ...(project.defaultThreadEnvMode != null + ? { defaultThreadEnvMode: project.defaultThreadEnvMode } + : {}), + ...stored, + }; + if (Object.keys(overrides).length === 0) { + return { settings, sources: ENVIRONMENT_SOURCES, overrides: EMPTY_OVERRIDES }; + } + const sources: Record = { + ...ENVIRONMENT_SOURCES, + }; + const effective: Record = { ...settings }; + for (const key of PROJECT_SCOPED_SERVER_SETTING_KEYS) { + if (!Object.hasOwn(overrides, key)) continue; + const value = overrides[key]; + // A model on a disabled provider falls back to the environment, like the + // environment-level guards do for these keys. + if ( + (key === "textGenerationModelSelection" || key === "defaultModelSelection") && + value !== undefined && + value !== null && + !isModelSelectionProviderEnabled(settings, value as ModelSelection) + ) { + continue; + } + effective[key] = value; + sources[key] = "project"; + } + return { settings: effective as ServerSettings, sources, overrides }; +} + +/** Replace the project's entry, dropping it entirely when nothing is overridden. */ +export function withProjectSettingsOverrides( + settings: Pick, + projectId: ProjectId, + next: ProjectSettingsOverrides | null, +): ServerSettings["projectSettingsOverrides"] { + const { [projectId]: _removed, ...rest } = settings.projectSettingsOverrides; + return next === null || Object.keys(next).length === 0 ? rest : { ...rest, [projectId]: next }; +} + +/** The project's entry with `keys` removed; `null` when that leaves it empty. */ +export function clearProjectSettingsOverrides( + settings: Pick, + projectId: ProjectId, + keys: readonly ProjectScopedServerSettingKey[], +): ProjectSettingsOverrides | null { + const current = settings.projectSettingsOverrides[projectId]; + if (current === undefined) return null; + const next = { ...current }; + for (const key of keys) delete next[key]; + return Object.keys(next).length === 0 ? null : next; +} diff --git a/packages/shared/src/serverSettings.test.ts b/packages/shared/src/serverSettings.test.ts index cc783fe64bf33..2658db3346e68 100644 --- a/packages/shared/src/serverSettings.test.ts +++ b/packages/shared/src/serverSettings.test.ts @@ -20,6 +20,9 @@ import { resolveProjectAutoPull, } from "./serverSettings.ts"; +/** Settings after the server has folded legacy per-project fields into `projectSettingsOverrides`. */ +const FOLDED_SERVER_SETTINGS = { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: true }; + describe("serverSettings helpers", () => { it("replaces SSH host lists when saving, editing, and removing hosts", () => { const host = { id: "mini", label: "Mac mini", target: "mini" }; @@ -40,14 +43,19 @@ describe("serverSettings helpers", () => { icon: "play" as const, runOnWorktreeCreate: false, }; - const defaults = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + const existing = { ...project, scripts: [{ ...action, command: "npm run lint" }] }; + // Before the one-time fold, scripts stored on the project aggregate still apply. + const unfolded = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + defaultProjectScripts: [action], + }); + expect(resolveProjectScripts(unfolded, existing)).toEqual(existing.scripts); + expect(projectScriptsInheritDefaults(unfolded, existing)).toBe(false); + const defaults = applyServerSettingsPatch(FOLDED_SERVER_SETTINGS, { defaultProjectScripts: [action], }); expect(resolveProjectScripts(defaults, project)).toEqual([action]); expect(projectScriptsInheritDefaults(defaults, project)).toBe(true); - const existing = { ...project, scripts: [{ ...action, command: "npm run lint" }] }; - expect(resolveProjectScripts(defaults, existing)).toEqual(existing.scripts); - expect(projectScriptsInheritDefaults(defaults, existing)).toBe(false); + expect(resolveProjectScripts(defaults, existing)).toEqual([action]); const disabled = applyServerSettingsPatch(defaults, { projectScriptOverrides: { [project.id]: [] }, }); @@ -82,7 +90,7 @@ describe("serverSettings helpers", () => { }; const firstAction = { ...defaultAction, command: "npm run lint" }; const secondAction = { ...defaultAction, command: "npm run build" }; - const firstUpdate = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + const firstUpdate = applyServerSettingsPatch(FOLDED_SERVER_SETTINGS, { defaultProjectScripts: [defaultAction], projectScriptOverrides: { [firstProject.id]: [firstAction] }, }); diff --git a/packages/shared/src/serverSettings.ts b/packages/shared/src/serverSettings.ts index f969e4412c303..bb209fd812bdd 100644 --- a/packages/shared/src/serverSettings.ts +++ b/packages/shared/src/serverSettings.ts @@ -4,6 +4,8 @@ import { resolveProviderInstanceEnabled, type ModelSelection, type ProjectId, + type ProjectScopedServerSettingKey, + type ProjectSettingsOverrides, type ProviderDriverKind, type ServerProvider, ServerSettings, @@ -24,22 +26,33 @@ import { const ServerSettingsJson = fromLenientJson(ServerSettings); const decodeServerSettingsJson = Schema.decodeUnknownOption(ServerSettingsJson); +/** @deprecated Read `resolveProjectSettings(...).settings.enableAgentBrowserAccess`. */ export function resolveProjectAgentBrowserAccess( - settings: Pick, + settings: Pick< + ServerSettings, + "enableAgentBrowserAccess" | "projectAgentBrowserAccessOverrides" | "projectSettingsOverrides" + >, projectId: ProjectId, ): boolean { return ( - settings.projectAgentBrowserAccessOverrides[projectId] ?? settings.enableAgentBrowserAccess + settings.projectSettingsOverrides[projectId]?.enableAgentBrowserAccess ?? + settings.projectAgentBrowserAccessOverrides[projectId] ?? + settings.enableAgentBrowserAccess ); } +/** @deprecated Read `resolveProjectSettings(...).settings.defaultAutoPull`. */ export function resolveProjectAutoPull( - settings: Pick, + settings: Pick< + ServerSettings, + "defaultAutoPull" | "projectAutoPullOverrides" | "projectSettingsOverrides" + >, projectId: ProjectId, legacyAutoPull: boolean | undefined, ): boolean { // Existing opt-ins stay enabled until explicitly overridden or reset. return ( + settings.projectSettingsOverrides[projectId]?.defaultAutoPull ?? settings.projectAutoPullOverrides[projectId] ?? (legacyAutoPull === true || settings.defaultAutoPull) ); @@ -160,10 +173,97 @@ function mergeSettingsEntries( return Object.fromEntries(next); } +/** + * Derived views of `projectSettingsOverrides` for clients that still read + * the legacy per-key maps. Recomputed on every patch and load so they + * cannot drift from the generic record. + */ +export function deriveLegacyProjectOverrides( + settings: Pick, +): Pick< + ServerSettings, + "projectAgentBrowserAccessOverrides" | "projectAutoPullOverrides" | "projectScriptOverrides" +> { + const projectAgentBrowserAccessOverrides: Record = {}; + const projectAutoPullOverrides: Record = {}; + const projectScriptOverrides: Record = {}; + for (const [projectId, entry] of Object.entries(settings.projectSettingsOverrides)) { + if (entry.enableAgentBrowserAccess !== undefined) { + projectAgentBrowserAccessOverrides[projectId] = entry.enableAgentBrowserAccess; + } + if (entry.defaultAutoPull !== undefined) { + projectAutoPullOverrides[projectId] = entry.defaultAutoPull; + } + if (entry.defaultProjectScripts !== undefined) { + projectScriptOverrides[projectId] = entry.defaultProjectScripts; + } + } + return { projectAgentBrowserAccessOverrides, projectAutoPullOverrides, projectScriptOverrides }; +} + +/** + * Rewrite a patch that still uses the legacy per-key project maps into + * entries of `projectSettingsOverrides`, so older clients keep editing the + * values the server actually reads. `null` in a legacy map clears that one + * override. + */ +function translateLegacyProjectOverridePatch( + current: Pick, + patch: ServerSettingsPatch, +): ServerSettingsPatch { + const { + projectAgentBrowserAccessOverrides, + projectAutoPullOverrides, + projectScriptOverrides, + ...rest + } = patch; + if ( + projectAgentBrowserAccessOverrides === undefined && + projectAutoPullOverrides === undefined && + projectScriptOverrides === undefined + ) { + return patch; + } + const currentEntries: Readonly> = + current.projectSettingsOverrides; + const entries = new Map( + Object.entries(rest.projectSettingsOverrides ?? {}), + ); + // A canonical entry in the same patch is the newer representation; a legacy + // map must not resurrect a key that entry deliberately omits. + const canonicalProjectIds = new Set(Object.keys(rest.projectSettingsOverrides ?? {})); + const applyKey = ( + map: Readonly> | undefined, + key: K, + ) => { + if (map === undefined) return; + for (const [projectId, value] of Object.entries(map)) { + if (canonicalProjectIds.has(projectId)) continue; + const entry: ProjectSettingsOverrides = { + ...(entries.get(projectId) ?? currentEntries[projectId] ?? {}), + }; + if (value === null || value === undefined) { + delete entry[key]; + } else { + entry[key] = value; + } + entries.set(projectId, Object.keys(entry).length === 0 ? null : entry); + } + }; + applyKey(projectAgentBrowserAccessOverrides, "enableAgentBrowserAccess"); + applyKey(projectAutoPullOverrides, "defaultAutoPull"); + applyKey(projectScriptOverrides, "defaultProjectScripts"); + return { + ...rest, + projectSettingsOverrides: Object.fromEntries(entries), + } as ServerSettingsPatch; +} + export function applyServerSettingsPatch( current: ServerSettings, - patch: ServerSettingsPatch, + rawPatch: ServerSettingsPatch, ): ServerSettings { + const patch = translateLegacyProjectOverridePatch(current, rawPatch); const selectionPatch = patch.textGenerationModelSelection; const { automaticGitFetchInterval, @@ -173,8 +273,13 @@ export function applyServerSettingsPatch( // Merged per entry below; its `null` removals must not reach deepMerge. usageLimitSources: usageLimitSourcesPatch, usagePriceOverrides: usagePriceOverridesPatch, - projectAgentBrowserAccessOverrides: projectAgentBrowserAccessOverridesPatch, - projectAutoPullOverrides: projectAutoPullOverridesPatch, + // Entry replacement: deepMerge would keep keys the client meant to clear. + projectSettingsOverrides: projectSettingsOverridesPatch, + // Already translated into `projectSettingsOverrides` above; the legacy + // maps are derived views and must never be merged directly. + projectAgentBrowserAccessOverrides: _legacyBrowserAccess, + projectAutoPullOverrides: _legacyAutoPull, + projectScriptOverrides: _legacyScripts, ...patchForMerge } = patch; const currentBackgroundActivity = normalizeServerBackgroundActivitySettings(current); @@ -231,19 +336,12 @@ export function applyServerSettingsPatch( ...(patch.providerInstances !== undefined ? { providerInstances: patch.providerInstances } : {}), - ...(projectAgentBrowserAccessOverridesPatch !== undefined + ...(projectSettingsOverridesPatch !== undefined ? { - projectAgentBrowserAccessOverrides: mergeSettingsEntries( - current.projectAgentBrowserAccessOverrides, - projectAgentBrowserAccessOverridesPatch, - ), - } - : {}), - ...(projectAutoPullOverridesPatch !== undefined - ? { - projectAutoPullOverrides: mergeSettingsEntries( - current.projectAutoPullOverrides, - projectAutoPullOverridesPatch, + projectSettingsOverrides: Object.fromEntries( + Object.entries( + mergeSettingsEntries(current.projectSettingsOverrides, projectSettingsOverridesPatch), + ).filter(([, entry]) => Object.keys(entry).length > 0), ), } : {}), @@ -253,14 +351,6 @@ export function applyServerSettingsPatch( ...(patch.defaultProjectScripts !== undefined ? { defaultProjectScripts: patch.defaultProjectScripts } : {}), - ...(patch.projectScriptOverrides !== undefined - ? { - projectScriptOverrides: { - ...current.projectScriptOverrides, - ...patch.projectScriptOverrides, - }, - } - : {}), ...(usageLimitSourcesPatch !== undefined ? { usageLimitSources: mergeSettingsEntries( @@ -291,6 +381,7 @@ export function applyServerSettingsPatch( ); const nextWithReplacements = { ...nextWithReplacementsBase, + ...deriveLegacyProjectOverrides(nextWithReplacementsBase), backgroundActivity: normalizedBackgroundActivity, automaticGitFetchInterval: resolvedBackgroundActivity.automaticGitFetchInterval, providerHealthRefreshInterval: resolvedBackgroundActivity.providerHealthRefreshInterval, From 8b2c0465def151f022e01dd04b1b6ba3859bd714 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 11 Sep 2026 13:56:39 -0700 Subject: [PATCH 29/63] feat(web): pick settings environment and project as two selects (#10636) --- .../settings/ProjectSettingsPanel.tsx | 71 +++--- .../components/settings/ProjectsSettings.tsx | 179 +++------------ .../SettingsScopeSelects.logic.test.ts | 88 +++++++ .../settings/SettingsScopeSelects.logic.ts | 55 +++++ .../settings/SettingsScopeSelects.tsx | 154 +++++++++++++ .../components/settings/settingsScope.test.ts | 216 ++++++++++++++++++ .../src/components/settings/settingsScope.ts | 163 +++++++++++++ .../settings/useSettingsProjectGroups.ts | 24 ++ apps/web/src/routes/settings.projects.tsx | 15 +- 9 files changed, 764 insertions(+), 201 deletions(-) create mode 100644 apps/web/src/components/settings/SettingsScopeSelects.logic.test.ts create mode 100644 apps/web/src/components/settings/SettingsScopeSelects.logic.ts create mode 100644 apps/web/src/components/settings/SettingsScopeSelects.tsx create mode 100644 apps/web/src/components/settings/settingsScope.test.ts create mode 100644 apps/web/src/components/settings/settingsScope.ts create mode 100644 apps/web/src/components/settings/useSettingsProjectGroups.ts diff --git a/apps/web/src/components/settings/ProjectSettingsPanel.tsx b/apps/web/src/components/settings/ProjectSettingsPanel.tsx index d88644fb7e3c7..a597e852c085d 100644 --- a/apps/web/src/components/settings/ProjectSettingsPanel.tsx +++ b/apps/web/src/components/settings/ProjectSettingsPanel.tsx @@ -68,7 +68,6 @@ import { } from "../../providerInstances"; import { getCustomModelOptionsByInstance } from "../../modelSelection"; import { - buildSidebarProjectSnapshots, type SidebarProjectGroupMember, type SidebarProjectSnapshot, } from "../../sidebarProjectGrouping"; @@ -115,6 +114,7 @@ import { ProjectFaviconPickerDialog, } from "./ProjectFaviconPickerDialog"; import { projectGroupTitleNeedsUpdate } from "./ProjectSettingsPanel.logic"; +import { useSettingsProjectGroups } from "./useSettingsProjectGroups"; const ProjectIconPickerDialog = lazy(() => import("./ProjectIconPickerDialog").then((module) => ({ @@ -128,31 +128,6 @@ export const PROJECT_GROUPING_MODE_LABELS: Record - new Map( - environments.map((environment) => [environment.environmentId, environment.label] as const), - ), - [environments], - ); - return useMemo( - () => - buildSidebarProjectSnapshots({ - projects, - settings: projectGroupingSettings, - primaryEnvironmentId, - resolveEnvironmentLabel: (environmentId) => environmentLabelById.get(environmentId) ?? null, - }).sort((a, b) => a.displayName.localeCompare(b.displayName)), - [environmentLabelById, primaryEnvironmentId, projectGroupingSettings, projects], - ); -} - function memberKey(member: { environmentId: string; id: string }): string { return `${member.environmentId}:${member.id}`; } @@ -160,9 +135,11 @@ function memberKey(member: { environmentId: string; id: string }): string { export function ProjectSettingsPanel({ projectKey, environmentId = null, + checkoutKey = null, }: { projectKey: string; environmentId?: EnvironmentId | null; + checkoutKey?: string | null; }) { const groups = useSettingsProjectGroups(); const navigate = useNavigate(); @@ -171,9 +148,11 @@ export function ProjectSettingsPanel({ const members = useMemo( () => selected?.memberProjects.filter( - (member) => environmentId === null || member.environmentId === environmentId, + (member) => + (environmentId === null || member.environmentId === environmentId) && + (checkoutKey === null || member.physicalProjectKey === checkoutKey), ) ?? [], - [selected, environmentId], + [selected, environmentId, checkoutKey], ); // Remember the members of the last rendered group so a grouping-rule change @@ -181,6 +160,7 @@ export function ProjectSettingsPanel({ const lastSelectionRef = useRef<{ key: string; environmentId: EnvironmentId | null; + checkoutKey: string | null; memberKeys: string[]; } | null>(null); useEffect(() => { @@ -188,28 +168,38 @@ export function ProjectSettingsPanel({ lastSelectionRef.current = { key: selected.projectKey, environmentId, + checkoutKey, memberKeys: members.map((member) => member.physicalProjectKey), }; - }, [selected, members, environmentId]); + }, [selected, members, environmentId, checkoutKey]); // A grouping-rule change replaces the group key mid-visit; follow the // project to its new key instead of parking on the not-found state. useEffect(() => { if (members.length > 0) return; const last = lastSelectionRef.current; - if (last?.key !== projectKey || last.environmentId !== environmentId) return; + if ( + last?.key !== projectKey || + last.environmentId !== environmentId || + last.checkoutKey !== checkoutKey + ) + return; const successor = groups.find((group) => group.memberProjects.some((member) => last.memberKeys.includes(member.physicalProjectKey)), ); if (successor) { void navigate({ to: "/settings/projects", - search: { project: successor.projectKey, machine: environmentId ?? undefined }, + search: { + project: successor.projectKey, + machine: environmentId ?? undefined, + checkout: checkoutKey ?? undefined, + }, replace: true, hashScrollIntoView: false, }); } - }, [groups, navigate, projectKey, members.length, environmentId]); + }, [groups, navigate, projectKey, members.length, environmentId, checkoutKey]); if (!selected) { return ( @@ -223,7 +213,7 @@ export function ProjectSettingsPanel({ if (members.length === 0) return (

- This project has no checkout on this machine. + This checkout is no longer available in the selected project and environment.

); const scopedGroup = { @@ -234,7 +224,7 @@ export function ProjectSettingsPanel({ }; return ( @@ -879,23 +869,14 @@ function ProjectDetail({ draftStore.clearProjectDraftThreadId(projectRef); } - if (isWholeGroup) { - if (hasOtherMembers) { - void navigate({ - to: "/settings/projects", - search: { project: group.projectKey, machine: undefined }, - replace: true, - }); - } else { - void navigate({ to: "/", replace: true }); - } + if (isWholeGroup && !hasOtherMembers) { + void navigate({ to: "/", replace: true }); } }, [ deleteProject, group.displayName, group.memberProjects.length, - group.projectKey, hasOtherMembers, navigate, reportFailure, diff --git a/apps/web/src/components/settings/ProjectsSettings.tsx b/apps/web/src/components/settings/ProjectsSettings.tsx index acc7326ac866a..a0ff91f03012c 100644 --- a/apps/web/src/components/settings/ProjectsSettings.tsx +++ b/apps/web/src/components/settings/ProjectsSettings.tsx @@ -1,168 +1,53 @@ -import { resolveEnvironmentMachineKind } from "@t3tools/contracts"; -import { ChevronDownIcon, FolderIcon } from "lucide-react"; -import { type ReactNode, useState } from "react"; -import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; -import { ProjectFavicon } from "../ProjectFavicon"; import { WorkspacePageContainer } from "../WorkspacePageContainer"; import { useEnvironments } from "../../state/environments"; -import { Toggle, ToggleGroup } from "../ui/toggle-group"; -import { - Combobox, - ComboboxEmpty, - ComboboxSearchInput, - ComboboxItem, - ComboboxList, - ComboboxPopup, - ComboboxTrigger, -} from "../ui/combobox"; -import { selectTriggerVariants } from "../ui/select"; -import { cn } from "../../lib/utils"; -import { ProjectSettingsPanel, useSettingsProjectGroups } from "./ProjectSettingsPanel"; +import { ProjectSettingsPanel } from "./ProjectSettingsPanel"; import { ProjectDefaultsSettings } from "./ProjectDefaultsSettings"; - -function ScopePicker({ - label, - value, - options, - onChange, -}: { - label: "project" | "machine"; - value: string | null; - options: ReadonlyArray<{ value: string; label: string; icon?: ReactNode }>; - onChange: (value: string | null) => void; -}) { - const [query, setQuery] = useState(""); - const selected = options.find((option) => option.value === value); - const allIcon = - label === "project" ? : null; - const items = [{ value: "all", label: `All ${label}s`, icon: allIcon }, ...options]; - return ( - item.value === (value ?? "all")) ?? null} - inputValue={query} - onInputValueChange={setQuery} - onOpenChange={() => setQuery("")} - onValueChange={(next) => { - if (next) onChange(next.value === "all" ? null : next.value); - }} - > - - - {value === null ? allIcon : selected?.icon} - - {value === null ? `All ${label}s` : (selected?.label ?? `Unavailable ${label}`)} - - - - - - - No matching {label}s. - - {(item: (typeof items)[number]) => ( - - {item.icon} - {item.label} - - )} - - - - ); -} +import { SettingsScopeSelects } from "./SettingsScopeSelects"; +import { resolveSettingsScope, type SettingsScopeSearch } from "./settingsScope"; +import { useSettingsProjectGroups } from "./useSettingsProjectGroups"; export function ProjectsSettings({ - projectKey, - machineId, + value, onScopeChange, }: { - projectKey: string | null; - machineId: string | null; - onScopeChange: (project: string | null, machine: string | null) => void; + value: SettingsScopeSearch; + onScopeChange: (scope: SettingsScopeSearch) => void; }) { const groups = useSettingsProjectGroups(); const { environments } = useEnvironments(); - const machine = environments.find((environment) => environment.environmentId === machineId); - const machineOptions = environments.map((environment) => ({ - value: environment.environmentId, - label: environment.label, - icon: ( - - ), - })); + const scope = resolveSettingsScope(value, groups, environments); + // The panel follows remembered members when grouping replaces a project key. + const projectScope = + scope.kind === "project" || + scope.kind === "checkout" || + (scope.kind === "unavailable" && + (scope.reason === "project-missing" || scope.reason === "checkout-missing")); return (
- -
- {environments.length > 3 ? ( - onScopeChange(projectKey, value)} - /> - ) : ( - { - const value = next[0]; - if (value) onScopeChange(projectKey, value === "all" ? null : value); - }} - > - All machines - {machineOptions.map((option) => ( - - {option.icon} - {option.label} - - ))} - - )} -
- ({ - value: group.projectKey, - label: group.displayName, - icon: , - }))} - onChange={(value) => onScopeChange(value, machineId)} - /> -
-
+ +
- {machineId !== null && !machine ? ( -

This machine is no longer available.

- ) : projectKey === null ? ( - - ) : ( + {value.project && projectScope ? ( + ) : scope.kind === "unavailable" ? ( +

{scope.message}

+ ) : ( + )}
); } +import { EnvironmentId } from "@t3tools/contracts"; diff --git a/apps/web/src/components/settings/SettingsScopeSelects.logic.test.ts b/apps/web/src/components/settings/SettingsScopeSelects.logic.test.ts new file mode 100644 index 0000000000000..d8e123631e619 --- /dev/null +++ b/apps/web/src/components/settings/SettingsScopeSelects.logic.test.ts @@ -0,0 +1,88 @@ +import { EnvironmentId } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { + environmentAxisValue, + projectAxisValue, + selectEnvironmentAxis, + selectProjectAxis, + settingsScopeEnvironmentLabel, +} from "./SettingsScopeSelects.logic"; + +const first = { + environmentId: EnvironmentId.make("first"), + label: "Development", + displayUrl: "https://first.example.com", +}; +const second = { + environmentId: EnvironmentId.make("second"), + label: "Development", + displayUrl: "https://second.example.com", +}; + +describe("settings scope environment labels", () => { + it("distinguishes same-name environments by address", () => { + const environments = [first, second]; + expect( + environments.map((environment) => settingsScopeEnvironmentLabel(environment, environments)), + ).toEqual([ + "Development · https://first.example.com", + "Development · https://second.example.com", + ]); + }); + + it("falls back to environment IDs when duplicate names have no display URL", () => { + const environments = [first, second].map((environment) => ({ + ...environment, + displayUrl: null, + })); + expect( + environments.map((environment) => settingsScopeEnvironmentLabel(environment, environments)), + ).toEqual(["Development · first", "Development · second"]); + }); + + it("keeps unique names compact and removes disambiguation after a rename", () => { + expect(settingsScopeEnvironmentLabel(first, [first])).toBe("Development"); + expect(settingsScopeEnvironmentLabel(first, [first, { ...second, label: "Production" }])).toBe( + "Development", + ); + }); +}); + +describe("settings scope axes", () => { + it("maps each axis to its search key and back", () => { + expect(projectAxisValue({})).toBe("all"); + expect(projectAxisValue({ project: "app" })).toBe("app"); + expect(selectProjectAxis({ machine: "second" }, "app")).toEqual({ + project: "app", + machine: "second", + }); + expect(selectProjectAxis({ machine: "second", project: "app" }, "all")).toEqual({ + machine: "second", + }); + expect(selectEnvironmentAxis({ project: "app" }, "first")).toEqual({ + project: "app", + machine: "first", + }); + expect(selectEnvironmentAxis({ project: "app", machine: "first" }, "all")).toEqual({ + project: "app", + }); + }); + + it("drops a checkout narrowing from older links when either axis changes", () => { + const checkout = { project: "app", checkout: "app@first", machine: "first" }; + expect(selectEnvironmentAxis(checkout, "second")).toEqual({ + project: "app", + machine: "second", + }); + expect(selectProjectAxis(checkout, "app")).toEqual({ project: "app", machine: "first" }); + }); +}); + +describe("environmentAxisValue", () => { + it("shows the checkout's environment for a legacy checkout link", () => { + expect(environmentAxisValue({ project: "p", checkout: "c" }, "laptop")).toBe("laptop"); + expect(environmentAxisValue({ project: "p" }, null)).toBe("all"); + expect(environmentAxisValue({ machine: "desk" }, "laptop")).toBe("desk"); + }); +}); diff --git a/apps/web/src/components/settings/SettingsScopeSelects.logic.ts b/apps/web/src/components/settings/SettingsScopeSelects.logic.ts new file mode 100644 index 0000000000000..166a31a40c056 --- /dev/null +++ b/apps/web/src/components/settings/SettingsScopeSelects.logic.ts @@ -0,0 +1,55 @@ +import type { EnvironmentPresentation } from "../../state/environments"; +import type { SettingsScopeSearch } from "./settingsScope"; + +type ScopeEnvironment = Pick; + +export function settingsScopeEnvironmentLabel( + environment: ScopeEnvironment, + environments: readonly ScopeEnvironment[], +) { + const duplicate = environments.some( + (other) => + other.environmentId !== environment.environmentId && other.label === environment.label, + ); + return duplicate + ? `${environment.label} · ${environment.displayUrl ?? environment.environmentId}` + : environment.label; +} + +export const ALL_ENVIRONMENTS_VALUE = "all"; +export const ALL_PROJECTS_VALUE = "all"; + +/** + * The environment axis: `all` or an environment id. A legacy checkout link + * without `machine` still names one environment, which the resolver supplies. + */ +export function environmentAxisValue( + search: SettingsScopeSearch, + resolvedEnvironmentId?: string | null, +): string { + return search.machine ?? resolvedEnvironmentId ?? ALL_ENVIRONMENTS_VALUE; +} + +/** The project axis: `all` or a project key. */ +export function projectAxisValue(search: SettingsScopeSearch): string { + return search.project ?? ALL_PROJECTS_VALUE; +} + +/** Choosing an environment keeps the project; a pre-existing checkout narrowing is dropped. */ +export function selectEnvironmentAxis( + search: SettingsScopeSearch, + value: string, +): SettingsScopeSearch { + const next: SettingsScopeSearch = {}; + if (search.project) next.project = search.project; + if (value !== ALL_ENVIRONMENTS_VALUE) next.machine = value; + return next; +} + +/** Choosing a project keeps the environment axis. */ +export function selectProjectAxis(search: SettingsScopeSearch, value: string): SettingsScopeSearch { + const next: SettingsScopeSearch = {}; + if (value !== ALL_PROJECTS_VALUE) next.project = value; + if (search.machine) next.machine = search.machine; + return next; +} diff --git a/apps/web/src/components/settings/SettingsScopeSelects.tsx b/apps/web/src/components/settings/SettingsScopeSelects.tsx new file mode 100644 index 0000000000000..6271a7313c29a --- /dev/null +++ b/apps/web/src/components/settings/SettingsScopeSelects.tsx @@ -0,0 +1,154 @@ +import { resolveEnvironmentMachineKind } from "@t3tools/contracts"; +import { LayersIcon } from "lucide-react"; + +import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping"; +import type { EnvironmentPresentation } from "../../state/environments"; +import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; +import { ProjectFavicon } from "../ProjectFavicon"; +import { + Select, + SelectGroup, + SelectGroupLabel, + SelectItem, + SelectPopup, + SelectTrigger, + SelectValue, +} from "../ui/select"; +import { resolveSettingsScope, type SettingsScopeSearch } from "./settingsScope"; +import { + ALL_ENVIRONMENTS_VALUE, + ALL_PROJECTS_VALUE, + environmentAxisValue, + projectAxisValue, + selectEnvironmentAxis, + selectProjectAxis, + settingsScopeEnvironmentLabel, +} from "./SettingsScopeSelects.logic"; + +const TRIGGER_CLASSNAME = "w-auto min-w-0 max-w-56 justify-start"; + +/** + * Two independent targets: which environments a change applies to, and + * which project it overrides. A project is the same project on every + * environment; the environment select alone decides where the override is + * written. Each axis defaults to "all". + */ +export function SettingsScopeSelects({ + value, + groups, + environments, + onChange, +}: { + value: SettingsScopeSearch; + groups: readonly SidebarProjectSnapshot[]; + environments: readonly EnvironmentPresentation[]; + onChange: (next: SettingsScopeSearch) => void; +}) { + const resolved = resolveSettingsScope(value, groups, environments); + const environmentValue = environmentAxisValue( + value, + resolved.kind === "checkout" ? resolved.environmentId : null, + ); + const projectValue = projectAxisValue(value); + const selectedEnvironment = environments.find( + (environment) => environment.environmentId === environmentValue, + ); + const selectedGroup = groups.find((group) => group.projectKey === value.project); + + return ( +
+ + + + {resolved.kind === "unavailable" ? ( + {resolved.message} + ) : null} +
+ ); +} diff --git a/apps/web/src/components/settings/settingsScope.test.ts b/apps/web/src/components/settings/settingsScope.test.ts new file mode 100644 index 0000000000000..166e7121f57dc --- /dev/null +++ b/apps/web/src/components/settings/settingsScope.test.ts @@ -0,0 +1,216 @@ +import { EnvironmentId, ProjectId } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import type { + SidebarProjectGroupMember, + SidebarProjectSnapshot, +} from "../../sidebarProjectGrouping"; +import { resolveSettingsScope, validateSettingsScopeSearch } from "./settingsScope"; + +const laptopId = EnvironmentId.make("laptop"); +const serverId = EnvironmentId.make("server"); +const environments = [ + { environmentId: laptopId, label: "Laptop" }, + { environmentId: serverId, label: "Server" }, +]; + +function member(id: string, environmentId: EnvironmentId): SidebarProjectGroupMember { + return { + id: ProjectId.make(id), + environmentId, + title: "T3 Code", + workspaceRoot: `/repos/${id}`, + physicalProjectKey: `${environmentId}:/repos/${id}`, + environmentLabel: + environments.find((environment) => environment.environmentId === environmentId)?.label ?? + null, + defaultModelSelection: null, + scripts: [], + createdAt: "2026-09-07T00:00:00.000Z", + updatedAt: "2026-09-07T00:00:00.000Z", + }; +} + +const first = member("first", laptopId); +const second = member("second", laptopId); +const third = member("third", serverId); +const other = member("other", serverId); + +function group( + projectKey: string, + members: readonly SidebarProjectGroupMember[], +): SidebarProjectSnapshot { + return { + ...members[0]!, + projectKey, + displayName: projectKey, + memberProjects: members, + memberProjectRefs: members.map((project) => ({ + environmentId: project.environmentId, + projectId: project.id, + })), + groupedProjectCount: members.length, + environmentPresence: "mixed", + allRemoteMembersAreDesktopLocal: false, + allRemoteMembersAreWsl: false, + remoteEnvironmentLabels: [], + }; +} + +const groups = [group("t3code", [first, second, third]), group("other", [other])]; + +describe("settings scope search", () => { + it.each(["device", "all"] as const)( + "clears narrower targets for an explicit %s selection", + (scope) => { + expect( + validateSettingsScopeSearch({ + scope, + project: "t3code", + machine: laptopId, + checkout: first.physicalProjectKey, + }), + ).toEqual({ scope }); + }, + ); + + it("retains legacy project and machine links without inventing an explicit broad scope", () => { + expect( + validateSettingsScopeSearch({ project: "t3code", machine: laptopId, unused: true }), + ).toEqual({ + project: "t3code", + machine: laptopId, + }); + }); + + it("retains an orphan checkout so it cannot turn into all environments", () => { + const search = validateSettingsScopeSearch({ checkout: first.physicalProjectKey }); + expect(search).toEqual({ checkout: first.physicalProjectKey }); + expect(resolveSettingsScope(search, groups, environments)).toMatchObject({ + kind: "unavailable", + reason: "project-required", + members: [], + environmentIds: [], + }); + }); +}); + +describe("settings scope resolution", () => { + it("distinguishes this device from the default aggregate target", () => { + expect(resolveSettingsScope({}, groups, environments)).toMatchObject({ + kind: "all", + environmentIds: [laptopId, serverId], + }); + expect(resolveSettingsScope({ scope: "device" }, groups, environments)).toMatchObject({ + kind: "device", + members: [], + environmentIds: [], + }); + }); + + it("resolves one environment without targeting its project overrides", () => { + expect(resolveSettingsScope({ machine: serverId }, groups, environments)).toMatchObject({ + kind: "environment", + environmentId: serverId, + environmentIds: [serverId], + members: [], + }); + }); + + it("keeps all physical members in a project aggregate, including several on one environment", () => { + expect(resolveSettingsScope({ project: "t3code" }, groups, environments)).toMatchObject({ + kind: "project", + environmentId: null, + members: [first, second, third], + environmentIds: [laptopId, serverId], + }); + }); + + it("preserves legacy project plus machine aggregates with multiple checkouts", () => { + expect( + resolveSettingsScope({ project: "t3code", machine: laptopId }, groups, environments), + ).toMatchObject({ + kind: "project", + environmentId: laptopId, + label: "t3code / Laptop", + members: [first, second], + environmentIds: [laptopId], + }); + }); + + it("narrows a checkout target to exactly one member, deriving its environment when omitted", () => { + expect( + resolveSettingsScope( + { project: "t3code", checkout: second.physicalProjectKey }, + groups, + environments, + ), + ).toMatchObject({ + kind: "checkout", + checkout: second, + environmentId: laptopId, + label: "t3code / Laptop · /repos/second", + members: [second], + environmentIds: [laptopId], + }); + }); + + it.each([ + { project: "missing" }, + { machine: "removed" }, + { project: "t3code", machine: "removed" }, + { project: "t3code", checkout: "deleted" }, + { project: "other", machine: laptopId }, + { project: "other", checkout: first.physicalProjectKey }, + { project: "t3code", machine: serverId, checkout: first.physicalProjectKey }, + ])("never widens an invalid or stale target: %j", (search) => { + expect(resolveSettingsScope(search, groups, environments)).toMatchObject({ + kind: "unavailable", + members: [], + environmentIds: [], + }); + }); + + it("leaves a removed checkout unavailable while sibling checkouts remain", () => { + const search = { + project: "t3code", + machine: laptopId, + checkout: first.physicalProjectKey, + }; + expect(resolveSettingsScope(search, groups, environments)).toMatchObject({ + kind: "checkout", + members: [first], + }); + expect( + resolveSettingsScope(search, [group("t3code", [second, third])], environments), + ).toMatchObject({ kind: "unavailable", members: [], environmentIds: [] }); + }); + + it("does not select another environment after removing a project's last local checkout", () => { + const search = { project: "t3code", machine: laptopId }; + expect(resolveSettingsScope(search, groups, environments)).toMatchObject({ + kind: "project", + members: [first, second], + }); + expect(resolveSettingsScope(search, [group("t3code", [third])], environments)).toMatchObject({ + kind: "unavailable", + members: [], + environmentIds: [], + }); + }); + + it("rejects a cached checkout whose environment was removed", () => { + expect( + resolveSettingsScope( + { project: "t3code", checkout: third.physicalProjectKey }, + groups, + environments.slice(0, 1), + ), + ).toMatchObject({ + kind: "unavailable", + reason: "environment-missing", + members: [], + environmentIds: [], + }); + }); +}); diff --git a/apps/web/src/components/settings/settingsScope.ts b/apps/web/src/components/settings/settingsScope.ts new file mode 100644 index 0000000000000..3d860d088520f --- /dev/null +++ b/apps/web/src/components/settings/settingsScope.ts @@ -0,0 +1,163 @@ +import type { EnvironmentId } from "@t3tools/contracts"; + +import type { + SidebarProjectGroupMember, + SidebarProjectSnapshot, +} from "../../sidebarProjectGrouping"; +import type { EnvironmentPresentation } from "../../state/environments"; + +export interface SettingsScopeSearch { + scope?: "device" | "all" | undefined; + project?: string | undefined; + machine?: string | undefined; + checkout?: string | undefined; +} + +type ScopeTargets = { + label: string; + members: readonly SidebarProjectGroupMember[]; + environmentIds: readonly EnvironmentId[]; +}; + +export type ResolvedSettingsScope = ScopeTargets & + ( + | { kind: "device" | "all" } + | { kind: "environment"; environmentId: EnvironmentId } + | { + kind: "project"; + group: SidebarProjectSnapshot; + environmentId: EnvironmentId | null; + } + | { + kind: "checkout"; + group: SidebarProjectSnapshot; + checkout: SidebarProjectGroupMember; + environmentId: EnvironmentId; + } + | { + kind: "unavailable"; + reason: "project-required" | "project-missing" | "environment-missing" | "checkout-missing"; + message: string; + } + ); + +/** Explicit broad targets replace narrower selections; stale IDs remain visible to the resolver. */ +export function validateSettingsScopeSearch(raw: Record): SettingsScopeSearch { + if (raw.scope === "device" || raw.scope === "all") return { scope: raw.scope }; + const stringValue = (value: unknown) => + typeof value === "string" && value.trim().length > 0 ? value : undefined; + const project = stringValue(raw.project); + const machine = stringValue(raw.machine); + const checkout = stringValue(raw.checkout); + return { + ...(project === undefined ? {} : { project }), + ...(machine === undefined ? {} : { machine }), + ...(checkout === undefined ? {} : { checkout }), + }; +} + +/** Resolves only existing targets. An unavailable selection never broadens a subsequent write. */ +export function resolveSettingsScope( + search: SettingsScopeSearch, + groups: readonly SidebarProjectSnapshot[], + environments: readonly Pick[], +): ResolvedSettingsScope { + const unavailable = ( + reason: Extract["reason"], + message: string, + ): ResolvedSettingsScope => ({ + kind: "unavailable", + reason, + label: "Unavailable selection", + message, + members: [], + environmentIds: [], + }); + + if (search.scope === "device") { + return { kind: "device", label: "This device", members: [], environmentIds: [] }; + } + if (search.scope === "all") { + return { + kind: "all", + label: "All environments", + members: [], + environmentIds: environments.map((environment) => environment.environmentId), + }; + } + if (search.checkout && !search.project) { + return unavailable("project-required", "Select a project to choose one of its checkouts."); + } + + const environment = environments.find((candidate) => candidate.environmentId === search.machine); + if (search.machine && !environment) { + return unavailable("environment-missing", "This environment is no longer available."); + } + + if (search.project) { + const group = groups.find((candidate) => candidate.projectKey === search.project); + if (!group) return unavailable("project-missing", "This project is no longer available."); + const members = group.memberProjects.filter( + (member) => + (search.machine === undefined || member.environmentId === search.machine) && + (search.checkout === undefined || member.physicalProjectKey === search.checkout), + ); + if (members.length === 0) { + return unavailable( + "checkout-missing", + search.checkout + ? "This checkout is no longer available in the selected project and environment." + : "This project has no checkout on this environment.", + ); + } + if (search.checkout) { + const checkout = members[0]!; + const checkoutEnvironment = environments.find( + (candidate) => candidate.environmentId === checkout.environmentId, + ); + if (!checkoutEnvironment) { + return unavailable( + "environment-missing", + "This checkout's environment is no longer available.", + ); + } + const sharesEnvironment = group.memberProjects.some( + (member) => + member.environmentId === checkout.environmentId && + member.physicalProjectKey !== checkout.physicalProjectKey, + ); + return { + kind: "checkout", + group, + checkout, + environmentId: checkout.environmentId, + label: `${group.displayName} / ${checkoutEnvironment.label}${sharesEnvironment ? ` · ${checkout.workspaceRoot}` : ""}`, + members, + environmentIds: [checkout.environmentId], + }; + } + return { + kind: "project", + group, + environmentId: environment?.environmentId ?? null, + label: `${group.displayName} / ${environment?.label ?? "All checkouts"}`, + members, + environmentIds: [...new Set(members.map((member) => member.environmentId))], + }; + } + if (environment) { + return { + kind: "environment", + environmentId: environment.environmentId, + label: environment.label, + members: [], + environmentIds: [environment.environmentId], + }; + } + return { + kind: "all", + label: "All environments", + members: [], + environmentIds: environments.map((candidate) => candidate.environmentId), + }; +} diff --git a/apps/web/src/components/settings/useSettingsProjectGroups.ts b/apps/web/src/components/settings/useSettingsProjectGroups.ts new file mode 100644 index 0000000000000..8eb25149939ef --- /dev/null +++ b/apps/web/src/components/settings/useSettingsProjectGroups.ts @@ -0,0 +1,24 @@ +import { useMemo } from "react"; + +import { useClientSettings } from "../../hooks/useSettings"; +import { selectProjectGroupingSettings } from "../../logicalProject"; +import { buildSidebarProjectSnapshots } from "../../sidebarProjectGrouping"; +import { useEnvironments, usePrimaryEnvironmentId } from "../../state/environments"; +import { useProjects } from "../../state/entities"; + +/** Settings uses the same logical projects as the sidebar, sorted by display name. */ +export function useSettingsProjectGroups() { + const projects = useProjects(); + const settings = useClientSettings(selectProjectGroupingSettings); + const primaryEnvironmentId = usePrimaryEnvironmentId(); + const { environments } = useEnvironments(); + return useMemo(() => { + const labels = new Map(environments.map((entry) => [entry.environmentId, entry.label])); + return buildSidebarProjectSnapshots({ + projects, + settings, + primaryEnvironmentId, + resolveEnvironmentLabel: (id) => labels.get(id) ?? null, + }).sort((a, b) => a.displayName.localeCompare(b.displayName)); + }, [environments, primaryEnvironmentId, projects, settings]); +} diff --git a/apps/web/src/routes/settings.projects.tsx b/apps/web/src/routes/settings.projects.tsx index fa79f46fbb2c2..36b20d1560feb 100644 --- a/apps/web/src/routes/settings.projects.tsx +++ b/apps/web/src/routes/settings.projects.tsx @@ -1,24 +1,21 @@ import { createFileRoute } from "@tanstack/react-router"; import { ProjectsSettings } from "../components/settings/ProjectsSettings"; +import { validateSettingsScopeSearch } from "../components/settings/settingsScope"; export const Route = createFileRoute("/settings/projects")({ - validateSearch: (search: Record) => ({ - project: typeof search.project === "string" ? search.project : undefined, - machine: typeof search.machine === "string" ? search.machine : undefined, - }), + validateSearch: validateSettingsScopeSearch, component: ProjectsRoute, }); function ProjectsRoute() { - const { project, machine } = Route.useSearch(); + const search = Route.useSearch(); const navigate = Route.useNavigate(); return ( { + value={search} + onScopeChange={(scope) => { void navigate({ - search: { project: project ?? undefined, machine: machine ?? undefined }, + search: scope, replace: true, }); }} From e22040dfc190d04573238c5db2e69df72e5f1017 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 11 Sep 2026 13:56:40 -0700 Subject: [PATCH 30/63] feat(settings): edit any scopable setting as a project override (#10639) --- .../features/settings/SettingsRouteScreen.tsx | 52 +- .../settings/autoSettleSettingsSync.test.ts | 78 ++ .../settings/autoSettleSettingsSync.ts | 31 + .../threads/new-task-flow-provider.tsx | 31 +- apps/web/src/components/ChatView.tsx | 64 +- apps/web/src/components/CommandPalette.tsx | 5 +- .../src/components/chat/DraftHeroHeadline.tsx | 13 +- .../chat/ProviderModelPicker.test.tsx | 15 + .../components/chat/ProviderModelPicker.tsx | 10 +- .../components/projectScriptEditor.test.tsx | 245 ++++ .../src/components/projectScriptEditor.tsx | 283 +++-- .../pullRequest/PullRequestDetailPanel.tsx | 27 +- .../settings/ConnectionsSettings.tsx | 5 +- .../settings/DiagnosticsSettings.tsx | 48 +- .../settings/IntegrationsSettings.test.tsx | 12 + .../settings/IntegrationsSettings.tsx | 75 +- .../settings/KeybindingsSettings.tsx | 57 +- .../settings/ProjectActionsSettings.tsx | 226 ++++ .../ProjectDefaultActionsSettings.tsx | 114 -- .../settings/ProjectDefaultsSettings.tsx | 709 +++++------- .../settings/ProjectSettingsPanel.tsx | 1031 +---------------- .../components/settings/ProjectsSettings.tsx | 40 +- .../settings/ProviderInstanceCard.tsx | 4 + ...ProviderSettingsPanel.environment.test.tsx | 33 +- .../settings/ProviderSettingsPanel.tsx | 31 +- .../settings/ResourceTelemetryDiagnostics.tsx | 41 +- .../src/components/settings/ScopedSwitch.tsx | 19 + .../settings/SettingInheritance.test.ts | 57 + .../settings/SettingInheritance.tsx | 303 +++++ .../settings/SettingsBreadcrumb.tsx | 202 +++- .../components/settings/SettingsPanels.tsx | 272 +++-- .../settings/SettingsScopeContext.tsx | 65 ++ .../settings/SettingsScopeNotice.tsx | 90 ++ .../settings/SettingsScopeSelects.tsx | 154 --- .../settings/SettingsSidebarNav.tsx | 17 +- .../settings/SharedSettingsMismatchAlert.tsx | 32 - .../settings/SnapShotSettings.test.tsx | 1 + .../settings/SourceControlSettings.tsx | 54 +- .../SourceControlWritingSettings.test.tsx | 221 ++++ .../settings/SourceControlWritingSettings.tsx | 232 +++- .../settings/scopedSettings.test.ts | 450 +++++++ .../src/components/settings/scopedSettings.ts | 361 ++++++ .../components/settings/settingsLayout.tsx | 190 ++- .../components/settings/settingsScope.test.ts | 27 +- .../src/components/settings/settingsScope.ts | 23 +- ...ogic.test.ts => settingsScopeAxis.test.ts} | 2 +- ...eSelects.logic.ts => settingsScopeAxis.ts} | 0 .../settings/settingsScopeNavigation.test.ts | 247 ++++ .../settings/settingsScopeNavigation.ts | 32 + .../settings/settingsSearch.test.ts | 199 +++- .../src/components/settings/settingsSearch.ts | 201 +++- .../useAvailableSettingsSearchItems.ts | 24 +- .../settings/useProjectScriptSettings.ts | 193 +++ .../settings/useScopedModelAvailability.ts | 55 + .../components/settings/useScopedSettings.ts | 121 ++ apps/web/src/components/ui/button.tsx | 2 + apps/web/src/components/ui/switch.tsx | 14 +- apps/web/src/hooks/useHandleNewThread.test.ts | 125 +- apps/web/src/hooks/useHandleNewThread.ts | 37 +- apps/web/src/hooks/useSettings.ts | 69 -- apps/web/src/lib/resourceTelemetryState.ts | 25 +- apps/web/src/routes/settings.integrations.tsx | 6 +- apps/web/src/routes/settings.projects.tsx | 20 +- apps/web/src/routes/settings.providers.tsx | 24 +- apps/web/src/routes/settings.tsx | 155 ++- apps/web/src/state/server.ts | 9 - docs/internals/overview.md | 9 + docs/user/project-settings.md | 62 +- docs/user/thread-sidebar.md | 10 +- 69 files changed, 5158 insertions(+), 2533 deletions(-) create mode 100644 apps/mobile/src/features/settings/autoSettleSettingsSync.test.ts create mode 100644 apps/mobile/src/features/settings/autoSettleSettingsSync.ts create mode 100644 apps/web/src/components/projectScriptEditor.test.tsx create mode 100644 apps/web/src/components/settings/ProjectActionsSettings.tsx delete mode 100644 apps/web/src/components/settings/ProjectDefaultActionsSettings.tsx create mode 100644 apps/web/src/components/settings/ScopedSwitch.tsx create mode 100644 apps/web/src/components/settings/SettingInheritance.test.ts create mode 100644 apps/web/src/components/settings/SettingInheritance.tsx create mode 100644 apps/web/src/components/settings/SettingsScopeContext.tsx create mode 100644 apps/web/src/components/settings/SettingsScopeNotice.tsx delete mode 100644 apps/web/src/components/settings/SettingsScopeSelects.tsx delete mode 100644 apps/web/src/components/settings/SharedSettingsMismatchAlert.tsx create mode 100644 apps/web/src/components/settings/SourceControlWritingSettings.test.tsx create mode 100644 apps/web/src/components/settings/scopedSettings.test.ts create mode 100644 apps/web/src/components/settings/scopedSettings.ts rename apps/web/src/components/settings/{SettingsScopeSelects.logic.test.ts => settingsScopeAxis.test.ts} (98%) rename apps/web/src/components/settings/{SettingsScopeSelects.logic.ts => settingsScopeAxis.ts} (100%) create mode 100644 apps/web/src/components/settings/settingsScopeNavigation.test.ts create mode 100644 apps/web/src/components/settings/settingsScopeNavigation.ts create mode 100644 apps/web/src/components/settings/useProjectScriptSettings.ts create mode 100644 apps/web/src/components/settings/useScopedModelAvailability.ts create mode 100644 apps/web/src/components/settings/useScopedSettings.ts diff --git a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx index 77036c2125170..d57904b7e7a2c 100644 --- a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx @@ -41,14 +41,8 @@ import { DEFAULT_SERVER_SETTINGS, MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS, MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS, - type ServerSettingsPatch, } from "@t3tools/contracts"; -import { - filterSharedServerPatch, - findSharedSettingsMismatches, - pickSharedServerSettings, - supportsSharedSettingsSync, -} from "@t3tools/client-runtime/state/shared-settings"; +import { supportsSharedSettingsSync } from "@t3tools/client-runtime/state/shared-settings"; import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled"; import { type AppUpdateCheckState, @@ -61,6 +55,7 @@ import { SettingsRow } from "./components/SettingsRow"; import { SettingsSection } from "./components/SettingsSection"; import { SettingsSwitchRow } from "./components/SettingsSwitchRow"; import { resolveAgentAwarenessPlatformPresentation } from "./SettingsRouteScreen.logic"; +import { planAutoSettleSettingsSync, type AutoSettleSettings } from "./autoSettleSettingsSync"; type NotificationStatus = "checking" | "enabled" | "disabled" | "unsupported"; type LiveActivityStatus = "checking" | "enabled" | "disabled" | "signed-out" | "linking"; @@ -588,10 +583,9 @@ function GeneralSettingsSection() { const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterDays ?? 3; /** - * Auto-settlement is a user preference that every server has to hold. Mobile - * has no primary environment, so the first eligible sync target provides the - * reference value. Edits fan out to every eligible target, and a mismatch row - * lets the user push the reference out. + * Mobile edits auto-settle defaults across connected, capable environments. + * The first target supplies the displayed values. Applying them leaves each + * environment's other defaults and overrides intact. */ function AutoSettleSettingsRows() { const { environments } = useEnvironments(); @@ -610,24 +604,20 @@ function AutoSettleSettingsRows() { return null; } - const writeToAll = (patch: ServerSettingsPatch) => { + const writeToAll = (patch: Partial) => { for (const environment of syncTargets) { void updateSettings({ environmentId: environment.environmentId, input: { patch } }); } }; - const mismatches = findSharedSettingsMismatches({ - primaryEnvironmentId: reference.environmentId, - primarySettings: referenceSettings, - primaryCapabilities: reference.serverConfig?.environment.capabilities, - environments: environments.map((environment) => ({ + const { patch: autoSettlePatch, mismatches } = planAutoSettleSettingsSync( + { environmentId: reference.environmentId, settings: referenceSettings }, + syncTargets.map((environment) => ({ environmentId: environment.environmentId, label: environment.label, - syncEligible: supportsSharedSettingsSync(environment), settings: environment.serverConfig?.settings ?? null, - capabilities: environment.serverConfig?.environment.capabilities, })), - }); + ); const afterDays = referenceSettings.sidebarAutoSettleAfterDays; const commitDays = () => { @@ -681,7 +671,7 @@ function AutoSettleSettingsRows() { {mismatches.length > 0 ? ( - Settings differ + Auto-settle defaults differ {mismatches.map((mismatch) => mismatch.label).join(", ")} @@ -689,30 +679,18 @@ function AutoSettleSettingsRows() { { - const patch = pickSharedServerSettings( - referenceSettings, - reference.serverConfig?.environment.capabilities, - ); for (const mismatch of mismatches) { - const target = environments.find( - (candidate) => candidate.environmentId === mismatch.environmentId, - ); void updateSettings({ environmentId: mismatch.environmentId, - input: { - patch: filterSharedServerPatch( - patch, - target?.serverConfig?.environment.capabilities, - target?.serverConfig?.settings, - referenceSettings, - ), - }, + input: { patch: autoSettlePatch }, }); } }} className="rounded-full bg-subtle px-4 py-2 active:opacity-70" > - Apply to all + + Apply auto-settle defaults + ) : null} diff --git a/apps/mobile/src/features/settings/autoSettleSettingsSync.test.ts b/apps/mobile/src/features/settings/autoSettleSettingsSync.test.ts new file mode 100644 index 0000000000000..ec550725adcf9 --- /dev/null +++ b/apps/mobile/src/features/settings/autoSettleSettingsSync.test.ts @@ -0,0 +1,78 @@ +import { DEFAULT_SERVER_SETTINGS, EnvironmentId } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { planAutoSettleSettingsSync } from "./autoSettleSettingsSync"; + +const reference = { + environmentId: EnvironmentId.make("reference"), + settings: { + ...DEFAULT_SERVER_SETTINGS, + sidebarAutoSettleAfterDays: 7, + sidebarAutoSettleOnMerge: true, + newWorktreesStartFromOrigin: false, + continueThreadsAfterServerUpdate: false, + }, +}; + +describe("auto-settle settings sync", () => { + it("ignores differences in independently configured environment settings", () => { + const target = { + environmentId: EnvironmentId.make("remote"), + label: "Remote", + settings: { + ...reference.settings, + newWorktreesStartFromOrigin: true, + continueThreadsAfterServerUpdate: true, + sourceControlWritingStyle: { + ...reference.settings.sourceControlWritingStyle, + customInstructions: "Keep this environment's writing instructions.", + }, + }, + }; + + const plan = planAutoSettleSettingsSync(reference, [target]); + + expect(plan.mismatches).toEqual([]); + expect(plan.patch).toEqual({ + sidebarAutoSettleAfterDays: 7, + sidebarAutoSettleOnMerge: true, + }); + }); + + it("applies only auto-settle defaults when another environment differs", () => { + const target = { + environmentId: EnvironmentId.make("remote"), + label: "Remote", + settings: { + ...reference.settings, + sidebarAutoSettleAfterDays: null, + sidebarAutoSettleOnMerge: false, + newWorktreesStartFromOrigin: true, + continueThreadsAfterServerUpdate: true, + sourceControlWritingStyle: { + ...reference.settings.sourceControlWritingStyle, + customInstructions: "Preserve these instructions.", + }, + }, + }; + + const plan = planAutoSettleSettingsSync(reference, [target]); + const updated = { ...target.settings, ...plan.patch }; + + expect(plan.mismatches).toEqual([target]); + expect(updated.sidebarAutoSettleAfterDays).toBe(7); + expect(updated.sidebarAutoSettleOnMerge).toBe(true); + expect(updated.newWorktreesStartFromOrigin).toBe(true); + expect(updated.continueThreadsAfterServerUpdate).toBe(true); + expect(updated.sourceControlWritingStyle).toEqual(target.settings.sourceControlWritingStyle); + }); + + it("does not compare the reference or a target without loaded settings", () => { + const plan = planAutoSettleSettingsSync(reference, [ + { ...reference, label: "Reference" }, + { environmentId: EnvironmentId.make("loading"), label: "Loading", settings: null }, + ]); + + expect(plan.mismatches).toEqual([]); + }); +}); diff --git a/apps/mobile/src/features/settings/autoSettleSettingsSync.ts b/apps/mobile/src/features/settings/autoSettleSettingsSync.ts new file mode 100644 index 0000000000000..6addfa381fded --- /dev/null +++ b/apps/mobile/src/features/settings/autoSettleSettingsSync.ts @@ -0,0 +1,31 @@ +import type { EnvironmentId, ServerSettings } from "@t3tools/contracts"; + +export type AutoSettleSettings = Pick< + ServerSettings, + "sidebarAutoSettleAfterDays" | "sidebarAutoSettleOnMerge" +>; + +interface AutoSettleSyncTarget { + readonly environmentId: EnvironmentId; + readonly label: string; + readonly settings: AutoSettleSettings | null; +} + +/** Receives connected, capable targets. Applying these defaults must preserve other settings. */ +export function planAutoSettleSettingsSync( + reference: { readonly environmentId: EnvironmentId; readonly settings: AutoSettleSettings }, + targets: readonly AutoSettleSyncTarget[], +) { + const patch: AutoSettleSettings = { + sidebarAutoSettleAfterDays: reference.settings.sidebarAutoSettleAfterDays, + sidebarAutoSettleOnMerge: reference.settings.sidebarAutoSettleOnMerge, + }; + const mismatches = targets.filter( + (target) => + target.environmentId !== reference.environmentId && + target.settings !== null && + (target.settings.sidebarAutoSettleAfterDays !== patch.sidebarAutoSettleAfterDays || + target.settings.sidebarAutoSettleOnMerge !== patch.sidebarAutoSettleOnMerge), + ); + return { patch, mismatches }; +} diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.tsx index 535581b58e756..d0687e7e7b66b 100644 --- a/apps/mobile/src/features/threads/new-task-flow-provider.tsx +++ b/apps/mobile/src/features/threads/new-task-flow-provider.tsx @@ -13,10 +13,12 @@ import { CommandId, DEFAULT_PROVIDER_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, + DEFAULT_SERVER_SETTINGS, MessageId, T3_PROJECT_FILE_NAME, ThreadId, } from "@t3tools/contracts"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile"; import { isDefaultThreadEnvModeSettled, @@ -428,17 +430,32 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { if (t3ProjectFileData === null || t3ProjectFileData.truncated) return null; return parseT3ProjectFile(t3ProjectFileData.contents)?.defaultThreadEnvMode ?? null; }, [t3ProjectFileData]); + // Environment settings with the project's overrides applied; the + // aggregate's own legacy fields still count until the server folds them. + const projectSettings = useMemo( + () => + resolveProjectSettings( + selectedEnvironmentServerConfig?.settings ?? DEFAULT_SERVER_SETTINGS, + selectedProject?.id ?? null, + selectedProject, + ), + [selectedEnvironmentServerConfig?.settings, selectedProject], + ); + const projectThreadEnvMode = + projectSettings.sources.defaultThreadEnvMode === "project" + ? projectSettings.settings.defaultThreadEnvMode + : undefined; const defaultWorkspaceMode: WorkspaceMode = resolveDefaultThreadEnvMode({ - projectSetting: selectedProject?.defaultThreadEnvMode, + projectSetting: projectThreadEnvMode, projectFile: t3ProjectFileDefaultMode, - globalDefault: selectedEnvironmentServerConfig?.settings.defaultThreadEnvMode ?? "local", + globalDefault: projectSettings.settings.defaultThreadEnvMode, }); // While unsettled the resolved default is provisional. Nothing may write // it into the draft during that window (the auto-branch effect does), or // the frozen interim value beats the t3.json default once it loads. const defaultWorkspaceModeSettled = isDefaultThreadEnvModeSettled({ explicitMode: selectedProjectDraft.workspaceSelection?.mode, - projectSetting: selectedProject?.defaultThreadEnvMode, + projectSetting: projectThreadEnvMode, projectFilePending: t3ProjectFileQuery.isPending, }); const workspaceMode = selectedProjectDraft.workspaceSelection?.mode ?? defaultWorkspaceMode; @@ -449,9 +466,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { // value keeps tracking the server setting when the config loads late. const draftStartFromOrigin = selectedProjectDraft.workspaceSelection?.startFromOrigin; const startFromOrigin = - draftStartFromOrigin ?? - selectedEnvironmentServerConfig?.settings.newWorktreesStartFromOrigin ?? - true; + draftStartFromOrigin ?? projectSettings.settings.newWorktreesStartFromOrigin; const runtimeMode = selectedProjectDraft.runtimeMode ?? DEFAULT_RUNTIME_MODE; // Antigravity keeps unavailable selections so sign-out or a catalog change @@ -463,9 +478,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { ); const projectDefaultModelSelection = resolveDefaultableModelSelection( selectedEnvironmentServerConfig, - selectedProject?.defaultModelSelection ?? - selectedEnvironmentServerConfig?.settings.defaultModelSelection ?? - null, + projectSettings.settings.defaultModelSelection, ); const storedStickyModelSelection = useStickyComposerModelSelection(); const stickyModelSelection = resolveDefaultableModelSelection( diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 51b9c5eabc488..fc529b02a73b9 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -68,6 +68,7 @@ import { projectScriptRuntimeEnv, resolveProjectScripts, } from "@t3tools/shared/projectScripts"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import { truncate } from "@t3tools/shared/String"; import { resolveThreadReferenceCopyTarget } from "@t3tools/shared/threadReference"; import { @@ -304,7 +305,6 @@ import { environmentServerConfigsAtom, primaryServerAvailableEditorsAtom, primaryServerKeybindingsAtom, - primaryServerSettingsAtom, serverEnvironment, } from "../state/server"; import { terminalEnvironment } from "../state/terminal"; @@ -1516,7 +1516,6 @@ export default function ChatView(props: ChatViewProps) { }, [routeKind, routeThreadRef, routeThreadState]); const markThreadVisited = useUiStateStore((store) => store.markThreadVisited); const settings = useEnvironmentSettings(environmentId); - const primaryServerSettings = useAtomValue(primaryServerSettingsAtom); const setStickyComposerModelSelection = useComposerDraftStore( (store) => store.setStickyModelSelection, ); @@ -1802,17 +1801,14 @@ export default function ChatView(props: ChatViewProps) { ? buildLocalDraftThread( threadId, draftThread, - fallbackDraftProject?.defaultModelSelection ?? - settings.defaultModelSelection ?? - NO_PROVIDER_MODEL_SELECTION, + resolveProjectSettings( + settings, + fallbackDraftProject?.id ?? null, + fallbackDraftProject ?? undefined, + ).settings.defaultModelSelection ?? NO_PROVIDER_MODEL_SELECTION, ) : undefined, - [ - draftThread, - fallbackDraftProject?.defaultModelSelection, - settings.defaultModelSelection, - threadId, - ], + [draftThread, fallbackDraftProject, settings, threadId], ); // Promotion is data-driven: the draft route keeps rendering while the // server thread (same pre-allocated ref) starts, so live state must not @@ -2033,12 +2029,16 @@ export default function ChatView(props: ChatViewProps) { [activeThread?.environmentId, activeThread?.projectId], ); const activeProject = useProject(activeProjectRef); + // Environment settings with the active project's overrides applied. + const activeProjectSettings = useMemo( + () => resolveProjectSettings(settings, activeProject?.id ?? null, activeProject ?? undefined), + [activeProject, settings], + ); const activeProjectScripts = useMemo( () => (activeProject ? resolveProjectScripts(settings, activeProject) : []), [activeProject, settings], ); - const activeProjectDefaultModelSelection = - activeProject?.defaultModelSelection ?? settings.defaultModelSelection; + const activeProjectDefaultModelSelection = activeProjectSettings.settings.defaultModelSelection; const handleNewThreadInActiveProject = useCallback(() => { startNewThreadForProject(activeProjectRef, handleNewThread); }, [activeProjectRef, handleNewThread]); @@ -3945,6 +3945,9 @@ export default function ChatView(props: ChatViewProps) { ], ); + const supportsProjectSettingsOverrides = + environmentById.get(environmentId)?.serverConfig?.environment.capabilities + .projectSettingsOverrides === true; const persistProjectScripts = useCallback( async (input: { projectId: ProjectId; @@ -3958,11 +3961,22 @@ export default function ChatView(props: ChatViewProps) { await updateProjectScriptSettings({ environmentId, input: { - patch: { - projectScriptOverrides: { - [input.projectId]: input.nextScripts, - }, - }, + // The canonical key on servers that understand it; the legacy + // per-project map is still translated on older ones. + patch: supportsProjectSettingsOverrides + ? { + projectSettingsOverrides: { + [input.projectId]: { + ...settings.projectSettingsOverrides[input.projectId], + defaultProjectScripts: input.nextScripts, + }, + }, + } + : { + projectScriptOverrides: { + [input.projectId]: input.nextScripts, + }, + }, }, }), () => undefined, @@ -3987,7 +4001,13 @@ export default function ChatView(props: ChatViewProps) { } return updateResult; }, - [environmentId, updateProjectScriptSettings, upsertKeybinding], + [ + environmentId, + settings.projectSettingsOverrides, + supportsProjectSettingsOverrides, + updateProjectScriptSettings, + upsertKeybinding, + ], ); const saveProjectScript = useCallback( async (input: NewProjectScriptInput): Promise> => { @@ -5396,7 +5416,7 @@ export default function ChatView(props: ChatViewProps) { ? (draftThread?.startFromOrigin ?? false) : canOverrideServerThreadEnvMode ? (pendingServerThreadStartFromOriginByThreadId[activeThread?.id ?? ""] ?? - primaryServerSettings.newWorktreesStartFromOrigin) + activeProjectSettings.settings.newWorktreesStartFromOrigin) : false; const sendEnvMode = resolveSendEnvMode({ requestedEnvMode: envMode, @@ -8045,7 +8065,7 @@ export default function ChatView(props: ChatViewProps) { envMode: mode, startFromOrigin: resolveNewDraftStartFromOrigin({ envMode: mode, - newWorktreesStartFromOrigin: primaryServerSettings.newWorktreesStartFromOrigin, + newWorktreesStartFromOrigin: activeProjectSettings.settings.newWorktreesStartFromOrigin, }), ...(mode === "worktree" && draftThread?.worktreePath ? { worktreePath: null } : {}), }); @@ -8057,7 +8077,7 @@ export default function ChatView(props: ChatViewProps) { composerDraftTarget, draftThread?.worktreePath, isLocalDraftThread, - primaryServerSettings.newWorktreesStartFromOrigin, + activeProjectSettings.settings.newWorktreesStartFromOrigin, setPendingServerThreadEnvMode, scheduleComposerFocus, setDraftThreadContext, diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsx index 4a9877f87a027..3533678ecb2d4 100644 --- a/apps/web/src/components/CommandPalette.tsx +++ b/apps/web/src/components/CommandPalette.tsx @@ -1814,8 +1814,7 @@ function OpenCommandPaletteDialog(props: { }, }); - // There is no projects listing page; the action targets the contextual - // project (active thread/draft, falling back to the first sidebar group). + // Target the active thread or draft's project, falling back to the first sidebar group. const contextualProjectGroup = (contextualProjectRef ? projectGroupByTargetKey.get( @@ -1867,8 +1866,6 @@ function OpenCommandPaletteDialog(props: { run: async () => { await navigate({ to: item.to, - search: (previous) => - item.to === "/settings/projects" ? { ...previous, project: undefined } : previous, hash: item.targetId ?? item.id, replace: pathname === item.to, hashScrollIntoView: false, diff --git a/apps/web/src/components/chat/DraftHeroHeadline.tsx b/apps/web/src/components/chat/DraftHeroHeadline.tsx index 4a9421f2011fb..1e0d295c098fc 100644 --- a/apps/web/src/components/chat/DraftHeroHeadline.tsx +++ b/apps/web/src/components/chat/DraftHeroHeadline.tsx @@ -27,6 +27,7 @@ import { MenuTrigger, } from "../ui/menu"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; interface DraftHeroHeadlineProps { readonly draftId: DraftId | null; @@ -150,11 +151,13 @@ export function DraftHeroHeadline({ ); if (!hasExplicitComposerModelSelection(currentDraft)) { applyStickyState(draftId); - const defaultModelSelection = - project.defaultModelSelection ?? - environments.find( - (environment) => environment.environmentId === project.environmentId, - )?.serverConfig?.settings.defaultModelSelection; + const environmentSettings = environments.find( + (environment) => environment.environmentId === project.environmentId, + )?.serverConfig?.settings; + const defaultModelSelection = environmentSettings + ? resolveProjectSettings(environmentSettings, project.id, project).settings + .defaultModelSelection + : project.defaultModelSelection; if (defaultModelSelection) { setModelSelection(draftId, defaultModelSelection, { replaceOptions: true, diff --git a/apps/web/src/components/chat/ProviderModelPicker.test.tsx b/apps/web/src/components/chat/ProviderModelPicker.test.tsx index b1bb8ba9c74a7..41e86f841ee4d 100644 --- a/apps/web/src/components/chat/ProviderModelPicker.test.tsx +++ b/apps/web/src/components/chat/ProviderModelPicker.test.tsx @@ -34,6 +34,7 @@ function renderPicker(input: { model: string; options: ReadonlyArray; includeEntry?: boolean; + triggerLabel?: string; }) { const instanceId = ProviderInstanceId.make(input.instanceId); const entry = providerEntry(input.instanceId, input.driver); @@ -45,11 +46,25 @@ function renderPicker(input: { instanceEntries={input.includeEntry === false ? [] : [entry]} modelOptionsByInstance={new Map([[instanceId, input.options]])} onInstanceModelChange={() => {}} + {...(input.triggerLabel ? { triggerLabel: input.triggerLabel } : {})} />, ); } describe("ProviderModelPicker", () => { + it("shows a neutral aggregate value without a representative model or availability badge", () => { + const markup = renderPicker({ + instanceId: "codex_personal", + driver: "codex", + model: "gpt-5", + options: [{ slug: "gpt-5", name: "GPT 5", isUnavailable: true }], + triggerLabel: "Mixed values", + }); + expect(markup).toContain("Mixed values"); + expect(markup).not.toContain("GPT 5"); + expect(markup).not.toContain("Unavailable"); + }); + it.each(["", ANTIGRAVITY_DEFAULT_MODEL])( "shows a choice prompt before Antigravity has an account catalog for %s", (model) => { diff --git a/apps/web/src/components/chat/ProviderModelPicker.tsx b/apps/web/src/components/chat/ProviderModelPicker.tsx index fb2cdb2f12809..6f777399451d0 100644 --- a/apps/web/src/components/chat/ProviderModelPicker.tsx +++ b/apps/web/src/components/chat/ProviderModelPicker.tsx @@ -48,6 +48,8 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: { open?: boolean; triggerVariant?: VariantProps["variant"]; triggerClassName?: string; + /** Aggregate settings can show a neutral value without claiming one provider is selected. */ + triggerLabel?: string; triggerAriaLabel?: string; onOpenChange?: (open: boolean) => void; onOpenProviderSetup?: (instanceId: ProviderInstanceId) => void; @@ -181,7 +183,7 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: { - {activeEntry ? ( + {activeEntry && props.triggerLabel === undefined ? ( } > - {triggerTitle} + {props.triggerLabel ?? triggerTitle} - {triggerLabel} + {props.triggerLabel ?? triggerLabel} - {selectedModel?.isUnavailable ? ( + {selectedModel?.isUnavailable && props.triggerLabel === undefined ? ( Unavailable diff --git a/apps/web/src/components/projectScriptEditor.test.tsx b/apps/web/src/components/projectScriptEditor.test.tsx new file mode 100644 index 0000000000000..becb3a369f62d --- /dev/null +++ b/apps/web/src/components/projectScriptEditor.test.tsx @@ -0,0 +1,245 @@ +import * as Cause from "effect/Cause"; +import { AsyncResult } from "effect/unstable/reactivity"; +import { act, StrictMode, type ReactNode } from "react"; +import { create, type ReactTestRenderer } from "react-test-renderer"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; + +vi.mock("./ui/dialog", () => ({ + Dialog: ({ open, children }: { open: boolean; children: ReactNode }) => (open ? children : null), + DialogDescription: "p", + DialogFooter: "footer", + DialogHeader: "header", + DialogPanel: "section", + DialogPopup: "section", + DialogTitle: "h2", +})); +vi.mock("./ui/alert-dialog", () => ({ + AlertDialog: ({ open, children }: { open: boolean; children: ReactNode }) => + open ? children : null, + AlertDialogClose: "button", + AlertDialogDescription: "p", + AlertDialogFooter: "footer", + AlertDialogHeader: "header", + AlertDialogPopup: "section", + AlertDialogTitle: "h2", +})); +vi.mock("./ui/button", () => ({ Button: "button" })); +vi.mock("./ui/input", () => ({ Input: "input" })); +vi.mock("./ui/label", () => ({ Label: "label" })); +vi.mock("./ui/popover", () => ({ + Popover: ({ children }: { children: ReactNode }) => children, + PopoverPopup: () => null, + PopoverTrigger: "button", +})); +vi.mock("./ui/switch", () => ({ Switch: "input" })); +vi.mock("./ui/textarea", () => ({ Textarea: "textarea" })); + +import { + EMPTY_PROJECT_SCRIPT_INPUT, + ProjectScriptEditorDialog, + type ProjectScriptActionResult, + type ProjectScriptEditorRequest, +} from "./projectScriptEditor"; + +const onSubmit = vi.fn[0]["onSubmit"]>(); +const onClose = vi.fn(); +const onDelete = vi.fn(); +let renderer: ReactTestRenderer | null; + +function request(name: string, error?: string): ProjectScriptEditorRequest { + return { + scriptId: name, + initial: { ...EMPTY_PROJECT_SCRIPT_INPUT, name, command: `run-${name}` }, + ...(error === undefined ? {} : { error }), + }; +} + +function editor(nextRequest: ProjectScriptEditorRequest) { + return ( + + + + ); +} + +function open(nextRequest: ProjectScriptEditorRequest) { + act(() => { + if (renderer) renderer.update(editor(nextRequest)); + else renderer = create(editor(nextRequest)); + }); +} + +function submit(): Promise { + return renderer!.root.findByType("form").props.onSubmit({ preventDefault() {} }); +} + +function saveButton() { + return renderer!.root.findAllByType("button").find((button) => button.props.type === "submit")!; +} + +function deferredSave() { + let resolve!: (result: ProjectScriptActionResult) => void; + let reject!: (error: Error) => void; + const promise = new Promise((resolveResult, rejectResult) => { + resolve = resolveResult; + reject = rejectResult; + }); + return { promise, resolve, reject }; +} + +beforeEach(() => { + renderer = null; + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + onSubmit.mockReset(); + onClose.mockReset(); + onDelete.mockReset(); +}); + +afterEach(async () => { + await act(async () => renderer?.unmount()); + vi.unstubAllGlobals(); +}); + +describe("project action editor save lifecycle", () => { + it("blocks repeated submits and edits until the current save completes", async () => { + const save = deferredSave(); + onSubmit.mockReturnValue(save.promise); + open(request("build")); + + let completion!: Promise; + act(() => { + completion = submit(); + void submit(); + }); + + expect(onSubmit).toHaveBeenCalledTimes(1); + expect(saveButton().props.disabled).toBe(true); + expect(renderer!.root.findByType("fieldset").props.disabled).toBe(true); + const cancel = renderer!.root + .findAllByType("button") + .find((button) => button.children.includes("Cancel"))!; + expect(cancel.props.disabled).not.toBe(true); + + await act(async () => { + save.resolve(AsyncResult.success(undefined)); + await completion; + }); + expect(onClose).toHaveBeenCalledTimes(1); + }); + + it("does not close a replacement request or release its in-flight save", async () => { + const first = deferredSave(); + const second = deferredSave(); + onSubmit.mockReturnValueOnce(first.promise).mockReturnValueOnce(second.promise); + open(request("build")); + let firstCompletion!: Promise; + act(() => { + firstCompletion = submit(); + }); + + open(request("test")); + expect(saveButton().props.disabled).toBe(false); + expect(renderer!.root.findByProps({ id: "script-name" }).props.value).toBe("test"); + let secondCompletion!: Promise; + act(() => { + secondCompletion = submit(); + }); + + await act(async () => { + first.resolve(AsyncResult.success(undefined)); + await firstCompletion; + }); + expect(onClose).not.toHaveBeenCalled(); + expect(saveButton().props.disabled).toBe(true); + + await act(async () => { + second.resolve(AsyncResult.success(undefined)); + await secondCompletion; + }); + expect(onClose).toHaveBeenCalledTimes(1); + expect(onSubmit.mock.calls.map(([scriptId]) => scriptId)).toEqual(["build", "test"]); + }); + + it.each(["failure", "rejection"] as const)( + "ignores a stale %s after the request changes", + async (outcome) => { + const save = deferredSave(); + onSubmit.mockReturnValue(save.promise); + open(request("build")); + let completion!: Promise; + act(() => { + completion = submit(); + }); + + open(request("test", "New request error")); + await act(async () => { + if (outcome === "failure") + save.resolve(AsyncResult.failure(Cause.fail(new Error("Old save error")))); + else save.reject(new Error("Old save error")); + await completion; + }); + + const messages = renderer!.root.findAllByType("p").flatMap((paragraph) => paragraph.children); + expect(messages).toContain("New request error"); + expect(messages).not.toContain("Old save error"); + expect(saveButton().props.disabled).toBe(false); + expect(onClose).not.toHaveBeenCalled(); + }, + ); + + it("shows a current save error and allows retry", async () => { + onSubmit.mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("Save failed")))); + onSubmit.mockResolvedValueOnce(AsyncResult.success(undefined)); + open(request("build")); + + await act(async () => { + await submit(); + }); + expect(renderer!.root.findAllByType("p").flatMap((paragraph) => paragraph.children)).toContain( + "Save failed", + ); + expect(saveButton().props.disabled).toBe(false); + expect(renderer!.root.findByType("fieldset").props.disabled).toBe(false); + expect(onClose).not.toHaveBeenCalled(); + + await act(async () => { + await submit(); + }); + expect(onSubmit).toHaveBeenCalledTimes(2); + expect(onClose).toHaveBeenCalledTimes(1); + }); + + it.each(["cancel", "unmount"] as const)("ignores save completion after %s", async (exit) => { + const save = deferredSave(); + onSubmit.mockReturnValue(save.promise); + open(request("build")); + let completion!: Promise; + act(() => { + completion = submit(); + }); + + act(() => { + if (exit === "cancel") { + renderer!.root + .findAllByType("button") + .find((button) => button.children.includes("Cancel"))! + .props.onClick(); + } else { + renderer!.unmount(); + renderer = null; + } + }); + onClose.mockClear(); + await act(async () => { + save.resolve(AsyncResult.success(undefined)); + await completion; + }); + expect(onClose).not.toHaveBeenCalled(); + }); +}); diff --git a/apps/web/src/components/projectScriptEditor.tsx b/apps/web/src/components/projectScriptEditor.tsx index 4ffd453955e91..74b189a02fc56 100644 --- a/apps/web/src/components/projectScriptEditor.tsx +++ b/apps/web/src/components/projectScriptEditor.tsx @@ -16,7 +16,14 @@ import { PlayIcon, WrenchIcon, } from "lucide-react"; -import React, { type FormEvent, type KeyboardEvent, useEffect, useState } from "react"; +import React, { + type FormEvent, + type KeyboardEvent, + useEffect, + useLayoutEffect, + useRef, + useState, +} from "react"; import { keybindingValueForCommand, @@ -156,9 +163,22 @@ export function ProjectScriptEditorDialog({ const [autoOpenPreview, setAutoOpenPreview] = useState(false); const [validationError, setValidationError] = useState(null); const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false); + const [savingRequest, setSavingRequest] = useState(null); + const pendingSubmissionRef = useRef<{ request: ProjectScriptEditorRequest } | null>(null); const isOpen = request !== null; const isEditing = request?.scriptId != null; + const isSaving = request !== null && savingRequest === request; + + // A save completion must not affect a replacement request or an unmounted editor. + useLayoutEffect( + () => () => { + if (pendingSubmissionRef.current?.request === request) { + pendingSubmissionRef.current = null; + } + }, + [request], + ); // Hydrate the form whenever a new request opens the dialog. useEffect(() => { @@ -172,8 +192,16 @@ export function ProjectScriptEditorDialog({ setPreviewUrl(request.initial.previewUrl ?? ""); setAutoOpenPreview(request.initial.autoOpenPreview); setValidationError(request.error ?? null); + setSavingRequest(null); }, [request]); + const close = () => { + pendingSubmissionRef.current = null; + setSavingRequest(null); + setIconPickerOpen(false); + onClose(); + }; + const captureKeybinding = (event: KeyboardEvent) => { if (event.key === "Tab") return; event.preventDefault(); @@ -188,7 +216,7 @@ export function ProjectScriptEditorDialog({ const submit = async (event: FormEvent) => { event.preventDefault(); - if (!request) return; + if (!request || pendingSubmissionRef.current !== null) return; const trimmedName = name.trim(); const trimmedCommand = command.trim(); if (trimmedName.length === 0) { @@ -228,16 +256,31 @@ export function ProjectScriptEditorDialog({ return; } - const result = await onSubmit(request.scriptId, payload); - if (result._tag === "Failure") { - if (!isAtomCommandInterrupted(result)) { - const error = squashAtomCommandFailure(result); + const submission = { request }; + pendingSubmissionRef.current = submission; + setSavingRequest(request); + setIconPickerOpen(false); + try { + const result = await onSubmit(request.scriptId, payload); + if (pendingSubmissionRef.current === submission) { + if (result._tag === "Failure") { + if (!isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + setValidationError(error instanceof Error ? error.message : "Failed to save action."); + } + } else { + close(); + } + } + } catch (error) { + if (pendingSubmissionRef.current === submission) { setValidationError(error instanceof Error ? error.message : "Failed to save action."); } - return; } - setIconPickerOpen(false); - onClose(); + if (pendingSubmissionRef.current === submission) { + pendingSubmissionRef.current = null; + setSavingRequest(null); + } }; return ( @@ -246,8 +289,7 @@ export function ProjectScriptEditorDialog({ open={isOpen} onOpenChange={(open) => { if (!open) { - setIconPickerOpen(false); - onClose(); + close(); } }} > @@ -259,112 +301,115 @@ export function ProjectScriptEditorDialog({ -
-
- -
- - - } - > - - - -
- {SCRIPT_ICONS.map((entry) => { - const isSelected = entry.id === icon; - return ( - - ); - })} -
-
-
+ +
+
+ +
+ + + } + > + + + +
+ {SCRIPT_ICONS.map((entry) => { + const isSelected = entry.id === icon; + return ( + + ); + })} +
+
+
+ setName(event.target.value)} + /> +
+
+
+ setName(event.target.value)} + id="script-keybinding" + placeholder="Press shortcut" + value={keybinding} + readOnly + onKeyDown={captureKeybinding} /> +

+ Press a shortcut. Use Backspace to clear. Shortcuts are + environment-wide. Projects using the same action share its shortcut. +

-
-
- - -

- Press a shortcut. Use Backspace to clear. -

-
-
- -