magus v0.4.3 is out. See what's new
¶ View markdown source · ✎ Suggest an edit
6 min read

magus\guard.spawn

magus\guard.spawn is the spawn-time sibling of magus\guard.shell and magus\guard.command. The root magusfile registers one function; the agent guard calls it on every subagent spawn and every message to an existing subagent it sees, hands it the request, and adds its answer to the built-in verdict.

magus ships no spawn rule. Whether a spawn must name a model, carry completion criteria, or run in its own worktree is your workspace's decision. The seam exists so that decision can live in your tree, in Buzz, versioned with the code it governs.

import "magus";

magus\guard.spawn(fun (req: SpawnRequest) > GuardVerdict {
    if (req.kind == "spawn" and req.model == "") {
        return magus\guard.deny("Name a model for this spawn.");
    }
    if (req.target != null and (req.target!.idleMs ?? 0) > 300000 and magus\guard.once("idle-{req.target!.agent}")) {
        return magus\guard.advise("{req.target!.agent} has been idle over five minutes; a fresh brief may be cheaper.");
    }
    if (req.target != null and (req.target!.contextTokens ?? 0) > 200000) {
        return magus\guard.deny("{req.target!.agent} carries over 200k tokens of context; spawn fresh with a narrow brief.");
    }
    return magus\guard.allow();
});

The request

field what it is
kind spawn, or continue for a message to an existing subagent
host the host the hook wiring named itself as
session the calling session's id
model the model the caller named, raw; empty when it named none
agentType the subagent type asked for
description the short task label the caller wrote
name the host's separate addressable name for the new agent
prompt the brief for a spawn, the message for a continue
background true only when the caller asked for it
isolated true when the caller asked for its own checkout, such as a worktree
parent the description the calling subagent was itself spawned with; empty for a root caller
role worker when a lease binds the calling session in this checkout, else root
lease the bound job row for a worker, null for root
target for a continue: the agent addressed and what magus recorded about it (below)

target is null on a spawn. On a continue:

field what it is
agent the name or id the caller addressed
idleMs milliseconds since magus last saw it spawned, continued or finish its spawn call; null when never
description the title the agent was spawned with; empty when magus never saw that spawn finish
model the model that spawn named, raw; empty when it named none
contextTokens the agent's last observed context size in tokens; null when the host reported no usage for it

contextTokens is input plus cache-read plus cache-write tokens from the latest usage record the host reported for that agent: what the model was handed on its last call, which is the context a resume has to rebuild. Claude Code reports it through SubagentStop, whose agent_transcript_path magus reads for its last usage record, looking at the file's final 512 KiB only. Cursor and Codex report no per-agent usage, so it stays null there.

Every field is what the host reported or magus recorded. A host that does not report a field leaves it empty; nothing is inferred from which host sent the call, and no model string is mapped to a tier.

magus\guard.once(key) is true the first time a key is asked in the calling session. magus\guard.count(key) adds one to a key's tally in that session and returns it. Both work only while the guard runs the rule.

magus\job\list() works inside the rule and answers from the job rows the guard read for this call, so the rule and the built-in verdict it adds to read the same store. Every job member that writes raises inside a rule.

Job attribution

A spawn whose title (description) reads <parent>/<role> <job>, where <job> names a declared, running or exited job, attributes the new agent to that job. From then on every hook call carrying that agent's id is graded under the job's lease. An explicit --lease still wins. The agent's job outranks the session's magus job exec binding, because a subagent shares its parent's session id and only its agent id tells the two apart, and it outranks a BAGGAGE claim, as every record does.

When the job has not reported a base and the spawn did not ask for its own checkout, magus records this checkout's revision and dirty-patch digest for it, the values magus job exec records, so the agent's first write is not refused for a missing exec. An isolated agent's checkout is one magus cannot see from the spawning side, so it reports its own base with magus job exec.

Attribution is recorded when the host reports the finished spawn call with the child's id. Claude Code reports that at launch for a background agent, and only on return for a foreground one, so a foreground agent's own calls run unattributed. A host with no agent id keeps BAGGAGE and magus job exec.

Strengthen only

