@aeondave/pi-persona-mind 0.9.7 → 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 -137
- package/CHANGELOG.md +287 -282
- package/LICENSE +24 -24
- package/README.md +1 -1
- package/docs/DESIGN.md +605 -605
- package/docs/MEMORY-RESEARCH.md +163 -163
- package/package.json +11 -2
- package/src/core/auto-optimize.ts +323 -323
- package/src/core/backlog.ts +228 -228
- package/src/core/blocked.ts +38 -38
- package/src/core/capture.ts +155 -155
- 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 -189
- 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 -201
- 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 -565
- 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 -48
- package/src/extension/review-evidence.ts +222 -222
- package/src/extension/worker-memory-anchor.ts +28 -28
- package/src/session-memory.ts +127 -127
- 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 -58
package/src/core/backlog.ts
CHANGED
|
@@ -1,228 +1,228 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The backlog faculty — pure domain logic, no I/O.
|
|
3
|
-
*
|
|
4
|
-
* A backlog entry is a DEFERRED INTENT: a lead or task the supervisor means to act on but has not
|
|
5
|
-
* yet. It is project-scoped and time-bounded like short-term memory (default 48h, then deleted from
|
|
6
|
-
* the store). The lifecycle (`open`/`taken`/`done`/`dropped`) and optional wake are why it is not
|
|
7
|
-
* just another STM note; durable identity still belongs in long-term memory. The `persona` field is
|
|
8
|
-
* who created it, used only for the view filter.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { contentId } from "./ids.ts";
|
|
12
|
-
|
|
13
|
-
export const BACKLOG_STATES = ["open", "taken", "done", "dropped"] as const;
|
|
14
|
-
/** Same default life as short-term memory — working intent goes stale just as working notes do. */
|
|
15
|
-
export const DEFAULT_BACKLOG_TTL_HOURS = 48;
|
|
16
|
-
export type BacklogState = (typeof BACKLOG_STATES)[number];
|
|
17
|
-
export const DEFAULT_BACKLOG_MAX = 20;
|
|
18
|
-
export const MAX_BACKLOG_MAX = 50;
|
|
19
|
-
export const MAX_BACKLOG_TEXT_CHARS = 8_192;
|
|
20
|
-
export const MAX_BACKLOG_TAGS = 64;
|
|
21
|
-
export const MAX_BACKLOG_TAG_CHARS = 256;
|
|
22
|
-
export const MAX_BACKLOG_NOTE_CHARS = 1_024;
|
|
23
|
-
export const MAX_BACKLOG_ID_CHARS = 128;
|
|
24
|
-
/** Keep a bounded, deterministic terminal history while preserving every active item. */
|
|
25
|
-
export const MAX_BACKLOG_TERMINAL_ENTRIES = 1_000;
|
|
26
|
-
const MAX_BACKLOG_PERSONA_CHARS = 128;
|
|
27
|
-
const SAFE_BACKLOG_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]*$/;
|
|
28
|
-
|
|
29
|
-
/** Clamp a user-facing backlog page to a small, finite range. */
|
|
30
|
-
export function clampBacklogMax(max: number | undefined): number {
|
|
31
|
-
if (max === undefined || Number.isNaN(max)) return DEFAULT_BACKLOG_MAX;
|
|
32
|
-
if (max === Number.POSITIVE_INFINITY) return MAX_BACKLOG_MAX;
|
|
33
|
-
if (max === Number.NEGATIVE_INFINITY) return 1;
|
|
34
|
-
return Math.max(1, Math.min(MAX_BACKLOG_MAX, Math.floor(max)));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface BacklogEntry {
|
|
38
|
-
id: string;
|
|
39
|
-
text: string;
|
|
40
|
-
state: BacklogState;
|
|
41
|
-
tags: string[];
|
|
42
|
-
/** Who created it (for the persona view filter). */
|
|
43
|
-
persona?: string;
|
|
44
|
-
createdAt: string;
|
|
45
|
-
/** Optional wake time — a lightweight in-extension alarm re-armed on session start. */
|
|
46
|
-
dueAtEpochMs?: number;
|
|
47
|
-
/** When this lead is deleted from the store. Absent on legacy rows ⇒ createdAt + 48h. */
|
|
48
|
-
expiresAt?: string;
|
|
49
|
-
/** A note attached on done/drop (why). */
|
|
50
|
-
note?: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface BacklogInput {
|
|
54
|
-
text: string;
|
|
55
|
-
tags?: string[];
|
|
56
|
-
persona?: string;
|
|
57
|
-
/** Arm a wake `dueInSeconds` from now. */
|
|
58
|
-
dueInSeconds?: number;
|
|
59
|
-
/** Hours until auto-delete (default 48). A later due time extends life so the wake can still fire. */
|
|
60
|
-
ttlHours?: number;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function iso(now: number): string {
|
|
64
|
-
return new Date(now).toISOString();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function normTags(tags: readonly string[]): string[] {
|
|
68
|
-
return [...new Set(tags.map((t) => t.trim().toLowerCase()).filter(Boolean))].sort();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Build a fresh open backlog entry. */
|
|
72
|
-
export function makeBacklog(input: BacklogInput, now: number): BacklogEntry {
|
|
73
|
-
const text = input.text.trim();
|
|
74
|
-
const tags = normTags(input.tags ?? []);
|
|
75
|
-
const entry: BacklogEntry = {
|
|
76
|
-
id: contentId("backlog", text, tags),
|
|
77
|
-
text,
|
|
78
|
-
state: "open",
|
|
79
|
-
tags,
|
|
80
|
-
createdAt: iso(now),
|
|
81
|
-
};
|
|
82
|
-
if (input.persona) entry.persona = input.persona;
|
|
83
|
-
const ttlHours = input.ttlHours ?? DEFAULT_BACKLOG_TTL_HOURS;
|
|
84
|
-
let expiresAtMs = now + ttlHours * 3_600_000;
|
|
85
|
-
if (input.dueInSeconds !== undefined) {
|
|
86
|
-
const dueAtMs = now + Math.round(input.dueInSeconds * 1000);
|
|
87
|
-
entry.dueAtEpochMs = dueAtMs;
|
|
88
|
-
if (dueAtMs > expiresAtMs) expiresAtMs = dueAtMs;
|
|
89
|
-
}
|
|
90
|
-
entry.expiresAt = iso(expiresAtMs);
|
|
91
|
-
return entry;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** True once a backlog item has passed its expiry (legacy rows without expiresAt use createdAt + 48h). */
|
|
95
|
-
export function isExpired(entry: BacklogEntry, now: number): boolean {
|
|
96
|
-
const expiryMs =
|
|
97
|
-
entry.expiresAt !== undefined
|
|
98
|
-
? Date.parse(entry.expiresAt)
|
|
99
|
-
: Date.parse(entry.createdAt) + DEFAULT_BACKLOG_TTL_HOURS * 3_600_000;
|
|
100
|
-
return Number.isFinite(expiryMs) && now >= expiryMs;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** Drop expired backlog items (any state) so stale leads do not linger on disk. */
|
|
104
|
-
export function pruneExpired(entries: readonly BacklogEntry[], now: number): BacklogEntry[] {
|
|
105
|
-
return entries.filter((e) => !isExpired(e, now));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export interface TransitionResult {
|
|
109
|
-
ok: boolean;
|
|
110
|
-
entries: BacklogEntry[];
|
|
111
|
-
reason?: "not_found" | "invalid_transition";
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function legalTransition(from: BacklogState, to: BacklogState): boolean {
|
|
115
|
-
if (from === "taken" && to === "taken") return true; // take is idempotent — the user/model already claimed it
|
|
116
|
-
if (from === "open") return to === "taken" || to === "done" || to === "dropped";
|
|
117
|
-
if (from === "taken") return to === "done" || to === "dropped";
|
|
118
|
-
return false;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** Move one entry (by id) through the legal state machine, optionally attaching a note. */
|
|
122
|
-
export function transition(entries: readonly BacklogEntry[], id: string, state: BacklogState, note?: string): TransitionResult {
|
|
123
|
-
const found = entries.find((e) => e.id === id);
|
|
124
|
-
if (!found) return { ok: false, entries: [...entries], reason: "not_found" };
|
|
125
|
-
if (!legalTransition(found.state, state)) return { ok: false, entries: [...entries], reason: "invalid_transition" };
|
|
126
|
-
if (found.state === state) return { ok: true, entries: [...entries] };
|
|
127
|
-
const next = entries.map((e) => {
|
|
128
|
-
if (e.id !== id) return e;
|
|
129
|
-
const updated: BacklogEntry = { ...e, state };
|
|
130
|
-
if (note !== undefined) updated.note = note;
|
|
131
|
-
return updated;
|
|
132
|
-
});
|
|
133
|
-
return { ok: true, entries: next };
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/** Unfinished work: open + taken (excludes done/dropped). */
|
|
137
|
-
export function openItems(entries: readonly BacklogEntry[]): BacklogEntry[] {
|
|
138
|
-
return entries.filter((e) => e.state === "open" || e.state === "taken");
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Compact completed backlog history without ever dropping actionable work.
|
|
143
|
-
*
|
|
144
|
-
* Terminal entries are retained newest-first by creation time, with the id as a deterministic
|
|
145
|
-
* tie-breaker. The returned array keeps the source order so compaction does not perturb the normal
|
|
146
|
-
* backlog ordering; only terminal entries older than the explicit history limit are removed.
|
|
147
|
-
*/
|
|
148
|
-
export function compactTerminal(entries: readonly BacklogEntry[]): BacklogEntry[] {
|
|
149
|
-
const terminal = entries.filter((entry) => entry.state === "done" || entry.state === "dropped");
|
|
150
|
-
if (terminal.length <= MAX_BACKLOG_TERMINAL_ENTRIES) return [...entries];
|
|
151
|
-
const keep = new Set(
|
|
152
|
-
[...terminal]
|
|
153
|
-
.sort((a, b) => {
|
|
154
|
-
const createdB = Date.parse(b.createdAt);
|
|
155
|
-
const createdA = Date.parse(a.createdAt);
|
|
156
|
-
const created = (Number.isFinite(createdB) ? createdB : 0) - (Number.isFinite(createdA) ? createdA : 0);
|
|
157
|
-
return created !== 0 ? created : b.id.localeCompare(a.id);
|
|
158
|
-
})
|
|
159
|
-
.slice(0, MAX_BACKLOG_TERMINAL_ENTRIES),
|
|
160
|
-
);
|
|
161
|
-
return entries.filter((entry) => entry.state === "open" || entry.state === "taken" || keep.has(entry));
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/** Deterministic actionable ordering: claimed work, due work, then most recently created. */
|
|
165
|
-
export function orderBacklog(entries: readonly BacklogEntry[], now: number): BacklogEntry[] {
|
|
166
|
-
const stateRank = (state: BacklogState): number => (state === "taken" ? 0 : state === "open" ? 1 : state === "done" ? 2 : 3);
|
|
167
|
-
const dueRank = (entry: BacklogEntry): number => {
|
|
168
|
-
if (entry.dueAtEpochMs === undefined) return 2;
|
|
169
|
-
return entry.dueAtEpochMs <= now ? 0 : 1;
|
|
170
|
-
};
|
|
171
|
-
return [...entries].sort((a, b) => {
|
|
172
|
-
const state = stateRank(a.state) - stateRank(b.state);
|
|
173
|
-
if (state !== 0) return state;
|
|
174
|
-
const due = dueRank(a) - dueRank(b);
|
|
175
|
-
if (due !== 0) return due;
|
|
176
|
-
if (a.dueAtEpochMs !== undefined && b.dueAtEpochMs !== undefined) {
|
|
177
|
-
const dueTime = a.dueAtEpochMs - b.dueAtEpochMs;
|
|
178
|
-
if (Number.isFinite(dueTime) && dueTime !== 0) return dueTime;
|
|
179
|
-
}
|
|
180
|
-
const createdA = Date.parse(a.createdAt);
|
|
181
|
-
const createdB = Date.parse(b.createdAt);
|
|
182
|
-
const recency = (Number.isFinite(createdB) ? createdB : 0) - (Number.isFinite(createdA) ? createdA : 0);
|
|
183
|
-
if (recency !== 0) return recency;
|
|
184
|
-
return a.id.localeCompare(b.id);
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/** The persona's view by default (its own entries); `all` shows every persona's. */
|
|
189
|
-
export function viewFor(entries: readonly BacklogEntry[], persona: string, all: boolean): BacklogEntry[] {
|
|
190
|
-
if (all) return [...entries];
|
|
191
|
-
return entries.filter((e) => e.persona === persona);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function isState(v: unknown): v is BacklogState {
|
|
195
|
-
return typeof v === "string" && (BACKLOG_STATES as readonly string[]).includes(v);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function isStringArray(v: unknown): v is string[] {
|
|
199
|
-
return Array.isArray(v) && v.every((x) => typeof x === "string");
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/** Runtime validation for one persisted entry (JsonStore.validateEntry). null ⇒ drop. */
|
|
203
|
-
export function validateBacklog(raw: unknown): BacklogEntry | null {
|
|
204
|
-
if (!raw || typeof raw !== "object") return null;
|
|
205
|
-
const o = raw as Record<string, unknown>;
|
|
206
|
-
if (typeof o.id !== "string" || o.id.length > MAX_BACKLOG_ID_CHARS || !SAFE_BACKLOG_ID.test(o.id) || !isState(o.state)) return null;
|
|
207
|
-
if (typeof o.text !== "string" || o.text.length === 0 || o.text.length > MAX_BACKLOG_TEXT_CHARS) return null;
|
|
208
|
-
if (!isStringArray(o.tags) || o.tags.length > MAX_BACKLOG_TAGS || o.tags.some((tag) => tag.length > MAX_BACKLOG_TAG_CHARS)) return null;
|
|
209
|
-
if (typeof o.createdAt !== "string" || !Number.isFinite(Date.parse(o.createdAt))) return null;
|
|
210
|
-
const entry: BacklogEntry = { id: o.id, text: o.text, state: o.state, tags: o.tags, createdAt: o.createdAt };
|
|
211
|
-
if (o.persona !== undefined) {
|
|
212
|
-
if (typeof o.persona !== "string" || o.persona.length === 0 || o.persona.length > MAX_BACKLOG_PERSONA_CHARS) return null;
|
|
213
|
-
entry.persona = o.persona;
|
|
214
|
-
}
|
|
215
|
-
if (o.dueAtEpochMs !== undefined) {
|
|
216
|
-
if (typeof o.dueAtEpochMs !== "number" || !Number.isFinite(o.dueAtEpochMs)) return null;
|
|
217
|
-
entry.dueAtEpochMs = o.dueAtEpochMs;
|
|
218
|
-
}
|
|
219
|
-
if (o.note !== undefined) {
|
|
220
|
-
if (typeof o.note !== "string" || o.note.length > MAX_BACKLOG_NOTE_CHARS) return null;
|
|
221
|
-
entry.note = o.note;
|
|
222
|
-
}
|
|
223
|
-
if (o.expiresAt !== undefined) {
|
|
224
|
-
if (typeof o.expiresAt !== "string" || !Number.isFinite(Date.parse(o.expiresAt))) return null;
|
|
225
|
-
entry.expiresAt = o.expiresAt;
|
|
226
|
-
}
|
|
227
|
-
return entry;
|
|
228
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The backlog faculty — pure domain logic, no I/O.
|
|
3
|
+
*
|
|
4
|
+
* A backlog entry is a DEFERRED INTENT: a lead or task the supervisor means to act on but has not
|
|
5
|
+
* yet. It is project-scoped and time-bounded like short-term memory (default 48h, then deleted from
|
|
6
|
+
* the store). The lifecycle (`open`/`taken`/`done`/`dropped`) and optional wake are why it is not
|
|
7
|
+
* just another STM note; durable identity still belongs in long-term memory. The `persona` field is
|
|
8
|
+
* who created it, used only for the view filter.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { contentId } from "./ids.ts";
|
|
12
|
+
|
|
13
|
+
export const BACKLOG_STATES = ["open", "taken", "done", "dropped"] as const;
|
|
14
|
+
/** Same default life as short-term memory — working intent goes stale just as working notes do. */
|
|
15
|
+
export const DEFAULT_BACKLOG_TTL_HOURS = 48;
|
|
16
|
+
export type BacklogState = (typeof BACKLOG_STATES)[number];
|
|
17
|
+
export const DEFAULT_BACKLOG_MAX = 20;
|
|
18
|
+
export const MAX_BACKLOG_MAX = 50;
|
|
19
|
+
export const MAX_BACKLOG_TEXT_CHARS = 8_192;
|
|
20
|
+
export const MAX_BACKLOG_TAGS = 64;
|
|
21
|
+
export const MAX_BACKLOG_TAG_CHARS = 256;
|
|
22
|
+
export const MAX_BACKLOG_NOTE_CHARS = 1_024;
|
|
23
|
+
export const MAX_BACKLOG_ID_CHARS = 128;
|
|
24
|
+
/** Keep a bounded, deterministic terminal history while preserving every active item. */
|
|
25
|
+
export const MAX_BACKLOG_TERMINAL_ENTRIES = 1_000;
|
|
26
|
+
const MAX_BACKLOG_PERSONA_CHARS = 128;
|
|
27
|
+
const SAFE_BACKLOG_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]*$/;
|
|
28
|
+
|
|
29
|
+
/** Clamp a user-facing backlog page to a small, finite range. */
|
|
30
|
+
export function clampBacklogMax(max: number | undefined): number {
|
|
31
|
+
if (max === undefined || Number.isNaN(max)) return DEFAULT_BACKLOG_MAX;
|
|
32
|
+
if (max === Number.POSITIVE_INFINITY) return MAX_BACKLOG_MAX;
|
|
33
|
+
if (max === Number.NEGATIVE_INFINITY) return 1;
|
|
34
|
+
return Math.max(1, Math.min(MAX_BACKLOG_MAX, Math.floor(max)));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface BacklogEntry {
|
|
38
|
+
id: string;
|
|
39
|
+
text: string;
|
|
40
|
+
state: BacklogState;
|
|
41
|
+
tags: string[];
|
|
42
|
+
/** Who created it (for the persona view filter). */
|
|
43
|
+
persona?: string;
|
|
44
|
+
createdAt: string;
|
|
45
|
+
/** Optional wake time — a lightweight in-extension alarm re-armed on session start. */
|
|
46
|
+
dueAtEpochMs?: number;
|
|
47
|
+
/** When this lead is deleted from the store. Absent on legacy rows ⇒ createdAt + 48h. */
|
|
48
|
+
expiresAt?: string;
|
|
49
|
+
/** A note attached on done/drop (why). */
|
|
50
|
+
note?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface BacklogInput {
|
|
54
|
+
text: string;
|
|
55
|
+
tags?: string[];
|
|
56
|
+
persona?: string;
|
|
57
|
+
/** Arm a wake `dueInSeconds` from now. */
|
|
58
|
+
dueInSeconds?: number;
|
|
59
|
+
/** Hours until auto-delete (default 48). A later due time extends life so the wake can still fire. */
|
|
60
|
+
ttlHours?: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function iso(now: number): string {
|
|
64
|
+
return new Date(now).toISOString();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function normTags(tags: readonly string[]): string[] {
|
|
68
|
+
return [...new Set(tags.map((t) => t.trim().toLowerCase()).filter(Boolean))].sort();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Build a fresh open backlog entry. */
|
|
72
|
+
export function makeBacklog(input: BacklogInput, now: number): BacklogEntry {
|
|
73
|
+
const text = input.text.trim();
|
|
74
|
+
const tags = normTags(input.tags ?? []);
|
|
75
|
+
const entry: BacklogEntry = {
|
|
76
|
+
id: contentId("backlog", text, tags),
|
|
77
|
+
text,
|
|
78
|
+
state: "open",
|
|
79
|
+
tags,
|
|
80
|
+
createdAt: iso(now),
|
|
81
|
+
};
|
|
82
|
+
if (input.persona) entry.persona = input.persona;
|
|
83
|
+
const ttlHours = input.ttlHours ?? DEFAULT_BACKLOG_TTL_HOURS;
|
|
84
|
+
let expiresAtMs = now + ttlHours * 3_600_000;
|
|
85
|
+
if (input.dueInSeconds !== undefined) {
|
|
86
|
+
const dueAtMs = now + Math.round(input.dueInSeconds * 1000);
|
|
87
|
+
entry.dueAtEpochMs = dueAtMs;
|
|
88
|
+
if (dueAtMs > expiresAtMs) expiresAtMs = dueAtMs;
|
|
89
|
+
}
|
|
90
|
+
entry.expiresAt = iso(expiresAtMs);
|
|
91
|
+
return entry;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** True once a backlog item has passed its expiry (legacy rows without expiresAt use createdAt + 48h). */
|
|
95
|
+
export function isExpired(entry: BacklogEntry, now: number): boolean {
|
|
96
|
+
const expiryMs =
|
|
97
|
+
entry.expiresAt !== undefined
|
|
98
|
+
? Date.parse(entry.expiresAt)
|
|
99
|
+
: Date.parse(entry.createdAt) + DEFAULT_BACKLOG_TTL_HOURS * 3_600_000;
|
|
100
|
+
return Number.isFinite(expiryMs) && now >= expiryMs;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Drop expired backlog items (any state) so stale leads do not linger on disk. */
|
|
104
|
+
export function pruneExpired(entries: readonly BacklogEntry[], now: number): BacklogEntry[] {
|
|
105
|
+
return entries.filter((e) => !isExpired(e, now));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface TransitionResult {
|
|
109
|
+
ok: boolean;
|
|
110
|
+
entries: BacklogEntry[];
|
|
111
|
+
reason?: "not_found" | "invalid_transition";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function legalTransition(from: BacklogState, to: BacklogState): boolean {
|
|
115
|
+
if (from === "taken" && to === "taken") return true; // take is idempotent — the user/model already claimed it
|
|
116
|
+
if (from === "open") return to === "taken" || to === "done" || to === "dropped";
|
|
117
|
+
if (from === "taken") return to === "done" || to === "dropped";
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Move one entry (by id) through the legal state machine, optionally attaching a note. */
|
|
122
|
+
export function transition(entries: readonly BacklogEntry[], id: string, state: BacklogState, note?: string): TransitionResult {
|
|
123
|
+
const found = entries.find((e) => e.id === id);
|
|
124
|
+
if (!found) return { ok: false, entries: [...entries], reason: "not_found" };
|
|
125
|
+
if (!legalTransition(found.state, state)) return { ok: false, entries: [...entries], reason: "invalid_transition" };
|
|
126
|
+
if (found.state === state) return { ok: true, entries: [...entries] };
|
|
127
|
+
const next = entries.map((e) => {
|
|
128
|
+
if (e.id !== id) return e;
|
|
129
|
+
const updated: BacklogEntry = { ...e, state };
|
|
130
|
+
if (note !== undefined) updated.note = note;
|
|
131
|
+
return updated;
|
|
132
|
+
});
|
|
133
|
+
return { ok: true, entries: next };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Unfinished work: open + taken (excludes done/dropped). */
|
|
137
|
+
export function openItems(entries: readonly BacklogEntry[]): BacklogEntry[] {
|
|
138
|
+
return entries.filter((e) => e.state === "open" || e.state === "taken");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Compact completed backlog history without ever dropping actionable work.
|
|
143
|
+
*
|
|
144
|
+
* Terminal entries are retained newest-first by creation time, with the id as a deterministic
|
|
145
|
+
* tie-breaker. The returned array keeps the source order so compaction does not perturb the normal
|
|
146
|
+
* backlog ordering; only terminal entries older than the explicit history limit are removed.
|
|
147
|
+
*/
|
|
148
|
+
export function compactTerminal(entries: readonly BacklogEntry[]): BacklogEntry[] {
|
|
149
|
+
const terminal = entries.filter((entry) => entry.state === "done" || entry.state === "dropped");
|
|
150
|
+
if (terminal.length <= MAX_BACKLOG_TERMINAL_ENTRIES) return [...entries];
|
|
151
|
+
const keep = new Set(
|
|
152
|
+
[...terminal]
|
|
153
|
+
.sort((a, b) => {
|
|
154
|
+
const createdB = Date.parse(b.createdAt);
|
|
155
|
+
const createdA = Date.parse(a.createdAt);
|
|
156
|
+
const created = (Number.isFinite(createdB) ? createdB : 0) - (Number.isFinite(createdA) ? createdA : 0);
|
|
157
|
+
return created !== 0 ? created : b.id.localeCompare(a.id);
|
|
158
|
+
})
|
|
159
|
+
.slice(0, MAX_BACKLOG_TERMINAL_ENTRIES),
|
|
160
|
+
);
|
|
161
|
+
return entries.filter((entry) => entry.state === "open" || entry.state === "taken" || keep.has(entry));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Deterministic actionable ordering: claimed work, due work, then most recently created. */
|
|
165
|
+
export function orderBacklog(entries: readonly BacklogEntry[], now: number): BacklogEntry[] {
|
|
166
|
+
const stateRank = (state: BacklogState): number => (state === "taken" ? 0 : state === "open" ? 1 : state === "done" ? 2 : 3);
|
|
167
|
+
const dueRank = (entry: BacklogEntry): number => {
|
|
168
|
+
if (entry.dueAtEpochMs === undefined) return 2;
|
|
169
|
+
return entry.dueAtEpochMs <= now ? 0 : 1;
|
|
170
|
+
};
|
|
171
|
+
return [...entries].sort((a, b) => {
|
|
172
|
+
const state = stateRank(a.state) - stateRank(b.state);
|
|
173
|
+
if (state !== 0) return state;
|
|
174
|
+
const due = dueRank(a) - dueRank(b);
|
|
175
|
+
if (due !== 0) return due;
|
|
176
|
+
if (a.dueAtEpochMs !== undefined && b.dueAtEpochMs !== undefined) {
|
|
177
|
+
const dueTime = a.dueAtEpochMs - b.dueAtEpochMs;
|
|
178
|
+
if (Number.isFinite(dueTime) && dueTime !== 0) return dueTime;
|
|
179
|
+
}
|
|
180
|
+
const createdA = Date.parse(a.createdAt);
|
|
181
|
+
const createdB = Date.parse(b.createdAt);
|
|
182
|
+
const recency = (Number.isFinite(createdB) ? createdB : 0) - (Number.isFinite(createdA) ? createdA : 0);
|
|
183
|
+
if (recency !== 0) return recency;
|
|
184
|
+
return a.id.localeCompare(b.id);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The persona's view by default (its own entries); `all` shows every persona's. */
|
|
189
|
+
export function viewFor(entries: readonly BacklogEntry[], persona: string, all: boolean): BacklogEntry[] {
|
|
190
|
+
if (all) return [...entries];
|
|
191
|
+
return entries.filter((e) => e.persona === persona);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function isState(v: unknown): v is BacklogState {
|
|
195
|
+
return typeof v === "string" && (BACKLOG_STATES as readonly string[]).includes(v);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function isStringArray(v: unknown): v is string[] {
|
|
199
|
+
return Array.isArray(v) && v.every((x) => typeof x === "string");
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Runtime validation for one persisted entry (JsonStore.validateEntry). null ⇒ drop. */
|
|
203
|
+
export function validateBacklog(raw: unknown): BacklogEntry | null {
|
|
204
|
+
if (!raw || typeof raw !== "object") return null;
|
|
205
|
+
const o = raw as Record<string, unknown>;
|
|
206
|
+
if (typeof o.id !== "string" || o.id.length > MAX_BACKLOG_ID_CHARS || !SAFE_BACKLOG_ID.test(o.id) || !isState(o.state)) return null;
|
|
207
|
+
if (typeof o.text !== "string" || o.text.length === 0 || o.text.length > MAX_BACKLOG_TEXT_CHARS) return null;
|
|
208
|
+
if (!isStringArray(o.tags) || o.tags.length > MAX_BACKLOG_TAGS || o.tags.some((tag) => tag.length > MAX_BACKLOG_TAG_CHARS)) return null;
|
|
209
|
+
if (typeof o.createdAt !== "string" || !Number.isFinite(Date.parse(o.createdAt))) return null;
|
|
210
|
+
const entry: BacklogEntry = { id: o.id, text: o.text, state: o.state, tags: o.tags, createdAt: o.createdAt };
|
|
211
|
+
if (o.persona !== undefined) {
|
|
212
|
+
if (typeof o.persona !== "string" || o.persona.length === 0 || o.persona.length > MAX_BACKLOG_PERSONA_CHARS) return null;
|
|
213
|
+
entry.persona = o.persona;
|
|
214
|
+
}
|
|
215
|
+
if (o.dueAtEpochMs !== undefined) {
|
|
216
|
+
if (typeof o.dueAtEpochMs !== "number" || !Number.isFinite(o.dueAtEpochMs)) return null;
|
|
217
|
+
entry.dueAtEpochMs = o.dueAtEpochMs;
|
|
218
|
+
}
|
|
219
|
+
if (o.note !== undefined) {
|
|
220
|
+
if (typeof o.note !== "string" || o.note.length > MAX_BACKLOG_NOTE_CHARS) return null;
|
|
221
|
+
entry.note = o.note;
|
|
222
|
+
}
|
|
223
|
+
if (o.expiresAt !== undefined) {
|
|
224
|
+
if (typeof o.expiresAt !== "string" || !Number.isFinite(Date.parse(o.expiresAt))) return null;
|
|
225
|
+
entry.expiresAt = o.expiresAt;
|
|
226
|
+
}
|
|
227
|
+
return entry;
|
|
228
|
+
}
|
package/src/core/blocked.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Deterministic, model-free detection of a DELEGATED leg that came back BLOCKED — the delegation-path
|
|
3
|
-
* mirror of capture.ts. A blocked/unknown leg is deferred intent by definition (a thread to come back
|
|
4
|
-
* to), so surfacing it as a backlog candidate turns a surrendered hand-off into captured intent before
|
|
5
|
-
* it is silently lost. No LLM, no network: it keys on the SAME explicit protocol markers pi-persona's
|
|
6
|
-
* PersistenceNudge uses — `[BLOCKED …]` (operator protocol) and `FLAG: UNKNOWN` (a CTF leg giving up).
|
|
7
|
-
*
|
|
8
|
-
* Like the capture cue, this is a NUDGE source, never an auto-writer: a false positive costs nothing
|
|
9
|
-
* more than one ignorable status-line hint the supervisor can dismiss.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
export interface BlockedCue {
|
|
13
|
-
/** The matched marker (with its inline reason, if any), trimmed and length-capped, for the nudge. */
|
|
14
|
-
snippet: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Same markers as pi-persona's SURRENDER_MARKERS; capture the bracketed reason so the nudge can echo it.
|
|
18
|
-
const MARKERS: readonly RegExp[] = [/\[BLOCKED\b[^\]\n]*\]?/i, /\bFLAG:\s*UNKNOWN\b/i];
|
|
19
|
-
const MAX_SNIPPET = 160;
|
|
20
|
-
|
|
21
|
-
function safeStatusText(text: string): string {
|
|
22
|
-
return text
|
|
23
|
-
.replace(/\u001b\[[0-?]*[ -/]*[@-~]/g, "")
|
|
24
|
-
.replace(/[\u0000-\u001f\u007f-\u009f]/gu, " ")
|
|
25
|
-
.replace(/\s+/g, " ")
|
|
26
|
-
.trim()
|
|
27
|
-
.slice(0, MAX_SNIPPET);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** The first blocked/unknown marker in `text`, or null when the report is clean or not a string. */
|
|
31
|
-
export function detectBlockedLeg(text: string): BlockedCue | null {
|
|
32
|
-
if (typeof text !== "string" || !text.trim()) return null;
|
|
33
|
-
for (const re of MARKERS) {
|
|
34
|
-
const m = re.exec(text);
|
|
35
|
-
if (m) return { snippet: safeStatusText(m[0]) };
|
|
36
|
-
}
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic, model-free detection of a DELEGATED leg that came back BLOCKED — the delegation-path
|
|
3
|
+
* mirror of capture.ts. A blocked/unknown leg is deferred intent by definition (a thread to come back
|
|
4
|
+
* to), so surfacing it as a backlog candidate turns a surrendered hand-off into captured intent before
|
|
5
|
+
* it is silently lost. No LLM, no network: it keys on the SAME explicit protocol markers pi-persona's
|
|
6
|
+
* PersistenceNudge uses — `[BLOCKED …]` (operator protocol) and `FLAG: UNKNOWN` (a CTF leg giving up).
|
|
7
|
+
*
|
|
8
|
+
* Like the capture cue, this is a NUDGE source, never an auto-writer: a false positive costs nothing
|
|
9
|
+
* more than one ignorable status-line hint the supervisor can dismiss.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export interface BlockedCue {
|
|
13
|
+
/** The matched marker (with its inline reason, if any), trimmed and length-capped, for the nudge. */
|
|
14
|
+
snippet: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Same markers as pi-persona's SURRENDER_MARKERS; capture the bracketed reason so the nudge can echo it.
|
|
18
|
+
const MARKERS: readonly RegExp[] = [/\[BLOCKED\b[^\]\n]*\]?/i, /\bFLAG:\s*UNKNOWN\b/i];
|
|
19
|
+
const MAX_SNIPPET = 160;
|
|
20
|
+
|
|
21
|
+
function safeStatusText(text: string): string {
|
|
22
|
+
return text
|
|
23
|
+
.replace(/\u001b\[[0-?]*[ -/]*[@-~]/g, "")
|
|
24
|
+
.replace(/[\u0000-\u001f\u007f-\u009f]/gu, " ")
|
|
25
|
+
.replace(/\s+/g, " ")
|
|
26
|
+
.trim()
|
|
27
|
+
.slice(0, MAX_SNIPPET);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The first blocked/unknown marker in `text`, or null when the report is clean or not a string. */
|
|
31
|
+
export function detectBlockedLeg(text: string): BlockedCue | null {
|
|
32
|
+
if (typeof text !== "string" || !text.trim()) return null;
|
|
33
|
+
for (const re of MARKERS) {
|
|
34
|
+
const m = re.exec(text);
|
|
35
|
+
if (m) return { snippet: safeStatusText(m[0]) };
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|