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
@@ -0,0 +1,163 @@
1
+ // The view (D5): the list of tree nodes that covers the whole chat, oldest first. Everything here is pure, so the
2
+ // merge order can be checked against the push oracle and a crash cannot leave a half-applied rule.
3
+ //
4
+ // A line is [level, index, text]. Line (l, i) covers the 2^l messages from i * 2^l on and is shown as
5
+ // `id+n|text`. A null text is a leaf that has no node yet; it is shown as a placeholder and never as text.
6
+ export const VIEW_MAX = 128_000;
7
+ export const VIEW_TARGET = 64_000;
8
+ export const WINDOW_MAX = 128_000;
9
+ export const WINDOW_TARGET = 96_000;
10
+ export const NODE_TEXT_MAX = 512;
11
+ export const LINES_PER_BLOCK = 4;
12
+ export const BANNER_WARN_BYTES = 64_000;
13
+ export const BANNER_ESCALATE_BYTES = 512_000;
14
+ export const PLACEHOLDER = "(not summarized yet: zoom it)";
15
+ export const emptyView = () => ({ lines: [], bytes: 0, T: 0, shrinking: false, batchSeq: 0 });
16
+ export const cloneView = (view) => ({ ...view, lines: view.lines.map((line) => [...line]) });
17
+ export const lineFirst = (line) => line[1] * 2 ** line[0];
18
+ export const lineSize = (line) => 2 ** line[0];
19
+ /** The line as the model reads it, newline included. */
20
+ export const lineText = (line) => `${lineFirst(line)}+${lineSize(line)}|${(line[2] ?? PLACEHOLDER).replace(/\n/gu, " ")}\n`;
21
+ export const lineBytes = (line) => Buffer.byteLength(lineText(line));
22
+ export const utf8Bytes = (text) => Buffer.byteLength(text);
23
+ function replaceLine(view, at, count, ...lines) {
24
+ for (let k = at; k < at + count; k += 1)
25
+ view.bytes -= lineBytes(view.lines[k]);
26
+ view.lines.splice(at, count, ...lines);
27
+ for (const line of lines)
28
+ view.bytes += lineBytes(line);
29
+ }
30
+ /**
31
+ * Index of the left line of the most due mergeable pair, or -1. Sibling lines (l, i) and (l, i + 1) with i even
32
+ * are a pair; its due value is (T - last) / 2^l, where `last` is the pair's last message. Ties go to the oldest
33
+ * pair. Only a pair whose parent is built can merge.
34
+ */
35
+ export function mostDue(view, nodes) {
36
+ const candidates = [];
37
+ for (let k = 0; k + 1 < view.lines.length; k += 1) {
38
+ const [l, i] = view.lines[k];
39
+ const next = view.lines[k + 1];
40
+ if (next[0] !== l || i % 2 !== 0 || next[1] !== i + 1)
41
+ continue;
42
+ const last = (i + 2) * 2 ** l - 1;
43
+ candidates.push([k, (view.T - last) / 2 ** l]);
44
+ }
45
+ candidates.sort((a, b) => b[1] - a[1] || a[0] - b[0]);
46
+ for (const [k] of candidates) {
47
+ const [l, i] = view.lines[k];
48
+ if (nodes.get(l + 1, i / 2) !== undefined)
49
+ return k;
50
+ }
51
+ return -1;
52
+ }
53
+ /** Merge the most due pair into its parent. False when no pair can merge. */
54
+ export function mergeOnce(view, nodes) {
55
+ const k = mostDue(view, nodes);
56
+ if (k < 0)
57
+ return false;
58
+ const [l, i] = view.lines[k];
59
+ replaceLine(view, k, 2, [l + 1, i / 2, nodes.get(l + 1, i / 2)]);
60
+ return true;
61
+ }
62
+ /** Fill leaves that were unbuilt when they were appended and have a node now. A one-time cache break at each. */
63
+ export function fillLeaves(view, nodes) {
64
+ view.lines.forEach((line, k) => {
65
+ if (line[2] !== null)
66
+ return;
67
+ const text = nodes.get(line[0], line[1]);
68
+ if (text !== undefined)
69
+ replaceLine(view, k, 1, [line[0], line[1], text]);
70
+ });
71
+ }
72
+ /** One batch: merge until the view is at most the target, or no pair can merge, which sets `shrinking`. */
73
+ export function shrink(view, nodes, max = VIEW_MAX, target = VIEW_TARGET) {
74
+ if (!view.shrinking && view.bytes > max)
75
+ view.batchSeq += 1;
76
+ while (view.bytes > target && mergeOnce(view, nodes))
77
+ ;
78
+ view.shrinking = view.bytes > target;
79
+ }
80
+ /** Append the leaf of the next message and, when the view is over the maximum or still shrinking, merge. */
81
+ export function pushMessage(view, nodes, max = VIEW_MAX, target = VIEW_TARGET) {
82
+ const line = [0, view.T, nodes.get(0, view.T) ?? null];
83
+ replaceLine(view, view.lines.length, 0, line);
84
+ view.T += 1;
85
+ if (view.bytes > max || view.shrinking)
86
+ shrink(view, nodes, max, target);
87
+ }
88
+ /** Advance the view to cover `T` messages, one message at a time (M5a). */
89
+ export function advanceTo(view, T, nodes, max = VIEW_MAX, target = VIEW_TARGET) {
90
+ fillLeaves(view, nodes);
91
+ while (view.T < T)
92
+ pushMessage(view, nodes, max, target);
93
+ }
94
+ export const earlierLine = (firstId, label) => `0+${firstId}|(earlier history not summarized: compactor ${label}; zoom ids < ${firstId})\n`;
95
+ const firstAtOrAfter = (lines, start) => {
96
+ let lo = 0;
97
+ let hi = lines.length;
98
+ while (lo < hi) {
99
+ const mid = (lo + hi) >> 1;
100
+ if (lineFirst(lines[mid]) < start)
101
+ lo = mid + 1;
102
+ else
103
+ hi = mid;
104
+ }
105
+ return lo;
106
+ };
107
+ /**
108
+ * The first message id of the rendered window. It holds until the window (with the one line that stands for
109
+ * everything before it) would pass 128,000 bytes, then jumps forward on a line boundary until it is at most
110
+ * 96,000. A compactor that is configured never windows.
111
+ */
112
+ export function nextWindowStart(lines, previous, label) {
113
+ if (label === "configured")
114
+ return 0;
115
+ let p = firstAtOrAfter(lines, previous);
116
+ if (p >= lines.length)
117
+ return previous;
118
+ let bytes = 0;
119
+ for (let k = p; k < lines.length; k += 1)
120
+ bytes += lineBytes(lines[k]);
121
+ const earlier = (k) => (k > 0 ? utf8Bytes(earlierLine(lineFirst(lines[k]), label)) : 0);
122
+ if (bytes + earlier(p) <= WINDOW_MAX)
123
+ return previous;
124
+ let best = lines.length - 1;
125
+ bytes = 0;
126
+ for (let k = lines.length - 1; k >= p; k -= 1) {
127
+ bytes += lineBytes(lines[k]);
128
+ if (bytes + earlier(k) > WINDOW_TARGET)
129
+ break;
130
+ best = k;
131
+ }
132
+ p = best;
133
+ return lineFirst(lines[p]);
134
+ }
135
+ /** The lines the model reads: the window, preceded by one line for everything before it. */
136
+ export function renderLines(lines, windowStart, label) {
137
+ const p = label === "configured" ? 0 : firstAtOrAfter(lines, windowStart);
138
+ const out = lines.slice(p).map(lineText);
139
+ if (p > 0 && p < lines.length)
140
+ out.unshift(earlierLine(lineFirst(lines[p]), label));
141
+ return out;
142
+ }
143
+ /**
144
+ * The view as text blocks (D21): full blocks of four lines counted from the start of the list, then one tail block
145
+ * with the partial trailing lines and the closing tag. The tail is the only block that changes when a line is
146
+ * appended, and it is the block that carries no cache mark.
147
+ */
148
+ export function viewBlocks(rendered) {
149
+ const full = Math.floor(rendered.length / LINES_PER_BLOCK);
150
+ const blocks = [];
151
+ for (let j = 0; j < full; j += 1)
152
+ blocks.push(`${j === 0 ? "<chat>\n" : ""}${rendered.slice(j * LINES_PER_BLOCK, (j + 1) * LINES_PER_BLOCK).join("")}`);
153
+ blocks.push(`${full === 0 ? "<chat>\n" : ""}${rendered.slice(full * LINES_PER_BLOCK).join("")}</chat>`);
154
+ return blocks;
155
+ }
156
+ /** The persistent banner for a view that is growing without a compactor. */
157
+ export function viewBanner(label, docBytes) {
158
+ if (label === "configured" || docBytes <= BANNER_WARN_BYTES)
159
+ return undefined;
160
+ if (docBytes > BANNER_ESCALATE_BYTES)
161
+ return `compactor ${label}: the chat view has grown to ${Math.round(docBytes / 1000)} KB and the model sees only the newest ${WINDOW_MAX / 1000} KB of it; run piroboros setup to add a compactor`;
162
+ return `compactor ${label}: history beyond the window is only reachable by zoom`;
163
+ }
@@ -0,0 +1,113 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { basename, join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { CONCLAVE_MEMBERS, THINKING_LEVELS } from "../config.js";
5
+ // Loads the Conclave definitions that ship in the repository: agents/<name>.md and skills/conclave/SKILL.md.
6
+ // Everything is validated before any member starts, and every failure is a named ConclaveDefError.
7
+ //
8
+ // Frontmatter keys: name, display_name, description, thinking, tools. A `model:` key is rejected, because the
9
+ // model is a provider and cost choice that belongs to the user's settings, while reasoning depth is a property of
10
+ // the role. Only vesperia may list write, edit, bash, akashic_memory or mnemosia_submit_feedback; the reviewers
11
+ // may read memory through mnemosia_brief.
12
+ export const KNOWN_TOOLS = ["read", "write", "edit", "bash", "find", "grep", "zoom", "date", "plan", "akashic_memory", "mnemosia_brief", "mnemosia_submit_feedback"];
13
+ export const WRITER_TOOLS = ["write", "edit", "bash", "akashic_memory", "mnemosia_submit_feedback"];
14
+ const KEYS = ["name", "display_name", "description", "thinking", "tools"];
15
+ const SKILL_KEYS = ["name", "description"];
16
+ export class ConclaveDefError extends Error {
17
+ code;
18
+ file;
19
+ constructor(code, file, message) {
20
+ super(`${file}: ${message}`);
21
+ this.code = code;
22
+ this.file = file;
23
+ }
24
+ }
25
+ /** The repository root, where agents/ and skills/ live. */
26
+ export const REPO_ROOT = fileURLToPath(new URL("../../", import.meta.url));
27
+ function split(text, file) {
28
+ const lines = text.replace(/^\u{FEFF}/u, "").replace(/\r\n?/gu, "\n").split("\n");
29
+ if (lines[0]?.trim() !== "---")
30
+ throw new ConclaveDefError("bad_frontmatter", file, "the file must start with a --- frontmatter block");
31
+ const end = lines.findIndex((line, index) => index > 0 && line.trim() === "---");
32
+ if (end < 0)
33
+ throw new ConclaveDefError("bad_frontmatter", file, "the frontmatter block is not closed with ---");
34
+ const meta = new Map();
35
+ for (const [offset, line] of lines.slice(1, end).entries()) {
36
+ if (line.trim() === "" || line.trim().startsWith("#"))
37
+ continue;
38
+ const colon = line.indexOf(":");
39
+ if (colon <= 0)
40
+ throw new ConclaveDefError("bad_frontmatter", file, `frontmatter line ${offset + 2} is not "key: value"`);
41
+ const key = line.slice(0, colon).trim();
42
+ if (meta.has(key))
43
+ throw new ConclaveDefError("bad_frontmatter", file, `the key ${key} appears twice`);
44
+ meta.set(key, line.slice(colon + 1).trim().replace(/^(["'])(.*)\1$/u, "$2"));
45
+ }
46
+ return { meta, body: lines.slice(end + 1).join("\n").trim() };
47
+ }
48
+ function checkKeys(meta, allowed, file) {
49
+ for (const key of meta.keys()) {
50
+ if (key === "model")
51
+ throw new ConclaveDefError("model_key", file, "the model: key is not allowed; set the model in settings.json (conclave.defaultModel or conclave.members.<name>.model)");
52
+ if (!allowed.includes(key))
53
+ throw new ConclaveDefError("unknown_key", file, `unknown frontmatter key ${key}; allowed keys are ${allowed.join(", ")}`);
54
+ }
55
+ }
56
+ export function parseAgent(text, file) {
57
+ const { meta, body } = split(text, file);
58
+ checkKeys(meta, KEYS, file);
59
+ const name = basename(file).replace(/\.md$/iu, "");
60
+ const need = (key) => {
61
+ const value = meta.get(key);
62
+ if (value === undefined || value === "")
63
+ throw new ConclaveDefError("bad_value", file, `the ${key}: key is required`);
64
+ return value;
65
+ };
66
+ if (need("name") !== name)
67
+ throw new ConclaveDefError("bad_value", file, `name: ${meta.get("name")} does not match the file name ${name}`);
68
+ if (!CONCLAVE_MEMBERS.includes(name))
69
+ throw new ConclaveDefError("bad_value", file, `${name} is not a Conclave seat (${CONCLAVE_MEMBERS.join(", ")})`);
70
+ const thinking = meta.get("thinking");
71
+ if (thinking !== undefined && !THINKING_LEVELS.includes(thinking))
72
+ throw new ConclaveDefError("bad_value", file, `thinking: must be one of ${THINKING_LEVELS.join(", ")}`);
73
+ const tools = need("tools").replace(/^\[(.*)\]$/u, "$1").split(",").map((tool) => tool.trim()).filter((tool) => tool !== "");
74
+ for (const tool of tools)
75
+ if (!KNOWN_TOOLS.includes(tool))
76
+ throw new ConclaveDefError("unknown_tool", file, `unknown tool ${tool}; known tools are ${KNOWN_TOOLS.join(", ")}`);
77
+ if (name !== "vesperia")
78
+ for (const tool of tools)
79
+ if (WRITER_TOOLS.includes(tool))
80
+ throw new ConclaveDefError("reviewer_writes", file, `${name} is read-only and may not list ${tool}`);
81
+ if (body === "")
82
+ throw new ConclaveDefError("bad_value", file, "the file has no instructions after the frontmatter");
83
+ return { name, displayName: need("display_name"), description: need("description"), ...(thinking === undefined ? {} : { thinking }), tools, body, file };
84
+ }
85
+ /** All eight agents and the skill. Throws ConclaveDefError for the first problem found. */
86
+ export function loadDefinitions(root = REPO_ROOT) {
87
+ const skillFile = join(root, "skills", "conclave", "SKILL.md");
88
+ if (!existsSync(skillFile))
89
+ throw new ConclaveDefError("missing_skill", "skills/conclave/SKILL.md", "the conclave skill is missing");
90
+ const skillParts = split(readFileSync(skillFile, "utf8"), "skills/conclave/SKILL.md");
91
+ checkKeys(skillParts.meta, SKILL_KEYS, "skills/conclave/SKILL.md");
92
+ if (skillParts.body === "")
93
+ throw new ConclaveDefError("missing_skill", "skills/conclave/SKILL.md", "the conclave skill is empty");
94
+ const agents = new Map();
95
+ for (const name of CONCLAVE_MEMBERS) {
96
+ const file = join(root, "agents", `${name}.md`);
97
+ const label = `agents/${name}.md`;
98
+ if (!existsSync(file))
99
+ throw new ConclaveDefError("missing_file", label, "the agent file is missing");
100
+ agents.set(name, parseAgent(readFileSync(file, "utf8"), label));
101
+ }
102
+ return { agents, skill: skillParts.body };
103
+ }
104
+ /** The instructions a member runs with: its own text, then the shared skill. */
105
+ export const instructionsFor = (agent, skill) => `${agent.body}\n\n${skill}`;
106
+ /** Find a seat by its name or the first word of its display name, ignoring case. */
107
+ export function findAgent(agents, query) {
108
+ const key = query.trim().toLowerCase();
109
+ for (const agent of agents.values())
110
+ if (agent.name === key || agent.displayName.split(/\s+/u)[0].toLowerCase() === key)
111
+ return agent;
112
+ return undefined;
113
+ }