@shanepadgett/tau-agent 0.20.2 → 0.22.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/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Tau is a custom agentic harness built with Pi extensions: tools, commands, prompts, skills, and themes.
4
4
 
5
+ The package also exports programmatic Tau capabilities for trusted Pi extensions. The built-in extensions and public API use the same underlying operations.
6
+
5
7
  ## Install
6
8
 
7
9
  ```bash
@@ -13,6 +15,22 @@ pi install ./path/to/tau-agent
13
15
  pi install ./path/to/tau-agent/packages/agent
14
16
  ```
15
17
 
18
+ ## Programmatic use
19
+
20
+ Install the package in the extension's project, then import from its root:
21
+
22
+ ```ts
23
+ import { generateImage } from "@shanepadgett/tau-agent";
24
+
25
+ const image = await generateImage(ctx, {
26
+ prompt: "A quiet mountain lake",
27
+ path: "assets/lake.png",
28
+ signal,
29
+ });
30
+ ```
31
+
32
+ See [Extending Tau Agent](./docs/extending-tau-agent.md) for the supported API.
33
+
16
34
  ## Development
17
35
 
18
36
  From the monorepo root:
@@ -1,8 +1,40 @@
1
1
  # Extending Tau Agent
2
2
 
3
- Tau Agent is a Pi extension harness. External integration uses Pi's native `pi.events` bus.
3
+ Tau Agent is a Pi extension harness and a programmatic API for trusted Pi extensions.
4
4
 
5
- The caller and Tau Agent must be loaded in the same Pi runtime. External callers use string channel names and documented payloads. They do not import Tau Agent internals.
5
+ Install `@shanepadgett/tau-agent` as a dependency when calling its API. Import only from the package root. Files under `extensions/` and other package paths are private.
6
+
7
+ ## Image generation
8
+
9
+ `generateImage()` runs the same image-generation pipeline as Tau's `image_gen` tool without opening UI, injecting messages, or starting an agent turn.
10
+
11
+ ```ts
12
+ import { generateImage } from "@shanepadgett/tau-agent";
13
+
14
+ const result = await generateImage(ctx, {
15
+ prompt: "A stone archway at sunrise",
16
+ provider: "openai",
17
+ path: candidatePath,
18
+ referencedImagePaths: [perspectivePath, palettePath],
19
+ signal,
20
+ });
21
+ ```
22
+
23
+ The context must provide `cwd`, `model`, and `modelRegistry`; Pi's `ExtensionContext` and `ExtensionCommandContext` already do. Relative destination and reference paths resolve from `ctx.cwd`.
24
+
25
+ Fields:
26
+
27
+ - `prompt`: complete non-empty image prompt.
28
+ - `provider`: optional `"openai"` or `"xai"` override. Without one, Tau follows the active model and falls back when authentication is unavailable.
29
+ - `path`: optional destination. Tau uses its external image store when omitted and never overwrites an explicit destination.
30
+ - `referencedImagePaths`: optional list of up to three local PNG, JPEG, or WebP files. Supplying references selects image editing or composition.
31
+ - `signal`: optional cancellation signal.
32
+
33
+ The promise resolves after publication with the generated bytes, absolute path, provider, model, operation, MIME type, width, and height. The bytes are the same bytes written to `result.path`, so callers can attach the image without reopening the file. Failures reject with a bounded, sanitized error. Cancellation before publication rejects with the signal's abort reason and removes staged files.
34
+
35
+ ## Events
36
+
37
+ External event integration uses Pi's native `pi.events` bus. The caller and Tau Agent must be loaded in the same Pi runtime. Event callers use string channel names and documented payloads. They do not import Tau Agent internals.
6
38
 
7
39
  Only events documented in this file are public. Extensions run trusted in-process; event emitters can ask Tau Agent to do work.
8
40
 
@@ -1,135 +1,39 @@
1
1
  # Context Pruning
2
2
 
3
- Context Pruning removes old tool calls and their results from the messages sent to the model. It does not delete them from the saved conversation. Branch switching, transcript review, and normal Pi compaction still see the original records.
3
+ Context Pruning gives the agent direct control over its future model context. It does not delete or rewrite the saved conversation.
4
4
 
