magus-sdk
暂无简介
- License 许可
- GPL-3.0
- 所属仓库
- egladman/magus
- 最近更新
- 2026-11-14
- Stars
- ★ 3
- Forks
- 0
03 SKILL.md 正文
Consuming magus as a Go library
{{if .Full}}This skill's reader has never run magus and does not know its subcommands. They
found github.com/egladman/magus on pkg.go.dev or in an import line. They want to
call it from their own Go program. Ground every answer in the actual exported API,
never in what the CLI does, which this reader cannot see:{{else}}The reader has never run magus; they found github.com/egladman/magus on
pkg.go.dev and want to call it from Go. Ground every answer in the exported API,
never the CLI:{{end}}
magus.go,register.go,run.go,judge.go,knowledge.go,describe.gotypes/repository.go,types/describe.goproject/impact/impact.go
Before anything else: can they go get it?
State what is verified, and claim no more. The root go.mod requires the nested
libs/gopherbuzz and libs/diagnostics modules at v0.2.0, and both tags are on
the origin (git ls-remote --tags origin lists libs/gopherbuzz/v0.2.0 and
libs/diagnostics/v0.2.0){{if .Full}}. Its replace directives point at the working
tree for in-repo builds only; a replace is never transitive, so a consumer resolves the
tagged versions the requires name. An older root tag may still require them at v0.0.0
and fail as unknown revision libs/gopherbuzz/v0.0.0{{end}}.
Say go get succeeds only for a version you ran it against, and say how: a scratch
module, go get github.com/egladman/magus@<version>, then go build ./...{{if .Full}}. A worked
example that compiles for you inside the module but fails for them is worse than
admitting you did not check. go install .../cmd/magus@<version> refuses the
root module at any version, since its go.mod carries replace directives{{end}}.
Entry points
| Call | Returns | Cache | Use when |
|---|---|---|---|
magus.Open(ctx, root, opts...) |
*magus.Magus |
builds one | the caller will Run targets or read cached results |
magus.Inspect(ctx, root, opts...) |
types.WorkspaceRepository |
none | pure introspection: list/describe/classify, never execute |
{{if .Full}}Both discover the root's projects and evaluate magusfiles the same way. Open calls
the same load() as Inspect, then also opens the on-disk cache. The only
difference is whether a cache is built, not what is discovered. Inspect returning the narrow
types.WorkspaceRepository interface rather than the concrete *Magus is
itself a hint: an introspection-only caller should code against the
interface, and a caller who later needs Run should switch to Open and get
the concrete type, not type-assert their way there.{{else}}A caller who later needs
Run switches to Open, never type-asserts.{{end}}
Both require magus.WithSpawnRule(magus.TierCeiling) (it has no default), and
a workspace with a magusfile needs import _ "github.com/egladman/magus/buzzengine".
magus.FindRoot(dir) walks up from dir to the workspace root, the same walk the CLI
does; an empty dir is an error, not the working directory.
| Call | What it answers |
|---|---|
(*Magus).JudgeCommand(ctx, session, dir, cmd), JudgeWrite(..., path) |
the guard's types.GuardJudgment: decision, Rule, Reason, Next; a deny is an answer, the error is a judgment that could not be made |
(*Magus).Run(ctx, targets, magus.WithSession(id)) |
judges each target first; a deny is a *types.GuardDeniedError and nothing runs |
WithEnv, WithStderr, WithCommandLine |
the process state a workspace reads; without each it reads no environment, discards notices, records an empty command line |
WithVCSDriver, WithCacheBackend (Open), WithObserver (Run) |
the three swappable parts, one option each |
std.MagusPutJob, MagusQuery, MagusRefs, MagusTrailRead, ... |
the Buzz host modules' Go doors, with typed options (types.JobPut, types.KnowledgeQueryOptions, ...); std is in the Go API lock |
The lease a call acts under rides ctx (types.WithJobLease). The SDK never retries,
re-grades or routes around a refusal.{{if .Full}} It has no agent loop: it answers what a loop asks
and never decides what runs next.{{end}}
The interface hierarchy: depend on the narrowest role
types.WorkspaceRepository embeds WorkspaceReader + TargetExpander + AffectedComputer + Inspector. *Magus (from Open) and the value Inspect returns
both satisfy all of it. Take the narrowest role a function uses{{if .Full}}.
types/repository.go says so:
"Prefer the narrowest embedded role a consumer actually uses."{{end}}
| Role | Methods | Answers |
|---|---|---|
WorkspaceReader |
Root, All, Get, Graph, VCSOptions, Where |
what projects exist, and the project dependency graph |
TargetExpander |
ExpandPath, ExpandAffected |
which concrete path:target pairs a target pattern names |
AffectedComputer |
Affected, AffectedFromPaths |
which projects a VCS changeset touches |
Inspector |
List*, Evaluate*, ClassifyFiles, TargetGraph, Workspace |
see the axis below |
A function that prints project names needs only WorkspaceReader{{if .Full}}. Widening its
parameter to WorkspaceRepository because Open returns that is the over-coupling
this hierarchy prevents: every future caller must construct or stub the whole
repository for a signature that only reads Root() and All(){{else}}. Widening it to
WorkspaceRepository makes every caller construct or stub the whole repository{{end}}.
The List / Evaluate / Classify axis
This is the SDK's organizing idea{{if .Full}}, stated in the Inspector doc comment in
types/repository.go{{end}}:
List*enumerates a DECLARATION: cheap, no resolution.Evaluate*RESOLVES one (spells bound, claims applied, charms patched in) and costs more.ClassifyFilesandTargetGraphare their own verbs; neither fits the split.
| Method | Reads | Cost |
|---|---|---|
ListProjects |
declared project facts (project-relative globs) | cheap |
ListTargets |
target name -> spell/project vocabulary | cheap |
ListCharms |
inverse charm index across every project | most expensive Inspector method (renders every charm x target x spell) |
EvaluateProjects |
resolved spells, workspace-rooted globs | resolves every project |
EvaluateTarget(ctx, t) |
full dispatch plan for one path:target |
resolves one target |
ClassifyFiles(ctx, paths) |
which project owns/declares each path | pure glob lookup, cheap even for a whole dirty tree |
TargetGraph(ctx) |
the ctx.needs DAG, read statically from magusfile source |
never executes a target body |
ProjectEntry.Sources and EvaluatedProject.Sources share a FIELD NAME with
DIFFERENT representations:
ListProjects: declared, project-relative globs.EvaluateProjects(through its embeddedProjectEntry): resolved, workspace-rooted globs, joined against the project path with magusfile globs folded in.
{{if .Full}}Reading one where the other was meant silently mismatches every glob it feeds. {{end}}The
field comment on ProjectEntry.Sources in types/describe.go is the one place this
is documented. Read it before writing code that consumes both.
ctx and cancellation
Every Inspector method takes ctx first and returns error last, the read-only
List* calls included. A cancelled walk returns an error, never a truncated slice.
{{if .Full}}- describeCancelled in describe.go names the walk and how far it got
("describe projects: cancelled after 2 of 8: context canceled").
{{end}}- The error wraps ctx.Err(), so errors.Is(err, context.Canceled) and
errors.Is(err, context.DeadlineExceeded) both hold.{{if .Full}}
- VERIFIED: cancelling before
ListProjectson a one-project workspace produces exactly that message, anderrors.Isreturns true.{{end}}
Never treat an empty or short List*/Evaluate* result as "the workspace has
nothing" without checking the error first.{{if .Full}} The reason is stated directly in
describe.go: a partial inventory reporting Count: 3 is indistinguishable from a
workspace that genuinely has three projects, so silently truncating would be a
wrong answer wearing a right answer's shape.{{else}} A partial inventory is
indistinguishable from a small one: a wrong answer wearing a right answer's shape.{{end}}
Two different graphs, easy to conflate
WorkspaceReader.Graph()returns*types.Graph: the PROJECT dependency graph (project -> project, fromdepends_on).Inspector.TargetGraph(ctx)returnstypes.TargetGraphOutput: the TARGET graph (target -> target, fromctx.needs), within and across projects.{{if .Full}} It is read statically from magusfile source and never runs a target body. It sees both arms of a runtime branch and reports a dependency cycle if one exists.{{else}} It is read statically, sees both arms of a branch, and reports cycles.{{end}}
Match the graph to the question's granularity{{if .Full}}. A caller doing scheduling or impact analysis usually holds both: Graph() for "does
project A depend on project B", TargetGraph() for "does target lint in project A
depend on target build in project B"{{end}}.
Sharp edges (verified, not folklore)
types.Target is dual-role. Know which role it plays before reading any field:
- As a work-unit,
Path/Name/Charms/Filesidentify whichpath:targetto run. - As a policy bag (
Project.TargetPoliciesvalues,EvaluatedTarget.Policy), onlySkipCache/Slots/Drift/RetryOnVolatilemean anything. Ignore the other 6 of its 10 fields{{if .Full}}; they sit unset (see the type's own disclaimer attypes/target.go:95-101){{end}}.
The Entry/Output/Report suffix split needs the ~20-line comment at the top
of types/describe.go. Most of it is a naming CONVENTION, not vocabulary. Read the comment
before you add a type{{if .Full}}; guess at the pattern instead and you misname it{{end}}.
Entryis added only when the bare name would collide with an existing type:ProjectEntrybecausetypes.Projectexists.Charmhas no suffix because nothing else claims that name.Outputmeans theInspectormethod itself returns this shape (ProjectsOutput,TargetGraphOutput).Reportmeans rebuilt at the render edge from a plain slice the method returned (FileReport,CharmReport).ListProjects/EvaluateProjectsare the deliberate exceptions: they return their*Outputtype directly{{if .Full}}, because they carry a realWorkspacefield a{definition, count, items}envelope cannot derive{{end}}.
Evaluating a magusfile needs the Buzz engine linked. A program that loads
magusfiles blank-imports github.com/egladman/magus/buzzengine, as cmd/magus does.
Without it, Open and Inspect fail with *types.EngineNotLinkedError (MGS1104) once
any project has a magusfile, and never return a workspace missing its targets. A
workspace with no magusfile opens without the engine.{{if .Full}}
TargetGraph reads magusfile source statically through the exported libs/gopherbuzz
parser, with or without the engine.{{end}}
The other path builds the workspace in Go: the exported Option/ProjectOption/
BindingOption wire API (register.go: WithSpell, WithTarget, WithDependsOn,
...). A target named with WithTarget counts as declared, as an export fun does.
Built-in spells (go, ts, rust, ...) are reachable this way{{if .Full}}: the
exported register.go functions call internal/spell on the caller's behalf{{end}}.
Testing code that uses the SDK
Test at the narrowest seam the code allows. Move to a real workspace only for what a fake cannot tell you.
- Unit tests take the narrowest role (
types.WorkspaceReaderand so on). Fake it by embedding the interface in a struct and overriding the one method the test calls. A test that asserts on the call generates its own mock: magus'stypesmocks are internal. - Integration tests use
testkit.NewWorkspace(t)(fromlibs/testkit) withmagus.Inspect. Inspect opens no cache. - Call
testkit.Main(m)fromTestMain, ortestkit.Isolate(t)in a serial test. TheOutput*lookups andPayloadByRefread the session store and other checkouts on a miss{{if .Full}}, so without isolation the result depends on the developer's machine{{end}}. - For
Run, declare a one-method interface and fake that.*magus.Magushas too many methods to fake whole. - Magusfile targets do not run outside this module, so a test that writes one and calls
Runproves nothing. Register a spell from Go withWithSpellinstead. - Register a programmatic spell with
register.Spell(tb, name, opts ...spells.Option)(libs/testkit/register), passingspells.WithTargetsand the like{{if .Full}}. The spell registry is process-global, so parallel tests that register spells race{{end}}.
Audit mode
Asked "does the SDK actually give me X", verify; never infer from naming:
- Is the type exported?
grep -n "^type <Name>" types/*.go *.go(or the package the docs claim owns it). A type used only insideinternal/is unreachable, whatever a doc comment implies. - Is the capability reachable without the CLI? Trace the call from the exported
entry point (
Open/Inspect/a method on*Magus) down through its imports. The capability stops where the trace crosses into aninternal/package nothing exported re-wraps{{if .Full}}.go-api-generatefails on an exported signature that names aninternal/type, so the locked packages cannot leak one{{end}}. - Is a boundary deliberate or accidental? Read the package doc comment first
(
doc.goor the top of the main file) before proposing a merge.- Deliberate:
spells/doc.godocuments thatspellsandinternal/spellwere collapsed from three packages to two on purpose{{if .Full}}.spellsis exported: the Buzz sources plusOp/Driver/Descriptor;internal/spellis the unexported bytecode decoder. It calls the dependency direction load-bearing (spellsimports nothing fromtypes;typesimportsspells) so the two cannot cycle. Merginginternal/spellintospellswould put bytecode framing behind the public API{{end}}. - Accidental: a package with no doc comment, one importer, and nothing it exports that must stay exported after a merge{{if .Full}}. That shape (see the {{skill "architecture-review"}} skill's table) is the accidental kind{{end}}.
- Deliberate:
Do not paper over a genuine gap with a workaround the reader did not ask for. If a capability is not reachable, say so plainly and name the exact package boundary that stops it{{if .Full}}, as specific as the MGS1104 refusal above{{end}}.