@oyasmi/pipiclaw 0.9.2 → 0.9.3-beta.2

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 (178) hide show
  1. package/README.en.md +250 -0
  2. package/README.md +3 -1
  3. package/dist/agent/channel-runner.d.ts +59 -2
  4. package/dist/agent/channel-runner.js +177 -76
  5. package/dist/agent/job-manager.d.ts +76 -21
  6. package/dist/agent/job-manager.js +447 -63
  7. package/dist/agent/maintenance-context.js +4 -5
  8. package/dist/agent/prompt/manifest.d.ts +0 -2
  9. package/dist/agent/prompt/manifest.js +2 -3
  10. package/dist/agent/prompt/sections.js +3 -3
  11. package/dist/agent/session-events.js +4 -8
  12. package/dist/agent/turn-prompt.d.ts +7 -7
  13. package/dist/agent/turn-prompt.js +6 -9
  14. package/dist/agent/types.d.ts +12 -0
  15. package/dist/agent/types.js +1 -0
  16. package/dist/channel/channel-context.d.ts +13 -0
  17. package/dist/channel/channel-context.js +21 -1
  18. package/dist/commands/catalog.js +47 -20
  19. package/dist/memory/commands.d.ts +8 -0
  20. package/dist/memory/commands.js +113 -148
  21. package/dist/memory/index-budget.d.ts +27 -0
  22. package/dist/memory/index-budget.js +99 -0
  23. package/dist/memory/journal.d.ts +20 -0
  24. package/dist/memory/journal.js +78 -0
  25. package/dist/memory/lifecycle.d.ts +9 -15
  26. package/dist/memory/lifecycle.js +55 -145
  27. package/dist/memory/maintenance-gates.d.ts +13 -47
  28. package/dist/memory/maintenance-gates.js +13 -92
  29. package/dist/memory/maintenance-state.d.ts +10 -16
  30. package/dist/memory/maintenance-state.js +12 -59
  31. package/dist/memory/maintenance-tuning.d.ts +7 -27
  32. package/dist/memory/maintenance-tuning.js +11 -28
  33. package/dist/memory/migrate.d.ts +15 -0
  34. package/dist/memory/migrate.js +364 -0
  35. package/dist/memory/reflect-job.d.ts +43 -0
  36. package/dist/memory/reflect-job.js +117 -0
  37. package/dist/memory/reflect.d.ts +52 -0
  38. package/dist/memory/reflect.js +313 -0
  39. package/dist/memory/render.d.ts +19 -0
  40. package/dist/memory/render.js +35 -0
  41. package/dist/memory/review-log.d.ts +2 -1
  42. package/dist/memory/scheduler.d.ts +2 -3
  43. package/dist/memory/scheduler.js +14 -49
  44. package/dist/memory/search.d.ts +35 -0
  45. package/dist/memory/search.js +297 -0
  46. package/dist/memory/session-search.js +1 -1
  47. package/dist/memory/store.d.ts +108 -0
  48. package/dist/memory/store.js +418 -0
  49. package/dist/memory/task-digest.d.ts +2 -2
  50. package/dist/memory/task-digest.js +29 -36
  51. package/dist/memory/tombstones.d.ts +0 -11
  52. package/dist/memory/tombstones.js +6 -37
  53. package/dist/memory/transcript.js +12 -5
  54. package/dist/playbooks/agent-delegation.md +37 -9
  55. package/dist/playbooks/background-jobs.md +1 -1
  56. package/dist/playbooks/event-scheduling.md +6 -2
  57. package/dist/playbooks/memory-and-learning.md +36 -12
  58. package/dist/playbooks/outbound-media.md +1 -1
  59. package/dist/playbooks/runtime-orientation.md +9 -8
  60. package/dist/playbooks/task-loop.md +110 -0
  61. package/dist/runtime/bootstrap.d.ts +3 -3
  62. package/dist/runtime/bootstrap.js +66 -41
  63. package/dist/runtime/events.d.ts +9 -0
  64. package/dist/runtime/events.js +43 -4
  65. package/dist/runtime/task-commands.d.ts +14 -15
  66. package/dist/runtime/task-commands.js +249 -485
  67. package/dist/runtime/task-driver.d.ts +41 -29
  68. package/dist/runtime/task-driver.js +170 -317
  69. package/dist/runtime/task-migration.d.ts +34 -30
  70. package/dist/runtime/task-migration.js +229 -183
  71. package/dist/runtime/task-wake.d.ts +6 -3
  72. package/dist/runtime/task-wake.js +26 -19
  73. package/dist/settings.d.ts +0 -29
  74. package/dist/settings.js +12 -35
  75. package/dist/shared/markdown-sections.d.ts +0 -1
  76. package/dist/shared/markdown-sections.js +0 -3
  77. package/dist/subagents/discovery.d.ts +2 -2
  78. package/dist/subagents/discovery.js +21 -10
  79. package/dist/subagents/external/progress-tail.js +6 -1
  80. package/dist/subagents/external/run.js +1 -0
  81. package/dist/subagents/runs.d.ts +19 -2
  82. package/dist/subagents/runs.js +73 -4
  83. package/dist/subagents/tool.d.ts +10 -8
  84. package/dist/subagents/tool.js +55 -94
  85. package/dist/tasks/brief.d.ts +17 -0
  86. package/dist/tasks/brief.js +57 -0
  87. package/dist/tasks/budget.d.ts +26 -0
  88. package/dist/tasks/budget.js +59 -0
  89. package/dist/tasks/cycle.d.ts +48 -0
  90. package/dist/tasks/cycle.js +94 -0
  91. package/dist/tasks/frontmatter.d.ts +73 -0
  92. package/dist/tasks/frontmatter.js +175 -0
  93. package/dist/tasks/ledger.d.ts +66 -106
  94. package/dist/tasks/ledger.js +90 -546
  95. package/dist/tasks/log.d.ts +88 -0
  96. package/dist/tasks/log.js +97 -0
  97. package/dist/tasks/rounds.d.ts +41 -0
  98. package/dist/tasks/rounds.js +53 -0
  99. package/dist/tasks/session-path.d.ts +9 -0
  100. package/dist/tasks/session-path.js +16 -0
  101. package/dist/tasks/steer.d.ts +8 -0
  102. package/dist/tasks/steer.js +79 -0
  103. package/dist/tasks/store.d.ts +60 -35
  104. package/dist/tasks/store.js +166 -107
  105. package/dist/tasks/ticket.d.ts +102 -0
  106. package/dist/tasks/ticket.js +209 -0
  107. package/dist/tasks/verification.d.ts +47 -0
  108. package/dist/tasks/verification.js +60 -1
  109. package/dist/tools/index.d.ts +13 -4
  110. package/dist/tools/index.js +5 -10
  111. package/dist/tools/memory-manage.d.ts +6 -16
  112. package/dist/tools/memory-manage.js +108 -129
  113. package/dist/tools/presentation.js +2 -1
  114. package/dist/tools/registry.d.ts +11 -2
  115. package/dist/tools/registry.js +17 -6
  116. package/dist/tools/subagent-manage.d.ts +0 -9
  117. package/dist/tools/subagent-manage.js +8 -17
  118. package/dist/tools/task-manage/create.js +2 -2
  119. package/dist/tools/task-manage/lifecycle.d.ts +9 -4
  120. package/dist/tools/task-manage/lifecycle.js +136 -188
  121. package/dist/tools/task-manage/log-view.d.ts +8 -0
  122. package/dist/tools/task-manage/log-view.js +24 -0
  123. package/dist/tools/task-manage/schema.d.ts +38 -14
  124. package/dist/tools/task-manage/schema.js +46 -27
  125. package/dist/tools/task-manage/shared.d.ts +35 -31
  126. package/dist/tools/task-manage/shared.js +130 -141
  127. package/dist/tools/task-manage/step-end.d.ts +11 -0
  128. package/dist/tools/task-manage/step-end.js +134 -0
  129. package/dist/tools/task-manage/types.d.ts +23 -32
  130. package/dist/tools/task-manage.d.ts +4 -3
  131. package/dist/tools/task-manage.js +30 -15
  132. package/dist/tools/tool-details.d.ts +1 -1
  133. package/dist/tui/app.js +0 -2
  134. package/examples/sub-agents/README.md +54 -38
  135. package/examples/sub-agents/builder-hard.md +21 -12
  136. package/examples/sub-agents/builder.md +24 -16
  137. package/examples/sub-agents/explorer.md +17 -15
  138. package/examples/sub-agents/git-committer.md +8 -6
  139. package/examples/sub-agents/planner.md +17 -16
  140. package/examples/sub-agents/reviewer.md +11 -9
  141. package/examples/sub-agents/verifier.md +22 -16
  142. package/examples/sub-agents/worker.md +22 -6
  143. package/package.json +1 -1
  144. package/dist/agent/effect-ledger.d.ts +0 -32
  145. package/dist/agent/effect-ledger.js +0 -89
  146. package/dist/memory/bootstrap.d.ts +0 -17
  147. package/dist/memory/bootstrap.js +0 -120
  148. package/dist/memory/candidates.d.ts +0 -26
  149. package/dist/memory/candidates.js +0 -202
  150. package/dist/memory/consolidation.d.ts +0 -54
  151. package/dist/memory/consolidation.js +0 -292
  152. package/dist/memory/extraction.d.ts +0 -54
  153. package/dist/memory/extraction.js +0 -240
  154. package/dist/memory/files.d.ts +0 -71
  155. package/dist/memory/files.js +0 -419
  156. package/dist/memory/maintenance-jobs.d.ts +0 -41
  157. package/dist/memory/maintenance-jobs.js +0 -265
  158. package/dist/memory/metadata.d.ts +0 -57
  159. package/dist/memory/metadata.js +0 -142
  160. package/dist/memory/probation.d.ts +0 -17
  161. package/dist/memory/probation.js +0 -44
  162. package/dist/memory/promotion.d.ts +0 -38
  163. package/dist/memory/promotion.js +0 -36
  164. package/dist/memory/recall.d.ts +0 -59
  165. package/dist/memory/recall.js +0 -746
  166. package/dist/memory/session.d.ts +0 -24
  167. package/dist/memory/session.js +0 -273
  168. package/dist/playbooks/task-driving.md +0 -92
  169. package/dist/playbooks/task-planning.md +0 -58
  170. package/dist/tasks/control.d.ts +0 -56
  171. package/dist/tasks/control.js +0 -169
  172. package/dist/tasks/transitions.d.ts +0 -16
  173. package/dist/tasks/transitions.js +0 -48
  174. package/dist/tools/task-manage/verification.d.ts +0 -19
  175. package/dist/tools/task-manage/verification.js +0 -91
  176. package/examples/sub-agents/documenter.md +0 -50
  177. package/examples/sub-agents/log-sifter.md +0 -47
  178. package/examples/sub-agents/scout.md +0 -34
