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