From 749bdadad598888f5ac9ecf64df537c8bdcd298a Mon Sep 17 00:00:00 2001 From: Saksham Uboweja <76595406+SakshamUboweja@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:55:27 +0000 Subject: [PATCH] plugin: learn from the calls a Claude Code subagent made Claude Code logs a Task subagent's calls only in /subagents/*.jsonl, and the fold gave micro-skills the session's own log, where there is just the Agent call. The ledger and tool pruning already read the subagent logs (claude_subagent_files). ClaudeCode now gives the mechanisms the session's log followed by its subagents' logs, as docs/PLUGIN.md says: their calls are counted with their session. --- tests/test_live_hooks.py | 30 ++++++++++++++++++++++++++++++ tokeneconomizer/plugin/hosts.py | 19 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/tests/test_live_hooks.py b/tests/test_live_hooks.py index 0513edaa..4a128607 100644 --- a/tests/test_live_hooks.py +++ b/tests/test_live_hooks.py @@ -143,6 +143,36 @@ def test_a_learned_fact_reaches_the_next_session_and_is_announced_once(env, repo assert starts[5]["hookSpecificOutput"]["additionalContext"] == ctx +def subagent_trap_session(path, repo, i): + """The trap above, met by a Task subagent: the session's log holds only its `Agent` call, + and Claude Code logs the subagent's calls in `/subagents/agent-*.jsonl`.""" + lines = [{"type": "user", "message": {"role": "user", "content": f"fix bug {i}"}}, + {"type": "assistant", "message": { + "id": f"msg_agent_{i}", "model": "claude-opus-5-5", "role": "assistant", + "usage": {"input_tokens": 10, "output_tokens": 50}, + "content": [{"type": "tool_use", "id": "tu_agent", "name": "Agent", + "input": {"description": "fix the bug", "prompt": f"fix bug {i}"}}]}}, + {"type": "user", "message": {"role": "user", "content": [ + {"type": "tool_result", "tool_use_id": "tu_agent", "content": "fixed"}]}}] + with open(path, "w") as fh: + fh.write("\n".join(json.dumps(x) for x in lines) + "\n") + sub = os.path.join(os.path.splitext(path)[0], "subagents") + os.makedirs(sub) + trap_session(os.path.join(sub, "agent-a1.jsonl"), repo, i) + return path + + +def test_a_fact_is_learned_from_the_calls_a_subagent_made(env, repo, tmp_path): + starts = [run_session(env, repo, f"s{i}", + subagent_trap_session(str(tmp_path / f"s{i}.jsonl"), repo, i)) + for i in range(6)] + assert all(s == {} for s in starts[:4]) + assert ("`pytest …` fails here with command not found; `python -m pytest …` works." + in starts[4]["hookSpecificOutput"]["additionalContext"]) + ledger = store_for(env, repo).read_lines("ledger.jsonl") + assert [r["prompts"] for r in ledger] == [1] * 6 # the subagent's prompt is not one + + def test_micro_skills_is_off_by_default_and_learns_nothing(env, repo, tmp_path, monkeypatch, capsys): """The sessions that teach the fact above leave nothing behind unless it is turned on.""" diff --git a/tokeneconomizer/plugin/hosts.py b/tokeneconomizer/plugin/hosts.py index 5351a9e7..2aaa41a6 100644 --- a/tokeneconomizer/plugin/hosts.py +++ b/tokeneconomizer/plugin/hosts.py @@ -334,6 +334,25 @@ def headless(self, env, transcript=None): return attended == "0" return str(env.get("CLAUDE_CODE_ENTRYPOINT", "")).startswith("sdk") + def mechanism_trace(self, transcript_path, workdir): + """The session's log, followed by each of its subagents' (`/subagents/`): the + calls a subagent made are only there, and belong to the session that spawned it, as its + tokens and tools do.""" + main = super().mechanism_trace(transcript_path, workdir) + subs = trace.claude_subagent_files(main) if main else [] + if not subs: + return main + out = os.path.join(workdir, "claude_trace.jsonl") + with open(out, "wb") as dst: + for path in [main] + subs: + try: + with open(path, "rb") as src: + data = src.read() + except OSError: + continue + dst.write(data if not data or data.endswith(b"\n") else data + b"\n") + return out + def session_usage(self, transcript_path): return trace.claude_session_usage(transcript_path)