pi-background-tasks 0.9.0 → 1.0.3

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.
Files changed (72) hide show
  1. package/BACKGROUND-TASKS-INSTRUCTIONS.md +63 -0
  2. package/PUBLISHING.md +43 -29
  3. package/README.md +233 -441
  4. package/TESTING.md +15 -9
  5. package/TEST_PLAN.md +43 -17
  6. package/docs/INDEX.md +157 -0
  7. package/docs/api/eventbus-v1.md +166 -0
  8. package/docs/assets/architecture.svg +78 -0
  9. package/docs/assets/footer-dock.svg +47 -0
  10. package/docs/assets/logo.svg +49 -0
  11. package/docs/attestations.json +189 -0
  12. package/docs/choose-a-workflow.md +98 -0
  13. package/docs/commands/bg-clear.md +70 -0
  14. package/docs/commands/bg-update.md +82 -0
  15. package/docs/commands/bg.md +90 -0
  16. package/docs/commands/fusion-models.md +70 -0
  17. package/docs/commands/fusion.md +69 -0
  18. package/docs/commands/jobs.md +74 -0
  19. package/docs/commands/kill.md +82 -0
  20. package/docs/commands/logs.md +90 -0
  21. package/docs/commands/task-manager.md +109 -0
  22. package/docs/concepts/completion-delivery.md +66 -0
  23. package/docs/concepts/context-projection-and-budgeting.md +79 -0
  24. package/docs/getting-started.md +122 -0
  25. package/docs/manifest.json +1825 -0
  26. package/docs/operations/configuration.md +110 -0
  27. package/docs/operations/releasing.md +67 -0
  28. package/docs/operations/testing.md +101 -0
  29. package/docs/operations/troubleshooting.md +38 -0
  30. package/docs/read-before-edit.md +94 -0
  31. package/docs/reference/runtime-contracts.md +213 -0
  32. package/docs/reference/shortcuts-and-dock.md +70 -0
  33. package/docs/subsystems/attested-pi-runs.md +141 -0
  34. package/docs/subsystems/background-task-runtime.md +85 -0
  35. package/docs/subsystems/child-launch-durability-and-safety.md +57 -0
  36. package/docs/subsystems/delegation.md +190 -0
  37. package/docs/subsystems/docs-freshness-gate.md +26 -0
  38. package/docs/subsystems/fusion.md +121 -0
  39. package/docs/subsystems/host-ui-and-telemetry.md +83 -0
  40. package/docs/tools/bg_delegate.md +193 -0
  41. package/docs/tools/bg_kill.md +114 -0
  42. package/docs/tools/bg_logs.md +133 -0
  43. package/docs/tools/bg_result.md +120 -0
  44. package/docs/tools/bg_run.md +168 -0
  45. package/docs/tools/bg_run_pi_attested.md +170 -0
  46. package/docs/tools/bg_status.md +111 -0
  47. package/docs/tools/fusion_investigate.md +116 -0
  48. package/docs/tools/fusion_reason.md +75 -0
  49. package/docs/tools/fusion_research.md +162 -0
  50. package/docs/tools/fusion_validate.md +206 -0
  51. package/logo.png +0 -0
  52. package/package.json +25 -7
  53. package/src/core/delegate/budget.ts +1 -1
  54. package/src/core/delegate/launch.ts +5 -0
  55. package/src/core/fusion/artifacts.ts +34 -4
  56. package/src/core/fusion/budget.ts +112 -20
  57. package/src/core/fusion/child-protocol.ts +82 -0
  58. package/src/core/fusion/clean-context.ts +91 -0
  59. package/src/core/fusion/config.ts +124 -35
  60. package/src/core/fusion/context.ts +29 -7
  61. package/src/core/fusion/evaluation.ts +392 -15
  62. package/src/core/fusion/orchestrator.ts +217 -23
  63. package/src/core/fusion/pi-child.ts +183 -23
  64. package/src/core/fusion/prompts.ts +39 -26
  65. package/src/core/fusion/source-policy.ts +257 -0
  66. package/src/core/fusion/types.ts +156 -11
  67. package/src/core/fusion/web-fetch.ts +104 -15
  68. package/src/core/fusion/workflows.ts +119 -65
  69. package/src/extension.ts +3 -3
  70. package/src/fusion-child-extension.ts +159 -120
  71. package/src/fusion-extension.ts +585 -240
  72. package/src/testing/normalize.ts +0 -22
