@llblab/pi-kit 0.26.0 → 0.27.1

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 (100) hide show
  1. package/BACKLOG.md +4 -2
  2. package/CHANGELOG.md +9 -0
  3. package/README.md +6 -6
  4. package/node_modules/@llblab/pi-claude-usage/AGENTS.md +1 -1
  5. package/node_modules/@llblab/pi-claude-usage/CHANGELOG.md +4 -0
  6. package/node_modules/@llblab/pi-claude-usage/README.md +3 -1
  7. package/node_modules/@llblab/pi-claude-usage/lib/status.ts +3 -8
  8. package/node_modules/@llblab/pi-claude-usage/lib/telegram.ts +15 -1
  9. package/node_modules/@llblab/pi-claude-usage/package.json +1 -1
  10. package/node_modules/@llblab/pi-codex-usage/AGENTS.md +1 -1
  11. package/node_modules/@llblab/pi-codex-usage/CHANGELOG.md +4 -0
  12. package/node_modules/@llblab/pi-codex-usage/README.md +2 -2
  13. package/node_modules/@llblab/pi-codex-usage/lib/status.ts +3 -11
  14. package/node_modules/@llblab/pi-codex-usage/lib/telegram.ts +15 -0
  15. package/node_modules/@llblab/pi-codex-usage/package.json +1 -1
  16. package/node_modules/@llblab/pi-state-flow/AGENTS.md +1 -1
  17. package/node_modules/@llblab/pi-state-flow/BACKLOG.md +12 -5
  18. package/node_modules/@llblab/pi-state-flow/CHANGELOG.md +9 -0
  19. package/node_modules/@llblab/pi-state-flow/README.md +116 -34
  20. package/node_modules/@llblab/pi-state-flow/dist/lib/acquisition.d.ts +24 -1
  21. package/node_modules/@llblab/pi-state-flow/dist/lib/acquisition.js +80 -1
  22. package/node_modules/@llblab/pi-state-flow/dist/lib/compaction.d.ts +17 -0
  23. package/node_modules/@llblab/pi-state-flow/dist/lib/compaction.js +40 -0
  24. package/node_modules/@llblab/pi-state-flow/dist/lib/extension.js +149 -298
  25. package/node_modules/@llblab/pi-state-flow/dist/lib/git.d.ts +29 -0
  26. package/node_modules/@llblab/pi-state-flow/dist/lib/git.js +58 -0
  27. package/node_modules/@llblab/pi-state-flow/dist/lib/operation.d.ts +37 -0
  28. package/node_modules/@llblab/pi-state-flow/dist/lib/operation.js +59 -0
  29. package/node_modules/@llblab/pi-state-flow/dist/lib/ownership.d.ts +31 -0
  30. package/node_modules/@llblab/pi-state-flow/dist/lib/ownership.js +117 -0
  31. package/node_modules/@llblab/pi-state-flow/dist/lib/protocol.js +7 -7
  32. package/node_modules/@llblab/pi-state-flow/dist/lib/query.js +1 -1
  33. package/node_modules/@llblab/pi-state-flow/dist/lib/status.d.ts +2 -0
  34. package/node_modules/@llblab/pi-state-flow/dist/lib/status.js +28 -0
  35. package/node_modules/@llblab/pi-state-flow/dist/lib/transition.js +6 -1
  36. package/node_modules/@llblab/pi-state-flow/dist/package.json +1 -1
  37. package/node_modules/@llblab/pi-state-flow/dist/skills/state-flow-guide/SKILL.md +14 -6
  38. package/node_modules/@llblab/pi-state-flow/dist/skills/state-flow-memory/SKILL.md +2 -2
  39. package/node_modules/@llblab/pi-state-flow/docs/README.md +19 -9
  40. package/node_modules/@llblab/pi-state-flow/docs/agent-contract-relocation.md +4 -4
  41. package/node_modules/@llblab/pi-state-flow/docs/architecture.md +644 -91
  42. package/node_modules/@llblab/pi-state-flow/docs/compatibility.md +116 -37
  43. package/node_modules/@llblab/pi-state-flow/docs/filesystem-recovery.md +118 -21
  44. package/node_modules/@llblab/pi-state-flow/docs/fork-contract.md +68 -8
  45. package/node_modules/@llblab/pi-state-flow/docs/lazy-state.md +88 -14
  46. package/node_modules/@llblab/pi-state-flow/docs/performance.md +83 -66
  47. package/node_modules/@llblab/pi-state-flow/docs/temporal-acceptance.md +391 -62
  48. package/node_modules/@llblab/pi-state-flow/docs/usage.md +317 -62
  49. package/node_modules/@llblab/pi-state-flow/lib/acquisition.ts +85 -1
  50. package/node_modules/@llblab/pi-state-flow/lib/compaction.ts +47 -0
  51. package/node_modules/@llblab/pi-state-flow/lib/extension.ts +161 -295
  52. package/node_modules/@llblab/pi-state-flow/lib/git.ts +63 -0
  53. package/node_modules/@llblab/pi-state-flow/lib/operation.ts +75 -0
  54. package/node_modules/@llblab/pi-state-flow/lib/ownership.ts +120 -0
  55. package/node_modules/@llblab/pi-state-flow/lib/protocol.ts +7 -8
  56. package/node_modules/@llblab/pi-state-flow/lib/query.ts +1 -1
  57. package/node_modules/@llblab/pi-state-flow/lib/runtime.ts +1 -1
  58. package/node_modules/@llblab/pi-state-flow/lib/snapshot.ts +1 -2
  59. package/node_modules/@llblab/pi-state-flow/lib/status.ts +29 -0
  60. package/node_modules/@llblab/pi-state-flow/lib/transition.ts +5 -1
  61. package/node_modules/@llblab/pi-state-flow/package.json +1 -1
  62. package/node_modules/@llblab/pi-state-flow/skills/state-flow-guide/SKILL.md +14 -6
  63. package/node_modules/@llblab/pi-state-flow/skills/state-flow-memory/SKILL.md +2 -2
  64. package/node_modules/@llblab/pi-telegram/BACKLOG.md +1 -0
  65. package/node_modules/@llblab/pi-telegram/CHANGELOG.md +10 -0
  66. package/node_modules/@llblab/pi-telegram/README.md +1 -1
  67. package/node_modules/@llblab/pi-telegram/dist/lib/bindings.d.ts +4 -1
  68. package/node_modules/@llblab/pi-telegram/dist/lib/bindings.js +16 -12
  69. package/node_modules/@llblab/pi-telegram/dist/lib/bus-follower.d.ts +1 -1
  70. package/node_modules/@llblab/pi-telegram/dist/lib/bus-follower.js +7 -1
  71. package/node_modules/@llblab/pi-telegram/dist/lib/commands.d.ts +12 -3
  72. package/node_modules/@llblab/pi-telegram/dist/lib/commands.js +137 -83
  73. package/node_modules/@llblab/pi-telegram/dist/lib/extension.js +26 -0
  74. package/node_modules/@llblab/pi-telegram/dist/lib/lifecycle.d.ts +57 -2
  75. package/node_modules/@llblab/pi-telegram/dist/lib/lifecycle.js +109 -4
  76. package/node_modules/@llblab/pi-telegram/dist/lib/model.js +2 -4
  77. package/node_modules/@llblab/pi-telegram/dist/lib/status.d.ts +3 -1
  78. package/node_modules/@llblab/pi-telegram/dist/lib/status.js +31 -1
  79. package/node_modules/@llblab/pi-telegram/dist/lib/sync.js +4 -4
  80. package/node_modules/@llblab/pi-telegram/dist/lib/threads.d.ts +1 -0
  81. package/node_modules/@llblab/pi-telegram/dist/lib/threads.js +17 -4
  82. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-retirement.d.ts +2 -2
  83. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-retirement.js +48 -12
  84. package/node_modules/@llblab/pi-telegram/dist/package.json +1 -1
  85. package/node_modules/@llblab/pi-telegram/docs/architecture.md +6 -5
  86. package/node_modules/@llblab/pi-telegram/docs/multi-instance-bus.md +2 -0
  87. package/node_modules/@llblab/pi-telegram/docs/public-api.md +2 -2
  88. package/node_modules/@llblab/pi-telegram/docs/ui-style.md +4 -0
  89. package/node_modules/@llblab/pi-telegram/lib/bindings.ts +17 -10
  90. package/node_modules/@llblab/pi-telegram/lib/bus-follower.ts +8 -2
  91. package/node_modules/@llblab/pi-telegram/lib/commands.ts +135 -97
  92. package/node_modules/@llblab/pi-telegram/lib/extension.ts +25 -0
  93. package/node_modules/@llblab/pi-telegram/lib/lifecycle.ts +140 -4
  94. package/node_modules/@llblab/pi-telegram/lib/model.ts +2 -4
  95. package/node_modules/@llblab/pi-telegram/lib/status.ts +30 -1
  96. package/node_modules/@llblab/pi-telegram/lib/sync.ts +4 -4
  97. package/node_modules/@llblab/pi-telegram/lib/threads.ts +21 -3
  98. package/node_modules/@llblab/pi-telegram/lib/workspace-retirement.ts +46 -13
  99. package/node_modules/@llblab/pi-telegram/package.json +1 -1
  100. package/package.json +5 -5
