@kanadego/dsh-heartbeat 2.2.1 → 2.3.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 (61) hide show
  1. package/README.md +108 -303
  2. package/client.js +186 -24
  3. package/dist/{bili-login-XQVEE4M6.js → bili-login-KDDU63Z2.js} +4 -4
  4. package/dist/{bindings-U2M3CO2D.js → bindings-LTSGGNIH.js} +3 -3
  5. package/dist/{chunk-AT32XCCI.js → chunk-43Q42HWH.js} +8 -2
  6. package/dist/chunk-43Q42HWH.js.map +1 -0
  7. package/dist/{chunk-LQKOYTRL.js → chunk-4OQXAHU5.js} +2 -2
  8. package/dist/{chunk-3QJJRXIR.js → chunk-5CVAW5AS.js} +11 -2
  9. package/dist/chunk-5CVAW5AS.js.map +1 -0
  10. package/dist/{chunk-IBBAISSW.js → chunk-66IUDHCY.js} +50 -1
  11. package/dist/chunk-66IUDHCY.js.map +1 -0
  12. package/dist/{chunk-3DNGY4DS.js → chunk-77UYDI2A.js} +2 -2
  13. package/dist/{chunk-CJK6RTMH.js → chunk-ABFO4QII.js} +896 -167
  14. package/dist/chunk-ABFO4QII.js.map +1 -0
  15. package/dist/{chunk-M72T5MZU.js → chunk-GOLMRQRW.js} +3 -2
  16. package/dist/{chunk-XYBII5PR.js → chunk-HSTGRQLP.js} +178 -39
  17. package/dist/chunk-HSTGRQLP.js.map +1 -0
  18. package/dist/{chunk-KZLVR4J7.js → chunk-IPTNSXOY.js} +2 -2
  19. package/dist/{chunk-DLOYERAK.js → chunk-JIRQ36F7.js} +3 -3
  20. package/dist/{chunk-ECXYNTEV.js → chunk-K47VV6IQ.js} +5 -3
  21. package/dist/chunk-K47VV6IQ.js.map +1 -0
  22. package/dist/{chunk-F2XVKX55.js → chunk-KGOC6EX4.js} +3 -3
  23. package/dist/{chunk-SIY3YHFY.js → chunk-MMI6OBEC.js} +2 -2
  24. package/dist/{chunk-N6SMIGYJ.js → chunk-QOLKUYQ3.js} +2 -2
  25. package/dist/cli/index.js +94 -23
  26. package/dist/cli/index.js.map +1 -1
  27. package/dist/{focus-store-RNHBAFYS.js → focus-store-XGTPIVF6.js} +3 -3
  28. package/dist/{inbox-ZLX2MH7A.js → inbox-7GGEMBUD.js} +4 -4
  29. package/dist/index.d.ts +60 -1
  30. package/dist/index.js +468 -155
  31. package/dist/index.js.map +1 -1
  32. package/dist/{migrate-XFZU5EBE.js → migrate-6HH2ZARV.js} +4 -4
  33. package/dist/{orchestrator-5BHYQ77Y.js → orchestrator-OJJM5QSV.js} +20 -11
  34. package/dist/{pool-76VLAZER.js → pool-QTY2MI56.js} +4 -4
  35. package/dist/runtime-RGZKOFP3.js +15 -0
  36. package/dist/{snapshot-SU2VGM4C.js → snapshot-6DFNGXTR.js} +5 -5
  37. package/package.json +1 -1
  38. package/dist/chunk-3QJJRXIR.js.map +0 -1
  39. package/dist/chunk-AT32XCCI.js.map +0 -1
  40. package/dist/chunk-CJK6RTMH.js.map +0 -1
  41. package/dist/chunk-ECXYNTEV.js.map +0 -1
  42. package/dist/chunk-IBBAISSW.js.map +0 -1
  43. package/dist/chunk-XYBII5PR.js.map +0 -1
  44. package/dist/runtime-W2OWJIHY.js +0 -11
  45. /package/dist/{bili-login-XQVEE4M6.js.map → bili-login-KDDU63Z2.js.map} +0 -0
  46. /package/dist/{bindings-U2M3CO2D.js.map → bindings-LTSGGNIH.js.map} +0 -0
  47. /package/dist/{chunk-LQKOYTRL.js.map → chunk-4OQXAHU5.js.map} +0 -0
  48. /package/dist/{chunk-3DNGY4DS.js.map → chunk-77UYDI2A.js.map} +0 -0
  49. /package/dist/{chunk-M72T5MZU.js.map → chunk-GOLMRQRW.js.map} +0 -0
  50. /package/dist/{chunk-KZLVR4J7.js.map → chunk-IPTNSXOY.js.map} +0 -0
  51. /package/dist/{chunk-DLOYERAK.js.map → chunk-JIRQ36F7.js.map} +0 -0
  52. /package/dist/{chunk-F2XVKX55.js.map → chunk-KGOC6EX4.js.map} +0 -0
  53. /package/dist/{chunk-SIY3YHFY.js.map → chunk-MMI6OBEC.js.map} +0 -0
  54. /package/dist/{chunk-N6SMIGYJ.js.map → chunk-QOLKUYQ3.js.map} +0 -0
  55. /package/dist/{focus-store-RNHBAFYS.js.map → focus-store-XGTPIVF6.js.map} +0 -0
  56. /package/dist/{inbox-ZLX2MH7A.js.map → inbox-7GGEMBUD.js.map} +0 -0
  57. /package/dist/{migrate-XFZU5EBE.js.map → migrate-6HH2ZARV.js.map} +0 -0
  58. /package/dist/{orchestrator-5BHYQ77Y.js.map → orchestrator-OJJM5QSV.js.map} +0 -0
  59. /package/dist/{pool-76VLAZER.js.map → pool-QTY2MI56.js.map} +0 -0
  60. /package/dist/{runtime-W2OWJIHY.js.map → runtime-RGZKOFP3.js.map} +0 -0
  61. /package/dist/{snapshot-SU2VGM4C.js.map → snapshot-6DFNGXTR.js.map} +0 -0
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  applyHeartbeatInterval,
4
4
  buildDigest,
5
5
  buildSeedReportTool,
6
+ checkDeliverySession,
6
7
  clearNote,
7
8
  formatFocusClock,
8
9
  getFocusUntil,
@@ -10,6 +11,7 @@ import {
10
11
  homeSessionId,
11
12
  inQuietHours,
12
13
  isArchivedSession,
14
+ isSubagentSession,
13
15
  notepadFilePath,
14
16
  notepadView,
15
17
  parseArchivedSessionIds,
@@ -24,12 +26,12 @@ import {
24
26
  sessionEvents,
25
27
  startOrchestrator,
26
28
  writeNote
27
- } from "./chunk-CJK6RTMH.js";
29
+ } from "./chunk-ABFO4QII.js";
28
30
  import {
29
31
  bilibiliSessionFilePath,
30
32
  createBiliLoginService,
31
33
  saveBiliSession
32
- } from "./chunk-SIY3YHFY.js";
34
+ } from "./chunk-MMI6OBEC.js";
33
35
  import {
34
36
  EXCERPT_HEADER,
35
37
  appendExcerpt,
@@ -44,24 +46,35 @@ import {
44
46
  removeExcerpt,
45
47
  updateUserPolicy,
46
48
  userPresetRoot
47
- } from "./chunk-F2XVKX55.js";
49
+ } from "./chunk-KGOC6EX4.js";
48
50
  import {
51
+ CORRECTIONS_DONE_HEADER,
52
+ CORRECTIONS_HEADER,
53
+ LEDGER_DONE_HEADER,
54
+ LEDGER_HEADER,
55
+ appendArchiveLines,
49
56
  appendEntry,
57
+ correctionsDoneFilePath,
58
+ correctionsFilePath,
59
+ ledgerDoneFilePath,
50
60
  ledgerFilePath,
51
61
  listWeeklyReports,
52
62
  localDayKey,
53
63
  markDone,
64
+ parseLedgerLine,
54
65
  readLedger,
55
66
  readWeeklyReport,
56
- scanPending
57
- } from "./chunk-XYBII5PR.js";
67
+ scanPending,
68
+ seedBookFile
69
+ } from "./chunk-HSTGRQLP.js";
58
70
  import {
59
71
  openPath
60
72
  } from "./chunk-OCBE2AKW.js";
