@astrosheep/pi-context 0.24.0 → 0.25.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.
Files changed (78) hide show
  1. package/README.md +52 -5
  2. package/dist/build-info.json +4 -0
  3. package/dist/extension.js +1861 -0
  4. package/dist/src/context/budget.js +150 -0
  5. package/dist/src/context/context-window.js +97 -0
  6. package/dist/src/context/prompts.js +94 -0
  7. package/dist/src/context/reset-lifecycle.js +134 -0
  8. package/dist/src/context/runtime.js +236 -0
  9. package/dist/src/context/thresholds.js +62 -0
  10. package/dist/src/dream/cli.js +1 -1
  11. package/dist/src/dream/doctor.js +34 -6
  12. package/dist/src/dream/runner.js +1 -1
  13. package/dist/src/dream/settings.js +30 -0
  14. package/dist/src/{history-tools.js → history/history-tools.js} +3 -3
  15. package/dist/src/{history.js → history/history.js} +8 -46
  16. package/dist/src/index.js +27 -94
  17. package/dist/src/notes/address.js +97 -16
  18. package/dist/src/notes/frontmatter.js +18 -3
  19. package/dist/src/notes/notes-snapshot.js +30 -0
  20. package/dist/src/notes/paths.js +64 -7
  21. package/dist/src/notes/session-replay.js +41 -0
  22. package/dist/src/notes/store.js +76 -22
  23. package/dist/src/notes/tools.js +7 -7
  24. package/dist/src/protocol.js +7 -5
  25. package/dist/src/settings.js +16 -0
  26. package/dist/src/tool-schema.js +1 -1
  27. package/dist/test/agent-loop.test.js +813 -221
  28. package/dist/test/boot.integration.test.js +167 -0
  29. package/dist/test/budget-settings.integration.test.js +126 -0
  30. package/dist/test/doctor.test.js +14 -36
  31. package/dist/test/dream.test.js +37 -380
  32. package/dist/test/helpers/extension.js +393 -0
  33. package/dist/test/history.integration.test.js +316 -0
  34. package/dist/test/notes.integration.test.js +273 -0
  35. package/dist/test/notes.test.js +40 -359
  36. package/dist/test/reset-lifecycle.test.js +248 -180
  37. package/docs/architecture.md +35 -18
  38. package/docs/reset-lifecycle.md +16 -14
  39. package/package.json +11 -10
  40. package/src/context/budget.ts +148 -0
  41. package/src/context/context-window.ts +103 -0
  42. package/src/context/prompts.ts +111 -0
  43. package/src/context/reset-lifecycle.ts +145 -0
  44. package/src/context/runtime.ts +246 -0
  45. package/src/context/thresholds.ts +78 -0
  46. package/src/dream/cli.ts +1 -1
  47. package/src/dream/doctor.ts +27 -6
  48. package/src/dream/runner.ts +1 -1
  49. package/src/dream/settings.ts +32 -0
  50. package/src/{history-tools.ts → history/history-tools.ts} +3 -3
  51. package/src/{history.ts → history/history.ts} +9 -48
  52. package/src/index.ts +27 -89
  53. package/src/notes/address.ts +82 -16
  54. package/src/notes/frontmatter.ts +20 -3
  55. package/src/notes/notes-snapshot.ts +40 -0
  56. package/src/notes/paths.ts +64 -7
  57. package/src/notes/session-replay.ts +53 -0
  58. package/src/notes/store.ts +78 -25
  59. package/src/notes/tools.ts +7 -7
  60. package/src/protocol.ts +7 -5
  61. package/src/settings.ts +20 -0
  62. package/src/tool-schema.ts +1 -2
  63. package/dist/src/budget.js +0 -65
  64. package/dist/src/notes/model.js +0 -101
  65. package/dist/src/prompts.js +0 -88
  66. package/dist/src/reset-lifecycle.js +0 -155
  67. package/dist/src/thresholds.js +0 -102
  68. package/dist/src/warning.js +0 -44
  69. package/dist/test/coherence.test.js +0 -371
  70. package/dist/test/history.test.js +0 -26
  71. package/dist/test/integration.test.js +0 -1759
  72. package/dist/test/pagination.property.test.js +0 -471
  73. package/src/budget.ts +0 -67
  74. package/src/notes/model.ts +0 -109
  75. package/src/prompts.ts +0 -91
  76. package/src/reset-lifecycle.ts +0 -173
  77. package/src/thresholds.ts +0 -110
  78. package/src/warning.ts +0 -46
