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.
@@ -0,0 +1,85 @@
1
+ ---
2
+ name: run-bg
3
+ description: Use when running any long or verbose shell command (make test, go test ./...,
4
+ make lint, builds) so output lands in a file instead of flooding context and the session
5
+ stays unblocked. Start the job, hand control back, check status later, and read only a
6
+ tail or a code-processed summary of the log.
7
+ ---
8
+
9
+ # Run in Background (pi-bgrun)
10
+
11
+ Run long/verbose commands detached. Output → file. Context stays clean; the session
12
+ never blocks. The extension wakes this session automatically when the job finishes —
13
+ no polling.
14
+
15
+ ## When to use
16
+ - Any command expected to run > ~30s OR emit > ~100 lines.
17
+ - Typical: `make test`, `go test ./...`, `make lint`, `make build`.
18
+ - Integration / infra suites (long-running, always background).
19
+
20
+ ## When NOT to use
21
+ - Commands that complete in < ~5s — the overhead isn't worth it.
22
+ - Short, quiet commands whose full output you actually need (`git status`).
23
+ - Interactive commands (prompts, REPL, SSH) — bgrun detaches from the terminal.
24
+
25
+ ## Tools
26
+
27
+ | Action | Tool |
28
+ |---|---|
29
+ | Start | `bgrun(command: "make test-short", name: "unit-tests")` → `started: <job-id>` (name is an optional short label; use it so jobs are recognizable in `bgstatus`, the status widget, and wake messages) |
30
+ | Status | `bgstatus(<job-id>)` or `bgstatus()` for all |
31
+ | Tail | `bgtail(<job-id>, 40)` |
32
+ | Clean | `bgclean(7)` |
33
+
34
+ ## Workflow
35
+
36
+ 1. **Start:** call `bgrun` with the command (and a short `name`, e.g. `name: "unit-tests"`).
37
+ Note the returned job-id. Continue other
38
+ work; you will be woken automatically when the job finishes.
39
+ 2. **On wake:** check the exit status in the wake message first.
40
+ - `exit: 0` → success. `bgtail` to confirm.
41
+ - `exit: <non-zero>` → failure. Analyze the log (see below).
42
+ 3. **If you need to check before the wake (non-blocking):** call `bgstatus` with the job id.
43
+ - `running` → keep doing other work. Do NOT spin a wait loop.
44
+ - `done exit=0` → success.
45
+ - `done exit=<non-zero>` → failure; analyze the log.
46
+ - `running` but the job should have finished long ago → likely crashed (the
47
+ process died without writing the exit marker). Analyze the log with
48
+ `ctx_execute_file`.
49
+
50
+ ### Reading results without flooding context
51
+
52
+ - **Quick peek (≤40 lines):** call `bgtail` with the job id and `lines: 40` — strips the `__BGRUN_EXIT__` marker.
53
+ - **Whole-log failure analysis:** `ctx_execute_file` on the log path:
54
+ ```
55
+ ctx_execute_file(
56
+ path: "~/.pi-bgrun/jobs/<JOB>.log",
57
+ language: "javascript",
58
+ code: "const L=FILE_CONTENT.split('\\n'); \
59
+ const fails=L.filter(l=>/(--- FAIL|FAIL|panic:|Error:)/.test(l)); \
60
+ console.log(`lines: ${L.length}, failures: ${fails.length}`); \
61
+ console.log(fails.slice(0,40).join('\\n'));"
62
+ )
63
+ ```
64
+ A 10 000-line `make test` log collapses to a ~30-line summary in context.
65
+
66
+ **Never `cat`, `Read`, or `bash cat` a full bgrun log.** Always `bgtail` or
67
+ `ctx_execute_file`.
68
+
69
+ ## After a pi restart or session switch
70
+
71
+ - The live wake does not survive a pi restart or a `/resume` to a different session
72
+ (the extension loses the child process handle). The log still completes on disk.
73
+ - After a restart/switch, `bgstatus` scans `~/.pi-bgrun/jobs/` and recovers exit codes
74
+ from the log's `__BGRUN_EXIT__=N` marker. If you were waiting on a job, run
75
+ `bgstatus` to find it.
76
+
77
+ ## Rules
78
+
79
+ - Call the tools; never hand-roll `nohup … &` inline.
80
+ - One job = one id. Multiple concurrent jobs are fine — each has its own log.
81
+ - Logs live in `~/.pi-bgrun/jobs` (override with `PI_BGRUN_DIR`).
82
+ - Run `bgclean` periodically; the extension also auto-sweeps old logs on startup
83
+ (14-day threshold).
84
+ - To stop a running job, use `bash` with `kill <pid>` (the pid is in the `bgstatus`
85
+ output). There is no `bgkill` tool.