61
73
  import {
74
+ getCurrentDeliveryId,
62
75
  getRuntime,
63
76
  setRuntime
64
- } from "./chunk-3QJJRXIR.js";
77
+ } from "./chunk-5CVAW5AS.js";
65
78
  import {
66
79
  activeSeeds,
67
80
  addSeed,
@@ -71,35 +84,37 @@ import {
71
84
  loadPool,
72
85
  restoreSeed,
73
86
  seedsFilePath
74
- } from "./chunk-N6SMIGYJ.js";
87
+ } from "./chunk-QOLKUYQ3.js";
88
+ import {
89
+ addBinding,
90
+ loadBindings,
91
+ removeBinding
92
+ } from "./chunk-77UYDI2A.js";
75
93
  import {
76
94
  clearFocusState,
77
95
  readFocusState,
78
96
  writeFocusState
79
- } from "./chunk-LQKOYTRL.js";
80
- import "./chunk-KZLVR4J7.js";
81
- import "./chunk-DLOYERAK.js";
97
+ } from "./chunk-4OQXAHU5.js";
98
+ import "./chunk-IPTNSXOY.js";
99
+ import "./chunk-JIRQ36F7.js";
82
100
  import {
83
101
  appendAuditLine,
84
102
  loadProfile,
85
103
  profileFilePath
86
- } from "./chunk-ECXYNTEV.js";
104
+ } from "./chunk-K47VV6IQ.js";
87
105
  import {
88
106
  dshHomeDir,
89
107
  initWorkspace,
90
108
  loadJson,
109
+ readText,
91
110
  saveJson,
111
+ withFileLock,
92
112
  writeText
93
- } from "./chunk-M72T5MZU.js";
94
- import {
95
- addBinding,
96
- loadBindings,
97
- removeBinding
98
- } from "./chunk-3DNGY4DS.js";
113
+ } from "./chunk-GOLMRQRW.js";
99
114
  import {
100
115
  atomicWriteFileSync,
101
116
  atomicWriteJsonSync
102
- } from "./chunk-IBBAISSW.js";
117
+ } from "./chunk-66IUDHCY.js";
103
118
 
104
119
  // src/index.ts
105
120
  import z from "@deepseek-ai/schemastery";
@@ -126,6 +141,10 @@ function sanitize(raw) {
126
141
  if (typeof r.statusbar === "boolean") out.statusbar = r.statusbar;
127
142
  if (typeof r.idleMode === "boolean") out.idleMode = r.idleMode;
128
143
  if (typeof r.tokenSaver === "boolean") out.tokenSaver = r.tokenSaver;
144
+ if (r.visionEngine === "" || r.visionEngine === "auto" || r.visionEngine === "native" || r.visionEngine === "modlens") {
145
+ out.visionEngine = r.visionEngine;
146
+ }
147
+ if (typeof r.concludeSilentTurn === "boolean") out.concludeSilentTurn = r.concludeSilentTurn;
129
148
  return out;
130
149
  }
