agent-yes 1.303.1 → 1.304.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 (184) hide show
  1. package/README.md +54 -0
  2. package/default.config.yaml +5 -5
  3. package/dist/{JsonlStore-BpYZZTap.js → JsonlStore-CB_0sfdT.js} +5 -4
  4. package/dist/{SUPPORTED_CLIS-Dq9Lq9WJ.js → SUPPORTED_CLIS-C_1IiOj3.js} +2 -2
  5. package/dist/SUPPORTED_CLIS-Do3GHAY1.js +12 -0
  6. package/dist/{agentShare-suB6yPgO.js → agentShare-BvpEm-Fr.js} +10 -9
  7. package/dist/{askCli-BU3CNCLU.js → askCli-KmMf-3wA.js} +6 -6
  8. package/dist/{browser-BmxN1Ur9.js → browser-BAFPmEvN.js} +2 -2
  9. package/dist/{callback-Ctj3_Wxp.js → callback-BiSUhjw7.js} +6 -6
  10. package/dist/callback-SqLD2mMX.js +17 -0
  11. package/dist/{callbackCore-DSJWhqAe.js → callbackCore-BZ_10zyc.js} +1 -1
  12. package/dist/{callbackCore-BesbQ6-Z.js → callbackCore-Deve5pv3.js} +1 -1
  13. package/dist/{channels-j5nWuzlQ.js → channels-Y36S0ccW.js} +2 -2
  14. package/dist/cli.js +118 -13
  15. package/dist/{cmdDsh-2LP0grim.js → cmdDsh-DZ8bLsH7.js} +1 -1
  16. package/dist/cmdKill-CzSBfWmx.js +116 -0
  17. package/dist/{cmdPs-wc1j85fi.js → cmdPs-DBt23Qz0.js} +11 -10
  18. package/dist/{cwdConflictWarn-skQSMwEh.js → cwdConflictWarn-y-epQyOj.js} +21 -5
  19. package/dist/{cwdPassthroughHint-BUbWMoz0.js → cwdPassthroughHint-CzWh0hrw.js} +1 -1
  20. package/dist/{expose-Dawq1LxC.js → expose-Bhsn5yUz.js} +2 -2
  21. package/dist/forceKill-BDqxx4uQ.js +44 -0
  22. package/dist/{forkNested-CavHyWzA.js → forkNested-3heLs5XC.js} +45 -5
  23. package/dist/{globalPidIndex-CRB5KtyL.js → globalPidIndex-BsjlId-h.js} +6 -5
  24. package/dist/{globalPidIndex-BftWlg2V.js → globalPidIndex-uoz8Esqz.js} +2 -2
  25. package/dist/{hist-Cs0KFSUW.js → hist-DY16wm7g.js} +1 -1
  26. package/dist/{identity-DA7YI9km.js → identity-C0MxfwI2.js} +1 -1
  27. package/dist/{identity-Bk-aG_xl.js → identity-Ckav3-Up.js} +1 -1
  28. package/dist/index.js +7 -7
  29. package/dist/{initMsg-CcTqFtsp.js → initMsg-BEVDWYnv.js} +1 -1
  30. package/dist/{initMsg-D9KJnfgC.js → initMsg-GPU52N-j.js} +1 -1
  31. package/dist/liagent/box-BSIrRRc_.js +2320 -0
  32. package/dist/liagent/box-DloywR57.d.ts +409 -0
  33. package/dist/liagent/box.d.ts +3 -0
  34. package/dist/liagent/box.js +3 -0
  35. package/dist/liagent/cli.d.ts +1 -0
  36. package/dist/liagent/cli.js +908 -0
  37. package/dist/liagent/client.d.ts +28 -0
  38. package/dist/liagent/client.js +240 -0
  39. package/dist/liagent/echo-BvzPSI0x.js +86 -0
  40. package/dist/liagent/echo-W2RcMEny.d.ts +38 -0
  41. package/dist/liagent/hub.d.ts +405 -0
  42. package/dist/liagent/hub.js +1831 -0
  43. package/dist/liagent/index.d.ts +14 -0
  44. package/dist/liagent/index.js +10 -0
  45. package/dist/liagent/liagent.js +339 -0
  46. package/dist/liagent/mic-audio-C3Shud_F.js +754 -0
  47. package/dist/liagent/mic-audio-DMCWtEI-.d.ts +317 -0
  48. package/dist/liagent/mic-button-CtZHFBSG.js +651 -0
  49. package/dist/liagent/mic-button-Y0ioPetQ.d.ts +248 -0
  50. package/dist/liagent/mic.d.ts +3 -0
  51. package/dist/liagent/mic.js +4 -0
  52. package/dist/liagent/progress-CJs08bXb.js +473 -0
  53. package/dist/liagent/progress-CfE-meHr.d.ts +205 -0
  54. package/dist/liagent/progress.d.ts +2 -0
  55. package/dist/liagent/progress.js +3 -0
  56. package/dist/liagent/protocol.d.ts +324 -0
  57. package/dist/liagent/protocol.js +504 -0
  58. package/dist/liagent/speech-rate-DXeJjRTc.js +42 -0
  59. package/dist/liagent/state-tree-BZCmAknI.js +353 -0
  60. package/dist/liagent/tree-BrR7NwMf.js +710 -0
  61. package/dist/liagent/tree-HB9RhX_H.d.ts +295 -0
  62. package/dist/liagent/tree.d.ts +2 -0
  63. package/dist/liagent/tree.js +4 -0
  64. package/dist/liagent/ui-strings-B03le3fh.d.ts +27 -0
  65. package/dist/liagent/ui-strings-CJIwW5Gf.js +794 -0
  66. package/dist/liagent/ui.d.ts +394 -0
  67. package/dist/liagent/ui.js +5 -0
  68. package/dist/liagent/voice-D7yeS9VM.js +802 -0
  69. package/dist/liagent/voice-Qo7SCgi8.d.ts +308 -0
  70. package/dist/liagent/voice.d.ts +3 -0
  71. package/dist/liagent/voice.js +4 -0
  72. package/dist/{notifyDaemon-C7GlfJ1E.js → notifyDaemon-DkUB7GV2.js} +10 -9
  73. package/dist/{openBrowser-1cIo4dJL.js → openBrowser-CQAOIrD6.js} +1 -1
  74. package/dist/oxmgrService-DlYHmqyI.js +37 -0
  75. package/dist/oxmgrService-o9L3I6FX.js +3 -0
  76. package/dist/{parentLink-DoEWVGld.js → parentLink-BcAYcO9L.js} +2 -2
  77. package/dist/{parentLink-B58V1RVd.js → parentLink-MBUD3oY2.js} +2 -2
  78. package/dist/{parentPingLoop-C73bbmCE.js → parentPingLoop-DDO592Wh.js} +5 -5
  79. package/dist/{pidStore-433kvh9D.js → pidStore-C50VPk3E.js} +3 -3
  80. package/dist/pidStore-COkPjpW8.js +6 -0
  81. package/dist/{procStats-BvmrBrOs.js → procStats-DwTxj3rs.js} +1 -1
  82. package/dist/{remotes-MfdV9M8u.js → remotes-BqLoB5GR.js} +2 -2
  83. package/dist/{remotes-DevcBeLl.js → remotes-BsMGvAay.js} +3 -3
  84. package/dist/{roomReplyAlias-DFqg_6IU.js → roomReplyAlias-B-v76TA8.js} +3 -3
  85. package/dist/roomReplyAlias-BzWUBkQy.js +6 -0
  86. package/dist/{rustBinary-7VK7oLlW.js → rustBinary-BrJ2p8wy.js} +2 -2
  87. package/dist/{rustBinary-BV4LxMMn.js → rustBinary-vpfpWj1q.js} +2 -2
  88. package/dist/{schedule-Dp0qkNO1.js → schedule-Bzjhl6-h.js} +15 -44
  89. package/dist/schedulesCli-CLqieyQZ.js +784 -0
  90. package/dist/senderAncestry-CVHeKk1Y.js +173 -0
  91. package/dist/senderAncestry-KhAUPCQs.js +3 -0
  92. package/dist/{serve-BpOIIOVc.js → serve-XwFYE0cE.js} +67 -69
  93. package/dist/{serveRust-CbBrcUrg.js → serveRust-BfWkeL6T.js} +31 -4
  94. package/dist/{setup-DdIBk6Iv.js → setup-D4-s2VZw.js} +5 -5
  95. package/dist/{share-w5oRbvlj.js → share-CbFetL5H.js} +1 -1
  96. package/dist/{share-DrS3LYqf.js → share-_TPYAGWC.js} +1 -1
  97. package/dist/{shareCmd-DDENkBW2.js → shareCmd-DUBsVI3e.js} +10 -10
  98. package/dist/spawnGate-Dfj9V76w.js +5 -0
  99. package/dist/{spawnGate-lr3tvA6U.js → spawnGate-Dj_WaS29.js} +3 -3
  100. package/dist/{subcommands-DxgvDmZX.js → subcommands-B7YZJL8n.js} +471 -307
  101. package/dist/subcommands-BZPxFgkT.js +15 -0
  102. package/dist/{systemPathLink-SjUzgSiq.js → systemPathLink-Y3iC7bYV.js} +1 -1
  103. package/dist/{terminal-C2h7tpe0.js → terminal-13cGj3h6.js} +2 -2
  104. package/dist/terminal.js +1 -1
  105. package/dist/{todoAutomation-TTp_Qz9G.js → todoAutomation-CKlrxuZv.js} +2 -2
  106. package/dist/{todoCli-BWm5nZE0.js → todoCli-CU9vSJSJ.js} +6 -6
  107. package/dist/{todoIdentity-BvXZKWJI.js → todoIdentity-CzS1e7VL.js} +2 -2
  108. package/dist/{todoStore-C9RTz0Vs.js → todoStore-msRCEUi1.js} +2 -2
  109. package/dist/{tray-BeRSozJz.js → tray-CLktEN3G.js} +1 -1
  110. package/dist/{trayApp-QQjqhDv1.js → trayApp-5_OlvQcz.js} +2 -2
  111. package/dist/trayApp-DkAPo0m6.js +5 -0
  112. package/dist/{ts-6LF7f4fx.js → ts-njuddXQz.js} +126 -57
  113. package/dist/{versionChecker-Bc62HNf6.js → versionChecker-BJycAanp.js} +2 -2
  114. package/dist/{webrtcLink-va_gAR77.js → webrtcLink-DcvacEci.js} +1 -1
  115. package/dist/{webrtcLink-SsInrVHU.js → webrtcLink-WxS1nTIs.js} +1 -1
  116. package/dist/{webrtcRemote-BlGyNZDC.js → webrtcRemote-C3xQyf3u.js} +2 -2
  117. package/dist/{widget-Crb5zeHL.js → widget-DmRwOlco.js} +3 -3
  118. package/dist/widgets.js +1 -1
  119. package/dist/{workspaceConfig-BLWB5Wu9.js → workspaceConfig-DBgsu81B.js} +1 -1
  120. package/dist/{ws-DIbwNz8Z.js → ws-ByO6JO6m.js} +3 -3
  121. package/dist/{ws-BRhPG1Vy.js → ws-D0-Jg88z.js} +9 -8
  122. package/lab/ui/index.html +1 -1
  123. package/lab/ui/lab/posts/2026-08-11-slack-term-safe-writes.md +1 -1
  124. package/package.json +73 -8
  125. package/ts/JsonlStore.ts +10 -3
  126. package/ts/cli-idle.bun.spec.ts +1 -1
  127. package/ts/cli-idle.spec.ts +1 -1
  128. package/ts/cli.ts +35 -0
  129. package/ts/cmdKill.bun.spec.ts +155 -0
  130. package/ts/cmdKill.spec.ts +155 -0
  131. package/ts/cmdKill.ts +183 -0
  132. package/ts/composerGuard.edges.spec.ts +91 -0
  133. package/ts/composerGuard.spec.ts +53 -6
  134. package/ts/composerGuard.ts +42 -13
  135. package/ts/conptyModeFilter.spec.ts +30 -0
  136. package/ts/conptyModeFilter.ts +53 -0
  137. package/ts/copyLiagent.ts +42 -0
  138. package/ts/cwdConflictWarn.io.spec.ts +50 -0
  139. package/ts/cwdConflictWarn.spec.ts +34 -1
  140. package/ts/cwdConflictWarn.ts +43 -5
  141. package/ts/cwdGuard.spec.ts +110 -0
  142. package/ts/cwdGuard.ts +118 -0
  143. package/ts/forceKill.ts +52 -0
  144. package/ts/forkNested.bun.spec.ts +41 -0
  145. package/ts/forkNested.spec.ts +41 -0
  146. package/ts/forkNested.ts +48 -1
  147. package/ts/globalPidIndex.edges.spec.ts +125 -0
  148. package/ts/globalPidIndex.ts +6 -2
  149. package/ts/index.ts +13 -1
  150. package/ts/interruptSend.bun.spec.ts +99 -0
  151. package/ts/interruptSend.spec.ts +99 -0
  152. package/ts/interruptSend.ts +51 -0
  153. package/ts/lockRegistry.spec.ts +49 -0
  154. package/ts/lsRenderCache.spec.ts +58 -0
  155. package/ts/needsInput.edges.spec.ts +23 -0
  156. package/ts/parseCliArgs.ts +7 -0
  157. package/ts/pushGatePlan.spec.ts +147 -0
  158. package/ts/roomReplyAlias.io.spec.ts +81 -0
  159. package/ts/schedule.ts +5 -1
  160. package/ts/schedules.bun.spec.ts +397 -0
  161. package/ts/schedules.spec.ts +397 -0
  162. package/ts/schedules.ts +517 -0
  163. package/ts/schedulesCli.ts +386 -0
  164. package/ts/sendControl.bun.spec.ts +12 -0
  165. package/ts/senderAncestry.ts +1 -3
  166. package/ts/serve.ts +22 -33
  167. package/ts/serveRust.spec.ts +29 -2
  168. package/ts/serveRust.ts +27 -2
  169. package/ts/sizeNego.bun.spec.ts +9 -2
  170. package/ts/sizeNego.spec.ts +9 -2
  171. package/ts/sizeNego.ts +10 -5
  172. package/ts/subcommands.spec.ts +348 -9
  173. package/ts/subcommands.ts +513 -93
  174. package/ts/suggestAgents.spec.ts +33 -0
  175. package/ts/terminal/browser.ts +1 -1
  176. package/ts/todoParse.spec.ts +22 -1
  177. package/ts/todoParse.ts +19 -0
  178. package/dist/SUPPORTED_CLIS-ByO5twge.js +0 -12
  179. package/dist/callback-D7qfh_7p.js +0 -16
  180. package/dist/pidStore-BRlKRHXY.js +0 -6
  181. package/dist/roomReplyAlias-DuEkSSDI.js +0 -6
  182. package/dist/spawnGate-DeQBTX-o.js +0 -5
  183. package/dist/subcommands-DmiswRy4.js +0 -14
  184. package/dist/trayApp-C4TAvRvU.js +0 -5
