@harness-sdk/harnessd 0.0.1 → 0.0.2

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 (66) hide show
  1. package/README.md +14 -13
  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.js +1 -1
  9. package/dist/claude.d.ts +12 -13
  10. package/dist/claude.d.ts.map +1 -1
  11. package/dist/claude.js +45 -59
  12. package/dist/claude.js.map +1 -1
  13. package/dist/cli.d.ts +5 -0
  14. package/dist/cli.d.ts.map +1 -1
  15. package/dist/cli.js +269 -114
  16. package/dist/cli.js.map +1 -1
  17. package/dist/config.d.ts +14 -1
  18. package/dist/config.d.ts.map +1 -1
  19. package/dist/config.js +53 -13
  20. package/dist/config.js.map +1 -1
  21. package/dist/daemon.d.ts +17 -4
  22. package/dist/daemon.d.ts.map +1 -1
  23. package/dist/daemon.js +112 -51
  24. package/dist/daemon.js.map +1 -1
  25. package/dist/launchd.d.ts +12 -16
  26. package/dist/launchd.d.ts.map +1 -1
  27. package/dist/launchd.js +28 -25
  28. package/dist/launchd.js.map +1 -1
  29. package/dist/prompts.d.ts +53 -0
  30. package/dist/prompts.d.ts.map +1 -0
  31. package/dist/prompts.js +171 -0
  32. package/dist/prompts.js.map +1 -0
  33. package/dist/service.d.ts +37 -0
  34. package/dist/service.d.ts.map +1 -0
  35. package/dist/service.js +71 -0
  36. package/dist/service.js.map +1 -0
  37. package/dist/setup.d.ts +37 -0
  38. package/dist/setup.d.ts.map +1 -0
  39. package/dist/setup.js +121 -0
  40. package/dist/setup.js.map +1 -0
  41. package/dist/system.d.ts +6 -0
  42. package/dist/system.d.ts.map +1 -0
  43. package/dist/system.js +16 -0
  44. package/dist/system.js.map +1 -0
  45. package/dist/systemd.d.ts +14 -0
  46. package/dist/systemd.d.ts.map +1 -0
  47. package/dist/systemd.js +62 -0
  48. package/dist/systemd.js.map +1 -0
  49. package/dist/watchdog.d.ts +14 -4
  50. package/dist/watchdog.d.ts.map +1 -1
  51. package/dist/watchdog.js +30 -24
  52. package/dist/watchdog.js.map +1 -1
  53. package/package.json +5 -3
  54. package/src/accounts.ts +242 -0
  55. package/src/bin.ts +6 -0
  56. package/src/claude.ts +65 -90
  57. package/src/cli.ts +413 -152
  58. package/src/config.ts +92 -14
  59. package/src/daemon.ts +131 -64
  60. package/src/launchd.ts +40 -35
  61. package/src/prompts.ts +255 -0
  62. package/src/service.ts +145 -0
  63. package/src/setup.ts +188 -0
  64. package/src/system.ts +11 -0
  65. package/src/systemd.ts +78 -0
  66. package/src/watchdog.ts +41 -33
package/src/claude.ts CHANGED
@@ -17,10 +17,8 @@ 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";
24
22
 
25
23
  const claudeDir = (env: NodeJS.ProcessEnv) =>
26
24
  env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude");
@@ -86,56 +84,95 @@ export const which = (name: string, path = process.env.PATH ?? "") => {
86
84
  );
87
85
  };
88
86
 
87
+ const shellQuote = (text: string) => `'${text.replaceAll("'", "'\\''")}'`;
88
+
89
89
  /**
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.
90
+ * harnessd's plugin, written before each start: one MCP server per channel of the config, each
91
+ * `harnessd channel <name>`, and a SessionStart hook that tells harnessd which conversation Claude Code is in, so a
92
+ * /clear or a /resume is the conversation the next start resumes.
92
93
  */
