wyrm-mcp 7.2.1 → 7.2.2

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.
Files changed (150) hide show
  1. package/LICENSE +26 -667
  2. package/NOTICE +14 -33
  3. package/dist/activation.js +1 -60
  4. package/dist/agent-daemon.js +4 -281
  5. package/dist/agent-loop.js +7 -332
  6. package/dist/analytics.js +13 -236
  7. package/dist/attribution.js +1 -49
  8. package/dist/audit.js +2 -457
  9. package/dist/auto-capture.js +3 -138
  10. package/dist/auto-orchestrator.js +1 -325
  11. package/dist/autoconfig.js +39 -840
  12. package/dist/buddy-runner.js +1 -109
  13. package/dist/buddy.js +14 -564
  14. package/dist/build-flags.js +1 -17
  15. package/dist/capabilities.js +3 -183
  16. package/dist/capture.js +1 -56
  17. package/dist/causality.js +6 -107
  18. package/dist/cli.js +20 -281
  19. package/dist/cloud/cli.js +5 -541
  20. package/dist/cloud/client.js +1 -221
  21. package/dist/cloud/crypto.js +1 -85
  22. package/dist/cloud/machine-id.js +2 -113
  23. package/dist/cloud/recovery.js +1 -60
  24. package/dist/cloud/sync-engine.js +7 -543
  25. package/dist/cloud-backup.js +5 -579
  26. package/dist/cloud-profile.js +1 -138
  27. package/dist/cloud-sync-entrypoint.js +1 -47
  28. package/dist/cloud-sync.js +2 -309
  29. package/dist/constellation.js +12 -168
  30. package/dist/context-build-budgeted.js +4 -144
  31. package/dist/context-ranking.js +1 -69
  32. package/dist/crypto.js +1 -179
  33. package/dist/daemon-write-endpoint.js +1 -290
  34. package/dist/daemon-writer.js +2 -406
  35. package/dist/database.js +43 -1110
  36. package/dist/deprecations.js +2 -162
  37. package/dist/design.js +13 -141
  38. package/dist/event-replication.js +1 -112
  39. package/dist/events-sse.js +7 -43
  40. package/dist/events.js +6 -238
  41. package/dist/failure-patterns.js +42 -659
  42. package/dist/federation.js +12 -236
  43. package/dist/goals.js +13 -101
  44. package/dist/golden.js +3 -355
  45. package/dist/handlers/agent.js +4 -165
  46. package/dist/handlers/alias-adapters.js +1 -129
  47. package/dist/handlers/aliases.js +1 -171
  48. package/dist/handlers/audit.js +1 -87
  49. package/dist/handlers/boundary.js +1 -221
  50. package/dist/handlers/capture.js +73 -1109
  51. package/dist/handlers/causality.js +7 -114
  52. package/dist/handlers/cloud.js +85 -382
  53. package/dist/handlers/companion.js +28 -459
  54. package/dist/handlers/datalake.js +7 -187
  55. package/dist/handlers/dispatch-context.js +0 -22
  56. package/dist/handlers/entity.js +25 -256
  57. package/dist/handlers/events.js +16 -335
  58. package/dist/handlers/failure.js +13 -340
  59. package/dist/handlers/goals.js +4 -296
  60. package/dist/handlers/intelligence.js +126 -674
  61. package/dist/handlers/invoicing.js +1 -70
  62. package/dist/handlers/mcpclient.js +6 -137
  63. package/dist/handlers/orchestration.js +40 -125
  64. package/dist/handlers/output-schemas.js +1 -24
  65. package/dist/handlers/presence.js +3 -99
  66. package/dist/handlers/project.js +28 -182
  67. package/dist/handlers/prompts.js +6 -157
  68. package/dist/handlers/quest.js +4 -224
  69. package/dist/handlers/recall.js +11 -218
  70. package/dist/handlers/registry.js +1 -167
  71. package/dist/handlers/resources.js +1 -288
  72. package/dist/handlers/review.js +11 -74
  73. package/dist/handlers/run.js +17 -487
  74. package/dist/handlers/search.js +15 -326
  75. package/dist/handlers/session.js +28 -615
  76. package/dist/handlers/share.js +8 -184
  77. package/dist/handlers/shims.js +1 -464
  78. package/dist/handlers/skill.js +67 -449
  79. package/dist/handlers/survivors.js +1 -120
  80. package/dist/handlers/symbols.js +8 -109
  81. package/dist/handlers/syncops.js +4 -302
  82. package/dist/handlers/types.js +1 -27
  83. package/dist/harvest.js +5 -191
  84. package/dist/hours.js +7 -156
  85. package/dist/http-auth.js +3 -321
  86. package/dist/http-fast.js +21 -1137
  87. package/dist/icons.js +1 -47
  88. package/dist/index.js +2 -924
  89. package/dist/indexer.js +4 -145
  90. package/dist/intelligence.js +31 -261
  91. package/dist/internal-dispatch.js +3 -212
  92. package/dist/keyset.js +1 -110
  93. package/dist/knowledge-graph.js +12 -176
  94. package/dist/license.js +2 -441
  95. package/dist/logger.js +2 -199
  96. package/dist/maintenance.js +2 -148
  97. package/dist/mcp-client.js +6 -262
  98. package/dist/memory-artifacts.js +30 -449
  99. package/dist/migrate-prompt.js +2 -124
  100. package/dist/migrations.js +40 -655
  101. package/dist/performance.js +1 -228
  102. package/dist/presence.js +11 -140
  103. package/dist/priority-embed.js +5 -164
  104. package/dist/providers/embedding-provider.js +1 -196
  105. package/dist/readonly-gate.js +1 -29
  106. package/dist/rehydration.js +9 -157
  107. package/dist/reindex.js +1 -88
  108. package/dist/render-target.js +21 -514
  109. package/dist/render.js +4 -280
  110. package/dist/repl-guard.js +1 -173
  111. package/dist/replication-daemon-entrypoint.js +1 -31
  112. package/dist/replication-daemon.js +2 -262
  113. package/dist/resilience.js +1 -591
  114. package/dist/reverse-bridge.js +5 -360
  115. package/dist/security.js +1 -244
  116. package/dist/session-seen.js +3 -51
  117. package/dist/setup.js +1 -260
  118. package/dist/skill-author.js +5 -168
  119. package/dist/spec-kit.js +1 -191
  120. package/dist/sqlite-busy.js +1 -154
  121. package/dist/statusline.js +11 -315
  122. package/dist/sub-agent.js +13 -262
  123. package/dist/summarizer.js +13 -139
  124. package/dist/symbols.js +7 -283
  125. package/dist/sync.js +5 -359
  126. package/dist/tasks-dispatch.js +1 -84
  127. package/dist/tasks.js +1 -282
  128. package/dist/token-budget.js +1 -143
  129. package/dist/tool-analytics.js +7 -129
  130. package/dist/tool-annotations.js +1 -365
  131. package/dist/tool-manifest-v2.json +1 -1
  132. package/dist/tool-manifest.json +1 -1
  133. package/dist/tool-profiles.js +1 -75
  134. package/dist/trace-harvest.js +6 -244
  135. package/dist/types.js +1 -30
  136. package/dist/ui-dashboard.js +41 -50
  137. package/dist/ulid.js +1 -81
  138. package/dist/validate.js +1 -129
  139. package/dist/vault.js +1 -534
  140. package/dist/vectors.js +3 -184
  141. package/dist/version-check.js +4 -136
  142. package/dist/visibility.js +19 -155
  143. package/dist/wyrm-cli.js +98 -2464
  144. package/dist/wyrm-guard.js +14 -424
  145. package/dist/wyrm-loop.js +3 -150
  146. package/dist/wyrm-manifest.json +1 -1
  147. package/dist/wyrm-statusline-daemon.js +1 -11
  148. package/dist/wyrm-statusline.js +4 -56
  149. package/dist/wyrm-ui.js +9 -77
  150. package/package.json +4 -2
