@operad-io/dev 0.1.1 → 0.3.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 (50) hide show
  1. package/README.md +1 -1
  2. package/dev.schema.json +53 -31
  3. package/package.json +9 -6
  4. package/skills/git/SKILL.md +70 -0
  5. package/skills/setup/SKILL.md +74 -0
  6. package/src/access-grants.ts +59 -0
  7. package/src/actions.ts +81 -17
  8. package/src/announce.ts +134 -17
  9. package/src/chat.ts +586 -36
  10. package/src/check-host.ts +111 -0
  11. package/src/check-load.ts +67 -0
  12. package/src/check.ts +45 -22
  13. package/src/clock.ts +29 -0
  14. package/src/clone-chat.ts +32 -0
  15. package/src/commands.ts +103 -34
  16. package/src/create-actions.ts +210 -0
  17. package/src/flaky.ts +45 -29
  18. package/src/flow-compiler.ts +156 -69
  19. package/src/followups.ts +20 -22
  20. package/src/gate.ts +254 -88
  21. package/src/git-state.ts +155 -0
  22. package/src/git.ts +92 -4
  23. package/src/index.ts +52 -6
  24. package/src/integrate.ts +162 -62
  25. package/src/integration-queue.ts +23 -10
  26. package/src/lock.ts +101 -5
  27. package/src/mainclone.ts +61 -5
  28. package/src/model-error.ts +70 -0
  29. package/src/model.ts +61 -35
  30. package/src/notcode.ts +51 -15
  31. package/src/places.ts +2 -2
  32. package/src/prepare.ts +7 -2
  33. package/src/project-flows.ts +135 -4
  34. package/src/project-settings.ts +110 -3
  35. package/src/register-requests.ts +121 -0
  36. package/src/repository-key.ts +4 -0
  37. package/src/rerun.ts +23 -7
  38. package/src/retry.ts +2 -12
  39. package/src/rework.ts +8 -0
  40. package/src/schedule.ts +342 -93
  41. package/src/service.ts +241 -185
  42. package/src/settings.ts +409 -65
  43. package/src/setup-git.ts +366 -0
  44. package/src/standing.ts +23 -6
  45. package/src/step-edge.ts +22 -0
  46. package/src/steps.ts +945 -179
  47. package/src/throughput.ts +4 -21
  48. package/src/timeouts.ts +10 -2
  49. package/src/views.ts +325 -86
  50. package/src/waits.ts +59 -0
