@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/claude.ts CHANGED
@@ -17,10 +17,11 @@ import { delimiter, join } from "node:path";
17
17
  import type { Harnessd } from "./config";
18
18
  import type { Watchdog } from "./watchdog";
19
19
 
20
- /** How Claude Code names a `--plugin-dir` plugin, and what `--channels` takes. */
20
+ /** How Claude Code names a `--plugin-dir` plugin. */
21
21
  export const PLUGIN = "harnessd@inline";
22
- export const MANAGED_SETTINGS =
23
- "/Library/Application Support/ClaudeCode/managed-settings.json";
22
+
23
+ /** The channel's MCP server in harnessd's plugin, and so each event's `source`: `plugin:harnessd:channel`. */
24
+ export const CHANNEL_SERVER = "channel";
24
25
 
25
26
  const claudeDir = (env: NodeJS.ProcessEnv) =>
26
27
  env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude");
@@ -86,56 +87,119 @@ export const which = (name: string, path = process.env.PATH ?? "") => {
86
87
  );
87
88
  };
88
89
 
90
+ const shellQuote = (text: string) => `'${text.replaceAll("'", "'\\''")}'`;
91
+
89
92
  /**
90
- * harnessd's plugin, written before each start from the config's channels: one MCP server per channel, each
91
- * `harnessd channel <name>`. None: no plugin.
93
+ * harnessd's plugin, written before each start: the channel's MCP server (`harnessd channel`) when the config
94
+ * subscribes to events, a command per server in its `bin` (`harnessd tools <server>`), a SessionStart hook that
95
+ * tells harnessd which conversation Claude Code is in, so a /clear or a /resume is the conversation the next start
96
+ * resumes, and a Notification hook that tells harnessd when Claude Code waits for input with nothing running.
92
97
  */
93
98
  export const writePlugin = (
94
99
  paths: Harnessd.Paths,
95
100
  config: Harnessd.Config,
96
101
  bin: string,
97
- ): boolean => {
102
+ ) => {
98
103
  rmSync(paths.plugin, { recursive: true, force: true });
99
- const names = Object.keys(config.channels);
100
- if (names.length === 0) return false;
101
104
  mkdirSync(join(paths.plugin, ".claude-plugin"), { recursive: true });
105
+ mkdirSync(paths.bin, { recursive: true });
106
+ const exports = Object.entries(paths.env).map(
107
+ ([name, value]) => `${name}=${shellQuote(value)}`,
108
+ );
109
+ const harnessd = [shellQuote(process.execPath), shellQuote(bin)];
110
+ for (const name of Object.keys(config.servers))
111
+ writeFileSync(
112
+ join(paths.bin, name),
113
+ [
114
+ "#!/bin/sh",
115
+ ...exports.map((line) => `export ${line}`),
116
+ `exec ${[...harnessd, "tools", name].join(" ")} "$@"`,
117
+ "",
118
+ ].join("\n"),
119
+ { mode: 0o755 },
120
+ );
121
+ const hook = (command: string) =>
122
+ [...exports, ...harnessd, command].join(" ");
102
123
  const manifest = {
103
124
  name: "harnessd",
104
125
  version: "1.0.0",
105
- description: "harnessd's channels: each one's events, and its tools",
106
- mcpServers: Object.fromEntries(
107
- names.map((name) => [
108
- name,
109
- {
126
+ description:
127
+ "harnessd's channel of the servers' events, and its SessionStart and Notification hooks",
128
+ ...(config.channel.subscribe.length > 0 && {
129
+ mcpServers: {
130
+ [CHANNEL_SERVER]: {
110
131
  command: process.execPath,
111
- args: [bin, "channel", name],
112
- env: { HARNESSD_HOME: paths.home },
132
+ args: [bin, "channel"],
133
+ env: paths.env,
113
134
  },
114
- ]),
115
- ),
116
- channels: names.map((name) => ({ server: name, displayName: name })),
135
+ },
136
+ channels: [{ server: CHANNEL_SERVER, displayName: "harnessd" }],
137
+ }),
138
+ hooks: {
139
+ SessionStart: [
140
+ { hooks: [{ type: "command", command: hook("session-start") }] },
141
+ ],
142
+ // Claude Code's own idle: a minute after a turn, with no background agent or dialog
143
+ Notification: [
144
+ {
145
+ matcher: "idle_prompt",
146
+ hooks: [{ type: "command", command: hook("idle") }],
147
+ },
148
+ ],
149
+ },
117
150
  };
118
151
  writeFileSync(
119
152
  join(paths.plugin, ".claude-plugin", "plugin.json"),
120
153
  `${JSON.stringify(manifest, null, 2)}\n`,
121
154
  );
122
- return true;
123
155
  };
124
156
 
157
+ /** The version a claude command prints, such as `2.1.335`; none when it doesn't run or prints no version. */
158
+ export const claudeVersion = (file: string, env: NodeJS.ProcessEnv) =>
159
+ new Promise<string | undefined>((resolve) =>
160
+ execFile(file, ["--version"], { env, timeout: 30_000 }, (error, stdout) =>
161
+ resolve(error ? undefined : /^\s*(\d+\.\d+\.\d+\S*)/.exec(stdout)?.[1]),
162
+ ),
163
+ );
164
+
165
+ /** Whether these claude arguments pick a permission mode themselves. */
166
+ const picksPermissionMode = (args: readonly string[]) =>
167
+ args.some(
168
+ (arg) =>
169
+ arg === "--dangerously-skip-permissions" ||
170
+ arg === "--permission-mode" ||
171
+ arg.startsWith("--permission-mode="),
172
+ );
173
+
174
+ /** Whether these claude arguments turn permission prompts off: the config's `args` (or prestart's) ask for it, harnessd never does. */
175
+ export const skipsPermissions = (args: readonly string[]) =>
176
+ args.some(
177
+ (arg, i) =>
178
+ arg === "--dangerously-skip-permissions" ||
179
+ arg === "--permission-mode=bypassPermissions" ||
180
+ (arg === "bypassPermissions" && args[i - 1] === "--permission-mode"),
181
+ );
182
+
125
183
  /**
126
- * The claude command line for one start (after the binary). `--channels` names the plugin only when the managed
127
- * settings let it in: a refused entry there also shuts out the same plugin loaded with the development flag.
184
+ * The claude command line for one start (after the binary). Claude Code runs in its auto permission mode, which
185
+ * decides each action itself, unless the config's arguments pick another mode. harnessd's plugin is written on this Mac at each start, so
186
+ * its channel loads with Claude Code's flag for local channels, which asks for no entry in an approved list. Claude
187
+ * Code then asks once per start whether this is local development; harnessd answers that (see prompts.ts).
128
188
  */
129
189
  export const claudeArgs = (o: {
130
190
  start: Watchdog.Start;
131
- plugin: string | undefined;
132
- allowed: boolean;
191
+ plugin: string;
133
192
  config: Harnessd.Config;
134
193
  extra: readonly string[];
135
194
  }) => [
136
- "--dangerously-skip-permissions",
137
- ...(o.plugin ? ["--plugin-dir", o.plugin] : []),
138
- ...(o.plugin && o.allowed ? ["--channels", `plugin:${PLUGIN}`] : []),
195
+ ...(picksPermissionMode([...o.config.args, ...o.extra])
196
+ ? []
197
+ : ["--permission-mode", "auto"]),
198
+ "--plugin-dir",
199
+ o.plugin,
200
+ ...(o.config.channel.subscribe.length > 0
201
+ ? ["--dangerously-load-development-channels", `plugin:${PLUGIN}`]
202
+ : []),
139
203
  ...(o.start.resume
140
204
  ? ["--resume", o.start.sessionId]
141
205
  : ["--session-id", o.start.sessionId]),
@@ -210,65 +274,3 @@ export const prestart = (
210
274
  },
211
275
  );
212
276
  });
