|
| 1 | +const vscode = require("vscode") |
| 2 | +const fs = require("fs") |
| 3 | +const path = require("path") |
| 4 | + |
| 5 | +// Absolute: the extension host's cwd is not the repo, so a relative path would scatter output. |
| 6 | +const OUT_DIR = process.env.LM_PROBE_OUT_DIR || "c:\\git\\<repo>\\.tmp\\transcripts" |
| 7 | + |
| 8 | +function write(name, data) { |
| 9 | + fs.mkdirSync(OUT_DIR, { recursive: true }) |
| 10 | + fs.writeFileSync(path.join(OUT_DIR, name), typeof data === "string" ? data : JSON.stringify(data, null, 2)) |
| 11 | +} |
| 12 | + |
| 13 | +const READ_TOOL = { |
| 14 | + name: "read_file", |
| 15 | + description: "Read the contents of a file at the given path.", |
| 16 | + inputSchema: { |
| 17 | + type: "object", |
| 18 | + properties: { path: { type: "string", description: "File path to read" } }, |
| 19 | + required: ["path"], |
| 20 | + }, |
| 21 | +} |
| 22 | + |
| 23 | +const TOOL_SYSTEM_PROMPT = [ |
| 24 | + "You are Zoo Code, an autonomous coding agent.", |
| 25 | + "You accomplish tasks by calling the tools provided to you.", |
| 26 | + "You MUST call exactly one tool per message. Never ask the user a question.", |
| 27 | + "Do not answer from memory; always read the file first using the read_file tool.", |
| 28 | +].join("\n") |
| 29 | + |
| 30 | +async function runOnce(model, scenario) { |
| 31 | + const record = { |
| 32 | + scenario: scenario.name, |
| 33 | + modelId: model.id, |
| 34 | + modelFamily: model.family, |
| 35 | + modelVendor: model.vendor, |
| 36 | + modelVersion: model.version, |
| 37 | + maxInputTokens: model.maxInputTokens, |
| 38 | + parts: [], |
| 39 | + concatenatedText: "", |
| 40 | + toolCallParts: [], |
| 41 | + error: null, |
| 42 | + } |
| 43 | + const messages = [] |
| 44 | + if (scenario.system) { |
| 45 | + messages.push(vscode.LanguageModelChatMessage.Assistant(scenario.system)) |
| 46 | + } |
| 47 | + for (const userText of scenario.userMessages) { |
| 48 | + messages.push(vscode.LanguageModelChatMessage.User(userText)) |
| 49 | + } |
| 50 | + const options = { justification: "Empirical probe of leaked tool-call formatting." } |
| 51 | + if (scenario.tools) { |
| 52 | + options.tools = [READ_TOOL] |
| 53 | + } |
| 54 | + try { |
| 55 | + const source = new vscode.CancellationTokenSource() |
| 56 | + const response = await model.sendRequest(messages, options, source.token) |
| 57 | + for await (const chunk of response.stream) { |
| 58 | + const typeName = chunk && chunk.constructor ? chunk.constructor.name : typeof chunk |
| 59 | + if (chunk instanceof vscode.LanguageModelTextPart) { |
| 60 | + record.parts.push({ type: typeName, value: chunk.value }) |
| 61 | + record.concatenatedText += chunk.value |
| 62 | + } else if (chunk instanceof vscode.LanguageModelToolCallPart) { |
| 63 | + const call = { type: typeName, name: chunk.name, callId: chunk.callId, input: chunk.input } |
| 64 | + record.parts.push(call) |
| 65 | + record.toolCallParts.push(call) |
| 66 | + } else { |
| 67 | + record.parts.push({ type: typeName, raw: String(chunk) }) |
| 68 | + } |
| 69 | + } |
| 70 | + } catch (error) { |
| 71 | + record.error = { name: error && error.name, message: error && error.message, stack: error && error.stack } |
| 72 | + } |
| 73 | + |
| 74 | + const text = record.concatenatedText |
| 75 | + record.markers = { |
| 76 | + hasInvoke: /<(?:antml:)?invoke\b/i.test(text), |
| 77 | + hasFunctionCalls: /<(?:antml:)?function_calls\b/i.test(text), |
| 78 | + hasAntmlNamespace: /antml:/i.test(text), |
| 79 | + invokeWrappedInFunctionCalls: /<(?:antml:)?function_calls\s*>[\s\S]*?<(?:antml:)?invoke\b/i.test(text), |
| 80 | + bareInvokeWithoutWrapper: /<(?:antml:)?invoke\b/i.test(text) && !/<(?:antml:)?function_calls\b/i.test(text), |
| 81 | + insideFencedCodeBlock: /```[\s\S]*?<(?:antml:)?invoke\b/i.test(text), |
| 82 | + } |
| 83 | + return record |
| 84 | +} |
| 85 | + |
| 86 | +function buildScenarios() { |
| 87 | + const longFiller = "This is filler context line used to grow the prompt toward the context window. ".repeat(4000) |
| 88 | + return [ |
| 89 | + { |
| 90 | + name: "A_tools_declared_compelling_prompt", |
| 91 | + tools: true, |
| 92 | + system: TOOL_SYSTEM_PROMPT, |
| 93 | + userMessages: ["Read the file c:/git/Zoo-Code/package.json and tell me the version field."], |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "B_tools_declared_no_system_prompt", |
| 97 | + tools: true, |
| 98 | + userMessages: ["Read the file c:/git/Zoo-Code/package.json and tell me the version field."], |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "C_tools_declared_long_context", |
| 102 | + tools: true, |
| 103 | + system: TOOL_SYSTEM_PROMPT, |
| 104 | + userMessages: [longFiller, "Now read the file c:/git/Zoo-Code/package.json and tell me the version field."], |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "D_no_tools_asked_to_emit_markup", |
| 108 | + tools: false, |
| 109 | + system: TOOL_SYSTEM_PROMPT, |
| 110 | + userMessages: [ |
| 111 | + "You have a tool named read_file that takes a path. Emit the tool invocation using Anthropic's internal function-call XML format, exactly as you would internally, to read c:/git/Zoo-Code/package.json. Output only the markup.", |
| 112 | + ], |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "E_quoted_markup_in_prose_false_positive_check", |
| 116 | + tools: true, |
| 117 | + system: TOOL_SYSTEM_PROMPT, |
| 118 | + userMessages: [ |
| 119 | + 'Do NOT call any tool. Instead, explain in prose what an invoke block looks like, and include a literal example using the tag name "invoke" with a name attribute of read_file and a parameter named path, written as plain text in your answer.', |
| 120 | + ], |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "F_quoted_markup_in_fenced_code_block", |
| 124 | + tools: true, |
| 125 | + system: TOOL_SYSTEM_PROMPT, |
| 126 | + userMessages: [ |
| 127 | + "Do NOT call any tool. Show me, inside a fenced markdown code block, an example of an invoke block naming the tool read_file with a parameter named path set to /etc/passwd. Only output the fenced code block.", |
| 128 | + ], |
| 129 | + }, |
| 130 | + ] |
| 131 | +} |
| 132 | + |
| 133 | +async function run() { |
| 134 | + const summary = { startedAt: new Date().toISOString(), vscodeVersion: vscode.version, models: [], runs: [] } |
| 135 | + let models = [] |
| 136 | + let selectError = null |
| 137 | + try { |
| 138 | + models = (await vscode.lm.selectChatModels({ vendor: "copilot" })) || [] |
| 139 | + } catch (error) { |
| 140 | + selectError = { name: error && error.name, message: error && error.message } |
| 141 | + } |
| 142 | + summary.selectError = selectError |
| 143 | + summary.allModels = models.map((model) => ({ |
| 144 | + id: model.id, |
| 145 | + family: model.family, |
| 146 | + vendor: model.vendor, |
| 147 | + version: model.version, |
| 148 | + maxInputTokens: model.maxInputTokens, |
| 149 | + })) |
| 150 | + |
| 151 | + const claudeModels = models.filter((model) => /claude/i.test(model.id) || /claude/i.test(model.family)) |
| 152 | + summary.claudeModelIds = claudeModels.map((model) => model.id) |
| 153 | + |
| 154 | + if (claudeModels.length === 0) { |
| 155 | + write("summary.json", summary) |
| 156 | + vscode.window.showErrorMessage( |
| 157 | + `LM Probe: no Claude models. selectChatModels returned ${models.length}. ${selectError ? selectError.message : ""}`, |
| 158 | + ) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + const scenarios = buildScenarios() |
| 163 | + const REPEATS = 5 |
| 164 | + for (const model of claudeModels) { |
| 165 | + for (const scenario of scenarios) { |
| 166 | + for (let iter = 1; iter <= REPEATS; iter++) { |
| 167 | + const record = await runOnce(model, scenario) |
| 168 | + record.iteration = iter |
| 169 | + summary.runs.push({ |
| 170 | + scenario: scenario.name, |
| 171 | + modelId: model.id, |
| 172 | + iteration: iter, |
| 173 | + markers: record.markers, |
| 174 | + toolCallPartCount: record.toolCallParts.length, |
| 175 | + textLength: record.concatenatedText.length, |
| 176 | + error: record.error ? record.error.message : null, |
| 177 | + }) |
| 178 | + write(`${model.id}__${scenario.name}__run${iter}.json`, record) |
| 179 | + write(`${model.id}__${scenario.name}__run${iter}.txt`, record.concatenatedText) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + summary.finishedAt = new Date().toISOString() |
| 185 | + write("summary.json", summary) |
| 186 | + vscode.window.showInformationMessage(`LM Probe complete: ${summary.runs.length} runs written to ${OUT_DIR}`) |
| 187 | +} |
| 188 | + |
| 189 | +function activate(context) { |
| 190 | + // MUST be user-initiated: vscode.lm consent is only granted from a real user gesture, so an |
| 191 | + // activation-time sendRequest is auto-denied with "cannot be used by 'scratch.lmprobe'". |
| 192 | + context.subscriptions.push( |
| 193 | + vscode.commands.registerCommand("lmprobe.run", () => |
| 194 | + run().catch((error) => { |
| 195 | + write("fatal.json", { message: String(error && error.message), stack: String(error && error.stack) }) |
| 196 | + }), |
| 197 | + ), |
| 198 | + ) |
| 199 | + vscode.window.showInformationMessage("LM Probe ready", "Run probe").then((choice) => { |
| 200 | + if (choice === "Run probe") { |
| 201 | + vscode.commands.executeCommand("lmprobe.run") |
| 202 | + } |
| 203 | + }) |
| 204 | +} |
| 205 | + |
| 206 | +module.exports = { activate, deactivate() {} } |
0 commit comments