@harness-sdk/harnessd 0.0.1 → 0.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 (70) hide show
  1. package/README.md +31 -18
  2. package/dist/accounts.d.ts +66 -0
  3. package/dist/accounts.d.ts.map +1 -0
  4. package/dist/accounts.js +166 -0
  5. package/dist/accounts.js.map +1 -0
  6. package/dist/bin.js +4 -0
  7. package/dist/bin.js.map +1 -1
  8. package/dist/channel.d.ts +15 -10
  9. package/dist/channel.d.ts.map +1 -1
  10. package/dist/channel.js +113 -158
  11. package/dist/channel.js.map +1 -1
  12. package/dist/claude.d.ts +17 -13
  13. package/dist/claude.d.ts.map +1 -1
  14. package/dist/claude.js +69 -59
  15. package/dist/claude.js.map +1 -1
  16. package/dist/cli.d.ts +5 -0
  17. package/dist/cli.d.ts.map +1 -1
  18. package/dist/cli.js +317 -131
  19. package/dist/cli.js.map +1 -1
  20. package/dist/config.d.ts +46 -10
  21. package/dist/config.d.ts.map +1 -1
  22. package/dist/config.js +142 -51
  23. package/dist/config.js.map +1 -1
  24. package/dist/daemon.d.ts +29 -4
  25. package/dist/daemon.d.ts.map +1 -1
  26. package/dist/daemon.js +174 -51
  27. package/dist/daemon.js.map +1 -1
  28. package/dist/launchd.d.ts +12 -16
  29. package/dist/launchd.d.ts.map +1 -1
  30. package/dist/launchd.js +28 -25
  31. package/dist/launchd.js.map +1 -1
  32. package/dist/prompts.d.ts +53 -0
  33. package/dist/prompts.d.ts.map +1 -0
  34. package/dist/prompts.js +171 -0
  35. package/dist/prompts.js.map +1 -0
  36. package/dist/service.d.ts +37 -0
  37. package/dist/service.d.ts.map +1 -0
  38. package/dist/service.js +71 -0
  39. package/dist/service.js.map +1 -0
  40. package/dist/setup.d.ts +37 -0
  41. package/dist/setup.d.ts.map +1 -0
  42. package/dist/setup.js +121 -0
  43. package/dist/setup.js.map +1 -0
  44. package/dist/system.d.ts +6 -0
  45. package/dist/system.d.ts.map +1 -0
  46. package/dist/system.js +16 -0
  47. package/dist/system.js.map +1 -0
  48. package/dist/systemd.d.ts +14 -0
  49. package/dist/systemd.d.ts.map +1 -0
  50. package/dist/systemd.js +62 -0
  51. package/dist/systemd.js.map +1 -0
  52. package/dist/watchdog.d.ts +14 -4
  53. package/dist/watchdog.d.ts.map +1 -1
  54. package/dist/watchdog.js +30 -24
  55. package/dist/watchdog.js.map +1 -1
  56. package/package.json +8 -12
  57. package/src/accounts.ts +242 -0
  58. package/src/bin.ts +6 -0
  59. package/src/channel.ts +151 -172
  60. package/src/claude.ts +90 -88
  61. package/src/cli.ts +494 -183
  62. package/src/config.ts +239 -63
  63. package/src/daemon.ts +215 -65
  64. package/src/launchd.ts +40 -35
  65. package/src/prompts.ts +255 -0
  66. package/src/service.ts +145 -0
  67. package/src/setup.ts +188 -0
  68. package/src/system.ts +11 -0
  69. package/src/systemd.ts +78 -0
  70. package/src/watchdog.ts +41 -33
