@adhdev/daemon-standalone 0.9.82-rc.183 → 0.9.82-rc.185

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/dist/index.js CHANGED
@@ -46224,9 +46224,10 @@ ${lastSnapshot}`;
46224
46224
  const windowMs = typeof src.recent_window_ms === "number" ? src.recent_window_ms : 5 * 6e4;
46225
46225
  const filePat = src.file_pattern ? globToRegex(src.file_pattern) : /.*\.jsonl$/;
46226
46226
  const sessionFloor = typeof input.sessionStartedAtMs === "number" ? input.sessionStartedAtMs : 0;
46227
+ const workspaceHint = typeof input.workspace === "string" && input.workspace.trim() ? input.workspace.trim() : "";
46227
46228
  let sourcePath = null;
46228
46229
  if (resolved.includes("*")) {
46229
- sourcePath = newestRecentFileAcrossGlob(resolved, filePat, windowMs, sessionFloor);
46230
+ sourcePath = pickSessionBoundFileAcrossGlob(resolved, filePat, windowMs, sessionFloor, workspaceHint) || newestRecentFileAcrossGlob(resolved, filePat, windowMs, sessionFloor);
46230
46231
  } else {
46231
46232
  let stat2 = null;
46232
46233
  try {
@@ -46236,10 +46237,10 @@ ${lastSnapshot}`;
46236
46237
  if (stat2 && stat2.isFile()) {
46237
46238
  sourcePath = resolved;
46238
46239
  } else if (stat2 && stat2.isDirectory()) {
46239
- sourcePath = newestRecentFile(resolved, filePat, windowMs, sessionFloor);
46240
+ sourcePath = pickSessionBoundFile(resolved, filePat, windowMs, sessionFloor, workspaceHint) || newestRecentFile(resolved, filePat, windowMs, sessionFloor);
46240
46241
  }
46241
46242
  if (!sourcePath && hasDateTemplateSegment(src.path)) {
46242
- sourcePath = newestRecentFileAcrossDateWindow(src.path, input, filePat, windowMs, sessionFloor);
46243
+ sourcePath = pickSessionBoundFileAcrossDateWindow(src.path, input, filePat, windowMs, sessionFloor, workspaceHint) || newestRecentFileAcrossDateWindow(src.path, input, filePat, windowMs, sessionFloor);
46243
46244
  }
46244
46245
  }
46245
46246
  if (!sourcePath) return null;
@@ -46551,6 +46552,105 @@ ${lastSnapshot}`;
46551
46552
  return 0;
46552
46553
  }
46553
46554
  }
46555
+ function readCandidateSessionMeta(filePath) {
46556
+ try {
46557
+ const fd = fs13.openSync(filePath, "r");
46558
+ try {
46559
+ const buf = Buffer.alloc(8192);
46560
+ const bytes = fs13.readSync(fd, buf, 0, buf.length, 0);
46561
+ if (bytes <= 0) return null;
46562
+ const text = buf.subarray(0, bytes).toString("utf8");
46563
+ const nl = text.indexOf("\n");
46564
+ const firstLine = (nl >= 0 ? text.slice(0, nl) : text).trim();
46565
+ if (!firstLine) return null;
46566
+ const parsed = JSON.parse(firstLine);
46567
+ if (String(parsed.type ?? "") !== "session_meta") return null;
46568
+ const payload = parsed.payload && typeof parsed.payload === "object" ? parsed.payload : null;
46569
+ if (!payload) return null;
46570
+ const cwd = typeof payload.cwd === "string" ? payload.cwd : void 0;
46571
+ const tsRaw = payload.timestamp;
46572
+ const tsMs = typeof tsRaw === "string" ? Date.parse(tsRaw) : typeof tsRaw === "number" ? tsRaw < 1e12 ? Math.floor(tsRaw * 1e3) : Math.floor(tsRaw) : NaN;
46573
+ return {
46574
+ cwd,
46575
+ sessionTimestampMs: Number.isFinite(tsMs) ? tsMs : void 0
46576
+ };
46577
+ } finally {
46578
+ fs13.closeSync(fd);
46579
+ }
46580
+ } catch {
46581
+ return null;
46582
+ }
46583
+ }
46584
+ function pickBoundFromEntries(candidatePaths, sessionFloorMs, workspaceHint) {
46585
+ if (!sessionFloorMs || !workspaceHint || candidatePaths.length === 0) return null;
46586
+ let workspaceResolved = workspaceHint;
46587
+ try {
46588
+ workspaceResolved = fs13.realpathSync(workspaceHint);
46589
+ } catch {
46590
+ }
46591
+ let best = null;
46592
+ for (const p of candidatePaths) {
46593
+ const meta3 = readCandidateSessionMeta(p);
46594
+ if (!meta3 || !meta3.cwd || meta3.sessionTimestampMs == null) continue;
46595
+ let candidateCwd = meta3.cwd;
46596
+ try {
46597
+ candidateCwd = fs13.realpathSync(meta3.cwd);
46598
+ } catch {
46599
+ }
46600
+ if (candidateCwd !== workspaceResolved && meta3.cwd !== workspaceHint) continue;
46601
+ const diff = Math.abs(meta3.sessionTimestampMs - sessionFloorMs);
46602
+ if (diff > SPAWN_BIND_GRACE_MS) continue;
46603
+ if (!best || diff < best.diff) best = { p, diff };
46604
+ }
46605
+ return best ? best.p : null;
46606
+ }
46607
+ function listMatchingFiles(dir, pattern) {
46608
+ let entries;
46609
+ try {
46610
+ entries = fs13.readdirSync(dir, { withFileTypes: true });
46611
+ } catch {
46612
+ return [];
46613
+ }
46614
+ const out = [];
46615
+ for (const e of entries) {
46616
+ if (!e.isFile() || !pattern.test(e.name)) continue;
46617
+ out.push(path25.join(dir, e.name));
46618
+ }
46619
+ return out;
46620
+ }
46621
+ function pickSessionBoundFile(dir, pattern, windowMs, sessionFloorMs, workspaceHint) {
46622
+ if (!sessionFloorMs || !workspaceHint) return null;
46623
+ const cutoff = Math.max(Date.now() - windowMs, sessionFloorMs - SPAWN_BIND_GRACE_MS);
46624
+ const files = listMatchingFiles(dir, pattern).filter((p) => safeMtimeMs(p) >= cutoff);
46625
+ return pickBoundFromEntries(files, sessionFloorMs, workspaceHint);
46626
+ }
46627
+ function pickSessionBoundFileAcrossGlob(template, pattern, windowMs, sessionFloorMs, workspaceHint) {
46628
+ if (!sessionFloorMs || !workspaceHint) return null;
46629
+ const dirs = expandDirGlob(template);
46630
+ const cutoff = Math.max(Date.now() - windowMs, sessionFloorMs - SPAWN_BIND_GRACE_MS);
46631
+ const files = [];
46632
+ for (const d of dirs) {
46633
+ for (const p of listMatchingFiles(d, pattern)) {
46634
+ if (safeMtimeMs(p) >= cutoff) files.push(p);
46635
+ }
46636
+ }
46637
+ return pickBoundFromEntries(files, sessionFloorMs, workspaceHint);
46638
+ }
46639
+ function pickSessionBoundFileAcrossDateWindow(template, input, pattern, windowMs, sessionFloorMs, workspaceHint) {
46640
+ if (!sessionFloorMs || !workspaceHint) return null;
46641
+ const cutoff = Math.max(Date.now() - windowMs, sessionFloorMs - SPAWN_BIND_GRACE_MS);
46642
+ const files = [];
46643
+ for (let dayOffset = 0; dayOffset < 3; dayOffset += 1) {
46644
+ const dayMs = sessionFloorMs - dayOffset * 24 * 60 * 60 * 1e3;
46645
+ const dayInput = { ...input, sessionStartedAtMs: sessionFloorMs };
46646
+ const resolved = expandPathForDate(template, dayInput, new Date(dayMs));
46647
+ if (!resolved) continue;
46648
+ for (const p of listMatchingFiles(resolved, pattern)) {
46649
+ if (safeMtimeMs(p) >= cutoff) files.push(p);
46650
+ }
46651
+ }
46652
+ return pickBoundFromEntries(files, sessionFloorMs, workspaceHint);
46653
+ }
46554
46654
  function jsonPathGet(record2, expr) {
46555
46655
  if (typeof expr !== "string") return void 0;
46556
46656
  if (expr.includes("||")) {
@@ -46754,6 +46854,7 @@ ${lastSnapshot}`;
46754
46854
  var os18;