A deny blocks the call. An advise reaches the agent only where no built-in rule already spoke. Nothing the function returns lifts a built-in deny, and it is not called when one stands.

A function that raises, runs past three seconds, or returns something other than a verdict judges nothing: the built-ins apply alone and the agent is told once per session that the workspace rule failed. That is the stance magus\guard.shell takes on a rule it cannot use. Registering twice, from a project's magusfile, or with a non-function stops the workspace load with MGS1045.

Tighten live, loosen on approval

When the magusfile is under version control and a file its root load read differs from the checked-out commit, the guard evaluates the rule twice: once from the working tree and once from the committed sources, and keeps the stricter answer. An edit that tightens applies on the next spawn; one that loosens waits for a commit. The committed side reads the changed files through the VCS layer rather than a checkout, and the unchanged ones from disk. Local spells imported by path are read from the working tree on both sides. Both sides load the root magusfile alone, never the whole workspace; see what the guard costs.

Hosts

host spawn continue contextTokens
Claude Code Agent, Task SendMessage SubagentStop
Cursor subagentStart none the host has not reported
Codex not wired: no prompt sent none the host has not reported
OpenCode not wired not wired not wired

Each change to the effective rules is recorded once on the activity trail as a guard_policy event, and every verdict event names the policy digest in force and which side decided it.

guardagentsspawnsubagentspolicymagusfilehooksstrengthen-onlyattributioncontext
Last updated (f60cf2b9)
Earlier changes on this page (3)

Full history ↗ · Blame source ↗

Glossary

Workspace

The magus root directory that owns a set of projects and shared config; the unit magus operates over. See workspace.

Project

A directory magus recognizes as a unit of work (it has a magusfile); the unit of caching, scheduling, and dependency tracking. See workspace.

Magusfile

The magusfile.buzz that declares a project's targets (as export funs) and binds its spells. See targets.

Target

A named operation (build, test, ...) you invoke with magus run <target>; it may compose a spell's tool-native operations and depend on other targets. See targets.

Op

A single tool-native command a target composes (long form: operation); the middle of the work hierarchy (Spell to Op to Target). See operations.

Spell

A language/runtime adapter (e.g. go, md) that maps generic targets onto a toolchain's real commands. See spells.

Buzz

The language magusfiles are written in (the .buzz engine). See engines.

Cache

The content-addressed store magus consults before running a target, so unchanged work is skipped. See cache.

CI

An ordinary magusfile-defined target you compose yourself with magus\needs - magus does not hardcode its stages. Magus.RunCI treats it specially only in that it strips the rw charm, it is the anchor magus affected ci keys off, and a selected scope with no project declaring it is a load error rather than a silent no-op. See targets.

Session

An agent host's conversation, by the id the host delivers to its hooks. magus never mints one: a record with no session is unattributed, and the OS user it carries says whose account ran it.

Job

The unit of delegated work, and one row of the job store: what an orchestrating agent handed out, with its goal, the checkpoint it was cut against, the paths it may write or must not touch, and the one check it runs. A job's holder is either a session, for work an orchestrator handed out, or the server, for its own maintenance. The store records; the agent guard is what reads those facts back when grading a write. See doctrine.

A job is not a run. magus run build web is a run, and no job exists for it. A job causes runs: its check executes as one, and a server job records the invocation of its last one. Jobs are listed with magus ls jobs and in the console's Jobs view; runs are listed in the Runs view.

Run

One target executing under one magus invocation, such as magus run test web or magus affected ci. A run keeps its captured output behind an output reference. Every magus run is a run whether or not any job asked for it; see Job for how the two relate.

Lease

The grant a holder takes on a job: the write and read paths that job declared, enforced in the checkout that took it with magus job exec. A job is the piece of work; a lease is permission over it.

Advisor

One read-only check from the advice suite: it reads the changeset through magus and writes one titled section of findings. The same advisors run as a pull request comment in CI and inside magus diff --impact locally.

Conventions

Placeholders

Angle brackets mark a value you replace with your own - never type the brackets:

magus run <target>
magus completion <shell>    # e.g. bash, zsh, fish

<target>, <path>, <shell>, <name> and the like are stand-ins, not literal text.