@operad-io/dev 0.2.0 → 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.
package/dev.schema.json CHANGED
@@ -144,7 +144,7 @@
144
144
  "minLength": 1
145
145
  },
146
146
  "remote": {
147
- "description": "The git remote task branches are fetched from and pushed to.",
147
+ "description": "The git remote task branches are fetched from and pushed to. Leave it out when the repository has no remote: the local main branch is then the base, nothing is fetched or pushed, and a merge only moves the local main branch. A repository that has the default remote and no `remote` setting uses it.",
148
148
  "type": "string",
149
149
  "minLength": 1,
150
150
  "default": "origin"
@@ -173,6 +173,15 @@
173
173
  "type": "integer",
174
174
  "minimum": 1
175
175
  },
176
+ "workIn": {
177
+ "title": "Work in",
178
+ "description": "Where a task's changes are made. branch (A branch in this folder): dev makes the task's branch in the main clone and the task works there, one task at a time; it cannot be combined with a concurrency above 1 or workers that add up to more than 1. worktree (A separate worktree): each task has its own worktree.",
179
+ "oneOf": [
180
+ { "const": "branch", "title": "A branch in this folder" },
181
+ { "const": "worktree", "title": "A separate worktree" }
182
+ ],
183
+ "default": "worktree"
184
+ },
176
185
  "sandbox": {
177
186
  "description": "Deprecated and ignored; kept so settings written by an earlier version still validate.",
178
187
  "type": "string",
@@ -590,7 +599,7 @@
590
599
  "if [ -e \"$OPERAD_WORKTREE-gate\" ]; then git worktree remove --force \"$OPERAD_WORKTREE-gate\"; fi",
591
600
  "git worktree prune",
592
601
  "if git show-ref --verify --quiet \"refs/heads/work/$OPERAD_TASK\"; then git branch -D \"work/$OPERAD_TASK\"; fi",
593
- "if git ls-remote --exit-code --heads \"$OPERAD_REMOTE\" \"work/$OPERAD_TASK\" >/dev/null; then git push \"$OPERAD_REMOTE\" --delete \"work/$OPERAD_TASK\"; else [ $? -eq 2 ]; fi"
602
+ "if [ -z \"$OPERAD_REMOTE\" ]; then :; elif git ls-remote --exit-code --heads \"$OPERAD_REMOTE\" \"work/$OPERAD_TASK\" >/dev/null; then git push \"$OPERAD_REMOTE\" --delete \"work/$OPERAD_TASK\"; else [ $? -eq 2 ]; fi"
594
603
  ]
595
604
  }
596
605
  },
@@ -601,7 +610,7 @@
601
610
  "if [ -e \"$OPERAD_WORKTREE-gate\" ]; then git worktree remove --force \"$OPERAD_WORKTREE-gate\"; fi",
602
611
  "git worktree prune",
603
612
  "if git show-ref --verify --quiet \"refs/heads/work/$OPERAD_TASK\"; then git branch -D \"work/$OPERAD_TASK\"; fi",
604
- "if git ls-remote --exit-code --heads \"$OPERAD_REMOTE\" \"work/$OPERAD_TASK\" >/dev/null; then git push \"$OPERAD_REMOTE\" --delete \"work/$OPERAD_TASK\"; else [ $? -eq 2 ]; fi"
613
+ "if [ -z \"$OPERAD_REMOTE\" ]; then :; elif git ls-remote --exit-code --heads \"$OPERAD_REMOTE\" \"work/$OPERAD_TASK\" >/dev/null; then git push \"$OPERAD_REMOTE\" --delete \"work/$OPERAD_TASK\"; else [ $? -eq 2 ]; fi"
605
614
  ]
606
615
  }
607
616
  },
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@operad-io/dev",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
7
7
  },