@@ -0,0 +1,397 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import path from "path";
3
+ import {
4
+ type RunState,
5
+ type StoreEntry,
6
+ cronError,
7
+ dueNow,
8
+ fmtTime,
9
+ launchdDaemonPlist,
10
+ legacyRows,
11
+ logFile,
12
+ MAX_SLEEP_MS,
13
+ nextCronId,
14
+ nextRuns,
15
+ parseCron,
16
+ parseCronAdd,
17
+ parseState,
18
+ parseStore,
19
+ pickAutostart,
20
+ renderMessage,
21
+ resolveTarget,
22
+ stateFile,
23
+ storeFile,
24
+ systemdDaemonUnit,
25
+ tick,
26
+ validTz,
27
+ wakeDelay,
28
+ windowsTaskArgs,
29
+ } from "./schedules.ts";
30
+
31
+ const TZ = "Asia/Tokyo";
32
+ // 2026-01-05 (Mon) 00:00 Asia/Tokyo == 2026-01-04T15:00Z
33
+ const T0 = Date.UTC(2026, 0, 4, 15, 0);
34
+ const MIN = 60000;
35
+ const fmt = (ms: number[], tz = TZ) => ms.map((t) => fmtTime(t, tz).slice(0, 16));
36
+
37
+ function entry(over: Partial<StoreEntry> = {}): StoreEntry {
38
+ return {
39
+ name: "c1",
40
+ cron: "*/5 * * * *",
41
+ tz: TZ,
42
+ target: "lane:alpha",
43
+ message: "ping {name} at {now}",
44
+ enabled: true,
45
+ missed: "skip",
46
+ source: "cron",
47
+ since: 0,
48
+ ...over,
49
+ };
50
+ }
51
+
52
+ describe("parseCron", () => {
53
+ it("expands steps, ranges, lists, names and dow 7", () => {
54
+ const c = parseCron("*/15 9-11,14 1,15 jan-mar 5-7");
55
+ expect(c.minute.values).toEqual([0, 15, 30, 45]);
56
+ expect(c.minute.star).toBe(false);
57
+ expect(c.hour.values).toEqual([9, 10, 11, 14]);
58
+ expect(c.dom.values).toEqual([1, 15]);
59
+ expect(c.month.values).toEqual([1, 2, 3]);
60
+ expect(c.dow.values).toEqual([0, 5, 6]);
61
+ expect(parseCron("* * * * *").dow.star).toBe(true);
62
+ expect(parseCron("5/20 * * * MON").minute.values).toEqual([5, 25, 45]);
63
+ expect(parseCron("0 0 * * */2").dow.values).toEqual([0, 2, 4, 6]);
64
+ expect(parseCron("10-30/10 * * * *").minute.values).toEqual([10, 20, 30]);
65
+ });
66
+ it("rejects bad expressions", () => {
67
+ expect(cronError("* * * *")).toMatch(/5 fields/);
68
+ expect(cronError("60 * * * *")).toMatch(/minute: 60 out of range/);
69
+ expect(cronError("* 5-2 * * *")).toMatch(/backwards/);
70
+ expect(cronError("*/0 * * * *")).toMatch(/bad step/);
71
+ expect(cronError("*/x * * * *")).toMatch(/bad step/);
72
+ expect(cronError("1/2/3 * * * *")).toMatch(/bad term/);
73
+ expect(cronError(", * * * *")).toMatch(/bad term/);
74
+ expect(cronError("* * * foo *")).toMatch(/not a number/);
75
+ expect(cronError("0 9 * * 1-5")).toBeNull();
76
+ });
77
+ });
78
+
79
+ describe("nextRuns", () => {
80
+ it("*/5 in the given tz", () => {
81
+ expect(fmt(nextRuns("*/5 * * * *", TZ, T0, 3))).toEqual([
82
+ "2026-01-05 00:05",
83
+ "2026-01-05 00:10",
84
+ "2026-01-05 00:15",
85
+ ]);
86
+ });
87
+ it("ranges and lists; weekdays only", () => {
88
+ // Fri 2026-01-09 17:40 -> next: Fri 18:00, then Mon 09:00
89
+ const fri = Date.UTC(2026, 0, 9, 8, 40);
90
+ expect(fmt(nextRuns("0,30 9-18 * * 1-5", TZ, fri, 3))).toEqual([
91
+ "2026-01-09 18:00",
92
+ "2026-01-09 18:30",
93
+ "2026-01-12 09:00",
94
+ ]);
95
+ });
96
+ it("same instant in another tz is a different wall clock", () => {
97
+ const [ny] = nextRuns("0 9 * * *", "America/New_York", T0, 1);
98
+ expect(new Date(ny!).toISOString()).toBe("2026-01-05T14:00:00.000Z");
99
+ const [tk] = nextRuns("0 9 * * *", TZ, T0, 1);
100
+ expect(new Date(tk!).toISOString()).toBe("2026-01-05T00:00:00.000Z");
101
+ });
102
+ it("dom and dow both restricted: either matches (cron OR)", () => {
103
+ // 2026-01-05 Mon: the 13th (Tue) OR Fridays
104
+ expect(fmt(nextRuns("0 0 13 * 5", TZ, T0, 3))).toEqual([
105
+ "2026-01-09 00:00",
106
+ "2026-01-13 00:00",
107
+ "2026-01-16 00:00",
108
+ ]);
109
+ });
110
+ it("months and impossible dates", () => {
111
+ expect(fmt(nextRuns("0 12 1 mar *", TZ, T0, 1))).toEqual(["2026-03-01 12:00"]);
112
+ expect(nextRuns("0 0 31 2 *", TZ, T0, 1)).toEqual([]);
113
+ });
114
+ });
115
+
116
+ describe("parseCronAdd", () => {
117
+ it("parses quoted and unquoted cron", () => {
118
+ expect(parseCronAdd(["*/10 * * * *", "lane:alpha", "send", "progress"], {})).toEqual({
119
+ ok: true,
120
+ cron: "*/10 * * * *",
121
+ tz: TZ,
122
+ target: "lane:alpha",
123
+ message: "send progress",
124
+ });
125
+ const r = parseCronAdd(["0", "9", "*", "*", "1-5", "1111", "hi"], { AY_CRON_TZ: "UTC" });
126
+ expect(r).toMatchObject({ ok: true, cron: "0 9 * * 1-5", tz: "UTC", target: "1111" });
127
+ });
128
+ it("validates", () => {
129
+ expect(parseCronAdd(["* * * * *", "x"], {})).toMatchObject({
130
+ ok: false,
131
+ error: expect.stringMatching(/usage/),
132
+ });
133
+ expect(parseCronAdd(["61 * * * *", "x", "m"], {})).toMatchObject({
134
+ ok: false,
135
+ error: expect.stringMatching(/bad cron/),
136
+ });
137
+ expect(parseCronAdd(["* * * * *", "x", "m"], { AY_CRON_TZ: "Mars/Base" })).toMatchObject({
138
+ ok: false,
139
+ error: expect.stringMatching(/unknown tz/),
140
+ });
141
+ expect(parseCronAdd(["* * * * *", "lane:", "m"], {})).toMatchObject({
142
+ ok: false,
143
+ error: expect.stringMatching(/lane name/),
144
+ });
145
+ expect(validTz(TZ)).toBe(true);
146
+ });
147
+ });
148
+
149
+ describe("store", () => {
150
+ it("parses store and state defensively", () => {
151
+ expect(parseStore(null)).toEqual({ version: 1, entries: [] });
152
+ expect(parseStore('{"entries":{}}').entries).toEqual([]);
153
+ expect(parseStore(JSON.stringify({ entries: [entry()] })).entries[0]!.name).toBe("c1");
154
+ expect(parseState(null)).toEqual({ lastTick: 0, last: {} });
155
+ expect(parseState("{bad")).toEqual({ lastTick: 0, last: {} });
156
+ expect(parseState('{"lastTick":5,"last":{"a":{"at":1,"ok":true,"code":0}}}').last.a!.ok).toBe(
157
+ true,
158
+ );
159
+ });
160
+ it("allocates ids", () => {
161
+ expect(nextCronId({ version: 1, entries: [] })).toBe("c1");
162
+ expect(
163
+ nextCronId({ version: 1, entries: [entry({ name: "c1" }), entry({ name: "c3" })] }),
164
+ ).toBe("c2");
165
+ });
166
+ it("paths honor env", () => {
167
+ expect(storeFile({ AY_SCHEDULES_STORE: "/s" })).toBe("/s");
168
+ expect(stateFile({ AY_SCHEDULES_STATE: "/t" })).toBe("/t");
169
+ expect(logFile({ AY_SCHEDULES_LOG: "/l" })).toBe("/l");
170
+ expect(storeFile({}, "/home/alice")).toBe(
171
+ path.join("/home/alice", ".config", "agent-yes", "schedules.json"),
172
+ );
173
+ expect(stateFile({}, "/home/alice")).toMatch(/schedules\.state\.json$/);
174
+ expect(logFile({}, "/home/alice")).toBe(
175
+ path.join("/home/alice", ".local", "share", "agent-yes", "schedules.log"),
176
+ );
177
+ });
178
+ it("renders messages", () => {
179
+ expect(renderMessage(entry(), T0)).toBe("ping c1 at 2026-01-05 00:00 Asia/Tokyo");
180
+ });
181
+ it("legacy oxmgr rows", () => {
182
+ expect(legacyRows("id name\n1 agent-yes-cron-a\n2 other\n")).toEqual(["1 agent-yes-cron-a"]);
183
+ });
184
+ });
185
+
186
+ /** Run the daemon loop against a fake clock: one tick per minute, recording fires. */
187
+ function simulate(entries: StoreEntry[], st: RunState, from: number, minutes: number) {
188
+ const fired: string[] = [];
189
+ for (let i = 0; i < minutes; i++) {
190
+ const now = from + i * MIN + 50;
191
+ for (const e of dueNow(entries, st, now)) {
192
+ fired.push(`${e.name}@${fmtTime(now, TZ).slice(11, 16)}`);
193
+ st.last[e.name] = { at: now, ok: true, code: 0 };
194
+ }
195
+ st.lastTick = now;
196
+ }
197
+ return fired;
198
+ }
199
+
200
+ describe("scheduler loop (fake clock)", () => {
201
+ it("fires */5 on time, once per slot", () => {
202
+ const st: RunState = { lastTick: T0 - MIN, last: {} };
203
+ expect(simulate([entry()], st, T0, 16)).toEqual([
204
+ "c1@00:00",
205
+ "c1@00:05",
206
+ "c1@00:10",
207
+ "c1@00:15",
208
+ ]);
209
+ });
210
+ it("every minute fires every minute", () => {
211
+ const st: RunState = { lastTick: T0 - 30000, last: {} };
212
+ expect(simulate([entry({ cron: "* * * * *", since: T0 - 30000 })], st, T0, 3)).toEqual([
213
+ "c1@00:00",
214
+ "c1@00:01",
215
+ "c1@00:02",
216
+ ]);
217
+ });
218
+ it("does not fire before an entry was added", () => {
219
+ const st: RunState = { lastTick: T0 - MIN, last: {} };
220
+ expect(simulate([entry({ since: T0 + 2 * MIN })], st, T0, 6)).toEqual(["c1@00:05"]);
221
+ });
222
+ it("skips disabled entries", () => {
223
+ expect(simulate([entry({ enabled: false })], { lastTick: T0 - MIN, last: {} }, T0, 10)).toEqual(
224
+ [],
225
+ );
226
+ });
227
+ it("restart after downtime: missed=skip fires nothing for the gap", () => {
228
+ const st: RunState = { lastTick: T0, last: {} };
229
+ // daemon down 00:01..00:32, restarts at 00:33
230
+ expect(dueNow([entry()], st, T0 + 33 * MIN)).toEqual([]);
231
+ });
232
+ it("restart after downtime: missed=run-once fires exactly once", () => {
233
+ const st: RunState = { lastTick: T0, last: {} };
234
+ const e = entry({ cron: "0 * * * *", missed: "run-once" });
235
+ expect(simulate([e], st, T0 + 200 * MIN, 3)).toEqual(["c1@03:20"]);
236
+ });
237
+ it("restart exactly on a slot still fires it (on time)", () => {
238
+ const st: RunState = { lastTick: T0, last: {} };
239
+ expect(dueNow([entry()], st, T0 + 30 * MIN + 50).map((e) => e.name)).toEqual(["c1"]);
240
+ });
241
+ });
242
+
243
+ /**
244
+ * Event-driven daemon on a fake clock: sleep wakeDelay(), unless the store changes
245
+ * first (`changes`: at -> new entries) or the machine suspends (`suspend`: the
246
+ * wake that was due at or after `from` happens at `until`).
247
+ */
248
+ function runDaemon(
249
+ initial: StoreEntry[],
250
+ start: number,
251
+ end: number,
252
+ o: {
253
+ changes?: [number, StoreEntry[]][];
254
+ suspend?: { from: number; until: number };
255
+ state?: RunState;
256
+ } = {},
257
+ ) {
258
+ let entries = initial;
259
+ let st: RunState = o.state ?? { lastTick: 0, last: {} };
260
+ let now = start;
261
+ const changes = [...(o.changes ?? [])];
262
+ const fired: string[] = [];
263
+ const wakes: string[] = [];
264
+ let suspended = false;
265
+ for (;;) {
266
+ const t = tick(entries, st, now);
267
+ for (const e of t.fire) {
268
+ fired.push(`${e.name}@${fmtTime(now, TZ).slice(11, 16)}`);
269
+ t.state.last[e.name] = { at: now, ok: true, code: 0 };
270
+ }
271
+ st = t.state;
272
+ let next = now + wakeDelay(entries, now);
273
+ if (changes.length && changes[0]![0] < next) {
274
+ const [at, es] = changes.shift()!;
275
+ next = at;
276
+ entries = es;
277
+ }
278
+ if (o.suspend && !suspended && next >= o.suspend.from) {
279
+ suspended = true;
280
+ next = o.suspend.until;
281
+ }
282
+ if (next > end) break;
283
+ now = next;
284
+ wakes.push(fmtTime(now, TZ).slice(11, 16));
285
+ }
286
+ return { fired, wakes };
287
+ }
288
+
289
+ describe("daemon wake-ups (no polling)", () => {
290
+ const hourly = entry({ name: "h", cron: "0 * * * *" });
291
+ it("sleeps until the earliest next fire, capped at a day", () => {
292
+ expect(wakeDelay([hourly, entry({ cron: "*/20 * * * *" })], T0 + 10 * MIN)).toBe(10 * MIN + 50);
293
+ expect(wakeDelay([], T0)).toBe(MAX_SLEEP_MS);
294
+ expect(wakeDelay([entry({ enabled: false })], T0)).toBe(MAX_SLEEP_MS);
295
+ expect(wakeDelay([entry({ cron: "0 0 1 1 *" })], T0)).toBe(MAX_SLEEP_MS);
296
+ });
297
+ it("wakes only at fire times and re-arms after each", () => {
298
+ const r = runDaemon([hourly], T0 + 10 * MIN, T0 + 3 * 60 * MIN + MIN);
299
+ expect(r.fired).toEqual(["h@01:00", "h@02:00", "h@03:00"]);
300
+ expect(r.wakes).toEqual(["01:00", "02:00", "03:00"]);
301
+ });
302
+ it("store changed while sleeping: re-plans at once", () => {
303
+ const r = runDaemon([hourly], T0 + 10 * MIN, T0 + 51 * MIN, {
304
+ changes: [[T0 + 31 * MIN, [hourly, entry({ name: "c2", since: T0 + 31 * MIN })]]],
305
+ });
306
+ expect(r.wakes.slice(0, 3)).toEqual(["00:31", "00:35", "00:40"]);
307
+ expect(r.fired).toEqual(["c2@00:35", "c2@00:40", "c2@00:45", "c2@00:50"]);
308
+ });
309
+ it("slept past the slot (suspend): skip fires nothing, run-once fires once", () => {
310
+ const sus = { from: T0 + 60 * MIN, until: T0 + 200 * MIN };
311
+ const skip = runDaemon([hourly], T0 + 10 * MIN, T0 + 241 * MIN, { suspend: sus });
312
+ expect(skip.fired).toEqual(["h@04:00"]);
313
+ const once = runDaemon([{ ...hourly, missed: "run-once" }], T0 + 10 * MIN, T0 + 241 * MIN, {
314
+ suspend: sus,
315
+ });
316
+ expect(once.fired).toEqual(["h@03:20", "h@04:00"]);
317
+ });
318
+ it("clock jumped backwards: no replay, resumes from the new time", () => {
319
+ const st: RunState = {
320
+ lastTick: T0 + 120 * MIN,
321
+ last: { h: { at: T0 + 120 * MIN, ok: true, code: 0 } },
322
+ };
323
+ const t = tick([hourly], st, T0 + 30 * MIN);
324
+ expect(t.clockBack).toBe(true);
325
+ expect(t.fire).toEqual([]);
326
+ expect(t.state.lastTick).toBe(T0 + 30 * MIN);
327
+ const r = runDaemon([hourly], T0 + 30 * MIN, T0 + 61 * MIN, { state: st });
328
+ expect(r.fired).toEqual(["h@01:00"]);
329
+ });
330
+ it("first start fires nothing missed", () => {
331
+ expect(tick([entry()], { lastTick: 0, last: {} }, T0 + 2 * MIN + 50).fire).toEqual([]);
332
+ expect(tick([entry()], { lastTick: 0, last: {} }, T0 + 50).fire.length).toBe(1);
333
+ });
334
+ });
335
+
336
+ describe("resolveTarget", () => {
337
+ const live = [
338
+ { pid: 1111, agent_id: "abc123", cwd: "/repo/alpha" },
339
+ { pid: 2222, agent_id: "abd456", cwd: "/repo/beta" },
340
+ ];
341
+ const lane = (ok: boolean, out: string) => () => ({ ok, out });
342
+ it("lanes go through ay lanes id", () => {
343
+ expect(resolveTarget("lane:alpha", live, lane(true, "zz9\n"))).toEqual({
344
+ ok: true,
345
+ agentId: "zz9",
346
+ });
347
+ expect(resolveTarget("lane:alpha", live, lane(false, "no live agent"))).toMatchObject({
348
+ ok: false,
349
+ error: expect.stringMatching(/lane "alpha" has no live agent: no live agent/),
350
+ });
351
+ expect(resolveTarget("lane:alpha", live, lane(false, ""))).toEqual({
352
+ ok: false,
353
+ error: `lane "alpha" has no live agent`,
354
+ });
355
+ });
356
+ it("agent id, pid, prefix, keyword", () => {
357
+ expect(resolveTarget("abd456", live, lane(false, ""))).toEqual({ ok: true, agentId: "abd456" });
358
+ expect(resolveTarget("1111", live, lane(false, ""))).toEqual({ ok: true, agentId: "abc123" });
359
+ expect(resolveTarget("abc", live, lane(false, ""))).toEqual({ ok: true, agentId: "abc123" });
360
+ expect(resolveTarget("beta", live, lane(false, ""))).toEqual({ ok: true, agentId: "abd456" });
361
+ expect(resolveTarget("ab", live, lane(false, ""))).toMatchObject({
362
+ ok: false,
363
+ error: expect.stringMatching(/matches 2/),
364
+ });
365
+ expect(resolveTarget("gamma", live, lane(false, ""))).toMatchObject({
366
+ ok: false,
367
+ error: expect.stringMatching(/no live agent/),
368
+ });
369
+ });
370
+ });
371
+
372
+ describe("daemon autostart", () => {
373
+ it("prefers oxmgr, then the OS's own", () => {
374
+ const p = (platform: string, hasOxmgr = false, systemdUser = false) =>
375
+ pickAutostart({ platform, hasOxmgr, systemdUser });
376
+ expect(p("linux", true, true)).toBe("oxmgr");
377
+ expect(p("linux", false, true)).toBe("systemd");
378
+ expect(p("linux")).toBe("nohup");
379
+ expect(p("darwin")).toBe("launchd");
380
+ expect(p("win32")).toBe("schtasks");
381
+ expect(p("freebsd")).toBe("nohup");
382
+ });
383
+ it("renders units", () => {
384
+ const u = systemdDaemonUnit(
385
+ ["/opt/my bun", "/x/ay.js", "schedules", "daemon", "run"],
386
+ "/usr/bin",
387
+ );
388
+ expect(u).toContain(`ExecStart="/opt/my bun" /x/ay.js schedules daemon run`);
389
+ expect(u).toContain("Restart=always");
390
+ const pl = launchdDaemonPlist(["/x/a&b", "run"], "/usr/bin", "/tmp/l");
391
+ expect(pl).toContain("<string>/x/a&amp;b</string>");
392
+ expect(pl).toContain("<key>KeepAlive</key><true/>");
393
+ expect(windowsTaskArgs(["C:\\Program Files\\ay.exe", "run"])).toContain(
394
+ `"C:\\Program Files\\ay.exe" run`,
395
+ );
396
+ });
397
+ });