@howaboua/pi-notes-compaction 0.0.15 → 0.0.17
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/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +17 -2
- package/index.ts +8 -0
- package/package.json +16 -13
- package/src/board/context-briefing.ts +245 -0
- package/src/board/delivery.ts +85 -0
- package/src/guidance.ts +121 -0
- package/src/notes/AGENTS.md +27 -0
- package/src/{activation.ts → notes/activation.ts} +8 -10
- package/src/{attachment-sharing.ts → notes/attachment-sharing.ts} +1 -1
- package/src/{attachment-snapshot.ts → notes/attachment-snapshot.ts} +33 -20
- package/src/{context-sharing.ts → notes/context-sharing.ts} +7 -2
- package/src/{extension-hooks.ts → notes/extension-hooks.ts} +1 -12
- package/src/{extension-sharing.ts → notes/extension-sharing.ts} +1 -1
- package/src/notes/feature.ts +234 -0
- package/src/notes/history-search-pruning.mjs +154 -0
- package/src/{history-search-worker.mjs → notes/history-search-worker.mjs} +1 -1
- package/src/{history-windows.ts → notes/history-windows.ts} +10 -22
- package/src/{history.ts → notes/history.ts} +6 -5
- package/src/{index.ts → notes/index.ts} +20 -11
- package/src/{integrations.ts → notes/integrations.ts} +19 -3
- package/src/{lifecycle.ts → notes/lifecycle.ts} +0 -2
- package/src/{note-query.ts → notes/note-query.ts} +3 -5
- package/src/notes/note-result.ts +119 -0
- package/src/{note-save-marker.ts → notes/note-save-marker.ts} +3 -1
- package/src/{notes-lookup.ts → notes/notes-lookup.ts} +1 -1
- package/src/{query-executor.ts → notes/query-executor.ts} +7 -2
- package/src/{query-input.ts → notes/query-input.ts} +1 -0
- package/src/{remote.ts → notes/remote.ts} +1 -1
- package/src/{saved-session-access.ts → notes/saved-session-access.ts} +14 -10
- package/src/{saved-session.ts → notes/saved-session.ts} +31 -15
- package/src/notes/saved-sharing-records.ts +170 -0
- package/src/notes/session-pruning-cleanup.mjs +163 -0
- package/src/notes/session-pruning-exit.mjs +312 -0
- package/src/{session-pruning-screen.ts → notes/session-pruning-screen.ts} +1 -1
- package/src/notes/session-pruning-sparse.mjs +133 -0
- package/src/notes/session-pruning-state.mjs +123 -0
- package/src/{session-pruning.ts → notes/session-pruning.ts} +39 -6
- package/src/{settings-command.ts → notes/settings-command.ts} +29 -42
- package/src/{settings-controls.ts → notes/settings-controls.ts} +61 -75
- package/src/{settings-screen.ts → notes/settings-screen.ts} +25 -11
- package/src/{settings.ts → notes/settings.ts} +37 -6
- package/src/{shared-notes.ts → notes/shared-notes.ts} +21 -13
- package/src/{shared-session-reader.ts → notes/shared-session-reader.ts} +8 -11
- package/src/{sharing-service.ts → notes/sharing-service.ts} +1 -1
- package/src/{tool-contracts.ts → notes/tool-contracts.ts} +71 -44
- package/src/{tools.ts → notes/tools.ts} +2 -2
- package/src/{tree-handoff-read.ts → notes/tree-handoff-read.ts} +47 -32
- package/src/{window-message.ts → notes/window-message.ts} +8 -25
- package/src/settings-navigation.ts +11 -0
- package/src/timestamps.ts +3 -0
- package/tsconfig.json +9 -0
- package/src/history-search-pruning.mjs +0 -120
- package/src/note-result.ts +0 -70
- package/src/saved-sharing-records.ts +0 -135
- /package/src/{agent-identity.ts → notes/agent-identity.ts} +0 -0
- /package/src/{binding-startup.ts → notes/binding-startup.ts} +0 -0
- /package/src/{bridge.ts → notes/bridge.ts} +0 -0
- /package/src/{compaction-projection.ts → notes/compaction-projection.ts} +0 -0
- /package/src/{context-owner.ts → notes/context-owner.ts} +0 -0
- /package/src/{fresh-checkpoint.ts → notes/fresh-checkpoint.ts} +0 -0
- /package/src/{fresh-notes.ts → notes/fresh-notes.ts} +0 -0
- /package/src/{history-search-protocol.ts → notes/history-search-protocol.ts} +0 -0
- /package/src/{history-search.ts → notes/history-search.ts} +0 -0
- /package/src/{idle-admission.ts → notes/idle-admission.ts} +0 -0
- /package/src/{model-role-config.ts → notes/model-role-config.ts} +0 -0
- /package/src/{model-role-screen.ts → notes/model-role-screen.ts} +0 -0
- /package/src/{model-roles.ts → notes/model-roles.ts} +0 -0
- /package/src/{notes.ts → notes/notes.ts} +0 -0
- /package/src/{rollover.ts → notes/rollover.ts} +0 -0
- /package/src/{saved-session-index.mjs → notes/saved-session-index.mjs} +0 -0
- /package/src/{saved-session.mjs → notes/saved-session.mjs} +0 -0
- /package/src/{session-pruning-content.ts → notes/session-pruning-content.ts} +0 -0
- /package/src/{session-pruning-plan.ts → notes/session-pruning-plan.ts} +0 -0
- /package/src/{session-pruning-policy.ts → notes/session-pruning-policy.ts} +0 -0
- /package/src/{session-pruning-source.ts → notes/session-pruning-source.ts} +0 -0
- /package/src/{session-pruning-transaction.ts → notes/session-pruning-transaction.ts} +0 -0
- /package/src/{sqlite.mjs → notes/sqlite.mjs} +0 -0
- /package/src/{store.ts → notes/store.ts} +0 -0
- /package/src/{task-boundary.ts → notes/task-boundary.ts} +0 -0
- /package/src/{tree-archive-manifest.mjs → notes/tree-archive-manifest.mjs} +0 -0
- /package/src/{tree-archive.ts → notes/tree-archive.ts} +0 -0
- /package/src/{tree-checkpoint.ts → notes/tree-checkpoint.ts} +0 -0
- /package/src/{window-checkpoint.ts → notes/window-checkpoint.ts} +0 -0
- /package/src/{windows.ts → notes/windows.ts} +0 -0
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import { open } from "node:fs/promises";
|
|
2
|
-
|
|
3
|
-
/** @typedef {{file:string; sessionId:string; generation:string; excluded:Set<string>}} PrunedHistory */
|
|
4
|
-
|
|
5
|
-
/** The live SessionManager still contains old text until restart. Read the committed
|
|
6
|
-
* source so later indexing, settings toggles and /reload cannot resurrect its terms.
|
|
7
|
-
*/
|
|
8
|
-
export function createHistoryPruning() {
|
|
9
|
-
/** @type {PrunedHistory | undefined} */
|
|
10
|
-
let cached;
|
|
11
|
-
const empty = new Set();
|
|
12
|
-
/** @param {import("./sqlite.mjs").Database} db
|
|
13
|
-
* @param {string} sessionId
|
|
14
|
-
* @param {string | undefined} file
|
|
15
|
-
* @returns {Promise<ReadonlySet<string>>}
|
|
16
|
-
*/
|
|
17
|
-
return async (db, sessionId, file) => {
|
|
18
|
-
if (!file) return empty;
|
|
19
|
-
let input;
|
|
20
|
-
try {
|
|
21
|
-
input = await open(file, "r");
|
|
22
|
-
} catch (error) {
|
|
23
|
-
// A new Pi session may not have flushed its first message yet.
|
|
24
|
-
if (error instanceof Error && "code" in error && error.code === "ENOENT")
|
|
25
|
-
return empty;
|
|
26
|
-
throw error;
|
|
27
|
-
}
|
|
28
|
-
let stream;
|
|
29
|
-
try {
|
|
30
|
-
const { size } = await input.stat();
|
|
31
|
-
if (!size) return empty;
|
|
32
|
-
stream = input.createReadStream({
|
|
33
|
-
start: 0,
|
|
34
|
-
end: size - 1,
|
|
35
|
-
autoClose: false,
|
|
36
|
-
encoding: "utf8",
|
|
37
|
-
});
|
|
38
|
-
/** @type {PrunedHistory | undefined} */
|
|
39
|
-
let source;
|
|
40
|
-
let pending = "";
|
|
41
|
-
for await (const chunk of stream) {
|
|
42
|
-
pending += chunk;
|
|
43
|
-
let start = 0;
|
|
44
|
-
let end;
|
|
45
|
-
while ((end = pending.indexOf("\n", start)) >= 0) {
|
|
46
|
-
const entry = JSON.parse(pending.slice(start, end));
|
|
47
|
-
start = end + 1;
|
|
48
|
-
if (!source) {
|
|
49
|
-
if (entry.type !== "session" || entry.id !== sessionId)
|
|
50
|
-
throw new Error("History source identity changed");
|
|
51
|
-
const marker = entry.notesPruning;
|
|
52
|
-
if (marker === undefined) return empty;
|
|
53
|
-
if (
|
|
54
|
-
marker?.version !== 1 ||
|
|
55
|
-
marker.sessionId !== sessionId ||
|
|
56
|
-
typeof marker.generation !== "string"
|
|
57
|
-
)
|
|
58
|
-
throw new Error("Invalid history pruning marker");
|
|
59
|
-
if (
|
|
60
|
-
cached?.file === file &&
|
|
61
|
-
cached.sessionId === sessionId &&
|
|
62
|
-
cached.generation === marker.generation
|
|
63
|
-
)
|
|
64
|
-
return cached.excluded;
|
|
65
|
-
source = {
|
|
66
|
-
file,
|
|
67
|
-
sessionId,
|
|
68
|
-
generation: marker.generation,
|
|
69
|
-
excluded: new Set(),
|
|
70
|
-
};
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
const message = entry.type === "message" ? entry.message : undefined;
|
|
74
|
-
if (
|
|
75
|
-
(entry.type === "custom" &&
|
|
76
|
-
entry.customType === "notes-compaction:pruned:v1") ||
|
|
77
|
-
(["user", "assistant"].includes(message?.role) &&
|
|
78
|
-
!visibleText(message.content))
|
|
79
|
-
)
|
|
80
|
-
source.excluded.add(entry.id);
|
|
81
|
-
}
|
|
82
|
-
pending = pending.slice(start);
|
|
83
|
-
}
|
|
84
|
-
// Only complete lines from the opened inode count. A concurrent append's
|
|
85
|
-
// incomplete suffix is new history, never an old pruned conversation.
|
|
86
|
-
if (!source) throw new Error("History source header is missing");
|
|
87
|
-
const excluded = source.excluded;
|
|
88
|
-
const removeTerms = db.prepare(
|
|
89
|
-
"DELETE FROM history_terms WHERE rowid = ?",
|
|
90
|
-
);
|
|
91
|
-
const removeDocument = db.prepare(
|
|
92
|
-
"DELETE FROM history_documents WHERE id = ?",
|
|
93
|
-
);
|
|
94
|
-
/** @type {import("./sqlite.mjs").Statement<[string], {id:number; item_id:string}>} */
|
|
95
|
-
const documents = db.prepare(
|
|
96
|
-
"SELECT id, item_id FROM history_documents WHERE session_id = ?",
|
|
97
|
-
);
|
|
98
|
-
db.transaction(() => {
|
|
99
|
-
for (const row of documents.all(sessionId)) {
|
|
100
|
-
if (!excluded.has(row.item_id)) continue;
|
|
101
|
-
removeTerms.run(row.id);
|
|
102
|
-
removeDocument.run(row.id);
|
|
103
|
-
}
|
|
104
|
-
}).immediate();
|
|
105
|
-
cached = source;
|
|
106
|
-
return excluded;
|
|
107
|
-
} finally {
|
|
108
|
-
stream?.destroy();
|
|
109
|
-
await input.close();
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/** @param {unknown} content */
|
|
115
|
-
function visibleText(content) {
|
|
116
|
-
return typeof content === "string"
|
|
117
|
-
? content.length > 0
|
|
118
|
-
: Array.isArray(content) &&
|
|
119
|
-
content.some((part) => part?.type === "text" && part.text?.length > 0);
|
|
120
|
-
}
|
package/src/note-result.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import type { AgentToolResult } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { Type } from "typebox";
|
|
3
|
-
import { Check } from "typebox/value";
|
|
4
|
-
|
|
5
|
-
const OPTIONAL_STRING = Type.Optional(Type.String());
|
|
6
|
-
const OPTIONAL_BOOLEAN = Type.Optional(Type.Boolean());
|
|
7
|
-
const MATCH = Type.Object({ line_number: Type.Integer(), line: Type.String() });
|
|
8
|
-
|
|
9
|
-
// Pi codemode needs this declaration to return structuredContent rather than text.
|
|
10
|
-
export const NOTES_OUTPUT_SCHEMA = Type.Object(
|
|
11
|
-
{},
|
|
12
|
-
{ additionalProperties: true },
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
const NOTES_RESULT_SCHEMA = Type.Object(
|
|
16
|
-
{
|
|
17
|
-
file: Type.Optional(
|
|
18
|
-
Type.Union([
|
|
19
|
-
Type.Object({
|
|
20
|
-
path: Type.String(),
|
|
21
|
-
content: OPTIONAL_STRING,
|
|
22
|
-
start_line: Type.Optional(Type.Integer()),
|
|
23
|
-
stop_line: Type.Optional(Type.Integer()),
|
|
24
|
-
total_lines: Type.Optional(Type.Integer()),
|
|
25
|
-
append_only: OPTIONAL_BOOLEAN,
|
|
26
|
-
}),
|
|
27
|
-
Type.Null(),
|
|
28
|
-
]),
|
|
29
|
-
),
|
|
30
|
-
files: Type.Optional(
|
|
31
|
-
Type.Array(
|
|
32
|
-
Type.Object({
|
|
33
|
-
path: Type.String(),
|
|
34
|
-
updated_at: OPTIONAL_STRING,
|
|
35
|
-
created_at: OPTIONAL_STRING,
|
|
36
|
-
matches: Type.Optional(Type.Array(MATCH)),
|
|
37
|
-
truncated: OPTIONAL_BOOLEAN,
|
|
38
|
-
append_only: OPTIONAL_BOOLEAN,
|
|
39
|
-
}),
|
|
40
|
-
),
|
|
41
|
-
),
|
|
42
|
-
usage: OPTIONAL_STRING,
|
|
43
|
-
warnings: Type.Optional(Type.Array(Type.String())),
|
|
44
|
-
warning: OPTIONAL_STRING,
|
|
45
|
-
truncated: OPTIONAL_BOOLEAN,
|
|
46
|
-
read_only: OPTIONAL_BOOLEAN,
|
|
47
|
-
saved_at: OPTIONAL_STRING,
|
|
48
|
-
captured_at: OPTIONAL_STRING,
|
|
49
|
-
results: OPTIONAL_STRING,
|
|
50
|
-
actions: Type.Optional(Type.Record(Type.String(), Type.String())),
|
|
51
|
-
writes: OPTIONAL_STRING,
|
|
52
|
-
paths: OPTIONAL_STRING,
|
|
53
|
-
ranges: OPTIONAL_STRING,
|
|
54
|
-
defaults: OPTIONAL_STRING,
|
|
55
|
-
},
|
|
56
|
-
{ additionalProperties: false },
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
/** One canonical payload for native text and every programmatic caller. */
|
|
60
|
-
export function noteResult(payload: unknown): AgentToolResult<unknown> {
|
|
61
|
-
if (!Check(NOTES_RESULT_SCHEMA, payload))
|
|
62
|
-
throw new Error(
|
|
63
|
-
"Notes returned invalid data. Read the note before retrying a write.",
|
|
64
|
-
);
|
|
65
|
-
return {
|
|
66
|
-
content: [{ type: "text", text: JSON.stringify(payload) }],
|
|
67
|
-
structuredContent: payload,
|
|
68
|
-
details: {},
|
|
69
|
-
};
|
|
70
|
-
}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { type Static, Type } from "typebox";
|
|
2
|
-
import { Check } from "typebox/value";
|
|
3
|
-
import {
|
|
4
|
-
CONTEXT_AGENT_PATTERN,
|
|
5
|
-
canonicalAgentName,
|
|
6
|
-
canonicalContextIdentity,
|
|
7
|
-
contextStorageAgentName,
|
|
8
|
-
pinnedContextIdentity,
|
|
9
|
-
} from "./agent-identity.js";
|
|
10
|
-
import type { SavedNotesSession } from "./saved-session.js";
|
|
11
|
-
|
|
12
|
-
const Agent = Type.String({ pattern: CONTEXT_AGENT_PATTERN });
|
|
13
|
-
export const Identity = Type.Object({
|
|
14
|
-
protocol: Type.Literal(1),
|
|
15
|
-
sessionId: Type.String(),
|
|
16
|
-
threadId: Type.String(),
|
|
17
|
-
agentName: Agent,
|
|
18
|
-
storageAgentName: Type.Optional(Agent),
|
|
19
|
-
storage: Type.Optional(
|
|
20
|
-
Type.Union([Type.Literal("session"), Type.Literal("remote")]),
|
|
21
|
-
),
|
|
22
|
-
routing: Type.Optional(Type.Unknown()),
|
|
23
|
-
});
|
|
24
|
-
export const Member = Type.Object({
|
|
25
|
-
threadId: Type.String(),
|
|
26
|
-
sessionId: Type.String(),
|
|
27
|
-
agentName: Agent,
|
|
28
|
-
machine: Type.String(),
|
|
29
|
-
sessionFile: Type.String(),
|
|
30
|
-
});
|
|
31
|
-
export const Plan = Type.Object({
|
|
32
|
-
controllerSessionId: Type.String(),
|
|
33
|
-
targetSessionId: Type.String(),
|
|
34
|
-
context: Type.Optional(
|
|
35
|
-
Type.Object({
|
|
36
|
-
controller: Identity,
|
|
37
|
-
target: Identity,
|
|
38
|
-
alias: Agent,
|
|
39
|
-
controllerAlias: Agent,
|
|
40
|
-
controllerFile: Type.Optional(Type.String()),
|
|
41
|
-
controllerMachine: Type.Optional(Type.String()),
|
|
42
|
-
controllerNotes: Type.Unknown(),
|
|
43
|
-
targetNotes: Type.Unknown(),
|
|
44
|
-
}),
|
|
45
|
-
),
|
|
46
|
-
});
|
|
47
|
-
export const Route = Type.Object({
|
|
48
|
-
plan: Plan,
|
|
49
|
-
machine: Type.String(),
|
|
50
|
-
sessionFile: Type.String(),
|
|
51
|
-
phase: Type.Union([Type.Literal("pending"), Type.Literal("ready")]),
|
|
52
|
-
detached: Type.Optional(
|
|
53
|
-
Type.Object({ context: Type.Optional(Type.Unknown()) }),
|
|
54
|
-
),
|
|
55
|
-
});
|
|
56
|
-
export const Detachment = Type.Object({
|
|
57
|
-
plan: Plan,
|
|
58
|
-
context: Type.Optional(Type.Unknown()),
|
|
59
|
-
});
|
|
60
|
-
export const Snapshot = Type.Object({
|
|
61
|
-
protocol: Type.Literal(1),
|
|
62
|
-
timestamp: Type.Number(),
|
|
63
|
-
files: Type.Array(
|
|
64
|
-
Type.Object({
|
|
65
|
-
path: Type.String(),
|
|
66
|
-
text: Type.String(),
|
|
67
|
-
createdAt: Type.Number(),
|
|
68
|
-
updatedAt: Type.Number(),
|
|
69
|
-
operation: Type.Union([
|
|
70
|
-
Type.Literal("local_append_overlay"),
|
|
71
|
-
Type.Literal("local_replacement"),
|
|
72
|
-
]),
|
|
73
|
-
}),
|
|
74
|
-
),
|
|
75
|
-
});
|
|
76
|
-
export const Parent = Type.Object({
|
|
77
|
-
transport: Type.Literal("shepherdr"),
|
|
78
|
-
parent: Type.String(),
|
|
79
|
-
savedParent: Type.Optional(
|
|
80
|
-
Type.Object({ machine: Type.String(), sessionFile: Type.String() }),
|
|
81
|
-
),
|
|
82
|
-
});
|
|
83
|
-
export type Identity = Static<typeof Identity>;
|
|
84
|
-
|
|
85
|
-
/** Ordinary family membership is immutable, explicit and native-session owned. */
|
|
86
|
-
export function savedFamilyIdentity(
|
|
87
|
-
session: Pick<SavedNotesSession, "threadId" | "entries">,
|
|
88
|
-
): Identity | undefined {
|
|
89
|
-
const identity = savedNotesIdentity(session);
|
|
90
|
-
if (
|
|
91
|
-
identity.storage === undefined ||
|
|
92
|
-
!session.entries.some(
|
|
93
|
-
(entry) =>
|
|
94
|
-
entry.type === "custom" &&
|
|
95
|
-
entry.customType === "codex-context-agent" &&
|
|
96
|
-
Check(Identity, entry.data) &&
|
|
97
|
-
entry.data.threadId === session.threadId,
|
|
98
|
-
)
|
|
99
|
-
)
|
|
100
|
-
return;
|
|
101
|
-
return identity;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** Persisted sharing is session-scoped. Forks never inherit the source agent's address. */
|
|
105
|
-
export function savedNotesIdentity(
|
|
106
|
-
session: Pick<SavedNotesSession, "threadId" | "entries">,
|
|
107
|
-
): Identity {
|
|
108
|
-
let found: Identity | undefined;
|
|
109
|
-
for (const entry of session.entries) {
|
|
110
|
-
if (entry.type !== "custom" || entry.customType !== "codex-context-agent")
|
|
111
|
-
continue;
|
|
112
|
-
if (!Check(Identity, entry.data))
|
|
113
|
-
throw new Error("Invalid saved notes identity");
|
|
114
|
-
if (entry.data.threadId !== session.threadId) continue;
|
|
115
|
-
if (found) throw new Error("Conflicting saved notes identities");
|
|
116
|
-
found = entry.data;
|
|
117
|
-
}
|
|
118
|
-
return canonicalContextIdentity(
|
|
119
|
-
found ??
|
|
120
|
-
pinnedContextIdentity(session.threadId, session.entries) ?? {
|
|
121
|
-
protocol: 1,
|
|
122
|
-
threadId: session.threadId,
|
|
123
|
-
sessionId: session.threadId,
|
|
124
|
-
agentName: canonicalAgentName(session.threadId),
|
|
125
|
-
},
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export function sameOwner(a: Identity, b: Identity): boolean {
|
|
130
|
-
return (
|
|
131
|
-
a.threadId === b.threadId &&
|
|
132
|
-
a.sessionId === b.sessionId &&
|
|
133
|
-
contextStorageAgentName(a) === contextStorageAgentName(b)
|
|
134
|
-
);
|
|
135
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|