@fish-under-sea/magpie-purge 0.8.4 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.en.md ADDED
@@ -0,0 +1,171 @@
1
+ <div align="center">
2
+
3
+ <img src="page/card.png" alt="magpie-purge" width="96">
4
+
5
+ # magpie-purge
6
+
7
+ <p><a href="README.md">简体中文</a> · <a href="README.en.md"><b>English</b></a></p>
8
+
9
+ <p>
10
+ <img alt="Status" src="https://img.shields.io/badge/Status-Maintained%20%7C%20%E5%B8%B8%E8%A7%84%E7%BB%B4%E6%8A%A4-1f6feb?style=flat-square">
11
+ <a href="https://www.npmjs.com/package/@fish-under-sea/magpie-purge"><img alt="npm" src="https://img.shields.io/npm/v/%40fish-under-sea%2Fmagpie-purge?label=npm&color=4f8cff&style=flat-square"></a>
12
+ <a href="LICENSE"><img alt="License" src="https://img.shields.io/badge/License-MIT-2ea44f?style=flat-square"></a>
13
+ <img alt="Language" src="https://img.shields.io/badge/JavaScript-ESM-f7df1e?style=flat-square">
14
+ </p>
15
+
16
+ <p><b>A magpie page plugin and gateway hook turning your local purge folder into a managed persona & skill source—inventory, validation, deduplication, rollback, and mode-based system prompt rewriting.</b></p>
17
+
18
+ </div>
19
+
20
+ > [!NOTE]
21
+ > **Maintenance Status: Maintained** — Actively maintained for upstream compatibility and key improvements. For flagship projects, visit [Fish-under-sea](https://github.com/Fish-under-sea).
22
+
23
+ This is a single-package **page plugin + gateway hook** for magpie—not a FISH Dock plugin, and it requires no host patches.
24
+
25
+ | Field | Details |
26
+ | --- | --- |
27
+ | npm | [`@fish-under-sea/magpie-purge`](https://www.npmjs.com/package/@fish-under-sea/magpie-purge) |
28
+ | Repository | <https://github.com/Fish-under-sea/magpie-purge> |
29
+ | Sidebar Title | 破甲&注入 (`magpie.page.titles`) |
30
+ | Current Version | `0.9.1` |
31
+ | License | MIT |
32
+
33
+ ## ✨ Features & What It Solves
34
+
35
+ Upstream `agent-purge`'s magpie plugin only reads personas from `~/.agent-purge/`, and its page cannot read disk directly. This plugin completes the chain in three ways:
36
+
37
+ 1. **Two explicit content sources only**: **22 bundled skills + 1 base persona** shipped with the package, plus files you explicitly **import**—no silent scanning of arbitrary disk folders.
38
+ 2. **Four recognized artifact types**: DSH session export archives (`dsh-session-archive`), DSH Cordis plugin packages, persona/skill Markdown files, and plain text / JSON manifests—malformed packages are rejected with clear reasons without partial writes.
39
+ 3. **Full page workbench**: Entry list, inspector (with raw text viewer), rewrite mode switcher, enable/disable toggles, version rollback, validation log, last-applied status, and native support for magpie 2.4.0+ Liquid Glass appearance.
40
+
41
+ ### Differences from Upstream
42
+
43
+ | Item | `agent-purge` (`magpie-plugin/`) | `magpie-purge` |
44
+ | --- | --- | --- |
45
+ | Sources | Reads `~/.agent-purge/` only | Bundled skill pack + `~/.magpie/purge/imports` (explicit imports only) |
46
+ | Type detection | Persona Markdown only | Session archive / DSH plugin / Persona / Skill / Text / Manifest |
47
+ | Injection | Single persona | Persona + explicitly enabled skills (including 22 built-in skills) |
48
+ | Page UI | Persona picker + import | Entry list + inspector + mode + rollback + validation logs |
49
+ | Snapshot | Static `personas.js` generated via script | Backend scans and writes `catalog.js`; page never reads disk |
50
+ | Versioning | None | Same `id` with new digest creates a new version (keeps 2 for rollback) |
51
+ | `agent-purge` integration | Reads `state.json` (mode read-only in UI) | Off by default; opt in via `useAgentPurge: true` |
52
+ | Visibility | None | Top status bar states whether injection is active, who is injected, and why |
53
+
54
+ ## 🚀 Quick Install
55
+
56
+ ```bash
57
+ # Install from npm
58
+ magpie plugin add @fish-under-sea/magpie-purge
59
+
60
+ # Or install from a local clone
61
+ git clone https://github.com/Fish-under-sea/magpie-purge
62
+ magpie plugin add "./magpie-purge"
63
+ magpie plugin list
64
+ ```
65
+
66
+ After installation, a new sidebar page (**破甲&注入**) appears in magpie. On first open, the backend inventories bundled assets and imported files and emits three artifacts:
67
+
68
+ ### Output Artifacts
69
+
70
+ | Artifact | Location | Purpose |
71
+ | --- | --- | --- |
72
+ | `catalog.json`, `imports/`, `last.json` | `~/.magpie/purge/` | User data—kept outside the package directory so npm upgrades never wipe imports |
73
+ | `page/catalog.js` | Package `page/` | **Metadata-only** snapshot loaded via `<script src>` |
74
+ | `page/catalog.json` | Package `page/` | **Full snapshot** (with body), fetched via `page/hostread.js` |
75
+
76
+ > Legacy `state/` directories inside the package folder are migrated automatically on first scan without overwriting existing files. Set `PURGE_STATE_DIR` to override `~/.magpie/purge` for multi-instance or test runs.
77
+
78
+ ## 📖 Usage & Architecture
79
+
80
+ ### Bundled Skill Pack & Base Persona
81
+
82
+ Ready out of the box: `skills/` ships **22 verbatim red-team skills** (imported from `dsh-purge`'s `skills/redteam/`), and `personas/` ships **1 base persona** (`大肥鱼`, weakened-boundary edition). Built-in entries carry a Built-in badge—they can be toggled on/off but cannot be deleted.
83
+
84
+ - **Click-to-toggle**: Click any row in the left list to toggle a skill or select/deselect a persona; click **View** on the right to inspect details and raw body.
85
+ - **Base persona (`default: true`)**: Acts as the default fallback when no persona is selected, defining tone, evidence standards, and how attached skills compose—while keeping safety boundaries intact (refusing illegal/harmful requests rather than acting as an unconditional executor).
86
+ - **Known difference**: The 22 verbatim skills reference 137 `redteam_*` tool calls originally registered by `dsh-purge`; magpie does not provide those tools. Texts are kept verbatim by design.
87
+
88
+ ### Configuration (`magpie.options`)
89
+
90
+ Stored in magpie's `plugins.json` and editable from the plugin page:
91
+
92
+ | Option | Description |
93
+ | --- | --- |
94
+ | `mode` | `strict` / `replace` / `append`; **empty means no injection** (equivalent to `off`) |
95
+ | `persona` | Persona `id`; empty falls back to `default: true` base persona, then first enabled persona |
96
+ | `agents` | Restrict injection to specific magpie agents (e.g. `["claude","codex"]`); empty array = all |
97
+ | `enabled` | `{ [id]: boolean }` — skills require explicit `true`; personas default to enabled |
98
+ | `pinned` | `{ [id]: digest }` — pin an entry to a specific historical version |
99
+ | `imports` | Import requests from UI (`{op, path, at}`), copied into `~/.magpie/purge/imports` |
100
+ | `inline` | Inline persona/skill bodies pasted in UI (not written to disk) |
101
+ | `gatewayTimeoutMs` | Hook timeout budget, default `1500` ms (magpie range `100–10000`) |
102
+ | `useAgentPurge` | Default `false`. Set `true` to read `~/.agent-purge/state.json` |
103
+ | `stripThink` | Default `true`: strip leading `<think>…</think>` blocks from responses (both buffered and streaming) |
104
+ | `refusalWatch` | Default `true`: conservatively log short empty refusals (`[purge] refusal opening`) without altering replies |
105
+ | `routing` | Targeted routing rules (`active` / `hubInject` / `agents` / `targets`) by Agent, routing group (`groupId`), provider, or model |
106
+
107
+ ```bash
108
+ # Opt in to agent-purge state or toggle response-side hooks
109
+ magpie plugin options magpie-purge '{"useAgentPurge":true}'
110
+ magpie plugin options magpie-purge '{"stripThink":false}'
111
+ magpie plugin options magpie-purge '{"refusalWatch":false}'
112
+ ```
113
+
114
+ **Rewrite modes** (aligned with upstream):
115
+ - **Off** (`mode` empty): passes requests untouched.
116
+ - **`strict`**: replaces the controllable system prompt with the persona (also removes tool instruction text inside the original system block).
117
+ - **`replace`**: prepends the persona and strips lines in the host system prompt that both self-identify and name a model/vendor.
118
+ - **`append`**: prepends the persona while preserving the original host system prompt.
119
+
120
+ ### UI, Response Hooks & Boundaries
121
+
122
+ - **Two-step import & status bar**: Top status bar shows live injection state and a one-click next action; imports use a two-step Select → Import flow to prevent accidental writes.
123
+ - **Targeted routing, Claude Desktop aliases & `auto/<model>` groups**:
124
+ - Configure targeted rules by Agent, **routing group (`groupId`)**, provider, or model in the secondary Appearance & Options view (`options.routing.targets`). Targets with `groupId` are strictly scoped to matching group requests and never leak to direct non-group traffic.
125
+ - **Claude Desktop / Claude Code alias resolution**: Under Claude Desktop, magpie hashes routing group refs into 10-digit aliases (`mythos-magpie-<n>`, `anthropic/magpie-<n>`, `claude-magpie-<n>`, `magpie-<n>.anthropic.*`, with optional `[1m]` suffix across the base entry and 4 tiers: `opus / sonnet / haiku / fable`). `lib/routing.mjs` resolves these aliases back to configured `groupId` values automatically using FNV-1a 64 `% 1e10`, and will prefer upstream `ctx.group` once [yetone/magpie#9222](https://github.com/yetone/magpie/issues/9222) lands.
126
+ - **Manual groups vs. auto-discovered groups**: Manual groups use canonical ref `group/<id>` (enter `<id>` or `group/<id>`); auto-discovered groups use `auto/<model>` and hash differently for Desktop aliases, so enter `auto/<model>` explicitly (e.g. `auto/deepseek-v3`).
127
+ - **Response-side hooks**: `onResponse` / `onEvent` strip leading `<think>` blocks (with chunk-boundary buffering) and log empty refusal openings—never retrying or rewriting reply content, always fail-open.
128
+ - **Why bodies are split out**: Inlining ~130 KB of red-team manuals into `page/catalog.js` previously triggered Windows Defender (`Trojan:Script/Wacatac.C!ml`). Separating bodies and loading them via `page/hostread.js` (direct `fetch` → host window `asset` bridge under `connect-src 'none'` CSP) eliminates false positives.
129
+ - **Liquid Glass (magpie 2.4.0+)**: `page/glass.js` consumes host four-region glass metrics (`sidebar / content / overlay / control`) and official `--magpie-*` tokens, degrading gracefully when `backdrop-filter` is unavailable or reduced transparency is enabled.
130
+ - **Boundaries**: Only processes traffic passing through the magpie gateway; the page never touches disk; imported archives are never executed; bad archives are rejected atomically.
131
+
132
+ ## 🛠️ Development, Preview & Structure
133
+
134
+ ```text
135
+ magpie-purge/
136
+ ├── package.json Plugin manifest: magpie.hooks / magpie.middleware / magpie.page / magpie.options
137
+ ├── hooks.js Gateway hooks: inventory + rewrite + response hooks (Bun side)
138
+ ├── middleware.js Two-stage provider/model targeted routing middleware
139
+ ├── skills/ 22 bundled skills (.md)
140
+ ├── personas/ Bundled base persona (大肥鱼)
141
+ ├── lib/ zip / inventory / rewrite / agentpurge / plan / bundled / state
142
+ ├── page/ Plugin UI (index.html / app.js / style.css / glass.js / plan.js / hostread.js / icon-lock.svg / card.png)
143
+ ├── preview/ Local preview: mock host + demo snapshots + standalone purge-preview.html
144
+ ├── tools/ scan.mjs / build-preview.mjs / card-icon.mjs
145
+ └── tests/ node --test suite
146
+ ```
147
+
148
+ ```bash
149
+ # 1) Standalone HTML preview (supports #theme=dark|light, #glass=off|lite|balanced|clear, #snap=ready|blocked)
150
+ node tools/build-preview.mjs # → preview/purge-preview.html
151
+
152
+ # 2) Live-reload preview server (use ?watch=0 for headless screenshots; --csp=magpie to test host CSP)
153
+ npm run preview:serve # → http://127.0.0.1:55172
154
+
155
+ # 3) Run tests (--test-isolation=none avoids Windows sandbox spawn EPERM)
156
+ node --test --test-isolation=none "tests/*.test.mjs"
157
+
158
+ # 4) Offline inventory scan
159
+ node tools/scan.mjs --import "D:\purge\my-file.md"
160
+ node tools/scan.mjs --json
161
+ ```
162
+
163
+ > **Two icons**: `magpie.icon` is for the plugin card (must be `https://` or `data:image/`, inlined from `page/card.png` via `tools/card-icon.mjs`); `magpie.page.icon` is for the sidebar & tab (`page/icon-lock.svg`).
164
+
165
+ ## 🤝 Credits & License
166
+
167
+ - **[ZiChuanLan/agent-purge](https://github.com/ZiChuanLan/agent-purge)** (by **ZiChuanLan**, MIT) — Origin of the four protocol targets, `off`/`strict`/`replace`/`append` semantics, Anthropic block preservation, and `stripIdentity` strategy.
168
+ - **[yetone/magpie](https://github.com/yetone/magpie)** — Host contract for page plugins and async gateway hooks.
169
+ - **Local `dsh-purge` red-team skill library** — Source of the 22 verbatim skills in `skills/`.
170
+
171
+ Licensed under **MIT**. See [`LICENSE`](LICENSE) and [`THIRD_PARTY_NOTICES.md`](THIRD_PARTY_NOTICES.md) for full attribution and upstream license text.