213
-
214
- /** What keeps harnessd's channels out of Claude Code on this Mac: its managed settings must let `harnessd@inline` in. */
215
- export const channelBlockers = (file = MANAGED_SETTINGS): string[] => {
216
- let settings: { channelsEnabled?: unknown; allowedChannelPlugins?: unknown } =
217
- {};
218
- try {
219
- const parsed: unknown = JSON.parse(readFileSync(file, "utf8"));
220
- if (typeof parsed === "object" && parsed !== null) settings = parsed;
221
- } catch {}
222
- const listed =
223
- Array.isArray(settings.allowedChannelPlugins) &&
224
- settings.allowedChannelPlugins.some(
225
- (entry: unknown) =>
226
- entry === PLUGIN ||
227
- (typeof entry === "object" &&
228
- entry !== null &&
229
- (entry as { plugin?: unknown }).plugin === "harnessd" &&
230
- (entry as { marketplace?: unknown }).marketplace === "inline"),
231
- );
232
- return [
233
- ...(settings.channelsEnabled === true
234
- ? []
235
- : [`${file} doesn't set channelsEnabled: true`]),
236
- ...(listed
237
- ? []
238
- : [`${file} doesn't list ${PLUGIN} in allowedChannelPlugins`]),
239
- ];
240
- };
241
-
242
- /** The managed settings with harnessd's channel let in; a list that wasn't there starts with Anthropic's own channels. */
243
- export const allowChannel = (
244
- file = MANAGED_SETTINGS,
245
- ): Record<string, unknown> => {
246
- // only a missing file starts from nothing: anything else there is policy to keep
247
- const settings: Record<string, unknown> = existsSync(file)
248
- ? JSON.parse(readFileSync(file, "utf8"))
249
- : {};
250
- if (
251
- typeof settings !== "object" ||
252
- settings === null ||
253
- Array.isArray(settings)
254
- )
255
- throw new Error(
256
- `${file} isn't a JSON object; harnessd won't write over it`,
257
- );
258
- const list = Array.isArray(settings.allowedChannelPlugins)
259
- ? settings.allowedChannelPlugins
260
- : ["telegram", "discord", "imessage", "fakechat"].map((plugin) => ({
261
- marketplace: "claude-plugins-official",
262
- plugin,
263
- }));
264
- return {
265
- ...settings,
266
- channelsEnabled: true,
267
- allowedChannelPlugins: [
268
- ...list.filter(
269
- (entry) => !(typeof entry === "object" && entry?.plugin === "harnessd"),
270
- ),
271
- { marketplace: "inline", plugin: "harnessd" },
272
- ],
273
- };
274
- };