@causyne/agent 1.20.12 → 1.20.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/LICENSE +1 -1
  2. package/package.json +1 -1
  3. package/src/SmartTools.js +33 -0
  4. package/src/SmartTools.ts +37 -0
  5. package/src/acp/client.js +157 -4
  6. package/src/acp/client.ts +152 -5
  7. package/src/agentLoop.js +857 -178
  8. package/src/agentLoop.ts +865 -183
  9. package/src/browser/browserRuntime.js +8 -1
  10. package/src/browser/browserRuntime.ts +8 -1
  11. package/src/browser/causyneBrowser.js +20 -4
  12. package/src/browser/causyneBrowser.ts +20 -4
  13. package/src/browser/urlPolicy.js +35 -0
  14. package/src/browser/urlPolicy.ts +34 -0
  15. package/src/cliMode.js +6 -2
  16. package/src/commands.js +14 -0
  17. package/src/contextManager.d.ts +1 -0
  18. package/src/core/contextCompactor.js +277 -0
  19. package/src/core/contextCompactor.ts +353 -0
  20. package/src/decideTools.js +157 -0
  21. package/src/decideTools.ts +195 -0
  22. package/src/decisionPolicy.js +5 -3
  23. package/src/decisionPolicy.ts +5 -3
  24. package/src/implementationPlan.js +284 -0
  25. package/src/implementationPlan.ts +275 -0
  26. package/src/observer/coldObserver.js +10 -4
  27. package/src/observer/coldObserver.ts +15 -7
  28. package/src/observer/evidence.js +11 -5
  29. package/src/observer/evidence.ts +13 -6
  30. package/src/observer.js +196 -0
  31. package/src/observer.ts +287 -0
  32. package/src/providers/acp.js +362 -109
  33. package/src/providers/acp.ts +359 -111
  34. package/src/providers/antigravity.js +133 -105
  35. package/src/providers/antigravity.ts +130 -109
  36. package/src/providers/base.d.ts +3 -0
  37. package/src/providers/budgetEnforcer.js +13 -0
  38. package/src/providers/budgetEnforcer.ts +13 -3
  39. package/src/provisioning/capabilityProvisioner.js +14 -10
  40. package/src/sandbox.js +4 -2
  41. package/src/saveTools.js +146 -0
  42. package/src/saveTools.ts +169 -0
  43. package/src/session/cachePressure.js +77 -0
  44. package/src/session/cachePressure.ts +122 -0
  45. package/src/smartToolsUse.js +69 -12
  46. package/src/smartToolsUse.ts +103 -12
  47. package/src/taskShape.js +7 -7
  48. package/src/taskShape.ts +7 -7
  49. package/src/timeMachine.js +55 -20
  50. package/src/timeMachine.ts +64 -20
  51. package/src/toolPacks.js +217 -310
  52. package/src/toolPacks.ts +505 -0
  53. package/src/tools/deferredMcpSearch.js +21 -13
  54. package/src/tools/deferredMcpSearch.ts +23 -15
  55. package/src/tools/updatePlan.js +2 -2
  56. package/src/tools/updatePlan.ts +2 -2
  57. package/src/toolsList.js +203 -0
  58. package/src/toolsList.ts +224 -0
  59. package/src/tui/chatRenderer.js +49 -14
  60. package/src/tui/codeStreamRenderer.js +16 -5
  61. package/src/tui/codeStreamRenderer.ts +14 -4
  62. package/src/tui/prompt.js +4 -2
  63. package/src/tui/sessionSidebar.js +10 -2
  64. package/src/tui/sessionSidebar.ts +10 -3
  65. package/src/tui/stageSurface.js +27 -1
  66. package/src/tui/text.d.ts +1 -0
  67. package/src/tui/turnRail.js +274 -0
  68. package/src/tui/turnRail.ts +334 -0
  69. package/src/types/contracts.ts +103 -0
  70. package/src/verificationPlan.js +141 -0
  71. package/src/verificationPlan.ts +166 -0
  72. package/src/toolPacks.d.ts +0 -10
package/src/toolPacks.js CHANGED
@@ -11,204 +11,170 @@
11
11
  // 3. Auto-unlock recovery — calling a locked-but-real tool unlocks its pack
12
12
  //
13
13
  // Set config.grantAllTools = true to disable deferral entirely.