8
8
  "dependencies": {
9
- "@operad-io/access-match": "0.2.0",
10
- "@operad-io/chat": "0.2.0",
11
- "@operad-io/engine": "0.2.0",
12
- "@operad-io/pool": "0.2.0",
13
- "@operad-io/review-grading": "0.2.0"
9
+ "@operad-io/access-match": "0.3.0",
10
+ "@operad-io/chat": "0.3.0",
11
+ "@operad-io/engine": "0.3.0",
12
+ "@operad-io/pool": "0.3.0",
13
+ "@operad-io/review-grading": "0.3.0"
14
14
  },
15
15
  "devDependencies": {
16
- "@operad-io/shell": "0.2.0",
16
+ "@operad-io/shell": "0.3.0",
17
17
  "@operad-io/test-support": "0.0.0"
18
18
  },
19
19
  "description": "operad: the dev plugin, plans and tasks on a repository",
@@ -0,0 +1,70 @@
1
+ ---
2
+ name: setup-git
3
+ description: Set up git here. The first task in a folder that has no git, or on a machine that has none: install git if needed, then propose the first commit.
4
+ ---
5
+
6
+ I want a task in the folder {target} and operad has told me that git is missing or not set
7
+ up there. Your job is to get git ready so the task can start. Reply in the language I write
8
+ to you in. Use these words with me: "Set up git here" (在这里设置 git); do not use the words
9
+ repository, worktree, branch or remote unless I use them first.
10
+
11
+ Rules that hold throughout:
12
+
13
+ - Git is needed only to create a task. Chatting in a folder and changing its files need
14
+ none. You only get here because I asked for a task: never raise git before that.
15
+ - operad does not bundle git. When it must be installed, it is installed with the
16
+ system's own tool.
17
+ - Every command you run goes through an approval I see in Pending, one command at a time,
18
+ with a line saying what it does. Never run one around that, never chain several, and
19
+ never use sudo or an administrator prompt unless the command below needs it.
20
+ - You write nothing into {target} yourself: no `git init`, no `.gitignore`, no commit.
21
+ Approving the proposal does all of it.
22
+
23
+ 1. Find out what is missing. Run `git --version`. If it prints a version, go to step 3. If
24
+ it fails with "command not found" or the system offers to install developer tools, git
25
+ is not installed: go to step 2. Do not run `git` inside {target} to find out.
26
+
27
+ 2. Install git, with the command for this system, each as an approved pending item. Tell me
28
+ in one sentence what it does first; it may open a system window or ask for my password.
29
+ - macOS: `xcode-select --install` (Apple's command line tools; a window opens and I click
30
+ Install). When it ends, run `git --version` again.
31
+ - Windows: `winget install Git.Git`. Then I may have to close and reopen the app so it
32
+ finds git; check with `git --version`.
33
+ - Linux: the package manager of the distribution, found with `cat /etc/os-release`:
34
+ `sudo apt-get install -y git` (Debian, Ubuntu), `sudo dnf install -y git` (Fedora,
35
+ RHEL), `sudo pacman -S --noconfirm git` (Arch), `sudo zypper install -y git`
36
+ (openSUSE), `sudo apk add git` (Alpine).
37
+ If the tool is missing or fails, say what it printed and what I can do (download git
38
+ from https://git-scm.com/downloads), and stop. Do not try other ways.
39
+
40
+ 3. Look at the folder, without changing anything. List what is in {target} (`ls`), and note
41
+ what must not go into a first commit: dependency folders (`node_modules`, `.venv`,
42
+ `target`, `vendor`), build output (`dist`, `build`, `.next`), secrets (`.env*`, `*.pem`,
43
+ `*.key`, `id_rsa*`), system files. If the folder already has a `.gitignore`, keep every
44
+ line of it.
45
+
46
+ 4. Name and email. A commit needs a name and an email. Ask me, in one sentence, for the
47
+ name and email to put on the commits; offer the computer account's name as the default.
48
+ If you do not know them, propose without `identity`: the proposal prefills them from the
49
+ computer account and shows them in its review. When git already has an identity on this
50
+ machine the proposal says so and uses it; do not ask again.
51
+
52
+ 5. Propose it. Call the operad `command` tool with plugin `bot`, verb `setup-git`,
53
+ arguments `{bot}` and `{target}`, and as input a JSON object: `{}` for the defaults, or
54
+ with only what you change:
55
+
56
+ {
57
+ "gitignore": "<the whole .gitignore text, your folder's own lines kept>",
58
+ "identity": { "name": "<name>", "email": "<email>" }
59
+ }
60
+
61
+ It makes a pending item titled "Set up git here" that shows the `.gitignore`, the list of
62
+ files the first commit would hold and the name and email. Nothing is written into the
63
+ folder or started until I approve it. With no input the command prints these steps again,
64
+ so always send at least `{}`. If it refuses, say what it names in plain words and fix it
65
+ (a `.gitignore` that lets a secret in, a folder that is my home folder or the top of a
66
+ disk, a folder inside another project: then tell me that project's folder).
67
+
68
+ 6. Report. Say what the pending item holds: how many files, anything you left out and why,
69
+ the name and email. If it says there are more than 5000 files, tell me to look at the
70
+ `.gitignore` before I approve. After I approve, the task I asked for can be created.
@@ -12,9 +12,12 @@ to you in.
12
12
  Reference, if you need a key's details: https://operad.io/docs/dev/ (the full key table)
13
13
  and https://operad.io/llms.txt.
14
14
 
15
- 1. Look at the repository. Run `git remote -v` and find the main branch name in {target}.
16
- The repository needs a git remote; if it has none, say so and stop. Check which agents
17
- are on PATH: `claude`, `pi`, `radian`. When none is, operad's own pi is the agent.
15
+ 1. Look at the folder. If {target} has a `.git` entry, run `git remote -v` and find the
16
+ main branch name there. A folder with no git, a machine without the git program, and a
17
+ repository with no git remote are all fine: do not run `git` in a folder that is not a
18
+ repository, do not ask me about git, and leave `remote` out of the file. If {target} is
19
+ inside another git repository, tell me that repository's folder and stop. Check which
20
+ agents are on PATH: `claude`, `pi`, `radian`. When none is, operad's own pi is the agent.
18
21
 
19
22
  2. Find this repo's checks. Read CI config (.github/workflows, .gitlab-ci.yml, ...), the
20
23
  Makefile, package.json scripts, pyproject, Cargo.toml or whatever applies. Choose the
@@ -23,17 +26,28 @@ and https://operad.io/llms.txt.
23
26
  current checkout; if one already fails on main, tell me instead of hiding it. If the
24
27
  repo has no checks, say so and leave `gate.commands` empty.
25
28
 
26
- 3. Find what a fresh checkout needs. Each task runs in its own git worktree, so dependencies
29
+ 3. Find what a fresh checkout needs. Each task runs in a fresh checkout, so dependencies
27
30
  are not there. Put the install step in `prepare` (for example
28
31
  `bun install --frozen-lockfile` or `npm ci`), only if the repo needs one.
29
32
 
30
- 4. Write the `.operad/dev.json` text. Use only keys that matter; keep it small. Use the
33
+ 4. Ask me which way to work, before you write the file. Skip this question when {target} is
34
+ not a git repository: choose the first way without asking. Say it in my language and in plain
35
+ words, about what changes for me; do not use the words clone, worktree or concurrency, and
36
+ do not ask me to know them. Offer two ways:
37
+ - In my folder: the agent makes a new branch in this folder and works there, one thing at
38
+ a time. It is best for me not to edit the folder while it works.
39
+ - Separately: each piece of work gets its own separate folder, so several can go at once
40
+ and my own folder stays untouched.
41
+ If I am not sure, or do not answer, choose the first (a new branch in my folder).
42
+
43
+ 5. Write the `.operad/dev.json` text. Use only keys that matter; keep it small. Use the
31
44
  `workers` form, never the old `worker` form:
32
45
 
33
46
  {
34
47
  "name": "<repo name>",
35
- "remote": "<remote>",
48
+ "remote": "<remote, left out when the repository has none>",
36
49
  "mainBranch": "<main branch>",
50
+ "workIn": "<branch or worktree, from my answer>",
37
51
  "workers": { "<agent>": { "use": "<agent>/agent", "args": [] } },
38
52
  "prepare": [ ... ],
39
53
  "gate": { "commands": [ ... ], "commandTimeoutSecs": 1800 },
@@ -44,14 +58,17 @@ and https://operad.io/llms.txt.
44
58
  - Workers and reviewer: use agents that are actually installed. If two or more are
45
59
  installed, use a different one for the reviewer than for the worker. `use` is
46
60
  `<plugin>/<step>`, such as `claude/agent`, `pi/agent`, `radian/agent`.
61
+ - `workIn` is `"branch"` for the first way and `"worktree"` for the second. Never write
62
+ those two words to me; they belong only in the file. With `"branch"`, leave out
63
+ `concurrency` (it is one at a time).
47
64
  - Leave `autoDefault` at "off": nothing merges without me.
48
65
  - Do not invent model names or flags. Leave `args` empty unless I give you some.
49
66
 
50
- 5. Propose it. Call the operad `command` tool with plugin `bot`, verb `setup-dev`,
67
+ 6. Propose it. Call the operad `command` tool with plugin `bot`, verb `setup-dev`,
51
68
  arguments `{bot}` and `{target}`, and the `.operad/dev.json` text as its input. It makes
52
69
  a pending item; nothing is turned on or written until I approve it in Pending. If it
53
70
  refuses, fix what it names and call it again.
54
71
 
55
- 6. Report. Show me the `.operad/dev.json` you proposed, say in one line why you chose each
56
- check command, worker and reviewer, and list anything you were unsure about. Tell me
72
+ 7. Report. Show me the `.operad/dev.json` you proposed, say in one line why you chose each
73
+ check command, worker and reviewer, and say which way of working I chose and what it means for me in a sentence, and list anything you were unsure about. Tell me
57
74
  to commit `.operad/dev.json` after I approve it (operad reads it from the main clone).
package/src/actions.ts CHANGED
@@ -380,6 +380,12 @@ const reverifyTaskAction: ActionTypeDefinition = {
380
380
  "the task has no work to verify yet; rework it instead",
381
381
  );
382
382
  }
383
+ if (latest.attempt.workDone === false) {
384
+ throw new DevError(
385
+ "reverify",
386
+ "the work did not finish, so there is nothing to verify again; rework it instead",
387
+ );
388
+ }
383
389
  return {
384
390
  ...latest,
385
391
  phase: "running",
@@ -501,9 +507,13 @@ export const actions: readonly ActionTypeDefinition[] = [
501
507
  runs: ctx.runs,
502
508
  home: ctx.home,
503
509
  });
504
- return cancelled.merged.length === 0
505
- ? undefined
506
- : `${cancelled.merged.map(idOfKey).join(", ")} merged before the cancel and stays merged`;
510
+ const lines = [
511
+ ...(cancelled.merged.length === 0
512
+ ? []
513
+ : [`${cancelled.merged.map(idOfKey).join(", ")} merged before the cancel and stays merged`]),
514
+ ...cancelled.notes,
515
+ ];
516
+ return lines.length === 0 ? undefined : lines.join("; ");
507
517
  },
508
518
  },
