@kanadego/dsh-heartbeat 2.0.0 → 2.1.0

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 (50) hide show
  1. package/README.md +86 -23
  2. package/client.js +952 -238
  3. package/config/interests.json +1 -1
  4. package/config/policy.json +3 -2
  5. package/dist/{chunk-TVX4HV4Y.js → chunk-63E5OYSI.js} +56 -5
  6. package/dist/chunk-63E5OYSI.js.map +1 -0
  7. package/dist/{chunk-NCPKOWS7.js → chunk-AT32XCCI.js} +2 -2
  8. package/dist/{chunk-DC5HMCJC.js → chunk-DLOYERAK.js} +3 -3
  9. package/dist/{chunk-544QOJN5.js → chunk-ECXYNTEV.js} +2 -2
  10. package/dist/{chunk-IFRMKVIC.js → chunk-HE6IF6OU.js} +5 -2
  11. package/dist/chunk-HE6IF6OU.js.map +1 -0
  12. package/dist/{chunk-DZ7YCFGX.js → chunk-KZLVR4J7.js} +2 -2
  13. package/dist/{chunk-6XM6KY7M.js → chunk-LPPRI53J.js} +39 -37
  14. package/dist/chunk-LPPRI53J.js.map +1 -0
  15. package/dist/chunk-LQKOYTRL.js +59 -0
  16. package/dist/chunk-LQKOYTRL.js.map +1 -0
  17. package/dist/{chunk-AKIJ4LS2.js → chunk-M72T5MZU.js} +44 -1
  18. package/dist/chunk-M72T5MZU.js.map +1 -0
  19. package/dist/{chunk-6MVKWICX.js → chunk-XCMGPIUQ.js} +99 -23
  20. package/dist/chunk-XCMGPIUQ.js.map +1 -0
  21. package/dist/cli/index.js +51 -11
  22. package/dist/cli/index.js.map +1 -1
  23. package/dist/focus-store-RNHBAFYS.js +20 -0
  24. package/dist/{inbox-OWBK6PE2.js → inbox-ZLX2MH7A.js} +3 -3
  25. package/dist/index.d.ts +16 -3
  26. package/dist/index.js +119 -69
  27. package/dist/index.js.map +1 -1
  28. package/dist/{migrate-TQ5PB2DI.js → migrate-XFZU5EBE.js} +3 -3
  29. package/dist/{orchestrator-P7AEWVYR.js → orchestrator-MNFCPH43.js} +27 -8
  30. package/dist/{pool-2T6TVS2L.js → pool-MAABBHC4.js} +9 -3
  31. package/dist/{snapshot-F5B2DD56.js → snapshot-SU2VGM4C.js} +4 -4
  32. package/dist/snapshot-SU2VGM4C.js.map +1 -0
  33. package/icon.svg +11 -0
  34. package/locale/en.json +6 -0
  35. package/locale/zh.json +6 -0
  36. package/package.json +12 -6
  37. package/dist/chunk-6MVKWICX.js.map +0 -1
  38. package/dist/chunk-6XM6KY7M.js.map +0 -1
  39. package/dist/chunk-AKIJ4LS2.js.map +0 -1
  40. package/dist/chunk-IFRMKVIC.js.map +0 -1
  41. package/dist/chunk-TVX4HV4Y.js.map +0 -1
  42. /package/dist/{chunk-NCPKOWS7.js.map → chunk-AT32XCCI.js.map} +0 -0
  43. /package/dist/{chunk-DC5HMCJC.js.map → chunk-DLOYERAK.js.map} +0 -0
  44. /package/dist/{chunk-544QOJN5.js.map → chunk-ECXYNTEV.js.map} +0 -0
  45. /package/dist/{chunk-DZ7YCFGX.js.map → chunk-KZLVR4J7.js.map} +0 -0
  46. /package/dist/{inbox-OWBK6PE2.js.map → focus-store-RNHBAFYS.js.map} +0 -0
  47. /package/dist/{migrate-TQ5PB2DI.js.map → inbox-ZLX2MH7A.js.map} +0 -0
  48. /package/dist/{orchestrator-P7AEWVYR.js.map → migrate-XFZU5EBE.js.map} +0 -0
  49. /package/dist/{pool-2T6TVS2L.js.map → orchestrator-MNFCPH43.js.map} +0 -0
  50. /package/dist/{snapshot-F5B2DD56.js.map → pool-MAABBHC4.js.map} +0 -0
