@aeondave/pi-persona-mind 0.9.6 → 0.9.8
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/AGENTS.md +137 -135
- package/CHANGELOG.md +287 -270
- package/LICENSE +24 -24
- package/README.md +84 -171
- package/docs/DESIGN.md +605 -573
- package/docs/MEMORY-RESEARCH.md +163 -163
- package/package.json +15 -6
- package/src/core/auto-optimize.ts +323 -249
- package/src/core/backlog.ts +228 -228
- package/src/core/blocked.ts +38 -38
- package/src/core/capture.ts +155 -154
- package/src/core/context-budget.ts +74 -74
- package/src/core/handoff-store.ts +111 -111
- package/src/core/handoff.ts +70 -70
- package/src/core/host-compat.ts +16 -16
- package/src/core/ids.ts +59 -59
- package/src/core/inject.ts +247 -247
- package/src/core/knowledge-review.ts +107 -107
- package/src/core/memory-read.ts +34 -34
- package/src/core/memory.ts +321 -321
- package/src/core/migrate.ts +713 -713
- package/src/core/mind-settings.ts +54 -54
- package/src/core/optimize-history.ts +163 -163
- package/src/core/optimize-service.ts +189 -180
- package/src/core/optimize.ts +127 -127
- package/src/core/retrieval.ts +164 -164
- package/src/core/scanner.ts +146 -146
- package/src/core/scope.ts +201 -205
- package/src/core/service.ts +720 -720
- package/src/core/session-memory.ts +71 -71
- package/src/core/store.ts +1002 -1002
- package/src/extension/activity.ts +79 -79
- package/src/extension/auto-optimize-session.ts +143 -143
- package/src/extension/auto-optimize.ts +565 -569
- package/src/extension/handoff.ts +23 -23
- package/src/extension/knowledge-review.ts +229 -229
- package/src/extension/memory-inspector.ts +82 -82
- package/src/extension/mind-actions.ts +38 -38
- package/src/extension/mind-worker.ts +32 -32
- package/src/extension/persona-dependency.ts +76 -76
- package/src/extension/reference-anchor.ts +48 -0
- package/src/extension/review-evidence.ts +222 -222
- package/src/extension/worker-memory-anchor.ts +28 -28
- package/src/index.ts +1357 -1310
- package/src/session-memory.ts +127 -122
- package/src/tools/backlog.ts +126 -126
- package/src/tools/memory-handoff.ts +30 -30
- package/src/tools/memory-history.ts +46 -46
- package/src/tools/memory.ts +6 -4
- package/src/ui/card-frame.ts +161 -161
- package/src/ui/inspector-frame.ts +44 -44
- package/src/ui/memory-inspector.ts +464 -464
- package/src/ui/mind-result-card.ts +48 -48
- package/src/ui/presentation.ts +87 -87
- package/src/ui/session-note-card.ts +58 -56
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
export const SESSION_NOTE_ENTRY_TYPE = "pi-persona-mind-session-note";
|
|
2
|
-
export const MAX_SESSION_NOTE_CHARS = 8_000;
|
|
3
|
-
|
|
4
|
-
import { MEMORY_KINDS, type MemoryKind } from "./memory.ts";
|
|
5
|
-
import { scanContent } from "./scanner.ts";
|
|
6
|
-
|
|
7
|
-
export const MAX_SESSION_NOTES = 64;
|
|
8
|
-
export const MAX_SESSION_NOTE_EVENTS = 256;
|
|
9
|
-
export const MAX_SESSION_NOTE_BYTES = 512 * 1024;
|
|
10
|
-
export const MAX_SESSION_SOURCE_CHARS = 1_000;
|
|
11
|
-
export interface SessionNoteInput { kind: MemoryKind; text: string; source?: string }
|
|
12
|
-
export interface SessionNote extends SessionNoteInput { id: string; recordedAt: string; originSessionId: string }
|
|
13
|
-
export type SessionNoteEvent = { version: 1; op: "save"; note: SessionNote } | { version: 1; op: "forget"; id: string };
|
|
14
|
-
export interface SessionReplay { notes: SessionNote[]; events: number; bytes: number; error?: string }
|
|
15
|
-
export const SESSION_MEMORY_KINDS = MEMORY_KINDS;
|
|
16
|
-
const KINDS: ReadonlySet<string> = new Set(SESSION_MEMORY_KINDS);
|
|
17
|
-
const ID = /^session:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
18
|
-
|
|
19
|
-
/** The scanner is applied on admission and replay; provenance is data too. */
|
|
20
|
-
export function sessionNoteError(input: SessionNoteInput): string | undefined {
|
|
21
|
-
if (!KINDS.has(input.kind)) return "invalid session note kind";
|
|
22
|
-
if (typeof input.text !== "string" || !input.text.trim() || input.text.length > MAX_SESSION_NOTE_CHARS) return `session note text must contain 1..${MAX_SESSION_NOTE_CHARS} characters`;
|
|
23
|
-
if (input.source !== undefined && (typeof input.source !== "string" || input.source.length > MAX_SESSION_SOURCE_CHARS)) return `session note source exceeds ${MAX_SESSION_SOURCE_CHARS} characters`;
|
|
24
|
-
for (const text of [input.text, input.source]) {
|
|
25
|
-
if (text === undefined) continue;
|
|
26
|
-
const scanned = scanContent(text);
|
|
27
|
-
if (!scanned.ok) return `session note rejected: ${scanned.reason ?? "content policy"}`;
|
|
28
|
-
}
|
|
29
|
-
return undefined;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function parseEvent(value: unknown): SessionNoteEvent | undefined {
|
|
33
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
34
|
-
const record = value as Record<string, unknown>;
|
|
35
|
-
if (record.version !== 1) return undefined;
|
|
36
|
-
if (record.op === "forget") return typeof record.id === "string" && ID.test(record.id) ? { version: 1, op: "forget", id: record.id } : undefined;
|
|
37
|
-
if (record.op !== "save" || !record.note || typeof record.note !== "object" || Array.isArray(record.note)) return undefined;
|
|
38
|
-
const note = record.note as SessionNote;
|
|
39
|
-
if (typeof note.id !== "string" || !ID.test(note.id) || typeof note.recordedAt !== "string" || !Number.isFinite(Date.parse(note.recordedAt)) || typeof note.originSessionId !== "string" || !note.originSessionId || note.originSessionId.length > 256 || /[\s\u0000-\u001f\u007f]/u.test(note.originSessionId) || sessionNoteError(note)) return undefined;
|
|
40
|
-
return { version: 1, op: "save", note: { id: note.id, kind: note.kind, text: note.text, recordedAt: note.recordedAt, originSessionId: note.originSessionId, ...(note.source === undefined ? {} : { source: note.source }) } };
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** All history owns the lifetime budget; only the selected branch owns visible notes. */
|
|
44
|
-
export function replaySessionNotes(branch: readonly unknown[], history: readonly unknown[]): SessionReplay {
|
|
45
|
-
let bytes = 0;
|
|
46
|
-
try {
|
|
47
|
-
for (const value of history) {
|
|
48
|
-
const encoded = JSON.stringify(value);
|
|
49
|
-
if (encoded === undefined) return { notes: [], events: history.length, bytes, error: "unaccountable session note history" };
|
|
50
|
-
bytes += new TextEncoder().encode(encoded).length;
|
|
51
|
-
if (!parseEvent(value)) return { notes: [], events: history.length, bytes, error: "unsupported or unsafe session note history" };
|
|
52
|
-
}
|
|
53
|
-
} catch { return { notes: [], events: history.length, bytes, error: "unaccountable session note history" }; }
|
|
54
|
-
if (history.length > MAX_SESSION_NOTE_EVENTS || bytes > MAX_SESSION_NOTE_BYTES) return { notes: [], events: history.length, bytes, error: "session note history exceeds its lifetime budget" };
|
|
55
|
-
const active = new Map<string, SessionNote>();
|
|
56
|
-
for (const value of branch) {
|
|
57
|
-
const event = parseEvent(value);
|
|
58
|
-
if (!event) return { notes: [], events: history.length, bytes, error: "unsupported or unsafe session note branch" };
|
|
59
|
-
if (event.op === "forget") active.delete(event.id);
|
|
60
|
-
else {
|
|
61
|
-
if (active.has(event.note.id)) return { notes: [], events: history.length, bytes, error: "duplicate session note identity" };
|
|
62
|
-
active.set(event.note.id, event.note);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
if (active.size > MAX_SESSION_NOTES) return { notes: [], events: history.length, bytes, error: "session note count exceeds its limit" };
|
|
66
|
-
return { notes: [...active.values()], events: history.length, bytes };
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function sessionEventFits(state: SessionReplay, event: SessionNoteEvent): boolean {
|
|
70
|
-
return state.events < MAX_SESSION_NOTE_EVENTS && state.bytes + new TextEncoder().encode(JSON.stringify(event)).length <= MAX_SESSION_NOTE_BYTES;
|
|
71
|
-
}
|
|
1
|
+
export const SESSION_NOTE_ENTRY_TYPE = "pi-persona-mind-session-note";
|
|
2
|
+
export const MAX_SESSION_NOTE_CHARS = 8_000;
|
|
3
|
+
|
|
4
|
+
import { MEMORY_KINDS, type MemoryKind } from "./memory.ts";
|
|
5
|
+
import { scanContent } from "./scanner.ts";
|
|
6
|
+
|
|
7
|
+
export const MAX_SESSION_NOTES = 64;
|
|
8
|
+
export const MAX_SESSION_NOTE_EVENTS = 256;
|
|
9
|
+
export const MAX_SESSION_NOTE_BYTES = 512 * 1024;
|
|
10
|
+
export const MAX_SESSION_SOURCE_CHARS = 1_000;
|
|
11
|
+
export interface SessionNoteInput { kind: MemoryKind; text: string; source?: string }
|
|
12
|
+
export interface SessionNote extends SessionNoteInput { id: string; recordedAt: string; originSessionId: string }
|
|
13
|
+
export type SessionNoteEvent = { version: 1; op: "save"; note: SessionNote } | { version: 1; op: "forget"; id: string };
|
|
14
|
+
export interface SessionReplay { notes: SessionNote[]; events: number; bytes: number; error?: string }
|
|
15
|
+
export const SESSION_MEMORY_KINDS = MEMORY_KINDS;
|
|
16
|
+
const KINDS: ReadonlySet<string> = new Set(SESSION_MEMORY_KINDS);
|
|
17
|
+
const ID = /^session:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
18
|
+
|
|
19
|
+
/** The scanner is applied on admission and replay; provenance is data too. */
|
|
20
|
+
export function sessionNoteError(input: SessionNoteInput): string | undefined {
|
|
21
|
+
if (!KINDS.has(input.kind)) return "invalid session note kind";
|
|
22
|
+
if (typeof input.text !== "string" || !input.text.trim() || input.text.length > MAX_SESSION_NOTE_CHARS) return `session note text must contain 1..${MAX_SESSION_NOTE_CHARS} characters`;
|
|
23
|
+
if (input.source !== undefined && (typeof input.source !== "string" || input.source.length > MAX_SESSION_SOURCE_CHARS)) return `session note source exceeds ${MAX_SESSION_SOURCE_CHARS} characters`;
|
|
24
|
+
for (const text of [input.text, input.source]) {
|
|
25
|
+
if (text === undefined) continue;
|
|
26
|
+
const scanned = scanContent(text);
|
|
27
|
+
if (!scanned.ok) return `session note rejected: ${scanned.reason ?? "content policy"}`;
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function parseEvent(value: unknown): SessionNoteEvent | undefined {
|
|
33
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
34
|
+
const record = value as Record<string, unknown>;
|
|
35
|
+
if (record.version !== 1) return undefined;
|
|
36
|
+
if (record.op === "forget") return typeof record.id === "string" && ID.test(record.id) ? { version: 1, op: "forget", id: record.id } : undefined;
|
|
37
|
+
if (record.op !== "save" || !record.note || typeof record.note !== "object" || Array.isArray(record.note)) return undefined;
|
|
38
|
+
const note = record.note as SessionNote;
|
|
39
|
+
if (typeof note.id !== "string" || !ID.test(note.id) || typeof note.recordedAt !== "string" || !Number.isFinite(Date.parse(note.recordedAt)) || typeof note.originSessionId !== "string" || !note.originSessionId || note.originSessionId.length > 256 || /[\s\u0000-\u001f\u007f]/u.test(note.originSessionId) || sessionNoteError(note)) return undefined;
|
|
40
|
+
return { version: 1, op: "save", note: { id: note.id, kind: note.kind, text: note.text, recordedAt: note.recordedAt, originSessionId: note.originSessionId, ...(note.source === undefined ? {} : { source: note.source }) } };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** All history owns the lifetime budget; only the selected branch owns visible notes. */
|
|
44
|
+
export function replaySessionNotes(branch: readonly unknown[], history: readonly unknown[]): SessionReplay {
|
|
45
|
+
let bytes = 0;
|
|
46
|
+
try {
|
|
47
|
+
for (const value of history) {
|
|
48
|
+
const encoded = JSON.stringify(value);
|
|
49
|
+
if (encoded === undefined) return { notes: [], events: history.length, bytes, error: "unaccountable session note history" };
|
|
50
|
+
bytes += new TextEncoder().encode(encoded).length;
|
|
51
|
+
if (!parseEvent(value)) return { notes: [], events: history.length, bytes, error: "unsupported or unsafe session note history" };
|
|
52
|
+
}
|
|
53
|
+
} catch { return { notes: [], events: history.length, bytes, error: "unaccountable session note history" }; }
|
|
54
|
+
if (history.length > MAX_SESSION_NOTE_EVENTS || bytes > MAX_SESSION_NOTE_BYTES) return { notes: [], events: history.length, bytes, error: "session note history exceeds its lifetime budget" };
|
|
55
|
+
const active = new Map<string, SessionNote>();
|
|
56
|
+
for (const value of branch) {
|
|
57
|
+
const event = parseEvent(value);
|
|
58
|
+
if (!event) return { notes: [], events: history.length, bytes, error: "unsupported or unsafe session note branch" };
|
|
59
|
+
if (event.op === "forget") active.delete(event.id);
|
|
60
|
+
else {
|
|
61
|
+
if (active.has(event.note.id)) return { notes: [], events: history.length, bytes, error: "duplicate session note identity" };
|
|
62
|
+
active.set(event.note.id, event.note);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (active.size > MAX_SESSION_NOTES) return { notes: [], events: history.length, bytes, error: "session note count exceeds its limit" };
|
|
66
|
+
return { notes: [...active.values()], events: history.length, bytes };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function sessionEventFits(state: SessionReplay, event: SessionNoteEvent): boolean {
|
|
70
|
+
return state.events < MAX_SESSION_NOTE_EVENTS && state.bytes + new TextEncoder().encode(JSON.stringify(event)).length <= MAX_SESSION_NOTE_BYTES;
|
|
71
|
+
}
|