@howaboua/pi-agent-comms 0.0.1 → 0.0.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.
- package/CHANGELOG.md +11 -0
- package/README.md +1 -1
- package/docs/BOARD.md +1 -1
- package/docs/NOTES.md +13 -1
- package/package.json +3 -3
- package/src/board/briefing.ts +2 -2
- package/src/board/settings/panel.ts +11 -8
- package/src/guidance.ts +14 -14
- package/src/notes/AGENTS.md +1 -1
- package/src/notes/feature.ts +67 -1
- package/src/notes/history-search-pruning.mjs +110 -76
- package/src/notes/history-search-worker.mjs +1 -1
- package/src/notes/notes-lookup.ts +1 -1
- package/src/notes/session-pruning-cleanup.mjs +163 -0
- package/src/notes/session-pruning-exit.mjs +312 -0
- package/src/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/notes/session-pruning.ts +34 -1
- package/src/notes/settings-controls.ts +13 -17
- package/src/notes/settings-screen.ts +2 -6
- package/src/notes/window-message.ts +6 -18
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
closeSync,
|
|
4
|
+
lstatSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
openSync,
|
|
7
|
+
readSync,
|
|
8
|
+
realpathSync,
|
|
9
|
+
unlinkSync,
|
|
10
|
+
} from "node:fs";
|
|
11
|
+
import { dirname, join, resolve } from "node:path";
|
|
12
|
+
|
|
13
|
+
/** @param {string} root */
|
|
14
|
+
function privateRoot(root) {
|
|
15
|
+
const stat = lstatSync(root);
|
|
16
|
+
if (
|
|
17
|
+
!stat.isDirectory() ||
|
|
18
|
+
stat.uid !== process.getuid?.() ||
|
|
19
|
+
(stat.mode & 0o077) !== 0
|
|
20
|
+
)
|
|
21
|
+
throw new Error(
|
|
22
|
+
`Session cleanup state ${JSON.stringify(root)} must be a private directory owned by this user. Restore its ownership/permissions before restarting Pi.`,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Exact-file coordination stays outside session directories so inactive Notes
|
|
27
|
+
* never needs to write beside a read-only saved session. No global process scan.
|
|
28
|
+
* @param {string} file
|
|
29
|
+
*/
|
|
30
|
+
function cleanupKey(file) {
|
|
31
|
+
let canonical;
|
|
32
|
+
try {
|
|
33
|
+
canonical = realpathSync(file);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (!(error instanceof Error && "code" in error && error.code === "ENOENT"))
|
|
36
|
+
throw error;
|
|
37
|
+
canonical = resolve(file);
|
|
38
|
+
}
|
|
39
|
+
return createHash("sha256").update(canonical).digest("hex");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @param {string} file @param {string} storageDirectory */
|
|
43
|
+
export function cleanupStatus(file, storageDirectory) {
|
|
44
|
+
return resolve(
|
|
45
|
+
storageDirectory,
|
|
46
|
+
"session-cleanup",
|
|
47
|
+
`${cleanupKey(file)}.json`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @param {string} file */
|
|
52
|
+
export function cleanupPaths(file) {
|
|
53
|
+
// Automatic cleanup is Linux-only. A fixed OS location, not TMPDIR, keeps
|
|
54
|
+
// two local Pi launch environments in the same admission domain.
|
|
55
|
+
const directory = join(
|
|
56
|
+
"/tmp",
|
|
57
|
+
`pi-notes-cleanup-${process.getuid?.() ?? "user"}`,
|
|
58
|
+
cleanupKey(file),
|
|
59
|
+
);
|
|
60
|
+
try {
|
|
61
|
+
privateRoot(dirname(directory));
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (!(error instanceof Error && "code" in error && error.code === "ENOENT"))
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
directory,
|
|
68
|
+
owners: join(directory, "owners"),
|
|
69
|
+
lock: join(directory, "lock"),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** @param {string} file */
|
|
74
|
+
function ensureCleanupState(file) {
|
|
75
|
+
const paths = cleanupPaths(file);
|
|
76
|
+
const root = dirname(paths.directory);
|
|
77
|
+
mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
78
|
+
privateRoot(root);
|
|
79
|
+
mkdirSync(paths.directory, { recursive: true, mode: 0o700 });
|
|
80
|
+
return paths;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Shared by startup admission and the after-exit writer.
|
|
84
|
+
* @param {string} file @returns {(()=>void)|undefined}
|
|
85
|
+
*/
|
|
86
|
+
export function tryCleanupLock(file) {
|
|
87
|
+
const paths = ensureCleanupState(file);
|
|
88
|
+
try {
|
|
89
|
+
const fd = openSync(paths.lock, "wx", 0o600);
|
|
90
|
+
closeSync(fd);
|
|
91
|
+
return () => {
|
|
92
|
+
unlinkSync(paths.lock);
|
|
93
|
+
};
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof Error && "code" in error && error.code === "EEXIST")
|
|
96
|
+
return;
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Pi accepts large headers. Read exactly the first complete source record,
|
|
102
|
+
* accumulating pieces once instead of imposing a smaller extension-only limit.
|
|
103
|
+
* @param {string} file @returns {unknown}
|
|
104
|
+
*/
|
|
105
|
+
export function cleanupHeader(file) {
|
|
106
|
+
const fd = openSync(file, "r");
|
|
107
|
+
/** @type {Buffer[]} */
|
|
108
|
+
const pieces = [];
|
|
109
|
+
let position = 0;
|
|
110
|
+
try {
|
|
111
|
+
while (true) {
|
|
112
|
+
const buffer = Buffer.alloc(8192);
|
|
113
|
+
const read = readSync(fd, buffer, 0, buffer.length, position);
|
|
114
|
+
if (!read) throw new Error("The saved session header is incomplete.");
|
|
115
|
+
position += read;
|
|
116
|
+
const end = buffer.subarray(0, read).indexOf(10);
|
|
117
|
+
pieces.push(buffer.subarray(0, end < 0 ? read : end));
|
|
118
|
+
if (end >= 0) return JSON.parse(Buffer.concat(pieces).toString("utf8"));
|
|
119
|
+
}
|
|
120
|
+
} finally {
|
|
121
|
+
closeSync(fd);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
+
import { dirname } from "node:path";
|
|
1
2
|
import type {
|
|
2
3
|
ExtensionAPI,
|
|
3
4
|
ExtensionContext,
|
|
4
5
|
} from "@earendil-works/pi-coding-agent";
|
|
5
6
|
import type { HistorySearch } from "./history-search.js";
|
|
7
|
+
import {
|
|
8
|
+
recordCleanupFailure,
|
|
9
|
+
scheduleSessionCleanup,
|
|
10
|
+
} from "./session-pruning-exit.mjs";
|
|
6
11
|
import { planSessionPruning } from "./session-pruning-plan.js";
|
|
7
12
|
import { RESTART_NOTICE } from "./session-pruning-policy.js";
|
|
13
|
+
import { cleanupStatus } from "./session-pruning-state.mjs";
|
|
8
14
|
import { commitSessionPruning } from "./session-pruning-transaction.js";
|
|
9
15
|
import type { NotesStore } from "./store.js";
|
|
10
16
|
import { windowDetails } from "./windows.js";
|
|
@@ -50,9 +56,36 @@ export class SessionPruning {
|
|
|
50
56
|
this.pending = undefined;
|
|
51
57
|
}
|
|
52
58
|
register(): void {
|
|
53
|
-
this.pi.on("session_shutdown", () => {
|
|
59
|
+
this.pi.on("session_shutdown", async (event, ctx) => {
|
|
54
60
|
this.epoch++;
|
|
55
61
|
this.pending = undefined;
|
|
62
|
+
if (event.reason !== "quit") {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const file = ctx.sessionManager.getSessionFile();
|
|
66
|
+
if (!file) return;
|
|
67
|
+
const statusPath = cleanupStatus(file, dirname(this.store.path));
|
|
68
|
+
try {
|
|
69
|
+
const policy = this.store.pruningPolicy(
|
|
70
|
+
ctx.sessionManager.getSessionId(),
|
|
71
|
+
);
|
|
72
|
+
if (process.platform === "linux" && (policy.windows || policy.days))
|
|
73
|
+
await scheduleSessionCleanup(
|
|
74
|
+
file,
|
|
75
|
+
ctx.sessionManager.getSessionId(),
|
|
76
|
+
statusPath,
|
|
77
|
+
);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
const message = `Session record cleanup could not start: ${String(error)}. Check local session storage and that Node.js is available as node, then exit Pi normally again. Retired payloads remain pruned.`;
|
|
80
|
+
recordCleanupFailure(
|
|
81
|
+
statusPath,
|
|
82
|
+
ctx.sessionManager.getSessionId(),
|
|
83
|
+
new Error(message),
|
|
84
|
+
);
|
|
85
|
+
ctx.ui.notify(message, "warning");
|
|
86
|
+
} finally {
|
|
87
|
+
this.store.close();
|
|
88
|
+
}
|
|
56
89
|
});
|
|
57
90
|
this.pi.on("agent_start", (_event, ctx) => {
|
|
58
91
|
// Pi emits agent_start before persisting incoming messages. Notes already
|
|
@@ -35,25 +35,25 @@ export function createNotesSettingsControls(
|
|
|
35
35
|
: `${Math.ceil(minutes / 60)}h`;
|
|
36
36
|
const list = new SettingsList(
|
|
37
37
|
[
|
|
38
|
-
{
|
|
39
|
-
id: "sessionPruning",
|
|
40
|
-
label: "Session auto-pruning",
|
|
41
|
-
currentValue: "select…",
|
|
42
|
-
values: ["select…", "settings…"],
|
|
43
|
-
description:
|
|
44
|
-
"Per-session history limits and Run now. Memory savings require restarting Pi.",
|
|
45
|
-
},
|
|
46
38
|
{
|
|
47
39
|
id: "active",
|
|
48
|
-
label: "
|
|
49
|
-
description: `Whitelist matches exact model IDs from any provider. Edit modelWhitelist in ${options.settingsPath}, then reload Pi
|
|
40
|
+
label: "Enabled",
|
|
41
|
+
description: `On uses all models. Whitelist matches exact model IDs from any provider. Edit modelWhitelist in ${options.settingsPath}, then reload Pi.`,
|
|
50
42
|
currentValue:
|
|
51
43
|
settings.active === "off"
|
|
52
44
|
? "Off"
|
|
53
45
|
: settings.active === "all"
|
|
54
|
-
? "
|
|
46
|
+
? "On"
|
|
55
47
|
: "Whitelist",
|
|
56
|
-
values: ["Off", "
|
|
48
|
+
values: ["Off", "On", "Whitelist"],
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
id: "sessionPruning",
|
|
52
|
+
label: "Session auto-pruning",
|
|
53
|
+
currentValue: "select…",
|
|
54
|
+
values: ["select…", "settings…"],
|
|
55
|
+
description:
|
|
56
|
+
"Per-session history limits and Run now. Memory savings require restarting Pi.",
|
|
57
57
|
},
|
|
58
58
|
{
|
|
59
59
|
id: "roles",
|
|
@@ -157,11 +157,7 @@ export function createNotesSettingsControls(
|
|
|
157
157
|
save({
|
|
158
158
|
...current,
|
|
159
159
|
active:
|
|
160
|
-
value === "Off"
|
|
161
|
-
? "off"
|
|
162
|
-
: value === "All models"
|
|
163
|
-
? "all"
|
|
164
|
-
: "whitelist",
|
|
160
|
+
value === "Off" ? "off" : value === "On" ? "all" : "whitelist",
|
|
165
161
|
});
|
|
166
162
|
else if (id === "sharing")
|
|
167
163
|
save({ ...current, shareSubagentContext: value === "on" });
|
|
@@ -29,7 +29,7 @@ export async function openNotesSettings(
|
|
|
29
29
|
sessionPruning: SessionPruningSettings;
|
|
30
30
|
},
|
|
31
31
|
): Promise<"roles" | "board" | undefined> {
|
|
32
|
-
let selected = "
|
|
32
|
+
let selected = "active";
|
|
33
33
|
while (true) {
|
|
34
34
|
const action = await ctx.ui.custom<ScreenAction>(
|
|
35
35
|
(tui, theme, _kb, done) => {
|
|
@@ -46,7 +46,7 @@ export async function openNotesSettings(
|
|
|
46
46
|
const rule = theme.fg("accent", "─".repeat(Math.max(0, width)));
|
|
47
47
|
return [
|
|
48
48
|
rule,
|
|
49
|
-
` ${theme.bold("Notes
|
|
49
|
+
` ${theme.bold("Notes")} / ${theme.fg("dim", options.boardTab === false ? "Roles" : "Board")}`,
|
|
50
50
|
"",
|
|
51
51
|
...options
|
|
52
52
|
.status()
|
|
@@ -54,10 +54,6 @@ export async function openNotesSettings(
|
|
|
54
54
|
wrap(line).map((part) => theme.fg("dim", part)),
|
|
55
55
|
),
|
|
56
56
|
"",
|
|
57
|
-
...wrap(
|
|
58
|
-
"This extension works best on GPT6+ models. For other models we recommend switching the active option to whitelist or switching normal compaction on to work alongside the notes system.",
|
|
59
|
-
),
|
|
60
|
-
"",
|
|
61
57
|
...list.render(width).slice(0, -2),
|
|
62
58
|
"",
|
|
63
59
|
...wrap(
|
|
@@ -15,31 +15,19 @@ export interface WindowDetails {
|
|
|
15
15
|
trim: boolean;
|
|
16
16
|
}
|
|
17
17
|
const GUIDANCE = `<context_window_guidance>
|
|
18
|
-
Notes are
|
|
18
|
+
Notes are short virtual resume points, not working memory. Use task notes and/or checkpoints as needed. Reuse existing notes and avoid duplicating state. Use relative paths. Keep only current goal, task-specific constraints or exceptions, decision/status, next action and evidence/history links. Keep reasoning and intermediate results in context. Omit harness instructions, tool usage and skill-read bookkeeping.
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
Write less: consolidate changed recovery state at substantive turn end, before replying. Skip routine turns and unchanged state unless explicit reminders or earlier handoffs require a save. If a no-action report needs recording, add only its run/report ID and "no owned action". Keep current state first; replace superseded status. Append only while existing state remains accurate. Use history on demand for older decisions and evidence instead of copying them into notes.
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
Link existing notes rather than duplicate. Recording does not authorise implementation.
|
|
25
|
-
|
|
26
|
-
Immediately before new_context, MUST save the checkpoint via notes. Save before handoff too. For rollover, call new_context after saving.
|
|
27
|
-
|
|
28
|
-
When recovering context, read the checkpoint and necessary links if available, then retrieve a few relevant user messages with history for intent and communication preferences. Past messages are context, not renewed requests. Current instructions take precedence. Resume the current task; read further history only as needed. Do not reread unchanged notes.
|
|
29
|
-
|
|
30
|
-
MUST reread required skills after rollover.
|
|
22
|
+
Save relevant notes before handoff and immediately before new_context. On recovery, read relevant notes, necessary links and a few relevant user messages with history, then resume the current task. Past messages are context, not renewed requests; current instructions win. Avoid unchanged rereads. Reread required skills after rollover. Recording does not authorise implementation.
|
|
31
23
|
</context_window_guidance>`;
|
|
32
24
|
|
|
33
25
|
const GPT_GUIDANCE = `<context_window_guidance>
|
|
34
|
-
Notes=virtual
|
|
35
|
-
|
|
36
|
-
Near substantive turn end, before reply→consolidate CHANGED recovery. notes write→create/replace; append→add. No saves amid ordinary tool calls; skip routine/unchanged state. Explicit reminders/earlier handoffs still apply.
|
|
37
|
-
|
|
38
|
-
Immediately before new_context MUST save checkpoint via notes; before handoff too. Rollover→new_context after save.
|
|
26
|
+
Notes=short virtual resume points, not working memory. Relative paths; task notes and/or checkpoints as needed. Reuse existing notes; avoid duplicate state. Keep only current goal, task constraints/exceptions, decision/status, next action, evidence/history links. Reasoning/intermediate results stay in context. Omit harness/tool instructions and skill-read state.
|
|
39
27
|
|
|
40
|
-
|
|
28
|
+
Fewer writes→consolidate changed recovery at substantive turn end BEFORE reply; skip routine/unchanged turns unless explicit reminders/earlier handoffs require saving. No-action report + save needed→add only run/report ID + "no owned action". Current first; replace superseded status. Append only while prior state stays accurate. History→older decisions/evidence on demand, not copied into notes.
|
|
41
29
|
|
|
42
|
-
|
|
30
|
+
Save relevant notes before handoff/immediately before new_context. Recovery→relevant notes, necessary links, a few relevant user messages via history→resume current task. Past messages≠renewed requests; current instructions win. No unchanged rereads. Reread required skills after rollover. Recording≠implementation permission.
|
|
43
31
|
</context_window_guidance>`;
|
|
44
32
|
|
|
45
33
|
export function registerWindowRenderer(pi: ExtensionAPI): void {
|