14
-
15
- // ---------------------------------------------------------------------------
16
- // Pack membership. Names match the `name` field of each tool schema, with dots
17
- // already normalised to underscores (see tools/registry.js TOOL_DEFINITIONS).
18
- // ---------------------------------------------------------------------------
19
-
20
14
  export const CORE_TOOLS = [
21
- 'sysRun',
22
- 'sysRunBg',
23
- 'terminal_persistent_shell',
24
- 'fs_read',
25
- 'fs_write',
26
- 'fs_edit',
27
- 'MultiEdit',
28
- 'fs_list',
29
- 'fs_search',
30
- 'fs_find',
31
- 'fs_move',
32
- 'fs_delete',
33
- 'gitOp',
34
- 'code_search_ripgrep',
35
- 'codebase_search',
36
- 'code_verify_syntax',
37
- 'web_search',
38
- 'web_fetch',
39
- 'update_plan',
40
- 'request_decision',
41
- 'memory_write',
42
- 'fs_read_skill',
43
- 'skill_create',
44
- 'tool_search'
15
+ 'sysRun',
16
+ 'sysRunBg',
17
+ 'terminal_persistent_shell',
18
+ 'fs_read',
19
+ 'fs_write',
20
+ 'fs_edit',
21
+ 'MultiEdit',
22
+ 'fs_list',
23
+ 'fs_search',
24
+ 'fs_find',
25
+ 'fs_move',
26
+ 'fs_delete',
27
+ 'gitOp',
28
+ 'code_search_ripgrep',
29
+ 'codebase_search',
30
+ 'code_verify_syntax',
31
+ 'web_search',
32
+ 'web_fetch',
33
+ 'update_plan',
34
+ 'request_decision',
35
+ 'memory_write',
36
+ 'fs_read_skill',
37
+ 'skill_create',
38
+ 'tool_search'
45
39
  ];
46
-
47
40
  export const PACKS = {
48
- game: [
49
- // Stage 1 of the game cascade calls check_system_specs to verify the
50
- // engine, so it must travel with this pack as well as with media.
51
- 'check_system_specs',
52
- 'godot_act',
53
- 'game_build_html5',
54
- 'game_asset_fetch',
55
- 'game_asset_harvest',
56
- 'game_lore_forge',
57
- 'game_dialogue_tree_gen',
58
- 'game_quest_arc_builder',
59
- 'game_animation_rig',
60
- 'game_ai_behavior_tree',
61
- 'game_cinematic_sequencer',
62
- 'game_audio_synth',
63
- 'game_vfx_emitter_gen',
64
- 'game_shader_forge',
65
- 'game_scene_build',
66
- 'game_entity_spawner',
67
- 'game_tilemap_gen',
68
- 'game_ui_hud_builder',
69
- 'game_economy_balancer',
70
- 'game_data_forge',
71
- 'game_web_runtime_inspector',
72
- 'game_canvas_audit'
73
- ],
74
- web: [
75
- 'browser_act',
76
- 'screenshot',
77
- 'vision_inspect_ui',
78
- 'view_image',
79
- 'astryx_oracle',
80
- 'ui_forge'
81
- ],
82
- media: [
83
- 'generate_media_via_api',
84
- 'execute_procedural_media',
85
- 'check_system_specs',
86
- 'manage_local_dependencies',
87
- 'blender_act',
88
- 'creative_apps',
89
- 'native_engine',
90
- 'resolve_act',
91
- 'palmier_act',
92
- 'document_forge',
93
- 'open_source_tool_provisioner'
94
- ],
95
- system: [
96
- // Channel B of the Three-Channel Doctrine: reading the screen as structured
97
- // text instead of pixels. Lives here rather than in core because most tasks
98
- // never touch a GUI, and tool_search brings it in the moment one does.
99
- 'ax_snapshot',
100
- // Channel C. Ships beside its observer on purpose: acting on a screen you
101
- // have not read is how an agent clicks the wrong button confidently.
102
- 'desktop_act',
103
- 'computer_use',
104
- 'process_list',
105
- 'proc_term',
106
- 'archive',
107
- 'clipboard',
108
- 'desktop_open',
109
- 'printer_print',
110
- 'printer_list_printers',
111
- 'printer_get_status'
112
- ],
113
- advanced: [
114
- 'tool_forge',
115
- 'plugin_manager',
116
- 'document_forge',
117
- 'agent_subagent_orchestrate',
118
- 'peer_agent_delegate',
119
- 'code_search_ast',
120
- 'code_list_symbols',
121
- 'pdf_read'
122
- ]
41
+ game: [
42
+ // Stage 1 of the game cascade calls check_system_specs to verify the
43
+ // engine, so it must travel with this pack as well as with media.
44
+ 'check_system_specs',
45
+ 'godot_act',
46
+ 'game_build_html5',
47
+ 'game_asset_fetch',
48
+ 'game_asset_harvest',
49
+ 'game_lore_forge',
50
+ 'game_dialogue_tree_gen',
51
+ 'game_quest_arc_builder',
52
+ 'game_animation_rig',
53
+ 'game_ai_behavior_tree',
54
+ 'game_cinematic_sequencer',
55
+ 'game_audio_synth',
56
+ 'game_vfx_emitter_gen',
57
+ 'game_shader_forge',
58
+ 'game_scene_build',
59
+ 'game_entity_spawner',
60
+ 'game_tilemap_gen',
61
+ 'game_ui_hud_builder',
62
+ 'game_economy_balancer',
63
+ 'game_data_forge',
64
+ 'game_web_runtime_inspector',
65
+ 'game_canvas_audit'
66
+ ],
67
+ web: [
68
+ 'browser_act',
69
+ 'screenshot',
70
+ 'vision_inspect_ui',
71
+ 'view_image',
72
+ 'astryx_oracle',
73
+ 'ui_forge'
74
+ ],
75
+ media: [
76
+ 'generate_media_via_api',
77
+ 'execute_procedural_media',
78
+ 'check_system_specs',
79
+ 'manage_local_dependencies',
80
+ 'blender_act',
81
+ 'creative_apps',
82
+ 'native_engine',
83
+ 'resolve_act',
84
+ 'palmier_act',
85
+ 'document_forge',
86
+ 'open_source_tool_provisioner'
87
+ ],
88
+ system: [
89
+ // Channel B of the Three-Channel Doctrine: reading the screen as structured
90
+ // text instead of pixels. Lives here rather than in core because most tasks
91
+ // never touch a GUI, and tool_search brings it in the moment one does.
92
+ 'ax_snapshot',
93
+ // Channel C. Ships beside its observer on purpose: acting on a screen you
94
+ // have not read is how an agent clicks the wrong button confidently.
95
+ 'desktop_act',
96
+ 'computer_use',
97
+ 'process_list',
98
+ 'proc_term',
99
+ 'archive',
100
+ 'clipboard',
101
+ 'desktop_open',
102
+ 'printer_print',
103
+ 'printer_list_printers',
104
+ 'printer_get_status'
105
+ ],
106
+ advanced: [
107
+ 'tool_forge',
108
+ 'plugin_manager',
109
+ 'document_forge',
110
+ 'agent_subagent_orchestrate',
111
+ 'peer_agent_delegate',
112
+ 'code_search_ast',
113
+ 'code_list_symbols',
114
+ 'pdf_read'
115
+ ]
123
116
  };