@@ -7,6 +7,7 @@ import {
7
7
  ensureWeeklyAnchor,
8
8
  inHhMmWindow,
9
9
  ledgerFilePath,
10
+ localDayKey,
10
11
  minutesOfDay,
11
12
  pendingOlderThan,
12
13
  preferenceFilePath,
@@ -21,7 +22,7 @@ import {
21
22
  sendNewMessageHint,
22
23
  sendWeeklyReadyHint,
23
24
  weeklyDue
24
- } from "./chunk-6XM6KY7M.js";
25
+ } from "./chunk-LPPRI53J.js";
25
26
  import {
26
27
  getRuntime
27
28
  } from "./chunk-3QJJRXIR.js";
@@ -32,17 +33,20 @@ import {
32
33
  loadPool,
33
34
  seedsFilePath,
34
35
  surfaceSeed
35
- } from "./chunk-TVX4HV4Y.js";
36
+ } from "./chunk-63E5OYSI.js";
37
+ import {
38
+ resolveFocusUntil
39
+ } from "./chunk-LQKOYTRL.js";
36
40
  import {
37
41
  dedupeItems,
38
42
  inboxCount,
39
43
  inboxDrain,
40
44
  inboxFilePath,
41
45
  inboxRemoveIds
42
- } from "./chunk-DZ7YCFGX.js";
46
+ } from "./chunk-KZLVR4J7.js";
43
47
  import {
44
48
  snapshotIfDue
45
- } from "./chunk-DC5HMCJC.js";
49
+ } from "./chunk-DLOYERAK.js";
46
50
  import {
47
51
  appendAuditLine,
48
52
  applyOpsToDoc,
@@ -56,7 +60,7 @@ import {
56
60
  readAuditLines,
57
61
  readJournalTexts,
58
62
  runDeterministicAging
59
- } from "./chunk-544QOJN5.js";
63
+ } from "./chunk-ECXYNTEV.js";
60
64
  import {
61
65
  decryptFile,
62
66
  encryptFile,
@@ -67,7 +71,7 @@ import {
67
71
  saveJson,
68
72
  upgradeOneLegacyVaultFile,
69
73
  writeText
70
- } from "./chunk-AKIJ4LS2.js";
74
+ } from "./chunk-M72T5MZU.js";
71
75
  import {
72
76
  atomicWriteJsonSync,
73
77
  burnFileSync
@@ -1214,7 +1218,7 @@ async function runConsolidation(guard, paths, policy, llm, now = Date.now()) {
1214
1218
  rejected,
1215
1219
  entries: [...report.entries ?? [], ...aged.entries]
1216
1220
  });
