managarten/packages/shared-ai/src/planner
Till JS 101af462a8 feat(shared-ai): LLM-facing task tool wrapper for runSubAgent (M3.2)
Exposes runSubAgent() as a tool the planner LLM can call natively,
matching Claude Code's `Task` tool shape: { subagent_type, description,
prompt } -> single-string summary.

New exports from @mana/shared-ai:

  - TASK_TOOL_NAME = 'task'
  - TASK_TOOL_SCHEMA — ToolSchema ready to drop into a runPlannerLoop
    `tools` array. subagent_type enum = research|plan|general;
    description+prompt required; defaultPolicy: 'auto' (control-flow,
    not a user-data write).
  - createTaskToolHandler(opts) — factory returning:
      - handle(call): structured ToolResult with the sub-agent's
        summary as message + data {subAgentType, toolsCalled,
        rounds, stopReason, usage}
      - cumulativeUsage(): rolled-up TokenUsage across all sub-agent
        invocations — parent budget accounting reads from here
      - invocationCount(): metric-ready counter

Why not in mana-tool-registry: `task` is a loop-internal control-flow
primitive, not a user-data operation. Registry is for habits/notes/etc.
where MCP exposure and space-scoping matter. task never touches mana-
sync and never crosses the MCP boundary.

Recursion guard is defense-in-depth: the primitive throws
SubAgentRecursionError, this handler catches parentDepth >=
MAX_SUB_AGENT_DEPTH up front and returns a structured ToolResult
instead so the LLM sees it as regular tool-feedback.

Exceptions from the sub-agent (provider down, network) get wrapped
as `{ success: false, message: 'Sub-agent failed: ...' }`. The parent
loop's round continues.

14 new tests covering schema shape, recursion rejection, argument
validation (4 cases), happy path with tool dispatch, cumulative
usage tracking across multiple invocations, exception wrapping,
and parent-dispatcher routing.

107 shared-ai tests green total (was 93).

M3.3 consumer wiring follows.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-23 19:05:09 +02:00
..
compact.test.ts feat(shared-ai): route compactor to Haiku-tier model by default (M2.5) 2026-04-23 18:26:50 +02:00
compact.ts feat(shared-ai): route compactor to Haiku-tier model by default (M2.5) 2026-04-23 18:26:50 +02:00
index.ts feat(shared-ai): LLM-facing task tool wrapper for runSubAgent (M3.2) 2026-04-23 19:05:09 +02:00
loop.test.ts feat(agent-loop): expose compactionsDone + compactedReminder producer 2026-04-23 15:36:21 +02:00
loop.ts feat(agent-loop): expose compactionsDone + compactedReminder producer 2026-04-23 15:36:21 +02:00
mock-llm.ts test(ai): promote MockLlmClient to a shared @mana/shared-ai export 2026-04-20 18:05:46 +02:00
parser.test.ts feat(shared-ai): extract planner + mission types to @mana/shared-ai 2026-04-15 00:01:57 +02:00
parser.ts feat(shared-ai): extract planner + mission types to @mana/shared-ai 2026-04-15 00:01:57 +02:00
prompt.ts chore(ai): P2 batch — prompt sync, perf, dedup, scope unification 2026-04-16 16:33:52 +02:00
sub-agent.test.ts feat(shared-ai): runSubAgent() primitive — Claude-Code I2A pattern (M3.1) 2026-04-23 18:59:05 +02:00
sub-agent.ts feat(shared-ai): runSubAgent() primitive — Claude-Code I2A pattern (M3.1) 2026-04-23 18:59:05 +02:00
system-prompt.ts feat(shared-ai): runPlannerLoop + compact system prompt for function calling 2026-04-20 15:31:01 +02:00
task-tool.test.ts feat(shared-ai): LLM-facing task tool wrapper for runSubAgent (M3.2) 2026-04-23 19:05:09 +02:00
task-tool.ts feat(shared-ai): LLM-facing task tool wrapper for runSubAgent (M3.2) 2026-04-23 19:05:09 +02:00
types.ts feat(ai): SSE streaming for foreground Mission Runner 2026-04-16 12:32:43 +02:00