@@ -1,155 +0,0 @@
1
- /** A reset request is session-local. Only this module schedules compaction/continuation. */
2
- export function registerResetLifecycle(pi, options) {
3
- let state = { phase: "idle" };
4
- let handledEntry;
5
- let active = true;
6
- const release = (attempt) => {
7
- if (attempt.settled)
8
- return;
9
- attempt.settled = true;
10
- if (state.phase === "compacting" && state.attempt === attempt) {
11
- state = { phase: "idle" };
12
- handledEntry = undefined;
13
- }
14
- attempt.release();
15
- };
16
- const clear = () => {
17
- if (state.phase === "compacting")
18
- release(state.attempt);
19
- state = { phase: "idle" };
20
- handledEntry = undefined;
21
- };
22
- const valid = (request, ctx) => active && options.isEnabled() && state.phase === "compacting" && state.attempt === request && ctx.sessionManager.getSessionId() === request.sessionId;
23
- const begin = (ctx) => {
24
- let releaseWait;
25
- const request = {
26
- completed: false,
27
- explicit: true,
28
- nextRequested: false,
29
- continuationStarted: false,
30
- sessionId: ctx.sessionManager.getSessionId(),
31
- settled: false,
32
- wait: new Promise((resolve) => { releaseWait = resolve; }),
33
- release: () => releaseWait(),
34
- };
35
- state = { phase: "compacting", attempt: request };
36
- const onError = (error) => {
37
- if (!valid(request, ctx))
38
- return;
39
- release(request);
40
- // Do not retry from settled in a tight loop. A later prompt may trigger a
41
- // native reset or explicitly request one.
42
- ctx.ui.notify(`pi-context: reset did not complete (${error.message}). The conversation is retained; resume with another prompt.`, "warning");
43
- };
44
- try {
45
- ctx.compact({
46
- onComplete: () => {
47
- if (!valid(request, ctx))
48
- return;
49
- // session_compact only confirms the boundary. onComplete runs after
50
- // Pi clears compaction state; sending inside the hook starts too early.
51
- // A queued user prompt may already have started at compaction_end.
52
- if (request.completed && ctx.isIdle() && !ctx.hasPendingMessages()) {
53
- // The SDK detaches sendMessage, so own the next settled event before
54
- // starting it. The originating agent_settled handler awaits wait.
55
- if (request.continuationStarted)
56
- return;
57
- request.continuationStarted = true;
58
- try {
59
- pi.sendMessage(options.continuation, { triggerTurn: true });
60
- }
61
- catch (error) {
62
- onError(error instanceof Error ? error : new Error(String(error)));
63
- }
64
- return;
65
- }
66
- release(request);
67
- },
68
- onError,
69
- });
70
- }
71
- catch (error) {
72
- onError(error instanceof Error ? error : new Error(String(error)));
73
- }
74
- return request;
75
- };
76
- // State is intentionally not resumed from a pending request: a loaded session must
77
- // not execute work from a tool that belonged to a previous runtime or tree branch.
78
- pi.on("session_start", () => { clear(); active = true; });
79
- pi.on("session_shutdown", () => { clear(); active = false; });
80
- pi.on("session_tree", clear);
81
- pi.on("agent_end", (_event, ctx) => {
82
- if (!active || !options.isEnabled())
83
- return;
84
- if (ctx.signal?.aborted) {
85
- // Esc cancels the user's run. Do not reset or resurrect it at settled.
86
- clear();
87
- }
88
- });
89
- pi.on("agent_settled", (_event, ctx) => {
90
- if (!active || !options.isEnabled() || !ctx.isIdle())
91
- return;
92
- if (state.phase === "compacting" && state.attempt.continuationStarted) {
93
- const preceding = state.attempt;
94
- if (!preceding.nextRequested) {
95
- release(preceding);
96
- return;
97
- }
98
- // This settled event belongs to the continuation started by preceding.
99
- // If it requested another reset, retain preceding until that reset's own
100
- // continuation settles. Its eventual nested handler only releases its own
101
- // waiter, so it never awaits itself.
102
- const next = begin(ctx);
103
- return next.wait.then(() => release(preceding));
104
- }
105
- if (state.phase !== "requested")
106
- return;
107
- // One owner for requested resets. Consume the request before any external call;
108
- // repeated settled events and reentrant callbacks are harmless.
109
- return begin(ctx).wait;
110
- });
111
- pi.on("session_before_compact", (event, ctx) => {
112
- if (!active || !options.isEnabled())
113
- return undefined;
114
- if (event.signal.aborted)
115
- return { cancel: true };
116
- // Automatic threshold/overflow compactions reset on the spot — no model turn.
117
- // The warning steer fired earlier (see warning.ts); what crosses the reserve
118
- // line now is the wipe itself.
119
- try {
120
- return options.buildReset(event, ctx, state.phase === "requested");
121
- }
122
- catch (error) {
123
- ctx.ui.notify(`pi-context: could not build reset (${String(error)}).`, "warning");
124
- return { cancel: true }; // Never fall through to a generated default summary.
125
- }
126
- });
127
- pi.on("session_compact", (event, ctx) => {
128
- if (!active || !options.isEnabled() || handledEntry === event.compactionEntry.id)
129
- return;
130
- if (!options.isCurrentReset(event.compactionEntry.id, ctx))
131
- return;
132
- handledEntry = event.compactionEntry.id;
133
- if (state.phase === "compacting")
134
- state.attempt.completed = !event.willRetry;
135
- else
136
- state = { phase: "idle" };
137
- // A native compaction (including overflow retry) owns its own scheduling.
138
- // Only a reset we requested gets a continuation from our onComplete callback.
139
- options.onReset(event.compactionEntry.id);
140
- });
141
- return {
142
- request() {
143
- if (state.phase === "idle") {
144
- state = { phase: "requested" };
145
- return "rollover_requested";
146
- }
147
- if (state.phase === "compacting" && state.attempt.continuationStarted && !state.attempt.nextRequested) {
148
- state.attempt.nextRequested = true;
149
- return "rollover_requested";
150
- }
151
- return "rollover_already_pending";
152
- },
153
- clear,
154
- };
155
- }
@@ -1,102 +0,0 @@
1
- import { SettingsManager } from "@earendil-works/pi-coding-agent";
2
- import { PI_CONTEXT_SETTINGS_KEY, PI_CONTEXT_DREAMER_KEY, DEFAULT_RESERVE_TOKENS, DEFAULT_REMINDER_MARGIN_TOKENS, WARNING_RUNWAY_TOKENS } from "./protocol.js";
3
- function isSettingsObject(value) {
4
- return typeof value === "object" && value !== null && !Array.isArray(value);
5
- }
6
- /** Read the raw "pi-context" object from one parsed settings scope. */
7
- function piContextSettings(settings) {
8
- if (!isSettingsObject(settings))
9
- return {};
10
- const value = settings[PI_CONTEXT_SETTINGS_KEY];
11
- return isSettingsObject(value) ? value : {};
12
- }
13
- /** Merge the global and project "pi-context" objects per key; project wins, mirroring Pi's deep merge. */
14
- export function mergePiContextSettings(globalSettings, projectSettings) {
15
- const merged = { ...piContextSettings(globalSettings), ...piContextSettings(projectSettings) };
16
- return { reminderMarginTokens: merged.reminderMarginTokens, dreamer: merged.dreamer };
17
- }
18
- /** A margin is usable only as a positive integer; anything else is ignored. */
19
- function validMargin(raw) {
20
- if (typeof raw !== "number" || !Number.isSafeInteger(raw) || raw <= 0)
21
- return undefined;
22
- return raw;
23
- }
24
- /**
25
- * Pure derivation of the thresholds from Pi's reserve: the reminder fires at reserve
26
- * plus the pi-context margin, the warning steer at reserve plus WARNING_RUNWAY_TOKENS.
27
- * An invalid margin degrades to the default and reports one warning. Pi's automatic
28
- * threshold/overflow compaction itself resets immediately, with no model turn.
29
- */
30
- export function deriveThresholds(reserveTokens, margins) {
31
- const warnings = [];
32
- const reminderKey = `${PI_CONTEXT_SETTINGS_KEY}.reminderMarginTokens`;
33
- let reminderMargin;
34
- if (margins.reminderMarginTokens === undefined)
35
- reminderMargin = DEFAULT_REMINDER_MARGIN_TOKENS;
36
- else {
37
- const parsed = validMargin(margins.reminderMarginTokens);
38
- if (parsed === undefined) {
39
- warnings.push(`pi-context: ${reminderKey} must be a positive integer; using default ${DEFAULT_REMINDER_MARGIN_TOKENS}.`);
40
- reminderMargin = DEFAULT_REMINDER_MARGIN_TOKENS;
41
- }
42
- else
43
- reminderMargin = parsed;
44
- }
45
- return { thresholds: { reminder: reserveTokens + reminderMargin, reserve: reserveTokens, warning: reserveTokens + WARNING_RUNWAY_TOKENS }, warnings };
46
- }
47
- /**
48
- * `pi-context.dreamer` is a non-empty model pattern. Anything else present is ignored
49
- * with one warning; absent means no configured pattern, so the automatic model applies.
50
- */
51
- export function deriveDreamer(settings) {
52
- const raw = settings.dreamer;
53
- if (raw === undefined)
54
- return { warnings: [] };
55
- if (typeof raw !== "string" || raw.trim().length === 0) {
56
- return { warnings: [`pi-context: ${PI_CONTEXT_SETTINGS_KEY}.${PI_CONTEXT_DREAMER_KEY} must be a non-empty string; ignoring it.`] };
57
- }
58
- return { pattern: raw.trim(), warnings: [] };
59
- }
60
- /**
61
- * Resolve the configurable dreamer model from Pi settings for a CLI invocation: global
62
- * `~/.pi/agent/settings.json` merged with the project's `.pi/settings.json`, project
63
- * values winning per key. A settings read failure degrades to no pattern with one warning.
64
- */
65
- export function readDreamerSettings(cwd = process.cwd()) {
66
- try {
67
- const settingsManager = SettingsManager.create(cwd, undefined, { projectTrusted: true });
68
- return deriveDreamer(mergePiContextSettings(settingsManager.getGlobalSettings(), settingsManager.getProjectSettings()));
69
- }
70
- catch (error) {
71
- return { warnings: [`pi-context: could not read settings; using the automatic dreamer model (${String(error)}).`] };
72
- }
73
- }
74
- let cached;
75
- /**
76
- * Session-level threshold resolution: Pi's compaction reserve plus the settings.json
77
- * "pi-context" margins. The file-backed read is cached until resetThresholds (called
78
- * on session_start/session_tree); invalid configuration degrades per offending key
79
- * with one warning and never throws during session operation.
80
- */
81
- export function thresholdsFor(ctx) {
82
- if (cached)
83
- return cached;
84
- try {
85
- const settingsManager = SettingsManager.create(ctx.cwd, undefined, { projectTrusted: ctx.isProjectTrusted() });
86
- // Pass the active model so per-model compaction.modelOverrides resolve (SDK 0.86);
87
- // on older runtimes the extra argument is ignored and the ordinary setting wins.
88
- const model = ctx.model;
89
- const derived = deriveThresholds(settingsManager.getCompactionSettings(model ? { provider: model.provider, id: model.id } : undefined).reserveTokens, mergePiContextSettings(settingsManager.getGlobalSettings(), settingsManager.getProjectSettings()));
90
- for (const warning of derived.warnings)
91
- ctx.ui.notify(warning, "warning");
92
- cached = derived.thresholds;
93
- }
94
- catch (error) {
95
- ctx.ui.notify(`pi-context: could not read settings; using defaults (${String(error)}).`, "warning");
96
- cached = { reminder: DEFAULT_RESERVE_TOKENS + DEFAULT_REMINDER_MARGIN_TOKENS, reserve: DEFAULT_RESERVE_TOKENS, warning: DEFAULT_RESERVE_TOKENS + WARNING_RUNWAY_TOKENS };
97
- }
98
- return cached;
99
- }
100
- export function resetThresholds() {
101
- cached = undefined;
102
- }
@@ -1,44 +0,0 @@
1
- import { WARNING_TYPE, GUIDANCE_OPEN_TAG, GUIDANCE_CLOSE_TAG, WARNING_PROMPT } from "./protocol.js";
2
- import { thresholdsFor, resetThresholds } from "./thresholds.js";
3
- import { hasWindowMessage, currentWindowId } from "./history.js";
4
- import { remainingTokens } from "./budget.js";
5
- /**
6
- * The final checkpoint warning, steered to the model once per window. Like the early
7
- * reminder, the steer text is model-facing only (display: false); the human learns
8
- * about it from the warning-level notify, not from a chat-visible message.
9
- */
10
- /** Trigger: does the steer fire at this remaining-token count? Pure. */
11
- export function warningDue(remaining, thresholds) {
12
- return remaining <= thresholds.warning;
13
- }
14
- /** Delivery: what happens when it fires. */
15
- export function steerWarning(pi, ctx, thresholds, remaining) {
16
- pi.sendMessage({ customType: WARNING_TYPE, content: `${GUIDANCE_OPEN_TAG}\n${WARNING_PROMPT}\n${GUIDANCE_CLOSE_TAG}`, display: false }, { triggerTurn: true });
17
- ctx.ui.notify(`pi-context: context budget critical (${Math.max(0, remaining - thresholds.reserve)} tokens before reserve) — final checkpoint warning steered to the model.`, "warning");
18
- }
19
- /** Registration: once-per-window guard plus trigger+delivery on the context hook. */
20
- export function registerWarning(pi, isEnabled) {
21
- let firedInWindow;
22
- // Threshold resolution is owned by budget.ts; this module only consumes the shared
23
- // cache (lazily on the context hook) so session_start never warns twice.
24
- pi.on("session_start", () => { firedInWindow = undefined; });
25
- pi.on("session_tree", () => { firedInWindow = undefined; resetThresholds(); });
26
- pi.on("context", (_event, ctx) => {
27
- const windowId = currentWindowId(ctx);
28
- if (!isEnabled() || firedInWindow === windowId || hasWindowMessage(ctx, WARNING_TYPE))
29
- return undefined;
30
- const remaining = remainingTokens(ctx);
31
- if (remaining === null)
32
- return undefined;
33
- const thresholds = thresholdsFor(ctx);
34
- if (!warningDue(remaining, thresholds))
35
- return undefined;
36
- firedInWindow = windowId;
37
- // The steer reaches the model at the next sampling step with at most the runway
38
- // of invisible budget left. After it, the model decides for itself: end the
39
- // window, or ride it into Pi's automatic compaction, which resets on the spot
40
- // with no turn (see reset-lifecycle).
41
- steerWarning(pi, ctx, thresholds, remaining);
42
- return undefined;
43
- });
44
- }