|
1 | 1 | import { describe, expect, test } from "bun:test"; |
| 2 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
2 | 5 | import { DIRECTOR_REGISTRY } from "./directors/registry.js"; |
3 | 6 | import { DIRECTOR_IDS, type DirectorId } from "./directors/types.js"; |
4 | 7 | import { |
| 8 | + assembleDirectorPrompt, |
5 | 9 | canonicalToolNamesForDirector, |
6 | 10 | directorPromptSizeTable, |
7 | 11 | formatPromptSizeTable, |
| 12 | + formatSkywalkerPrefixTable, |
| 13 | + assembleSkywalkerInferEnvelope, |
| 14 | + measureSkywalkerPrefix, |
8 | 15 | type PromptSizeFamily, |
9 | 16 | } from "./prompt-sizes.js"; |
| 17 | +import { |
| 18 | + advertisedToolNamesForSessionMode, |
| 19 | + CATALOG_TOOL_NAMES, |
| 20 | + CORE_TOOL_NAMES, |
| 21 | +} from "./tool-search.js"; |
| 22 | +import { MAX_AGENTS_MD_BYTES } from "./context-extensions.js"; |
| 23 | +import { loadSessionChatPrompt } from "../session/runtime-assembly.js"; |
| 24 | +import { createAdvertisedToolset } from "../session/assemble-runtime.js"; |
| 25 | +import { resolveExecDirectorOverlay } from "../exec/runner.js"; |
10 | 26 |
|
11 | 27 | /** |
12 | 28 | * Prompt size budget (CL-7664). Numeric asserts only — copy edits must not |
@@ -208,3 +224,105 @@ describe("director prompt size budget", () => { |
208 | 224 | } |
209 | 225 | }); |
210 | 226 | }); |
| 227 | + |
| 228 | +describe("skywalker grok prefix (infer envelope vs trimmed director)", () => { |
| 229 | + test("keeps AGENTS.md and core tools on the infer envelope", () => { |
| 230 | + const prompt = assembleSkywalkerInferEnvelope(); |
| 231 | + expect(prompt).toContain("## Project guidance (AGENTS.md, reference)"); |
| 232 | + expect(prompt).toContain("Follow the repository conventions."); |
| 233 | + for (const name of CORE_TOOL_NAMES) { |
| 234 | + if (name === "wait_agents") continue; |
| 235 | + expect(prompt, name).toContain(`- ${name}:`); |
| 236 | + } |
| 237 | + }); |
| 238 | + |
| 239 | + test("does not substitute the trimmed director prompt on grok", () => { |
| 240 | + const size = measureSkywalkerPrefix(); |
| 241 | + expect(size.agentsMdCap).toBe(MAX_AGENTS_MD_BYTES); |
| 242 | + expect(size.inferEnvelopeChars).toBeGreaterThan(5000); |
| 243 | + expect(size.trimmedDirectorChars).toBeGreaterThan(5000); |
| 244 | + expect(size.inferEnvelopeBytes).toBeGreaterThanOrEqual( |
| 245 | + size.inferEnvelopeChars, |
| 246 | + ); |
| 247 | + expect(size.inferEnvelopeChars).not.toBe(size.trimmedDirectorChars); |
| 248 | + const infer = assembleSkywalkerInferEnvelope(); |
| 249 | + expect(infer).not.toContain("Finish bias (xAI / Grok worker):"); |
| 250 | + }); |
| 251 | + |
| 252 | + test("formatSkywalkerPrefixTable reports both prefixes and the AGENTS.md cap", () => { |
| 253 | + const size = measureSkywalkerPrefix(); |
| 254 | + const table = formatSkywalkerPrefixTable(size); |
| 255 | + expect(table).toContain( |
| 256 | + `| skywalker infer envelope (canonical AGENTS.md) | ${size.inferEnvelopeChars} (${size.inferEnvelopeBytes}) |`, |
| 257 | + ); |
| 258 | + expect(table).toContain( |
| 259 | + `| skywalker trimmed director (grok) | ${size.trimmedDirectorChars} (${size.trimmedDirectorBytes}) |`, |
| 260 | + ); |
| 261 | + expect(table).toContain(`| live AGENTS.md cap | ${size.agentsMdCap} |`); |
| 262 | + }); |
| 263 | + |
| 264 | + // Production pin: a Grok fork at the runner that swapped loadSessionChatPrompt |
| 265 | + // or advertisedToolNamesForSessionMode for the trimmed director would fail here, |
| 266 | + // not only the fixture size inequality above. |
| 267 | + test("grok primary uses loadSessionChatPrompt and CORE+CATALOG, not the trimmed director", async () => { |
| 268 | + const cwd = await mkdtemp(join(tmpdir(), "grok-primary-prefix-")); |
| 269 | + try { |
| 270 | + const agentsBody = "GROK_PRIMARY_KEEPS_AGENTS_MD\n"; |
| 271 | + await writeFile(join(cwd, "AGENTS.md"), agentsBody); |
| 272 | + const availability = { |
| 273 | + languageServerAvailable: true, |
| 274 | + operatorAvailable: true, |
| 275 | + waitAgentsMounted: true, |
| 276 | + } as const; |
| 277 | + |
| 278 | + const { systemPrompt } = await loadSessionChatPrompt({ |
| 279 | + cwd, |
| 280 | + skillDirs: [], |
| 281 | + sessionMode: "orchestrator", |
| 282 | + toolAvailability: availability, |
| 283 | + skills: [], |
| 284 | + }); |
| 285 | + expect(systemPrompt).toContain( |
| 286 | + "## Project guidance (AGENTS.md, reference)", |
| 287 | + ); |
| 288 | + expect(systemPrompt).toContain(agentsBody.trim()); |
| 289 | + for (const name of CORE_TOOL_NAMES) { |
| 290 | + expect(systemPrompt, name).toContain(`- ${name}:`); |
| 291 | + } |
| 292 | + |
| 293 | + const trimmed = assembleDirectorPrompt("skywalker", "grok"); |
| 294 | + expect(trimmed).not.toContain( |
| 295 | + "## Project guidance (AGENTS.md, reference)", |
| 296 | + ); |
| 297 | + expect(trimmed).not.toContain(agentsBody.trim()); |
| 298 | + |
| 299 | + const advertised = advertisedToolNamesForSessionMode( |
| 300 | + "orchestrator", |
| 301 | + availability, |
| 302 | + ); |
| 303 | + expect(advertised).toEqual([...CORE_TOOL_NAMES, ...CATALOG_TOOL_NAMES]); |
| 304 | + const trimmedTools = canonicalToolNamesForDirector( |
| 305 | + DIRECTOR_REGISTRY.skywalker, |
| 306 | + "grok", |
| 307 | + ); |
| 308 | + expect(trimmedTools).not.toContain("list_dir"); |
| 309 | + expect(trimmedTools).not.toContain("tool_search"); |
| 310 | + expect(trimmedTools).not.toContain("skill_search"); |
| 311 | + |
| 312 | + const overlay = resolveExecDirectorOverlay("skywalker"); |
| 313 | + expect(overlay.systemPrompt).toBeUndefined(); |
| 314 | + expect(overlay.advertisedAllow).toBeUndefined(); |
| 315 | + const { isAdvertised } = createAdvertisedToolset({ |
| 316 | + sessionMode: "orchestrator", |
| 317 | + toolAvailability: availability, |
| 318 | + getProvider: () => ({ providerName: "xai/default", model: "grok-4.6" }), |
| 319 | + builtInPrefix: overlay.advertisedAllow, |
| 320 | + }); |
| 321 | + for (const name of advertised) { |
| 322 | + expect(isAdvertised(name), name).toBe(true); |
| 323 | + } |
| 324 | + } finally { |
| 325 | + await rm(cwd, { recursive: true, force: true }); |
| 326 | + } |
| 327 | + }); |
| 328 | +}); |
0 commit comments