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,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native history decoder. This lane owns nothing but the host boundary: it allocates the stable
|
|
3
|
+
* file-order seq, selects the active branch, and decodes session entries into typed facts. Pairing,
|
|
4
|
+
* bounding, and the readable projection belong to the shared domain in ../history/history.ts.
|
|
5
|
+
*
|
|
6
|
+
* The address contract is deliberate and unchanged in spirit: a seq is allocated from entry kind and
|
|
7
|
+
* role alone, never from visible content, so appending or branching never renumbers an existing
|
|
8
|
+
* address. What changed is that a result folded into a pairing no longer doubles as a second
|
|
9
|
+
* readable address for its call.
|
|
10
|
+
*/
|
|
11
|
+
import type { ToolCall } from "@earendil-works/pi-ai";
|
|
12
|
+
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
13
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import type { SessionReader } from "./session-reader.js";
|
|
15
|
+
import { isWindowMarker, rootWindowId } from "./window.js";
|
|
16
|
+
import { contentText, projectHistory, type DecodedHistoryItem, type DecodedHistoryWindow, type HistoryProjection, type NestedCallRecord, type ToolCallStatus } from "../history/history.js";
|
|
17
|
+
|
|
18
|
+
function mapRole(role: AgentMessage["role"]): DecodedHistoryItem["role"] | undefined {
|
|
19
|
+
if (role === "user" || role === "assistant") return role;
|
|
20
|
+
if (role === "toolResult" || role === "bashExecution") return "tool";
|
|
21
|
+
if (role === "custom") return "developer";
|
|
22
|
+
if (role === "compactionSummary" || role === "branchSummary") return "system";
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isToolCall(part: unknown): part is ToolCall {
|
|
27
|
+
return typeof part === "object" && part !== null && (part as { type?: unknown }).type === "toolCall";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function mappedMessage(message: AgentMessage): boolean {
|
|
31
|
+
return mapRole(message.role) !== undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Seq allocation depends on entry kind and role, never on visible content. */
|
|
35
|
+
function sequenceCount(entry: SessionEntry): number {
|
|
36
|
+
if (entry.type === "compaction" || entry.type === "branch_summary" || entry.type === "custom_message") return 1;
|
|
37
|
+
if (entry.type !== "message" || !mappedMessage(entry.message)) return 0;
|
|
38
|
+
if (entry.message.role !== "assistant" || !Array.isArray(entry.message.content)) return 1;
|
|
39
|
+
return 1 + entry.message.content.filter(isToolCall).length;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function messageContent(message: AgentMessage): string {
|
|
43
|
+
switch (message.role) {
|
|
44
|
+
case "bashExecution":
|
|
45
|
+
return message.output;
|
|
46
|
+
case "branchSummary":
|
|
47
|
+
case "compactionSummary":
|
|
48
|
+
return message.summary;
|
|
49
|
+
default:
|
|
50
|
+
return contentText(message.content);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Pass the host's nested-call record through as recorded. It is evidence about calls this run made,
|
|
56
|
+
* not a second transcript: the host stores no nested results, and this lane invents none.
|
|
57
|
+
*/
|
|
58
|
+
function nestedCallsOf(message: AgentMessage): { calls: NestedCallRecord[]; complete: boolean } | undefined {
|
|
59
|
+
if (message.role !== "toolResult" || message.nestedCalls === undefined) return undefined;
|
|
60
|
+
return { calls: message.nestedCalls.calls, complete: message.nestedCalls.complete };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function toolInfo(message: AgentMessage): Pick<DecodedHistoryItem, "toolName" | "arguments" | "outputTruncated" | "fullOutputPath" | "toolError" | "toolCallId" | "nestedCalls" | "recordedStatus"> {
|
|
64
|
+
if (message.role === "bashExecution") {
|
|
65
|
+
// A standalone bash run records its own outcome, so its status is read here rather than
|
|
66
|
+
// inferred from a result message that will never arrive: a cancelled or nonzero run is an
|
|
67
|
+
// error, and a run with no recorded exit code stays unknown instead of being called ok.
|
|
68
|
+
const status: ToolCallStatus = message.cancelled === true ? "error" : typeof message.exitCode === "number" ? (message.exitCode === 0 ? "ok" : "error") : "unfinished";
|
|
69
|
+
// A truncated run is only half the record without the on-disk path: surface both.
|
|
70
|
+
return { toolName: "bash", arguments: { command: message.command }, recordedStatus: status, outputTruncated: message.truncated || undefined, fullOutputPath: message.truncated ? message.fullOutputPath : undefined };
|
|
71
|
+
}
|
|
72
|
+
if (message.role !== "toolResult") return {};
|
|
73
|
+
const nestedCalls = nestedCallsOf(message);
|
|
74
|
+
return {
|
|
75
|
+
toolName: message.toolName,
|
|
76
|
+
toolCallId: message.toolCallId,
|
|
77
|
+
toolError: message.isError === true ? true : undefined,
|
|
78
|
+
...(nestedCalls ? { nestedCalls } : {}),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function toolCallItems(windowId: string, entry: { id: string; timestamp?: string }, message: AgentMessage, entrySeq: number): DecodedHistoryItem[] {
|
|
83
|
+
if (message.role !== "assistant" || !Array.isArray(message.content)) return [];
|
|
84
|
+
const items: DecodedHistoryItem[] = [];
|
|
85
|
+
let callIndex = 0;
|
|
86
|
+
for (const part of message.content) {
|
|
87
|
+
if (!isToolCall(part)) continue;
|
|
88
|
+
items.push({
|
|
89
|
+
seq: entrySeq + callIndex + 1,
|
|
90
|
+
windowId,
|
|
91
|
+
role: "tool_call",
|
|
92
|
+
text: "",
|
|
93
|
+
createdAt: entry.timestamp,
|
|
94
|
+
toolName: part.name,
|
|
95
|
+
toolCallId: part.id,
|
|
96
|
+
arguments: part.arguments,
|
|
97
|
+
});
|
|
98
|
+
callIndex += 1;
|
|
99
|
+
}
|
|
100
|
+
return items;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Build stable internal addresses, then project the active branch into public events. */
|
|
104
|
+
export function historyFromSession(ctx: SessionReader): HistoryProjection {
|
|
105
|
+
const seqByEntryId = new Map<string, number>();
|
|
106
|
+
let nextSeq = 1;
|
|
107
|
+
for (const entry of ctx.sessionManager.getEntries()) {
|
|
108
|
+
const count = sequenceCount(entry);
|
|
109
|
+
if (count === 0) continue;
|
|
110
|
+
seqByEntryId.set(entry.id, nextSeq);
|
|
111
|
+
nextSeq += count;
|
|
112
|
+
}
|
|
113
|
+
const highestSeq = nextSeq - 1;
|
|
114
|
+
|
|
115
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
116
|
+
let window: DecodedHistoryWindow = { windowId: rootWindowId(sessionId), items: [] };
|
|
117
|
+
const rawWindows = [window];
|
|
118
|
+
const branchSeqs = new Set<number>();
|
|
119
|
+
|
|
120
|
+
for (const entry of ctx.sessionManager.getBranch()) {
|
|
121
|
+
if (isWindowMarker(entry)) {
|
|
122
|
+
window = { windowId: entry.data.windowId, createdAt: entry.timestamp, items: [] };
|
|
123
|
+
rawWindows.push(window);
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const entrySeq = seqByEntryId.get(entry.id);
|
|
127
|
+
if (entrySeq === undefined) continue;
|
|
128
|
+
branchSeqs.add(entrySeq);
|
|
129
|
+
|
|
130
|
+
if (entry.type === "compaction" || entry.type === "branch_summary") {
|
|
131
|
+
window.items.push({ seq: entrySeq, windowId: window.windowId, role: "system", text: entry.summary, createdAt: entry.timestamp });
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (entry.type === "message") {
|
|
135
|
+
const role = mapRole(entry.message.role);
|
|
136
|
+
if (!role) continue;
|
|
137
|
+
window.items.push({
|
|
138
|
+
seq: entrySeq,
|
|
139
|
+
windowId: window.windowId,
|
|
140
|
+
role,
|
|
141
|
+
text: messageContent(entry.message),
|
|
142
|
+
createdAt: entry.timestamp,
|
|
143
|
+
...toolInfo(entry.message),
|
|
144
|
+
});
|
|
145
|
+
const calls = toolCallItems(window.windowId, entry, entry.message, entrySeq);
|
|
146
|
+
for (const call of calls) branchSeqs.add(call.seq);
|
|
147
|
+
window.items.push(...calls);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (entry.type === "custom_message") {
|
|
151
|
+
window.items.push({
|
|
152
|
+
seq: entrySeq,
|
|
153
|
+
windowId: window.windowId,
|
|
154
|
+
role: "developer",
|
|
155
|
+
text: contentText(entry.content),
|
|
156
|
+
createdAt: entry.timestamp,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return projectHistory(rawWindows, highestSeq, branchSeqs);
|
|
162
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import type { NotesIdentity } from "../../notes/identity.js";
|
|
5
|
+
import { projectKey, slugify } from "../../notes/paths.js";
|
|
6
|
+
|
|
7
|
+
/** Pi's default notes root; environment access stays on the host side of the library boundary. */
|
|
8
|
+
export function notesRoot(): string {
|
|
9
|
+
const override = process.env.PI_NOTES_HOME;
|
|
10
|
+
return override && override.length > 0 ? resolve(override) : join(homedir(), ".agents", "notes");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Translate the current Pi runtime into a fresh, explicit identity snapshot. */
|
|
14
|
+
export function agentSlug(_ctx: ExtensionContext): string {
|
|
15
|
+
return slugify(process.env.PI_NOTES_AGENT ?? "anonymous");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Pi's active model identity is resolved live so a mid-session switch retargets @model. */
|
|
19
|
+
export function modelSlug(ctx: ExtensionContext): string {
|
|
20
|
+
const id = ctx.model?.id;
|
|
21
|
+
return id ? slugify(id.split("/").pop() ?? id) : "default";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Translate the current Pi runtime into one explicit notes identity snapshot. */
|
|
25
|
+
export function notesIdentityFromPi(ctx: ExtensionContext, home = notesRoot()): NotesIdentity {
|
|
26
|
+
return {
|
|
27
|
+
home,
|
|
28
|
+
sessionId: ctx.sessionManager.getSessionId(),
|
|
29
|
+
projectKey: projectKey(ctx.cwd),
|
|
30
|
+
agent: agentSlug(ctx),
|
|
31
|
+
model: modelSlug(ctx),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { generateDiffString, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { notesWrite, notesUpdate, notesRead, notesList, notesSearch } from "../../tools/notes.js";
|
|
3
|
+
import { notesIdentityFromPi } from "./adapter.js";
|
|
4
|
+
import { NOTES_NAMESPACE, registerOperation } from "../tool-result.js";
|
|
5
|
+
import type { NoteChange } from "../../notes/store.js";
|
|
6
|
+
|
|
7
|
+
function renderDiff(change: NoteChange): string {
|
|
8
|
+
return change.kind === "none" ? "" : generateDiffString(change.before, change.after).diff;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Register the five shared note operations; each one supplies Pi's session identity and nothing else. */
|
|
12
|
+
export function registerNotesTools(pi: ExtensionAPI) {
|
|
13
|
+
registerOperation(pi, notesWrite, (params, ctx) => notesWrite.execute(params, notesIdentityFromPi(ctx)), NOTES_NAMESPACE);
|
|
14
|
+
registerOperation(pi, notesUpdate, (params, ctx) => notesUpdate.execute(params, notesIdentityFromPi(ctx), renderDiff), NOTES_NAMESPACE);
|
|
15
|
+
registerOperation(pi, notesRead, (params, ctx) => notesRead.execute(params, notesIdentityFromPi(ctx)), NOTES_NAMESPACE);
|
|
16
|
+
registerOperation(pi, notesList, (params, ctx) => notesList.execute(params, notesIdentityFromPi(ctx)), NOTES_NAMESPACE);
|
|
17
|
+
registerOperation(pi, notesSearch, (params, ctx) => notesSearch.execute(params, notesIdentityFromPi(ctx)), NOTES_NAMESPACE);
|
|
18
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { ExtensionAPI, ExtensionContext, SessionBoundaryDraft, SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { BOOT_TYPE, CONTINUATION_TYPE, RESET_MARKER_TYPE, RESET_PROMPT_TYPE } from "../entries.js";
|
|
4
|
+
import { CONTINUATION } from "./text.js";
|
|
5
|
+
import { currentWindowId, isWindowBootEntry, isWindowMarker, previousWindowId, type WindowMarker } from "../window.js";
|
|
6
|
+
import { buildBootMessage, sendBoot, type IncompleteNotesNotifier } from "../boot.js";
|
|
7
|
+
|
|
8
|
+
export type ResetTailState = { readonly boot: boolean; readonly continuation: boolean };
|
|
9
|
+
|
|
10
|
+
/** Match the hidden continuation entry that carries the one reset message. */
|
|
11
|
+
export function isWindowContinuationEntry(entry: SessionEntry): boolean {
|
|
12
|
+
return entry.type === "custom_message" && entry.customType === CONTINUATION_TYPE;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The single continuation sender: the only reset prose persisted for a window. */
|
|
16
|
+
export function sendContinuation(pi: ExtensionAPI): void {
|
|
17
|
+
pi.sendMessage(
|
|
18
|
+
{ customType: CONTINUATION_TYPE, content: CONTINUATION, display: false },
|
|
19
|
+
{ triggerTurn: false },
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The closed, ordered reset shape: retain-none native checkpoint, marker, matching boot,
|
|
25
|
+
* continuation, then an optional command prompt. The boot snapshot is acquired asynchronously after capturing this identity.
|
|
26
|
+
*/
|
|
27
|
+
export async function buildResetDrafts(
|
|
28
|
+
ctx: ExtensionContext,
|
|
29
|
+
notifyIncompleteNotes?: IncompleteNotesNotifier,
|
|
30
|
+
isCurrent: () => boolean = () => true,
|
|
31
|
+
prompt?: string,
|
|
32
|
+
): Promise<SessionBoundaryDraft[]> {
|
|
33
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
34
|
+
const previousId = currentWindowId(ctx);
|
|
35
|
+
const sessionPrefix = sessionId.slice(0, 8);
|
|
36
|
+
const usedWindowIds = new Set(
|
|
37
|
+
ctx.sessionManager.getBranch().filter(isWindowMarker).map((entry) => entry.data.windowId),
|
|
38
|
+
);
|
|
39
|
+
let windowId: string;
|
|
40
|
+
do {
|
|
41
|
+
windowId = `pcw:${sessionPrefix}:${randomUUID().slice(0, 8)}`;
|
|
42
|
+
} while (usedWindowIds.has(windowId));
|
|
43
|
+
const boot = await buildBootMessage(ctx, windowId, previousId, notifyIncompleteNotes, isCurrent);
|
|
44
|
+
return [
|
|
45
|
+
{ type: "compaction", summary: "", firstKeptEntryId: null },
|
|
46
|
+
{ type: "custom", customType: RESET_MARKER_TYPE, data: { windowId } },
|
|
47
|
+
{ type: "custom_message", customType: BOOT_TYPE, content: boot.content, display: false, details: { windowId } },
|
|
48
|
+
{ type: "custom_message", customType: CONTINUATION_TYPE, content: CONTINUATION, display: false },
|
|
49
|
+
...(prompt ? [{ type: "custom_message" as const, customType: RESET_PROMPT_TYPE, content: prompt, display: true }] : []),
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Inspect the persisted tail of a reset marker. It reports which reset messages are present
|
|
55
|
+
* only while the tail stays repairable: metadata may follow the marker, but real conversation,
|
|
56
|
+
* a foreign message, a later marker, or a misordered/duplicate reset artifact refuses repair.
|
|
57
|
+
*/
|
|
58
|
+
export function inspectResetTail(ctx: ExtensionContext, markerId: string, windowId: string): ResetTailState | undefined {
|
|
59
|
+
const branch = ctx.sessionManager.getBranch();
|
|
60
|
+
const markerIndex = branch.findIndex((entry) => entry.id === markerId);
|
|
61
|
+
if (markerIndex < 0) return undefined;
|
|
62
|
+
let boot = false;
|
|
63
|
+
let continuation = false;
|
|
64
|
+
for (const entry of branch.slice(markerIndex + 1)) {
|
|
65
|
+
if (isWindowBootEntry(entry, windowId)) {
|
|
66
|
+
// The boot is unique and must precede the continuation; a repeat or a late
|
|
67
|
+
// boot would move the provider boundary or misorder the reset shape.
|
|
68
|
+
if (boot || continuation) return undefined;
|
|
69
|
+
boot = true;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (isWindowContinuationEntry(entry)) {
|
|
73
|
+
if (continuation || !boot) return undefined;
|
|
74
|
+
continuation = true;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (isWindowMarker(entry) || entry.type === "message" || entry.type === "custom_message" || entry.type === "compaction" || entry.type === "branch_summary") {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { boot, continuation };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Emit only the reset artifacts an incomplete tail is missing, in the closed order. */
|
|
85
|
+
export async function repairResetTail(
|
|
86
|
+
pi: ExtensionAPI,
|
|
87
|
+
ctx: ExtensionContext,
|
|
88
|
+
marker: WindowMarker,
|
|
89
|
+
notifyIncompleteNotes?: IncompleteNotesNotifier,
|
|
90
|
+
isCurrent: () => boolean = () => true,
|
|
91
|
+
): Promise<void> {
|
|
92
|
+
const tail = inspectResetTail(ctx, marker.id, marker.data.windowId);
|
|
93
|
+
if (!tail || (tail.boot && tail.continuation)) return;
|
|
94
|
+
const boot = tail.boot ? undefined : await buildBootMessage(ctx, marker.data.windowId, previousWindowId(ctx, marker.id), notifyIncompleteNotes, isCurrent);
|
|
95
|
+
if (!isCurrent()) return;
|
|
96
|
+
const latest = inspectResetTail(ctx, marker.id, marker.data.windowId);
|
|
97
|
+
if (!latest) return;
|
|
98
|
+
if (!latest.boot && boot) sendBoot(pi, boot);
|
|
99
|
+
if (!latest.continuation && (latest.boot || boot)) sendContinuation(pi);
|
|
100
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { SessionReader } from "../session-reader.js";
|
|
2
|
+
import { isCheckpointBackedReset, isWindowBootEntry, isWindowMarker } from "../window.js";
|
|
3
|
+
import { isWindowContinuationEntry } from "./artifacts.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Confirm a native boundary on this branch, independent of whether its suffix is still
|
|
7
|
+
* repairable. Later conversation cannot uncommit a checkpoint -> marker -> boot ->
|
|
8
|
+
* continuation already persisted by Pi. No shadow commit state is maintained here.
|
|
9
|
+
*/
|
|
10
|
+
export function resetBoundaryCommitted(ctx: SessionReader, markerId: string, windowId: string): boolean {
|
|
11
|
+
const branch = ctx.sessionManager.getBranch();
|
|
12
|
+
const markerIndex = branch.findIndex((entry) => entry.id === markerId);
|
|
13
|
+
const marker = branch[markerIndex];
|
|
14
|
+
if (!marker || !isWindowMarker(marker) || marker.data.windowId !== windowId || !isCheckpointBackedReset(ctx, marker)) return false;
|
|
15
|
+
let boot = false;
|
|
16
|
+
for (const entry of branch.slice(markerIndex + 1)) {
|
|
17
|
+
if (isWindowBootEntry(entry, windowId)) {
|
|
18
|
+
if (boot || entry.type !== "custom_message" || entry.display !== false) return false;
|
|
19
|
+
boot = true;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (isWindowContinuationEntry(entry)) return boot && entry.type === "custom_message" && entry.display === false;
|
|
23
|
+
if (isWindowMarker(entry) || entry.type === "message" || entry.type === "custom_message" || entry.type === "compaction" || entry.type === "branch_summary") return false;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|