@cortexkit/magic-context 0.34.1 → 0.34.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 (2) hide show
  1. package/dist/index.js +100 -32
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -35523,6 +35523,15 @@ var init_migrations = __esm(() => {
35523
35523
  ensureColumn(db, "session_meta", "detected_context_limit_provenance", "TEXT NOT NULL DEFAULT 'unknown'");
35524
35524
  }
35525
35525
  }
35526
+ },
35527
+ {
35528
+ version: 75,
35529
+ description: "persist mural cue validation rejection latches",
35530
+ up(db) {
35531
+ if (!tableExists(db, "memories"))
35532
+ return;
35533
+ ensureColumn(db, "memories", "mural_cue_rejection_count", "INTEGER NOT NULL DEFAULT 0");
35534
+ }
35526
35535
  }
35527
35536
  ];
35528
35537
  LATEST_MIGRATION_VERSION = MIGRATIONS.reduce((max, m3) => Math.max(max, m3.version), 0);
@@ -168015,6 +168024,7 @@ import {
168015
168024
  mkdtempSync,
168016
168025
  readdirSync as readdirSync2,
168017
168026
  readFileSync as readFileSync10,
168027
+ statSync as statSync7,
168018
168028
  unlinkSync as unlinkSync2
168019
168029
  } from "node:fs";
168020
168030
  import { basename as basename3, dirname as dirname10, join as join12 } from "node:path";
@@ -168026,24 +168036,57 @@ function getPersistedSchemaVersion(db) {
168026
168036
  const row = db.prepare("SELECT COALESCE(MAX(version), 0) AS version FROM schema_migrations WHERE version < ?").get(FORK_MIGRATION_VERSION_FLOOR);
168027
168037
  return row?.version ?? 0;
168028
168038
  }
