Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
ChatImageAttachment,
CheckpointId,
ClaudeSettings,
ComposerContextId,
EnvironmentId,
MessageId,
type ModelSelection,
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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" },
Expand Down
33 changes: 33 additions & 0 deletions apps/server/src/provider/Drivers/ClaudeSkillDispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,37 @@ 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]",
"",
'<t3_context version="1">',
'<context kind="terminal" id="ctx_term">',
"1 | $implement all",
"</context>",
"</t3_context>",
].join("\n");
expect(planClaudeSkillDispatch(prompt, SKILLS)).toEqual({
leadingText: undefined,
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 <t3_context version="1">.',
"Then $review the implementation [Terminal: Terminal 1 line 1; ref=ctx_term]",
"",
'<t3_context version="1">',
'<context kind="terminal" id="ctx_term">',
"1 | $implement all",
"</context>",
"</t3_context>",
].join("\n");
expect(planClaudeSkillDispatch(prompt, SKILLS)?.commandText).toBe(
`/review${prompt.slice(prompt.indexOf("$review") + "$review".length)}`,
);
});
});
4 changes: 4 additions & 0 deletions apps/server/src/provider/Drivers/ClaudeSkillDispatch.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions packages/shared/src/composerContextReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,37 @@ const ESCAPED_ENVELOPE_TAG = new RegExp(
);
const MARKER_ENVELOPE_PREFIX = `\n\n<${CONTEXT_ENVELOPE_TAG} version="1">\n<${CONTEXT_ENTRY_TAG} kind="`;

const ENVELOPE_CLOSE_LINE = `\n${ENVELOPE_CLOSE}`;

/**
* Spans of `projectComposerContextForProvider` output that T3 wrote rather than the user:
* reference markers and the context envelope.
*/
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,
}));
// 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(MARKER_ENVELOPE_PREFIX, 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.
Expand Down
Loading