notesoup 0.0.0-stage → 0.40.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/LICENSE +21 -0
- package/README.md +94 -2
- package/dist/src/boot/render.d.ts +16 -0
- package/dist/src/boot/render.js +95 -0
- package/dist/src/boot/snapshot.d.ts +32 -0
- package/dist/src/boot/snapshot.js +36 -0
- package/dist/src/boot/text.d.ts +17 -0
- package/dist/src/boot/text.js +48 -0
- package/dist/src/budget/constants.d.ts +9 -0
- package/dist/src/budget/constants.js +9 -0
- package/dist/src/budget/policy.d.ts +19 -0
- package/dist/src/budget/policy.js +36 -0
- package/dist/src/budget/text.d.ts +8 -0
- package/dist/src/budget/text.js +10 -0
- package/dist/src/claude/helper.d.ts +15 -0
- package/dist/src/claude/helper.js +87 -0
- package/dist/src/dream/doctor.d.ts +2 -0
- package/dist/src/dream/doctor.js +160 -0
- package/dist/src/dream/gates.d.ts +10 -0
- package/dist/src/dream/gates.js +26 -0
- package/dist/src/dream/git.d.ts +21 -0
- package/dist/src/dream/git.js +71 -0
- package/dist/src/dream/jail.d.ts +11 -0
- package/dist/src/dream/jail.js +71 -0
- package/dist/src/dream/lock.d.ts +31 -0
- package/dist/src/dream/lock.js +99 -0
- package/dist/src/dream/playbook.d.ts +1 -0
- package/dist/src/dream/playbook.js +2 -0
- package/dist/src/dream/report.d.ts +7 -0
- package/dist/src/dream/report.js +43 -0
- package/dist/src/dream/result.d.ts +9 -0
- package/dist/src/dream/result.js +1 -0
- package/dist/src/dream/settings.d.ts +10 -0
- package/dist/src/dream/settings.js +14 -0
- package/dist/src/history/history.d.ts +156 -0
- package/dist/src/history/history.js +212 -0
- package/dist/src/history/query.d.ts +63 -0
- package/dist/src/history/query.js +201 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/notes/address.d.ts +31 -0
- package/dist/src/notes/address.js +123 -0
- package/dist/src/notes/constants.d.ts +3 -0
- package/dist/src/notes/constants.js +3 -0
- package/dist/src/notes/errors.d.ts +11 -0
- package/dist/src/notes/errors.js +13 -0
- package/dist/src/notes/frontmatter.d.ts +44 -0
- package/dist/src/notes/frontmatter.js +145 -0
- package/dist/src/notes/fs-error.d.ts +2 -0
- package/dist/src/notes/fs-error.js +5 -0
- package/dist/src/notes/identity.d.ts +10 -0
- package/dist/src/notes/identity.js +33 -0
- package/dist/src/notes/index.d.ts +5 -0
- package/dist/src/notes/index.js +3 -0
- package/dist/src/notes/paths.d.ts +21 -0
- package/dist/src/notes/paths.js +113 -0
- package/dist/src/notes/store.d.ts +103 -0
- package/dist/src/notes/store.js +474 -0
- package/dist/src/pi/boot.d.ts +24 -0
- package/dist/src/pi/boot.js +56 -0
- package/dist/src/pi/budget.d.ts +13 -0
- package/dist/src/pi/budget.js +178 -0
- package/dist/src/pi/dream/cli.d.ts +10 -0
- package/dist/src/pi/dream/cli.js +140 -0
- package/dist/src/pi/dream/runner.d.ts +27 -0
- package/dist/src/pi/dream/runner.js +90 -0
- package/dist/src/pi/dream/settings.d.ts +7 -0
- package/dist/src/pi/dream/settings.js +17 -0
- package/dist/src/pi/entries.d.ts +7 -0
- package/dist/src/pi/entries.js +7 -0
- package/dist/src/pi/estimate.d.ts +16 -0
- package/dist/src/pi/estimate.js +99 -0
- package/dist/src/pi/extension.d.ts +10 -0
- package/dist/src/pi/extension.js +24 -0
- package/dist/src/pi/history-tools.d.ts +3 -0
- package/dist/src/pi/history-tools.js +9 -0
- package/dist/src/pi/history.d.ts +4 -0
- package/dist/src/pi/history.js +150 -0
- package/dist/src/pi/notes/adapter.d.ts +10 -0
- package/dist/src/pi/notes/adapter.js +27 -0
- package/dist/src/pi/notes/tools.d.ts +3 -0
- package/dist/src/pi/notes/tools.js +15 -0
- package/dist/src/pi/reset/artifacts.d.ts +24 -0
- package/dist/src/pi/reset/artifacts.js +84 -0
- package/dist/src/pi/reset/committed.d.ts +7 -0
- package/dist/src/pi/reset/committed.js +28 -0
- package/dist/src/pi/reset/lifecycle.d.ts +101 -0
- package/dist/src/pi/reset/lifecycle.js +277 -0
- package/dist/src/pi/reset/text.d.ts +5 -0
- package/dist/src/pi/reset/text.js +6 -0
- package/dist/src/pi/runtime.d.ts +3 -0
- package/dist/src/pi/runtime.js +206 -0
- package/dist/src/pi/session-reader.d.ts +5 -0
- package/dist/src/pi/session-reader.js +1 -0
- package/dist/src/pi/thresholds.d.ts +17 -0
- package/dist/src/pi/thresholds.js +33 -0
- package/dist/src/pi/tool-names.d.ts +7 -0
- package/dist/src/pi/tool-names.js +7 -0
- package/dist/src/pi/tool-result.d.ts +15 -0
- package/dist/src/pi/tool-result.js +29 -0
- package/dist/src/pi/window.d.ts +41 -0
- package/dist/src/pi/window.js +127 -0
- package/dist/src/settings.d.ts +9 -0
- package/dist/src/settings.js +18 -0
- package/dist/src/text-match.d.ts +5 -0
- package/dist/src/text-match.js +16 -0
- package/dist/src/tools/history.d.ts +58 -0
- package/dist/src/tools/history.js +210 -0
- package/dist/src/tools/notes.d.ts +250 -0
- package/dist/src/tools/notes.js +360 -0
- package/dist/src/tools/output.d.ts +23 -0
- package/dist/src/tools/output.js +39 -0
- package/dist/src/tools/result.d.ts +82 -0
- package/dist/src/tools/result.js +104 -0
- package/dist/src/tools/schema.d.ts +18 -0
- package/dist/src/tools/schema.js +31 -0
- package/dist/test/agent-loop.test.d.ts +1 -0
- package/dist/test/agent-loop.test.js +1497 -0
- package/dist/test/boot.integration.test.d.ts +1 -0
- package/dist/test/boot.integration.test.js +294 -0
- package/dist/test/budget-settings.integration.test.d.ts +1 -0
- package/dist/test/budget-settings.integration.test.js +144 -0
- package/dist/test/claude-mod.test.d.ts +1 -0
- package/dist/test/claude-mod.test.js +404 -0
- package/dist/test/dependencies.test.d.ts +1 -0
- package/dist/test/dependencies.test.js +101 -0
- package/dist/test/doctor.test.d.ts +1 -0
- package/dist/test/doctor.test.js +37 -0
- package/dist/test/dream-skill.test.d.ts +1 -0
- package/dist/test/dream-skill.test.js +72 -0
- package/dist/test/dream.test.d.ts +1 -0
- package/dist/test/dream.test.js +136 -0
- package/dist/test/helpers/dependency-graph.d.ts +5 -0
- package/dist/test/helpers/dependency-graph.js +73 -0
- package/dist/test/helpers/extension-test-environment.d.ts +1 -0
- package/dist/test/helpers/extension-test-environment.js +9 -0
- package/dist/test/helpers/extension.d.ts +119 -0
- package/dist/test/helpers/extension.js +367 -0
- package/dist/test/helpers/notes.d.ts +6 -0
- package/dist/test/helpers/notes.js +13 -0
- package/dist/test/history.integration.test.d.ts +1 -0
- package/dist/test/history.integration.test.js +633 -0
- package/dist/test/host-neutral.test.d.ts +1 -0
- package/dist/test/host-neutral.test.js +274 -0
- package/dist/test/notes-library.test.d.ts +1 -0
- package/dist/test/notes-library.test.js +214 -0
- package/dist/test/notes.integration.test.d.ts +1 -0
- package/dist/test/notes.integration.test.js +515 -0
- package/dist/test/notes.test.d.ts +1 -0
- package/dist/test/notes.test.js +530 -0
- package/dist/test/reset-lifecycle.test.d.ts +1 -0
- package/dist/test/reset-lifecycle.test.js +605 -0
- package/docs/architecture.md +63 -0
- package/docs/claude-code-mod.md +9 -0
- package/docs/notes-library.md +74 -0
- package/docs/reset-lifecycle.md +75 -0
- package/docs/sdk-integration.md +39 -0
- package/docs/skills.md +30 -0
- package/docs/tool-results.md +78 -0
- package/mods/notesoup/.claude-plugin/plugin.json +8 -0
- package/mods/notesoup/dist/claude/helper.js +9265 -0
- package/mods/notesoup/hooks/hooks.json +4 -0
- package/mods/notesoup/hooks/register.ts +240 -0
- package/mods/notesoup/playbook.md +34 -0
- package/mods/notesoup/skills/dream/SKILL.md +12 -0
- package/mods/pi-context/dist/claude/helper.js +9324 -0
- package/package.json +89 -5
- package/playbook.md +34 -0
- package/skills/dream/SKILL.md +12 -0
- package/skills/memory/SKILL.md +59 -0
- package/src/boot/render.ts +115 -0
- package/src/boot/snapshot.ts +46 -0
- package/src/boot/text.ts +58 -0
- package/src/budget/constants.ts +9 -0
- package/src/budget/policy.ts +36 -0
- package/src/budget/text.ts +11 -0
- package/src/claude/helper.ts +94 -0
- package/src/dream/doctor.ts +117 -0
- package/src/dream/gates.ts +26 -0
- package/src/dream/git.ts +73 -0
- package/src/dream/jail.ts +62 -0
- package/src/dream/lock.ts +82 -0
- package/src/dream/playbook.ts +3 -0
- package/src/dream/report.ts +36 -0
- package/src/dream/result.ts +2 -0
- package/src/dream/settings.ts +16 -0
- package/src/history/history.ts +286 -0
- package/src/history/query.ts +222 -0
- package/src/index.ts +1 -0
- package/src/notes/address.ts +110 -0
- package/src/notes/constants.ts +3 -0
- package/src/notes/errors.ts +15 -0
- package/src/notes/frontmatter.ts +166 -0
- package/src/notes/fs-error.ts +5 -0
- package/src/notes/identity.ts +40 -0
- package/src/notes/index.ts +24 -0
- package/src/notes/paths.ts +109 -0
- package/src/notes/store.ts +494 -0
- package/src/pi/boot.ts +88 -0
- package/src/pi/budget.ts +178 -0
- package/src/pi/dream/cli.ts +92 -0
- package/src/pi/dream/runner.ts +91 -0
- package/src/pi/dream/settings.ts +17 -0
- package/src/pi/entries.ts +7 -0
- package/src/pi/estimate.ts +116 -0
- package/src/pi/extension.ts +27 -0
- package/src/pi/history-tools.ts +11 -0
- package/src/pi/history.ts +162 -0
- package/src/pi/notes/adapter.ts +33 -0
- package/src/pi/notes/tools.ts +18 -0
- package/src/pi/reset/artifacts.ts +100 -0
- package/src/pi/reset/committed.ts +26 -0
- package/src/pi/reset/lifecycle.ts +342 -0
- package/src/pi/reset/text.ts +10 -0
- package/src/pi/runtime.ts +200 -0
- package/src/pi/session-reader.ts +6 -0
- package/src/pi/thresholds.ts +50 -0
- package/src/pi/tool-names.ts +9 -0
- package/src/pi/tool-result.ts +41 -0
- package/src/pi/window.ts +133 -0
- package/src/settings.ts +22 -0
- package/src/text-match.ts +14 -0
- package/src/tools/history.ts +234 -0
- package/src/tools/notes.ts +381 -0
- package/src/tools/output.ts +41 -0
- package/src/tools/result.ts +144 -0
- package/src/tools/schema.ts +31 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import { notesIdentityFromPi } from "../../src/pi/notes/adapter.js";
|
|
2
|
+
import { PI_TOOL_NAMES } from "../../src/pi/tool-names.js";
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
|
+
import { SessionManager, SettingsManager, } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import notesoup, { createNotesoup } from "../../src/pi/extension.js";
|
|
9
|
+
import { loadNotesSnapshot } from "../../src/boot/snapshot.js";
|
|
10
|
+
import { renderBootBlock } from "../../src/boot/render.js";
|
|
11
|
+
import { agentSlug, modelSlug } from "../../src/pi/notes/adapter.js";
|
|
12
|
+
import { rootWindowId } from "../../src/pi/window.js";
|
|
13
|
+
import { TOOL_OUTPUT_MAX_BYTES } from "../../src/tools/output.js";
|
|
14
|
+
import { structuredBytes } from "../../src/tools/result.js";
|
|
15
|
+
let defaultCwd = "/private/tmp/notesoup-test-cwd";
|
|
16
|
+
let registerTempPath;
|
|
17
|
+
/**
|
|
18
|
+
* Install the process-level settings and notes roots needed by one test file.
|
|
19
|
+
* The caller owns the lifecycle hooks; this helper never registers tests or hooks.
|
|
20
|
+
*/
|
|
21
|
+
export function installExtensionTestEnvironment(prefix = "notesoup-test") {
|
|
22
|
+
const previousAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
23
|
+
const previousNotesHome = process.env.PI_NOTES_HOME;
|
|
24
|
+
const defaultAgentDir = mkdtempSync(join(tmpdir(), `${prefix}-agent-`));
|
|
25
|
+
const cwd = mkdtempSync(join(tmpdir(), `${prefix}-cwd-`));
|
|
26
|
+
defaultCwd = cwd;
|
|
27
|
+
const notesRoots = new Set();
|
|
28
|
+
const tempPaths = new Set();
|
|
29
|
+
registerTempPath = (path) => { tempPaths.add(path); };
|
|
30
|
+
const newNotesRoot = () => {
|
|
31
|
+
const root = mkdtempSync(join(tmpdir(), `${prefix}-notes-`));
|
|
32
|
+
notesRoots.add(root);
|
|
33
|
+
process.env.PI_NOTES_HOME = root;
|
|
34
|
+
return root;
|
|
35
|
+
};
|
|
36
|
+
const beforeEach = () => {
|
|
37
|
+
process.env.PI_CODING_AGENT_DIR = defaultAgentDir;
|
|
38
|
+
newNotesRoot();
|
|
39
|
+
};
|
|
40
|
+
const afterEach = () => {
|
|
41
|
+
process.env.PI_CODING_AGENT_DIR = defaultAgentDir;
|
|
42
|
+
for (const root of notesRoots)
|
|
43
|
+
rmSync(root, { recursive: true, force: true });
|
|
44
|
+
notesRoots.clear();
|
|
45
|
+
for (const path of tempPaths)
|
|
46
|
+
rmSync(path, { recursive: true, force: true });
|
|
47
|
+
tempPaths.clear();
|
|
48
|
+
if (previousNotesHome === undefined)
|
|
49
|
+
delete process.env.PI_NOTES_HOME;
|
|
50
|
+
else
|
|
51
|
+
process.env.PI_NOTES_HOME = previousNotesHome;
|
|
52
|
+
};
|
|
53
|
+
const dispose = () => {
|
|
54
|
+
afterEach();
|
|
55
|
+
for (const root of notesRoots)
|
|
56
|
+
rmSync(root, { recursive: true, force: true });
|
|
57
|
+
rmSync(defaultAgentDir, { recursive: true, force: true });
|
|
58
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
59
|
+
registerTempPath = undefined;
|
|
60
|
+
if (previousAgentDir === undefined)
|
|
61
|
+
delete process.env.PI_CODING_AGENT_DIR;
|
|
62
|
+
else
|
|
63
|
+
process.env.PI_CODING_AGENT_DIR = previousAgentDir;
|
|
64
|
+
if (previousNotesHome === undefined)
|
|
65
|
+
delete process.env.PI_NOTES_HOME;
|
|
66
|
+
else
|
|
67
|
+
process.env.PI_NOTES_HOME = previousNotesHome;
|
|
68
|
+
};
|
|
69
|
+
return { cwd, agentDir: defaultAgentDir, beforeEach, afterEach, newNotesRoot, dispose };
|
|
70
|
+
}
|
|
71
|
+
export function writeJson(path, value) {
|
|
72
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
73
|
+
writeFileSync(path, JSON.stringify(value, null, 2));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Materialize global (agentDir/settings.json) and project (cwd/.pi/settings.json)
|
|
77
|
+
* settings in temp directories, then read them back through the same public
|
|
78
|
+
* SettingsManager.create the extension uses. Never touches the real ~/.pi.
|
|
79
|
+
*/
|
|
80
|
+
export function settingsFixture(options = {}) {
|
|
81
|
+
const cwd = mkdtempSync(join(tmpdir(), "notesoup-cwd-"));
|
|
82
|
+
const agentDir = mkdtempSync(join(tmpdir(), "notesoup-agent-"));
|
|
83
|
+
const global = { ...(options.global ?? {}) };
|
|
84
|
+
if (options.reserveTokens !== undefined)
|
|
85
|
+
global.compaction = { reserveTokens: options.reserveTokens };
|
|
86
|
+
writeJson(join(agentDir, "settings.json"), global);
|
|
87
|
+
if (options.project)
|
|
88
|
+
writeJson(join(cwd, ".pi", "settings.json"), options.project);
|
|
89
|
+
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
90
|
+
registerTempPath?.(cwd);
|
|
91
|
+
registerTempPath?.(agentDir);
|
|
92
|
+
const fixtureManager = SettingsManager.create(cwd, agentDir, { projectTrusted: true });
|
|
93
|
+
const projectReserve = options.project?.compaction?.reserveTokens;
|
|
94
|
+
assert.equal(fixtureManager.getCompactionSettings().reserveTokens, projectReserve ?? options.reserveTokens ?? 16_384, "fixture reserve reads back");
|
|
95
|
+
return { cwd, agentDir };
|
|
96
|
+
}
|
|
97
|
+
/** TypeBox's TSchema does not expose `type`/`required` statically; read them structurally. */
|
|
98
|
+
export function objectSchema(tool) {
|
|
99
|
+
return tool?.parameters;
|
|
100
|
+
}
|
|
101
|
+
export function manager(persisted = false) {
|
|
102
|
+
if (!persisted)
|
|
103
|
+
return SessionManager.inMemory("/private/tmp/notesoup-test");
|
|
104
|
+
const dir = mkdtempSync(join(tmpdir(), "notesoup-session-"));
|
|
105
|
+
return SessionManager.create("/private/tmp/notesoup-test", dir);
|
|
106
|
+
}
|
|
107
|
+
export function makeExtension(sessionManager, settingsManager) {
|
|
108
|
+
const captured = { tools: new Map(), handlers: new Map(), commands: new Map(), sent: [], contextMessages: [], flags: [] };
|
|
109
|
+
const api = {
|
|
110
|
+
registerFlag(name) {
|
|
111
|
+
captured.flags.push(name);
|
|
112
|
+
},
|
|
113
|
+
registerTool(tool) {
|
|
114
|
+
captured.tools.set(tool.name, tool);
|
|
115
|
+
},
|
|
116
|
+
registerCommand(name, options) {
|
|
117
|
+
captured.commands.set(name, options);
|
|
118
|
+
},
|
|
119
|
+
on(name, handler) {
|
|
120
|
+
const handlers = captured.handlers.get(name) ?? [];
|
|
121
|
+
handlers.push(handler);
|
|
122
|
+
captured.handlers.set(name, handlers);
|
|
123
|
+
},
|
|
124
|
+
appendEntry(customType, data) {
|
|
125
|
+
sessionManager.appendCustomEntry(customType, data);
|
|
126
|
+
},
|
|
127
|
+
sendMessage(message, options) {
|
|
128
|
+
captured.sent.push({ message, options });
|
|
129
|
+
sessionManager.appendCustomMessageEntry(message.customType, message.content, message.display, message.details);
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
// The harness implements only the ExtensionAPI members this extension uses.
|
|
133
|
+
(settingsManager ? createNotesoup({ settingsManager }) : notesoup)(api);
|
|
134
|
+
return captured;
|
|
135
|
+
}
|
|
136
|
+
export async function explicitBoot(ctx, currentWindowId, previousWindowId) {
|
|
137
|
+
return renderBootBlock({
|
|
138
|
+
tools: PI_TOOL_NAMES,
|
|
139
|
+
agentName: agentSlug(ctx),
|
|
140
|
+
modelName: modelSlug(ctx),
|
|
141
|
+
firstWindowId: rootWindowId(ctx.sessionManager.getSessionId()),
|
|
142
|
+
currentWindowId,
|
|
143
|
+
previousWindowId,
|
|
144
|
+
notes: await loadNotesSnapshot(notesIdentityFromPi(ctx), Date.now()),
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
export function context(sessionManager, compact, usage, idle = true, cwd = defaultCwd, projectTrusted = true, model) {
|
|
148
|
+
const notices = [];
|
|
149
|
+
const compactionRequests = [];
|
|
150
|
+
const fake = {
|
|
151
|
+
sessionManager,
|
|
152
|
+
model: typeof model === "string" ? { id: model } : model,
|
|
153
|
+
getContextUsage: () => usage,
|
|
154
|
+
compact: (options) => { compactionRequests.push(options); compact?.(options); },
|
|
155
|
+
isIdle: () => idle,
|
|
156
|
+
hasPendingMessages: () => false,
|
|
157
|
+
cwd,
|
|
158
|
+
isProjectTrusted: () => projectTrusted,
|
|
159
|
+
ui: { notify: (message, type) => notices.push({ message, type }) },
|
|
160
|
+
};
|
|
161
|
+
// Only the members the extension reads; the rest of the ExtensionContext surface is unused.
|
|
162
|
+
return Object.assign(fake, { notices, compactionRequests });
|
|
163
|
+
}
|
|
164
|
+
export function noticesOf(ctx) {
|
|
165
|
+
return ctx.notices;
|
|
166
|
+
}
|
|
167
|
+
export function sentOf(captured, customType) {
|
|
168
|
+
return captured.sent.filter((sent) => sent.message.customType === customType);
|
|
169
|
+
}
|
|
170
|
+
export async function call(captured, name, params, ctx) {
|
|
171
|
+
const tool = captured.tools.get(name);
|
|
172
|
+
assert.ok(tool, `registered ${name}`);
|
|
173
|
+
return tool.execute("call-1", params, new AbortController().signal, () => { }, toolContext(ctx));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* The context Pi hands a registered tool: the session context plus the nested-call surface
|
|
177
|
+
* (ExtensionToolContext). The registered notes/history tools never call another tool, so `tools`
|
|
178
|
+
* is empty and `executeTool` refuses instead of pretending a runtime exists.
|
|
179
|
+
*/
|
|
180
|
+
export function toolContext(ctx, callable = []) {
|
|
181
|
+
// The prototype keeps whatever the caller handed in reachable, including session methods on a
|
|
182
|
+
// real manager, while the nested-call members stay local to this one tool invocation.
|
|
183
|
+
return Object.assign(Object.create(ctx), {
|
|
184
|
+
tools: callable,
|
|
185
|
+
executeTool: async (callerId, name) => ({
|
|
186
|
+
toolCall: { type: "toolCall", id: `${callerId}/1`, name, arguments: {} },
|
|
187
|
+
result: { content: [{ type: "text", text: `nested tool ${name} is unavailable in this harness` }], details: undefined, isError: true },
|
|
188
|
+
isError: true,
|
|
189
|
+
}),
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/** Decoded JSON of a tool that has not moved to structured outcomes: reset and budget tools only. */
|
|
193
|
+
export function resultJson(result) {
|
|
194
|
+
const text = result.content[0];
|
|
195
|
+
assert.ok(text && text.type === "text", "tool result carries text");
|
|
196
|
+
return JSON.parse(text.text);
|
|
197
|
+
}
|
|
198
|
+
function outcomeOf(result) {
|
|
199
|
+
const structured = result.structuredContent;
|
|
200
|
+
assert.ok(typeof structured === "object" && structured !== null && !Array.isArray(structured), "tool result carries a structured outcome");
|
|
201
|
+
return structured;
|
|
202
|
+
}
|
|
203
|
+
function isOperationError(value) {
|
|
204
|
+
if (typeof value !== "object" || value === null)
|
|
205
|
+
return false;
|
|
206
|
+
const candidate = value;
|
|
207
|
+
return typeof candidate.code === "string" && typeof candidate.message === "string";
|
|
208
|
+
}
|
|
209
|
+
function isTextWindow(value) {
|
|
210
|
+
if (typeof value !== "object" || value === null)
|
|
211
|
+
return false;
|
|
212
|
+
const candidate = value;
|
|
213
|
+
return typeof candidate.text === "string" && typeof candidate.total_chars === "number" &&
|
|
214
|
+
(candidate.next_offset_chars === null || typeof candidate.next_offset_chars === "number");
|
|
215
|
+
}
|
|
216
|
+
/** Data of a successful outcome, asserting both the structured success and the non-error result. */
|
|
217
|
+
export function resultData(result) {
|
|
218
|
+
const outcome = outcomeOf(result);
|
|
219
|
+
assert.equal("error" in outcome, false, `expected success, got refusal ${JSON.stringify(outcome)}`);
|
|
220
|
+
assert.notEqual(result.isError, true, "a successful outcome is never delivered as an error result");
|
|
221
|
+
return outcome;
|
|
222
|
+
}
|
|
223
|
+
/** The typed refusal of a failed outcome, asserting the error result and the refusal shape. */
|
|
224
|
+
export function resultError(result) {
|
|
225
|
+
const outcome = outcomeOf(result);
|
|
226
|
+
assert.equal(result.isError, true, "a refusal is delivered as an error result");
|
|
227
|
+
const error = { code: outcome.code, message: outcome.error };
|
|
228
|
+
assert.ok(isOperationError(error), "refusal carries a code and an error message");
|
|
229
|
+
return error;
|
|
230
|
+
}
|
|
231
|
+
export function resultRead(result) {
|
|
232
|
+
const data = resultData(result);
|
|
233
|
+
assert.ok(isTextWindow(data), "read carries flat text and pagination fields");
|
|
234
|
+
return { ...data, content: data.text };
|
|
235
|
+
}
|
|
236
|
+
/** Assert both surfaces of one result fit the wire budget: the model text and the structured payload. */
|
|
237
|
+
export function assertWithinBudget(result, message) {
|
|
238
|
+
const text = result.content[0];
|
|
239
|
+
const bytes = text && text.type === "text" ? Buffer.byteLength(text.text, "utf8") : 0;
|
|
240
|
+
assert.ok(bytes <= TOOL_OUTPUT_MAX_BYTES, `${message}: model text is ${bytes} bytes over the ${TOOL_OUTPUT_MAX_BYTES}-byte budget`);
|
|
241
|
+
if (result.structuredContent !== undefined) {
|
|
242
|
+
const structured = structuredBytes(result.structuredContent);
|
|
243
|
+
assert.ok(structured <= TOOL_OUTPUT_MAX_BYTES, `${message}: structured outcome is ${structured} bytes over the ${TOOL_OUTPUT_MAX_BYTES}-byte budget`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
export async function runManualCompact(captured, ctx) {
|
|
247
|
+
const handler = captured.handlers.get("session_before_compact")?.[0];
|
|
248
|
+
assert.ok(handler, "session_before_compact handler registered");
|
|
249
|
+
const event = {
|
|
250
|
+
type: "session_before_compact",
|
|
251
|
+
reason: "manual",
|
|
252
|
+
willRetry: false,
|
|
253
|
+
signal: new AbortController().signal,
|
|
254
|
+
branchEntries: ctx.sessionManager.getBranch(),
|
|
255
|
+
preparation: {
|
|
256
|
+
firstKeptEntryId: "",
|
|
257
|
+
messagesToSummarize: [],
|
|
258
|
+
turnPrefixMessages: [],
|
|
259
|
+
isSplitTurn: false,
|
|
260
|
+
tokensBefore: 0,
|
|
261
|
+
fileOps: { read: new Set(), written: new Set(), edited: new Set() },
|
|
262
|
+
settings: { enabled: true, reserveTokens: 0, keepRecentTokens: 0 },
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
return (await handler(event, ctx));
|
|
266
|
+
}
|
|
267
|
+
export async function runHandlers(captured, name, event, ctx) {
|
|
268
|
+
const isIdle = ctx.isIdle;
|
|
269
|
+
if (name === "agent_settled")
|
|
270
|
+
ctx.isIdle = () => true;
|
|
271
|
+
try {
|
|
272
|
+
for (const handler of captured.handlers.get(name) ?? [])
|
|
273
|
+
await handler(event, ctx);
|
|
274
|
+
}
|
|
275
|
+
finally {
|
|
276
|
+
ctx.isIdle = isIdle;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
export async function runCommand(captured, name, args, ctx) {
|
|
280
|
+
const command = captured.commands.get(name);
|
|
281
|
+
assert.ok(command, `${name} command registered`);
|
|
282
|
+
const notices = [];
|
|
283
|
+
const cmdCtx = Object.assign({}, ctx, {
|
|
284
|
+
waitForIdle: async () => { },
|
|
285
|
+
ui: { notify: (message, type) => notices.push({ message, type }) },
|
|
286
|
+
});
|
|
287
|
+
await command.handler(args, cmdCtx);
|
|
288
|
+
return notices;
|
|
289
|
+
}
|
|
290
|
+
export async function runContextHook(captured, ctx, eventOverride = {}) {
|
|
291
|
+
const handlers = captured.handlers.get("context") ?? [];
|
|
292
|
+
assert.ok(handlers.length > 0, "context handler registered");
|
|
293
|
+
// Pi invokes every registered context handler in order; budget and warning each own one.
|
|
294
|
+
let result;
|
|
295
|
+
for (const handler of handlers) {
|
|
296
|
+
const returned = (await handler({ type: "context", messages: [], ...eventOverride }, ctx));
|
|
297
|
+
if (returned !== undefined) {
|
|
298
|
+
captured.contextMessages.push(...returned.messages);
|
|
299
|
+
result = result ? { messages: [...result.messages, ...returned.messages] } : returned;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
export async function runContextWithSystemHook(captured, ctx, messages) {
|
|
305
|
+
const handlers = captured.handlers.get("context_with_system") ?? [];
|
|
306
|
+
assert.ok(handlers.length > 0, "context_with_system handler registered");
|
|
307
|
+
let result;
|
|
308
|
+
for (const handler of handlers) {
|
|
309
|
+
const returned = (await handler({ type: "context_with_system", messages }, ctx));
|
|
310
|
+
if (returned !== undefined) {
|
|
311
|
+
captured.contextMessages.push(...returned.messages);
|
|
312
|
+
result = result ? { messages: [...result.messages, ...returned.messages] } : returned;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return result;
|
|
316
|
+
}
|
|
317
|
+
export async function commitTurnEndBoundary(captured, sessionManager, ctx) {
|
|
318
|
+
let entries = [];
|
|
319
|
+
let shouldContinue = false;
|
|
320
|
+
const event = {
|
|
321
|
+
type: "turn_end",
|
|
322
|
+
entries,
|
|
323
|
+
continue: false,
|
|
324
|
+
context: { contextEntries: [], contextMessages: [], llmMessages: [], pendingMessages: [], canContinue: true },
|
|
325
|
+
outcome: "completed",
|
|
326
|
+
turnIndex: 0,
|
|
327
|
+
message: { role: "assistant", content: [], stopReason: "stop", timestamp: Date.now() },
|
|
328
|
+
toolResults: [],
|
|
329
|
+
messageEntryId: "assistant-entry",
|
|
330
|
+
toolResultEntryIds: [],
|
|
331
|
+
};
|
|
332
|
+
for (const handler of captured.handlers.get("turn_end") ?? []) {
|
|
333
|
+
event.entries = entries;
|
|
334
|
+
const result = (await handler(event, ctx));
|
|
335
|
+
if (result?.entries !== undefined)
|
|
336
|
+
entries = result.entries;
|
|
337
|
+
if (result?.continue !== undefined)
|
|
338
|
+
shouldContinue = result.continue;
|
|
339
|
+
}
|
|
340
|
+
for (const entry of entries) {
|
|
341
|
+
switch (entry.type) {
|
|
342
|
+
case "custom":
|
|
343
|
+
sessionManager.appendCustomEntry(entry.customType, entry.data);
|
|
344
|
+
break;
|
|
345
|
+
case "custom_message":
|
|
346
|
+
sessionManager.appendCustomMessageEntry(entry.customType, entry.content, entry.display, entry.details);
|
|
347
|
+
break;
|
|
348
|
+
case "compaction":
|
|
349
|
+
sessionManager.appendCompaction(entry.summary, entry.firstKeptEntryId, 0, entry.details, true, entry.usage);
|
|
350
|
+
break;
|
|
351
|
+
case "context_edit":
|
|
352
|
+
sessionManager.appendContextEdit(entry.targetId, entry.replacement);
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return { entries, continue: shouldContinue };
|
|
357
|
+
}
|
|
358
|
+
export function appendText(sessionManager, role, text, toolName = "bash") {
|
|
359
|
+
const base = {
|
|
360
|
+
role,
|
|
361
|
+
content: [{ type: "text", text }],
|
|
362
|
+
timestamp: Date.now(),
|
|
363
|
+
...(role === "assistant" ? { stopReason: "stop" } : {}),
|
|
364
|
+
...(role === "toolResult" ? { toolCallId: "call-1", toolName, isError: false } : {}),
|
|
365
|
+
};
|
|
366
|
+
return sessionManager.appendMessage(base);
|
|
367
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type NoteRow, type NotesQuery, type Scope } from "../../src/notes/index.js";
|
|
3
|
+
/** Test-only conveniences for fixtures that still model notes through a live Pi context. */
|
|
4
|
+
export declare function physicalPath(scope: Scope, path: string, ctx: ExtensionContext, who?: string): string;
|
|
5
|
+
export declare function scopeDir(scope: Scope, ctx: ExtensionContext, who?: string): string;
|
|
6
|
+
export declare function listNotes(ctx: ExtensionContext, query?: NotesQuery): Promise<NoteRow[]>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createNotesStore } from "../../src/notes/index.js";
|
|
2
|
+
import { notesIdentityFromPi } from "../../src/pi/notes/adapter.js";
|
|
3
|
+
import { physicalPath as corePhysicalPath, scopeDir as coreScopeDir } from "../../src/notes/paths.js";
|
|
4
|
+
/** Test-only conveniences for fixtures that still model notes through a live Pi context. */
|
|
5
|
+
export function physicalPath(scope, path, ctx, who) {
|
|
6
|
+
return corePhysicalPath(scope, path, notesIdentityFromPi(ctx), who);
|
|
7
|
+
}
|
|
8
|
+
export function scopeDir(scope, ctx, who) {
|
|
9
|
+
return coreScopeDir(scope, notesIdentityFromPi(ctx), who);
|
|
10
|
+
}
|
|
11
|
+
export function listNotes(ctx, query = {}) {
|
|
12
|
+
return createNotesStore(notesIdentityFromPi(ctx)).list(query);
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|