piroboros 0.0.0-stage → 0.2.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.
Files changed (118) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE +39 -0
  4. package/README.md +140 -2
  5. package/agents/astrelia.md +31 -0
  6. package/agents/avaritia.md +31 -0
  7. package/agents/elaria.md +31 -0
  8. package/agents/nytheria.md +32 -0
  9. package/agents/raskreia.md +32 -0
  10. package/agents/samaela.md +40 -0
  11. package/agents/vaelia.md +35 -0
  12. package/agents/vesperia.md +45 -0
  13. package/bin/piroboros.mjs +17 -0
  14. package/dist/auth.js +105 -0
  15. package/dist/base.js +19 -0
  16. package/dist/chat/budget.js +87 -0
  17. package/dist/chat/compactloop.js +63 -0
  18. package/dist/chat/compactor.js +530 -0
  19. package/dist/chat/extension.js +36 -0
  20. package/dist/chat/hook.js +40 -0
  21. package/dist/chat/log.js +317 -0
  22. package/dist/chat/outbound.js +114 -0
  23. package/dist/chat/plan.js +64 -0
  24. package/dist/chat/prompt.js +115 -0
  25. package/dist/chat/status.js +37 -0
  26. package/dist/chat/stuck.js +83 -0
  27. package/dist/chat/tools.js +163 -0
  28. package/dist/chat/tree.js +73 -0
  29. package/dist/chat/turn.js +305 -0
  30. package/dist/chat/view.js +163 -0
  31. package/dist/conclave/agents.js +113 -0
  32. package/dist/conclave/pipeline.js +715 -0
  33. package/dist/conclave/report.js +43 -0
  34. package/dist/config.js +614 -0
  35. package/dist/durable.js +177 -0
  36. package/dist/guard.js +346 -0
  37. package/dist/lock.js +56 -0
  38. package/dist/main.js +348 -0
  39. package/dist/memory/akashic/api.js +121 -0
  40. package/dist/memory/akashic/confusables.js +41 -0
  41. package/dist/memory/akashic/scanner.js +67 -0
  42. package/dist/memory/akashic/snapshot.js +31 -0
  43. package/dist/memory/akashic/store.js +1254 -0
  44. package/dist/memory/akashic/tools.js +168 -0
  45. package/dist/memory/akashic/vault.js +984 -0
  46. package/dist/memory/host.js +124 -0
  47. package/dist/memory/mnemosia/adapters/akashic-memories.js +180 -0
  48. package/dist/memory/mnemosia/adapters/contract.js +219 -0
  49. package/dist/memory/mnemosia/agents.js +387 -0
  50. package/dist/memory/mnemosia/brief.js +890 -0
  51. package/dist/memory/mnemosia/claims.js +609 -0
  52. package/dist/memory/mnemosia/conflicts.js +769 -0
  53. package/dist/memory/mnemosia/escalation.js +257 -0
  54. package/dist/memory/mnemosia/feedback.js +108 -0
  55. package/dist/memory/mnemosia/filters.js +248 -0
  56. package/dist/memory/mnemosia/intent.js +221 -0
  57. package/dist/memory/mnemosia/open.js +34 -0
  58. package/dist/memory/mnemosia/orchestrator.js +926 -0
  59. package/dist/memory/mnemosia/plan.js +266 -0
  60. package/dist/memory/mnemosia/policy.js +426 -0
  61. package/dist/memory/mnemosia/progressive.js +550 -0
  62. package/dist/memory/mnemosia/provenance.js +458 -0
  63. package/dist/memory/mnemosia/qualifiers.js +428 -0
  64. package/dist/memory/mnemosia/rank.js +525 -0
  65. package/dist/memory/mnemosia/relations.js +156 -0
  66. package/dist/memory/mnemosia/requirements.js +138 -0
  67. package/dist/memory/mnemosia/rerank.js +253 -0
  68. package/dist/memory/mnemosia/retention.js +174 -0
  69. package/dist/memory/mnemosia/rollback.js +337 -0
  70. package/dist/memory/mnemosia/select.js +607 -0
  71. package/dist/memory/mnemosia/similarity.js +164 -0
  72. package/dist/memory/mnemosia/situation.js +251 -0
  73. package/dist/memory/mnemosia/task.js +418 -0
  74. package/dist/memory/mnemosia/temporal.js +136 -0
  75. package/dist/memory/mnemosia/tokens.js +66 -0
  76. package/dist/memory/mnemosia/tools.js +160 -0
  77. package/dist/memory/mnemosia/trace.js +306 -0
  78. package/dist/memory/mnemosia/types.js +298 -0
  79. package/dist/models.js +360 -0
  80. package/dist/paths.js +103 -0
  81. package/dist/redact.js +79 -0
  82. package/dist/runtime.js +378 -0
  83. package/dist/search-format.js +102 -0
  84. package/dist/search.js +351 -0
  85. package/dist/setup.js +366 -0
  86. package/dist/shell.js +110 -0
  87. package/dist/skills.js +311 -0
  88. package/dist/telemetry.js +84 -0
  89. package/dist/tui/app.js +1011 -0
  90. package/dist/tui/commands.js +121 -0
  91. package/dist/tui/keys.js +83 -0
  92. package/dist/tui/pane.js +387 -0
  93. package/dist/tui/panel.js +60 -0
  94. package/dist/tui/picker.js +25 -0
  95. package/dist/tui/sanitize.js +59 -0
  96. package/dist/tui/settings.js +198 -0
  97. package/dist/tui/setup.js +256 -0
  98. package/dist/tui/theme.js +26 -0
  99. package/dist/tui/thinking/parse.js +1094 -0
  100. package/dist/tui/thinking/select.js +86 -0
  101. package/dist/tui/thinking/state.js +137 -0
  102. package/dist/tui/thinking/types.js +3 -0
  103. package/dist/tui/thinking/view.js +74 -0
  104. package/dist/tui/viewport.js +208 -0
  105. package/dist/undo.js +258 -0
  106. package/dist/web.js +933 -0
  107. package/docs/conclave.md +21 -0
  108. package/docs/configuration.md +48 -0
  109. package/docs/development.md +25 -0
  110. package/docs/interface.md +33 -0
  111. package/docs/memory.md +11 -0
  112. package/docs/privacy.md +13 -0
  113. package/docs/tools.md +20 -0
  114. package/docs/web.md +38 -0
  115. package/models.json.example +34 -0
  116. package/package.json +76 -4
  117. package/settings.json.example +29 -0
  118. package/skills/conclave/SKILL.md +76 -0
