Loading...
Loading...
Synthesizes raw learnings and codebase analysis into an interlinked Markdown Knowledge Base (KB). Use at the beginning of a loop to build or update architecture.md, entities, and vulnerabilities. Don't use for generating threat models or formulating execution plans.
npx skill4agent add google/mantis mantis-architectureworkspace/learnings.jsonlworkspace/kb//mantis-architecture--snapshot_root=<dir>SNAPSHOT_ROOT--snapshot_id=<id>SNAPSHOT_ID--state_root=<dir>workspace/--target_root=<dir>snapshot_pinnedworkspace/learnings.jsonlworkspace/historical_learnings.jsonlworkspace/kb/workspace/.mantis_state.jsonworkspace/.mantis_state.jsonactive_snapshotrootsnapshot_idsnapshot_pinnedsnapshot_historygithgrepoworkspace/.mantis_state.jsonkb_snapshot_idSNAPSHOT_IDworkspace/.mantis_state.jsonchanged_fileschanged_files_statuschanged_files_passstate.pass_numberworkspace/kb/architecture.mdentities/[component_name].mdvulnerabilities/[CWE-ID].mdindex.mdworkspace/kb/dependencies.jsonmantis-plan{}workspace/learnings.jsonlworkspace/archive/learnings/learnings_pass_${N}_${X}.jsonl<!-- KB_SNAPSHOT: <SNAPSHOT_ID> -->index.mdentities/*.mdvulnerabilities/*.mdkb_snapshot_idSNAPSHOT_IDworkspace/.mantis_state.jsonworkspace/archive/kb/kb_pass_${N}_${X}/workspace/learnings.jsonlworkspace/learnings.jsonlKB_SNAPSHOTworkspace/archive/kb/kb_pass_${N}_${X_kb}/kb_snapshot_idworkspace/learnings.jsonlLOCATOR RESOLUTION (before reading ANY target code or artifact):
0. ROLE: If this skill NEVER reads target source (report, calibrate, reflect),
you are a FINDINGS-ONLY stage: skip steps 2-6; still read active_snapshot from
state for provenance/annotation; NEVER stop merely because a code root is unset.
1. Determine CODE_ROOT, in this priority order:
a. If --target_root is passed on THIS invocation, CODE_ROOT = --target_root.
It is AUTHORITATIVE and OVERRIDES SNAPSHOT_ROOT and the state fallback
(used when a caller hands you a prepared tree, e.g. a patched shadow).
b. Else if --snapshot_root (or SNAPSHOT_ROOT) is passed, use it.
c. Else read state_root/workspace/.mantis_state.json (state_root from
--state_root if passed, else ./workspace/... relative to the current dir)
-> active_snapshot.root / .snapshot_id / .snapshot_pinned.
d. Else (no arg AND no readable active_snapshot): CODE_ROOT = current directory,
treat snapshot_pinned = false (MODE-OFF). Do NOT stop.
2. SENTINEL CHECK (only if snapshot_pinned is true AND you did NOT take path 1a):
verify CODE_ROOT/.mantis_snapshot_id exists and equals SNAPSHOT_ID. If missing
or different -> STOP "snapshot sentinel mismatch". (A --target_root tree (1a) is
deliberately mutated and is sentinel-EXEMPT.)
3. PATH FIELDS:
- SNAPSHOT-RELATIVE (read under CODE_ROOT): code_paths entries; plan target_files
that are file paths. Strip ONLY a trailing ":<digits>". A code_paths entry
containing "://" is a URL/endpoint, NOT a file read. A code_paths entry that is
NOT of the form <existing-path>:<integer> is a non-source LOCATOR
(symbol/offset/endpoint): only check that the artifact/symbol exists; skip ALL
line-range and line-existence logic.
- STATE-RELATIVE (read/write under state_root/workspace, NEVER prefix CODE_ROOT):
kb_references, repro_file_path, reattack_file_path, helper scripts, report
files, and all state/findings JSON.
4. Never WRITE under CODE_ROOT when snapshot_pinned is true. Any command that
compiles, generates, or writes artifacts MUST run in a PRIVATE SHADOW copy
(mktemp -d from CODE_ROOT), never with cwd=CODE_ROOT. Read-only inspection may
cd into CODE_ROOT.
5. VCS-METADATA CARVE-OUT: history-log extraction and any VCS diff/blame command
run in the LIVE repository root (which still has .git/.hg/.repo), NOT CODE_ROOT
(the snapshot copy strips VCS metadata). Do NOT stop merely because CODE_ROOT
lacks .git/.hg/.repo.
6. Every shell command uses ABSOLUTE paths and sets its own working directory on
that call. Do NOT assume the working directory persists between calls.CODE_ROOTSNAPSHOT_IDsnapshot_pinnedworkspace/kb/...state_root/workspaceCODE_ROOTCODE_ROOT- `CUR` = `SNAPSHOT_ID` (resolved by Block A; the empty string if
`active_snapshot` was absent). `PINNED` = `snapshot_pinned` (false if absent).
- **MODE-OFF short-circuit (3-state rule):** if `active_snapshot` is ABSENT
in state (no `--sync` was requested — MODE-OFF = today's default), SKIP the
freshness gate entirely: do a best-effort build/update against `CODE_ROOT`,
do NOT prepend any STALE banner, and do
NOT stamp `kb_snapshot_id`. This is byte-for-byte today's behavior. (Only
HALT and PINNED run the gate below.)
- `KB_ID` = the `kb_snapshot_id` value in `.mantis_state.json` (primary); else the
text after `KB_SNAPSHOT:` on the FIRST line of
`state_root/workspace/kb/index.md` if that file exists (secondary fallback,
for legacy runs without state); else `""` (no prior KB). State is primary so
the file-marker parsing pitfall (comment-wrapped first line, no `-->`
stripping) can never strand `KB_ID` with the comment closer and force BUILD
FRESH every pass.
- Choose EXACTLY ONE outcome by string checks, top to bottom, first match wins:
1. `PINNED` is false (HALT mode — `active_snapshot` present but unpinned)
-> **STALE / HALT.** Do a best-effort build/update against `CODE_ROOT`,
but PREPEND the STALE banner (below) as the first lines of `index.md`.
Do NOT claim currency: leave the
banner in place. Set `kb_snapshot_id` = `CUR` (a `live:` id).
2. Else `KB_ID` == `CUR` (both non-empty) -> **CURRENT.** Do the incremental
update + decay-check (step 4) as today. Re-stamp `KB_SNAPSHOT: CUR` on
every (re)written file. REMOVE any STALE banner previously prepended to
`index.md`.
3. Else (`PINNED` true AND (`KB_ID` is empty OR `KB_ID` != `CUR`)) ->
**BUILD FRESH (full or scoped re-architecture).** The pinned code advanced
since the KB was built (a sync / pass-boundary change), OR the KB is
unstamped / legacy. Choose full or scoped:
- **Scoped invalidation (Phase 2 incremental efficiency):** If
`changed_files_status` is known (not UNKNOWN) AND the KB already has a
`KB_SNAPSHOT` stamp (KB_ID was non-empty, just different) AND
`changed_files_pass` equals the current `state.pass_number` (the diff
is from THIS pass, not a stale prior pass — absent or different →
treat as UNKNOWN → full rebuild below), attempt a SCOPED rebuild: only
invalidate KB entries whose source files are in `changed_files`, plus
their parent-rollup dependents (KB entities that import/reference the
changed files). Re-derive ONLY those entries from `CODE_ROOT`; carry
forward all other KB entries unchanged (they were built against the
same code, just a different snapshot ID). Re-stamp `KB_SNAPSHOT: CUR`
on every (re)written file.
- **Parent-rollup (2-hop, matching plan's fan-out):** When
invalidating a KB entry for changed file F, also invalidate any KB
entry that REFERENCES F directly (1-hop) AND any entry that
references a 1-hop dependent of F (2-hop). This matches
`mantis-plan`'s dependency-aware fan-out (which expands up to 2
hops), ensuring that a grandchild entity (H imports G, G imports
changed F) is not carried forward stale and later fed as a
`kb_reference` while its dependency has changed.
- **Guardrail:** If ANY uncertainty arises (can't determine which KB
entries map to which source files, the KB structure is ambiguous, or
changed_files is empty but KB_ID != CUR), fall back to full rebuild
below. Never carry forward a stale entry for a changed file.
- **Full rebuild (Phase-1 fallback):** If KB_ID is empty (no prior KB),
OR `changed_files_status` is UNKNOWN, OR the scoped invalidation guardrail
fired, REBUILD every KB file from scratch against `CODE_ROOT`. Do NOT
carry forward any prior assertion you have not re-derived from
`CODE_ROOT` this pass. Stamp `KB_SNAPSHOT: CUR`. REMOVE any STALE banner.
- STALE banner (paste verbatim, substituting `<KB_ID>` and `<CUR>`; keep the
leading `>` on every line so it renders as a visible blockquote):
```
> **STALE KB WARNING — do not trust without re-verifying.**
> snapshot_pinned=false, or the KB was built against a different snapshot.
> KB_SNAPSHOT=<KB_ID> does not match active_snapshot.snapshot_id=<CUR>.
> Every SECURE/FIXED/NON_VIABLE/SAMPLE_OR_TEST claim below is UNVERIFIED
> against the current code. Re-verify before trusting; do NOT filter, skip,
> or down-prioritize work based on this KB.
```workspace/learnings.jsonlworkspace/historical_learnings.jsonlworkspace/learnings.jsonlworkspace/historical_learnings.jsonlCODE_ROOTworkspace/kb/workspace/kb/architecture.mdworkspace/kb/entities/[component_name].mdauth_module.mdworkspace/kb/vulnerabilities/[CWE-ID_or_BugClass].mdCWE-79.mdMemory-Corruption.mdworkspace/kb/index.md[Auth Module](entities/auth_module.md)index.mdentities/*.mdvulnerabilities/*.md<!-- KB_SNAPSHOT: <SNAPSHOT_ID> -->CURactive_snapshot--syncKB_SNAPSHOTCURarch:145active_snapshot<!-- KB_SNAPSHOT: -->arch:147-152KB_SNAPSHOT: CUR:164:178:195KB_SNAPSHOT(AS_OF:<snapshot>)AS_OFkb_snapshot_idSNAPSHOT_IDdiscovery_commitmantis-criticworkspace/kb/entities/CODE_ROOTCODE_ROOTKB_SNAPSHOT<!-- KB_SNAPSHOT: <SNAPSHOT_ID> -->SNAPSHOT_IDworkspace/learnings.jsonlmkdir -p workspace/archive/learnings/N"pass_number"workspace/.mantis_state.jsonworkspace/archive/findings_pass_NloopN_findingsNmax_found + 1Xlearnings_pass_${N}_*.jsonlworkspace/archive/learnings/mv workspace/learnings.jsonl workspace/archive/learnings/learnings_pass_${N}_${X}.jsonlmkdir -p workspace/archive/kb/X_kbkb_pass_${N}_*workspace/archive/kb/workspace/kb/cp -a workspace/kb/. workspace/archive/kb/kb_pass_${N}_${X_kb}/kb_snapshot_idSNAPSHOT_IDCURworkspace/.mantis_state.jsonactive_snapshotkb_snapshot_idkb_snapshot_idCURindex.mdworkspace/learnings.jsonlworkspace/learnings.jsonlKB_SNAPSHOTkb_snapshot_id