package/dist/vault.js CHANGED
@@ -1,534 +1 @@
1
- /**
2
- * Wyrm secret vault — local-first, encrypted-at-rest secret storage.
3
- *
4
- * Secrets (npm tokens, API keys, signing keys, …) are stored AES-256-GCM
5
- * -encrypted in `~/.wyrm/vault.enc`. The thing that actually decides how SAFE
6
- * the vault is, is WHERE the 32-byte master key lives. Wyrm supports three
7
- * backends, resolved automatically (override with `WYRM_VAULT_BACKEND`):
8
- *
9
- * keychain — the master key lives in the OS secret store (Secret Service /
10
- * libsecret on Linux, Keychain on macOS), NEVER on disk. This is
11
- * the safe default on a desktop: a stolen copy of `~/.wyrm` (a
12
- * backup, a synced dotfiles repo, an exfiltrated tarball) has the
13
- * ciphertext but NOT the key, so the AES actually protects you.
14
- * passphrase — key derived from `WYRM_VAULT_PASSPHRASE` via scrypt; no key on
15
- * disk. Best for headless / CI where there's no desktop keyring.
16
- * keyfile — random key in `~/.wyrm/vault.key` (0600). LEGACY / last resort:
17
- * the key sits beside the ciphertext, so at-rest encryption only
18
- * protects against someone who gets `vault.enc` but not the dir.
19
- * `wyrm vault secure` migrates a keyfile vault to keychain.
20
- *
21
- * Threat model — honest boundaries. The keychain backend defeats OFFLINE theft
22
- * of `~/.wyrm` (the realistic risk: backups, rsync, cloud-synced dotfiles, a
23
- * leaked copy). It does NOT defend against a malicious process running AS YOU in
24
- * an UNLOCKED login session — that process can ask the Secret Service for the
25
- * key exactly like Wyrm does. Defending against that needs per-access prompts or
26
- * hardware confirmation (TPM/Secure Enclave), which break non-interactive use by
27
- * the agent. For the strongest at-rest posture in an untrusted environment, use
28
- * `passphrase` mode and don't cache the passphrase.
29
- *
30
- * GCM gives confidentiality AND integrity (tamper → decrypt throws). The only
31
- * runtime dep is the OS keyring CLI (`secret-tool`/`security`), invoked via
32
- * execFileSync (no shell). On LINUX the key is passed to `secret-tool` on STDIN —
33
- * never argv. On macOS the `security` CLI has no stdin mode for `add-generic-password
34
- * -w`, so the (random, non-user) master key is passed on argv, which is visible only
35
- * to a SAME-USER `ps` — and a same-user process is already outside the keychain
36
- * backend's threat model (it can read the keychain directly). Pure stdlib otherwise.
37
- *
38
- * @copyright 2026 Ghost Protocol (Pvt) Ltd.
39
- * @license AGPL-3.0-or-later — dual-licensed; commercial terms: ghosts.lk@proton.me. See LICENSE.
40
- */
41
- import { createCipheriv, createDecipheriv, randomBytes, scryptSync, timingSafeEqual } from 'node:crypto';
42
- import { existsSync, readFileSync, writeFileSync, mkdirSync, chmodSync, unlinkSync, statSync, copyFileSync, openSync, fsyncSync, closeSync, writeSync } from 'node:fs';
43
- import { execFileSync } from 'node:child_process';
44
- import { homedir, platform } from 'node:os';
45
- import { join } from 'node:path';
46
- const ALGO = 'aes-256-gcm';
47
- const IV_LEN = 12;
48
- const TAG_LEN = 16;
49
- /** Secret Service attributes that identify Wyrm's master key. */
50
- const KEYCHAIN_SERVICE = 'wyrm-vault';
51
- const KEYCHAIN_LABEL = 'Wyrm Vault master key';
52
- function vaultDir() { return process.env.WYRM_VAULT_DIR || join(homedir(), '.wyrm'); }
53
- function vaultFile() { return join(vaultDir(), 'vault.enc'); }
54
- function keyFile() { return join(vaultDir(), 'vault.key'); }
55
- function saltFile() { return join(vaultDir(), 'vault.salt'); }
56
- function markerFile() { return join(vaultDir(), 'vault.meta'); }
57
- // ── Salt for passphrase mode ──────────────────────────────────────────────────
58
- /** Per-install scrypt salt for passphrase mode. A single hardcoded global salt
59
- * lets the same passphrase derive the same key across every install (rainbow /
60
- * precompute risk), so generate a random 16-byte salt once and persist it
61
- * (non-secret). Back-compat: if a passphrase vault already exists from before
62
- * per-install salts, keep its legacy global salt so existing secrets still
63
- * decrypt. */
64
- function passphraseSalt() {
65
- const sf = saltFile();
66
- if (existsSync(sf))
67
- return readFileSync(sf);
68
- mkdirSync(vaultDir(), { recursive: true, mode: 0o700 });
69
- const salt = existsSync(vaultFile()) ? Buffer.from('wyrm-vault-v1') : randomBytes(16);
70
- writeFileSync(sf, salt, { mode: 0o600 });
71
- try {
72
- chmodSync(sf, 0o600);
73
- }
74
- catch { /* best-effort */ }
75
- return salt;
76
- }
77
- // ── Backend marker (non-secret) ─────────────────────────────────────────────────
78
- /** A vault declares which backend it was created/secured under in `vault.meta`
79
- * (non-secret JSON). Any build then resolves the RIGHT backend for an existing
80
- * vault and can fail LOUDLY when that backend is unavailable — instead of
81
- * silently minting a fresh key that can never decrypt the existing ciphertext
82
- * (the exact footgun that orphans a vault when a keyring is locked/missing). */
83
- function readMarker() {
84
- try {
85
- const m = JSON.parse(readFileSync(markerFile(), 'utf8'));
86
- if (m.backend === 'keychain' || m.backend === 'passphrase' || m.backend === 'keyfile')
87
- return m.backend;
88
- }
89
- catch { /* absent / unreadable → legacy vault: fall back to heuristics + the existence guard */ }
90
- return null;
91
- }
92
- function writeMarker(backend) {
93
- try {
94
- mkdirSync(vaultDir(), { recursive: true, mode: 0o700 });
95
- writeFileSync(markerFile(), JSON.stringify({ backend, v: 1 }), { mode: 0o600 });
96
- try {
97
- chmodSync(markerFile(), 0o600);
98
- }
99
- catch { /* best-effort */ }
100
- }
101
- catch { /* non-fatal: the marker is an optimization; the vault.enc-exists guard still protects */ }
102
- }
103
- // ── OS keychain (Secret Service on Linux, Keychain on macOS) ────────────────────
104
- /** The keyring CLI to drive. Overridable for tests via WYRM_VAULT_SECRETTOOL. */
105
- function secretToolCmd() { return process.env.WYRM_VAULT_SECRETTOOL || 'secret-tool'; }
106
- /** A stable per-vault-dir account label so multiple WYRM_VAULT_DIRs don't collide. */
107
- function keychainAccount() { return vaultDir(); }
108
- function isENOENT(e) {
109
- return !!e && typeof e === 'object' && e.code === 'ENOENT';
110
- }
111
- /** Read the master key from the OS keychain, or undefined if absent. */
112
- function keychainGet() {
113
- try {
114
- if (platform() === 'darwin') {
115
- const out = execFileSync('security', ['find-generic-password', '-s', KEYCHAIN_SERVICE, '-a', keychainAccount(), '-w'], { timeout: 8000, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
116
- const b64 = out.trim();
117
- return b64 ? Buffer.from(b64, 'base64') : undefined;
118
- }
119
- const out = execFileSync(secretToolCmd(), ['lookup', 'service', KEYCHAIN_SERVICE, 'path', keychainAccount()], { timeout: 8000, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
120
- const b64 = out.trim();
121
- return b64 ? Buffer.from(b64, 'base64') : undefined;
122
- }
123
- catch (e) {
124
- if (isENOENT(e))
125
- throw new Error('OS keychain CLI not found (install libsecret/`secret-tool`), or use WYRM_VAULT_PASSPHRASE / `wyrm vault secure --backend passphrase`.');
126
- // Non-ENOENT (e.g. "no such secret") → key simply isn't there.
127
- return undefined;
128
- }
129
- }
130
- /** Store the master key in the OS keychain. Secret goes via STDIN, never argv. */
131
- function keychainStore(key) {
132
- const b64 = key.toString('base64');
133
- if (platform() === 'darwin') {
134
- // -U updates if present; -w reads the value from the arg, so feed via stdin-safe path:
135
- // `security` has no stdin mode for -w, but the value is base64 of a random key (not the
136
- // user secret itself) and the process is the user's own — acceptable. Prefer add w/ update.
137
- execFileSync('security', ['add-generic-password', '-U', '-s', KEYCHAIN_SERVICE, '-a', keychainAccount(), '-l', KEYCHAIN_LABEL, '-w', b64], { timeout: 8000, stdio: ['ignore', 'ignore', 'ignore'] });
138
- return;
139
- }
140
- execFileSync(secretToolCmd(), ['store', '--label', KEYCHAIN_LABEL, 'service', KEYCHAIN_SERVICE, 'path', keychainAccount()], { timeout: 8000, input: b64, stdio: ['pipe', 'ignore', 'ignore'] });
141
- }
142
- /** Remove the master key from the OS keychain (best-effort). */
143
- function keychainClear() {
144
- try {
145
- if (platform() === 'darwin') {
146
- execFileSync('security', ['delete-generic-password', '-s', KEYCHAIN_SERVICE, '-a', keychainAccount()], { timeout: 8000, stdio: ['ignore', 'ignore', 'ignore'] });
147
- }
148
- else {
149
- execFileSync(secretToolCmd(), ['clear', 'service', KEYCHAIN_SERVICE, 'path', keychainAccount()], { timeout: 8000, stdio: ['ignore', 'ignore', 'ignore'] });
150
- }
151
- }
152
- catch { /* nothing to clear / unavailable */ }
153
- }
154
- /** Is the OS keychain reachable at all (CLI present + responds)? */
155
- function keychainAvailable() {
156
- try {
157
- if (platform() === 'darwin') {
158
- execFileSync('security', ['find-generic-password', '-s', KEYCHAIN_SERVICE, '-a', '__wyrm_probe__'], { timeout: 8000, stdio: ['ignore', 'ignore', 'ignore'] });
159
- return true; // found (unlikely) → reachable
160
- }
161
- execFileSync(secretToolCmd(), ['lookup', 'service', KEYCHAIN_SERVICE, 'path', '__wyrm_probe__'], { timeout: 8000, stdio: ['ignore', 'ignore', 'ignore'] });
162
- return true; // probe key absent but the CLI/daemon answered → reachable
163
- }
164
- catch (e) {
165
- if (isENOENT(e))
166
- return false; // CLI not installed
167
- // Exit 1 = "no such secret" → service reachable. Any other failure → treat as available
168
- // only if it's the benign not-found; a timeout/daemon error means NOT usable.
169
- const status = e.status;
170
- // libsecret returns 1 on a clean "not found"; macOS `security` returns 44
171
- // (errSecItemNotFound). Either means the keyring is reachable but our probe key
172
- // is absent. Anything else (timeout, daemon error, locked) → not usable.
173
- return status === 1 || (platform() === 'darwin' && status === 44);
174
- }
175
- }
176
- /** Does the keychain currently hold OUR master key? */
177
- function keychainHasKey() {
178
- try {
179
- return keychainGet() !== undefined;
180
- }
181
- catch {
182
- return false;
183
- }
184
- }
185
- // ── Backend resolution ──────────────────────────────────────────────────────────
186
- /** Decide which backend to use. `WYRM_VAULT_PASSPHRASE` always wins; otherwise
187
- * honor an explicit `WYRM_VAULT_BACKEND`; otherwise auto-pick the safest backend
188
- * that won't break an existing vault. */
189
- export function resolveBackend() {
190
- if (process.env.WYRM_VAULT_PASSPHRASE)
191
- return 'passphrase';
192
- const explicit = (process.env.WYRM_VAULT_BACKEND || 'auto').toLowerCase();
193
- if (explicit === 'passphrase' || explicit === 'keychain' || explicit === 'keyfile')
194
- return explicit;
195
- // auto: an EXISTING vault declares its backend in vault.meta — honor it so we never
196
- // resolve to a DIFFERENT backend and mint a stray key. If the declared backend is
197
- // unavailable, masterKey() fails loudly (it does NOT fall through to keyfile).
198
- if (existsSync(vaultFile())) {
199
- const marked = readMarker();
200
- if (marked)
201
- return marked;
202
- }
203
- if (keychainHasKey())
204
- return 'keychain'; // already migrated / a keychain vault exists
205
- if (existsSync(keyFile()))
206
- return 'keyfile'; // legacy keyfile vault — keep it working (we warn elsewhere)
207
- if (keychainAvailable())
208
- return 'keychain'; // fresh box with a working keyring → safe by default
209
- return 'keyfile'; // no keyring (headless) and no passphrase → last resort
210
- }
211
- /** The 32-byte master key for the resolved backend. A new key is minted ONLY for a
212
- * brand-new vault (no `vault.enc` yet). If `vault.enc` already exists but no key is
213
- * available for the resolved backend, we FAIL LOUDLY rather than mint a fresh key —
214
- * a new key can never decrypt existing ciphertext, it can only orphan the vault and
215
- * surface as a misleading "decrypt failed". */
216
- function masterKey() {
217
- const backend = resolveBackend();
218
- const vaultExists = existsSync(vaultFile());
219
- if (backend === 'passphrase') {
220
- const pass = process.env.WYRM_VAULT_PASSPHRASE;
221
- if (!pass)
222
- throw new Error('passphrase backend selected but WYRM_VAULT_PASSPHRASE is not set.');
223
- if (!vaultExists)
224
- writeMarker('passphrase');
225
- return scryptSync(pass, passphraseSalt(), 32);
226
- }
227
- if (backend === 'keychain') {
228
- let k = keychainGet();
229
- if (!k) {
230
- if (vaultExists)
231
- throw new Error('vault.enc exists but its master key is not in the OS keychain (keyring locked/unavailable, or the key was cleared). ' +
232
- 'Unlock your login keyring and retry, or set WYRM_VAULT_PASSPHRASE if this vault uses a passphrase. ' +
233
- 'Refusing to mint a new key — it cannot decrypt your existing secrets and would orphan them.');
234
- k = randomBytes(32);
235
- keychainStore(k);
236
- writeMarker('keychain');
237
- }
238
- if (k.length !== 32)
239
- throw new Error('keychain holds a malformed master key (expected 32 bytes).');
240
- return k;
241
- }
242
- // keyfile
243
- const kf = keyFile();
244
- if (!existsSync(kf)) {
245
- if (vaultExists)
246
- throw new Error('vault.enc exists but vault.key is missing — this vault was likely created under a different backend (keychain/passphrase). ' +
247
- 'Unlock your OS keyring or set WYRM_VAULT_PASSPHRASE. ' +
248
- 'Refusing to mint a new keyfile — it cannot decrypt your existing secrets and would orphan them.');
249
- mkdirSync(vaultDir(), { recursive: true, mode: 0o700 });
250
- const k = randomBytes(32);
251
- writeFileSync(kf, k, { mode: 0o600 });
252
- try {
253
- chmodSync(kf, 0o600);
254
- }
255
- catch { /* best-effort on platforms w/o chmod */ }
256
- writeMarker('keyfile');
257
- return k;
258
- }
259
- return readFileSync(kf);
260
- }
261
- // ── Crypto core (explicit-key variants so migration can re-key) ─────────────────
262
- function decryptWith(raw, key) {
263
- if (raw.length < IV_LEN + TAG_LEN)
264
- throw new Error('vault file is corrupt (too short)');
265
- const iv = raw.subarray(0, IV_LEN);
266
- const tag = raw.subarray(IV_LEN, IV_LEN + TAG_LEN);
267
- const ct = raw.subarray(IV_LEN + TAG_LEN);
268
- const d = createDecipheriv(ALGO, key, iv);
269
- d.setAuthTag(tag);
270
- const pt = Buffer.concat([d.update(ct), d.final()]);
271
- return JSON.parse(pt.toString('utf8'));
272
- }
273
- function encryptWith(secrets, key) {
274
- const iv = randomBytes(IV_LEN);
275
- const c = createCipheriv(ALGO, key, iv);
276
- const ct = Buffer.concat([c.update(JSON.stringify(secrets), 'utf8'), c.final()]);
277
- const tag = c.getAuthTag();
278
- return Buffer.concat([iv, tag, ct]);
279
- }
280
- function loadWith(key) {
281
- const vf = vaultFile();
282
- if (!existsSync(vf))
283
- return {};
284
- try {
285
- return decryptWith(readFileSync(vf), key);
286
- }
287
- catch {
288
- throw new Error('vault decrypt failed — wrong key/passphrase, or the vault was tampered with.');
289
- }
290
- }
291
- function persistWith(secrets, key) {
292
- mkdirSync(vaultDir(), { recursive: true, mode: 0o700 });
293
- writeFileSync(vaultFile(), encryptWith(secrets, key), { mode: 0o600 });
294
- try {
295
- chmodSync(vaultFile(), 0o600);
296
- }
297
- catch { /* best-effort */ }
298
- }
299
- function load() {
300
- if (!existsSync(vaultFile()))
301
- return {};
302
- return loadWith(masterKey());
303
- }
304
- function persist(secrets) { persistWith(secrets, masterKey()); }
305
- // ── Public API ──────────────────────────────────────────────────────────────────
306
- export function vaultSet(name, value) {
307
- if (!name || !/^[\w.\-:/@]+$/.test(name))
308
- throw new Error('invalid secret name');
309
- const s = load();
310
- s[name] = value;
311
- persist(s);
312
- }
313
- export function vaultGet(name) { return load()[name]; }
314
- export function vaultHas(name) { return name in load(); }
315
- export function vaultList() { return Object.keys(load()).sort(); }
316
- export function vaultRemove(name) {
317
- const s = load();
318
- if (!(name in s))
319
- return false;
320
- delete s[name];
321
- persist(s);
322
- return true;
323
- }
324
- /** Destroy the entire vault (encrypted store; keeps the key material). */
325
- export function vaultDestroy() {
326
- try {
327
- unlinkSync(vaultFile());
328
- }
329
- catch { /* already gone */ }
330
- try {
331
- unlinkSync(markerFile());
332
- }
333
- catch { /* marker describes a now-deleted vault */ }
334
- }
335
- /** Overwrite a file's bytes with random data, fsync, then unlink. Best-effort on
336
- * copy-on-write filesystems (btrfs/APFS may relocate), which is why migration
337
- * ROTATES the key — even a recovered keyfile decrypts nothing afterward. */
338
- function shredFile(path) {
339
- try {
340
- const size = statSync(path).size;
341
- const fd = openSync(path, 'r+');
342
- try {
343
- if (size > 0) {
344
- const junk = randomBytes(size);
345
- writeSync(fd, junk, 0, size, 0);
346
- fsyncSync(fd);
347
- }
348
- }
349
- finally {
350
- closeSync(fd);
351
- }
352
- }
353
- catch { /* file gone / unwritable — fall through to unlink */ }
354
- try {
355
- unlinkSync(path);
356
- }
357
- catch { /* already gone */ }
358
- }
359
- /** Find the key that actually decrypts the CURRENT ciphertext, by trying every
360
- * key we can construct (keyfile, keychain, passphrase) — used by migration so it
361
- * works regardless of what backend the environment currently resolves to. */
362
- function detectCurrentKey() {
363
- if (!existsSync(vaultFile()))
364
- return { key: null, from: resolveBackend() };
365
- const raw = readFileSync(vaultFile());
366
- const candidates = [];
367
- if (existsSync(keyFile()))
368
- candidates.push({ from: 'keyfile', key: readFileSync(keyFile()) });
369
- try {
370
- const k = keychainGet();
371
- if (k)
372
- candidates.push({ from: 'keychain', key: k });
373
- }
374
- catch { /* keychain unreachable */ }
375
- if (process.env.WYRM_VAULT_PASSPHRASE)
376
- candidates.push({ from: 'passphrase', key: scryptSync(process.env.WYRM_VAULT_PASSPHRASE, passphraseSalt(), 32) });
377
- for (const c of candidates) {
378
- try {
379
- decryptWith(raw, c.key);
380
- return { key: c.key, from: c.from };
381
- }
382
- catch { /* try next */ }
383
- }
384
- throw new Error('could not decrypt the current vault with any available key (keyfile / keychain / passphrase).');
385
- }
386
- /** Migrate the vault to a safer backend: re-key under the OS keychain (default)
387
- * or a passphrase, verify every secret still decrypts, then shred the plaintext
388
- * `vault.key`. Backs up `vault.enc` first; restores it if verification fails. */
389
- export function vaultSecure(opts = {}) {
390
- const to = opts.backend ?? 'keychain';
391
- const rotate = opts.rotate ?? true;
392
- // 1) Decrypt everything with the SOURCE key. We must NOT use load()/resolveBackend()
393
- // here: when migrating to passphrase, WYRM_VAULT_PASSPHRASE (the TARGET) is already
394
- // set, so resolveBackend would point at the target. Instead, try every key we can
395
- // construct and use whichever actually decrypts the existing ciphertext.
396
- const { key: srcKey, from } = detectCurrentKey();
397
- const current = srcKey ? decryptWith(readFileSync(vaultFile()), srcKey) : {};
398
- const count = Object.keys(current).length;
399
- // 2) Back up the ciphertext before touching anything.
400
- let backup = '';
401
- if (existsSync(vaultFile())) {
402
- backup = vaultFile() + '.bak.' + new Date().toISOString().replace(/[:.]/g, '-');
403
- copyFileSync(vaultFile(), backup);
404
- try {
405
- chmodSync(backup, 0o600);
406
- }
407
- catch { /* best-effort */ }
408
- }
409
- // 3) Establish the new key under the target backend. CAPTURE the prior keychain slot
410
- // FIRST: the keychain has a single service::path slot, so keychainStore overwrites
411
- // (destroys) any existing key — we must be able to put it back if migration fails.
412
- let newKey;
413
- let prevKeychain;
414
- if (to === 'keychain') {
415
- if (!keychainAvailable())
416
- throw new Error('OS keychain not reachable — cannot migrate to keychain. Unlock your login keyring, or use `--backend passphrase`.');
417
- prevKeychain = keychainGet(); // K_old when re-securing an existing keychain vault, else undefined
418
- newKey = rotate ? randomBytes(32) : (prevKeychain ?? randomBytes(32));
419
- keychainStore(newKey);
420
- }
421
- else {
422
- const pass = process.env.WYRM_VAULT_PASSPHRASE;
423
- if (!pass)
424
- throw new Error('migrate to passphrase requires WYRM_VAULT_PASSPHRASE to be set.');
425
- newKey = scryptSync(pass, passphraseSalt(), 32);
426
- }
427
- // Undo the keychain mutation we just made (restore prior key, or clear if none).
428
- const restoreKeychain = () => {
429
- if (to !== 'keychain')
430
- return;
431
- if (prevKeychain) {
432
- try {
433
- keychainStore(prevKeychain);
434
- }
435
- catch { /* best-effort */ }
436
- }
437
- else
438
- keychainClear();
439
- };
440
- // 4) Re-encrypt under the new key and VERIFY (full contents, not just key names)
441
- // before we destroy anything. On ANY failure, fully roll back to the state we
442
- // found: restore the source ciphertext AND the prior keychain key.
443
- try {
444
- persistWith(current, newKey);
445
- const check = decryptWith(readFileSync(vaultFile()), newKey);
446
- const a = Buffer.from(JSON.stringify(check));
447
- const b = Buffer.from(JSON.stringify(current));
448
- if (a.length !== b.length || !timingSafeEqual(a, b))
449
- throw new Error('post-migration verification mismatch');
450
- }
451
- catch (e) {
452
- if (backup) {
453
- // NEVER delete the backup unless the restore actually succeeded.
454
- try {
455
- copyFileSync(backup, vaultFile());
456
- }
457
- catch (err) {
458
- restoreKeychain();
459
- throw new Error(`CRITICAL: vault rollback failed — vault.enc may be partially written. Restore manually: cp "${backup}" "${vaultFile()}" (${err.message})`);
460
- }
461
- try {
462
- unlinkSync(backup);
463
- }
464
- catch { /* best-effort */ }
465
- }
466
- restoreKeychain();
467
- throw new Error('migration aborted (vault left unchanged): ' + e.message);
468
- }
469
- // 5) Record the new backend so any future build resolves it (and fails loudly if
470
- // it can't reach it) instead of minting a stray key.
471
- writeMarker(to);
472
- // 6) Success — retire the old key material.
473
- let keyfileShredded = false;
474
- if (existsSync(keyFile())) {
475
- shredFile(keyFile());
476
- keyfileShredded = true;
477
- }
478
- if (to === 'passphrase')
479
- keychainClear(); // ensure no stale keychain copy lingers
480
- // 7) Remove the transient backup. When the key was rotated the old ciphertext is now
481
- // UNDECRYPTABLE (old key destroyed), so the backup is dead weight, not a safety net.
482
- // The verified live vault is the source of truth; for real backups use `wyrm sync export`.
483
- if (backup) {
484
- try {
485
- unlinkSync(backup);
486
- }
487
- catch { /* best-effort */ }
488
- backup = '';
489
- }
490
- return { from, to, rotated: rotate, secrets: count, backup, keyfileShredded };
491
- }
492
- /** Where things live + the security posture (for `vault info`; never prints secrets). */
493
- export function vaultPaths() {
494
- const backend = resolveBackend();
495
- let count = 0;
496
- try {
497
- count = vaultList().length;
498
- }
499
- catch { /* locked */ }
500
- const keyfileOnDisk = existsSync(keyFile());
501
- const kcAvail = backend === 'keychain' ? true : (() => { try {
502
- return keychainAvailable();
503
- }
504
- catch {
505
- return false;
506
- } })();
507
- // "secure" = the master key is NOT a plaintext file sitting next to the ciphertext.
508
- const secure = (backend === 'keychain' || backend === 'passphrase') && !keyfileOnDisk;
509
- return { dir: vaultDir(), vault: vaultFile(), key: keyFile(), backend, mode: backend, count, keyfileOnDisk, keychainAvailable: kcAvail, secure };
510
- }
511
- /** A short, actionable advisory for a connecting AI/operator — empty when the vault
512
- * is already secure, so it self-resolves once `wyrm vault setup` has been run. Used
513
- * by `wyrm_session_prime` to AUTOMATICALLY guide the client AI to set credential
514
- * storage up properly. Never contains secret values. */
515
- export function vaultAdvisory() {
516
- let p;
517
- try {
518
- p = vaultPaths();
519
- }
520
- catch {
521
- return [];
522
- }
523
- if (p.secure)
524
- return []; // already safe — don't nag
525
- const lines = [];
526
- lines.push(p.count > 0
527
- ? `⚠️ The credential vault holds ${p.count} secret(s) but the master key is a PLAINTEXT file (\`${p.key}\`) sitting beside the ciphertext — at-rest encryption is effectively defeated against anyone who can read \`${p.dir}\` (a backup, a synced dotfiles repo, an exfiltrated tarball).`
528
- : `⚠️ The credential vault's master key would be stored as a plaintext file beside the ciphertext.`);
529
- lines.push(p.keychainAvailable
530
- ? 'Fix in one step — run **`wyrm vault setup`** (moves the key into the OS keychain, rotates it, shreds the plaintext key). Then store secrets with `printf %s "$TOKEN" | wyrm vault set <name>` and use them via `wyrm vault exec <name> -- <cmd>` (never echoed).'
531
- : 'No OS keychain on this host — set `WYRM_VAULT_PASSPHRASE`, then run **`wyrm vault setup`** (derives the key from the passphrase; nothing on disk).');
532
- return lines;
533
- }
534
- //# sourceMappingURL=vault.js.map
1
+ import{createCipheriv as X,createDecipheriv as Q,randomBytes as d,scryptSync as U,timingSafeEqual as Z}from"node:crypto";import{existsSync as o,readFileSync as u,writeFileSync as R,mkdirSync as T,chmodSync as S,unlinkSync as g,statSync as ee,copyFileSync as I,openSync as te,fsyncSync as re,closeSync as ne,writeSync as ie}from"node:fs";import{execFileSync as f}from"node:child_process";import{homedir as oe,platform as w}from"node:os";import{join as A}from"node:path";const $="aes-256-gcm",k=12,V=16,y="wyrm-vault",D="Wyrm Vault master key";function c(){return process.env.WYRM_VAULT_DIR||A(oe(),".wyrm")}function i(){return A(c(),"vault.enc")}function l(){return A(c(),"vault.key")}function se(){return A(c(),"vault.salt")}function x(){return A(c(),"vault.meta")}function M(){const e=se();if(o(e))return u(e);T(c(),{recursive:!0,mode:448});const t=o(i())?Buffer.from("wyrm-vault-v1"):d(16);R(e,t,{mode:384});try{S(e,384)}catch{}return t}function ce(){try{const e=JSON.parse(u(x(),"utf8"));if(e.backend==="keychain"||e.backend==="passphrase"||e.backend==="keyfile")return e.backend}catch{}return null}function L(e){try{T(c(),{recursive:!0,mode:448}),R(x(),JSON.stringify({backend:e,v:1}),{mode:384});try{S(x(),384)}catch{}}catch{}}function P(){return process.env.WYRM_VAULT_SECRETTOOL||"secret-tool"}function m(){return c()}function J(e){return!!e&&typeof e=="object"&&e.code==="ENOENT"}function O(){try{if(w()==="darwin"){const n=f("security",["find-generic-password","-s",y,"-a",m(),"-w"],{timeout:8e3,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).trim();return n?Buffer.from(n,"base64"):void 0}const t=f(P(),["lookup","service",y,"path",m()],{timeout:8e3,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).trim();return t?Buffer.from(t,"base64"):void 0}catch(e){if(J(e))throw new Error("OS keychain CLI not found (install libsecret/`secret-tool`), or use WYRM_VAULT_PASSPHRASE / `wyrm vault secure --backend passphrase`.");return}}function Y(e){const t=e.toString("base64");if(w()==="darwin"){f("security",["add-generic-password","-U","-s",y,"-a",m(),"-l",D,"-w",t],{timeout:8e3,stdio:["ignore","ignore","ignore"]});return}f(P(),["store","--label",D,"service",y,"path",m()],{timeout:8e3,input:t,stdio:["pipe","ignore","ignore"]})}function j(){try{w()==="darwin"?f("security",["delete-generic-password","-s",y,"-a",m()],{timeout:8e3,stdio:["ignore","ignore","ignore"]}):f(P(),["clear","service",y,"path",m()],{timeout:8e3,stdio:["ignore","ignore","ignore"]})}catch{}}function H(){try{return w()==="darwin"?(f("security",["find-generic-password","-s",y,"-a","__wyrm_probe__"],{timeout:8e3,stdio:["ignore","ignore","ignore"]}),!0):(f(P(),["lookup","service",y,"path","__wyrm_probe__"],{timeout:8e3,stdio:["ignore","ignore","ignore"]}),!0)}catch(e){if(J(e))return!1;const t=e.status;return t===1||w()==="darwin"&&t===44}}function ae(){try{return O()!==void 0}catch{return!1}}function C(){if(process.env.WYRM_VAULT_PASSPHRASE)return"passphrase";const e=(process.env.WYRM_VAULT_BACKEND||"auto").toLowerCase();if(e==="passphrase"||e==="keychain"||e==="keyfile")return e;if(o(i())){const t=ce();if(t)return t}return ae()?"keychain":o(l())?"keyfile":H()?"keychain":"keyfile"}function G(){const e=C(),t=o(i());if(e==="passphrase"){const n=process.env.WYRM_VAULT_PASSPHRASE;if(!n)throw new Error("passphrase backend selected but WYRM_VAULT_PASSPHRASE is not set.");return t||L("passphrase"),U(n,M(),32)}if(e==="keychain"){let n=O();if(!n){if(t)throw new Error("vault.enc exists but its master key is not in the OS keychain (keyring locked/unavailable, or the key was cleared). Unlock your login keyring and retry, or set WYRM_VAULT_PASSPHRASE if this vault uses a passphrase. Refusing to mint a new key \u2014 it cannot decrypt your existing secrets and would orphan them.");n=d(32),Y(n),L("keychain")}if(n.length!==32)throw new Error("keychain holds a malformed master key (expected 32 bytes).");return n}const r=l();if(!o(r)){if(t)throw new Error("vault.enc exists but vault.key is missing \u2014 this vault was likely created under a different backend (keychain/passphrase). Unlock your OS keyring or set WYRM_VAULT_PASSPHRASE. Refusing to mint a new keyfile \u2014 it cannot decrypt your existing secrets and would orphan them.");T(c(),{recursive:!0,mode:448});const n=d(32);R(r,n,{mode:384});try{S(r,384)}catch{}return L("keyfile"),n}return u(r)}function W(e,t){if(e.length<k+V)throw new Error("vault file is corrupt (too short)");const r=e.subarray(0,k),n=e.subarray(k,k+V),h=e.subarray(k+V),a=Q($,t,r);a.setAuthTag(n);const N=Buffer.concat([a.update(h),a.final()]);return JSON.parse(N.toString("utf8"))}function ue(e,t){const r=d(k),n=X($,t,r),h=Buffer.concat([n.update(JSON.stringify(e),"utf8"),n.final()]),a=n.getAuthTag();return Buffer.concat([r,a,h])}function fe(e){const t=i();if(!o(t))return{};try{return W(u(t),e)}catch{throw new Error("vault decrypt failed \u2014 wrong key/passphrase, or the vault was tampered with.")}}function q(e,t){T(c(),{recursive:!0,mode:448}),R(i(),ue(e,t),{mode:384});try{S(i(),384)}catch{}}function b(){return o(i())?fe(G()):{}}function z(e){q(e,G())}function ge(e,t){if(!e||!/^[\w.\-:/@]+$/.test(e))throw new Error("invalid secret name");const r=b();r[e]=t,z(r)}function we(e){return b()[e]}function Ae(e){return e in b()}function ye(){return Object.keys(b()).sort()}function be(e){const t=b();return e in t?(delete t[e],z(t),!0):!1}function Ee(){try{g(i())}catch{}try{g(x())}catch{}}function le(e){try{const t=ee(e).size,r=te(e,"r+");try{if(t>0){const n=d(t);ie(r,n,0,t,0),re(r)}}finally{ne(r)}}catch{}try{g(e)}catch{}}function he(){if(!o(i()))return{key:null,from:C()};const e=u(i()),t=[];o(l())&&t.push({from:"keyfile",key:u(l())});try{const r=O();r&&t.push({from:"keychain",key:r})}catch{}process.env.WYRM_VAULT_PASSPHRASE&&t.push({from:"passphrase",key:U(process.env.WYRM_VAULT_PASSPHRASE,M(),32)});for(const r of t)try{return W(e,r.key),{key:r.key,from:r.from}}catch{}throw new Error("could not decrypt the current vault with any available key (keyfile / keychain / passphrase).")}function _e(e={}){const t=e.backend??"keychain",r=e.rotate??!0,{key:n,from:h}=he(),a=n?W(u(i()),n):{},N=Object.keys(a).length;let s="";if(o(i())){s=i()+".bak."+new Date().toISOString().replace(/[:.]/g,"-"),I(i(),s);try{S(s,384)}catch{}}let v,E;if(t==="keychain"){if(!H())throw new Error("OS keychain not reachable \u2014 cannot migrate to keychain. Unlock your login keyring, or use `--backend passphrase`.");E=O(),v=r?d(32):E??d(32),Y(v)}else{const p=process.env.WYRM_VAULT_PASSPHRASE;if(!p)throw new Error("migrate to passphrase requires WYRM_VAULT_PASSPHRASE to be set.");v=U(p,M(),32)}const B=()=>{if(t==="keychain")if(E)try{Y(E)}catch{}else j()};try{q(a,v);const p=W(u(i()),v),_=Buffer.from(JSON.stringify(p)),F=Buffer.from(JSON.stringify(a));if(_.length!==F.length||!Z(_,F))throw new Error("post-migration verification mismatch")}catch(p){if(s){try{I(s,i())}catch(_){throw B(),new Error(`CRITICAL: vault rollback failed \u2014 vault.enc may be partially written. Restore manually: cp "${s}" "${i()}" (${_.message})`)}try{g(s)}catch{}}throw B(),new Error("migration aborted (vault left unchanged): "+p.message)}L(t);let K=!1;if(o(l())&&(le(l()),K=!0),t==="passphrase"&&j(),s){try{g(s)}catch{}s=""}return{from:h,to:t,rotated:r,secrets:N,backup:s,keyfileShredded:K}}function de(){const e=C();let t=0;try{t=ye().length}catch{}const r=o(l()),n=e==="keychain"?!0:(()=>{try{return H()}catch{return!1}})(),h=(e==="keychain"||e==="passphrase")&&!r;return{dir:c(),vault:i(),key:l(),backend:e,mode:e,count:t,keyfileOnDisk:r,keychainAvailable:n,secure:h}}function Re(){let e;try{e=de()}catch{return[]}if(e.secure)return[];const t=[];return t.push(e.count>0?`\u26A0\uFE0F The credential vault holds ${e.count} secret(s) but the master key is a PLAINTEXT file (\`${e.key}\`) sitting beside the ciphertext \u2014 at-rest encryption is effectively defeated against anyone who can read \`${e.dir}\` (a backup, a synced dotfiles repo, an exfiltrated tarball).`:"\u26A0\uFE0F The credential vault's master key would be stored as a plaintext file beside the ciphertext."),t.push(e.keychainAvailable?'Fix in one step \u2014 run **`wyrm vault setup`** (moves the key into the OS keychain, rotates it, shreds the plaintext key). Then store secrets with `printf %s "$TOKEN" | wyrm vault set <name>` and use them via `wyrm vault exec <name> -- <cmd>` (never echoed).':"No OS keychain on this host \u2014 set `WYRM_VAULT_PASSPHRASE`, then run **`wyrm vault setup`** (derives the key from the passphrase; nothing on disk)."),t}export{C as resolveBackend,Re as vaultAdvisory,Ee as vaultDestroy,we as vaultGet,Ae as vaultHas,ye as vaultList,de as vaultPaths,be as vaultRemove,_e as vaultSecure,ge as vaultSet};