5
- The feature can also remove old full-file autoread messages and old private reasoning. User messages, visible assistant prose, branch summaries, compaction summaries, shell records, and unrelated custom messages remain.
5
+ `context_prune` creates a hard checkpoint. Everything before that checkpoint leaves future model input unless the agent explicitly carries it forward. Messages after the checkpoint remain unchanged.
6
6
 
7
- ## The important distinction
7
+ Before calling the tool, the agent writes visible prose containing the durable conclusions, user constraints, conditional relevance, and next action that must survive. That prose is part of the checkpoint turn.
8
8
 
9
- Context usage and pruning are separate decisions.
9
+ ## Selections
10
10
 
11
- - The context percentage decides when Tau gives the agent a pruning hint.
12
- - A `context_prune` call decides what old evidence the agent wants to remove or retain.
13
- - Tau then checks whether that exact removal is safe and whether it saves enough tokens.
11
+ The tool accepts three required lists:
14
12
 
15
- Being at 70% context does not force Tau to apply a prune. With the default settings, 70% is high enough for a pressure hint, but every prune still has to pass the checks below.
13
+ - `keepToolCalls` retains exact earlier tool exchanges by tool-call ID. Parallel calls remain independently selectable: retaining one call does not retain its siblings.
14
+ - `keepFiles` reads each selected file from disk and carries its complete current contents forward as a fresh autoread snapshot. It does not require an earlier complete read.
15
+ - `deferFiles` carries forward a short advisory note explaining why a file is irrelevant now and when to reconsider it.
16
16
 
17
- ## What happens when the agent calls `context_prune`
17
+ Duplicate selections are collapsed. A file selected in both `keepFiles` and `deferFiles` is kept. Missing, unreadable, or otherwise unsnapshotable files produce warnings in the successful tool result; they do not block the checkpoint or other file snapshots.
18
18
 
19
- The agent supplies three required lists:
19
+ Context Pruning does not impose a minimum token saving and does not reject a checkpoint because old context has unusual bookkeeping. Pi supplies the current provider context. Tau removes unretained tool calls and their matching results with the same ID, drops other pre-checkpoint messages, and leaves the checkpoint turn and later messages intact.
20
20
 
21
- - `keepFiles`: files whose complete current contents must remain available.
22
- - `keepToolCalls`: exact earlier tool calls and results that must remain available.
23
- - `deferFiles`: files that are currently irrelevant but may matter under a stated condition.
21
+ ## Automatic and manual requests
24
22
 
25
- Unless retained by one of the first two lists, every complete earlier tool exchange and every autoread currently eligible for pruning is selected for removal. The current `context_prune` call is the anchor: later messages and tool results are unaffected by that anchor.
23
+ Tau checks context growth after tool-using turns and can send progressively stronger private instructions from `nudgeInstructions`. Growth is measured from the first tool-using turn after the latest checkpoint. Branch navigation and compaction reconstruct that baseline from the active branch.
26
24
 