@@ -0,0 +1,109 @@
1
+ ---
2
+ doc_id: commands/task-manager
3
+ audience: user
4
+ mode: mixed
5
+ review_policy: contract
6
+ stability: stable
7
+ covers_surfaces: [command:bg-tasks, command:tasks]
8
+ covers_sources: []
9
+ ---
10
+ # `/tasks` and `/bg-tasks`
11
+
12
+ <!-- pi-docs:begin name="command-contract-tasks-bg-tasks" generator="scripts/docs/generate.mjs" -->
13
+ | Command | Description | Provenance |
14
+ | --- | --- | --- |
15
+ | `/tasks` | Open the Claude-like background task manager UI | `src/extension.ts:532` |
16
+ | `/bg-tasks` | Open the background task manager UI | `src/extension.ts:540` |
17
+ <!-- pi-docs:end name="command-contract-tasks-bg-tasks" -->
18
+
19
+ Open the interactive background task manager. `/tasks` and `/bg-tasks` are aliases.
20
+
21
+ ## Synopsis
22
+
23
+
24
+ `/tasks [exact-task-id]`
25
+
26
+ `/bg-tasks [exact-task-id]`
27
+
28
+ ## When to use
29
+
30
+ Use the task manager when you want the host UI: select tasks, inspect a live output tail, stop one or all running tasks, rerun a task, copy/show an output path, or review recent finished task history.
31
+
32
+ ## Defaults
33
+
34
+ - No argument opens the list view.
35
+ - An exact task id opens detail view for that task and marks it seen in the footer. Unlike `/logs`, `/kill`, and the task tools, this optional UI argument is not prefix-resolved.
36
+ - If there are no running tasks but finished history exists, the list opens in history mode.
37
+
38
+ ## Lifecycle
39
+
40
+ The manager is an overlay dock. Opening it sets the footer hint to `focused` and temporarily hides the `/bg-clear` hint; closing returns the footer to the normal `Shift↓` hint. Opening a finished task's detail view marks that task seen. Merely opening the list or closing the dock does **not** clear other finished badges; use [`/bg-clear`](bg-clear.md) to clear them together.
41
+
42
+ List view sorts tasks as running, failed, killed, then completed; within a status, newest terminal/start time appears first. Status labels shown in the UI are `running`, `error` for `failed`, `stopped` for `killed`, and `done` for `completed`.
43
+
44
+ ## Examples
45
+
46
+ ```text
47
+ /tasks
48
+ /bg-tasks b1234
49
+ ```
50
+
51
+ ## Output/result
52
+
53
+ This command opens UI only. In non-interactive mode, it emits an error notification:
54
+
55
+ ```text
56
+ Background task manager requires an interactive Pi UI. Use /jobs, /logs, or the bg_status/bg_logs tools in non-interactive mode.
57
+ ```
58
+
59
+ ## Controls
60
+
61
+ List view:
62
+
63
+ - `↑`/`↓`: select.
64
+ - `PgUp`/`PgDn`: page selection.
65
+ - `Enter`/`→`: open output detail.
66
+ - `k`: stop selected running task.
67
+ - `a`, `A`, or `K`: stop all running tasks; press again to confirm.
68
+ - `h`: show/hide history.
69
+ - `R`: rerun selected task with notification enabled and wake disabled.
70
+ - `c`/`C`: show output path.
71
+ - `Esc`, `q`, `Q`, `x`, `X`: close.
72
+
73
+ Detail view:
74
+
75
+ - `←`: return to list.
76
+ - `↑`/`↓`/`PgUp`/`PgDn`: scroll output.
77
+ - `r`: refresh and resume following tail.
78
+ - `k`: stop running task.
79
+ - `R`: rerun task.
80
+ - `c`/`C`: show output path.
81
+ - close keys are the same as list view.
82
+
83
+ ## Detail output tail semantics
84
+
85
+ The detail view reads a UI-only tail buffer of 128 KiB once per second while following. It displays 12 output lines. Scrolling up pauses live following and freezes the buffer so the view stays stable. Scrolling/pageing back to the bottom resumes follow mode; `r` also resumes follow and refreshes. This UI tail is larger than the model-facing log cap and is separate from [`bg_logs`](../tools/bg_logs.md).
86
+
87
+ ## Errors
88
+
89
+ Stop, stop-all, rerun, and output-read failures are reported inside the dock as action messages. A missing output file appears as `Output file not found: <path>`.
90
+
91
+ ## Runtime artifacts
92
+
93
+ The manager reads task snapshots and output files from the current extension runtime. It does not reattach to detached historical OS processes after Pi shutdown/reload.
94
+
95
+ ## Safety boundaries
96
+
97
+ Stopping uses the same runtime kill path as [`/kill`](kill.md) and [`bg_kill`](../tools/bg_kill.md). Shell commands are not sandboxed; the manager only controls tracked tasks.
98
+
99
+ ## Related docs
100
+
101
+ - [Shortcuts and dock](../reference/shortcuts-and-dock.md)
102
+ - [`/jobs`](jobs.md)
103
+ - [`/logs`](logs.md)
104
+ - [`/kill`](kill.md)
105
+ - [Host UI and telemetry](../subsystems/host-ui-and-telemetry.md)
106
+
107
+ ## Source ownership/reference
108
+
109
+ Surface registration and dock wiring live in `src/extension.ts`; UI behavior is implemented in `src/ui/background-tasks-manager.ts` and owned by [host-ui-and-telemetry](../subsystems/host-ui-and-telemetry.md).
@@ -0,0 +1,66 @@
1
+ ---
2
+ doc_id: concepts/completion-delivery
3
+ audience: agent
4
+ mode: authored
5
+ review_policy: contract
6
+ stability: stable
7
+ covers_surfaces: [renderer:background-task-notification]
8
+ covers_sources: []
9
+ ---
10
+ # Completion delivery
11
+
12
+ Background tasks can finish silently, notify the terminal, or notify and wake the agent. The flags are task-owned.
13
+
14
+ ## Delivery modes
15
+
16
+ | `notifyOnCompletion` | `triggerOnCompletion` | Actual mode |
17
+ |---|---:|---|
18
+ | `true` | `true` | Durable terminal notification and automatic follow-up turn. Default for [`bg_run`](../tools/bg_run.md). |
19
+ | `true` | `false` | Durable terminal notification only; no provider follow-up. Default for [`/bg`](../commands/bg.md). |
20
+ | `false` | `true` | Manual monitoring; `triggerOnCompletion` has no effect without a notification. |
21
+ | `false` | `false` | Manual monitoring. |
22
+
23
+ ## Critical behavior
24
+
25
+ - `/bg` is display-only by default: it sets `notifyOnCompletion:true` and `triggerOnCompletion:false`.
26
+ - `bg_run` defaults to durable notification plus follow-up turn: `notifyOnCompletion:true` and `triggerOnCompletion:true`.
27
+ - `bg_status` and `bg_logs` are point-in-time inspection tools, not polling primitives.
28
+ - A received `<background-task-notification>` is metadata-backed terminal-status truth. The output stream has finished/closed, but ordinary `.output` bytes are not explicitly fsynced. Do not call `bg_status` only to reconfirm status; call `bg_logs` only if output bytes are needed.
29
+
30
+ ## Notification payload
31
+
32
+ When enabled and not during shutdown, terminal completion sends a custom message with:
33
+
34
+ - `<task-id>`
35
+ - `<task-name>`
36
+ - `<status>` (`completed`, `failed`, or `killed`)
37
+ - optional `<exit-code>`
38
+ - optional `<error>`
39
+ - `<output-file>`
40
+ - `<summary>`
41
+ - `<guidance>` that says terminal state and output metadata are durable and not to reconfirm with `bg_status`.
42
+
43
+ The structured details contain the task snapshot, including delivery flags and `notified` state.
44
+
45
+ ## Agent guidance
46
+
47
+ After default `bg_run`, continue only independent useful work. If there is no such work, briefly acknowledge and end the turn; the follow-up notification will wake the agent. Do not sleep, poll `bg_status`, or repeatedly read `bg_logs` merely to wait.
48
+
49
+ If either completion flag was intentionally disabled, manual inspection is allowed when deliberate, but still avoid tight polling.
50
+
51
+ ## Failures and suppression
52
+
53
+ If notification send fails, the task resets `notified:false` and logs the error; it does not silently pretend delivery happened. During Pi session shutdown/reload, notifications are suppressed while running tasks are killed.
54
+
55
+ ## Related docs
56
+
57
+ - [`bg_run`](../tools/bg_run.md)
58
+ - [`bg_status`](../tools/bg_status.md)
59
+ - [`bg_logs`](../tools/bg_logs.md)
60
+ - [`/bg`](../commands/bg.md)
61
+ - [Background task runtime](../subsystems/background-task-runtime.md)
62
+ - [Host UI and telemetry](../subsystems/host-ui-and-telemetry.md)
63
+
64
+ ## Source ownership/reference
65
+
66
+ Delivery receipt text is derived by runtime helpers and sent by extension registration; primary behavioral ownership is split between [background-task-runtime](../subsystems/background-task-runtime.md) and [host-ui-and-telemetry](../subsystems/host-ui-and-telemetry.md).
@@ -0,0 +1,79 @@
1
+ ---
2
+ doc_id: concepts/context-projection-and-budgeting
3
+ audience: agent
4
+ mode: authored
5
+ review_policy: behavioral
6
+ stability: evolving
7
+ covers_surfaces: []
8
+ covers_sources: [src/core/context/parent-snapshot.ts, src/core/context/token-budget.ts, src/core/context/visible-conversation-v2.ts]
9
+ ---
10
+ # Context projection and budgeting
11
+
12
+ Primary sources: `src/core/context/parent-snapshot.ts`, `src/core/context/visible-conversation-v2.ts`, and `src/core/context/token-budget.ts`.
13
+
14
+ ## Shared visible conversation transform
15
+
16
+ `VISIBLE_CONVERSATION_TRANSFORM_ID` is `visible-conversation-ledger-v2`. It is shared by Fusion reason and delegate seeding so each consumer gets the same disposition for the same parent session bytes.
17
+
18
+ The transform has no behavioral knobs:
19
+
20
+ - user text: retained verbatim;
21
+ - assistant text: retained verbatim;
22
+ - assistant thinking: omitted into the ledger;
23
+ - tool-call arguments: omitted into the ledger;
24
+ - tool-result text: omitted into the ledger;
25
+ - tool-result images: ledger-only;
26
+ - user images: marker-only text (`[Image omitted from fusion text transcript: <mime>]`);
27
+ - empty text blocks: counted, not serialized;
28
+ - unknown roles/block types: loud `UnsupportedConversationBlockError`.
29
+
30
+ Each omitted event records source ordinal, block ordinal, kind, payload byte length, payload SHA-256, and optional tool/mime metadata. The ledger root is hash-framed and independent of consumer envelopes, so Fusion and delegate can seal the same transform output into different schemas without changing the underlying omitted-event commitment.
31
+
32
+ ## Parent snapshot and branch exclusion
33
+
34
+ `parent-snapshot.ts` adapts Pi `SessionManager` entries into LLM messages. Tool callers can exclude the active assistant leaf that contains the in-flight tool call; this prevents the child from seeing its own request and sibling calls as completed parent history. Commands do not exclude a leaf.
35
+
36
+ Callers must snapshot once and complete downstream launch/admission from that frozen snapshot. Re-reading the parent session during launch would allow seed drift.
37
+
38
+ ## Reason input vs clean input
39
+
40
+ - Fusion `reason` uses the parent visible-conversation projection. Its canonical input includes the system prompt, request authority, projection entries, accounting, and a separate `context-omission-ledger.json` artifact.
41
+ - Fusion `investigate`, `research`, and `validate` use clean-task input. They carry the explicit request and declared sources where applicable; they intentionally do not carry parent system prompt, conversation projection, or omission ledger.
42
+ - Delegate uses the shared projection inside its seed, but its directive text is marked authoritative and projected history is supporting/untrusted.
43
+
44
+ No path silently truncates parent context. Oversized projections or prompts must be rejected with budget details or represented by hashed artifact receipts where that consumer defines a receipt protocol.
45
+
46
+ ## Token estimator concepts
47
+
48
+ `token-budget.ts` is byte arithmetic, not a tokenizer. It produces upper-bound style estimates from UTF-8 byte classes:
49
+
50
+ - `normal` known text/json bytes;
51
+ - `multibyte` bytes;
52
+ - `dense_ascii` bytes;
53
+ - `unknown_output_contract` bytes.
54
+
55
+ Families are `anthropic`, `openai-codex`, and `unknown`. Exact model overrides currently include Anthropic Opus/Fable routes and OpenAI Codex GPT routes. Unknown providers and unbacked models fall to a provable 1.00 B/token floor.
56
+
57
+ Calibration facts in code:
58
+
59
+ - calibration id: `pi-background-tasks.input-token-calibration.v1`;
60
+ - large-prompt floor: 50 KiB;
61
+ - affine fixed reserve: 512 tokens;
62
+ - observed large Fusion prompt corpus: 882 prompts, dated 2026-08-02;
63
+ - Anthropic configured rate: 1.73 B/token after haircut;
64
+ - OpenAI Codex configured rate: 2.89 B/token after haircut;
65
+ - conservative/floor rates are used for delegate scope, small prompts, unbacked models, unknown providers, capacity guards, and dense-ASCII out-of-domain cases.
66
+
67
+ The dense-ASCII gate is explicitly a low-whitespace heuristic proxy, not a tokenizer guarantee. Calibration applies only when the input is in the measured domain and the route capacity can hold that domain.
68
+
69
+ ## Budget invariants
70
+
71
+ - `allowedInputTokens()` returns a signed number and never clamps unusable routes to zero.
72
+ - Callers must reject unusable or too-small context windows before spawning children.
73
+ - Multibyte bytes cannot bypass accounting.
74
+ - Unknown output contracts are charged separately; future output cannot be assumed to be cheap.
75
+ - Rate-source warnings are part of the contract and should be surfaced in refusal details.
76
+
77
+ ## No silent truncation
78
+
79
+ Model-visible context may be compacted only by explicit policy receipts with exact accounting and hashes. It must never be clipped, head/tail previewed, route-substituted, or hidden behind a fallback estimator without reporting the source and limitation.
@@ -0,0 +1,122 @@
1
+ ---
2
+ doc_id: getting-started
3
+ audience: user
4
+ mode: authored
5
+ review_policy: contract
6
+ stability: stable
7
+ covers_surfaces: []
8
+ covers_sources: []
9
+ ---
10
+ # Getting started
11
+
12
+ This guide gets from install to useful background work in a few minutes.
13
+
14
+ ## 1. Install
15
+
16
+ ```bash
17
+ pi install npm:pi-background-tasks@latest
18
+ ```
19
+
20
+ For a project-local install:
21
+
22
+ ```bash
23
+ pi install npm:pi-background-tasks@latest -l
24
+ ```
25
+
26
+ For current repository state rather than a release tag:
27
+
28
+ ```bash
29
+ pi install git:github.com/ismailsaleekh/pi-background-tasks@main
30
+ ```
31
+
32
+ For a local checkout/package path:
33
+
34
+ ```bash
35
+ pi install .
36
+ pi install . -l
37
+ ```
38
+
39
+ ## 2. Start your first `/bg` task
40
+
41
+ Inside a project, run:
42
+
43
+ ```text
44
+ /bg --name "Docs server" npm run docs:dev
45
+ ```
46
+
47
+ `/bg` starts a tracked shell command, returns a task id, and writes output under `.pi/tasks/...`. The command is still an ordinary local shell command; the package tracks it but does not sandbox it.
48
+
49
+ ## 3. Observe completion
50
+
51
+ Use the footer dock or commands:
52
+
53
+ ```text
54
+ /jobs
55
+ /logs <task id> 20000
56
+ ```
57
+
58
+ Press **Shift↓** to open the dock when the `bg ...` footer appears. `/bg-clear` acknowledges finished-task footer notices.
59
+
60
+ ## 4. Start an agent-launched background task
61
+
62
+ When Pi itself should start a long command, use the `bg_run` tool with the strict schema:
63
+
64
+ ```json
65
+ {
66
+ "name": "Typecheck",
67
+ "command": "npm run typecheck",
68
+ "isAgent": false
69
+ }
70
+ ```
71
+
72
+ `bg_run` defaults `notifyOnCompletion:true` and `triggerOnCompletion:true`, so Pi should not sleep or poll merely to wait. The terminal notification is the wake-up path.
73
+
74
+ Set `isAgent:true` only when the shell command launches a child Pi/LLM agent, such as `pi -p ...` or `pi --mode json ...`.
75
+
76
+ ## 5. Delegate read-only investigation
77
+
78
+ Use `bg_delegate` when the worker needs the current conversation as background but should not block the parent:
79
+
80
+ ```json
81
+ {
82
+ "name": "Config audit",
83
+ "prompt": "Inspect package configuration and report where background-task output limits are defined. Include file paths and concise evidence.",
84
+ "capability": "inspect",
85
+ "autoDeliver": "never"
86
+ }
87
+ ```
88
+
89
+ The child receives a frozen visible-conversation projection, its own session id/session directory, and read/search/list tools only. It has no shell, no write/edit tools, no network, no recursive delegate, and no Fusion.
90
+
91
+ After the completion notification, retrieve the committed answer:
92
+
93
+ ```json
94
+ {
95
+ "taskId": "<task id from bg_delegate>",
96
+ "delivery": "inline"
97
+ }
98
+ ```
99
+
100
+ `bg_result` is point-in-time: a running task returns a typed not-ready state and never blocks. A committed answer is hash-verified before bytes are returned and is never silently truncated.
101
+
102
+ ## 6. Run first Fusion reasoning
103
+
104
+ For self-contained synthesis:
105
+
106
+ ```json
107
+ {"prompt":"Compare a foreground command, bg_run, and bg_delegate for a ten-minute repository audit."}
108
+ ```
109
+
110
+ Call this with `fusion_reason`, or use:
111
+
112
+ ```text
113
+ /fusion Compare a foreground command, bg_run, and bg_delegate for a ten-minute repository audit.
114
+ ```
115
+
116
+ Fusion runs three candidates, blind evaluation, optional bounded repair only if evaluator JSON is invalid, and merger. `/fusion` and `fusion_reason` receive a versioned conversation projection; investigate/research/validate receive clean task input only.
117
+
118
+ ## Next links
119
+
120
+ - [Choose a workflow](choose-a-workflow.md)
121
+ - [Configuration](operations/configuration.md)
122
+ - [README landing page](../README.md)