From ccae953c46c09255038897b2872eb4952f8637e5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 19:23:01 -0800 Subject: [PATCH 001/106] fix(server): ACP agents keep the kept conversation after Edit from here (#30188) --- .../src/orchestration-v2/CommandPolicy.ts | 4 +- .../src/orchestration-v2/Orchestrator.ts | 37 +- .../ProviderTurnStartService.test.ts | 27 +- .../ProviderTurnStartService.ts | 95 +++++- .../src/orchestration-v2/contextBoundary.ts | 39 +++ .../ProviderSwitch.integration.test.ts | 321 +++++++++++++++++- docs/user/providers-acp.md | 4 +- packages/provider-acp/src/server/adapter.ts | 4 +- 8 files changed, 463 insertions(+), 68 deletions(-) create mode 100644 apps/server/src/orchestration-v2/contextBoundary.ts diff --git a/apps/server/src/orchestration-v2/CommandPolicy.ts b/apps/server/src/orchestration-v2/CommandPolicy.ts index cd56460369bd6..67d61332ae7c8 100644 --- a/apps/server/src/orchestration-v2/CommandPolicy.ts +++ b/apps/server/src/orchestration-v2/CommandPolicy.ts @@ -322,9 +322,11 @@ const ensureNativeFork: CommandPolicyV2Shape["ensureNativeFork"] = (input) => { }; const ensureRollback: CommandPolicyV2Shape["ensureRollback"] = (input) => { + // Without native truncation, the next turn replays the kept conversation as a handoff. if ( !input.capabilities.threads.canRollbackThread || - !input.capabilities.checkpointing.providerCanRollbackConversation + (!input.capabilities.checkpointing.providerCanRollbackConversation && + !input.capabilities.context.canConsumeHandoffSummaries) ) { return Effect.fail( unsupported(input, "rollback", "providerInstanceId conversation rollback is unavailable"), diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 27b7a2e12878d..f023fe5aa8697 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -82,6 +82,7 @@ import { import * as KeyedLock from "@t3tools/shared/KeyedLock"; import { modelSelectionsEqual } from "@t3tools/shared/model"; import { modelSwitchSince } from "./modelSwitch.ts"; +import { contextBoundaryOrdinal, isScheduledFreshRun } from "./contextBoundary.ts"; import { isPlaceholderThreadTitle } from "./placeholderThreadTitle.ts"; import { combinePullRequestWatchWakes, type PullRequestWatchWake } from "./pullRequestWatch.ts"; import { recordThreadOpen } from "@t3tools/shared/paletteSearch"; @@ -1364,42 +1365,6 @@ function appendContextHandoffId( return handoffId === null ? handoffIds : Array.from(new Set([...handoffIds, handoffId])); } -/** A fresh-session schedule run starts a side conversation; the thread's own one continues past it. */ -function isScheduledFreshRun( - run: Pick, - turnItems: OrchestrationV2ThreadProjection["turnItems"], -): boolean { - // Turn items, unlike messages, are not narrowed to the runs since the latest compaction. - return ( - run.freshContext === true && - turnItems.some( - (item) => - item.type === "user_message" && - item.messageId === run.userMessageId && - item.scheduledTaskId !== undefined, - ) - ); -} - -/** Ordinal of the latest started fresh-context run. Runs before it never reach a provider again. */ -function contextBoundaryOrdinal( - projection: Pick, -): number { - let boundary = 0; - for (const run of projection.runs) { - if ( - run.freshContext === true && - run.status !== "queued" && - !cancelledBeforeStart(run) && - run.ordinal > boundary && - !isScheduledFreshRun(run, projection.turnItems) - ) { - boundary = run.ordinal; - } - } - return boundary; -} - /** The provider thread a message continues: the active one, unless a fresh-session schedule run started it. */ function conversationProviderThread( projection: Pick< diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index 7b0759ae99b0b..95595c3a0a2ae 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -1028,19 +1028,6 @@ function makeLocalCommandHarness(input: { ), ); const fallbackEnsureThread = vi.fn(() => Effect.succeed(providerThread)); - const resumeFallbackSession = { - driver: providerThread.driver, - resumeThread: () => - Effect.fail( - input.resumeFailure ?? - new ProviderAdapter.ProviderAdapterEventStreamError({ - driver: providerThread.driver, - providerSessionId, - cause: "native thread is gone", - }), - ), - ensureThread: fallbackEnsureThread, - }; const providerSession = { id: providerSessionId, driver: providerThread.driver, @@ -1059,6 +1046,20 @@ function makeLocalCommandHarness(input: { updatedAt: now, lastError: null, }; + const resumeFallbackSession = { + driver: providerThread.driver, + providerSession, + resumeThread: () => + Effect.fail( + input.resumeFailure ?? + new ProviderAdapter.ProviderAdapterEventStreamError({ + driver: providerThread.driver, + providerSessionId, + cause: "native thread is gone", + }), + ), + ensureThread: fallbackEnsureThread, + }; const startTurn = vi.fn((_turn: ProviderAdapter.ProviderAdapterV2TurnInput) => Effect.void); const sideChatThread = { ...providerThread, diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 5996a090bf4bd..7764bca5cca0c 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -9,6 +9,7 @@ import { type OrchestrationV2ProviderThread, type OrchestrationV2Run, type OrchestrationV2RunAttempt, + type OrchestrationV2ThreadProjection, type OrchestrationV2TurnItem, isCompactionCommandMessage, parseGoalCommand, @@ -43,6 +44,7 @@ import * as EventSink from "./EventSink.ts"; import * as ProviderUsageAnalytics from "../telemetry/ProviderUsageAnalytics.ts"; import * as ContextHandoffService from "./ContextHandoffService.ts"; import { isActiveRun } from "./CommandPolicy.ts"; +import { contextBoundaryOrdinal } from "./contextBoundary.ts"; import { DEFAULT_HANDOFF_TOKEN_CAP, handoffTokenCapConfig, @@ -118,6 +120,53 @@ const refusedBeforePrompt = (error: unknown): boolean => error.detail === CLAUDE_QUERY_ENDED_BEFORE_TURN_DETAIL) || (Predicate.hasProperty(error, "cause") && refusedBeforePrompt(error.cause)); +/** + * Whether a non-truncating provider's native session lost the conversation kept before `run`. + * Rollback resets it: the newest accepted turn was rolled back (works without legacy-missing + * native ids) or ran under another native id (rollback of runs the provider never accepted). + */ +const nativeSessionMissesKeptTurns = ( + projection: Pick< + OrchestrationV2ThreadProjection, + "thread" | "runs" | "attempts" | "providerTurns" | "contextHandoffs" + >, + providerThread: OrchestrationV2ProviderThread, + run: OrchestrationV2Run, +): boolean => { + const nativeId = providerThread.nativeThreadRef?.nativeId; + if (nativeId === undefined) return false; + const acceptedAttemptIds = new Set(projection.providerTurns.map((turn) => turn.runAttemptId)); + const accepted = projection.attempts.filter((attempt) => acceptedAttemptIds.has(attempt.id)); + const runsById = new Map(projection.runs.map((source) => [source.id, source])); + const ordinalOf = (attempt: OrchestrationV2RunAttempt) => + runsById.get(attempt.runId)?.ordinal ?? 0; + const newest = accepted + .filter((attempt) => attempt.providerThreadId === providerThread.id) + .toSorted((a, b) => ordinalOf(a) - ordinalOf(b) || a.attemptOrdinal - b.attemptOrdinal) + .at(-1); + if (newest === undefined) return false; + // Inherited fork history is kept even when every local run was rolled back. + const keepsHistory = + (projection.thread.forkedFrom?.type === "run" && projection.thread.lineage.sideChat !== true) || + accepted.some((attempt) => { + const source = runsById.get(attempt.runId); + return ( + source !== undefined && source.ordinal < run.ordinal && source.status !== "rolled_back" + ); + }); + return ( + keepsHistory && + (runsById.get(newest.runId)?.status === "rolled_back" || + (newest.nativeThreadId !== undefined && newest.nativeThreadId !== nativeId)) && + !projection.contextHandoffs.some( + (handoff) => + handoff.toProviderThreadId === providerThread.id && + handoff.delivery?.nativeThreadId === nativeId && + handoff.delivery.status !== "pending", + ) + ); +}; + /** Missing execution records cannot be repaired by retrying a start. */ export class ProviderTurnStartRunStateMissingError extends Schema.TaggedError()( "ProviderTurnStartRunStateMissingError", @@ -1084,6 +1133,8 @@ export const layer: Layer.Layer< type: "provider_resume_fallback", }); const createdAt = yield* DateTime.now; + const history = yield* projectionStore.getTurnStartHistory(input.threadId); + const boundary = contextBoundaryOrdinal({ runs: projection.runs, turnItems: history }); const handoff = yield* contextHandoffService.prepareProviderHandoff({ threadId: projection.thread.id, targetRunId: run.id, @@ -1095,13 +1146,25 @@ export const layer: Layer.Layer< coveredRunOrdinals: { from: 1, to: Math.max(1, run.ordinal - 1) }, strategy: "full_thread_summary", runs: projection.runs, - items: (yield* projectionStore.getTurnStartHistory(input.threadId)).filter( - (item) => - item.runId === null || - projection.runs.some( - (source) => source.id === item.runId && source.ordinal < run.ordinal, - ), - ), + items: [ + ...(boundary === 0 + ? yield* ContextHandoffService.readInheritedHandoffItems( + projectionStore, + projection.thread, + ) + : []), + ...history.filter((item) => + item.runId === null + ? boundary === 0 + : projection.runs.some( + (source) => + source.id === item.runId && + source.ordinal >= boundary && + source.ordinal < run.ordinal && + source.status !== "rolled_back", + ), + ), + ], createdAt, }); return { handoff, transferId, createdAt }; @@ -1328,15 +1391,21 @@ export const layer: Layer.Layer< handoff.delivery?.nativeThreadId === providerThread.nativeThreadRef?.nativeId && handoff.delivery?.status === "pending", ); + // A provider that cannot truncate rolls back into a fresh native session holding none of the kept turns. + const nativeSessionLacksKeptTurns = + !session.providerSession.capabilities.checkpointing.providerCanRollbackConversation && + nativeSessionMissesKeptTurns(projection, providerThread, run); const resumed = yield* Effect.result( - uncertainDelivery + uncertainDelivery || nativeSessionLacksKeptTurns ? Effect.fail( new ProviderAdapter.ProviderAdapterTurnStartError({ driver: session.driver, threadId: projection.thread.id, providerThreadId: providerThread.id, runId, - cause: "Uncertain native history injection", + cause: uncertainDelivery + ? "Uncertain native history injection" + : "Native session holds none of the kept conversation", }), ) : session.resumeThread({ @@ -1416,9 +1485,11 @@ export const layer: Layer.Layer< runId, reason: uncertainDelivery ? "uncertain_history_delivery" - : resumed._tag === "Success" - ? "handoff_budget_exhausted" - : "resume_failed", + : nativeSessionLacksKeptTurns + ? "native_session_lacks_kept_turns" + : resumed._tag === "Success" + ? "handoff_budget_exhausted" + : "resume_failed", errorTag: resumed._tag === "Failure" ? resumed.failure._tag : "ContextHandoffBudgetError", }); const replacement = yield* loadFromProvider( diff --git a/apps/server/src/orchestration-v2/contextBoundary.ts b/apps/server/src/orchestration-v2/contextBoundary.ts new file mode 100644 index 0000000000000..1d9ca8d552fa2 --- /dev/null +++ b/apps/server/src/orchestration-v2/contextBoundary.ts @@ -0,0 +1,39 @@ +import type { OrchestrationV2Run, OrchestrationV2ThreadProjection } from "@t3tools/contracts"; + +import { cancelledBeforeStart } from "./RestartBackgroundNote.ts"; + +/** A fresh-session schedule run starts a side conversation; the thread's own one continues past it. */ +export function isScheduledFreshRun( + run: Pick, + turnItems: OrchestrationV2ThreadProjection["turnItems"], +): boolean { + // Turn items, unlike messages, are not narrowed to the runs since the latest compaction. + return ( + run.freshContext === true && + turnItems.some( + (item) => + item.type === "user_message" && + item.messageId === run.userMessageId && + item.scheduledTaskId !== undefined, + ) + ); +} + +/** Ordinal of the latest started fresh-context run. Runs before it never reach a provider again. */ +export function contextBoundaryOrdinal( + projection: Pick, +): number { + let boundary = 0; + for (const run of projection.runs) { + if ( + run.freshContext === true && + run.status !== "queued" && + !cancelledBeforeStart(run) && + run.ordinal > boundary && + !isScheduledFreshRun(run, projection.turnItems) + ) { + boundary = run.ordinal; + } + } + return boundary; +} diff --git a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts index edc12dcd98840..289e99b414990 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts @@ -131,6 +131,8 @@ function makeTestAdapter(input: { readonly backgroundTasksEndWithProcess?: true; readonly backgroundWork?: Ref.Ref; readonly closedSessions?: Ref.Ref>; + // ACP cannot truncate, so its rollback answers with a fresh, empty native session. + readonly rollback?: "truncates" | "replaces_native_thread"; }): ProviderAdapter.ProviderAdapterV2["Service"] { return { instanceId: input.instanceId, @@ -448,8 +450,25 @@ function makeTestAdapter(input: { respondToRuntimeRequest: () => Effect.void, readThreadSnapshot: () => unimplemented(input.driver, "readThreadSnapshot unused in provider switch test"), - rollbackThread: () => - unimplemented(input.driver, "rollbackThread unused in provider switch test"), + rollbackThread: ({ providerThread }) => + input.rollback === undefined + ? unimplemented(input.driver, "rollbackThread unused in provider switch test") + : Effect.succeed({ + providerThread: + input.rollback === "truncates" || providerThread.nativeThreadRef === null + ? providerThread + : { + ...providerThread, + nativeThreadRef: { + ...providerThread.nativeThreadRef, + nativeId: `${providerThread.nativeThreadRef.nativeId}:after-rollback`, + }, + nativeConversationHeadRef: null, + }, + providerTurns: [], + messages: [], + runtimeRequests: [], + }), forkThread: () => unimplemented(input.driver, "forkThread unused in provider switch test"), }; @@ -1630,6 +1649,304 @@ describe("orchestration v2 provider switching", () => { }), ); + it.live.each([ + { + name: "an ACP rollback that starts a fresh agent session", + instanceId: GROK_MODEL_SELECTION.instanceId, + driver: GROK_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + modelSelection: GROK_MODEL_SELECTION, + rollback: "replaces_native_thread", + failResumeAfterRollback: false, + history: "plain", + delivers: ["favourite colour is teal"], + withholds: ["favourite animal is an otter"], + }, + { + name: "a native rollback whose next resume fails", + instanceId: CLAUDE_MODEL_SELECTION.instanceId, + driver: CLAUDE_DRIVER, + capabilities: ClaudeProviderCapabilitiesV2, + modelSelection: CLAUDE_MODEL_SELECTION, + rollback: "truncates", + failResumeAfterRollback: true, + history: "plain", + delivers: ["favourite colour is teal"], + withholds: ["favourite animal is an otter"], + }, + { + // Pi forks into a new native thread that still holds the kept turns. + name: "a native rollback that forks a new native thread", + instanceId: CLAUDE_MODEL_SELECTION.instanceId, + driver: CLAUDE_DRIVER, + capabilities: ClaudeProviderCapabilitiesV2, + modelSelection: CLAUDE_MODEL_SELECTION, + rollback: "replaces_native_thread", + failResumeAfterRollback: false, + history: "plain", + delivers: [], + withholds: ["favourite colour is teal", "favourite animal is an otter"], + }, + { + name: "an ACP rollback where no attempt recorded its native session", + instanceId: GROK_MODEL_SELECTION.instanceId, + driver: GROK_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + modelSelection: GROK_MODEL_SELECTION, + rollback: "replaces_native_thread", + failResumeAfterRollback: false, + history: "legacy_all_turns", + delivers: ["favourite colour is teal"], + withholds: ["favourite animal is an otter"], + }, + { + name: "an ACP rollback of a forked thread to its baseline", + instanceId: GROK_MODEL_SELECTION.instanceId, + driver: GROK_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + modelSelection: GROK_MODEL_SELECTION, + rollback: "replaces_native_thread", + failResumeAfterRollback: false, + history: "forked_to_baseline", + delivers: ["home town is Oslo"], + withholds: ["favourite colour is teal", "favourite animal is an otter"], + }, + { + name: "an ACP rollback in a thread restarted with fresh context", + instanceId: GROK_MODEL_SELECTION.instanceId, + driver: GROK_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + modelSelection: GROK_MODEL_SELECTION, + rollback: "replaces_native_thread", + failResumeAfterRollback: false, + history: "after_fresh_context", + delivers: ["favourite colour is teal"], + withholds: ["home town is Oslo", "favourite animal is an otter"], + }, + ] as const)( + "gives the agent the kept conversation and not the rewound turn after $name", + (scenario) => + Effect.scoped( + Effect.gen(function* () { + const name = `rollback-kept-context-${scenario.driver}-${scenario.history}`; + const cwd = yield* checkpointWorkspace(name); + const capturedTurns = yield* Ref.make>([]); + const injectedHistory = yield* Ref.make>([]); + const failResumeOnce = yield* Ref.make(false); + const nativeThreadGeneration = yield* Ref.make(0); + const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([ + makeTestAdapter({ + instanceId: scenario.instanceId, + driver: scenario.driver, + capabilities: scenario.capabilities, + modelSelection: scenario.modelSelection, + responseByRunOrdinal: { 1: "noted", 2: "noted", 3: "noted" }, + capturedTurns, + injectedHistory, + failResumeOnce, + nativeThreadGeneration, + rollback: scenario.rollback, + }), + ]); + yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const runToCompletion = Effect.fn("runToCompletion")(function* ( + ordinal: number, + text: string, + thread: ThreadId = threadId, + freshContext = false, + ) { + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make(`${thread}:${ordinal}`), + threadId: thread, + messageId: MessageId.make(`${thread}:${ordinal}`), + createdBy: "user", + creationSource: "web", + text, + attachments: [], + modelSelection: scenario.modelSelection, + ...(freshContext ? { freshContext: true } : {}), + dispatchMode: { type: "start_immediately" }, + }); + yield* orchestrator.streamStoredEvents.pipe( + Stream.filter( + ({ event }) => + event.type === "run.updated" && + event.threadId === thread && + event.payload.ordinal === ordinal && + (event.payload.status === "completed" || event.payload.status === "failed"), + ), + Stream.runHead, + ); + yield* worker.drain(); + }); + const createThread = (thread: ThreadId) => + orchestrator.dispatch({ + type: "thread.create", + commandId: CommandId.make(`${thread}:create`), + threadId: thread, + projectId, + createdBy: "user", + creationSource: "web", + title: "Edit from here", + modelSelection: scenario.modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + }); + if (scenario.history === "forked_to_baseline") { + const sourceThreadId = ThreadId.make(`${name}:source`); + yield* createThread(sourceThreadId); + yield* runToCompletion( + 1, + "My home town is Oslo. Reply with only the word: noted", + sourceThreadId, + ); + yield* orchestrator.dispatch({ + type: "thread.fork", + commandId: CommandId.make(`${name}:fork`), + sourceThreadId, + targetThreadId: threadId, + sourcePoint: { + type: "run", + runId: (yield* orchestrator.getThreadProjection(sourceThreadId)).runs[0]!.id, + }, + createdBy: "user", + creationSource: "web", + }); + } else { + yield* createThread(threadId); + } + // Runs before a fresh-context run never reach a provider again. + const first = scenario.history === "after_fresh_context" ? 2 : 1; + if (first === 2) { + yield* runToCompletion(1, "My home town is Oslo. Reply with only the word: noted"); + } + yield* runToCompletion( + first, + "My favourite colour is teal. Reply with only the word: noted", + threadId, + first === 2, + ); + yield* runToCompletion( + first + 1, + "My favourite animal is an otter. Reply with only the word: noted", + ); + const beforeRollback = yield* orchestrator.getThreadProjection(threadId); + if (scenario.history === "legacy_all_turns") { + yield* (yield* EventSink.EventSinkV2).write({ + events: beforeRollback.attempts.map( + ({ nativeThreadId: _nativeThreadId, ...legacy }, index) => ({ + id: EventId.make(`${name}:legacy-attempt:${index}`), + type: "run-attempt.updated" as const, + threadId, + occurredAt: beforeRollback.thread.createdAt, + payload: legacy, + }), + ), + }); + } + const keptOrdinal = scenario.history === "forked_to_baseline" ? 0 : first; + const keptCheckpoint = beforeRollback.checkpoints.find( + (checkpoint) => + (checkpoint.appRunOrdinal ?? 0) === keptOrdinal && checkpoint.status === "ready", + ); + if (keptCheckpoint === undefined) { + return yield* Effect.die("Expected a checkpoint after the first turn."); + } + yield* orchestrator.dispatch({ + type: "checkpoint.rollback", + commandId: CommandId.make(`${name}:rollback`), + threadId, + scopeId: keptCheckpoint.scopeId, + checkpointId: keptCheckpoint.id, + restoreFiles: false, + }); + yield* orchestrator.streamStoredEvents.pipe( + Stream.filter( + ({ event }) => + (event.type === "run.updated" && + event.payload.ordinal === first + 1 && + event.payload.status === "rolled_back") || + (event.type === "thread.metadata-updated" && + event.payload.rollbackFailure != null), + ), + Stream.runHead, + ); + yield* worker.drain(); + assert.deepEqual( + (yield* orchestrator.getThreadProjection(threadId)).runs.map((run) => run.status), + scenario.history === "forked_to_baseline" + ? ["rolled_back", "rolled_back"] + : [...(first === 2 ? ["completed"] : []), "completed", "rolled_back"], + ); + if (scenario.failResumeAfterRollback) { + const providerSessionId = (yield* orchestrator.getThreadProjection(threadId)) + .providerThreads[0]?.providerSessionId; + if (providerSessionId == null) { + return yield* Effect.die("Expected a provider session after the rollback."); + } + yield* orchestrator.dispatch({ + type: "provider-session.detach", + commandId: CommandId.make(`${name}:detach`), + threadId, + providerSessionId, + }); + yield* worker.drain(); + yield* Ref.set(failResumeOnce, true); + } + const historyBeforeRun3 = (yield* Ref.get(injectedHistory)).length; + yield* runToCompletion(first + 2, "What did I say first?"); + + const run3 = (yield* Ref.get(capturedTurns)).at(-1); + const delivered = + (run3?.text ?? "") + + (yield* encodeJson((yield* Ref.get(injectedHistory)).slice(historyBeforeRun3))); + for (const kept of scenario.delivers) { + assert.include(delivered, kept, "the agent's session lacks this kept turn"); + } + for (const withheld of scenario.withholds) { + assert.notInclude(delivered, withheld, "the session holds it or it was rewound"); + } + if (scenario.name === "an ACP rollback that starts a fresh agent session") { + const recoveries = (projection: OrchestrationV2ThreadProjection) => + projection.contextHandoffs.filter( + (handoff) => handoff.strategy === "full_thread_summary", + ).length; + const recoveriesBeforeRun4 = recoveries( + yield* orchestrator.getThreadProjection(threadId), + ); + const sessionsBeforeRun4 = yield* Ref.get(nativeThreadGeneration); + yield* runToCompletion(4, "And what did I say second?"); + assert.strictEqual( + recoveries(yield* orchestrator.getThreadProjection(threadId)), + recoveriesBeforeRun4, + "the recovered session holds the kept turns, so the next turn replays nothing", + ); + assert.strictEqual(yield* Ref.get(nativeThreadGeneration), sessionsBeforeRun4); + } + }).pipe( + Effect.provide( + ProviderReplayHarness.layerWithRegistry( + { + name, + runtimePolicyOverride: { + cwd, + approvalPolicy: "never", + sandboxPolicy: { type: "readOnly" }, + }, + }, + layerRegistry, + ), + ), + ); + }), + ), + ); + it.live.each([ ["background work", new ClaudeBackgroundWorkBlocksQueryReplacementError()], ["an unreadable append file", new ClaudeAppendSystemPromptFileError({ path: "missing.md" })], diff --git a/docs/user/providers-acp.md b/docs/user/providers-acp.md index 701d5638b3fd8..453c777e3c8c0 100644 --- a/docs/user/providers-acp.md +++ b/docs/user/providers-acp.md @@ -128,8 +128,8 @@ remain agent-specific. ## Checkpoints Checkpoint rollback restores your files as usual. ACP agents cannot rewind their own conversation, -so the next turn after a rollback starts a fresh agent session that no longer remembers the -conversation from before the checkpoint. +so the next turn after a rollback starts a fresh agent session and T3 Code sends it the kept +conversation, without the turns you rolled back. Registry instances are not used for T3's app-owned text generation, such as thread titles, commit messages, branch names, or pull request descriptions. Configure a text-generation-capable provider diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 5934240905edf..696ee6b30d8d7 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -802,8 +802,8 @@ export const AcpProviderCapabilitiesV2 = { checkpointing: { appCanCheckpointFilesystem: true, supportsNestedCheckpointScopes: true, - // ACP lacks truncation; rollback restores T3 checkpoints and starts a fresh agent session. - providerCanRollbackConversation: true, + // ACP lacks truncation; rollback starts a fresh agent session and turn start replays the kept history. + providerCanRollbackConversation: false, providerRollbackReturnsSnapshot: true, providerCanReadConversationSnapshot: false, }, From f390ae7f1507872e0799685d3806fc309dad0d75 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 19:44:16 -0800 Subject: [PATCH 002/106] fix(ssh): an older client no longer replaces a newer running SSH server (#29428) --- apps/server/src/cli/sshHelper.test.ts | 21 +++ apps/server/src/cli/sshHelper.ts | 41 +++++- packages/ssh/src/tunnel.test.ts | 196 ++++++++++++++++++++++++++ packages/ssh/src/tunnel.ts | 11 +- 4 files changed, 265 insertions(+), 4 deletions(-) create mode 100644 apps/server/src/cli/sshHelper.test.ts diff --git a/apps/server/src/cli/sshHelper.test.ts b/apps/server/src/cli/sshHelper.test.ts new file mode 100644 index 0000000000000..63ef8b6dc8456 --- /dev/null +++ b/apps/server/src/cli/sshHelper.test.ts @@ -0,0 +1,21 @@ +import { assert, it } from "@effect/vitest"; + +import { keepsRunningServer } from "./sshHelper.ts"; + +const runtime = (fields: Record) => + JSON.stringify({ version: 1, pid: 42, port: 3773, ...fields }); + +it("keeps a same-or-newer server and replaces an older or unidentified one", () => { + assert.isTrue(keepsRunningServer(runtime({ serverVersion: "0.1.6" }), 42, "0.1.6")); + assert.isTrue( + keepsRunningServer(runtime({ serverVersion: "0.1.7-nightly.20261102.4080" }), 42, "0.1.6"), + ); + assert.isFalse( + keepsRunningServer(runtime({ serverVersion: "0.1.7-nightly.20261102.4080" }), 42, "0.1.7"), + ); + assert.isFalse(keepsRunningServer(runtime({ serverVersion: "0.1.5" }), 42, "0.1.6")); + assert.isFalse(keepsRunningServer(runtime({ serverVersion: "0.1.6" }), 7, "0.1.6")); + assert.isFalse(keepsRunningServer(runtime({}), 42, "0.1.6")); + assert.isFalse(keepsRunningServer(runtime({ serverVersion: "latest" }), 42, "0.1.6")); + assert.isFalse(keepsRunningServer("", 42, "0.1.6")); +}); diff --git a/apps/server/src/cli/sshHelper.ts b/apps/server/src/cli/sshHelper.ts index e98e5e0537af1..3a47f5b661290 100644 --- a/apps/server/src/cli/sshHelper.ts +++ b/apps/server/src/cli/sshHelper.ts @@ -7,8 +7,11 @@ import * as NodeNet from "node:net"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; +import * as Schema from "effect/Schema"; import { Argument, Command } from "effect/cli"; +import packageJson from "../../package.json" with { type: "json" }; +import { compareExactServiceVersions, isExactServiceVersion } from "../cloud/serviceProtocol.ts"; import * as DesktopUpdateMarker from "../desktopUpdate/DesktopUpdateMarker.ts"; import { serviceLauncherIsServing, stateDirOwned } from "../serviceLauncher.ts"; @@ -128,6 +131,35 @@ const runtimePort = Command.make("runtime-port", { ), ); +const decodeRunningServer = Schema.decodeUnknownOption( + Schema.fromJsonString(Schema.Struct({ pid: Schema.Number, serverVersion: Schema.String })), +); + +/** True when the runtime file names `pid` and a server version at or above `ownVersion`. */ +export const keepsRunningServer = (runtimeJson: string, pid: number, ownVersion: string) => + Option.match(decodeRunningServer(runtimeJson), { + onNone: () => false, + onSome: (runtime) => + runtime.pid === pid && + isExactServiceVersion(runtime.serverVersion) && + compareExactServiceVersions(runtime.serverVersion, ownVersion) >= 0, + }); + +/** Exits 0 when the managed server `pid` runs this version or a newer one, else 1. */ +const keepServer = Command.make("keep-server", { + runtimeFile: Argument.String("runtime-file"), + pid: Argument.Int("pid"), +}).pipe( + Command.withHandler(({ runtimeFile, pid }) => + Effect.sync(() => { + const runtimeJson = NodeFS.existsSync(runtimeFile) + ? NodeFS.readFileSync(runtimeFile, "utf8") + : ""; + if (!keepsRunningServer(runtimeJson, pid, packageJson.version)) process.exitCode = 1; + }), + ), +); + const updateRemaining = Command.make("update-remaining", { markerFile: Argument.String("marker-file"), }).pipe( @@ -158,5 +190,12 @@ const homeOwned = Command.make("home-owned", { export const sshHelperCommand = Command.make("__ssh-helper").pipe( Command.unlisted, - Command.withSubcommands([pickPort, waitReady, runtimePort, updateRemaining, homeOwned]), + Command.withSubcommands([ + pickPort, + waitReady, + runtimePort, + updateRemaining, + homeOwned, + keepServer, + ]), ); diff --git a/packages/ssh/src/tunnel.test.ts b/packages/ssh/src/tunnel.test.ts index d2cd22f49d654..2a0b014ded7af 100644 --- a/packages/ssh/src/tunnel.test.ts +++ b/packages/ssh/src/tunnel.test.ts @@ -1,6 +1,7 @@ // @effect-diagnostics nodeBuiltinImport:off - Runs the generated runner in a real shell. import * as NodeChildProcess from "node:child_process"; import * as NodeFS from "node:fs"; +import * as NodeNet from "node:net"; import * as NodeOS from "node:os"; import * as NodePath from "node:path"; @@ -1406,6 +1407,201 @@ server.listen(Number(flag("--port") ?? 0), "127.0.0.1", () => { // bind unless both sockets set SO_REUSEPORT, so on Linux the preload adds it to // the picker's probe and the holder sets it too. +// Reuse decisions live in generated shell, so drive the real launch script in +// a temporary HOME. Each archive version gets a pre-installed fake runtime +// whose server writes the default runtime file the way `t3 serve` does. +describe("archive launch script", () => { + const windowsHost = HostProcess.Platform.defaultValue() === "win32"; + const FAKE_T3 = `const fs = require("node:fs"); +const http = require("node:http"); +const net = require("node:net"); +const path = require("node:path"); +const [version, ...args] = process.argv.slice(2); +const flag = (name) => args[args.indexOf(name) + 1]; +const probe = (port) => + new Promise((resolve) => { + http + .get({ hostname: "127.0.0.1", port, path: "/", timeout: 1000 }, (response) => { + response.resume(); + resolve(response.statusCode === 200); + }) + .once("error", () => resolve(false)); + }); +const canBind = (port) => + new Promise((resolve) => { + const server = net.createServer(); + server.once("error", () => resolve(false)); + server.listen(port, "127.0.0.1", () => server.close(() => resolve(true))); + }); +(async () => { + if (args[0] === "--version") return console.log(version); + if (args[0] === "serve") { + const server = http.createServer((_request, response) => response.end("ok")); + server.listen(Number(flag("--port")), "127.0.0.1", () => { + const port = server.address().port; + const userdata = path.join(flag("--base-dir"), "userdata"); + fs.mkdirSync(userdata, { recursive: true }); + fs.writeFileSync( + path.join(userdata, "server-runtime.json"), + JSON.stringify({ + version: 1, + pid: process.pid, + port, + origin: "http://127.0.0.1:" + port, + startedAt: new Date().toISOString(), + serverVersion: version, + }), + ); + }); + return; + } + const [helper, a, b, c] = args.slice(1); + if (helper === "pick-port") { + const start = Number(fs.existsSync(a) ? fs.readFileSync(a, "utf8") : b); + for (let port = start; port < start + Number(c); port += 1) { + if (await canBind(port)) return process.stdout.write(String(port)); + } + } else if (helper === "wait-ready") { + const deadline = Date.now() + Number(b); + while (Date.now() < deadline) { + if (await probe(Number(a))) return; + await new Promise((resolve) => setTimeout(resolve, 100)); + } + } else if (helper === "runtime-port") { + try { + const runtime = JSON.parse(fs.readFileSync(a, "utf8")); + process.kill(runtime.pid, 0); + return process.stdout.write(runtime.pid + " " + runtime.port); + } catch {} + } else if (helper === "keep-server") { + try { + const runtime = JSON.parse(fs.readFileSync(a, "utf8")); + const order = runtime.serverVersion.localeCompare(version, "en", { numeric: true }); + if (String(runtime.pid) === b && order >= 0) return; + } catch {} + } + process.exitCode = 1; +})(); +`; + + const isAlive = (pid: number) => { + try { + process.kill(pid, 0); + return true; + } catch { + return false; + } + }; + + const freeLoopbackPort = Effect.callback((resume) => { + const server = NodeNet.createServer(); + server.listen(0, "127.0.0.1", () => { + const address = server.address(); + server.close(() => + resume(Effect.succeed(typeof address === "object" && address ? address.port : 0)), + ); + }); + }); + + const runScript = (home: string, args: ReadonlyArray) => + Effect.gen(function* () { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const child = yield* spawner.spawn(ChildProcess.make("sh", args, { env: { HOME: home } })); + const collect = (stream: Stream.Stream) => + stream.pipe(Stream.decodeText(), Stream.mkString); + const [stdout, stderr, exitCode] = yield* Effect.all( + [collect(child.stdout), collect(child.stderr), child.exitCode.pipe(Effect.map(Number))], + { concurrency: "unbounded" }, + ); + return { stdout, stderr, exitCode }; + }); + + // Installs a fake runtime per version so the runner never downloads. The + // seeded remembered port keeps the test off the default 3773, which a live + // T3 on this machine may own. + const prepareLaunch = (versions: ReadonlyArray) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-archive-launch-" }); + const home = `${root}/home`; + const stateDir = `${home}/.t3/ssh-launch/test-state`; + const fakeT3 = `${root}/fake-t3.cjs`; + yield* fs.makeDirectory(stateDir, { recursive: true }); + yield* fs.writeFileString(`${stateDir}/port`, `${yield* freeLoopbackPort}\n`); + yield* fs.writeFileString(fakeT3, FAKE_T3); + for (const version of versions) { + const runtimeDir = `${home}/.t3/runtime/versions/${version}`; + yield* fs.makeDirectory(runtimeDir, { recursive: true }); + yield* fs.writeFileString( + `${runtimeDir}/t3`, + `#!/bin/sh\nexec node '${fakeT3}' '${version}' "$@"\n`, + ); + yield* fs.chmod(`${runtimeDir}/t3`, 0o755); + yield* fs.writeFileString(`${runtimeDir}/.install-complete`, `${version}\n`); + } + const readManagedPid = fs + .readFileString(`${stateDir}/pid`) + .pipe(Effect.map((pid) => Number(pid.trim()))); + const readServerVersion = fs + .readFileString(`${home}/.t3/userdata/server-runtime.json`) + .pipe(Effect.map((raw) => (JSON.parse(raw) as { serverVersion: string }).serverVersion)); + // Launched servers outlive the script, so stop each PID it recorded. + const launchedPids = new Set(); + yield* Effect.addFinalizer(() => + readManagedPid.pipe( + Effect.map((pid) => [...launchedPids, pid]), + Effect.orElseSucceed(() => [...launchedPids]), + Effect.map((pids) => { + for (const pid of pids) { + if (isAlive(pid)) process.kill(pid, "SIGKILL"); + } + }), + ), + ); + const connect = (archiveVersion: string) => + Effect.gen(function* () { + const launch = `${root}/launch-${archiveVersion}.sh`; + yield* fs.writeFileString(launch, SshTunnel.buildRemoteLaunchScript({ archiveVersion })); + const result = yield* runScript(home, [launch, "test-state"]); + assert.equal(result.exitCode, 0, result.stderr); + const pid = yield* readManagedPid; + launchedPids.add(pid); + return pid; + }); + return { connect, readManagedPid, readServerVersion }; + }); + + it.live.skipIf(windowsHost)( + "a reconnect from an older client keeps a newer managed server running", + () => + Effect.gen(function* () { + const { connect, readServerVersion } = yield* prepareLaunch(["0.1.5", "0.1.6"]); + const newerPid = yield* connect("0.1.6"); + assert.equal(yield* readServerVersion, "0.1.6"); + + const pidAfterOlderClient = yield* connect("0.1.5"); + assert.isTrue( + isAlive(newerPid), + "the 0.1.6 managed server was killed by a 0.1.5 reconnect", + ); + assert.equal(pidAfterOlderClient, newerPid); + assert.equal(yield* readServerVersion, "0.1.6"); + }).pipe(Effect.provide(NodeServices.layer)), + ); + + it.live.skipIf(windowsHost)("a newer client still replaces an older managed server", () => + Effect.gen(function* () { + const { connect, readServerVersion } = yield* prepareLaunch(["0.1.5", "0.1.6"]); + const olderPid = yield* connect("0.1.5"); + + const newerPid = yield* connect("0.1.6"); + assert.notEqual(newerPid, olderPid); + assert.isFalse(isAlive(olderPid)); + assert.equal(yield* readServerVersion, "0.1.6"); + }).pipe(Effect.provide(NodeServices.layer)), + ); +}); + describe("SSH launch during desktop update", () => { it.effect("archive launcher reuses the replacement without host Node", () => Effect.gen(function* () { diff --git a/packages/ssh/src/tunnel.ts b/packages/ssh/src/tunnel.ts index 0b27a94acb33d..3b0be642130c4 100644 --- a/packages/ssh/src/tunnel.ts +++ b/packages/ssh/src/tunnel.ts @@ -715,6 +715,11 @@ server_running() { *Z*) return 1 ;; esac } +# An older client replacing a same-or-newer server would run below the database's migrations. +keeps_running_server() { + [ "$T3_ARCHIVE_MODE" = "1" ] && + "$RUNNER_FILE" __ssh-helper keep-server "$DEFAULT_RUNTIME_FILE" "$REMOTE_PID" >/dev/null 2>&1 +} @@T3_MANAGED_SERVER_RUNNING_SCRIPT@@ # Both are SQLite locks the OS drops with their holder: a server holds # server-owner.sqlite, and a boot service holds service-serving.lock from @@ -878,8 +883,8 @@ if [ "$REMOTE_MANAGED" = "external" ]; then fi elif [ -n "$REMOTE_PORT" ] && managed_server_running "$REMOTE_PID"; then # Reconnect owns the local tunnel, not the lifetime of an already running - # server. A changed runner is the update path and still replaces it. - if [ "$RUNNER_CHANGED" -eq 1 ]; then + # server. A changed runner is the update path and replaces an older server. + if [ "$RUNNER_CHANGED" -eq 1 ] && ! keeps_running_server; then kill "$REMOTE_PID" 2>/dev/null || true wait_for_pid_exit "$REMOTE_PID" REMOTE_PID="" @@ -1106,7 +1111,7 @@ export function buildRemoteNodeEnvScript(input?: RemoteT3RunnerOptions): string ); } -function buildRemoteLaunchScript(input?: RemoteT3RunnerOptions): string { +export function buildRemoteLaunchScript(input?: RemoteT3RunnerOptions): string { return applyScriptPlaceholders(REMOTE_LAUNCH_SCRIPT, { T3_UPDATE_MARKER_FILE: DESKTOP_UPDATE_IN_PROGRESS_MARKER_FILE, T3_UPDATE_TTL_MS: String(DESKTOP_UPDATE_IN_PROGRESS_TTL_MS), From d7373c44feaa85f561c0f1e829ea1c31918acbeb Mon Sep 17 00:00:00 2001 From: Jackson Miller Date: Mon, 16 Nov 2026 22:49:48 -0500 Subject: [PATCH 003/106] fix(scripts): dev --share no longer replaces another server's Serve route (#37888) Co-authored-by: Julius Marminge --- scripts/lib/dev-share.test.ts | 97 ++++++++++++++++++++++++++++++++--- scripts/lib/dev-share.ts | 52 ++++++++++++++++--- 2 files changed, 134 insertions(+), 15 deletions(-) diff --git a/scripts/lib/dev-share.test.ts b/scripts/lib/dev-share.test.ts index 64456fbcda242..e21b8f04ba7b1 100644 --- a/scripts/lib/dev-share.test.ts +++ b/scripts/lib/dev-share.test.ts @@ -1,4 +1,5 @@ import { assert, describe, it } from "@effect/vitest"; +import * as NetService from "@t3tools/shared/Net"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Sink from "effect/Sink"; @@ -8,6 +9,7 @@ import { ChildProcessSpawner } from "effect/process"; import { type DevShareError, DevServeFailedError, + DevServePortTakenError, shareDevServer, unshareDevServer, } from "./dev-share.ts"; @@ -22,21 +24,40 @@ interface CallResult { const encode = (value: string) => Stream.make(new TextEncoder().encode(value)); +const serveStatusWithRoot = (webPort: number, proxy: string) => + JSON.stringify({ + Web: { [`host.example.ts.net:${String(webPort)}`]: { Handlers: { "/": { Proxy: proxy } } } }, + }); + const spawnerLayer = (input: { readonly off?: CallResult; readonly serve?: CallResult; + readonly serveStatus?: CallResult & { readonly stdout?: string }; + readonly liveLoopbackPorts?: ReadonlyArray; readonly calls?: Array>; -}) => - Layer.succeed( +}) => { + const live = new Set(input.liveLoopbackPorts ?? []); + const unused = Effect.die("unused in dev-share tests"); + const net = Layer.succeed(NetService.NetService, { + hasListenerOnHost: (port: number) => Effect.succeed(live.has(port)), + canListenOnHost: () => unused, + isPortAvailableOnLoopback: () => unused, + reserveLoopbackPort: () => unused, + findAvailablePort: () => unused, + }); + const spawner = Layer.succeed( ChildProcessSpawner.ChildProcessSpawner, ChildProcessSpawner.make((command) => { const args = "args" in command ? (command.args as ReadonlyArray) : []; input.calls?.push(args); - const result: CallResult = args.includes("status") - ? { exitCode: 0 } - : args.includes("off") - ? (input.off ?? { exitCode: 0 }) - : (input.serve ?? { exitCode: 0 }); + const isServeStatus = args[0] === "serve" && args.includes("status"); + const result: CallResult & { readonly stdout?: string } = isServeStatus + ? (input.serveStatus ?? { exitCode: 0, stdout: "{}" }) + : args.includes("status") + ? { exitCode: 0, stdout: TAILNET_STATUS } + : args.includes("off") + ? (input.off ?? { exitCode: 0 }) + : (input.serve ?? { exitCode: 0 }); return Effect.succeed( ChildProcessSpawner.makeHandle({ @@ -46,7 +67,7 @@ const spawnerLayer = (input: { kill: () => Effect.void, unref: Effect.succeed(Effect.void), stdin: Sink.drain, - stdout: args.includes("status") ? encode(TAILNET_STATUS) : Stream.empty, + stdout: result.stdout !== undefined ? encode(result.stdout) : Stream.empty, stderr: result.stderr ? encode(result.stderr) : Stream.empty, all: Stream.empty, getInputFd: () => Sink.drain, @@ -55,6 +76,8 @@ const spawnerLayer = (input: { ); }), ); + return Layer.merge(spawner, net); +}; describe("unshareDevServer", () => { // `tailscale serve … off` exits 1 when the port had no mapping, which is the @@ -131,4 +154,62 @@ describe("shareDevServer", () => { assert.include(error.message, "permission denied"); }), ); + + it.effect("leaves a mapping that another running server holds alone", () => + Effect.gen(function* () { + const calls: Array> = []; + const error: DevShareError = yield* shareDevServer({ webPort: 5788 }).pipe( + Effect.provide( + spawnerLayer({ + serveStatus: { + exitCode: 0, + stdout: serveStatusWithRoot(5788, "http://127.0.0.1:9555"), + }, + liveLoopbackPorts: [9555], + calls, + }), + ), + Effect.flip, + ); + + assert.instanceOf(error, DevServePortTakenError); + assert.isUndefined(calls.find((args) => args.includes("off") || args.includes("--bg"))); + }), + ); + + it.effect("replaces a mapping that dials a port nothing listens on", () => + Effect.gen(function* () { + const calls: Array> = []; + yield* shareDevServer({ webPort: 5788 }).pipe( + Effect.provide( + spawnerLayer({ + serveStatus: { + exitCode: 0, + stdout: serveStatusWithRoot(5788, "http://127.0.0.1:9555"), + }, + calls, + }), + ), + ); + + assert.isDefined(calls.find((args) => args.includes("off"))); + assert.isDefined(calls.find((args) => args.includes("--bg"))); + }), + ); + + it.effect("leaves the port alone when its mapping cannot be read", () => + Effect.gen(function* () { + const calls: Array> = []; + const error: DevShareError = yield* shareDevServer({ webPort: 5788 }).pipe( + Effect.provide( + spawnerLayer({ serveStatus: { exitCode: 1, stderr: "permission denied" }, calls }), + ), + Effect.flip, + ); + + assert.instanceOf(error, DevServeFailedError); + assert.equal(error.stage, "read-existing"); + assert.isUndefined(calls.find((args) => args.includes("off") || args.includes("--bg"))); + }), + ); }); diff --git a/scripts/lib/dev-share.ts b/scripts/lib/dev-share.ts index 0c42fe9f3116c..4d906bcd71fe4 100644 --- a/scripts/lib/dev-share.ts +++ b/scripts/lib/dev-share.ts @@ -4,6 +4,7 @@ import { buildTailscaleHttpsBaseUrl, disableTailscaleServe, ensureTailscaleServe, + readTailscaleServeRootHolder, readTailscaleStatus, type TailscaleCommandError, type TailscaleServeHttpsDisabledError, @@ -61,7 +62,7 @@ export class TailnetNameMissingError extends Schema.TaggedError()( "DevServeFailedError", { - stage: Schema.Literals(["clear-existing", "serve"]), + stage: Schema.Literals(["read-existing", "clear-existing", "serve"]), webPort: Schema.Number, explanation: Schema.optional(Schema.String), cause: Schema.optional(Schema.Defect()), @@ -70,9 +71,11 @@ export class DevServeFailedError extends Schema.TaggedError override get message(): string { const port = String(this.webPort); const base = - this.stage === "clear-existing" - ? `could not clear the existing mapping for port ${port}. Run \`tailscale serve --https=${port} --set-path=/ off\` and retry` - : `could not serve port ${port} on the tailnet (it is no longer served; any previous mapping for it was cleared before this attempt)`; + this.stage === "read-existing" + ? `could not read the existing mapping for port ${port}, so it was left alone` + : this.stage === "clear-existing" + ? `could not clear the existing mapping for port ${port}. Run \`tailscale serve --https=${port} --set-path=/ off\` and retry` + : `could not serve port ${port} on the tailnet (it is no longer served; any previous mapping for it was cleared before this attempt)`; return this.explanation ? `${base}: ${this.explanation}` : base; } @@ -81,10 +84,24 @@ export class DevServeFailedError extends Schema.TaggedError } } +export class DevServePortTakenError extends Schema.TaggedError()( + "DevServePortTakenError", + { webPort: Schema.Number }, +) { + override get message(): string { + return `tailnet port ${String(this.webPort)} already serves another running server, so it was left alone`; + } + + get hint(): string { + return `Pick another port with T3CODE_PORT_OFFSET, or remove that mapping with \`tailscale serve --https=${String(this.webPort)} --set-path=/ off\` if nothing needs it.`; + } +} + export type DevShareError = | TailscaleUnavailableError | TailnetNameMissingError - | DevServeFailedError; + | DevServeFailedError + | DevServePortTakenError; /** Finalizer cleanup must be uninterruptible or cancellation can leave stale mappings. */ export const unshareDevServer = ( @@ -138,6 +155,27 @@ export const shareDevServer = Effect.fn("devShare.shareDevServer")(function* (in return yield* new TailnetNameMissingError(); } + const target = input.localTarget ?? { port: input.webPort, host: "localhost" }; + // A mapping left by an earlier run, or one dialing a dead loopback port, is safe to replace. + const holder = yield* readTailscaleServeRootHolder(input.webPort, { + port: target.port, + host: target.host.replace(/^\[|\]$/gu, ""), + }).pipe( + Effect.mapError((error) => { + const explanation = + error._tag === "TailscaleStatusParseError" ? undefined : explainCommandFailure(error); + return new DevServeFailedError({ + stage: "read-existing", + webPort: input.webPort, + ...(explanation !== undefined ? { explanation } : {}), + cause: error, + }); + }), + ); + if (holder === "other") { + return yield* new DevServePortTakenError({ webPort: input.webPort }); + } + const cleared = yield* unshareDevServer(input.webPort); if (!cleared.cleared) { // Refuse stale routes: the page could load while /ws and /api still point at a dead backend. @@ -151,9 +189,9 @@ export const shareDevServer = Effect.fn("devShare.shareDevServer")(function* (in // Vite's localhost can bind only ::1; a 127.0.0.1 proxy target would return 502. yield* ensureTailscaleServe({ - localPort: input.localTarget?.port ?? input.webPort, + localPort: target.port, servePort: input.webPort, - localHost: input.localTarget?.host ?? "localhost", + localHost: target.host, }).pipe( Effect.mapError((error) => { const explanation = explainCommandFailure(error); From e9d5bae5cf2846da8a4b035052de0a4faabb5fbd Mon Sep 17 00:00:00 2001 From: pcnf <138956102+bignosecss@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:08:46 +0800 Subject: [PATCH 004/106] fix(web): linked PR comment highlight keeps its full outline (#37881) Co-authored-by: Julius Marminge --- apps/web/src/components/pullRequest/PullRequestTimelineTab.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/web/src/components/pullRequest/PullRequestTimelineTab.tsx b/apps/web/src/components/pullRequest/PullRequestTimelineTab.tsx index 9feedcbc27347..f5534749b6433 100644 --- a/apps/web/src/components/pullRequest/PullRequestTimelineTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestTimelineTab.tsx @@ -276,7 +276,8 @@ function ConversationCard({ ? { "data-pr-link-target": true, tabIndex: -1, - className: "group rounded-lg py-2 ring-1 ring-primary/40 outline-none", + // Inset: the collapsible panel clips an outer ring's left and right edges. + className: "group rounded-lg py-2 ring-1 ring-inset ring-primary/40 outline-none", } : { className: "group py-2" })} > From f6d196463cf74f1d776e23578f4e920af470e7f7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 20:17:53 -0800 Subject: [PATCH 005/106] fix(shared): stopping an MCP call cancels its stalled SSE response (#37872) --- .../Adapters/AcpAdapterV2.test.ts | 123 ++++++++++++++++++ packages/provider-acp/src/server/adapter.ts | 11 +- .../src/server/mcpOverAcpBridge.ts | 26 +++- .../shared/src/mcpResponsePayloads.test.ts | 30 +++++ packages/shared/src/mcpResponsePayloads.ts | 30 ++++- 5 files changed, 210 insertions(+), 10 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 863f2279ef2a3..62f9d5f8bfbb4 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -11,6 +11,7 @@ import { AcpRegistryCatalog } from "@t3tools/provider-acp-registry/server/AcpReg import { decodeAcpReplayTranscript, makeAcpReplayRuntime } from "./AcpAdapterV2.testkit.ts"; // @effect-diagnostics nodeBuiltinImport:off import * as NodeFS from "node:fs"; +import * as NodeHttp from "node:http"; import * as NodePath from "node:path"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; @@ -5796,6 +5797,128 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + it.live("Stop closes an MCP-over-ACP call stalled on its SSE response", () => + Effect.gen(function* () { + const responseStarted = yield* Deferred.make(); + const responseClosed = yield* Deferred.make(); + const server = yield* Effect.acquireRelease( + Effect.promise( + () => + new Promise((resolve) => { + const server = NodeHttp.createServer((_request, response) => { + response.on("close", () => Deferred.doneUnsafe(responseClosed, Effect.void)); + response.writeHead(200, { "content-type": "text/event-stream" }); + response.write('data: {"jsonrpc":"2.0","method":"notifications/progress"}\n\n'); + Deferred.doneUnsafe(responseStarted, Effect.void); + }); + server.listen(0, "127.0.0.1", () => resolve(server)); + }), + ), + (server) => + Effect.sync(() => { + server.closeAllConnections(); + server.close(); + }), + ); + const address = server.address(); + if (address === null || typeof address === "string") { + return yield* Effect.die("Expected a TCP address"); + } + type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"]; + let mcpConnect: Parameters[0] | undefined; + let mcpMessage: Parameters[0] | undefined; + const instanceId = ProviderInstanceId.make("acp-test-stop-mcp-call"); + const threadId = ThreadId.make("thread-acp-stop-mcp-call"); + const idAllocator = yield* IdAllocator.IdAllocatorV2; + yield* (yield* McpProviderSessions.McpProviderSessions).set({ + environmentId: EnvironmentId.make("environment-acp-stop-mcp-call"), + threadId, + providerSessionId: "mcp-session-acp-stop-mcp-call", + providerInstanceId: instanceId, + endpoint: `http://127.0.0.1:${address.port}/mcp`, + authorizationHeader: "Bearer stop-mcp-call-token", + browserToolsAvailable: false, + }); + const adapter = yield* makeAcpAdapterV2({ + instanceId, + flavor: { + driver: ACP_TEST_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + interruptPromptOnCancel: true, + makeRuntime: makeMockRuntime({ + childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner, + mockAgentPath: yield* bundledMockAgentPath, + environment: { T3_ACP_HANG_PROMPT_FOREVER: "1" }, + wrapRuntime: (runtime) => ({ + ...runtime, + handleMcpConnect: (handler) => + Effect.sync(() => { + mcpConnect = handler; + }).pipe(Effect.andThen(runtime.handleMcpConnect(handler))), + handleMcpMessage: (handler) => + Effect.sync(() => { + mcpMessage = handler; + }).pipe(Effect.andThen(runtime.handleMcpMessage(handler))), + }), + }), + }, + selfInvocation: yield* resolveSelfInvocation(), + clientTerminals: {}, + }); + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: process.cwd(), + }); + const modelSelection = { instanceId, model: "default" } as const; + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make("provider-session-acp-stop-mcp-call"), + modelSelection, + runtimePolicy, + }); + const providerThread = yield* runtime.ensureThread({ + threadId, + modelSelection, + runtimePolicy, + }); + const turnFiber = yield* runtime + .startTurn( + makeTurnInput({ + threadId, + providerThread, + instanceId, + runtimePolicy, + now: yield* DateTime.now, + }), + ) + .pipe(Effect.forkScoped); + if (mcpConnect === undefined || mcpMessage === undefined) { + return yield* Effect.die("ACP runtime must register MCP-over-ACP handlers"); + } + const { connectionId } = yield* mcpConnect( + { serverId: "t3-code" }, + { requestId: "stop-mcp-connect", method: "mcp/connect" }, + ); + const call = yield* mcpMessage( + { connectionId, method: "tools/call", params: { name: "task_status", arguments: {} } }, + { requestId: "stop-mcp-call", method: "mcp/message" }, + ).pipe(Effect.exit, Effect.forkScoped); + yield* Deferred.await(responseStarted); + + yield* runtime.interruptTurn({ + providerThread, + providerTurnId: idAllocator.derive.providerTurn({ + driver: ACP_TEST_DRIVER, + nativeTurnId: acpScopedNativeId(instanceId, "mock-session-1:turn:1"), + }), + }); + assert.isTrue(Exit.isFailure(yield* Fiber.join(call))); + yield* Deferred.await(responseClosed); + yield* Fiber.join(turnFiber); + }).pipe(Effect.provide(layerTest)), + ); + it.effect.each([ { label: "grants an ID-only execute approval its tracked command", diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 696ee6b30d8d7..7929572d71a5c 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -6243,10 +6243,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ), ); - const cancelPendingRuntimeRequests = Effect.fnUntraced(function* () { + const cancelPendingRuntimeRequests = Effect.fnUntraced(function* ( + options: { readonly stopMcpCalls: boolean } = { stopMcpCalls: true }, + ) { const waiting = turnAttached; turnAttached = yield* Deferred.make(); yield* Deferred.succeed(waiting, false); + // session/cancel does not end the agent's MCP calls; T3 runs them, so it stops them. + if (options.stopMcpCalls && runtimeMcpBridge !== undefined) { + yield* runtimeMcpBridge.stopCalls; + } const requests = yield* Ref.modify(pendingRuntimeRequests, (current) => [ [...current.values()], new Map(), @@ -9622,7 +9628,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( if (hardRestart || turnInput.requestRuntimeRestart === true) { yield* quarantineStoppedRun(); } - yield* cancelPendingRuntimeRequests(); + // A settled soft interrupt keeps background subagents, and with them their MCP calls. + yield* cancelPendingRuntimeRequests({ stopMcpCalls: !settledSoftInterrupt }); // Finalize before killing so projection catches up and concurrent interrupts see activeTurn cleared. if ( (hardRestart || context.promptSettled || settledSoftInterrupt) && diff --git a/packages/provider-acp/src/server/mcpOverAcpBridge.ts b/packages/provider-acp/src/server/mcpOverAcpBridge.ts index 449b05019af27..3ec47074f1445 100644 --- a/packages/provider-acp/src/server/mcpOverAcpBridge.ts +++ b/packages/provider-acp/src/server/mcpOverAcpBridge.ts @@ -1,4 +1,5 @@ // @effect-diagnostics globalJsonInEffect:off +import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; import * as Semaphore from "effect/Semaphore"; @@ -81,6 +82,8 @@ export interface AcpMcpOverAcpBridge { readonly disconnect: ( request: AcpSchema.DisconnectMcpRequest, ) => Effect.Effect; + /** Fails every call in flight, so Stop releases their HTTP responses. */ + readonly stopCalls: Effect.Effect; readonly dispose: Effect.Effect; } @@ -89,6 +92,7 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun ): Effect.fn.Return { const fetchImplementation = options.fetchImplementation ?? fetch; const connections = yield* Effect.sync(() => new Map()); + let callsStopped = yield* Deferred.make(); const connectionFor = (connectionId: string): Effect.Effect => Effect.suspend(() => { @@ -108,7 +112,7 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun connection.protocolVersion === null ? connection.mutex.withPermits(1)(effect) : effect, ); - const send = ( + const sendNow = ( connection: Connection, message: unknown, ): Effect.Effect, AcpMcpOverAcpError> => @@ -156,6 +160,21 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun }).pipe(Effect.mapError(bridgeError)), ); + const send = ( + connection: Connection, + message: unknown, + ): Effect.Effect, AcpMcpOverAcpError> => + Effect.suspend(() => + Effect.raceFirst( + sendNow(connection, message), + Deferred.await(callsStopped).pipe( + Effect.andThen( + Effect.fail(new AcpMcpOverAcpError({ message: "The MCP call was stopped." })), + ), + ), + ), + ); + const disconnect = Effect.fn("mcpOverAcpBridge.disconnect")(function* ( request: AcpSchema.DisconnectMcpRequest, ): Effect.fn.Return { @@ -244,6 +263,11 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun }); }), disconnect, + stopCalls: Effect.suspend(() => { + const stopped = callsStopped; + callsStopped = Deferred.makeUnsafe(); + return Deferred.succeed(stopped, undefined); + }).pipe(Effect.asVoid), dispose: Effect.suspend(() => Effect.forEach( [...connections.keys()], diff --git a/packages/shared/src/mcpResponsePayloads.test.ts b/packages/shared/src/mcpResponsePayloads.test.ts index 8027d6393f95b..a18a798e95bd1 100644 --- a/packages/shared/src/mcpResponsePayloads.test.ts +++ b/packages/shared/src/mcpResponsePayloads.test.ts @@ -1,5 +1,6 @@ import { expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; import * as Stream from "effect/Stream"; import { responsePayloads } from "./mcpResponsePayloads.ts"; @@ -91,3 +92,32 @@ it.effect("picks the SSE reader from the media type alone, whatever its case", ( expect(Array.from(jsonPayloads)).toEqual([{ jsonrpc: "2.0", id: 1, result: {} }]); }), ); + +it.effect("releases a stalled SSE response body when its consumer is interrupted", () => + Effect.gen(function* () { + const reading = Promise.withResolvers(); + let controller!: ReadableStreamDefaultController; + const body = new ReadableStream( + { + start(value) { + controller = value; + }, + pull() { + reading.resolve(); + }, + }, + { highWaterMark: 0 }, + ); + const response = new Response(body, { headers: { "content-type": "text/event-stream" } }); + const consumer = yield* Stream.runDrain(responsePayloads(response, String)).pipe( + Effect.forkChild, + ); + yield* Effect.promise(() => reading.promise); + yield* Fiber.interrupt(consumer); + try { + expect(body.locked, "interrupted SSE response must release its body").toBe(false); + } finally { + if (body.locked) controller.close(); + } + }), +); diff --git a/packages/shared/src/mcpResponsePayloads.ts b/packages/shared/src/mcpResponsePayloads.ts index 4c8d2e6fd306b..c591cf69372aa 100644 --- a/packages/shared/src/mcpResponsePayloads.ts +++ b/packages/shared/src/mcpResponsePayloads.ts @@ -4,12 +4,15 @@ import * as Stream from "effect/Stream"; const SSE_EVENT_SEPARATOR = /(?:\r\n|\r(?!\n)|\n){2}/u; -async function* sseDataLines(response: Response): AsyncGenerator { - if (response.body === null) return; +async function* sseDataLines( + reader: ReadableStreamDefaultReader, +): AsyncGenerator { const decoder = new TextDecoder(); let buffered = ""; - for await (const chunk of response.body) { - buffered += decoder.decode(chunk as Uint8Array, { stream: true }); + while (true) { + const { done, value } = await reader.read(); + if (done) return; + buffered += decoder.decode(value, { stream: true }); let separator = SSE_EVENT_SEPARATOR.exec(buffered); while (separator !== null) { const rawEvent = buffered.slice(0, separator.index); @@ -37,9 +40,22 @@ export function responsePayloads( } const mediaType = (response.headers.get("content-type") ?? "").split(";", 1)[0]!.trim(); if (mediaType.toLowerCase() === "text/event-stream") { - return Stream.fromAsyncIterable(sseDataLines(response), onError).pipe( - Stream.mapEffect((data) => Effect.try({ try: () => JSON.parse(data), catch: onError })), - ); + const body = response.body; + if (body === null) return Stream.empty; + // Interrupting fromAsyncIterable cannot return a generator parked on read(), so the + // reader is cancelled from a finalizer instead; that settles the read and closes the body. + return Stream.unwrap( + Effect.acquireRelease( + Effect.sync(() => body.getReader()), + (reader) => + Effect.promise(() => + reader + .cancel() + .catch(() => undefined) + .then(() => reader.releaseLock()), + ), + ).pipe(Effect.map((reader) => Stream.fromAsyncIterable(sseDataLines(reader), onError))), + ).pipe(Stream.mapEffect((data) => Effect.try({ try: () => JSON.parse(data), catch: onError }))); } return Stream.unwrap( Effect.tryPromise({ try: () => response.text(), catch: onError }).pipe( From 8753e4d36bfdc33f0df9046ea239ad8f361b8ff1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 20:19:18 -0800 Subject: [PATCH 006/106] fix(web,mobile): Send now tells you why it could not send (#31283) --- .../features/threads/ThreadQueueControl.tsx | 36 ++++++++------ .../chat/QueuedRunsControl.browser.test.tsx | 48 ++++++++++++++++++- .../src/components/chat/QueuedRunsControl.tsx | 35 +++++++++----- 3 files changed, 92 insertions(+), 27 deletions(-) diff --git a/apps/mobile/src/features/threads/ThreadQueueControl.tsx b/apps/mobile/src/features/threads/ThreadQueueControl.tsx index c251283ef8839..16ae5b6cf8093 100644 --- a/apps/mobile/src/features/threads/ThreadQueueControl.tsx +++ b/apps/mobile/src/features/threads/ThreadQueueControl.tsx @@ -191,22 +191,30 @@ export function ThreadQueueSheet({ route }: StaticScreenProps) { busyRef.current = true; setBusyRunId(runId); void Haptics.selectionAsync(); - if (action === "remove") { - await cancel(buildCancelQueuedRunCommand({ ...target, runId })); - } else if (workflow?.canSendNow) { - await resume({ ...target, input: { threadId: target.threadId, runId } }); - } else if (workflow?.activeRun && workflow.canPromoteToSteer) { - await promote({ - ...target, - input: { - threadId: target.threadId, - queuedRunId: runId, - targetRunId: workflow.activeRun.id, - }, - }); - } + const result = + action === "remove" + ? await cancel(buildCancelQueuedRunCommand({ ...target, runId })) + : workflow?.canSendNow + ? await resume({ ...target, input: { threadId: target.threadId, runId } }) + : workflow?.activeRun && workflow.canPromoteToSteer + ? await promote({ + ...target, + input: { + threadId: target.threadId, + queuedRunId: runId, + targetRunId: workflow.activeRun.id, + }, + }) + : null; busyRef.current = false; setBusyRunId(null); + if (action === "steer" && result?._tag === "Failure" && !isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + Dialog.alert( + "Could not send queued message", + error instanceof Error ? error.message : "Try again.", + ); + } }; const content = ( diff --git a/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx b/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx index 8781327b5e8ad..4f4daf7c798b4 100644 --- a/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx +++ b/apps/web/src/components/chat/QueuedRunsControl.browser.test.tsx @@ -1,11 +1,18 @@ import { RegistryContext } from "@effect/atom-react"; import type { EnvironmentThreadState } from "@t3tools/client-runtime/state/threads"; -import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts"; +import { + CommandId, + EnvironmentId, + MessageId, + OrchestrationV2DispatchCommandError, + ThreadId, +} from "@t3tools/contracts"; import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations"; import { formatComposerContextReference, toComposerContextId, } from "@t3tools/shared/composerContextReferences"; +import * as Cause from "effect/Cause"; import * as DateTime from "effect/DateTime"; import * as Option from "effect/Option"; import { AsyncResult, type Atom, AtomRegistry } from "effect/reactivity"; @@ -16,6 +23,7 @@ import { page } from "vite-plus/test/browser"; const state = vi.hoisted(() => ({ threadState: null as unknown as Atom.Writable>, workflow: null as unknown, + commandResult: undefined as unknown, })); vi.mock(import("@t3tools/client-runtime/state/thread-workflows"), async (importOriginal) => ({ @@ -66,7 +74,7 @@ vi.mock(import("../../state/threads"), async (importOriginal) => { vi.mock(import("../../state/use-atom-command"), async (importOriginal) => ({ ...(await importOriginal()), - useAtomCommand: (() => async () => undefined) as never, + useAtomCommand: (() => async () => state.commandResult) as never, })); vi.mock(import("../../assets/assetUrls"), async (importOriginal) => ({ @@ -80,12 +88,14 @@ import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/th import { act, render as renderUi } from "../../../test/render"; import { makeStreamingTimelineFixture, makeThreadProjectionFixture } from "../../test-fixtures"; +import { ToastProvider } from "../ui/toast"; import { QueuedRunsControl } from "./QueuedRunsControl"; import { ExpandedImageDialog } from "./ExpandedImageDialog"; import type { ExpandedImagePreview } from "./ExpandedImagePreview"; function setup() { state.workflow = null; + state.commandResult = undefined; } const baseProps = { @@ -399,3 +409,37 @@ it("does not re-render while assistant text streams into the thread", async () = await act(() => registry.set(state.threadState, thread("Hello there", true))); expect(renders).toBeGreaterThan(settled); }); + +it("tells the user why Send now was rejected", async () => { + setup(); + const reason = "Target run run:active is stopping and cannot be steered."; + state.workflow = { + activeRun: { id: "run:active" }, + canPromoteToSteer: true, + queuedRuns: [ + { + run: { id: "run:queued", userMessageId: "message:queued" }, + text: "Follow up", + attachments: [], + }, + ], + }; + state.commandResult = AsyncResult.failure( + Cause.fail( + new OrchestrationV2DispatchCommandError({ + commandId: CommandId.make("command:promote"), + commandType: "queued-message.promote-to-steer", + message: reason, + detail: reason, + }), + ), + ); + await using _screen = await renderUi( + + + , + ); + + await page.getByRole("button", { name: "Send now" }).click(); + await vi.waitFor(() => expect(page.getByText(reason).element()).toBeVisible()); +}); diff --git a/apps/web/src/components/chat/QueuedRunsControl.tsx b/apps/web/src/components/chat/QueuedRunsControl.tsx index 8972bf468448b..49cbc8f050eea 100644 --- a/apps/web/src/components/chat/QueuedRunsControl.tsx +++ b/apps/web/src/components/chat/QueuedRunsControl.tsx @@ -1,4 +1,8 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment"; +import { + isAtomCommandInterrupted, + squashAtomCommandFailure, +} from "@t3tools/client-runtime/state/runtime"; import { deriveThreadQueueWorkflowState } from "@t3tools/client-runtime/state/thread-workflows"; import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations"; import { @@ -34,6 +38,7 @@ import { useAtomCommand } from "../../state/use-atom-command"; import { isImageAttachment, type ChatImageAttachment, type ChatMessage } from "../../types"; import { cn } from "~/lib/utils"; import { Button, InlineButton } from "../ui/button"; +import { toastManager } from "../ui/toast"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import type { TimestampFormat } from "@t3tools/contracts/settings"; import { GuestMessageName } from "./guestMessageStyle"; @@ -168,19 +173,27 @@ export function QueuedRunsControl({ if (!canSendRun(freshContext) || steerInFlightRef.current) return; steerInFlightRef.current = true; setBusyRunId(queuedRunId); - if (workflow?.canSendNow) { - await resume({ - environmentId: props.environmentId, - input: { threadId: props.threadId, runId: queuedRunId }, - }); - } else if (activeRun !== null) { - await promote({ - environmentId: props.environmentId, - input: { threadId: props.threadId, queuedRunId, targetRunId: activeRun.id }, - }); - } + const result = workflow?.canSendNow + ? await resume({ + environmentId: props.environmentId, + input: { threadId: props.threadId, runId: queuedRunId }, + }) + : activeRun !== null + ? await promote({ + environmentId: props.environmentId, + input: { threadId: props.threadId, queuedRunId, targetRunId: activeRun.id }, + }) + : null; steerInFlightRef.current = false; setBusyRunId(null); + if (result?._tag === "Failure" && !isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + toastManager.add({ + type: "error", + title: "Could not send queued message", + description: error instanceof Error ? error.message : "An error occurred.", + }); + } }; useImperativeHandle(ref, () => ({ From a5c28da6c8bf676e49bf285765fb5e51ae2c8c71 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 20:22:50 -0800 Subject: [PATCH 007/106] fix(server): a delegated task reports the answer its child gives after an awaited Monitor ends (#37901) --- .../SubagentProjection.test.ts | 26 +++++++++++++++++++ .../src/server/subagentProjection.ts | 6 ++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/SubagentProjection.test.ts b/apps/server/src/orchestration-v2/SubagentProjection.test.ts index 2f764233ba97b..476477070b9ad 100644 --- a/apps/server/src/orchestration-v2/SubagentProjection.test.ts +++ b/apps/server/src/orchestration-v2/SubagentProjection.test.ts @@ -365,6 +365,32 @@ it("waits for nested work and retains the report across monitor acknowledgements }); assert.equal(progress.state, "result_available"); assert.equal(progress.resultRun?.id, report.id); + const ended = { ...report, id: RunId.make("monitor-ended"), ordinal: 4 }; + const afterMonitorEnded = delegatedTaskProgress({ + ...projection, + runs: [run, report, monitor, ended], + messages: [ + { + ...artifacts.message, + runId: monitor.id, + notification: { + source: { kind: "monitor" }, + outcome: "updated", + summary: "Monitor update", + }, + }, + { + ...artifacts.message, + runId: ended.id, + notification: { + source: { kind: "monitor" }, + outcome: "completed", + summary: 'Monitor "ticks" finished', + }, + }, + ], + }); + assert.equal(afterMonitorEnded.resultRun?.id, ended.id); }); it("exposes the provider failure rather than a progress message from the failed run", () => { diff --git a/packages/provider-core/src/server/subagentProjection.ts b/packages/provider-core/src/server/subagentProjection.ts index f1b0a41905fb3..54ff6e0084e1a 100644 --- a/packages/provider-core/src/server/subagentProjection.ts +++ b/packages/provider-core/src/server/subagentProjection.ts @@ -301,7 +301,11 @@ export function delegatedTaskProgress(projection: { ["completed", "failed", "cancelled", "interrupted", "rolled_back"].includes(status); const monitorRuns = new Set( projection.messages - .filter((message) => message.notification?.source.kind === "monitor") + .filter( + (message) => + message.notification?.source.kind === "monitor" && + message.notification.outcome === "updated", + ) .map((message) => message.runId), ); const workRuns = projection.runs.filter( From 907ff258e07bdbfd5d659ca6c32aded1ffda2500 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 20:27:26 -0800 Subject: [PATCH 008/106] fix(provider-opencode): goal turns end when OpenCode pauses the goal silently (#37894) --- .../Adapters/OpenCode2AdapterV2.test.ts | 82 +++++++++++++++++-- .../src/server/v2/adapter.ts | 61 ++++++++------ 2 files changed, 111 insertions(+), 32 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts index e4c985daa1652..cd1bdc9044a5c 100644 --- a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts @@ -8074,12 +8074,13 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { ); it.effect.each([ - { status: "complete", reconnect: false }, - { status: "paused", reconnect: false }, - { status: "complete", reconnect: true }, + { status: "complete", reconnect: false, silent: false }, + { status: "paused", reconnect: false, silent: false }, + { status: "complete", reconnect: true, silent: false }, + { status: "paused", reconnect: false, silent: true }, ] as const)( - "keeps an OpenCode goal turn open until $status, with reconnect $reconnect", - ({ status, reconnect }) => + "keeps an OpenCode goal turn open until $status, with reconnect $reconnect and silent $silent", + ({ status, reconnect, silent }) => Effect.gen(function* () { const recovered = reconnected({ [SESSION]: { type: "running" } }); const { runtime, thread } = yield* resumed( @@ -8092,6 +8093,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { reply("session.command", null), event("rpc.opencode.goal.changed", { sessionID: SESSION, goal: nativeGoal("active") }), event("session.execution.succeeded", { sessionID: SESSION }), + out("rpc.call", ""), + reply("rpc.call", { output: nativeGoal(silent ? status : "active") }), ...(status === "complete" ? reconnect ? [ @@ -8104,7 +8107,14 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { ] : [event("session.execution.started", { sessionID: SESSION })] : []), - event("rpc.opencode.goal.changed", { sessionID: SESSION, goal: nativeGoal(status) }), + ...(silent + ? [] + : [ + event("rpc.opencode.goal.changed", { + sessionID: SESSION, + goal: nativeGoal(status), + }), + ]), ...(status === "complete" ? [ event("session.text.ended", { @@ -8251,6 +8261,64 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { }), ); + it.effect("a goal refresh that reconnect supersedes leaves the turn to reconnect's read", () => + Effect.gen(function* () { + const gate = makeProviderReplayGate(["before-reconnect", "reconnect-reading"]); + const recovered = reconnected({ [SESSION]: { type: "running" } }); + const elsewhere = { sessionID: "ses_elsewhere", goal: null }; + const { runtime, thread } = yield* resumed( + [ + out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "" }), + promptAccepted, + event("rpc.opencode.goal.changed", { sessionID: SESSION, goal: nativeGoal("active") }), + event("session.execution.succeeded", { sessionID: SESSION }), + out("rpc.call", ""), + reply("rpc.call", ""), + event("rpc.opencode.goal.changed", elsewhere, "before-reconnect"), + ...recovered.slice(0, 2), + out("rpc.call", ""), + event("rpc.opencode.goal.changed", elsewhere, "reconnect-reading"), + reply("rpc.call", { output: nativeGoal("active") }), + ...emptyShellInventory, + ...recovered.slice(2), + ...noOpenRequests, + event("session.execution.started", { sessionID: SESSION }), + event("session.text.ended", { + sessionID: SESSION, + assistantMessageID: "msg_goal_final", + ordinal: 0, + text: "Final goal output", + }), + event("rpc.opencode.goal.changed", { sessionID: SESSION, goal: nativeGoal("complete") }), + event("session.execution.succeeded", { sessionID: SESSION }), + ], + { version: "2.2.5", replayGate: gate }, + ); + const events = yield* runtime.events.pipe( + Stream.takeUntil((e) => e.type === "turn.terminal"), + Stream.runCollect, + Effect.forkScoped, + ); + yield* runtime.startTurn(turnInput(thread)); + yield* Effect.promise(() => gate.waitForReached("before-reconnect")); + yield* TestClock.adjust("3 seconds"); + gate.release("before-reconnect"); + yield* Effect.promise(() => gate.waitForReached("reconnect-reading")); + yield* TestClock.adjust("2 seconds"); + gate.release("reconnect-reading"); + const collected = yield* Fiber.join(events); + assert.isTrue( + collected.some( + (e) => + e.type === "turn_item.updated" && + e.turnItem.type === "assistant_message" && + e.turnItem.text === "Final goal output", + ), + ); + assert.deepInclude(collected.at(-1), { type: "turn.terminal", status: "completed" }); + }), + ); + it.effect.each([ { initial: "active", recovered: "paused" }, { initial: "paused", recovered: "active" }, @@ -8273,6 +8341,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { reply("rpc.call", { output: recovered === "unreadable" ? {} : nativeGoal(recovered) }), ...(recovered === "active" ? [ + out("rpc.call", ""), + reply("rpc.call", { output: nativeGoal("active") }), event("session.execution.started", { sessionID: SESSION }), event("session.text.ended", { sessionID: SESSION, diff --git a/packages/provider-opencode/src/server/v2/adapter.ts b/packages/provider-opencode/src/server/v2/adapter.ts index 2d787b0c5f0b9..0072f9d1897b7 100644 --- a/packages/provider-opencode/src/server/v2/adapter.ts +++ b/packages/provider-opencode/src/server/v2/adapter.ts @@ -1052,9 +1052,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr input: { sessionID: state.sessionId }, }); const goal = yield* decodeGoal(result.output); - yield* lock.withPermit( + return yield* lock.withPermit( Effect.gen(function* () { - if (state.goalRevision === revision) yield* updateGoal(state, goal); + if (state.goalRevision !== revision) return false; + yield* updateGoal(state, goal); + return true; }), ); }); @@ -3182,6 +3184,9 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr ) { if (end.status === "completed" && (turn.steers.size > 0 || goalHoldsTurn(state))) { turn.heldEnd = end; + // OpenCode can pause a goal without telling this server, such as another process's + // restart sweep, so confirm the goal is still active before waiting on it. + if (goalHoldsTurn(state) && !state.goalRefreshing) yield* refreshGoal(state); return; } yield* finishTurn(state, end); @@ -3192,6 +3197,33 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr ? finishTurn(state, turn.heldEnd) : Effect.void; + // Holds a goal turn until OpenCode's goal is read again; a failed read releases it. + const refreshGoal = Effect.fnUntraced(function* (state: ThreadState) { + state.goalFresh = false; + state.goalRefreshing = true; + state.goalRevision += 1; + const revision = state.goalRevision; + yield* Effect.forkIn( + readGoal(state).pipe( + Effect.timeout(INVENTORY_TIMEOUT), + Effect.catch(() => + Effect.logWarning("Could not refresh OpenCode goal state.").pipe(Effect.as(false)), + ), + Effect.flatMap((applied) => + lock.withPermit( + Effect.gen(function* () { + if (state.goalRevision === revision) state.goalRefreshing = false; + // A newer goal event or a reconnect's read owns settlement now. + else if (!applied) return; + if (state.active !== undefined) yield* endIfSettled(state, state.active); + }), + ), + ), + ), + sessionScope, + ); + }); + const handleEvent = Effect.fnUntraced(function* (event: OpenCode2Client.OpenCode2StreamEvent) { if (event.type === "shell.created") { const sessionId = stringField(event.data.info.metadata, "sessionID"); @@ -3353,33 +3385,10 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr if (!supportsNativeGoals()) return; const goal = yield* decodeGoal(event.data["goal"]).pipe(Effect.result); if (goal._tag === "Failure") { - state.goalFresh = false; - state.goalRefreshing = true; - state.goalRevision += 1; - const revision = state.goalRevision; yield* Effect.logWarning( "Could not decode an OpenCode goal update; refreshing its state.", ); - yield* Effect.forkIn( - readGoal(state).pipe( - Effect.timeout(INVENTORY_TIMEOUT), - Effect.catch(() => - Effect.logWarning( - "Could not refresh OpenCode goal state after an unreadable update.", - ), - ), - Effect.ensuring( - lock.withPermit( - Effect.gen(function* () { - if (state.goalRevision === revision) state.goalRefreshing = false; - if (state.active !== undefined) yield* endIfSettled(state, state.active); - }), - ), - ), - ), - sessionScope, - ); - return; + return yield* refreshGoal(state); } yield* updateGoal(state, goal.success); if (state.active !== undefined) yield* endIfSettled(state, state.active); From 77fe1d6a0242541b74d6062f9e9a1cfc7fea8f45 Mon Sep 17 00:00:00 2001 From: Simone Date: Tue, 17 Nov 2026 05:32:50 +0100 Subject: [PATCH 009/106] fix(cli): ignore non-executable paths when checking installation (#37903) Co-authored-by: Simone <185146821+Lucenx9@users.noreply.github.com> Co-authored-by: Julius Marminge --- apps/server/src/cli/install.test.ts | 9 ++++++++- apps/server/src/cli/install.ts | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/apps/server/src/cli/install.test.ts b/apps/server/src/cli/install.test.ts index 104718a568cd8..f7aaeca3120cc 100644 --- a/apps/server/src/cli/install.test.ts +++ b/apps/server/src/cli/install.test.ts @@ -72,13 +72,20 @@ it.layer(NodeServices.layer)("t3 install", (it) => { // Another t3 earlier on PATH still runs first, so `t3` would not run this one. const otherBin = path.join(root, "other-bin"); yield* fs.makeDirectory(otherBin); - yield* fs.writeFileString(path.join(otherBin, "t3"), ""); + yield* fs.writeFileString(path.join(otherBin, "t3"), "", { mode: 0o755 }); const shadowed = yield* provide(installCli(), { PATH: `${otherBin}:${path.join(root, "bin")}`, }); assert.isFalse(shadowed.onPath); assert.equal(shadowed.shadowedBy, path.join(otherBin, "t3")); assert.isUndefined(shadowed.pathCommand); + yield* fs.remove(path.join(otherBin, "t3")); + yield* fs.makeDirectory(path.join(otherBin, "t3")); + const directory = yield* provide(installCli(), { + PATH: `${otherBin}:${path.join(root, "bin")}`, + }); + assert.isTrue(directory.onPath); + assert.isUndefined(directory.shadowedBy); const recorded = yield* fs.readFileString(path.join(runtimeDir, "launchers")); assert.equal(recorded, `\n${yield* fs.realPath(path.join(root, "bin"))}/t3\n`); diff --git a/apps/server/src/cli/install.ts b/apps/server/src/cli/install.ts index b95e1ffefb359..4e6420842dc9d 100644 --- a/apps/server/src/cli/install.ts +++ b/apps/server/src/cli/install.ts @@ -1,5 +1,5 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; -import { pathSetupCommand } from "@t3tools/shared/shell"; +import { pathSetupCommand, resolveCommandPath } from "@t3tools/shared/shell"; import * as Console from "effect/Console"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; @@ -156,7 +156,7 @@ export const installCli = Effect.fn("cli.install.install")(function* () { if (!path.isAbsolute(entry) || detectCliRunner(`${entry}/`) !== null) continue; for (const name of windows ? ["t3.cmd", "t3.exe"] : ["t3"]) { const candidate = path.join(entry, name); - if (yield* fs.exists(candidate).pipe(Effect.orElseSucceed(() => false))) return candidate; + if (yield* resolveCommandPath(candidate, { env: environment })) return candidate; } } return undefined; From f0572cf1b96b979c7c98e6d5f855cc7c9f475697 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 20:35:46 -0800 Subject: [PATCH 010/106] fix(source-control-bitbucket): read ssh.bitbucket.org remotes as Bitbucket Cloud (#37892) --- packages/shared/src/cloneUrl.test.ts | 9 +++++++++ packages/shared/src/cloneUrl.ts | 10 ++++++++-- .../src/shared/definition.test.ts | 11 +++++++++++ .../src/shared/definition.ts | 13 ++++++------- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/packages/shared/src/cloneUrl.test.ts b/packages/shared/src/cloneUrl.test.ts index 03a041a70cbbf..279ffe104eefd 100644 --- a/packages/shared/src/cloneUrl.test.ts +++ b/packages/shared/src/cloneUrl.test.ts @@ -78,6 +78,15 @@ describe("cloneUrlForProtocol", () => { ); }); + it("switches Bitbucket Cloud between its SSH and HTTPS hosts", () => { + expect(cloneUrlForProtocol("https://bitbucket.org/workspace/repo.git", "ssh")).toBe( + "git@ssh.bitbucket.org:workspace/repo.git", + ); + expect(cloneUrlForProtocol("git@ssh.bitbucket.org:workspace/repo.git", "https")).toBe( + "https://bitbucket.org/workspace/repo.git", + ); + }); + it("preserves encoded Azure repository components and same-protocol URLs", () => { const ssh = "git@ssh.dev.azure.com:v3/Fabrikam/Project%20One/fab%20repo"; const https = "https://dev.azure.com/Fabrikam/Project%20One/_git/fab%20repo"; diff --git a/packages/shared/src/cloneUrl.ts b/packages/shared/src/cloneUrl.ts index 37ffdb54f1652..17f6a6db1aa0d 100644 --- a/packages/shared/src/cloneUrl.ts +++ b/packages/shared/src/cloneUrl.ts @@ -56,13 +56,19 @@ export function cloneUrlForProtocol(url: string, protocol: CloneProtocol): strin const azure = /^([^/?#]+)\/([^/?#]+)\/_git\/([^/?#]+)$/.exec(parsed.path); return azure ? `git@ssh.dev.azure.com:v3/${azure[1]}/${azure[2]}/${azure[3]}` : null; } + // Bitbucket Cloud serves SSH only from `ssh.bitbucket.org` and HTTPS only from `bitbucket.org`. + const host = /^(?:(?:alt)?ssh\.)?bitbucket\.org$/i.test(parsed.host) + ? protocol === "ssh" + ? "ssh.bitbucket.org" + : "bitbucket.org" + : parsed.host; // SCP paths are literal; URI paths use percent escapes. try { - if (protocol === "ssh") return `git@${parsed.host}:${decodeURIComponent(parsed.path)}`; + if (protocol === "ssh") return `git@${host}:${decodeURIComponent(parsed.path)}`; const path = parsed.scp ? parsed.path.split("/").map(encodeURIComponent).join("/") : parsed.path; - return `https://${parsed.host}/${path}`; + return `https://${host}/${path}`; } catch { return null; } diff --git a/packages/source-control-bitbucket/src/shared/definition.test.ts b/packages/source-control-bitbucket/src/shared/definition.test.ts index da844c2b2a2ab..8f33b724e328d 100644 --- a/packages/source-control-bitbucket/src/shared/definition.test.ts +++ b/packages/source-control-bitbucket/src/shared/definition.test.ts @@ -76,4 +76,15 @@ describe("Bitbucket remotes", () => { ); expect(recognize("https://notbitbucket.example.com/workspace/repo.git")).toBeNull(); }); + + it.each([ + "git@ssh.bitbucket.org:workspace/repo.git", + "ssh://git@altssh.bitbucket.org:443/workspace/repo.git", + ])("recognises Bitbucket Cloud's SSH host in %s", (url) => { + expect(recognize(url)).toEqual({ + kind: "bitbucket", + name: "Bitbucket", + baseUrl: "https://bitbucket.org", + }); + }); }); diff --git a/packages/source-control-bitbucket/src/shared/definition.ts b/packages/source-control-bitbucket/src/shared/definition.ts index 8084a56c3b95c..69ee21f005acf 100644 --- a/packages/source-control-bitbucket/src/shared/definition.ts +++ b/packages/source-control-bitbucket/src/shared/definition.ts @@ -124,14 +124,13 @@ export const definition = defineSourceControl({ ); }, checkoutChangeRequestHost: () => null, + // Cloud also clones from `ssh.bitbucket.org` and `altssh.bitbucket.org`. recognizeRemote: ({ host, hostname }) => - hostname === "bitbucket.org" || hostname.split(".").includes("bitbucket") - ? { - kind: KIND, - name: hostname === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted", - baseUrl: `https://${host}`, - } - : null, + hostname === "bitbucket.org" || hostname.endsWith(".bitbucket.org") + ? { kind: KIND, name: "Bitbucket", baseUrl: "https://bitbucket.org" } + : hostname.split(".").includes("bitbucket") + ? { kind: KIND, name: "Bitbucket Self-Hosted", baseUrl: `https://${host}` } + : null, isChangeRequestInRepository: (identity, link) => isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link), canReadChangeRequestOnHost: (identity, link) => From 4f781196e12337e5c14a6f621fbb2715d425bd95 Mon Sep 17 00:00:00 2001 From: Michael Huo <60860148+mikeohuo@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:41:15 -0500 Subject: [PATCH 011/106] feat(server): t3.json can name sparse-checkout patterns for new worktrees (#26820) Co-authored-by: maria-rcks Co-authored-by: Julius Marminge --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 150 +++++++++++++++++++ apps/server/src/vcs/GitVcsDriverCore.ts | 141 ++++++++++++++--- docs/user/project-settings.md | 9 ++ packages/contracts/src/t3ProjectFile.ts | 9 ++ 4 files changed, 287 insertions(+), 22 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index efaf9826408cd..7f6a6442ce501 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -6906,6 +6906,51 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }).pipe(withGitFileProtocol), ); + it.effect("sparse worktrees read .gitmodules and t3.json from HEAD", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const pathService = yield* Path.Path; + const submoduleRepo = yield* makeTmpDir("git-submodule-"); + yield* initRepoWithCommit(submoduleRepo); + yield* writeTextFile(submoduleRepo, "SHARED.md", "# shared\n"); + yield* git(submoduleRepo, ["add", "."]); + yield* git(submoduleRepo, ["commit", "-m", "shared"]); + const cwd = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + yield* git(cwd, ["submodule", "add", NodeURL.pathToFileURL(submoduleRepo).href, "shared"]); + yield* writeTextFile(cwd, "sparse-patterns.txt", "/shared/\n"); + const driver = yield* GitVcsDriver.GitVcsDriver; + const worktrees = yield* makeTmpDir("git-worktrees-"); + + const createSparse = Effect.fn(function* (projectFile: string, branch: string) { + yield* writeTextFile(cwd, "t3.json", projectFile); + yield* git(cwd, ["add", "."]); + yield* git(cwd, ["commit", "-m", branch]); + const sparsePath = pathService.join(worktrees, branch); + // A relative path also covers the git commands that run inside the new worktree. + yield* driver.createWorktree({ + cwd, + path: pathService.relative(cwd, sparsePath), + refName: initialBranch, + newRefName: branch, + }); + assert.isFalse(yield* fileSystem.exists(pathService.join(sparsePath, ".gitmodules"))); + assert.isFalse(yield* fileSystem.exists(pathService.join(sparsePath, "t3.json"))); + return yield* fileSystem.exists(pathService.join(sparsePath, "shared", "SHARED.md")); + }); + + assert.isTrue( + yield* createSparse(`{ "worktreeSparsePatternsPath": "sparse-patterns.txt" }`, "sparse"), + ); + assert.isFalse( + yield* createSparse( + `{ "worktreeSparsePatternsPath": "sparse-patterns.txt", "worktreeSubmodules": "none" }`, + "sparse-none", + ), + ); + }).pipe(withGitFileProtocol), + ); + it.effect("still creates the worktree when submodule checkout fails", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; @@ -7157,6 +7202,111 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }).pipe(withGitFileProtocol), ); + it.effect("checks out only the t3.json sparse patterns in a new worktree", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const pathService = yield* Path.Path; + const repo = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(repo); + yield* writeTextFile(repo, "src/app.ts", "export {};\n"); + yield* writeTextFile(repo, "assets/large.bin", "large\n"); + yield* writeTextFile(repo, "sparse-patterns.txt", "/*\n!/assets/\n"); + yield* writeTextFile( + repo, + "t3.json", + `{ "worktreeSparsePatternsPath": "./sparse-patterns.txt" }`, + ); + yield* git(repo, ["add", "."]); + yield* git(repo, ["commit", "-m", "sparse patterns"]); + yield* git(repo, ["checkout", "-q", "-b", "previous"]); + yield* git(repo, ["checkout", "-q", initialBranch]); + const driver = yield* GitVcsDriver.GitVcsDriver; + const worktrees = yield* makeTmpDir("git-worktrees-"); + // A project opened at a subdirectory still reads both files from the repository root. + const cwd = pathService.join(repo, "src"); + + const assertSparse = Effect.fn(function* (worktreePath: string, branch: string) { + assert.isTrue(yield* fileSystem.exists(pathService.join(worktreePath, "src", "app.ts"))); + assert.isFalse( + yield* fileSystem.exists(pathService.join(worktreePath, "assets", "large.bin")), + ); + assert.equal(yield* git(worktreePath, ["sparse-checkout", "list"]), "/*\n!/assets/"); + assert.equal(yield* git(worktreePath, ["branch", "--show-current"]), branch); + }); + + const sparse = pathService.join(worktrees, "sparse"); + yield* driver.createWorktree({ + cwd, + path: pathService.relative(cwd, sparse), + refName: initialBranch, + newRefName: "feature/sparse", + }); + yield* assertSparse(sparse, "feature/sparse"); + + const fromPrevious = pathService.join(worktrees, "from-previous"); + yield* driver.createWorktree({ + cwd, + path: fromPrevious, + refName: "-", + newRefName: "feature/from-previous", + }); + yield* assertSparse(fromPrevious, "feature/from-previous"); + assert.notInclude(yield* git(repo, ["worktree", "list", "--porcelain"]), "locked"); + + // Only the sparse checkout runs the hook, so its failure removes the locked registration. + yield* writeTextFile(repo, ".git/hooks/post-checkout", "#!/bin/sh\nexit 1\n"); + yield* fileSystem.chmod(pathService.join(repo, ".git/hooks/post-checkout"), 0o755); + const failed = pathService.join(worktrees, "failed"); + yield* Effect.flip( + driver.createWorktree({ + cwd, + path: failed, + refName: initialBranch, + newRefName: "feature/failed", + }), + ); + assert.isFalse(yield* fileSystem.exists(failed)); + assert.notInclude(yield* git(repo, ["worktree", "list", "--porcelain"]), "feature/failed"); + assert.equal(yield* git(repo, ["branch", "--list", "feature/failed"]), ""); + }), + ); + + it.effect("fails without a worktree when the t3.json sparse pattern path is not a file", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const pathService = yield* Path.Path; + const cwd = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + yield* writeTextFile(cwd, "patterns/keep.txt", "/*\n"); + const driver = yield* GitVcsDriver.GitVcsDriver; + const worktrees = yield* makeTmpDir("git-worktrees-"); + for (const patternsPath of ["missing.txt", "patterns"]) { + yield* writeTextFile( + cwd, + "t3.json", + JSON.stringify({ worktreeSparsePatternsPath: patternsPath }), + ); + yield* git(cwd, ["add", "."]); + yield* git(cwd, ["commit", "-m", "missing sparse patterns"]); + const worktreePath = pathService.join(worktrees, patternsPath); + + const error = yield* Effect.flip( + driver.createWorktree({ + cwd, + path: worktreePath, + refName: initialBranch, + newRefName: `feature/${patternsPath}`, + }), + ); + + assert.include(error.detail, patternsPath); + assert.isFalse(yield* fileSystem.exists(worktreePath)); + assert.notInclude(yield* git(cwd, ["worktree", "list"]), worktreePath); + assert.equal(yield* git(cwd, ["branch", "--list", `feature/${patternsPath}`]), ""); + } + }), + ); + it.effect("still creates the worktree when the base-ref config stays locked", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 89bb1fa039ddd..ef45e272555ee 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -5018,6 +5018,47 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }, ); + // A new worktree has no files yet, so t3.json and its pattern file come from the ref itself. + const readWorktreeSparsePatterns = Effect.fn("readWorktreeSparsePatterns")(function* ( + cwd: string, + refName: string, + ) { + // `git show` does not read a lone - as the previous checkout either. + const ref = refName === "-" ? "@{-1}" : refName; + const projectFile = yield* executeGit( + "GitVcsDriver.createWorktree.readProjectFile", + cwd, + ["show", `${ref}:${T3_PROJECT_FILE_NAME}`], + { allowNonZeroExit: true }, + ); + if (projectFile.exitCode !== 0) return null; + const file = parseT3ProjectFile(projectFile.stdout); + if (file === null) { + yield* Effect.logWarning("t3.json is invalid; checking out the full worktree", { cwd, ref }); + return null; + } + if (file.worktreeSparsePatternsPath === undefined) return null; + // Git reads :./path from the working directory, so keep the path repository-relative. + const patternsPath = file.worktreeSparsePatternsPath.replace(/^(?:\.\/)+/, ""); + const patterns = patternsPath.split("/").includes("..") + ? null + : yield* executeGit( + "GitVcsDriver.createWorktree.readSparsePatterns", + cwd, + ["cat-file", "blob", `${ref}:${patternsPath}`], + { allowNonZeroExit: true }, + ); + if (patterns === null || patterns.exitCode !== 0) { + return yield* new GitCommandError({ + operation: "GitVcsDriver.createWorktree", + command: "git worktree add", + cwd, + detail: `t3.json names the sparse-checkout pattern file "${file.worktreeSparsePatternsPath}", which is not a file in ${refName}.`, + }); + } + return patterns.stdout; + }); + // A checkout stamps most files in the second it writes the index, and Git re-reads every file // stamped in the index's own second or later ("racily clean") on each status, diff and add. // T3's own git calls never rewrite a worktree's index, so refresh it once. @@ -5141,6 +5182,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* worktreePath = `${basePath}-${suffix}`; } } + // Later steps run git inside the worktree, so resolve a relative path the way worktree add does. + worktreePath = path.resolve(input.cwd, worktreePath); + // Read before the branch exists, so a missing pattern file leaves nothing behind. + const sparsePatterns = yield* readWorktreeSparsePatterns(input.cwd, input.refName); const newRefName = input.newRefName; // Older Git still creates worktrees normally, but cannot prove ownership for cancellation. const addHelp = newRefName @@ -5167,6 +5212,19 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const checkoutRef = newRefName ?? input.refName; const progress = options?.progress; const onCheckoutProgress = progress?.onCheckoutProgress; + const checkoutProgressOptions: ExecuteGitOptions = onCheckoutProgress + ? { + // Git only prints checkout progress when stderr is a tty or the + // delay elapsed. GIT_PROGRESS_DELAY=0 forces it through the pipe. + env: { GIT_PROGRESS_DELAY: "0" }, + progress: { + onStderrLine: (line) => { + const parsed = parseGitCheckoutProgressLine(line); + return parsed ? onCheckoutProgress(parsed) : Effect.void; + }, + }, + } + : {}; // Both reads leave the repository alone, so they share one round trip before the checkout. const [configuredCheckoutWorkers, base] = yield* Effect.all( @@ -5179,7 +5237,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* { concurrency: "unbounded" }, ); const checkoutWorkers = configuredCheckoutWorkers ?? "0"; - const fastCheckout = checkoutCache !== undefined && options?.fastCheckout === true; + // The cache clones a full checkout, so a sparse worktree uses Git's own checkout. + const fastCheckout = + checkoutCache !== undefined && options?.fastCheckout === true && sparsePatterns === null; const cacheGeneration = fastCheckout ? yield* checkoutCache.currentGeneration : 0; const stockCheckout = executeGitWithStableDiagnostics( "GitVcsDriver.createWorktree", @@ -5189,6 +5249,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* `checkout.workers=${checkoutWorkers}`, "worktree", "add", + ...(sparsePatterns === null ? [] : ["--no-checkout"]), ...(lockReason ? ["--lock", "--reason", lockReason] : []), "--", worktreePath, @@ -5202,21 +5263,42 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* // Hooks and filter drivers may print without bound; only progress lines are read. appendTruncationMarker: true, keepLineCallbacksAfterTruncation: true, - ...(onCheckoutProgress - ? { - // Git only prints checkout progress when stderr is a tty or the - // delay elapsed. GIT_PROGRESS_DELAY=0 forces it through the pipe. - env: { GIT_PROGRESS_DELAY: "0" }, - progress: { - onStderrLine: (line) => { - const parsed = parseGitCheckoutProgressLine(line); - return parsed ? onCheckoutProgress(parsed) : Effect.void; - }, - }, - } - : {}), + ...(sparsePatterns === null ? checkoutProgressOptions : {}), }, ); + const sparseCheckout = + sparsePatterns === null + ? Effect.void + : Effect.gen(function* () { + // The first sparse-checkout in a repository enables extensions.worktreeConfig. + yield* runGit( + "GitVcsDriver.createWorktree.sparseCheckout", + worktreePath, + ["sparse-checkout", "set", "--no-cone", "--stdin"], + { stdin: sparsePatterns, writesSharedConfig: true }, + ); + yield* executeGitWithStableDiagnostics( + "GitVcsDriver.createWorktree.checkout", + worktreePath, + ["-c", `checkout.workers=${checkoutWorkers}`, "checkout"], + { + fallbackErrorDetail: "git checkout failed", + timeoutMs: null, + appendTruncationMarker: true, + keepLineCallbacksAfterTruncation: true, + ...checkoutProgressOptions, + }, + ); + }).pipe( + // A worktree without its files is no use. removeWorktree refuses a locked worktree, + // so one that carries the lock reason is unlocked first. + Effect.onError(() => + (lockReason + ? releaseWorktreeRegistration(input.cwd, lockReason, true) + : removeWorktree({ cwd: input.cwd, path: worktreePath, force: true }) + ).pipe(Effect.ignore), + ), + ); // Set inside the claim's uninterruptible region: a pending cancel still surfaces as it ends. let claimed = false; const onWorktreeClaimed = progress?.onWorktreeClaimed; @@ -5233,6 +5315,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* .pipe(Effect.flatMap((cloned) => (cloned ? Effect.void : stockCheckout))) : stockCheckout ).pipe( + Effect.andThen(sparseCheckout), Effect.andThen( fastCheckout ? checkoutCache.schedule(worktreePath, cacheGeneration) : Effect.void, ), @@ -5279,9 +5362,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ); // worktree add leaves submodules empty, potentially omitting project skills, tools or source. - const hasSubmodules = yield* fileSystem - .exists(path.join(worktreePath, ".gitmodules")) - .pipe(Effect.orElseSucceed(() => false)); + // Ask HEAD, because sparse patterns can leave .gitmodules out of the working tree. + const hasSubmodules = yield* runGit( + "GitVcsDriver.createWorktree.findSubmodules", + worktreePath, + ["cat-file", "-e", "HEAD:.gitmodules"], + ).pipe( + Effect.as(true), + Effect.orElseSucceed(() => false), + ); const submoduleMode = !hasSubmodules ? { value: "none" as const, source: "environment" as const } : resolveProjectFileBackedSetting( @@ -5289,9 +5378,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* options?.submodules ?? null, options?.submodules != null ? null - : yield* fileSystem.readFileString(path.join(worktreePath, T3_PROJECT_FILE_NAME)).pipe( - Effect.flatMap((contents) => { - const file = parseT3ProjectFile(contents); + : // Read HEAD for the same reason: sparse patterns can leave t3.json out too. + yield* executeGit( + "GitVcsDriver.createWorktree.readProjectFile", + worktreePath, + ["show", `HEAD:${T3_PROJECT_FILE_NAME}`], + { allowNonZeroExit: true }, + ).pipe( + Effect.flatMap((result) => { + if (result.exitCode !== 0) return Effect.succeed(null); + const file = parseT3ProjectFile(result.stdout); return file === null ? Effect.logWarning("t3.json is invalid; initializing submodules recursively", { worktreePath, @@ -5347,8 +5443,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* yield* runGitStdout("GitVcsDriver.createWorktree.listSubmodules", worktreePath, [ "config", "-z", - "--file", - ".gitmodules", + // HEAD, not the working tree: sparse patterns can leave .gitmodules out. + "--blob", + "HEAD:.gitmodules", "--get-regexp", "^submodule\\..*\\.path$", ]), diff --git a/docs/user/project-settings.md b/docs/user/project-settings.md index 899640ad23590..c07ba6f17c94f 100644 --- a/docs/user/project-settings.md +++ b/docs/user/project-settings.md @@ -197,6 +197,15 @@ itself, or **Skip** to leave them for a setup script. It resolves in the same or workspace default: a `"worktreeSubmodules"` value in the `t3.json` of the branch being checked out applies when the project and environment are both on **Inherit**. +New worktrees check out every file. For a large repository, set `"worktreeSparsePatternsPath"` in +`t3.json` to a repository-relative file of git sparse-checkout patterns in non-cone syntax. New +worktrees then check out only the matching files, before any other file is written. Both files are +read from the branch being checked out, so the patterns can leave out `t3.json` and `.gitmodules`. +If the pattern file is missing, creating the worktree fails instead of checking out everything. +Submodules still follow **Submodules** above, also under excluded paths; set it to **Skip** to leave +them empty. The first sparse worktree turns on Git's `extensions.worktreeConfig` for the repository. +This needs Git 2.35 or newer. + ## Worktree location New worktrees go in the `worktrees` folder of the T3 home directory. To put them somewhere else, diff --git a/packages/contracts/src/t3ProjectFile.ts b/packages/contracts/src/t3ProjectFile.ts index 2bed72cc5a252..1855fb7db7d62 100644 --- a/packages/contracts/src/t3ProjectFile.ts +++ b/packages/contracts/src/t3ProjectFile.ts @@ -102,6 +102,15 @@ export const T3ProjectFile = Schema.Struct({ 'How new worktrees populate git submodules: "recursive" (the default) initializes nested submodules too, "top-level" initializes only those declared by this repository, and "none" leaves every submodule empty for a setup script to handle. A project or environment setting in T3 Code overrides this.', }), ), + worktreeSparsePatternsPath: Schema.optionalKey( + trimmedNonEmpty( + { + description: + 'Repository-relative path to a git sparse-checkout pattern file in non-cone syntax (e.g. ".t3/sparse-checkout"). New worktrees check out only the files it matches. Both files are read from the branch being checked out; a missing pattern file fails the worktree instead of checking out everything.', + }, + T3_PROJECT_FILE_PATH_MAX_LENGTH, + ), + ), scripts: Schema.optionalKey( Schema.Array(T3ProjectFileScript) .annotate({ From ebd007e6aeb3984412689346a80cb47ebeac1f19 Mon Sep 17 00:00:00 2001 From: Jackson Miller Date: Mon, 16 Nov 2026 23:43:57 -0500 Subject: [PATCH 012/106] fix(web): View instructions closes the skill details popover (#37900) --- apps/web/src/components/ChatMarkdown.browser.test.tsx | 3 ++- apps/web/src/components/chat/SkillInlineText.tsx | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/ChatMarkdown.browser.test.tsx b/apps/web/src/components/ChatMarkdown.browser.test.tsx index 5821e70fcc94c..bce40138b9716 100644 --- a/apps/web/src/components/ChatMarkdown.browser.test.tsx +++ b/apps/web/src/components/ChatMarkdown.browser.test.tsx @@ -529,7 +529,7 @@ describe("ChatMarkdown workspace file opens", () => { }); describe("ChatMarkdown skill chips", () => { - it("shows the skill's details and opens its instructions", async () => { + it("shows the skill's details and opens its instructions, closing the details", async () => { setup(); const threadRef = { environmentId: EnvironmentId.make("workspace"), @@ -556,6 +556,7 @@ describe("ChatMarkdown skill chips", () => { await userEvent.click(page.getByRole("button", { name: "View instructions" })); expect(openFile).toHaveBeenCalledExactlyOnceWith(threadRef, "/skills/review/SKILL.md"); + await expect.element(page.getByText("Reviews the current branch.")).not.toBeInTheDocument(); }); it("names a mentioned model as its provider does, without a thread", async () => { diff --git a/apps/web/src/components/chat/SkillInlineText.tsx b/apps/web/src/components/chat/SkillInlineText.tsx index d2489e6ce2993..2691b8dafbb41 100644 --- a/apps/web/src/components/chat/SkillInlineText.tsx +++ b/apps/web/src/components/chat/SkillInlineText.tsx @@ -8,6 +8,7 @@ import { useRightPanelStore } from "~/rightPanelStore"; import { SKILL_CHIP_ICON_SVG } from "../composerInlineChip"; import { ContextChipPopover } from "../contextChipParts"; import { Button } from "../ui/button"; +import { PopoverClose } from "../ui/popover"; import { ModelMentionChip } from "./ModelMentionChip"; function SkillInlineText(props: { @@ -135,9 +136,12 @@ export function SkillChipPopover(props: {

{props.label}

{props.description || "No description is available for this skill."}

{props.onViewInstructions ? ( - + ) : null} From 84d34d40f8455c8024a371d3008ff15b75645f57 Mon Sep 17 00:00:00 2001 From: Pieterjan Muller <1681106+pjmuller@users.noreply.github.com> Date: Tue, 17 Nov 2026 05:51:23 +0100 Subject: [PATCH 013/106] feat(cli): choose model options when starting a thread with t3 thread start (#23932) Co-authored-by: Julius Marminge --- apps/server/src/cli/thread.test.ts | 46 ++++++++++++++++++++++++ apps/server/src/cli/thread.ts | 58 ++++++++++++++++++++++++------ docs/user/outside-agents.md | 5 +++ 3 files changed, 98 insertions(+), 11 deletions(-) diff --git a/apps/server/src/cli/thread.test.ts b/apps/server/src/cli/thread.test.ts index 5252d81b2a22f..a20331ee17d92 100644 --- a/apps/server/src/cli/thread.test.ts +++ b/apps/server/src/cli/thread.test.ts @@ -10,6 +10,7 @@ import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; import * as Path from "effect/Path"; import { ChildProcess, ChildProcessSpawner } from "effect/process"; import * as TestClock from "effect/testing/TestClock"; @@ -23,6 +24,7 @@ import { persistServerRuntimeState, } from "../serverRuntimeState.ts"; import { + launchModelSelection, readLatestMessages, resolveProject, threadCommand, @@ -371,6 +373,50 @@ it.effect("--wait reports its run's result when a later run is the one waiting", }), ); +it.effect("start sends each --option as a typed model option", () => + Effect.gen(function* () { + const selection = yield* launchModelSelection({ + provider: Option.some("codex"), + model: Option.some("gpt-6.1-sol"), + option: ["reasoningEffort=high", " fastMode = false ", "note=a=b"], + }); + + assert.deepStrictEqual(selection, { + instanceId: "codex", + model: "gpt-6.1-sol", + options: [ + { id: "reasoningEffort", value: "high" }, + { id: "fastMode", value: false }, + { id: "note", value: "a=b" }, + ], + }); + }), +); + +it.effect("start rejects a repeated --option before reaching the server", () => + Effect.gen(function* () { + const error = yield* Command.runWith(threadCommand, { version: "0.0.0" })([ + "start", + ".", + "hi", + "--provider", + "codex", + "--model", + "gpt-6.1-sol", + "--option", + "reasoningEffort=high", + "--option", + "reasoningEffort=low", + ]).pipe(Effect.flip); + + assert.strictEqual(error.message, "--option reasoningEffort is given more than once."); + }).pipe( + Effect.provide( + Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer, TestConsole.layer), + ), + ), +); + /** Serves t3_thread_read pages the way the server does: positions count every item. */ const timelineTools = (types: ReadonlyArray) => { const items = types.map((type, position) => ({ position, itemId: `item-${position}`, type })); diff --git a/apps/server/src/cli/thread.ts b/apps/server/src/cli/thread.ts index 98d18a6c9d240..3566b75e20773 100644 --- a/apps/server/src/cli/thread.ts +++ b/apps/server/src/cli/thread.ts @@ -717,6 +717,45 @@ const launchWorkspaceStrategy = (input: { }), }); +/** + * Options are model specific, so they need an explicit model. `true` and + * `false` become booleans because boolean options such as fastMode only match + * a boolean. + */ +export const launchModelSelection = Effect.fn("cli.thread.launchModelSelection")(function* (input: { + readonly provider: Option.Option; + readonly model: Option.Option; + readonly option: ReadonlyArray; +}) { + if (Option.isSome(input.provider) !== Option.isSome(input.model)) { + return yield* new ThreadUsageError({ detail: "--provider and --model go together." }); + } + if (Option.isNone(input.provider) || Option.isNone(input.model)) { + if (input.option.length > 0) { + return yield* new ThreadUsageError({ detail: "--option needs --provider and --model." }); + } + return undefined; + } + const options: Array<{ readonly id: string; readonly value: string | boolean }> = []; + for (const entry of input.option) { + const separator = entry.indexOf("="); + const id = separator < 0 ? "" : entry.slice(0, separator).trim(); + const value = separator < 0 ? "" : entry.slice(separator + 1).trim(); + if (id.length === 0 || value.length === 0) { + return yield* new ThreadUsageError({ detail: `--option '${entry}' is not id=value.` }); + } + if (options.some((option) => option.id === id)) { + return yield* new ThreadUsageError({ detail: `--option ${id} is given more than once.` }); + } + options.push({ id, value: value === "true" ? true : value === "false" ? false : value }); + } + return { + instanceId: input.provider.value, + model: input.model.value, + ...(options.length === 0 ? {} : { options }), + }; +}); + const jsonFlag = Flag.Boolean("json").pipe( Flag.withDescription("Print JSON instead of text."), Flag.withDefault(false), @@ -793,6 +832,12 @@ const threadStartCommand = Command.make("start", { Flag.withDescription("Model slug. Defaults to the project's default model."), Flag.optional, ), + option: Flag.String("option").pipe( + Flag.withDescription( + "Model option as id=value, such as effort=high. Repeat for more. Needs --provider and --model.", + ), + Flag.atLeast(0), + ), worktree: Flag.String("worktree").pipe( Flag.withDescription( "Run in a new git worktree based on this branch, tag, or commit, instead of the project folder.", @@ -811,9 +856,7 @@ const threadStartCommand = Command.make("start", { Effect.gen(function* () { const message = yield* readMessageArgument(flags.message); const workspaceStrategy = yield* launchWorkspaceStrategy(flags); - if (Option.isSome(flags.provider) !== Option.isSome(flags.model)) { - return yield* new ThreadUsageError({ detail: "--provider and --model go together." }); - } + const modelSelection = yield* launchModelSelection(flags); const title = yield* decodeTitle( Option.getOrElse(flags.title, () => titleFromMessage(message)), ).pipe(Effect.mapError(() => new ThreadUsageError({ detail: "The title is empty." }))); @@ -828,14 +871,7 @@ const threadStartCommand = Command.make("start", { title, message, ...(workspaceStrategy === undefined ? {} : { workspaceStrategy }), - ...(Option.isSome(flags.provider) && Option.isSome(flags.model) - ? { - modelSelection: { - instanceId: flags.provider.value, - model: flags.model.value, - }, - } - : {}), + ...(modelSelection === undefined ? {} : { modelSelection }), }, ThreadLaunchResult, ); diff --git a/docs/user/outside-agents.md b/docs/user/outside-agents.md index 84d00cc98b16c..97bc0d385a3d7 100644 --- a/docs/user/outside-agents.md +++ b/docs/user/outside-agents.md @@ -153,6 +153,11 @@ default model and permission mode, and runs in the project folder unless you pass `--worktree `. `send` never interrupts: a busy thread gets the message as its next turn. Pass `-` as the message to read it from stdin. +To choose the model, pass `--provider --model `. Add +`--option id=value` for each model option, such as `--option effort=high`. +Option ids and values depend on the model; `true` and `false` set on/off +options. + `--wait` prints the agent's reply. It exits 1 when the run fails or stops, and 2 when the agent waits for an answer you give in the app. Add `--json` for a result that always includes the thread id. From 0058a4abb3adda91313c8e22d9a183cac0c4b8d5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 20:54:19 -0800 Subject: [PATCH 014/106] fix(server): Antigravity finds the agy CLI on PATH when no binary path is set (#37916) --- apps/server/src/provider/AntigravityInstallation.test.ts | 5 +++++ apps/server/src/provider/AntigravityInstallation.ts | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/apps/server/src/provider/AntigravityInstallation.test.ts b/apps/server/src/provider/AntigravityInstallation.test.ts index dcd3e40030093..a9d98adf217ab 100644 --- a/apps/server/src/provider/AntigravityInstallation.test.ts +++ b/apps/server/src/provider/AntigravityInstallation.test.ts @@ -1032,6 +1032,11 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("Antigravity inst executablePath: alias, harnessPath: null, }); + expect(yield* installation.resolve(undefined, { PATH: cliDirectory })).toMatchObject({ + executablePath: alias, + harnessPath: null, + source: "path", + }); expect(yield* installation.resolve(other).pipe(Effect.flip)).toMatchObject({ operation: "resolve", }); diff --git a/apps/server/src/provider/AntigravityInstallation.ts b/apps/server/src/provider/AntigravityInstallation.ts index feb412867f2e4..7c33d909ad849 100644 --- a/apps/server/src/provider/AntigravityInstallation.ts +++ b/apps/server/src/provider/AntigravityInstallation.ts @@ -401,9 +401,11 @@ export const makeAntigravityInstallation = Effect.fn("AntigravityInstallation.ma ); return installed; } - for (const candidate of yield* commandCandidates(names.executable, processEnvironment)) { - const selected = yield* fromExternal(candidate, "path"); - if (selected) return selected; + for (const command of [names.executable, "agy"]) { + for (const candidate of yield* commandCandidates(command, processEnvironment)) { + const selected = yield* fromExternal(candidate, "path"); + if (selected) return selected; + } } return yield* installationError( "resolve", From 90a098bb4b5f22bb9178f1e4d129a0e09bedccbb Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 21:01:10 -0800 Subject: [PATCH 015/106] fix(provider-grok): grok update keeps a script install on Grok's own updater under npx (#37917) --- .../provider-grok/src/server/driver.test.ts | 39 +++++++++++++++++++ packages/provider-grok/src/server/driver.ts | 8 +++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/provider-grok/src/server/driver.test.ts b/packages/provider-grok/src/server/driver.test.ts index 56fc4d184c999..6f3fcd2d63a28 100644 --- a/packages/provider-grok/src/server/driver.test.ts +++ b/packages/provider-grok/src/server/driver.test.ts @@ -172,6 +172,45 @@ it.layer(layerTest)("GrokDriver", (it) => { }); } + // An `npx t3` server inherits npm_config_user_agent, which Grok ranks above `[cli] installer`. + it.effect.skipIf(windowsHost)("keeps a script install off the npm updater under npx", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const home = yield* fs.realPath(yield* fs.makeTempDirectoryScoped({ prefix: "t3-grok-" })); + const downloaded = path.join(home, ".grok", "downloads", "grok-linux-x86_64"); + const binaryPath = path.join(home, ".grok", "bin", "grok"); + yield* fs.makeDirectory(path.dirname(downloaded), { recursive: true }); + yield* fs.makeDirectory(path.dirname(binaryPath), { recursive: true }); + yield* fs.writeFileString(downloaded, "#!/bin/sh\n"); + yield* fs.chmod(downloaded, 0o755); + yield* fs.symlink(downloaded, binaryPath); + + const instance = yield* GrokDriver.create({ + instanceId: ProviderInstanceId.make("grok-script"), + displayName: "Grok test", + enabled: false, + environment: [ + { + name: "npm_config_user_agent", + value: "npm/11.6.2 node/v24.11.0 linux x64 workspaces/false", + sensitive: false, + }, + ], + config: { ...GrokDriver.defaultConfig(), binaryPath }, + }); + const updateEnv = (yield* instance.snapshot.resolveMaintenance()).update?.env ?? {}; + // The maintenance runner spawns with extendEnv, layering this over the server's process.env. + const env: NodeJS.ProcessEnv = { + npm_config_user_agent: "npm/11.6.2 node/v24.11.0", + ...updateEnv, + }; + expect(env.GROK_INSTALLER === "internal" || env.npm_config_user_agent === undefined).toBe( + true, + ); + }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawner)), + ); + it.effect("stays manual-only when the configured executable does not exist", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/packages/provider-grok/src/server/driver.ts b/packages/provider-grok/src/server/driver.ts index 41ccfe6519f44..4162aae23e9b3 100644 --- a/packages/provider-grok/src/server/driver.ts +++ b/packages/provider-grok/src/server/driver.ts @@ -99,7 +99,13 @@ const UPDATE: ProviderMaintenanceCapabilitiesResolver = { updateArgs: ["update"], updateLockKey: "grok", platform: context.platform, - env: { ...context.env, npm_config_allow_scripts: GROK_NPM_PACKAGE }, + env: { + ...context.env, + npm_config_allow_scripts: GROK_NPM_PACKAGE, + // An npx-launched server's npm_config_user_agent outranks Grok's `[cli] installer`, steering + // a script install to npm. The runner extends process.env; only undefined removes a key. + npm_config_user_agent: undefined, + }, }); }), }; From 428d365932a7331361b9e64485158a42ba9d34e1 Mon Sep 17 00:00:00 2001 From: Mateusz Choma Date: Tue, 17 Nov 2026 06:08:09 +0100 Subject: [PATCH 016/106] feat(server): configurable usage history retention (#28111) Co-authored-by: chomamateusz-agent Co-authored-by: Julius Marminge --- apps/server/src/serverSettings.test.ts | 7 +- apps/server/src/serverSettings.ts | 6 +- apps/server/src/usage/UsageService.test.ts | 249 +++++++++++++++--- apps/server/src/usage/UsageService.ts | 34 ++- docs/user/usage.md | 7 + packages/contracts/src/settings.ts | 11 + .../src/server/CursorUsageAccounts.ts | 14 +- .../src/server/accountCache.test.ts | 29 +- .../src/server/accountCache.ts | 8 +- 9 files changed, 316 insertions(+), 49 deletions(-) diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts index 873649cb168f5..03b89e495553c 100644 --- a/apps/server/src/serverSettings.test.ts +++ b/apps/server/src/serverSettings.test.ts @@ -2365,7 +2365,10 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" }).pipe(Effect.provide(layerServerSettings())), ); - it.effect("leaves an unreadable settings.json untouched instead of folding over it", () => + it.effect.each([ + { label: "malformed JSON", broken: '{"defaultAutoPull": tru' }, + { label: "invalid retention", broken: '{"usageHistoryRetentionDays": 0}' }, + ])("leaves settings.json with $label untouched and disables destructive defaults", ({ broken }) => Effect.gen(function* () { const serverConfig = yield* ServerConfig.ServerConfig; const fileSystem = yield* FileSystem.FileSystem; @@ -2380,12 +2383,12 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" ${"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.isFalse(settings.continueThreadsAfterServerUpdate); + assert.equal(settings.usageHistoryRetentionDays, "unlimited"); assert.deepEqual(settings.projectSettingsOverrides, {}); assert.equal(yield* fileSystem.readFileString(serverConfig.settingsPath), broken); diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts index 4dc30d29a6cff..81106d27b7c76 100644 --- a/apps/server/src/serverSettings.ts +++ b/apps/server/src/serverSettings.ts @@ -806,7 +806,11 @@ const make = Effect.gen(function* () { if (decoded._tag === "Failure" || legacySettings._tag === "Failure") { const failure = decoded._tag === "Failure" ? decoded : legacySettings; // The file may hold an opt-out we can no longer read; never resume threads on a guess. - settings = { ...managed.defaults, continueThreadsAfterServerUpdate: false }; + settings = { + ...managed.defaults, + continueThreadsAfterServerUpdate: false, + usageHistoryRetentionDays: "unlimited", + }; if (failure._tag === "Failure") { parseFailure = Option.some(Cause.squash(failure.cause)); yield* Effect.logWarning("failed to parse settings.json, using defaults", { diff --git a/apps/server/src/usage/UsageService.test.ts b/apps/server/src/usage/UsageService.test.ts index 01cbf3a9f52ce..9a7f7ab82c8f1 100644 --- a/apps/server/src/usage/UsageService.test.ts +++ b/apps/server/src/usage/UsageService.test.ts @@ -25,6 +25,7 @@ import { } from "@t3tools/contracts"; import * as Duration from "effect/Duration"; import * as Clock from "effect/Clock"; +import * as DateTime from "effect/DateTime"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; @@ -289,12 +290,14 @@ function makeFakeCursor() { events: [] as { readonly timestampMs: number; readonly outputTokens: number }[], error: null as string | null, gate: undefined as Deferred.Deferred | undefined, + started: undefined as Deferred.Deferred | undefined, calls: [] as { readonly sinceMs: number; readonly untilMs: number }[], credentials: [] as unknown[], }; const read = (credential: unknown, sinceMs: number, untilMs: number) => Effect.gen(function* () { state.calls.push({ sinceMs, untilMs }); + if (state.started !== undefined) yield* Deferred.succeed(state.started, undefined); state.credentials.push(credential); if (state.gate !== undefined) yield* Deferred.await(state.gate); const { accountKey, error } = state; @@ -1864,45 +1867,119 @@ describe("UsageService", () => { }), ); - it.effect("restores the Cursor account cache after a restart", () => + it.effect.each([ + { label: "default", retention: undefined, cutoff: CURSOR_NOW - 92 * 24 * HOUR_MS }, + { label: "unlimited", retention: "unlimited" as const, cutoff: 0 }, + { label: "36500 days", retention: 36500, cutoff: 0 }, + ])( + "propagates the $label cutoff and restores the Cursor account cache after a restart", + ({ retention, cutoff }) => + Effect.gen(function* () { + const { settings, home } = yield* setup; + yield* TestClock.setTime(CURSOR_NOW); + const before = makeFakeCursor(); + before.state.events = [ + { timestampMs: CURSOR_NOW - 30 * HOUR_MS, outputTokens: 5 }, + { timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 7 }, + ]; + yield* Effect.gen(function* () { + const first = yield* makeWithCursor(before.read); + const original = yield* first.readSummary({ ...WINDOW, awaitRefresh: true }); + assert.deepStrictEqual(before.state.calls, [{ sinceMs: cutoff, untilMs: CURSOR_NOW }]); + yield* first.awaitPersisted; + + const after = makeFakeCursor(); + after.state.events = before.state.events; + const restarted = yield* makeWithCursor(after.read); + const restored = yield* restarted.readSummary(WINDOW); + assert.isUndefined(cursorSource(restored)?.refreshing); + assert.deepStrictEqual(restored.buckets, original.buckets); + assert.deepStrictEqual(cursorSource(restored), cursorSource(original)); + assert.strictEqual(after.state.calls.length, 0); + + yield* TestClock.adjust(Duration.minutes(2)); + const refreshed = yield* restarted.readSummary({ ...WINDOW, awaitRefresh: true }); + assert.deepStrictEqual(refreshed.buckets, original.buckets); + assert.deepStrictEqual(after.state.calls, [ + { sinceMs: CURSOR_NOW - HOUR_MS, untilMs: CURSOR_NOW + 2 * 60 * 1000 }, + ]); + }).pipe( + Effect.provide( + layerService({ + prefix: "usage-service-cursor-restart", + environment: { CURSOR_API_KEY: "test-key" }, + home, + settings: { + ...settings, + ...(retention === undefined ? {} : { usageHistoryRetentionDays: retention }), + }, + }), + ), + ); + }), + ); + + it.effect("widens Cursor history after awaiting a shared narrower refresh", () => Effect.gen(function* () { const { settings, home } = yield* setup; yield* TestClock.setTime(CURSOR_NOW); - const before = makeFakeCursor(); - before.state.events = [ - { timestampMs: CURSOR_NOW - 30 * HOUR_MS, outputTokens: 5 }, + const cursor = makeFakeCursor(); + cursor.state.gate = yield* Deferred.make(); + cursor.state.started = yield* Deferred.make(); + cursor.state.events = [ + { timestampMs: CURSOR_NOW - 120 * 24 * HOUR_MS, outputTokens: 5 }, { timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 7 }, ]; - yield* Effect.gen(function* () { - const first = yield* makeWithCursor(before.read); - const original = yield* first.readSummary({ ...WINDOW, awaitRefresh: true }); - yield* first.awaitPersisted; - - const after = makeFakeCursor(); - after.state.events = before.state.events; - const restarted = yield* makeWithCursor(after.read); - const restored = yield* restarted.readSummary(WINDOW); - assert.isUndefined(cursorSource(restored)?.refreshing); - assert.deepStrictEqual(restored.buckets, original.buckets); - assert.deepStrictEqual(cursorSource(restored), cursorSource(original)); - assert.strictEqual(after.state.calls.length, 0); - - yield* TestClock.adjust(Duration.minutes(2)); - const refreshed = yield* restarted.readSummary({ ...WINDOW, awaitRefresh: true }); - assert.deepStrictEqual(refreshed.buckets, original.buckets); - assert.deepStrictEqual(after.state.calls, [ - { sinceMs: CURSOR_NOW - HOUR_MS, untilMs: CURSOR_NOW + 2 * 60 * 1000 }, - ]); - }).pipe( - Effect.provide( - layerService({ - prefix: "usage-service-cursor-restart", - home, - settings, + const context = yield* Layer.build( + layerCursorUsageAccounts( + Layer.succeed(CursorAccountReader.CursorAccountReader, { read: cursor.read }), + ).pipe( + Layer.provide( + layerService({ + prefix: "usage-service-cursor-shared-refresh", + home, + settings, + platform: "darwin", + }), + ), + ), + ); + const accounts = Context.get(context, CursorUsageAccounts.CursorUsageAccounts); + const input = { + instances: [ + { + instanceId: ProviderInstanceId.make("cursor"), environment: { CURSOR_API_KEY: "test-key" }, - }), + }, + ], + windowStartMs: 0, + retentionCutoffMs: CURSOR_NOW - 92 * 24 * HOUR_MS, + awaitRefresh: false, + }; + const narrow = yield* accounts.scan(input); + assert.isTrue(narrow[0]?.refreshing); + yield* Deferred.await(cursor.state.started); + const wider = yield* accounts + .scan({ ...input, retentionCutoffMs: 0, awaitRefresh: true }) + .pipe(Effect.forkChild({ startImmediately: true })); + assert.strictEqual(cursor.state.calls.length, 1); + yield* Deferred.succeed(cursor.state.gate, undefined); + const sources = yield* Fiber.join(wider); + assert.deepStrictEqual(cursor.state.calls, [ + { sinceMs: input.retentionCutoffMs, untilMs: CURSOR_NOW }, + { sinceMs: 0, untilMs: CURSOR_NOW }, + ]); + assert.strictEqual(sources[0]?.status, "ok"); + assert.deepStrictEqual( + sources[0]?.files?.flatMap((file) => + file.records.map(({ timestampMs, totals }) => ({ + timestampMs, + outputTokens: totals.outputTokens, + })), ), + cursor.state.events, ); + yield* accounts.awaitPersisted; }), ); @@ -3536,6 +3613,116 @@ describe("UsageService", () => { }), ); + it.effect.each([ + { label: "the default 92 days", retention: undefined, expected: 1 }, + { label: "unlimited", retention: "unlimited" as const, expected: 15 }, + ])( + "retains deleted transcripts according to $label across restart and settings changes", + ({ retention, expected }) => + Effect.gen(function* () { + const { transcript, settings, home } = yield* setup; + const now = Date.parse("2026-08-02T12:00:00Z"); + yield* TestClock.setTime(now); + const window = { ...WINDOW, sinceDay: UsageDay.make("2024-01-01") }; + const files = [92, 93, 180, 730].map((days, index) => ({ + path: `${transcript}-${index}.jsonl`, + timestamp: now - days * 86400000, + content: claudeLine(index, 2 ** index).replace( + "2026-08-01T10:00:00Z", + DateTime.formatIso(DateTime.makeUnsafe(now - days * 86400000)), + ), + })); + yield* Effect.promise(async () => { + for (const file of files) { + await NodeFSP.writeFile(file.path, file.content); + await NodeFSP.utimes(file.path, file.timestamp / 1000, file.timestamp / 1000); + } + }); + yield* Effect.gen(function* () { + const service = yield* UsageService.make; + const first = yield* service.readSummary(window); + assert.strictEqual(totalOutputTokens(first), 15); + yield* service.awaitPersisted; + yield* Effect.promise(() => Promise.all(files.map((file) => NodeFSP.rm(file.path)))); + const restarted = yield* UsageService.make; + const saved = yield* restarted.readSummary(window); + assert.strictEqual(totalOutputTokens(saved), expected); + assert.deepStrictEqual(saved.sources[0]?.fingerprint, first.sources[0]?.fingerprint); + const narrow = yield* restarted.readSummary(WINDOW); + assert.strictEqual(totalOutputTokens(narrow), 0); + assert.strictEqual(narrow.sources[0]?.distinctSessions, 0); + + const config = yield* ServerSettings.ServerSettingsService; + yield* config.updateSettings({ usageHistoryRetentionDays: 1 }); + assert.strictEqual(totalOutputTokens(yield* restarted.readSummary(window)), 0); + yield* restarted.awaitPersisted; + yield* config.updateSettings({ usageHistoryRetentionDays: "unlimited" }); + const afterPruning = yield* UsageService.make; + assert.strictEqual(totalOutputTokens(yield* afterPruning.readSummary(window)), 0); + }).pipe( + Effect.provide( + layerService({ + prefix: "usage-service-retention", + home, + settings: { + ...settings, + ...(retention === undefined ? {} : { usageHistoryRetentionDays: retention }), + }, + }), + ), + ); + }), + ); + + it.effect("keeps retained history when an obsolete scan finishes after retention widens", () => + Effect.gen(function* () { + const { transcript, settings, home } = yield* setup; + yield* TestClock.setTime(CURSOR_NOW); + const timestamp = CURSOR_NOW - 180 * 24 * HOUR_MS; + yield* Effect.promise(async () => { + await NodeFSP.writeFile( + transcript, + claudeLine(1, 11).replace( + "2026-08-01T10:00:00Z", + DateTime.formatIso(DateTime.makeUnsafe(timestamp)), + ), + ); + await NodeFSP.utimes(transcript, timestamp / 1000, timestamp / 1000); + }); + const started = yield* Deferred.make(); + const release = yield* Deferred.make(); + let scans = 0; + yield* Effect.gen(function* () { + const config = yield* ServerSettings.ServerSettingsService; + const service = yield* UsageService.make.pipe( + Effect.provideService(CursorUsageAccounts.CursorUsageAccounts, { + scan: () => + Effect.gen(function* () { + if (++scans === 1) { + yield* Deferred.succeed(started, undefined); + yield* Deferred.await(release); + } + return []; + }), + awaitPersisted: Effect.void, + }), + ); + const window = { ...WINDOW, sinceDay: UsageDay.make("2024-01-01") }; + const oldScan = yield* service.readSummary(window).pipe(Effect.forkChild); + yield* Deferred.await(started); + yield* config.updateSettings({ usageHistoryRetentionDays: "unlimited" }); + assert.strictEqual(totalOutputTokens(yield* service.readSummary(window)), 11); + yield* service.awaitPersisted; + yield* Effect.promise(() => NodeFSP.rm(transcript)); + yield* Deferred.succeed(release, undefined); + yield* Fiber.join(oldScan); + yield* service.awaitPersisted; + const restarted = yield* UsageService.make; + assert.strictEqual(totalOutputTokens(yield* restarted.readSummary(window)), 11); + }).pipe(Effect.provide(layerService({ prefix: "usage-retention-race", home, settings }))); + }), + ); + it.effect("credits the same copy of a duplicate after its transcripts are deleted", () => Effect.gen(function* () { const { transcript, settings, home } = yield* setup; diff --git a/apps/server/src/usage/UsageService.ts b/apps/server/src/usage/UsageService.ts index a59fbc66a02ca..30edc903a580f 100644 --- a/apps/server/src/usage/UsageService.ts +++ b/apps/server/src/usage/UsageService.ts @@ -77,7 +77,6 @@ const RATES_REFRESH_FLOOR_MS = 60 * 1000; const MTIME_SLACK_MS = 36 * 60 * 60 * 1000; const MAX_HOURLY_WINDOW_MS = 24 * 60 * 60 * 1000; -const CACHE_RETENTION_DAYS = 92; const STALE_TEMP_DIRECTORY_MS = 60 * 60 * 1000; const TRANSCRIPT_READ_CONCURRENCY = 4; @@ -872,7 +871,10 @@ export const make = Effect.gen(function* () { const windowStartMs = (hourlyWindow?.sinceTimeMs ?? DateTime.toEpochMillis(windowStart.value)) - MTIME_SLACK_MS; - const retentionCutoffMs = startedAtMs - CACHE_RETENTION_DAYS * 24 * 60 * 60 * 1000; + const retentionCutoffMs = + settings.usageHistoryRetentionDays === "unlimited" + ? 0 + : Math.max(0, startedAtMs - settings.usageHistoryRetentionDays * 24 * 60 * 60 * 1000); const awaitRefresh = input.awaitRefresh === true; const collect = Effect.gen(function* () { @@ -1144,14 +1146,26 @@ export const make = Effect.gen(function* () { }); } - const pruned = pruneScanCache(fileCache, retentionCutoffMs); - if (pruned > 0) { - cacheDirty = true; - // Pruned files are no longer saved, so nothing before the cutoff stays covered. - for (const [key, since] of coveredSince) { - if (since < retentionCutoffMs) coveredSince.set(key, retentionCutoffMs); - } - } + yield* settingsService + .withSettingsSnapshot((currentSettings) => + Effect.sync(() => { + if (settings.usageHistoryRetentionDays === currentSettings.usageHistoryRetentionDays) { + const pruned = pruneScanCache(fileCache, retentionCutoffMs); + if (pruned > 0) { + cacheDirty = true; + // Pruned files are no longer saved, so nothing before the cutoff stays covered. + for (const [key, since] of coveredSince) { + if (since < retentionCutoffMs) coveredSince.set(key, retentionCutoffMs); + } + } + } + }), + ) + .pipe( + Effect.catchCause((cause) => + Effect.logWarning("failed to read settings before pruning the usage cache", { cause }), + ), + ); const aggregated = aggregator.finish(); const readAt = yield* DateTime.now; diff --git a/docs/user/usage.md b/docs/user/usage.md index e6b3a46d5a14e..5160becc21567 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -20,6 +20,13 @@ record are missing from the totals. Text generation (thread titles, commit messages, PR text, branch names and review guides) is not in the totals for Codex or Claude Code 2.0.63 and newer, because those runs save no session history. +The server keeps scanned usage history for 92 days by default. Claude Code deletes its own +transcripts after 30 days, so for older Claude usage this history is the only record left. To keep +it longer, set `usageHistoryRetentionDays` in `/userdata/settings.json` to a number of days +such as `365`, or to `"unlimited"`. Usage views still show at most 90 days. Lowering the value +prunes older history on the next refresh, and raising it again does not bring pruned history back. +While `settings.json` cannot be read, nothing is pruned. + OpenCode reads its SQLite database and older JSON history. Antigravity reads local conversation databases, including T3-managed profiles. Set `OPENCODE_DATA_DIR` or `ANTIGRAVITY_DATA_DIR` on the server to read a different data directory; comma-separated paths read multiple directories. diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index 0013e288983e0..040b5e0736a31 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -1348,6 +1348,11 @@ export const StorageCleanupSettings = Schema.Struct({ }); export type StorageCleanupSettings = typeof StorageCleanupSettings.Type; +const UsageHistoryRetentionDays = Schema.Union([ + Schema.Int.check(Schema.isGreaterThan(0)), + Schema.Literal("unlimited"), +]); + const CloudSpeechCredential = SettingsSecretValue.pipe( Schema.withDecodingDefault(Effect.succeed("")), ); @@ -1583,6 +1588,10 @@ export const ServerSettings = Schema.Struct({ Schema.withDecodingDefault(Effect.succeed({})), ), + usageHistoryRetentionDays: UsageHistoryRetentionDays.pipe( + Schema.withDecodingDefault(Effect.succeed(92)), + ), + usagePriceOverrides: Schema.Record(TrimmedNonEmptyString, UsageModelPriceOverride).pipe( Schema.withDecodingDefault(Effect.succeed({})), ), @@ -1971,6 +1980,8 @@ export const ServerSettingsPatch = Schema.Struct({ Schema.Record(SecretProviderId, Schema.NullOr(SecretProviderConfigWrite)), ), + usageHistoryRetentionDays: Schema.optionalKey(UsageHistoryRetentionDays), + usagePriceOverrides: Schema.optionalKey( Schema.Record(TrimmedNonEmptyString, Schema.NullOr(UsageModelPriceOverride)), ), diff --git a/packages/provider-cursor/src/server/CursorUsageAccounts.ts b/packages/provider-cursor/src/server/CursorUsageAccounts.ts index 8c36c107a409c..088a59ca285ed 100644 --- a/packages/provider-cursor/src/server/CursorUsageAccounts.ts +++ b/packages/provider-cursor/src/server/CursorUsageAccounts.ts @@ -198,11 +198,19 @@ const make = Effect.gen(function* () { let refreshing = false; if ( (recentFailure === undefined || nowMs - recentFailure.atMs >= CURSOR_ACCOUNT_TTL_MS) && - !isCursorCacheFresh(caches.get(credentialKey), nowMs) + !isCursorCacheFresh(caches.get(credentialKey), nowMs, retentionStartMs) ) { const refresh = yield* startRefresh(credential, credentialKey, retentionStartMs); - if (awaitRefresh) yield* Deferred.await(refresh); - else refreshing = true; + if (awaitRefresh) { + yield* Deferred.await(refresh); + if ( + !failures.has(credentialKey) && + !isCursorCacheFresh(caches.get(credentialKey), nowMs, retentionStartMs) + ) { + const retry = yield* startRefresh(credential, credentialKey, retentionStartMs); + yield* Deferred.await(retry); + } + } else refreshing = true; } const cache = caches.get(credentialKey); diff --git a/packages/provider-cursor/src/server/accountCache.test.ts b/packages/provider-cursor/src/server/accountCache.test.ts index 939bc6fc22f03..f14e68efe19be 100644 --- a/packages/provider-cursor/src/server/accountCache.test.ts +++ b/packages/provider-cursor/src/server/accountCache.test.ts @@ -1,6 +1,6 @@ import { assert, describe, it } from "@effect/vitest"; -import { cursorFetchRange, mergeCursorFetch } from "./accountCache.ts"; +import { cursorFetchRange, isCursorCacheFresh, mergeCursorFetch } from "./accountCache.ts"; import type { UsageRecord } from "@t3tools/provider-core/server/usage"; const DAY_MS = 24 * 60 * 60 * 1000; @@ -31,6 +31,20 @@ describe("cursorAccountCache", () => { fetchedAtMs: 10 * DAY_MS, records: [record(5 * DAY_MS)], }; + assert.isTrue(isCursorCacheFresh(cache, 10 * DAY_MS, 0)); + const narrow = { ...cache, sinceMs: DAY_MS }; + assert.isFalse(isCursorCacheFresh(narrow, 10 * DAY_MS, 0)); + const widenedRange = cursorFetchRange(narrow, 0, 10 * DAY_MS); + assert.deepStrictEqual(widenedRange, { + sinceMs: 0, + untilMs: 10 * DAY_MS, + }); + const widened = mergeCursorFetch(narrow, "a", widenedRange, cache.records, 10 * DAY_MS, 0); + assert.isTrue(isCursorCacheFresh(widened.cache, 10 * DAY_MS, 0)); + assert.deepStrictEqual(cursorFetchRange(widened.cache, 0, 11 * DAY_MS), { + sinceMs: 10 * DAY_MS - 60 * 60 * 1000, + untilMs: 11 * DAY_MS, + }); const nowMs = 100 * DAY_MS; const range = cursorFetchRange(cache, 10 * DAY_MS, nowMs); assert.deepStrictEqual(range, { sinceMs: 10 * DAY_MS, untilMs: nowMs }); @@ -75,6 +89,19 @@ describe("cursorAccountCache", () => { ); assert.isTrue(pruned.changed); assert.deepStrictEqual(pruned.cache.records, [record(10 * DAY_MS)]); + assert.isTrue( + mergeCursorFetch(cache, "a", range, [record(10 * DAY_MS)], 11 * DAY_MS, 2 * DAY_MS).changed, + ); + assert.isTrue( + mergeCursorFetch( + { ...cache, sinceMs: DAY_MS, records: [] }, + "a", + { sinceMs: 0, untilMs: 11 * DAY_MS }, + [], + 11 * DAY_MS, + 0, + ).changed, + ); assert.isTrue( mergeCursorFetch( cache, diff --git a/packages/provider-cursor/src/server/accountCache.ts b/packages/provider-cursor/src/server/accountCache.ts index c5a68592423df..3d36083626bef 100644 --- a/packages/provider-cursor/src/server/accountCache.ts +++ b/packages/provider-cursor/src/server/accountCache.ts @@ -25,8 +25,13 @@ interface CursorFetchRange { export function isCursorCacheFresh( cache: CursorAccountCache | undefined, nowMs: number, + retentionStartMs: number, ): cache is CursorAccountCache { - return cache !== undefined && nowMs - cache.fetchedAtMs < CURSOR_ACCOUNT_TTL_MS; + return ( + cache !== undefined && + cache.sinceMs <= retentionStartMs && + nowMs - cache.fetchedAtMs < CURSOR_ACCOUNT_TTL_MS + ); } export function cursorFetchRange( @@ -61,6 +66,7 @@ export function mergeCursorFetch( } const changed = cache === undefined || + cache.sinceMs > retentionStartMs || pruned || replacedKeys.size !== added.length || added.some((record) => !replacedKeys.has(record.dedupeKey)); From 1b8ac6d77ee35ab8ce27d875352805db01bf791f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 21:09:40 -0800 Subject: [PATCH 017/106] fix(gitlab): large merge request files show counts and load their diff (#37898) --- .../src/server/GitLabPullRequestCli.test.ts | 84 ++++++++++++ .../src/server/GitLabPullRequestCli.ts | 122 ++++++++++++++++-- .../src/server/GitLabPullRequestProvider.ts | 1 + .../src/server/gitLabMergeRequestJson.test.ts | 1 + .../src/server/gitLabMergeRequestJson.ts | 9 +- 5 files changed, 201 insertions(+), 16 deletions(-) diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts index a0ef196677430..4d8ed5b95d0eb 100644 --- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts +++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts @@ -1129,6 +1129,90 @@ layer("GitLabPullRequestCli.layer", (it) => { }), ); + it.effect("counts a file GitLab withheld as too large and offers to read it alone", () => + Effect.gen(function* () { + mockedExecute + .mockReturnValueOnce( + Effect.succeed( + output( + JSON.stringify([ + { old_path: "src/a.ts", new_path: "src/a.ts", diff: "@@ -1 +1 @@\n-a\n+b\n" }, + { old_path: "src/huge.txt", new_path: "src/huge.txt", diff: "", too_large: true }, + ]), + ), + ), + ) + .mockReturnValueOnce( + Effect.succeed( + output( + JSON.stringify({ + data: { + project: { + mergeRequest: { + diffStatsSummary: { fileCount: 2 }, + diffStats: [ + { path: "src/a.ts", additions: 1, deletions: 1 }, + { path: "src/huge.txt", additions: 20000, deletions: 0 }, + ], + }, + }, + }, + }), + ), + ), + ); + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; + + const diff = yield* cli.getMergeRequestDiff({ cwd: "/w", repository: "acme/web", number: 7 }); + + expect(diff.omittedFileStats).toEqual([ + { path: "src/huge.txt", additions: 20000, deletions: 0 }, + ]); + assert.isTrue(diff.withheldFilesReadable); + }), + ); + + it.effect("reads one withheld file's section from the merge request's raw diff", () => + Effect.gen(function* () { + mockedExecute.mockReturnValueOnce( + Effect.succeed( + output( + [ + "diff --git a/src/a.ts b/src/a.ts", + "--- a/src/a.ts", + "+++ b/src/a.ts", + "@@ -1 +1 @@", + "-a", + "+b", + "diff --git a/src/huge.txt b/src/huge.txt", + "new file mode 100644", + "--- /dev/null", + "+++ b/src/huge.txt", + "@@ -0,0 +1,2 @@", + "+one", + "+two", + "", + ].join("\n"), + ), + ), + ); + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; + + const diff = yield* cli.getMergeRequestDiff({ + cwd: "/w", + repository: "acme/web", + number: 7, + path: "src/huge.txt", + }); + + expect(argsOfCall(0)[1]).toBe("projects/acme%2Fweb/merge_requests/7/raw_diffs"); + assert.strictEqual( + diff.patch, + "diff --git a/src/huge.txt b/src/huge.txt\nnew file mode 100644\n--- /dev/null\n+++ b/src/huge.txt\n@@ -0,0 +1,2 @@\n+one\n+two\n", + ); + }), + ); + it.effect( "pages a merge request diff 30 files at a time, which GitLab 15.7 to 17.10 accept", () => diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts index b8ad433c96df7..588e6a084fd7e 100644 --- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts +++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts @@ -1,5 +1,9 @@ import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage"; import { isCommitSha } from "@t3tools/source-control-core/server/pullRequestUtils"; +import { + diffFileSection, + truncateLongDiffLines, +} from "@t3tools/source-control-core/server/diffFileRevisions"; import * as Context from "effect/Context"; import * as Digest from "effect/crypto/Digest"; import * as DateTime from "effect/DateTime"; @@ -25,6 +29,7 @@ import type { PullRequestListState, PullRequestMergeCapabilities, PullRequestMergeMethod, + PullRequestOmittedFileStat, PullRequestReaction, PullRequestReactionContent, PullRequestReviewCommentDraft, @@ -344,6 +349,9 @@ const diffPageSize = (commit: string | undefined) => const CONVERSATION_PAGES = 10; const DIFF_MAX_OUTPUT_BYTES = 8 * 1024 * 1024; const DIFF_TIMEOUT_MS = 60_000; +const WITHHELD_FILE_DIFF_MAX_OUTPUT_BYTES = 32 * 1024 * 1024; +// Withheld files are often generated or minified, where one line can be megabytes. +const WITHHELD_FILE_MAX_LINE_LENGTH = 1_000; const isVcsProcessExitError = Schema.is(VcsProcessExitError); const DIFF_FILE_MAX_OUTPUT_BYTES = 1024 * 1024; // REST list rows carry full descriptions, so a page can pass the 1 MB default output cap. @@ -370,6 +378,8 @@ export interface GitLabMergeRequestDiffSlice { readonly patch: string; readonly truncated: boolean; readonly nextCursor: string | null; + readonly omittedFileStats?: ReadonlyArray; + readonly withheldFilesReadable?: boolean; } export class GitLabPullRequestCli extends Context.Service< @@ -482,6 +492,8 @@ export class GitLabPullRequestCli extends Context.Service< readonly number: number; readonly cursor?: string | undefined; readonly commit?: string | undefined; + /** Reads one file a page withheld as too large; ignores `cursor`. */ + readonly path?: string | undefined; }) => Effect.Effect; readonly getMergeRequestDiffFileContents: (input: { @@ -1174,7 +1186,13 @@ export const make = Effect.gen(function* () { readonly number: number; readonly page: number; readonly commit?: string | undefined; - }): Effect.Effect => + }): Effect.Effect< + GitLabMergeRequestDiffSlice & { + readonly withheldPaths: ReadonlyArray; + readonly legacy: boolean; + }, + GitLabPullRequestCliError + > => api({ cwd: input.cwd, host: input.host, @@ -1246,6 +1264,8 @@ export const make = Effect.gen(function* () { patch: patch.length === 0 ? patch : patch.replace(/\n?$/, "\n"), truncated: decoded.success.truncated, nextCursor: morePages ? String(input.page + 1) : null, + withheldPaths: decoded.success.withheldPaths, + legacy, }); }), ); @@ -1847,6 +1867,67 @@ export const make = Effect.gen(function* () { ): Effect.Effect => read.pipe(Effect.catchIf(isOptionalReadFailure, () => Effect.succeed(fallback))); + const mergeRequestFileStats = (input: { + readonly cwd: string; + readonly host?: string | undefined; + readonly repository: string; + readonly number: number; + }) => + graphqlRead({ + cwd: input.cwd, + host: input.host, + operation: "getMergeRequestFileStats", + query: MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY, + variables: { fullPath: input.repository, iid: String(input.number) }, + decode: decodeMergeRequestFileStatsJson, + }); + + // GitLab has no per-file diff, so a withheld file is cut from the whole raw diff here and + // only its section reaches the client. `/raw_diffs` needs GitLab 17.9 or newer. + const withheldFileDiff = (input: { + readonly cwd: string; + readonly host?: string | undefined; + readonly repository: string; + readonly number: number; + readonly commit?: string | undefined; + readonly path: string; + }): Effect.Effect => { + const readError = (message: string) => + new GitLabMergeRequestReadError({ + command: "glab", + cwd: input.cwd, + operation: "getMergeRequestDiff", + cause: new Error(message), + }); + if (input.commit !== undefined) { + return Effect.fail(readError("A commit's withheld files cannot be read one at a time.")); + } + return api({ + cwd: input.cwd, + host: input.host, + path: `projects/${projectPath(input.repository)}/merge_requests/${input.number}/raw_diffs`, + maxOutputBytes: WITHHELD_FILE_DIFF_MAX_OUTPUT_BYTES, + timeoutMs: DIFF_TIMEOUT_MS, + }).pipe( + Effect.flatMap((result) => { + const section = result.stdoutTruncated ? null : diffFileSection(result.stdout, input.path); + return section === null + ? Effect.fail( + readError( + result.stdoutTruncated + ? `The diff for ${input.path} is too large to load.` + : `GitLab's diff does not include ${input.path}.`, + ), + ) + : Effect.succeed({ + patch: truncateLongDiffLines(section, WITHHELD_FILE_MAX_LINE_LENGTH), + truncated: false, + nextCursor: null, + }); + }), + ); + }; + const graphqlMutation = (input: { readonly cwd: string; readonly host?: string | undefined; @@ -2305,15 +2386,8 @@ export const make = Effect.gen(function* () { listMergeRequestStats, - getMergeRequestFileStats: Effect.fn("GitLabPullRequestCli.getMergeRequestFileStats")((input) => - graphqlRead({ - cwd: input.cwd, - host: input.host, - operation: "getMergeRequestFileStats", - query: MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY, - variables: { fullPath: input.repository, iid: String(input.number) }, - decode: decodeMergeRequestFileStatsJson, - }), + getMergeRequestFileStats: Effect.fn("GitLabPullRequestCli.getMergeRequestFileStats")( + mergeRequestFileStats, ), getMergeRequestWatchFingerprint: Effect.fn( @@ -2426,13 +2500,33 @@ export const make = Effect.gen(function* () { number: input.number, ...(input.commit === undefined ? {} : { commit: input.commit }), }; - if (input.cursor === undefined) { - return yield* diffPage({ ...target, page: 1 }); + if (input.path !== undefined) { + return yield* withheldFileDiff({ ...target, path: input.path }); } - const page = diffCursorPage(input.cursor); + const page = input.cursor === undefined ? 1 : diffCursorPage(input.cursor); if (page === null) return yield* new GitLabDiffCursorError({ command: "glab", cwd: input.cwd }); - return yield* diffPage({ ...target, page }); + const { withheldPaths, legacy, ...slice } = yield* diffPage({ ...target, page }); + // A commit's diff has no per-file counts or raw diff to read a withheld file from. + if (input.commit !== undefined) return slice; + const readable = page === 1 && !legacy ? { withheldFilesReadable: true } : {}; + if (withheldPaths.length === 0) return { ...slice, ...readable }; + const stats = yield* orWhenUnanswered(mergeRequestFileStats(target), { + files: [], + truncated: true, + }); + const statsByPath = new Map(stats.files.map((file) => [file.path, file])); + const omittedFileStats = withheldPaths.flatMap((path) => { + const stat = statsByPath.get(path); + return stat === undefined + ? [] + : [{ path, additions: stat.additions, deletions: stat.deletions }]; + }); + return { + ...slice, + ...readable, + ...(omittedFileStats.length === 0 ? {} : { omittedFileStats }), + }; }), getMergeRequestDiffFileContents: Effect.fn( diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts index 1341205793bfd..9a3881b249f87 100644 --- a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts +++ b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts @@ -47,6 +47,7 @@ const CAPABILITIES: PullRequestCapabilities = { reviewers: { request: true, listCandidates: true, searchCandidates: true }, labels: true, edit: { changeRequest: true, comment: true, maintainerEdits: true }, + withheldFileDiff: true, }; const UNAVAILABLE_REQUIREMENT = { diff --git a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts index 6905daf47d729..22a21e242b28a 100644 --- a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts +++ b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts @@ -681,6 +681,7 @@ describe("decodeMergeRequestDiffsJson", () => { expect(result.truncated).toBe(true); expect(result.patch).toContain("diff --git a/big.bin b/big.bin"); + expect(result.withheldPaths).toEqual(["big.bin"]); }); it("reports how many files GitLab returned, so the caller can page", () => { diff --git a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts index daf00998711c7..995ae74939260 100644 --- a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts +++ b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts @@ -1014,6 +1014,8 @@ export interface GitLabMergeRequestPatch { readonly patch: string; readonly truncated: boolean; readonly rawCount: number; + /** Files GitLab sent without their diff because they are too large or collapsed. */ + readonly withheldPaths: ReadonlyArray; } export function decodeMergeRequestDiffsJson( @@ -1044,6 +1046,7 @@ export function decodeMergeRequestChangesJson( function mergeRequestPatch(entries: ReadonlyArray): GitLabMergeRequestPatch { const sections: string[] = []; + const withheldPaths: string[] = []; let truncated = false; for (const entry of entries) { const file = decodeDiffEntry(entry); @@ -1053,8 +1056,9 @@ function mergeRequestPatch(entries: ReadonlyArray): GitLabMergeRequestP } const value = file.value; const hunks = value.diff ?? ""; - if (hunks.length === 0) { - truncated = truncated || value.too_large === true || value.collapsed === true; + if (hunks.length === 0 && (value.too_large === true || value.collapsed === true)) { + truncated = true; + withheldPaths.push(value.new_path); } const { from, to } = diffHeaderPaths(value); const header = [ @@ -1078,6 +1082,7 @@ function mergeRequestPatch(entries: ReadonlyArray): GitLabMergeRequestP patch: sections.join("\n"), truncated, rawCount: entries.length, + withheldPaths, }; } From 55c134a346d5830399c82e647f29101fec22ecee Mon Sep 17 00:00:00 2001 From: Abhishek B R Date: Tue, 17 Nov 2026 11:01:02 +0530 Subject: [PATCH 018/106] fix(client-runtime): Tab completes a folder name when the list shows fuzzy matches (#37925) Co-authored-by: Julius Marminge --- .../client-runtime/src/state/filesystem.test.ts | 12 ++++++++++++ packages/client-runtime/src/state/filesystem.ts | 13 ++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/client-runtime/src/state/filesystem.test.ts b/packages/client-runtime/src/state/filesystem.test.ts index 3234454bc7a8c..f0c87f7c7f447 100644 --- a/packages/client-runtime/src/state/filesystem.test.ts +++ b/packages/client-runtime/src/state/filesystem.test.ts @@ -107,6 +107,18 @@ describe("filesystem browse model", () => { isComplete: false, }); expect(getFilesystemBrowseTabCompletion([], "x")).toBeNull(); + // The list also shows substring and fuzzy matches; Tab completes only the typed name. + expect(getFilesystemBrowseTabCompletion([entry("t3code"), entry("my-t3code")], "t3")).toEqual({ + name: "t3code", + isComplete: true, + }); + expect( + getFilesystemBrowseTabCompletion( + [entry("OpenSourced"), entry("OpenAI"), entry("reopen"), entry("photos")], + "op", + ), + ).toEqual({ name: "Open", isComplete: false }); + expect(getFilesystemBrowseTabCompletion([entry("t3code")], "tcd")).toBeNull(); }); it("matches folder names stored decomposed, as macOS Finder names them", () => { diff --git a/packages/client-runtime/src/state/filesystem.ts b/packages/client-runtime/src/state/filesystem.ts index c5b3072f4b199..5a5a907fd1829 100644 --- a/packages/client-runtime/src/state/filesystem.ts +++ b/packages/client-runtime/src/state/filesystem.ts @@ -135,7 +135,14 @@ export function getFilesystemBrowseTabCompletion( visibleEntries: ReadonlyArray, filterQuery: string, ): { readonly name: string; readonly isComplete: boolean } | null { - const [first, ...rest] = visibleEntries; + const lowerQuery = filterQuery.toLowerCase(); + const lowerNormalizedQuery = filterQuery.normalize("NFC").toLowerCase(); + const prefixEntries = visibleEntries.filter( + (entry) => + entry.name.toLowerCase().startsWith(lowerQuery) || + entry.name.normalize("NFC").toLowerCase().startsWith(lowerNormalizedQuery), + ); + const [first, ...rest] = prefixEntries; if (first === undefined) return null; if (rest.length === 0) return { name: first.name, isComplete: true }; let prefix = Array.from(first.name); @@ -151,8 +158,8 @@ export function getFilesystemBrowseTabCompletion( } const name = prefix.join(""); const lowerName = name.toLowerCase(); - return lowerName.length > filterQuery.toLowerCase().length && - visibleEntries.every((entry) => entry.name.toLowerCase().startsWith(lowerName)) + return lowerName.length > lowerQuery.length && + prefixEntries.every((entry) => entry.name.toLowerCase().startsWith(lowerName)) ? { name, isComplete: false } : null; } From 44ebe671951f1331db8e7bebbe8af30a2db665bd Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Tue, 17 Nov 2026 00:35:12 -0500 Subject: [PATCH 019/106] fix(web): preserve citations when adding review comments (#35300) Co-authored-by: Julius Marminge --- .../pullRequest/PullRequestCodeTab.tsx | 3 +- .../pullRequest/guide/GuideExcerpt.tsx | 3 +- .../pullRequestDetail.logic.test.ts | 47 +++++++++++++++---- .../pullRequest/pullRequestDetail.logic.ts | 10 +++- apps/web/src/reviewCommentContext.test.ts | 9 ++++ apps/web/src/reviewCommentContext.ts | 4 ++ 6 files changed, 65 insertions(+), 11 deletions(-) diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index 5acc37153d736..454cdaafcbf3a 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -97,6 +97,7 @@ import { cn } from "~/lib/utils"; import { createPullRequestDiffFileContentsLoader } from "~/lib/diffFileContents"; import { buildDiffReviewComment, + diffReviewSelectionId, resolveDiffReviewAnchor, type ReviewCommentContext, } from "~/reviewCommentContext"; @@ -1076,7 +1077,7 @@ function PullRequestCodeTab({ file === undefined ? null : buildDiffReviewComment({ - id: `pull-request-selection:${anchor.fileKey}:${anchor.range.start}:${anchor.range.end}`, + id: `pull-request-selection:${diffReviewSelectionId(anchor.fileKey, anchor.range)}`, sectionId: `pull-request:${detail.number}`, sectionTitle: `PR #${detail.number} review`, filePath: anchor.path, diff --git a/apps/web/src/components/pullRequest/guide/GuideExcerpt.tsx b/apps/web/src/components/pullRequest/guide/GuideExcerpt.tsx index 28b9898e644ba..309b5c126cced 100644 --- a/apps/web/src/components/pullRequest/guide/GuideExcerpt.tsx +++ b/apps/web/src/components/pullRequest/guide/GuideExcerpt.tsx @@ -18,6 +18,7 @@ import { import { PREFERRED_HIGHLIGHTER } from "~/lib/syntaxHighlighting"; import { buildDiffReviewComment, + diffReviewSelectionId, resolveDiffReviewAnchor, type ReviewCommentContext, } from "~/reviewCommentContext"; @@ -237,7 +238,7 @@ function ExcerptDiff({ const askAgent = (request: string) => { if (draft === null || onAskAgent === null) return; const comment = buildDiffReviewComment({ - id: `pull-request-guide:${excerpt.path}:${draft.range.start}:${draft.range.end}`, + id: `pull-request-guide:${diffReviewSelectionId(excerpt.path, draft.range)}`, sectionId: `pull-request-guide:${excerpt.path}`, sectionTitle: "Review guide", filePath: excerpt.path, diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts index b7c31f9a1c3c1..04931ff06ea28 100644 --- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts @@ -13,7 +13,14 @@ import { type ThreadPullRequestLink, } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; -import { reviewCommentContextLabel } from "~/lib/composerContextRecords"; +import { + formatComposerContextReference, + projectComposerContextForProvider, +} from "@t3tools/shared/composerContextReferences"; +import { + reviewCommentContextLabel, + reviewCommentContextRecord, +} from "~/lib/composerContextRecords"; import { buildAddSelectionToAgentHandoff, @@ -1027,35 +1034,59 @@ describe("asking about a change rather than working on it", () => { }); it("keeps both pull requests when two repositories share a number", () => { - const selection = (id: string) => ({ + const selection = (id: string, line = 12) => ({ id, sectionId: "pull-request:42", sectionTitle: "PR #42 review", filePath: "apps/web/src/page.tsx", - startIndex: 11, - endIndex: 17, - rangeLabel: "L12-L18", + startIndex: line - 1, + endIndex: line + 5, + rangeLabel: `L${line}-L${line + 6}`, text: "", diff: "+const answer = 42;", }); const otherUrl = "https://github.com/acme/other/pull/42"; const first = buildAddSelectionToAgentHandoff({ ...base, - comment: selection("pull-request-selection:t3code:page.tsx:12:18"), + comment: selection("pull-request-selection:page.tsx:12:18"), request: "why?", }); const second = buildAddSelectionToAgentHandoff({ ...base, url: otherUrl, - comment: selection("pull-request-selection:other:page.tsx:12:18"), + comment: selection("pull-request-selection:page.tsx:12:18"), request: "and here?", }); - const draft = appendHandoff(appendHandoff({ prompt: "", reviewComments: [] }, first), second); + const third = buildAddSelectionToAgentHandoff({ + ...base, + comment: selection("pull-request-selection:page.tsx:20:26", 20), + request: "and this other range in the first pull request?", + }); + const draft = appendHandoff( + appendHandoff(appendHandoff({ prompt: "", reviewComments: [] }, first), second), + third, + ); expect(draft.reviewComments.map((comment) => comment.pullRequest?.url)).toEqual([ base.url, undefined, otherUrl, undefined, + undefined, + ]); + expect( + draft.reviewComments + .filter((comment) => comment.diff !== "") + .map((comment) => { + const record = reviewCommentContextRecord(comment); + return projectComposerContextForProvider({ + text: formatComposerContextReference(record), + records: [record], + }); + }), + ).toEqual([ + expect.stringContaining(base.url), + expect.stringContaining(otherUrl), + expect.stringContaining(base.url), ]); }); diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts index 1f4c69ff74524..4f5c3948de520 100644 --- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts +++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts @@ -1342,7 +1342,15 @@ export function buildAddSelectionToAgentHandoff(input: { }): FixFindingsHandoff { return { prompt: bounded(input.request), - reviewComments: [pullRequestContextComment(input, []), { ...input.comment, text: "" }], + reviewComments: [ + pullRequestContextComment(input, []), + { + ...input.comment, + id: `pull-request-selection:${input.url}:${input.comment.id}`, + sectionTitle: `${input.comment.sectionTitle} (${boundedField(input.url)})`, + text: "", + }, + ], }; } diff --git a/apps/web/src/reviewCommentContext.test.ts b/apps/web/src/reviewCommentContext.test.ts index d538a0ac19908..a92e3c7ceb5df 100644 --- a/apps/web/src/reviewCommentContext.test.ts +++ b/apps/web/src/reviewCommentContext.test.ts @@ -6,6 +6,7 @@ import { buildDiffReviewComment, buildFileReviewComment, buildReviewCommentRenderablePatch, + diffReviewSelectionId, formatReviewCommentFence, resolveDiffReviewAnchor, resolveDiffReviewPosition, @@ -74,6 +75,14 @@ describe("review comment context parsing", () => { fenceLanguage: "diff", }), ); + const ids = ( + [ + { start: 2, end: 2, side: "deletions" }, + { start: 2, end: 2, side: "additions" }, + { start: 2, end: 2, side: "deletions", endSide: "additions" }, + ] as const + ).map((range) => diffReviewSelectionId("src/app.ts", range)); + expect(new Set(ids).size).toBe(ids.length); }); it("keeps a selection's trailing blank line and whitespace in the rendered excerpt", () => { diff --git a/apps/web/src/reviewCommentContext.ts b/apps/web/src/reviewCommentContext.ts index b169dd41885cf..37375f2dd6419 100644 --- a/apps/web/src/reviewCommentContext.ts +++ b/apps/web/src/reviewCommentContext.ts @@ -452,6 +452,10 @@ function formatDiffReviewRangeLabel(lines: ReadonlyArray): strin : `${marker}${firstNumber} to ${marker}${lastNumber}`; } +export function diffReviewSelectionId(fileKey: string, range: SelectedLineRange): string { + return `${fileKey}:${range.side ?? "additions"}:${range.start}:${range.endSide ?? range.side ?? "additions"}:${range.end}`; +} + export function buildDiffReviewComment(input: { id: string; sectionId: string; From 2b2d65d131332d9fb15f5390d2d79ab6e21e3825 Mon Sep 17 00:00:00 2001 From: primevishnu <71140952+primevishnu@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:06:32 +0530 Subject: [PATCH 020/106] fix(server): report managed runtime verification timeouts (#37896) Co-authored-by: Julius Marminge --- .../provider/AntigravityInstallation.test.ts | 58 +++++++++++++++++ .../src/provider/AntigravityInstallation.ts | 38 +++++++----- .../src/provider/CodexInstallation.test.ts | 62 +++++++++++++++++++ apps/server/src/provider/CodexInstallation.ts | 25 ++++---- 4 files changed, 155 insertions(+), 28 deletions(-) diff --git a/apps/server/src/provider/AntigravityInstallation.test.ts b/apps/server/src/provider/AntigravityInstallation.test.ts index a9d98adf217ab..8de41a4b3b364 100644 --- a/apps/server/src/provider/AntigravityInstallation.test.ts +++ b/apps/server/src/provider/AntigravityInstallation.test.ts @@ -17,6 +17,7 @@ import * as Schema from "effect/Schema"; import * as Scope from "effect/Scope"; import * as Sink from "effect/Sink"; import * as Stream from "effect/Stream"; +import * as TestClock from "effect/testing/TestClock"; import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http"; import * as ChildProcess from "effect/process/ChildProcess"; import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; @@ -717,6 +718,63 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("Antigravity inst }), ); + it.effect.each(["downloaded", "existing", "concurrent"] as const)( + "reports a verification timeout for a %s runtime without changing the active release", + (source) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const asset = yield* releaseAsset(); + const entered = yield* Deferred.make(); + let verificationCount = 0; + const { installation, stagingReleased, requests, validations } = yield* makeHarness({ + previous: true, + asset, + fileSystem: FileSystem.FileSystem.of({ + ...fs, + rename: (from, to) => + source === "concurrent" && path.basename(path.dirname(to)) === "versions" + ? writeRelease(path.dirname(path.dirname(to)), asset, false).pipe( + Effect.provideService(FileSystem.FileSystem, fs), + Effect.provideService(Path.Path, path), + Effect.andThen( + Effect.fail( + PlatformError.systemError({ + _tag: "AlreadyExists", + module: "FileSystem", + method: "rename", + pathOrDescriptor: to, + }), + ), + ), + ) + : fs.rename(from, to), + }), + validate: () => + Effect.suspend(() => + source === "concurrent" && verificationCount++ === 0 + ? Effect.void + : Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)), + ), + }); + if (source === "existing") { + yield* writeRelease(installation.managedDirectory, asset, false); + } + yield* installation.start; + yield* Deferred.await(entered); + yield* TestClock.adjust("90 seconds"); + expect(yield* terminalState(installation)).toMatchObject({ + phase: "failed", + message: + "Antigravity runtime verification timed out after 90 seconds. Check this environment's server logs for startup errors, then retry installation.", + }); + if (source !== "existing") yield* Deferred.await(stagingReleased); + yield* expectPreviousRelease(installation); + expect(requests).toHaveLength(source === "existing" ? 0 : 1); + expect(validations).toHaveLength(source === "concurrent" ? 2 : 1); + }), + ); + it.effect.each(["download", "extract", "active pointer"] as const)( "preserves the old runtime after an ENOSPC error during %s", (stage) => diff --git a/apps/server/src/provider/AntigravityInstallation.ts b/apps/server/src/provider/AntigravityInstallation.ts index 7c33d909ad849..f16849e2f56ea 100644 --- a/apps/server/src/provider/AntigravityInstallation.ts +++ b/apps/server/src/provider/AntigravityInstallation.ts @@ -444,6 +444,16 @@ export const makeAntigravityInstallation = Effect.fn("AntigravityInstallation.ma ), ); + const withValidationTimeout = Effect.timeoutOrElse({ + duration: VALIDATION_TIMEOUT, + orElse: () => + Effect.fail( + installationError( + "verify", + "Antigravity runtime verification timed out after 90 seconds. Check this environment's server logs for startup errors, then retry installation.", + ), + ), + }); const validate = options.validate ?? Effect.fn("AntigravityInstallation.validate")( @@ -606,10 +616,7 @@ export const makeAntigravityInstallation = Effect.fn("AntigravityInstallation.ma ); } yield* report("verifying", "Checking the installed runtime."); - yield* validate(existing, asset.version).pipe( - Effect.scoped, - Effect.timeout(VALIDATION_TIMEOUT), - ); + yield* validate(existing, asset.version).pipe(Effect.scoped, withValidationTimeout); yield* activate(); return; } @@ -866,7 +873,7 @@ export const makeAntigravityInstallation = Effect.fn("AntigravityInstallation.ma managedVersionDirectory: pairDirectory, }, asset.version, - ).pipe(Effect.scoped, Effect.timeout(VALIDATION_TIMEOUT)); + ).pipe(Effect.scoped, withValidationTimeout); const record: InstalledRelease = { releaseId, version: asset.version, @@ -889,10 +896,7 @@ export const makeAntigravityInstallation = Effect.fn("AntigravityInstallation.ma completedRelease(releaseId).pipe( Effect.flatMap((existing) => existing.version === asset.version - ? validate(existing, asset.version).pipe( - Effect.scoped, - Effect.timeout(VALIDATION_TIMEOUT), - ) + ? validate(existing, asset.version).pipe(Effect.scoped, withValidationTimeout) : Effect.fail( installationError( "activate", @@ -900,12 +904,14 @@ export const makeAntigravityInstallation = Effect.fn("AntigravityInstallation.ma ), ), ), - Effect.mapError(() => - installationError( - "activate", - "Could not publish the Antigravity runtime. The previous release is unchanged. Try again.", - cause, - ), + Effect.mapError((error) => + isInstallationError(error) && error.operation === "verify" + ? error + : installationError( + "activate", + "Could not publish the Antigravity runtime. The previous release is unchanged. Try again.", + cause, + ), ), ), ), @@ -1019,7 +1025,7 @@ export const makeAntigravityInstallation = Effect.fn("AntigravityInstallation.ma ? install(asset!) : validate(reuse, reuse.version!).pipe( Effect.scoped, - Effect.timeout(VALIDATION_TIMEOUT), + withValidationTimeout, Effect.andThen(ensureEligible(reuse.version!)), Effect.andThen( SubscriptionRef.update(state, (value) => ({ diff --git a/apps/server/src/provider/CodexInstallation.test.ts b/apps/server/src/provider/CodexInstallation.test.ts index 5536fe7a1704d..59f19b822012c 100644 --- a/apps/server/src/provider/CodexInstallation.test.ts +++ b/apps/server/src/provider/CodexInstallation.test.ts @@ -461,6 +461,68 @@ it.effect( }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), ); +it.effect.each(["cached update", "reverify"] as const)( + "reports a verification timeout during %s and preserves the managed runtime", + (operation) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-verify-test-" }); + const managed = `${baseDir}/tools/codex`; + const installedVersion = operation === "cached update" ? "0.156.0" : asset.version; + const target = "aarch64-pc-windows-msvc"; + const checksum = "a".repeat(64); + for (const version of new Set([installedVersion, asset.version])) { + const directory = `${managed}/${version}`; + yield* fs.makeDirectory(`${directory}/bin`, { recursive: true }); + yield* fs.makeDirectory(`${directory}/codex-path`, { recursive: true }); + for (const name of ["bin/codex.exe", "bin/codex-code-mode-host.exe", "codex-path/rg.exe"]) { + yield* fs.writeFileString(`${directory}/${name}`, "fixture executable"); + } + yield* fs.writeFileString( + `${directory}/codex-package.json`, + JSON.stringify({ layoutVersion: 1, version, target, entrypoint: "bin/codex.exe" }), + ); + yield* fs.writeFileString( + `${directory}/.install-complete.json`, + JSON.stringify({ version, target, sha256: checksum }), + ); + } + const pointer = JSON.stringify({ version: installedVersion }); + yield* fs.writeFileString(`${managed}/active.json`, pointer); + const entered = yield* Deferred.make(); + const h = yield* makeHarness({ + baseDir, + platform: "win32", + manifestCurrent: Effect.succeed({ + ...ModelManifest.BUNDLED_MODEL_MANIFEST, + compatibility: [ + { + driver: "codex", + t3CodeRange: ">=0.0.0", + ranges: [{ range: ">=0.156.0", status: "supported" }], + }, + ], + }), + options: { + releaseAsset: operation === "reverify" ? null : { ...asset, target, sha256: checksum }, + validate: () => Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)), + }, + }); + yield* h.installation.start; + yield* Deferred.await(entered); + yield* TestClock.adjust("90 seconds"); + expect(yield* terminalState(h.installation)).toMatchObject({ + phase: "failed", + installedVersion, + message: + "Codex runtime verification timed out after 90 seconds. Check this environment's server logs for startup errors, then retry installation.", + }); + expect(yield* fs.readFileString(`${managed}/active.json`)).toBe(pointer); + expect((yield* h.installation.resolve()).version).toBe(installedVersion); + expect(h.downloads()).toBe(0); + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), +); + it.effect("reports a download timeout and cleans the staging directory", () => Effect.gen(function* () { const started = yield* Deferred.make(); diff --git a/apps/server/src/provider/CodexInstallation.ts b/apps/server/src/provider/CodexInstallation.ts index c1e489ec0e1eb..7a9c80ebbf007 100644 --- a/apps/server/src/provider/CodexInstallation.ts +++ b/apps/server/src/provider/CodexInstallation.ts @@ -543,6 +543,16 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function ); return true; }); + const withValidationTimeout = Effect.timeoutOrElse({ + duration: "90 seconds", + orElse: () => + Effect.fail( + installationError( + "verify", + "Codex runtime verification timed out after 90 seconds. Check this environment's server logs for startup errors, then retry installation.", + ), + ), + }); const validate = options.validate ?? Effect.fn("CodexInstallation.validate")( @@ -613,10 +623,7 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function "The existing managed Codex release differs from the official package. Remove it and reinstall.", ); yield* ensureEligible(); - yield* validate(existing, release.version).pipe( - Effect.scoped, - Effect.timeout("90 seconds"), - ); + yield* validate(existing, release.version).pipe(Effect.scoped, withValidationTimeout); yield* activate(); return; } @@ -710,10 +717,7 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function ); const executable = yield* fromDirectory(runtime, release.version, release.target); yield* ensureEligible(); - yield* validate(executable, release.version).pipe( - Effect.scoped, - Effect.timeout("90 seconds"), - ); + yield* validate(executable, release.version).pipe(Effect.scoped, withValidationTimeout); yield* fs.writeFileString( path.join(runtime, ".install-complete.json"), yield* encodeRecord({ @@ -758,10 +762,7 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function function* () { const active = yield* readRecord(activePath, ActiveRelease); const executable = yield* completedRelease(active.version); - yield* validate(executable, executable.version).pipe( - Effect.scoped, - Effect.timeout("90 seconds"), - ); + yield* validate(executable, executable.version).pipe(Effect.scoped, withValidationTimeout); const kept = yield* keptInstall(executable.version); if (kept === null) { yield* SubscriptionRef.update(state, (current) => ({ ...current, ...offer(kept) })); From 5377eb9f73e2d1ad2eb7b7bb5d43e9208a795e99 Mon Sep 17 00:00:00 2001 From: Yarden Zamir Date: Tue, 17 Nov 2026 07:40:56 +0200 Subject: [PATCH 021/106] fix(contracts): place usageHistoryRetentionDays in a machine map column Upstream added the server setting usageHistoryRetentionDays (pingdotgg/t3code#28111). The machine map test needs every setting key in exactly one column; it is a shared-only setting like the other usage keys. --- packages/contracts/src/machineMap.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/contracts/src/machineMap.test.ts b/packages/contracts/src/machineMap.test.ts index adbb1a9057639..97850aaeae0d5 100644 --- a/packages/contracts/src/machineMap.test.ts +++ b/packages/contracts/src/machineMap.test.ts @@ -87,6 +87,7 @@ const SHARED_ONLY_SETTINGS: Readonly> "textGenerationModelSelection", "threadAddressing", "threadTitleInstructions", + "usageHistoryRetentionDays", "usageModelAliases", "usageModelFilter", "usagePriceOverrides", From a6016e64f8693a24600f1f2cce7419f436c66ce2 Mon Sep 17 00:00:00 2001 From: Simone Date: Tue, 17 Nov 2026 06:47:37 +0100 Subject: [PATCH 022/106] fix(server): match ignore wildcards by UTF-8 byte (#37927) Co-authored-by: Simone <185146821+Lucenx9@users.noreply.github.com> --- apps/server/src/workspace/IgnorePatterns.test.ts | 3 +++ apps/server/src/workspace/IgnorePatterns.ts | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/apps/server/src/workspace/IgnorePatterns.test.ts b/apps/server/src/workspace/IgnorePatterns.test.ts index f10345566eca5..35dba03ca1fb3 100644 --- a/apps/server/src/workspace/IgnorePatterns.test.ts +++ b/apps/server/src/workspace/IgnorePatterns.test.ts @@ -32,6 +32,9 @@ describe("parseIgnoreFile", () => { expect(decide("link-[0-9]", "link-7")).toBe("ignored"); expect(decide("link-[!0-9]", "link-7")).toBeUndefined(); expect(decide("link-?", "link-/")).toBeUndefined(); + expect(decide("link-?", "link-ø")).toBeUndefined(); + expect(decide("link-??", "link-ø")).toBe("ignored"); + expect(decide("link-ø", "link-ø")).toBe("ignored"); expect(decide("\\!bang\n\\#hash\nstar\\*", "!bang")).toBe("ignored"); expect(decide("\\!bang\n\\#hash\nstar\\*", "#hash")).toBe("ignored"); expect(decide("\\!bang\n\\#hash\nstar\\*", "starx")).toBeUndefined(); diff --git a/apps/server/src/workspace/IgnorePatterns.ts b/apps/server/src/workspace/IgnorePatterns.ts index 1180b02316ef9..b303c5bf1fa23 100644 --- a/apps/server/src/workspace/IgnorePatterns.ts +++ b/apps/server/src/workspace/IgnorePatterns.ts @@ -21,6 +21,12 @@ interface Pattern { export type IgnoreDecision = "ignored" | "included"; +const utf8 = new TextEncoder(); + +// Git wildcards consume UTF-8 bytes, including inside character classes. +const globBytes = (value: string) => + Array.from(utf8.encode(value), (byte) => String.fromCharCode(byte)); + function parseClass(chars: ReadonlyArray, start: number) { let index = start + 1; const negated = chars[index] === "!" || chars[index] === "^"; @@ -58,7 +64,7 @@ function parseLine(rawLine: string): Pattern | undefined { const anchored = line.slice(0, -1).includes("/"); if (line.startsWith("/")) line = line.slice(1); - const chars = Array.from(line); + const chars = globBytes(line); const tokens: Token[] = anchored ? [] : [{ type: "folders" }]; // A `[` that never closes leaves every later `[` unclosed too, so one failed scan settles them // all; rescanning the suffix for each would make `[[[[…` quadratic. @@ -158,7 +164,7 @@ export function parseIgnoreFile(contents: string): IgnoreFile { const patterns = contents.split("\n").flatMap((line) => parseLine(line) ?? []); return { decide: (relativePath, isFolder) => { - const path = Array.from(relativePath); + const path = globBytes(relativePath); for (let index = patterns.length - 1; index >= 0; index -= 1) { const pattern = patterns[index]!; if (pattern.folderOnly && !isFolder) continue; From c71f98cadf66f121d5e6ae25057bfc68792bc02c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 21:57:37 -0800 Subject: [PATCH 023/106] fix(server): thread reads no longer load every run of the thread (#37915) --- .../OrchestratorMcpService.activity.test.ts | 5 ++ apps/server/src/mcp/OrchestratorMcpService.ts | 60 +++++++++++---- apps/server/src/mcp/toolkits/core.test.ts | 1 + .../src/orchestration-v2/Orchestrator.ts | 6 ++ .../orchestration-v2/ProjectionStore.test.ts | 25 ++++++ .../src/orchestration-v2/ProjectionStore.ts | 77 ++++++++++++++----- .../ProviderTurnControlService.test.ts | 1 + .../ThreadManagementService.ts | 6 ++ apps/server/src/peer/RemoteDelegation.test.ts | 2 + .../src/relay/AgentAwarenessRelay.test.ts | 1 + 10 files changed, 149 insertions(+), 35 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts index 742b5969b64cb..4a7b08fbd870b 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts @@ -220,6 +220,7 @@ it("readThread preserves caller access errors and prefers activity-run status ov input.threadId === parentThreadId ? Effect.succeed(projection) : Effect.die(`unexpected thread ${input.threadId}`), + getRunCount: () => Effect.succeed(projection.runs.length), } satisfies Partial), ServerSettings.layerTest(), Layer.mock(ProviderRegistry.ProviderRegistry)({ @@ -364,6 +365,7 @@ it("readThread pages cut-off text without splitting an emoji across pages", asyn }, getThreadRecords: () => Effect.succeed(projection), getProjectThreadRecords: () => Effect.succeed(projection), + getRunCount: () => Effect.succeed(projection.runs.length), getThreadShell: () => Effect.succeed(projection.thread as unknown as OrchestrationV2ThreadShell), } satisfies Partial), @@ -471,6 +473,7 @@ it("readThread returns the newest items that fit the response budget", async () }, getThreadRecords: () => Effect.succeed(projection), getProjectThreadRecords: () => Effect.succeed(projection), + getRunCount: () => Effect.succeed(projection.runs.length), getThreadShell: () => Effect.succeed(projection.thread as unknown as OrchestrationV2ThreadShell), } satisfies Partial), @@ -577,6 +580,7 @@ it("readThread keeps an activity page within an MCP client's output limit and pa }, getThreadRecords: () => Effect.succeed(projection), getProjectThreadRecords: () => Effect.succeed(projection), + getRunCount: () => Effect.succeed(projection.runs.length), getThreadShell: () => Effect.succeed(projection.thread as unknown as OrchestrationV2ThreadShell), } satisfies Partial), @@ -858,6 +862,7 @@ it("readThread and sendToThread reach threads in other projects", async () => { hasMore: false, hasEarlier: false, }), + getRunCount: (threadId) => Effect.succeed(foreignProjection(threadId).runs.length), getProjectThreadRecords: (input) => input.threadId === foreignThreadId && input.projectId === foreignProjectId ? Effect.succeed(foreignProjection(input.threadId)) diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index b7908d1d74878..7fd6e0888daf5 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -973,6 +973,7 @@ function listItemFromShell( function threadDetail( scope: McpInvocationScope, projection: Pick, + runCount: number, itemCount: number, shell: OrchestrationV2ThreadShell, nowMs: number, @@ -1018,7 +1019,7 @@ function threadDetail( ...(projection.thread.delegatedFrom == null ? {} : { delegatedFrom: projection.thread.delegatedFrom }), - runCount: projection.runs.length, + runCount, itemCount, pendingRequestCount: projection.runtimeRequests.filter( (request) => request.status === "pending", @@ -1464,6 +1465,7 @@ const make = Effect.gen(function* () { const loadReadableThread = Effect.fn("OrchestratorMcpService.loadReadableThread")(function* ( scope: McpInvocationScope, threadId: ThreadId, + runLimit: number, ) { const { parent } = yield* loadCaller(scope, []); const shell = yield* threadManagement @@ -1478,20 +1480,38 @@ const make = Effect.gen(function* () { ); } const input = { projectId: shell.projectId, threadId }; - // Transfers are needed only to acknowledge a caller's child-task result. - const records = - parent === undefined - ? threadManagement - .getProjectThreadRecords(input, ["runs", "providerTurns", "runtimeRequests"]) - .pipe(Effect.map((target) => ({ ...target, contextTransfers: [] }))) - : threadManagement.getProjectThreadRecords(input, [ - "runs", - "providerTurns", - "runtimeRequests", - "contextTransfers", - ]); - const target = yield* records.pipe(Effect.mapError(threadManagementFailure)); - return { parent, target, shell } as const; + // Acknowledging a caller's child-task result matches it against every run and transfer. + if ( + parent !== undefined && + shell.lineage.parentThreadId === parent.thread.id && + shell.lineage.relationshipToParent === "subagent" + ) { + const target = yield* threadManagement + .getProjectThreadRecords(input, [ + "runs", + "providerTurns", + "runtimeRequests", + "contextTransfers", + ]) + .pipe(Effect.mapError(threadManagementFailure)); + return { parent, target, shell, runCount: target.runs.length } as const; + } + // Long threads hold thousands of runs; the read shows only the recent and active ones. + const [records, runCount] = yield* Effect.all([ + threadManagement.getProjectThreadRecords(input, ["runs", "runtimeRequests"], { + latestRunLimit: runLimit, + runStatuses: ThreadManagementService.ACTIVE_RUN_STATUSES, + runtimeRequestStatuses: ["pending"], + }), + threadManagement.getRunCount(threadId), + ]).pipe(Effect.mapError(threadManagementFailure)); + const { providerTurns } = yield* threadManagement + .getProjectThreadRecords(input, ["providerTurns"], { + providerTurnAttemptIds: records.runs.flatMap((run) => run.activeAttemptId ?? []), + }) + .pipe(Effect.mapError(threadManagementFailure)); + const target = { ...records, providerTurns, contextTransfers: [] }; + return { parent, target, shell, runCount } as const; }); const loadProviders = providerRegistry.getProviders; @@ -3302,7 +3322,12 @@ const make = Effect.gen(function* () { } satisfies OrchestratorMcpThreadListResult; }), readThread: Effect.fn("OrchestratorMcpService.readThread")(function* (scope, input) { - const { parent, target, shell } = yield* loadReadableThread(scope, input.threadId); + const runLimit = input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT; + const { parent, target, shell, runCount } = yield* loadReadableThread( + scope, + input.threadId, + runLimit, + ); const view = input.view ?? "messages"; const limit = input.limit ?? DEFAULT_THREAD_READ_LIMIT; const maxChars = input.maxCharsPerItem ?? DEFAULT_THREAD_ITEM_MAX_CHARS; @@ -3341,6 +3366,7 @@ const make = Effect.gen(function* () { const thread = threadDetail( scope, target, + runCount, timeline.totalItems, shell, yield* Clock.currentTimeMillis, @@ -3350,7 +3376,7 @@ const make = Effect.gen(function* () { ); const recentRuns = target.runs .toSorted((left, right) => right.ordinal - left.ordinal) - .slice(0, input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT) + .slice(0, runLimit) .map((run) => threadRun(target, run)); const { items, start } = timelineItemsWithinBudget( { thread, recentRuns, nextPosition: null, hasMore: false, earlierPosition: null }, diff --git a/apps/server/src/mcp/toolkits/core.test.ts b/apps/server/src/mcp/toolkits/core.test.ts index 81614e5eb4274..6a98097e8fa47 100644 --- a/apps/server/src/mcp/toolkits/core.test.ts +++ b/apps/server/src/mcp/toolkits/core.test.ts @@ -1336,6 +1336,7 @@ it.effect("discovers workflow stop targets and requires orchestration capability getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)), getProjectThreadRecords: () => Effect.succeed(McpToolAccessTestkit.idleThreadProjection(shell)), + getRunCount: () => Effect.succeed(1), getTimelinePage: () => Effect.succeed({ items: [row], totalItems: 1, hasMore: false, hasEarlier: false }), stopWorkflow: (input) => diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index f023fe5aa8697..33d60ca7c8eaf 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -436,6 +436,7 @@ export interface OrchestratorV2Shape { options: ProjectionTimelinePageOptions, ) => Effect.Effect; readonly getMessageCount: (threadId: ThreadId) => Effect.Effect; + readonly getRunCount: (threadId: ThreadId) => Effect.Effect; readonly getMessagesById: ( threadId: ThreadId, messageIds: ReadonlyArray, @@ -16229,6 +16230,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio .getMessageCount(threadId) .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause }))); }), + getRunCount: Effect.fn("OrchestratorV2.getRunCount")(function (threadId) { + return projectionStore + .getRunCount(threadId) + .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause }))); + }), getMessagesById: Effect.fn("OrchestratorV2.getMessagesById")(function (threadId, messageIds) { return projectionStore .getMessagesById(threadId, messageIds) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index b961ef1e0dc34..2c667bf43679f 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -729,6 +729,31 @@ it.effect("idle compaction filters keep the latest run, live work and the newest .join("\n"); assert.notInclude(runPlan, "orchestration_v2_projection_runs_thread_ordinal_idx"); assert.match(runPlan, /SEARCH orchestration_v2_projection_runs USING INDEX \w*status\w*/); + // The newest runs by ordinal, whatever their status, plus older runs the statuses match. + for (const [filter, expected] of [ + [{ latestRunLimit: 2 }, [4, 5]], + [{ latestRunLimit: 2, runStatuses: ["rolled_back"] as const }, [1, 4, 5]], + ] as const) { + const traced = traceSqlStatements(); + const recent = yield* store + .getThreadRecords(threadId, ["runs"], filter) + .pipe(Effect.withTracer(traced.tracer)); + assert.deepEqual( + recent.runs.map((run) => run.ordinal), + [...expected], + ); + const recentQuery = traced.statements.find((statement) => + statement.includes("FROM orchestration_v2_projection_runs"), + ); + assert.isDefined(recentQuery); + // Fetches the chosen runs by key instead of walking every run of the thread. + assert.match( + (yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${recentQuery}`)) + .map((row) => row.detail) + .join("\n"), + /^SEARCH orchestration_v2_projection_runs USING INDEX \w+ \(run_id=\?\)/, + ); + } // An id filter reads by primary key, not every row of the thread. const byId = traceSqlStatements(); const found = yield* store diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 289bdce2276c0..73b3d7dd5f8d2 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -363,6 +363,8 @@ export interface ProjectionRecordFilter { readonly runStatuses?: ReadonlyArray; /** Keeps the thread's highest-ordinal run even when `runStatuses` excludes it. */ readonly includeLatestRun?: true; + /** The thread's N highest-ordinal runs, plus any run `runStatuses` matches. Ignores `includeLatestRun`. */ + readonly latestRunLimit?: number; readonly providerTurnThreadIds?: ReadonlyArray; /** Only the provider turn whose token usage was updated last. */ readonly latestProviderTurnUsageOnly?: true; @@ -536,6 +538,7 @@ export interface ProjectionStoreV2Shape { limit: number, ) => Effect.Effect; readonly getSubagentCount: (threadId: ThreadId) => Effect.Effect; + readonly getRunCount: (threadId: ThreadId) => Effect.Effect; /** Child threads of the thread's app-owned tasks, in task order, without decoding the tasks. */ readonly listAppOwnedChildThreadIds: ( threadId: ThreadId, @@ -3925,23 +3928,43 @@ export const layer: Layer.Layer = (fields !== undefined && !fields.includes("runs")) || (window === undefined && (matchesNothing(filter?.runIds) || - (matchesNothing(filter?.runStatuses) && filter?.includeLatestRun !== true))) + (matchesNothing(filter?.runStatuses) && + filter?.includeLatestRun !== true && + filter?.latestRunLimit === undefined))) ? Effect.succeed([]) : window === undefined ? sql` SELECT payload_json FROM orchestration_v2_projection_runs - WHERE ${threadColumn(filter?.runIds)} = ${threadId} + WHERE ${threadColumn(filter?.latestRunLimit === undefined ? filter?.runIds : [])} = ${threadId} ${filter?.runIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.runIds)}))`} ${ - filter?.runStatuses === undefined - ? sql`ORDER BY ordinal ASC` - : filter.includeLatestRun !== true - ? // `+ordinal` stops SQLite from walking every run in the (thread_id, ordinal) - // index to skip a sort; a status index reads only matching runs. - sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)})) + filter?.latestRunLimit !== undefined + ? sql`AND run_id IN ( + SELECT run_id FROM ( + SELECT run_id FROM orchestration_v2_projection_runs + WHERE thread_id = ${threadId} + ORDER BY ordinal DESC + LIMIT ${filter.latestRunLimit} + ) + ${ + filter.runStatuses === undefined + ? sql`` + : sql`UNION + SELECT run_id FROM orchestration_v2_projection_runs + WHERE thread_id = ${threadId} + AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))` + } + ) + ORDER BY ordinal ASC` + : filter?.runStatuses === undefined + ? sql`ORDER BY ordinal ASC` + : filter.includeLatestRun !== true + ? // `+ordinal` stops SQLite from walking every run in the (thread_id, ordinal) + // index to skip a sort; a status index reads only matching runs. + sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)})) ORDER BY +ordinal ASC` - : sql`AND (status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)})) + : sql`AND (status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)})) OR run_id = ( SELECT run_id FROM orchestration_v2_projection_runs WHERE thread_id = ${threadId} @@ -6317,6 +6340,13 @@ export const layer: Layer.Layer = Effect.map((rows) => rows[0]?.bytes ?? 0), Effect.mapError(controlReadError(threadId)), ); + const getRunCount: ProjectionStoreV2Shape["getRunCount"] = (threadId) => + sql<{ + count: number; + }>`SELECT COUNT(*) AS count FROM orchestration_v2_projection_runs WHERE thread_id = ${threadId}`.pipe( + Effect.map((rows) => rows[0]?.count ?? 0), + Effect.mapError(controlReadError(threadId)), + ); const getSubagentCount: ProjectionStoreV2Shape["getSubagentCount"] = (threadId) => sql<{ count: number; @@ -6804,7 +6834,6 @@ export const layer: Layer.Layer = */ const readTimelineWindow = Effect.fn("ProjectionStore.readTimelineWindow")(function* ( threadId: ThreadId, - runs: ProjectionRecords<"runs">["runs"], includePersistentOutput: boolean, where: Statement.Fragment, limit: Statement.Fragment = sql``, @@ -6823,7 +6852,9 @@ export const layer: Layer.Layer = persistent_output: number; attempt_id: string | null; attempt_status: string | null; + run_status: OrchestrationV2Run["status"] | null; }>`SELECT item.turn_item_id, item.run_id, item.node_id, item.parent_item_id, item.type, item.status, item.ordinal, + run.status AS run_status, ${includePersistentOutput ? sql`attempt.attempt_id` : sql`NULL`} AS attempt_id, ${includePersistentOutput ? sql`attempt.status` : sql`NULL`} AS attempt_status, ${includePersistentOutput ? sql`item.inline_work_output` : sql`0`} AS persistent_output, @@ -6831,6 +6862,7 @@ export const layer: Layer.Layer = CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.createdBy') END AS created_by, CASE WHEN item.type IN ('user_message', 'assistant_message') THEN json_extract(item.payload_json, '$.messageId') END AS message_id FROM orchestration_v2_projection_turn_items AS item + LEFT JOIN orchestration_v2_projection_runs AS run ON run.run_id = item.run_id ${ includePersistentOutput ? sql`LEFT JOIN orchestration_v2_projection_nodes AS node ON node.node_id = item.node_id @@ -6843,7 +6875,6 @@ export const layer: Layer.Layer = } WHERE item.thread_id = ${threadId} ${where} ORDER BY item.ordinal ASC, item.turn_item_id ASC ${limit}`; - const runStatuses = new Map(runs.map((run) => [String(run.id), run.status])); return rows.map((row): TimelineIndexItem => ({ id: TurnItemId.make(row.turn_item_id), threadId, @@ -6853,7 +6884,7 @@ export const layer: Layer.Layer = type: row.type, status: row.status, ordinal: row.ordinal, - runStatus: row.run_id === null ? undefined : runStatuses.get(row.run_id), + runStatus: row.run_status ?? undefined, attemptId: row.attempt_id, attemptStatus: row.attempt_status, persistentOutput: row.persistent_output === 1, @@ -6895,9 +6926,18 @@ export const layer: Layer.Layer = AND NOT (json_extract(payload_json, '$.forkedFrom.type') IS 'run' AND json_extract(payload_json, '$.lineage.sideChat') IS NOT 1)`; if (ownRowsOnly.length === 0) return null; - const records = yield* getThreadRecords(threadId, ["runs", "attempts"]); + const records = yield* getThreadRecords(threadId, []); if (inheritedRunFork(records.thread) !== null) return null; - const superseded = records.attempts.filter((attempt) => attempt.status === "superseded"); + // Only these runs and attempts can hide rows, and their columns are all visibility reads. + const [hidingRuns, superseded] = yield* Effect.all([ + sql<{ id: RunId; status: OrchestrationV2Run["status"] }>` + SELECT run_id AS id, status FROM orchestration_v2_projection_runs + WHERE thread_id = ${threadId} AND status IN ('rolled_back', 'queued', 'cancelled')`, + sql<{ runId: RunId; rootNodeId: NodeId; status: "superseded" }>` + SELECT run_id AS runId, root_node_id AS rootNodeId, status + FROM orchestration_v2_projection_run_attempts + WHERE thread_id = ${threadId} AND status = 'superseded'`, + ]); const stopRequests = superseded.length === 0 ? [] @@ -6905,8 +6945,8 @@ export const layer: Layer.Layer = SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items WHERE thread_id = ${threadId} AND type = 'run_interrupt_request'`; const isVisible = createOrchestrationV2TurnItemVisibility({ - runs: records.runs, - attempts: records.attempts, + runs: hidingRuns, + attempts: superseded, items: stopRequests.map((row) => ({ type: "run_interrupt_request" as const, runId: row.run_id === null ? null : RunId.make(row.run_id), @@ -6918,7 +6958,7 @@ export const layer: Layer.Layer = // are found without reading the thread. const stoppedRuns = new Set(stopRequests.map((row) => row.run_id)); const runsWithStatus = (...statuses: ReadonlyArray) => - records.runs.flatMap((run) => (statuses.includes(run.status) ? [String(run.id)] : [])); + hidingRuns.flatMap((run) => (statuses.includes(run.status) ? [String(run.id)] : [])); const rolledBack = runsWithStatus("rolled_back"); const heldInput = runsWithStatus("queued", "cancelled"); const steered = [...new Set(superseded.map((attempt) => String(attempt.runId)))].filter( @@ -6990,7 +7030,7 @@ export const layer: Layer.Layer = hidden, visibleBefore, read: (where: Statement.Fragment, limit?: Statement.Fragment) => - readTimelineWindow(threadId, records.runs, includePersistentOutput, where, limit), + readTimelineWindow(threadId, includePersistentOutput, where, limit), }; }); type LocalTimeline = NonNullable>>; @@ -9373,6 +9413,7 @@ export const layer: Layer.Layer = getMessageCount: readOnReader(getMessageCount), getTranscriptByteFloor: readOnReader(getTranscriptByteFloor), getSubagentCount: readOnReader(getSubagentCount), + getRunCount: readOnReader(getRunCount), listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds), hasPendingDelegatedCompletion: readOnReader(hasPendingDelegatedCompletion), getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal), diff --git a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts index 069e36ceef916..48eacd6bbd18f 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts @@ -247,6 +247,7 @@ it.effect( getTranscriptByteFloor: () => Effect.die("unused transcript byte floor"), hasPendingDelegatedCompletion: () => Effect.die("unused hasPendingDelegatedCompletion"), getSubagentCount: () => Effect.die("unused subagent count"), + getRunCount: () => Effect.die("unused run count"), listAppOwnedChildThreadIds: () => Effect.die("unused listAppOwnedChildThreadIds"), getNextTurnItemOrdinal: () => Effect.die("unused ordinal read"), getTurnItem: () => Effect.die("unused turn item read"), diff --git a/apps/server/src/orchestration-v2/ThreadManagementService.ts b/apps/server/src/orchestration-v2/ThreadManagementService.ts index 611d9a3244f24..bfd676d7a3a22 100644 --- a/apps/server/src/orchestration-v2/ThreadManagementService.ts +++ b/apps/server/src/orchestration-v2/ThreadManagementService.ts @@ -405,6 +405,7 @@ export interface ThreadManagementServiceShape { readonly getThreadHistoryPage: Orchestrator.OrchestratorV2["Service"]["getThreadHistoryPage"]; readonly getTimelinePage: Orchestrator.OrchestratorV2["Service"]["getTimelinePage"]; readonly getMessageCount: Orchestrator.OrchestratorV2["Service"]["getMessageCount"]; + readonly getRunCount: Orchestrator.OrchestratorV2["Service"]["getRunCount"]; readonly getMessagesById: Orchestrator.OrchestratorV2["Service"]["getMessagesById"]; readonly getTurnItem: (input: { readonly threadId: ThreadId; @@ -1720,6 +1721,11 @@ const make = Effect.gen(function* () { Effect.andThen(orchestrator.getMessageCount(threadId)), ); }), + getRunCount: Effect.fn("ThreadManagementService.getRunCount")(function (threadId) { + return ensureProjectionTranscript(threadId).pipe( + Effect.andThen(orchestrator.getRunCount(threadId)), + ); + }), getMessagesById: Effect.fn("ThreadManagementService.getMessagesById")( function (threadId, messageIds) { return ensureProjectionTranscript(threadId).pipe( diff --git a/apps/server/src/peer/RemoteDelegation.test.ts b/apps/server/src/peer/RemoteDelegation.test.ts index 90c6eb83baae5..34e021cc798d0 100644 --- a/apps/server/src/peer/RemoteDelegation.test.ts +++ b/apps/server/src/peer/RemoteDelegation.test.ts @@ -280,6 +280,8 @@ const boxToolkitLayer = (thread: BoxThread) => { getThreadShell: shellOf, getProjectThreadRecords: (input) => projectionOf(input.threadId), getThreadRecords: (threadId) => projectionOf(threadId), + getRunCount: (threadId) => + projectionOf(threadId).pipe(Effect.map((projection) => projection.runs.length)), waitForThread: (input) => Effect.gen(function* () { yield* Ref.update(thread.waitedFor, (seen) => [...seen, input.runId]); diff --git a/apps/server/src/relay/AgentAwarenessRelay.test.ts b/apps/server/src/relay/AgentAwarenessRelay.test.ts index 10388e99fe7c5..5cde458a1cd30 100644 --- a/apps/server/src/relay/AgentAwarenessRelay.test.ts +++ b/apps/server/src/relay/AgentAwarenessRelay.test.ts @@ -238,6 +238,7 @@ const makeTestRelay = Effect.fnUntraced(function* ( getThreadHistoryPage: () => Effect.die("unused"), getTimelinePage: () => Effect.die("Unused timeline read"), getMessageCount: () => Effect.die("unused message count"), + getRunCount: () => Effect.die("unused run count"), getMessagesById: () => Effect.die("unused messages by id"), getTurnItem: () => Effect.die("unused turn item read"), getThreadRecords: () => Effect.die("unused record read"), From 20501a0221312c225e505dd8f2aa88f0455cbda5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 22:00:44 -0800 Subject: [PATCH 024/106] feat(issues): add issue tracking core with GitHub issues (#19259) Co-authored-by: Claude Fable 5.2 --- apps/server/src/auth/RpcAuthorization.test.ts | 53 + apps/server/src/auth/RpcAuthorization.ts | 5 + apps/server/src/issue/IssueService.test.ts | 346 ++++++ apps/server/src/issue/IssueService.ts | 569 +++++++++ apps/server/src/issue/IssueTrackerRegistry.ts | 36 + .../src/observability/RpcInstrumentation.ts | 10 + apps/server/src/server.ts | 5 + apps/server/src/ws.test.ts | 2 + apps/server/src/ws.ts | 22 +- .../contracts/src/clientRpcPermissions.ts | 5 + packages/contracts/src/index.ts | 1 + packages/contracts/src/issue.ts | 471 ++++++++ packages/contracts/src/rpc.ts | 109 ++ packages/source-control-core/package.json | 4 + .../src/server/IssueTracker.ts | 178 +++ .../source-control-core/src/server/driver.ts | 3 + .../src/shared/definition.ts | 25 +- packages/source-control-github/package.json | 3 +- .../schema/github.graphql.ts | 29 + .../src/server/GitHubIssueTracker.test.ts | 553 +++++++++ .../src/server/GitHubIssueTracker.ts | 442 +++++++ .../src/server/driver.ts | 2 + .../src/server/gitHubIssueJson.ts | 350 ++++++ .../src/server/gitHubIssueTemplates.ts | 159 +++ .../src/server/gitHubPullRequestJson.ts | 2 +- .../src/server/issues.graphql | 392 +++++++ .../src/server/issues.graphql.ts | 1029 +++++++++++++++++ pnpm-lock.yaml | 3 + 28 files changed, 4799 insertions(+), 9 deletions(-) create mode 100644 apps/server/src/issue/IssueService.test.ts create mode 100644 apps/server/src/issue/IssueService.ts create mode 100644 apps/server/src/issue/IssueTrackerRegistry.ts create mode 100644 packages/contracts/src/issue.ts create mode 100644 packages/source-control-core/src/server/IssueTracker.ts create mode 100644 packages/source-control-github/src/server/GitHubIssueTracker.test.ts create mode 100644 packages/source-control-github/src/server/GitHubIssueTracker.ts create mode 100644 packages/source-control-github/src/server/gitHubIssueJson.ts create mode 100644 packages/source-control-github/src/server/gitHubIssueTemplates.ts create mode 100644 packages/source-control-github/src/server/issues.graphql create mode 100644 packages/source-control-github/src/server/issues.graphql.ts diff --git a/apps/server/src/auth/RpcAuthorization.test.ts b/apps/server/src/auth/RpcAuthorization.test.ts index 2302170f99a97..0cd838f129340 100644 --- a/apps/server/src/auth/RpcAuthorization.test.ts +++ b/apps/server/src/auth/RpcAuthorization.test.ts @@ -8,6 +8,8 @@ import { DEFAULT_SERVER_SETTINGS, ProviderDriverKind, ProviderInstanceId, + ProjectId, + IssueTrackerKind, SecretProviderId, ThreadId, AuthOrchestrationOperateScope, @@ -144,6 +146,57 @@ describe("RPC scope middleware", () => { ); }); +it.effect("allows issue reads but requires a source-control grant before commenting", () => + Effect.gen(function* () { + const tested = [WS_METHODS.issuesList, WS_METHODS.issuesComment] as const; + const group = WsRpcGroup.omit( + ...[...WsRpcGroup.requests.keys()].filter( + (tag): tag is Exclude => + !(tested as ReadonlyArray).includes(tag), + ), + ); + const comments: string[] = []; + const handlers = Layer.mergeAll( + group.toLayerHandler(WS_METHODS.issuesList, () => + Effect.succeed({ entries: [], trackers: [], errors: [], viewers: {}, nextCursors: {} }), + ), + group.toLayerHandler(WS_METHODS.issuesComment, ({ body }) => + Effect.sync(() => { + comments.push(body); + }), + ), + ); + const reader = yield* RpcTest.makeClient(group).pipe( + Effect.provide(Layer.merge(handlers, RpcAuthorization.layer([AuthOrchestrationReadScope]))), + ); + const comment = { + projectId: ProjectId.make("web"), + tracker: IssueTrackerKind.make("github"), + scope: "acme/web", + key: "42", + body: "Repro attached.", + }; + + expect((yield* reader[WS_METHODS.issuesList]({ state: "open" })).entries).toEqual([]); + expect(yield* reader[WS_METHODS.issuesComment](comment).pipe(Effect.flip)).toMatchObject({ + _tag: "EnvironmentAuthorizationError", + requiredPermission: AuthSourceControlWriteScope, + }); + expect(comments).toEqual([]); + + const writer = yield* RpcTest.makeClient(group).pipe( + Effect.provide( + Layer.merge( + handlers, + RpcAuthorization.layer([AuthOrchestrationReadScope, AuthSourceControlWriteScope]), + ), + ), + ); + yield* writer[WS_METHODS.issuesComment](comment); + expect(comments).toEqual(["Repro attached."]); + }).pipe(Effect.scoped), +); + it.effect.each([WS_METHODS.subscribeVcsBranch, WS_METHODS.subscribeVcsStatus] as const)( "enforces read permission on stream %s", (method) => diff --git a/apps/server/src/auth/RpcAuthorization.ts b/apps/server/src/auth/RpcAuthorization.ts index 7925fd869799f..6868a8b2e7286 100644 --- a/apps/server/src/auth/RpcAuthorization.ts +++ b/apps/server/src/auth/RpcAuthorization.ts @@ -132,6 +132,11 @@ export const RPC_REQUIRED_SCOPES = { [WS_METHODS.pullRequestsStack]: AuthOrchestrationReadScope, [WS_METHODS.pullRequestsLinkedThreads]: AuthOrchestrationReadScope, [WS_METHODS.pullRequestsDetail]: AuthOrchestrationReadScope, + [WS_METHODS.issuesList]: AuthOrchestrationReadScope, + [WS_METHODS.issuesDetail]: AuthOrchestrationReadScope, + [WS_METHODS.issuesComments]: AuthOrchestrationReadScope, + [WS_METHODS.issuesTemplates]: AuthOrchestrationReadScope, + [WS_METHODS.issuesCandidates]: AuthOrchestrationReadScope, [WS_METHODS.pullRequestsPreview]: AuthOrchestrationReadScope, [WS_METHODS.pullRequestsChecks]: AuthOrchestrationReadScope, [WS_METHODS.pullRequestsActivity]: AuthOrchestrationReadScope, diff --git a/apps/server/src/issue/IssueService.test.ts b/apps/server/src/issue/IssueService.test.ts new file mode 100644 index 0000000000000..133f51234838c --- /dev/null +++ b/apps/server/src/issue/IssueService.test.ts @@ -0,0 +1,346 @@ +import { describe, expect, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; +import { + IssueTrackerKind, + SourceControlProviderKind, + type OrchestrationProjectShell, + type ProjectId, +} from "@t3tools/contracts"; +import { + IssueTrackerError, + type IssueTrackerApi, + type TrackerIssueDetail, +} from "@t3tools/source-control-core/server/IssueTracker"; + +import { normalizeGitRemoteUrl } from "@t3tools/shared/git"; + +import * as ProjectService from "../project/ProjectService.ts"; +import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts"; +import * as IssueService from "./IssueService.ts"; +import * as IssueTrackerRegistry from "./IssueTrackerRegistry.ts"; + +const GITHUB = IssueTrackerKind.make("github"); + +function project(input: { + readonly id: string; + readonly repository?: string; + readonly provider?: string; + readonly host?: string; +}): OrchestrationProjectShell { + const host = input.host ?? "github.com"; + return { + id: input.id as ProjectId, + title: input.id, + workspaceRoot: `/w/${input.id}`, + ...(input.repository + ? { + repositoryIdentity: { + canonicalKey: normalizeGitRemoteUrl(`https://${host}/${input.repository}.git`), + locator: { + source: "git-remote" as const, + remoteName: "origin", + remoteUrl: `https://${host}/${input.repository}.git`, + }, + provider: SourceControlProviderKind.make(input.provider ?? "github"), + displayName: input.repository, + }, + } + : {}), + defaultModelSelection: null, + scripts: [], + createdAt: "2026-07-01T00:00:00Z", + updatedAt: "2026-07-01T00:00:00Z", + }; +} + +function issue(key: string, updatedAt = "2026-07-02T00:00:00Z"): TrackerIssueDetail { + return { + key, + displayKey: `#${key}`, + title: `Issue ${key}`, + url: `https://github.com/acme/web/issues/${key}`, + author: null, + state: { category: "open", name: "open" }, + createdAt: "2026-07-01T00:00:00Z", + updatedAt, + closedAt: null, + assignees: [], + labels: [], + commentCount: 0, + body: "", + viewer: "julius", + viewerPermissions: { setState: true, comment: true, edit: true, labels: true, assignees: true }, + comments: [], + commentsCursor: null, + events: [], + linkedPullRequests: [], + }; +} + +function fakeTracker(overrides: Partial = {}): IssueTrackerApi { + return { + kind: GITHUB, + capabilities: { + create: true, + edit: true, + comment: true, + labels: true, + assignees: true, + reactions: false, + templates: false, + linkedPullRequests: true, + events: true, + search: true, + states: [ + { category: "open", name: "open" }, + { category: "closed", name: "closed" }, + ], + closeReasons: ["completed"], + }, + getViewer: () => Effect.succeed("julius"), + listIssues: () => Effect.succeed({ items: [], nextCursor: null }), + getIssue: (input) => Effect.succeed(issue(input.key)), + getComments: () => Effect.succeed({ comments: [], nextCursor: null }), + comment: () => Effect.void, + update: () => Effect.void, + setState: () => Effect.void, + ...overrides, + }; +} + +function makeService(input: { + readonly projects: ReadonlyArray; + readonly trackers: ReadonlyArray; +}) { + return IssueService.IssueService.pipe( + Effect.provide( + IssueService.layer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.succeed( + IssueTrackerRegistry.IssueTrackerRegistry, + IssueTrackerRegistry.fromTrackers(input.trackers), + ), + Layer.mock(ProjectService.ProjectService)({ + listShells: () => Effect.succeed(input.projects), + getShell: (projectId) => + Effect.succeed( + Option.fromNullishOr(input.projects.find((p) => p.id === projectId)), + ), + }), + Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({ + resolve: () => Effect.succeed(null), + }), + ), + ), + ), + ), + ); +} + +describe("IssueService", () => { + it.effect("reads another repository on the same host but refuses to write to it", () => + Effect.gen(function* () { + const writes: string[] = []; + const service = yield* makeService({ + projects: [project({ id: "web", repository: "acme/web" })], + trackers: [ + fakeTracker({ + comment: (input) => + Effect.sync(() => { + writes.push(`${input.scope}#${input.key}`); + }), + }), + ], + }); + const own = { projectId: "web" as ProjectId, tracker: GITHUB, scope: "acme/web", key: "1" }; + const external = { ...own, scope: "other/backend", key: "7" }; + + expect((yield* service.detail(own)).writable).toBe(true); + // The external issue is served by the project's checkout and credentials, read-only. + const detail = yield* service.detail(external); + expect(detail).toMatchObject({ writable: false, scope: "other/backend", key: "7" }); + + yield* service.comment({ ...own, body: "ok" }); + const refused = yield* service.comment({ ...external, body: "no" }).pipe(Effect.result); + expect(refused).toMatchObject({ + _tag: "Failure", + failure: { _tag: "IssueOperationError", reason: "read-only" }, + }); + expect(writes).toEqual(["acme/web#1"]); + + // Another host is not reachable through this project's credentials at all. + const elsewhere = yield* Effect.flip(service.detail({ ...own, host: "ghe.example.test" })); + expect(elsewhere).toMatchObject({ + _tag: "IssueUnavailableError", + reason: "tracker-unsupported", + }); + }), + ); + + it.effect("sends writes as interactive requests and reads as background ones", () => + Effect.gen(function* () { + const interactive: Array = []; + const record = (operation: string) => + Effect.gen(function* () { + interactive.push([operation, yield* SourceControlRateLimit.Interactive]); + }); + const service = yield* makeService({ + projects: [project({ id: "web", repository: "acme/web" })], + trackers: [ + fakeTracker({ + getComments: () => + record("comments").pipe(Effect.as({ comments: [], nextCursor: null })), + comment: () => record("comment"), + }), + ], + }); + const ref = { + projectId: "web" as ProjectId, + tracker: GITHUB, + scope: "acme/web", + key: "42", + }; + yield* service.comments({ ...ref, cursor: "next" }); + yield* service.comment({ ...ref, body: "Repro attached." }); + expect(interactive).toEqual([ + ["comments", false], + ["comment", true], + ]); + }), + ); + + it.effect("refuses what the tracker did not declare before calling it", () => + Effect.gen(function* () { + let calls = 0; + const service = yield* makeService({ + projects: [project({ id: "web", repository: "acme/web" })], + trackers: [ + fakeTracker({ + setState: () => + Effect.sync(() => { + calls += 1; + }), + setReaction: () => + Effect.sync(() => { + calls += 1; + }), + }), + ], + }); + const ref = { projectId: "web" as ProjectId, tracker: GITHUB, scope: "acme/web", key: "1" }; + + yield* service.setState({ ...ref, state: "closed", closeReason: "completed" }); + const unknownState = yield* Effect.flip(service.setState({ ...ref, state: "In Review" })); + expect(unknownState._tag).toBe("IssueOperationError"); + const unknownReason = yield* Effect.flip( + service.setState({ ...ref, state: "closed", closeReason: "not-planned" }), + ); + expect(unknownReason._tag).toBe("IssueOperationError"); + const reaction = yield* Effect.flip( + service.react({ ...ref, content: "heart", reacted: true }), + ); + expect(reaction._tag).toBe("IssueOperationError"); + expect(calls).toBe(1); + }), + ); + + it.effect("deduplicates and pages repositories despite partial failures", () => + Effect.gen(function* () { + const listed: string[] = []; + let failContinuation = true; + const service = yield* makeService({ + projects: [ + project({ id: "web", repository: "Acme/Web" }), + project({ id: "web-worktree", repository: "acme/web" }), + project({ id: "api", repository: "acme/api" }), + project({ id: "docs", repository: "acme/docs" }), + project({ + id: "gitlab", + repository: "acme/gl", + provider: "gitlab", + host: "gitlab.com", + }), + project({ id: "plain" }), + ], + trackers: [ + fakeTracker({ + listIssues: (input) => + Effect.gen(function* () { + listed.push(input.scope); + if (input.scope === "acme/api") + return yield* new IssueTrackerError({ + tracker: GITHUB, + operation: "listIssues", + reason: "failed", + // A tracker may fail without a detail; the wire error still needs one. + detail: " ", + }); + if (input.scope === "acme/docs") + return { items: [issue("3", "2026-07-04T00:00:00Z")], nextCursor: null }; + if (input.cursor === "next" && failContinuation) { + failContinuation = false; + return yield* new IssueTrackerError({ + tracker: GITHUB, + operation: "listIssues", + reason: "failed", + detail: "try again", + }); + } + if (input.cursor === "next") + return { items: [issue("4", "2026-07-01T00:00:00Z")], nextCursor: null }; + return { + items: [issue("1"), issue("2", "2026-07-05T00:00:00Z")], + nextCursor: "next", + }; + }), + }), + ], + }); + const result = yield* service.list({ state: "open" }); + expect(listed.sort()).toEqual(["Acme/Web", "acme/api", "acme/docs"]); + expect(result.entries.map((entry) => [entry.projectId, entry.key])).toEqual([ + ["web", "2"], + ["docs", "3"], + ["web", "1"], + ]); + expect( + result.entries.filter((entry) => entry.projectId === "web").map((entry) => entry.scope), + ).toEqual(["Acme/Web", "Acme/Web"]); + expect(result.nextCursors).toEqual({ "github.com Acme/Web": "next" }); + expect(result.errors).toEqual([ + { projectId: "api", projectTitle: "api", message: "The issue tracker request failed." }, + ]); + expect(result.viewers).toEqual({ "github.com": "julius" }); + expect(result.trackers).toEqual([ + expect.objectContaining({ host: "github.com", projectCount: 4, configured: true }), + expect.objectContaining({ + host: "gitlab.com", + tracker: "gitlab", + projectCount: 1, + configured: false, + capabilities: null, + }), + ]); + + // A continuation only reads the scopes it names. + listed.length = 0; + const failed = yield* service.list({ + state: "open", + cursors: result.nextCursors, + }); + expect(listed).toEqual(["Acme/Web"]); + expect(failed.nextCursors).toEqual(result.nextCursors); + expect(failed.errors).toEqual([ + { projectId: "web", projectTitle: "web", message: "try again" }, + ]); + const next = yield* service.list({ state: "open", cursors: failed.nextCursors }); + expect(next.entries.map((entry) => [entry.projectId, entry.key])).toEqual([["web", "4"]]); + expect(next.nextCursors).toEqual({}); + expect(next.errors).toEqual([]); + }), + ); +}); diff --git a/apps/server/src/issue/IssueService.ts b/apps/server/src/issue/IssueService.ts new file mode 100644 index 0000000000000..4b1a9a1167432 --- /dev/null +++ b/apps/server/src/issue/IssueService.ts @@ -0,0 +1,569 @@ +// References outside the project's canonical repository may be read on the same host, +// but every write must pass through requireWritable. +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import { + IssueOperationError, + IssueTrackerKind, + IssueUnavailableError, + type IssueCandidates, + type IssueCommentInput, + type IssueCommentsPage, + type IssueCommentsPageInput, + type IssueCreateInput, + type IssueCreateResult, + type IssueDetail, + type IssueError, + type IssueListEntry, + type IssueListInput, + type IssueListProjectError, + type IssueListResult, + type IssueReactionInput, + type IssueRef, + type IssueScopeRef, + type IssueSetStateInput, + type IssueTemplateList, + type IssueTrackerSummary, + type IssueUpdateInput, + type OrchestrationProjectShell, +} from "@t3tools/contracts"; +import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; +import { + IssueTrackerError, + type IssueTrackerApi, +} from "@t3tools/source-control-core/server/IssueTracker"; + +import * as ProjectService from "../project/ProjectService.ts"; +import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts"; +import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts"; +import * as IssueTrackerRegistry from "./IssueTrackerRegistry.ts"; + +const DEFAULT_LIST_LIMIT = 50; +const SCOPE_CONCURRENCY = 4; + +const interactive = + (call: (input: I) => Effect.Effect) => + (input: I) => + call(input).pipe(Effect.provideService(SourceControlRateLimit.Interactive, true)); + +const traced = + (operation: string, call: (input: I) => Effect.Effect) => + (input: I) => + call(input).pipe(Effect.withSpan(`IssueService.${operation}`)); + +/** A tracker may fail with no detail; the wire errors carry a non-empty one. */ +function trackerDetail(error: IssueTrackerError): string { + return error.detail.trim() || "The issue tracker request failed."; +} + +/** One project's resolved tracker: which driver, where it is served from, and its container. */ +interface ProjectTrackerBinding { + readonly canonicalKey: string; + readonly tracker: IssueTrackerKind; + readonly host: string; + readonly scope: string; +} + +/** A project the registry has a tracker for, ready to serve reads and writes. */ +interface SupportedProject extends ProjectTrackerBinding { + readonly project: OrchestrationProjectShell; + readonly api: IssueTrackerApi; +} + +/** + * The git-host path of resolution: the tracker is the host's own, named like the remote's + * provider kind. Null for a project with no remote or a kind no tracker claims. + */ +function resolveGitHostBinding(project: OrchestrationProjectShell): ProjectTrackerBinding | null { + const identity = project.repositoryIdentity; + const scope = sourceControlDefinitions.issueScope(identity); + const kind = identity?.provider; + if (!identity || kind === undefined || kind === "unknown" || scope === null) return null; + return { + canonicalKey: identity.canonicalKey, + tracker: IssueTrackerKind.make(kind), + host: sourceControlDefinitions.changeRequestHost(identity, kind), + scope, + }; +} + +function listCursorKey(host: string, scope: string): string { + return `${host} ${scope}`; +} + +function isTrackerUnusable(error: IssueTrackerError): boolean { + return error.reason === "missing-tool" || error.reason === "unauthenticated"; +} + +function toIssueError(operation: string): (error: IssueTrackerError) => IssueError { + return (error) => + isTrackerUnusable(error) + ? new IssueUnavailableError({ + reason: error.reason === "missing-tool" ? "tool-missing" : "unauthenticated", + tracker: error.tracker, + cause: error, + }) + : new IssueOperationError({ + operation, + detail: trackerDetail(error), + ...(error.reason === "not-found" || error.reason === "rate-limited" + ? { reason: error.reason } + : {}), + ...(error.retryAt === undefined ? {} : { retryAt: error.retryAt }), + cause: error, + }); +} + +const readOnly = (operation: string) => + new IssueOperationError({ + operation, + reason: "read-only", + detail: + "This issue belongs to a repository outside the selected project and cannot be changed from here.", + }); + +const unsupported = (operation: string, what: string) => + new IssueOperationError({ operation, detail: `This tracker does not support ${what}.` }); + +export class IssueService extends Context.Service< + IssueService, + { + readonly list: (input: IssueListInput) => Effect.Effect; + readonly detail: (input: IssueRef) => Effect.Effect; + readonly comments: ( + input: IssueCommentsPageInput, + ) => Effect.Effect; + readonly templates: (input: IssueScopeRef) => Effect.Effect; + readonly candidates: (input: IssueScopeRef) => Effect.Effect; + readonly create: (input: IssueCreateInput) => Effect.Effect; + readonly comment: (input: IssueCommentInput) => Effect.Effect; + readonly update: (input: IssueUpdateInput) => Effect.Effect; + readonly setState: (input: IssueSetStateInput) => Effect.Effect; + readonly react: (input: IssueReactionInput) => Effect.Effect; + } +>()("t3/issue/IssueService") {} + +const make = Effect.gen(function* () { + const registry = yield* IssueTrackerRegistry.IssueTrackerRegistry; + const projects = yield* ProjectService.ProjectService; + const repositoryIdentities = yield* RepositoryIdentityResolver.RepositoryIdentityResolver; + + const resolveProjectTracker = ( + project: OrchestrationProjectShell, + ): Effect.Effect => + (project.repositoryIdentity != null + ? Effect.succeed(project.repositoryIdentity) + : repositoryIdentities.resolve(project.workspaceRoot) + ).pipe( + Effect.map((repositoryIdentity) => resolveGitHostBinding({ ...project, repositoryIdentity })), + ); + + const listProjects = (filter: { + readonly projectId?: IssueListInput["projectId"]; + readonly host?: string | undefined; + }) => + (filter.projectId === undefined + ? projects.listShells() + : projects.getShell(filter.projectId).pipe(Effect.map(Option.toArray)) + ).pipe( + Effect.mapError( + (cause) => + new IssueOperationError({ + operation: "listProjects", + detail: "The project list could not be read.", + cause, + }), + ), + Effect.flatMap((shells) => + Effect.forEach( + shells, + (project) => + resolveProjectTracker(project).pipe( + Effect.map((binding) => (binding === null ? [] : [{ project, ...binding }])), + ), + { concurrency: SCOPE_CONCURRENCY }, + ), + ), + Effect.map((bound) => { + const host = filter.host?.trim().toLowerCase(); + const supported: SupportedProject[] = []; + const unimplemented = new Map< + string, + { tracker: IssueTrackerKind; projectCount: number } + >(); + for (const candidate of bound.flat()) { + if (host !== undefined && candidate.host !== host) continue; + const api = registry.get(candidate.tracker); + if (api === null) { + const counted = unimplemented.get(candidate.host); + if (counted === undefined) + unimplemented.set(candidate.host, { tracker: candidate.tracker, projectCount: 1 }); + else counted.projectCount += 1; + continue; + } + supported.push({ ...candidate, api }); + } + return { supported, unimplemented }; + }), + ); + + /** + * The project whose checkout and credentials serve a reference, and whether the reference is + * the project's own scope. A reference naming another scope on the same tracker and host is + * served by the project but marked read-only. + */ + const requireProject = ( + ref: IssueScopeRef, + ): Effect.Effect => + listProjects({ projectId: ref.projectId }).pipe( + Effect.flatMap(({ supported }) => { + const own = supported[0]; + if (own === undefined || own.tracker !== ref.tracker) { + return Effect.fail(new IssueUnavailableError({ reason: "tracker-unsupported" })); + } + const host = ref.host?.trim().toLowerCase() ?? own.host; + if (host !== own.host) { + return Effect.fail(new IssueUnavailableError({ reason: "tracker-unsupported" })); + } + const scope = ref.scope.trim(); + const writable = scope.toLowerCase() === own.scope.toLowerCase(); + return Effect.succeed({ ...own, scope, writable }); + }), + ); + + const requireWritable = (ref: IssueScopeRef, operation: string) => + requireProject(ref).pipe( + Effect.filterOrFail( + (project) => project.writable, + () => readOnly(operation), + ), + ); + + const list: IssueService["Service"]["list"] = Effect.fn("IssueService.list")(function* (input) { + const { supported, unimplemented } = yield* listProjects(input); + const limit = input.limit ?? DEFAULT_LIST_LIMIT; + // Worktrees of one repository are separate projects; read each scope once. + const scopes = new Map(); + for (const project of supported) { + if (!scopes.has(project.canonicalKey)) scopes.set(project.canonicalKey, project); + } + const continuing = input.cursors; + const viewers = new Map(); + const trackers = new Map(); + const entries: IssueListEntry[] = []; + const errors: IssueListProjectError[] = []; + const nextCursors: Record = {}; + + const viewerRequests = new Map>(); + for (const project of scopes.values()) { + if (viewerRequests.has(project.host)) continue; + const request = yield* Effect.cached( + project.api + .getViewer({ cwd: project.project.workspaceRoot, host: project.host }) + .pipe(Effect.tap((viewer) => Effect.sync(() => viewers.set(project.host, viewer)))), + ); + viewerRequests.set(project.host, request); + } + + // Every project counts, including worktrees of a scope read once below. + for (const project of supported) { + const existing = trackers.get(project.host); + trackers.set( + project.host, + existing === undefined + ? { + tracker: project.tracker, + host: project.host, + projectCount: 1, + capabilities: project.api.capabilities, + configured: true, + detail: null, + } + : { ...existing, projectCount: existing.projectCount + 1 }, + ); + } + const recordFailure = (project: SupportedProject, failure: IssueTrackerError) => { + const existing = trackers.get(project.host); + if (existing !== undefined && isTrackerUnusable(failure)) + trackers.set(project.host, { + ...existing, + configured: false, + detail: trackerDetail(failure), + }); + }; + + yield* Effect.forEach( + scopes.values(), + (project) => + Effect.gen(function* () { + const key = listCursorKey(project.host, project.scope); + // A continuation names the scopes it wants; one not named has nothing to add. + if (continuing !== undefined && continuing[key] === undefined) return; + const cursor = continuing?.[key]; + const page = yield* viewerRequests.get(project.host)!.pipe( + Effect.flatMap((viewer) => + project.api.listIssues({ + cwd: project.project.workspaceRoot, + host: project.host, + scope: project.scope, + state: input.state, + involvement: input.involvement ?? "all", + viewer, + limit, + query: input.query, + cursor, + }), + ), + Effect.result, + ); + if (page._tag === "Failure") { + if (cursor !== undefined) nextCursors[key] = cursor; + recordFailure(project, page.failure); + errors.push({ + projectId: project.project.id, + projectTitle: project.project.title, + message: trackerDetail(page.failure), + }); + return; + } + for (const item of page.success.items) { + entries.push({ + ...item, + tracker: project.tracker, + host: project.host, + scope: project.scope, + projectId: project.project.id, + projectTitle: project.project.title, + }); + } + if (page.success.nextCursor !== null) nextCursors[key] = page.success.nextCursor; + }), + { concurrency: SCOPE_CONCURRENCY, discard: true }, + ); + + for (const [host, { tracker, projectCount }] of unimplemented) { + trackers.set(host, { + tracker, + host, + projectCount, + capabilities: null, + configured: false, + detail: "Issues cannot be browsed for this host yet.", + }); + } + entries.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt)); + return { + viewers: Object.fromEntries(viewers), + trackers: [...trackers.values()], + entries, + errors, + nextCursors, + }; + }); + + const detail: IssueService["Service"]["detail"] = (input) => + requireProject(input).pipe( + Effect.flatMap((project) => + project.api + .getIssue({ + cwd: project.project.workspaceRoot, + host: project.host, + scope: project.scope, + key: input.key, + }) + .pipe( + Effect.mapError(toIssueError("detail")), + Effect.map((issue): IssueDetail => ({ + ...issue, + tracker: project.tracker, + host: project.host, + scope: project.scope, + projectId: project.project.id, + projectTitle: project.project.title, + writable: project.writable, + capabilities: project.api.capabilities, + })), + ), + ), + ); + + const comments: IssueService["Service"]["comments"] = (input) => + requireProject(input).pipe( + Effect.flatMap((project) => + project.api + .getComments({ + cwd: project.project.workspaceRoot, + host: project.host, + scope: project.scope, + key: input.key, + cursor: input.cursor, + }) + .pipe(Effect.mapError(toIssueError("comments"))), + ), + ); + + const templates: IssueService["Service"]["templates"] = (input) => + requireProject(input).pipe( + Effect.flatMap((project) => + project.api.listTemplates === undefined || !project.api.capabilities.templates + ? Effect.fail(unsupported("templates", "issue templates")) + : project.api + .listTemplates({ + cwd: project.project.workspaceRoot, + host: project.host, + scope: project.scope, + }) + .pipe(Effect.mapError(toIssueError("templates"))), + ), + ); + + const candidates: IssueService["Service"]["candidates"] = (input) => + requireProject(input).pipe( + Effect.flatMap((project) => + project.api.listCandidates === undefined || + !(project.api.capabilities.labels || project.api.capabilities.assignees) + ? Effect.fail(unsupported("candidates", "labels or assignees")) + : project.api + .listCandidates({ + cwd: project.project.workspaceRoot, + host: project.host, + scope: project.scope, + }) + .pipe(Effect.mapError(toIssueError("candidates"))), + ), + ); + + const create: IssueService["Service"]["create"] = (input) => + requireWritable(input, "create").pipe( + Effect.flatMap((project) => + project.api.create === undefined || !project.api.capabilities.create + ? Effect.fail(unsupported("create", "filing issues")) + : project.api + .create({ + cwd: project.project.workspaceRoot, + host: project.host, + scope: project.scope, + title: input.title, + body: input.body, + labels: input.labels, + assignees: input.assignees, + }) + .pipe(Effect.mapError(toIssueError("create"))), + ), + ); + + const comment: IssueService["Service"]["comment"] = (input) => + requireWritable(input, "comment").pipe( + Effect.flatMap((project) => { + if (input.body.trim().length === 0) { + return Effect.fail( + new IssueOperationError({ operation: "comment", detail: "A comment needs some text." }), + ); + } + return project.api.comment === undefined || !project.api.capabilities.comment + ? Effect.fail(unsupported("comment", "comments")) + : project.api + .comment({ + cwd: project.project.workspaceRoot, + host: project.host, + scope: project.scope, + key: input.key, + body: input.body, + }) + .pipe(Effect.mapError(toIssueError("comment"))); + }), + ); + + const update: IssueService["Service"]["update"] = (input) => + requireWritable(input, "update").pipe( + Effect.flatMap((project) => { + const { capabilities } = project.api; + const wantsWords = input.title !== undefined || input.body !== undefined; + if (!wantsWords && input.labels === undefined && input.assignees === undefined) { + return Effect.fail( + new IssueOperationError({ operation: "update", detail: "Nothing to change." }), + ); + } + if (project.api.update === undefined) return Effect.fail(unsupported("update", "edits")); + if (wantsWords && !capabilities.edit) + return Effect.fail(unsupported("update", "editing the title or body")); + if (input.labels !== undefined && !capabilities.labels) + return Effect.fail(unsupported("update", "labels")); + if (input.assignees !== undefined && !capabilities.assignees) + return Effect.fail(unsupported("update", "assignees")); + return project.api + .update({ + cwd: project.project.workspaceRoot, + host: project.host, + scope: project.scope, + key: input.key, + title: input.title, + body: input.body, + labels: input.labels, + assignees: input.assignees, + }) + .pipe(Effect.mapError(toIssueError("update"))); + }), + ); + + const setState: IssueService["Service"]["setState"] = (input) => + requireWritable(input, "setState").pipe( + Effect.flatMap((project) => { + const { capabilities } = project.api; + const target = capabilities.states.find((state) => state.name === input.state); + if (project.api.setState === undefined || target === undefined) + return Effect.fail(unsupported("setState", `the state "${input.state}"`)); + if ( + input.closeReason !== undefined && + (target.category !== "closed" || !capabilities.closeReasons.includes(input.closeReason)) + ) + return Effect.fail(unsupported("setState", `the close reason "${input.closeReason}"`)); + return project.api + .setState({ + cwd: project.project.workspaceRoot, + host: project.host, + scope: project.scope, + key: input.key, + state: input.state, + closeReason: input.closeReason, + }) + .pipe(Effect.mapError(toIssueError("setState"))); + }), + ); + + const react: IssueService["Service"]["react"] = (input) => + requireWritable(input, "react").pipe( + Effect.flatMap((project) => + project.api.setReaction === undefined || !project.api.capabilities.reactions + ? Effect.fail(unsupported("react", "reactions")) + : project.api + .setReaction({ + cwd: project.project.workspaceRoot, + host: project.host, + scope: project.scope, + key: input.key, + subjectId: input.subjectId, + content: input.content, + reacted: input.reacted, + }) + .pipe(Effect.mapError(toIssueError("react"))), + ), + ); + + return IssueService.of({ + list, + detail: traced("detail", detail), + comments: traced("comments", comments), + templates: traced("templates", templates), + candidates: interactive(traced("candidates", candidates)), + create: interactive(traced("create", create)), + comment: interactive(traced("comment", comment)), + update: interactive(traced("update", update)), + setState: interactive(traced("setState", setState)), + react: interactive(traced("react", react)), + }); +}); + +export const layer = Layer.effect(IssueService, make); diff --git a/apps/server/src/issue/IssueTrackerRegistry.ts b/apps/server/src/issue/IssueTrackerRegistry.ts new file mode 100644 index 0000000000000..58e7dbadb0463 --- /dev/null +++ b/apps/server/src/issue/IssueTrackerRegistry.ts @@ -0,0 +1,36 @@ +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import type { IssueTrackerKind } from "@t3tools/contracts"; +import type { IssueTrackerApi } from "@t3tools/source-control-core/server/IssueTracker"; + +import * as BuiltInDrivers from "../sourceControl/builtInDrivers.ts"; + +export class IssueTrackerRegistry extends Context.Service< + IssueTrackerRegistry, + { + /** Null for a kind with no implementation, which the service reports as unsupported. */ + readonly get: (kind: IssueTrackerKind) => IssueTrackerApi | null; + readonly kinds: ReadonlyArray; + } +>()("t3/issue/IssueTrackerRegistry") {} + +/** Exported for tests, which stand a registry up from trackers they supply themselves. */ +export function fromTrackers( + trackers: ReadonlyArray, +): IssueTrackerRegistry["Service"] { + const byKind = new Map(trackers.map((tracker) => [tracker.kind, tracker])); + return { + get: (kind) => byKind.get(kind) ?? null, + kinds: trackers.map((tracker) => tracker.kind), + }; +} + +const make = Effect.gen(function* () { + const drivers = yield* BuiltInDrivers.SourceControlDrivers; + return fromTrackers(drivers.flatMap(({ instance }) => instance.issues ?? [])); +}); + +export const layer = Layer.effect(IssueTrackerRegistry, make).pipe( + Layer.provide(BuiltInDrivers.layer), +); diff --git a/apps/server/src/observability/RpcInstrumentation.ts b/apps/server/src/observability/RpcInstrumentation.ts index 4549ee8a33c10..2cd5497c56bd9 100644 --- a/apps/server/src/observability/RpcInstrumentation.ts +++ b/apps/server/src/observability/RpcInstrumentation.ts @@ -144,6 +144,16 @@ const RPC_AGGREGATES = { [WS_METHODS.pullRequestsRequestReviewers]: "pull-requests", [WS_METHODS.pullRequestsLabelCandidates]: "pull-requests", [WS_METHODS.pullRequestsSetLabels]: "pull-requests", + [WS_METHODS.issuesList]: "issues", + [WS_METHODS.issuesDetail]: "issues", + [WS_METHODS.issuesComments]: "issues", + [WS_METHODS.issuesTemplates]: "issues", + [WS_METHODS.issuesCandidates]: "issues", + [WS_METHODS.issuesCreate]: "issues", + [WS_METHODS.issuesComment]: "issues", + [WS_METHODS.issuesUpdate]: "issues", + [WS_METHODS.issuesSetState]: "issues", + [WS_METHODS.issuesReact]: "issues", [WS_METHODS.sourceControlLookupRepository]: "source-control", [WS_METHODS.sourceControlCloneRepository]: "source-control", [WS_METHODS.sourceControlPublishRepository]: "source-control", diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 01b659b0944ad..4cc0eee4aead6 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -54,6 +54,8 @@ import { speechHttpApiLayer } from "./speech/http.ts"; import { speechStreamRouteLayer } from "./speech/stream.ts"; import * as PullRequestProviderRegistry from "./pullRequest/PullRequestProviderRegistry.ts"; import * as PullRequestService from "./pullRequest/PullRequestService.ts"; +import * as IssueService from "./issue/IssueService.ts"; +import * as IssueTrackerRegistry from "./issue/IssueTrackerRegistry.ts"; import * as SqlitePersistence from "./persistence/Sqlite.ts"; import * as PullRequestFilesViewed from "./persistence/PullRequestFilesViewed.ts"; import * as PullRequestReviewGuides from "./persistence/PullRequestReviewGuides.ts"; @@ -438,6 +440,8 @@ const layerPullRequestService = PullRequestService.layer.pipe( Layer.provide(SourceControlRateLimit.layer), ); +const layerIssueService = IssueService.layer.pipe(Layer.provide(IssueTrackerRegistry.layer)); + const layerGitManager = GitManager.layer.pipe( Layer.provide(Layer.merge(ProjectionStoreV2.layer, ProjectStore.layer)), Layer.provideMerge(RuntimeLayer.layerProjectSetupScriptRunner), @@ -862,6 +866,7 @@ const layerMakeRoutes = Layer.mergeAll( Layer.provide(PeerLinkRequests.layer), // Share the service so HTTP/WS clients single-flight reads and invalidate each other's cached patches. Layer.provide(layerPullRequestService), + Layer.provide(layerIssueService), // The same activity instance RunExecutionService reports to, by layer memoization. Layer.provide( ServerBrowser.layer.pipe( diff --git a/apps/server/src/ws.test.ts b/apps/server/src/ws.test.ts index f8ad4d3229bab..418474b058bbc 100644 --- a/apps/server/src/ws.test.ts +++ b/apps/server/src/ws.test.ts @@ -114,6 +114,7 @@ import * as ProviderMaintenanceRunner from "./provider/providerMaintenanceRunner import * as ProviderRegistry from "./provider/ProviderRegistry.ts"; import * as ProviderSessionManager from "./orchestration-v2/ProviderSessionManager.ts"; import * as PullRequestReviewGuideService from "./pullRequest/PullRequestReviewGuideService.ts"; +import * as IssueService from "./issue/IssueService.ts"; import * as PullRequestService from "./pullRequest/PullRequestService.ts"; import * as PullRequestSyncReactor from "./orchestration-v2/PullRequestSyncReactor.ts"; import * as RelayClient from "@t3tools/shared/relayClient"; @@ -858,6 +859,7 @@ const layerUnusedConfigRpcServices = Layer.mergeAll( Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({}), Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({}), Layer.mock(PullRequestService.PullRequestService)({}), + Layer.mock(IssueService.IssueService)({}), Layer.mock(PullRequestSyncReactor.PullRequestSyncReactor)({}), Layer.mock(RelayClient.RelayClient)({}), Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({}), diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 5cbca7c24a93d..59162c47a95ac 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -249,6 +249,7 @@ import * as UsageService from "./usage/UsageService.ts"; import * as TraceDiagnostics from "./diagnostics/TraceDiagnostics.ts"; import * as TriageService from "./triage/TriageService.ts"; import * as PullRequestService from "./pullRequest/PullRequestService.ts"; +import * as IssueService from "./issue/IssueService.ts"; import * as PullRequestReviewGuideService from "./pullRequest/PullRequestReviewGuideService.ts"; import { listLinkedPullRequestThreads } from "./pullRequest/linkedThreads.ts"; import { pullRequestSyncKey } from "./pullRequest/pullRequestSyncKey.ts"; @@ -716,7 +717,13 @@ const ServerWsRpcGroup = WsRpcGroup.middleware(RpcInstrumentation); // crosses TypeScript's instantiation limit and the layer's requirements degrade to // `any`, so the handlers are registered as two disjoint, smaller groups. type ServerWsRpc = RpcGroup.Rpcs; -const SPLIT_WS_RPC_PREFIXES = ["server", "pullRequests", "provider", "orchestration"] as const; +const SPLIT_WS_RPC_PREFIXES = [ + "server", + "pullRequests", + "issues", + "provider", + "orchestration", +] as const; type SplitWsRpc = Extract< ServerWsRpc, { readonly _tag: `${(typeof SPLIT_WS_RPC_PREFIXES)[number]}.${string}` } @@ -1474,6 +1481,7 @@ export const layerWsRpc = ( const secretRequests = yield* SecretRequests.SecretRequests; const guestShares = yield* GuestShareService.GuestShareService; const pullRequests = yield* PullRequestService.PullRequestService; + const issues = yield* IssueService.IssueService; const reviewGuides = yield* PullRequestReviewGuideService.PullRequestReviewGuideService; const pullRequestSync = yield* PullRequestSyncReactor.PullRequestSyncReactor; const deviceService = yield* DeviceService.DeviceService; @@ -1962,6 +1970,16 @@ export const layerWsRpc = ( }); const handlers = ServerWsRpcGroup.of({ + [WS_METHODS.issuesList]: (input) => issues.list(input), + [WS_METHODS.issuesDetail]: (input) => issues.detail(input), + [WS_METHODS.issuesComments]: (input) => issues.comments(input), + [WS_METHODS.issuesTemplates]: (input) => issues.templates(input), + [WS_METHODS.issuesCandidates]: (input) => issues.candidates(input), + [WS_METHODS.issuesCreate]: (input) => issues.create(input), + [WS_METHODS.issuesComment]: (input) => issues.comment(input), + [WS_METHODS.issuesUpdate]: (input) => issues.update(input), + [WS_METHODS.issuesSetState]: (input) => issues.setState(input), + [WS_METHODS.issuesReact]: (input) => issues.react(input), [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command) => Effect.annotateCurrentSpan({ "orchestration_v2.command_id": command.commandId, @@ -3576,6 +3594,7 @@ export const layer = Layer.unwrap( const serverSelfUpdate = yield* ServerSelfUpdate.ServerSelfUpdate; const npmRelease = yield* NpmRelease.NpmRelease; const pullRequests = yield* PullRequestService.PullRequestService; + const issues = yield* IssueService.IssueService; const reviewGuides = yield* PullRequestReviewGuideService.PullRequestReviewGuideService; const peerLinks = yield* PeerLinks.PeerLinks; const peerLinkRequests = yield* PeerLinkRequests.PeerLinkRequests; @@ -3665,6 +3684,7 @@ export const layer = Layer.unwrap( Layer.provide(Layer.succeed(ServerSelfUpdate.ServerSelfUpdate, serverSelfUpdate)), // Share server-lifetime PR caches so WS mutations invalidate every client's HTTP diffs. Layer.provide(Layer.succeed(PullRequestService.PullRequestService, pullRequests)), + Layer.provide(Layer.succeed(IssueService.IssueService, issues)), Layer.provide(OtlpEndpointCheck.layer), Layer.provide( Layer.succeed( diff --git a/packages/contracts/src/clientRpcPermissions.ts b/packages/contracts/src/clientRpcPermissions.ts index 8681c1ea8abeb..f3ff5fa5ab42c 100644 --- a/packages/contracts/src/clientRpcPermissions.ts +++ b/packages/contracts/src/clientRpcPermissions.ts @@ -40,6 +40,11 @@ export const CLIENT_GUARDED_RPC_SCOPES = { [WS_METHODS.pullRequestsSetFilesViewed]: AuthSourceControlWriteScope, [WS_METHODS.pullRequestsRequestReviewers]: AuthSourceControlWriteScope, [WS_METHODS.pullRequestsSetLabels]: AuthSourceControlWriteScope, + [WS_METHODS.issuesCreate]: AuthSourceControlWriteScope, + [WS_METHODS.issuesComment]: AuthSourceControlWriteScope, + [WS_METHODS.issuesUpdate]: AuthSourceControlWriteScope, + [WS_METHODS.issuesSetState]: AuthSourceControlWriteScope, + [WS_METHODS.issuesReact]: AuthSourceControlWriteScope, // Writing a guide runs a model on this environment's providers, which reading one does not. [WS_METHODS.pullRequestsGenerateReviewGuide]: AuthOrchestrationOperateScope, [WS_METHODS.projectsCreateFromThread]: AuthOrchestrationOperateScope, diff --git a/packages/contracts/src/index.ts b/packages/contracts/src/index.ts index 8dd47df33a21f..c005fdd03900d 100644 --- a/packages/contracts/src/index.ts +++ b/packages/contracts/src/index.ts @@ -36,6 +36,7 @@ export * from "./projectClone.ts"; export * from "./speech.ts"; export * from "./pullRequest.ts"; export * from "./pullRequestReviewGuide.ts"; +export * from "./issue.ts"; export * from "./orchestrationDispatch.ts"; export * from "./orchestrationProject.ts"; export * from "./orchestrationV2.ts"; diff --git a/packages/contracts/src/issue.ts b/packages/contracts/src/issue.ts new file mode 100644 index 0000000000000..936c573c66e06 --- /dev/null +++ b/packages/contracts/src/issue.ts @@ -0,0 +1,471 @@ +import * as Schema from "effect/Schema"; +import * as HttpServerRespondable from "effect/http/HttpServerRespondable"; +import * as HttpServerResponse from "effect/http/HttpServerResponse"; + +import { + IsoDateTime, + NonNegativeInt, + PositiveInt, + ProjectId, + TrimmedNonEmptyString, +} from "./baseSchemas.ts"; +import { PullRequestActor, PullRequestLabel, PullRequestState } from "./pullRequest.ts"; + +/** + * Which tracker an issue lives in. Git hosts reuse their `SourceControlProviderKind` spelling + * (`github`, `gitlab`); standalone trackers get their own (`linear`, `jira`). Branded apart from + * the source control kind because the two sets are not the same: Bitbucket is a git host with no + * tracker, and Linear is a tracker with no git host. + */ +export const IssueTrackerKind = TrimmedNonEmptyString.check( + Schema.isMaxLength(64), + Schema.isPattern(/^[a-z][a-z0-9-]*$/), +).pipe(Schema.brand("IssueTrackerKind")); +export type IssueTrackerKind = typeof IssueTrackerKind.Type; + +export const IssueActor = PullRequestActor; +export type IssueActor = typeof IssueActor.Type; + +export const IssueLabel = PullRequestLabel; +export type IssueLabel = typeof IssueLabel.Type; + +/** + * Where an issue stands, in two parts: the category every tracker has, and the name this tracker + * gives it. GitHub only has the two; Linear and Jira have a workflow of their own ("In Review", + * "Backlog"), which `name` carries verbatim so the page can show the tracker's word while every + * filter and badge keys off `category`. + */ +export const IssueStateCategory = Schema.Literals(["open", "closed"]); +export type IssueStateCategory = typeof IssueStateCategory.Type; + +export const IssueState = Schema.Struct({ + category: IssueStateCategory, + name: TrimmedNonEmptyString, + /** Why it was closed, where the tracker records one (GitHub: `completed` / `not-planned`). */ + closeReason: Schema.optional(TrimmedNonEmptyString), +}); +export type IssueState = typeof IssueState.Type; + +/** What a listing asks for: either category, or both. */ +export const IssueListState = Schema.Literals(["all", "open", "closed"]); +export type IssueListState = typeof IssueListState.Type; + +export const IssueInvolvement = Schema.Literals(["all", "assigned", "authored", "mentioned"]); +export type IssueInvolvement = typeof IssueInvolvement.Type; + +export const IssueReactionContent = Schema.Literals([ + "thumbs-up", + "thumbs-down", + "laugh", + "hooray", + "confused", + "heart", + "rocket", + "eyes", +]); +export type IssueReactionContent = typeof IssueReactionContent.Type; + +export const IssueReaction = Schema.Struct({ + content: IssueReactionContent, + count: PositiveInt, + /** Who reacted, as far as the tracker named them; the viewer is left out and shown as "You". */ + actors: Schema.Array(TrimmedNonEmptyString), + viewerHasReacted: Schema.Boolean, +}); +export type IssueReaction = typeof IssueReaction.Type; + +/** + * Addresses one issue: `scope` is the tracker's own container (`owner/repo`, a team key) and + * `key` the issue's native id in it. `host` is absent for the kind's public host. `projectId` + * names the project whose credentials serve the read, which need not own the issue. + */ +export const IssueRef = Schema.Struct({ + projectId: ProjectId, + tracker: IssueTrackerKind, + host: Schema.optional(TrimmedNonEmptyString), + scope: TrimmedNonEmptyString, + key: TrimmedNonEmptyString, +}); +export type IssueRef = typeof IssueRef.Type; + +/** A scope with no issue in it, for reads about the container itself: templates, candidates. */ +export const IssueScopeRef = Schema.Struct({ + projectId: ProjectId, + tracker: IssueTrackerKind, + host: Schema.optional(TrimmedNonEmptyString), + scope: TrimmedNonEmptyString, +}); +export type IssueScopeRef = typeof IssueScopeRef.Type; + +/** One row of a listing. Carries everything a list needs and nothing a detail needs. */ +export const IssueSummary = Schema.Struct({ + tracker: IssueTrackerKind, + host: TrimmedNonEmptyString, + scope: TrimmedNonEmptyString, + key: TrimmedNonEmptyString, + /** How the tracker writes the key for people: `#123` on a git host, `ENG-123` on Linear. */ + displayKey: TrimmedNonEmptyString, + title: TrimmedNonEmptyString, + url: TrimmedNonEmptyString, + author: Schema.NullOr(IssueActor), + state: IssueState, + createdAt: IsoDateTime, + updatedAt: IsoDateTime, + closedAt: Schema.NullOr(IsoDateTime), + assignees: Schema.Array(IssueActor), + labels: Schema.Array(IssueLabel), + commentCount: NonNegativeInt, +}); +export type IssueSummary = typeof IssueSummary.Type; + +export const IssueListEntry = Schema.Struct({ + ...IssueSummary.fields, + projectId: ProjectId, + projectTitle: TrimmedNonEmptyString, +}); +export type IssueListEntry = typeof IssueListEntry.Type; + +export const IssueComment = Schema.Struct({ + id: TrimmedNonEmptyString, + author: Schema.NullOr(IssueActor), + body: Schema.String, + createdAt: IsoDateTime, + url: Schema.NullOr(Schema.String), + /** Absent from a tracker with no reactions, which differs from none on this comment. */ + reactions: Schema.optional(Schema.Array(IssueReaction)), +}); +export type IssueComment = typeof IssueComment.Type; + +/** + * Something that happened to an issue that nobody wrote words for: it was closed, labelled, + * assigned, renamed. Kinds are the common vocabulary; a tracker drops what it cannot map rather + * than guessing. + */ +export const IssueEventKind = Schema.Literals([ + "closed", + "reopened", + "labeled", + "unlabeled", + "assigned", + "unassigned", + "renamed", + "referenced", + "state-changed", +]); +export type IssueEventKind = typeof IssueEventKind.Type; + +export const IssueEvent = Schema.Struct({ + id: TrimmedNonEmptyString, + kind: IssueEventKind, + actor: Schema.NullOr(IssueActor), + createdAt: IsoDateTime, + /** What the event was about, where it has a subject: a label name, a login, a new title. */ + detail: Schema.NullOr(TrimmedNonEmptyString), +}); +export type IssueEvent = typeof IssueEvent.Type; + +/** A change request the tracker says closes or mentions this issue. */ +export const IssueLinkedPullRequest = Schema.Struct({ + repository: TrimmedNonEmptyString, + number: PositiveInt, + title: TrimmedNonEmptyString, + url: TrimmedNonEmptyString, + state: PullRequestState, + isDraft: Schema.Boolean, + /** Merging it closes this issue, rather than only citing it. */ + closesIssue: Schema.Boolean, +}); +export type IssueLinkedPullRequest = typeof IssueLinkedPullRequest.Type; + +/** + * What a tracker can do at all, so a surface never offers an action that would fail. The + * service refuses a call outside these before it reaches the driver. + */ +export const IssueCapabilities = Schema.Struct({ + create: Schema.Boolean, + /** Title and body can be rewritten after filing. */ + edit: Schema.Boolean, + comment: Schema.Boolean, + labels: Schema.Boolean, + assignees: Schema.Boolean, + /** Reactions can be read and written on the issue and its comments. */ + reactions: Schema.Boolean, + /** The scope offers starting points for a new issue. */ + templates: Schema.Boolean, + /** The tracker reports linked change requests of its own accord. */ + linkedPullRequests: Schema.Boolean, + /** State changes arrive as events beside the comments, not only as comments. */ + events: Schema.Boolean, + /** The tracker narrows a listing by free text itself. */ + search: Schema.Boolean, + /** + * The states this tracker can be asked to move an issue into, by name. A git host offers + * `open` and `closed`; a workflow tracker offers its own columns. Empty means state cannot be + * changed from here. + */ + states: Schema.Array(IssueState), + /** Reasons a close can carry. Empty means a close takes none. */ + closeReasons: Schema.Array(TrimmedNonEmptyString), +}); +export type IssueCapabilities = typeof IssueCapabilities.Type; + +/** + * What the signed-in account may do with this issue, which `capabilities` says nothing about. + * A permission the tracker reports nothing about is granted, so a refusal comes from the + * tracker, which says why, rather than from a missing control. + */ +export const IssueViewerPermissions = Schema.Struct({ + setState: Schema.Boolean, + comment: Schema.Boolean, + edit: Schema.Boolean, + labels: Schema.Boolean, + assignees: Schema.Boolean, +}); +export type IssueViewerPermissions = typeof IssueViewerPermissions.Type; + +export const IssueDetail = Schema.Struct({ + ...IssueSummary.fields, + projectId: ProjectId, + projectTitle: TrimmedNonEmptyString, + /** False for an issue outside every project's scope, which is read-only through this server. */ + writable: Schema.Boolean, + capabilities: IssueCapabilities, + viewerPermissions: IssueViewerPermissions, + /** The signed-in account, so a comment edit is offered only on the viewer's own remarks. */ + viewer: Schema.NullOr(TrimmedNonEmptyString), + body: Schema.String, + reactions: Schema.optional(Schema.Array(IssueReaction)), + comments: Schema.Array(IssueComment), + /** Where older comments carry on; null once the conversation is whole. */ + commentsCursor: Schema.NullOr(TrimmedNonEmptyString), + events: Schema.Array(IssueEvent), + linkedPullRequests: Schema.Array(IssueLinkedPullRequest), +}); +export type IssueDetail = typeof IssueDetail.Type; + +/** Older comments, one page past a cursor the detail or a previous page handed over. */ +export const IssueCommentsPageInput = Schema.Struct({ + ...IssueRef.fields, + cursor: TrimmedNonEmptyString.check(Schema.isMaxLength(4_096)), +}); +export type IssueCommentsPageInput = typeof IssueCommentsPageInput.Type; + +export const IssueCommentsPage = Schema.Struct({ + comments: Schema.Array(IssueComment), + nextCursor: Schema.NullOr(TrimmedNonEmptyString), +}); +export type IssueCommentsPage = typeof IssueCommentsPage.Type; + +/** Where each scope a listing already reached carries on, keyed `" "`. */ +export const IssueListCursors = Schema.Record( + TrimmedNonEmptyString, + TrimmedNonEmptyString.check(Schema.isMaxLength(4_096)), +); +export type IssueListCursors = typeof IssueListCursors.Type; + +export const IssueListInput = Schema.Struct({ + state: IssueListState, + involvement: Schema.optional(IssueInvolvement), + projectId: Schema.optional(ProjectId), + /** Narrows to one host, named as the host itself. Absent means every host the workspace has. */ + host: Schema.optional(TrimmedNonEmptyString), + /** Rows per scope, which with a continuation is rows per slice. */ + limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 200 }))), + /** Carry on from an earlier answer: only the scopes named here are read, one slice each. */ + cursors: Schema.optional(IssueListCursors), + /** Free text the trackers themselves are asked to match. */ + query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))), +}); +export type IssueListInput = typeof IssueListInput.Type; + +/** One tracker account the workspace has projects on, and whether it can be read right now. */ +export const IssueTrackerSummary = Schema.Struct({ + tracker: IssueTrackerKind, + host: TrimmedNonEmptyString, + projectCount: PositiveInt, + capabilities: Schema.NullOr(IssueCapabilities), + /** False when the tracker's tool or credentials are missing, with `detail` saying which. */ + configured: Schema.Boolean, + detail: Schema.NullOr(TrimmedNonEmptyString), +}); +export type IssueTrackerSummary = typeof IssueTrackerSummary.Type; + +export const IssueListProjectError = Schema.Struct({ + projectId: ProjectId, + projectTitle: TrimmedNonEmptyString, + message: TrimmedNonEmptyString, +}); +export type IssueListProjectError = typeof IssueListProjectError.Type; + +export const IssueListResult = Schema.Struct({ + /** The signed-in account per host, which is what involvement compares against. */ + viewers: Schema.Record(TrimmedNonEmptyString, TrimmedNonEmptyString), + trackers: Schema.Array(IssueTrackerSummary), + /** By update, newest first, across every scope this answer covers. */ + entries: Schema.Array(IssueListEntry), + errors: Schema.Array(IssueListProjectError), + /** Where each scope carries on, sent straight back as `cursors`. Absent once a scope is done. */ + nextCursors: IssueListCursors, +}); +export type IssueListResult = typeof IssueListResult.Type; + +const IssueTitle = TrimmedNonEmptyString.check(Schema.isMaxLength(1024)); +// Not trimmed: a body is markdown, where leading spaces open a code block. The bound keeps +// oversized payloads off the wire; the service rejects a comment that is only whitespace. +const IssueBody = Schema.String.check(Schema.isMaxLength(65_536)); +const CommentBody = Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536)); + +export const IssueCreateInput = Schema.Struct({ + ...IssueScopeRef.fields, + title: IssueTitle, + /** May be empty: an issue with a title and nothing else is a legitimate one. */ + body: IssueBody, + labels: Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(50)), + assignees: Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(25)), +}); +export type IssueCreateInput = typeof IssueCreateInput.Type; + +export const IssueCreateResult = Schema.Struct({ + key: TrimmedNonEmptyString, + displayKey: TrimmedNonEmptyString, + url: TrimmedNonEmptyString, +}); +export type IssueCreateResult = typeof IssueCreateResult.Type; + +export const IssueCommentInput = Schema.Struct({ + ...IssueRef.fields, + body: CommentBody, +}); +export type IssueCommentInput = typeof IssueCommentInput.Type; + +/** + * Rewrites the issue's own fields. Every field is optional and only the present ones are + * written; `labels` and `assignees` are the whole set, which is how every tracker writes them. + * The service refuses a request with nothing present. + */ +export const IssueUpdateInput = Schema.Struct({ + ...IssueRef.fields, + title: Schema.optional(IssueTitle), + body: Schema.optional(IssueBody), + labels: Schema.optional(Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(50))), + assignees: Schema.optional(Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(25))), +}); +export type IssueUpdateInput = typeof IssueUpdateInput.Type; + +/** Moves the issue to one of the states the tracker declared in `capabilities.states`. */ +export const IssueSetStateInput = Schema.Struct({ + ...IssueRef.fields, + state: TrimmedNonEmptyString, + /** Only meaningful for a closing state on a tracker that declared `closeReasons`. */ + closeReason: Schema.optional(TrimmedNonEmptyString), +}); +export type IssueSetStateInput = typeof IssueSetStateInput.Type; + +export const IssueReactionInput = Schema.Struct({ + ...IssueRef.fields, + /** Absent reacts to the issue itself; present names a comment id as the detail carried it. */ + subjectId: Schema.optional(TrimmedNonEmptyString), + content: IssueReactionContent, + reacted: Schema.Boolean, +}); +export type IssueReactionInput = typeof IssueReactionInput.Type; + +/** + * One starting point a scope offers for a new issue. Only the body-shaped kind travels here: + * GitHub's typed issue forms are flattened to a body by the driver, so a composer on any + * tracker is one text box with a draft in it. + */ +export const IssueTemplate = Schema.Struct({ + key: TrimmedNonEmptyString, + name: TrimmedNonEmptyString, + about: Schema.String, + title: Schema.String, + body: Schema.String, + labels: Schema.Array(TrimmedNonEmptyString), + assignees: Schema.Array(TrimmedNonEmptyString), +}); +export type IssueTemplate = typeof IssueTemplate.Type; + +export const IssueTemplateList = Schema.Struct({ + templates: Schema.Array(IssueTemplate), + /** False where the scope's config forbids an issue filed from nothing. */ + blankIssuesEnabled: Schema.Boolean, +}); +export type IssueTemplateList = typeof IssueTemplateList.Type; + +export const IssueLabelCandidate = Schema.Struct({ + ...IssueLabel.fields, + description: Schema.NullOr(Schema.String), +}); +export type IssueLabelCandidate = typeof IssueLabelCandidate.Type; + +export const IssueAssigneeCandidate = Schema.Struct({ + ...IssueActor.fields, + /** How the tracker addresses this person on a write, which is not always the login. */ + id: TrimmedNonEmptyString, +}); +export type IssueAssigneeCandidate = typeof IssueAssigneeCandidate.Type; + +/** The labels and people a scope offers, read when a picker opens rather than with every issue. */ +export const IssueCandidates = Schema.Struct({ + labels: Schema.Array(IssueLabelCandidate), + assignees: Schema.Array(IssueAssigneeCandidate), + /** The tracker has more than were read, so somebody missing is not necessarily ineligible. */ + truncated: Schema.Boolean, +}); +export type IssueCandidates = typeof IssueCandidates.Type; + +export const IssueUnavailableReason = Schema.Literals([ + "tool-missing", + "unauthenticated", + "tracker-unsupported", +]); +export type IssueUnavailableReason = typeof IssueUnavailableReason.Type; + +/** The tracker cannot be read at all for this project, as opposed to one request failing. */ +export class IssueUnavailableError extends Schema.TaggedError()( + "IssueUnavailableError", + { + reason: IssueUnavailableReason, + tracker: Schema.optional(IssueTrackerKind), + cause: Schema.optional(Schema.Defect()), + }, + { httpApiStatus: 503 }, +) { + [HttpServerRespondable.symbol]() { + return HttpServerResponse.schemaJson(IssueUnavailableError)(this, { status: 503 }); + } + + override get message(): string { + switch (this.reason) { + case "tool-missing": + return "The tool this tracker is read through is not installed or set up."; + case "unauthenticated": + return "This tracker has no working credentials."; + case "tracker-unsupported": + return "Issues cannot be browsed for this project yet."; + } + } +} + +export class IssueOperationError extends Schema.TaggedError()( + "IssueOperationError", + { + operation: Schema.String, + detail: TrimmedNonEmptyString, + reason: Schema.optional(Schema.Literals(["not-found", "read-only", "rate-limited"])), + retryAt: Schema.optional(Schema.Number), + cause: Schema.optional(Schema.Defect()), + }, + { httpApiStatus: 502 }, +) { + [HttpServerRespondable.symbol]() { + return HttpServerResponse.schemaJson(IssueOperationError)(this, { status: 502 }); + } + + override get message(): string { + return `Issue operation ${this.operation} failed: ${this.detail}`; + } +} + +export type IssueError = IssueUnavailableError | IssueOperationError; diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index e1d392cd080b6..2d4e057b7ae01 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -169,6 +169,25 @@ import { ProviderUploadFeedbackResult, } from "./provider.ts"; import { ProviderInstanceId, ProviderInstanceMutation } from "./providerInstance.ts"; +import { + IssueCandidates, + IssueCommentInput, + IssueCommentsPage, + IssueCommentsPageInput, + IssueCreateInput, + IssueCreateResult, + IssueDetail, + IssueListInput, + IssueListResult, + IssueOperationError, + IssueReactionInput, + IssueRef, + IssueScopeRef, + IssueSetStateInput, + IssueTemplateList, + IssueUnavailableError, + IssueUpdateInput, +} from "./issue.ts"; import { GuestShareCreateError, GuestShareCreateInput, @@ -602,6 +621,18 @@ export const WS_METHODS = { pullRequestsLabelCandidates: "pullRequests.labelCandidates", pullRequestsSetLabels: "pullRequests.setLabels", + // Issue tracking methods + issuesList: "issues.list", + issuesDetail: "issues.detail", + issuesComments: "issues.comments", + issuesTemplates: "issues.templates", + issuesCandidates: "issues.candidates", + issuesCreate: "issues.create", + issuesComment: "issues.comment", + issuesUpdate: "issues.update", + issuesSetState: "issues.setState", + issuesReact: "issues.react", + sourceControlLookupRepository: "sourceControl.lookupRepository", sourceControlCloneRepository: "sourceControl.cloneRepository", sourceControlPublishRepository: "sourceControl.publishRepository", @@ -1283,6 +1314,74 @@ const WsPullRequestsSetLabelsRpc = Rpc.make(WS_METHODS.pullRequestsSetLabels, { error: PullRequestRpcError, }); +const IssueRpcError = Schema.Union([ + IssueUnavailableError, + IssueOperationError, + EnvironmentAuthorizationError, +]); + +const WsIssuesListRpc = Rpc.make(WS_METHODS.issuesList, { + payload: IssueListInput, + success: IssueListResult, + error: IssueRpcError, +}); + +const WsIssuesDetailRpc = Rpc.make(WS_METHODS.issuesDetail, { + payload: IssueRef, + success: IssueDetail, + error: IssueRpcError, +}); + +/** Older comments, one page at a time, past the cursor the detail handed over. */ +const WsIssuesCommentsRpc = Rpc.make(WS_METHODS.issuesComments, { + payload: IssueCommentsPageInput, + success: IssueCommentsPage, + error: IssueRpcError, +}); + +const WsIssuesTemplatesRpc = Rpc.make(WS_METHODS.issuesTemplates, { + payload: IssueScopeRef, + success: IssueTemplateList, + error: IssueRpcError, +}); + +/** Read when a label or assignee picker opens, not with every issue. */ +const WsIssuesCandidatesRpc = Rpc.make(WS_METHODS.issuesCandidates, { + payload: IssueScopeRef, + success: IssueCandidates, + error: IssueRpcError, +}); + +const WsIssuesCreateRpc = Rpc.make(WS_METHODS.issuesCreate, { + payload: IssueCreateInput, + success: IssueCreateResult, + error: IssueRpcError, +}); + +const WsIssuesCommentRpc = Rpc.make(WS_METHODS.issuesComment, { + payload: IssueCommentInput, + success: Schema.Void, + error: IssueRpcError, +}); + +const WsIssuesUpdateRpc = Rpc.make(WS_METHODS.issuesUpdate, { + payload: IssueUpdateInput, + success: Schema.Void, + error: IssueRpcError, +}); + +const WsIssuesSetStateRpc = Rpc.make(WS_METHODS.issuesSetState, { + payload: IssueSetStateInput, + success: Schema.Void, + error: IssueRpcError, +}); + +const WsIssuesReactRpc = Rpc.make(WS_METHODS.issuesReact, { + payload: IssueReactionInput, + success: Schema.Void, + error: IssueRpcError, +}); + const WsSourceControlLookupRepositoryRpc = Rpc.make(WS_METHODS.sourceControlLookupRepository, { payload: SourceControlRepositoryLookupInput, success: SourceControlRepositoryInfo, @@ -2111,6 +2210,16 @@ export class RpcScopeAuthorization extends RpcMiddleware.Service()( + "IssueTrackerError", + { + tracker: IssueTrackerKindSchema, + operation: Schema.String, + reason: Schema.Literals([ + "missing-tool", + "unauthenticated", + "rate-limited", + "not-found", + "failed", + ]), + detail: Schema.String, + retryAt: Schema.optional(Schema.Number), + cause: Schema.optional(Schema.Defect()), + }, +) { + override get message(): string { + return `${this.tracker} failed in ${this.operation}: ${this.detail}`; + } +} + +/** + * Where a tracker's issues are addressed from. `cwd` is a checkout whose credentials serve the + * read; `host` is where the tracker is served from (`github.com`, a GHE install, `linear.app`); + * `scope` is the tracker's own container — a repository path, a team or project key. + */ +export interface IssueScopeTarget { + readonly cwd: string; + readonly host: string; + readonly scope: string; +} + +export interface IssueTarget extends IssueScopeTarget { + /** The issue's native id within the scope: `"123"` on GitHub, `"ENG-123"` on Linear. */ + readonly key: string; +} + +/** An issue as the tracker sees it, before the service attaches project context. */ +export type TrackerIssueSummary = Omit; + +export interface TrackerIssuePage { + readonly items: ReadonlyArray; + /** Where the next slice starts, or null once the tracker has nothing more. Opaque. */ + readonly nextCursor: string | null; +} + +export interface TrackerIssueDetail extends TrackerIssueSummary { + readonly body: string; + readonly viewer: string | null; + readonly viewerPermissions: IssueViewerPermissions; + readonly reactions?: ReadonlyArray; + readonly comments: ReadonlyArray; + /** Where older comments carry on; null once the conversation is whole. */ + readonly commentsCursor: string | null; + readonly events: ReadonlyArray; + readonly linkedPullRequests: ReadonlyArray; +} + +export interface IssueTrackerApi { + readonly kind: IssueTrackerKind; + readonly capabilities: IssueCapabilities; + + /** The signed-in account, which is what involvement filtering compares against. */ + readonly getViewer: (input: { + readonly cwd: string; + readonly host: string; + }) => Effect.Effect; + + readonly listIssues: ( + input: IssueScopeTarget & { + readonly state: IssueListState; + readonly involvement: IssueInvolvement; + readonly viewer: string; + readonly limit: number; + /** Free text as the tracker understands it; ignored by a tracker without `search`. */ + readonly query?: string | undefined; + /** A cursor this tracker handed back from an earlier page. Absent asks for the first. */ + readonly cursor?: string | undefined; + }, + ) => Effect.Effect; + + /** The issue with its first page of conversation, in one read where the tracker allows. */ + readonly getIssue: (input: IssueTarget) => Effect.Effect; + + /** One page of older comments past a cursor `getIssue` or a previous page handed over. */ + readonly getComments: ( + input: IssueTarget & { readonly cursor: string }, + ) => Effect.Effect; + + /** Only called when `capabilities.create` is true. */ + readonly create?: ( + input: IssueScopeTarget & { + readonly title: string; + readonly body: string; + readonly labels: ReadonlyArray; + readonly assignees: ReadonlyArray; + }, + ) => Effect.Effect; + + /** Only called when `capabilities.comment` is true. */ + readonly comment?: ( + input: IssueTarget & { readonly body: string }, + ) => Effect.Effect; + + /** + * Rewrites the present fields. Only called with a field the tracker declared it can write + * (`edit` for title and body, `labels`, `assignees`), and never with all of them absent. + */ + readonly update?: ( + input: IssueTarget & { + readonly title?: string | undefined; + readonly body?: string | undefined; + readonly labels?: ReadonlyArray | undefined; + readonly assignees?: ReadonlyArray | undefined; + }, + ) => Effect.Effect; + + /** Only called with a state name from `capabilities.states`. */ + readonly setState?: ( + input: IssueTarget & { + readonly state: string; + readonly closeReason?: string | undefined; + }, + ) => Effect.Effect; + + /** + * Adds a reaction or takes it back. `subjectId` is a comment id as the detail carried it; + * absent means the issue itself. Only called when `capabilities.reactions` is true. + */ + readonly setReaction?: ( + input: IssueTarget & { + readonly subjectId?: string | undefined; + readonly content: IssueReactionContent; + readonly reacted: boolean; + }, + ) => Effect.Effect; + + /** Only called when `capabilities.templates` is true. */ + readonly listTemplates?: ( + input: IssueScopeTarget, + ) => Effect.Effect; + + /** Only called when `capabilities.labels` or `capabilities.assignees` is true. */ + readonly listCandidates?: ( + input: IssueScopeTarget, + ) => Effect.Effect; +} diff --git a/packages/source-control-core/src/server/driver.ts b/packages/source-control-core/src/server/driver.ts index c4fb4106a2054..7f86d0fff3b9e 100644 --- a/packages/source-control-core/src/server/driver.ts +++ b/packages/source-control-core/src/server/driver.ts @@ -15,6 +15,7 @@ import type * as Scope from "effect/Scope"; import type { SourceControlDefinition } from "../shared/definition.ts"; import type { SourceControlProviderDiscoverySpec } from "./discovery.ts"; +import type { IssueTrackerApi } from "./IssueTracker.ts"; import type { PullRequestProviderApi } from "./PullRequestProvider.ts"; import type * as SourceControlHost from "./SourceControlHost.ts"; import type * as SourceControlProvider from "./SourceControlProvider.ts"; @@ -53,6 +54,8 @@ export interface SourceControlDriverInstance { readonly discovery: SourceControlProviderDiscoverySpec; /** Null for a host whose pull requests this build cannot read. */ readonly pullRequests: PullRequestProviderApi | null; + /** Absent for a host whose issues this build cannot read. */ + readonly issues?: IssueTrackerApi; /** * Fetches media the definition's `mediaUrl` claimed, with the credential the host serves it * to. Null when the fetch went nowhere a response could come from, such as a redirect loop. diff --git a/packages/source-control-core/src/shared/definition.ts b/packages/source-control-core/src/shared/definition.ts index 09248a72e35b3..34ee2ad2a4bbf 100644 --- a/packages/source-control-core/src/shared/definition.ts +++ b/packages/source-control-core/src/shared/definition.ts @@ -342,6 +342,11 @@ export interface SourceControlDefinition { * path, `owner/name`. */ readonly repositorySelector?: (identity: RepositorySelectorIdentity) => string | null; + /** + * The container the project's issues live in, where that is not its repository: Azure DevOps + * keeps work items per project. Null when the repository cannot be placed. + */ + readonly issueScope?: (identity: RepositorySelectorIdentity) => string | null; /** * The key of a remote's lower case `host/path`, `.git` suffix kept, for a host that spells it * apart from `normalizeGitRemoteUrl`: over SSH and the web, or with `.git` as part of the name. @@ -661,6 +666,8 @@ export interface SourceControlDefinitionRegistry { readonly repositorySelector: ( identity: RepositorySelectorIdentity | null | undefined, ) => string | null; + /** Where the project's issues live: the host's `issueScope`, else its repository. */ + readonly issueScope: (identity: RepositorySelectorIdentity | null | undefined) => string | null; readonly changeRequestHost: ( identity: ChangeRequestHostIdentity, kind: Kind, @@ -726,6 +733,10 @@ export function makeSourceControlDefinitionRegistry( }; const remoteOwner = (remote: GitRemoteHost) => recognitionOrder.find((definition) => definition.recognizeRemote(remote) !== null); + const repositorySelector = (identity: RepositorySelectorIdentity | null | undefined) => + identity?.provider === undefined + ? defaultRepositorySelector(identity) + : (byKind.get(identity.provider)?.repositorySelector ?? defaultRepositorySelector)(identity); const parseChangeRequestUrl = (value: string): ParsedChangeRequest | null => { const url = httpUrl(value); if (url === null) return null; @@ -826,12 +837,14 @@ export function makeSourceControlDefinitionRegistry( }, canonicalRepositoryKey: (key) => repositoryKeyOwner(key)?.canonical ?? canonicalKeyPath(key), findByRepositoryKey: (key) => repositoryKeyOwner(key)?.definition, - repositorySelector: (identity) => - identity?.provider === undefined - ? defaultRepositorySelector(identity) - : (byKind.get(identity.provider)?.repositorySelector ?? defaultRepositorySelector)( - identity, - ), + repositorySelector, + issueScope: (identity) => { + const issueScope = + identity?.provider === undefined ? undefined : byKind.get(identity.provider)?.issueScope; + return issueScope === undefined || identity == null + ? repositorySelector(identity) + : issueScope(identity); + }, changeRequestHost: (identity, kind) => (kind === undefined ? undefined : byKind.get(kind)?.changeRequestHost?.(identity)) ?? projectChangeRequestHost(identity, kind), diff --git a/packages/source-control-github/package.json b/packages/source-control-github/package.json index 61e4476bb2517..3fe7980d737ae 100644 --- a/packages/source-control-github/package.json +++ b/packages/source-control-github/package.json @@ -23,7 +23,8 @@ "@t3tools/provider-core": "workspace:*", "@t3tools/shared": "workspace:*", "@t3tools/source-control-core": "workspace:*", - "effect": "catalog:" + "effect": "catalog:", + "yaml": "catalog:" }, "devDependencies": { "@effect/graphql-generator": "catalog:", diff --git a/packages/source-control-github/schema/github.graphql.ts b/packages/source-control-github/schema/github.graphql.ts index 332060b960ff8..a2e6cb7f92e18 100644 --- a/packages/source-control-github/schema/github.graphql.ts +++ b/packages/source-control-github/schema/github.graphql.ts @@ -95,6 +95,17 @@ export type FileViewedState = typeof FileViewedState.Type export const IssueState = Schema.Literals(["CLOSED", "OPEN"]) export type IssueState = typeof IssueState.Type +/** + * The possible state reasons of an issue. + * + * - `COMPLETED`: An issue that has been closed as completed + * - `DUPLICATE`: An issue that has been closed as a duplicate. + * - `NOT_PLANNED`: An issue that has been closed as not planned + * - `REOPENED`: An issue that has been reopened + */ +export const IssueStateReason = Schema.Literals(["COMPLETED", "DUPLICATE", "NOT_PLANNED", "REOPENED"]) +export type IssueStateReason = typeof IssueStateReason.Type + /** * Detailed status information about a pull request merge. * @@ -298,10 +309,26 @@ export interface UpdateRefsInput extends Schema.Struct.Type Effect.succeedNone, + get: (host) => + Effect.succeed({ + host, + token: Redacted.make("test-token"), + source: "gh" as const, + fingerprint: "test", + }), + }), + Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ + enabled: false, + token: () => Effect.succeedNone, + invalidate: () => Effect.void, + }), + Layer.succeed(HttpClient.HttpClient, httpClient), + ), + ), + ); +} + +function tracker(onRequest: (variables: unknown) => void, templateRepository?: unknown) { + return GitHubIssueTracker.make.pipe( + Effect.provide( + transport( + HttpClient.make((request) => { + if (request.body._tag !== "Uint8Array") + return Effect.die("Expected a GraphQL JSON request."); + const { variables } = decodeVariables(new TextDecoder().decode(request.body.body)); + onRequest(variables); + const body = + templateRepository !== undefined + ? JSON.stringify({ data: { repository: templateRepository } }) + : "labelsCursor" in variables + ? JSON.stringify({ + data: { + repository: { + issue: { + labels: { + pageInfo: { hasNextPage: false, endCursor: null }, + nodes: [{ name: "needs repro", color: "ffffff" }], + }, + assignees: { + pageInfo: { hasNextPage: false, endCursor: null }, + nodes: [], + }, + }, + }, + }, + }) + : DETAIL_RESPONSE; + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + new Response(body, { + headers: { "content-type": "application/json" }, + }), + ), + ); + }), + ), + ), + ); +} + +const encodeTemplateList = Schema.encodeSync(IssueTemplateList); + +const decodeBody = Schema.decodeUnknownSync( + Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)), +); + +describe("GitHubIssueTracker", () => { + it.effect("writes an issue lifecycle without mutating pull requests or foreign comments", () => + Effect.gen(function* () { + const writes: unknown[] = []; + let issueState: { state: string; stateReason: string | null } = { + state: "OPEN", + stateReason: null, + }; + const http = HttpClient.make((request) => { + if (request.body._tag !== "Uint8Array") return Effect.die("Expected a JSON request."); + const text = new TextDecoder().decode(request.body.body); + let response: unknown = {}; + if (request.url === "https://api.github.com/graphql") { + const { query, variables } = decodeVariables(text); + expect(request.method).toBe("POST"); + const operation = query.match(/(?:query|mutation)\s+(\w+)/)?.[1]; + if (operation === "IssueNodeId" || operation === "IssueCommentScope") { + expect(variables).toMatchObject({ owner: "acme", name: "web" }); + response = { + data: { + repository: { + issue: variables["number"] === 42 ? { id: "I_42", ...issueState } : null, + }, + ...(operation === "IssueCommentScope" + ? { node: { __typename: "IssueComment", issue: { id: "I_other" } } } + : {}), + }, + }; + } else { + expect(["AddReaction", "RemoveReaction"]).toContain(operation); + expect(variables).toEqual({ subjectId: "I_42", content: "THUMBS_UP" }); + writes.push(operation); + response = { + data: { + [operation === "AddReaction" ? "addReaction" : "removeReaction"]: { + reaction: { content: "THUMBS_UP" }, + }, + }, + }; + } + } else { + const body = decodeBody(text); + writes.push([request.method, request.url, body]); + if (typeof body["state"] === "string") { + issueState = { + state: body["state"].toUpperCase(), + stateReason: + typeof body["state_reason"] === "string" + ? body["state_reason"].toUpperCase() + : null, + }; + } + if (request.url.endsWith("/issues")) { + response = { number: 42, html_url: "https://github.com/acme/web/issues/42" }; + } + } + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + new Response(JSON.stringify(response), { + headers: { "content-type": "application/json" }, + }), + ), + ); + }); + const api = yield* GitHubIssueTracker.make.pipe(Effect.provide(transport(http))); + const target = { cwd: "/w", host: "github.com", scope: "acme/web", key: "42" }; + expect( + yield* api.create({ + ...target, + title: "Fixture", + body: "Body", + labels: ["bug"], + assignees: ["octocat"], + }), + ).toEqual({ + key: "42", + displayKey: "#42", + url: "https://github.com/acme/web/issues/42", + }); + yield* api.comment({ ...target, body: "Comment" }); + yield* api.update({ ...target, title: "Updated", body: "Edited", labels: [], assignees: [] }); + yield* api.setState({ ...target, state: "closed", closeReason: "not-planned" }); + // GitHub ignores a new reason on a closed issue, so it is refused instead of sent. + expect( + yield* api + .setState({ ...target, state: "closed", closeReason: "completed" }) + .pipe(Effect.result), + ).toMatchObject({ _tag: "Failure", failure: { operation: "setState", reason: "failed" } }); + yield* api.setState({ ...target, state: "open" }); + yield* api.setReaction({ ...target, content: "thumbs-up", reacted: true }); + yield* api.setReaction({ ...target, content: "thumbs-up", reacted: false }); + + const pullRequest = { ...target, key: "50" }; + expect( + yield* api.update({ ...pullRequest, title: "Do not edit" }).pipe(Effect.result), + ).toMatchObject({ _tag: "Failure", failure: { operation: "update", reason: "not-found" } }); + expect( + yield* api.setState({ ...pullRequest, state: "closed" }).pipe(Effect.result), + ).toMatchObject({ + _tag: "Failure", + failure: { operation: "setState", reason: "not-found" }, + }); + expect( + yield* api.comment({ ...pullRequest, body: "Do not comment" }).pipe(Effect.result), + ).toMatchObject({ _tag: "Failure", failure: { operation: "comment", reason: "not-found" } }); + expect( + yield* Effect.flip( + api.setReaction({ + ...target, + subjectId: "IC_other", + content: "thumbs-up", + reacted: true, + }), + ), + ).toMatchObject({ operation: "setReaction", reason: "failed" }); + + expect(writes).toEqual([ + [ + "POST", + "https://api.github.com/repos/acme/web/issues", + { title: "Fixture", body: "Body", labels: ["bug"], assignees: ["octocat"] }, + ], + ["POST", "https://api.github.com/repos/acme/web/issues/42/comments", { body: "Comment" }], + [ + "PATCH", + "https://api.github.com/repos/acme/web/issues/42", + { title: "Updated", body: "Edited", labels: [], assignees: [] }, + ], + [ + "PATCH", + "https://api.github.com/repos/acme/web/issues/42", + { state: "closed", state_reason: "not_planned" }, + ], + ["PATCH", "https://api.github.com/repos/acme/web/issues/42", { state: "open" }], + "AddReaction", + "RemoveReaction", + ]); + }), + ); + + it.effect("offers YAML issue forms when blank issues are disabled", () => + Effect.gen(function* () { + const form = { + __typename: "Blob", + isTruncated: false, + text: `name: Bug report +description: Report broken behavior. +title: "[Bug]: " +labels: [bug, needs-triage] +assignees: "octocat, theo" +body: + - type: markdown + attributes: + value: Search existing issues first. + - type: checkboxes + attributes: + label: Before submitting + options: + - label: I searched existing issues. + required: true + - type: dropdown + attributes: + label: Area + description: Which part is affected? + options: [Web, Server] + default: 1 + validations: + required: true + - type: textarea + attributes: + label: Steps to reproduce + placeholder: Start the app. + - type: input + attributes: + label: Version + value: main + - type: upload + attributes: + label: Supporting files + description: Redact secrets before uploading. +`, + }; + const config = { + __typename: "Blob", + isTruncated: false, + text: '"blank_issues_enabled": false # use a template', + }; + const repository = { + issueTemplates: [], + templateDirectory: { + __typename: "Tree", + entries: [ + { name: "config.yml", object: config }, + { name: "bug_report.yml", object: form }, + { + name: "empty_name.yml", + object: { ...form, text: 'name: ""\ndescription: Empty name\nbody: []' }, + }, + { + name: "blank_name.yml", + object: { ...form, text: 'name: " "\ndescription: Blank name\nbody: []' }, + }, + { + name: "invalid.yml", + object: { ...form, text: "name: Missing required metadata\nbody: []" }, + }, + ], + }, + }; + const api = yield* tracker(() => {}, repository); + const input = { cwd: "/w", host: "github.com", scope: "acme/web" }; + const result = yield* api.listTemplates!(input); + expect(encodeTemplateList(result)).toEqual(result); + expect(result.blankIssuesEnabled).toBe(false); + expect(result.templates).toHaveLength(1); + expect(result.templates[0]).toMatchObject({ + key: "bug_report.yml", + name: "Bug report", + about: "Report broken behavior.", + title: "[Bug]: ", + labels: ["bug", "needs-triage"], + assignees: ["octocat", "theo"], + }); + expect(result.templates[0]?.body).toBe(`Search existing issues first. + +### Before submitting + +- [ ] I searched existing issues. (required) + +### Area + +Which part is affected? + +Required. + +Choose one: + +- Web +- Server (default) + +### Steps to reproduce + +Example: +> Start the app. + +### Version + +main + +### Supporting files + +Redact secrets before uploading.`); + config.isTruncated = true; + expect(yield* Effect.flip(api.listTemplates!(input))).toMatchObject({ + reason: "failed", + detail: "GitHub returned incomplete issue template contents.", + }); + }), + ); + + it.effect("maps a detail read to the neutral issue shape", () => + Effect.gen(function* () { + const seen: unknown[] = []; + const api = yield* tracker((variables) => { + seen.push(variables); + }); + const issue = yield* api.getIssue({ + cwd: "/w", + host: "github.com", + scope: "acme/web", + key: "42", + }); + expect(seen).toEqual([ + { owner: "acme", name: "web", number: 42 }, + { + owner: "acme", + name: "web", + number: 42, + labelsCursor: "labels-next", + assigneesCursor: null, + }, + ]); + expect(issue).toMatchObject({ + key: "42", + displayKey: "#42", + title: "Composer loses focus", + body: "Typing in the composer drops focus after a paste.", + state: { category: "closed", name: "closed", closeReason: "duplicate" }, + author: { + login: "octocat", + name: "Octo Cat", + avatarUrl: "https://avatars/octocat", + }, + assignees: [{ login: "theo" }], + labels: [ + { name: "bug", color: "d73a4a" }, + { name: "needs repro", color: "ffffff" }, + ], + commentCount: 3, + viewer: "julius", + // Triage may label, assign, close and reopen; `viewerCanUpdate: false` withholds edits. + viewerPermissions: { + setState: true, + edit: false, + labels: true, + assignees: true, + comment: true, + }, + commentsCursor: "Y3Vyc29y", + }); + // The viewer is left out of `actors` and shown as "You"; an empty group is dropped. + expect(issue.comments[0]?.reactions).toEqual([ + { content: "thumbs-up", count: 2, actors: ["theo"], viewerHasReacted: true }, + ]); + // The closing reference wins over the cross-reference sighting of the same pull request. + expect(issue.linkedPullRequests).toEqual([ + { + repository: "acme/web", + number: 50, + title: "fix(web): keep composer focus", + url: "https://github.com/acme/web/pull/50", + state: "merged", + isDraft: false, + closesIssue: true, + }, + ]); + // An event kind this page has no vocabulary for is dropped rather than guessed at. + expect(issue.events.map((event) => [event.kind, event.detail])).toEqual([ + ["labeled", "bug"], + ["referenced", "acme/web#50"], + ["closed", null], + ]); + }), + ); +}); diff --git a/packages/source-control-github/src/server/GitHubIssueTracker.ts b/packages/source-control-github/src/server/GitHubIssueTracker.ts new file mode 100644 index 0000000000000..d290522a08d16 --- /dev/null +++ b/packages/source-control-github/src/server/GitHubIssueTracker.ts @@ -0,0 +1,442 @@ +import * as Effect from "effect/Effect"; +import * as Result from "effect/Result"; +import { GraphQLClient, GraphQLClientError, GraphQLGroup } from "effect/graphql"; +import { + IssueTrackerKind, + type IssueCapabilities, + type IssueInvolvement, +} from "@t3tools/contracts"; +import { + IssueTrackerError, + type IssueTrackerApi, +} from "@t3tools/source-control-core/server/IssueTracker"; + +import * as GitHubRest from "./GitHubRest.ts"; +import { readIssueTemplates } from "./gitHubIssueTemplates.ts"; +import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts"; +import * as GitHubGraphQlClient from "./gitHubGraphQlClient.ts"; +import { IssuesGraphQLGroup } from "./issues.graphql.ts"; +import { AddReaction, RemoveReaction } from "./pullRequestMutations.graphql.ts"; +import { gitHubReactionContent } from "./gitHubPullRequestJson.ts"; +import { + decodeCreatedIssueJson, + toIssueCandidates, + toIssueComments, + toIssueDetail, + toIssueSearch, +} from "./gitHubIssueJson.ts"; + +const KIND = IssueTrackerKind.make("github"); + +const CAPABILITIES: IssueCapabilities = { + create: true, + edit: true, + comment: true, + labels: true, + assignees: true, + reactions: true, + templates: true, + linkedPullRequests: true, + events: true, + search: true, + states: [ + { category: "open", name: "open" }, + { category: "closed", name: "closed" }, + ], + closeReasons: ["completed", "not-planned"], +}; + +const REPOSITORY = /^[A-Za-z0-9][A-Za-z0-9-]*\/[A-Za-z0-9_.-]+$/; + +function searchTerms(query: string): ReadonlyArray { + return query + .split(/\s+/) + .filter((word) => word.length > 0) + .map((word) => `"${word.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`); +} + +const INVOLVEMENT_QUALIFIER: Record = { + all: null, + assigned: "assignee", + authored: "author", + mentioned: "mentions", +}; + +function searchQuery(input: { + readonly scope: string; + readonly state: "all" | "open" | "closed"; + readonly involvement: IssueInvolvement; + readonly viewer: string; + readonly query?: string | undefined; +}): string { + const qualifier = INVOLVEMENT_QUALIFIER[input.involvement]; + return [ + "is:issue", + `repo:${input.scope.trim()}`, + ...(input.state === "all" ? [] : [`is:${input.state}`]), + ...(qualifier === null ? [] : [`${qualifier}:${input.viewer}`]), + "sort:updated-desc", + ...searchTerms(input.query ?? ""), + ].join(" "); +} + +const CLOSE_REASON: Record = { + completed: "completed", + "not-planned": "not_planned", +}; + +function reasonFor( + error: GitHubRequestPolicy.GitHubApiError | GraphQLClientError.GraphQLClientError, +): IssueTrackerError["reason"] { + switch (error._tag) { + case "GitHubCliMissingError": + return "missing-tool"; + case "GitHubNotSignedInError": + case "GitHubHostDisabledError": + case "GitHubApiAuthenticationError": + return "unauthenticated"; + case "GitHubApiRateLimitError": + case "SourceControlRateLimitPausedError": + return "rate-limited"; + case "GitHubApiNotFoundError": + return "not-found"; + default: + return "failed"; + } +} + +const FAILURE_DETAIL: Record = { + "missing-tool": "GitHub CLI is not installed.", + unauthenticated: "GitHub authentication is unavailable. Sign in and retry.", + "rate-limited": "GitHub API rate limit exceeded.", + "not-found": "GitHub could not find the requested issue.", + failed: "The GitHub issue request failed.", +}; + +const group = GraphQLGroup.merge( + IssuesGraphQLGroup, + GraphQLGroup.make(AddReaction, RemoveReaction), +).middleware(GitHubGraphQlClient.GitHubGraphQlAuth); + +export const make = Effect.gen(function* () { + const api = yield* GitHubRest.GitHubRest; + const client = yield* GraphQLClient.make(group).pipe(Effect.provide(GitHubGraphQlClient.layer)); + + const fail = + (operation: string) => + (error: GitHubRequestPolicy.GitHubApiError | GraphQLClientError.GraphQLClientError) => + new IssueTrackerError({ + tracker: KIND, + operation, + reason: reasonFor(error), + detail: GraphQLClientError.isGraphQLClientError(error) + ? `GitHub returned an unreadable ${operation} response.` + : FAILURE_DETAIL[reasonFor(error)], + ...("retryAt" in error && error.retryAt !== undefined ? { retryAt: error.retryAt } : {}), + cause: error, + }); + + const invalid = (operation: string, detail: string, reason: "failed" | "not-found" = "failed") => + new IssueTrackerError({ tracker: KIND, operation, reason, detail }); + + const notFound = (operation: string) => + invalid(operation, "GitHub could not find the requested issue.", "not-found"); + + const repository = Effect.fn("GitHubIssueTracker.repository")(function* ( + scope: string, + operation: string, + ) { + if (!REPOSITORY.test(scope) || scope.endsWith("/.") || scope.endsWith("/..")) { + return yield* invalid(operation, "A repository was named that GitHub cannot address."); + } + const [owner = "", name = ""] = scope.split("/"); + return { owner, name }; + }); + + const issue = Effect.fn("GitHubIssueTracker.issue")(function* ( + input: { readonly scope: string; readonly key: string }, + operation: string, + ) { + const repo = yield* repository(input.scope, operation); + const number = Number(input.key); + if (!/^[1-9]\d*$/.test(input.key) || !Number.isSafeInteger(number) || number > 2_147_483_647) { + return yield* notFound(operation); + } + return { ...repo, number, path: `repos/${repo.owner}/${repo.name}/issues/${number}` }; + }); + + const graphql = + (host: string, operation: string) => + ( + effect: Effect.Effect, + ) => + effect.pipe(GitHubGraphQlClient.call({ host, operation }), Effect.mapError(fail(operation))); + + const verifiedIssue = Effect.fn("GitHubIssueTracker.verifiedIssue")(function* ( + input: { readonly host: string; readonly scope: string; readonly key: string }, + operation: string, + ) { + const target = yield* issue(input, operation); + const { owner, name, number } = target; + // REST issue endpoints also accept pull requests; resolve an Issue before writing. + const result = yield* client + .IssueNodeId({ owner, name, number }) + .pipe(graphql(input.host, operation)); + const found = result.repository?.issue; + if (found == null) return yield* notFound(operation); + return { ...target, id: found.id, state: found.state, stateReason: found.stateReason }; + }); + + const patchIssue = ( + input: { readonly host: string; readonly scope: string; readonly key: string }, + operation: string, + body: Record, + ) => + verifiedIssue(input, operation).pipe( + Effect.flatMap(({ path }) => + api + .rest({ host: input.host, operation, method: "PATCH", path, body }) + .pipe(Effect.asVoid, Effect.mapError(fail(operation))), + ), + ); + + return { + kind: KIND, + capabilities: CAPABILITIES, + + getViewer: (input) => + client.IssueViewer({}).pipe( + graphql(input.host, "getViewer"), + Effect.map(({ viewer }) => viewer.login), + ), + + listIssues: (input) => + repository(input.scope, "listIssues").pipe( + Effect.flatMap(() => + client + .IssueSearch({ + q: searchQuery(input), + cursor: input.cursor, + rows: Math.min(Math.max(Math.trunc(input.limit), 1), 100), + }) + .pipe(graphql(input.host, "listIssues"), Effect.map(toIssueSearch)), + ), + ), + + getIssue: Effect.fn("GitHubIssueTracker.getIssue")(function* (input) { + const { owner, name, number } = yield* issue(input, "getIssue"); + const result = yield* client + .IssueDetail({ owner, name, number }) + .pipe(graphql(input.host, "getIssue")); + const detail = toIssueDetail(result); + const raw = result.repository?.issue; + if (detail === null || raw == null) return yield* notFound("getIssue"); + // Editing replaces these sets. Finish their pages before handing them to a client. + let labels = raw.labels; + let assignees = raw.assignees; + const seen = new Set(); + const allLabels = [...detail.labels]; + const allAssignees = [...detail.assignees]; + while (labels?.pageInfo.hasNextPage === true || assignees.pageInfo.hasNextPage) { + const labelsCursor = labels?.pageInfo.hasNextPage ? labels.pageInfo.endCursor : null; + const assigneesCursor = assignees.pageInfo.hasNextPage + ? assignees.pageInfo.endCursor + : null; + const key = `${labelsCursor ?? ""} ${assigneesCursor ?? ""}`; + if ( + (labels?.pageInfo.hasNextPage && labelsCursor === null) || + (assignees.pageInfo.hasNextPage && assigneesCursor === null) || + seen.has(key) + ) { + return yield* invalid( + "getIssue", + "GitHub did not return complete issue labels or assignees.", + ); + } + seen.add(key); + const page = yield* client + .IssueCollections({ owner, name, number, labelsCursor, assigneesCursor }) + .pipe(graphql(input.host, "getIssue")); + const next = page.repository?.issue; + if (next == null) return yield* notFound("getIssue"); + if (labels?.pageInfo.hasNextPage) { + labels = next.labels; + if (next.labels === null) { + return yield* invalid("getIssue", "GitHub did not return complete issue labels."); + } + allLabels.push( + ...(next.labels.nodes ?? []).flatMap((node) => + node === null ? [] : [{ name: node.name, color: node.color }], + ), + ); + } + if (assignees.pageInfo.hasNextPage) { + assignees = next.assignees; + allAssignees.push( + ...(next.assignees.nodes ?? []).flatMap((node) => + node === null + ? [] + : [{ login: node.login, name: node.name, avatarUrl: node.avatarUrl }], + ), + ); + } + } + return { ...detail, labels: allLabels, assignees: allAssignees }; + }), + + getComments: (input) => + issue(input, "getComments").pipe( + Effect.flatMap(({ owner, name, number }) => + client + .IssueComments({ owner, name, number, cursor: input.cursor }) + .pipe(graphql(input.host, "getComments")), + ), + Effect.flatMap((result) => + result.repository?.issue == null + ? Effect.fail(notFound("getComments")) + : Effect.succeed(toIssueComments(result)), + ), + ), + + create: (input) => + repository(input.scope, "create").pipe( + Effect.flatMap(({ owner, name }) => + api + .rest({ + host: input.host, + operation: "create", + method: "POST", + path: `repos/${owner}/${name}/issues`, + body: { + title: input.title, + body: input.body, + labels: input.labels, + assignees: input.assignees, + }, + }) + .pipe(Effect.mapError(fail("create"))), + ), + Effect.flatMap((response) => { + const decoded = decodeCreatedIssueJson(response.body); + return Result.isSuccess(decoded) + ? Effect.succeed({ + key: String(decoded.success.number), + displayKey: `#${decoded.success.number}`, + url: decoded.success.url, + }) + : Effect.fail( + new IssueTrackerError({ + tracker: KIND, + operation: "create", + reason: "failed", + detail: "GitHub returned an unreadable create response.", + cause: decoded.failure, + }), + ); + }), + ), + + comment: (input) => + verifiedIssue(input, "comment").pipe( + Effect.flatMap(({ path }) => + api + .rest({ + host: input.host, + operation: "comment", + method: "POST", + path: `${path}/comments`, + body: { body: input.body }, + }) + .pipe(Effect.asVoid, Effect.mapError(fail("comment"))), + ), + ), + + update: (input) => + patchIssue(input, "update", { + ...(input.title === undefined ? {} : { title: input.title }), + ...(input.body === undefined ? {} : { body: input.body }), + ...(input.labels === undefined ? {} : { labels: input.labels }), + ...(input.assignees === undefined ? {} : { assignees: input.assignees }), + }), + + setState: Effect.fn("GitHubIssueTracker.setState")(function* (input) { + const { path, state, stateReason } = yield* verifiedIssue(input, "setState"); + const reason = + input.state === "closed" && input.closeReason !== undefined + ? CLOSE_REASON[input.closeReason] + : undefined; + // GitHub ignores `state_reason` unless the state changes, and would answer with success. + if (reason !== undefined && state === "CLOSED" && stateReason?.toLowerCase() !== reason) { + return yield* invalid( + "setState", + "GitHub cannot change why a closed issue was closed. Reopen it, then close it again.", + ); + } + yield* api + .rest({ + host: input.host, + operation: "setState", + method: "PATCH", + path, + body: { state: input.state, ...(reason === undefined ? {} : { state_reason: reason }) }, + }) + .pipe(Effect.mapError(fail("setState"))); + }), + + setReaction: Effect.fn("GitHubIssueTracker.setReaction")(function* (input) { + let subjectId: string; + if (input.subjectId === undefined) { + const target = yield* verifiedIssue(input, "setReaction"); + subjectId = target.id; + } else { + const { owner, name, number } = yield* issue(input, "setReaction"); + // Node ids are global: prove the comment belongs to this issue before writing. + const result = yield* client + .IssueCommentScope({ owner, name, number, commentId: input.subjectId }) + .pipe(graphql(input.host, "setReaction")); + const expected = result.repository?.issue?.id; + if ( + expected === undefined || + result.node?.__typename !== "IssueComment" || + result.node.issue.id !== expected + ) { + return yield* invalid( + "setReaction", + "The comment does not belong to the selected issue.", + ); + } + subjectId = input.subjectId; + } + const variables = { subjectId, content: gitHubReactionContent(input.content) }; + yield* ( + input.reacted + ? client.AddReaction(variables).pipe(Effect.asVoid) + : client.RemoveReaction(variables).pipe(Effect.asVoid) + ).pipe(graphql(input.host, "setReaction")); + }), + + listTemplates: (input) => + repository(input.scope, "listTemplates").pipe( + Effect.flatMap(({ owner, name }) => + client.IssueTemplates({ owner, name }).pipe(graphql(input.host, "listTemplates")), + ), + Effect.flatMap(({ repository }) => + repository === null + ? Effect.fail(notFound("listTemplates")) + : readIssueTemplates(repository), + ), + ), + + listCandidates: (input) => + repository(input.scope, "listCandidates").pipe( + Effect.flatMap(({ owner, name }) => + client.IssueCandidates({ owner, name }).pipe(graphql(input.host, "listCandidates")), + ), + Effect.flatMap(({ repository }) => + repository === null + ? Effect.fail(notFound("listCandidates")) + : Effect.succeed(toIssueCandidates(repository)), + ), + ), + } satisfies IssueTrackerApi; +}); diff --git a/packages/source-control-github/src/server/driver.ts b/packages/source-control-github/src/server/driver.ts index 2fb2dfb2899c7..05844f6608fe6 100644 --- a/packages/source-control-github/src/server/driver.ts +++ b/packages/source-control-github/src/server/driver.ts @@ -17,6 +17,7 @@ import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts"; import * as GitHubPullRequestProvider from "./GitHubPullRequestProvider.ts"; import * as GitHubRest from "./GitHubRest.ts"; import * as GitHubHttpClient from "./GitHubHttpClient.ts"; +import * as GitHubIssueTracker from "./GitHubIssueTracker.ts"; import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts"; // `GitHubRest.layerWithDependencies` carries the quota reserve every GitHub reader shares, so it @@ -104,6 +105,7 @@ const makeInstance = Effect.gen(function* () { sourceControl: GitHubSourceControlProvider.make, discovery: GitHubSourceControlProvider.makeDiscovery, pullRequests: GitHubPullRequestProvider.make, + issues: GitHubIssueTracker.make, fetchMedia: GitHubMediaFetch.make.pipe( Effect.provideService(HttpClient.HttpClient, mediaHttpClient), ), diff --git a/packages/source-control-github/src/server/gitHubIssueJson.ts b/packages/source-control-github/src/server/gitHubIssueJson.ts new file mode 100644 index 0000000000000..a41fe34fb08fc --- /dev/null +++ b/packages/source-control-github/src/server/gitHubIssueJson.ts @@ -0,0 +1,350 @@ +import * as Result from "effect/Result"; +import * as Schema from "effect/Schema"; +import { TrimmedNonEmptyString } from "@t3tools/contracts"; +import type { + IssueActor, + IssueComment, + IssueEvent, + IssueEventKind, + IssueLabel, + IssueLinkedPullRequest, + IssueState, + IssueTemplate, + IssueViewerPermissions, + PullRequestState, +} from "@t3tools/contracts"; +import { decodeJsonResult } from "@t3tools/shared/schemaJson"; +import type { TrackerIssueSummary } from "@t3tools/source-control-core/server/IssueTracker"; + +import type * as Issues from "./issues.graphql.ts"; +import { toReactions } from "./gitHubPullRequestJson.ts"; + +type DetailIssue = NonNullable["issue"]>; +type Actor = { + readonly login: string; + readonly name?: string | null; + readonly avatarUrl: string | null; +}; +type Label = NonNullable["nodes"]>[number]>; +type TimelineItem = NonNullable[number]>; +type Comments = Omit; +type Reference = NonNullable< + NonNullable["nodes"]>[number] +>; + +function trimmed(value: string | null | undefined): string | null { + const text = value?.trim() ?? ""; + return text.length > 0 ? text : null; +} + +function toActor(raw: Actor | null | undefined): IssueActor | null { + const login = trimmed(raw?.login); + return login === null + ? null + : { + login, + name: trimmed(raw != null && "name" in raw ? raw.name : null), + avatarUrl: trimmed(raw?.avatarUrl), + }; +} + +function toActors(raw: ReadonlyArray | null | undefined): ReadonlyArray { + return (raw ?? []).flatMap((entry) => toActor(entry) ?? []); +} + +function toLabels(raw: ReadonlyArray