509
519
  {
@@ -523,7 +533,7 @@ export const actions: readonly ActionTypeDefinition[] = [
523
533
  runs: ctx.runs,
524
534
  home: ctx.home,
525
535
  });
526
- return cancelled.alreadyMerged;
536
+ return cancelled.alreadyMerged ?? cancelled.note;
527
537
  },
528
538
  },
529
539
  {
package/src/announce.ts CHANGED
@@ -86,7 +86,10 @@ export function waitingReason(
86
86
  return { reason: "run stopped during work", step: "work" };
87
87
  }
88
88
  if (conflicted) return { reason: "conflict" };
89
- if (edge.result === "nothing") return { reason: "work did nothing" };
89
+ if (edge.result === "nothing") {
90
+ // The only note a "nothing" visit carries is the main-checkout line.
91
+ return { reason: "work did nothing", detail: nonEmpty(attempt?.workNote) };
92
+ }
90
93
  if (attempt?.workTimedOut === true) {
91
94
  return { reason: "work timed out", detail: nonEmpty(attempt.workNote) };
92
95
  }
@@ -178,7 +181,9 @@ export function waitingPrompt(
178
181
  return "The checks and the review passed; the task is ready to merge.";
179
182
  }
180
183
  if (found.reason === "work did nothing") {
181
- return "The worker changed nothing.";
184
+ return found.detail === undefined
185
+ ? "The worker changed nothing."
186
+ : `The worker changed nothing in its worktree, but it ${found.detail}.`;
182
187
  }
183
188
  if (found.reason === "work timed out") {
184
189
  return `The worker timed out.${found.detail === undefined ? "" : ` ${found.detail}.`}`;
package/src/chat.ts CHANGED
@@ -9,12 +9,14 @@ import { messageOf } from "@operad-io/engine";
9
9
  import {
10
10
  ACCESS_ACTION_REQUEST,
11
11
  CHAT_TYPE,
12
+ ACCESS_FACT,
12
13
  ENTRY_FACT,
13
14
  WORKTREE_EVENT,
14
15
  WORKTREE_NOTE,
15
16
  WORKTREE_REPLY_EVENT,
16
17
  agentConditionals,
17
18
  added,
19
+ type AccessChangedData,
18
20
  type AgentChoices,
19
21
  chatData,
20
22
  chatRef,
@@ -51,6 +53,8 @@ import {
51
53
  type DevSettings,
52
54
  } from "./settings.ts";
53
55
  import { git } from "./git.ts";
56
+ import { gitStateOf } from "./git-state.ts";
57
+ import { cloneHolder } from "./service.ts";
54
58
  import { isRegistered, prepareWorktree, workBranch, worktreePath } from "./steps.ts";
55
59
 
56
60
  /**
@@ -223,16 +227,33 @@ export function repositoryChatContext(input: {
223
227
  readonly repository: string;
224
228
  readonly cwd: string;
225
229
  readonly readOnly: boolean;
230
+ /** The chat works in the main clone on the current branch, at its access level (workIn branch). */
231
+ readonly inPlace?: boolean;
232
+ /** The task that holds the main clone on its own branch; the reply is read-only meanwhile. */
233
+ readonly heldBy?: string | undefined;
226
234
  readonly branch?: string | undefined;
235
+ /** The folder has no git (no program, no repository or no commit): nothing records or undoes a change. */
236
+ readonly noGit?: boolean;
227
237
  readonly skills: readonly string[];
228
238
  readonly denyCommands: readonly string[];
229
239
  }): RepositoryChatContext {
230
240
  const root = realPathOrUndefined(input.cwd) ?? resolve(input.cwd);
231
241
  const where = [
232
242
  `You are answering in an operad chat about the repository ${input.repository}, in ${input.cwd}.`,
233
- input.readOnly
243
+ input.heldBy !== undefined
244
+ ? `This chat is read-only in the main clone for now: the task ${input.heldBy} holds the clone on its own branch and is the only one that changes files there. Read and answer, but do not change files or run commands that change anything; when the person wants a change, tell them to wait until ${input.heldBy} is merged, cancelled or rejected, and then ask again.`
245
+ : input.inPlace === true
246
+ ? "This chat works in the main clone, on the branch that is checked out, at its access level. Changes made here are changes to the repository itself; no worktree is opened."
247
+ : input.readOnly
234
248
  ? "This chat is read-only in the main clone. Commands that only read run at once; a call that changes a file asks the person first."
235
249
  : `This chat works in its own worktree on the branch ${input.branch ?? "unknown"}. Changes reach main through a task, not by merging here.`,
250
+ ...(input.noGit === true
251
+ ? [
252
+ input.inPlace === true && input.heldBy === undefined
253
+ ? "This folder has no git (no repository with a commit), so nothing records or undoes a change. The chat may change files here at its access level; say what you changed."
254
+ : "This folder has no git (no repository with a commit).",
255
+ ]
256
+ : []),
236
257
  ...(input.denyCommands.length === 0
237
258
  ? []
238
259
  : [`These commands are refused here: ${input.denyCommands.join(", ")}.`]),
@@ -260,6 +281,30 @@ export function repositoryChatContext(input: {
260
281
  return { where, text: parts.join("\n\n") };
261
282
  }
262
283
 
284
+ /** True when the folder is known to have no git; a folder git cannot judge claims nothing. */
285
+ function folderHasNoGit(folder: string): boolean {
286
+ try {
287
+ const state = gitStateOf(folder).state;
288
+ return state === "no program" || state === "no repository" || state === "no commit";
289
+ } catch {
290
+ return false;
291
+ }
292
+ }
293
+
294
+ /** Why a worktree cannot be opened in the folder, or undefined when git is ready there. */
295
+ function worktreeRefusal(folder: string): string | undefined {
296
+ try {
297
+ const state = gitStateOf(folder);
298
+ if (state.state === "ready") return undefined;
299
+ if (state.state === "subfolder") {
300
+ return `this folder is inside the git repository at ${state.root}; a worktree needs the repository's own folder`;
301
+ }
302
+ return "this folder has no git yet, so a chat cannot get a worktree here";
303
+ } catch (error) {
304
+ return messageOf(error);
305
+ }
306
+ }
307
+
263
308
  /** The event dev sends after a live worktree yes, so the chat answers the same message there. */
264
309
  export const CHAT_REPLY_EVENT = WORKTREE_REPLY_EVENT;
265
310
 
@@ -460,7 +505,7 @@ const chatAction: ActionTypeDefinition = {
460
505
  ...(effort === undefined ? {} : { effort }),
461
506
  ...(effort === undefined || effortDefault === undefined ? {} : { effortDefault }),
462
507
  cwd: repository.path,
463
- access: chatAccess(skills.roots, true),
508
+ access: chatAccess(skills.roots, settings.workIn !== "branch"),
464
509
  accessLevel: "plan",
465
510
  accessDefault: "plan",
466
511
  skills: skills.directories,
@@ -515,7 +560,11 @@ export async function runChatSettings(ctx: StepContext, params: unknown): Promis
515
560
  } catch (error) {
516
561
  return failed(messageOf(error));
517
562
  }
518
- const access = chatAccess(skills.roots, !hasWorktree);
563
+ const inPlace = !hasWorktree && settings.workIn === "branch";
564
+ // A task that holds the clone on its own branch is the only writer there: the
565
+ // reply reads and says why (ADR 0028 D3, ADR 0039 D3).
566
+ const holder = inPlace ? await cloneHolder(ctx.store, name) : undefined;
567
+ const access = chatAccess(skills.roots, (!hasWorktree && !inPlace) || holder !== undefined);
519
568
  const worktree = isRecord(stored.data.worktree) ? stored.data.worktree : {};
520
569
  const branch =
521
570
  hasWorktree
@@ -529,7 +578,10 @@ export async function runChatSettings(ctx: StepContext, params: unknown): Promis
529
578
  const context = repositoryChatContext({
530
579
  repository: name,
531
580
  cwd,
532
- readOnly: !hasWorktree,
581
+ ...(hasWorktree || !folderHasNoGit(repository.path) ? {} : { noGit: true }),
582
+ readOnly: !hasWorktree && !inPlace,
583
+ inPlace,
584
+ ...(holder === undefined ? {} : { heldBy: holder }),
533
585
  ...(branch === undefined ? {} : { branch }),
534
586
  skills: skills.directories,
535
587
  denyCommands,
@@ -541,6 +593,8 @@ export async function runChatSettings(ctx: StepContext, params: unknown): Promis
541
593
  access,
542
594
  skills: [...skills.directories],
543
595
  context,
596
+ // Nothing to offer when the chat works in the main clone itself.
597
+ ...(inPlace ? { worktreeOffer: false } : {}),
544
598
  },
545
599
  };
546
600
  }
@@ -566,6 +620,11 @@ export async function runChatAccess(ctx: ActionContext, args: unknown): Promise<
566
620
  const chat = chatData(stored.data);
567
621
  const parent = typeof chat.parent === "string" ? chat.parent : "";
568
622
  if (!parent.startsWith("repo:")) return "a bot chat works in its own directory";
623
+ const repoObject = await ctx.store.get("repo", parent.slice("repo:".length));
624
+ if (repoObject !== undefined) {
625
+ const settings = await readDevSettings(repositoryData(repoObject.data).path);
626
+ if (settings.workIn === "branch") return "no worktree is needed: the chat works in the main clone";
627
+ }
569
628
  const result = await openChatWorktree(
570
629
  ctx,
571
630
  ctx.object.id,
@@ -605,6 +664,7 @@ async function openChatWorktree(
605
664
  chatId: string,
606
665
  runId: string,
607
666
  cause: string,
667
+ raiseLevel = false,
608
668
  ): Promise<PluginStepResult> {
609
669
  const stored = await ctx.store.get(CHAT_TYPE, chatId);
610
670
  if (stored === undefined) return failed(`there is no chat ${chatId}`);
@@ -624,6 +684,8 @@ async function openChatWorktree(
624
684
  } catch (error) {
625
685
  return failed(error instanceof DevError ? error.message : String(error));
626
686
  }
687
+ const refusal = worktreeRefusal(repository.path);
688
+ if (refusal !== undefined) return failed(refusal);
627
689
  const worktreeName = chatWorktreeName(chatId);
628
690
  const prepared = prepareWorktree(repository, settings, worktreeName);
629
691
  if (prepared.kind !== "ready") {
@@ -647,11 +709,18 @@ async function openChatWorktree(
647
709
  const clean: Record<string, unknown> = { ...fresh.data };
648
710
  // Only chat:access marks this in memory, to call its hidden action; it is never stored.
649
711
  delete clean[ACCESS_ACTION_REQUEST];
712
+ // A chat that asked for the worktree is read-only only by its level `plan`;
713
+ // the worktree is where it writes, so the level moves to `edits` with it. A
714
+ // level the person set above `plan` stays as it is.
715
+ const freshChat = chatData(fresh.data);
716
+ const level = freshChat.accessLevel ?? (isRecord(fresh.data.access) && fresh.data.access.readOnly === true ? "plan" : "ask");
717
+ const raised = raiseLevel && level === "plan";
650
718
  await ctx.store.put(
651
719
  CHAT_TYPE,
652
720
  chatId,
653
721
  {
654
722
  ...clean,
723
+ ...(raised ? { accessLevel: "edits" } : {}),
655
724
  cwd: path,
656
725
  access: chatAccess(skills.roots, false),
657
726
  skills: [...skills.directories],
@@ -666,6 +735,10 @@ async function openChatWorktree(
666
735
  object: chatRef(chatId),
667
736
  data: { runId, kind: "worktree", text: WORKTREE_NOTE },
668
737
  });
738
+ if (raised) {
739
+ const changed: AccessChangedData = { by: { via: "dev", id: "worktree" }, from: "plan", to: "edits" };
740
+ await ctx.facts.append({ type: ACCESS_FACT, object: chatRef(chatId), data: changed });
741
+ }
669
742
  return { result: "done", data: { moved: true, path, branch } };
670
743
  }
671
744
 
@@ -677,12 +750,13 @@ async function openChatWorktree(
677
750
  export async function runChatWorktree(ctx: StepContext): Promise<PluginStepResult> {
678
751
  const object = ctx.object;
679
752
  if (object === undefined || object.type !== CHAT_TYPE) return failed("this runs on a chat");
680
- const result = await openChatWorktree(ctx, object.id, ctx.runId, "step:dev/chat_worktree");
681
- const data = isRecord(result.data) ? result.data : {};
753
+ const result = await openChatWorktree(ctx, object.id, ctx.runId, "step:dev/chat_worktree", true);
682
754
  // A live yes ended the reply that asked; answer the same message again now
683
- // that the chat is writable. A yes that came after the reply ended only
684
- // opens the worktree, so it starts no reply.
685
- if (data.moved === true && ctx.trigger?.type === WORKTREE_EVENT && !lateWorktree(ctx)) {
755
+ // that the chat is writable. A reply that ended because the chat moved to
756
+ // its worktree meanwhile (its level was raised) is answered again the same
757
+ // way. A yes that came after the reply ended only opens the worktree, so it
758
+ // starts no reply.
759
+ if (result.result === "done" && ctx.trigger?.type === WORKTREE_EVENT && !lateWorktree(ctx)) {
686
760
  await ctx.emit({
687
761
  type: CHAT_REPLY_EVENT,
688
762
  key: `worktree:${object.id}`,
package/src/check.ts CHANGED
@@ -1,6 +1,7 @@
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
  */
@@ -10,6 +11,7 @@ import { messageOf, type MissingItem, type PluginBaseContext } from "@operad-io/
10
11
  /** The check's answer when something is missing (ADR 0010 D7). */
11
12
  type SetupMissing = { readonly note: string; readonly missing: readonly MissingItem[] };
12
13
  import { git } from "./git.ts";
14
+ import { gitStateOf } from "./git-state.ts";
13
15
  import { listRepositories } from "./service.ts";
14
16
  import { readDevSettings } from "./settings.ts";
15
17
 
@@ -26,40 +28,52 @@ function listsBranch(output: string, branch: string): boolean {
26
28
  .some((line) => line.split("\t")[1]?.trim() === ref);
27
29
  }
28
30
 
29
- /** 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. */
30
32
  async function checkRepository(
31
33
  repository: { readonly name: string; readonly path: string },
32
- ): Promise<string> {
34
+ ): Promise<string | undefined> {
33
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
+ }
34
48
  // The init check must never wait for a credential prompt; the shared runner
35
49
  // retries an empty success under load.
36
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,
54
+ remoteName,
41
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. */
@@ -74,7 +88,11 @@ export async function checkDev(ctx: PluginBaseContext): Promise<string | SetupMi
74
88
 
75
89
  const failures: string[] = [];
76
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.
77
94
  try {
95
+ if (gitStateOf(repository.path).state !== "ready") continue;
78
96
  await checkRepository(repository);
79
97
  } catch (error) {
80
98
  failures.push(`repository "${repository.name}": ${messageOf(error)}`);
@@ -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
+ }
@@ -305,7 +305,7 @@ function workParams(stage: WorkStage): Readonly<Record<string, unknown>> {
305
305
  ...(stage.pool === undefined ? {} : { flowPool: stage.pool }),
306
306
  ...(stage.brief === undefined ? {} : { flowBrief: stage.brief }),
307
307
  ...(stage.workerBrief === undefined ? {} : { flowWorkerBrief: stage.workerBrief }),
308
- ...(stage.commit === true ? { flowCommit: true } : {}),
308
+ ...(stage.commit === undefined ? {} : { flowCommit: stage.commit }),
309
309
  ...(stage.beforeAttempt === undefined ? {} : { beforeAttempt: stage.beforeAttempt }),
310
310
  };
311
311
  }