proper-base 0.6.0 → 0.8.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 +270 -46
- package/UPDATES.md +129 -0
- package/index.ts +265 -136
- package/package.json +4 -3
- package/src/auto-update/availability.ts +132 -0
- package/src/auto-update/index.ts +403 -0
- package/src/auto-update/inventory.mjs +96 -0
- package/src/auto-update/readiness.ts +94 -0
- package/src/auto-update/runtime.ts +268 -0
- package/src/auto-update/settings.ts +146 -0
- package/src/autocomplete-details.ts +1 -1
- package/src/base-keybindings.ts +114 -0
- package/src/clipboard-selection.ts +310 -0
- package/src/context-tokens.ts +340 -0
- package/src/fast-mode.ts +74 -13
- package/src/footer-colors.ts +56 -3
- package/src/host-interop.ts +49 -0
- package/src/image-preview.ts +75 -66
- package/src/proactive-delegation.ts +47 -17
- package/src/prompt-display-host.ts +166 -0
- package/src/prompt-display.ts +44 -82
- package/src/prompt-sections.ts +16 -0
- package/src/session-list.ts +132 -21
- package/src/settings.ts +94 -30
- package/src/skill-context.ts +238 -159
- package/src/skills.ts +587 -0
- package/src/startup-defaults.ts +4 -122
- package/src/sticky-defaults.ts +199 -0
- package/src/transcript-cleanup.ts +8 -2
- package/src/wheel-scroll.ts +24 -27
package/src/settings.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
mkdirSync,
|
|
4
|
+
readFileSync,
|
|
5
|
+
renameSync,
|
|
6
|
+
rmSync,
|
|
7
|
+
writeFileSync,
|
|
8
|
+
} from "node:fs";
|
|
2
9
|
import { join } from "node:path";
|
|
3
10
|
|
|
4
11
|
import type { Component, TUI } from "@earendil-works/pi-tui";
|
|
@@ -23,6 +30,13 @@ const TOGGLES = [
|
|
|
23
30
|
description:
|
|
24
31
|
"proper-base: clicking the prompt moves the cursor; off leaves the cursor where typing put it",
|
|
25
32
|
},
|
|
33
|
+
{
|
|
34
|
+
id: "proper-base-skill-context",
|
|
35
|
+
key: "skillContext",
|
|
36
|
+
label: "Skill context management",
|
|
37
|
+
description:
|
|
38
|
+
"proper-base: load multiple skills, manage snapshots and restore full instructions after compaction; applies to the next run",
|
|
39
|
+
},
|
|
26
40
|
] as const;
|
|
27
41
|
type ToggleKey = (typeof TOGGLES)[number]["key"];
|
|
28
42
|
|
|
@@ -64,8 +78,13 @@ function readToggle(agentDir: string, key: ToggleKey): boolean {
|
|
|
64
78
|
readFileSync(configPath(agentDir), "utf8"),
|
|
65
79
|
) as unknown;
|
|
66
80
|
return (parsed as Record<string, unknown> | null)?.[key] !== false;
|
|
67
|
-
} catch {
|
|
68
|
-
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (
|
|
83
|
+
error instanceof SyntaxError ||
|
|
84
|
+
typeof (error as NodeJS.ErrnoException).code === "string"
|
|
85
|
+
)
|
|
86
|
+
return true;
|
|
87
|
+
throw error;
|
|
69
88
|
}
|
|
70
89
|
}
|
|
71
90
|
|
|
@@ -77,28 +96,59 @@ export function readEditorMouseEnabled(agentDir: string): boolean {
|
|
|
77
96
|
return readToggle(agentDir, "editorMouse");
|
|
78
97
|
}
|
|
79
98
|
|
|
80
|
-
|
|
81
|
-
|
|
99
|
+
export function readSkillContextEnabled(agentDir: string): boolean {
|
|
100
|
+
try {
|
|
101
|
+
const parsed: unknown = JSON.parse(
|
|
102
|
+
readFileSync(configPath(agentDir), "utf8"),
|
|
103
|
+
);
|
|
104
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
105
|
+
return false;
|
|
106
|
+
const value = (parsed as Record<string, unknown>).skillContext;
|
|
107
|
+
return value === undefined || value === true;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return true;
|
|
110
|
+
if (
|
|
111
|
+
error instanceof SyntaxError ||
|
|
112
|
+
typeof (error as NodeJS.ErrnoException).code === "string"
|
|
113
|
+
)
|
|
114
|
+
return false;
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function writeSkillContextEnabled(
|
|
120
|
+
agentDir: string,
|
|
121
|
+
enabled: boolean,
|
|
122
|
+
): void {
|
|
123
|
+
writeToggle(agentDir, "skillContext", enabled);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Fresh-disk merge and atomic replacement; failed saves keep the live value. */
|
|
82
127
|
function writeToggle(agentDir: string, key: ToggleKey, enabled: boolean): void {
|
|
128
|
+
let parsed: Record<string, unknown> = {};
|
|
83
129
|
try {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const existing = JSON.parse(
|
|
87
|
-
readFileSync(configPath(agentDir), "utf8"),
|
|
88
|
-
) as unknown;
|
|
89
|
-
if (existing && typeof existing === "object") {
|
|
90
|
-
parsed = existing as Record<string, unknown>;
|
|
91
|
-
}
|
|
92
|
-
} catch {
|
|
93
|
-
// Damaged or absent config starts fresh.
|
|
94
|
-
}
|
|
95
|
-
parsed[key] = enabled;
|
|
96
|
-
writeFileSync(
|
|
97
|
-
configPath(agentDir),
|
|
98
|
-
`${JSON.stringify(parsed, null, "\t")}\n`,
|
|
130
|
+
const existing: unknown = JSON.parse(
|
|
131
|
+
readFileSync(configPath(agentDir), "utf8"),
|
|
99
132
|
);
|
|
100
|
-
|
|
101
|
-
|
|
133
|
+
if (!existing || typeof existing !== "object" || Array.isArray(existing))
|
|
134
|
+
throw new SyntaxError(
|
|
135
|
+
"proper-base.json must contain an object; preference was not saved.",
|
|
136
|
+
);
|
|
137
|
+
parsed = existing as Record<string, unknown>;
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
140
|
+
}
|
|
141
|
+
parsed[key] = enabled;
|
|
142
|
+
mkdirSync(agentDir, { recursive: true });
|
|
143
|
+
const temporary = `${configPath(agentDir)}.${randomUUID()}.tmp`;
|
|
144
|
+
try {
|
|
145
|
+
writeFileSync(temporary, `${JSON.stringify(parsed, null, "\t")}\n`, {
|
|
146
|
+
flag: "wx",
|
|
147
|
+
mode: 0o600,
|
|
148
|
+
});
|
|
149
|
+
renameSync(temporary, configPath(agentDir));
|
|
150
|
+
} finally {
|
|
151
|
+
rmSync(temporary, { force: true });
|
|
102
152
|
}
|
|
103
153
|
}
|
|
104
154
|
|
|
@@ -139,8 +189,7 @@ function findEditorContainer(
|
|
|
139
189
|
}
|
|
140
190
|
|
|
141
191
|
/**
|
|
142
|
-
* Add proper-base's toggles to Pi's native `/settings` menu
|
|
143
|
-
* action rail and prompt mouse clicks.
|
|
192
|
+
* Add proper-base's toggles to Pi's native `/settings` menu.
|
|
144
193
|
*
|
|
145
194
|
* Pi has no extension hook into that menu, so this is a guarded
|
|
146
195
|
* compatibility layer in the house style: the container Pi mounts selectors
|
|
@@ -148,11 +197,11 @@ function findEditorContainer(
|
|
|
148
197
|
* settings selector that mounts receives the extra items spliced into its
|
|
149
198
|
* `SettingsList`, whose instance-held `onChange` is wrapped to keep the
|
|
150
199
|
* toggles out of Pi's own switch. A renamed component or list shape installs
|
|
151
|
-
* or injects nothing and
|
|
200
|
+
* or injects nothing; config-file and command controls remain available.
|
|
152
201
|
*
|
|
153
|
-
* Choices persist as `sessionRail` and `
|
|
154
|
-
* directory's `proper-base.json
|
|
155
|
-
* next
|
|
202
|
+
* Choices persist as `sessionRail`, `editorMouse` and `skillContext` in the
|
|
203
|
+
* agent directory's `proper-base.json`. Skill management reads its preference
|
|
204
|
+
* at the next run; the editor toggles remain cached for the session.
|
|
156
205
|
*
|
|
157
206
|
* @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Session action rail]]
|
|
158
207
|
* @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Prompt mouse clicks]]
|
|
@@ -161,15 +210,18 @@ export function installSettings(
|
|
|
161
210
|
tui: TUI,
|
|
162
211
|
editor: Component,
|
|
163
212
|
agentDir: string,
|
|
213
|
+
onError?: (error: Error) => void,
|
|
164
214
|
): SettingsController {
|
|
165
215
|
const state: Record<ToggleKey, boolean> = {
|
|
166
216
|
sessionRail: readToggle(agentDir, "sessionRail"),
|
|
167
217
|
editorMouse: readToggle(agentDir, "editorMouse"),
|
|
218
|
+
skillContext: readSkillContextEnabled(agentDir),
|
|
168
219
|
};
|
|
169
220
|
let disposed = false;
|
|
170
221
|
let uninstall: (() => void) | undefined;
|
|
171
222
|
|
|
172
223
|
const inject = (selector: SettingsHost): void => {
|
|
224
|
+
state.skillContext = readSkillContextEnabled(agentDir);
|
|
173
225
|
const list = selector.settingsList;
|
|
174
226
|
const items = list?.items;
|
|
175
227
|
if (!list || !Array.isArray(items) || typeof list.onChange !== "function")
|
|
@@ -188,8 +240,20 @@ export function installSettings(
|
|
|
188
240
|
list.onChange = (id, value) => {
|
|
189
241
|
const toggle = TOGGLES.find((entry) => entry.id === id);
|
|
190
242
|
if (toggle) {
|
|
191
|
-
|
|
192
|
-
|
|
243
|
+
if (disposed || (value !== "true" && value !== "false")) return;
|
|
244
|
+
try {
|
|
245
|
+
writeToggle(agentDir, toggle.key, value === "true");
|
|
246
|
+
state[toggle.key] = value === "true";
|
|
247
|
+
} catch (error) {
|
|
248
|
+
if (
|
|
249
|
+
!(error instanceof SyntaxError) &&
|
|
250
|
+
typeof (error as NodeJS.ErrnoException).code !== "string"
|
|
251
|
+
)
|
|
252
|
+
throw error;
|
|
253
|
+
onError?.(error as Error);
|
|
254
|
+
}
|
|
255
|
+
const item = items.find((entry) => entry.id === id);
|
|
256
|
+
if (item) item.currentValue = String(state[toggle.key]);
|
|
193
257
|
return;
|
|
194
258
|
}
|
|
195
259
|
native(id, value);
|
package/src/skill-context.ts
CHANGED
|
@@ -1,25 +1,79 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
type ContextEditEntry,
|
|
4
|
+
parseSkillBlock,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
2
6
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
/** Combined character budget for every skill carried across one compaction. */
|
|
10
|
-
const MAX_CARRY_CHARS = 24000;
|
|
11
|
-
const TRUNCATED = "\n\n[skill content truncated to fit the context budget]";
|
|
7
|
+
export const SKILL_CONTEXT_ENTRY = "proper-base-skill-context";
|
|
8
|
+
export const SKILL_CONTEXT_TOOL = "skill_context";
|
|
9
|
+
/** Full instruction bodies above this threshold require user confirmation. */
|
|
10
|
+
export const SKILL_CONTEXT_WARNING_CHARS = 200_000;
|
|
11
|
+
const RESTORE_START = "<proper_base_skill_context>\n";
|
|
12
|
+
const RESTORE_END = "\n</proper_base_skill_context>\n\n";
|
|
12
13
|
|
|
13
|
-
type
|
|
14
|
+
export type SkillSnapshot = {
|
|
15
|
+
name: string;
|
|
16
|
+
location: string;
|
|
17
|
+
block: string;
|
|
18
|
+
hash: string;
|
|
19
|
+
};
|
|
20
|
+
export type SkillSelection = SkillSnapshot & {
|
|
21
|
+
selected: boolean;
|
|
22
|
+
source: "user" | "model";
|
|
23
|
+
};
|
|
24
|
+
export type SkillChange =
|
|
25
|
+
| { action: "load"; blocks: string[] }
|
|
26
|
+
| { action: "remove"; locations: string[] }
|
|
27
|
+
| { action: "clear" };
|
|
28
|
+
export type ContextMessage = {
|
|
14
29
|
role: string;
|
|
15
30
|
content?: unknown;
|
|
31
|
+
summary?: string;
|
|
32
|
+
toolName?: string;
|
|
33
|
+
isError?: boolean;
|
|
16
34
|
};
|
|
17
|
-
type
|
|
18
|
-
type SessionEntry = {
|
|
35
|
+
export type SkillBranchEntry = {
|
|
19
36
|
type: string;
|
|
20
|
-
|
|
37
|
+
id?: string;
|
|
38
|
+
targetId?: string;
|
|
39
|
+
replacement?: ContextEditEntry["replacement"];
|
|
40
|
+
message?: ContextMessage;
|
|
41
|
+
customType?: string;
|
|
42
|
+
data?: unknown;
|
|
21
43
|
};
|
|
22
|
-
type
|
|
44
|
+
type TextPart = { type: "text"; text: string };
|
|
45
|
+
|
|
46
|
+
export class SkillContextError extends Error {}
|
|
47
|
+
|
|
48
|
+
export function skillSnapshot(block: string): SkillSnapshot | undefined {
|
|
49
|
+
const parsed = parseSkillBlock(block);
|
|
50
|
+
if (!parsed || parsed.userMessage) return undefined;
|
|
51
|
+
return {
|
|
52
|
+
name: parsed.name,
|
|
53
|
+
location: parsed.location,
|
|
54
|
+
block,
|
|
55
|
+
hash: createHash("sha256").update(block).digest("hex"),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Only leading Pi skill blocks are instructions, never quoted inline examples. */
|
|
60
|
+
export function splitSkillText(text: string): {
|
|
61
|
+
skills: SkillSnapshot[];
|
|
62
|
+
request: string;
|
|
63
|
+
} {
|
|
64
|
+
const skills: SkillSnapshot[] = [];
|
|
65
|
+
let request = text;
|
|
66
|
+
while (request.startsWith('<skill name="')) {
|
|
67
|
+
const parsed = parseSkillBlock(request);
|
|
68
|
+
if (!parsed) break;
|
|
69
|
+
const block = `<skill name="${parsed.name}" location="${parsed.location}">\n${parsed.content}\n</skill>`;
|
|
70
|
+
const snapshot = skillSnapshot(block);
|
|
71
|
+
if (!snapshot) break;
|
|
72
|
+
skills.push(snapshot);
|
|
73
|
+
request = parsed.userMessage ?? "";
|
|
74
|
+
}
|
|
75
|
+
return { skills, request };
|
|
76
|
+
}
|
|
23
77
|
|
|
24
78
|
function isTextPart(value: unknown): value is TextPart {
|
|
25
79
|
return (
|
|
@@ -30,171 +84,196 @@ function isTextPart(value: unknown): value is TextPart {
|
|
|
30
84
|
);
|
|
31
85
|
}
|
|
32
86
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
text: string,
|
|
39
|
-
): { name: string; block: string; request: string } | undefined {
|
|
40
|
-
const parsed = parseSkillBlock(text);
|
|
41
|
-
if (!parsed) return undefined;
|
|
42
|
-
return {
|
|
43
|
-
name: parsed.name,
|
|
44
|
-
block: `<skill name="${parsed.name}" location="${parsed.location}">\n${parsed.content}\n</skill>`,
|
|
45
|
-
request: parsed.userMessage ?? "",
|
|
46
|
-
};
|
|
87
|
+
function texts(message: ContextMessage): string[] {
|
|
88
|
+
if (typeof message.content === "string") return [message.content];
|
|
89
|
+
return Array.isArray(message.content)
|
|
90
|
+
? message.content.filter(isTextPart).map((part) => part.text)
|
|
91
|
+
: [];
|
|
47
92
|
}
|
|
48
93
|
|
|
49
|
-
function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (!isTextPart(part)) continue;
|
|
57
|
-
const invocation = readInvocation(part.text);
|
|
58
|
-
if (invocation) return invocation;
|
|
59
|
-
}
|
|
60
|
-
return undefined;
|
|
94
|
+
function skillMessage(message: ContextMessage): boolean {
|
|
95
|
+
return (
|
|
96
|
+
message.role === "user" ||
|
|
97
|
+
(message.role === "toolResult" &&
|
|
98
|
+
message.toolName === SKILL_CONTEXT_TOOL &&
|
|
99
|
+
message.isError !== true)
|
|
100
|
+
);
|
|
61
101
|
}
|
|
62
102
|
|
|
63
|
-
function
|
|
64
|
-
message:
|
|
65
|
-
|
|
66
|
-
):
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return { ...message, content
|
|
103
|
+
function mapText<T extends ContextMessage>(
|
|
104
|
+
message: T,
|
|
105
|
+
transform: (text: string) => string,
|
|
106
|
+
): T {
|
|
107
|
+
if (typeof message.content === "string") {
|
|
108
|
+
const content = transform(message.content);
|
|
109
|
+
return content === message.content ? message : { ...message, content };
|
|
70
110
|
}
|
|
71
|
-
if (!Array.isArray(content)) return message;
|
|
72
|
-
let
|
|
73
|
-
const
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
111
|
+
if (!Array.isArray(message.content)) return message;
|
|
112
|
+
let changed = false;
|
|
113
|
+
const content = message.content.map((part) => {
|
|
114
|
+
if (!isTextPart(part)) return part;
|
|
115
|
+
const text = transform(part.text);
|
|
116
|
+
if (text === part.text) return part;
|
|
117
|
+
changed = true;
|
|
118
|
+
return { ...part, text };
|
|
77
119
|
});
|
|
78
|
-
return
|
|
120
|
+
return changed ? { ...message, content } : message;
|
|
79
121
|
}
|
|
80
122
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
123
|
+
/** Apply branch-local edits before interpreting invocation or control records. */
|
|
124
|
+
export function selectedSkills(branch: SkillBranchEntry[]): SkillSelection[] {
|
|
125
|
+
const edits = new Map<string, ContextEditEntry["replacement"]>();
|
|
126
|
+
for (const entry of branch) {
|
|
127
|
+
if (entry.type === "context_edit" && entry.targetId)
|
|
128
|
+
edits.set(entry.targetId, entry.replacement ?? null);
|
|
85
129
|
}
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
130
|
+
const state = new Map<string, SkillSelection>();
|
|
131
|
+
const load = (block: string, source: SkillSelection["source"]) => {
|
|
132
|
+
for (const skill of splitSkillText(block).skills) {
|
|
133
|
+
const previous = state.get(skill.location);
|
|
134
|
+
state.delete(skill.location);
|
|
135
|
+
state.set(skill.location, {
|
|
136
|
+
...skill,
|
|
137
|
+
selected: true,
|
|
138
|
+
source:
|
|
139
|
+
previous?.selected && previous.source === "user" ? "user" : source,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
for (const entry of branch) {
|
|
144
|
+
if (entry.type === "message" && entry.message) {
|
|
145
|
+
let message = entry.message;
|
|
146
|
+
if (entry.id && edits.has(entry.id)) {
|
|
147
|
+
const replacement = edits.get(entry.id);
|
|
148
|
+
if (!replacement) continue;
|
|
149
|
+
message = { ...message, content: replacement.content };
|
|
150
|
+
}
|
|
151
|
+
if (skillMessage(message))
|
|
152
|
+
for (const text of texts(message))
|
|
153
|
+
load(text, message.role === "user" ? "user" : "model");
|
|
154
|
+
} else if (
|
|
155
|
+
entry.type === "custom" &&
|
|
156
|
+
entry.customType === SKILL_CONTEXT_ENTRY &&
|
|
157
|
+
entry.data &&
|
|
158
|
+
typeof entry.data === "object"
|
|
159
|
+
) {
|
|
160
|
+
const data = entry.data as Partial<SkillChange>;
|
|
161
|
+
if (data.action === "load" && Array.isArray(data.blocks)) {
|
|
162
|
+
for (const block of data.blocks)
|
|
163
|
+
if (typeof block === "string") load(block, "user");
|
|
164
|
+
} else if (data.action === "clear") {
|
|
165
|
+
for (const skill of state.values()) skill.selected = false;
|
|
166
|
+
} else if (data.action === "remove" && Array.isArray(data.locations)) {
|
|
167
|
+
for (const location of data.locations) {
|
|
168
|
+
const skill = state.get(location);
|
|
169
|
+
if (skill) skill.selected = false;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
93
173
|
}
|
|
94
|
-
|
|
95
|
-
const next = [...content];
|
|
96
|
-
next[index] = { ...part, text: `${prefix}${part.text}` };
|
|
97
|
-
return { ...message, content: next };
|
|
174
|
+
return [...state.values()];
|
|
98
175
|
}
|
|
99
176
|
|
|
100
|
-
function
|
|
101
|
-
return
|
|
177
|
+
export function skillContextChars(skills: SkillSnapshot[]): number {
|
|
178
|
+
return skills.reduce((sum, skill) => sum + skill.block.length + 2, 0);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function stripRestore(text: string): string {
|
|
182
|
+
const start = text.indexOf(RESTORE_START);
|
|
183
|
+
if (start < 0) return text;
|
|
184
|
+
const end = text.indexOf(RESTORE_END, start);
|
|
185
|
+
return end < 0
|
|
186
|
+
? text
|
|
187
|
+
: text.slice(0, start) + text.slice(end + RESTORE_END.length);
|
|
102
188
|
}
|
|
103
189
|
|
|
104
190
|
/**
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
* compaction summarizes the copy away while the model keeps working under
|
|
110
|
-
* instructions it can no longer read. This collapses repeats to a short note
|
|
111
|
-
* and carries the most recent body across a compaction boundary, bounded by a
|
|
112
|
-
* character budget so the restored text cannot re-trigger the compaction that
|
|
113
|
-
* just ran.
|
|
114
|
-
*
|
|
115
|
-
* The first copy is never rewritten, so the cached request prefix stays byte
|
|
116
|
-
* stable across turns.
|
|
191
|
+
* Project full selected snapshots without changing stored messages. Restoring
|
|
192
|
+
* reference instructions is not a new workflow invocation. The branch is the
|
|
193
|
+
* registry, so resume, forks, context edits and /clear need no second store.
|
|
194
|
+
* @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Skill context]]
|
|
117
195
|
*/
|
|
118
196
|
export function pinSkillContext<T extends ContextMessage>(
|
|
119
197
|
messages: T[],
|
|
120
|
-
branch:
|
|
198
|
+
branch: SkillBranchEntry[],
|
|
121
199
|
): T[] {
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return message;
|
|
200
|
+
const selections = selectedSkills(branch);
|
|
201
|
+
const active = selections.filter((skill) => skill.selected);
|
|
202
|
+
const state = new Map(selections.map((skill) => [skill.location, skill]));
|
|
203
|
+
const present = new Set<string>();
|
|
204
|
+
const next = messages.map((original) => {
|
|
205
|
+
let message = original;
|
|
206
|
+
if (message.role === "compactionSummary" && message.summary) {
|
|
207
|
+
const summary = stripRestore(message.summary);
|
|
208
|
+
if (summary !== message.summary) message = { ...message, summary };
|
|
132
209
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
210
|
+
if (!skillMessage(message)) return message;
|
|
211
|
+
return mapText(message, (raw) => {
|
|
212
|
+
const text = stripRestore(raw);
|
|
213
|
+
const { skills, request } = splitSkillText(text);
|
|
214
|
+
if (!skills.length) return text;
|
|
215
|
+
let changed = false;
|
|
216
|
+
const blocks: string[] = [];
|
|
217
|
+
const notes: string[] = [];
|
|
218
|
+
for (const skill of skills) {
|
|
219
|
+
const selection = state.get(skill.location);
|
|
220
|
+
let reason: string | undefined;
|
|
221
|
+
if (selection && !selection.selected) reason = "inactive";
|
|
222
|
+
else if (selection && selection.hash !== skill.hash)
|
|
223
|
+
reason = "superseded";
|
|
224
|
+
else if (present.has(skill.hash))
|
|
225
|
+
reason = "already loaded earlier in this conversation";
|
|
226
|
+
else present.add(skill.hash);
|
|
227
|
+
if (!reason) blocks.push(skill.block);
|
|
228
|
+
else {
|
|
229
|
+
changed = true;
|
|
230
|
+
notes.push(
|
|
231
|
+
`[skill ${JSON.stringify(skill.name)} is ${reason}; this request remains a separate request]`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return changed
|
|
236
|
+
? [...blocks, ...notes, ...(request ? [request] : [])].join("\n\n")
|
|
237
|
+
: text;
|
|
238
|
+
});
|
|
139
239
|
});
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
)
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
const blocks: string[] = [];
|
|
176
|
-
let used = 0;
|
|
177
|
-
for (const invocation of [...missing.values()].reverse()) {
|
|
178
|
-
const block = truncate(invocation.block, MAX_SKILL_CHARS);
|
|
179
|
-
if (used + block.length > MAX_CARRY_CHARS) continue;
|
|
180
|
-
used += block.length;
|
|
181
|
-
blocks.unshift(block);
|
|
182
|
-
}
|
|
183
|
-
return blocks.length ? `${blocks.join("\n\n")}\n\n` : undefined;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/** The first user turn after the newest compaction summary. */
|
|
187
|
-
function restoreAnchor(messages: ContextMessage[]): number {
|
|
188
|
-
let summary = -1;
|
|
189
|
-
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
190
|
-
if (messages[index]?.role === "compactionSummary") {
|
|
191
|
-
summary = index;
|
|
192
|
-
break;
|
|
240
|
+
const missing = active.filter((skill) => !present.has(skill.hash));
|
|
241
|
+
if (missing.length || selections.some((skill) => !skill.selected)) {
|
|
242
|
+
const selected =
|
|
243
|
+
active
|
|
244
|
+
.map((skill) => `${skill.name} [${skill.hash.slice(0, 12)}]`)
|
|
245
|
+
.join(", ") || "none";
|
|
246
|
+
const restored = `${RESTORE_START}Currently selected skills: ${selected}. Only these selected versions apply; other historical skill guidance is inactive even when mentioned in a summary. Retained reference instructions follow. Apply only where relevant to the current request. Do not repeat completed actions or treat restoration as a new invocation. Explicit user priorities and higher-priority instructions still apply; report material conflicts rather than assuming skill order decides them.\n\n${missing.map((skill) => skill.block).join("\n\n")}${RESTORE_END}`;
|
|
247
|
+
// Anchor after the latest summary; a split tool loop may have no user
|
|
248
|
+
// message left. Append to that summary instead of breaking tool pairs.
|
|
249
|
+
let summary = -1;
|
|
250
|
+
for (let i = 0; i < next.length; i++)
|
|
251
|
+
if (next[i]?.role === "compactionSummary") summary = i;
|
|
252
|
+
const anchor = next.findIndex(
|
|
253
|
+
(message, i) => i > summary && message.role === "user",
|
|
254
|
+
);
|
|
255
|
+
if (anchor >= 0) {
|
|
256
|
+
let added = false;
|
|
257
|
+
const message = next[anchor] as T;
|
|
258
|
+
next[anchor] = mapText(message, (text) => {
|
|
259
|
+
if (added) return text;
|
|
260
|
+
added = true;
|
|
261
|
+
return restored + text;
|
|
262
|
+
});
|
|
263
|
+
if (!added && Array.isArray(message.content))
|
|
264
|
+
next[anchor] = {
|
|
265
|
+
...message,
|
|
266
|
+
content: [{ type: "text", text: restored }, ...message.content],
|
|
267
|
+
};
|
|
268
|
+
} else if (summary >= 0) {
|
|
269
|
+
const message = next[summary] as T;
|
|
270
|
+
next[summary] = {
|
|
271
|
+
...message,
|
|
272
|
+
summary: `${restored}${message.summary ?? ""}`,
|
|
273
|
+
};
|
|
193
274
|
}
|
|
194
275
|
}
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
}
|
|
199
|
-
return -1;
|
|
276
|
+
if (next.every((message, index) => message === messages[index]))
|
|
277
|
+
return messages;
|
|
278
|
+
return next;
|
|
200
279
|
}
|