@oyasmi/pipiclaw 0.8.4 → 0.8.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/README.md +2 -1
- package/dist/agent/channel-runner.d.ts +28 -0
- package/dist/agent/channel-runner.js +137 -45
- package/dist/agent/commands.d.ts +2 -2
- package/dist/agent/commands.js +10 -1
- package/dist/agent/maintenance-context.js +1 -1
- package/dist/agent/prompt/builder.d.ts +32 -0
- package/dist/agent/prompt/builder.js +252 -0
- package/dist/agent/prompt/extension.d.ts +22 -0
- package/dist/agent/prompt/extension.js +24 -0
- package/dist/agent/prompt/manifest.d.ts +51 -0
- package/dist/agent/prompt/manifest.js +84 -0
- package/dist/agent/prompt/resources.d.ts +18 -0
- package/dist/agent/prompt/resources.js +61 -0
- package/dist/agent/prompt/sections.d.ts +39 -0
- package/dist/agent/prompt/sections.js +267 -0
- package/dist/agent/prompt/types.d.ts +110 -0
- package/dist/agent/prompt/types.js +11 -0
- package/dist/agent/types.d.ts +2 -0
- package/dist/agent/workspace-resources.d.ts +25 -13
- package/dist/agent/workspace-resources.js +28 -41
- package/dist/index.d.ts +7 -2
- package/dist/index.js +6 -2
- package/dist/memory/transcript.js +7 -6
- package/dist/playbooks/catalog.d.ts +22 -1
- package/dist/playbooks/catalog.js +57 -4
- package/dist/playbooks/event-scheduling.md +2 -0
- package/dist/playbooks/memory-and-learning.md +2 -0
- package/dist/playbooks/runtime-orientation.md +1 -0
- package/dist/playbooks/task-closeout.md +3 -1
- package/dist/playbooks/task-delegation.md +3 -1
- package/dist/playbooks/task-driving.md +2 -0
- package/dist/playbooks/task-planning.md +2 -0
- package/dist/playbooks/task-recurring.md +2 -0
- package/dist/playbooks/task-repair.md +3 -1
- package/dist/runtime/bootstrap.js +7 -38
- package/dist/runtime/dingtalk.d.ts +1 -0
- package/dist/runtime/dingtalk.js +3 -0
- package/dist/runtime/workspace-templates.d.ts +11 -0
- package/dist/runtime/workspace-templates.js +51 -0
- package/dist/tui/commands.d.ts +1 -0
- package/dist/tui/commands.js +2 -0
- package/dist/tui/turn-controller.js +1 -0
- package/package.json +1 -1
- package/dist/agent/prompt-builder.d.ts +0 -12
- package/dist/agent/prompt-builder.js +0 -71
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt pipeline: filter → sort → render → budget → fingerprint (spec 025).
|
|
3
|
+
*
|
|
4
|
+
* The output is deterministic: same build context in, same bytes and same
|
|
5
|
+
* fingerprint out. Nothing here may read the clock, the filesystem iteration
|
|
6
|
+
* order, a channel id, or anything else that would make two channels in one
|
|
7
|
+
* workspace disagree — that is what makes the provider-side prompt cache hold.
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { FINAL_BOUNDARY_SECTION, MAIN_PROMPT_SECTIONS } from "./sections.js";
|
|
11
|
+
/** Bumped whenever the runtime-authored prompt text changes in a way worth attributing in telemetry. */
|
|
12
|
+
export const RUNTIME_PROMPT_VERSION = 2;
|
|
13
|
+
/** Above this the prompt still works but is worth a warning. */
|
|
14
|
+
export const SOFT_TOTAL_BUDGET_CHARS = 20_000;
|
|
15
|
+
/**
|
|
16
|
+
* Above this we shrink user-owned catalogs and files; runtime core is never cut.
|
|
17
|
+
*
|
|
18
|
+
* Both totals cover the Pipiclaw-owned sections only. pi renders `<available_skills>`
|
|
19
|
+
* after them and Pipiclaw cannot trim that list without also destroying `/skill:name`
|
|
20
|
+
* (spec 025 §6.10), so skills are *warned about* here, not shrunk: the prompt actually
|
|
21
|
+
* sent can exceed this cap by the size of the skills catalog.
|
|
22
|
+
*/
|
|
23
|
+
export const HARD_TOTAL_BUDGET_CHARS = 32_000;
|
|
24
|
+
/** Skills are pi's to render; over this we can only tell the operator (spec 025 §8.1). */
|
|
25
|
+
export const SKILLS_BUDGET_CHARS = 6_000;
|
|
26
|
+
/**
|
|
27
|
+
* Least-important first: the order in which sections give up characters when the
|
|
28
|
+
* whole prompt exceeds the hard cap (spec 025 §8.2). Runtime-authored sections
|
|
29
|
+
* are absent by design — they are never shrunk.
|
|
30
|
+
*/
|
|
31
|
+
const SHRINK_ORDER = ["subagents", "playbooks", "workspace.agents", "workspace.soul"];
|
|
32
|
+
/** A shrunk workspace file still keeps this much of itself; below it, the file says nothing useful. */
|
|
33
|
+
const MIN_SHRUNK_SECTION_CHARS = 800;
|
|
34
|
+
export function sha256(text) {
|
|
35
|
+
return createHash("sha256").update(text, "utf8").digest("hex");
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Rough token estimate for reporting only. CJK runs about one token per
|
|
39
|
+
* character, Latin text about four characters per token; the provider's real
|
|
40
|
+
* tokenizer is the authority, and the usage ledger records what it billed.
|
|
41
|
+
*/
|
|
42
|
+
const CJK_REGEX = /\p{Script=Han}|\p{Script=Hiragana}|\p{Script=Katakana}|\p{Script=Hangul}/u;
|
|
43
|
+
export function estimateTokens(text) {
|
|
44
|
+
let cjk = 0;
|
|
45
|
+
for (const char of text) {
|
|
46
|
+
if (CJK_REGEX.test(char))
|
|
47
|
+
cjk++;
|
|
48
|
+
}
|
|
49
|
+
return Math.ceil(cjk + (text.length - cjk) / 4);
|
|
50
|
+
}
|
|
51
|
+
function truncateItems(content, maxChars) {
|
|
52
|
+
const lines = content.split("\n");
|
|
53
|
+
const kept = [];
|
|
54
|
+
let used = 0;
|
|
55
|
+
let omitted = 0;
|
|
56
|
+
for (const line of lines) {
|
|
57
|
+
const cost = line.length + 1;
|
|
58
|
+
if (omitted === 0 && used + cost <= maxChars - 80) {
|
|
59
|
+
kept.push(line);
|
|
60
|
+
used += cost;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
omitted++;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
kept.push(`[${omitted} more entries omitted: prompt budget reached. Ask for the full list if you need it.]`);
|
|
67
|
+
return kept.join("\n");
|
|
68
|
+
}
|
|
69
|
+
function truncateHeadTail(content, maxChars, hint) {
|
|
70
|
+
const marker = `\n\n[... ${hint} ...]\n\n`;
|
|
71
|
+
const room = Math.max(0, maxChars - marker.length);
|
|
72
|
+
const head = Math.floor(room * 0.6);
|
|
73
|
+
const tail = room - head;
|
|
74
|
+
return `${content.slice(0, head).trimEnd()}${marker}${content.slice(content.length - tail).trimStart()}`;
|
|
75
|
+
}
|
|
76
|
+
function applyOverflow(definition, content, diagnostics) {
|
|
77
|
+
if (content.length <= definition.maxChars) {
|
|
78
|
+
return { content, truncated: false };
|
|
79
|
+
}
|
|
80
|
+
switch (definition.overflow) {
|
|
81
|
+
case "omit":
|
|
82
|
+
diagnostics.push({
|
|
83
|
+
level: "warning",
|
|
84
|
+
sectionId: definition.id,
|
|
85
|
+
message: `${content.length} chars exceeds the ${definition.maxChars} char budget; section omitted.`,
|
|
86
|
+
});
|
|
87
|
+
return { content: "", truncated: true };
|
|
88
|
+
case "truncate-items":
|
|
89
|
+
diagnostics.push({
|
|
90
|
+
level: "warning",
|
|
91
|
+
sectionId: definition.id,
|
|
92
|
+
message: `${content.length} chars exceeds the ${definition.maxChars} char budget; trailing entries dropped.`,
|
|
93
|
+
});
|
|
94
|
+
return { content: truncateItems(content, definition.maxChars), truncated: true };
|
|
95
|
+
case "truncate-head-tail":
|
|
96
|
+
diagnostics.push({
|
|
97
|
+
level: "warning",
|
|
98
|
+
sectionId: definition.id,
|
|
99
|
+
message: `${content.length} chars exceeds the ${definition.maxChars} char budget; middle truncated. Run /context detail, and move procedures into workspace skills so they load on demand.`,
|
|
100
|
+
});
|
|
101
|
+
return {
|
|
102
|
+
content: truncateHeadTail(content, definition.maxChars, "truncated: see /context detail"),
|
|
103
|
+
truncated: true,
|
|
104
|
+
};
|
|
105
|
+
case "error":
|
|
106
|
+
// Runtime-authored text overflowing its budget is a development error, not a
|
|
107
|
+
// user problem: fail the build's diagnostics (tests assert on them) but still
|
|
108
|
+
// ship a bounded prompt rather than crashing a live channel.
|
|
109
|
+
diagnostics.push({
|
|
110
|
+
level: "error",
|
|
111
|
+
sectionId: definition.id,
|
|
112
|
+
message: `runtime-authored section is ${content.length} chars, over its ${definition.maxChars} char budget. Shorten it in src/agent/prompt/sections.ts.`,
|
|
113
|
+
});
|
|
114
|
+
return {
|
|
115
|
+
content: truncateHeadTail(content, definition.maxChars, "runtime section over budget"),
|
|
116
|
+
truncated: true,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function resolve(definition, context, diagnostics) {
|
|
121
|
+
if (definition.modes && !definition.modes.includes(context.mode)) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
if (definition.requiresAllTools?.some((name) => !context.tools.some((tool) => tool.name === name))) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
const raw = definition.render(context)?.trim();
|
|
128
|
+
if (!raw) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
const { content, truncated } = applyOverflow(definition, raw, diagnostics);
|
|
132
|
+
if (!content) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
id: definition.id,
|
|
137
|
+
order: definition.order,
|
|
138
|
+
source: definition.source,
|
|
139
|
+
authority: definition.authority,
|
|
140
|
+
cacheClass: definition.cacheClass,
|
|
141
|
+
content,
|
|
142
|
+
rawChars: raw.length,
|
|
143
|
+
injectedChars: content.length,
|
|
144
|
+
truncated,
|
|
145
|
+
sha256: sha256(content),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/** Re-shrink user-owned sections until the whole prompt fits the hard cap. */
|
|
149
|
+
function enforceTotalBudget(sections, diagnostics) {
|
|
150
|
+
const total = () => sections.reduce((sum, section) => sum + section.injectedChars, 0) + 2 * (sections.length - 1);
|
|
151
|
+
for (const id of SHRINK_ORDER) {
|
|
152
|
+
if (total() <= HARD_TOTAL_BUDGET_CHARS) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const index = sections.findIndex((section) => section.id === id);
|
|
156
|
+
if (index < 0)
|
|
157
|
+
continue;
|
|
158
|
+
const section = sections[index];
|
|
159
|
+
const overBy = total() - HARD_TOTAL_BUDGET_CHARS;
|
|
160
|
+
const target = section.injectedChars - overBy;
|
|
161
|
+
if (target < MIN_SHRUNK_SECTION_CHARS) {
|
|
162
|
+
sections.splice(index, 1);
|
|
163
|
+
diagnostics.push({
|
|
164
|
+
level: "warning",
|
|
165
|
+
sectionId: id,
|
|
166
|
+
message: `dropped to keep the prompt under the ${HARD_TOTAL_BUDGET_CHARS} char hard cap. Run /context detail to see what is competing for space.`,
|
|
167
|
+
});
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const content = truncateHeadTail(section.content, target, "shrunk to fit the total prompt budget");
|
|
171
|
+
sections[index] = {
|
|
172
|
+
...section,
|
|
173
|
+
content,
|
|
174
|
+
injectedChars: content.length,
|
|
175
|
+
truncated: true,
|
|
176
|
+
sha256: sha256(content),
|
|
177
|
+
};
|
|
178
|
+
diagnostics.push({
|
|
179
|
+
level: "warning",
|
|
180
|
+
sectionId: id,
|
|
181
|
+
message: `shrunk to fit the ${HARD_TOTAL_BUDGET_CHARS} char hard cap. Run /context detail.`,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
if (total() > HARD_TOTAL_BUDGET_CHARS) {
|
|
185
|
+
diagnostics.push({
|
|
186
|
+
level: "error",
|
|
187
|
+
sectionId: "prompt",
|
|
188
|
+
message: `system prompt is ${total()} chars, over the ${HARD_TOTAL_BUDGET_CHARS} char hard cap even after shrinking every user-owned section.`,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* What pi's `<available_skills>` block will cost, close enough to budget against:
|
|
194
|
+
* name, description and location inside a five-line XML wrapper, plus the preamble.
|
|
195
|
+
*/
|
|
196
|
+
export function estimateSkillsPromptChars(skills) {
|
|
197
|
+
if (!skills || skills.length === 0)
|
|
198
|
+
return 0;
|
|
199
|
+
const SKILL_WRAPPER_CHARS = 90;
|
|
200
|
+
const PREAMBLE_CHARS = 290;
|
|
201
|
+
return (PREAMBLE_CHARS +
|
|
202
|
+
skills.reduce((sum, skill) => sum + skill.name.length + skill.description.length + SKILL_WRAPPER_CHARS, 0));
|
|
203
|
+
}
|
|
204
|
+
export function buildPipiclawSystemPrompt(context, definitions = MAIN_PROMPT_SECTIONS) {
|
|
205
|
+
const diagnostics = [];
|
|
206
|
+
const ids = new Set();
|
|
207
|
+
const orders = new Set();
|
|
208
|
+
for (const definition of [...definitions, FINAL_BOUNDARY_SECTION]) {
|
|
209
|
+
if (ids.has(definition.id))
|
|
210
|
+
throw new Error(`Duplicate prompt section id: ${definition.id}`);
|
|
211
|
+
if (orders.has(definition.order))
|
|
212
|
+
throw new Error(`Duplicate prompt section order: ${definition.order}`);
|
|
213
|
+
ids.add(definition.id);
|
|
214
|
+
orders.add(definition.order);
|
|
215
|
+
}
|
|
216
|
+
const sections = [...definitions]
|
|
217
|
+
.sort((a, b) => a.order - b.order)
|
|
218
|
+
.map((definition) => resolve(definition, context, diagnostics))
|
|
219
|
+
.filter((section) => section !== undefined);
|
|
220
|
+
enforceTotalBudget(sections, diagnostics);
|
|
221
|
+
const footerSection = resolve(FINAL_BOUNDARY_SECTION, context, diagnostics);
|
|
222
|
+
const text = sections.map((section) => section.content).join("\n\n");
|
|
223
|
+
const footer = footerSection?.content ?? "";
|
|
224
|
+
const allSections = footerSection ? [...sections, footerSection] : sections;
|
|
225
|
+
const totalChars = text.length + footer.length;
|
|
226
|
+
if (totalChars > SOFT_TOTAL_BUDGET_CHARS) {
|
|
227
|
+
diagnostics.push({
|
|
228
|
+
level: "warning",
|
|
229
|
+
sectionId: "prompt",
|
|
230
|
+
message: `system prompt is ${totalChars} chars, over the ${SOFT_TOTAL_BUDGET_CHARS} char soft target. Run /context detail.`,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
// Skills sit outside every budget above: pi renders them and owns the same list that
|
|
234
|
+
// backs `/skill:name`, so trimming here would delete commands. Report instead of cut.
|
|
235
|
+
const skillsChars = estimateSkillsPromptChars(context.skills);
|
|
236
|
+
if (skillsChars > SKILLS_BUDGET_CHARS) {
|
|
237
|
+
diagnostics.push({
|
|
238
|
+
level: "warning",
|
|
239
|
+
sectionId: "skills",
|
|
240
|
+
message: `skills catalog is ≈${skillsChars} chars, over the ${SKILLS_BUDGET_CHARS} char budget, and is appended after the sections above (Pipiclaw cannot trim it without dropping /skill:name). Run /context detail, then shorten or remove workspace skill descriptions.`,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
text,
|
|
245
|
+
footer,
|
|
246
|
+
sections: allSections,
|
|
247
|
+
diagnostics,
|
|
248
|
+
totalChars,
|
|
249
|
+
estimatedTokens: estimateTokens(text) + estimateTokens(footer),
|
|
250
|
+
fingerprint: sha256(`v${RUNTIME_PROMPT_VERSION}\n${text}\n${footer}`),
|
|
251
|
+
};
|
|
252
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Final-boundary extension (spec 025 §6.11, §10.6).
|
|
3
|
+
*
|
|
4
|
+
* pi always appends its own tail — skills, current date, cwd — after a custom
|
|
5
|
+
* system prompt, so the last thing the model would otherwise read is
|
|
6
|
+
* user-authored content. This extension runs at `before_agent_start`, the only
|
|
7
|
+
* seam that sees the fully assembled prompt, and appends the runtime boundary
|
|
8
|
+
* footer after it. It is a pure function of the prompt pi built: same input,
|
|
9
|
+
* same bytes, so the provider-side cache prefix is unaffected.
|
|
10
|
+
*
|
|
11
|
+
* The callback also hands the runner the prompt that is actually sent, so
|
|
12
|
+
* `/context` and the debug manifest report the real thing rather than a stale
|
|
13
|
+
* base prompt. Replace this with an SDK renderer seam once pi grows one.
|
|
14
|
+
*/
|
|
15
|
+
import type { ExtensionFactory } from "@earendil-works/pi-coding-agent";
|
|
16
|
+
export interface PromptBoundaryExtensionOptions {
|
|
17
|
+
/** The footer text; empty disables the append (the prompt is still reported). */
|
|
18
|
+
getFooter: () => string;
|
|
19
|
+
/** Receives the exact system prompt handed to the provider for this turn. */
|
|
20
|
+
onFinalPrompt?: (systemPrompt: string) => void;
|
|
21
|
+
}
|
|
22
|
+
export declare function createPromptBoundaryExtension(options: PromptBoundaryExtensionOptions): ExtensionFactory;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Final-boundary extension (spec 025 §6.11, §10.6).
|
|
3
|
+
*
|
|
4
|
+
* pi always appends its own tail — skills, current date, cwd — after a custom
|
|
5
|
+
* system prompt, so the last thing the model would otherwise read is
|
|
6
|
+
* user-authored content. This extension runs at `before_agent_start`, the only
|
|
7
|
+
* seam that sees the fully assembled prompt, and appends the runtime boundary
|
|
8
|
+
* footer after it. It is a pure function of the prompt pi built: same input,
|
|
9
|
+
* same bytes, so the provider-side cache prefix is unaffected.
|
|
10
|
+
*
|
|
11
|
+
* The callback also hands the runner the prompt that is actually sent, so
|
|
12
|
+
* `/context` and the debug manifest report the real thing rather than a stale
|
|
13
|
+
* base prompt. Replace this with an SDK renderer seam once pi grows one.
|
|
14
|
+
*/
|
|
15
|
+
export function createPromptBoundaryExtension(options) {
|
|
16
|
+
return (pi) => {
|
|
17
|
+
pi.on("before_agent_start", (event) => {
|
|
18
|
+
const footer = options.getFooter().trim();
|
|
19
|
+
const systemPrompt = footer ? `${event.systemPrompt.trimEnd()}\n\n${footer}\n` : event.systemPrompt;
|
|
20
|
+
options.onFinalPrompt?.(systemPrompt);
|
|
21
|
+
return { systemPrompt };
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt manifest and the `/context` report (spec 025 §9).
|
|
3
|
+
*
|
|
4
|
+
* Zero LLM cost: everything here is computed from the last build plus counters
|
|
5
|
+
* the runner already keeps. Tool JSON schemas are reported alongside the system
|
|
6
|
+
* prompt because they are billed the same way and are often the larger half.
|
|
7
|
+
*/
|
|
8
|
+
import type { PromptBuildResult } from "./types.js";
|
|
9
|
+
export interface PromptTurnContextStats {
|
|
10
|
+
durableMemoryChars: number;
|
|
11
|
+
taskDigestChars: number;
|
|
12
|
+
recalledMemoryChars: number;
|
|
13
|
+
userMessageChars: number;
|
|
14
|
+
}
|
|
15
|
+
export interface PromptContextReportInput {
|
|
16
|
+
build: PromptBuildResult;
|
|
17
|
+
/** What the provider actually received: our prompt + pi's skills/date/cwd tail + the boundary footer. */
|
|
18
|
+
finalPrompt?: string;
|
|
19
|
+
skills: Array<{
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
}>;
|
|
23
|
+
toolNames: string[];
|
|
24
|
+
toolSchemaChars: number;
|
|
25
|
+
lastTurn?: PromptTurnContextStats;
|
|
26
|
+
detail: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface PromptSectionManifestEntry {
|
|
29
|
+
id: string;
|
|
30
|
+
order: number;
|
|
31
|
+
source: string;
|
|
32
|
+
authority: string;
|
|
33
|
+
cacheClass: string;
|
|
34
|
+
rawChars: number;
|
|
35
|
+
injectedChars: number;
|
|
36
|
+
truncated: boolean;
|
|
37
|
+
sha256: string;
|
|
38
|
+
}
|
|
39
|
+
export interface PromptManifest {
|
|
40
|
+
fingerprint: string;
|
|
41
|
+
totalChars: number;
|
|
42
|
+
estimatedTokens: number;
|
|
43
|
+
sections: PromptSectionManifestEntry[];
|
|
44
|
+
diagnostics: PromptBuildResult["diagnostics"];
|
|
45
|
+
/** Present when the fully assembled prompt (including pi's tail) was captured. */
|
|
46
|
+
finalPromptSha256?: string;
|
|
47
|
+
finalPromptChars?: number;
|
|
48
|
+
}
|
|
49
|
+
/** Section metadata without any section body — safe to write next to a channel. */
|
|
50
|
+
export declare function buildPromptManifest(build: PromptBuildResult, finalPrompt?: string): PromptManifest;
|
|
51
|
+
export declare function renderContextReport(input: PromptContextReportInput): string;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt manifest and the `/context` report (spec 025 §9).
|
|
3
|
+
*
|
|
4
|
+
* Zero LLM cost: everything here is computed from the last build plus counters
|
|
5
|
+
* the runner already keeps. Tool JSON schemas are reported alongside the system
|
|
6
|
+
* prompt because they are billed the same way and are often the larger half.
|
|
7
|
+
*/
|
|
8
|
+
import { estimateSkillsPromptChars, estimateTokens, sha256 } from "./builder.js";
|
|
9
|
+
/** Section metadata without any section body — safe to write next to a channel. */
|
|
10
|
+
export function buildPromptManifest(build, finalPrompt) {
|
|
11
|
+
return {
|
|
12
|
+
fingerprint: build.fingerprint,
|
|
13
|
+
totalChars: build.totalChars,
|
|
14
|
+
estimatedTokens: build.estimatedTokens,
|
|
15
|
+
sections: build.sections.map(({ content: _content, ...entry }) => entry),
|
|
16
|
+
diagnostics: build.diagnostics,
|
|
17
|
+
finalPromptSha256: finalPrompt ? sha256(finalPrompt) : undefined,
|
|
18
|
+
finalPromptChars: finalPrompt?.length,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function formatNumber(value) {
|
|
22
|
+
return value.toLocaleString("en-US");
|
|
23
|
+
}
|
|
24
|
+
function pad(text, width) {
|
|
25
|
+
return text.length >= width ? text : text + " ".repeat(width - text.length);
|
|
26
|
+
}
|
|
27
|
+
export function renderContextReport(input) {
|
|
28
|
+
const { build, finalPrompt, detail } = input;
|
|
29
|
+
const lines = ["# Context Report", ""];
|
|
30
|
+
lines.push(`System prompt (Pipiclaw-owned): ${formatNumber(build.totalChars)} chars, ~${formatNumber(build.estimatedTokens)} tokens`);
|
|
31
|
+
if (finalPrompt) {
|
|
32
|
+
// The string the provider actually caches: our sections + pi's skills/date/cwd tail +
|
|
33
|
+
// the boundary footer. It is the fingerprint that matters for cache hits — the build
|
|
34
|
+
// fingerprint below only covers the part Pipiclaw owns, and stays stable across a
|
|
35
|
+
// date rollover that does invalidate the provider's cached prefix.
|
|
36
|
+
lines.push(`Sent to the model (incl. skills, date, cwd): ${formatNumber(finalPrompt.length)} chars, ~${formatNumber(estimateTokens(finalPrompt))} tokens, sha256:${sha256(finalPrompt).slice(0, 16)}`);
|
|
37
|
+
}
|
|
38
|
+
lines.push(`Fingerprint (Pipiclaw sections): sha256:${build.fingerprint.slice(0, 16)}`);
|
|
39
|
+
lines.push("");
|
|
40
|
+
const width = Math.max(...build.sections.map((section) => section.id.length), 12) + 2;
|
|
41
|
+
for (const section of build.sections) {
|
|
42
|
+
const flags = [section.cacheClass];
|
|
43
|
+
if (section.truncated)
|
|
44
|
+
flags.push(`truncated from ${formatNumber(section.rawChars)}`);
|
|
45
|
+
lines.push(`- ${pad(section.id, width)}${formatNumber(section.injectedChars).padStart(7)} chars ${flags.join(", ")}`);
|
|
46
|
+
}
|
|
47
|
+
lines.push("");
|
|
48
|
+
lines.push(`Tools: ${input.toolNames.length} registered; JSON schemas ≈ ${formatNumber(input.toolSchemaChars)} chars (billed on top of the system prompt)`);
|
|
49
|
+
lines.push(`Skills: ${input.skills.length} visible, ≈${formatNumber(estimateSkillsPromptChars(input.skills))} chars (rendered by pi after the sections above, outside the section budgets)`);
|
|
50
|
+
if (input.lastTurn) {
|
|
51
|
+
const turn = input.lastTurn;
|
|
52
|
+
lines.push("");
|
|
53
|
+
lines.push("Last turn context (per-turn, not cached):");
|
|
54
|
+
lines.push(`- durable memory bootstrap: ${formatNumber(turn.durableMemoryChars)} chars`);
|
|
55
|
+
lines.push(`- recalled memory: ${formatNumber(turn.recalledMemoryChars)} chars`);
|
|
56
|
+
lines.push(`- task digest: ${formatNumber(turn.taskDigestChars)} chars`);
|
|
57
|
+
lines.push(`- user message: ${formatNumber(turn.userMessageChars)} chars`);
|
|
58
|
+
}
|
|
59
|
+
if (build.diagnostics.length > 0) {
|
|
60
|
+
lines.push("");
|
|
61
|
+
lines.push("Diagnostics:");
|
|
62
|
+
for (const diagnostic of build.diagnostics) {
|
|
63
|
+
lines.push(`- [${diagnostic.level}] ${diagnostic.sectionId}: ${diagnostic.message}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (detail) {
|
|
67
|
+
lines.push("");
|
|
68
|
+
lines.push("Detail:");
|
|
69
|
+
for (const section of build.sections) {
|
|
70
|
+
lines.push(`- ${section.id} (order ${section.order}, ${section.authority}) ← ${section.source} · sha256:${section.sha256.slice(0, 12)}`);
|
|
71
|
+
}
|
|
72
|
+
if (input.toolNames.length > 0) {
|
|
73
|
+
lines.push(`- tools: ${input.toolNames.join(", ")}`);
|
|
74
|
+
}
|
|
75
|
+
if (input.skills.length > 0) {
|
|
76
|
+
lines.push(`- skills: ${input.skills.map((skill) => skill.name).join(", ")}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
lines.push("");
|
|
81
|
+
lines.push("Run `/context detail` for per-section sources, hashes, and the tool/skill inventory.");
|
|
82
|
+
}
|
|
83
|
+
return lines.join("\n");
|
|
84
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspace resources that enter the system prompt: SOUL.md and AGENTS.md.
|
|
3
|
+
*
|
|
4
|
+
* Pipiclaw reads and wraps them itself (pi's own `agentsFiles` path is disabled)
|
|
5
|
+
* so that their order, budget and framing are ours. Budgets are applied to the
|
|
6
|
+
* file body here — not to the rendered section — so a truncated file can never
|
|
7
|
+
* cut its own closing tag.
|
|
8
|
+
*/
|
|
9
|
+
import type { LoadedPromptResource, PromptDiagnostic } from "./types.js";
|
|
10
|
+
export declare const SOUL_BUDGET_CHARS = 5000;
|
|
11
|
+
export declare const AGENTS_BUDGET_CHARS = 8000;
|
|
12
|
+
export interface WorkspacePromptResources {
|
|
13
|
+
soul?: LoadedPromptResource;
|
|
14
|
+
agents?: LoadedPromptResource;
|
|
15
|
+
diagnostics: PromptDiagnostic[];
|
|
16
|
+
}
|
|
17
|
+
/** Read SOUL.md and AGENTS.md from the workspace root, budgeted and template-aware. */
|
|
18
|
+
export declare function loadWorkspacePromptResources(workspaceDir: string): WorkspacePromptResources;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspace resources that enter the system prompt: SOUL.md and AGENTS.md.
|
|
3
|
+
*
|
|
4
|
+
* Pipiclaw reads and wraps them itself (pi's own `agentsFiles` path is disabled)
|
|
5
|
+
* so that their order, budget and framing are ours. Budgets are applied to the
|
|
6
|
+
* file body here — not to the rendered section — so a truncated file can never
|
|
7
|
+
* cut its own closing tag.
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import * as log from "../../log.js";
|
|
12
|
+
import { DEFAULT_AGENTS, DEFAULT_SOUL, isDefaultWorkspaceTemplate } from "../../runtime/workspace-templates.js";
|
|
13
|
+
import { clipText } from "../../shared/text-utils.js";
|
|
14
|
+
export const SOUL_BUDGET_CHARS = 5_000;
|
|
15
|
+
export const AGENTS_BUDGET_CHARS = 8_000;
|
|
16
|
+
function readWorkspaceFile(path) {
|
|
17
|
+
if (!existsSync(path)) {
|
|
18
|
+
return "";
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
return readFileSync(path, "utf-8").replace(/\r/g, "").trim();
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
log.logWarning(`Failed to read ${path}`, String(error));
|
|
25
|
+
return "";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function loadResource(path, template, budget, sectionId, overflowAdvice, diagnostics) {
|
|
29
|
+
const content = readWorkspaceFile(path);
|
|
30
|
+
if (!content) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
if (isDefaultWorkspaceTemplate(content, template)) {
|
|
34
|
+
diagnostics.push({
|
|
35
|
+
level: "info",
|
|
36
|
+
sectionId,
|
|
37
|
+
message: `${path} still holds the bootstrap template; not injected.`,
|
|
38
|
+
});
|
|
39
|
+
return { path, content, isDefaultTemplate: true };
|
|
40
|
+
}
|
|
41
|
+
if (content.length <= budget) {
|
|
42
|
+
return { path, content, isDefaultTemplate: false };
|
|
43
|
+
}
|
|
44
|
+
diagnostics.push({
|
|
45
|
+
level: "warning",
|
|
46
|
+
sectionId,
|
|
47
|
+
message: `${path} is ${content.length} chars; injected ${budget}. ${overflowAdvice}`,
|
|
48
|
+
});
|
|
49
|
+
const clipped = clipText(content, budget, {
|
|
50
|
+
headRatio: 0.6,
|
|
51
|
+
omitHint: `\n\n[... truncated: injected ${budget} of ${content.length} characters. ${overflowAdvice} ...]\n\n`,
|
|
52
|
+
});
|
|
53
|
+
return { path, content: clipped, isDefaultTemplate: false };
|
|
54
|
+
}
|
|
55
|
+
/** Read SOUL.md and AGENTS.md from the workspace root, budgeted and template-aware. */
|
|
56
|
+
export function loadWorkspacePromptResources(workspaceDir) {
|
|
57
|
+
const diagnostics = [];
|
|
58
|
+
const soul = loadResource(join(workspaceDir, "SOUL.md"), DEFAULT_SOUL, SOUL_BUDGET_CHARS, "workspace.soul", "Run /context detail, and keep SOUL.md to identity and voice.", diagnostics);
|
|
59
|
+
const agents = loadResource(join(workspaceDir, "AGENTS.md"), DEFAULT_AGENTS, AGENTS_BUDGET_CHARS, "workspace.agents", "Run /context detail, and move task-specific procedures into workspace skills so they load on demand.", diagnostics);
|
|
60
|
+
return { soul, agents, diagnostics };
|
|
61
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Section definitions for the main agent's system prompt (spec 025).
|
|
3
|
+
*
|
|
4
|
+
* Order ranges are reserved so a section can be added inside a domain without
|
|
5
|
+
* renumbering the rest, and so a later model-family overlay has room:
|
|
6
|
+
*
|
|
7
|
+
* 100–199 identity & interaction
|
|
8
|
+
* 200–299 execution contract
|
|
9
|
+
* 300–399 runtime hard invariants
|
|
10
|
+
* 400–499 tools
|
|
11
|
+
* 500–599 playbook / sub-agent catalogs
|
|
12
|
+
* 600–699 workspace instructions (SOUL, AGENTS)
|
|
13
|
+
* 700–799 skills (rendered by pi, not by us — see resources.ts)
|
|
14
|
+
* 800–899 final boundary (appended after pi's tail — see extension.ts)
|
|
15
|
+
* 900–999 environment / overlays (pi appends date + cwd)
|
|
16
|
+
*
|
|
17
|
+
* Content rules (spec 025 §4.6): a line stays here only if missing it once can
|
|
18
|
+
* break safety, durable state or an external side effect; or it applies to
|
|
19
|
+
* nearly every turn; or it is a progressive-disclosure entry point. Procedure,
|
|
20
|
+
* examples and recovery live in the playbooks.
|
|
21
|
+
*/
|
|
22
|
+
import type { PromptSectionDefinition } from "./types.js";
|
|
23
|
+
export declare const IDENTITY_SECTION: PromptSectionDefinition;
|
|
24
|
+
export declare const EXECUTION_SECTION: PromptSectionDefinition;
|
|
25
|
+
export declare const INVARIANTS_SECTION: PromptSectionDefinition;
|
|
26
|
+
export declare const TASK_CORE_SECTION: PromptSectionDefinition;
|
|
27
|
+
export declare const TOOLS_SECTION: PromptSectionDefinition;
|
|
28
|
+
export declare const PLAYBOOKS_SECTION: PromptSectionDefinition;
|
|
29
|
+
export declare const SUBAGENTS_SECTION: PromptSectionDefinition;
|
|
30
|
+
export declare const SOUL_SECTION: PromptSectionDefinition;
|
|
31
|
+
export declare const AGENTS_SECTION: PromptSectionDefinition;
|
|
32
|
+
/**
|
|
33
|
+
* Injected *after* pi's tail (skills, date, cwd) by the prompt extension, so the
|
|
34
|
+
* last thing the model reads is the runtime boundary rather than user-authored
|
|
35
|
+
* content. It restates the boundary only — enforcement lives in the guards.
|
|
36
|
+
*/
|
|
37
|
+
export declare const FINAL_BOUNDARY_SECTION: PromptSectionDefinition;
|
|
38
|
+
/** The prompt body Pipiclaw owns, in cognitive order. The footer is rendered separately. */
|
|
39
|
+
export declare const MAIN_PROMPT_SECTIONS: PromptSectionDefinition[];
|