168029
- function unreadableDiscovery(path4) {
168039
+ function unreadableDiscovery(path4, arm) {
168030
168040
  return {
168031
168041
  state: "unreadable",
168032
168042
  serverPids: [],
168033
168043
  staleFiles: [],
168034
- unreadableFile: path4
168044
+ unreadableFile: path4,
168045
+ unreadableArm: arm
168035
168046
  };
168036
168047
  }
168048
+ function invalidDiscoveryReason(raw) {
168049
+ const trimmed = raw.trim();
168050
+ if (trimmed.startsWith("{")) {
168051
+ try {
168052
+ const parsed = JSON.parse(trimmed);
168053
+ if ("pid" in parsed) {
168054
+ const pid = Number(parsed.pid);
168055
+ if (!Number.isInteger(pid) || pid <= 0)
168056
+ return "invalid-pid";
168057
+ }
168058
+ } catch {}
168059
+ }
168060
+ return "parse-invalid";
168061
+ }
168062
+ function classifyJunkDiscovery(portFile, raw, staleFiles) {
168063
+ let mtimeMs;
168064
+ try {
168065
+ mtimeMs = rpcDiscoveryFs.statSync(portFile).mtimeMs;
168066
+ } catch (error51) {
168067
+ if (error51.code === "ENOENT")
168068
+ return null;
168069
+ return unreadableDiscovery(portFile, "io");
168070
+ }
168071
+ const ageMs = Date.now() - mtimeMs;
168072
+ if (!Number.isFinite(ageMs) || ageMs < RPC_DISCOVERY_PARSE_GRACE_MS) {
168073
+ return unreadableDiscovery(portFile, "parse");
168074
+ }
168075
+ staleFiles.push(portFile);
168076
+ const reason = invalidDiscoveryReason(raw);
168077
+ log3(`[magic-context] removing stale RPC discovery file ${portFile}: ${reason} record older than 10 minutes`);
168078
+ return null;
168079
+ }
168037
168080
  function inspectRpcServerDiscovery(storageDir) {
168038
168081
  const rpcRoot = join12(storageDir, "rpc");
168039
168082
  let projectEntries;
168040
168083
  try {
168041
- projectEntries = readdirSync2(rpcRoot, { withFileTypes: true });
168084
+ projectEntries = rpcDiscoveryFs.readdirSync(rpcRoot, { withFileTypes: true });
168042
168085
  } catch (error51) {
168043
168086
  if (error51.code === "ENOENT") {
168044
168087
  return { state: "absent", serverPids: [], staleFiles: [] };
168045
168088
  }
168046
- return unreadableDiscovery(rpcRoot);
168089
+ return unreadableDiscovery(rpcRoot, "io");
168047
168090
  }
168048
168091
  const portFiles = [];
168049
168092
  for (const projectEntry of projectEntries) {
@@ -168052,9 +168095,11 @@ function inspectRpcServerDiscovery(storageDir) {
168052
168095
  const projectDir = join12(rpcRoot, projectEntry.name);
168053
168096
  let entries;
168054
168097
  try {
168055
- entries = readdirSync2(projectDir);
168056
- } catch {
168057
- return unreadableDiscovery(projectDir);
168098
+ entries = rpcDiscoveryFs.readdirSync(projectDir);
168099
+ } catch (error51) {
168100
+ if (error51.code === "ENOENT")
168101
+ continue;
168102
+ return unreadableDiscovery(projectDir, "io");
168058
168103
  }
168059
168104
  for (const entry of entries) {
168060
168105
  if (entry === "port" || entry.startsWith("port-") && entry.endsWith(".json")) {
@@ -168070,15 +168115,21 @@ function inspectRpcServerDiscovery(storageDir) {
168070
168115
  for (const portFile of portFiles) {
168071
168116
  let raw;
168072
168117
  try {
168073
- raw = readFileSync10(portFile, "utf8");
168074
- } catch {
168075
- return unreadableDiscovery(portFile);
168118
+ raw = rpcDiscoveryFs.readFileSync(portFile, "utf8");
168119
+ } catch (error51) {
168120
+ if (error51.code === "ENOENT")
168121
+ continue;
168122
+ return unreadableDiscovery(portFile, "io");
168076
168123
  }
168077
168124
  const filename = basename3(portFile);
168078
168125
  const pidFromName = /^port-(\d+)/.exec(filename)?.[1];
168079
- const record2 = parseRpcPortFile(raw, pidFromName ? Number(pidFromName) : 0);
168126
+ const fallbackPid = pidFromName ? Number(pidFromName) : 0;
168127
+ const record2 = parseRpcPortFile(raw, fallbackPid);
168080
168128
  if (!record2 || !Number.isInteger(record2.pid) || record2.pid <= 0) {
168081
- return unreadableDiscovery(portFile);
168129
+ const junk = classifyJunkDiscovery(portFile, raw, staleFiles);
168130
+ if (junk)
168131
+ return junk;
168132
+ continue;
168082
168133
  }
168083
168134
  if (isPidAlive(record2.pid) && isPidIdentityPlausible(record2))
168084
168135
  pids.add(record2.pid);
@@ -168087,9 +168138,9 @@ function inspectRpcServerDiscovery(storageDir) {
168087
168138
  }
168088
168139
  for (const staleFile of staleFiles) {
168089
168140
  try {
168090
- unlinkSync2(staleFile);
168141
+ rpcDiscoveryFs.unlinkSync(staleFile);
168091
168142
  } catch {
168092
- return unreadableDiscovery(staleFile);
168143
+ return unreadableDiscovery(staleFile, "io");
168093
168144
  }
168094
168145
  }
168095
168146
  const serverPids = [...pids].sort((a3, b2) => a3 - b2);
@@ -168104,7 +168155,7 @@ function getLiveMigrationBlockingProcesses(storageDir) {
168104
168155
  return [];
168105
168156
  return discovery.serverPids.map((pid) => ({ harness: "OpenCode server", pid }));
168106
168157
  }
168107
- var databases, pendingAsyncOpens, persistenceByDatabase, persistenceErrorByDatabase, pathByDatabase, LATEST_SUPPORTED_VERSION = 74, PERMISSIONS_ENFORCEABLE;
168158
+ var databases, pendingAsyncOpens, persistenceByDatabase, persistenceErrorByDatabase, pathByDatabase, LATEST_SUPPORTED_VERSION = 75, PERMISSIONS_ENFORCEABLE, RPC_DISCOVERY_PARSE_GRACE_MS, defaultRpcDiscoveryFs, rpcDiscoveryFs;
168108
168159
  var init_storage_db = __esm(async () => {
168109
168160
  init_data_path();
168110
168161
  init_logger();
@@ -168122,6 +168173,14 @@ var init_storage_db = __esm(async () => {
168122
168173
  persistenceErrorByDatabase = new WeakMap;
168123
168174
  pathByDatabase = new WeakMap;
168124
168175
  PERMISSIONS_ENFORCEABLE = process.platform !== "win32";
168176
+ RPC_DISCOVERY_PARSE_GRACE_MS = 10 * 60 * 1000;
168177
+ defaultRpcDiscoveryFs = {
168178
+ readdirSync: (path4, options) => options?.withFileTypes ? readdirSync2(path4, { withFileTypes: true }) : readdirSync2(path4),
168179
+ readFileSync: (path4, encoding) => String(readFileSync10(path4, encoding)),
168180
+ statSync: (path4) => ({ mtimeMs: statSync7(path4).mtimeMs }),
168181
+ unlinkSync: (path4) => unlinkSync2(path4)
168182
+ };
168183
+ rpcDiscoveryFs = defaultRpcDiscoveryFs;
168125
168184
  });
168126
168185
 
168127
168186
  // src/lib/database-access.ts
@@ -170141,7 +170200,7 @@ import {
170141
170200
  mkdirSync as mkdirSync8,
170142
170201
  readFileSync as readFileSync11,
170143
170202
  renameSync as renameSync3,
170144
- statSync as statSync7,
170203
+ statSync as statSync8,
170145
170204
  writeFileSync as writeFileSync6
170146
170205
  } from "node:fs";
170147
170206
  import { dirname as dirname11, join as join17 } from "node:path";
@@ -170177,8 +170236,8 @@ function writeTuiConfigAtomic(configPath, config2) {
170177
170236
  const tmpPath = `${configPath}.tmp`;
170178
170237
  writeFileSync6(tmpPath, body);
170179
170238
  try {
170180
- if (statSync7(configPath, { throwIfNoEntry: false })?.isFile()) {
170181
- chmodSync3(tmpPath, statSync7(configPath).mode & 511);
170239
+ if (statSync8(configPath, { throwIfNoEntry: false })?.isFile()) {
170240
+ chmodSync3(tmpPath, statSync8(configPath).mode & 511);
170182
170241
  }
170183
170242
  } catch {}
170184
170243
  renameSync3(tmpPath, configPath);
@@ -170458,7 +170517,7 @@ var init_redaction2 = __esm(() => {
170458
170517
  });
170459
170518
 
170460
170519
  // src/lib/diagnostics-opencode.ts
170461
- import { existsSync as existsSync20, readdirSync as readdirSync3, readFileSync as readFileSync12, statSync as statSync8 } from "node:fs";
170520
+ import { existsSync as existsSync20, readdirSync as readdirSync3, readFileSync as readFileSync12, statSync as statSync9 } from "node:fs";
170462
170521
  import { createRequire as createRequire2 } from "node:module";
170463
170522
  import { homedir as homedir14 } from "node:os";
170464
170523
  import { join as join18 } from "node:path";
@@ -170497,7 +170556,7 @@ function getStorageDir() {
170497
170556
  }
170498
170557
  function fileSize(path6) {
170499
170558
  try {
170500
- return existsSync20(path6) ? statSync8(path6).size : 0;
170559
+ return existsSync20(path6) ? statSync9(path6).size : 0;
170501
170560
  } catch {
170502
170561
  return 0;
170503
170562
  }
@@ -170574,7 +170633,7 @@ function listDumpsInDir(dir, limit) {
170574
170633
  return { count: 0, recent: [] };
170575
170634
  try {
170576
170635
  const entries = readdirSync3(dir).filter((name2) => name2.endsWith(".xml")).map((name2) => {
170577
- const stat = statSync8(join18(dir, name2));
170636
+ const stat = statSync9(join18(dir, name2));
170578
170637
  return {
170579
170638
  name: name2,
170580
170639
  mtime: stat.mtimeMs,
@@ -170780,7 +170839,7 @@ async function collectDiagnostics() {
170780
170839
  const storageDirPath = getStorageDir();
170781
170840
  const contextDbPath = join18(storageDirPath, "context.db");
170782
170841
  const logPath = getMagicContextLogPath2("opencode");
170783
- const logFileSize = existsSync20(logPath) ? statSync8(logPath).size : 0;
170842
+ const logFileSize = existsSync20(logPath) ? statSync9(logPath).size : 0;
170784
170843
  let compactionEnabled = false;
170785
170844
  try {
170786
170845
  compactionEnabled = isCompactionEnabled(loadPluginConfig(process.cwd()));
@@ -171679,6 +171738,15 @@ function formatFailClosedBlockingProcesses(processes) {
171679
171738
  }
171680
171739
  function formatFailClosedBlockingMessage(reason) {
171681
171740
  if (reason.kind === "migration_guard") {
171741
+ if (reason.unreadableFile) {
171742
+ const arm = reason.unreadableArm ?? "io";
171743
+ const recovery = arm === "io" ? `If no OpenCode server is running, it is safe to delete ${reason.unreadableFile} and retry.` : `The file may be a recent incomplete write; retry after the file is older than the ten-minute grace window, or stop OpenCode before deleting it.`;
171744
+ return [
171745
+ `Magic Context cannot migrate the shared database because RPC discovery file ${reason.unreadableFile} is uncertain (${arm} arm), so the absence of a live OpenCode server cannot be proven.`,
171746
+ recovery,
171747
+ `Recovery: ${FAIL_CLOSED_DOCTOR_COMMAND}`
171748
+ ].join(" ");
171749
+ }
171682
171750
  return [
171683
171751
  `Magic Context cannot migrate the shared database because ${formatFailClosedBlockingProcesses(reason.blockingProcesses)} may be running an older Magic Context build that would fail against the migrated database.`,
171684
171752
  "Restart the blocking process (it will pick up the new build and migrate on start), or shut it down and retry.",
@@ -171836,7 +171904,7 @@ var init_doctor_opencode_cache = __esm(() => {
171836
171904
 
171837
171905
  // src/commands/doctor-opencode.ts
171838
171906
  import { execSync as execSync2, spawnSync as spawnSync3 } from "node:child_process";
171839
- import { existsSync as existsSync23, readFileSync as readFileSync15, statSync as statSync9 } from "node:fs";
171907
+ import { existsSync as existsSync23, readFileSync as readFileSync15, statSync as statSync10 } from "node:fs";
171840
171908
  import { createRequire as createRequire4 } from "node:module";
171841
171909
  import { homedir as homedir15 } from "node:os";
171842
171910
  import { join as join21 } from "node:path";
@@ -172704,7 +172772,7 @@ async function runDoctor(options = {}) {
172704
172772
  }
172705
172773
  const logPath = getMagicContextLogPath2("opencode");
172706
172774
  if (existsSync23(logPath)) {
172707
- const logStat = statSync9(logPath);
172775
+ const logStat = statSync10(logPath);
172708
172776
  const sizeKb = (logStat.size / 1024).toFixed(0);
172709
172777
  log2.info(`Log file: ${logPath} (${sizeKb} KB)`);
172710
172778
  } else {
@@ -173007,7 +173075,7 @@ var init_config2 = __esm(() => {
173007
173075
 
173008
173076
  // src/lib/diagnostics-pi.ts
173009
173077
  import { createHash as createHash4 } from "node:crypto";
173010
- import { existsSync as existsSync25, readdirSync as readdirSync4, readFileSync as readFileSync17, statSync as statSync10 } from "node:fs";
173078
+ import { existsSync as existsSync25, readdirSync as readdirSync4, readFileSync as readFileSync17, statSync as statSync11 } from "node:fs";
173011
173079
  import { createRequire as createRequire5 } from "node:module";
173012
173080
  import { homedir as homedir16, userInfo as userInfo2 } from "node:os";
173013
173081
  import { join as join22 } from "node:path";
@@ -173025,7 +173093,7 @@ function getSelfVersion3() {
173025
173093
  }
173026
173094
  function fileSize2(path6) {
173027
173095
  try {
173028
- return existsSync25(path6) ? statSync10(path6).size : 0;
173096
+ return existsSync25(path6) ? statSync11(path6).size : 0;
173029
173097
  } catch {
173030
173098
  return 0;
173031
173099
  }
@@ -173133,7 +173201,7 @@ function collectPiRecentSessions() {
173133
173201
  }
173134
173202
  for (const file2 of files) {
173135
173203
  try {
173136
- const mtime = statSync10(join22(slugDir, file2)).mtimeMs;
173204
+ const mtime = statSync11(join22(slugDir, file2)).mtimeMs;
173137
173205
  const sessionId = file2.replace(/\.jsonl$/, "");
173138
173206
  candidates.push({ sessionId, directory, mtime });
173139
173207
  } catch {}
@@ -173188,7 +173256,7 @@ function listDumpsInDir2(dir, limit) {
173188
173256
  return { count: 0, recent: [] };
173189
173257
  try {
173190
173258
  const entries = readdirSync4(dir).filter((name2) => name2.endsWith(".xml")).map((name2) => {
173191
- const stat = statSync10(join22(dir, name2));
173259
+ const stat = statSync11(join22(dir, name2));
173192
173260
  return {
173193
173261
  name: name2,
173194
173262
  mtime: stat.mtimeMs,
@@ -173262,7 +173330,7 @@ async function collectDiagnostics2(cwd = process.cwd()) {
173262
173330
  const storageDirPath = getMagicContextStorageDir();
173263
173331
  const dbPath = join22(storageDirPath, "context.db");
173264
173332
  const logPath = getMagicContextLogPath2("pi");
173265
- const logFileSize = existsSync25(logPath) ? statSync10(logPath).size : 0;
173333
+ const logFileSize = existsSync25(logPath) ? statSync11(logPath).size : 0;
173266
173334
  const otherPiExtensions = packages.filter((entry) => !isPiMagicContextPackageEntry(entry)).map(describePiPackageEntry);
173267
173335
  const recentSessions = collectPiRecentSessions();
173268
173336
  const historianDumps = collectPiHistorianDumps(recentSessions);
@@ -173474,7 +173542,7 @@ var init_logs_pi = __esm(() => {
173474
173542
 
173475
173543
  // src/commands/doctor-pi.ts
173476
173544
  import { execFileSync as execFileSync5, spawnSync as spawnSync4 } from "node:child_process";
173477
- import { existsSync as existsSync26, mkdirSync as mkdirSync9, readdirSync as readdirSync5, readFileSync as readFileSync19, rmSync as rmSync3, statSync as statSync11 } from "node:fs";
173545
+ import { existsSync as existsSync26, mkdirSync as mkdirSync9, readdirSync as readdirSync5, readFileSync as readFileSync19, rmSync as rmSync3, statSync as statSync12 } from "node:fs";
173478
173546
  import { createRequire as createRequire6 } from "node:module";
173479
173547
  import { homedir as homedir17 } from "node:os";
173480
173548
  import { dirname as dirname13, isAbsolute as isAbsolute4, join as join24 } from "node:path";
@@ -173690,7 +173758,7 @@ function findPiMagicContextCacheDirs(cwd, expectedVersion, force = false) {
173690
173758
  return;
173691
173759
  let stat;
173692
173760
  try {
173693
- stat = statSync11(path6);
173761
+ stat = statSync12(path6);
173694
173762
  } catch {
173695
173763
  return;
173696
173764
  }
@@ -173953,7 +174021,7 @@ async function runHealthChecks(options) {
173953
174021
  }
173954
174022
  const logPath = getMagicContextLogPath2("pi");
173955
174023
  if (existsSync26(logPath)) {
173956
- const stat = statSync11(logPath);
174024
+ const stat = statSync12(logPath);
173957
174025
  const sizeKb = (stat.size / 1024).toFixed(0);
173958
174026
  const lines = readFileSync19(logPath, "utf-8").split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
173959
174027
  add(results, "info", `Log file: ${logPath} (${sizeKb} KB)`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/magic-context",
3
- "version": "0.34.1",
3
+ "version": "0.34.2",
4
4
  "description": "Unified CLI for Magic Context — setup, doctor, and migration across OpenCode and Pi",
5
5
  "keywords": [
6
6
  "opencode",