27
- The agent is instructed to put durable conclusions, conditional information, and its next action in visible prose immediately before the call. That prose survives pruning. Tau enforces that `context_prune` is the only tool call in its assistant message, but it does not judge whether the preceding prose is complete or useful.
28
-
29
- Tau prepares the result without changing the active context. It estimates the token count before and after the proposed removal, including any replacement file contents and deferred-file note. It applies the prune only when the estimated saving is at least `minimumReclaimTokens`.
30
-
31
- After a successful call, Tau removes the selected evidence from future model input. The saved conversation remains unchanged. The tool result records exactly which tool exchanges and autoreads were removed, retained, or refreshed.
32
-
33
- ## Why a prune is skipped
34
-
35
- A skipped prune removes nothing and publishes no deferred-file note or replacement file contents. Tau skips when any of these checks fails:
36
-
37
- 1. Context Pruning is disabled.
38
- 2. Tau does not have a usable copy of the exact messages most recently sent to the model.
39
- 3. The `context_prune` call is missing from the active branch.
40
- 4. Another tool call appears beside `context_prune` in the same assistant message.
41
- 5. The current model input contains duplicate, mismatched, or orphaned tool records that Tau cannot safely remove. An unmatched call from an explicitly aborted assistant response is discarded and does not block pruning.
42
- 6. A retained tool-call ID is duplicated, missing, or is not a complete exchange in the current model input.
43
- 7. Two selected file paths resolve to the same file, including aliases and symbolic links across `keepFiles` and `deferFiles`.
44
- 8. A retained file does not have an earlier complete-file read, has malformed read evidence, cannot currently be read as UTF-8, or exceeds the 1 MiB complete-file limit.
45
- 9. The estimated saving is below `minimumReclaimTokens`.
46
-
47
- Cancellation, a session lifecycle change during preparation, and failures while publishing an already prepared prune are reported as tool failures rather than skipped prunes.
48
-
49
- ### “The latest provider-context projection is unavailable”
50
-
51
- This error uses an internal term. In plain language, Tau did not have a usable copy of the exact message list the model had just received.
52
-
53
- Tau normally saves that list while Pi prepares a model turn. Tau clears it on session start, branch changes, compaction, and shutdown. It also refuses to save a list with invalid tool-call/result pairing. If the list is missing or belongs to an earlier session state, `context_prune` stops before checking the agent’s selections or estimating savings.
54
-
55
- Pi can save an aborted assistant response that requested a tool but never received a result. Tau discards that abandoned call when preparing later model input. Other unmatched calls and results still fail validation because Tau cannot prove whether their evidence is incomplete.
56
-
57
- For example, a row that says `context_prune 29 selections` followed by this error means:
58
-
59
- - the agent supplied 29 items across the three selection lists;
60
- - Tau did not inspect or apply those selections;
61
- - no old tool calls were removed;
62
- - the context percentage did not cause the rejection;
63
- - the current implementation does not reconstruct the missing list during that call.
64
-
65
- The response tells the agent not to retry immediately because repeated calls in the same state would usually fail for the same reason. A later model turn should normally give Tau a new list. Repeated occurrences indicate a feature defect or a session history that Tau cannot safely process.
66
-
67
- ## Retaining files
68
-
69
- `keepFiles` preserves complete current file knowledge, not necessarily the original read row.
70
-
71
- Each selected file must have been read completely earlier in the current model input. A partial read is insufficient. Tau then reads the file from disk again and chooses the cheaper valid representation:
72
-
73
- - Keep an existing complete snapshot when it still matches the file.
74
- - Keep a baseline plus later read diffs when that chain reconstructs the current file and costs no more than a fresh snapshot.
75
- - Add one fresh complete snapshot when the earlier evidence is stale, broken, or more expensive.
76
-
77
- The entire prune is skipped if any selected file fails. Every retained file must currently exist, be valid UTF-8, and fit within the 1 MiB complete-file snapshot limit. Read the complete file before selecting it for retention.
78
-
79
- Paths are resolved from the session working directory. Paths outside it are stored as absolute paths. Existing symbolic links and other aliases are resolved before duplicate checks.
80
-
81
- The required `relevance` text explains the agent’s selection. It does not loosen any validation rule.
82
-
83
- ## Retaining tool exchanges
84
-
85
- `keepToolCalls` retains both sides of an earlier exchange: the assistant’s tool call and the matching tool result. The ID must occur exactly once, the tool names must match, and the result must follow the call.
86
-
87
- Retention is exact. Selecting one call does not retain nearby calls from the same assistant message. Tau can remove one parallel tool exchange while keeping another, then removes the empty assistant message if no content remains.
88
-
89
- The required `relevance` text explains why the exchange matters. Tau does not use that text to infer additional exchanges to retain.
90
-
91
- ## Deferring files
92
-
93
- `deferFiles` does not read or preserve file contents. It adds a hidden advisory note telling the model why each file was deferred and when to reconsider it. The latest successful anchor replaces the previous deferred-file list.
94
-
95
- A deferred path may be missing. It still participates in canonical path and duplicate checks. Its `reason` and `relevantWhen` text are passed to the model as written.
96
-
97
- ## Automatic hints
98
-
99
- Tau checks context usage only after a turn that produced at least one tool result. It emits at most one marker for the strongest newly crossed boundary.
100
-
101
- With the defaults:
102
-
103
- - `nudgeEveryPercent: 20` allows markers after enough context growth to cross 20% intervals.
104
- - `pressurePercent: 50` makes a marker a pressure hint only when raw usage is greater than 50%.
105
-
106
- Below the pressure threshold, the hidden instruction says no prune is required unless broad exploration has converged or substantial evidence is already irrelevant. Above it, the instruction asks the agent to finish the current coherent step and prune when the projected saving is substantial. A hint never calls the tool or bypasses its checks.
107
-
108
- After a successful prune, the first later tool-using turn records a new usage baseline and emits no automatic marker. Tau waits for context usage to grow by another `nudgeEveryPercent` from that baseline. The baseline and crossed boundaries are stored with the active branch so compaction and branch navigation do not immediately repeat hints.
109
-
110
- The visible marker is compact. The full instruction stays hidden, and the agent is told not to discuss internal context management.
111
-
112
- ## Manual requests
113
-
114
- Run `/prune` with no arguments to ask the agent to create an anchor and continue unfinished work. The command starts an agent turn immediately. It does not force a successful prune or bypass file, tool-pairing, current-message, lifecycle, or minimum-savings checks.
115
-
116
- Extra arguments produce `Usage: /prune`. When Context Pruning is disabled, the command reports that state and does not start a prune turn.
117
-
118
- Asking the agent to prune in ordinary chat has the same execution boundaries once it calls `context_prune`.
25
+ Run `/prune` with no arguments to ask the agent to create a checkpoint and continue its task immediately.
119
26
 