@@ -0,0 +1,111 @@
1
+ /**
2
+ * The test host and its slots: the home's `dev.testHost` and `dev.testSlots`
3
+ * become the environment of every gate, integration and rerun command. A
4
+ * repository whose commands use `remote-check.sh` is told when neither
5
+ * the settings nor serve's own environment names a host.
6
+ */
7
+
8
+ import type { CheckCommand, DevSettings } from "./settings.ts";
9
+
10
+ /** The variable `remote-check.sh` reads to find its host. */
11
+ export const CHECK_HOST_VARIABLE = "OPERAD_CHECK_HOST";
12
+
13
+ /** The variable `remote-check.sh` reads for how many checks run at once. */
14
+ export const CHECK_SLOTS_VARIABLE = "OPERAD_CHECK_SLOTS";
15
+
16
+ /** The script a repository's check commands may use to run on another machine. */
17
+ export const REMOTE_CHECK_SCRIPT = "remote-check.sh";
18
+
19
+ /** An environment to read the explicit variables from; a test passes its own. */
20
+ type Environment = Readonly<Record<string, string | undefined>>;
21
+
22
+ /** The host a setting names, or undefined when the setting is absent. */
23
+ function hostSetting(settings: Readonly<Record<string, unknown>>): string | undefined {
24
+ const value = settings.testHost;
25
+ if (value === undefined) return undefined;
26
+ if (typeof value !== "string" || value.trim().length === 0) {
27
+ throw new Error("dev settings: testHost must be a non-empty string");
28
+ }
29
+ return value;
30
+ }
31
+
32
+ /** How many checks the setting allows on the host, or undefined. */
33
+ function slotsSetting(settings: Readonly<Record<string, unknown>>): number | undefined {
34
+ const value = settings.testSlots;
35
+ if (value === undefined) return undefined;
36
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 1) {
37
+ throw new Error("dev settings: testSlots must be a whole number of 1 or more");
38
+ }
39
+ return value;
40
+ }
41
+
42
+ /**
43
+ * The host the commands should use: the explicit variable in serve's own
44
+ * environment first, then the home's setting. Empty means the check runs here,
45
+ * which is also what an absent host means.
46
+ */
47
+ export function checkHostOf(
48
+ settings: Readonly<Record<string, unknown>>,
49
+ env: Environment = process.env,
50
+ ): string | undefined {
51
+ const explicit = env[CHECK_HOST_VARIABLE];
52
+ if (explicit !== undefined) return explicit.length === 0 ? undefined : explicit;
53
+ return hostSetting(settings);
54
+ }
55
+
56
+ /**
57
+ * The environment a gate, integration or rerun command gets: the host and
58
+ * slots from the home's settings. A variable already in serve's own
59
+ * environment wins, including an empty one, and is left to be inherited.
60
+ */
61
+ export function checkEnvironment(
62
+ settings: Readonly<Record<string, unknown>>,
63
+ env: Environment = process.env,
64
+ ): Readonly<Record<string, string>> {
65
+ const out: Record<string, string> = {};
66
+ if (env[CHECK_HOST_VARIABLE] === undefined) {
67
+ const host = hostSetting(settings);
68
+ if (host !== undefined) out[CHECK_HOST_VARIABLE] = host;
69
+ }
70
+ if (env[CHECK_SLOTS_VARIABLE] === undefined) {
71
+ const slots = slotsSetting(settings);
72
+ if (slots !== undefined) out[CHECK_SLOTS_VARIABLE] = String(slots);
73
+ }
74
+ return out;
75
+ }
76
+
77
+ /** Whether one check command, or the rerun of its failed tests, uses the script. */
78
+ function commandUsesRemoteCheck(command: CheckCommand): boolean {
79
+ const run = typeof command === "string" ? command : command.run;
80
+ if (run.includes(REMOTE_CHECK_SCRIPT)) return true;
81
+ return typeof command !== "string" && (command.failed?.rerun.includes(REMOTE_CHECK_SCRIPT) ?? false);
82
+ }
83
+
84
+ /** Whether any check command a repository runs uses `remote-check.sh`. */
85
+ export function usesRemoteCheck(settings: DevSettings): boolean {
86
+ if (settings.gate.commands.some(commandUsesRemoteCheck)) return true;
87
+ if (settings.integration.keyCommands.some(commandUsesRemoteCheck)) return true;
88
+ for (const flow of Object.values(settings.flows)) {
89
+ for (const stage of flow.stages) {
90
+ if (stage.stage !== "test" && stage.stage !== "check") continue;
91
+ if (stage.commands?.some(commandUsesRemoteCheck) === true) return true;
92
+ }
93
+ }
94
+ return false;
95
+ }
96
+
97
+ /**
98
+ * The one line dev says, and marks where a person looks, when a repository's
99
+ * check commands use `remote-check.sh` but no host is set. Undefined
100
+ * when a host is set or the commands do not use the script.
101
+ */
102
+ export function checkHostWarning(
103
+ repository: string,
104
+ repositorySettings: DevSettings,
105
+ homeSettings: Readonly<Record<string, unknown>>,
106
+ env: Environment = process.env,
107
+ ): string | undefined {
108
+ if (checkHostOf(homeSettings, env) !== undefined) return undefined;
109
+ if (!usesRemoteCheck(repositorySettings)) return undefined;
110
+ return `check commands use ${REMOTE_CHECK_SCRIPT} but no test host is set, so they run here (repository "${repository}"; set dev.testHost or ${CHECK_HOST_VARIABLE})`;
111
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * The load readings `remote-check.sh` writes into a check log. A check that
3
+ * runs here uses the reader in `notcode.ts`; a check on the test host carries
4
+ * its own readings back, so dev judges the machine the command ran on.
5
+ */
6
+
7
+ import type { MachineLoad } from "@operad-io/engine/platform";
8
+
9
+ /** A load reading, with the host it came from when the check ran elsewhere. */
10
+ export interface CheckLoadReading extends MachineLoad {
11
+ readonly host?: string | undefined;
12
+ /** Set when the host could not read its own load; it is judged as quiet. */
13
+ readonly unknown?: boolean | undefined;
14
+ }
15
+
16
+ /** The load when one run of a command started and when it ended. */
17
+ export interface CheckLoad {
18
+ readonly start: CheckLoadReading;
19
+ readonly end: CheckLoadReading;
20
+ }
21
+
22
+ const LOAD_LINE = /^remote-check: load (?:(?<load1>\d+(?:\.\d+)?) on (?<cores>\d+) cores|unknown) on (?<host>.+?)\s*$/;
23
+
24
+ /**
25
+ * The readings in one run's part of a check log; undefined when it ran here.
26
+ * Each line is its own reading: an unknown end line does not discard a known
27
+ * start, and an unknown start does not discard a known end.
28
+ */
29
+ export function loadsFromLog(text: string): CheckLoad | undefined {
30
+ const readings: CheckLoadReading[] = [];
31
+ for (const line of text.replace(/\r\n/g, "\n").split("\n")) {
32
+ const found = LOAD_LINE.exec(line);
33
+ if (found === null) continue;
34
+ const load1 = found.groups?.load1;
35
+ const cores = found.groups?.cores;
36
+ const host = found.groups?.host?.trim();
37
+ if (load1 !== undefined && cores !== undefined) {
38
+ readings.push({
39
+ load1: Number(load1),
40
+ cores: Number(cores),
41
+ ...(host === undefined || host === "" ? {} : { host }),
42
+ });
43
+ } else {
44
+ readings.push({
45
+ load1: 0,
46
+ cores: 0,
47
+ ...(host === undefined || host === "" ? {} : { host }),
48
+ unknown: true,
49
+ });
50
+ }
51
+ }
52
+ const first = readings[0];
53
+ const last = readings[readings.length - 1];
54
+ return first === undefined || last === undefined ? undefined : { start: first, end: last };
55
+ }
56
+
57
+ /**
58
+ * A load reading as a history line says it: `under load 23.8 on 16 cores on
59
+ * host`, or `under load unknown on host`. `shown` keeps a line's own rounding.
60
+ */
61
+ export function underLoad(load: CheckLoadReading, shown = String(load.load1)): string {
62
+ if (load.unknown === true) {
63
+ return load.host === undefined ? "under load unknown" : `under load unknown on ${load.host}`;
64
+ }
65
+ const host = load.host === undefined ? "" : ` on ${load.host}`;
66
+ return `under load ${shown} on ${load.cores} cores${host}`;
67
+ }
package/src/check.ts CHANGED
@@ -1,21 +1,20 @@
1
1
  /**
2
- * The dev plugin's `init` check: every registered repository has a remote that
3
- * answers and publishes its main branch. The check only reads: it asks the
2
+ * The dev plugin's `init` check: every registered repository that has a remote
3
+ * has one that answers and publishes its main branch; one without a remote has
4
+ * its main branch locally. The check only reads: it asks the
4
5
  * remote for its heads through the remote's name, so git's URL rewriting and
5
6
  * credentials apply as they would for a real command, and writes nothing.
6
7
  */
7
8
 
8
- import { messageOf, type PluginBaseContext } from "@operad-io/engine";
9
- import { git as runGit, type GitResult } from "./git.ts";
9
+ import { messageOf, type MissingItem, type PluginBaseContext } from "@operad-io/engine";
10
+
11
+ /** The check's answer when something is missing (ADR 0010 D7). */
12
+ type SetupMissing = { readonly note: string; readonly missing: readonly MissingItem[] };
13
+ import { git } from "./git.ts";
14
+ import { gitStateOf } from "./git-state.ts";
10
15
  import { listRepositories } from "./service.ts";
11
16
  import { readDevSettings } from "./settings.ts";
12
17
 
13
- function git(cwd: string, args: readonly string[]): GitResult {
14
- // The init check must never wait for a credential prompt; the shared runner
15
- // retries an empty success under load.
16
- return runGit(cwd, args, { env: { ...process.env, GIT_TERMINAL_PROMPT: "0" } });
17
- }
18
-
19
18
  /** Git's own explanation, every line of it, or a plain note when it said nothing. */
20
19
  function detail(text: string): string {
21
20
  return text.length === 0 ? "no detail" : text;
@@ -29,47 +28,71 @@ function listsBranch(output: string, branch: string): boolean {
29
28
  .some((line) => line.split("\t")[1]?.trim() === ref);
30
29
  }
31
30
 
32
- /** Checks one repository and returns the remote it used. */
31
+ /** Checks one repository and returns the remote it used, or undefined when it has none. */
33
32
  async function checkRepository(
34
33
  repository: { readonly name: string; readonly path: string },
35
- ): Promise<string> {
34
+ ): Promise<string | undefined> {
36
35
  const settings = await readDevSettings(repository.path);
36
+ const remoteName = settings.remote;
37
+ if (remoteName === undefined) {
38
+ // No remote: the local main branch is the base, and it must exist.
39
+ const local = git(repository.path, [
40
+ "show-ref",
41
+ "--verify",
42
+ "--quiet",
43
+ `refs/heads/${settings.mainBranch}`,
44
+ ]);
45
+ if (!local.ok) throw new Error(`no branch "${settings.mainBranch}"`);
46
+ return undefined;
47
+ }
48
+ // The init check must never wait for a credential prompt; the shared runner
49
+ // retries an empty success under load.
50
+ const env = { ...process.env, GIT_TERMINAL_PROMPT: "0" };
37
51
  const remote = git(repository.path, [
38
52
  "remote",
39
53
  "get-url",
40
- settings.remote,
41
- ]);
54
+ remoteName,
55
+ ], { env });
42
56
  if (!remote.ok || remote.out.length === 0) {
43
- throw new Error(`remote "${settings.remote}" does not exist`);
57
+ throw new Error(`remote "${remoteName}" does not exist`);
44
58
  }
45
59
 
46
60
  const listed = git(repository.path, [
47
61
  "ls-remote",
48
62
  "--heads",
49
- settings.remote,
63
+ remoteName,
50
64
  settings.mainBranch,
51
- ]);
65
+ ], { env });
52
66
  if (!listed.ok) {
53
67
  throw new Error(
54
- `cannot reach remote "${settings.remote}": ${detail(listed.err)}`,
68
+ `cannot reach remote "${remoteName}": ${detail(listed.err)}`,
55
69
  );
56
70
  }