124
-
125
117
  export const PACK_NAMES = Object.keys(PACKS);
126
-
127
118
  /**
128
119
  * The core tools that ship in EVERY request on a provider with no native
129
120
  * schema deferral. The rest of CORE_TOOLS is withheld until the model asks for
130
121
  * it with `tool_search`, exactly as a capability pack is.
131
- *
132
- * WHY: Anthropic can hold schemas server-side (`defer_loading` +
133
- * `tool_search_tool_regex`), so on Claude the full core costs nothing to
134
- * advertise. That is one provider out of eleven. On OpenAI, Gemini, DeepSeek,
135
- * Qwen, Muse, xAI, Moonshot, Ollama and the two BYOK paths, all 24 core
136
- * schemas were serialised into every single request — 3,341 tokens, of which a
137
- * typical turn calls two.
138
- *
139
- * WHAT IS IN, AND WHY EXACTLY THESE: read, write, edit, list and search cover
140
- * every file operation a first turn can need; `sysRun` is the escape hatch that
141
- * can reach anything the other resident tools cannot; `update_plan` is how a
142
- * multi-step task announces its plan on step one; `tool_search` is the door to
143
- * deferred tools. `request_decision` is injected on the first turn only when
144
- * task policy detects an unresolved durable choice. With this set the agent is
145
- * never stuck — worst case it spends one call
146
- * discovering the tool it wants, and the tool it discovers stays loaded for the
147
- * rest of the task.
148
- *
149
- * WHAT WITHHOLDING DOES NOT DO: it does not remove capability. `resolveTool()`
150
- * still resolves a withheld core tool by name, so a model that calls one from
151
- * memory simply gets it. Only the schema is absent, never the tool.
152
122
  */
