@llblab/pi-state-flow 0.2.2 → 0.3.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/AGENTS.md +5 -3
- package/BACKLOG.md +1 -2
- package/CHANGELOG.md +14 -0
- package/README.md +23 -6
- package/lib/terminal.ts +12 -3
- package/package.json +1 -1
package/AGENTS.md
CHANGED
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
- Preserve Pi's native tool loop and complete inspectable session trace; project completed-run history only at user-run boundaries. Retain persistent and current-run context-bearing custom messages from other extensions, plus the complete current-run trajectory, except State Flow's own validation feedback when it is represented separately.
|
|
6
6
|
- Expose one fully materialized state with exactly `contract`, `working`, and `response`; the first two are flexible memory objects and the third is the latest complete user-facing answer string.
|
|
7
7
|
- Require no State Flow patch on tool-bearing assistant responses; retain the current run's user message, assistant tool calls, and tool results until one terminal response commits state.
|
|
8
|
-
-
|
|
8
|
+
- Accept a non-empty ordinary terminal answer without a State Flow comment as an empty memory patch: preserve `contract` and `working`, replace `response`, and use the same staging, Skill-compilation validation, and commit lifecycle. Explicit comment markers (including malformed or embedded ones) must not silently fall back to ordinary text. When a patch is present, require one top-level transcript-private `<!-- state_flow … -->` comment containing only `contract` and `working`, one separating blank line, then the user-facing answer exactly once. At `message_end`, remove the comment and stage the transition; at `turn_end`, reconcile `response` with Pi's finalized assistant message after all chained handlers by concatenating finalized text blocks without inserted characters, then commit. Route missing text or a tool call added after terminal validation through the same bounded regeneration chain. Transcript-private does not mean confidential during streaming.
|
|
9
9
|
- Treat terminal state as a decision-relevant handoff, not narration: compile stable requirements, decisions, rejected approaches, interface commitments, and reusable operational knowledge into `contract`; retain verified facts, artifacts, validation, failures, current domain state, unresolved work, and exact continuation in `working`.
|
|
10
|
+
- Preserve active constraints, unresolved questions, consequential negative results, and the next discriminating check before compression. Distinguish observations, user requirements, assistant decisions, and hypotheses; do not promote assistant conclusions to user requirements. Retain useful source locators and validity conditions for consequential facts without mandatory per-value metadata. Keep rejection reasons and reconsideration conditions. Reconcile contradictions through evidence or user clarification instead of silently overwriting established constraints or observations; retain unresolved conflicts and decision-relevant hypotheses as uncertain. These are protocol obligations, not deterministic semantic validation gates.
|
|
11
|
+
- Treat `working` as last observations, not a live workspace. Revalidate volatile facts before consequential actions; after interruption or branch navigation inspect relevant external effects before repeating operations. Failed state commits and restored memory do not undo tool effects. Missing evidence proves neither success nor absence of effects: retain uncertainty and the next check. Keep revalidation targeted, without routine Skill rereads, action ledgers, or runtime freshness/rollback guarantees.
|
|
10
12
|
- Treat each successful `SKILL.md` read as episode-level acquisition using the finalized tool-execution arguments after mutable interception: require a non-empty `contract.compiled_skills[exactReadPath]`, keep its nested representation fully flexible without `coverage`/`rules` schema, reject the terminal commit when missing, treat complete matching compilations as authoritative, and forbid routine rereads without an uncovered detail, incomplete compilation, concrete source-change evidence, reconciliation failure, or explicit user request.
|
|
11
|
-
- Make the model audit and optimize complete state at every handoff: reorganize inefficient structure, merge fragmented facts, compress history into conclusions, and delete stale, completed, redundant,
|
|
12
|
-
- Accept empty `contract` and `working` patches when nothing future-relevant changed and memory is already efficient; never force invented bookkeeping. Always require `response`. Semantic usefulness and optimization remain protocol-owned because deterministic validation cannot prove them.
|
|
13
|
+
- Make the model audit and optimize complete state at every handoff: reorganize inefficient structure, merge fragmented facts, compress history into conclusions, and delete stale, completed, redundant, or low-value keys while preserving active commitments and evidence.
|
|
14
|
+
- Accept empty `contract` and `working` patches when nothing future-relevant changed and memory is already efficient; never force invented bookkeeping. The comment may be omitted for a no-op memory patch. Always require `response`. Semantic usefulness and optimization remain protocol-owned because deterministic validation cannot prove them.
|
|
13
15
|
- Keep validation retries hidden from finalized user-facing messages; expose diagnostic detail only through transcript-private feedback and persisted runtime state, while documenting that streaming observers can still see generated terminal envelopes. Tool-bearing retry turns must preserve cumulative validation-attempt accounting. If retries are exhausted, aborted, or cannot continue before the agent settles, keep State Flow enabled and the last committed state intact; abandon only the transient validation chain so the next user run rotates normally.
|
|
14
16
|
- Recursively materialize patches immediately; empty objects preserve, nested object-key `null` deletes, and `null` anywhere in materialized state including arrays is invalid.
|
|
15
17
|
- Do not impose project schemas, state or patch byte caps, dynamic growth pressure, observation envelopes, action authorization, action ledgers, or state-size limits.
|
package/BACKLOG.md
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
# Backlog
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
- Add optional addressable evidence retrieval when benchmark failures show irreversible premature projection.
|
|
3
|
+
No planned work.
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.0: Optional memory patches and evidence-aware handoffs
|
|
4
|
+
|
|
5
|
+
- Require targeted revalidation of volatile observations and inspection of external effects after interruption or branch navigation; missing memory proves neither success nor absence of effects. Document the absence of rollback/freshness guarantees and preserve the restriction on routine Skill rereads. Add instruction-presence tests, not semantic or integration guarantees.
|
|
6
|
+
- Strengthen the handoff protocol with evidence-aware continuation, requirement/observation/hypothesis distinctions, conditional rejections, and contradiction reconciliation; preserve decision-relevant uncertainty during optimization. Add instruction-presence regressions, not semantic guarantees. Increase the protocol-only character budget to 3,700 (3,900 with bootstrap) for these obligations; state remains uncapped.
|
|
7
|
+
- Accept ordinary non-empty terminal answers as empty memory patches: preserve memory and update the response without unnecessary regeneration, including answers split across text blocks.
|
|
8
|
+
- Keep malformed explicit envelopes and missing Skill compilations on the existing bounded validation path.
|
|
9
|
+
- Clear the backlog; documented integration and continuation-quality limitations remain, not release commitments.
|
|
10
|
+
|
|
11
|
+
## 0.2.3: Document protocol and validation limitations
|
|
12
|
+
|
|
13
|
+
- Document that duplicate-comment detection also rejects literal State Flow envelope examples inside fenced or inline code in terminal answers, and provide a plain-JSON workaround.
|
|
14
|
+
- Clarify that mock lifecycle tests are not real Pi scheduler integration checks and that structural validation cannot guarantee semantic memory retention.
|
|
15
|
+
- No runtime behavior changes.
|
|
16
|
+
|
|
3
17
|
## 0.2.2: README structure hotfix
|
|
4
18
|
|
|
5
19
|
- Separate installation from usage, remove the redundant quick-start section, and move architecture details after the user-facing documentation.
|
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ State Flow preserves Pi's native tool loop inside each user request:
|
|
|
11
11
|
```text
|
|
12
12
|
user request
|
|
13
13
|
→ model → tool → model → tool → model
|
|
14
|
-
→ one terminal patch
|
|
14
|
+
→ one terminal answer (memory patch when needed)
|
|
15
15
|
→ committed state + displayed response
|
|
16
16
|
```
|
|
17
17
|
|
|
@@ -43,7 +43,7 @@ Pi packages execute with full user permissions; review the source before install
|
|
|
43
43
|
|
|
44
44
|
In a fresh session, the next prompt starts the first stateful run. Each later non-retry user prompt becomes the current turn-stable specification while committed state remains intact. When enabled inside an existing session, the first complete run retains the active pre-Flow context and must migrate every future-relevant fact into its terminal handoff.
|
|
45
45
|
|
|
46
|
-
The extension registers no model tools. Tool-bearing assistant responses use ordinary Pi tools and contain no State Flow patch.
|
|
46
|
+
The extension registers no model tools. Tool-bearing assistant responses use ordinary Pi tools and contain no State Flow patch. When memory changes, a terminal assistant response contains one transcript-private memory-patch comment, one separating blank line, and the user-facing answer exactly once. When memory is unchanged, an ordinary non-empty answer is sufficient: the runtime treats the missing comment as an empty memory patch, preserving `contract` and `working` while replacing `response`. Transcript-private means removed from the finalized message and later model context, not confidential during streaming:
|
|
47
47
|
|
|
48
48
|
```html
|
|
49
49
|
<!-- state_flow {"contract":{...},"working":{...}} -->
|
|
@@ -51,7 +51,7 @@ The extension registers no model tools. Tool-bearing assistant responses use ord
|
|
|
51
51
|
Complete user-facing answer
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
At `message_end`,
|
|
54
|
+
At `message_end`, an explicit private comment must start the single terminal text block and be followed by exactly one blank line. Without a State Flow comment marker, ordinary text blocks are concatenated without inserted characters and preserved unchanged; no regeneration is triggered merely because the comment is absent. Malformed, incomplete, or embedded State Flow comment markers still fail explicit-envelope validation rather than silently becoming no-op patches. It removes only the comment and separator and stages all three state fields. At `turn_end`, it reconciles `response` with Pi's finalized assistant message after all chained `message_end` handlers, concatenating any text blocks without inserting characters, then commits. If a later handler removes all answer text or adds a tool call after terminal validation, the same hidden regeneration chain runs. Arbitrary response Markdown remains outside the HTML comment, so its own content cannot terminate the private frame. Invalid terminal commits are regenerated through hidden feedback up to three times. If all retries fail, State Flow remains enabled, preserves the last committed snapshot, abandons only the transient validation chain, and lets the next user request start cleanly from that state. Tool-bearing turns do not reset the attempt count. Aborting a regeneration has the same non-destructive behavior.
|
|
55
55
|
|
|
56
56
|
Commands:
|
|
57
57
|
|
|
@@ -97,15 +97,29 @@ State and patch size have no byte, growth, pressure, or project-schema limit. Pa
|
|
|
97
97
|
|
|
98
98
|
These three fields are the complete **materialized State Flow state**, but they are not the only state that can shape model behavior. A situational fourth, exogenous state lives outside the handoff: the current project, workspace, tools, processes, and runtime environment. It can change while work is in progress—including through the model's own tool effects—and later observations of those changes can alter subsequent behavior. State Flow neither snapshots nor rolls back this external state; `working` should retain only the decision-relevant facts needed to reconnect the next run to it.
|
|
99
99
|
|
|
100
|
+
### Reconnecting to external state
|
|
101
|
+
|
|
102
|
+
Treat `working` as the last observation, not a live workspace. Before consequential actions, revalidate the volatile facts that action depends on: for example, the current revision and dirty files before editing, or the remote publication status before retrying a release. This is targeted inspection, not a requirement to reread stable knowledge or compiled Skills routinely.
|
|
103
|
+
|
|
104
|
+
After an interruption or session-branch navigation, inspect relevant external effects before repeating operations. A failed terminal commit does not undo file edits, running processes, or remote requests; restoring older memory does not restore the workspace. Missing memory is evidence of neither success nor absence of effects. If the effect cannot be verified, retain that uncertainty and the next discriminating check instead of blindly retrying or claiming completion.
|
|
105
|
+
|
|
106
|
+
These are protocol obligations, not runtime freshness checks, rollback, or exactly-once execution guarantees. No action ledger or new evidence-retrieval tool is introduced.
|
|
107
|
+
|
|
100
108
|
## Terminal handoff quality
|
|
101
109
|
|
|
102
|
-
The terminal
|
|
110
|
+
The terminal answer commits a handoff, not a progress phrase or transcript summary. A memory patch is required when future-relevant memory changes; omitting it is only shorthand for preserving existing memory, not a way to infer new memory from prose. The next run may see only this state plus its new user prompt. A fresh model should be able to continue without rereading, rediscovering, re-deriving decisions, or repeating failed approaches.
|
|
103
111
|
|
|
104
112
|
Useful handoffs capture:
|
|
105
113
|
|
|
106
114
|
- Stable requirements, constraints, decisions, rejected approaches, and compiled operational rules in `contract`.
|
|
107
115
|
- Verified facts, changed artifacts, validation evidence, failures, unresolved work, current domain state, and exact continuation in `working`.
|
|
108
116
|
|
|
117
|
+
Before compressing, preserve active constraints, unresolved questions, consequential negative results, and the next check that would distinguish competing explanations. Keep observations, user requirements, assistant decisions, and hypotheses distinguishable; an assistant conclusion is not a user requirement. Retain decision-relevant hypotheses as uncertain rather than deleting them merely because they are unverified.
|
|
118
|
+
|
|
119
|
+
For consequential facts, include a compact source locator and validity condition when useful (for example, a test command and the revision it checked), not mandatory metadata on every value. Record why an approach was rejected and what would justify reconsidering it. When new information conflicts with an established constraint or observation, reconcile it using evidence or user clarification; neither an unsupported new claim nor fallible old memory wins automatically. If unresolved, preserve the conflict and the next discriminating check.
|
|
120
|
+
|
|
121
|
+
These are model obligations, not semantic validation gates. Protocol tests check that the instructions remain present; only continuation evaluations can establish whether a fresh agent makes the next correct decision.
|
|
122
|
+
|
|
109
123
|
Do not store raw source, logs, tool output, reasoning traces, or vague values such as `"continue work"` and `"in progress"`.
|
|
110
124
|
|
|
111
125
|
### Skill compilation
|
|
@@ -131,9 +145,9 @@ No `coverage`/`rules` schema is imposed. The model chooses the smallest structur
|
|
|
131
145
|
|
|
132
146
|
A complete matching compilation is authoritative episode memory and replaces routine rereading. Rereading is justified only by an explicitly uncovered detail, an incomplete compilation, concrete source-change evidence, a contradiction or execution failure requiring reconciliation, or an explicit user request. The mere possibility that a source changed is not sufficient. A justified reread refreshes the compilation and removes obsolete rules.
|
|
133
147
|
|
|
134
|
-
State is a minimal sufficient memory, not an append-only diary. At every handoff the model audits the complete state and may reorganize inefficient structure, merge fragments, replace verbose history with current conclusions, and delete stale, completed, redundant,
|
|
148
|
+
State is a minimal sufficient memory, not an append-only diary. At every handoff the model audits the complete state and may reorganize inefficient structure, merge fragments, replace verbose history with current conclusions, and delete stale, completed, redundant, or low-value keys with `null`. Active requirements, decisions, interfaces, verified evidence, and unresolved work must survive optimization.
|
|
135
149
|
|
|
136
|
-
The model must not invent bookkeeping merely to change `contract` or `working`. When a run creates no future-relevant information and existing memory is already efficient, both objects remain empty while `response` still contains the actual answer. Structurally invalid handoffs are regenerated through hidden validation feedback; retry diagnostics are not user-facing output.
|
|
150
|
+
The model must not invent bookkeeping merely to change `contract` or `working`. When a run creates no future-relevant information and existing memory is already efficient, both patch objects may remain empty, or the comment may be omitted entirely, while `response` still contains the actual answer. Successful Skill reads still require their compilations in materialized memory even when the comment is omitted. Bootstrap migration remains a model obligation: a plain answer cannot migrate pre-Flow context automatically. Structurally invalid handoffs are regenerated through hidden validation feedback; retry diagnostics are not user-facing output.
|
|
137
151
|
|
|
138
152
|
## Context lifecycle
|
|
139
153
|
|
|
@@ -156,6 +170,7 @@ The normative protocol remains in the system prompt throughout a tool/retry chai
|
|
|
156
170
|
|
|
157
171
|
- State commits exactly once per successful complete agent run, at terminal `message_end`/`turn_end`.
|
|
158
172
|
- Tool-bearing responses do not require or commit patches and retain ordinary Pi tool behavior, including multiple sequential calls. The runtime removes an accidental leading terminal envelope only when its JSON, fields, separator, and non-empty response are all structurally valid; malformed or quoted examples remain untouched.
|
|
173
|
+
- Terminal answers cannot contain another complete State Flow comment, even inside a fenced code block or inline code: duplicate detection scans the entire answer body, not Markdown structure. When explaining the protocol while State Flow is enabled, describe the fields or show plain JSON without the HTML comment delimiters. The literal envelope examples in this README are documentation, not valid content to copy into a terminal answer body.
|
|
159
174
|
- A failed tool remains in the current run trajectory for ordinary model reconciliation before terminal commit.
|
|
160
175
|
- If a run never reaches a valid terminal handoff, its external tool effects may exist while persistent State Flow state remains at the previous commit.
|
|
161
176
|
- The terminal envelope exists in ordinary assistant text until `message_end`; `message_update`, RPC, JSON, or other streaming consumers can observe it. Never place secrets in State Flow state.
|
|
@@ -173,6 +188,8 @@ npm install
|
|
|
173
188
|
npm run validate
|
|
174
189
|
```
|
|
175
190
|
|
|
191
|
+
Validation covers TypeScript checking, automated tests, and an extension import smoke check. Lifecycle tests use a mock Pi event harness; they do not establish behavior under the real Pi scheduler, queued prompts, abort/compaction interactions, or combinations of extensions. Those scenarios still need integration checks. Structural patch validation also cannot prove that a handoff preserved every important requirement or compiled a Skill faithfully.
|
|
192
|
+
|
|
176
193
|
## Project status
|
|
177
194
|
|
|
178
195
|
- [Open work](BACKLOG.md)
|
package/lib/terminal.ts
CHANGED
|
@@ -19,16 +19,18 @@ response: previous complete answer; runtime replaces it on commit.
|
|
|
19
19
|
|
|
20
20
|
TOOLS: Use normal Pi tools without a state_flow comment or intermediate patch. Continue until one terminal response; the current trajectory stays visible.
|
|
21
21
|
|
|
22
|
-
TERMINAL (no tool),
|
|
22
|
+
TERMINAL (no tool): If memory is unchanged, output only the complete answer. Otherwise:
|
|
23
23
|
<!-- state_flow {"contract":{...},"working":{...}} -->
|
|
24
24
|
|
|
25
25
|
Complete user-facing answer
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
With a patch: one blank separator, no fence or duplicate; never put literal --> in JSON. Runtime strips the comment. Without one: preserve memory. Always store the non-empty answer as response.
|
|
28
28
|
|
|
29
29
|
PATCH: contract and working are mandatory flexible objects. Recursive object merge; {} preserves; arrays/primitives replace; nested key null deletes. Materialized null is forbidden, including in arrays.
|
|
30
30
|
|
|
31
|
-
HANDOFF + MEMORY OPTIMIZATION: Assume this trajectory disappears after commit. Preserve all decision-relevant knowledge needed to continue without rereading, rediscovery, re-derivation, or repeated failures. Put durable knowledge in contract and current execution state in working. Audit both as minimal sufficient memory: merge fragments, replace history with conclusions, and delete stale, completed, redundant,
|
|
31
|
+
HANDOFF + MEMORY OPTIMIZATION: Assume this trajectory disappears after commit. Preserve all decision-relevant knowledge needed to continue without rereading, rediscovery, re-derivation, or repeated failures. Keep active constraints, unresolved questions, consequential negative results, and the next discriminating check. Distinguish observations, user requirements, decisions, and hypotheses; never promote assistant conclusions to user requirements. For consequential facts, keep useful source locators and validity conditions, not metadata on every value. Retain rejection reasons and reconsideration conditions. Reconcile contradictions using evidence or user clarification; unsupported claims must not overwrite established constraints or observations. Put durable knowledge in contract and current execution state in working. Audit both as minimal sufficient memory: merge fragments, replace history with conclusions, and delete stale, completed, redundant, or low-value keys; retain decision-relevant hypotheses as uncertain while preserving active requirements, decisions, interfaces, evidence, and unresolved work. Omit raw sources, logs, tool output, reasoning, and vague narration. Never invent memory changes.
|
|
32
|
+
|
|
33
|
+
REALITY CHECK: working records last observations, not a live workspace. Revalidate volatile facts before consequential actions. After interruption or branch navigation, inspect relevant external effects before repeating operations; failed state commits and restored memory do not undo tool effects. If evidence is unavailable, retain uncertainty and the next check; never infer success or absence of effects from missing memory. Revalidation is targeted, not routine Skill rereading.
|
|
32
34
|
|
|
33
35
|
SKILL COMPILATION: After a successful SKILL.md read, compile its future-useful rules, applicability, syntax, routing, constraints, and failures at contract.compiled_skills[exact read path] before commit. Use a compact non-empty shape, not raw Skill text. A matching compilation is authoritative: MUST NOT reread for recall or routine activation. Reread only for an uncovered detail, incomplete compilation, concrete source-change evidence, contradiction/failure reconciliation, or explicit user request. Mere possibility of change is not evidence. Refresh after a justified reread.
|
|
34
36
|
|
|
@@ -40,6 +42,13 @@ export function parseTerminalPatch(content: unknown): { patch: StateDocument; re
|
|
|
40
42
|
const textBlocks = content
|
|
41
43
|
.map((block, index) => ({ block, index }))
|
|
42
44
|
.filter(({ block }) => isObject(block) && block.type === "text" && typeof block.text === "string");
|
|
45
|
+
const response = textBlocks.map(({ block }) => (block as { text: string }).text).join("");
|
|
46
|
+
// Missing envelopes are no-op memory patches, not validation failures.
|
|
47
|
+
// Detect even incomplete markers so malformed explicit patches cannot fall through.
|
|
48
|
+
if (!/<!--\s*state_flow\b/.test(response)) {
|
|
49
|
+
if (response.trim().length === 0) throw new Error("Terminal State Flow response body must be non-empty");
|
|
50
|
+
return { patch: { contract: {}, working: {}, response }, responseContent: content };
|
|
51
|
+
}
|
|
43
52
|
if (textBlocks.length !== 1) {
|
|
44
53
|
throw new Error(`Expected exactly one terminal State Flow text block, found ${textBlocks.length}`);
|
|
45
54
|
}
|