package/dist/undo.js ADDED
@@ -0,0 +1,258 @@
1
+ import { createHash } from "node:crypto";
2
+ import { appendFileSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
3
+ import { dirname, join } from "node:path";
4
+ import { canonicalize, isInside } from "./paths.js";
5
+ // Undo store for a Vesperia run (P13a). Before a write or edit by Vesperia, the guard saves the file's bytes under
6
+ // ${home}/undo/<vesperia conversation id>/. One JSON and one .bin per path, named by the hash of the guard's folded
7
+ // path key. The copies are exact: they are never redacted, so they may hold secrets that were already in the file.
8
+ // Only the latest run is kept. Whether a path was captured already is decided by the JSON on disk, never by memory.
9
+ // Changes made through bash are not captured; the bash calls are only counted.
10
+ export const FILE_CAP = 1_048_576;
11
+ export const RUN_CAP = 20 * 1_048_576;
12
+ const sha = (data) => createHash("sha256").update(data).digest("hex");
13
+ const nameOf = (key) => sha(key);
14
+ const root = (home) => join(home, "undo");
15
+ const dirOf = (home, run) => join(root(home), String(run));
16
+ /** Make the run's folder. The first time a new run appears, every other run's folder is deleted. Vesperia's start calls it, so a run that writes nothing still replaces the older folder. */
17
+ export function ensureRun(home, run) {
18
+ const dir = dirOf(home, run);
19
+ if (!existsSync(dir)) {
20
+ try {
21
+ for (const other of readdirSync(root(home)))
22
+ if (other !== String(run))
23
+ rmSync(join(root(home), other), { recursive: true, force: true });
24
+ }
25
+ catch { /* no undo folder yet */ }
26
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
27
+ }
28
+ return dir;
29
+ }
30
+ const runBytes = (dir) => {
31
+ let total = 0;
32
+ for (const name of readdirSync(dir))
33
+ if (name.endsWith(".bin"))
34
+ total += statSync(join(dir, name)).size;
35
+ return total;
36
+ };
37
+ /** Save the pre-image of `path` (canonical) unless this run already has one. Throws only on a storage error; the guard fails open. */
38
+ export function capture(home, run, key, path, cwd) {
39
+ const dir = ensureRun(home, run);
40
+ const json = join(dir, `${nameOf(key)}.json`);
41
+ const bin = join(dir, `${nameOf(key)}.bin`);
42
+ if (existsSync(json))
43
+ return;
44
+ const record = { path, cwd, existed: false };
45
+ let bytes;
46
+ let info;
47
+ try {
48
+ info = statSync(path);
49
+ }
50
+ catch {
51
+ info = undefined;
52
+ }
53
+ if (info !== undefined) {
54
+ record.existed = true;
55
+ record.size = info.size;
56
+ if (!info.isFile())
57
+ record.notRestorable = "not a regular file";
58
+ else if (info.size > FILE_CAP)
59
+ record.notRestorable = "larger than 1 MB";
60
+ else if (runBytes(dir) + info.size > RUN_CAP)
61
+ record.notRestorable = "the run's copies reached 20 MB";
62
+ else {
63
+ try {
64
+ bytes = readFileSync(path);
65
+ record.preHash = sha(bytes);
66
+ record.size = bytes.length;
67
+ }
68
+ catch {
69
+ record.notRestorable = "could not be read";
70
+ }
71
+ }
72
+ }
73
+ // The JSON is the marker: a .bin without one is a crashed capture, and the write has not happened yet.
74
+ rmSync(bin, { force: true });
75
+ if (bytes !== undefined)
76
+ writeFileSync(bin, bytes, { flag: "wx", mode: 0o600 });
77
+ try {
78
+ writeFileSync(json, JSON.stringify(record), { flag: "wx", mode: 0o600 });
79
+ }
80
+ catch (error) {
81
+ if (error.code !== "EEXIST")
82
+ throw error;
83
+ }
84
+ }
85
+ /** After a failed capture: a JSON-only record so the undo preview still lists the file. Throws when even that cannot be stored. */
86
+ export function captureLost(home, run, key, path, cwd, error) {
87
+ const code = error?.code ?? "unknown error";
88
+ const record = { path, cwd, existed: existsSync(path), notRestorable: `the copy could not be saved: ${code}` };
89
+ try {
90
+ writeFileSync(join(ensureRun(home, run), `${nameOf(key)}.json`), JSON.stringify(record), { flag: "wx", mode: 0o600 });
91
+ }
92
+ catch (e) {
93
+ if (e.code !== "EEXIST")
94
+ throw e;
95
+ }
96
+ }
97
+ /** The hash Vesperia left behind; the last write wins. Nothing happens for a path that was not captured. */
98
+ export function recordPost(home, run, key, postHash) {
99
+ const json = join(dirOf(home, run), `${nameOf(key)}.json`);
100
+ if (!existsSync(json))
101
+ return;
102
+ const record = JSON.parse(readFileSync(json, "utf8"));
103
+ record.postHash = postHash;
104
+ writeFileSync(json, JSON.stringify(record), { mode: 0o600 });
105
+ }
106
+ /** Count one shell call of the run. An append is one step, so parallel calls do not lose counts. */
107
+ export function countBash(home, run) {
108
+ appendFileSync(join(ensureRun(home, run), "bash.count"), "x", { mode: 0o600 });
109
+ }
110
+ /** The stored run, or undefined when there is none. */
111
+ export function loadRun(home) {
112
+ let runs;
113
+ try {
114
+ runs = readdirSync(root(home)).filter((name) => /^\d+$/u.test(name)).map(Number).sort((a, b) => b - a);
115
+ }
116
+ catch {
117
+ return undefined;
118
+ }
119
+ const run = runs[0];
120
+ if (run === undefined)
121
+ return undefined;
122
+ const dir = dirOf(home, run);
123
+ const records = [];
124
+ for (const name of readdirSync(dir)) {
125
+ if (!name.endsWith(".json"))
126
+ continue;
127
+ try {
128
+ records.push(JSON.parse(readFileSync(join(dir, name), "utf8")));
129
+ }
130
+ catch { /* a damaged record is not listed; the undo cannot trust it */ }
131
+ }
132
+ let bash = 0;
133
+ try {
134
+ bash = statSync(join(dir, "bash.count")).size;
135
+ }
136
+ catch { /* none */ }
137
+ return { run, records, bash };
138
+ }
139
+ const hashOf = (path) => {
140
+ try {
141
+ return lstatSync(path).isFile() ? sha(readFileSync(path)) : undefined;
142
+ }
143
+ catch {
144
+ return undefined;
145
+ }
146
+ };
147
+ /** What an undo would do with one record. Nothing stored is trusted: the path is checked again and the copy is hashed again. */
148
+ export function assess(home, run, record, allowed) {
149
+ if (record.notRestorable !== undefined)
150
+ return { kind: "skip", reason: record.notRestorable };
151
+ let target;
152
+ try {
153
+ target = canonicalize(record.path, record.cwd);
154
+ if (target !== record.path)
155
+ return { kind: "skip", reason: "the path no longer resolves to the stored one" };
156
+ if (!isInside(canonicalize(record.cwd), target))
157
+ return { kind: "skip", reason: "outside the run's working directory" };
158
+ }
159
+ catch {
160
+ return { kind: "skip", reason: "the path could not be evaluated" };
161
+ }
162
+ if (!allowed(target, record.cwd))
163
+ return { kind: "skip", reason: "the path is protected" };
164
+ if (record.existed) {
165
+ const bin = join(dirOf(home, run), `${nameOf(process.platform === "win32" ? target.toLowerCase() : target)}.bin`);
166
+ let bytes;
167
+ try {
168
+ bytes = readFileSync(bin);
169
+ }
170
+ catch {
171
+ return { kind: "skip", reason: "the saved copy is missing" };
172
+ }
173
+ if (sha(bytes) !== record.preHash)
174
+ return { kind: "skip", reason: "the saved copy is damaged" };
175
+ }
176
+ let current;
177
+ try {
178
+ lstatSync(target);
179
+ current = hashOf(target) ?? "?";
180
+ }
181
+ catch {
182
+ current = undefined;
183
+ }
184
+ if (record.existed) {
185
+ if (current === record.preHash)
186
+ return { kind: "unchanged" };
187
+ return current !== undefined && current === record.postHash ? { kind: "restore" } : { kind: "changed" };
188
+ }
189
+ if (current === undefined)
190
+ return { kind: "unchanged" };
191
+ return current === record.postHash ? { kind: "delete" } : { kind: "changed" };
192
+ }
193
+ export const plan = (home, folder, allowed) => folder.records.map((record) => ({ record, verdict: assess(home, folder.run, record, allowed) }));
194
+ const list = (title, paths) => (paths.length === 0 ? [] : [`${title} (${paths.length}):`, ...paths.map((path) => ` ${path}`)]);
195
+ const bashLine = (bash) => (bash === 0 ? "Vesperia ran no shell commands." : `Vesperia ran ${bash} shell command${bash === 1 ? "" : "s"}; changes made through bash are not covered.`);
196
+ const MEMORY_LINE = "Vesperia's decision memory, if any, stays quarantined until you run /akashic accept; there is no reject command.";
197
+ const pick = (entries, kind) => entries.filter((e) => e.verdict.kind === kind).map((e) => e.record.path);
198
+ const skipped = (entries) => entries.flatMap((e) => (e.verdict.kind === "skip" ? [`${e.record.path} (${e.verdict.reason})`] : []));
199
+ /** The list shown before anything is touched. */
200
+ export function preview(entries, bash) {
201
+ return [
202
+ "Undo of the last Vesperia run. Only write and edit changes are covered.",
203
+ ...list("Files to restore", pick(entries, "restore")),
204
+ ...list("Files to delete (created by the run)", pick(entries, "delete")),
205
+ ...list("Skipped, changed since the run", pick(entries, "changed")),
206
+ ...list("Not restorable", skipped(entries)),
207
+ ...list("Already as before the run", pick(entries, "unchanged")),
208
+ bashLine(bash),
209
+ MEMORY_LINE,
210
+ ].join("\n");
211
+ }
212
+ /** Do the restores and deletions, each one assessed again just before it runs. Returns the report and whether anything was restored. */
213
+ export function apply(home, folder, allowed) {
214
+ const done = [];
215
+ const failed = [];
216
+ const dirs = new Set();
217
+ for (const { record } of plan(home, folder, allowed)) {
218
+ // Assessed again here, after the user confirmed, so a change in between is caught.
219
+ const verdict = assess(home, folder.run, record, allowed);
220
+ try {
221
+ if (verdict.kind === "restore")
222
+ writeFileSync(record.path, readFileSync(join(dirOf(home, folder.run), `${nameOf(process.platform === "win32" ? record.path.toLowerCase() : record.path)}.bin`)));
223
+ else if (verdict.kind === "delete") {
224
+ unlinkSync(record.path);
225
+ dirs.add(dirname(record.path));
226
+ }
227
+ }
228
+ catch (error) {
229
+ failed.push(`${record.path} (${error instanceof Error ? error.message : String(error)})`);
230
+ continue;
231
+ }
232
+ done.push({ record, verdict });
233
+ }
234
+ const left = [...dirs].filter((dir) => { try {
235
+ return readdirSync(dir).length === 0;
236
+ }
237
+ catch {
238
+ return false;
239
+ } });
240
+ if (failed.length === 0)
241
+ rmSync(dirOf(home, folder.run), { recursive: true, force: true });
242
+ return {
243
+ restored: done.some((e) => e.verdict.kind === "restore" || e.verdict.kind === "delete"),
244
+ text: [
245
+ "Undo finished.",
246
+ ...list("Restored", pick(done, "restore")),
247
+ ...list("Deleted", pick(done, "delete")),
248
+ ...list("Failed", failed),
249
+ ...(failed.length === 0 ? [] : ["Run /conclave undo again to retry the failed files."]),
250
+ ...list("Skipped, changed since the run", pick(done, "changed")),
251
+ ...list("Not restorable", skipped(done)),
252
+ ...list("Already as before the run", pick(done, "unchanged")),
253
+ ...list("Empty folders left in place", left),
254
+ bashLine(folder.bash),
255
+ MEMORY_LINE,
256
+ ].join("\n"),
257
+ };
258
+ }