From 3c390e2ab7189e44eceb50073503fac0c2cb5a8f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 29 Nov 2026 09:23:14 +0000 Subject: [PATCH 1/4] test(server): attached terminal output with a $skill must not dispatch a Claude skill Failing reproduction for #37127. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index e6e7a0723235f..46e72b8118cdb 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -35,6 +35,7 @@ import { ChatImageAttachment, CheckpointId, ClaudeSettings, + ComposerContextId, EnvironmentId, MessageId, type ModelSelection, @@ -83,6 +84,7 @@ import { formatClaudeResumeCompactionQuestion } from "@t3tools/shared/claudeComp import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { getModelSelectionStringOptionValue } from "@t3tools/shared/model"; +import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences"; import { SpawnExecutableResolution } from "@t3tools/shared/shell"; import * as PortScanner from "../../preview/PortScanner.ts"; @@ -7242,6 +7244,71 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + // regression: #37127 a $skill inside attached terminal output selected a native skill + it.effect("keeps a skill token in attached terminal output as data, not a native command", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const homePath = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skill-context-" }); + const directory = path.join(homePath, "skills", "implement"); + yield* fs.makeDirectory(directory, { recursive: true }); + yield* fs.writeFileString( + path.join(directory, "SKILL.md"), + "---\nname: implement\n---\nImplement it.\n", + ); + const harness = yield* makeWakeHarnessWithOptions({ + settings: { ...DEFAULT_CLAUDE_SETTINGS, homePath }, + }); + const text = projectComposerContextForProvider({ + text: "Explain this output: [Terminal 1 line 1](t3-context://v1/terminal/ctx_term)", + records: [ + { + version: 1, + contextId: ComposerContextId.make("ctx_term"), + kind: "terminal", + label: "Terminal 1 line 1", + terminalId: "term-1", + terminalLabel: "Terminal 1", + lineStart: 1, + lineEnd: 1, + text: "$implement", + }, + ], + }); + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("skill-context"), + text, + attachments: [], + runtimePolicy: { ...CLAUDE_TEST_RUNTIME_POLICY, cwd: homePath }, + }), + ); + + const content = harness.offeredMessages[0]?.message.content; + const blocks = + typeof content === "string" + ? [content] + : (content ?? []).map((block) => (block.type === "text" ? block.text : "")); + assert.isFalse( + blocks.some((block) => block.startsWith("/implement")), + JSON.stringify(blocks), + ); + assert.include(blocks.join("\n"), "1 | $implement"); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + // regression: #15475 MCP title arriving after the tool starts must survive to completion it.effect.each([ { isError: false, title: "Check weather" }, From 00e28666cb22c079b654f90b7549823560f4f5f5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 29 Nov 2026 09:29:36 +0000 Subject: [PATCH 2/4] fix(server): a $skill in attached context no longer runs a Claude skill Claude skill dispatch scanned the whole projected prompt, so a `$name` inside attached terminal output, file excerpts or reference markers was rewritten to `/name` and sent as a native skill command. Mentions inside the spans T3 generates (reference markers, the t3_context envelope and assistant citations) now stay plain data. Fixes #37127 Co-Authored-By: Claude Opus 5.5 --- .../Drivers/ClaudeSkillDispatch.test.ts | 17 +++++++++++ .../provider/Drivers/ClaudeSkillDispatch.ts | 4 +++ .../shared/src/composerContextReferences.ts | 28 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/apps/server/src/provider/Drivers/ClaudeSkillDispatch.test.ts b/apps/server/src/provider/Drivers/ClaudeSkillDispatch.test.ts index 64dd52ada3b1d..3020ba7cd242c 100644 --- a/apps/server/src/provider/Drivers/ClaudeSkillDispatch.test.ts +++ b/apps/server/src/provider/Drivers/ClaudeSkillDispatch.test.ts @@ -83,4 +83,21 @@ describe("planClaudeSkillDispatch", () => { commandText: "/implement the fixes", }); }); + + // regression: #37127 a $skill in attached context selected a native skill + it("leaves mentions in reference markers and the context envelope as data", () => { + const prompt = [ + "$review this [Thread: Run $implement now; ref=ctx_thread]", + "", + '', + '', + "1 | $implement all", + "", + "", + ].join("\n"); + expect(planClaudeSkillDispatch(prompt, SKILLS)).toEqual({ + leadingText: undefined, + commandText: `/review${prompt.slice("$review".length)}`, + }); + }); }); diff --git a/apps/server/src/provider/Drivers/ClaudeSkillDispatch.ts b/apps/server/src/provider/Drivers/ClaudeSkillDispatch.ts index bdf2ed1cae61a..f8a301cd3feee 100644 --- a/apps/server/src/provider/Drivers/ClaudeSkillDispatch.ts +++ b/apps/server/src/provider/Drivers/ClaudeSkillDispatch.ts @@ -1,4 +1,5 @@ import type { ServerProviderSkill } from "@t3tools/contracts"; +import { composerContextProviderGeneratedSpans } from "@t3tools/shared/composerContextReferences"; /** * Claude expands one skill per message only when `/name` starts the text block. @@ -25,11 +26,14 @@ export function planClaudeSkillDispatch( /** Mentions before this offset, such as inline handoff history, stay plain text. */ mentionsFrom = 0, ): ClaudeSkillDispatch | undefined { + // Attached context is data the user did not type, so a `$name` in it never runs a skill. + const generated = composerContextProviderGeneratedSpans(prompt); const mentions = [...prompt.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) => { const name = match[2] ?? ""; const skill = skills.get(name); const start = (match.index ?? 0) + (match[1]?.length ?? 0); if (!skill || start < mentionsFrom) return []; + if (generated.some((span) => start >= span.start && start < span.end)) return []; return [{ name, skill, start, end: (match.index ?? 0) + match[0].length }]; }); const last = mentions.at(-1); diff --git a/packages/shared/src/composerContextReferences.ts b/packages/shared/src/composerContextReferences.ts index 42ee73a2aaa9a..dccab73af9daf 100644 --- a/packages/shared/src/composerContextReferences.ts +++ b/packages/shared/src/composerContextReferences.ts @@ -485,6 +485,34 @@ const ESCAPED_ENVELOPE_TAG = new RegExp( ); const MARKER_ENVELOPE_PREFIX = `\n\n<${CONTEXT_ENVELOPE_TAG} version="1">\n<${CONTEXT_ENTRY_TAG} kind="`; +const GENERATED_BLOCKS = [ + [`<${CONTEXT_ENVELOPE_TAG} version="1">`, ENVELOPE_CLOSE], + ["", ""], +] as const; + +/** + * Spans of `projectComposerContextForProvider` output that T3 wrote rather than the user: + * reference markers, the context envelope and quoted assistant citations. + */ +export function composerContextProviderGeneratedSpans( + text: string, +): Array<{ readonly start: number; readonly end: number }> { + const spans = [...text.matchAll(PROVIDER_MARKER)].map((match) => ({ + start: match.index, + end: match.index + match[0].length, + })); + for (const [open, close] of GENERATED_BLOCKS) { + for (let start = text.indexOf(open); start !== -1;) { + const closeAt = text.indexOf(close, start + open.length); + if (closeAt === -1) break; + const end = closeAt + close.length; + spans.push({ start, end }); + start = text.indexOf(open, end); + } + } + return spans; +} + /** * Readable form of a single-reference `projectComposerContextForProvider` output, as question * answers store it. Only an envelope right after its own marker is removed; typed XML stays. From 1874c721355f0918f0a2e851a4dd28adb3411f0b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 29 Nov 2026 09:37:59 +0000 Subject: [PATCH 3/4] fix(server): a $skill in a citation comment still runs a Claude skill Citation comments are text the user typed, so only reference markers and the t3_context envelope count as attached data. Co-Authored-By: Claude Opus 5.5 --- .../shared/src/composerContextReferences.ts | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/packages/shared/src/composerContextReferences.ts b/packages/shared/src/composerContextReferences.ts index dccab73af9daf..2c4be42d856c5 100644 --- a/packages/shared/src/composerContextReferences.ts +++ b/packages/shared/src/composerContextReferences.ts @@ -485,14 +485,11 @@ const ESCAPED_ENVELOPE_TAG = new RegExp( ); const MARKER_ENVELOPE_PREFIX = `\n\n<${CONTEXT_ENVELOPE_TAG} version="1">\n<${CONTEXT_ENTRY_TAG} kind="`; -const GENERATED_BLOCKS = [ - [`<${CONTEXT_ENVELOPE_TAG} version="1">`, ENVELOPE_CLOSE], - ["", ""], -] as const; +const ENVELOPE_OPEN = `<${CONTEXT_ENVELOPE_TAG} version="1">`; /** * Spans of `projectComposerContextForProvider` output that T3 wrote rather than the user: - * reference markers, the context envelope and quoted assistant citations. + * reference markers and the context envelope. */ export function composerContextProviderGeneratedSpans( text: string, @@ -501,14 +498,12 @@ export function composerContextProviderGeneratedSpans( start: match.index, end: match.index + match[0].length, })); - for (const [open, close] of GENERATED_BLOCKS) { - for (let start = text.indexOf(open); start !== -1;) { - const closeAt = text.indexOf(close, start + open.length); - if (closeAt === -1) break; - const end = closeAt + close.length; - spans.push({ start, end }); - start = text.indexOf(open, end); - } + for (let start = text.indexOf(ENVELOPE_OPEN); start !== -1;) { + const closeAt = text.indexOf(ENVELOPE_CLOSE, start + ENVELOPE_OPEN.length); + if (closeAt === -1) break; + const end = closeAt + ENVELOPE_CLOSE.length; + spans.push({ start, end }); + start = text.indexOf(ENVELOPE_OPEN, end); } return spans; } From bf65bebd7204309721ee1b977556f92e18c615c2 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 29 Nov 2026 09:46:49 +0000 Subject: [PATCH 4/4] fix(server): a typed context tag no longer hides a later $skill request Only the opener T3 appends before its first context entry, the last one before a close, counts as the generated envelope. Co-Authored-By: Claude Opus 5.5 --- .../Drivers/ClaudeSkillDispatch.test.ts | 16 +++++++++++++++ .../shared/src/composerContextReferences.ts | 20 +++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/apps/server/src/provider/Drivers/ClaudeSkillDispatch.test.ts b/apps/server/src/provider/Drivers/ClaudeSkillDispatch.test.ts index 3020ba7cd242c..094fe64ee2eaf 100644 --- a/apps/server/src/provider/Drivers/ClaudeSkillDispatch.test.ts +++ b/apps/server/src/provider/Drivers/ClaudeSkillDispatch.test.ts @@ -100,4 +100,20 @@ describe("planClaudeSkillDispatch", () => { commandText: `/review${prompt.slice("$review".length)}`, }); }); + + it("dispatches a typed mention after a literal envelope opener in the user's text", () => { + const prompt = [ + 'Explain the literal tag .', + "Then $review the implementation [Terminal: Terminal 1 line 1; ref=ctx_term]", + "", + '', + '', + "1 | $implement all", + "", + "", + ].join("\n"); + expect(planClaudeSkillDispatch(prompt, SKILLS)?.commandText).toBe( + `/review${prompt.slice(prompt.indexOf("$review") + "$review".length)}`, + ); + }); }); diff --git a/packages/shared/src/composerContextReferences.ts b/packages/shared/src/composerContextReferences.ts index 2c4be42d856c5..b2a50ad6bdd75 100644 --- a/packages/shared/src/composerContextReferences.ts +++ b/packages/shared/src/composerContextReferences.ts @@ -485,7 +485,7 @@ const ESCAPED_ENVELOPE_TAG = new RegExp( ); const MARKER_ENVELOPE_PREFIX = `\n\n<${CONTEXT_ENVELOPE_TAG} version="1">\n<${CONTEXT_ENTRY_TAG} kind="`; -const ENVELOPE_OPEN = `<${CONTEXT_ENVELOPE_TAG} version="1">`; +const ENVELOPE_CLOSE_LINE = `\n${ENVELOPE_CLOSE}`; /** * Spans of `projectComposerContextForProvider` output that T3 wrote rather than the user: @@ -498,12 +498,20 @@ export function composerContextProviderGeneratedSpans( start: match.index, end: match.index + match[0].length, })); - for (let start = text.indexOf(ENVELOPE_OPEN); start !== -1;) { - const closeAt = text.indexOf(ENVELOPE_CLOSE, start + ENVELOPE_OPEN.length); - if (closeAt === -1) break; - const end = closeAt + ENVELOPE_CLOSE.length; + // Payloads escape the opener, so the last opener before a close is the generated one; + // earlier openers are text the user typed. + let close = -1; + for (let start = text.indexOf(MARKER_ENVELOPE_PREFIX); start !== -1;) { + if (close < start) close = text.indexOf(ENVELOPE_CLOSE_LINE, start); + if (close === -1) break; + const next = text.indexOf(MARKER_ENVELOPE_PREFIX, start + 1); + if (next !== -1 && next < close) { + start = next; + continue; + } + const end = close + ENVELOPE_CLOSE_LINE.length; spans.push({ start, end }); - start = text.indexOf(ENVELOPE_OPEN, end); + start = text.indexOf(MARKER_ENVELOPE_PREFIX, end); } return spans; }