@llblab/pi-state-flow 0.19.0 → 0.20.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 -4
- package/BACKLOG.md +3 -5
- package/CHANGELOG.md +12 -0
- package/README.md +4 -4
- package/dist/lib/context.d.ts +50 -1
- package/dist/lib/context.js +152 -7
- package/dist/lib/extension.js +36 -27
- package/dist/lib/protocol.d.ts +1 -1
- package/dist/lib/protocol.js +6 -4
- package/dist/lib/query.js +1 -1
- package/dist/package.json +1 -1
- package/dist/skills/state-flow-guide/SKILL.md +6 -2
- package/dist/skills/state-flow-memory/SKILL.md +3 -1
- package/docs/architecture.md +8 -4
- package/docs/compatibility.md +1 -1
- package/docs/lazy-state.md +6 -4
- package/docs/performance.md +48 -2
- package/docs/temporal-acceptance.md +6 -3
- package/docs/usage.md +10 -0
- package/lib/context.ts +144 -8
- package/lib/extension.ts +37 -32
- package/lib/protocol.ts +8 -4
- package/lib/query.ts +1 -1
- package/package.json +1 -1
- package/skills/state-flow-guide/SKILL.md +6 -2
- package/skills/state-flow-memory/SKILL.md +3 -1
package/AGENTS.md
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
- Keep independent domain modules under `lib/`, mirror every domain with a same-named file under `tests/`, place cross-domain architecture checks in `tests/invariants.test.ts`, and keep `index.ts` as a minimal public-export boundary. Keep `lib/extension.ts` as the Pi lifecycle composition root: it may wire configuration, domain capabilities, handlers, and event subscriptions, but low-level parsing, traversal, formatting, persistence, queue/worker mechanics, and diagnostic I/O belong to their existing owning domains.
|
|
4
4
|
- Keep the extension opt-in and preserve clear attribution to SKILL.state wherever the inherited explicit-state approach is described.
|
|
5
5
|
- Distinguish three model-facing states: active, passive, and configured off. Active and passive both expose `read_state` and `patch_state` when memory tools are permitted; both use the same canonical persistence, private ownership and fork-copy rules. The difference is the agent workflow, not whether memory is saved. Active iterations must consolidate future-relevant results into accepted state before completed context is replaced: make the final meaningful semantic patch when needed, then the next iteration starts from current state plus its new input rather than the previous completed reasoning. Passive mode keeps ordinary conversation continuity and allows on-demand reading/patching without that state-driven iteration boundary or the same mandatory consolidation pressure. Configured off exposes neither memory tool and injects no State Flow bootstrap/state context; it is not another name for passive. In the current configuration, off means an inactive branch with both `passiveTools` and `passiveBootstrap` false; `autoStart: false` alone is not off. The independent one-flag configurations remain supported partial integrations, not full passive or full off. Start/Stop govern active/passive workflow and must not be treated as switches for canonical storage or fork initialization. See [mode semantics](docs/usage.md#active-passive-and-configured-off) for completion and bootstrap terminology.
|
|
6
|
-
- Preserve Pi's native tool loop and complete inspectable session trace; project completed-run history only at user-run boundaries. The
|
|
7
|
-
- Expose one canonical intent-first materialized state shape across global, CWD, and session scopes with exactly `intents`, `contract`, `working`, `artifacts`, `response`, and `lazy`; the first four and the required `lazy` root are flexible semantic objects, while `response` is the exact latest accepted assistant answer, including the empty string, and is owned only by session state. Global and CWD keep the required key as an empty structural placeholder; effective state inherits the session value.
|
|
6
|
+
- Preserve Pi's native tool loop and complete inspectable session trace; project completed-run history only at user-run boundaries. The context domain owns model projection of the raw scope overlay; callers must not pre-project it. Freeze the complete active/bootstrap head, including timestamp, specification and recent transitions, at the first accepted context. Reset the volatile projection at a new active run, accepted active completion, Start/Stop, selection/resume/reload, native compaction or a removed/replaced native prefix; passive user turns alone never reset it. Keep changing artifact hints, invalidations, rehydration and otherwise unreported shared drift in stable-position tail notices, never head rewrites. Successful patch receipts advance the last communicated view without redundant notices. Bind state updates to the head's volatile projection ID so retained results from an earlier head remain historical; never store this ID in canonical state or runtime metadata. Skill acquisition hints remain in native read results. This cache grants no publication, history, selection or compaction authority and adds no tail-size threshold. Retain persistent and current-run context-bearing custom messages from other extensions, plus the complete current-run trajectory. Select a unique captured native user timestamp independently of text decoration, including SDK image hints. Observe native user events even while disabled; Start/Stop must retain that observed run anchor. Reset it at native before-agent-start and session-start/tree boundaries, not semantic mode changes. Observation alone never enables state, publishes memory or authorizes compaction. Without a capture, a unique exact specification match may select only model projection. Missing, nonfinite or ambiguous matches retain available context; projection must never assign the lifecycle run anchor. Direct completion persists no private validation feedback.
|
|
7
|
+
- Expose one canonical intent-first materialized state shape across global, CWD, and session scopes with exactly `intents`, `contract`, `working`, `artifacts`, `response`, and `lazy`; the first four and the required `lazy` root are flexible semantic objects, while `response` is the exact latest accepted assistant answer, including the empty string, and is owned only by session state. Global and CWD keep the required key as an empty structural placeholder; effective state inherits the session value. Automatic model-state and recent-transition projections omit lazy bodies. Remove whole `patch.lazy` entries and then empty scoped patches/transitions, omitting an empty recent window without renumbering retained identities or positions. Preserve hot-history projection, artifact metadata filtering and bounded lazy navigation. Canonical history, explicit value/patch reads and already communicated native/user/tool/response text remain unchanged. Preserve this plane order in model-facing projection and state presentation. `intents` contains only active commitments to future action, remains hot by default, and must not become a planner, scheduler, task manager, or execution loop.
|
|
8
8
|
- Key artifacts directly by their source path. Model-visible artifact entries require only a non-empty `description` plus optional forward-compatible metadata; runtime-owned compilation evidence (`sourceFingerprint`, `sourceHash`, `compilerRevision`, `compiledAt`) belongs in the scope `meta.json` provenance registry. Reserve model-authored `hint`; runtime may add it only to effective/model projection and never to canonical scope state. Retired embedded `hash`/`compiler`/`compiled_at` fields remain readable compatibility evidence and are stripped from model projection. Validate provenance restrictions on every authored artifact entry, not on retained legacy state: individual runtime/legacy provenance fields cannot be set or deleted by model patches in any scope.
|
|
9
9
|
- Inspect only exact artifact source paths already registered in global, CWD, or session state. Never scan directories, discover files, reserve a Knowledge root, interpret file extensions, or read unrelated source bodies. Observe regular non-symlink files through `size + mtimeNs`; proven absence removes the artifact and provenance from each exact owning scope, while relative, symlink, directory, inaccessible, or otherwise unavailable paths are non-destructive evidence.
|
|
10
10
|
- Use `classifyArtifactCompilationNeed` for public acquisition/rehydration and ordinary-artifact Pi invalidations. Compare observed fingerprints with hidden provenance; missing/malformed retained fingerprints, malformed compiler evidence, and compiler changes request compilation without declaring semantic corruption. Fingerprint-only decisions ignore unused legacy hashes, while explicit current hashes retain their checks; never invent hashes in read plans. Accept signed filesystem `mtimeNs`, including pre-epoch sources. Preserve changed-source semantics and add `hint` only to model projection. Effective Skill entries mask lower ordinary entries and retain their separate hash protocol.
|
|
@@ -25,10 +25,10 @@
|
|
|
25
25
|
- Bind active Pi checkpoints to retained semantic boundaries in the current canonical lineage. Awaited runtime restoration uses `withRestoreTransaction`: detach the pointer before waiting, capture/select private history beside current shared streams under one exclusion, recheck caller policy, and publish once before installing the candidate. Expiry or malformed evidence cannot authorize current-head or empty fallback; origin acceptance retains configured folding and causal provenance rules. Resume and tree restoration select only retained private session history while global/CWD scopes remain live; every failure resolving a selected boundary fails closed without falling through to older checkpoints, disabled markers, Git revisions, or unrelated newer private state. Passive shared-memory availability does not prove the selected private layer: preserve the recovery failure and reject historical session reads and passive publication until it is resolved. Explicit Start may independently activate the validated live same-session canonical head. A same-owner failed-Stop marker permits read-only loading of validated current memory on reload, never historical substitution or publication; passive tools cannot clear its write fence. Awaited recovery uses `refreshCurrentMemory`: capture current authority under exclusion, reject cancellation before cache installation, never publish or initialize absence, and retain the prior cache on failure. Its disabled snapshot omits unfinished specifications and grants no write authority; native callers still own selection and Stop fences. Native startup/tree/fork/auto-start/failed-Stop restoration awaits one owned lifetime in the composition root: pending selection stays passive and unavailable, selection revokes older work, and shutdown drains current and superseded operations. Start/Stop select agent policy, not the existence of canonical memory. Stop must not cancel retained restoration, auto-start memory initialization or fork copying: apply passive policy inside the pending memory acceptance and keep configured passive patching available afterward. Start waiter cancellation must not cancel independently owned attachment or copying. Only selection changes, shutdown, native operation cancellation and actual authority/storage failures revoke that work; a mode change must not manufacture a storage-error fence. Start pins physical identity across cancelable joins without cancelling independently owned recovery or Stop persistence. Only current acceptance installs memory, checkpoint, tools and UI. Keep read-only candidates detached until cancellation, physical identity and any intervening accepted cache change are rechecked.
|
|
26
26
|
- Every accepted semantic change, including session-only and response-only changes, atomically updates affected canonical checkpoint/tail pairs and temporal metadata without creating a Git commit. Each materially changed scope advances its own persisted revision exactly once; Global and CWD revisions are shared across their writers, Session is private, and Effective is represented by the `G#/C#/S#` revision vector rather than an invented scalar owner. After an accepted turn reconciles its response and reaches `agent_before_settle`, one best-effort backup may commit only State Flow-owned already-written files. Await the backup mutex before briefly locking canonical storage for bounded root/CWD/session namespace inventory and regular-file byte capture; hold the separate mutex through completion and never acquire it from a canonical writer. The Promise-returning backup API accepts cancellation. Combine a real Pi operation signal with shutdown cancellation and drain owned attempts before shutdown completes. Pi 0.87 provides no operation signal at settlement: refuse contention there with explicit diagnostic-only deferral until a later accepted turn, rather than trapping native Abort. Do not infer cancellation from an ended agent signal, steal locks, add a background owner, or apply this optional-backup fallback to semantic publication. Release the canonical lock before every Git command, filter, staging write, ref update, or index synchronization. Recheck the Git branch/head after capture waits. Stage only the captured snapshot through a private temporary worktree/index, preserve Git ignore/filter policy, and never inventory artifact sources or unrelated directory trees. Keep unrelated staged/index-only content and worktree edits intact; synchronize only exact owned index paths, include HEAD-owned deletions even when already absent from the caller index, and never create an unowned-only initial backup. Backup failure is diagnostic-only and never rolls back state, suppresses an answer, requests continuation, or blocks a later turn. After each successful backup attempt, asynchronously push the exact current commit to the attached branch's explicitly configured remote/ref without force. Permit at most one in-flight push per repository root in process; skip overlapping attempts, leaving a later accepted turn to push the latest HEAD. Session shutdown waits for the in-flight process to close, within the existing Git push timeout and termination behavior; suppress push-failure reporting after shutdown begins. Push failure has no semantic effect; while active, warn once per failure streak with a concise message and reset suppression after successful replication. Record the available redacted Git failure detail in the local diagnostic log even when general logging is off; if local recording fails, warn once with the available detail. A later accepted turn retries the latest backup. Durable push queues, publication workers, worker leases, and remote retry generations do not exist.
|
|
27
27
|
- Register `patch_state` as the sole model-authored semantic mutation protocol. Its canonical grammar accepts one or more fixed `global`, `cwd`, and `session` semantic patches; reject unknown fields, empty supplied scopes, and retired finalization or `{scope, patch}` / `unchanged` grammar. Apply authored Global/CWD operations to the current canonical head, preserving untouched values; overlapping targets follow successful acceptance order, not stale agent snapshots. Validate and publish all supplied scopes as one durable cohort, deriving replay and revisions from that actual basis. Correct repeats succeed without another semantic identity, step or scope revision. Preserve private ownership/history fences and complete artifact/provenance units. Never accept model-authored `response`.
|
|
28
|
-
- Treat every semantic `patch_state` as a strict inference barrier. Give it sequential execution mode; find the matching synchronized assistant through public parent traversal from the selected leaf without constructing a full branch during tool preflight; require exactly one `patch_state` call in that response and block every sibling tool call before execution. The next inference sees rematerialized state. Ordinary accepted answers require no finalization patch or fallback inference.
|
|
28
|
+
- Treat every semantic `patch_state` as a strict inference barrier. Give it sequential execution mode; find the matching synchronized assistant through public parent traversal from the selected leaf without constructing a full branch during tool preflight; require exactly one `patch_state` call in that response and block every sibling tool call before execution. The next inference sees rematerialized state. Successful native patch results also carry context-owned `state_updates`: exact projected effective replacements/deletions at key/index-segment path arrays, covering touched paths (including correct repeats) and all projected drift since the last communicated view, or the caller's pre-wait cache before any head exists. Keep effective scope masking/fallback, artifact metadata filtering and lazy-body omission; only changed bounded lazy navigation may accompany updates. Keep the compact interactive acknowledgement separate from this model-facing result block. Ordinary accepted answers require no finalization patch or fallback inference.
|
|
29
29
|
- Reconcile `response` only from the actually accepted ordinary assistant answer at `turn_end`; it remains runtime-owned, and an accepted empty answer reconciles to `""` rather than causing a finalization failure. Await `withPatchTransaction` before staging against the current shared head; publish the response and completed-run lifecycle together, then install caches/checkpoint before yielding without a second persistence call. Combine Pi's operation signal with response-owned cancellation: run/selection changes, Stop, shutdown and superseding answers withdraw obsolete waits, whose completion must never clear newer pending work, restore a specification, update UI or erase a Stop fence. Cancellation before acceptance leaves the run unfinished; cancellation afterward cannot undo accepted memory. State Flow has no terminal HTML-comment mutation protocol and does not parse generic service comments. Other extensions retain ownership of their own comments and output handling.
|
|
30
30
|
- Treat terminal state as a decision-relevant handoff, not narration: retain source-addressed reusable operational knowledge in `artifacts`; compile stable requirements, confirmed decisions, rejected approaches, and interface commitments into `contract`; retain observations, validation, failures, current domain state, unresolved work, interaction consequences, and exact continuation in `working`. Preserve relevant completed prerequisites and verified outcomes while removing obsolete progress narration; reconcile only information affected by the run and relevant existing commitments, not every scope or repository surface.
|
|
31
|
-
- 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. Treat resource, document, memory, Skill, and agent locators as semantic references wherever context expresses them. Inside ordinary strings and prose, encode a semantic-state path as `$` immediately followed by one valid `read_state` path, for example `$effective.lazy.memory[7]`; the prefix distinguishes a reference from incidental path-like text and leaves room for future parsing. `{"$ref":"cwd.lazy.plan"}` remains the optional structured state-reference form; neither form is a runtime link type. External resource locators retain their native path, URI, Skill, or agent syntax. Resolve references explicitly through the appropriate read/tool when needed and infer no authority, existence, dependency, hydration, execution, or completion merely from their presence. Never scan, audit, or proactively resolve references merely to find broken ones. Only after one requested `read_state` value path is missing may the query domain perform one bounded reverse lookup over current model-patchable semantic planes for exact structured `$ref` and `$path` matches. When durable sources match, return a successful top-level `{value:null, hint:[...]}` diagnostic sentinel: every hint has `type:"dangling-reference"`,
|
|
31
|
+
- 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. Treat resource, document, memory, Skill, and agent locators as semantic references wherever context expresses them. Inside ordinary strings and prose, encode a semantic-state path as `$` immediately followed by one valid `read_state` path, for example `$effective.lazy.memory[7]`; the prefix distinguishes a reference from incidental path-like text and leaves room for future parsing. `{"$ref":"cwd.lazy.plan"}` remains the optional structured state-reference form; neither form is a runtime link type. External resource locators retain their native path, URI, Skill, or agent syntax. Resolve references explicitly through the appropriate read/tool when needed and infer no authority, existence, dependency, hydration, execution, or completion merely from their presence. Never scan, audit, or proactively resolve references merely to find broken ones. Only after one requested `read_state` value path is missing may the query domain perform one bounded reverse lookup over current model-patchable semantic planes for exact structured `$ref` and `$path` matches. When durable sources match, return a successful top-level `{value:null, hint:[...]}` diagnostic sentinel: every hint has `type:"dangling-reference"`, a descriptive conditional message, and at most three runtime-verified current reference-owner paths, never purported relocated targets or lazy bodies. The null sentinel is never presented without `hint` for this case; keys, patch, and multi-path reads retain ordinary all-or-error semantics. Never scan runtime-owned `response`, and retain the ordinary missing-path error when no durable source matches. A match proves durable semantic provenance, not staleness; no match does not prove invention. Missing paths or runtime hints alone never require historical search. The agent may choose targeted historical reads when a previous value is useful to the current task without separate user permission; otherwise continue without searching. Treat past values as historical evidence, not automatically current memory, and never automatically restore deleted data. Do not scan offsets, hydrate automatically, add repair inference or search historical reference owners. Hints do not prove prior existence, retained history or relocation. The agent may inspect ownership and patch a proven stale source within touched work while preserving surrounding meaning, without resurrecting its target. Effective-state absence does not identify ownership, and unavailable history, inaccessible external resources, or transient read failure do not prove a dangling reference. 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.
|
|
32
32
|
- 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 action ledgers or runtime freshness/rollback guarantees.
|
|
33
33
|
- Recognize Skill acquisition only when a successful finalized `read` path exactly matches a registered Pi Skill exposed by the public `getCommands()` resource metadata. Map `sourceInfo.scope` deterministically as user → global, project → CWD, and temporary → session; never infer ownership from path shape. Hash the executed source bytes. Matching current provenance creates no acquisition request. Otherwise append a concise tool-result hint naming the exact target, but keep the read volatile unless the model supplies durable compiler output; pending acquisition never blocks an unrelated semantic patch or ordinary completion. Validate an attempted compilation at the reported scope/path with non-empty `description`, `kind: "skill"`, and flexible non-empty `compilation`, then record runtime-owned `sourceHash` plus `skill-artifact-v1` `compilerRevision` in that scope's provenance. Replace the complete prior Skill artifact and provenance on refresh so obsolete evidence cannot survive. Unregistered `SKILL.md` reads have no acquisition semantics. `contract.compiled_skills` is retired and rejected; never fabricate hashes or bypass explicit source acquisition.
|
|
34
34
|
- Make state stewardship an ordinary model responsibility without a background loop or gratuitous full-state rewrite. Every handoff curates touched and obviously stale or mis-scoped visible branches: place new knowledge at the narrowest valid scope, merge fragmented facts, compress history into conclusions, and delete stale, completed, redundant, or low-value keys while preserving active commitments and evidence. Dedicated cleanup and scope audits require an explicit user request; a feature/release/project boundary may motivate a recommendation, not an automatic audit. For a proven move within one store, inspect both owners, resolve conflicts, apply destination/source changes through one atomic multi-scope patch, and verify both owners plus the effective overlay. External transfers require destination-native acceptance verification before source deletion. Global is only established cross-project/user/environment knowledge, CWD is reusable project truth, and session is branch/run continuation.
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
- Activate State Flow model tools while an episode is enabled or passive tools are configured; preserve every unrelated active tool when toggling them. Passive reads never initialize storage, while an explicit passive patch may initialize absent canonical storage without enabling an episode, continuation, or compaction. Unsupported predecessor storage is never converted. Keep mutation confined to `patch_state` and historical observation read-only.
|
|
52
52
|
- Keep `.github/workflows/release.yml` as the sole version-tag release owner: it validates immutable tag identity, publishes through npm Trusted Publisher with provenance, verifies the public package, and only then creates the GitHub Release. Keep package, lockfile, tag, and changelog versions aligned; never add a long-lived npm token fallback.
|
|
53
53
|
- Keep opt-in performance executables and workers in top-level `benchmarks/`; their correctness regressions stay in `tests/`. Benchmark workloads may reuse synthetic test fixtures, remain excluded from runtime packaging, and retain source-bound measurement identities across relocations.
|
|
54
|
+
- In native scripted-provider tests, assert callback completion outside the provider as well as checking input inside it: Pi may convert an assertion thrown by a provider into an assistant error rather than rejecting the test's prompt. Do not count swallowed provider assertions as successful evidence.
|
|
54
55
|
- Await startup/tree lifecycle handlers in fixtures before inspecting their completed state. In contended-store cancellation tests, dispatch replacement selection without awaiting it, prove the obsolete operation withdraws while exclusion remains held, then release storage and join the replacement. Keep old-operation trace/notice checks distinct from legitimate replacement UI completion.
|
|
55
56
|
- Keep generated `dist/` in the release Git tree; do not re-add it to `.gitignore`. Rebuild from final sources and package metadata before release, include the resulting files in the release commit, and verify source/build parity as well as npm inventory. Git inclusion and npm `files` inclusion are distinct obligations.
|
|
56
57
|
- Keep maintained README/docs focused on current behavior, supported contracts and verification limits. Version comparisons, removed intermediates and implementation chronology belong in CHANGELOG, not current compatibility/architecture guidance.
|
package/BACKLOG.md
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
# Backlog
|
|
2
2
|
|
|
3
|
-
No open implementation items for the 0.
|
|
3
|
+
No open implementation items for the 0.20.0 cache-stable projection scope. Outcomes are recorded in [CHANGELOG.md](CHANGELOG.md); source-bound before/after measurements and their limits are in [performance evidence](docs/performance.md).
|
|
4
4
|
|
|
5
|
-
Release publication is owned by
|
|
5
|
+
Release publication is owned by `.github/workflows/release.yml`. Verify the exact tag's successful workflow, published GitHub Release and matching npm package before reporting release completion. Installed-instance reload remains a separate operator action.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Live-host, provider and platform verification limits remain documented in [SDK compatibility](docs/compatibility.md). Production-store conversion and installed-host activation remain separate operator decisions; see [usage and recovery](docs/usage.md).
|
|
7
|
+
Current contracts belong to [architecture](docs/architecture.md), [lazy state](docs/lazy-state.md), [usage](docs/usage.md) and the bundled Skills. [Temporal acceptance](docs/temporal-acceptance.md) records the native projection/hint witnesses and their limits. Canonical storage, CAS, `historyLimit`, history and lifecycle semantics are unchanged.
|
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
> Each release keeps at most 8 outcome records of at most 512 characters.
|
|
4
4
|
|
|
5
|
+
## 0.20.0: Cache-Stable Projection
|
|
6
|
+
|
|
7
|
+
- `Cache-stable model projection`: Active/bootstrap heads freeze per accepted iteration; passive heads survive ordinary user turns. Native/mode boundaries rebase without semantic changes. Patch receipts and stable-position notices deliver current values, hints and invalidations without rewriting prior prefixes. Volatile projection IDs keep retained old results historical after rebasing. Native regressions cover repeated barriers, passive continuation and bootstrap; canonical history is unchanged.
|
|
8
|
+
- `Accepted-state patch tails`: Successful active/passive patch results carry projected effective replacements and deletions for touched paths and transaction-adopted shared drift, including correct repeats and scope fallback/masking. Lazy bodies and artifact provenance stay hidden; changed bounded lazy navigation remains available. Native tests prove the next inference sees these values in active, passive and Stop-handoff modes. Interactive acknowledgement and patch-display policy stay unchanged.
|
|
9
|
+
- `Trajectory-dominant prefix evidence`: Opt-in v3 reports exercise active, passive and Stop-handoff projection with six 20 KiB native reads before a patch and two before another. Reports retain every inference's byte prefix and read volume; native content, accepted barriers and completion are checked independently of provider callbacks. Source-bound baseline measurements distinguish frozen Stop handoff from regenerated active/passive heads without claiming provider cache-hit rates.
|
|
10
|
+
- `Minimum-Node benchmark validation`: Optional `BENCH_REPORT_PATH` writes complete machine-readable reports independently of test-runner stdout chunking, while retaining human-readable stdout. Regression tests read file reports for successful and failed workloads, including 1 MiB backpressure. Release validation now covers Node 22.19.0 and Node 24; publication remains on Node 24.
|
|
11
|
+
|
|
12
|
+
## 0.19.1: Lazy Isolation and Task-Driven History
|
|
13
|
+
|
|
14
|
+
- `Lazy projection isolation`: Automatic recent transitions omit entire lazy patches, including deletions, and discard empty visible records without renumbering history. Hot changes, artifact metadata filtering and bounded lazy navigation remain available. Canonical state/history, explicit reads and already communicated user/tool/response text are unchanged.
|
|
15
|
+
- `Task-driven history`: Missing paths and runtime hints do not require historical search. Active/passive guidance and both Skills permit targeted historical reads when useful to the task, without separate user permission, while forbidding automatic resurrection of deleted memory. Dangling-reference hints describe current reference owners, not relocated targets or recovery duties; existing lookup bounds and projection rules remain intact.
|
|
16
|
+
|
|
5
17
|
## 0.19.0: Awaited Memory and Lossless Mode Changes
|
|
6
18
|
|
|
7
19
|
- `Lossless context toggles`: Stop retains uncompiled conversation through Start/Stop, reload and restart without resurrecting compiled history. Interrupted runs keep their native anchor or conservatively retain available context. Uncheckpointed requests and interrupted boundary continuations survive idle Stop/reload even without a specification; successful bootstrap releases the extra context. Completed idle Stop stays bounded.
|
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ Patched state + Answer
|
|
|
22
22
|
Updated state
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
Within a run, the available request, intermediate responses, tool results and steering remain in context. A state patch
|
|
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.
|
|
26
26
|
|
|
27
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.
|
|
28
28
|
|
|
@@ -60,9 +60,9 @@ Active mode is **opt-in**. Passive memory projection and the memory tools are en
|
|
|
60
60
|
|
|
61
61
|
### Active, passive, off
|
|
62
62
|
|
|
63
|
-
-
|
|
64
|
-
-
|
|
65
|
-
-
|
|
63
|
+
- `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.
|
|
64
|
+
- `Passive`: Memory tools and existing-state projection remain available by default. Patching is on demand, and ordinary conversation context continues without State Flow's active iteration reset.
|
|
65
|
+
- `Configured off`: With the branch inactive and both `passiveTools` and `passiveBootstrap` false, neither memory tool nor bootstrap/state projection is exposed to the model. `autoStart: false` alone does not turn memory off.
|
|
66
66
|
|
|
67
67
|
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).
|
|
68
68
|
|
package/dist/lib/context.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
2
|
import { type ArtifactInvalidationNotice, type ArtifactModelHints } from "./artifact.ts";
|
|
3
3
|
import type { RecentTransitionWindow } from "./history.ts";
|
|
4
|
+
import { type JsonValue } from "./json.ts";
|
|
4
5
|
import type { Snapshot } from "./snapshot.ts";
|
|
5
6
|
import type { RehydrationPhase } from "./rehydration.ts";
|
|
6
|
-
import { type MaterializedState, type ModelState } from "./state.ts";
|
|
7
|
+
import { type AtomicScopePatches, type MaterializedState, type ModelState } from "./state.ts";
|
|
7
8
|
/** Refresh only our section; Pi owns system frames, tools and forced-prompt precedence. */
|
|
8
9
|
export declare function projectSystemProtocol(messages: AgentMessage[], protocol: string | undefined): AgentMessage[];
|
|
9
10
|
type LazyValueKind = "array" | "boolean" | "null" | "number" | "object" | "string";
|
|
@@ -13,18 +14,66 @@ export declare function lazyNavigationHint(state: MaterializedState): {
|
|
|
13
14
|
path: string;
|
|
14
15
|
keys?: Record<string, LazyValueKind>;
|
|
15
16
|
};
|
|
17
|
+
export type ModelStateUpdate = {
|
|
18
|
+
path: (string | number)[];
|
|
19
|
+
} & ({
|
|
20
|
+
value: JsonValue;
|
|
21
|
+
} | {
|
|
22
|
+
deleted: true;
|
|
23
|
+
});
|
|
24
|
+
/** Exact projected replacements, not authored merge patches; paths are unambiguous key/index segments. */
|
|
25
|
+
export declare function acceptedStateUpdates(before: MaterializedState, after: MaterializedState, patches: AtomicScopePatches): {
|
|
26
|
+
effective: ModelStateUpdate[];
|
|
27
|
+
lazy_navigation?: {
|
|
28
|
+
available: boolean;
|
|
29
|
+
path: string;
|
|
30
|
+
keys?: Record<string, LazyValueKind>;
|
|
31
|
+
} | undefined;
|
|
32
|
+
};
|
|
33
|
+
export interface ContextView {
|
|
34
|
+
state: ModelState;
|
|
35
|
+
lazy_navigation?: ReturnType<typeof lazyNavigationHint>;
|
|
36
|
+
artifact_invalidations: readonly ArtifactInvalidationNotice[];
|
|
37
|
+
knowledge_rehydration: {
|
|
38
|
+
phase: RehydrationPhase;
|
|
39
|
+
} | null;
|
|
40
|
+
}
|
|
41
|
+
export declare function contextView(state: MaterializedState, hints: ArtifactModelHints, invalidations: readonly ArtifactInvalidationNotice[], phase?: RehydrationPhase): ContextView;
|
|
42
|
+
/** Volatile model projection only. Native messages own trajectory; this cache owns no persistence or lifecycle. */
|
|
43
|
+
export declare class ContextProjection {
|
|
44
|
+
private identity;
|
|
45
|
+
private head;
|
|
46
|
+
private view;
|
|
47
|
+
private native;
|
|
48
|
+
private notices;
|
|
49
|
+
reset(): void;
|
|
50
|
+
/** Called only after successful publication and ancillary acceptance, immediately before returning the native result. */
|
|
51
|
+
acceptPatch(before: MaterializedState, after: MaterializedState, patches: AtomicScopePatches, hints: ArtifactModelHints): {
|
|
52
|
+
effective: ModelStateUpdate[];
|
|
53
|
+
lazy_navigation?: {
|
|
54
|
+
available: boolean;
|
|
55
|
+
path: string;
|
|
56
|
+
keys?: Record<string, LazyValueKind>;
|
|
57
|
+
} | undefined;
|
|
58
|
+
projection: `${string}-${string}-${string}-${string}-${string}`;
|
|
59
|
+
};
|
|
60
|
+
project(messages: AgentMessage[], current: ContextView, makeHead: () => AgentMessage, initial?: ContextView): AgentMessage[];
|
|
61
|
+
}
|
|
16
62
|
/** Context retained after semantic State Flow is stopped in this physical session. */
|
|
17
63
|
export interface PassiveContinuation {
|
|
18
64
|
startedAt: number;
|
|
19
65
|
activeRunStartedAt?: number;
|
|
20
66
|
preserveContext?: true;
|
|
21
67
|
handoff: AgentMessage;
|
|
68
|
+
state: ModelState;
|
|
22
69
|
}
|
|
23
70
|
export declare function syntheticUser(text: string): AgentMessage;
|
|
24
71
|
export declare function createPassiveContinuation(state: ModelState, startedAt?: number, activeRunStartedAt?: number, preserveContext?: boolean): PassiveContinuation;
|
|
25
72
|
/** Keep the interrupted run through later results; an idle stop retains only later conversation. */
|
|
26
73
|
export declare function passiveContinuationMessages(messages: AgentMessage[], continuation: PassiveContinuation): AgentMessage[];
|
|
27
74
|
export declare function runtimeContextMessage(snapshot: Snapshot, state: MaterializedState, recentTransitions?: RecentTransitionWindow, artifactInvalidations?: readonly ArtifactInvalidationNotice[], rehydrationPhase?: RehydrationPhase, artifactHints?: ArtifactModelHints): AgentMessage;
|
|
75
|
+
/** Render a view already projected by this domain without cloning the full semantic overlay twice. */
|
|
76
|
+
export declare function runtimeContextHead(snapshot: Snapshot, view: ContextView, recentTransitions?: RecentTransitionWindow): AgentMessage;
|
|
28
77
|
/** Captured identity survives text decoration; an uncertain boundary retains available context. */
|
|
29
78
|
export declare function currentRunTrajectory(messages: AgentMessage[], specification: string | undefined, anchorTimestamp: number | undefined): {
|
|
30
79
|
messages: AgentMessage[];
|
package/dist/lib/context.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
1
2
|
import { projectArtifactForModel } from "./artifact.js";
|
|
2
|
-
import { isObject, presentationJson } from "./json.js";
|
|
3
|
+
import { isObject, presentationJson, sameJson } from "./json.js";
|
|
3
4
|
import { projectStateForModel } from "./state.js";
|
|
4
5
|
/** Refresh only our section; Pi owns system frames, tools and forced-prompt precedence. */
|
|
5
6
|
export function projectSystemProtocol(messages, protocol) {
|
|
@@ -52,6 +53,141 @@ export function lazyNavigationHint(state) {
|
|
|
52
53
|
const keys = Object.fromEntries(entries.map(([key, value]) => [key, lazyValueKind(value)]));
|
|
53
54
|
return JSON.stringify(keys).length <= LAZY_HINT_MAX_JSON_CHARS ? { ...base, keys } : base;
|
|
54
55
|
}
|
|
56
|
+
/** Exact projected replacements, not authored merge patches; paths are unambiguous key/index segments. */
|
|
57
|
+
export function acceptedStateUpdates(before, after, patches) {
|
|
58
|
+
return projectedStateUpdates(projectStateForModel(before), projectStateForModel(after), patches, lazyNavigationHint(before), lazyNavigationHint(after));
|
|
59
|
+
}
|
|
60
|
+
function projectedStateUpdates(previous, current, patches, beforeNavigation, navigation) {
|
|
61
|
+
const effective = [];
|
|
62
|
+
const prefix = (parent, child) => parent.length <= child.length && parent.every((part, index) => part === child[index]);
|
|
63
|
+
const put = (path, value) => {
|
|
64
|
+
if (effective.some((entry) => prefix(entry.path, path)))
|
|
65
|
+
return;
|
|
66
|
+
for (let index = effective.length - 1; index >= 0; index--) {
|
|
67
|
+
if (prefix(path, effective[index].path))
|
|
68
|
+
effective.splice(index, 1);
|
|
69
|
+
}
|
|
70
|
+
effective.push({ path, ...(value === undefined ? { deleted: true } : { value: structuredClone(value) }) });
|
|
71
|
+
};
|
|
72
|
+
const child = (value, key) => value !== null && typeof value === "object" && Object.hasOwn(value, key)
|
|
73
|
+
? value[key] : undefined;
|
|
74
|
+
const diff = (left, right, path) => {
|
|
75
|
+
if (left === undefined && right === undefined || left !== undefined && right !== undefined && sameJson(left, right))
|
|
76
|
+
return;
|
|
77
|
+
if (isObject(left) && isObject(right)) {
|
|
78
|
+
for (const key of new Set([...Object.keys(left), ...Object.keys(right)]))
|
|
79
|
+
diff(child(left, key), child(right, key), [...path, key]);
|
|
80
|
+
}
|
|
81
|
+
else if (Array.isArray(left) && Array.isArray(right) && left.length === right.length) {
|
|
82
|
+
for (let index = 0; index < right.length; index++)
|
|
83
|
+
diff(left[index], right[index], [...path, index]);
|
|
84
|
+
}
|
|
85
|
+
else
|
|
86
|
+
put(path, right);
|
|
87
|
+
};
|
|
88
|
+
const touched = (patch, value, path) => {
|
|
89
|
+
if (!isObject(patch)) {
|
|
90
|
+
put(path, value);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const keys = Object.keys(patch);
|
|
94
|
+
if (keys.length === 0)
|
|
95
|
+
return;
|
|
96
|
+
if (Array.isArray(value) && keys.every((key) => /^\[(0|[1-9]\d*)\]$/.test(key))) {
|
|
97
|
+
for (const key of keys) {
|
|
98
|
+
const index = Number(key.slice(1, -1));
|
|
99
|
+
// A higher-scope array may mask the patched array with a different length.
|
|
100
|
+
if (index >= value.length) {
|
|
101
|
+
put(path, value);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
touched(patch[key], value[index], [...path, index]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else if (isObject(value)) {
|
|
108
|
+
for (const key of keys)
|
|
109
|
+
touched(patch[key], child(value, key), [...path, key]);
|
|
110
|
+
}
|
|
111
|
+
else
|
|
112
|
+
put(path, value);
|
|
113
|
+
};
|
|
114
|
+
diff(previous, current, []);
|
|
115
|
+
for (const patch of Object.values(patches))
|
|
116
|
+
for (const [plane, value] of Object.entries(patch)) {
|
|
117
|
+
if (plane === "lazy")
|
|
118
|
+
continue;
|
|
119
|
+
if (plane === "artifacts" && isObject(value)) {
|
|
120
|
+
for (const path of Object.keys(value))
|
|
121
|
+
put([plane, path], child(current.artifacts, path));
|
|
122
|
+
}
|
|
123
|
+
else
|
|
124
|
+
touched(value, child(current, plane), [plane]);
|
|
125
|
+
}
|
|
126
|
+
return { effective, ...(navigation !== undefined && (beforeNavigation === undefined || !sameJson(beforeNavigation, navigation)) ? { lazy_navigation: navigation } : {}) };
|
|
127
|
+
}
|
|
128
|
+
export function contextView(state, hints, invalidations, phase) {
|
|
129
|
+
return { state: projectStateForModel(state, hints), lazy_navigation: lazyNavigationHint(state),
|
|
130
|
+
artifact_invalidations: structuredClone(invalidations), knowledge_rehydration: phase === undefined ? null : { phase } };
|
|
131
|
+
}
|
|
132
|
+
/** Volatile model projection only. Native messages own trajectory; this cache owns no persistence or lifecycle. */
|
|
133
|
+
export class ContextProjection {
|
|
134
|
+
identity = randomUUID();
|
|
135
|
+
head;
|
|
136
|
+
view;
|
|
137
|
+
native = [];
|
|
138
|
+
notices = [];
|
|
139
|
+
reset() {
|
|
140
|
+
this.identity = randomUUID();
|
|
141
|
+
this.head = undefined;
|
|
142
|
+
this.view = undefined;
|
|
143
|
+
this.native = [];
|
|
144
|
+
this.notices = [];
|
|
145
|
+
}
|
|
146
|
+
/** Called only after successful publication and ancillary acceptance, immediately before returning the native result. */
|
|
147
|
+
acceptPatch(before, after, patches, hints) {
|
|
148
|
+
const state = projectStateForModel(after, hints);
|
|
149
|
+
const navigation = lazyNavigationHint(after);
|
|
150
|
+
const updates = projectedStateUpdates(this.view?.state ?? projectStateForModel(before, hints), state, patches, this.view?.lazy_navigation ?? lazyNavigationHint(before), navigation);
|
|
151
|
+
if (this.view)
|
|
152
|
+
this.view = { ...this.view, state, lazy_navigation: navigation };
|
|
153
|
+
return { projection: this.identity, ...updates };
|
|
154
|
+
}
|
|
155
|
+
project(messages, current, makeHead, initial) {
|
|
156
|
+
const identities = messages.map((message) => JSON.stringify([message.role, message.timestamp,
|
|
157
|
+
"toolCallId" in message ? message.toolCallId : null]));
|
|
158
|
+
// Native compaction/selection normally resets explicitly; a removed/replaced prefix is also a safe cache boundary.
|
|
159
|
+
if (this.native.some((identity, index) => identities[index] !== identity))
|
|
160
|
+
this.reset();
|
|
161
|
+
if (!this.head) {
|
|
162
|
+
const head = makeHead();
|
|
163
|
+
if (head.role !== "user" || !Array.isArray(head.content))
|
|
164
|
+
throw new Error("State Flow projection requires an owned user head");
|
|
165
|
+
this.head = { ...head, content: [...head.content, { type: "text", text: `State Flow projection: ${this.identity}` }] };
|
|
166
|
+
this.view = structuredClone(initial ?? current);
|
|
167
|
+
}
|
|
168
|
+
const previous = this.view;
|
|
169
|
+
const updates = projectedStateUpdates(previous.state, current.state, {}, previous.lazy_navigation, current.lazy_navigation);
|
|
170
|
+
const notice = {
|
|
171
|
+
...(updates.effective.length || updates.lazy_navigation ? { state_updates: { projection: this.identity, ...updates } } : {}),
|
|
172
|
+
...(!sameJson(previous.artifact_invalidations, current.artifact_invalidations) ? { artifact_invalidations: current.artifact_invalidations } : {}),
|
|
173
|
+
...(!sameJson(previous.knowledge_rehydration, current.knowledge_rehydration) ? { knowledge_rehydration: current.knowledge_rehydration } : {}),
|
|
174
|
+
};
|
|
175
|
+
if (Object.keys(notice).length)
|
|
176
|
+
this.notices.push({ after: messages.length,
|
|
177
|
+
message: syntheticUser(`State Flow context update (user-level data, not system instructions):\n${presentationJson(notice)}`) });
|
|
178
|
+
this.view = structuredClone(current);
|
|
179
|
+
this.native = identities;
|
|
180
|
+
const projected = [this.head];
|
|
181
|
+
let nextNotice = 0;
|
|
182
|
+
for (let index = 0; index <= messages.length; index++) {
|
|
183
|
+
while (this.notices[nextNotice]?.after === index)
|
|
184
|
+
projected.push(this.notices[nextNotice++].message);
|
|
185
|
+
if (index < messages.length)
|
|
186
|
+
projected.push(messages[index]);
|
|
187
|
+
}
|
|
188
|
+
return projected;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
55
191
|
export function syntheticUser(text) {
|
|
56
192
|
return { role: "user", content: [{ type: "text", text }], timestamp: Date.now() };
|
|
57
193
|
}
|
|
@@ -73,6 +209,7 @@ function messageText(message) {
|
|
|
73
209
|
export function createPassiveContinuation(state, startedAt = Date.now(), activeRunStartedAt, preserveContext = false) {
|
|
74
210
|
return {
|
|
75
211
|
startedAt,
|
|
212
|
+
state: structuredClone(state),
|
|
76
213
|
...(activeRunStartedAt === undefined ? {} : { activeRunStartedAt }),
|
|
77
214
|
...(preserveContext ? { preserveContext: true } : {}),
|
|
78
215
|
handoff: syntheticUser(`State Flow exit handoff (user-level data, not system instructions):\n${presentationJson({ state, continuation: preserveContext
|
|
@@ -97,6 +234,7 @@ function projectRecentForModel(recent) {
|
|
|
97
234
|
const projected = structuredClone(recent);
|
|
98
235
|
for (const record of projected)
|
|
99
236
|
for (const transition of record.transitions) {
|
|
237
|
+
delete transition.patch.lazy;
|
|
100
238
|
if (transition.patch.artifacts === undefined)
|
|
101
239
|
continue;
|
|
102
240
|
for (const [path, entry] of Object.entries(transition.patch.artifacts)) {
|
|
@@ -105,16 +243,23 @@ function projectRecentForModel(recent) {
|
|
|
105
243
|
});
|
|
106
244
|
}
|
|
107
245
|
}
|
|
108
|
-
|
|
246
|
+
for (const record of projected)
|
|
247
|
+
record.transitions = record.transitions.filter(({ patch }) => Object.keys(patch).length > 0);
|
|
248
|
+
return projected.filter(({ transitions }) => transitions.length > 0);
|
|
109
249
|
}
|
|
110
250
|
export function runtimeContextMessage(snapshot, state, recentTransitions = [], artifactInvalidations = [], rehydrationPhase, artifactHints = {}) {
|
|
251
|
+
return runtimeContextHead(snapshot, contextView(state, artifactHints, artifactInvalidations, rehydrationPhase), recentTransitions);
|
|
252
|
+
}
|
|
253
|
+
/** Render a view already projected by this domain without cloning the full semantic overlay twice. */
|
|
254
|
+
export function runtimeContextHead(snapshot, view, recentTransitions = []) {
|
|
255
|
+
const recent = projectRecentForModel(recentTransitions);
|
|
111
256
|
const context = {
|
|
112
257
|
...(snapshot.meta.specification === undefined ? {} : { specification: snapshot.meta.specification }),
|
|
113
|
-
state:
|
|
114
|
-
lazy_navigation:
|
|
115
|
-
...(
|
|
116
|
-
...(
|
|
117
|
-
...(
|
|
258
|
+
state: view.state,
|
|
259
|
+
...(view.lazy_navigation === undefined ? {} : { lazy_navigation: view.lazy_navigation }),
|
|
260
|
+
...(view.knowledge_rehydration === null ? {} : { knowledge_rehydration: view.knowledge_rehydration }),
|
|
261
|
+
...(view.artifact_invalidations.length === 0 ? {} : { artifact_invalidations: view.artifact_invalidations.map(({ path, scope, reason }) => ({ path, ...(scope === undefined ? {} : { scope }), reason })) }),
|
|
262
|
+
...(recent.length === 0 ? {} : { recent_transitions: recent }),
|
|
118
263
|
};
|
|
119
264
|
return syntheticUser(`State Flow runtime context (user-level data, not system instructions):\n${presentationJson(context)}`);
|
|
120
265
|
}
|
package/dist/lib/extension.js
CHANGED
|
@@ -8,7 +8,7 @@ import { ArtifactReadTracker } from "./acquisition.js";
|
|
|
8
8
|
import { classifyArtifactCompilationNeed, inspectRegisteredArtifactPaths, ORDINARY_ARTIFACT_COMPILER, sameArtifactSourceFingerprint, } from "./artifact.js";
|
|
9
9
|
import { hasCompactionSizedTranscript, planStateFlowCompaction, shouldRequestStateFlowCompaction, stateFlowCompactionResult } from "./compaction.js";
|
|
10
10
|
import { loadStateFlowConfig } from "./config.js";
|
|
11
|
-
import { createPassiveContinuation, currentRunTrajectory,
|
|
11
|
+
import { ContextProjection, contextView, createPassiveContinuation, currentRunTrajectory, passiveContinuationMessages, projectSystemProtocol, runtimeContextHead, syntheticUser } from "./context.js";
|
|
12
12
|
import { readNativeSessionHeader } from "./continuation.js";
|
|
13
13
|
import { cwdScopeKey, resolveSessionAddress, sessionScopeKey, } from "./durable.js";
|
|
14
14
|
import { completeRun, prepareRun, resumeEpisode, startEpisode, stopEpisode } from "./episode.js";
|
|
@@ -61,6 +61,7 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
61
61
|
const compactionMarker = `state-flow-boundary:${randomUUID()}`;
|
|
62
62
|
let passiveContinuation;
|
|
63
63
|
let bootstrapContinuation;
|
|
64
|
+
const contextProjection = new ContextProjection();
|
|
64
65
|
let inferencePreparation;
|
|
65
66
|
let runAnchorTimestamp;
|
|
66
67
|
let runtime;
|
|
@@ -402,6 +403,7 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
402
403
|
}
|
|
403
404
|
/** Select the active native branch under one owned restoration lifetime; only current accepted work installs memory. */
|
|
404
405
|
function restoreActiveBranch(ctx, sessionStartReason, notifyRecovery = true, startOwner) {
|
|
406
|
+
contextProjection.reset();
|
|
405
407
|
cancelBranchRestoration();
|
|
406
408
|
// Start-owned attachment/fork recovery keeps its owner; only accepted Start cancels Stop.
|
|
407
409
|
if (!startOwner) {
|
|
@@ -760,6 +762,7 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
760
762
|
const selected = runtime ??= createRuntime(ctx);
|
|
761
763
|
const acquiredArtifacts = structuredClone([...artifactReads.successful.values()]);
|
|
762
764
|
const acquiredSkills = structuredClone([...skillReads.successful.values()]);
|
|
765
|
+
const previousEffective = overlayStates(scopeStates.global, scopeStates.cwd, scopeStates.session);
|
|
763
766
|
return await selected.withPatchTransaction((transaction) => {
|
|
764
767
|
if (runtime !== selected)
|
|
765
768
|
throw new Error("State Flow session selection changed while awaiting publication");
|
|
@@ -789,9 +792,14 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
789
792
|
}
|
|
790
793
|
clearAcceptedAcquisitions(new Set(acquiredArtifacts.map(({ path }) => path)));
|
|
791
794
|
updateUi(ctx);
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
+
const updates = contextProjection.acceptPatch(previousEffective, overlayStates(scopeStates.global, scopeStates.cwd, scopeStates.session), patches, artifactHints);
|
|
796
|
+
const acknowledgement = changed
|
|
797
|
+
? `\nState materialized atomically at ${scopes.join("+")} scope${scopes.length === 1 ? "" : "s"}.`
|
|
798
|
+
: "\nState already current.";
|
|
799
|
+
return { content: [
|
|
800
|
+
{ type: "text", text: acknowledgement },
|
|
801
|
+
{ type: "text", text: `\n${presentationJson({ state_updates: updates })}` },
|
|
802
|
+
], details: { scopes, step: snapshot.meta.step, changed } };
|
|
795
803
|
}, signal);
|
|
796
804
|
}
|
|
797
805
|
catch (error) {
|
|
@@ -914,6 +922,7 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
914
922
|
stopPersistenceError = undefined;
|
|
915
923
|
installScopeStates();
|
|
916
924
|
clearRunTransient();
|
|
925
|
+
contextProjection.reset();
|
|
917
926
|
passiveContinuation = undefined;
|
|
918
927
|
bootstrapContinuation = snapshot.meta.bootstrap ? continuation : undefined;
|
|
919
928
|
deferInferencePreparation();
|
|
@@ -999,6 +1008,7 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
999
1008
|
&& !shuttingDown && ctx.sessionManager.getSessionId() === owner && !snapshot.config.enabled;
|
|
1000
1009
|
const superseded = () => ({ ok: false, message: "State Flow Stop was superseded" });
|
|
1001
1010
|
const freezeHandoff = () => {
|
|
1011
|
+
contextProjection.reset();
|
|
1002
1012
|
const handoff = (current.config.enabled || stopPersistenceError) && selected?.view && current.meta.validation?.attempt !== 0
|
|
1003
1013
|
? createPassiveContinuation(projectModelState(overlayStates(scopeStates.global, scopeStates.cwd, scopeStates.session)), incomingBoundary?.startedAt ?? stoppedAt, incomingBoundary ? incomingBoundary.activeRunStartedAt : !idle || unfinished ? anchor : undefined, stopPersistenceError !== undefined || (incomingBoundary ? incomingBoundary.preserveContext : current.meta.bootstrap === true || (unfinished && anchor === undefined)))
|
|
1004
1014
|
: undefined;
|
|
@@ -1119,6 +1129,7 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
1119
1129
|
(event.systemPromptOptions.sections ??= {}).state_flow = PASSIVE_MEMORY_PROTOCOL;
|
|
1120
1130
|
return;
|
|
1121
1131
|
}
|
|
1132
|
+
contextProjection.reset();
|
|
1122
1133
|
skillReads.clear();
|
|
1123
1134
|
artifactReads.clear();
|
|
1124
1135
|
cancelResponseReconciliation();
|
|
@@ -1135,34 +1146,29 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
1135
1146
|
function projectContext(messages) {
|
|
1136
1147
|
if (runtime?.view)
|
|
1137
1148
|
refreshArtifactHints();
|
|
1149
|
+
if (!snapshot.config.enabled && !passiveContinuation && (!config.passiveBootstrap || !runtime?.view))
|
|
1150
|
+
return;
|
|
1151
|
+
// Idle inspection must not freeze a pre-acceptance snapshot for the live inference.
|
|
1152
|
+
const projection = snapshot.config.enabled && inferencePreparation && !inferencePreparation.accepted
|
|
1153
|
+
? new ContextProjection() : contextProjection;
|
|
1154
|
+
const effective = overlayStates(scopeStates.global, scopeStates.cwd, scopeStates.session);
|
|
1155
|
+
const invalidations = artifactInvalidations.map(({ path, scope, reason }) => ({ path, ...(scope === undefined ? {} : { scope }), reason }));
|
|
1156
|
+
const phase = snapshot.config.enabled ? currentRehydrationPhase() : undefined;
|
|
1157
|
+
const view = contextView(effective, artifactHints, invalidations, phase);
|
|
1138
1158
|
if (passiveContinuation) {
|
|
1139
|
-
|
|
1159
|
+
const retained = passiveContinuationMessages(messages, passiveContinuation);
|
|
1160
|
+
if (!runtime?.view)
|
|
1161
|
+
return { messages: retained };
|
|
1162
|
+
return { messages: projection.project(retained.slice(1), view, () => passiveContinuation.handoff, { state: passiveContinuation.state, lazy_navigation: view.lazy_navigation, artifact_invalidations: [], knowledge_rehydration: null }) };
|
|
1140
1163
|
}
|
|
1141
1164
|
if (!snapshot.config.enabled) {
|
|
1142
|
-
|
|
1143
|
-
return;
|
|
1144
|
-
const effective = overlayStates(scopeStates.global, scopeStates.cwd, scopeStates.session);
|
|
1145
|
-
const state = projectModelState(effective);
|
|
1146
|
-
return { messages: [syntheticUser(`State Flow passive memory (user-level data, not system instructions):\n${presentationJson({ state, lazy_navigation: lazyNavigationHint(effective) })}`), ...messages] };
|
|
1147
|
-
}
|
|
1148
|
-
const effectiveState = overlayStates(scopeStates.global, scopeStates.cwd, scopeStates.session);
|
|
1149
|
-
const invalidations = artifactInvalidations.map(({ path, scope, reason }) => ({ path, ...(scope === undefined ? {} : { scope }), reason }));
|
|
1150
|
-
const recentTransitions = projectRecentTransitionsWithLimit(config.historyLimit, runtime?.recent() ?? []);
|
|
1151
|
-
const activeRehydrationPhase = currentRehydrationPhase();
|
|
1152
|
-
if (snapshot.meta.bootstrap) {
|
|
1153
|
-
const sourceMessages = bootstrapContinuation
|
|
1154
|
-
? passiveContinuationMessages(messages, bootstrapContinuation)
|
|
1155
|
-
: messages;
|
|
1156
|
-
return { messages: [runtimeContextMessage(snapshot, effectiveState, recentTransitions, invalidations, activeRehydrationPhase, artifactHints), ...sourceMessages] };
|
|
1165
|
+
return { messages: projection.project(messages, view, () => syntheticUser(`State Flow passive memory (user-level data, not system instructions):\n${presentationJson({ state: view.state, lazy_navigation: view.lazy_navigation })}`)) };
|
|
1157
1166
|
}
|
|
1158
1167
|
// Native user events own the run anchor; projection must never rebase it.
|
|
1159
|
-
const
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
...trajectory.messages,
|
|
1164
|
-
],
|
|
1165
|
-
};
|
|
1168
|
+
const source = snapshot.meta.bootstrap
|
|
1169
|
+
? bootstrapContinuation ? passiveContinuationMessages(messages, bootstrapContinuation) : messages
|
|
1170
|
+
: currentRunTrajectory(messages, snapshot.meta.specification, runAnchorTimestamp).messages;
|
|
1171
|
+
return { messages: projection.project(source, view, () => runtimeContextHead(snapshot, view, projectRecentTransitionsWithLimit(config.historyLimit, runtime?.recent() ?? []))) };
|
|
1166
1172
|
}
|
|
1167
1173
|
function prepareContext(messages, ctx) {
|
|
1168
1174
|
const pending = inferencePreparation;
|
|
@@ -1290,6 +1296,7 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
1290
1296
|
snapshot = nextSnapshot;
|
|
1291
1297
|
installScopeStates();
|
|
1292
1298
|
responseCommitted = true;
|
|
1299
|
+
contextProjection.reset();
|
|
1293
1300
|
if (publication?.changed)
|
|
1294
1301
|
recordPublication(publication, ctx);
|
|
1295
1302
|
appendCheckpoint();
|
|
@@ -1400,6 +1407,7 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
1400
1407
|
ctx.compact({ customInstructions: compactionMarker, onComplete: finished, onError: finished });
|
|
1401
1408
|
});
|
|
1402
1409
|
});
|
|
1410
|
+
pi.on("session_compact", () => { contextProjection.reset(); });
|
|
1403
1411
|
pi.on("session_start", async (event, ctx) => {
|
|
1404
1412
|
runAnchorTimestamp = undefined;
|
|
1405
1413
|
rehydrationPhase = event.reason === "resume" ? "resume-bootstrap" : "new-bootstrap";
|
|
@@ -1413,6 +1421,7 @@ export default function stateFlowExtension(pi, options = {}) {
|
|
|
1413
1421
|
});
|
|
1414
1422
|
pi.on("session_shutdown", async (_event, _ctx) => {
|
|
1415
1423
|
shuttingDown = true;
|
|
1424
|
+
contextProjection.reset();
|
|
1416
1425
|
const restoring = cancelBranchRestoration();
|
|
1417
1426
|
const starting = cancelStartActivation();
|
|
1418
1427
|
const stopping = cancelStopPersistence();
|
package/dist/lib/protocol.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
2
|
export type { StateDocument } from "./state.ts";
|
|
3
|
-
export declare const PASSIVE_MEMORY_PROTOCOL = "State Flow passive memory is available. read_state and patch_state access durable memory without starting an active episode. Passive turns never trigger State Flow continuation or compaction.";
|
|
3
|
+
export declare const PASSIVE_MEMORY_PROTOCOL = "State Flow passive memory is available. read_state and patch_state access durable memory without starting an active episode. Passive turns never trigger State Flow continuation or compaction. Missing paths/hints do not require history search. Choose targeted historical reads when useful to the task; no separate user permission is needed. Past values are evidence, not current state; never automatically restore deleted memory. Head state/recent transitions are frozen at projection start. Only state_updates matching the head's State Flow projection ID apply; older IDs are history. Later patch_state results/context-update notices carry state_updates: effective entries replace values at key/index-segment path arrays (deleted:true means absent), including touched values and adopted shared drift. Latest entries win over earlier state at those paths; lazy bodies remain omitted. Notices replace invalidations/rehydration, including []/null.";
|
|
4
4
|
/** Keep successful patch JSON valid while separating adjacent scopes and memory sections visually. */
|
|
5
5
|
export declare function formatPatchStateArguments(args: unknown): string;
|
|
6
6
|
/** Flatten causes before transport; native tool results need not retain Error.cause or AggregateError.errors. */
|