93
94
  export const writePlugin = (
94
95
  paths: Harnessd.Paths,
95
96
  config: Harnessd.Config,
96
97
  bin: string,
97
- ): boolean => {
98
+ ) => {
98
99
  rmSync(paths.plugin, { recursive: true, force: true });
99
- const names = Object.keys(config.channels);
100
- if (names.length === 0) return false;
101
100
  mkdirSync(join(paths.plugin, ".claude-plugin"), { recursive: true });
101
+ const names = Object.keys(config.channels);
102
+ const sessionStart = [
103
+ ...Object.entries(paths.env).map(
104
+ ([name, value]) => `${name}=${shellQuote(value)}`,
105
+ ),
106
+ shellQuote(process.execPath),
107
+ shellQuote(bin),
108
+ "session-start",
109
+ ].join(" ");
102
110
  const manifest = {
103
111
  name: "harnessd",
104
112
  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
- {
110
- command: process.execPath,
111
- args: [bin, "channel", name],
112
- env: { HARNESSD_HOME: paths.home },
113
- },
114
- ]),
115
- ),
116
- channels: names.map((name) => ({ server: name, displayName: name })),
113
+ description:
114
+ "harnessd's channels, each one's events and tools, and its SessionStart hook",
115
+ ...(names.length > 0 && {
116
+ mcpServers: Object.fromEntries(
117
+ names.map((name) => [
118
+ name,
119
+ {
120
+ command: process.execPath,
121
+ args: [bin, "channel", name],
122
+ env: paths.env,
123
+ },
124
+ ]),
125
+ ),
126
+ channels: names.map((name) => ({ server: name, displayName: name })),
127
+ }),
128
+ hooks: {
129
+ SessionStart: [{ hooks: [{ type: "command", command: sessionStart }] }],
130
+ },
117
131
  };
118
132
  writeFileSync(
119
133
  join(paths.plugin, ".claude-plugin", "plugin.json"),
120
134
  `${JSON.stringify(manifest, null, 2)}\n`,
121
135
  );
122
- return true;
123
136
  };
124
137
 
138
+ /** Whether these claude arguments pick a permission mode themselves. */
139
+ const picksPermissionMode = (args: readonly string[]) =>
140
+ args.some(
141
+ (arg) =>
142
+ arg === "--dangerously-skip-permissions" ||
143
+ arg === "--permission-mode" ||
144
+ arg.startsWith("--permission-mode="),
145
+ );
146
+
147
+ /** Whether these claude arguments turn permission prompts off: the config's `args` (or prestart's) ask for it, harnessd never does. */
148
+ export const skipsPermissions = (args: readonly string[]) =>
149
+ args.some(
150
+ (arg, i) =>
151
+ arg === "--dangerously-skip-permissions" ||
152
+ arg === "--permission-mode=bypassPermissions" ||
153
+ (arg === "bypassPermissions" && args[i - 1] === "--permission-mode"),
154
+ );
155
+
125
156
  /**
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.
157
+ * The claude command line for one start (after the binary). Claude Code runs in its auto permission mode, which
158
+ * decides each action itself, unless the config's arguments pick another mode. harnessd's plugin is written on this Mac at each start, so
159
+ * its channels load with Claude Code's flag for local channels, which asks for no entry in an approved list. Claude
160
+ * Code then asks once per start whether this is local development; harnessd answers that (see prompts.ts).
128
161
  */
129
162
  export const claudeArgs = (o: {
130
163
  start: Watchdog.Start;
131
- plugin: string | undefined;
132
- allowed: boolean;
164
+ plugin: string;
133
165
  config: Harnessd.Config;
134
166
  extra: readonly string[];
135
167
  }) => [
136
- "--dangerously-skip-permissions",
137
- ...(o.plugin ? ["--plugin-dir", o.plugin] : []),
138
- ...(o.plugin && o.allowed ? ["--channels", `plugin:${PLUGIN}`] : []),
168
+ ...(picksPermissionMode([...o.config.args, ...o.extra])
169
+ ? []
170
+ : ["--permission-mode", "auto"]),
171
+ "--plugin-dir",
172
+ o.plugin,
173
+ ...(Object.keys(o.config.channels).length > 0
174
+ ? ["--dangerously-load-development-channels", `plugin:${PLUGIN}`]
175
+ : []),
139
176
  ...(o.start.resume
140
177
  ? ["--resume", o.start.sessionId]
141
178
  : ["--session-id", o.start.sessionId]),
@@ -210,65 +247,3 @@ export const prestart = (
210
247
  },
211
248
  );
212
249
  });
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
- };