46755
46855
  var path25;
46756
46856
  var UUID_RE;
46857
+ var SPAWN_BIND_GRACE_MS;
46757
46858
  var init_native_history_executor = __esm2({
46758
46859
  "src/providers/spec/native-history-executor.ts"() {
46759
46860
  "use strict";
@@ -46761,6 +46862,7 @@ ${lastSnapshot}`;
46761
46862
  os18 = __toESM2(require("os"));
46762
46863
  path25 = __toESM2(require("path"));
46763
46864
  UUID_RE = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i;
46865
+ SPAWN_BIND_GRACE_MS = 1e4;
46764
46866
  }
46765
46867
  });
46766
46868
  function extractTimestampValue(value) {
@@ -87029,6 +87131,21 @@ var StandaloneServer = class {
87029
87131
  }
87030
87132
  };
87031
87133
  async function main() {
87134
+ if (!process.env.ADHDEV_CONFIG_DIR || !process.env.ADHDEV_CONFIG_DIR.trim()) {
87135
+ process.env.ADHDEV_CONFIG_DIR = path4.join(os5.homedir(), ".adhdev-standalone");
87136
+ }
87137
+ try {
87138
+ const isolatedDir = process.env.ADHDEV_CONFIG_DIR;
87139
+ const legacyLedger = path4.join(os5.homedir(), ".adhdev", "mesh-ledger");
87140
+ const isolatedExists = fs3.existsSync(isolatedDir);
87141
+ const isolatedEmpty = !isolatedExists || fs3.readdirSync(isolatedDir).filter((name) => name !== ".DS_Store").length === 0;
87142
+ const legacyHasLedger = fs3.existsSync(legacyLedger) && fs3.readdirSync(legacyLedger).some((name) => name.endsWith(".jsonl"));
87143
+ if (isolatedEmpty && legacyHasLedger) {
87144
+ const line = "\u2139 standalone now stores its state under " + isolatedDir + ". If you want to carry over prior mesh ledger from ~/.adhdev/mesh-ledger, copy ~/.adhdev/mesh-ledger to " + path4.join(isolatedDir, "mesh-ledger") + " once and restart. (Set ADHDEV_CONFIG_DIR=~/.adhdev to keep the legacy location.)";
87145
+ process.stderr.write(line + "\n");
87146
+ }
87147
+ } catch {
87148
+ }
87032
87149
  const helperMode = await (0, import_daemon_core2.maybeRunDaemonUpgradeHelperFromEnv)();
87033
87150
  if (helperMode) {
87034
87151
  return;