pi-background-run 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 stablekernel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # pi-background-run
2
+
3
+ Run long shell commands (test suites, builds, linters) as detached background jobs
4
+ so your pi agent session stays unblocked and its context stays clean. Output lands
5
+ in a file under `~/.pi-bgrun/jobs/`; the command returns immediately. When the job
6
+ finishes, pi-background-run **wakes the live agent session** so it proactively reads the
7
+ results and continues — no polling, no human intervention.
8
+
9
+ Built as a [pi](https://github.com/earendil-works/pi-coding-agent) extension. No
10
+ shell runner, no poller, no sidecar files — the extension spawns the job in-process,
11
+ detects completion via the child `exit` event, and calls `pi.sendUserMessage` to wake
12
+ the agent. The log file is self-describing (full output + a trailing
13
+ `__BGRUN_EXIT__=N` marker), so exit codes survive pi restarting.
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ pi install npm:pi-background-run
19
+ ```
20
+
21
+ > The npm package is `pi-background-run` — npm blocked the name `pi-bgrun`
22
+ > (too similar to the existing `pi-bg-run`).
23
+
24
+ Or the scoped alias (same code, permanent namespace claim):
25
+
26
+ ```bash
27
+ pi install npm:@stablekernel/pi-background-run
28
+ ```
29
+
30
+ Restart pi after install so the extension loads.
31
+
32
+ ## Tools registered
33
+
34
+ | Tool | Purpose |
35
+ | ------ | --------- |
36
+ | `bgrun` | Launch a command detached in the background. Optional `name` gives the job a short human-readable label. Returns `started: <job-id>` immediately. Wakes the session automatically on completion. |
37
+ | `bgstatus` | List jobs (running + done) with exit codes. Reads the in-memory table while pi is alive; scans the jobs dir after restart. |
38
+ | `bgtail` | Print the last N lines of a job's log (default 40), stripping the exit marker. |
39
+ | `bgclean` | Remove old job logs (default 7 days). Skips running jobs while pi is alive. |
40
+
41
+ `bgwait` and `bgkill` are not provided — the pi port has no shell runner. Use
42
+ `bash` with `kill` if you ever need to stop a running job.
43
+
44
+ ## How it works
45
+
46
+ ```
47
+ agent calls bgrun(command: "make test-short", name: "unit-tests")
48
+ → extension resolves log path: ~/.pi-bgrun/jobs/<slug>-<ts>-<pid>.log
49
+ → spawn('sh', ['-c', '<cmd>; ec=$?; printf "\\n__BGRUN_EXIT__=%d\\n" "$ec"; exit $ec'],
50
+ { stdio: ['ignore', logFd, logFd], detached: true }).unref()
51
+ → records job in-memory + appends a bgrun-job entry to the session
52
+ → returns "started: <job-id>"
53
+
54
+ child 'exit' event fires:
55
+ → extension records exit code, appends a done entry
56
+ → pi.sendUserMessage(wake) when idle (triggers a turn)
57
+ or pi.sendUserMessage(wake, { deliverAs: 'followUp' }) when busy
58
+ → ctx.ui.notify(...) — toast for the human
59
+ → ctx.ui.setWidget("bgrun", ...) — updates/clears the live status widget
60
+ ```
61
+
62
+ The child writes the log directly via its own stdout fd (no pipe to pi), so the job
63
+ survives pi crashing and the log completes on disk. The trailing
64
+ `__BGRUN_EXIT__=N` marker makes the log self-describing — `bgstatus` recovers the
65
+ exit code even after a restart.
66
+
67
+ ## Reading results without flooding context
68
+
69
+ - **Quick peek:** `bgtail <id> 40` — last 40 lines, marker stripped.
70
+ - **Whole-log analysis:** `ctx_execute_file` on `~/.pi-bgrun/jobs/<id>.log` to
71
+ extract only failure lines. Never `cat` or `Read` a full bgrun log.
72
+
73
+ ## Configuration
74
+
75
+ | Variable | Default | Description |
76
+ |---|---|---|
77
+ | `PI_BGRUN_DIR` | `~/.pi-bgrun/jobs` | Override where job logs are stored. |
78
+
79
+ ## Status
80
+
81
+ Early / pre-release. See `.pi/wip/pi-port-plan.md` in the source tree for the design.