@operad-io/cli 0.1.1 → 0.2.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 CHANGED
@@ -6,10 +6,12 @@
6
6
 
7
7
  ```sh
8
8
  bun add -g @operad-io/cli
9
- operad --help
9
+ operad
10
10
  ```
11
11
 
12
- `npm install -g @operad-io/cli` also works while `bun` is on your `PATH`. Then run `operad init` and `operad serve` or `operad tui`.
12
+ `npm install -g @operad-io/cli` also works while `bun` is on your `PATH`. With
13
+ no command, `operad` creates the home, starts `serve`, and opens the default
14
+ bot's new chat; `operad --help` lists every command.
13
15
 
14
16
  ## Documentation
15
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@operad-io/cli",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "operad: an open orchestration layer for coding agents",
5
5
  "type": "module",
6
6
  "repository": {
@@ -12,17 +12,21 @@
12
12
  "operad": "bin/operad.ts"
13
13
  },
14
14
  "dependencies": {
15
- "@operad-io/engine": "0.1.1",
16
- "@operad-io/mcp": "0.1.1",
17
- "@operad-io/shell": "0.1.1",
18
- "@operad-io/chat": "0.1.1",
19
- "@operad-io/dev": "0.1.1",
20
- "@operad-io/harness-codex": "0.1.1",
21
- "@operad-io/harness-pi": "0.1.1",
22
- "@operad-io/harness-radian": "0.1.1",
23
- "@operad-io/pool": "0.1.1",
24
- "@operad-io/bot": "0.1.1",
25
- "@operad-io/telegram": "0.1.1"
15
+ "@operad-io/engine": "0.2.0",
16
+ "@operad-io/mcp": "0.2.0",
17
+ "@operad-io/shell": "0.2.0",
18
+ "@operad-io/chat": "0.2.0",
19
+ "@operad-io/dev": "0.2.0",
20
+ "@operad-io/harness-codex": "0.2.0",
21
+ "@operad-io/harness-pi": "0.2.0",
22
+ "@operad-io/harness-radian": "0.2.0",
23
+ "@operad-io/pool": "0.2.0",
24
+ "@operad-io/bot": "0.2.0",
25
+ "@operad-io/telegram": "0.2.0",
26
+ "@operad-io/memory": "0.2.0"
27
+ },
28
+ "devDependencies": {
29
+ "@operad-io/test-support": "0.0.0"
26
30
  },
27
31
  "license": "MIT",
