@theronap/agnoclast-mcp 0.9.278 → 0.9.279

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.
@@ -276,7 +276,7 @@ if (cmd === 'login') {
276
276
  // ADR-0033 §5 C2. Guarded: adds the new namespace's allow rules and VERIFIES them from disk
277
277
  // before flipping mcpServers.cortex -> .agnoclast, so a seat that never received C1 cannot end up
278
278
  // with renamed tools and no permissions. Idempotent; --dry-run reports the plan and changes
279
- // nothing. NOT wired into any hook yet — activating it is the C2 release itself.
279
+ // nothing. Also runs on its own at every server start since 0.9.279 (autoMigrateKeys).
280
280
  const { runKeyMigration } = await import('../lib/migrate_key.mjs')
281
281
  const res = runKeyMigration({ dryRun: rest.includes('--dry-run') })
282
282
  process.stdout.write(JSON.stringify(res, null, 2) + '\n')
@@ -331,6 +331,10 @@ if (cmd === 'login') {
331
331
  // on their next install/repair; drop this branch a release after that has had time to propagate.
332
332
  } else {
333
333
  // Default: run the MCP server (stays alive; never exits).
334
+ // First, finish the cortex -> agnoclast key flip if this machine still has the old key. Best-effort,
335
+ // stderr only (stdout is the MCP channel); see autoMigrateKeys.
336
+ const { autoMigrateKeys } = await import('../lib/migrate_key.mjs')
337
+ autoMigrateKeys()
334
338
  const { runServer } = await import('../lib/server.mjs')
335
339
  await runServer(VERSION)
336
340
  }
@@ -1,5 +1,5 @@
1
1
  import { homedir } from 'node:os'
2
- import { existsSync } from 'node:fs'
2
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs'
3
3
  import { join } from 'node:path'
4
4
  import { readJson, backupFile, ensureDir, writeJson } from './editors/_fsutil.mjs'
5
5
  import { allowedToolsFor } from './editors/claude.mjs'
@@ -41,6 +41,13 @@ export function migratedMcpServers(servers) {
41
41
  const out = { ...(servers ?? {}) }
42
42
  if (out[NEW_KEY] && !out[OLD_KEY]) return { servers: out, moved: false, reason: 'already-new-key' }
43
43
  if (!out[OLD_KEY]) return { servers: out, moved: false, reason: 'no-entry' }
44
+ // BOTH keys (a pre-0.9.277 setup re-created `cortex` beside a migrated `agnoclast`): the new key is
45
+ // the one to keep — it is the one the person or a newer setup last chose. Copying `cortex` over it
46
+ // would carry the older spec across, which is how a @latest seat gets knocked back to @stable.
47
+ if (out[NEW_KEY]) {
48
+ delete out[OLD_KEY]
49
+ return { servers: out, moved: true, reason: 'dropped-duplicate' }
50
+ }
44
51
  // Carry the entry across untouched: command, args (the package spec) and env all survive, so a
45
52
  // dogfooder pinned to @latest stays pinned and the token is not re-derived.
46
53
  out[NEW_KEY] = out[OLD_KEY]
@@ -79,14 +86,18 @@ export function runKeyMigration({ home = homedir(), dryRun = false, io = {} } =
79
86
  const statePath = migrationStatePath(home)
80
87
  const required = allowedToolsFor(NEW_KEY)
81
88
 
82
- if (exists(statePath)) {
83
- try { if (read(statePath)?.to === NEW_KEY) return { status: 'already', statePath } } catch { /* re-run */ }
84
- }
85
-
89
+ // The config is checked BEFORE the state file. The state file used to short-circuit on its own, so
90
+ // a `cortex` entry re-created after the flip (an older setup run, 2026-10) was never looked at
91
+ // again. "Done" is only true while the config agrees.
86
92
  let cfg
87
93
  try { cfg = read(claudeJson) } catch (e) { return { status: 'aborted', step: 'read-config', reason: e.message } }
88
94
  const plan = migratedMcpServers(cfg?.mcpServers)
89
- if (!plan.moved) return { status: 'nothing-to-migrate', reason: plan.reason }
95
+ if (!plan.moved) {
96
+ if (exists(statePath)) {
97
+ try { if (read(statePath)?.to === NEW_KEY) return { status: 'already', statePath } } catch { /* fall through */ }
98
+ }
99
+ return { status: 'nothing-to-migrate', reason: plan.reason }
100
+ }
90
101
 
91
102
  // ── 1+2. Allowlist FIRST, then verify from disk. ───────────────────────────
92
103
  let settings
@@ -130,10 +141,73 @@ export function runKeyMigration({ home = homedir(), dryRun = false, io = {} } =
130
141
  if (!cfgAfter?.mcpServers?.[NEW_KEY]) {
131
142
  return { status: 'aborted', step: 'verify-config', reason: 'config key did not persist' }
132
143
  }
144
+ if (cfgAfter.mcpServers[OLD_KEY]) {
145
+ return { status: 'aborted', step: 'verify-config', reason: 'old key still present' }
146
+ }
133
147
 
134
148
  // ── 5. Record it. Best-effort: a missing state file only costs an idempotent re-run. ──
135
- const record = { from: OLD_KEY, to: NEW_KEY, allowsAdded: missing.length }
149
+ const record = { from: OLD_KEY, to: NEW_KEY, allowsAdded: missing.length, ...(plan.reason ? { reason: plan.reason } : {}) }
136
150
  try { mkdir(statePath); write(statePath, record) } catch { /* non-fatal */ }
137
151
 
138
152
  return { status: 'migrated', ...record, statePath }
139
153
  }
154
+
155
+ /** PURE. A Codex config.toml with our `cortex` tables renamed to `agnoclast` — or, when `agnoclast`
156
+ * tables are already there, with the `cortex` ones dropped. Returns null when there is nothing to do.
157
+ * Only our two table headers are touched; every other line survives byte for byte. */
158
+ export function migratedCodexToml(text) {
159
+ const lines = (text || '').split('\n')
160
+ const header = (l) => { const t = l.trim(); return t.startsWith('[') && t.endsWith(']') ? t : null }
161
+ const OLD = new Set([`[mcp_servers.${OLD_KEY}]`, `[mcp_servers.${OLD_KEY}.env]`])
162
+ if (!lines.some((l) => OLD.has(header(l)))) return null
163
+ if (lines.some((l) => header(l) === `[mcp_servers.${NEW_KEY}]`)) {
164
+ const kept = []
165
+ let skipping = false
166
+ for (const l of lines) {
167
+ const h = header(l)
168
+ if (h) skipping = OLD.has(h)
169
+ if (!skipping) kept.push(l)
170
+ }
171
+ return kept.join('\n')
172
+ }
173
+ return lines.map((l) => (OLD.has(header(l)) ? l.replace(`mcp_servers.${OLD_KEY}`, `mcp_servers.${NEW_KEY}`) : l)).join('\n')
174
+ }
175
+
176
+ /** Cursor and Codex: the same flip, no allowlist to order against (neither has one). Best-effort per
177
+ * editor; returns what changed. A malformed file is left alone, never rewritten. */
178
+ export function migrateOtherEditors({ home = homedir() } = {}) {
179
+ const changed = []
180
+ const cursor = join(home, '.cursor', 'mcp.json')
181
+ try {
182
+ if (existsSync(cursor)) {
183
+ const cfg = readJson(cursor)
184
+ const plan = migratedMcpServers(cfg?.mcpServers)
185
+ if (plan.moved) { backupFile(cursor); writeJson(cursor, { ...cfg, mcpServers: plan.servers }); changed.push('cursor') }
186
+ }
187
+ } catch { /* leave it */ }
188
+ const codex = join(home, '.codex', 'config.toml')
189
+ try {
190
+ if (existsSync(codex)) {
191
+ const next = migratedCodexToml(readFileSync(codex, 'utf8'))
192
+ if (next !== null) { backupFile(codex); writeFileSync(codex, next); changed.push('codex') }
193
+ }
194
+ } catch { /* leave it */ }
195
+ return changed
196
+ }
197
+
198
+ /** Run at every MCP server start (bin/cortex-mcp.mjs default branch) — the C2 activation that was
199
+ * planned in ADR-0033 and never wired, so seats that were told "it will happen for you" kept the
200
+ * `cortex` key for months. NEVER throws and NEVER writes stdout: stdout is the MCP stdio channel.
201
+ * The new name takes effect at the editor's next start; the running session keeps its servers. */
202
+ export function autoMigrateKeys({ home = homedir(), log = (m) => process.stderr.write(m + '\n') } = {}) {
203
+ try {
204
+ const res = runKeyMigration({ home })
205
+ const others = migrateOtherEditors({ home })
206
+ if (res.status === 'migrated' || others.length) {
207
+ log(`agnoclast: renamed the \`${OLD_KEY}\` MCP entry to \`${NEW_KEY}\` (${[res.status === 'migrated' ? 'claude' : null, ...others].filter(Boolean).join(', ')}); takes effect next restart`)
208
+ }
209
+ return { claude: res, others }
210
+ } catch (e) {
211
+ return { claude: { status: 'aborted', step: 'unexpected', reason: e?.message }, others: [] }
212
+ }
213
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theronap/agnoclast-mcp",
3
- "version": "0.9.278",
3
+ "version": "0.9.279",
4
4
  "description": "Connect your AI assistant to Cortex — your org's projects, activity, gaps, and directives, scoped to you.",
5
5
  "type": "module",
6
6
  "bin": {