@@ -1,120 +0,0 @@
1
- import { splitH2Sections } from "../shared/markdown-sections.js";
2
- import { clipTextByPromptUnits, countPromptUnits } from "../shared/prompt-units.js";
3
- import { stripCrAndTrim } from "../shared/text-utils.js";
4
- import { buildMemoryCandidateId } from "./candidates.js";
5
- import { parseChannelMemoryEntries } from "./files.js";
6
- const FIRST_TURN_MEMORY_SNAPSHOT_MAX_CHARS = 3_000;
7
- /**
8
- * Automatic-context share for the first-turn durable snapshot (spec 026 §5.3).
9
- * It is a fallback, not a full memory dump: turn-specific recall carries the rest.
10
- */
11
- export const FIRST_TURN_BOOTSTRAP_MAX_UNITS = 400;
12
- const MIN_SECTION_CHARS = 600;
13
- const MIN_SECTION_UNITS = 100;
14
- const CHANNEL_MEMORY_WEIGHT = 0.6;
15
- const ZERO_BUDGET = { chars: 0, units: 0 };
16
- function hasVisibleContent(content) {
17
- return content.replace(/<!--[\s\S]*?-->/g, "").trim().length > 0;
18
- }
19
- /** Keep `text` within both the char and unit ceilings, head/tail (or head-only when headRatio ≥ 1). */
20
- function clipToBudget(text, budget, headRatio) {
21
- return clipTextByPromptUnits(text, budget.units, {
22
- headRatio,
23
- maxChars: budget.chars,
24
- marker: "\n\n[... omitted for length ...]\n\n",
25
- }).text;
26
- }
27
- // Channel MEMORY.md is "structured sections up top, `## Update <ts>` blocks appended
28
- // at the tail". A plain head clip would drop the newest facts, so pick sections by
29
- // priority (structured first, then newest Update blocks) but render in document order
30
- // for readability. A section is kept only if it fits both the char and unit budget.
31
- function selectChannelMemoryForBootstrap(content, budget) {
32
- const sections = splitH2Sections(content).filter((section) => hasVisibleContent(section.content));
33
- if (sections.length === 0) {
34
- return clipToBudget(content, budget, 1);
35
- }
36
- const renderSection = (section) => `## ${section.heading}\n\n${section.content}`;
37
- const structured = sections.filter((section) => !section.heading.startsWith("Update "));
38
- const updatesNewestFirst = sections.filter((section) => section.heading.startsWith("Update ")).reverse();
39
- const byPriority = [...structured, ...updatesNewestFirst];
40
- const chosen = new Set();
41
- let usedChars = 0;
42
- let usedUnits = 0;
43
- for (const section of byPriority) {
44
- const text = renderSection(section);
45
- const separatorChars = chosen.size > 0 ? 2 : 0;
46
- const costChars = separatorChars + text.length;
47
- const costUnits = countPromptUnits(text); // section separators are whitespace → 0 units
48
- if (usedChars + costChars <= budget.chars && usedUnits + costUnits <= budget.units) {
49
- chosen.add(section);
50
- usedChars += costChars;
51
- usedUnits += costUnits;
52
- }
53
- }
54
- if (chosen.size === 0) {
55
- return clipToBudget(renderSection(byPriority[0]), budget, 0.5);
56
- }
57
- return sections
58
- .filter((section) => chosen.has(section))
59
- .map(renderSection)
60
- .join("\n\n");
61
- }
62
- /** Split the total budget across channel (60%) and workspace memory in both dimensions. */
63
- function splitBudget(max) {
64
- const channelChars = Math.max(MIN_SECTION_CHARS, Math.floor(max.chars * CHANNEL_MEMORY_WEIGHT));
65
- const workspaceChars = Math.max(MIN_SECTION_CHARS, max.chars - channelChars);
66
- const channelUnits = Math.max(MIN_SECTION_UNITS, Math.floor(max.units * CHANNEL_MEMORY_WEIGHT));
67
- const workspaceUnits = Math.max(MIN_SECTION_UNITS, max.units - channelUnits);
68
- const channel = {
69
- chars: channelChars + Math.max(0, max.chars - channelChars - workspaceChars),
70
- units: channelUnits + Math.max(0, max.units - channelUnits - workspaceUnits),
71
- };
72
- return [channel, { chars: workspaceChars, units: workspaceUnits }];
73
- }
74
- function allocateBudgets(channelMemory, workspaceMemory, max) {
75
- if (!channelMemory && !workspaceMemory) {
76
- return [ZERO_BUDGET, ZERO_BUDGET];
77
- }
78
- if (!channelMemory) {
79
- return [ZERO_BUDGET, max];
80
- }
81
- if (!workspaceMemory) {
82
- return [max, ZERO_BUDGET];
83
- }
84
- return splitBudget(max);
85
- }
86
- export function buildFirstTurnMemoryBootstrap(options) {
87
- return buildFirstTurnMemoryBootstrapResult(options).renderedText;
88
- }
89
- export function buildFirstTurnMemoryBootstrapResult(options) {
90
- const max = {
91
- chars: options.maxChars ?? FIRST_TURN_MEMORY_SNAPSHOT_MAX_CHARS,
92
- units: options.maxUnits ?? FIRST_TURN_BOOTSTRAP_MAX_UNITS,
93
- };
94
- const channelMemory = stripCrAndTrim(options.channelMemory);
95
- const workspaceMemory = stripCrAndTrim(options.workspaceMemory);
96
- if (!channelMemory && !workspaceMemory) {
97
- return { renderedText: "", includedCandidateIds: [] };
98
- }
99
- const [channelBudget, workspaceBudget] = allocateBudgets(channelMemory, workspaceMemory, max);
100
- const sections = [
101
- "<durable_memory_snapshot>",
102
- "Durable memory bootstrap for the first user turn in this session.",
103
- "Use it as background context together with any turn-specific recalled snippets.",
104
- ];
105
- const includedCandidateIds = [];
106
- if (channelMemory) {
107
- const selectedChannelMemory = channelBudget.chars > 0 ? selectChannelMemoryForBootstrap(channelMemory, channelBudget) : channelMemory;
108
- sections.push("", "[Channel MEMORY.md]");
109
- sections.push(selectedChannelMemory);
110
- includedCandidateIds.push(...parseChannelMemoryEntries(selectedChannelMemory).map((entry) => entry.id));
111
- }
112
- if (workspaceMemory) {
113
- const selectedWorkspaceMemory = workspaceBudget.chars > 0 ? clipToBudget(workspaceMemory, workspaceBudget, 1) : workspaceMemory;
114
- sections.push("", "[Workspace MEMORY.md]");
115
- sections.push(selectedWorkspaceMemory);
116
- includedCandidateIds.push(...splitH2Sections(selectedWorkspaceMemory).map((section) => buildMemoryCandidateId("workspace-memory", section.heading)));
117
- }
118
- sections.push("</durable_memory_snapshot>");
119
- return { renderedText: sections.join("\n"), includedCandidateIds };
120
- }
@@ -1,26 +0,0 @@
1
- export interface MemoryCandidate {
2
- id: string;
3
- source: "workspace-memory" | "channel-memory" | "channel-session" | "channel-history";
4
- path: string;
5
- title: string;
6
- content: string;
7
- searchText?: string;
8
- timestamp?: string;
9
- sectionKind?: string;
10
- priority: number;
11
- entryId?: string;
12
- }
13
- export interface BuildMemoryCandidatesOptions {
14
- workspaceDir: string;
15
- channelDir: string;
16
- }
17
- export declare function buildMemoryCandidateId(source: MemoryCandidate["source"], title: string, timestamp?: string): string;
18
- export declare class MemoryCandidateStore {
19
- private files;
20
- private inflight;
21
- invalidate(path?: string): void;
22
- getCandidates(options: BuildMemoryCandidatesOptions): Promise<MemoryCandidate[]>;
23
- private loadFileCandidates;
24
- }
25
- export declare function createMemoryCandidateStore(): MemoryCandidateStore;
26
- export declare function buildMemoryCandidates(options: BuildMemoryCandidatesOptions, store?: MemoryCandidateStore): Promise<MemoryCandidate[]>;
@@ -1,202 +0,0 @@
1
- import { stat } from "fs/promises";
2
- import { join } from "path";
3
- import { readOptionalTextFile } from "../shared/fs-utils.js";
4
- import { splitH1Sections, splitH2Sections } from "../shared/markdown-sections.js";
5
- import { stripCrAndTrim } from "../shared/text-utils.js";
6
- import { getChannelHistoryPath, getChannelMemoryPath, getChannelSessionPath, parseChannelMemoryEntries, parseUpdateHeadingTimestamp, } from "./files.js";
7
- async function readOptionalFile(path) {
8
- return stripCrAndTrim(await readOptionalTextFile(path));
9
- }
10
- function slugify(value) {
11
- return (value
12
- .toLowerCase()
13
- .replace(/[^a-z0-9]+/g, "-")
14
- .replace(/^-+|-+$/g, "") || "section");
15
- }
16
- function sameFingerprint(a, b) {
17
- return a.exists === b.exists && a.mtimeMs === b.mtimeMs && a.ctimeMs === b.ctimeMs && a.size === b.size;
18
- }
19
- function inferPriority(source, title) {
20
- const normalizedTitle = title.trim().toLowerCase();
21
- if (source === "channel-session") {
22
- if (normalizedTitle === "current state")
23
- return 18;
24
- if (normalizedTitle === "next steps")
25
- return 17;
26
- if (normalizedTitle === "errors & corrections")
27
- return 16;
28
- if (normalizedTitle === "constraints")
29
- return 15;
30
- if (normalizedTitle === "user intent")
31
- return 15;
32
- if (normalizedTitle === "active files")
33
- return 14;
34
- if (normalizedTitle === "decisions")
35
- return 14;
36
- if (normalizedTitle === "session title")
37
- return 13;
38
- return 12;
39
- }
40
- if (source === "channel-memory") {
41
- if (normalizedTitle.includes("constraints"))
42
- return 11;
43
- if (normalizedTitle.includes("decisions"))
44
- return 10;
45
- if (normalizedTitle.includes("open loops"))
46
- return 10;
47
- if (normalizedTitle.includes("preferences"))
48
- return 9;
49
- if (normalizedTitle.includes("ongoing work"))
50
- return 9;
51
- return 8;
52
- }
53
- if (source === "workspace-memory") {
54
- return 6;
55
- }
56
- return 4;
57
- }
58
- const ENTRY_ID_COMMENT_GLOBAL = /\s*<!--\s*id:m-[a-z0-9]+\s*-->/gi;
59
- function stripEntryIdComments(text) {
60
- return text.replace(ENTRY_ID_COMMENT_GLOBAL, "");
61
- }
62
- export function buildMemoryCandidateId(source, title, timestamp) {
63
- return `${source}:${slugify(title)}:${timestamp ?? ""}`;
64
- }
65
- function buildCandidate(source, path, title, content, timestamp, searchText, id, entryId) {
66
- const cleanContent = stripEntryIdComments(content);
67
- return {
68
- id: id ?? buildMemoryCandidateId(source, title, timestamp),
69
- source,
70
- path,
71
- title,
72
- content: cleanContent,
73
- searchText: searchText === undefined ? undefined : stripEntryIdComments(searchText),
74
- timestamp,
75
- sectionKind: title.trim().toLowerCase(),
76
- priority: inferPriority(source, title),
77
- entryId,
78
- };
79
- }
80
- function buildWorkspaceOrChannelMemoryCandidates(source, path, content) {
81
- if (source === "channel-memory") {
82
- const entries = parseChannelMemoryEntries(content);
83
- if (entries.length > 0) {
84
- return entries.map((entry) => buildCandidate(source, path, entry.sectionHeading, entry.content, entry.timestamp, entry.content, entry.id, entry.id));
85
- }
86
- }
87
- const sections = splitH2Sections(content);
88
- if (sections.length === 0 && content) {
89
- return [
90
- buildCandidate(source, path, source === "workspace-memory" ? "Workspace Memory" : "Channel Memory", content),
91
- ];
92
- }
93
- return sections
94
- .filter((section) => section.content.trim())
95
- .map((section) => {
96
- // `## Update <ISO>` blocks carry their write time in the heading; surfacing it
97
- // lets recall apply a recency boost and keeps appended-block ids distinct.
98
- const timestamp = source === "channel-memory" ? parseUpdateHeadingTimestamp(section.heading) : undefined;
99
- return buildCandidate(source, path, section.heading, section.content, timestamp);
100
- });
101
- }
102
- function buildSessionCandidates(path, content) {
103
- const sections = splitH1Sections(content).filter((section) => section.content.trim());
104
- const sessionTitle = sections.find((section) => section.heading.toLowerCase() === "session title")?.content ?? "";
105
- return sections.map((section) => buildCandidate("channel-session", path, section.heading, section.content, undefined, section.heading.toLowerCase() === "session title" || !sessionTitle.trim()
106
- ? section.content
107
- : `${sessionTitle.trim()}\n${section.content}`));
108
- }
109
- function buildHistoryCandidates(path, content) {
110
- const sections = splitH2Sections(content).filter((section) => section.content.trim());
111
- if (sections.length === 0) {
112
- return [];
113
- }
114
- const foldedSections = sections.filter((section) => section.heading.startsWith("Folded History Through "));
115
- const recentSectionLimit = 8;
116
- const recentSections = sections.slice(-recentSectionLimit);
117
- const selectedSections = Array.from(new Set([...foldedSections, ...recentSections]));
118
- return selectedSections.map((section) => buildCandidate("channel-history", path, section.heading, section.content, section.heading));
119
- }
120
- async function readFingerprint(path) {
121
- try {
122
- const stats = await stat(path);
123
- return {
124
- exists: true,
125
- mtimeMs: stats.mtimeMs,
126
- ctimeMs: stats.ctimeMs,
127
- size: stats.size,
128
- };
129
- }
130
- catch {
131
- return {
132
- exists: false,
133
- mtimeMs: 0,
134
- ctimeMs: 0,
135
- size: 0,
136
- };
137
- }
138
- }
139
- export class MemoryCandidateStore {
140
- constructor() {
141
- this.files = new Map();
142
- this.inflight = new Map();
143
- }
144
- invalidate(path) {
145
- if (!path) {
146
- this.files.clear();
147
- this.inflight.clear();
148
- return;
149
- }
150
- this.files.delete(path);
151
- this.inflight.delete(path);
152
- }
153
- async getCandidates(options) {
154
- const definitions = [
155
- {
156
- path: getChannelSessionPath(options.channelDir),
157
- build: buildSessionCandidates,
158
- },
159
- {
160
- path: getChannelMemoryPath(options.channelDir),
161
- build: (path, content) => buildWorkspaceOrChannelMemoryCandidates("channel-memory", path, content),
162
- },
163
- {
164
- path: join(options.workspaceDir, "MEMORY.md"),
165
- build: (path, content) => buildWorkspaceOrChannelMemoryCandidates("workspace-memory", path, content),
166
- },
167
- {
168
- path: getChannelHistoryPath(options.channelDir),
169
- build: buildHistoryCandidates,
170
- },
171
- ];
172
- const candidateGroups = await Promise.all(definitions.map(async (definition) => this.loadFileCandidates(definition.path, definition.build)));
173
- return candidateGroups.flat();
174
- }
175
- async loadFileCandidates(path, build) {
176
- const pending = this.inflight.get(path);
177
- if (pending) {
178
- return pending;
179
- }
180
- const work = (async () => {
181
- const fingerprint = await readFingerprint(path);
182
- const cached = this.files.get(path);
183
- if (cached && sameFingerprint(cached.fingerprint, fingerprint)) {
184
- return cached.candidates;
185
- }
186
- const content = fingerprint.exists ? await readOptionalFile(path) : "";
187
- const candidates = build(path, content);
188
- this.files.set(path, { fingerprint, candidates });
189
- return candidates;
190
- })().finally(() => {
191
- this.inflight.delete(path);
192
- });
193
- this.inflight.set(path, work);
194
- return work;
195
- }
196
- }
197
- export function createMemoryCandidateStore() {
198
- return new MemoryCandidateStore();
199
- }
200
- export async function buildMemoryCandidates(options, store = createMemoryCandidateStore()) {
201
- return store.getCandidates(options);
202
- }
@@ -1,54 +0,0 @@
1
- import type { AgentMessage } from "@earendil-works/pi-agent-core";
2
- import type { Api, Model } from "@earendil-works/pi-ai";
3
- import { type SessionEntry } from "@earendil-works/pi-coding-agent";
4
- import type { PipiclawMemoryMaintenanceSettings } from "../settings.js";
5
- import { type MemoryPromotionCandidate } from "./promotion.js";
6
- import type { MemorySourceWindow } from "./source-window.js";
7
- export type ConsolidationMode = "idle" | "boundary";
8
- export interface ConsolidationRunOptions {
9
- channelId?: string;
10
- channelDir: string;
11
- model: Model<Api>;
12
- resolveApiKey: (model: Model<Api>) => Promise<string>;
13
- messages: AgentMessage[];
14
- sessionEntries?: SessionEntry[];
15
- sourceWindow?: MemorySourceWindow;
16
- mode?: ConsolidationMode;
17
- usageCorrelationId?: string;
18
- /** Durable-write bar; defaults to the same threshold the growth review uses. */
19
- minAutoWriteConfidence?: number;
20
- }
21
- export interface InlineConsolidationResult {
22
- skipped: boolean;
23
- appendedMemoryEntries: number;
24
- /**
25
- * Ops sent per tier (spec 037, D6), for review-log breakdown. Pre-application tallies — like
26
- * `rejectedMemoryOps`, these describe what the gate decided, not what ultimately landed after
27
- * dedup/tombstone checks inside `applyChannelMemoryOps`.
28
- */
29
- appendedDurableEntries: number;
30
- appendedProbationaryEntries: number;
31
- appendedHistoryBlock: boolean;
32
- /** Candidates the confidence gate turned down (including probation over the per-run cap), surfaced for the review log. */
33
- rejectedMemoryOps: MemoryPromotionCandidate[];
34
- }
35
- export interface StructuralMaintenanceStats {
36
- memoryCleanupNeeded: boolean;
37
- historyFoldingNeeded: boolean;
38
- hasMemoryContent: boolean;
39
- hasHistoryContent: boolean;
40
- }
41
- export declare function shouldCleanupChannelMemory(currentMemory: string): boolean;
42
- export declare function shouldFoldChannelHistory(currentHistory: string): boolean;
43
- export declare function getStructuralMaintenanceStats(currentMemory: string, currentHistory: string): StructuralMaintenanceStats;
44
- export declare function runInlineConsolidation(options: ConsolidationRunOptions): Promise<InlineConsolidationResult>;
45
- export type MemoryCleanupShrinkGuard = Pick<PipiclawMemoryMaintenanceSettings, "cleanupShrinkGuardMinRatio" | "cleanupShrinkGuardMinChars">;
46
- export declare class MemoryCleanupRejectedError extends Error {
47
- constructor(reason: string);
48
- }
49
- export interface MemoryCleanupResult {
50
- rewritten: boolean;
51
- droppedEntryIds: string[];
52
- }
53
- export declare function cleanupChannelMemory(options: ConsolidationRunOptions, currentMemory: string, guard?: MemoryCleanupShrinkGuard): Promise<MemoryCleanupResult>;
54
- export declare function foldChannelHistory(options: ConsolidationRunOptions, currentHistory: string): Promise<boolean>;