28
32
  "files": [
@@ -7,6 +7,7 @@
7
7
  * problems on the error stream, and the next change tries again.
8
8
  */
9
9
 
10
+ import { messageOf } from "@operad-io/engine";
10
11
  import {
11
12
  describeChange,
12
13
  diffFingerprints,
@@ -14,9 +15,12 @@ import {
14
15
  newHashCache,
15
16
  codeRoots,
16
17
  pluginFile,
18
+ secondsOf,
17
19
  waitForCodeChange,
18
20
  } from "@operad-io/engine";
19
21
  import type { CodeFingerprint, CodeRoot } from "@operad-io/engine";
22
+ import { systemClock } from "@operad-io/engine/platform";
23
+ import type { Clock } from "@operad-io/engine/platform";
20
24
  import { basename, extname, join } from "node:path";
21
25
  import { fileURLToPath } from "node:url";
22
26
 
@@ -161,24 +165,77 @@ export function loadsAsWellAs(
161
165
  return problems.every((line) => keys.has(problemKey(line)));
162
166
  }
163
167
 
168
+ /**
169
+ * What a load check found: the problems that did not load, or that the check
170
+ * itself did not finish, in which case the code was not judged at all.
171
+ */
172
+ export interface LoadProbe {
173
+ readonly problems: readonly string[];
174
+ /** False when the check did not finish: its problems say why. */
175
+ readonly finished: boolean;
176
+ }
177
+
178
+ /** How long a load check may run before the old code is kept. */
179
+ export const PROBE_TIMEOUT_MS = 60_000;
180
+
181
+ export interface ProbeLoadOptions {
182
+ /** The command that loads the code; a test gives one that does not end. */
183
+ readonly command?: readonly string[] | undefined;
184
+ readonly timeoutMs?: number | undefined;
185
+ /** The timers of the limit; tests inject theirs. */
186
+ readonly clock?: Clock | undefined;
187
+ }
188
+
164
189
  /**
165
190
  * Starts a fresh process that loads the code on disk and reports what did not
166
191
  * load, one line each. A process that cannot start reports that as the problem.
192
+ * A process that does not end is ended: a check that hangs has no verdict, and
193
+ * the watch that waits for it must not wait for good.
167
194
  */
168
- export async function probeLoad(home: string): Promise<string[]> {
195
+ export async function probeLoad(
196
+ home: string,
197
+ options: ProbeLoadOptions = {},
198
+ ): Promise<LoadProbe> {
199
+ const clock = options.clock ?? systemClock;
200
+ const timeoutMs = options.timeoutMs ?? PROBE_TIMEOUT_MS;
169
201
  const child = Bun.spawn({
170
- cmd: [process.execPath, CLI_ENTRY, "check-load"],
202
+ cmd: options.command === undefined ? [process.execPath, CLI_ENTRY, "check-load"] : [...options.command],
171
203
  cwd: home,
172
204
  env: { ...process.env, OPERAD_HOME: home },
173
205
  stdin: "ignore",
174
206
  stdout: "pipe",
175
207
  stderr: "pipe",
176
208
  });
177
- const [code, out, err] = await Promise.all([
178
- child.exited,
179
- new Response(child.stdout).text(),
180
- new Response(child.stderr).text(),
181
- ]);
209
+ const read = (async (): Promise<{ code: number; out: string; err: string }> => {
210
+ const [code, out, err] = await Promise.all([
211
+ child.exited,
212
+ new Response(child.stdout).text(),
213
+ new Response(child.stderr).text(),
214
+ ]);
215
+ return { code, out, err };
216
+ })();
217
+ let timer: unknown;
218
+ const limit = new Promise<"timeout">((resolve) => {
219
+ timer = clock.setTimeout(() => resolve("timeout"), timeoutMs);
220
+ });
221
+ let outcome: { code: number; out: string; err: string } | "timeout";
222
+ try {
223
+ outcome = await Promise.race([read, limit]);
224
+ } finally {
225
+ clock.clearTimeout(timer);
226
+ }
227
+ if (outcome === "timeout") {
228
+ // This process started the child, so it may end it. A descendant that keeps
229
+ // the pipes open is not waited for either: the check has no verdict.
230
+ child.kill("SIGKILL");
231
+ void read.catch(() => undefined);
232
+ await child.exited.catch(() => undefined);
233
+ return {
234
+ problems: [`the load check did not finish within ${secondsOf(timeoutMs)}`],
235
+ finished: false,
236
+ };
237
+ }
238
+ const { code, out, err } = outcome;
182
239
  if (code !== 0) {
183
240
  const last = err
184
241
  .split("\n")
@@ -186,9 +243,12 @@ export async function probeLoad(home: string): Promise<string[]> {
186
243
  .filter((line) => line.length > 0)
187
244
  .slice(-3)
188
245
  .join(" ");
189
- return [`operad does not start: ${last.length > 0 ? last : `exit ${code}`}`];
246
+ return {
247
+ problems: [`operad does not start: ${last.length > 0 ? last : `exit ${code}`}`],
248
+ finished: true,
249
+ };
190
250
  }
191
- return out.split("\n").filter((line) => line.length > 0);
251
+ return { problems: out.split("\n").filter((line) => line.length > 0), finished: true };
192
252
  }
193
253
 
194
254
  export interface CodeCheckOptions {
@@ -197,6 +257,8 @@ export interface CodeCheckOptions {
197
257
  /** The problems this process had when it loaded. */
198
258
  readonly known: readonly string[];
199
259
  readonly onProblem: (message: string) => void;
260
+ /** How long the fresh process may take; defaults to `PROBE_TIMEOUT_MS`. */
261
+ readonly probeTimeoutMs?: number | undefined;
200
262
  /**
201
263
  * When set, a change that does not load is waited out instead of skipped:
202
264
  * the check does not return until the files load or match what the process
@@ -223,10 +285,16 @@ export function codeChecker(
223
285
  const seen = fingerprintCode(watched, options.snapshot.cache);
224
286
  const change = diffFingerprints(options.snapshot.baseline, current);
225
287
  if (change === undefined) return undefined;
226
- const problems = await probeLoad(options.home);
227
- if (loadsAsWellAs(problems, options.known)) return describeChange(change);
288
+ const probe = await probeLoad(options.home, {
289
+ timeoutMs: options.probeTimeoutMs,
290
+ });
291
+ if (probe.finished && loadsAsWellAs(probe.problems, options.known)) {
292
+ return describeChange(change);
293
+ }
228
294
  options.onProblem(
229
- `code changed (${describeChange(change)}) but does not load; the old code keeps running: ${problems.join("; ")}`,
295
+ probe.finished
296
+ ? `code changed (${describeChange(change)}) but does not load; the old code keeps running: ${probe.problems.join("; ")}`
297
+ : `code changed (${describeChange(change)}) but the load check did not finish; the old code keeps running: ${probe.problems.join("; ")}`,
230
298
  );
231
299
  if (options.waitPollMs === undefined) return undefined;
232
300
  // A settings file that did not read may be fixed, so it wakes the wait
@@ -318,7 +386,7 @@ export async function superviseServe(
318
386
  // The old code is gone too: say so rather than leave the one line
319
387
  // that promised its addresses standing as the last word.
320
388
  log(
321
- `reload: the old code could not listen either (${error instanceof Error ? error.message : String(error)}); this serve ends\n`,
389
+ `reload: the old code could not listen either (${messageOf(error)}); this serve ends\n`,
322
390
  );
323
391
  throw error;
324
392
  } finally {
package/src/defaults.ts CHANGED
@@ -7,19 +7,30 @@ import {
7
7
  type PluginOffer,
8
8
  } from "@operad-io/engine";
9
9
 
10
- /** The plugins `operad init` installs, in load order. */
11
- export const DEFAULT_PLUGINS: readonly string[] = ["shell", "radian", "chat", "pool", "dev"];
12
-
13
10
  /**
14
- * Plugins shipped with operad that `plugin enable <name>` can add. Most are not
15
- * in the default config; `pool` is, and stays here so a home made before init
16
- * installed it can still enable it.
11
+ * Plugins shipped with operad that `plugin enable <name>` can add, including
12
+ * those a new home leaves off (`shell`, `dev`, `pool`) and those a home made
13
+ * before the current defaults may still need (`bot`, `memory`, the harnesses).
17
14
  */
18
- export const OPTIONAL_PLUGINS: readonly string[] = ["pi", "claude", "codex", "pool", "bot", "telegram"];
15
+ export const OPTIONAL_PLUGINS: readonly string[] = ["shell", "pi", "claude", "codex", "pool", "bot", "telegram", "memory", "dev"];
16
+
17
+ /** The shipped plugins a plugin needs; enabling it enables these first (ADR 0012 D2). */
18
+ export const PLUGIN_NEEDS: Readonly<Record<string, readonly string[]>> = {
19
+ dev: ["pool", "shell"],
20
+ };
19
21
 
20
22
  /** The plugins whose package is installed into the home rather than shipped with the CLI. */
21
23
  export const INSTALL_ON_ENABLE: readonly string[] = ["claude"];
22
24
 
25
+ /** The pi library operad installs into the home when no agent is on `PATH` (ADR 0023 D9). */
26
+ export const PI_CODING_AGENT_PACKAGE = "@earendil-works/pi-coding-agent";
27
+
28
+ /** The exact pi version operad installs; it moves with an operad release. */
29
+ export const PI_CODING_AGENT_VERSION = "1.6.0";
30
+
31
+ /** The agent commands `operad init` looks for, in this order. */
32
+ export const INIT_HARNESS_PLUGINS: readonly string[] = ["radian", "claude", "pi"];
33
+
23
34
  /** Harness plugins are named after their agent; their npm packages are `harness-<agent>`. */
24
35
  const HARNESS_PLUGINS: readonly string[] = ["claude", "codex", "pi", "radian"];
25
36
 
@@ -28,24 +39,25 @@ export function defaultPluginModule(name: string): string {
28
39
  return HARNESS_PLUGINS.includes(name) ? `@operad-io/harness-${name}` : `@operad-io/${name}`;
29
40
  }
30
41
 
31
- /** The actions `operad init` lets a lead run: the dev plugin's rework family and the chat plugin's close. */
32
- export const DEFAULT_LEAD_ACTIONS: readonly string[] = [
33
- "dev:rework",
34
- "dev:reverify",
35
- "dev:reject",
36
- "dev:priority",
37
- "dev:concurrency",
38
- "chat:close",
39
- ];
42
+ /** The actions `operad init` lets a lead run: only actions of the plugins a new home enables. */
43
+ export const DEFAULT_LEAD_ACTIONS: readonly string[] = ["chat:close"];
40
44
 
41
45
  /** The allowed actions that also need the confirmation. */
42
46
  export const DEFAULT_CONFIRM_ACTIONS: readonly string[] = ["chat:close"];
43
47
 
44
- /** The config a new home starts with. */
45
- export function distributionConfig(home: string): OperadConfig {
48
+ /** The plugins of a new home: the assistant's fixed ones plus its harnesses. */
49
+ export function defaultPluginsFor(harnesses: readonly string[]): readonly string[] {
50
+ return ["chat", "bot", "memory", ...harnesses];
51
+ }
52
+
53
+ /** The config a new home starts with, with the harness plugins passed in place of the fallback. */
54
+ export function distributionConfig(
55
+ home: string,
56
+ harnesses: readonly string[] = ["radian"],
57
+ ): OperadConfig {
46
58
  return {
47
59
  ...defaultConfig(home),
48
- plugins: DEFAULT_PLUGINS.map((name) => ({
60
+ plugins: defaultPluginsFor(harnesses).map((name) => ({
49
61
  name,
50
62
  module: defaultPluginModule(name),
51
63
  enabled: true,
package/src/doctor.ts ADDED
@@ -0,0 +1,204 @@
1
+ /**
2
+ * `operad doctor`: one read-only pass over a home. It asks an environment for
3
+ * each fact and turns the answers into one line per fact; every problem line
4
+ * ends with the next command to type. The environment is a parameter so a test
5
+ * supplies the facts and nothing touches the machine or the network.
6
+ */
7
+
8
+ /** One plugin the home enables. */
9
+ export interface DoctorPlugin {
10
+ readonly name: string;
11
+ readonly loaded: boolean;
12
+ /** Why it did not load. */
13
+ readonly problem?: string | undefined;
14
+ }
15
+
16
+ /** One enabled agent (harness plugin). */
17
+ export interface DoctorAgent {
18
+ readonly name: string;
19
+ readonly version?: string | undefined;
20
+ /** `undefined` when the agent cannot say. */
21
+ readonly signedIn?: boolean | undefined;
22
+ /** The bot whose agent this is, when operad's own login covers the agent. */
23
+ readonly loginBot?: string | undefined;
24
+ /** Why the agent cannot be used, when it cannot. */
25
+ readonly problem?: string | undefined;
26
+ }
27
+
28
+ /** What `operad pending` reports as a setup problem. */
29
+ export interface DoctorSetupItem {
30
+ readonly id: string;
31
+ readonly prompt: string;
32
+ }
33
+
34
+ export interface DoctorEnv {
35
+ /** Whether the home directory exists. */
36
+ homeExists(): boolean;
37
+ /** Whether `config.json` exists; throws with the reason when it is invalid. */
38
+ config(): Promise<{ readonly present: boolean }>;
39
+ plugins(): Promise<readonly DoctorPlugin[]>;
40
+ agents(): Promise<readonly DoctorAgent[]>;
41
+ serve(): Promise<{ readonly answering: boolean }>;
42
+ /** The test host when one is set; `undefined` when none is. */
43
+ testHost(): Promise<{ readonly host: string; readonly reachable: boolean } | undefined>;
44
+ setup(): Promise<readonly DoctorSetupItem[]>;
45
+ }
46
+
47
+ export interface DoctorCheck {
48
+ readonly id: string;
49
+ readonly ok: boolean;
50
+ readonly message: string;
51
+ /** The command to type next; present on every problem. */
52
+ readonly next?: string | undefined;
53
+ }
54
+
55
+ export interface DoctorReport {
56
+ readonly ok: boolean;
57
+ readonly checks: readonly DoctorCheck[];
58
+ }
59
+
60
+ /**
61
+ * The agent's own command that signs in, for an agent operad's login does not
62
+ * cover. Each was checked against that agent's `--help`.
63
+ */
64
+ const SIGN_IN: Readonly<Record<string, string>> = {
65
+ claude: "claude auth login",
66
+ codex: "codex login",
67
+ pi: "pi auth login <provider>",
68
+ radian: "radian login",
69
+ };
70
+
71
+ function good(id: string, message: string): DoctorCheck {
72
+ return { id, ok: true, message };
73
+ }
74
+
75
+ function bad(id: string, message: string, next: string): DoctorCheck {
76
+ return { id, ok: false, message, next };
77
+ }
78
+
79
+ function reasonOf(error: unknown): string {
80
+ return error instanceof Error ? error.message : String(error);
81
+ }
82
+
83
+ /** Runs every check once. It never throws: a failed ask is a problem line. */
84
+ export async function runDoctor(env: DoctorEnv): Promise<DoctorReport> {
85
+ const checks: DoctorCheck[] = [];
86
+
87
+ let loadable = false;
88
+ if (!env.homeExists()) {
89
+ checks.push(bad("home", "the operad home does not exist", "operad init"));
90
+ } else {
91
+ try {
92
+ const config = await env.config();
93
+ if (config.present) {
94
+ checks.push(good("home", "home and config.json are valid"));
95
+ loadable = true;
96
+ } else {
97
+ checks.push(bad("home", "the home has no config.json", "operad init"));
98
+ }
99
+ } catch (error) {
100
+ checks.push(
101
+ bad(
102
+ "home",
103
+ `config.json is not valid: ${reasonOf(error)}`,
104
+ "operad doctor",
105
+ ),
106
+ );
107
+ }
108
+ }
109
+
110
+ if (loadable) {
111
+ try {
112
+ for (const plugin of await env.plugins()) {
113
+ checks.push(
114
+ plugin.loaded
115
+ ? good(`plugin:${plugin.name}`, `plugin ${plugin.name} is loaded`)
116
+ : bad(
117
+ `plugin:${plugin.name}`,
118
+ `plugin ${plugin.name} did not load: ${plugin.problem ?? "no reason given"}`,
119
+ `operad plugin disable ${plugin.name}`,
120
+ ),
121
+ );
122
+ }
123
+ const agents = await env.agents();
124
+ for (const agent of agents) {
125
+ const id = `agent:${agent.name}`;
126
+ const version = agent.version === undefined ? "" : ` ${agent.version}`;
127
+ if (agent.problem !== undefined) {
128
+ checks.push(
129
+ bad(id, `agent ${agent.name} cannot be used: ${agent.problem}`, "operad init"),
130
+ );
131
+ } else if (agent.signedIn === false) {
132
+ checks.push(
133
+ bad(
134
+ id,
135
+ `agent ${agent.name}${version} is not signed in`,
136
+ agent.loginBot === undefined
137
+ ? (SIGN_IN[agent.name] ?? `sign in to ${agent.name}, then operad doctor`)
138
+ : `operad bot:login ${agent.loginBot}`,
139
+ ),
140
+ );
141
+ } else {
142
+ checks.push(
143
+ good(
144
+ id,
145
+ `agent ${agent.name}${version}${agent.signedIn === true ? ", signed in" : ""}`,
146
+ ),
147
+ );
148
+ }
149
+ }
150
+ if (agents.length === 0) {
151
+ checks.push(bad("agents", "no agent is enabled", "operad plugin enable pi"));
152
+ }
153
+ } catch (error) {
154
+ checks.push(bad("plugins", `plugins could not be read: ${reasonOf(error)}`, "operad doctor"));
155
+ }
156
+ }
157
+
158
+ try {
159
+ const serve = await env.serve();
160
+ checks.push(
161
+ serve.answering
162
+ ? good("serve", "serve is answering")
163
+ : bad("serve", "serve is not answering", "operad serve"),
164
+ );
165
+ } catch (error) {
166
+ checks.push(bad("serve", `serve could not be checked: ${reasonOf(error)}`, "operad serve"));
167
+ }
168
+
169
+ if (loadable) {
170
+ try {
171
+ const host = await env.testHost();
172
+ if (host !== undefined) {
173
+ checks.push(
174
+ host.reachable
175
+ ? good("test-host", `test host ${host.host} is reachable`)
176
+ : bad("test-host", `test host ${host.host} is not reachable`, `ssh ${host.host}`),
177
+ );
178
+ }
179
+ for (const item of await env.setup()) {
180
+ // A standing item that already names a command: that is the next step.
181
+ const named = /`([^`]+)`/.exec(item.prompt)?.[1];
182
+ checks.push(
183
+ named === undefined
184
+ ? bad(`setup:${item.id}`, item.prompt, "operad pending")
185
+ : { id: `setup:${item.id}`, ok: false, message: item.prompt, next: named },
186
+ );
187
+ }
188
+ } catch (error) {
189
+ checks.push(bad("setup", `setup could not be read: ${reasonOf(error)}`, "operad pending"));
190
+ }
191
+ }
192
+
193
+ return { ok: checks.every((check) => check.ok), checks };
194
+ }
195
+
196
+ /** The lines `operad doctor` prints: `ok` or `problem`, then the fact and, for a problem, what to type. */
197
+ export function formatDoctor(report: DoctorReport): string {
198
+ const lines = report.checks.map((check) => {
199
+ if (check.ok) return `ok ${check.message}`;
200
+ const next = check.next === undefined ? "" : ` -> ${check.next}`;
201
+ return `problem ${check.message}${next}`;
202
+ });
203
+ return `${lines.join("\n")}\n`;
204
+ }
package/src/http-bind.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  * bounded time before the new code gives up.
6
6
  */
7
7
 
8
+ import { messageOf } from "@operad-io/engine";
8
9
  import { ConfigError, HttpStopError } from "@operad-io/engine";
9
10
  import { RELOAD_EXIT, takeSupervisedFlag } from "./code-reload.ts";
10
11
 
@@ -22,7 +23,7 @@ export interface BindRetryOptions {
22
23
  /** The new code could not listen in time; the old code has to serve. */
23
24
  export class BindFailed extends Error {
24
25
  constructor(cause: unknown) {
25
- super(cause instanceof Error ? cause.message : String(cause));
26
+ super(messageOf(cause));
26
27
  this.name = "BindFailed";
27
28
  }
28
29
  }