Loading...
Loading...
Generates minimal security fixes using transactional isolation (shadow directories or file backups), applies patches, and verifies them. Use when security findings are successfully reproduced and need patches applied and verified. Don't use for initial vulnerability research or reproduction payload generation.
npx skill4agent add google/mantis mantis-patch/mantis-patch [--target_root=<path>] [--state_root=<path>] [--snapshot_root=<path>] [--snapshot_id=<id>] [--snapshot_pinned=<true|false>]--target_root--snapshot_root--target_root--snapshot_root--snapshot_id--snapshot_pinnedtruefalse--state_rootworkspace/workspace/...<state_root>/workspace/...workspace/findings/patch_status"VERIFIED_SECURE""MITIGATION_PROPOSED"workspace/.mantis_state.jsonworkspace/.mantis_state.jsonactive_snapshotrootsnapshot_idsnapshot_pinned--snapshot_*repro_file_pathrun_command"patch_status""patch_diff""reattack_status""reattack_variants"workspace/learnings.jsonlworkspace/helpers/append_patch.pyworkspace/findings/patch_status"VERIFIED_SECURE""MITIGATION_PROPOSED"patch_base_snapshotpatch_base_snapshotactive_snapshot--snapshot_*patch_statustarget.c.bak-[id]try...finallyappend_patch.pyLOCATOR 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.[!NOTE] CURRENT-PASS CHECK (defensive; the binding guarantee is on the harness perScenario 2): ifmantis-pipeline-adapteris present ANDactive_snapshot, treat the snapshot as STALE for this pass — STOP "stale active_snapshot: pass mismatch" or degrade as HALT (active_snapshot.pass != state.pass_numbereffectively false: no authoritative verdicts, Block B NOT_MATCHED, reproducesnapshot_pinned). This catches a custom harness that preservednot_attemptedacross the Stage 15 pass increment without re-pinning. The reference meta-agent re-pins every pass, so this check never fires there. Block B itself cannot detect this (it isactive_snapshot-only, notsnapshot_id-aware).pass
active_snapshot--snapshot_root--snapshot_id--target_rootsnapshot_pinned == truesnapshot_pinned == falselive:SNAPSHOT MATCH CHECK for finding F (decides MATCHED vs NOT_MATCHED):
1. If snapshot_pinned is false -> NOT_MATCHED. Stop.
2. Read F.discovery_commit:
- missing OR empty OR the literal "MIXED" -> NOT_MATCHED.
- not exactly equal to SNAPSHOT_ID -> NOT_MATCHED.
- exactly equal to SNAPSHOT_ID -> MATCHED.
There is no other route to MATCHED; never fuzzy-compare. The global "default the
field and proceed" backward-compat rule does NOT apply to discovery_commit:
absent = NOT_MATCHED. (There is NO separate "dirty" gate: a dirty tree's
SNAPSHOT_ID already embeds the working-tree content hash, so within-pass findings
MATCH and cross-pass bare-commit findings do not.)patch_base_snapshotworkspace/findings/patch_status"VERIFIED_SECURE""MITIGATION_PROPOSED"patch_base_snapshotpatch_base_snapshotpatch_statusrepro_status"reproduced""Exploit Chain:""chainer""constituent_findings"patch_diff"patch_status""MITIGATION_PROPOSED""Exploit Chain:""constituent_findings""constituent_findings"workspace/findings/<uuid>.json"status""status""FALSE_POSITIVE""status""FALSE_POSITIVE""status""DUPLICATE""duplicate_of"<uuid>.jsonworkspace/findings/<uuid>.jsonworkspace/findings/.trash/<uuid>.jsonworkspace/archive/findings_pass_*/<uuid>.jsonworkspace/archive/loop*_findings/<uuid>.json"status""DUPLICATE""duplicate_of""status""patch_status""DUPLICATE""patch_status""ERROR""patch_status""patch_status"patch_base_snapshot"patch_status""patch_status""ERROR""patch_status""ERROR""patch_status""VERIFICATION_FAILED""patch_status""VERIFICATION_FAILED"{"VERIFIED_SECURE", "MITIGATION_PROPOSED", "VERIFICATION_INCOMPLETE"}patch_base_snapshot"MITIGATION_PROPOSED""patch_status""MITIGATION_PROPOSED""MITIGATION_PROPOSED""VERIFICATION_INCOMPLETE""patch_status""VERIFICATION_INCOMPLETE""VERIFIED_SECURE""patch_status""VERIFIED_SECURE"git branchgit checkoutgit stash"patch"patch_base_snapshotPINNED mode AND"patch"patch_base_snapshottry...finally/tmpmktemp -dtempfile.mkdtemp()/workspace/Cwdrun_command/workspace/state_root/workspace//workspace/state_root/workspace/mantis-reproducerun_commandreattack_run_command{TARGET_ROOT}run_commandreattack_run_command{TARGET_ROOT}mktemp -d{TARGET_ROOT}workspace/.workspace_edit.lockfcntl.flockfcntl.LOCK_EX*.bak-[current_finding_id]cp target.c target.c.bak-[finding_id]diff -u --label target.c --label target.c target.c.bak-[finding_id] target.cbaseourstheirsgit merge-filediff3<state_root>/.mantis_snapshots/signaturepatch_diffpatch_base_snapshotSNAPSHOT_IDdiscovery_commit== SNAPSHOT_IDdiscovery_commitpatch_base_snapshotchanged_filessignature--snapshot_keepchanged_fileschanged_files_statusworkspace/.mantis_state.jsonactive_snapshotpatch-rebased-from: pass_<N-1>--reattack@mantis-reproduce --reattackreattack_status = "inconclusive_baseline_changed"failed_to_bypasssnapshot_pinned=falselive:@mantis-reproduce --reattack"repro_file_path""run_command"CwdREACHED-SINK EVIDENCE GATE (mechanical):
Each reproducer produces REACHED-SINK EVIDENCE via ONE channel, recorded in repro_hints:
(a) script/source harness -> write the exact bytes MANTIS_REACHED_ENTRYPOINT to a
sidecar file $SENTINEL_FILE and flush+fsync (or unbuffered write) BEFORE
invoking the sink. (A file survives a crash that truncates buffered stdout.)
(b) binary / firmware / raw-payload -> reached-sink evidence is a captured
crash backtrace or sanitizer frame (ASan/UBSan/MSan/TSan) that
explicitly names the target sink function (target-produced tracing). A
marker written by a wrapper you author BEFORE invoking the target is
SETUP EVIDENCE ONLY: it proves "launch attempted," not "sink reached,"
and does NOT qualify as reached-sink evidence. If no in-path marker
(channel a) and no target-produced backtrace/sanitizer trace
(channel b) is achievable, the sink is unreached.
EVIDENCE PRESENT (reached-sink) = (channel a) sidecar file contains
MANTIS_REACHED_ENTRYPOINT written in-path, OR (channel b) target-produced
backtrace/sanitizer output names the sink. A wrapper pre-launch marker alone is
NOT evidence present.
EVIDENCE ABSENT includes: any compiler/build nonzero exit; exit 127 (command not
found); exit 2 with a "No such file" message.
DECISION GATE (gate the DECISION, not specific verdict strings):
- Record repro_status = reproduced OR statically_confirmed ONLY if EVIDENCE is
PRESENT. If ABSENT -> repro_status = not_attempted (retry-eligible), STOP.
- In patch verification, EVIDENCE is required on the UNPATCHED baseline (Block G),
NOT on the post-patch attack run (a correct patch legitimately stops the input
before the sink).
- If NO evidence channel is achievable for this target, downgrade to
not_attempted / VERIFICATION_INCOMPLETE. NEVER synthesize the marker.VERIFIED_SECUREUNPATCHED-BASELINE + POST-PATCH GATE (before any VERIFIED_SECURE):
0. APPLICABILITY: applies ONLY when repro_status == reproduced (a runnable PoC
exists). If repro_status == statically_confirmed (no runnable PoC),
VERIFIED_SECURE is NOT applicable -> record MITIGATION_PROPOSED (NEVER ERROR).
1. UNPATCHED BASELINE: run the reproducer against a FRESH UNPATCHED copy of
SNAPSHOT_ROOT using Block F. If evidence ABSENT or the bug does NOT trigger ->
patch_status = ERROR ("reproducer_invalid_on_current_snapshot"); do NOT set
VERIFIED_SECURE.
2. POST-PATCH INTEGRITY (distinguish "blocked by patch" from "build break"):
- REQUIRED: the patched build must COMPILE with exit 0. Nonzero ->
VERIFICATION_INCOMPLETE.
- REQUIRED (benign control): run a legitimate BENIGN (non-attack) input through
the patched build; it MUST reach the sink path (EVIDENCE PRESENT via Block F)
with no crash. This proves the patch narrowed the bug rather than breaking the
harness/entrypoint. If NO benign control can be constructed for this target,
you may NOT claim VERIFIED_SECURE on a clean-run-only basis -> land
VERIFICATION_INCOMPLETE and record why the benign control was infeasible.
(Strict mode: a clean compile + clean run WITHOUT a benign control reaching
the sink is never sufficient for VERIFIED_SECURE.)
3. ATTACK ON PATCH: run the attack input through the patched build. VERIFIED_SECURE
requires ALL of: step 1 triggered on unpatched (evidence present), step 2
integrity holds, step 3 attack does NOT crash/trigger, AND the `--reattack` run
also fails to bypass. Otherwise VERIFICATION_FAILED.VERIFIED_SECURErepro_hintslive:patch_status"VERIFICATION_INCOMPLETE""VERIFIED_SECURE"repro_file_pathcode_pathspatch_status"ERROR""VERIFIED_SECURE"exit 127patch_status"VERIFIED_SECURE"@mantis-reproduce --reattackreattack_status = "failed_to_bypass"reattack_variantsVERIFICATION_INCOMPLETEVERIFIED_SECURE"VERIFICATION_INCOMPLETE""VERIFIED_SECURE"patch_status"ERROR""reproducer_invalid_on_current_snapshot""VERIFIED_SECURE"repro_status == statically_confirmed"MITIGATION_PROPOSED"@mantis-reproduce --reattack --finding_id=[finding_id]@mantis-reproduce--reattack--target_root=<PATCHED_SHADOW_ROOT>--target_root--snapshot_pinned=false--state_root=<state_root>reattack_*state_root/workspace/--finding_id=[finding_id]--reattack.--finding_id=[finding_id]--reattackreattack_statusreattack_file_pathreattack_run_commandreattack_outputstate_root/workspace/findings/repro_*"patch_status""VERIFICATION_INCOMPLETE"reattack_statusSNAPSHOT_MISMATCHsetup_failedpatch_status"VERIFICATION_INCOMPLETE"VERIFIED_SECURESNAPSHOT_MISMATCHreattack_status"inconclusive_baseline_changed"patch_status"VERIFICATION_INCOMPLETE""ERROR""reproducer_invalid_on_current_snapshot""VERIFIED_SECURE"VERIFIED_SECURE"patch_diff"diff -u --label target.c --label target.c target.c.bak-[finding_id] target.c"patch_diff"rm -rf <shadow_directory>cp target.c.bak-[finding_id] target.crm target.c.bak-[finding_id]workspace/reproducers/workspace/helpers/workspace/learnings.jsonlworkspace/learnings.jsonl/mantis-plan{"title": "[security_flaw_title]", "code_paths": ["[path1:line1]"], "status": "[VERIFIED_SECURE / MITIGATION_PROPOSED / VERIFICATION_INCOMPLETE / VERIFICATION_FAILED / ERROR]", "patch_base_snapshot": "[current SNAPSHOT_ID, or omit in LEGACY mode]", "snapshot": "[current SNAPSHOT_ID, or omit in MODE-OFF]"}state_root/workspace/helpers/append_patch.py# MANTIS_HELPER_VERSION = 2MANTIS_HELPER_VERSION = 2patch_base_snapshot"patch_status""VERIFIED_SECURE""MITIGATION_PROPOSED""VERIFICATION_INCOMPLETE""VERIFICATION_FAILED""ERROR""patch_base_snapshot""patch_diff""reattack_status""reattack_file_path""reattack_run_command""reattack_output""reattack_variants""reattack_variants"{"description": "...", "triggered": true/false}"history"{
"stage": "patch",
"action": "patched",
"details": "Patch status evaluated as [VERIFIED_SECURE/MITIGATION_PROPOSED/VERIFICATION_INCOMPLETE/VERIFICATION_FAILED/ERROR] on snapshot [patch_base_snapshot or 'legacy']",
"pass_number": <current_pass_number>,
"timestamp": "<current_iso8601_timestamp>"
}