From 12deedf2cd2e0ffecbef7dffd911ea4201cf6bf2 Mon Sep 17 00:00:00 2001 From: Bastien Riviere Date: Fri, 27 Nov 2026 17:17:06 +0100 Subject: [PATCH] chore(lint): run commands only through exec, and record how to stop work --- .agents/skills/pi-durable/SKILL.md | 3 ++- CODING_STANDARDS.md | 1 + daemon/src/workspaces.ts | 2 ++ docs/adr/0005-cancel-a-mission.md | 17 +++++++++++++++++ justfile | 6 +++--- lint/rules/child-process-through-exec.yml | 12 ++++++++++++ lint/tests/child-process-through-exec-test.yml | 10 ++++++++++ 7 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 docs/adr/0005-cancel-a-mission.md create mode 100644 lint/rules/child-process-through-exec.yml create mode 100644 lint/tests/child-process-through-exec-test.yml diff --git a/.agents/skills/pi-durable/SKILL.md b/.agents/skills/pi-durable/SKILL.md index 781d8d7..e0dbafb 100644 --- a/.agents/skills/pi-durable/SKILL.md +++ b/.agents/skills/pi-durable/SKILL.md @@ -1,6 +1,6 @@ --- name: pi-durable -description: Find your way in Pi Durable (@earendil-works/pi-durable), the harness that runs Pilot's Chats. Use before you change daemon/src/harness-host.ts, or add a tool, a signal, a prompt section, an extension, a hook or a task for Pilot's agents. +description: Find your way in Pi Durable (@earendil-works/pi-durable), the harness that runs Pilot's Chats. Use before you change daemon/src/harness-host.ts, stop a Chat, or add a tool, a signal, a prompt section, an extension, a hook or a task for Pilot's agents. --- # Pi Durable @@ -32,6 +32,7 @@ Everything is in `daemon/node_modules/@earendil-works/pi-durable/`: - **A tool that waits for the Owner** (the `question` tool): define a signal with `defineSignal`. `execute` first reads it with `api.readSignal`. With no value, the tool records its work and returns `{ sleep: { signals: [Signal.key(KEY)] } }`. The call then holds no process and survives a restart. `harness.signal(taskId, Signal, KEY, value, context)` wakes it, and `execute` runs again with the same arguments. Mark it `replay: "safe"`, and give it `cancel` to clean up when its Chat is stopped while it waits. - **Prompt sections**: `section(key, render)` renders as `…`. `{ tag: false }` renders the text alone. Sections render in the order of the extension, and only a changed section is sent again: keep `render` stable, so the provider's prompt cache stays warm. - **Tool guidelines**: Pi Durable's `defineTool` has only a name, a description and parameters. Pi-coding-agent's `promptSnippet`, `promptGuidelines` and `label` do not exist here. Pilot puts a tool's guidelines in a `` section (`RoleTool` in `harness-host.ts`). +- **Stopping a Chat** (`stopChat`, `Conversation.abort()`): Pi Durable aborts the `context.abortSignal` of each running tool call, and resolves only once the Chat is idle. So it waits for `execute` to return. A tool that runs a long command passes that signal down, or the stop waits for the command. A parked call holds no `execute`: its `cancel` runs instead. README "Abort and Subagents", and `abort` in `dist/harness/scheduler.d.ts`. ## Rules in Pilot diff --git a/CODING_STANDARDS.md b/CODING_STANDARDS.md index 22b0a70..32fbbe9 100644 --- a/CODING_STANDARDS.md +++ b/CODING_STANDARDS.md @@ -25,3 +25,4 @@ A review reads this file. `just lint` checks the format and Biome's lint rules ( 7. **Agent text follows `docs/agent-text.md`.** A tool's description, its parameters, its guidelines, a role prompt and a skill each hold their own kind of text, and a guideline names its tool. 8. **One note for each new cause.** Code that runs again (a resume, a retry, a poll) writes its Activity note only when its cause changes. A test runs it twice and counts the notes. 9. **A weaker test says why.** A change that loosens or deletes an existing assertion says why in the pull request body. +10. **Stop work by aborting it, then waiting for it.** Work that a stop must end takes an `AbortSignal` down to the command it runs. The code that stops it aborts the signal and waits for the work to return before it deletes what the work uses. A status in the database, such as a Task's `Stopped`, refuses new work. A check before each write does not stop a write that has started (`docs/adr/0005-cancel-a-mission.md`). diff --git a/daemon/src/workspaces.ts b/daemon/src/workspaces.ts index 6b1600f..2ba3a7d 100644 --- a/daemon/src/workspaces.ts +++ b/daemon/src/workspaces.ts @@ -2,6 +2,8 @@ // in a folder of its own, so two Tasks never share one: /.workspaces///. // Next to each workspace's folder: the setup command's log, the process group of the programs it left running, and the other repositories // cloned for the Task. They all go with the workspace (deleteWorkspace). +// The setup command can leave programs running after it ends, so it runs with spawn, not exec. deleteWorkspace ends its process group. +// ast-grep-ignore: child-process-through-exec import { spawn } from "node:child_process"; import { existsSync, writeFileSync } from "node:fs"; import { mkdir, open, readdir, readFile, rename, rm, rmdir, stat } from "node:fs/promises"; diff --git a/docs/adr/0005-cancel-a-mission.md b/docs/adr/0005-cancel-a-mission.md new file mode 100644 index 0000000..2bce6fb --- /dev/null +++ b/docs/adr/0005-cancel-a-mission.md @@ -0,0 +1,17 @@ +# Cancel a Mission: abort running work, then wait for it + +Cancel Mission stops a Mission's Chats, closes its Inbox items and deletes its workspaces. It changes nothing on GitHub: the branches and pull requests stay, and Pilot still reads their state. A Task's agent can be in the middle of a GitHub write, such as a push or a new draft, when the Owner cancels. + +Pilot marks the Mission's unfinished Tasks `Stopped` in the same write that marks the Mission `cancelling`. A `Stopped` Task takes no new tool call. Then the Task's delivery aborts its `AbortSignal`, which kills the running `gh`, `git` and `jj` commands with their process group (`daemon/src/exec.ts`), and waits for each call to return. Only then does Pilot delete the workspace. A cancellation that a restart cut off finishes when the Daemon opens again. + +## Considered Options + +- A check before each GitHub write (`beforeWrite`), and a `stopped()` check in each module. The first version did this. A write that had started still landed, and the checks spread over six files. +- Wait for the running calls without aborting them. This is simpler, but a cancel can then wait two minutes for a push. +- Close the Mission's pull requests on cancel. The Owner or a Teammate may still want them, and Pilot does not change GitHub without the Owner's word. + +## Consequences + +- A killed push or draft can still reach GitHub. The push lease makes a retry safe. Pilot looks for a draft whose reply was lost by its branch, for 30 minutes after the request (`recoverDrafts`). After that, a draft that GitHub opened without an answer stays on GitHub, and Pilot does not show it. +- Every command runs through `daemon/src/exec.ts` (`lint/rules/child-process-through-exec.yml`). +- Repo removal and a future stop of one Task use the same order: mark, abort, wait, then delete. diff --git a/justfile b/justfile index 08f4a15..31b4184 100644 --- a/justfile +++ b/justfile @@ -97,9 +97,9 @@ fmt: # Run every test: the Daemon's, the App's against the scripted Daemon, then the gallery's test: test-daemon test-app test-gallery -# Run the Daemon's tests -test-daemon: - pnpm -C daemon test +# Run the Daemon's tests: all of them, or the files given, for example: just test-daemon test/tasks.test.ts --test-name-pattern=cancel +test-daemon *args: + {{ if args == "" { "pnpm -C daemon test" } else { "cd daemon && node --test " + args } }} # Run the App's tests against the scripted Daemon. Arguments go to nsh test, for example: just test-app --filter Inbox [positional-arguments] diff --git a/lint/rules/child-process-through-exec.yml b/lint/rules/child-process-through-exec.yml new file mode 100644 index 0000000..9659f39 --- /dev/null +++ b/lint/rules/child-process-through-exec.yml @@ -0,0 +1,12 @@ +id: child-process-through-exec +language: tsx +severity: error +message: 'Run a command with exec from daemon/src/exec.ts. A program that must outlive the call, such as a setup command, imports spawn with "// ast-grep-ignore: child-process-through-exec" above the import and a comment that says why.' +note: exec runs each command in its own process group, so an abort, a timeout or too much output also kills the programs it starts, such as Git's helpers. A second way to run commands once let an aborted push keep running. +files: [daemon/src/**] +ignores: [daemon/src/exec.ts] +rule: + kind: import_statement + regex: 'from\s+["''](node:)?child_process["'']' + not: + regex: '^import\s+type\b' diff --git a/lint/tests/child-process-through-exec-test.yml b/lint/tests/child-process-through-exec-test.yml new file mode 100644 index 0000000..6a5cb00 --- /dev/null +++ b/lint/tests/child-process-through-exec-test.yml @@ -0,0 +1,10 @@ +id: child-process-through-exec +# ast-grep test does not read ast-grep-ignore comments: just lint's scan checks them, on daemon/src/workspaces.ts. +valid: + - import { exec } from "./exec.ts"; + - import type { ChildProcess } from "node:child_process"; +invalid: + - import { execFile } from "node:child_process"; + - import { spawn } from "child_process"; + - import * as childProcess from "node:child_process"; + - import { type ExecFileOptions, execFile } from "node:child_process"; -- knotmirror 0.0.1