131
150
  function uiConfigPath(settingsDir) {
@@ -270,6 +289,120 @@ function setWanderWindows(guard, paths, rawWindows) {
270
289
  // src/statusbar/time-inject.ts
271
290
  import fs3 from "fs";
272
291
  import path3 from "path";
292
+
293
+ // src/statusbar/track.ts
294
+ var capabilityCache = /* @__PURE__ */ new Map();
295
+ function supportsInHistory(session) {
296
+ const key = session.id;
297
+ const len = sessionEventCount(session);
298
+ const hit = capabilityCache.get(key);
299
+ if (hit && hit.scannedUpTo === len) return hit.supported;
300
+ const from = hit && len >= hit.scannedUpTo ? hit.scannedUpTo : 0;
301
+ let supported = false;
302
+ if (from !== 0 && hit) supported = hit.supported;
303
+ const readTail = (fromSeq) => {
304
+ if (typeof session.snapshotEvents === "function") {
305
+ try {
306
+ const slice = session.snapshotEvents(fromSeq);
307
+ if (Array.isArray(slice)) return slice;
308
+ } catch {
309
+ }
310
+ }
311
+ return sessionEvents(session).slice(fromSeq);
312
+ };
313
+ for (const e of readTail(from)) {
314
+ if (e.type === "request/context") {
315
+ supported = e.data?.systemPromptUpdate === "in-history";
316
+ }
317
+ }
318
+ capabilityCache.set(key, { scannedUpTo: len, supported });
319
+ return supported;
320
+ }
321
+ var SCENE_LABELS = {
322
+ "quiet-hours": "\u9759\u9ED8\u65F6\u6BB5\uFF0C\u4E16\u754C\u7761\u4E86",
323
+ "just-spoke": "\u521A\u53BB\u548C\u4F60\u8BF4\u8FC7\u8BDD",
324
+ wandering: "\u6B63\u5728\u95F2\u901B\u770B\u65B0\u4E1C\u897F",
325
+ busy: "\u770B\u5230\u4F60\u5728\u5FD9\uFF0C\u4E0D\u53BB\u6253\u6270",
326
+ present: "\u5728\u573A\u5F85\u7740",
327
+ away: "\u4F60\u4E0D\u5728\uFF0C\u81EA\u5DF1\u5F85\u7740"
328
+ };
329
+ function renderStatusText(status, opts) {
330
+ if (!status) return "";
331
+ const label = SCENE_LABELS[status.scene] ?? "\u5728\u573A";
332
+ const note = opts?.withNote === false ? "" : status.note ? `\u2014\u2014${status.note}` : "";
333
+ return `\u5FC3\u8DF3\u6B64\u523B\uFF1A${label}${note}\u3002`;
334
+ }
335
+ var pinnedTrack = /* @__PURE__ */ new Map();
336
+ function pinTrack(sessionId, session) {
337
+ const track = trackFor(session);
338
+ pinnedTrack.set(sessionId, track);
339
+ return track;
340
+ }
341
+ function pinnedTrackFor(sessionId, session) {
342
+ return pinnedTrack.get(sessionId) ?? trackFor(session);
343
+ }
344
+ var lastTrack = /* @__PURE__ */ new Map();
345
+ function noteTrack(auditFile, sessionId, track) {
346
+ if (lastTrack.get(sessionId) === track) return;
347
+ lastTrack.set(sessionId, track);
348
+ try {
349
+ appendAuditLine(auditFile, { event: "statusbar_track", sessionId, track });
350
+ } catch {
351
+ }
352
+ }
353
+ var lastSkip = /* @__PURE__ */ new Map();
354
+ function noteStatusbarSkip(auditFile, sessionId, reason) {
355
+ if (reason === null) {
356
+ lastSkip.delete(sessionId);
357
+ return;
358
+ }
359
+ if (lastSkip.get(sessionId) === reason) return;
360
+ lastSkip.set(sessionId, reason);
361
+ try {
362
+ appendAuditLine(auditFile, { event: "statusbar_skipped", sessionId, reason });
363
+ } catch {
364
+ }
365
+ }
366
+ function trackFor(session) {
367
+ return supportsInHistory(session) ? "system-prompt" : "pre-step";
368
+ }
369
+ function registerStatusbarSection(ctx, guard, paths, opts) {
370
+ ctx.inject(["systemPrompt"], (scoped) => {
371
+ const spCtx = scoped;
372
+ const section = spCtx.systemPrompt?.section;
373
+ if (typeof section !== "function") {
374
+ spCtx.logger?.warn("heartbeat: systemPrompt service has no section API, statusbar Track A unavailable");
375
+ return;
376
+ }
377
+ spCtx.effect(() => section.call(spCtx.systemPrompt, {
378
+ name: "heartbeat:status",
379
+ order: 5e3,
380
+ // between the persona prefix (0) and the harness block (10000)
381
+ text: (context) => {
382
+ if (!opts.enabled()) {
383
+ noteTrack(paths.logsDir + "/heartbeat.jsonl", context.agent?.session.id ?? "(none)", "off");
384
+ return "";
385
+ }
386
+ const agent = context.agent;
387
+ if (!agent?.session) return "";
388
+ if (opts.isDeliverSession) {
389
+ const delivery = opts.isDeliverSession(agent.session);
390
+ if (!delivery.allowed) {
391
+ noteStatusbarSkip(paths.logsDir + "/heartbeat.jsonl", agent.session.id, delivery.reason ?? "not-deliver");
392
+ return "";
393
+ }
394
+ noteStatusbarSkip(paths.logsDir + "/heartbeat.jsonl", agent.session.id, null);
395
+ }
396
+ const track = pinnedTrackFor(agent.session.id, agent.session);
397
+ noteTrack(paths.logsDir + "/heartbeat.jsonl", agent.session.id, track);
398
+ if (track !== "system-prompt") return "";
399
+ return renderStatusText(opts.reader.read(guard, paths), { withNote: false });
400
+ }
401
+ }), "heartbeat: statusbar section");
402
+ });
403
+ }
404
+
405
+ // src/statusbar/time-inject.ts
273
406
  function shouldInjectTime(input) {
274
407
  if (input.step !== 1) return false;
275
408
  if (!input.originIsUserSplice) return false;
@@ -293,13 +426,6 @@ function turnOriginIsUserSplice(session) {
293
426
  }
294
427
  return false;
295
428
  }
296
- function isSubagentSession(session) {
297
- if (!session) return false;
298
- const header = session.header;
299
- if (header?.origin === "subagent") return true;
300
- if (typeof header?.delegationDepth === "number" && header.delegationDepth > 0) return true;
301
- return sessionEvents(session).slice(0, 8).some((e) => e?.type === "subagent/descriptor");
302
- }
303
429
  function formatElapsed(ms) {
304
430
  const minutes = Math.max(1, Math.round(ms / 6e4));
305
431
  if (minutes < 60) return `${minutes} \u5206\u949F`;
@@ -388,9 +514,15 @@ function registerTimeInjection(ctx, guard, dataDir, opts) {
388
514
  lastMessageTime: lastMessageTime(payload.agent.session)
389
515
  });
390
516
  if (opts.getStatusLine && track) {
391
- const statusLine = opts.getStatusLine(payload.agent.session, track);
392
- if (statusLine) text += `
517
+ const delivery = opts.isDeliverSession?.(payload.agent.session);
518
+ if (delivery && !delivery.allowed) {
519
+ noteStatusbarSkip(opts.paths.logsDir + "/heartbeat.jsonl", sessionId, delivery.reason ?? "not-deliver");
520
+ } else {
521
+ if (delivery) noteStatusbarSkip(opts.paths.logsDir + "/heartbeat.jsonl", sessionId, null);
522
+ const statusLine = opts.getStatusLine(payload.agent.session, track);
523
+ if (statusLine) text += `
393
524
  ${statusLine}`;
525
+ }
394
526
  }
395
527
  const message = createUserMessage({
396
528
  content: [{ type: "text", text }],
@@ -535,7 +667,7 @@ function buildEndpoints(deps, isLive) {
535
667
  }
536
668
  let flags = { statusbarEnabled: true, timeInjectMin: 25, idleMode: false };
537
669
  try {
538
- const { getRuntime: getRuntime2 } = await import("./runtime-W2OWJIHY.js");
670
+ const { getRuntime: getRuntime2 } = await import("./runtime-RGZKOFP3.js");
539
671
  const f = getRuntime2().flags;
540
672
  flags = { statusbarEnabled: f.statusbarEnabled(), timeInjectMin: f.timeInjectMin(), idleMode: f.idleMode() };
541
673
  } catch {
@@ -712,7 +844,7 @@ function buildEndpoints(deps, isLive) {
712
844
  return err("bad-request", "config.set needs a patch object");
713
845
  }
714
846
  const keys = Object.keys(patch).filter(
715
- (k) => ["intervalMin", "maxDailySend", "timeInjectMin", "statusbar", "idleMode", "tokenSaver", "psyEnabled", "quietStart", "quietEnd"].includes(k)
847
+ (k) => ["intervalMin", "maxDailySend", "timeInjectMin", "statusbar", "idleMode", "tokenSaver", "visionEngine", "concludeSilentTurn", "psyEnabled", "quietStart", "quietEnd"].includes(k)
716
848
  );
717
849
  if (keys.length === 0) return err("bad-request", "config.set has no recognized field");
718
850
  try {
@@ -775,7 +907,29 @@ function buildEndpoints(deps, isLive) {
775
907
  },
776
908
  "ledger.open"() {
777
909
  const f = ledgerFilePath(paths.dataDir);
778
- if (!fs4.existsSync(guard.assert(f))) fs4.writeFileSync(f, "# \u8D26\u672C\n", "utf8");
910
+ seedBookFile(guard, f, LEDGER_HEADER);
911
+ openPath(f);
912
+ return ok({ path: f });
913
+ },
914
+ // 2.3.0: the live book only shows open rows; the archive is a read-only
915
+ // companion (same button row in the card). Missing archive = empty file.
916
+ "ledger.openDone"() {
917
+ const f = ledgerDoneFilePath(paths.dataDir);
918
+ seedBookFile(guard, f, LEDGER_DONE_HEADER);
919
+ openPath(f);
920
+ return ok({ path: f });
921
+ },
922
+ // 错题本 (v2.3.0): same two-button semantics as the ledger; `pending` is
923
+ // the count of unreviewed rows in the live book.
924
+ "corrections.open"() {
925
+ const f = correctionsFilePath(paths.dataDir);
926
+ seedBookFile(guard, f, CORRECTIONS_HEADER);
927
+ openPath(f);
928
+ return ok({ path: f, pending: scanPending(guard, f).length });
929
+ },
930
+ "corrections.openDone"() {
931
+ const f = correctionsDoneFilePath(paths.dataDir);
932
+ seedBookFile(guard, f, CORRECTIONS_DONE_HEADER);
779
933
  openPath(f);
780
934
  return ok({ path: f });
781
935
  },
@@ -795,7 +949,7 @@ function buildEndpoints(deps, isLive) {
795
949
  async "migrate.export"(p) {
796
950
  const passphrase = String(p.passphrase ?? "");
797
951
  if (!passphrase) return err("bad-request", "\u9700\u8981\u8BBE\u7F6E\u53E3\u4EE4");
798
- const { collectMigrationEntries, encryptContainer } = await import("./migrate-XFZU5EBE.js");
952
+ const { collectMigrationEntries, encryptContainer } = await import("./migrate-6HH2ZARV.js");
799
953
  const { entries } = collectMigrationEntries(guard, paths);
800
954
  if (entries.length === 0) return err("bad-request", "\u6CA1\u6709\u53EF\u6253\u5305\u7684\u8BB0\u5FC6\u6587\u4EF6\uFF08data/ \u662F\u7A7A\u7684\uFF09");
801
955
  const out = path4.join(paths.exportsDir, `heartbeat-memory-${localDayKey()}.hbmig`);
@@ -807,7 +961,7 @@ function buildEndpoints(deps, isLive) {
807
961
  const passphrase = String(p.passphrase ?? "");
808
962
  const file = String(p.file ?? "");
809
963
  if (!passphrase || !file) return err("bad-request", "\u9700\u8981\u5BB9\u5668\u8DEF\u5F84\u4E0E\u53E3\u4EE4");
810
- const { decryptContainer, applyMigrationEntries } = await import("./migrate-XFZU5EBE.js");
964
+ const { decryptContainer, applyMigrationEntries } = await import("./migrate-6HH2ZARV.js");
811
965
  let text;
812
966
  try {
813
967
  text = fs4.readFileSync(file, "utf8");
@@ -902,97 +1056,6 @@ function installHeartbeatRpc(ctx, deps) {
902
1056
  });
903
1057
  }
904
1058
 
905
- // src/statusbar/track.ts
906
- var capabilityCache = /* @__PURE__ */ new Map();
907
- function supportsInHistory(session) {
908
- const key = session.id;
909
- const len = sessionEventCount(session);
910
- const hit = capabilityCache.get(key);
911
- if (hit && hit.scannedUpTo === len) return hit.supported;
912
- const from = hit && len >= hit.scannedUpTo ? hit.scannedUpTo : 0;
913
- let supported = false;
914
- if (from !== 0 && hit) supported = hit.supported;
915
- const readTail = (fromSeq) => {
916
- if (typeof session.snapshotEvents === "function") {
917
- try {
918
- const slice = session.snapshotEvents(fromSeq);
919
- if (Array.isArray(slice)) return slice;
920
- } catch {
921
- }
922
- }
923
- return sessionEvents(session).slice(fromSeq);
924
- };
925
- for (const e of readTail(from)) {
926
- if (e.type === "request/context") {
927
- supported = e.data?.systemPromptUpdate === "in-history";
928
- }
929
- }
930
- capabilityCache.set(key, { scannedUpTo: len, supported });
931
- return supported;
932
- }
933
- var SCENE_LABELS = {
934
- "quiet-hours": "\u9759\u9ED8\u65F6\u6BB5\uFF0C\u4E16\u754C\u7761\u4E86",
935
- "just-spoke": "\u521A\u53BB\u548C\u4F60\u8BF4\u8FC7\u8BDD",
936
- wandering: "\u6B63\u5728\u95F2\u901B\u770B\u65B0\u4E1C\u897F",
937
- busy: "\u770B\u5230\u4F60\u5728\u5FD9\uFF0C\u4E0D\u53BB\u6253\u6270",
938
- present: "\u5728\u573A\u5F85\u7740",
939
- away: "\u4F60\u4E0D\u5728\uFF0C\u81EA\u5DF1\u5F85\u7740"
940
- };
941
- function renderStatusText(status, opts) {
942
- if (!status) return "";
943
- const label = SCENE_LABELS[status.scene] ?? "\u5728\u573A";
944
- const note = opts?.withNote === false ? "" : status.note ? `\u2014\u2014${status.note}` : "";
945
- return `\u5FC3\u8DF3\u6B64\u523B\uFF1A${label}${note}\u3002`;
946
- }
947
- var pinnedTrack = /* @__PURE__ */ new Map();
948
- function pinTrack(sessionId, session) {
949
- const track = trackFor(session);
950
- pinnedTrack.set(sessionId, track);
951
- return track;
952
- }
953
- function pinnedTrackFor(sessionId, session) {
954
- return pinnedTrack.get(sessionId) ?? trackFor(session);
955
- }
956
- var lastTrack = /* @__PURE__ */ new Map();
957
- function noteTrack(auditFile, sessionId, track) {
958
- if (lastTrack.get(sessionId) === track) return;
959
- lastTrack.set(sessionId, track);
960
- try {
961
- appendAuditLine(auditFile, { event: "statusbar_track", sessionId, track });
962
- } catch {
963
- }
964
- }
965
- function trackFor(session) {
966
- return supportsInHistory(session) ? "system-prompt" : "pre-step";
967
- }
968
- function registerStatusbarSection(ctx, guard, paths, opts) {
969
- ctx.inject(["systemPrompt"], (scoped) => {
970
- const spCtx = scoped;
971
- const section = spCtx.systemPrompt?.section;
972
- if (typeof section !== "function") {
973
- spCtx.logger?.warn("heartbeat: systemPrompt service has no section API, statusbar Track A unavailable");
974
- return;
975
- }
976
- spCtx.effect(() => section.call(spCtx.systemPrompt, {
977
- name: "heartbeat:status",
978
- order: 5e3,
979
- // between the persona prefix (0) and the harness block (10000)
980
- text: (context) => {
981
- if (!opts.enabled()) {
982
- noteTrack(paths.logsDir + "/heartbeat.jsonl", context.agent?.session.id ?? "(none)", "off");
983
- return "";
984
- }
985
- const agent = context.agent;
986
- if (!agent?.session) return "";
987
- const track = pinnedTrackFor(agent.session.id, agent.session);
988
- noteTrack(paths.logsDir + "/heartbeat.jsonl", agent.session.id, track);
989
- if (track !== "system-prompt") return "";
990
- return renderStatusText(opts.reader.read(guard, paths), { withNote: false });
991
- }
992
- }), "heartbeat: statusbar section");
993
- });
994
- }
995
-
996
1059
  // src/core/preset-definition.ts
997
1060
  function heartbeatPresetDefinition(id = "heartbeat") {
998
1061
  return {
@@ -1056,16 +1119,16 @@ function clean(v) {
1056
1119
  function fmtEntry(e) {
1057
1120
  return `#${e.id} [${e.status}] ${e.date} ${e.text}`;
1058
1121
  }
1059
- function buildLedgerTool(guard, file) {
1122
+ function buildBookTool(guard, file, labels, audit) {
1060
1123
  return {
1061
- name: "ledger",
1062
- description: '\u5171\u4EAB\u8D26\u672C\u5DE5\u5177\uFF08\u5FC3\u8DF3\u63D2\u4EF6\u7684\u5F85\u529E\u4E0E\u4E8B\u9879\u8BB0\u5F55\uFF09\u3002\u5F53\u7528\u6237\u63D0\u5230\u8BA1\u5212\u3001\u5F85\u529E\u3001\u627F\u8BFA\u3001\u60F3\u505A\u7684\u4E8B\uFF0C\u6216\u8005\u67D0\u4EF6\u4E8B\u5DF2\u7ECF\u5B8C\u6210\u65F6\uFF0C\u4E3B\u52A8\u8C03\u7528\u672C\u5DE5\u5177\u767B\u8BB0\u6216\u52FE\u6389\u2014\u2014\u4E0D\u9700\u8981\u7B49\u7528\u6237\u660E\u786E\u8BF4"\u8BB0\u4E00\u4E0B"\u3002action=add \u767B\u8BB0\u65B0\u4E8B\u9879\uFF08text \u5FC5\u586B\uFF09\uFF1Baction=list \u67E5\u770B\u5F53\u524D\u672A\u5B8C\u6210\u4E8B\u9879\uFF1Baction=done \u52FE\u6389\u4E00\u6761\uFF08key \u4E3A #id \u6216\u6587\u672C\u5B50\u4E32\uFF09\u3002',
1124
+ name: labels.name,
1125
+ description: labels.description,
1063
1126
  parameters: {
1064
1127
  type: "object",
1065
1128
  properties: {
1066
1129
  action: { type: "string", enum: ["add", "list", "done"], description: "add=\u767B\u8BB0\uFF0Clist=\u67E5\u672A\u5B8C\u6210\uFF0Cdone=\u52FE\u6389" },
1067
- text: { type: "string", description: "action=add \u65F6\u5FC5\u586B\uFF1A\u4E8B\u9879\u5185\u5BB9\uFF08\u4E00\u53E5\u8BDD\uFF09" },
1068
- key: { type: "string", description: "action=done \u65F6\u5FC5\u586B\uFF1A#id \u6216\u4E8B\u9879\u6587\u672C\u7684\u552F\u4E00\u5B50\u4E32" }
1130
+ text: { type: "string", description: labels.textHint },
1131
+ key: { type: "string", description: labels.keyHint }
1069
1132
  },
1070
1133
  required: ["action"],
1071
1134
  additionalProperties: false
@@ -1086,7 +1149,7 @@ function buildLedgerTool(guard, file) {
1086
1149
  }
1087
1150
  },
1088
1151
  timeoutMs: 5e3,
1089
- // Ledger writes go through read-modify-write on one file; never parallel.
1152
+ // Both books write through read-modify-write on one file; never parallel.
1090
1153
  isConcurrencySafe: () => false,
1091
1154
  async execute(args) {
1092
1155
  const a = args ?? {};
@@ -1094,26 +1157,142 @@ function buildLedgerTool(guard, file) {
1094
1157
  if (action === "add") {
1095
1158
  const text = clean(a.text);
1096
1159
  if (!text) return { ok: false, message: "add \u9700\u8981\u975E\u7A7A text" };
1097
- const e = appendEntry(guard, file, text);
1098
- return { ok: true, message: `\u5DF2\u767B\u8BB0 ${fmtEntry(e)}` };
1160
+ const e = appendEntry(guard, file, text, Date.now(), labels.header);
1161
+ return { ok: true, message: `${labels.addedVerb} ${fmtEntry(e)}` };
1099
1162
  }
1100
1163
  if (action === "done") {
1101
1164
  const key = clean(a.key);
1102
- if (!key) return { ok: false, message: "done \u9700\u8981 key\uFF08#id \u6216\u6587\u672C\u5B50\u4E32\uFF09" };
1103
- const e = markDone(guard, file, key);
1104
- if (!e) return { ok: false, message: `\u6CA1\u6709\u627E\u5230\u5339\u914D\u300C${key}\u300D\u7684\u672A\u5B8C\u6210\u4E8B\u9879\uFF08\u5148\u7528 list \u67E5\uFF09` };
1105
- return { ok: true, message: `\u5DF2\u5B8C\u6210 ${fmtEntry(e)}` };
1165
+ if (!key) return { ok: false, message: `done \u9700\u8981 key\uFF08#id \u6216\u6587\u672C\u5B50\u4E32\uFF09` };
1166
+ const e = markDone(guard, file, key, Date.now(), void 0, void 0, audit);
1167
+ if (!e) return { ok: false, message: `\u6CA1\u6709\u627E\u5230\u5339\u914D\u300C${key}\u300D\u7684${labels.listNoun}\uFF08\u5148\u7528 list \u67E5\uFF09` };
1168
+ return { ok: true, message: `${labels.doneVerb} ${fmtEntry(e)}` };
1106
1169
  }
1107
1170
  if (action === "list") {
1108
1171
  const items = scanPending(guard, file).map(fmtEntry);
1109
- return { ok: true, message: items.length > 0 ? `\u5F53\u524D\u672A\u5B8C\u6210 ${items.length} \u9879\uFF1A
1110
- ${items.join("\n")}` : "\u8D26\u672C\u91CC\u6CA1\u6709\u672A\u5B8C\u6210\u4E8B\u9879" };
1172
+ return {
1173
+ ok: true,
1174
+ message: items.length > 0 ? `${labels.listTitle} ${items.length} \u9879\uFF1A
1175
+ ${items.join("\n")}` : `${labels.bookName}\u91CC\u6CA1\u6709${labels.listNoun}`
1176
+ };
1111
1177
  }
1112
1178
  const total = readLedger(guard, file).entries.length;
1113
- return { ok: false, message: `\u672A\u77E5 action\uFF0C\u8D26\u672C\u5171 ${total} \u6761` };
1179
+ return { ok: false, message: `\u672A\u77E5 action\uFF0C${labels.bookName}\u5171 ${total} \u6761` };
1114
1180
  }
1115
1181
  };
1116
1182
  }
1183
+ var LEDGER_LABELS = {
1184
+ name: "ledger",
1185
+ header: LEDGER_HEADER,
1186
+ description: '\u5171\u4EAB\u8D26\u672C\u5DE5\u5177\uFF08\u5FC3\u8DF3\u63D2\u4EF6\u7684\u5F85\u529E\u4E0E\u4E8B\u9879\u8BB0\u5F55\uFF09\u3002\u5F53\u7528\u6237\u63D0\u5230\u8BA1\u5212\u3001\u5F85\u529E\u3001\u627F\u8BFA\u3001\u60F3\u505A\u7684\u4E8B\uFF0C\u6216\u8005\u67D0\u4EF6\u4E8B\u5DF2\u7ECF\u5B8C\u6210\u65F6\uFF0C\u4E3B\u52A8\u8C03\u7528\u672C\u5DE5\u5177\u767B\u8BB0\u6216\u52FE\u6389\u2014\u2014\u4E0D\u9700\u8981\u7B49\u7528\u6237\u660E\u786E\u8BF4"\u8BB0\u4E00\u4E0B"\u3002action=add \u767B\u8BB0\u65B0\u4E8B\u9879\uFF08text \u5FC5\u586B\uFF09\uFF1Baction=list \u67E5\u770B\u5F53\u524D\u672A\u5B8C\u6210\u4E8B\u9879\uFF1Baction=done \u52FE\u6389\u4E00\u6761\uFF08key \u4E3A #id \u6216\u6587\u672C\u5B50\u4E32\uFF09\u3002',
1187
+ addedVerb: "\u5DF2\u767B\u8BB0",
1188
+ doneVerb: "\u5DF2\u5B8C\u6210",
1189
+ listTitle: "\u5F53\u524D\u672A\u5B8C\u6210",
1190
+ listNoun: "\u672A\u5B8C\u6210\u4E8B\u9879",
1191
+ bookName: "\u8D26\u672C",
1192
+ textHint: "action=add \u65F6\u5FC5\u586B\uFF1A\u4E8B\u9879\u5185\u5BB9\uFF08\u4E00\u53E5\u8BDD\uFF09",
1193
+ keyHint: "action=done \u65F6\u5FC5\u586B\uFF1A#id \u6216\u4E8B\u9879\u6587\u672C\u7684\u552F\u4E00\u5B50\u4E32"
1194
+ };
1195
+ var CORRECTIONS_LABELS = {
1196
+ name: "corrections",
1197
+ header: CORRECTIONS_HEADER,
1198
+ description: '\u9519\u9898\u672C\u5DE5\u5177\uFF08\u8BB0\u5F55\u7528\u6237\u5BF9\u4F60\u7684\u7EA0\u6B63\uFF09\u3002\u5F53\u7528\u6237\u6307\u51FA\u4F60\u8BF4\u9519\u4E86\u3001\u505A\u9519\u4E86\u3001\u8BB0\u9519\u4E86\uFF0C\u6216\u8005\u7EA0\u6B63\u4F60\u7684\u8BF4\u6CD5\u4E0E\u505A\u6CD5\u65F6\uFF0C\u4E3B\u52A8\u8C03\u7528\u672C\u5DE5\u5177\u8BB0\u4E00\u6761\u2014\u2014\u4E0D\u9700\u8981\u7B49\u7528\u6237\u8BF4"\u8BB0\u4E00\u4E0B"\u3002action=add \u8BB0\u4E00\u6761\u7EA0\u6B63\uFF08text \u5FC5\u586B\uFF0C\u4E00\u53E5\u8BDD\u5199\u6E05\u7528\u6237\u7EA0\u6B63\u4E86\u4EC0\u4E48\uFF09\uFF1Baction=list \u67E5\u770B\u8FD8\u6CA1\u590D\u76D8\u7684\u6761\u76EE\uFF1Baction=done \u52FE\u6389\u4E00\u6761\uFF08key \u4E3A #id \u6216\u6587\u672C\u5B50\u4E32\uFF09\u2014\u2014\u52FE\u6389\u8868\u793A\u8FD9\u6761\u6559\u8BAD\u5DF2\u7ECF\u5199\u8FDB\u4EBA\u8BBE\u5361\u6216\u957F\u671F\u8BB0\u5FC6\u3002',
1199
+ addedVerb: "\u5DF2\u8BB0\u4E0B",
1200
+ doneVerb: "\u5DF2\u590D\u76D8",
1201
+ listTitle: "\u5F85\u590D\u76D8",
1202
+ listNoun: "\u5F85\u590D\u76D8\u6761\u76EE",
1203
+ bookName: "\u9519\u9898\u672C",
1204
+ textHint: "action=add \u65F6\u5FC5\u586B\uFF1A\u7528\u6237\u7EA0\u6B63\u4E86\u4EC0\u4E48\uFF08\u4E00\u53E5\u8BDD\uFF09",
1205
+ keyHint: "action=done \u65F6\u5FC5\u586B\uFF1A#id \u6216\u7EA0\u6B63\u6587\u672C\u7684\u552F\u4E00\u5B50\u4E32"
1206
+ };
1207
+ function buildLedgerTool(guard, file, audit) {
1208
+ return buildBookTool(guard, file, LEDGER_LABELS, audit);
1209
+ }
1210
+ function buildCorrectionsTool(guard, file, audit) {
1211
+ return buildBookTool(guard, file, CORRECTIONS_LABELS, audit);
1212
+ }
1213
+
1214
+ // src/ledger/archive.ts
1215
+ import fs5 from "fs";
1216
+ import path5 from "path";
1217
+ var STATE_FILE = "state.json";
1218
+ function stateFilePath(settingsDir) {
1219
+ return path5.join(settingsDir, STATE_FILE);
1220
+ }
1221
+ function readState(guard, settingsDir) {
1222
+ try {
1223
+ const raw = readText(guard, stateFilePath(settingsDir), "");
1224
+ if (!raw.trim()) return {};
1225
+ const parsed = JSON.parse(raw);
1226
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
1227
+ } catch {
1228
+ return {};
1229
+ }
1230
+ }
1231
+ function writeState(guard, settingsDir, state) {
1232
+ writeText(guard, stateFilePath(settingsDir), JSON.stringify(state, null, 2) + "\n");
1233
+ }
1234
+ function bookSpecs(dataDir) {
1235
+ return [
1236
+ {
1237
+ label: "ledger",
1238
+ marker: "ledgerDoneMigrated",
1239
+ mainFile: ledgerFilePath(dataDir),
1240
+ doneFile: ledgerDoneFilePath(dataDir),
1241
+ mainHeader: LEDGER_HEADER,
1242
+ doneHeader: LEDGER_DONE_HEADER
1243
+ },
1244
+ {
1245
+ label: "corrections",
1246
+ marker: "correctionsDoneMigrated",
1247
+ mainFile: correctionsFilePath(dataDir),
1248
+ doneFile: correctionsDoneFilePath(dataDir),
1249
+ mainHeader: CORRECTIONS_HEADER,
1250
+ doneHeader: CORRECTIONS_DONE_HEADER
1251
+ }
1252
+ ];
1253
+ }
1254
+ function pad2(n) {
1255
+ return String(n).padStart(2, "0");
1256
+ }
1257
+ function backupPathFor(guard, file, now) {
1258
+ const d = new Date(now);
1259
+ const stamp = `${d.getFullYear()}${pad2(d.getMonth() + 1)}${pad2(d.getDate())}-${pad2(d.getHours())}${pad2(d.getMinutes())}${pad2(d.getSeconds())}`;
1260
+ const base = `${file}.bak-${stamp}`;
1261
+ let out = base;
1262
+ for (let i = 1; fs5.existsSync(guard.assert(out)); i += 1) out = `${base}-${i}`;
1263
+ return out;
1264
+ }
1265
+ function migrateBookDone(guard, spec, settingsDir, audit, now = Date.now()) {
1266
+ return withFileLock(spec.mainFile, () => {
1267
+ const state = readState(guard, settingsDir);
1268
+ if (state[spec.marker] === true) return { skipped: true, count: 0, backup: null };
1269
+ const doneLines = [];
1270
+ const keptLines = [];
1271
+ const exists = fs5.existsSync(guard.assert(spec.mainFile));
1272
+ if (exists) {
1273
+ for (const line of readText(guard, spec.mainFile, spec.mainHeader).split("\n")) {
1274
+ const entry = parseLedgerLine(line);
1275
+ if (entry && entry.status === "done") doneLines.push(line);
1276
+ else keptLines.push(line);
1277
+ }
1278
+ }
1279
+ let backup = null;
1280
+ let appended = 0;
1281
+ if (doneLines.length > 0) {
1282
+ backup = backupPathFor(guard, spec.mainFile, now);
1283
+ fs5.copyFileSync(guard.assert(spec.mainFile), guard.assert(backup));
1284
+ appended = appendArchiveLines(guard, spec.doneFile, spec.doneHeader, doneLines, now);
1285
+ writeText(guard, spec.mainFile, keptLines.join("\n").replace(/\n*$/, "\n"));
1286
+ }
1287
+ state[spec.marker] = true;
1288
+ writeState(guard, settingsDir, state);
1289
+ audit({ event: `${spec.label}_done_migrated`, count: appended, found: doneLines.length });
1290
+ return { skipped: false, count: appended, backup };
1291
+ });
1292
+ }
1293
+ function runBookDoneMigrations(guard, dataDir, settingsDir, audit, now = Date.now()) {
1294
+ return bookSpecs(dataDir).map((spec) => migrateBookDone(guard, spec, settingsDir, audit, now));
1295
+ }
1117
1296
 
1118
1297
  // src/notepad/tool.ts
1119
1298
  var MAX_NOTE_CHARS = 160;
@@ -1133,7 +1312,7 @@ function fmtView(v) {
1133
1312
  return `\u5FC3\u4E8B\u5323\u91CC\u73B0\u5728\u6709\uFF1A${v.text}
1134
1313
  \uFF08\u7559\u4E8E${beats}\uFF0C\u5DF2\u6401 ${fmtMinutes(v.sinceMinutes)}\uFF0C${wakes}\uFF09`;
1135
1314
  }
1136
- function buildNotepadTool(guard, file) {
1315
+ function buildNotepadTool(guard, file, gate = {}) {
1137
1316
  return {
1138
1317
  name: "notepad",
1139
1318
  description: '\u5FC3\u4E8B\u5323\uFF08\u5FC3\u8DF3\u63D2\u4EF6\u7684"\u7559\u7ED9\u4E0B\u4E00\u8DF3\u7684\u81EA\u5DF1"\u4FBF\u7B7E\uFF09\u3002\u53EA\u6709\u4E00\u683C\uFF1A\u5199\u4E0B\u65B0\u7684\u5C31\u76D6\u6389\u65E7\u7684\u3002\u5F53\u4F60\u6709\u8BDD\u60F3\u7559\u5230\u4E0B\u6B21\u5FC3\u8DF3\u518D\u8BF4\u3001\u6216\u8005\u6709\u4E00\u4EF6\u653E\u4E0D\u4E0B\u7684\u4E8B\u65F6\uFF0C\u7528 action=write \u7559\u4E0B\u4E00\u53E5\u5F15\u5B50\u2014\u2014\u4E0D\u8981\u5199\u6574\u6BB5\uFF0C\u4E0B\u6B21\u8DF3\u7531\u90A3\u65F6\u7684\u4F60\u91CD\u65B0\u5C55\u5F00\u3002\u60F3\u7559\u4F4F\u539F\u6765\u7684\u5C31\u7167\u539F\u6837\u518D\u5199\u4E00\u904D\uFF08\u5B57\u6837\u4E00\u81F4\u7B97\u4FDD\u7559\uFF0C\u5B83\u7684\u5E74\u9F84\u4E0D\u4F1A\u91CD\u7B97\uFF09\uFF1B\u5199\u522B\u7684\u5C31\u662F\u6362\u6389\u5B83\u3002action=read \u770B\u73B0\u5728\u683C\u5B50\u91CC\u662F\u4EC0\u4E48\uFF1Baction=clear \u4E3B\u52A8\u653E\u6389\u3002\u6295\u9012\u5305\u8BFB\u8FC7\u4E4B\u540E\uFF0C\u4EC0\u4E48\u90FD\u4E0D\u505A\u5B83\u5C31\u4E0B\u4E00\u8DF3\u6CA1\u4E86\u3002\u7559\u4E0D\u7559\u90FD\u884C\uFF0C\u6CA1\u8BDD\u5C31\u7A7A\u7740\u3002',
@@ -1171,7 +1350,15 @@ function buildNotepadTool(guard, file) {
1171
1350
  timeoutMs: 5e3,
1172
1351
  // Read-modify-write on one file, and the note's lifecycle depends on it.
1173
1352
  isConcurrencySafe: () => false,
1174
- async execute(args) {
1353
+ async execute(args, exec) {
1354
+ const session = exec?.agent?.session;
1355
+ if (gate.isDeliverySession && session?.id && !gate.isDeliverySession(session)) {
1356
+ try {
1357
+ gate.onBlocked?.(session.id);
1358
+ } catch {
1359
+ }
1360
+ return { ok: false, message: "\u5FC3\u4E8B\u5323\u53EA\u5728\u6295\u9012\u5BF9\u8BDD\u91CC\u7528" };
1361
+ }
1175
1362
  const a = args ?? {};
1176
1363
  const action = String(a.action ?? "");
1177
1364
  try {
@@ -1271,8 +1458,8 @@ ${items.join("\n")}` };
1271
1458
  }
1272
1459
 
1273
1460
  // src/sources/bilibili.ts
1274
- import fs5 from "fs";
1275
- import path5 from "path";
1461
+ import fs6 from "fs";
1462
+ import path6 from "path";
1276
1463
  var BILI_HISTORY_URL = "https://api.bilibili.com/x/web-interface/history/cursor?ps=8";
1277
1464
  var BILI_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36";
1278
1465
  var BILI_SESSION_FILE = "bilibili-session.json";
@@ -1295,7 +1482,7 @@ function readVaultSessdata(guard, dataDir) {
1295
1482
  try {
1296
1483
  const session = loadJson(
1297
1484
  guard,
1298
- path5.join(dataDir, BILI_SESSION_FILE)
1485
+ path6.join(dataDir, BILI_SESSION_FILE)
1299
1486
  );
1300
1487
  const field = session?.sessdata;
1301
1488
  if (typeof field !== "string") return null;
@@ -1310,7 +1497,7 @@ function readVaultSessdata(guard, dataDir) {
1310
1497
  function readCookieFileSessdata(file) {
1311
1498
  if (!file) return null;
1312
1499
  try {
1313
- const raw = fs5.readFileSync(file, "utf8");
1500
+ const raw = fs6.readFileSync(file, "utf8");
1314
1501
  if (!raw) return null;
1315
1502
  const v = parseSessdata(raw);
1316
1503
  return v || null;
@@ -1360,7 +1547,7 @@ function emptyBiliState() {
1360
1547
  return { lastFetchAt: 0, bvids: [] };
1361
1548
  }
1362
1549
  function bilibiliStatePath(dataDir) {
1363
- return path5.join(dataDir, BILI_STATE_FILE);
1550
+ return path6.join(dataDir, BILI_STATE_FILE);
1364
1551
  }
1365
1552
  function normalizeBiliState(raw) {
1366
1553
  const out = emptyBiliState();
@@ -1514,6 +1701,29 @@ function stopBilibiliSource() {
1514
1701
 
1515
1702
  // src/index.ts
1516
1703
  var name = "heartbeat";
1704
+ var VISION_ENGINE_LITERALS = ["native", "modlens", "auto"];
1705
+ function auditInvalidVisionEngines(invalid, alreadyAudited, write) {
1706
+ for (const bad of invalid) {
1707
+ const key = `${bad.source}:${bad.value}`;
1708
+ if (alreadyAudited.has(key)) continue;
1709
+ try {
1710
+ const ok2 = write({ event: "vision_engine_invalid", source: bad.source, value: bad.value });
1711
+ if (ok2 !== false) alreadyAudited.add(key);
1712
+ } catch {
1713
+ }
1714
+ }
1715
+ }
1716
+ function resolveVisionEngineSetting(configured, card) {
1717
+ const invalid = [];
1718
+ const pick = (raw, source) => {
1719
+ const v = raw.trim();
1720
+ if (!v) return "";
1721
+ if (VISION_ENGINE_LITERALS.includes(v)) return v;
1722
+ invalid.push({ source, value: v });
1723
+ return "";
1724
+ };
1725
+ return { value: pick(configured, "config") || pick(card, "card"), invalid };
1726
+ }
1517
1727
  var inject = ["agents"];
1518
1728
  var Config = z.object({
1519
1729
  /** Override the runtime data dir (workspace guard boundary). Empty = default (<packageRoot>/data). */
@@ -1557,11 +1767,34 @@ var Config = z.object({
1557
1767
  /** v2.0.0 摘抄本工具:留句子和小事的地方(默认开)。 */
1558
1768
  excerptTool: z.boolean().default(true),
1559
1769
  /** v1.8.0 引擎室追加工具:逗号分隔的全局工具名,存在才加入白名单(bili 压缩工具自动探测,无需手填)。 */
1560
- extraTools: z.string().default("")
1770
+ extraTools: z.string().default(""),
1771
+ /**
1772
+ * 视觉识别引擎(v2.2.1-native.1):`auto` / `native` / `modlens`。
1773
+ * 留空 = 跟随 `data/settings/vision.json` 的 engine,再回落到内置默认 auto。
1774
+ * 这里给一个正式设置项,是为了让"默认走哪条路"可以不改代码就调——
1775
+ * 例如想保守地维持 v2.2.1 行为(只用 ModLens)就填 `modlens`。
1776
+ */
1777
+ visionEngine: z.string().default("")
1561
1778
  });
1562
1779
  function apply(ctx, config = {}) {
1563
1780
  const paths = initWorkspace(config.dataDir ? { dataDir: config.dataDir } : {});
1564
1781
  const guard = createPathGuard(paths.dataDir);
1782
+ try {
1783
+ runBookDoneMigrations(guard, paths.dataDir, paths.settingsDir, (entry) => {
1784
+ try {
1785
+ appendAuditLine(guard.assert(paths.logsDir + "/heartbeat.jsonl"), entry);
1786
+ } catch {
1787
+ }
1788
+ });
1789
+ } catch (e) {
1790
+ try {
1791
+ appendAuditLine(guard.assert(paths.logsDir + "/heartbeat.jsonl"), {
1792
+ event: "book_done_migration_failed",
1793
+ error: String(e).slice(0, 160)
1794
+ });
1795
+ } catch {
1796
+ }
1797
+ }
1565
1798
  let policy = loadPolicy(guard, paths.configDir, paths.settingsDir);
1566
1799
  const ui = loadUiConfig(guard, paths.settingsDir);
1567
1800
  if (ui.intervalMin && ui.intervalMin >= 1) {
@@ -1593,7 +1826,13 @@ function apply(ctx, config = {}) {
1593
1826
  statusbarEnabled: () => statusbarEnabledRef,
1594
1827
  timeInjectMin: () => timeInjectMinRef,
1595
1828
  idleMode: () => idleModeRef,
1596
- tokenSaver: () => tokenSaverRef
1829
+ tokenSaver: () => tokenSaverRef,
1830
+ // v2.2.1-native.1: card / composition engine choice ('' = vision.json).
1831
+ // Lazy on purpose: the refs below are declared after this call.
1832
+ visionEngine: () => effectiveVisionEngine(),
1833
+ // 2.3.0 experimental (default off): silent report concludes the turn.
1834
+ // Same lazy pattern as the refs above.
1835
+ concludeSilentTurn: () => concludeSilentTurnRef
1597
1836
  }
1598
1837
  });
1599
1838
  let presetRegistration;
@@ -1651,6 +1890,19 @@ function apply(ctx, config = {}) {
1651
1890
  let statusbarEnabledRef = ui.statusbar ?? config.statusbar === true;
1652
1891
  let idleModeRef = ui.idleMode ?? config.idleMode === true;
1653
1892
  let tokenSaverRef = ui.tokenSaver ?? config.tokenSaver === true;
1893
+ let visionEngineConfigRef = config.visionEngine ?? "";
1894
+ let visionEngineUiRef = ui.visionEngine ?? "";
1895
+ const invalidEnginesAudited = /* @__PURE__ */ new Set();
1896
+ let concludeSilentTurnRef = ui.concludeSilentTurn === true;
1897
+ const effectiveVisionEngine = () => {
1898
+ const resolved = resolveVisionEngineSetting(visionEngineConfigRef, visionEngineUiRef);
1899
+ auditInvalidVisionEngines(resolved.invalid, invalidEnginesAudited, (entry) => (
1900
+ // a3 中3: forward the success verdict — `false` means the line did NOT
1901
+ // land and the typo must stay unconsumed for the next beat's retry.
1902
+ appendAuditLine(guard.assert(paths.logsDir + "/heartbeat.jsonl"), entry)
1903
+ ));
1904
+ return resolved.value;
1905
+ };
1654
1906
  const applyLiveSettings = (v) => {
1655
1907
  if (v.intervalMin && v.intervalMin >= 1) applyHeartbeatInterval(deps, v.intervalMin);
1656
1908
  if (v.maxDailySend && v.maxDailySend >= 1) getRuntime().policy.gate.maxDailySend = v.maxDailySend;
@@ -1667,6 +1919,7 @@ function apply(ctx, config = {}) {
1667
1919
  const v = sectionSource?.();
1668
1920
  if (!v) return;
1669
1921
  applyLiveSettings(v);
1922
+ visionEngineConfigRef = typeof v.visionEngine === "string" ? v.visionEngine : "";
1670
1923
  ctx.logger.info("heartbeat: settings overrides live (interval %s, cap %s, timeInject %s)", v.intervalMin ?? "-", v.maxDailySend ?? "-", v.timeInjectMin ?? "-");
1671
1924
  } catch (e) {
1672
1925
  ctx.logger.warn("heartbeat: settings override failed (%s)", String(e).slice(0, 120));
@@ -1726,6 +1979,10 @@ function apply(ctx, config = {}) {
1726
1979
  statusbar: statusbarEnabledRef,
1727
1980
  idleMode: idleModeRef,
1728
1981
  tokenSaver: tokenSaverRef,
1982
+ // card-stored choice ('' = follow vision.json); the effective engine also
1983
+ // honours a composition-level config pin (see effectiveVisionEngine).
1984
+ visionEngine: visionEngineUiRef,
1985
+ concludeSilentTurn: concludeSilentTurnRef,
1729
1986
  psyEnabled: getRuntime().policy.profile.psyEnabled,
1730
1987
  quietStart: getRuntime().policy.gate.quietHours.start,
1731
1988
  quietEnd: getRuntime().policy.gate.quietHours.end
@@ -1744,10 +2001,13 @@ function apply(ctx, config = {}) {
1744
2001
  q.end = next.end;
1745
2002
  }
1746
2003
  const merged = saveUiConfig(guard, paths.settingsDir, patch);
2004
+ visionEngineUiRef = merged.visionEngine ?? "";
2005
+ concludeSilentTurnRef = merged.concludeSilentTurn === true;
1747
2006
  applyLiveSettings(merged);
1748
2007
  };
1749
2008
  installHeartbeatRpc(ctx, { paths, guard, policy, ui: { get: uiGet, set: uiSet } });
1750
- const registerGlobalTool = (name2, build) => {
2009
+ const deliverySessionVerdict = (session) => checkDeliverySession({ sessionId: session.id, session, guard, settingsDir: paths.settingsDir });
2010
+ const registerGlobalTool = (name2, build, when) => {
1751
2011
  ctx.inject(["tools"], (scoped) => {
1752
2012
  const audit = (entry) => {
1753
2013
  try {
@@ -1758,7 +2018,16 @@ function apply(ctx, config = {}) {
1758
2018
  try {
1759
2019
  const tools = scoped.tools;
1760
2020
  if (!tools || typeof tools.register !== "function") throw new Error("ToolRuntime.register unavailable on this host");
1761
- tools.register(build());
2021
+ if (when) {
2022
+ try {
2023
+ tools.register(build(), { when });
2024
+ } catch {
2025
+ tools.register(build());
2026
+ audit({ event: `${name2}_tool_when_unsupported` });
2027
+ }
2028
+ } else {
2029
+ tools.register(build());
2030
+ }
1762
2031
  audit({ event: `${name2}_tool_registered` });
1763
2032
  } catch (e) {
1764
2033
  audit({ event: `${name2}_tool_register_failed`, error: String(e).slice(0, 160) });
@@ -1766,13 +2035,53 @@ function apply(ctx, config = {}) {
1766
2035
  });
1767
2036
  };
1768
2037
  if (config.ledgerTool !== false) {
1769
- registerGlobalTool("ledger", () => buildLedgerTool(guard, ledgerFilePath(paths.dataDir)));
2038
+ const auditBookLine = (entry) => {
2039
+ try {
2040
+ appendAuditLine(guard.assert(paths.logsDir + "/heartbeat.jsonl"), entry);
2041
+ } catch {
2042
+ }
2043
+ };
2044
+ registerGlobalTool("ledger", () => buildLedgerTool(guard, ledgerFilePath(paths.dataDir), auditBookLine));
2045
+ registerGlobalTool("corrections", () => buildCorrectionsTool(guard, correctionsFilePath(paths.dataDir), auditBookLine));
1770
2046
  }
1771
2047
  if (config.seedReportTool !== false) {
1772
- registerGlobalTool("seed_report", () => buildSeedReportTool(guard, reportFilePath(paths.dataDir)));
2048
+ const auditReportLine = (entry) => {
2049
+ try {
2050
+ appendAuditLine(guard.assert(paths.logsDir + "/heartbeat.jsonl"), entry);
2051
+ } catch {
2052
+ }
2053
+ };
2054
+ registerGlobalTool("seed_report", () => buildSeedReportTool(
2055
+ guard,
2056
+ reportFilePath(paths.dataDir),
2057
+ () => concludeSilentTurnRef,
2058
+ auditReportLine,
2059
+ () => getCurrentDeliveryId(),
2060
+ // P1-1 (2.3.0, 账本 #d891e0): the lock is unconditional inside the
2061
+ // tool — a self-invented or repeated delivery id is always rejected;
2062
+ // this wiring only supplies the live-id getter and the audit sink.
2063
+ { onRejected: auditReportLine }
2064
+ ));
1773
2065
  }
1774
2066
  if (config.notepadTool !== false) {
1775
- registerGlobalTool("notepad", () => buildNotepadTool(guard, notepadFilePath(paths.dataDir)));
2067
+ const notepadAuditFile = paths.logsDir + "/heartbeat.jsonl";
2068
+ const notepadAllowed = (session) => {
2069
+ if (!session?.id) return false;
2070
+ return deliverySessionVerdict(session).allowed;
2071
+ };
2072
+ registerGlobalTool(
2073
+ "notepad",
2074
+ () => buildNotepadTool(guard, notepadFilePath(paths.dataDir), {
2075
+ isDeliverySession: (session) => notepadAllowed(session),
2076
+ onBlocked: (sessionId) => {
2077
+ try {
2078
+ appendAuditLine(guard.assert(notepadAuditFile), { event: "notepad_blocked", sessionId });
2079
+ } catch {
2080
+ }
2081
+ }
2082
+ }),
2083
+ (context) => notepadAllowed(context.agent?.session)
2084
+ );
1776
2085
  }
1777
2086
  if (config.excerptTool !== false) {
1778
2087
  registerGlobalTool("excerpt", () => buildExcerptTool(guard, excerptFilePath(paths.dataDir)));
@@ -1796,7 +2105,8 @@ function apply(ctx, config = {}) {
1796
2105
  const statusReader = new StatusReader();
1797
2106
  registerStatusbarSection(ctx, guard, paths, {
1798
2107
  enabled: () => statusbarEnabledRef,
1799
- reader: statusReader
2108
+ reader: statusReader,
2109
+ isDeliverSession: deliverySessionVerdict
1800
2110
  });
1801
2111
  ctx.effect(() => {
1802
2112
  registerTimeInjection(ctx, guard, paths.dataDir, {
@@ -1816,7 +2126,8 @@ function apply(ctx, config = {}) {
1816
2126
  return "";
1817
2127
  }
1818
2128
  return track === "pre-step" ? renderStatusText(statusReader.read(guard, paths)) : "";
1819
- }
2129
+ },
2130
+ isDeliverSession: deliverySessionVerdict
1820
2131
  });
1821
2132
  return void 0;
1822
2133
  }, "heartbeat: time injection");
@@ -1837,8 +2148,10 @@ function apply(ctx, config = {}) {
1837
2148
  export {
1838
2149
  Config,
1839
2150
  apply,
2151
+ auditInvalidVisionEngines,
1840
2152
  deepMerge,
1841
2153
  inject,
1842
- name
2154
+ name,
2155
+ resolveVisionEngineSetting
1843
2156
  };
1844
2157
  //# sourceMappingURL=index.js.map