pi-context-kit 0.4.4 → 0.4.5
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 +7 -0
- package/extensions/index.ts +31 -7
- package/node_modules/pi-hl-utils/src/layered-settings.ts +47 -11
- package/package.json +1 -1
- package/src/shake.ts +87 -7
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
Pi shows the entries newer than your installed version after `pi update`.
|
|
4
4
|
|
|
5
|
+
## 0.4.5
|
|
6
|
+
|
|
7
|
+
### Patch Changes
|
|
8
|
+
|
|
9
|
+
- Shake no longer stalls a turn on large tool output with many unclosed HTML tags or code fences. A code fence now closes only on a fence at least as long as its opening fence, and the elision removes the whole closing fence.
|
|
10
|
+
- Notices now show provider errors, settings problems and file errors without terminal control sequences, so such text can no longer change the window title, clear the screen or add hidden links. Line breaks and tabs stay.
|
|
11
|
+
|
|
5
12
|
## 0.4.4
|
|
6
13
|
|
|
7
14
|
### Patch Changes
|
package/extensions/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
SettingsManager,
|
|
17
17
|
type TurnEndEvent,
|
|
18
18
|
} from "@earendil-works/pi-coding-agent";
|
|
19
|
+
import { stripTerminalSequences } from "@earendil-works/pi-tui";
|
|
19
20
|
import { errorText } from "pi-hl-utils/errors";
|
|
20
21
|
import {
|
|
21
22
|
layeredMenuScopes,
|
|
@@ -93,7 +94,8 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
93
94
|
const report = (ctx: ExtensionContext, problems: string[]) => {
|
|
94
95
|
const message = problems.join("; ");
|
|
95
96
|
if (message && message !== reported)
|
|
96
|
-
|
|
97
|
+
notify(
|
|
98
|
+
ctx,
|
|
97
99
|
`pi-context-kit settings need attention: ${message}. Type /context-kit to see them.`,
|
|
98
100
|
"warning",
|
|
99
101
|
);
|
|
@@ -172,14 +174,14 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
172
174
|
// Maintenance needs a model with a context window, so `window` is set whenever `output` is.
|
|
173
175
|
const window = turn.ctx.model?.contextWindow;
|
|
174
176
|
if (output && window)
|
|
175
|
-
turn.ctx
|
|
177
|
+
notify(turn.ctx, freedNotice(count([]), count(turn.result.edits), window), "info");
|
|
176
178
|
showStatus(turn.ctx, turn.config, turn.result.tokens);
|
|
177
179
|
return output;
|
|
178
180
|
};
|
|
179
181
|
|
|
180
182
|
const warn = (ctx: ExtensionContext, error: unknown, fallback: string, what = "handoff note") => {
|
|
181
183
|
const message = errorText(error);
|
|
182
|
-
|
|
184
|
+
notify(ctx, `pi-context-kit: ${what} failed (${message}). ${fallback}`, "warning");
|
|
183
185
|
};
|
|
184
186
|
|
|
185
187
|
/** Pi's summary at the extension's threshold, because Pi's own hook waits for Pi's threshold. */
|
|
@@ -536,7 +538,8 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
536
538
|
try {
|
|
537
539
|
renameSync(item.file, `${item.file}.migrated`);
|
|
538
540
|
} catch (error) {
|
|
539
|
-
|
|
541
|
+
notify(
|
|
542
|
+
ctx,
|
|
540
543
|
`pi-context-kit: settings were imported, but renaming ${item.file} failed (${errorText(error)}). The file stays in place and is not imported again.`,
|
|
541
544
|
"warning",
|
|
542
545
|
);
|
|
@@ -546,7 +549,8 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
546
549
|
}
|
|
547
550
|
}
|
|
548
551
|
if (imports.length > 0)
|
|
549
|
-
|
|
552
|
+
notify(
|
|
553
|
+
ctx,
|
|
550
554
|
[
|
|
551
555
|
'pi-context-kit moved its settings into Pi\'s settings.json under "context-kit". The 0.3 "contextKit" entry is no longer read.',
|
|
552
556
|
...renamed,
|
|
@@ -635,7 +639,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
635
639
|
// The settings warning points here, so it shows again.
|
|
636
640
|
reported = "";
|
|
637
641
|
return {
|
|
638
|
-
status: () =>
|
|
642
|
+
status: () => notify(ctx, summaryText(settings(ctx)), "info"),
|
|
639
643
|
async menu() {
|
|
640
644
|
await ctx.ui.custom<void>(
|
|
641
645
|
(tui, theme, _keybindings, done) =>
|
|
@@ -646,7 +650,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
646
650
|
await applyChange(ctx, command.scope, command);
|
|
647
651
|
showStatus(ctx);
|
|
648
652
|
},
|
|
649
|
-
fail: (cause) =>
|
|
653
|
+
fail: (cause) => notify(ctx, `pi-context-kit: ${errorText(cause)}`, "error"),
|
|
650
654
|
};
|
|
651
655
|
},
|
|
652
656
|
});
|
|
@@ -783,6 +787,26 @@ function parseImported(item: SettingsImport): ContextKitOptionsPatch {
|
|
|
783
787
|
}
|
|
784
788
|
}
|
|
785
789
|
|
|
790
|
+
function notify(ctx: ExtensionContext, text: string, type: "info" | "warning" | "error"): void {
|
|
791
|
+
ctx.ui.notify(displayText(text), type);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* Error and settings text without terminal controls: pi-tui writes notice text to the terminal as is.
|
|
796
|
+
* Keeps tabs and line breaks.
|
|
797
|
+
*/
|
|
798
|
+
function displayText(text: string): string {
|
|
799
|
+
return Array.from(stripTerminalSequences(text).replace(/\r\n?/g, "\n"))
|
|
800
|
+
.filter((char) => char === "\n" || char === "\t" || !isControlCharacter(char))
|
|
801
|
+
.join("");
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/** C0, DEL and C1 controls. */
|
|
805
|
+
function isControlCharacter(char: string): boolean {
|
|
806
|
+
const code = char.charCodeAt(0);
|
|
807
|
+
return code < 0x20 || (code >= 0x7f && code <= 0x9f);
|
|
808
|
+
}
|
|
809
|
+
|
|
786
810
|
/** One-line status for modes without the settings menu. */
|
|
787
811
|
function summaryText(config: ContextKitConfig): string {
|
|
788
812
|
if (!config.enabled) return "pi-context-kit: automatic cleanup off. Settings: /context-kit";
|
|
@@ -89,6 +89,14 @@ export function resolveLayeredOptions<Config extends object>(
|
|
|
89
89
|
return { settings, sources };
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
// Settings text reaches terminal notifications; raw control characters would run escape sequences.
|
|
93
|
+
export function escapeControls(text: string): string {
|
|
94
|
+
return text.replace(
|
|
95
|
+
/\p{Cc}/gu,
|
|
96
|
+
(char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
92
100
|
/** Why {@link rewriteNamespaceDocument} refused a stored settings document. */
|
|
93
101
|
export type InvalidSettingsDocument = "malformed" | "root" | "namespace";
|
|
94
102
|
|
|
@@ -151,6 +159,7 @@ export interface LayeredSettingsDefinition<
|
|
|
151
159
|
merge?: Merge;
|
|
152
160
|
/** Keep the namespace in a settings document after its last option is removed. */
|
|
153
161
|
keepEmptyNamespace?: boolean;
|
|
162
|
+
globalOnlyKeys?: readonly (keyof Static<S> & keyof Config & string)[];
|
|
154
163
|
}
|
|
155
164
|
|
|
156
165
|
/**
|
|
@@ -275,8 +284,16 @@ export function defineLayeredSettings<
|
|
|
275
284
|
type OptionKey = keyof Options & keyof Config & string;
|
|
276
285
|
type Layers = LayeredSettingsLayers<Options>;
|
|
277
286
|
type Change = LayeredSettingChange<Options>;
|
|
278
|
-
const {
|
|
279
|
-
|
|
287
|
+
const {
|
|
288
|
+
namespace,
|
|
289
|
+
label,
|
|
290
|
+
schema,
|
|
291
|
+
defaults,
|
|
292
|
+
sessionEntryType,
|
|
293
|
+
merge,
|
|
294
|
+
keepEmptyNamespace,
|
|
295
|
+
globalOnlyKeys = [],
|
|
296
|
+
} = definition;
|
|
280
297
|
if (!Value.Check(schema, {}))
|
|
281
298
|
throw new Error(`${label} settings schema must make every option optional`);
|
|
282
299
|
const keySchema = Type.KeyOf(schema);
|
|
@@ -317,6 +334,16 @@ export function defineLayeredSettings<
|
|
|
317
334
|
return structuredClone(empty);
|
|
318
335
|
}
|
|
319
336
|
|
|
337
|
+
function checkScope(options: Options, source: LayeredSettingScope): void {
|
|
338
|
+
if (source === "global") return;
|
|
339
|
+
for (const key of globalOnlyKeys) {
|
|
340
|
+
if (Object.hasOwn(options, key))
|
|
341
|
+
throw new Error(
|
|
342
|
+
`Invalid ${source} ${label} settings: ${key} is global-only; set it in global settings`,
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
320
347
|
// Native settings contain arbitrary authored JSON; the option schema validates it before use. SDK command tests exercise the boundary.
|
|
321
348
|
function parseOptions(value: unknown, source: LayeredSettingScope): Options {
|
|
322
349
|
if (value === undefined) return noOptions();
|
|
@@ -324,12 +351,13 @@ export function defineLayeredSettings<
|
|
|
324
351
|
const issue = Value.Errors(schema, value)[0];
|
|
325
352
|
if (issue?.schemaPath === "#/additionalProperties")
|
|
326
353
|
throw new Error(
|
|
327
|
-
`Invalid ${source} ${label} settings: unknown option ${issue.instancePath.slice(1)}`,
|
|
354
|
+
`Invalid ${source} ${label} settings: unknown option ${escapeControls(issue.instancePath.slice(1))}`,
|
|
328
355
|
);
|
|
329
356
|
throw new Error(
|
|
330
|
-
`Invalid ${source} ${label} settings${issue?.instancePath ?? ""}: ${issue?.message ?? "invalid options"}`,
|
|
357
|
+
`Invalid ${source} ${label} settings${escapeControls(issue?.instancePath ?? "")}: ${issue?.message ?? "invalid options"}`,
|
|
331
358
|
);
|
|
332
359
|
}
|
|
360
|
+
checkScope(value, source);
|
|
333
361
|
return structuredClone(value);
|
|
334
362
|
}
|
|
335
363
|
|
|
@@ -340,6 +368,7 @@ export function defineLayeredSettings<
|
|
|
340
368
|
.findLast((item) => item.type === "custom" && item.customType === sessionEntryType);
|
|
341
369
|
if (!entry || entry.type !== "custom") return noOptions();
|
|
342
370
|
if (!isSessionData(entry.data)) throw new Error(`Invalid ${label} session settings`);
|
|
371
|
+
checkScope(entry.data.overrides, "session");
|
|
343
372
|
return structuredClone(entry.data.overrides);
|
|
344
373
|
}
|
|
345
374
|
|
|
@@ -368,11 +397,12 @@ export function defineLayeredSettings<
|
|
|
368
397
|
authored = readLayers(session.settingsManager),
|
|
369
398
|
) {
|
|
370
399
|
if (authored.global instanceof Error) throw authored.global;
|
|
371
|
-
const
|
|
372
|
-
if (
|
|
400
|
+
const project = session.settingsManager.isProjectTrusted() ? authored.project : noOptions();
|
|
401
|
+
if (project instanceof Error) throw project;
|
|
402
|
+
checkScope(project, "project");
|
|
373
403
|
const layers: (readonly [LayeredSettingScope, LayeredSettingLayer<Config>])[] = [
|
|
374
404
|
["global", authored.global],
|
|
375
|
-
["project",
|
|
405
|
+
["project", project],
|
|
376
406
|
];
|
|
377
407
|
if (sessionEntryType !== undefined)
|
|
378
408
|
layers.push(["session", readOverrides(session.sessionManager)]);
|
|
@@ -411,7 +441,7 @@ export function defineLayeredSettings<
|
|
|
411
441
|
keepEmpty: keepEmptyNamespace === true,
|
|
412
442
|
invalid: (reason, cause) =>
|
|
413
443
|
reason === "malformed" && cause
|
|
414
|
-
? cause
|
|
444
|
+
? new SyntaxError(escapeControls(cause.message), { cause })
|
|
415
445
|
: new Error(`Invalid ${scope} settings document; refusing to overwrite it`),
|
|
416
446
|
update: (stored) => {
|
|
417
447
|
const next = { ...stored };
|
|
@@ -441,7 +471,12 @@ export function defineLayeredSettings<
|
|
|
441
471
|
session: LayeredSettingsSession,
|
|
442
472
|
): LenientLayeredSettings<Options, Config> {
|
|
443
473
|
// Pi reads an unloadable file as empty, so only its recorded error shows the problem.
|
|
444
|
-
const invalid: Error[] = session.settingsManager.drainErrors().map(
|
|
474
|
+
const invalid: Error[] = session.settingsManager.drainErrors().map(
|
|
475
|
+
(entry) =>
|
|
476
|
+
new Error(escapeControls(entry.error.message), {
|
|
477
|
+
cause: entry.error,
|
|
478
|
+
}),
|
|
479
|
+
);
|
|
445
480
|
const layers = readLayers(session.settingsManager);
|
|
446
481
|
const usable = (layer: Options | Error): Options => {
|
|
447
482
|
if (!(layer instanceof Error)) return layer;
|
|
@@ -486,8 +521,9 @@ export function defineLayeredSettings<
|
|
|
486
521
|
const overrides = readOverrides(session.sessionManager);
|
|
487
522
|
if (change.action === "inherit") Reflect.deleteProperty(overrides, change.key);
|
|
488
523
|
else Object.assign(overrides, change.patch);
|
|
489
|
-
|
|
490
|
-
|
|
524
|
+
const validated = parseOptions(overrides, "session");
|
|
525
|
+
pi.appendEntry(sessionEntry, { version: 1, overrides: validated });
|
|
526
|
+
return validated;
|
|
491
527
|
}
|
|
492
528
|
|
|
493
529
|
return { ...settings, readOverrides, readSettingsLeniently, writeChange };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-context-kit",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.5",
|
|
4
4
|
"description": "Keep long Pi coding agent sessions sharp. Clears old file reads and tool outputs from the context, writes the compaction summary in the background, and lets the agent recall the original text.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"coding-agent",
|
package/src/shake.ts
CHANGED
|
@@ -23,7 +23,18 @@ export interface ShakeOptions {
|
|
|
23
23
|
|
|
24
24
|
/** A fenced code block, or an XML-like element with a matching closing tag. */
|
|
25
25
|
const BLOCK =
|
|
26
|
-
/(`{3,}|~{3,})[^\n]*\n[\s\S]*?\n[ \t]*\1|<([A-Za-z][\w.:-]*)(?:\s[^<>]*)?>[\s\S]*?<\/\2>/
|
|
26
|
+
/(?=(`{3,}|~{3,}))\1[^\n]*\n[\s\S]*?\n[ \t]*\1(?:(?<=`)`*|~*)|<([A-Za-z][\w.:-]*)(?:\s[^<>]*)?>[\s\S]*?<\/\2>/y;
|
|
27
|
+
const BLOCK_START = /(?<!`)`{3,}|(?<!~)~{3,}|<[A-Za-z]/g;
|
|
28
|
+
const OPEN_TAG = /<([A-Za-z][\w.:-]*)(?:\s[^<>]*)?>/y;
|
|
29
|
+
const CLOSE_TAG = /<\/([A-Za-z][\w.:-]*)>/g;
|
|
30
|
+
const FENCE_CLOSER = /\n[ \t]*(`{3,}|~{3,})/g;
|
|
31
|
+
|
|
32
|
+
/** Lines that can close a fence of one character: newline positions, ascending. */
|
|
33
|
+
interface FenceClosers {
|
|
34
|
+
at: number[];
|
|
35
|
+
/** Longest closing run on this line or any later one. */
|
|
36
|
+
longest: number[];
|
|
37
|
+
}
|
|
27
38
|
|
|
28
39
|
/**
|
|
29
40
|
* Elides large blocks and images from older messages. Tool calls and thinking stay intact,
|
|
@@ -93,12 +104,81 @@ function shakeContent(
|
|
|
93
104
|
}
|
|
94
105
|
|
|
95
106
|
function elideBlocks(text: string, id: string, minTokens: number): string {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
107
|
+
const lastCloser = new Map(
|
|
108
|
+
Array.from(text.matchAll(CLOSE_TAG), (match) => [match[1], match.index]),
|
|
109
|
+
);
|
|
110
|
+
const fenceClosers = indexFenceClosers(text);
|
|
111
|
+
let result = "";
|
|
112
|
+
let copied = 0;
|
|
113
|
+
let lineEnd = -1;
|
|
114
|
+
BLOCK_START.lastIndex = 0;
|
|
115
|
+
for (let start = BLOCK_START.exec(text); start; start = BLOCK_START.exec(text)) {
|
|
116
|
+
// Reused for every opener on the line, so many openers on one long line stay linear.
|
|
117
|
+
if (lineEnd < start.index) {
|
|
118
|
+
const next = text.indexOf("\n", start.index);
|
|
119
|
+
lineEnd = next < 0 ? text.length : next;
|
|
120
|
+
}
|
|
121
|
+
BLOCK.lastIndex = start.index;
|
|
122
|
+
const block = cannotClose(text, start, lineEnd, fenceClosers, lastCloser)
|
|
123
|
+
? null
|
|
124
|
+
: BLOCK.exec(text);
|
|
125
|
+
if (!block) continue;
|
|
126
|
+
const tokens = Math.ceil(block[0].length / 4);
|
|
127
|
+
const notice = `[Elided block: about ${tokens} tokens. Call recall with entryId "${id}" to read it.]`;
|
|
128
|
+
result += text.slice(copied, start.index) + (tokens < minTokens ? block[0] : notice);
|
|
129
|
+
copied = BLOCK_START.lastIndex = BLOCK.lastIndex;
|
|
130
|
+
}
|
|
131
|
+
return result + text.slice(copied);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Rules out openers without a closer before scanning, so unclosed input does not rescan the
|
|
136
|
+
* text to its end once per opener.
|
|
137
|
+
*/
|
|
138
|
+
function cannotClose(
|
|
139
|
+
text: string,
|
|
140
|
+
start: RegExpExecArray,
|
|
141
|
+
lineEnd: number,
|
|
142
|
+
fenceClosers: ReadonlyMap<string, FenceClosers>,
|
|
143
|
+
lastCloser: ReadonlyMap<string | undefined, number>,
|
|
144
|
+
): boolean {
|
|
145
|
+
if (!start[0].startsWith("<")) {
|
|
146
|
+
return !closesAfter(fenceClosers.get(start[0].charAt(0)), lineEnd, start[0].length);
|
|
147
|
+
}
|
|
148
|
+
OPEN_TAG.lastIndex = start.index;
|
|
149
|
+
const name = OPEN_TAG.exec(text)?.[1];
|
|
150
|
+
return name !== undefined && (lastCloser.get(name) ?? -1) < OPEN_TAG.lastIndex;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function indexFenceClosers(text: string): Map<string, FenceClosers> {
|
|
154
|
+
const byMark = new Map<string, FenceClosers>();
|
|
155
|
+
for (const match of text.matchAll(FENCE_CLOSER)) {
|
|
156
|
+
const run = match[1] ?? "";
|
|
157
|
+
const mark = run.charAt(0);
|
|
158
|
+
const closers = byMark.get(mark) ?? { at: [], longest: [] };
|
|
159
|
+
closers.at.push(match.index);
|
|
160
|
+
closers.longest.push(run.length);
|
|
161
|
+
byMark.set(mark, closers);
|
|
162
|
+
}
|
|
163
|
+
for (const { longest } of byMark.values()) {
|
|
164
|
+
for (let i = longest.length - 2; i >= 0; i--) {
|
|
165
|
+
longest[i] = Math.max(longest[i] ?? 0, longest[i + 1] ?? 0);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return byMark;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Whether a line after `lineEnd` closes a fence of `length` marks. */
|
|
172
|
+
function closesAfter(closers: FenceClosers | undefined, lineEnd: number, length: number): boolean {
|
|
173
|
+
if (!closers) return false;
|
|
174
|
+
let low = 0;
|
|
175
|
+
let high = closers.at.length;
|
|
176
|
+
while (low < high) {
|
|
177
|
+
const middle = (low + high) >>> 1;
|
|
178
|
+
if ((closers.at[middle] ?? 0) > lineEnd) high = middle;
|
|
179
|
+
else low = middle + 1;
|
|
180
|
+
}
|
|
181
|
+
return (closers.longest[low] ?? 0) >= length;
|
|
102
182
|
}
|
|
103
183
|
|
|
104
184
|
function imageNotice(id: string): TextContent {
|