@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.
- package/README.md +31 -18
- package/dist/accounts.d.ts +66 -0
- package/dist/accounts.d.ts.map +1 -0
- package/dist/accounts.js +166 -0
- package/dist/accounts.js.map +1 -0
- package/dist/bin.js +4 -0
- package/dist/bin.js.map +1 -1
- package/dist/channel.d.ts +15 -10
- package/dist/channel.d.ts.map +1 -1
- package/dist/channel.js +113 -158
- package/dist/channel.js.map +1 -1
- package/dist/claude.d.ts +17 -13
- package/dist/claude.d.ts.map +1 -1
- package/dist/claude.js +69 -59
- package/dist/claude.js.map +1 -1
- package/dist/cli.d.ts +5 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +317 -131
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +46 -10
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +142 -51
- package/dist/config.js.map +1 -1
- package/dist/daemon.d.ts +29 -4
- package/dist/daemon.d.ts.map +1 -1
- package/dist/daemon.js +174 -51
- package/dist/daemon.js.map +1 -1
- package/dist/launchd.d.ts +12 -16
- package/dist/launchd.d.ts.map +1 -1
- package/dist/launchd.js +28 -25
- package/dist/launchd.js.map +1 -1
- package/dist/prompts.d.ts +53 -0
- package/dist/prompts.d.ts.map +1 -0
- package/dist/prompts.js +171 -0
- package/dist/prompts.js.map +1 -0
- package/dist/service.d.ts +37 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +71 -0
- package/dist/service.js.map +1 -0
- package/dist/setup.d.ts +37 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +121 -0
- package/dist/setup.js.map +1 -0
- package/dist/system.d.ts +6 -0
- package/dist/system.d.ts.map +1 -0
- package/dist/system.js +16 -0
- package/dist/system.js.map +1 -0
- package/dist/systemd.d.ts +14 -0
- package/dist/systemd.d.ts.map +1 -0
- package/dist/systemd.js +62 -0
- package/dist/systemd.js.map +1 -0
- package/dist/watchdog.d.ts +14 -4
- package/dist/watchdog.d.ts.map +1 -1
- package/dist/watchdog.js +30 -24
- package/dist/watchdog.js.map +1 -1
- package/package.json +8 -12
- package/src/accounts.ts +242 -0
- package/src/bin.ts +6 -0
- package/src/channel.ts +151 -172
- package/src/claude.ts +90 -88
- package/src/cli.ts +494 -183
- package/src/config.ts +239 -63
- package/src/daemon.ts +215 -65
- package/src/launchd.ts +40 -35
- package/src/prompts.ts +255 -0
- package/src/service.ts +145 -0
- package/src/setup.ts +188 -0
- package/src/system.ts +11 -0
- package/src/systemd.ts +78 -0
- 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
|
|
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
|
-
/**
|
|
71
|
-
readonly
|
|
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
|
-
|
|
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 +
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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;
|