@adhdev/daemon-core 0.9.82-rc.182 → 0.9.82-rc.184

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.mjs CHANGED
@@ -11397,9 +11397,10 @@ function executeJsonl(src, input) {
11397
11397
  const windowMs = typeof src.recent_window_ms === "number" ? src.recent_window_ms : 5 * 6e4;
11398
11398
  const filePat = src.file_pattern ? globToRegex(src.file_pattern) : /.*\.jsonl$/;
11399
11399
  const sessionFloor = typeof input.sessionStartedAtMs === "number" ? input.sessionStartedAtMs : 0;
11400
+ const workspaceHint = typeof input.workspace === "string" && input.workspace.trim() ? input.workspace.trim() : "";
11400
11401
  let sourcePath = null;
11401
11402
  if (resolved.includes("*")) {
11402
- sourcePath = newestRecentFileAcrossGlob(resolved, filePat, windowMs, sessionFloor);
11403
+ sourcePath = pickSessionBoundFileAcrossGlob(resolved, filePat, windowMs, sessionFloor, workspaceHint) || newestRecentFileAcrossGlob(resolved, filePat, windowMs, sessionFloor);
11403
11404
  } else {
11404
11405
  let stat2 = null;
11405
11406
  try {
@@ -11409,10 +11410,10 @@ function executeJsonl(src, input) {
11409
11410
  if (stat2 && stat2.isFile()) {
11410
11411
  sourcePath = resolved;
11411
11412
  } else if (stat2 && stat2.isDirectory()) {
11412
- sourcePath = newestRecentFile(resolved, filePat, windowMs, sessionFloor);
11413
+ sourcePath = pickSessionBoundFile(resolved, filePat, windowMs, sessionFloor, workspaceHint) || newestRecentFile(resolved, filePat, windowMs, sessionFloor);
11413
11414
  }
11414
11415
  if (!sourcePath && hasDateTemplateSegment(src.path)) {
11415
- sourcePath = newestRecentFileAcrossDateWindow(src.path, input, filePat, windowMs, sessionFloor);
11416
+ sourcePath = pickSessionBoundFileAcrossDateWindow(src.path, input, filePat, windowMs, sessionFloor, workspaceHint) || newestRecentFileAcrossDateWindow(src.path, input, filePat, windowMs, sessionFloor);
11416
11417
  }
11417
11418
  }
11418
11419
  if (!sourcePath) return null;
@@ -11724,6 +11725,105 @@ function safeMtimeMs(p) {
11724
11725
  return 0;
11725
11726
  }
11726
11727
  }
11728
+ function readCandidateSessionMeta(filePath) {
11729
+ try {
11730
+ const fd = fs13.openSync(filePath, "r");
11731
+ try {
11732
+ const buf = Buffer.alloc(8192);
11733
+ const bytes = fs13.readSync(fd, buf, 0, buf.length, 0);
11734
+ if (bytes <= 0) return null;
11735
+ const text = buf.subarray(0, bytes).toString("utf8");
11736
+ const nl = text.indexOf("\n");
11737
+ const firstLine = (nl >= 0 ? text.slice(0, nl) : text).trim();
11738
+ if (!firstLine) return null;
11739
+ const parsed = JSON.parse(firstLine);
11740
+ if (String(parsed.type ?? "") !== "session_meta") return null;
11741
+ const payload = parsed.payload && typeof parsed.payload === "object" ? parsed.payload : null;
11742
+ if (!payload) return null;
11743
+ const cwd = typeof payload.cwd === "string" ? payload.cwd : void 0;
11744
+ const tsRaw = payload.timestamp;
11745
+ const tsMs = typeof tsRaw === "string" ? Date.parse(tsRaw) : typeof tsRaw === "number" ? tsRaw < 1e12 ? Math.floor(tsRaw * 1e3) : Math.floor(tsRaw) : NaN;
11746
+ return {
11747
+ cwd,
11748
+ sessionTimestampMs: Number.isFinite(tsMs) ? tsMs : void 0
11749
+ };
11750
+ } finally {
11751
+ fs13.closeSync(fd);
11752
+ }
11753
+ } catch {
11754
+ return null;
11755
+ }
11756
+ }
11757
+ function pickBoundFromEntries(candidatePaths, sessionFloorMs, workspaceHint) {
11758
+ if (!sessionFloorMs || !workspaceHint || candidatePaths.length === 0) return null;
11759
+ let workspaceResolved = workspaceHint;
11760
+ try {
11761
+ workspaceResolved = fs13.realpathSync(workspaceHint);
11762
+ } catch {
11763
+ }
11764
+ let best = null;
11765
+ for (const p of candidatePaths) {
11766
+ const meta = readCandidateSessionMeta(p);
11767
+ if (!meta || !meta.cwd || meta.sessionTimestampMs == null) continue;
11768
+ let candidateCwd = meta.cwd;
11769
+ try {
11770
+ candidateCwd = fs13.realpathSync(meta.cwd);
11771
+ } catch {
11772
+ }
11773
+ if (candidateCwd !== workspaceResolved && meta.cwd !== workspaceHint) continue;
11774
+ const diff = Math.abs(meta.sessionTimestampMs - sessionFloorMs);
11775
+ if (diff > SPAWN_BIND_GRACE_MS) continue;
11776
+ if (!best || diff < best.diff) best = { p, diff };
11777
+ }
11778
+ return best ? best.p : null;
11779
+ }
11780
+ function listMatchingFiles(dir, pattern) {
11781
+ let entries;
11782
+ try {
11783
+ entries = fs13.readdirSync(dir, { withFileTypes: true });
11784
+ } catch {
11785
+ return [];
11786
+ }
11787
+ const out = [];
11788
+ for (const e of entries) {
11789
+ if (!e.isFile() || !pattern.test(e.name)) continue;
11790
+ out.push(path25.join(dir, e.name));
11791
+ }
11792
+ return out;
11793
+ }
11794
+ function pickSessionBoundFile(dir, pattern, windowMs, sessionFloorMs, workspaceHint) {
11795
+ if (!sessionFloorMs || !workspaceHint) return null;
11796
+ const cutoff = Math.max(Date.now() - windowMs, sessionFloorMs - SPAWN_BIND_GRACE_MS);
11797
+ const files = listMatchingFiles(dir, pattern).filter((p) => safeMtimeMs(p) >= cutoff);
11798
+ return pickBoundFromEntries(files, sessionFloorMs, workspaceHint);
11799
+ }
11800
+ function pickSessionBoundFileAcrossGlob(template, pattern, windowMs, sessionFloorMs, workspaceHint) {
11801
+ if (!sessionFloorMs || !workspaceHint) return null;
11802
+ const dirs = expandDirGlob(template);
11803
+ const cutoff = Math.max(Date.now() - windowMs, sessionFloorMs - SPAWN_BIND_GRACE_MS);
11804
+ const files = [];
11805
+ for (const d of dirs) {
11806
+ for (const p of listMatchingFiles(d, pattern)) {
11807
+ if (safeMtimeMs(p) >= cutoff) files.push(p);
11808
+ }
11809
+ }
11810
+ return pickBoundFromEntries(files, sessionFloorMs, workspaceHint);
11811
+ }
11812
+ function pickSessionBoundFileAcrossDateWindow(template, input, pattern, windowMs, sessionFloorMs, workspaceHint) {
11813
+ if (!sessionFloorMs || !workspaceHint) return null;
11814
+ const cutoff = Math.max(Date.now() - windowMs, sessionFloorMs - SPAWN_BIND_GRACE_MS);
11815
+ const files = [];
11816
+ for (let dayOffset = 0; dayOffset < 3; dayOffset += 1) {
11817
+ const dayMs = sessionFloorMs - dayOffset * 24 * 60 * 60 * 1e3;
11818
+ const dayInput = { ...input, sessionStartedAtMs: sessionFloorMs };
11819
+ const resolved = expandPathForDate(template, dayInput, new Date(dayMs));
11820
+ if (!resolved) continue;
11821
+ for (const p of listMatchingFiles(resolved, pattern)) {
11822
+ if (safeMtimeMs(p) >= cutoff) files.push(p);
11823
+ }
11824
+ }
11825
+ return pickBoundFromEntries(files, sessionFloorMs, workspaceHint);
11826
+ }
11727
11827
  function jsonPathGet(record, expr) {
11728
11828
  if (typeof expr !== "string") return void 0;
11729
11829
  if (expr.includes("||")) {
@@ -11923,11 +12023,12 @@ function evalTerm(t, record) {
11923
12023
  }
11924
12024
  return t.negate ? !result : result;
11925
12025
  }
11926
- var UUID_RE;
12026
+ var UUID_RE, SPAWN_BIND_GRACE_MS;
11927
12027
  var init_native_history_executor = __esm({
11928
12028
  "src/providers/spec/native-history-executor.ts"() {
11929
12029
  "use strict";
11930
12030
  UUID_RE = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i;
12031
+ SPAWN_BIND_GRACE_MS = 1e4;
11931
12032
  }
11932
12033
  });
11933
12034