@llblab/pi-kit 0.23.2 → 0.24.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.
- package/CHANGELOG.md +8 -0
- package/README.md +2 -2
- package/node_modules/@llblab/pi-state-flow/AGENTS.md +2 -2
- package/node_modules/@llblab/pi-state-flow/BACKLOG.md +3 -3
- package/node_modules/@llblab/pi-state-flow/CHANGELOG.md +12 -0
- package/node_modules/@llblab/pi-state-flow/README.md +1 -1
- package/node_modules/@llblab/pi-state-flow/dist/lib/context.d.ts +50 -1
- package/node_modules/@llblab/pi-state-flow/dist/lib/context.js +248 -5
- package/node_modules/@llblab/pi-state-flow/dist/lib/extension.js +36 -27
- package/node_modules/@llblab/pi-state-flow/dist/lib/protocol.d.ts +1 -1
- package/node_modules/@llblab/pi-state-flow/dist/lib/protocol.js +3 -2
- package/node_modules/@llblab/pi-state-flow/dist/package.json +1 -1
- package/node_modules/@llblab/pi-state-flow/dist/skills/state-flow-guide/SKILL.md +3 -1
- package/node_modules/@llblab/pi-state-flow/docs/architecture.md +6 -2
- package/node_modules/@llblab/pi-state-flow/docs/performance.md +48 -2
- package/node_modules/@llblab/pi-state-flow/docs/temporal-acceptance.md +4 -3
- package/node_modules/@llblab/pi-state-flow/lib/context.ts +223 -6
- package/node_modules/@llblab/pi-state-flow/lib/extension.ts +37 -32
- package/node_modules/@llblab/pi-state-flow/lib/protocol.ts +4 -2
- package/node_modules/@llblab/pi-state-flow/package.json +1 -1
- package/node_modules/@llblab/pi-state-flow/skills/state-flow-guide/SKILL.md +3 -1
- package/node_modules/@llblab/pi-telegram/AGENTS.md +1 -1
- package/node_modules/@llblab/pi-telegram/CHANGELOG.md +5 -0
- package/node_modules/@llblab/pi-telegram/dist/lib/bus-follower.d.ts +2 -0
- package/node_modules/@llblab/pi-telegram/dist/lib/bus-follower.js +55 -2
- package/node_modules/@llblab/pi-telegram/dist/lib/bus-leader.d.ts +21 -0
- package/node_modules/@llblab/pi-telegram/dist/lib/bus-leader.js +144 -1
- package/node_modules/@llblab/pi-telegram/dist/lib/bus.d.ts +9 -0
- package/node_modules/@llblab/pi-telegram/dist/lib/bus.js +19 -0
- package/node_modules/@llblab/pi-telegram/dist/lib/commands.d.ts +13 -0
- package/node_modules/@llblab/pi-telegram/dist/lib/commands.js +29 -6
- package/node_modules/@llblab/pi-telegram/dist/lib/extension.js +16 -0
- package/node_modules/@llblab/pi-telegram/dist/lib/locks.js +5 -1
- package/node_modules/@llblab/pi-telegram/dist/lib/polling.js +6 -2
- package/node_modules/@llblab/pi-telegram/dist/lib/threads.d.ts +7 -0
- package/node_modules/@llblab/pi-telegram/dist/lib/threads.js +13 -5
- package/node_modules/@llblab/pi-telegram/dist/package.json +1 -1
- package/node_modules/@llblab/pi-telegram/docs/multi-instance-bus.md +2 -2
- package/node_modules/@llblab/pi-telegram/docs/public-api.md +1 -1
- package/node_modules/@llblab/pi-telegram/lib/bus-follower.ts +79 -1
- package/node_modules/@llblab/pi-telegram/lib/bus-leader.ts +197 -0
- package/node_modules/@llblab/pi-telegram/lib/bus.ts +33 -0
- package/node_modules/@llblab/pi-telegram/lib/commands.ts +38 -6
- package/node_modules/@llblab/pi-telegram/lib/extension.ts +15 -0
- package/node_modules/@llblab/pi-telegram/lib/locks.ts +6 -1
- package/node_modules/@llblab/pi-telegram/lib/polling.ts +10 -2
- package/node_modules/@llblab/pi-telegram/lib/threads.ts +19 -5
- package/node_modules/@llblab/pi-telegram/package.json +1 -1
- package/package.json +3 -3
|
@@ -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
|
+
...(updates ? [{ 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();
|
|
@@ -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. 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.";
|
|
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. When present, patch_state results/context-update notices carry state_updates: effective entries replace values at key/index-segment path arrays (deleted:true means absent); direct writes need no echo; shared drift remains visible. 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. */
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const TASK_DRIVEN_HISTORY = "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.";
|
|
2
|
-
|
|
2
|
+
const PATCH_RESULT_PROTOCOL = "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. When present, patch_state results/context-update notices carry state_updates: effective entries replace values at key/index-segment path arrays (deleted:true means absent); direct writes need no echo; shared drift remains visible. Latest entries win over earlier state at those paths; lazy bodies remain omitted. Notices replace invalidations/rehydration, including []/null.";
|
|
3
|
+
export 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. ${TASK_DRIVEN_HISTORY} ${PATCH_RESULT_PROTOCOL}`;
|
|
3
4
|
const PATCH_DISPLAY_SECTION_KEYS = new Set(["global", "cwd", "session", "intents", "contract", "working", "artifacts", "response", "lazy"]);
|
|
4
5
|
/** Keep successful patch JSON valid while separating adjacent scopes and memory sections visually. */
|
|
5
6
|
export function formatPatchStateArguments(args) {
|
|
@@ -94,7 +95,7 @@ SCOPES: Use the narrowest scope: session=branch/run continuation by default; cwd
|
|
|
94
95
|
|
|
95
96
|
READ: Use read_state for concrete scope/retained-history gaps. lazy_navigation lists bounded effective lazy keys, not bodies. Unscoped=effective; effective/global/cwd/session select overlay or owner. Arrays use indices or [start..end]; keys gives structure, patch the intersected change. ${TASK_DRIVEN_HISTORY}
|
|
96
97
|
|
|
97
|
-
WRITE: patch_state is the sole model-authored semantic mutation mechanism; all supplied scopes are validated and durably accepted as one atomic transition. Call alone in an assistant response; await acceptance. Global/CWD use current canonical values after cancelable lock waiting. Correct repeats succeed without new revisions.
|
|
98
|
+
WRITE: patch_state is the sole model-authored semantic mutation mechanism; all supplied scopes are validated and durably accepted as one atomic transition. Call alone in an assistant response; await acceptance. Global/CWD use current canonical values after cancelable lock waiting. Correct repeats succeed without new revisions. ${PATCH_RESULT_PROTOCOL}
|
|
98
99
|
|
|
99
100
|
PATCH: Use global/cwd/session object patches for material updates, not acknowledgments. Omit empty scopes. artifacts/contract/working/intents are objects; lazy is ordinary JSON. Omitted fields persist. Never patch runtime config/meta/response. Objects merge recursively; arrays/primitives replace. An object containing only canonical "[N]" keys recursively patches array elements. Indexed deletion is forbidden; nested object null deletes; materialized null is forbidden.
|
|
100
101
|
|
|
@@ -32,7 +32,9 @@ Scopes overlay `global → cwd → session`: cross-project, project, branch/run.
|
|
|
32
32
|
|
|
33
33
|
## Read
|
|
34
34
|
|
|
35
|
-
Reuse sufficient visible state. `
|
|
35
|
+
Reuse sufficient visible state. The memory head and its recent transitions are frozen at projection start. Apply later `state_updates` only when their `projection` matches the head's `State Flow projection:` ID; older native results remain historical. Effective update paths are key/index arrays whose values replace that path, while `deleted: true` means absence. Current notices can replace invalidation lists or rehydration phase, including clearing them with `[]` or `null`; lazy bodies still require explicit reads.
|
|
36
|
+
|
|
37
|
+
`read_state` accepts `path` or `paths`, never both. Multi-path reads succeed or fail together. Projections: `value` (default), `keys` (structure), `patch` (intersecting change at the selected boundary).
|
|
36
38
|
|
|
37
39
|
Example arguments:
|
|
38
40
|
|
|
@@ -73,7 +73,11 @@ A changed accepted response is runtime-owned, session-only semantic state and ad
|
|
|
73
73
|
|
|
74
74
|
## Pi lifecycle
|
|
75
75
|
|
|
76
|
-
`patch_state` is the sole mutation tool. It acquires store exclusion before selecting current Global/CWD, stages the authored operations while preserving private Session ownership, and validates/publishes one atomic cohort. Replay, history and revisions use that actual basis; correct repeats create no semantic transition. It then acts as an inference barrier. Pi executes no sibling tools from the same assistant response; the next inference sees the rematerialized current effective state.
|
|
76
|
+
`patch_state` is the sole mutation tool. It acquires store exclusion before selecting current Global/CWD, stages the authored operations while preserving private Session ownership, and validates/publishes one atomic cohort. Replay, history and revisions use that actual basis; correct repeats create no semantic transition. It then acts as an inference barrier. Pi executes no sibling tools from the same assistant response; the next inference sees the rematerialized current effective state. Its successful native tool result includes a `state_updates` block, separate from the compact acknowledgement used by interactive rendering. `effective` entries contain a `path` array of exact object keys/array indices plus either a replacing `value` or `deleted: true`. These are accepted effective values, not authored merge operations: scope-local deletion can reveal a lower value, and higher scopes can mask an accepted lower-scope write. Entries conservatively cover changed scope fallbacks, masked or overlapping multi-scope touches and projected changes since the last communicated view, including shared refreshes before or during the transaction. Predictable direct non-null leaf writes are omitted when the accepted effective value matches the authored value and no other authored scope touches that path or its ancestors/descendants; disjoint multi-scope writes are independent. Explicit Session scalar/array replacements also omit matching receipts despite lower-scope overlap; ambiguous object merges and deletions remain conservative. Coalesced object additions/replacements can likewise omit an exact authored object, without suppressing foreign sibling fields. Indexed-array patch selectors become numeric update paths only against a communicated in-bounds array basis; object keys with the same spelling are literal. Deletions are omitted only when the accepted effective value equals the previously communicated value and no other authored scope overlaps. An effective-only head cannot prove a hidden lower-scope fallback from its own basis, so changed fallbacks remain visible. Artifact card replacements or merges can be omitted when their projected authored fields applied to an already communicated card exactly match the accepted card after stripping retired provenance; an unchanged previously communicated hint remains part of that known card. New, removed or changed hints and unexpected semantic drift remain visible. Lazy navigation exposes only bounded key/kind summaries, never bodies. An accepted summary needs no receipt when the complete previously communicated catalog and non-deleting top-level writes predict the entire accepted catalog; deletions, overlapping keys, incomplete catalogs and drift remain conservative. Unrelated unchanged branches are omitted. A result with no remaining reconciliation has only the acknowledgement. Arrays with changed length are replaced as a unit, while equal-length changes may target indices. Artifact cards use the normal metadata filter and touched cards replace their whole projected entry. Lazy bodies never enter this block; changed bounded `lazy_navigation` may accompany it. Each update carries a volatile `projection` ID matching the head's `State Flow projection:` text block. The protocol applies only matching updates: retained native results with older IDs remain historical and cannot override a new head. Within that projection, the latest entry supersedes earlier context only at its path. The context domain owns this projection, not storage or the lifecycle composition root.
|
|
77
|
+
|
|
78
|
+
`ContextProjection` freezes the whole initial head, including its timestamp, specification and recent transitions. Signal-less inspection before active preparation accepts uses a disposable projection, so it cannot freeze a stale specification or pre-maintenance state for the live inference. Active/bootstrap runs rebase on new input and accepted completion; a native continuation after completion gets a fresh head without resurrecting the completed specification. Passive runs keep their head across ordinary user turns. Both modes rebase on Start/Stop, selection/resume/reload, native compaction and a removed/replaced native prefix. No per-patch or size-threshold rebase exists. The volatile ID is not a semantic transition identity and enters neither canonical files nor runtime metadata.
|
|
79
|
+
|
|
80
|
+
Unreported state changes, changing artifact hints, invalidation lists and rehydration phase append as synthetic tail notices at the native-message boundary where they first appeared. Later inference retains those exact messages at those positions before new native messages, rather than moving them to the latest tail. Empty invalidation lists and a null rehydration envelope explicitly clear earlier notices. Accepted patch receipts advance the communicated view; their state deltas are not redundantly emitted as synthetic notices. Stop handoff uses its captured state as the initial basis, including changes before its first projection. Skill acquisition guidance remains attached to native read results. The cache owns no persistence, publication authority or continuation scheduler, and does not redact native history.
|
|
77
81
|
|
|
78
82
|
Tool preflight follows Pi's public `getLeafEntry()` / `getEntry(parentId)` links to the nearest assistant containing the current call ID. It inspects that response's complete tool batch without constructing the whole branch or caching a batch across calls/selections. Foreign custom entries and earlier sibling results remain in the native trace. A missing call ID still searches the selected ancestry and preserves the existing unmatched-call behavior; this is not an unconditional constant-time guarantee. See [measured traversal evidence](performance.md#tool-preflight-parent-traversal).
|
|
79
83
|
|
|
@@ -109,7 +113,7 @@ After an accepted non-bootstrap run settles with no queued input, State Flow may
|
|
|
109
113
|
|
|
110
114
|
Pi 0.87 supports retain-none boundary compactions, but State Flow intentionally does not use them. Completed canonical state omits the exact user prompt, and foreign custom context can legitimately occur inside the latest retained iteration; hiding both would make the projected semantic state a lossy substitute for native context. Unknown or smaller usage, foreign custom metadata or native `custom_message` context in the removed prefix, stale selection, Stop/bootstrap/error/abort, and pending input do not produce this boundary. User manual and native threshold/overflow compaction remain unmodified; unaccepted work stays under Pi's native compaction contract.
|
|
111
115
|
|
|
112
|
-
The Pi adapter passes its raw cached scope overlay to `
|
|
116
|
+
The Pi adapter passes its raw cached scope overlay to the context domain's `contextView`, which owns model sanitization. `runtimeContextHead` serializes that already-projected view only when the projection cache needs a new head; `runtimeContextMessage` remains the raw-input convenience builder. The adapter never sanitizes or pre-projects the overlay itself. `currentRunTrajectory` selects a unique captured user timestamp without requiring specification-text equality: Pi may append image normalization hints after `before_agent_start`. Without a captured timestamp, only a unique exact specification match can select a projected suffix. Missing, nonfinite, or ambiguous selection retains all available context. Projection never assigns `runAnchorTimestamp`; native user events own that lifecycle identity, so a projection fallback cannot become compaction authority. With a selected boundary, one retained-message array preserves foreign custom messages at every position and ordinary messages from the original run, including images, tools and steering, without copying discarded ordinary prefixes. The necessary scan and Pi's earlier native-message clone remain history-dependent. See [context-cost evidence](performance.md#context-projection-and-trajectory-selection).
|
|
113
117
|
|
|
114
118
|
## Storage and identity
|
|
115
119
|
|
|
@@ -28,7 +28,53 @@ The v3 report records `promptPrefixRuns` for each State Flow and native Pi user
|
|
|
28
28
|
|
|
29
29
|
A bounded local sample used `BENCH_PATCHES=2 BENCH_SAMPLES=1 BENCH_ROUNDS=2 BENCH_STATE_BYTES=1024 BENCH_POST_RESUME=1 npm run benchmark` on Node 26.8.1 Linux/x64 and Pi/AI 0.87.0. The runtime-source SHA-256 was `0f4eea2394c52c6ac545260cae967a16581b8768a24a8c611c13a389e6df01a9`, workload-source SHA-256 `e9b8158d953d8622776aa075835c4aa95c98855ed5c90e85de05dccae1087e91`, base commit `5f0f688650738d4d0e9850b2529b0273c47121a8` (the measured tree had uncommitted 0.18.1 changes). Both source hashes stayed unchanged during the run and all correctness phases passed. At user run 1, native Pi's second inference shared 2,776 of 2,777 prior serialized bytes; State Flow shared 9,365 of 9,467 at inference 2 and 9,173 with inference 2 at inference 3 (one accepted barrier). In both State Flow runs, the 21-byte serialized `specification` value duplicated the 21-byte current native user string value; native Pi had no projection field. These are synthetic short-run observations, not representative cache-hit rates or timing predictions.
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
Byte-prefix measurements omit provider framing, tool schemas, tokenization, cache policies and quality. In particular, serialized synthetic-message timestamps can shorten this proxy prefix without proving provider-visible cache churn. Fresh state visibility after barriers must be validated independently of prefix preservation.
|
|
32
|
+
|
|
33
|
+
### Trajectory-dominant baseline
|
|
34
|
+
|
|
35
|
+
The opt-in `BENCH_PREFIX=1` probe in the [benchmark guide](../benchmarks/README.md) exercises active memory, ordinary passive memory and Stop handoff separately. Each measured run issues six 20,497-byte native reads, a patch, two more reads and a second patch. Exact read content remains visible through all eleven inferences; both patches and terminal completion are checked outside the provider. The v3 `trajectory[].promptPrefixRuns` entries retain every inference, not just aggregate ratios.
|
|
36
|
+
|
|
37
|
+
Baseline: Node 26.8.1 Linux/x64, Pi/AI 0.87.0, base commit `2834dcb447f58867b579c6f2709bc71211b9537a` with uncommitted benchmark changes; runtime SHA-256 `a5a961d4d706d4a2d6649201553e39484d0e4c29d27cf4014f072b39cd78a01a`, workload SHA-256 `1785e73e4992c3d986d2851f2d81963c0b0cfde8ceacfd4337659ebc9d1492ba`. Both source identities remained unchanged and the native workload passed. The local report is `/tmp/state-flow-prefix-baseline.json`; the reproducible command and measurements below do not depend on retaining that temporary file.
|
|
38
|
+
|
|
39
|
+
Each cell is **shared prefix / current serialized context bytes**; `—` means first inference. Inferences 8 and 11 follow accepted patches.
|
|
40
|
+
|
|
41
|
+
| Inference | Active | Passive | Stop handoff |
|
|
42
|
+
| ---: | ---: | ---: | ---: |
|
|
43
|
+
| 1 | — / 9860 | — / 7197 | — / 6019 |
|
|
44
|
+
| 2 | 9732 / 31025 | 5801 / 28360 | 6018 / 27183 |
|
|
45
|
+
| 3 | 9731 / 52193 | 5801 / 49528 | 27182 / 48353 |
|
|
46
|
+
| 4 | 9732 / 73359 | 5801 / 70696 | 48352 / 69519 |
|
|
47
|
+
| 5 | 9732 / 94528 | 5801 / 91865 | 69518 / 90688 |
|
|
48
|
+
| 6 | 9733 / 115695 | 5802 / 113034 | 90687 / 111857 |
|
|
49
|
+
| 7 | 9732 / 136864 | 5801 / 134201 | 111856 / 133026 |
|
|
50
|
+
| 8 | 9212 / 137813 | 5666 / 134999 | 133025 / 133800 |
|
|
51
|
+
| 9 | 9907 / 158983 | 5825 / 156169 | 133799 / 154965 |
|
|
52
|
+
| 10 | 9907 / 180150 | 5825 / 177338 | 154964 / 176134 |
|
|
53
|
+
| 11 | 9235 / 181075 | 5689 / 178112 | 176133 / 176908 |
|
|
54
|
+
|
|
55
|
+
In that baseline, Stop handoff already reused a frozen message, unlike active and ordinary passive projection. A warm prefix alone does not prove updated memory reaches the model; freshness has separate native-SDK regressions.
|
|
56
|
+
|
|
57
|
+
### Frozen-head measurement
|
|
58
|
+
|
|
59
|
+
The implemented projection freezes whole heads, including timestamps, and delivers accepted values and changing notices at stable tail positions. Active completion/new runs, native compaction/selection and Start/Stop are cache boundaries; passive user turns and patches are not. Volatile projection IDs distinguish current updates from retained results after a rebase. See [projection semantics](architecture.md#pi-lifecycle) for ownership and limits.
|
|
60
|
+
|
|
61
|
+
The identical trajectory workload (`1785e73e4992c3d986d2851f2d81963c0b0cfde8ceacfd4337659ebc9d1492ba`) on the same Node/Pi stack measured runtime SHA-256 `d2c424a52d55b0c1ca47a8b1a1beba9c0dda665c8f024d6aa3b6ad95af9d3b46`, with unchanged base commit and uncommitted implementation changes. Both source identities remained stable; all native workload assertions passed. Local report: `/tmp/state-flow-prefix-after.json`.
|
|
62
|
+
|
|
63
|
+
| Inference | Active | Passive | Stop handoff |
|
|
64
|
+
| ---: | ---: | ---: | ---: |
|
|
65
|
+
| 1 | — / 10461 | — / 7985 | — / 6620 |
|
|
66
|
+
| 2 | 10460 / 31625 | 7984 / 29148 | 6619 / 27784 |
|
|
67
|
+
| 3 | 31624 / 52792 | 29147 / 50316 | 27783 / 48952 |
|
|
68
|
+
| 4 | 52791 / 73959 | 50315 / 71484 | 48951 / 70120 |
|
|
69
|
+
| 5 | 73958 / 95127 | 71483 / 92653 | 70119 / 91287 |
|
|
70
|
+
| 6 | 95126 / 116295 | 92652 / 113822 | 91286 / 112456 |
|
|
71
|
+
| 7 | 116294 / 137463 | 113821 / 134989 | 112455 / 133625 |
|
|
72
|
+
| 8 | 137462 / 138416 | 134988 / 135941 | 133624 / 134579 |
|
|
73
|
+
| 9 | 138415 / 159580 | 135940 / 157106 | 134578 / 155742 |
|
|
74
|
+
| 10 | 159579 / 180746 | 157105 / 178275 | 155741 / 176911 |
|
|
75
|
+
| 11 | 180745 / 181697 | 178274 / 179229 | 176910 / 177863 |
|
|
76
|
+
|
|
77
|
+
Every continuation shares **all prior serialized bytes except the closing array bracket**, including both patch barriers in all three modes. At inference 8, the active prefix grows from 9,212 baseline bytes to 137,462; passive grows from 5,666 to 134,988. Initial contexts grow modestly because result guidance and projection identity are explicit. These are message-byte measurements, not provider cache accounting, latency, token-cost or quality guarantees. Regression tests assert prefix equality independently of exact host timestamps/IDs; separate native tests prove accepted-state freshness, repeated barriers, passive cross-turn stability and bootstrap rebasing.
|
|
32
78
|
|
|
33
79
|
## Current cost model
|
|
34
80
|
|
|
@@ -49,7 +95,7 @@ This is a bounded-allocation improvement, not an unconditional constant-time cla
|
|
|
49
95
|
|
|
50
96
|
## Context projection and trajectory selection
|
|
51
97
|
|
|
52
|
-
|
|
98
|
+
The context domain projects the cached semantic overlay once per context emission to compare current state with its last communicated view. It serializes the complete head only at a projection boundary; later synthetic notices retain their original native-message positions. Projection caching targets request-prefix stability, not constant-time state processing: view copies/diffs remain state-dependent and notices accumulate until a natural reset, without a size threshold. `currentRunTrajectory` allocates one retained-message array rather than arrays for discarded ordinary prefixes. Foreign custom context may require scanning earlier entries, and Pi may clone native messages before the extension runs.
|
|
53
99
|
|
|
54
100
|
`tests/context.test.ts` exercises small and large semantic payloads, zero and two hundred prior request/answer pairs, repeated requests, stale/missing anchors, foreign custom messages, and post-barrier context emission. These tests assert projection counts and retained identities; they impose no wall-time threshold.
|
|
55
101
|
|
|
@@ -15,8 +15,8 @@ This is a maintained property-to-test map for the current canonical-file contrac
|
|
|
15
15
|
9. **Unchanged scopes stay stable:** The sparse temporal example checks global state at a boundary where only CWD/session change; “true no-ops do not enter history while response-only changes do” also checks that an unchanged session receives no new patch.
|
|
16
16
|
10. **Historical deletion overlay:** `tests/temporal.test.ts` — “mixed sparse changes and deletion overlays match an independent snapshot oracle through compaction” explicitly checks session → CWD → global fallback and retained historical values.
|
|
17
17
|
11. **Barrier shifts current to offset 1:** `tests/integration.test.ts` — “real Pi patch_state barriers rematerialize every scope before the next inference” observes the predecessor immediately after a barrier.
|
|
18
|
-
12. **Next inference sees new current state:** The same real-Pi test
|
|
19
|
-
13. **No automatic old full-state duplication:** `tests/context.test.ts` — “projects only the latest seven compact accepted transitions” rejects full-state records in transition context. The real-Pi barrier test requires exactly one
|
|
18
|
+
12. **Next inference sees new current state:** The same real-Pi test reconstructs current state from the frozen head plus matching-projection result/tail replacements after session, CWD, and global barriers. “real Pi reads prior scoped state lazily after a barrier and rejects path offset eight without a transition” adds model-tool access to the predecessor.
|
|
19
|
+
13. **No automatic old full-state duplication:** `tests/context.test.ts` — “projects only the latest seven compact accepted transitions” rejects full-state records in transition context. The real-Pi barrier test requires exactly one frozen runtime head per inference, with fresh changes in matching-projection tails. Explicitly requested history remains ordinary tool-result trajectory, not eager snapshot injection.
|
|
20
20
|
14. **Accepted response changes are transitions:** `tests/extension.test.ts` verifies that an ordinary accepted answer becomes runtime-owned `response` without a finalization patch or fallback inference, and “an empty accepted answer finalizes the run and stores an empty response” proves `""` is accepted after an earlier barrier. `tests/transition.test.ts` proves the empty value is an ordinary Session-owned semantic change when it replaces prior text.
|
|
21
21
|
15. **Correct repeats succeed without semantic changes:** “accepts canonical atomic scope patches and correct repeats without another checkpoint” preserves canonical bytes and native checkpoint count on repetition, while still rejecting empty supplied scopes and obsolete finalization-shaped calls. Runtime current-head witnesses preserve revisions, lineage and step across identical patches and compilations; a changed accepted response remains a runtime-owned transition.
|
|
22
22
|
16. **Configured hot-history bounds:** `tests/temporal.test.ts` verifies hot-range and unavailable pre-origin boundaries; the real-Pi history-reader test rejects offset eight at the default limit seven without a transition. `tests/config.test.ts` exercises materialized and scope patch-history paths at limits 0, 1, 7, and 12, including single-path/one-item batch reads above seven and distinct configured versus actually retained boundaries.
|
|
@@ -56,9 +56,10 @@ This is a maintained property-to-test map for the current canonical-file contrac
|
|
|
56
56
|
- `tests/integration.test.ts` native new/resume/fork witnesses preserve canonical files and retained boundaries. Fork witnesses cover selected private state over current shared layers, fresh child ownership, reload/resume, disabled-source fencing, identity/CWD refusal, retry, and expired-boundary refusal. `tests/runtime.test.ts` restore/fork provenance witnesses preserve current-head, untouched-path, and shared evidence while discarding evidence for later-changed private artifacts, including change-away-and-back. The test-only provenance reader is checked against nonempty canonical `meta.json` evidence in every scope before and after reload; native restore/fork tests verify missing-evidence invalidation, explicit recompilation, parent preservation, and provenance across reload.
|
|
57
57
|
- `tests/recovery.test.ts` and `tests/runtime.test.ts` distinguish malformed checkpoints from expired retained boundaries, prevent fallthrough or newer-state substitution, and require detached single-use restoration followed by canonical origin acceptance.
|
|
58
58
|
- `tests/extension.test.ts` — “tool preflight walks only the selected native suffix for matching calls, without rebuilding a branch” uses real in-memory `SessionManager` trees with zero/two hundred prior request-answer pairs, intervening custom/results, and a later unselected assistant reusing a call ID. It requires exact parent visits, no full-branch construction, preserved sibling/duplicate-patch rejection and unchanged complete native entries. The native sibling-tool witness in `tests/integration.test.ts` separately observes all three start/end events and requires zero branch reads during preflight/execution while accepting the correct state/answer.
|
|
59
|
+
- `tests/context.test.ts` proves whole-head identity, stable-position changing/cleared notices, drift since the last communicated view, no redundant post-receipt notices, and reset identity fencing. Native “real Pi bootstrap/passive keeps a byte-stable head” witnesses repeated barriers, active completion rebasing and passive cross-turn continuity. “real Pi passive reload distinguishes retained old receipts from the refreshed head” keeps old native results while requiring a different projection ID and the newly adopted shared value. The trajectory benchmark's contract test requires every continuation prefix to equal the entire preceding serialized array minus its closing bracket in active, passive and Stop-handoff modes; this is not provider cache accounting.
|
|
59
60
|
- `tests/context.test.ts` — “enabled context projects the complete overlay once in ordinary and bootstrap runs” counts one full-overlay clone at 8 KiB and 1 MiB while preserving selected/cached state, native entries, input messages and artifact semantics. “current run trajectory allocates no arrays of discarded ordinary history” observes source-derived arrays after zero/two hundred historical request-answer pairs while preserving foreign context, current tools, steering, reference identity and order. Anchor tests prefer captured identity over normalized text, retain images/tools/steering, and preserve available context on missing/nonfinite/colliding identities or ambiguous specification matches; unique text fallback remains projection-only. The native barrier witness separately counts one marked full-overlay clone per post-barrier `emitContext()` invocation; it still requires each next inference to see all accepted scope changes.
|
|
60
61
|
- `tests/compaction.test.ts` owns State Flow's completed-run compaction policy: captured run-anchor retention across steering/tools, missing/ambiguous/unfinished-anchor refusal, large-history eligibility, generation-owned invocation, foreign `custom`/`custom_message` prefix refusal, queued-work exclusion, stale selection, benign preparation refusal and shutdown fencing. Its harness emits native user `message_end` and checks that repeated projection plus steering cannot promote missing, ambiguous or unobserved anchors into compaction authority. The native “real Pi compaction retains the original run through steering, tool results, and foreign context” cases actually compact plaintext and SDK-normalized image requests with two steering messages, a successful read/patch, and persisted custom context; image/read-result evidence reaches later model calls, exact message ids survive reload, the trace prefix stays byte-identical, and no model summary is requested. The short “real Pi retains a normalized image and read evidence through steering without compaction” controls check State Flow enabled/disabled parity before any compaction. Existing native witnesses cover ordinary compaction/cold resume and leave threshold compaction after partial tool work native.
|
|
61
|
-
- `tests/integration.test.ts` — “real Pi boundary continuation keeps accepted State Flow memory” exercises actual companion `turn_end` and `agent_before_settle` draft entries and continuation. Each next provider input has
|
|
62
|
+
- `tests/integration.test.ts` — “real Pi boundary continuation keeps accepted State Flow memory” exercises actual companion `turn_end` and `agent_before_settle` draft entries and continuation. Each next provider input has one freshly rebased head after accepted completion and matching-projection updates after a continued patch, the accepted response and current tool declarations. There is only one `before_agent_start`, no restored specification in runtime checkpoints or model projection, exactly one requested continuation and correct final response/step. The pure “projects accepted memory without resurrecting a completed specification for boundary continuation” witness preserves source state, omits lazy bodies, and keeps available context when both specification and native capture are absent.
|
|
62
63
|
- `tests/integration.test.ts` — “real Pi context edits remain canonical through tools, tree selection and reload” proves native user-content replacement, assistant/custom-message omission and live tool-result replacement reach provider input without resurrecting raw history. Selecting an unedited branch restores its native view without overwriting accepted semantic files; selecting the edited branch and reloading preserves its edits and independent memory. Raw JSONL stays append-only.
|
|
63
64
|
- `tests/integration.test.ts` — “real Pi structured prompt composes with context hooks” covers active, passive, disabled and explicit foreign-force modes. Conversation hooks exclude systems; full-system hooks receive them; companion before-run and per-request sections plus actual tool declarations survive. User specifications remain outside system authority. Stop/Start on subsequent user requests removes/reinstates the owned section once.
|
|
64
65
|
- `tests/integration.test.ts` — “real Pi preserves native run identity across mode toggles” covers initially enabled, mid-tool Start and repeated Start/Stop with passive bootstrap on/off. Actual provider inputs retain original user/read evidence but not the preceding disabled request; persisted Stop markers name the observed native user, and fixture reload preserves trajectory, frozen state and raw trace prefix. `tests/compaction.test.ts` — “native session boundaries invalidate observed run capture without projection reacquisition” pairs session-start/tree resets with an admitted unchanged-run control. Run identity remains native rather than guessed; an independent optional Stop-marker flag preserves uncompiled bootstrap context without changing the captured anchor.
|