{"success":true,"name":"brimind-subsession-tree","description":"Keep one main session clean by giving each separate subject its own sub-session, down to depth 5, with every spawn reported to the owner along with its estimated and real token cost. Use when a task splits into independent subjects, when a subject would flood the current context (big search, long build, a whole client), when two subjects can run in parallel, and before opening any sub-session, sub","category":"unitary","path":"unitary/brimind-subsession-tree/SKILL.md","file":"SKILL.md","content":"---\nname: brimind-subsession-tree\ndescription: Keep one main session clean by giving each separate subject its own sub-session, down to depth 5, with every spawn reported to the owner along with its estimated and real token cost. Use when a task splits into independent subjects, when a subject would flood the current context (big search, long build, a whole client), when two subjects can run in parallel, and before opening any sub-session, subagent or agent-service conversation. Also use when deciding whether NOT to spawn (small task, same subject, next phase).\n---\n> Imported from [PRIVATE_HOST] `~/unitary/agents/s3a/skills/brimind-subsession-tree`. Paths are relative to `[PRIVATE_HOST]:[PATH]`; run commands there via `ssh [PRIVATE_HOST] 'cd ~/unitary && ...'`. Owner send rules from skill i7-remote-ops still apply.\n\n\n# Sub-session tree: one main session, one sub-session per subject\n\nOwner, 2026-10-07: *\"always have main session, and subsession to a level of 5 in\ndepth, even an AI agent can spawn subsession for each subject, according to the\nscope and the need\"*, and *\"dont ask but i should know, and if it did take more\ntokens or less\"*.\n\nSo: **spawn without asking, tell the owner every time, and give him the estimate next to what it really cost.**\n\n## 1. Spawn or not: decide first\n\n| Situation | Do | Why |\n|---|---|---|\n| New subject, unrelated to what this session is on | 🟢 **Sub-session** | One subject per session keeps each context clean |\n| Heavy work where only the conclusion matters (sweep, research, a whole build) | 🟢 **Sub-session** | The detail stays down there and only the result comes back up |\n| Two or more independent subjects | 🟢 **One sub-session each, in parallel** | Saves wall-clock time |\n| Same subject, next phase | 🟡 **Stay here.** In the direct chat, use `mark_chapter` | A new session would lose the context it needs |\n| Small task (< ~5 tool calls, < ~20k tokens) | 🔴 **Stay here** | A spawn costs more than the task |\n| The subject needs what this session already knows in detail | 🔴 **Stay here**, or hand over a full brief | A child starts from zero |\n| You are at depth 5 | 🔴 **Do it yourself** | The service refuses depth 6 |\n\nIf you're unsure, stay. A sub-session has to earn its startup cost.\n\n## 2. How to spawn\n\nReal depth only exists through **agent-service** (`:3060`). Claude Code's own\nsubagents (the Agent tool) cannot nest, so from inside a subagent nothing goes deeper.\n\n```bash\nnode scripts/agent.js ask \"<self-contained brief: goal, inputs, done-when, what to return>\" \\\n  --agent <id> --subject \"<one subject, ≤120 chars>\" --estimate <tokens>\n```\n\n- Inside an agent-service turn, the parent is filled in for you\n  (`AGENT_SERVICE_SESSION_ID`), so the new session is your child at depth + 1.\n- From the owner's direct chat, which isn't an agent-service session, the first\n  spawn opens a main session at depth 0. Spawn from it with `--parent <sessionId>`.\n  If the subject is big enough for its own window, use the app's `spawn_task` instead.\n- To continue a child: `--session <id>`. Don't open a second child for the same subject.\n- Library form: `agent.ask(brief, { agent, subject, estimateTokens })`.\n- The brief must stand alone. The child sees nothing of your conversation.\n- Ask the child to return **a short result**, never its whole transcript.\n\n### Estimating tokens (billed = input + output + cache writes)\n\n| Kind of work | Estimate |\n|---|---|\n| One question or a short draft | 20k |\n| Read a few files, small fix + tests | 80k |\n| Research or sweep across a folder | 150k |\n| Build a feature or site, several rounds | 300k–600k |\n\n## 3. The guards (enforced in code: `agent-service/lib/tree.js`)\n\n| Guard | Default | Env (may lower, never raise depth) | Refusal `code` |\n|---|---|---|---|\n| Depth under the main session | **5** | `AGENT_SERVICE_TREE_MAX_DEPTH` | `depth_limit` |\n| Children per session | 6 | `AGENT_SERVICE_TREE_MAX_CHILDREN` | `children_limit` |\n| Billed tokens per whole tree | 5,000,000 | `AGENT_SERVICE_TREE_MAX_TOKENS` | `tree_tokens` |\n\nWhen a guard refuses, don't route around it with `--main` or a new tree. Do the work\nin the session you're in, reuse an existing child, or merge subjects.\n\n## Verify the model that actually runs\n\nFor current Unitary jobs, begin with `node agents/sub.mjs spawn` and use the dispatcher’s task-level model and effort. An above-caller pick opens on temporary settings: register the child, raise model and effort, then send the task on the next turn. Compare the final stored pick to runtime telemetry, not the planned model or the model used to open the window. An [PRIVATE_HOST] Desktop work brief may be read only after this comparison passes; Mac rows without runtime telemetry remain unverified. The same model as main is acceptable when independently chosen for the task. Use scratch DBs with a unique name for each integration suite; parallel suites that share a DB can delete one another's rows.\n\n## 4. Telling the owner (required, no exceptions)\n\nEvery answer from a sub-session carries `subsession`:\n`{subject, depth, tokens, estimateTokens, vsEstimate: more|less|equal, deltaTokens, tree}`.\nThe CLI prints it as one line on stderr. Every spawn and every turn is also written to\n`agent-service/data/subsessions.jsonl`. You can see the whole tree with\n`node scripts/agent.js tree <sessionId>` or `GET /api/sessions/<id>/tree`.\n\nWhen a child finishes, report one row per sub-session in the owner's colored-table format:\n\n| | Sub-session | Depth | Est. | Real | Δ |\n|---|---|---|---|---|---|\n| 🟢 | pricing for Hotel X | 1 | 80k | 62k | **less** −18k |\n| 🔴 | site build | 2 | 300k | 410k | **more** +110k |\n\n🟢 = less than or equal to the estimate, 🟡 = up to 25% over, 🔴 = more than 25% over.\nWhen a row is red, give the reason in one line, so the next estimate is better.\n","linked_files":[],"origin":"brimind","owner_id":null,"source":"router"}