Files
gabrielvidalandClaude Opus 5.5 1f1cd8e9da feat(conversations): hub children that report back keep their parent running, listed with its subagents
spawned_live.py: the reporting children still at work, per parent (memoized),
merged into runningAgents and _resolve_state (never over interrupted/review).
spawnedChildren carry startedAt/messages/tokens/subagentCount/shortTitle/prompt/report;
spawnedByChain for the breadcrumb; SSE parent cards + roots for child writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 17:14:55 +01:00

551 lines
26 KiB
Python

"""Subagent (sidechain) linkage: the transcript tree under ``subagents/``."""
import functools
import json
import pathlib
import re
import time
import conversations
from conversations import ids, short_title, spawned_live
from conversations.cards import _conv_meta, _conv_title
from conversations.resources import merge_children
from core.state import AppState
# The path helpers below run once per summary on every list/agents build
# (thousands of calls per request) and `pathlib.relative_to` alone costs
# ~0.2 ms a call — memoized, since a transcript path maps to the same id forever.
@functools.lru_cache(maxsize=32768)
def _conv_id(path: str) -> str:
"""Transcript path → its public id, the UUIDv7 every response carries
(conversations/ids.py) — the path itself never leaves the backend."""
return ids.uid_for_path(path)
# ── subagent (sidechain) linkage ─────────────────────────────────────────────
# Subagent transcripts live at `<convId>/subagents/agent-<id>.jsonl` next to the
# root `<convId>.jsonl`. Claude Code writes **every** nested agent into that one
# flat dir, whatever its depth: a subagent's own Task calls spawn grandchildren
# whose `agent-<id>.meta.json` names the spawner in `parentAgentId` (absent on a
# depth-1 agent, whose parent is the root conversation) and the level in
# `spawnDepth`. So the directory is a tree keyed by `parentAgentId`, and two
# "parent" notions matter: the **root** conversation (the `<convId>.jsonl` the
# dir belongs to — where usage rolls up and where the list card lives) and the
# **direct parent** (the transcript holding the Task card that spawned it —
# where the card links and the running state come from). Sidechains are parsed
# like any transcript (flagged `isSidechain`) but hidden from the top-level
# lists; each is reachable from its parent's Task/Agent card, at any depth.
@functools.lru_cache(maxsize=32768)
def _is_sidechain_path(abs_path: str) -> bool:
return "subagents" in pathlib.Path(abs_path).parts
@functools.lru_cache(maxsize=32768)
def _root_path_of(abs_path: str) -> str | None:
"""Given a subagent transcript path (any depth), the root `<convId>.jsonl`."""
parts = pathlib.Path(abs_path).parts
if "subagents" not in parts:
return None
idx = parts.index("subagents")
conv_dir = pathlib.Path(*parts[:idx]) # …/<convId>
return str(conv_dir.with_suffix(".jsonl")) # …/<convId>.jsonl
def _agent_id_of(abs_path: str) -> str | None:
"""`…/subagents/agent-<id>.jsonl` → `<id>`; None for a root transcript."""
if not _is_sidechain_path(abs_path):
return None
stem = pathlib.Path(abs_path).stem
return stem[len("agent-"):] if stem.startswith("agent-") else stem
def _parent_path_of(abs_path: str) -> str | None:
"""Given a subagent transcript path, its **direct** parent's transcript: the
sibling `agent-<parentAgentId>.jsonl` for a nested agent, else the root
`<convId>.jsonl`. Not memoised on the path alone: the meta.json may not be
written yet on the first look (the cached reader only keeps non-empty
reads), and a nested agent must not get pinned to the root."""
root = _root_path_of(abs_path)
if not root:
return None
pid = _subagent_meta_cached(pathlib.Path(abs_path)).get("parentAgentId")
if pid:
return str(pathlib.Path(abs_path).with_name(f"agent-{pid}.jsonl"))
return root
def _parent_chain_of(abs_path: str) -> list[str]:
"""Every ancestor transcript of a subagent, root first, direct parent last.
Bounded by the dir's size (a bad `parentAgentId` cycle can't loop)."""
chain: list[str] = []
seen: set[str] = set()
cur = _parent_path_of(abs_path)
while cur and cur not in seen:
chain.append(cur)
seen.add(cur)
cur = _parent_path_of(cur)
chain.reverse()
return chain
def _subagent_dir_of(ap: pathlib.Path) -> pathlib.Path | None:
"""The flat `subagents/` dir a transcript's descendants live in — beside a
root (`<convId>/subagents/`), the own dir for a sidechain."""
if _is_sidechain_path(str(ap)):
return ap.parent
d = ap.with_suffix("") / "subagents"
return d if d.is_dir() else None
def _subagent_children(ap: pathlib.Path) -> list[tuple[pathlib.Path, dict]]:
"""The **direct** children of a transcript, as `(agent jsonl, meta)` pairs
in spawn order (meta files sort by agent id, which Claude Code assigns in
order): a root's are the metas with no `parentAgentId`, a sidechain's are
the ones naming its agent id. A meta without `parentAgentId` under an
older transcript is a depth-1 agent by definition, so pre-nesting archives
keep their one-level reading."""
sub_dir = _subagent_dir_of(ap)
if sub_dir is None or not sub_dir.is_dir():
return []
own = _agent_id_of(str(ap))
out = []
for meta_f in sorted(sub_dir.glob("*.meta.json")):
stem = meta_f.name[:-len(".meta.json")] # agent-<id>
child = sub_dir / f"{stem}.jsonl"
if child == ap:
continue
mj = _subagent_meta_cached(child)
if (mj.get("parentAgentId") or None) == own:
out.append((child, mj))
return out
def _subagent_descendants(ap: pathlib.Path) -> list[pathlib.Path]:
"""Every transcript under `ap` in the agent tree (children, grandchildren…),
breadth-first. For a root that is the whole `subagents/` dir."""
out: list[pathlib.Path] = []
queue = [ap]
seen: set[str] = {str(ap)}
while queue:
cur = queue.pop(0)
for child, _mj in _subagent_children(cur):
if str(child) in seen:
continue
seen.add(str(child))
out.append(child)
queue.append(child)
return out
def _with_child_models(models: list[str] | None, children: list[dict]) -> list[str]:
"""The conversation's own models (busiest first) followed by any model only
its subagents ran on — so a thread on opus whose Explore agents ran haiku
is tagged with both. Appended, never interleaved: `models[0]` stays the
main thread's busiest model, which is what the family/footprint reads."""
out = list(models or [])
for c in children:
for m in c.get("models") or ([c["model"]] if c.get("model") else []):
if m not in out:
out.append(m)
return out
def _rollup_children(summary: dict, children: list[dict]) -> dict:
"""Fold each subagent's aggregate usage into the parent's totals and its
`agent` activity bucket, so a conversation's cost includes the sub-agents it
spawned. Returns a shallow copy; leaves the stored summary untouched."""
if not children:
return summary
s = dict(summary)
usage = dict(summary.get("usage") or conversations.zero_usage())
by_tool = {k: dict(v) for k, v in (summary.get("byTool") or {}).items()}
by_tool_sub = {k: {sk: dict(sv) for sk, sv in v.items()}
for k, v in (summary.get("byToolSub") or {}).items()}
add_tokens = add_output = 0
add_cost = 0.0
for c in children:
cu = c.get("usage") or {}
for k in usage:
usage[k] = (usage.get(k) or 0) + (cu.get(k) or 0)
add_tokens += c.get("tokens") or 0
add_cost += c.get("cost") or 0.0
add_output += cu.get("output") or 0
agent = by_tool.get("agent") or {"tokens": 0, "cost": 0.0, "output": 0}
by_tool["agent"] = {"tokens": (agent.get("tokens") or 0) + add_tokens,
"cost": (agent.get("cost") or 0.0) + add_cost,
"output": (agent.get("output") or 0) + add_output}
# Mirror the rollup into the drill-down: a "subagents" sub of the agent bucket.
asub = by_tool_sub.setdefault("agent", {})
sa = asub.get("subagents") or {"tokens": 0, "cost": 0.0, "output": 0}
asub["subagents"] = {"tokens": (sa.get("tokens") or 0) + add_tokens,
"cost": (sa.get("cost") or 0.0) + add_cost,
"output": (sa.get("output") or 0) + add_output}
s["usage"] = usage
s["byTool"] = by_tool
s["byToolSub"] = by_tool_sub
s["tokens"] = (summary.get("tokens") or 0) + add_tokens
s["cost"] = (summary.get("cost") or 0.0) + add_cost
s["models"] = _with_child_models(summary.get("models"), children)
s["subagentCount"] = len(children)
s["subagentTokens"] = add_tokens
s["subagentCost"] = add_cost
# Plan units and the per-model / per-effort split roll up the same way.
add_units = sum(c.get("planUnits") or 0.0 for c in children)
s["subagentPlanUnits"] = add_units
s["planUnits"] = (summary.get("planUnits") or 0.0) + add_units
s["byModel"] = merge_by_model(summary.get("byModel"),
*(c.get("byModel") for c in children))
# The work-context agent's lookups (an issue's title, a channel's name)
# land in its own transcript: the parent's resources borrow them.
return merge_children(s, children)
def merge_by_model(*parts) -> dict:
"""Sum ``byModel`` maps (parser shape) into a fresh one, busiest first."""
out: dict[str, dict] = {}
for bm in parts:
for model, d in (bm or {}).items():
e = out.setdefault(model, {"turns": 0, "tokens": 0, "cost": 0.0,
"planUnits": 0.0,
"usage": conversations.zero_usage(),
"efforts": {}})
for k in ("turns", "tokens", "cost", "planUnits"):
e[k] += d.get(k) or 0
u = d.get("usage") or {}
e["usage"] = {k: e["usage"][k] + (u.get(k) or 0) for k in e["usage"]}
for eff, ed in (d.get("efforts") or {}).items():
ee = e["efforts"].setdefault(eff, {"turns": 0, "tokens": 0, "output": 0,
"cost": 0.0, "planUnits": 0.0})
for k in ee:
ee[k] += ed.get(k) or 0
return dict(sorted(out.items(), key=lambda kv: -kv[1]["cost"]))
def _subagent_meta(sub_jsonl: pathlib.Path) -> dict:
"""Read the `agent-<id>.meta.json` sitting next to a subagent transcript."""
meta_f = sub_jsonl.with_name(sub_jsonl.stem + ".meta.json")
try:
return json.loads(meta_f.read_text(encoding="utf-8"))
except (OSError, ValueError):
return {}
# meta.json files are written once at spawn and never change, so list endpoints
# may cache them. Missing/empty files are negative-cached briefly so hundreds
# of historical subagents without a .meta.json don't hit disk on every 2 s
# transcript write, while a brand-new subagent's sidecar is still picked up.
_SUBAGENT_META_CACHE: dict[str, dict] = {}
_SUBAGENT_META_MISS: dict[str, float] = {}
_SUBAGENT_META_MISS_TTL = 60.0
def _subagent_meta_cached(sub_jsonl: "pathlib.Path | str") -> dict:
"""Also takes the path as a string: the list's agents map asks for every
subagent transcript in the store after each write, and building a `Path`
(then `str()` of it) per lookup was most of that map's cost."""
key = sub_jsonl if isinstance(sub_jsonl, str) else str(sub_jsonl)
hit = _SUBAGENT_META_CACHE.get(key)
if hit is not None:
return hit
now = time.monotonic()
miss_at = _SUBAGENT_META_MISS.get(key)
if miss_at is not None and now - miss_at < _SUBAGENT_META_MISS_TTL:
return {}
mj = _subagent_meta(pathlib.Path(sub_jsonl))
if mj:
_SUBAGENT_META_CACHE[key] = mj
_SUBAGENT_META_MISS.pop(key, None)
else:
_SUBAGENT_META_MISS[key] = now
return mj
# The standalone ask-form tool's `create` output carries the form's links
# (`"url":"http://127.0.0.1:7462/f/<id>?token=…"`, `"publicUrl":"https://
# ask-form.xyz/f/<id>?token=…"`): the id, and the link the hub's mirror can't
# get any other way (askform/store.py `learn`).
_ASKFORM_LINK_RE = re.compile(rb"(https?://[^\s\"'\\]+/f/([a-z0-9][a-z0-9-]{0,60}_[a-z0-9]{6})\?token=[A-Za-z0-9_-]+)")
_LINE_TS_RE = re.compile(rb'"timestamp":"([0-9T:.+Z-]{10,40})"')
_FORM_SCAN: dict[str, tuple[int, tuple[str, ...], dict[str, dict], dict[str, str]]] = {}
def _line_ts(chunk: bytes, pos: int) -> str | None:
"""The `timestamp` of the transcript line holding ``chunk[pos]`` — when the
form's create call came back, i.e. when it opened."""
lo = chunk.rfind(b"\n", 0, pos) + 1
hi = chunk.find(b"\n", pos)
m = _LINE_TS_RE.search(chunk, lo, hi if hi >= 0 else len(chunk))
return m.group(1).decode() if m else None
def _scan_forms(sub_jsonl: pathlib.Path) -> tuple[tuple[str, ...], dict[str, dict], dict[str, str]]:
key = str(sub_jsonl)
try:
size = sub_jsonl.stat().st_size
except OSError:
return (), {}, {}
done, ids, links, ats = _FORM_SCAN.get(key, (0, (), {}, {}))
if size < done:
done, ids, links, ats = 0, (), {}, {}
if size > done:
start = max(0, done - 512) # a match straddling the last read's end
try:
with open(sub_jsonl, "rb") as fh:
fh.seek(start)
chunk = fh.read(size - start)
except OSError:
return ids, links, ats
links = {k: dict(v) for k, v in links.items()}
ats = dict(ats)
hits = [(m.start(), m.group(2).decode(), m.group(1).decode())
for m in _ASKFORM_LINK_RE.finditer(chunk)]
for pos, fid, url in sorted(hits):
if fid not in ids:
ids = (*ids, fid)
if fid not in ats:
ts = _line_ts(chunk, pos)
if ts:
ats[fid] = ts
if url:
slot = "url" if "://127.0.0.1" in url or "://localhost" in url else "publicUrl"
links.setdefault(fid, {})[slot] = url
_FORM_SCAN[key] = (size, ids, links, ats)
return ids, links, ats
def form_links(jsonl: pathlib.Path) -> dict[str, dict]:
"""The ask-form links one transcript printed — ``{form id: {url, publicUrl}}``
— from its `create` output or anything the agent said (the same scan as a
subagent's, incremental per file)."""
return dict(_scan_forms(jsonl)[1])
def _subagent_forms(sub_jsonl: pathlib.Path) -> tuple[list[str], dict[str, dict]]:
"""Form ids in a subagent's transcript, in order — the hub's own
``frm_…`` ones and the standalone ask-form tool's — plus, for the
latter, the links seen (``{id: {url, publicUrl}}``)."""
ids, links, _ = _scan_forms(sub_jsonl)
return list(ids), dict(links)
def _subagent_form_ids(sub_jsonl: pathlib.Path) -> list[str]:
return _subagent_forms(sub_jsonl)[0]
def _subtree_forms(deps: AppState, child: pathlib.Path, own_meta: dict,
grand: list[pathlib.Path], session: str | None) -> list[dict]:
"""Every form asked in a child's whole subtree — its own and its
descendants', a clarifier spawned three agents down included — as
``{id, at, agentId, agentType}`` in the order they opened. The Agent card
that spawned the child carries them, so they surface on this thread (and,
level by level, on the root) rather than only on the page of the agent
that happened to ask. Teaches the ask-form mirror each link on the way: a
subagent shares its parent's sessionId, so the form belongs to this one's."""
out: list[dict] = []
seen: set[str] = set()
for path, mj in [(child, own_meta), *((g, _subagent_meta_cached(g)) for g in grand)]:
ids, links, ats = _scan_forms(path)
for fid in ids:
if fid in seen:
continue
seen.add(fid)
out.append({"id": fid, "at": ats.get(fid), "agentId": _conv_id(str(path)),
"agentType": mj.get("agentType")})
for fid, lk in links.items():
deps.askform_store.learn(fid, url=lk.get("url"), public_url=lk.get("publicUrl"),
session=session)
# Chronological across the subtree; a form with no readable time keeps its
# place after the timed ones (stable sort).
out.sort(key=lambda f: (f["at"] is None, f["at"] or ""))
return out
def _running_agents(deps: AppState, summary: dict, state: str | None = None) -> list[dict]:
"""The subagents a conversation still has in flight.
An open Task call only means "running" while the conversation itself is: a
run that was interrupted (or died) mid-agent leaves its call open forever,
and so does every transcript written before the parser learned to close a
background agent on its `<task-notification>`. Nothing is running there."""
ra = summary.get("runningAgents") or []
# The hub children still at work that report back (spawned_live.py): the
# hub's own subagents, listed with the native ones.
kids = ([] if summary.get("isSidechain")
else spawned_live.running_child_agents(deps, summary.get("sessionId")))
if not ra and not kids:
return []
st = state if state is not None else _conv_meta(deps, summary).get("state")
return [*ra, *kids] if st == "running" else []
def _is_deploying(deps: AppState, summary: dict, state: str | None = None) -> bool:
"""True while this conversation has a deploy command in flight (issued, no
tool_result yet) — gated the same way as `_running_agents`: a dangling call
left open by an interrupted/dead run isn't actually deploying anymore."""
if not summary.get("deploying"):
return False
st = state if state is not None else _conv_meta(deps, summary).get("state")
return st == "running"
def _sidechain_state(deps: AppState, ap: pathlib.Path) -> str | None:
"""A subagent's own lifecycle state: it runs exactly as long as the parent's
Task call is still open. `None` if `ap` isn't a subagent transcript.
Its `_conv_meta` state can't be trusted: a sidechain's sessionId is the
*parent's*, so the sidecar lookup returns the parent's (possibly stale)
state, and the recency fallback would keep a just-finished subagent "running"
for the whole recency window."""
pp = _is_sidechain_path(str(ap)) and _parent_path_of(str(ap))
if not pp:
return None
psum = deps.store.get_summary(pp) or {}
tuid = _subagent_meta(ap).get("toolUseId")
# A nested agent's parent is itself a sidechain: resolve *its* state the
# same way (up the chain to the root), since its own meta state would be
# the root's too.
pstate = _sidechain_state(deps, pathlib.Path(pp))
open_calls = {a.get("toolUseId") for a in _running_agents(deps, psum, pstate)}
return "running" if tuid and tuid in open_calls else "finished"
def _attach_subagents(deps: AppState, ap: pathlib.Path, data: dict) -> dict:
"""Enrich a parsed thread with its subagents: link each Task/Agent card to
the **direct** child transcript it spawned (via meta.json `toolUseId`) and,
on a root conversation, roll every descendant's usage into its totals. A
subagent's own view gets the same card linking (its Task calls spawned the
grandchildren) plus a `parentConversation` back-link and the `parentChain`
breadcrumb up to the root — but no rollup: an agent page reports its own
spend, the root the whole tree's. Returns the (mutated) data."""
is_sub = _is_sidechain_path(str(ap))
if is_sub:
# Link back up: the direct parent + the exact Task card, and the chain
# of ancestors (root first) for the breadcrumb.
chain = _parent_chain_of(str(ap))
mj = _subagent_meta(ap)
if chain:
pp = chain[-1]
psum = deps.store.get_summary(pp) or {}
data["parentConversation"] = {
"id": _conv_id(pp),
"title": _conv_title(deps, psum),
"toolUseId": mj.get("toolUseId"),
"agentType": mj.get("agentType"),
"description": mj.get("description"),
}
refs = []
for anc in chain:
asum = deps.store.get_summary(anc) or {}
amj = (_subagent_meta_cached(pathlib.Path(anc))
if _is_sidechain_path(anc) else {})
refs.append({"id": _conv_id(anc), "title": _conv_title(deps, asum),
"agentType": amj.get("agentType"),
"description": amj.get("description")})
data["parentChain"] = refs
data["spawnDepth"] = mj.get("spawnDepth") or len(chain) or 1
# Mark every agent card with whether its agent is still running (the
# parser tracks the open calls as `runningAgents`) — including the ones
# whose subagent transcript hasn't been written yet, which is exactly the
# moment the card most needs to say "running".
open_calls = {a.get("toolUseId") for a in (data.get("runningAgents") or [])}
for it in data.get("thread") or []:
if it.get("name") == "agent" and it.get("toolUseId"):
it["agentRunning"] = it["toolUseId"] in open_calls
# Gather the direct children from the flat `subagents/` dir.
children = _subagent_children(ap)
if not children:
return data
# A native subagent runs in its parent's harness — its card names it
# only when that differs from the page's (a hub child's parent).
harness = ((data.get("meta") or {}).get("harness") or data.get("harness") or "claude")
link_by_tooluse: dict[str, dict] = {}
infos_by_id: dict[str, dict] = {}
paths_by_id: dict[str, str] = {}
subtree_cost: dict[str, tuple[float, int]] = {}
for child, mj in children:
csum = deps.store.get_summary(str(child)) or {}
paths_by_id[_conv_id(str(child))] = str(child)
tuid = mj.get("toolUseId")
grand = _subagent_descendants(child)
info = {"id": _conv_id(str(child)),
"agentType": mj.get("agentType"),
"description": mj.get("description"),
"title": csum.get("title"), "model": csum.get("model"),
"tokens": csum.get("tokens"), "cost": csum.get("cost"),
"messages": csum.get("messages"),
"startedAt": csum.get("startedAt"),
"endedAt": csum.get("endedAt"),
"state": "running" if tuid in open_calls else "finished",
"subagentCount": len(grand),
"harness": harness}
refs = _subtree_forms(deps, child, mj, grand, data.get("sessionId"))
if refs:
info["forms"] = [f["id"] for f in refs]
info["formRefs"] = refs
infos_by_id[info["id"]] = info
if tuid:
link_by_tooluse[tuid] = info
# What the card that spawned this child actually caused: the child's
# own spend plus everything it spawned in turn (the grandchildren have
# no card on *this* page). The ref itself keeps the child's own cost —
# the number its page shows.
cost = csum.get("cost") or 0.0
toks = csum.get("tokens") or 0
for g in grand:
gs = deps.store.get_summary(str(g)) or {}
cost += gs.get("cost") or 0.0
toks += gs.get("tokens") or 0
subtree_cost[info["id"]] = (cost, toks)
# Link each agent card to its child and collect the subagents in the
# order they were spawned (thread order), so the detail view can list them.
ordered: list[dict] = []
seen: set[str] = set()
for it in data.get("thread") or []:
tuid = it.get("toolUseId")
info = tuid and link_by_tooluse.get(tuid)
if info:
it["subagent"] = info
# A subagent's spend is rolled into the parent's total and its `agent`
# bucket below, but it was billed to the *child's* transcript — so no
# parent thread item carries it, and the cost-growth chart (which sums
# thread items) would report less than the total. Credit it to the card
# that spawned it: the turn that actually caused the spend.
child_cost, child_tokens = subtree_cost.get(info["id"], (0.0, 0))
if child_cost > 0:
it["turnCost"] = (it.get("turnCost") or 0.0) + child_cost
it["turnTokens"] = (it.get("turnTokens") or 0) + child_tokens
# It's all downstream agent work, whatever the card's own turn drove.
it["turnBucket"] = "agent"
if info["id"] not in seen:
ordered.append(info)
seen.add(info["id"])
for cid, info in infos_by_id.items(): # any orphans (no matching card)
if cid not in seen:
ordered.append(info)
seen.add(cid)
# No card claimed this child's cost above, but the rollup still counts
# it — park it on an invisible carrier so the chart stays whole.
o_cost, o_tokens = subtree_cost.get(cid, (0.0, 0))
if o_cost > 0 and data.get("thread") is not None:
data["thread"].append({
"role": "assistant", "kind": "turn", "first": False, "out": 0,
"turnCost": o_cost, "turnTokens": o_tokens,
"turnBucket": "agent",
})
# Their short titles: cached, or asked for now in the background — this
# page is where they are seen (the Task cards share these dicts).
short_title.for_subagents(deps, ordered, paths_by_id)
data["subagents"] = ordered
if is_sub:
return data # an agent page reports its own spend only
# Roll every descendant's usage into the root totals / `agent` bucket.
child_summaries = [cs for cs in (deps.store.get_summary(str(d))
for d in _subagent_descendants(ap)) if cs]
rolled = _rollup_children(data, child_summaries)
for k in ("usage", "byTool", "byToolSub", "tokens", "cost",
"subagentCount", "subagentTokens", "subagentCost"):
if k in rolled:
data[k] = rolled[k]
return data