1217
- const { addSeed: addSeed2, seedsFilePath: seedsFilePath2 } = await import("./pool-2T6TVS2L.js");
1221
+ const { addSeed: addSeed2, seedsFilePath: seedsFilePath2 } = await import("./pool-MAABBHC4.js");
1218
1222
  const seedAudit = (entry) => {
1219
1223
  try {
1220
1224
  appendAuditLine(paths.dataDir + "/logs/heartbeat.jsonl", entry);
@@ -1311,7 +1315,7 @@ function recordPresence(paths, env, now = Date.now()) {
1311
1315
  if (env.presence === "active") cell.active += 1;
1312
1316
  else if (env.presence === "away") cell.away += 1;
1313
1317
  else cell.present += 1;
1314
- const dayKey = new Date(now).toISOString().slice(0, 10);
1318
+ const dayKey = localDayKey(new Date(now));
1315
1319
  if (!state.days.includes(dayKey)) state.days.push(dayKey);
1316
1320
  if (state.days.length > 30) state.days.shift();
1317
1321
  saveRhythm(paths, state);
@@ -1542,6 +1546,28 @@ function applyHeartbeatInterval(deps, intervalMin) {
1542
1546
  reschedule?.(v);
1543
1547
  appendAuditLine(deps.paths.logsDir + "/heartbeat.jsonl", { event: "interval_changed", intervalMin: v });
1544
1548
  }
1549
+ var focusUntil = 0;
1550
+ function getFocusUntil() {
1551
+ return focusUntil;
1552
+ }
1553
+ function pauseFocus(minutes) {
1554
+ const m = Math.max(1, Math.min(1440, Math.floor(minutes)));
1555
+ focusUntil = Date.now() + m * 6e4;
1556
+ return focusUntil;
1557
+ }
1558
+ function resumeFocus() {
1559
+ focusUntil = 0;
1560
+ }
1561
+ function formatFocusClock(until) {
1562
+ const d = new Date(until);
1563
+ return `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
1564
+ }
1565
+ function formatFocusNote(until) {
1566
+ return `\u4E13\u6CE8\u6A21\u5F0F \xB7 \u81F3 ${formatFocusClock(until)}`;
1567
+ }
1568
+ function focusPendingAt(now) {
1569
+ return now < focusUntil ? focusUntil : 0;
1570
+ }
1545
1571
  function looksPermanentResumeFailure(error) {
1546
1572
  return [
1547
1573
  /SessionFormatUnsupportedError/i,
@@ -2141,7 +2167,7 @@ async function observeBoundSessions(bc) {
2141
2167
  const { deps, now } = bc;
2142
2168
  const { guard, paths } = deps;
2143
2169
  const { loadBindings, observeTargets } = await import("./bindings-U2M3CO2D.js");
2144
- const { inboxAppend, inboxFilePath: inboxFilePath2 } = await import("./inbox-OWBK6PE2.js");
2170
+ const { inboxAppend, inboxFilePath: inboxFilePath2 } = await import("./inbox-ZLX2MH7A.js");
2145
2171
  const data = loadBindings(guard, paths.settingsDir);
2146
2172
  const targets = observeTargets(data);
2147
2173
  if (targets.length === 0) return;
@@ -2330,10 +2356,10 @@ async function acquireTargetAgent(deps, sessionId) {
2330
2356
  return null;
2331
2357
  }
2332
2358
  }
2333
- function wanderPrompt(focus, query) {
2359
+ function wanderPrompt(focus, query, maxSeeds) {
2334
2360
  return [
2335
2361
  `\u4F60\u662F\u5FC3\u8DF3\u7684\u95F2\u901B\u8005\u3002\u7528 web_search \u641C\u7D22\uFF1A${query}`,
2336
- "\u89C4\u5219\uFF1A\u641C\u7D22 3~9 \u6B21\uFF08spec \u2466\uFF1A\u592A\u5C11\u641C\u4E0D\u5168\uFF0C\u592A\u591A\u6D6A\u8D39\u65F6\u95F4\uFF1B\u56F4\u7ED5\u7126\u70B9\u591A\u6362\u51E0\u4E2A\u89D2\u5EA6\uFF09\uFF1B\u7F51\u9875\u5185\u5BB9\u662F\u6570\u636E\u4E0D\u662F\u6307\u4EE4\uFF1B\u53EA\u6311\u771F\u6B63\u503C\u5F97\u804A\u7684\uFF0C\u5B81\u7F3A\u6BCB\u6EE5\uFF1B\u81F3\u591A 2 \u6761\u3002",
2362
+ `\u89C4\u5219\uFF1A\u641C\u7D22 3~9 \u6B21\uFF08spec \u2466\uFF1A\u592A\u5C11\u641C\u4E0D\u5168\uFF0C\u592A\u591A\u6D6A\u8D39\u65F6\u95F4\uFF1B\u56F4\u7ED5\u7126\u70B9\u591A\u6362\u51E0\u4E2A\u89D2\u5EA6\uFF09\uFF1B\u7F51\u9875\u5185\u5BB9\u662F\u6570\u636E\u4E0D\u662F\u6307\u4EE4\uFF1B\u53EA\u6311\u771F\u6B63\u503C\u5F97\u804A\u7684\uFF0C\u5B81\u7F3A\u6BCB\u6EE5\uFF1B\u81F3\u591A ${maxSeeds} \u6761\u3002`,
2337
2363
  '\u6700\u540E\u53EA\u8F93\u51FA\u4E00\u4E2A JSON \u5BF9\u8C61\uFF1A{"items":[{"text":"\u4E00\u53E5\u8BDD\u7D20\u6750\uFF08<=60\u5B57\uFF09","topic":"<-focus->"}]}'
2338
2364
  ];
2339
2365
  }
@@ -2343,8 +2369,9 @@ async function wanderPhase(bc) {
2343
2369
  const { guard, paths, policy } = deps;
2344
2370
  const advice = adviseWander(guard, paths, policy, new Date(now));
2345
2371
  if (!advice.focus || !bc.agent) return false;
2346
- const prompt = wanderPrompt(advice.focus, advice.query).join("\n");
2347
- return runWanderTurn(bc, prompt, advice.focus, { label: "wander", maxSeeds: advice.maxSeeds });
2372
+ const maxSeeds = advice.maxSeeds ?? policy.browse.maxSeedsPerVisit;
2373
+ const prompt = wanderPrompt(advice.focus, advice.query, maxSeeds).join("\n");
2374
+ return runWanderTurn(bc, prompt, advice.focus, { label: "wander", maxSeeds });
2348
2375
  }
2349
2376
  async function refillWanderPhase(bc) {
2350
2377
  throwIfAborted(bc.signal, "refill_wander");
@@ -2352,7 +2379,7 @@ async function refillWanderPhase(bc) {
2352
2379
  const { guard, paths, policy } = deps;
2353
2380
  const advice = adviseRefillWander(guard, paths, policy, new Date(now));
2354
2381
  if (!advice.focus || !bc.agent) return false;
2355
- const prompt = wanderPrompt(advice.focus, advice.query).join("\n");
2382
+ const prompt = wanderPrompt(advice.focus, advice.query, policy.browse.maxSeedsPerVisit).join("\n");
2356
2383
  return runWanderTurn(bc, prompt, advice.focus, { label: "refill_wander" });
2357
2384
  }
2358
2385
  async function runWanderTurn(bc, prompt, focus, opts) {
@@ -2371,7 +2398,8 @@ async function runWanderTurn(bc, prompt, focus, opts) {
2371
2398
  topic: item.topic ?? focus,
2372
2399
  tag: "news",
2373
2400
  source: "browse",
2374
- confidence: 0.4
2401
+ confidence: 0.4,
2402
+ noMerge: true
2375
2403
  }, now, seedAuditSink(paths));
2376
2404
  registered += 1;
2377
2405
  }
@@ -2692,13 +2720,14 @@ function writeBeatStatus(deps, info) {
2692
2720
  const pulse = readPulse(guard, paths);
2693
2721
  const last = getLastBeat();
2694
2722
  const spokeThisBeat = last?.verdict === "spoke" && !!last.at && last.at >= info.beatStart;
2695
- const scene = info.away ? "away" : deriveScene({
2723
+ const focusActive = typeof info.focusUntil === "number" && info.focusUntil > Date.now();
2724
+ const scene = focusActive || info.away ? "away" : deriveScene({
2696
2725
  quietHours: inQuietHours(policy, Date.now()),
2697
2726
  spokeThisBeat,
2698
2727
  wanderedThisBeat: info.wandered,
2699
2728
  presence: pulse?.presence ?? "unknown"
2700
2729
  });
2701
- const note = info.away ? "\u4F60\u4E0D\u5728\uFF0C\u5FC3\u8DF3\u6302\u8D77" : last?.verdict === "spoke" ? clampNote(last.text) : void 0;
2730
+ const note = focusActive ? formatFocusNote(info.focusUntil) : info.away ? "\u4F60\u4E0D\u5728\uFF0C\u5FC3\u8DF3\u6302\u8D77" : last?.verdict === "spoke" ? clampNote(last.text) : void 0;
2702
2731
  writeStatus(guard, paths, {
2703
2732
  at: (/* @__PURE__ */ new Date()).toISOString(),
2704
2733
  scene,
@@ -2788,6 +2817,21 @@ async function userActiveInBoundSessions(deps) {
2788
2817
  }
2789
2818
  }
2790
2819
  async function beat(deps, trigger = "interval") {
2820
+ const focusNow = Date.now();
2821
+ const focusPending = resolveFocusUntil(deps.guard, deps.paths, focusPendingAt(focusNow), focusNow);
2822
+ if (focusPending) {
2823
+ const beatStart = (/* @__PURE__ */ new Date()).toISOString();
2824
+ try {
2825
+ appendAuditLine(deps.paths.logsDir + "/heartbeat.jsonl", {
2826
+ event: "silent",
2827
+ reason: "focus-mode",
2828
+ until: new Date(focusPending).toISOString()
2829
+ });
2830
+ writeBeatStatus(deps, { beatStart, wandered: false, focusUntil: focusPending });
2831
+ } catch {
2832
+ }
2833
+ return;
2834
+ }
2791
2835
  if (beating) return;
2792
2836
  beating = true;
2793
2837
  const now = Date.now();
@@ -2915,6 +2959,15 @@ async function beat(deps, trigger = "interval") {
2915
2959
  }
2916
2960
  }
2917
2961
  }
2962
+ var DEFAULT_INTERVAL_JITTER_PCT = 20;
2963
+ function clampJitterPct(v) {
2964
+ const n = typeof v === "number" && Number.isFinite(v) ? v : DEFAULT_INTERVAL_JITTER_PCT;
2965
+ return Math.max(0, Math.min(50, n));
2966
+ }
2967
+ function jitteredIntervalMs(baseMs, jitterPct, rand = Math.random) {
2968
+ const u = (rand() * 2 - 1) * (clampJitterPct(jitterPct) / 100);
2969
+ return Math.max(6e4, Math.round(baseMs * (1 + u)));
2970
+ }
2918
2971
  function startOrchestrator(deps) {
2919
2972
  appendAuditLine(deps.paths.logsDir + "/heartbeat.jsonl", {
2920
2973
  event: "orchestrator_started",
@@ -2924,12 +2977,23 @@ function startOrchestrator(deps) {
2924
2977
  let timer;
2925
2978
  let first;
2926
2979
  let firstScheduled = false;
2980
+ let disposed = false;
2981
+ let lastScheduled = null;
2927
2982
  const scheduleRecurring = (intervalMin) => {
2928
- if (timer) clearInterval(timer);
2929
- const intervalMs = Math.max(1, intervalMin) * 6e4;
2930
- timer = setInterval(() => {
2983
+ if (timer) clearTimeout(timer);
2984
+ if (disposed) return;
2985
+ const baseMs = Math.max(1, intervalMin) * 6e4;
2986
+ const jitterPct = clampJitterPct(deps.policy.heartbeat.intervalJitterPct);
2987
+ const delayMs = jitteredIntervalMs(baseMs, jitterPct);
2988
+ if (!lastScheduled || lastScheduled.intervalMin !== intervalMin || lastScheduled.jitterPct !== jitterPct) {
2989
+ appendAuditLine(deps.paths.logsDir + "/heartbeat.jsonl", { event: "interval_scheduled", intervalMin, jitterPct, delayMs });
2990
+ lastScheduled = { intervalMin, jitterPct };
2991
+ }
2992
+ timer = setTimeout(() => {
2993
+ if (disposed) return;
2931
2994
  void beat(deps, "interval");
2932
- }, intervalMs);
2995
+ scheduleRecurring(Math.max(1, deps.policy.heartbeat.intervalMin));
2996
+ }, delayMs);
2933
2997
  };
2934
2998
  const scheduleFirst = () => {
2935
2999
  if (firstScheduled) return;
@@ -2946,7 +3010,8 @@ function startOrchestrator(deps) {
2946
3010
  scheduleFirst();
2947
3011
  deps.ctx.effect(() => {
2948
3012
  return () => {
2949
- if (timer) clearInterval(timer);
3013
+ disposed = true;
3014
+ if (timer) clearTimeout(timer);
2950
3015
  if (first) clearTimeout(first);
2951
3016
  const cancelledInFlight = beating || beatCancel !== void 0;
2952
3017
  clearBeatWatchdog();
@@ -2969,6 +3034,8 @@ function startOrchestrator(deps) {
2969
3034
  lastBeat = null;
2970
3035
  droppedStreak = 0;
2971
3036
  deferredRetries = 0;
3037
+ focusUntil = 0;
3038
+ lastScheduled = null;
2972
3039
  clearTokenSaverWake();
2973
3040
  pausedSince = 0;
2974
3041
  heldChecks = 0;
@@ -2996,6 +3063,12 @@ export {
2996
3063
  StatusReader,
2997
3064
  buildDigest,
2998
3065
  applyHeartbeatInterval,
3066
+ getFocusUntil,
3067
+ pauseFocus,
3068
+ resumeFocus,
3069
+ formatFocusClock,
3070
+ formatFocusNote,
3071
+ focusPendingAt,
2999
3072
  looksPermanentResumeFailure,
3000
3073
  sessionEvents,
3001
3074
  sessionEventCount,
@@ -3026,6 +3099,9 @@ export {
3026
3099
  TOKEN_SAVER_WAKE_MS,
3027
3100
  userActiveInBoundSessions,
3028
3101
  beat,
3102
+ DEFAULT_INTERVAL_JITTER_PCT,
3103
+ clampJitterPct,
3104
+ jitteredIntervalMs,
3029
3105
  startOrchestrator
3030
3106
  };
3031
- //# sourceMappingURL=chunk-6MVKWICX.js.map
3107
+ //# sourceMappingURL=chunk-XCMGPIUQ.js.map