153
123
  export const RESIDENT_TOOLS = [
154
- 'fs_read',
155
- 'fs_write',
156
- 'fs_edit',
157
- 'fs_list',
158
- 'fs_search',
159
- 'sysRun',
160
- 'update_plan',
161
- 'tool_search'
124
+ 'fs_read',
125
+ 'fs_write',
126
+ 'fs_edit',
127
+ 'fs_list',
128
+ 'fs_search',
129
+ 'sysRun',
130
+ 'update_plan',
131
+ 'tool_search'
162
132
  ];
163
-
164
133
  const RESIDENT_TOOL_SET = new Set(RESIDENT_TOOLS);
165
-
166
134
  /** True when this core tool ships in every request even without native deferral. */
167
135
  export function isResidentTool(toolName) {
168
- return RESIDENT_TOOL_SET.has(toolName);
136
+ return RESIDENT_TOOL_SET.has(toolName);
169
137
  }
170
-
171
138
  // A tool may legitimately belong to more than one pack — check_system_specs is
172
139
  // Stage 1 of the game cascade and Stage 2 of the media hierarchy — so
173
140
  // membership is a set, not a single value.
174
141
  const TOOL_TO_PACKS = (() => {
175
- const map = new Map();
176
- for (const packName of PACK_NAMES) {
177
- for (const toolName of PACKS[packName]) {
178
- if (!map.has(toolName)) map.set(toolName, new Set());
179
- map.get(toolName).add(packName);
142
+ const map = new Map();
143
+ for (const packName of PACK_NAMES) {
144
+ for (const toolName of PACKS[packName]) {
145
+ let set = map.get(toolName);
146
+ if (!set) {
147
+ set = new Set();
148
+ map.set(toolName, set);
149
+ }
150
+ set.add(packName);
151
+ }
180
152
  }
181
- }
182
- return map;
153
+ return map;
183
154
  })();
184
-
185
155
  const CORE_TOOL_SET = new Set(CORE_TOOLS);
186
-
187
156
  /** Every pack a tool belongs to. Empty for core and forged tools. */
188
157
  export function packsForTool(toolName) {
189
- return TOOL_TO_PACKS.get(toolName) || null;
158
+ return TOOL_TO_PACKS.get(toolName) || null;
190
159
  }
191
-
192
160
  /** Primary pack for a tool, or 'core' if it is always loaded. */
193
161
  export function packForTool(toolName) {
194
- if (CORE_TOOL_SET.has(toolName)) return 'core';
195
- const packs = TOOL_TO_PACKS.get(toolName);
196
- return packs ? [...packs][0] : null;
162
+ if (CORE_TOOL_SET.has(toolName))
163
+ return 'core';
164
+ const packs = TOOL_TO_PACKS.get(toolName);
165
+ return packs ? ([...packs][0] ?? null) : null;
197
166
  }
198
-
199
167
  export function isCoreTool(toolName) {
200
- return CORE_TOOL_SET.has(toolName);
168
+ return CORE_TOOL_SET.has(toolName);
201
169
  }
202
-
203
170
  // ---------------------------------------------------------------------------
204
171
  // Pack playbooks. These were previously hardcoded into the always-on
205
172
  // SYSTEM_PROMPT, costing every task ~2,000 tokens of instructions about game
206
173
  // cascades and media hierarchies it would never use. They now travel with their
207
174
  // pack, so they arrive exactly when they become relevant.
208
175
  // ---------------------------------------------------------------------------
209
-
210
176
  export const PACK_PLAYBOOKS = {
211
- web: `# FRONTEND ENGINEERING MANDATE (CREATIVE DIVERSITY ENGINE)
177
+ web: `# FRONTEND ENGINEERING MANDATE (CREATIVE DIVERSITY ENGINE)
212
178
  When building websites, UIs, dashboards, or web applications, follow this design system hierarchy:
213
179
  ## Design System Selection
214
180
  1. **If the user specifies a design system** (Tailwind, Material-UI, Astryx, etc.) — use it without question.
@@ -228,8 +194,7 @@ You MUST produce visually UNIQUE output on every project. Specifically:
228
194
  - **REQUIRED**: After building, use 'screenshot' or 'browser_act' to VISUALLY VERIFY your work. If it looks like every other AI-generated site, tear it down and rebuild with a different approach.
229
195
  - **REQUIRED**: Once the design is finished, call 'memory_write' with the creativePattern argument (project, designApproach, colorPalette, fonts). This is what lets the next project deliberately differ from this one instead of drifting back into the same house style.
230
196
  - **ENFORCED, NOT ADVISORY**: every HTML/CSS file you write is read back and scored by an automatic gate before the task can finish (src/slopDetector.js). It looks for the stock gradient hexes, purple-to-blue ramps, placeholder marketing copy, emoji used as icons, default-only typography, the centered-hero/3-cards/"Get Started" skeleton and the stock soft shadow. Score too high and you will be sent back to rebuild with a named design movement. Design it properly the first time; the gate is not fooled by swapping one gradient for another.`,
231
-
232
- media: `# Media Generation Protocol (Sovereign 5-Stage Hierarchy & Seamless Consent Routing)
197
+ media: `# Media Generation Protocol (Sovereign 5-Stage Hierarchy & Seamless Consent Routing)
233
198
 
234
199
  ## Native engine-first for file media work
235
200
  For editing or producing media from files in the workspace, check \`native_engine\` status first; engines are added one at a time, starting with film. If the engine is missing, explain the download and ask before calling \`install\` with \`confirm: true\`. When the needed engine is installed, use \`catalog\` -> \`execute\` or \`batch\`. Pass \`project\` to edit an existing document or \`saveAs\` for a new one, then \`render\` a preview and inspect it before exporting. Engines are headless: never use MCP servers, \`computer_use\`, or screenshots to drive them. The generation chain below applies only when no native engine capability exists or the user explicitly asks for generated media.
@@ -346,191 +311,133 @@ Blender genuinely missing. Then say so plainly, offer the two options — instal
346
311
  with \`execute_procedural_media\` — and let the user choose. Never start a large
347
312
  download on your own initiative.
348
313
  `,
349
-
350
- game: `# Sovereign Game Development Cascade & Enforcement Engine
314
+ game: `# Sovereign Game Development Cascade & Enforcement Engine
351
315
  When the user asks you to build, create, test, expand, or develop a game, follow these rules:
352
316
 
353
317
  - **Browser / Three.js / WebGL / Python Games**: If the user explicitly requests a Three.js, WebGL, Canvas, HTML5, or Python game (or mentions libraries like Three.js, 3js, NumPy, Pygame), you do NOT need Godot. Build the game directly in HTML/JS/CSS or Python using \`fs_write\` (or \`file_write\`) and run/verify using \`sysRun\` or \`game_build_html5\`.
354
318
  - **Godot / Full Engine Games**: When building a full engine game (Godot), follow the 7-stage production methodology using \`godot_act\`, \`game_lore_forge\`, \`game_asset_harvest\`, \`game_scene_build\`, and verify with \`game_web_runtime_inspector\` and \`game_canvas_audit\`.`
355
319
  };
356
-
357
320
  // ---------------------------------------------------------------------------
358
321
  // Intent routing.
359
322
  //
360
- // The previous implementation used bare substring matching, which produced
361
- // catastrophic false positives: 'ui' matched "build", "quick" and
362
- // "requirements"; 'race' matched "trace". Those false positives triggered
363
- // completion-blocking guards that forced dozens of extra LLM round trips on
364
- // tasks that had nothing to do with games or web UI.
365
- //
366
323
  // Every matcher below is anchored on word boundaries and tested against the
367
324
  // raw prompt only.
368
325
  // ---------------------------------------------------------------------------
369
-
370
- const GAME_INTENT = /\b(game|games|gamedev|godot|unity|unreal|rpg|platformer|roguelike|arcade|sprite|spritesheet|tilemap|voxel|minecraft|aetheria|voxellia|nes|snes|gameplay|player character|level design|boss fight)\b|\.(tscn|gd)\b/i;
371
-
372
- const WEB_INTENT = /\b(web|webpage|website|frontend|front-end|landing page|homepage|dashboard|portfolio|html|css|scss|tailwind|bootstrap|react|vue|svelte|angular|next\.?js|nuxt|astro|ui|ux|user interface|web app|webapp|spa|dom|responsive|ui forge|component forge|component generation|ui component|three\.?js|threejs|webgl|canvas|babylon)\b/i;
373
-
326
+ const GAME_INTENT = /\b(game|games|gamedev|godot|unity|unreal|rpg|platformer|roguelike|arcade|sprite|spritesheet|tilemap|voxel|minecraft|aetheria|voxellia|nes|snes|gameplay|player character|level design|boss fight|simulator|simulation|simulasi)\b|\.(tscn|gd)\b|[-_]3d\b|\b3d[-_]/i;
327
+ const WEB_INTENT = /\b(web|webpage|website|frontend|front-end|landing page|homepage|dashboard|portfolio|html|css|scss|tailwind|bootstrap|react|vue|svelte|angular|next\.?js|nuxt|astro|ui|ux|user interface|web app|webapp|spa|dom|responsive|ui forge|component forge|component generation|ui component|three?\.?js|three?js|webgl|canvas|babylon)\b/i;
374
328
  const MEDIA_INTENT = /\b(image|images|picture|photo|render|3d model|blender|animation|animate|video|movie|film|audio|sound|sfx|music|song|voice|tts|text.to.speech|narration|podcast|thumbnail|poster|illustration|logo)\b/i;
375
-
376
329
  const SYSTEM_INTENT = /\b(process|processes|pid|kill|port|zip|unzip|tar|archive|compress|extract|clipboard|copy to clipboard|print|printer|open in browser|launch app|computer use|vision grounding|desktop vision|gui agent|coordinate click|desktop control)\b/i;
377
-
378
330
  const ADVANCED_INTENT = /\b(pdf|document|proposal|screenplay|shotlist|shot list|voiceover|dubbing|forge a tool|custom tool|new tool|plugin|plugins|custom plugin|subagent|sub-agent|parallel agent|swarm|ast|symbols|refactor across)\b/i;
379
-
380
331
  const INTENT_MATCHERS = {
381
- game: GAME_INTENT,
382
- web: WEB_INTENT,
383
- media: MEDIA_INTENT,
384
- system: SYSTEM_INTENT,
385
- advanced: ADVANCED_INTENT
332
+ game: GAME_INTENT,
333
+ web: WEB_INTENT,
334
+ media: MEDIA_INTENT,
335
+ system: SYSTEM_INTENT,
336
+ advanced: ADVANCED_INTENT
386
337
  };
387
-
388
338
  /** True when the prompt genuinely describes game development work. */
389
339
  export function isGameIntent(prompt) {
390
- return GAME_INTENT.test(prompt || '');
340
+ return GAME_INTENT.test(prompt || '');
391
341
  }
392
-
393
342
  /** True when the prompt genuinely describes web/UI work. */
394
343
  export function isWebIntent(prompt) {
395
- return WEB_INTENT.test(prompt || '');
344
+ return WEB_INTENT.test(prompt || '');
396
345
  }
397
-
398
346
  /**
399
347
  * Decide which packs to unlock before the first LLM call, so the common case
400
348
  * costs zero discovery round trips.
401
- *
402
- * @param {string} prompt The user's task text.
403
- * @param {object} workspaceContext Optional perception output; its detected
404
- * frameworks and files are strong signals.
405
- * @returns {string[]} pack names to unlock
406
349
  */
407
350
  export function routePacks(prompt, workspaceContext = null) {
408
- const unlocked = new Set();
409
- const text = prompt || '';
410
-
411
- for (const packName of PACK_NAMES) {
412
- if (INTENT_MATCHERS[packName].test(text)) {
413
- unlocked.add(packName);
351
+ const unlocked = new Set();
352
+ const text = prompt || '';
353
+ for (const packName of PACK_NAMES) {
354
+ if (INTENT_MATCHERS[packName].test(text)) {
355
+ unlocked.add(packName);
356
+ }
414
357
  }
415
- }
416
-
417
- // Workspace signals. A Godot project on disk is far more reliable evidence of
418
- // game work than any keyword, and a detected web framework means the agent
419
- // will eventually need to look at what it built.
420
- if (workspaceContext) {
421
- const signals = [
422
- workspaceContext.framework,
423
- workspaceContext.projectType,
424
- workspaceContext.designSystem,
425
- Array.isArray(workspaceContext.keyFiles) ? workspaceContext.keyFiles.join(' ') : ''
426
- ]
427
- .filter(Boolean)
428
- .join(' ')
429
- .toLowerCase();
430
-
431
- if (/godot|project\.godot|unity|\.tscn/.test(signals)) unlocked.add('game');
432
- if (/react|vue|svelte|next|nuxt|astro|angular|tailwind|vite|html/.test(signals)) unlocked.add('web');
433
- }
434
-
435
- return [...unlocked];
358
+ // Workspace signals. A Godot project on disk is far more reliable evidence of
359
+ // game work than any keyword, and a detected web framework means the agent
360
+ // will eventually need to look at what it built.
361
+ if (workspaceContext) {
362
+ const signals = [
363
+ workspaceContext.framework,
364
+ workspaceContext.projectType,
365
+ workspaceContext.designSystem,
366
+ Array.isArray(workspaceContext.keyFiles) ? workspaceContext.keyFiles.join(' ') : ''
367
+ ]
368
+ .filter(Boolean)
369
+ .join(' ')
370
+ .toLowerCase();
371
+ if (/godot|project\.godot|unity|\.tscn/.test(signals))
372
+ unlocked.add('game');
373
+ if (/react|vue|svelte|next|nuxt|astro|angular|tailwind|vite|html/.test(signals))
374
+ unlocked.add('web');
375
+ }
376
+ return [...unlocked];
436
377
  }
437
-
438
378
  /**
439
379
  * Resolve the tool schemas that should enter the context window.
440
380
  * Core tools always load; pack tools load only once their pack is unlocked.
441
- *
442
- * Ordering is APPEND-ONLY and this matters a great deal for cost. Every
443
- * provider's prompt cache binds on an identical prefix, and tool definitions
444
- * sit at the very front of that prefix — so if unlocking a pack mid-task
445
- * reshuffles the array, the cache is invalidated from the first changed byte
446
- * onward, taking the system prompt and the whole message history with it.
447
- * Sorting pack tools alphabetically did exactly that: unlocking `system`
448
- * inserted `archive` ahead of `astryx_oracle` and truncated the stable prefix
449
- * to 82%. Ordering by unlock sequence instead keeps 100% of the previously
450
- * cached prefix intact and appends only the new tail.
451
- *
452
- * @param {Array} allDefinitions Full TOOL_DEFINITIONS list.
453
- * @param {Set} unlockedPacks Pack names unlocked for this task.
454
- * @param {Set} unlockedTools Individually unlocked tool names.
455
- * @param {Array} unlockOrder Pack names in the order they were unlocked.
456
- * @returns {Array} filtered definitions, deterministically ordered
457
381
  */
458
382
  export function resolveActiveTools(allDefinitions, unlockedPacks, unlockedTools, unlockOrder = null, options = {}) {
459
- const packs = unlockedPacks || new Set();
460
- const tools = unlockedTools || new Set();
461
- // Providers with native schema deferral (Anthropic) leave this off — server
462
- // side deferral is strictly better, since it costs no discovery round trip.
463
- const residentOnly = options.residentOnly === true;
464
-
465
- const active = allDefinitions.filter(def => {
466
- if (isCoreTool(def.name)) {
467
- if (!residentOnly) return true;
468
- return isResidentTool(def.name) || tools.has(def.name);
469
- }
470
- if (tools.has(def.name)) return true;
471
- if (def.xCausyneDeferred === true) return false;
472
- const memberOf = packsForTool(def.name);
473
- // A native tool with no declared pack stays available. Manifest plugins are
474
- // marked xCausyneDeferred and enter through tool_search, so enabling many
475
- // plugins does not permanently spend their schemas on unrelated tasks.
476
- if (!memberOf) return true;
477
- for (const pack of memberOf) {
478
- if (packs.has(pack)) return true;
479
- }
480
- return false;
481
- });
482
-
483
- // The stable head of the array. Without residentOnly that is the whole core;
484
- // with it, only the resident subset — and that distinction matters for the
485
- // prompt cache, not just for token count.
486
- //
487
- // A core tool unlocked mid-task must NOT be slotted back into its CORE_TOOLS
488
- // position: that inserts into the middle of the array and invalidates the
489
- // cached prefix from that point on, every time. It is appended to the tail
490
- // below instead, so the head stays byte-identical for the whole task.
491
- const isHead = (name) => !residentOnly || isResidentTool(name);
492
- const coreFirst = active.filter(d => isCoreTool(d.name) && isHead(d.name));
493
- coreFirst.sort((a, b) => CORE_TOOLS.indexOf(a.name) - CORE_TOOLS.indexOf(b.name));
494
-
495
- // Pack tools grouped by unlock sequence, alphabetical only within a group so
496
- // the tail added by any single unlock is itself deterministic.
497
- const sequence = Array.isArray(unlockOrder) && unlockOrder.length > 0
498
- ? unlockOrder
499
- : [...packs];
500
- const rankOfPack = new Map(sequence.map((name, index) => [name, index]));
501
-
502
- const rankOf = (def) => {
503
- // A non-resident core tool the model discovered via tool_search. Ranked
504
- // ahead of every pack so the tail stays grouped, but behind the fixed head.
505
- if (isCoreTool(def.name)) return -1;
506
- const memberOf = packsForTool(def.name);
507
- if (!memberOf) return Number.MAX_SAFE_INTEGER; // forged tools last
508
- let best = Number.MAX_SAFE_INTEGER;
509
- for (const pack of memberOf) {
510
- const rank = rankOfPack.get(pack);
511
- if (rank !== undefined && rank < best) best = rank;
512
- }
513
- return best;
514
- };
515
-
516
- const rest = active.filter(d => !isCoreTool(d.name) || !isHead(d.name));
517
- rest.sort((a, b) => {
518
- const diff = rankOf(a) - rankOf(b);
519
- return diff !== 0 ? diff : a.name.localeCompare(b.name);
520
- });
521
-
522
- return [...coreFirst, ...rest];
383
+ const packs = unlockedPacks instanceof Set ? unlockedPacks : new Set(unlockedPacks || []);
384
+ const tools = unlockedTools instanceof Set ? unlockedTools : new Set(unlockedTools || []);
385
+ const residentOnly = options.residentOnly === true;
386
+ const active = allDefinitions.filter(def => {
387
+ if (isCoreTool(def.name)) {
388
+ if (!residentOnly)
389
+ return true;
390
+ return isResidentTool(def.name) || tools.has(def.name);
391
+ }
392
+ if (tools.has(def.name))
393
+ return true;
394
+ if (def.xCausyneDeferred === true)
395
+ return false;
396
+ const memberOf = packsForTool(def.name);
397
+ if (!memberOf)
398
+ return true;
399
+ for (const pack of memberOf) {
400
+ if (packs.has(pack))
401
+ return true;
402
+ }
403
+ return false;
404
+ });
405
+ const isHead = (name) => !residentOnly || isResidentTool(name);
406
+ const coreFirst = active.filter(d => isCoreTool(d.name) && isHead(d.name));
407
+ coreFirst.sort((a, b) => CORE_TOOLS.indexOf(a.name) - CORE_TOOLS.indexOf(b.name));
408
+ const sequence = Array.isArray(unlockOrder) && unlockOrder.length > 0
409
+ ? unlockOrder
410
+ : [...packs];
411
+ const rankOfPack = new Map(sequence.map((name, index) => [name, index]));
412
+ const rankOf = (def) => {
413
+ if (isCoreTool(def.name))
414
+ return -1;
415
+ const memberOf = packsForTool(def.name);
416
+ if (!memberOf)
417
+ return Number.MAX_SAFE_INTEGER;
418
+ let best = Number.MAX_SAFE_INTEGER;
419
+ for (const pack of memberOf) {
420
+ const rank = rankOfPack.get(pack);
421
+ if (rank !== undefined && rank < best)
422
+ best = rank;
423
+ }
424
+ return best;
425
+ };
426
+ const rest = active.filter(d => !isCoreTool(d.name) || !isHead(d.name));
427
+ rest.sort((a, b) => {
428
+ const diff = rankOf(a) - rankOf(b);
429
+ return diff !== 0 ? diff : a.name.localeCompare(b.name);
430
+ });
431
+ return [...coreFirst, ...rest];
523
432
  }
524
-
525
433
  /** Human-readable catalogue injected into the system prompt so the model knows what it can unlock. */
526
434
  export function getPackCatalogue(unlockedPacks) {
527
- const packs = unlockedPacks || new Set();
528
- const lines = PACK_NAMES.map(name => {
529
- const state = packs.has(name) ? 'LOADED' : 'available';
530
- return `- ${name} (${PACKS[name].length} tools, ${state})`;
531
- });
532
-
533
- return `# EXTENDED CAPABILITY PACKS
435
+ const packs = unlockedPacks instanceof Set ? unlockedPacks : new Set(unlockedPacks || []);
436
+ const lines = PACK_NAMES.map(name => {
437
+ const state = packs.has(name) ? 'LOADED' : 'available';
438
+ return `- ${name} (${PACKS[name].length} tools, ${state})`;
439
+ });
440
+ return `# EXTENDED CAPABILITY PACKS
534
441
  Beyond the tools currently in your context you have access to specialised capability packs. Nothing is disabled — packs load on demand to keep your context focused.
535
442
  ${lines.join('\n')}
536
443
  To load a pack, call \`tool_search\` with a query describing what you need (for example "godot scene", "generate music", "kill process on port"). The matching tool schemas become available immediately on your next turn. Never claim a capability is missing without searching first.`;