package/src/watchdog.ts CHANGED
@@ -3,17 +3,19 @@ import { randomUUID } from "node:crypto";
3
3
  export namespace Watchdog {
4
4
  /** What harnessd keeps across its own restarts. */
5
5
  export type State = {
6
- /** The conversation the next start resumes. */
6
+ /** The conversation Claude Code is in, and the next start resumes. */
7
7
  readonly sessionId?: string;
8
8
  /** Stopped with `harnessd stop`: nothing starts until `harnessd start`. */
9
9
  readonly stopped?: boolean;
10
+ /** The account Claude Code last ran on, of the config's accounts. */
11
+ readonly account?: string;
10
12
  };
11
13
 
12
14
  /** One start of Claude Code: a new conversation with this id, or a resume of it. */
13
15
  export type Start = {
14
16
  readonly sessionId: string;
15
17
  readonly resume: boolean;
16
- /** The first message: the one `harnessd new` gave, or why a resumed conversation restarted. */
18
+ /** The first message of a resumed conversation: why it restarted. */
17
19
  readonly prompt?: string;
18
20
  };
19
21
 
@@ -22,6 +24,8 @@ export namespace Watchdog {
22
24
  /** Resolves with the exit code (128 + signal for a signal). */
23
25
  readonly exited: Promise<number>;
24
26
  readonly kill: (signal: NodeJS.Signals) => void;
27
+ /** After the exit: why it ended, when the launcher knows and the next start can go on in the same conversation at once. */
28
+ readonly ended?: () => string | undefined;
25
29
  };
26
30
 
27
31
  export type Timing = {
@@ -35,6 +39,8 @@ export namespace Watchdog {
35
39
  readonly runningAfterMs: number;
36
40
  /** How long a stop waits after SIGTERM before SIGKILL. */
37
41
  readonly killAfterMs: number;
42
+ /** How long start and a restart wait for Claude Code to start: longer than a prestart command may take (60 s). */
43
+ readonly spawnWaitMs: number;
38
44
  };
39
45
 
40
46
  export type Options = {
@@ -67,8 +73,10 @@ export namespace Watchdog {
67
73
  readonly status: () => Status;
68
74
  readonly stop: () => Promise<string>;
69
75
  readonly start: () => Promise<string>;
70
- /** Starts a new conversation, with `message` as its first message. */
71
- readonly fresh: (message?: string) => Promise<string>;
76
+ /** Claude Code moved to this conversation (a /clear, a /resume): later starts resume it. */
77
+ readonly follow: (sessionId: string) => void;
78
+ /** Stops the Claude Code that runs and starts it again at once, in the same conversation, which is told `reason`. With `pid`, only while that start still runs. */
79
+ readonly restart: (reason: string, pid?: number) => Promise<string>;
72
80
  /** Stops Claude Code and ends `run`, keeping the conversation for the next harnessd. */
73
81
  readonly shutdown: () => Promise<void>;
74
82
  };
@@ -81,9 +89,12 @@ const DEFAULT_TIMING: Watchdog.Timing = {
81
89
  resumeFailedMs: 30_000,
82
90
  runningAfterMs: 3_000,
83
91
  killAfterMs: 10_000,
92
+ spawnWaitMs: 75_000,
84
93
  };
85
94
 
86
- const SPAWN_WAIT_MS = 30_000;
95
+ /** What start and a restart answer when Claude Code didn't start. */
96
+ export const NOT_STARTED =
97
+ "Claude Code didn't start in time; harnessd status shows the log.";
87
98
 
88
99
  export const watchdog = (options: Watchdog.Options): Watchdog.Watchdog => {
89
100
  const timing = { ...DEFAULT_TIMING, ...options.timing };
@@ -98,7 +109,6 @@ export const watchdog = (options: Watchdog.Options): Watchdog.Watchdog => {
98
109
  let shuttingDown = false;
99
110
  let restartNow = false;
100
111
  let fresh = false;
101
- let firstMessage: string | undefined;
102
112
  let reason = "harnessd started (a login, a reboot or an update)";
103
113
  let wakeUp = () => {};
104
114
  let finished: Promise<void> | undefined;
@@ -126,7 +136,7 @@ export const watchdog = (options: Watchdog.Options): Watchdog.Watchdog => {
126
136
  const timer = setTimeout(() => {
127
137
  spawnWaiters.delete(done);
128
138
  resolve(false);
129
- }, timing.killAfterMs + SPAWN_WAIT_MS);
139
+ }, timing.killAfterMs + timing.spawnWaitMs);
130
140
  spawnWaiters.add(done);
131
141
  });
132
142
  const kill = async (run: Watchdog.Run) => {
@@ -154,12 +164,7 @@ export const watchdog = (options: Watchdog.Options): Watchdog.Watchdog => {
154
164
  resume: true,
155
165
  prompt: `harnessd restarted Claude Code: ${reason}. This is the same conversation.`,
156
166
  };
157
- // a new conversation asked for stays asked for, with its first message, until a start of it succeeds
158
- return {
159
- sessionId: newId(),
160
- resume: false,
161
- ...(firstMessage !== undefined && { prompt: firstMessage }),
162
- };
167
+ return { sessionId: newId(), resume: false };
163
168
  };
164
169
 
165
170
  const loop = async () => {
@@ -201,10 +206,7 @@ export const watchdog = (options: Watchdog.Options): Watchdog.Watchdog => {
201
206
  reason = "the start before this one failed";
202
207
  }
203
208
  if (run) {
204
- if (!start.resume) {
205
- fresh = false;
206
- firstMessage = undefined;
207
- }
209
+ if (!start.resume) fresh = false;
208
210
  const spawnedAt = now();
209
211
  set({ pid: run.pid });
210
212
  for (const waiter of spawnWaiters) waiter();
@@ -228,6 +230,12 @@ export const watchdog = (options: Watchdog.Options): Watchdog.Watchdog => {
228
230
  continue;
229
231
  }
230
232
  if (options.state.read().stopped) continue;
233
+ const ended = run.ended?.();
234
+ if (ended !== undefined) {
235
+ reason = ended;
236
+ backoff = timing.minBackoffMs;
237
+ continue;
238
+ }
231
239
  if (start.resume && ran < timing.resumeFailedMs) {
232
240
  fresh = true;
233
241
  reason = `resuming conversation ${start.sessionId} failed (exit code ${code})`;
@@ -277,26 +285,26 @@ export const watchdog = (options: Watchdog.Options): Watchdog.Watchdog => {
277
285
  reason = "started with harnessd start";
278
286
  const spawned = nextSpawn();
279
287
  wakeUp();
280
- return (await spawned)
281
- ? "Started Claude Code."
282
- : "Claude Code didn't start in time; see harnessd logs.";
288
+ return (await spawned) ? "Started Claude Code." : NOT_STARTED;
283
289
  }),
284
- fresh: (message) =>
290
+ follow: (sessionId) => {
291
+ if (sessionId === options.state.read().sessionId) return;
292
+ options.state.write({ ...options.state.read(), sessionId });
293
+ set({ sessionId });
294
+ options.log(`Claude Code moved to conversation ${sessionId}`);
295
+ },
296
+ restart: (why, pid) =>
285
297
  serial(async () => {
286
298
  await launching;
287
- options.state.write({ ...options.state.read(), stopped: false });
288
- fresh = true;
289
- firstMessage = message;
290
- reason = "a new conversation was asked for";
291
- const spawned = nextSpawn();
292
299
  const run = current;
293
- if (run) {
294
- restartNow = true;
295
- await kill(run);
296
- } else wakeUp();
297
- return (await spawned)
298
- ? `Started a new conversation (${status.sessionId}).`
299
- : "Claude Code didn't start in time; see harnessd logs.";
300
+ if (!run) return "Claude Code isn't running.";
301
+ if (pid !== undefined && run.pid !== pid)
302
+ return "That start of Claude Code is over.";
303
+ reason = why;
304
+ const spawned = nextSpawn();
305
+ restartNow = true;
306
+ await kill(run);
307
+ return (await spawned) ? "Restarted Claude Code." : NOT_STARTED;
300
308
  }),
301
309
  shutdown: async () => {
302
310
  shuttingDown = true;