@@ -4,13 +4,18 @@
4
4
 
5
5
  **Incremental scoped context/memory compiler for Pi.**
6
6
 
7
- When enabled, State Flow maintains explicit state across requests and sessions. The agent incrementally compiles requirements, decisions, findings and source knowledge into durable memory rather than carrying every completed exchange into the next request.
7
+ When enabled, State Flow maintains explicit state across requests and sessions. Instead of carrying every completed exchange into the next request, the agent incrementally compiles requirements, decisions, findings and source knowledge into durable memory.
8
8
 
9
- Drawing on the explicit-state approach of [SKILL.state](https://arxiv.org/html/2608.26263v2), State Flow combines durable state with Pi's native conversation context. In this hybrid, **state carries continuity between user runs; Pi's native context carries the working trajectory within a run.** The conversation is not reset after each model response or tool call. Pi retains ownership of execution, session navigation and the full inspectable trace.
9
+ The idea comes from the explicit-state approach of [SKILL.state](https://arxiv.org/html/2608.26263v2), combined with Pi's native conversation context:
10
+
11
+ - **State carries continuity between user runs.**
12
+ - **Pi's native context carries the working trajectory within a run.**
13
+
14
+ The conversation is not reset after each model response or tool call. Pi keeps ownership of execution, session navigation and the full inspectable trace.
10
15
 
11
16
  ## How it works
12
17
 
13
- In Active mode, a user run starts with effective memory and a new request. The agent works through Pi's ordinary inference/tool loop, updates memory when useful information changes, and returns an ordinary answer. The next run receives the accepted state and compact recent transitions instead of the completed ordinary conversation history.
18
+ In Active mode, each user run starts with the effective memory plus the new request. The agent works through Pi's ordinary inference/tool loop, updates memory when useful information changes, and returns an ordinary answer. The next run receives the accepted state and compact recent transitions instead of the completed conversation history.
14
19
 
15
20
  ```text
16
21
  Current state + Request
@@ -22,11 +27,23 @@ Patched state + Answer
22
27
  Updated state
23
28
  ```
24
29
 
25
- Within a run, the available request, intermediate responses, tool results and steering remain in context. The initial memory head stays byte-stable: accepted changes arrive in patch results, and changing runtime notices append at the tail without moving earlier messages. Passive memory uses the same approach across ordinary user turns, rebasing at native or mode boundaries. Projection IDs distinguish current updates from retained historical results. A state patch neither rewrites the head nor discards the working trajectory. Persistent context-bearing messages from other extensions are preserved as well.
30
+ What the model sees during a run:
31
+
32
+ - The request, intermediate responses, tool results and steering stay in context.
33
+ - The initial memory head stays byte-stable. Accepted changes arrive in patch results, and changing runtime notices are appended at the tail without moving earlier messages.
34
+ - Projection IDs tell current updates apart from retained historical results.
35
+ - A state patch neither rewrites the head nor discards the working trajectory.
36
+ - Persistent context-bearing messages from other extensions are preserved.
37
+
38
+ Passive memory uses the same approach across ordinary user turns and rebases only at native or mode boundaries.
39
+
40
+ Why this helps:
26
41
 
27
- This reduces reliance on repeated model-generated summaries of an accumulating transcript. Retaining the current trajectory also allows prompt-cache reuse while the relevant prefix remains unchanged. Avoiding summary calls and repeated prompt processing can improve responsiveness; the result depends on the model, provider, workload and frequency of state changes, not a fixed latency guarantee.
42
+ - It reduces reliance on repeated model-generated summaries of a growing transcript.
43
+ - Keeping the current trajectory lets prompt caches be reused while the relevant prefix is unchanged.
44
+ - Avoiding summary calls and repeated prompt processing can improve responsiveness. The effect depends on the model, provider, workload and how often state changes; there is no fixed latency guarantee.
28
45
 
29
- Native compaction remains available for long runs. State Flow may also request a completed-history boundary without another model summary, retaining the complete latest accepted run. Neither mechanism deletes Pi's append-only session trace. See [lifecycle behavior](docs/usage.md#session-behavior) and [performance evidence](docs/performance.md).
46
+ Native compaction remains available for long runs. State Flow may also request a completed-history boundary without another model summary, keeping the complete latest accepted run. Neither mechanism deletes Pi's append-only session trace. See [lifecycle behavior](docs/usage.md#session-behavior) and [performance evidence](docs/performance.md).
30
47
 
31
48
  ## Installation and activation
32
49
 
@@ -50,22 +67,28 @@ Enable active State Flow on the current branch:
50
67
  /state-flow-active
51
68
  ```
52
69
 
53
- Starting in an existing conversation retains its context for one complete bootstrap run so the agent can compile what matters.
70
+ Starting in an existing conversation keeps its context for one complete bootstrap run, so the agent can compile what matters.
71
+
72
+ Commands:
54
73
 
55
74
  - `/state-flow-active`: Select the active, state-driven iteration workflow.
56
75
  - `/state-flow-passive`: Select ordinary conversation with both memory tools and existing-memory projection.
57
76
  - `/state-flow-off`: Remove both memory tools and all State Flow model context, without deleting memory.
58
77
  - `/state-flow-status`: Inspect effective state, retained history and known recovery issues without scanning sources or changing state.
59
78
 
60
- New sessions default to **Off**; select Passive for ordinary conversation with memory tools, or Active for state-driven episodes. Passive projects state only after a validated memory view is available. Off removes State Flow's model-facing tools and context without deleting memory. Mode changes affect only this session; the global default applies only to new sessions. See [mode semantics](docs/usage.md#active-passive-and-configured-off), [configuration](docs/usage.md#configuration) and [status and controls](docs/usage.md#status-and-controls).
79
+ New sessions default to **Off**. Choose Passive for ordinary conversation with memory tools, or Active for state-driven episodes. A mode change affects only the current session; the global default applies only to new sessions. See [mode semantics](docs/usage.md#active-passive-and-configured-off), [configuration](docs/usage.md#configuration) and [status and controls](docs/usage.md#status-and-controls).
61
80
 
62
81
  ### Active, passive, off
63
82
 
64
- - `Active`: Memory tools are available; the agent consolidates necessary final state changes before completing an iteration. Subsequent iterations use accepted state and new input rather than completed prior reasoning.
65
- - `Passive`: Both memory tools and existing-state projection remain available. Patching is on demand, and ordinary conversation context continues without State Flow's active iteration reset.
66
- - `Off`: Neither memory tool nor State Flow context—including a frozen passive handoff—is exposed to the model. Startup/reload/tree attachment does not read or restore memory; selecting Off cancels pending memory waits and records only native policy/bookmarks. Explicit Passive/Active acquires memory later. Stored memory and Pi's native trace remain intact.
83
+ - `Active`: Memory tools are available, and the agent consolidates necessary final state changes before completing an iteration. Later iterations use accepted state and new input rather than completed prior reasoning.
84
+ - `Passive`: Both memory tools are available, and existing state is projected once a validated memory view is available. The agent patches on demand, and ordinary conversation context continues without State Flow's active iteration reset.
85
+ - `Off`: The model sees neither memory tool nor any State Flow context, including a frozen passive handoff.
86
+ - Startup, reload and tree attachment do not read or restore memory.
87
+ - Selecting Off cancels pending memory waits and records only native policy/bookmarks.
88
+ - Choosing Passive or Active later acquires memory.
89
+ - Stored memory and Pi's native trace stay intact.
67
90
 
68
- Modes select agent behavior, not a different disk persistence or fork-copy mechanism. Final consolidation does not require an empty ceremonial patch, and context projection does not delete native history. See [mode semantics and bootstrap terminology](docs/usage.md#active-passive-and-configured-off).
91
+ Modes select agent behavior; they do not change disk persistence or fork copying. Final consolidation needs no empty ceremonial patch, and context projection does not delete native history. See [mode semantics and bootstrap terminology](docs/usage.md#active-passive-and-configured-off).
69
92
 
70
93
  ## State model
71
94
 
@@ -77,30 +100,51 @@ Memory has three ownership scopes:
77
100
  - `cwd`: Knowledge shared by sessions in the same working directory.
78
101
  - `session`: State belonging to the current session and its selected branch.
79
102
 
80
- They compose recursively in **global → CWD → session** order. More-specific values override lower-scope values, while object fields merge. Removing a local value can reveal an inherited value again.
103
+ They compose recursively in **global → CWD → session** order:
104
+
105
+ - More-specific values override broader ones, while object fields merge.
106
+ - Removing a local value can reveal an inherited value again.
81
107
 
82
- The agent receives the **effective view** of this composition, not three unrelated memory dumps. It can read that view or inspect an individual scope when ownership matters. `effective` is a computed view, not a fourth storage scope. Scope precedence does not elevate memory into system-level instructions.
108
+ The agent receives the **effective view** of this composition, not three unrelated memory dumps. It can read that view, or inspect one scope when ownership matters. `effective` is a computed view, not a fourth storage scope. Scope precedence does not turn memory into system-level instructions.
83
109
 
84
110
  ### Semantic planes
85
111
 
86
112
  Runtime views provide these documented planes:
87
113
 
88
- - `intents`: Active commitments to future action.
89
- - `contract`: Requirements, decisions, constraints and interface commitments.
90
- - `working`: Observations, results, uncertainties and current continuation.
114
+ - `intents`: The queue of chosen actions. Work from intents: a structured `{"$ref"}` inside an intent owns a same-scope `working`/`lazy` key, and deleting the intent deletes what it owns unless another intent still references it. Textual `$path` mentions only use; unowned entries remain legal.
115
+ - `contract`: Requirements, decisions, rejected approaches, constraints and interface commitments.
116
+ - `working`: Temporary context of those actions: observations, results and uncertainties.
91
117
  - `artifacts`: Source-addressed descriptions and compiled knowledge.
92
- - `response`: The exact latest accepted answer, including an empty string, captured by the runtime only in Session. Global/CWD receive no newly accepted answers; stored scopes may omit response entirely. Effective uses the highest-priority nonempty value.
93
- - `lazy`: Supporting memory available through explicit reads, with its body omitted from baseline model context.
118
+ - `response`: The exact latest accepted answer, including an empty string. The runtime captures it only in Session; Global/CWD receive no newly accepted answers, and stored scopes may omit it entirely. Effective uses the highest-priority nonempty value.
119
+ - `lazy`: Supporting memory available through explicit reads; its body is omitted from baseline model context.
120
+
121
+ Every plane is optional on disk:
122
+
123
+ - Stored checkpoints and patches may omit any documented plane, including `intents`, `lazy` or `response`.
124
+ - Current and historical views assemble only the known fields actually present in the selected scopes; absent fields are not filled in. Empty `response` counts as absent.
125
+ - Readers ignore unknown top-level fields, and writers emit only known fields. Nested data inside known planes is unrestricted.
126
+ - Reading or starting never rewrites data just to normalize it, and a missing field is not a storage-format error.
127
+
128
+ These planes organize ordinary JSON; there is no project-specific schema. The model updates every plane except `response`, which is runtime-owned.
94
129
 
95
- Stored checkpoints and patches may omit any documented plane, including `intents`, `lazy` or `response`. Current and historical views assemble only known fields actually present in the selected scopes, without filling absent fields. Empty `response` is treated as absent. Checkpoint and patch readers ignore unknown top-level fields; writers emit only known fields. Nested data within known planes is unrestricted. Reading or starting never rewrites data just to normalize it; missing fields are not a storage-format error.
130
+ Revisions:
96
131
 
97
- These planes organize ordinary JSON rather than imposing a project-specific schema. The model updates the semantic planes except `response`, which is runtime-owned. Global and CWD revisions are shared by their canonical stores, Session has its own revision, and Effective has no scalar owner: its identity is the `g#c#s#` vector. One atomic patch advances each materially changed scope once. Memory remains fallible: storing an observation does not make it current or correct.
132
+ - Global and CWD revisions are shared by their canonical stores; Session has its own revision.
133
+ - Effective has no single owner: its identity is the `g#c#s#` vector.
134
+ - One atomic patch advances each materially changed scope once.
98
135
 
99
- Registered Pi Skills may be compiled into source-addressed artifacts when durable guidance is useful. Pi's resource provenance determines ownership: user Skills map to global, project Skills to CWD and temporary Skills to session. A matching source hash needs no update; an uncompiled read remains ordinary volatile context and does not block unrelated patches.
136
+ Memory remains fallible: storing an observation does not make it current or correct.
137
+
138
+ Registered Pi Skills may be compiled into source-addressed artifacts when durable guidance is useful. Pi's resource provenance decides the owner: user Skills map to global, project Skills to CWD and temporary Skills to session. A matching source hash needs no update. An uncompiled read stays ordinary volatile context and does not block unrelated patches.
100
139
 
101
140
  ## Incremental updates and history
102
141
 
103
- `patch_state` updates one or more named scopes atomically. It waits cancelably for the store lock, then applies authored Global/CWD patches to current canonical values, preserving other writers' untouched fields. Overlapping assignments follow successful acceptance order; correct repeats succeed without new semantic revisions. Session remains private. Object patches merge recursively, and `null` deletes an object key rather than becoming stored data.
142
+ `patch_state` updates one or more named scopes atomically:
143
+
144
+ - It waits cancelably for the store lock, then applies authored Global/CWD patches to the current canonical values, so other writers' untouched fields survive.
145
+ - Overlapping assignments follow successful acceptance order. Correct repeats succeed without new semantic revisions.
146
+ - Session remains private.
147
+ - Object patches merge recursively, and `null` deletes an object key instead of being stored.
104
148
 
105
149
  ```json
106
150
  {
@@ -114,7 +158,7 @@ Registered Pi Skills may be compiled into source-addressed artifacts when durabl
114
158
  }
115
159
  ```
116
160
 
117
- During an active episode, a material patch is an inference barrier: sibling tool calls are blocked, and the next inference sees the accepted effective state. Ordinary completion needs no finalization patch or additional State Flow reasoning loop.
161
+ During an active episode, a material patch is an inference barrier: sibling tool calls are blocked, and the next inference sees the accepted effective state. Ordinary completion needs no finalization patch or extra State Flow reasoning loop.
118
162
 
119
163
  `read_state` provides targeted current and historical access:
120
164
 
@@ -127,29 +171,67 @@ During an active episode, a material patch is an inference barrier: sibling tool
127
171
  - `cwd.patches[0]`: The latest retained CWD semantic patch.
128
172
  - `effective.lazy.memory[0..3]`: A bounded slice of a stored collection.
129
173
 
130
- Historical materializations and scope patch histories use the configurable **`historyLimit`**, from **0 to 100**, with a default of **7**. Materialized offsets refer to accepted semantic transitions, not user messages or an independent counter for each scope. Requested history must still exist in the active lineage; increasing the limit cannot recreate discarded history. Older patches fold into the checkpoint without removing current values.
174
+ How much history is kept:
175
+
176
+ - Historical materializations and scope patch histories use the configurable **`historyLimit`**, from **0 to 100**, default **7**.
177
+ - Offsets count accepted semantic transitions, not user messages or a separate counter per scope.
178
+ - Requested history must still exist in the active lineage; raising the limit cannot recreate discarded history.
179
+ - Older patches fold into the checkpoint without removing current values.
131
180
 
132
- Array ranges, structural `keys` reads and path-intersected `patch` projections support progressive access without loading whole memory collections. See [progressive memory](docs/lazy-state.md) and [tool contracts](docs/architecture.md#model-tools).
181
+ Array ranges, structural `keys` reads and path-intersected `patch` projections let the agent explore memory without loading whole collections. See [progressive memory](docs/lazy-state.md) and [tool contracts](docs/architecture.md#model-tools).
133
182
 
134
183
  ## Persistence, backups and continuity
135
184
 
136
- The default store is `~/.pi/agent/state-flow/`, independent of registered source files. Canonical `checkpoint.json`, `patches.jsonl` and `meta.json` files hold each scope's state, retained changes and metadata; session configuration and runtime identity are stored separately.
185
+ The default store is `~/.pi/agent/state-flow/`, independent of registered source files. Each scope keeps its state, retained changes and metadata in canonical `checkpoint.json`, `patches.jsonl` and `meta.json` files. Session configuration and runtime identity are stored separately.
186
+
187
+ **Persistence is optimistic across abrupt shutdown.**
188
+
189
+ - Short publication exclusion keeps independent shared fields from cooperating writers; on overlap, the last accepted write wins.
190
+ - Per-file atomic replacement does not guarantee power-loss survival or crash-atomic recovery.
191
+ - This is an [accepted limitation](docs/filesystem-recovery.md#power-loss-durability), not a release gate; no additional recovery journal or storage format is planned.
192
+
193
+ **Git backups are optional.** When the store is a configured Git repository:
194
+
195
+ - Accepted active turns may create versioned backups of State Flow-owned files.
196
+ - If the store is busy and Pi provides no cancellable settlement wait, the backup is explicitly deferred. A later accepted turn retries, without changing memory or blocking native Abort.
197
+ - If the attached branch has an explicitly configured remote, State Flow then pushes the exact current backup commit there, asynchronously and without force.
198
+ - Within one Pi process, at most one push per repository runs at a time. Overlapping attempts are skipped, and a later accepted turn pushes the latest backup.
199
+ - Off cancels pending captures and its admitted push, without rolling back accepted state or backup commits; canceled work emits no late memory warnings.
200
+ - Shutdown cancels owned pushes and waits for that repository's active push to close or time out.
201
+ - Commit failures warn locally. Repeated push failures produce one concise warning until a push succeeds, with redacted Git detail kept in the local diagnostic log. Neither failure rejects or rolls back accepted memory.
202
+ - Backup needs a Git commit identity; accepting and persisting state does not.
203
+ - Git history can be inspected separately, but it is not the authority for `read_state` or for automatic restoration of expired semantic boundaries.
204
+
205
+ **Resume, tree navigation and forks:**
206
+
207
+ - Memory-enabled resume and tree navigation restore the selected retained session boundary over the current shared global/CWD memory. Off defers this acquisition.
208
+ - A new session gets its own session layer.
209
+ - Supported native forks copy the selected session state into a new owner without changing the parent's private data.
210
+ - Expired, incomplete or contradictory boundaries never silently substitute newer private state during restoration.
211
+ - Explicit Start is a mode change, not historical restoration. It activates the validated **current** memory of that same session, including private state, even after expired active/passive, interrupted or pre-runtime selections. Available aligned history and revisions survive; unavailable history is not recreated.
212
+ - Malformed storage, unsafe fork copying and concurrent writes remain fenced.
213
+
214
+ See [fork support](docs/usage.md#fork-support-and-limits) and [storage recovery](docs/usage.md#storage-and-recovery).
137
215
 
138
- **Persistence is optimistic across abrupt shutdown.** Short publication exclusion preserves independent shared fields between cooperating writers; the last accepted write wins on overlap. Per-file atomic replacement does not guarantee power-loss survival or crash-atomic recovery. That [accepted limitation](docs/filesystem-recovery.md#power-loss-durability) is not a release gate; no additional recovery journal or storage format is planned.
216
+ **For SDK and launcher integrations:**
139
217
 
140
- **Git backups are optional.** When the store is a configured Git repository, accepted active turns may create versioned backups of State Flow-owned files. A busy store explicitly defers backup when Pi provides no cancellable settlement wait (including SDK 0.87); a later accepted turn retries without changing memory or blocking native Abort. If the attached branch has an explicitly configured remote, State Flow then pushes the exact current backup commit there asynchronously and without force. Within one Pi process, only one push per repository can run at a time; overlapping attempts are skipped and a later accepted turn pushes the latest backup. Off cancels pending captures and its admitted push without rolling back accepted state or backup commits; canceled work emits no late memory warnings. Shutdown cancels owned pushes and waits for that repository's active push to close or time out. Commit failures warn locally; repeated push failures produce one concise warning until a successful push, with redacted Git detail kept in the local diagnostic log. Neither failure rejects or rolls back accepted memory. Backup needs a Git commit identity, but accepting and persisting state does not. Git history can be inspected separately, but it is not the authority for `read_state` or automatic restoration of expired semantic boundaries.
218
+ - [Advisory continuation APIs](docs/architecture.md#session-continuation) inspect provenance and build candidates asynchronously with host cancellation. They neither open native sessions nor install automatic resume.
219
+ - Run preparation and missing-artifact maintenance wait cancelably before inference; embeddings can also use the [transaction APIs](docs/architecture.md#asynchronous-storage-transaction).
141
220
 
142
- Memory-enabled resume and tree navigation restore the selected retained session boundary over current shared global/CWD memory; Off defers this acquisition. A new session gets its own session layer. Supported native forks copy selected session state into a new owner without changing the parent's private data. Expired, incomplete or contradictory boundaries never silently substitute newer private state during restoration. Explicit Start is a mode change, not historical restoration: it activates validated **current** memory of that same session, including private state, even after expired active/passive, interrupted or pre-runtime selections. Available aligned history and revisions survive; unavailable history is not recreated. Malformed storage, unsafe fork copying and concurrent writes remain fenced. See [fork support](docs/usage.md#fork-support-and-limits) and [storage recovery](docs/usage.md#storage-and-recovery).
221
+ How mode changes interact with storage:
143
222
 
144
- SDK/launcher integrations can use [advisory continuation APIs](docs/architecture.md#session-continuation). Provenance inspection and candidate building are now asynchronous and accept host cancellation; they neither open native sessions nor install automatic resume. Run preparation and missing-artifact maintenance now wait cancelably before inference; embeddings can also use the [transaction APIs](docs/architecture.md#asynchronous-storage-transaction). Selecting Passive switches local policy/context immediately and awaits runtime-only persistence. Off instead cancels owned memory waits and saves only native mode/continuation/fork bookkeeping, without validating or rewriting canonical storage. Explicit Off inspection may read current stored values through a disposable validated reader; it does not activate memory, install cache or change the selected historical/fork boundary. Missing private authority remains unavailable. Active waits for coherent capture/acceptance; another mode or selection can withdraw its wait. Passive retains independently owned restoration/fork work; Off cancels it and defers later acquisition. Failed Passive persistence preserves local policy and fences publication until accepted activation.
223
+ - Selecting Passive switches local policy/context immediately and awaits runtime-only persistence. Passive keeps independently owned restoration/fork work. If its persistence fails, local policy stays and publication is fenced until an accepted activation.
224
+ - Off cancels owned memory waits and saves only native mode/continuation/fork bookkeeping, without validating or rewriting canonical storage. It also cancels restoration/fork work and defers later acquisition.
225
+ - Explicit Off inspection may read current stored values through a disposable validated reader. It does not activate memory, install cache or change the selected historical/fork boundary. Missing private authority stays unavailable.
226
+ - Active waits for a coherent capture and acceptance; another mode or selection can withdraw its wait.
145
227
 
146
- The canonical storage-format boundary introduced in 0.17 still applies: current versions accept only the canonical store contract and provide no in-place predecessor converter. Preserve existing data and check the [format boundary](docs/usage.md#moving-a-store-and-the-017-format-boundary) before changing versions or moving a store.
228
+ State Flow accepts only the canonical store contract and provides no in-place format converter. Preserve existing data and check the [format boundary](docs/usage.md#moving-a-store-and-supported-formats) before changing versions or moving a store.
147
229
 
148
230
  ## Operational boundaries
149
231
 
150
- State Flow adds memory, not another agent controller. It does not introduce background reasoning, a scheduler, automatic reference hydration or rollback of external tool effects. State and the current trajectory are not size-capped; performance depends on how much useful information the agent retains.
232
+ State Flow adds memory, not another agent controller. It does not add background reasoning, a scheduler, automatic reference hydration or rollback of external tool effects. State and the current trajectory are not size-capped; performance depends on how much useful information the agent retains.
151
233
 
152
- The packaged `state-flow-guide` Skill covers concrete operations and recovery. `state-flow-memory` supports explicitly requested curation, for example: **“Review and clean State Flow state”** Normal handoffs reconcile touched memory; dedicated cleanup is not an automatic audit after each task.
234
+ The packaged `state-flow-guide` Skill covers concrete operations and recovery. `state-flow-memory` supports explicitly requested curation, for example: **“Review and clean State Flow state”** Normal handoffs reconcile the memory they touch; dedicated cleanup is not an automatic audit after each task.
153
235
 
154
236
  Treat state, diagnostic logs and backups as private data. Revalidate consequential observations before acting, and verify a transfer's destination before deleting its source. Removing a value from current state does not erase older histories or remote copies.
155
237
 
@@ -1,4 +1,5 @@
1
- import { type ArtifactInvalidationReason, type ArtifactInvalidationRequest, type ArtifactSourceIdentity } from "./artifact.ts";
1
+ import { type ArtifactInvalidationReason, type ArtifactInvalidationRequest, type ArtifactSourceIdentity, type ArtifactProvenanceRegistry } from "./artifact.ts";
2
+ import type { AtomicScopePatches, ScopedSemanticStates, ScopedStates, StateScope } from "./state.ts";
2
3
  /** Why the caller is considering source-body acquisition. */
3
4
  export type ArtifactAcquisitionIntent = "routine" | "new-session" | "relevant-gap" | "exact-source" | "exact-edit" | "contradiction-or-failure" | "explicit-request" | "maintenance";
4
5
  export type ArtifactAcquisitionReason = ArtifactInvalidationReason | "materialized-gap" | "exact-source" | "exact-edit" | "contradiction-or-failure" | "explicit-request" | "maintenance";
@@ -37,3 +38,25 @@ export declare class ArtifactReadTracker {
37
38
  * stay on materialized state; only a concrete source need permits rereading.
38
39
  */
39
40
  export declare function decideArtifactAcquisition(source: ArtifactSourceIdentity, metadata: unknown, compiler: string, options: ArtifactAcquisitionOptions): ArtifactAcquisitionDecision;
41
+ export declare const SOURCE_CHANGED_HINT = "Source changed since this artifact was compiled. Read and recompile it before relying on it.";
42
+ /**
43
+ * One selected branch's ordinary-artifact acquisition plan: runtime-observed
44
+ * invalidations, their model hints and the read tracker correlated to them.
45
+ * The tracker's candidates always equal the current invalidation plan.
46
+ */
47
+ export declare class ArtifactAcquisitionState {
48
+ #private;
49
+ readonly reads: ArtifactReadTracker;
50
+ get invalidations(): readonly ArtifactInvalidationRequest[];
51
+ get hints(): Record<string, string>;
52
+ /** Drop the invalidation plan; hints remain until the next refresh. */
53
+ clearInvalidations(): void;
54
+ /** Forget plan and hints when no memory view is selected. */
55
+ reset(): void;
56
+ /** Re-observe exact registered sources for the selected scope artifacts. */
57
+ refresh(states: ScopedStates, provenance: (scope: StateScope) => ArtifactProvenanceRegistry): void;
58
+ /** Accepted compilations leave the plan; correlated read evidence is single-use. */
59
+ acceptAcquired(paths?: ReadonlySet<string>): void;
60
+ }
61
+ /** Deletion patches for registered artifacts whose exact source is observed missing, in every owning scope. */
62
+ export declare function missingArtifactRemovals(states: ScopedSemanticStates): AtomicScopePatches;
@@ -1,4 +1,4 @@
1
- import { classifyArtifactCompilationNeed, inspectRegisteredArtifactPaths, sameArtifactSourceFingerprint, } from "./artifact.js";
1
+ import { classifyArtifactCompilationNeed, inspectRegisteredArtifactPaths, sameArtifactSourceFingerprint, ORDINARY_ARTIFACT_COMPILER, } from "./artifact.js";
2
2
  import { isObject } from "./json.js";
3
3
  function readPath(toolName, args) {
4
4
  return toolName === "read" && isObject(args) && typeof args.path === "string"
@@ -89,3 +89,82 @@ export function decideArtifactAcquisition(source, metadata, compiler, options) {
89
89
  return { kind: "read-source", reason: "maintenance" };
90
90
  }
91
91
  }
92
+ export const SOURCE_CHANGED_HINT = "Source changed since this artifact was compiled. Read and recompile it before relying on it.";
93
+ const SCOPES = ["global", "cwd", "session"];
94
+ /**
95
+ * One selected branch's ordinary-artifact acquisition plan: runtime-observed
96
+ * invalidations, their model hints and the read tracker correlated to them.
97
+ * The tracker's candidates always equal the current invalidation plan.
98
+ */
99
+ export class ArtifactAcquisitionState {
100
+ reads = new ArtifactReadTracker();
101
+ #invalidations = [];
102
+ #hints = {};
103
+ get invalidations() { return this.#invalidations; }
104
+ get hints() { return this.#hints; }
105
+ /** Drop the invalidation plan; hints remain until the next refresh. */
106
+ clearInvalidations() { this.#setInvalidations([]); }
107
+ /** Forget plan and hints when no memory view is selected. */
108
+ reset() {
109
+ this.#hints = {};
110
+ this.#setInvalidations([]);
111
+ }
112
+ /** Re-observe exact registered sources for the selected scope artifacts. */
113
+ refresh(states, provenance) {
114
+ const paths = new Set();
115
+ for (const scope of SCOPES)
116
+ for (const path of Object.keys(states[scope].artifacts))
117
+ paths.add(path);
118
+ const observations = new Map(inspectRegisteredArtifactPaths(paths).map((observation) => [observation.path, observation]));
119
+ const hints = {};
120
+ const invalidations = new Map();
121
+ for (const scope of SCOPES) {
122
+ const registry = provenance(scope);
123
+ for (const [path, metadata] of Object.entries(states[scope].artifacts)) {
124
+ // A narrower owner replaces broader evidence for the same path.
125
+ delete hints[path];
126
+ invalidations.delete(path);
127
+ if (metadata.kind === "skill")
128
+ continue;
129
+ const observed = observations.get(path);
130
+ if (observed?.kind !== "present")
131
+ continue;
132
+ const need = classifyArtifactCompilationNeed({ path, scope, sourceFingerprint: observed.fingerprint }, metadata, ORDINARY_ARTIFACT_COMPILER, false, registry[path]);
133
+ if (need.kind !== "requires-compilation")
134
+ continue;
135
+ if (need.reason === "source-changed")
136
+ hints[path] = SOURCE_CHANGED_HINT;
137
+ invalidations.set(path, { path, scope, reason: need.reason });
138
+ }
139
+ }
140
+ this.#hints = hints;
141
+ this.#setInvalidations([...invalidations.values()].sort((left, right) => left.path.localeCompare(right.path)));
142
+ }
143
+ /** Accepted compilations leave the plan; correlated read evidence is single-use. */
144
+ acceptAcquired(paths = new Set(this.reads.successful.keys())) {
145
+ this.#setInvalidations(this.#invalidations.filter(({ path }) => !paths.has(path)));
146
+ this.reads.clear();
147
+ }
148
+ #setInvalidations(invalidations) {
149
+ this.#invalidations = invalidations;
150
+ this.reads.setCandidates(invalidations);
151
+ }
152
+ }
153
+ /** Deletion patches for registered artifacts whose exact source is observed missing, in every owning scope. */
154
+ export function missingArtifactRemovals(states) {
155
+ const owners = new Map();
156
+ for (const scope of SCOPES)
157
+ for (const path of Object.keys(states[scope].artifacts ?? {})) {
158
+ owners.set(path, [...owners.get(path) ?? [], scope]);
159
+ }
160
+ const removals = {};
161
+ for (const observation of inspectRegisteredArtifactPaths(owners.keys())) {
162
+ if (observation.kind !== "missing")
163
+ continue;
164
+ for (const scope of owners.get(observation.path) ?? []) {
165
+ const artifacts = (removals[scope]?.artifacts ?? {});
166
+ removals[scope] = { ...(removals[scope] ?? {}), artifacts: { ...artifacts, [observation.path]: null } };
167
+ }
168
+ }
169
+ return removals;
170
+ }
@@ -47,4 +47,21 @@ export declare function stateFlowCompactionResult(plan: StateFlowCompactionPlan,
47
47
  }): CompactionResult<StateFlowCompactionDetails> | {
48
48
  cancel: true;
49
49
  } | undefined;
50
+ type CompactionEvent = Parameters<typeof stateFlowCompactionResult>[2];
51
+ /** Completed-history compaction request state owned by one extension instance. */
52
+ export declare class StateFlowCompactionRequests {
53
+ #private;
54
+ get inFlight(): boolean;
55
+ get stopped(): boolean;
56
+ /** Permanently refuse owned requests (shutdown). */
57
+ stop(): void;
58
+ /** Drop the run-local plan; a stale callback can no longer clear a newer one. */
59
+ clear(): void;
60
+ /** Install a plan and return its unique per-request marker. */
61
+ begin(plan: StateFlowCompactionPlan): string;
62
+ /** Native completion clears only its own request. */
63
+ finish(marker: string): void;
64
+ /** Answer session_before_compact: foreign requests pass, stale or unpermitted owned ones cancel. */
65
+ resolve(event: CompactionEvent, permitted: boolean): ReturnType<typeof stateFlowCompactionResult>;
66
+ }
50
67
  export {};
@@ -1,3 +1,4 @@
1
+ import { randomUUID } from "node:crypto";
1
2
  import { estimateTokens } from "@earendil-works/pi-coding-agent";
2
3
  export const STATE_FLOW_COMPACTION_SUMMARY = "State Flow accepted the completed work before this boundary. Current memory is restored from its retained semantic boundary and projected separately; use the retained native entries for subsequent work.";
3
4
  /** A modest margin above Pi's default 20k retained suffix absorbs estimation drift. */
@@ -65,3 +66,42 @@ export function stateFlowCompactionResult(plan, marker, event) {
65
66
  details: structuredClone(plan.details),
66
67
  };
67
68
  }
69
+ const STATE_FLOW_COMPACTION_MARKER_PREFIX = "state-flow-boundary:";
70
+ /** Completed-history compaction request state owned by one extension instance. */
71
+ export class StateFlowCompactionRequests {
72
+ #plan;
73
+ #marker;
74
+ #inFlight = false;
75
+ #stopped = false;
76
+ #prefix = `${STATE_FLOW_COMPACTION_MARKER_PREFIX}${randomUUID()}:`;
77
+ get inFlight() { return this.#inFlight; }
78
+ get stopped() { return this.#stopped; }
79
+ /** Permanently refuse owned requests (shutdown). */
80
+ stop() { this.#stopped = true; }
81
+ /** Drop the run-local plan; a stale callback can no longer clear a newer one. */
82
+ clear() {
83
+ this.#plan = undefined;
84
+ this.#inFlight = false;
85
+ }
86
+ /** Install a plan and return its unique per-request marker. */
87
+ begin(plan) {
88
+ this.#plan = plan;
89
+ const marker = this.#marker = `${this.#prefix}${randomUUID()}`;
90
+ this.#inFlight = true;
91
+ return marker;
92
+ }
93
+ /** Native completion clears only its own request. */
94
+ finish(marker) {
95
+ if (this.#marker === marker)
96
+ this.clear();
97
+ }
98
+ /** Answer session_before_compact: foreign requests pass, stale or unpermitted owned ones cancel. */
99
+ resolve(event, permitted) {
100
+ if (event.reason !== "manual" || !event.customInstructions?.startsWith(STATE_FLOW_COMPACTION_MARKER_PREFIX))
101
+ return undefined;
102
+ const marker = this.#marker;
103
+ if (!permitted || this.#stopped || !this.#plan || !marker || event.customInstructions !== marker)
104
+ return { cancel: true };
105
+ return stateFlowCompactionResult(this.#plan, marker, event);
106
+ }
107
+ }