57
71
  if (!listsBranch(listed.out, settings.mainBranch)) {
58
72
  throw new Error(
59
- `remote "${settings.remote}" has no branch "${settings.mainBranch}"`,
73
+ `remote "${remoteName}" has no branch "${settings.mainBranch}"`,
60
74
  );
61
75
  }
62
- return settings.remote;
76
+ return remoteName;
63
77
  }
64
78
 
65
79
  /** Runs the repository loop and reports one line, or throws with every failure. */
66
- export async function checkDev(ctx: PluginBaseContext): Promise<string> {
80
+ export async function checkDev(ctx: PluginBaseContext): Promise<string | SetupMissing> {
67
81
  const repositories = await listRepositories(ctx.store);
68
- if (repositories.length === 0) return "no repositories registered";
82
+ if (repositories.length === 0) {
83
+ return {
84
+ note: "no repositories registered",
85
+ missing: [{ name: "repository", description: "no repository is registered with dev" }],
86
+ };
87
+ }
69
88
 
70
89
  const failures: string[] = [];
71
90
  for (const repository of repositories) {
91
+ // A folder without git, or with no commit yet, is not a failure: git is
92
+ // needed only to create a task, and the setup there makes the folder ready.
93
+ // A folder git cannot judge is a failure with git's own message.
72
94
  try {
95
+ if (gitStateOf(repository.path).state !== "ready") continue;
73
96
  await checkRepository(repository);
74
97
  } catch (error) {
75
98
  failures.push(`repository "${repository.name}": ${messageOf(error)}`);
package/src/clock.ts ADDED
@@ -0,0 +1,29 @@
1
+ /** The clock and other test seams dev's steps and waits share, so one test can move all of them. */
2
+
3
+ import type { ReviewRunReason, ReviewRunRecord, ReviewRunRole } from "./model.ts";
4
+
5
+ /** Test-only hooks: a child process crashes at a named point in a review. */
6
+ export const reviewTestHooks: {
7
+ now?: (() => number) | undefined;
8
+ /** Waits instead of the real timer, so a test can move the clock. */
9
+ wait?:
10
+ | ((ms: number, signal: AbortSignal | undefined) => Promise<boolean>)
11
+ | undefined;
12
+ beforeRun?:
13
+ | ((run: { readonly role: ReviewRunRole; readonly reason: ReviewRunReason }) => void)
14
+ | undefined;
15
+ /** Runs after the reviewer answered and before its run is written. */
16
+ afterCall?:
17
+ | ((run: { readonly role: ReviewRunRole; readonly reason: ReviewRunReason }) => void)
18
+ | undefined;
19
+ afterRunSaved?: ((run: ReviewRunRecord) => void) | undefined;
20
+ /** Runs after the reviewer wrote a valid report, before the store copy. */
21
+ beforeCopy?: (() => void) | undefined;
22
+ /** Runs while a workspace report is open, before the folders are checked again. */
23
+ afterOpen?: (() => void) | undefined;
24
+ } = {};
25
+
26
+ /** The time now in milliseconds; a test may replace it. */
27
+ export function now(): number {
28
+ return reviewTestHooks.now?.() ?? Date.now();
29
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The chat half of the main clone's exclusion between a task and a repository
3
+ * chat (ADR 0028 D3, ADR 0039 D3): a task does not start while a chat reply
4
+ * that can write in the clone is being written.
5
+ */
6
+
7
+ import type { ObjectStore, RunControl } from "@operad-io/engine";
8
+ import { CHAT_TYPE, chatData, readChat, writingReplyNow } from "@operad-io/chat";
9
+ import { listAll } from "./model.ts";
10
+
11
+ /**
12
+ * The id of a chat about this repository whose reply is being written now and
13
+ * can write in the main clone: its access level is `edits` or `full` and it has
14
+ * no worktree of its own. Undefined when there is none.
15
+ */
16
+ export async function writingChat(
17
+ store: ObjectStore,
18
+ repositoryName: string,
19
+ runs: Pick<RunControl, "active"> | undefined,
20
+ ): Promise<string | undefined> {
21
+ for (const object of await listAll(store, CHAT_TYPE)) {
22
+ const chat = chatData(object.data);
23
+ if (chat.parent !== `repo:${repositoryName}` || chat.closed) continue;
24
+ if (typeof object.data.worktree === "object" && object.data.worktree !== null) continue;
25
+ if (chat.accessLevel !== "edits" && chat.accessLevel !== "full") continue;
26
+ const read = readChat(store, object.id);
27
+ if ((await writingReplyNow(read.facts, read.messages, read.entries, runs)) !== undefined) {
28
+ return object.id;
29
+ }
30
+ }
31
+ return undefined;
32
+ }
package/src/commands.ts CHANGED
@@ -3,9 +3,18 @@
3
3
  import type {
4
4
  CommandContext,
5
5
  CommandDefinition,
6
+ ObjectStore,
6
7
  StoredObject,
7
8
  } from "@operad-io/engine";
8
- import { DevError, bareId, briefBody, storeKey, taskData, type RepositoryData } from "./model.ts";
9
+ import {
10
+ DevError,
11
+ idOfKey,
12
+ briefBody,
13
+ slugFromTitle,
14
+ storeKey,
15
+ taskData,
16
+ type RepositoryData,
17
+ } from "./model.ts";
9
18
  import { chatWorktreeOf } from "./chat.ts";
10
19
  import { flakyTasksByName } from "./flaky.ts";
11
20
  import { listRerunCounts } from "./reruns.ts";
@@ -189,6 +198,36 @@ async function assertFlow(repository: RepositoryData, flow: string): Promise<voi
189
198
  }
190
199
  }
191
200
 
201
+ export interface CreatePlanOptions {
202
+ readonly repository: RepositoryData;
203
+ /** Unset takes the slug from the title. */
204
+ readonly slug?: string | undefined;
205
+ readonly title: string;
206
+ readonly request?: string | undefined;
207
+ /** As the command's `--concurrency` value, before it is checked. */
208
+ readonly concurrency?: string | undefined;
209
+ /** As the command's `--flow` value, before it is checked. */
210
+ readonly flow?: string | undefined;
211
+ }
212
+
213
+ /** Creates a plan by the command's path; an action and `plan add` both use it. */
214
+ export async function createPlan(
215
+ store: ObjectStore,
216
+ options: CreatePlanOptions,
217
+ ): Promise<StoredObject> {
218
+ const flow = flowFlag(options.flow);
219
+ if (flow !== undefined) await assertFlow(options.repository, flow);
220
+ return addPlan(store, {
221
+ repository: options.repository,
222
+ slug: options.slug ?? slugFromTitle(options.title, "plan"),
223
+ title: options.title,
224
+ request: options.request ?? "",
225
+ concurrency: concurrencyFlag(options.concurrency),
226
+ auto: (await readDevSettings(options.repository.path)).autoDefault,
227
+ ...(flow === undefined ? {} : { flow }),
228
+ });
229
+ }
230
+
192
231
  async function runPlan(
193
232
  ctx: CommandContext,
194
233
  argv: readonly string[],
@@ -199,18 +238,15 @@ async function runPlan(
199
238
  throw new DevError("command", `plan ${call.name} takes flags, not ${call.rest[0]}`);
200
239
  }
201
240
  const repository = await objectByPlan(parsed, ctx.store, ctx.cwd);
202
- const flow = flowFlag(one(parsed, "--flow"));
203
- if (flow !== undefined) await assertFlow(repository, flow);
204
- const plan = await addPlan(ctx.store, {
241
+ const plan = await createPlan(ctx.store, {
205
242
  repository,
206
243
  slug: required(parsed, "--slug", "plan add"),
207
244
  title: required(parsed, "--title", "plan add"),
208
245
  request: await readStdinText(ctx.input),
209
- concurrency: concurrencyFlag(one(parsed, "--concurrency")),
210
- auto: (await readDevSettings(repository.path)).autoDefault,
211
- ...(flow === undefined ? {} : { flow }),
246
+ concurrency: one(parsed, "--concurrency"),
247
+ flow: one(parsed, "--flow"),
212
248
  });
213
- return bareId(plan.id);
249
+ return idOfKey(plan.id);
214
250
  }
215
251
 
216
252
  function editable(task: StoredObject): void {
@@ -264,6 +300,58 @@ async function newBrief(ctx: CommandContext, body: string): Promise<string> {
264
300
  return editText(body, { root: ctx.store.root });
265
301
  }
266
302
 
303
+ export interface CreateTaskOptions {
304
+ readonly repository: RepositoryData;
305
+ /** The plan's bare id, without its repository. */
306
+ readonly plan: string;
307
+ /** Unset takes the slug from the title. */
308
+ readonly slug?: string | undefined;
309
+ readonly title: string;
310
+ readonly brief?: string | undefined;
311
+ readonly after?: readonly string[] | undefined;
312
+ readonly area?: string | undefined;
313
+ /** As the command's `--flow` value, before it is checked. */
314
+ readonly flow?: string | undefined;
315
+ readonly chat?: string | undefined;
316
+ }
317
+
318
+ /** Creates a task by the command's path; an action and `task add` both use it. */
319
+ export async function createTask(
320
+ store: ObjectStore,
321
+ options: CreateTaskOptions,
322
+ ): Promise<StoredObject> {
323
+ const planKey = storeKey(options.repository.name, options.plan);
324
+ const planObject = await store.get("plan", planKey);
325
+ if (planObject === undefined) {
326
+ throw new DevError("plan", `no plan "${options.plan}"`);
327
+ }
328
+ if (
329
+ (planObject.data as { repository?: unknown }).repository !==
330
+ options.repository.name
331
+ ) {
332
+ throw new DevError(
333
+ "plan",
334
+ `plan "${options.plan}" is not in repository "${options.repository.name}"`,
335
+ );
336
+ }
337
+ const flow = flowFlag(options.flow);
338
+ if (flow !== undefined) await assertFlow(options.repository, flow);
339
+ const worktree =
340
+ options.chat === undefined
341
+ ? undefined
342
+ : await chatWorktreeOf(store, options.repository, options.chat);
343
+ return addTask(store, {
344
+ plan: planKey,
345
+ slug: options.slug ?? slugFromTitle(options.title, "task"),
346
+ title: options.title,
347
+ brief: options.brief ?? "",
348
+ after: options.after ?? [],
349
+ area: options.area,
350
+ ...(flow === undefined ? {} : { flow }),
351
+ ...(worktree === undefined ? {} : { worktree }),
352
+ });
353
+ }
354
+
267
355
  async function runTask(
268
356
  ctx: CommandContext,
269
357
  argv: readonly string[],
@@ -284,37 +372,18 @@ async function runTask(
284
372
  throw new DevError("command", `task add takes flags, not ${call.rest[0]}`);
285
373
  }
286
374
  const repository = await objectByPlan(parsed, ctx.store, ctx.cwd);
287
- const plan = required(parsed, "--plan", "task add");
288
- const planKey = storeKey(repository.name, plan);
289
- const planObject = await ctx.store.get("plan", planKey);
290
- if (planObject === undefined) {
291
- throw new DevError("plan", `no plan "${plan}"`);
292
- }
293
- if (
294
- (planObject.data as { repository?: unknown }).repository !==
295
- repository.name
296
- ) {
297
- throw new DevError(
298
- "plan",
299
- `plan "${plan}" is not in repository "${repository.name}"`,
300
- );
301
- }
302
- const flow = flowFlag(one(parsed, "--flow"));
303
- if (flow !== undefined) await assertFlow(repository, flow);
304
- const chat = one(parsed, "--chat");
305
- const worktree =
306
- chat === undefined ? undefined : await chatWorktreeOf(ctx.store, repository, chat);
307
- const task = await addTask(ctx.store, {
308
- plan: planKey,
375
+ const task = await createTask(ctx.store, {
376
+ repository,
377
+ plan: required(parsed, "--plan", "task add"),
309
378
  slug: required(parsed, "--slug", "task add"),
310
379
  title: required(parsed, "--title", "task add"),
311
380
  brief: await readStdinText(ctx.input),
312
381
  after: many(parsed, "--after"),
313
382
  area: one(parsed, "--area"),
314
- ...(flow === undefined ? {} : { flow }),
315
- ...(worktree === undefined ? {} : { worktree }),
383
+ flow: one(parsed, "--flow"),
384
+ chat: one(parsed, "--chat"),
316
385
  });
317
- return bareId(task.id);
386
+ return idOfKey(task.id);
318
387
  }
319
388
 
320
389
  const id = call.rest[0];
@@ -343,7 +412,7 @@ async function runTask(
343
412
  const area = one(parsed, "--area");
344
413
  const flow = flowFlag(one(parsed, "--flow"));
345
414
  if (flow !== undefined) await assertFlow(target.repository, flow);
346
- return bareId(
415
+ return idOfKey(
347
416
  (
348
417
  await updateTask(ctx.store, key, {
349
418
  ...(title === undefined ? {} : { title }),