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,136 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
4
|
+
import test from "node:test";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { basename, join } from "node:path";
|
|
7
|
+
import { acquireLock, releaseLock } from "../src/dream/lock.js";
|
|
8
|
+
import { dreamerDeleteToolDefinition, dreamerWriteToolDefinitions } from "../src/pi/dream/runner.js";
|
|
9
|
+
import { main } from "../src/pi/dream/cli.js";
|
|
10
|
+
const fixture = () => mkdtempSync(join(tmpdir(), "dream-test-"));
|
|
11
|
+
function scriptedSession(run) {
|
|
12
|
+
return async ({ cwd }) => {
|
|
13
|
+
let handler = () => { };
|
|
14
|
+
const session = {
|
|
15
|
+
subscribe(next) { handler = next; return () => { }; },
|
|
16
|
+
async prompt() { run(handler, cwd); },
|
|
17
|
+
dispose() { },
|
|
18
|
+
};
|
|
19
|
+
return session;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
async function captureErrors(run) {
|
|
23
|
+
const errors = [];
|
|
24
|
+
const original = console.error;
|
|
25
|
+
console.error = (...parts) => { errors.push(parts.map(String).join(" ")); };
|
|
26
|
+
try {
|
|
27
|
+
return { code: await run(), errors };
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
console.error = original;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
test("dream lock refuses existing owners and preserves ownership", () => {
|
|
34
|
+
const home = fixture();
|
|
35
|
+
const lock = join(home, ".dream.lock");
|
|
36
|
+
writeFileSync(lock, "999999 dead-owner");
|
|
37
|
+
const refused = acquireLock(lock);
|
|
38
|
+
assert.equal(refused.held, false);
|
|
39
|
+
assert.equal(refused.reason, "lock gate: lock already exists");
|
|
40
|
+
assert.equal(readFileSync(lock, "utf8"), "999999 dead-owner");
|
|
41
|
+
rmSync(lock);
|
|
42
|
+
const held = acquireLock(lock);
|
|
43
|
+
assert.equal(held.held, true);
|
|
44
|
+
const contender = acquireLock(lock);
|
|
45
|
+
assert.equal(contender.held, false);
|
|
46
|
+
assert.equal(readFileSync(lock, "utf8").trim(), `${process.pid} ${held.token}`);
|
|
47
|
+
writeFileSync(lock, "123 other-owner");
|
|
48
|
+
releaseLock(held);
|
|
49
|
+
assert.equal(readFileSync(lock, "utf8"), "123 other-owner", "release cannot remove a replacement owner");
|
|
50
|
+
const own = acquireLock(lock);
|
|
51
|
+
assert.equal(own.held, false);
|
|
52
|
+
rmSync(lock);
|
|
53
|
+
const released = acquireLock(lock);
|
|
54
|
+
assert.equal(released.held, true);
|
|
55
|
+
releaseLock(released);
|
|
56
|
+
assert.equal(existsSync(lock), false, "release removes the lock this run owns");
|
|
57
|
+
});
|
|
58
|
+
test("dreamer writes stay inside the notes home, including symlink escapes", async () => {
|
|
59
|
+
const home = fixture();
|
|
60
|
+
const outside = fixture();
|
|
61
|
+
const tools = new Map(dreamerWriteToolDefinitions(home).map((tool) => [tool.name, tool]));
|
|
62
|
+
const ctx = { cwd: home };
|
|
63
|
+
await tools.get("write").execute("write", { path: "global/x.md", content: "one" }, undefined, undefined, ctx);
|
|
64
|
+
assert.equal(readFileSync(join(home, "global/x.md"), "utf8"), "one");
|
|
65
|
+
const rejects = async (tool, path) => {
|
|
66
|
+
const params = tool === "write" ? { path, content: "outside" } : { path, edits: [{ oldText: "one", newText: "outside" }] };
|
|
67
|
+
await assert.rejects(() => tools.get(tool).execute("escape", params, undefined, undefined, ctx), /dream-test/);
|
|
68
|
+
};
|
|
69
|
+
await rejects("write", "../outside.md");
|
|
70
|
+
await rejects("edit", "/tmp/outside.md");
|
|
71
|
+
symlinkSync(outside, join(home, "escape"));
|
|
72
|
+
await rejects("write", "escape/outside.md");
|
|
73
|
+
assert.equal(existsSync(join(outside, "outside.md")), false);
|
|
74
|
+
});
|
|
75
|
+
test("dreamer delete is jailed to the notes home and refuses live notes", async () => {
|
|
76
|
+
const home = fixture();
|
|
77
|
+
const outside = fixture();
|
|
78
|
+
mkdirSync(join(home, "global"), { recursive: true });
|
|
79
|
+
const frontmatter = "---\norigin: self\ncreatedAt: 2026-09-01T00:00:00.000+08:00\nupdatedAt: 2026-09-01T00:00:00.000+08:00\nlastAccessed: 2026-09-01T00:00:00.000+08:00\naccessCount: 0\n";
|
|
80
|
+
writeFileSync(join(home, "global/dead.md"), `${frontmatter}crumpledAt: 2026-09-02T00:00:00.000+08:00\n---\ndead\n`);
|
|
81
|
+
writeFileSync(join(home, "global/live.md"), `${frontmatter}---\nlive\n`);
|
|
82
|
+
writeFileSync(join(outside, "keep.md"), "stay");
|
|
83
|
+
const del = dreamerDeleteToolDefinition(home);
|
|
84
|
+
const ctx = { cwd: home };
|
|
85
|
+
await del.execute("d1", { path: "global/dead.md" }, undefined, undefined, ctx);
|
|
86
|
+
assert.equal(existsSync(join(home, "global/dead.md")), false);
|
|
87
|
+
await assert.rejects(() => del.execute("d2", { path: "global/live.md" }, undefined, undefined, ctx), /not a crumpled note/);
|
|
88
|
+
assert.equal(existsSync(join(home, "global/live.md")), true);
|
|
89
|
+
await assert.rejects(() => del.execute("d3", { path: join("..", basename(outside), "keep.md") }, undefined, undefined, ctx), /write jail/);
|
|
90
|
+
symlinkSync(join(outside, "keep.md"), join(home, "link.md"));
|
|
91
|
+
await assert.rejects(() => del.execute("d4", { path: "link.md" }, undefined, undefined, ctx), /write jail/);
|
|
92
|
+
assert.equal(existsSync(join(outside, "keep.md")), true);
|
|
93
|
+
});
|
|
94
|
+
test("CLI lists dreamer deletions in the report", async () => {
|
|
95
|
+
const home = fixture();
|
|
96
|
+
const sessionFactory = scriptedSession((handler, cwd) => {
|
|
97
|
+
mkdirSync(join(cwd, "global"), { recursive: true });
|
|
98
|
+
handler({ type: "tool_execution_start", toolName: "delete", args: { path: "global/dead.md" } });
|
|
99
|
+
handler({ type: "message_end", message: { role: "assistant", stopReason: "stop", content: [{ type: "text", text: "dreamt" }] } });
|
|
100
|
+
});
|
|
101
|
+
const { code, errors } = await captureErrors(() => main(["--notes-home", home, "--force"], { sessionFactory, dreamerSettings: () => ({ warnings: [] }) }));
|
|
102
|
+
assert.equal(code, 0, errors.join("\n"));
|
|
103
|
+
const reports = readdirSync(join(home, "dreams"));
|
|
104
|
+
assert.equal(reports.length, 1);
|
|
105
|
+
assert.match(readFileSync(join(home, "dreams", reports[0]), "utf8"), /- delete: global\/dead\.md/);
|
|
106
|
+
});
|
|
107
|
+
test("CLI records a final audit failure and preserves the report", async () => {
|
|
108
|
+
const home = fixture();
|
|
109
|
+
const sessionFactory = scriptedSession((_handler, cwd) => {
|
|
110
|
+
mkdirSync(join(cwd, "global"), { recursive: true });
|
|
111
|
+
writeFileSync(join(cwd, "global/ok.md"), "ok");
|
|
112
|
+
rmSync(join(cwd, ".git"), { recursive: true, force: true });
|
|
113
|
+
writeFileSync(join(cwd, ".git"), "broken");
|
|
114
|
+
});
|
|
115
|
+
const { code, errors } = await captureErrors(() => main(["--notes-home", home, "--force"], { sessionFactory, dreamerSettings: () => ({ warnings: [] }) }));
|
|
116
|
+
assert.notEqual(code, 0);
|
|
117
|
+
assert.ok(errors.some((line) => /final audit failed/.test(line)));
|
|
118
|
+
const reports = readdirSync(join(home, "dreams"));
|
|
119
|
+
assert.equal(reports.length, 1);
|
|
120
|
+
assert.match(readFileSync(join(home, "dreams", reports[0]), "utf8"), /Final audit failed/);
|
|
121
|
+
});
|
|
122
|
+
test("CLI commits partial writes when report writing fails", async () => {
|
|
123
|
+
const home = fixture();
|
|
124
|
+
writeFileSync(join(home, "dreams"), "not a directory");
|
|
125
|
+
const sessionFactory = scriptedSession((handler, cwd) => {
|
|
126
|
+
mkdirSync(join(cwd, "global"), { recursive: true });
|
|
127
|
+
writeFileSync(join(cwd, "global/partial.md"), "half");
|
|
128
|
+
handler({ type: "tool_execution_start", toolName: "write", args: { path: "global/partial.md", content: "half" } });
|
|
129
|
+
handler({ type: "message_end", message: { role: "assistant", stopReason: "error", errorMessage: "Insufficient Balance" } });
|
|
130
|
+
});
|
|
131
|
+
const { code, errors } = await captureErrors(() => main(["--notes-home", home, "--force"], { sessionFactory, dreamerSettings: () => ({ warnings: [] }) }));
|
|
132
|
+
assert.notEqual(code, 0);
|
|
133
|
+
assert.ok(errors.some((line) => /could not write report/.test(line)));
|
|
134
|
+
assert.match(execFileSync("git", ["log", "--format=%s"], { cwd: home, encoding: "utf8" }).trim(), /\(failed\)/);
|
|
135
|
+
assert.equal(execFileSync("git", ["show", "HEAD:global/partial.md"], { cwd: home, encoding: "utf8" }), "half");
|
|
136
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
/** AST edges include erased types and re-exports, not merely emitted runtime imports. */
|
|
3
|
+
export declare function moduleReferences(source: ts.SourceFile): string[];
|
|
4
|
+
/** Resolve every reachable edge with TypeScript, including local aliases and external declarations. */
|
|
5
|
+
export declare function dependencyViolations(root: string, entries: string[], options: ts.CompilerOptions, runtimeDependencies: Set<string>): string[];
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { builtinModules } from "node:module";
|
|
2
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
3
|
+
import { relative, resolve, sep } from "node:path";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
const builtins = new Set(builtinModules.map((name) => name.replace(/^node:/, "")));
|
|
6
|
+
const piPackage = /^(?:@earendil-works|@mariozechner)\/pi-(?:ai|agent-core|coding-agent)(?:\/|$)/;
|
|
7
|
+
/** AST edges include erased types and re-exports, not merely emitted runtime imports. */
|
|
8
|
+
export function moduleReferences(source) {
|
|
9
|
+
const references = new Set();
|
|
10
|
+
const add = (node) => {
|
|
11
|
+
if (node && ts.isStringLiteralLike(node))
|
|
12
|
+
references.add(node.text);
|
|
13
|
+
};
|
|
14
|
+
const visit = (node) => {
|
|
15
|
+
if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node))
|
|
16
|
+
add(node.moduleSpecifier);
|
|
17
|
+
if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference))
|
|
18
|
+
add(node.moduleReference.expression);
|
|
19
|
+
if (ts.isImportTypeNode(node) && ts.isLiteralTypeNode(node.argument))
|
|
20
|
+
add(node.argument.literal);
|
|
21
|
+
if (ts.isCallExpression(node) && (node.expression.kind === ts.SyntaxKind.ImportKeyword || (ts.isIdentifier(node.expression) && node.expression.text === "require")))
|
|
22
|
+
add(node.arguments[0]);
|
|
23
|
+
ts.forEachChild(node, visit);
|
|
24
|
+
};
|
|
25
|
+
visit(source);
|
|
26
|
+
return [...references];
|
|
27
|
+
}
|
|
28
|
+
function packageName(specifier) {
|
|
29
|
+
return specifier.startsWith("@") ? specifier.split("/").slice(0, 2).join("/") : specifier.split("/")[0];
|
|
30
|
+
}
|
|
31
|
+
/** Resolve every reachable edge with TypeScript, including local aliases and external declarations. */
|
|
32
|
+
export function dependencyViolations(root, entries, options, runtimeDependencies) {
|
|
33
|
+
root = realpathSync(root);
|
|
34
|
+
const violations = [];
|
|
35
|
+
const visited = new Set();
|
|
36
|
+
const cache = ts.createModuleResolutionCache(root, (path) => path, options);
|
|
37
|
+
const sourceRoot = resolve(root, "src");
|
|
38
|
+
const display = (path) => relative(root, path).split(sep).join("/");
|
|
39
|
+
const native = (path) => path === resolve(sourceRoot, "index.ts") || path.startsWith(resolve(sourceRoot, "pi") + sep) || /node_modules\/(?:@earendil-works|@mariozechner)\/pi-(?:ai|agent-core|coding-agent)(?:\/|$)/.test(path.split(sep).join("/"));
|
|
40
|
+
const walk = (file, chain) => {
|
|
41
|
+
file = realpathSync(file);
|
|
42
|
+
if (native(file)) {
|
|
43
|
+
violations.push([...chain, display(file)].join(" -> "));
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (visited.has(file))
|
|
47
|
+
return;
|
|
48
|
+
visited.add(file);
|
|
49
|
+
const here = [...chain, display(file)];
|
|
50
|
+
const source = ts.createSourceFile(file, readFileSync(file, "utf8"), ts.ScriptTarget.Latest, true);
|
|
51
|
+
for (const specifier of moduleReferences(source)) {
|
|
52
|
+
if (specifier.startsWith("node:") || builtins.has(specifier))
|
|
53
|
+
continue;
|
|
54
|
+
if (piPackage.test(specifier)) {
|
|
55
|
+
violations.push([...here, specifier].join(" -> "));
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const resolution = ts.resolveModuleName(specifier, file, options, ts.sys, cache).resolvedModule;
|
|
59
|
+
if (!resolution) {
|
|
60
|
+
violations.push([...here, `unresolved ${specifier}`].join(" -> "));
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
// A shared dependency must be intentional, not accidentally supplied by a Pi peer/dev dependency.
|
|
64
|
+
if (file.startsWith(sourceRoot + sep) && resolution.isExternalLibraryImport && !runtimeDependencies.has(packageName(specifier))) {
|
|
65
|
+
violations.push([...here, `undeclared runtime dependency ${specifier}`].join(" -> "));
|
|
66
|
+
}
|
|
67
|
+
walk(resolution.resolvedFileName, here);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
for (const entry of entries)
|
|
71
|
+
walk(resolve(root, entry), []);
|
|
72
|
+
return violations;
|
|
73
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function installExtensionTestHooks(prefix: string): import("./extension.js").ExtensionTestEnvironment;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import { installExtensionTestEnvironment } from "./extension.js";
|
|
3
|
+
export function installExtensionTestHooks(prefix) {
|
|
4
|
+
const environment = installExtensionTestEnvironment(prefix);
|
|
5
|
+
test.beforeEach(() => environment.beforeEach());
|
|
6
|
+
test.afterEach(() => environment.afterEach());
|
|
7
|
+
test.after(() => environment.dispose());
|
|
8
|
+
return environment;
|
|
9
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { AgentTool, AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import { type ContextUsage, type ExtensionAPI, type ExtensionContext, type ExtensionToolContext, type RegisteredCommand, SessionManager, SettingsManager, type SessionBoundaryDraft, type ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { type OperationError, type TextWindow } from "../../src/tools/result.js";
|
|
4
|
+
export type ExtensionTestEnvironment = {
|
|
5
|
+
readonly cwd: string;
|
|
6
|
+
readonly agentDir: string;
|
|
7
|
+
beforeEach(): void;
|
|
8
|
+
afterEach(): void;
|
|
9
|
+
newNotesRoot(): string;
|
|
10
|
+
dispose(): void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Install the process-level settings and notes roots needed by one test file.
|
|
14
|
+
* The caller owns the lifecycle hooks; this helper never registers tests or hooks.
|
|
15
|
+
*/
|
|
16
|
+
export declare function installExtensionTestEnvironment(prefix?: string): ExtensionTestEnvironment;
|
|
17
|
+
export type Notice = {
|
|
18
|
+
message: string;
|
|
19
|
+
type?: "info" | "warning" | "error";
|
|
20
|
+
};
|
|
21
|
+
export declare function writeJson(path: string, value: unknown): void;
|
|
22
|
+
export type SettingsFixture = {
|
|
23
|
+
cwd: string;
|
|
24
|
+
agentDir: string;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Materialize global (agentDir/settings.json) and project (cwd/.pi/settings.json)
|
|
28
|
+
* settings in temp directories, then read them back through the same public
|
|
29
|
+
* SettingsManager.create the extension uses. Never touches the real ~/.pi.
|
|
30
|
+
*/
|
|
31
|
+
export declare function settingsFixture(options?: {
|
|
32
|
+
global?: Record<string, unknown>;
|
|
33
|
+
project?: Record<string, unknown>;
|
|
34
|
+
reserveTokens?: number;
|
|
35
|
+
}): SettingsFixture;
|
|
36
|
+
export type EventHandler = (event: never, ctx: ExtensionContext) => unknown;
|
|
37
|
+
type SendMessageArg = Parameters<ExtensionAPI["sendMessage"]>[0];
|
|
38
|
+
export type SentMessage = {
|
|
39
|
+
message: SendMessageArg;
|
|
40
|
+
options: {
|
|
41
|
+
triggerTurn?: boolean;
|
|
42
|
+
} | undefined;
|
|
43
|
+
};
|
|
44
|
+
export type Captured = {
|
|
45
|
+
tools: Map<string, ToolDefinition>;
|
|
46
|
+
handlers: Map<string, EventHandler[]>;
|
|
47
|
+
commands: Map<string, CommandOptions>;
|
|
48
|
+
sent: SentMessage[];
|
|
49
|
+
contextMessages: unknown[];
|
|
50
|
+
flags: string[];
|
|
51
|
+
};
|
|
52
|
+
export type CommandOptions = Omit<RegisteredCommand, "name" | "sourceInfo">;
|
|
53
|
+
export type CompactionHookResult = {
|
|
54
|
+
cancel: true;
|
|
55
|
+
} | {
|
|
56
|
+
compaction: {
|
|
57
|
+
summary: string;
|
|
58
|
+
firstKeptEntryId: string | null;
|
|
59
|
+
tokensBefore: number;
|
|
60
|
+
details?: unknown;
|
|
61
|
+
};
|
|
62
|
+
} | undefined;
|
|
63
|
+
/** TypeBox's TSchema does not expose `type`/`required` statically; read them structurally. */
|
|
64
|
+
export declare function objectSchema(tool: ToolDefinition | undefined): {
|
|
65
|
+
type?: string;
|
|
66
|
+
required?: string[];
|
|
67
|
+
} | undefined;
|
|
68
|
+
export declare function manager(persisted?: boolean): SessionManager;
|
|
69
|
+
export declare function makeExtension(sessionManager: SessionManager, settingsManager?: SettingsManager): Captured;
|
|
70
|
+
export declare function explicitBoot(ctx: ExtensionContext, currentWindowId: string, previousWindowId: string | undefined): Promise<string>;
|
|
71
|
+
export declare function context(sessionManager: SessionManager, compact?: ExtensionContext["compact"], usage?: ContextUsage, idle?: boolean, cwd?: string, projectTrusted?: boolean, model?: string | {
|
|
72
|
+
provider: string;
|
|
73
|
+
id: string;
|
|
74
|
+
}): ExtensionContext;
|
|
75
|
+
export declare function noticesOf(ctx: ExtensionContext): Notice[];
|
|
76
|
+
export declare function sentOf(captured: Captured, customType: string): SentMessage[];
|
|
77
|
+
export declare function call(captured: Captured, name: string, params: Record<string, unknown>, ctx: ExtensionContext): Promise<AgentToolResult<unknown>>;
|
|
78
|
+
/**
|
|
79
|
+
* The context Pi hands a registered tool: the session context plus the nested-call surface
|
|
80
|
+
* (ExtensionToolContext). The registered notes/history tools never call another tool, so `tools`
|
|
81
|
+
* is empty and `executeTool` refuses instead of pretending a runtime exists.
|
|
82
|
+
*/
|
|
83
|
+
export declare function toolContext(ctx: ExtensionContext, callable?: readonly AgentTool[]): ExtensionToolContext;
|
|
84
|
+
/** Decoded JSON of a tool that has not moved to structured outcomes: reset and budget tools only. */
|
|
85
|
+
export declare function resultJson<T>(result: AgentToolResult<unknown>): T;
|
|
86
|
+
/** Data of a successful outcome, asserting both the structured success and the non-error result. */
|
|
87
|
+
export declare function resultData<T>(result: AgentToolResult<unknown>): T;
|
|
88
|
+
/** The typed refusal of a failed outcome, asserting the error result and the refusal shape. */
|
|
89
|
+
export declare function resultError(result: AgentToolResult<unknown>): OperationError;
|
|
90
|
+
/** Identity a read outcome carries next to its window; tests narrow it with an explicit type. */
|
|
91
|
+
export type ReadIdentity = {
|
|
92
|
+
metadata?: Record<string, unknown>;
|
|
93
|
+
address?: string;
|
|
94
|
+
seq?: number;
|
|
95
|
+
window_id?: string;
|
|
96
|
+
};
|
|
97
|
+
/** The structured data of a read outcome: the window plus whatever identity the operation adds. */
|
|
98
|
+
export type ReadOutcome<T extends object = ReadIdentity> = TextWindow & T;
|
|
99
|
+
/** A read as tests consume it: its window fields, plus `content` as a test-only alias for `text`. */
|
|
100
|
+
export type ReadWindowResult<T extends object = ReadIdentity> = TextWindow & {
|
|
101
|
+
content: string;
|
|
102
|
+
} & T;
|
|
103
|
+
export declare function resultRead<T extends object = ReadIdentity>(result: AgentToolResult<unknown>): ReadWindowResult<T>;
|
|
104
|
+
/** Assert both surfaces of one result fit the wire budget: the model text and the structured payload. */
|
|
105
|
+
export declare function assertWithinBudget(result: AgentToolResult<unknown>, message: string): void;
|
|
106
|
+
export declare function runManualCompact(captured: Captured, ctx: ExtensionContext): Promise<CompactionHookResult>;
|
|
107
|
+
export declare function runHandlers(captured: Captured, name: string, event: unknown, ctx: ExtensionContext): Promise<void>;
|
|
108
|
+
export declare function runCommand(captured: Captured, name: string, args: string, ctx: ExtensionContext): Promise<Notice[]>;
|
|
109
|
+
export type ContextHookResult = {
|
|
110
|
+
messages: unknown[];
|
|
111
|
+
} | undefined;
|
|
112
|
+
export declare function runContextHook(captured: Captured, ctx: ExtensionContext, eventOverride?: Record<string, unknown>): Promise<ContextHookResult>;
|
|
113
|
+
export declare function runContextWithSystemHook(captured: Captured, ctx: ExtensionContext, messages: unknown[]): Promise<ContextHookResult>;
|
|
114
|
+
export declare function commitTurnEndBoundary(captured: Captured, sessionManager: SessionManager, ctx: ExtensionContext): Promise<{
|
|
115
|
+
entries: SessionBoundaryDraft[];
|
|
116
|
+
continue: boolean;
|
|
117
|
+
}>;
|
|
118
|
+
export declare function appendText(sessionManager: SessionManager, role: "user" | "assistant" | "toolResult", text: string, toolName?: string): string;
|
|
119
|
+
export {};
|