120
27
  ## Branches, compaction, and display
121
28
 
122
- Applied anchors belong to the active session branch. Switching branches rebuilds the removed-evidence set, deferred-file list, automatic-hint baseline, and warning-colored rows from that branch’s valid tool results. A skipped or malformed prune result does not become an anchor.
123
-
124
- Context Pruning does not cancel or replace Pi’s manual, threshold, or overflow compaction. Compaction clears Tau’s saved model-input list; Tau must receive another model-input event before a later prune can use it.
29
+ Checkpoints belong to the active branch. Switching branches rebuilds the latest checkpoint, deferred-file state, automatic-hint baseline, and warning-colored pruned rows from that branch.
125
30
 
126
- Tau-owned tool rows and autoreads turn warning-colored when their evidence has been pruned. Native Pi rows and fallback rows that do not use Tau’s row-state renderer do not change color. Their evidence is still removed from model input.
31
+ Normal Pi compaction remains independent. When a compaction no longer includes an old checkpoint turn, that checkpoint has nothing left to filter.
127
32
 
128
33
  ## Settings
129
34
 
130
35
  Settings live under `extensions.contextPruning` in Tau settings.
131
36
 
132
- - `enabled`: enables the tool, `/prune`, automatic markers, branch replay, context filtering, and pruned-row display. Defaults to `true`.
133
- - `nudgeEveryPercent`: integer context-growth interval between automatic hints. Allowed range: `1` through `100`. Defaults to `20`.
134
- - `pressurePercent`: integer usage percentage above which hints use pressure wording. Allowed range: `1` through `99`. Defaults to `50`.
135
- - `minimumReclaimTokens`: positive integer minimum estimated saving required to apply a prune. Defaults to `8000`.
37
+ - `enabled`: enables the tool, `/prune`, projection, markers, and branch replay. Defaults to `true`.
38
+ - `nudgeEveryPercent`: context-growth interval between automatic hints, from `1` through `100`. Defaults to `20`.
39
+ - `nudgeInstructions`: ordered list of one through five nonempty instructions. Later reminders repeat the final instruction. Defaults to three escalating instructions.
@@ -1,6 +1,5 @@
1
1
  import {
2
2
  defineTool,
3
- type ContextEvent,
4
3
  type ExtensionAPI,
5
4
  type ExtensionContext,
6
5
  type SessionEntry,
@@ -8,7 +7,7 @@ import {
8
7
  import {
9
8
  replayContextPruningState,
10
9
  setContextPruningEnabled,
11
- type ContextPruneDetailsV1,
10
+ type ContextPruneDetailsV2,
12
11
  } from "../../shared/context-pruning-state.ts";
13
12
  import { emitTauEvent, onTauEvent } from "../../shared/events.ts";
14
13
  import { loadTauExtensionSettings } from "../../shared/settings/load.ts";
@@ -16,23 +15,16 @@ import { createToolRowStateStore } from "../../shared/tool-row-state.ts";
16
15
  import { contextPruneParameters, executeContextPrune } from "./prune.ts";
17
16
  import { projectContext } from "./projection.ts";
18
17
  import {
19
- parseContextPruningNudgeDetailsV1,
18
+ parseContextPruningNudgeDetailsV2,
20
19
  renderContextPruneCall,
21
20
  renderContextPruneResult,
22
21
  renderContextPruningNudge,
23
- type ContextPruningNudgeDetailsV1,
22
+ type ContextPruningNudgeDetailsV2,
24
23
  } from "./render.ts";
25
24
  import contextPruningSettings from "./settings.ts";
26
25
 
27
- interface ProjectionCache {
28
- generation: number;
29
- input: ContextEvent["messages"];
30
- stateKey: string;
31
- messages: ContextEvent["messages"];
32
- }
33
-
34
26
  const TOOL_DESCRIPTION =
35
- "Create a context anchor after broad exploration converges, after a context-pressure nudge, or when substantial irrelevant tool evidence has accumulated. Do not use after routine tool batches or when expected savings are small. Immediately before calling, state retained conclusions, conditional relevance, and the next action in visible prose. Call context_prune alone in its tool batch.";
27
+ "Create a hard context checkpoint after broad exploration converges, when a context-pruning nudge directs it, or when stale evidence has accumulated. Everything before the checkpoint is removed from future model context unless selected for retention. Immediately before calling, state durable conclusions, conditional relevance, and the next action in visible prose.";
36
28
  const NUDGE_MESSAGE_TYPE = "tau.context-pruning.nudge";
37
29
  const NUDGE_BASELINE_ENTRY_TYPE = "tau.context-pruning.nudge-baseline";
38
30
 
@@ -40,15 +32,15 @@ interface NudgeState {
40
32
  anchorToolCallId: string | undefined;
41
33
  growthBaselinePercent: number | undefined;
42
34
  highestBoundary: number;
35
+ highestTier: number;
36
+ terminalTierReached: boolean;
43
37
  }
44
38
 
45
39
  export default function contextPruningExtension(pi: ExtensionAPI): void {
46
40
  let enabled = false;
47
41
  let lifecycleGeneration = 0;
48
- let projectionCache: ProjectionCache | undefined;
49
- let minimumReclaimTokens = contextPruningSettings.defaults.minimumReclaimTokens;
50
42
  let nudgeEveryPercent = contextPruningSettings.defaults.nudgeEveryPercent;
51
- let pressurePercent = contextPruningSettings.defaults.pressurePercent;
43
+ let nudgeInstructions = contextPruningSettings.defaults.nudgeInstructions;
52
44
  let toolRegistered = false;
53
45
  let commandRegistered = false;
54
46
  let visualRows = new Set<string>();
@@ -56,9 +48,11 @@ export default function contextPruningExtension(pi: ExtensionAPI): void {
56
48
  anchorToolCallId: undefined,
57
49
  growthBaselinePercent: 0,
58
50
  highestBoundary: 0,
51
+ highestTier: 0,
52
+ terminalTierReached: false,
59
53
  };
60
54
  const rowState = createToolRowStateStore(pi, "context-pruning.tool-row-state");
61
- pi.registerMessageRenderer<ContextPruningNudgeDetailsV1>(NUDGE_MESSAGE_TYPE, (message, _options, theme) =>
55
+ pi.registerMessageRenderer<ContextPruningNudgeDetailsV2>(NUDGE_MESSAGE_TYPE, (message, _options, theme) =>
62
56
  renderContextPruningNudge(message.details, theme),
63
57
  );
64
58
 
@@ -76,7 +70,6 @@ export default function contextPruningExtension(pi: ExtensionAPI): void {
76
70
 
77
71
  const clearEphemeralState = () => {
78
72
  lifecycleGeneration += 1;
79
- projectionCache = undefined;
80
73
  };
81
74
  const setContextPruneToolActive = (active: boolean) => {
82
75
  if (!toolRegistered) return;
@@ -105,37 +98,32 @@ export default function contextPruningExtension(pi: ExtensionAPI): void {
105
98
  const settings = await loadTauExtensionSettings(ctx, contextPruningSettings);
106
99
  if (generation !== lifecycleGeneration) return;
107
100
  enabled = settings.enabled;
108
- minimumReclaimTokens = settings.minimumReclaimTokens;
109
101
  nudgeEveryPercent = settings.nudgeEveryPercent;
110
- pressurePercent = settings.pressurePercent;
102
+ nudgeInstructions = settings.nudgeInstructions;
111
103
  setContextPruningEnabled(enabled);
112
104
  if (enabled && !toolRegistered) {
113
105
  pi.registerTool(
114
- defineTool<typeof contextPruneParameters, ContextPruneDetailsV1>({
106
+ defineTool<typeof contextPruneParameters, ContextPruneDetailsV2>({
115
107
  name: "context_prune",
116
108
  label: "context_prune",
117
109
  description: TOOL_DESCRIPTION,
118
110
  promptSnippet:
119
111
  "Prune substantial stale tool evidence after stating durable conclusions and the next action",
120
112
  promptGuidelines: [
121
- "Use context_prune after broad exploration converges, after a context-pressure nudge, or when substantial irrelevant evidence has accumulated.",
122
- "Do not use context_prune after routine tool batches or when expected savings are small.",
123
- "Immediately before context_prune, write visible prose with retained conclusions, conditional relevance, and the next action, then call context_prune alone in its tool batch.",
113
+ "Use context_prune after broad exploration converges, when a context-pruning nudge directs it, or when substantial irrelevant evidence has accumulated.",
114
+ "A final-tier context-pruning nudge means preserve durable conclusions and prune before further tool work.",
115
+ "Everything before context_prune leaves future model context unless selected in keepFiles or keepToolCalls, so preserve durable conclusions, user constraints, conditional relevance, and the next action in visible prose immediately before calling it.",
124
116
  ],
125
117
  parameters: contextPruneParameters,
126
118
  executionMode: "sequential",
127
119
  async execute(toolCallId, params, signal, _onUpdate, executionContext) {
128
- const cache = projectionCache;
129
120
  return executeContextPrune({
130
- pi,
131
121
  toolCallId,
132
122
  params,
133
123
  signal,
134
124
  ctx: executionContext,
135
- projection: cache && { generation: cache.generation, messages: cache.messages },
125
+ generation: lifecycleGeneration,
136
126
  currentGeneration: () => lifecycleGeneration,
137
- currentEnabled: () => enabled,
138
- minimumReclaimTokens,
139
127
  });
140
128
  },
141
129
  renderCall(args, theme, context) {
@@ -170,17 +158,20 @@ export default function contextPruningExtension(pi: ExtensionAPI): void {
170
158
  commandContext.sessionManager.getBranch(),
171
159
  true,
172
160
  ).latestAnchorToolCallId;
173
- pi.sendMessage<ContextPruningNudgeDetailsV1>(
161
+ pi.sendMessage<ContextPruningNudgeDetailsV2>(
174
162
  {
175
163
  customType: NUDGE_MESSAGE_TYPE,
176
164
  content: manualPruneSteeringMessage(),
177
165
  display: true,
178
166
  details: {
179
- v: 1,
167
+ v: 2,
180
168
  kind: "manual",
181
169
  percent: null,
182
170
  boundary: null,
183
- pressure: false,
171
+ reminder: null,
172
+ tier: null,
173
+ tierCount: null,
174
+ tierFloor: null,
184
175
  anchorToolCallId: anchorToolCallId ?? null,
185
176
  growthBaselinePercent: null,
186
177
  },
@@ -207,7 +198,13 @@ export default function contextPruningExtension(pi: ExtensionAPI): void {
207
198
  enabled = false;
208
199
  setContextPruneToolActive(false);
209
200
  visualRows.clear();
210
- nudgeState = { anchorToolCallId: undefined, growthBaselinePercent: 0, highestBoundary: 0 };
201
+ nudgeState = {
202
+ anchorToolCallId: undefined,
203
+ growthBaselinePercent: 0,
204
+ highestBoundary: 0,
205
+ highestTier: 0,
206
+ terminalTierReached: false,
207
+ };
211
208
  setContextPruningEnabled(false);
212
209
  pushVisualSnapshot();
213
210
  });
@@ -233,61 +230,51 @@ export default function contextPruningExtension(pi: ExtensionAPI): void {
233
230
  return undefined;
234
231
  }
235
232
  const baseline = nudgeState.growthBaselinePercent ?? 0;
236
- if (percent - baseline < nudgeEveryPercent) return undefined;
237
- const boundary = Math.floor(percent / nudgeEveryPercent) * nudgeEveryPercent;
233
+ const reminder = Math.floor((percent - baseline) / nudgeEveryPercent);
234
+ if (reminder < 1) return undefined;
235
+ const boundary = baseline + reminder * nudgeEveryPercent;
238
236
  if (boundary <= nudgeState.highestBoundary) return undefined;
239
- const pressure = rawPercent > pressurePercent;
240
- const details: ContextPruningNudgeDetailsV1 = {
241
- v: 1,
237
+ const tierCount = nudgeInstructions.length;
238
+ const tierFloor = nudgeState.terminalTierReached ? tierCount : Math.min(nudgeState.highestTier, tierCount);
239
+ const tier = Math.max(Math.min(reminder, tierCount), tierFloor);
240
+ const instruction = nudgeInstructions[tier - 1] ?? nudgeInstructions[0];
241
+ const details: ContextPruningNudgeDetailsV2 = {
242
+ v: 2,
242
243
  kind: "automatic",
243
244
  percent,
244
245
  boundary,
245
- pressure,
246
+ reminder,
247
+ tier,
248
+ tierCount,
249
+ tierFloor,
246
250
  anchorToolCallId: activeAnchor ?? null,
247
251
  growthBaselinePercent: baseline,
248
252
  };
249
- pi.sendMessage<ContextPruningNudgeDetailsV1>(
253
+ pi.sendMessage<ContextPruningNudgeDetailsV2>(
250
254
  {
251
255
  customType: NUDGE_MESSAGE_TYPE,
252
- content: automaticPruneSteeringMessage(pressure),
256
+ content: automaticPruneSteeringMessage(instruction, tier === tierCount),
253
257
  display: true,
254
258
  details,
255
259
  },
256
260
  { deliverAs: "steer" },
257
261
  );
258
262
  nudgeState.highestBoundary = boundary;
263
+ nudgeState.highestTier = Math.max(nudgeState.highestTier, tier);
264
+ nudgeState.terminalTierReached ||= tier === tierCount;
259
265
  return undefined;
260
266
  });
261
267
 
262
268
  pi.on("context", (event, ctx) => {
263
269
  if (!enabled) return undefined;
264
270
  const state = replayContextPruningState(ctx.sessionManager.getBranch(), true);
265
- const stateKey = JSON.stringify([
266
- state.latestAnchorToolCallId,
267
- [...state.prunedToolCallIds],
268
- [...state.prunedAutoreadRowIds],
269
- ]);
270
- if (
271
- projectionCache?.generation === lifecycleGeneration &&
272
- projectionCache.input === event.messages &&
273
- projectionCache.stateKey === stateKey
274
- ) {
275
- return { messages: projectionCache.messages };
276
- }
277
-
278
- try {
279
- const messages = projectContext(event.messages, state);
280
- projectionCache = { generation: lifecycleGeneration, input: event.messages, stateKey, messages };
281
- const nextRows = new Set([...state.prunedToolCallIds, ...state.prunedAutoreadRowIds]);
282
- if (!setsEqual(visualRows, nextRows)) {
283
- visualRows = nextRows;
284
- pushVisualSnapshot();
285
- }
286
- return { messages };
287
- } catch {
288
- projectionCache = undefined;
289
- return undefined;
271
+ const messages = projectContext(event.messages, state);
272
+ const nextRows = new Set([...state.prunedToolCallIds, ...state.prunedAutoreadRowIds]);
273
+ if (!setsEqual(visualRows, nextRows)) {
274
+ visualRows = nextRows;
275
+ pushVisualSnapshot();
290
276
  }
277
+ return { messages };
291
278
  });
292
279
  }
293
280
 
@@ -300,6 +287,8 @@ function setsEqual(left: ReadonlySet<string>, right: ReadonlySet<string>): boole
300
287
  function reconstructNudgeState(branch: readonly SessionEntry[], anchorToolCallId: string | undefined): NudgeState {
301
288
  let growthBaselinePercent = anchorToolCallId === undefined ? 0 : undefined;
302
289
  let highestBoundary = 0;
290
+ let highestTier = 0;
291
+ let terminalTierReached = false;
303
292
  let anchorResultIndex = -1;
304
293
  if (anchorToolCallId !== undefined) {
305
294
  anchorResultIndex = branch.findIndex(
@@ -326,19 +315,27 @@ function reconstructNudgeState(branch: readonly SessionEntry[], anchorToolCallId
326
315
  continue;
327
316
  }
328
317
  if (entry.type !== "custom_message" || entry.customType !== NUDGE_MESSAGE_TYPE) continue;
329
- const details = parseContextPruningNudgeDetailsV1(entry.details);
318
+ const details = parseContextPruningNudgeDetailsV2(entry.details);
330
319
  if (
331
320
  !details ||
332
321
  details.kind !== "automatic" ||
322
+ index <= anchorResultIndex ||
333
323
  details.anchorToolCallId !== (anchorToolCallId ?? null) ||
334
324
  details.boundary === null ||
335
- details.growthBaselinePercent === null
325
+ details.growthBaselinePercent === null ||
326
+ (growthBaselinePercent !== undefined && details.growthBaselinePercent !== growthBaselinePercent)
336
327
  )
337
328
  continue;
329
+ const expectedTierFloor: number = terminalTierReached
330
+ ? details.tierCount
331
+ : Math.min(highestTier, details.tierCount);
332
+ if (details.boundary <= highestBoundary || details.tierFloor !== expectedTierFloor) continue;
338
333
  highestBoundary = Math.max(highestBoundary, details.boundary);
334
+ highestTier = Math.max(highestTier, details.tier);
335
+ terminalTierReached ||= details.tier === details.tierCount;
339
336
  growthBaselinePercent = details.growthBaselinePercent;
340
337
  }
341
- return { anchorToolCallId, growthBaselinePercent, highestBoundary };
338
+ return { anchorToolCallId, growthBaselinePercent, highestBoundary, highestTier, terminalTierReached };
342
339
  }
343
340
 
344
341
  function parseNudgeBaseline(value: unknown): { v: 1; anchorToolCallId: string; baselinePercent: number } | undefined {
@@ -365,15 +362,16 @@ function parseNudgeBaseline(value: unknown): { v: 1; anchorToolCallId: string; b
365
362
  };
366
363
  }
367
364
 
368
- function automaticPruneSteeringMessage(pressure: boolean): string {
365
+ function automaticPruneSteeringMessage(instruction: string, finalTier: boolean): string {
369
366
  const silent =
370
367
  "Internal context-management instruction. Follow it silently. Do not mention or acknowledge context percentages, prune messages, or internal context management.";
371
- if (pressure) {
372
- return `${silent} Finish the current coherent step. When projected savings are substantial, preserve durable conclusions and the next action in visible prose, then call context_prune alone. Do not prune after a routine tool batch or retry immediately after a skipped prune.`;
373
- }
374
- return `${silent} No prune is required unless broad exploration has converged or substantial evidence is already irrelevant. Continue coherent work. If pruning becomes worthwhile, preserve durable conclusions and the next action in visible prose, then call context_prune alone.`;
368
+ const protocol =
369
+ "When pruning, first preserve durable conclusions, user constraints, conditional relevance, and the next action in visible prose, then call context_prune.";
370
+ return finalTier
371
+ ? `${silent} ${instruction} This is the final reminder tier. Create a context anchor before further tool work. ${protocol}`
372
+ : `${silent} ${instruction} ${protocol}`;
375
373
  }
376
374
 
377
375
  function manualPruneSteeringMessage(): string {
378
- return "Internal context-management instruction. Follow it silently without mentioning this request. Create a context anchor with context_prune, then continue unfinished work. First preserve durable conclusions, conditional relevance, and the next action in visible prose. Call context_prune alone. If projected savings are too small, continue work without immediately retrying.";
376
+ return "Internal context-management instruction. Follow it silently without mentioning this request. Create a hard context checkpoint with context_prune, then continue unfinished work. First preserve durable conclusions, user constraints, conditional relevance, and the next action in visible prose.";
379
377
  }