pi-background-tasks 0.7.7 → 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 +234 -385
  4. package/TESTING.md +15 -9
  5. package/TEST_PLAN.md +46 -13
  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 +29 -6
  53. package/src/core/delegate/budget.ts +1 -1
  54. package/src/core/delegate/launch.ts +6 -0
  55. package/src/core/fusion/artifacts.ts +80 -5
  56. package/src/core/fusion/budget.ts +129 -28
  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 +33 -6
  61. package/src/core/fusion/evaluation.ts +392 -15
  62. package/src/core/fusion/orchestrator.ts +274 -25
  63. package/src/core/fusion/pi-child.ts +635 -10
  64. package/src/core/fusion/prompts.ts +167 -6
  65. package/src/core/fusion/source-policy.ts +257 -0
  66. package/src/core/fusion/types.ts +232 -5
  67. package/src/core/fusion/web-fetch.ts +993 -0
  68. package/src/core/fusion/workflows.ts +184 -0
  69. package/src/extension.ts +3 -3
  70. package/src/fusion-child-extension.ts +370 -54
  71. package/src/fusion-extension.ts +625 -125
  72. package/src/testing/normalize.ts +0 -22
@@ -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)