pi-background-run 0.2.1 → 0.4.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
@@ -2,9 +2,10 @@
2
2
 
3
3
  Run long shell commands (test suites, builds, linters) as detached background jobs
4
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.
5
+ on disk — the full log plus a trailing exit marker so nothing large ever enters
6
+ the conversation; the command returns immediately. When the job finishes,
7
+ pi-background-run **wakes the live agent session** so it proactively reads a
8
+ condensed digest of the results and continues — no polling, no human intervention.
8
9
 
9
10
  Built as a [pi](https://github.com/earendil-works/pi-coding-agent) extension. No
10
11
  shell runner, no poller, no sidecar files — the extension spawns the job in-process,
@@ -18,9 +19,6 @@ the agent. The log file is self-describing (full output + a trailing
18
19
  pi install npm:pi-background-run
19
20
  ```
20
21
 
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
22
  Or the scoped alias (same code, permanent namespace claim):
25
23
 
26
24
  ```bash
@@ -35,17 +33,35 @@ Restart pi after install so the extension loads.
35
33
  | ------ | --------- |
36
34
  | `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
35
  | `bgstatus` | Show job status. With an id: any job's state + exit code. Without: this session's running jobs (finished jobs hidden by default — pass `includeDone: true` or set `showCompletedJobs`). Jobs from other sessions are only listed when `adoptForeignJobs` is enabled. |
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 retention: `cleanupDays` config (7 days). Always runs not throttled. |
36
+ | `bgtail` | Print the last N lines of a job's log (default 40), **condensed for context**: ANSI escapes stripped, repeated lines collapsed, long lines and total size capped. Pass `raw: true` to skip condensing. |
37
+ | `bgclean` | Remove old job logs. **Default scope: this session's jobs only** — other sessions' logs are untouched. Pass `all: true` to sweep the whole shared jobs dir. Retention: `cleanupDays` config (7 days). Never removes a running job's log. |
38
+
39
+ ## Slash commands
40
+
41
+ Human-facing mirrors of the read/clean tools, usable directly in the TUI
42
+ without asking the agent (registered via `pi.registerCommand` — a separate
43
+ registration from the agent tools above, which is why tools alone never show
44
+ up as `/` commands):
45
+
46
+ | Command | Purpose |
47
+ | --- | --- |
48
+ | `/bgstatus [id] [done]` | One job's status by id, or the session listing (`done`/`all` includes finished jobs). |
49
+ | `/bgtail <id> [lines]` | Tail a job's log (condensed, same as the tool). |
50
+ | `/bgclean [days] [all]` | Remove old logs — session-scoped by default; `all` sweeps every session's. |
40
51
 
41
- `bgwait` and `bgkill` are not providedthe pi port has no shell runner. Use
42
- `bash` with `kill` if you ever need to stop a running job.
52
+ `/bgrun` is deliberately not a command starting jobs (and reacting to their
53
+ wake messages) is the agent's workflow.
54
+
55
+ ## Roadmap / not provided
56
+
57
+ - `bgkill` — not implemented; use `bash` with `kill` (job ids end in the child pid) if you ever need to stop a running job.
58
+ - `bgwait` — not implemented; the wake mechanism makes blocking on a job unnecessary in the normal flow.
43
59
 
44
60
  ## How it works
45
61
 
46
- ```
62
+ ```text
47
63
  agent calls bgrun(command: "make test-short", name: "unit-tests")
48
- → extension resolves log path: ~/.pi-bgrun/jobs/<slug>-<ts>-<pid>.log
64
+ → extension resolves log path: <jobsDir>/<slug>-<ts>-<pid>.log (default ~/.pi-bgrun/jobs/)
49
65
  → spawn('sh', ['-c', '<cmd>; ec=$?; printf "\\n__BGRUN_EXIT__=%d\\n" "$ec"; exit $ec'],
50
66
  { stdio: ['ignore', logFd, logFd], detached: true }).unref()
51
67
  → records job in-memory + appends a bgrun-job entry to the session
@@ -66,14 +82,21 @@ exit code even after a restart.
66
82
 
67
83
  ## Reading results without flooding context
68
84
 
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.
85
+ Two-tier read model the log file stays complete on disk for deep analysis;
86
+ only bounded digests ever enter the conversation:
87
+
88
+ - **Quick peek:** `bgtail <id>` — condensed last-40-lines (ANSI stripped, repeats
89
+ collapsed, ~8KB cap). The wake message itself already carries the exit code
90
+ and the log's last line, so many turns need no follow-up read at all.
91
+ - **Whole-log analysis:** `ctx_execute_file` on the job's log path to extract
92
+ only failure lines. Never `cat` or `Read` a full bgrun log.
72
93
 
73
94
  ## Configuration
74
95
 
75
- The jobs dir (`~/.pi-bgrun/jobs`) is shared by **every pi session on the
76
- machine**. By default each session only *tracks its own jobs*: the widget and
96
+ The jobs dir (default `~/.pi-bgrun/jobs`, overridable via `jobsDir` / `PI_BGRUN_DIR`)
97
+ is shared by **every pi session on the machine** that sharing is what enables
98
+ cross-session job lookup, session-restart reconstruction, and machine-wide
99
+ cleanup. By default each session only *tracks its own jobs*: the widget and
77
100
  `bgstatus` listings show this session's running jobs, and finished jobs are
78
101
  hidden (ask for them explicitly with `bgstatus includeDone: true`). Jobs
79
102
  started by other sessions can still be inspected by id, but they don't clutter
@@ -90,6 +113,7 @@ config file (trusted projects only) ← environment variables**.
90
113
  "adoptForeignJobs": false,
91
114
  "showCompletedJobs": false,
92
115
  "cleanupDays": 7,
116
+ "globalAutoClean": true,
93
117
  "jobsDir": "/some/other/dir"
94
118
  }
95
119
  ```
@@ -101,14 +125,29 @@ Environment variables (same knobs, handy for one-off overrides):
101
125
  | `PI_BGRUN_DIR` | `~/.pi-bgrun/jobs` | Override where job logs are stored. |
102
126
  | `PI_BGRUN_FOREIGN_JOBS` | `false` | Adopt other sessions' running jobs into this session's widget and job list. Adopted jobs are polled so they leave the widget when they finish. |
103
127
  | `PI_BGRUN_SHOW_COMPLETED` | `false` | Include finished jobs in `bgstatus` listings by default. |
104
- | `PI_BGRUN_CLEANUP_DAYS` | `7` | Log retention for auto-clean sweeps and the `bgclean` default. |
128
+ | `PI_BGRUN_CLEANUP_DAYS` | `7` | Log retention for cleanup sweeps and the `bgclean` default. |
129
+ | `PI_BGRUN_GLOBAL_AUTO_CLEAN` | `true` | Set `0`/`false` to disable the automatic global orphan sweep (see below). |
105
130
 
106
131
  ### Log cleanup
107
132
 
108
- - **Auto-sweep** runs at `session_start` and `session_shutdown`, but at most
109
- **once per `cleanupDays`** (tracked by a `.last-clean` marker in the jobs dir)
110
- — restart-heavy workflows don't re-sweep on every launch.
111
- - **Manual** `bgclean` always runs immediately and refreshes the marker.
133
+ Cleanup follows the same principle as everything else: **one session should
134
+ not delete another session's artifacts.**
135
+
136
+ - **Session-scoped auto-sweep (default)** runs at `session_start` and
137
+ `session_shutdown` and removes only *this session's* finished logs older
138
+ than `cleanupDays`. Cheap and unthrottled.
139
+ - **Global orphan sweep (default on; opt out with `globalAutoClean: false` /
140
+ `PI_BGRUN_GLOBAL_AUTO_CLEAN=0`)** — also sweeps the whole shared jobs dir at
141
+ session boundaries, removing *finished* logs (exit marker, or dead pid)
142
+ older than `cleanupDays`. This is what keeps orphans from sessions that
143
+ crashed or will never be resumed from accumulating: a week-old finished log
144
+ is garbage under the same retention its owning session would apply itself.
145
+ Throttled to once per `cleanupDays` via a `.last-clean` marker so
146
+ restart-heavy workflows don't re-sweep on every launch. Running jobs are
147
+ pid-protected, so live sessions are never affected.
148
+ - **Manual**: `bgclean` cleans this session's old logs; `bgclean` with
149
+ `all: true` sweeps every session's logs immediately (and refreshes the
150
+ marker).
112
151
  - Running jobs are never swept while their pid is alive.
113
152
 
114
153
  ## Status