@fractaal/pi-goal-x 0.20.1 → 0.28.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/README.md +42 -290
- package/dist/goal-auditor.d.ts +7 -61
- package/dist/goal-auditor.js +98 -300
- package/dist/goal-auditor.js.map +1 -1
- package/dist/goal-contract.d.ts +205 -0
- package/dist/goal-contract.js +135 -0
- package/dist/goal-contract.js.map +1 -0
- package/dist/goal-transcript-events.d.ts +54 -0
- package/dist/goal-transcript-events.js +177 -0
- package/dist/goal-transcript-events.js.map +1 -0
- package/dist/goal-widget.d.ts +13 -0
- package/dist/goal-widget.js +45 -0
- package/dist/goal-widget.js.map +1 -0
- package/dist/goal.d.ts +8 -49
- package/dist/goal.js +519 -3643
- package/dist/goal.js.map +1 -1
- package/dist/legacy-migration.d.ts +61 -0
- package/dist/legacy-migration.js +306 -0
- package/dist/legacy-migration.js.map +1 -0
- package/docs/architecture.md +14 -232
- package/extensions/goal-auditor.ts +108 -363
- package/extensions/goal-contract.ts +149 -0
- package/extensions/goal-transcript-events.ts +194 -0
- package/extensions/goal-widget.ts +50 -0
- package/extensions/goal.ts +557 -3927
- package/extensions/legacy-migration.ts +340 -0
- package/package.json +46 -19
- package/dist/goal-compaction.d.ts +0 -10
- package/dist/goal-compaction.js +0 -104
- package/dist/goal-compaction.js.map +0 -1
- package/dist/goal-core.d.ts +0 -19
- package/dist/goal-core.js +0 -66
- package/dist/goal-core.js.map +0 -1
- package/dist/goal-draft.d.ts +0 -71
- package/dist/goal-draft.js +0 -201
- package/dist/goal-draft.js.map +0 -1
- package/dist/goal-ledger.d.ts +0 -139
- package/dist/goal-ledger.js +0 -306
- package/dist/goal-ledger.js.map +0 -1
- package/dist/goal-policy.d.ts +0 -127
- package/dist/goal-policy.js +0 -359
- package/dist/goal-policy.js.map +0 -1
- package/dist/goal-pool.d.ts +0 -17
- package/dist/goal-pool.js +0 -85
- package/dist/goal-pool.js.map +0 -1
- package/dist/goal-questionnaire.d.ts +0 -56
- package/dist/goal-questionnaire.js +0 -733
- package/dist/goal-questionnaire.js.map +0 -1
- package/dist/goal-record.d.ts +0 -94
- package/dist/goal-record.js +0 -155
- package/dist/goal-record.js.map +0 -1
- package/dist/goal-settings.d.ts +0 -56
- package/dist/goal-settings.js +0 -184
- package/dist/goal-settings.js.map +0 -1
- package/dist/goal-tool-names.d.ts +0 -20
- package/dist/goal-tool-names.js +0 -69
- package/dist/goal-tool-names.js.map +0 -1
- package/dist/prompts/goal-prompts.d.ts +0 -17
- package/dist/prompts/goal-prompts.js +0 -304
- package/dist/prompts/goal-prompts.js.map +0 -1
- package/dist/storage/goal-files.d.ts +0 -40
- package/dist/storage/goal-files.js +0 -320
- package/dist/storage/goal-files.js.map +0 -1
- package/dist/widgets/goal-escape-dialog.d.ts +0 -15
- package/dist/widgets/goal-escape-dialog.js +0 -118
- package/dist/widgets/goal-escape-dialog.js.map +0 -1
- package/dist/widgets/goal-notifications.d.ts +0 -5
- package/dist/widgets/goal-notifications.js +0 -9
- package/dist/widgets/goal-notifications.js.map +0 -1
- package/dist/widgets/goal-widget.d.ts +0 -58
- package/dist/widgets/goal-widget.js +0 -282
- package/dist/widgets/goal-widget.js.map +0 -1
- package/dist/widgets/task-list-overlay.d.ts +0 -8
- package/dist/widgets/task-list-overlay.js +0 -321
- package/dist/widgets/task-list-overlay.js.map +0 -1
- package/docs/agent-flow-design.md +0 -432
- package/docs/agentic-runtime-prd.md +0 -764
- package/docs/goal-ts-refactor-test-strategy.md +0 -82
- package/docs/pi-autoresearch-survey.md +0 -45
- package/extensions/goal-compaction.ts +0 -133
- package/extensions/goal-core.ts +0 -77
- package/extensions/goal-draft.ts +0 -263
- package/extensions/goal-ledger.ts +0 -354
- package/extensions/goal-policy.ts +0 -389
- package/extensions/goal-pool.ts +0 -98
- package/extensions/goal-questionnaire.ts +0 -779
- package/extensions/goal-record.ts +0 -256
- package/extensions/goal-settings.ts +0 -181
- package/extensions/goal-tool-names.ts +0 -76
- package/extensions/prompts/goal-prompts.ts +0 -308
- package/extensions/storage/goal-files.ts +0 -352
- package/extensions/widgets/goal-escape-dialog.ts +0 -146
- package/extensions/widgets/goal-notifications.ts +0 -9
- package/extensions/widgets/goal-widget.ts +0 -378
- package/extensions/widgets/task-list-overlay.ts +0 -389
|
@@ -1,354 +0,0 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import { normalizeRelPath, nowIso, safeIdPart, type GoalRecord } from "./goal-record.ts";
|
|
4
|
-
|
|
5
|
-
export const GOAL_LEDGER_FILE = ".pi/goals/goal_events.jsonl";
|
|
6
|
-
export const SESSION_GOAL_LEDGER_DIR = ".pi/goals/sessions";
|
|
7
|
-
|
|
8
|
-
export type GoalLedgerEvent =
|
|
9
|
-
| { type: "goal_created"; goalId: string; objective: string; sisyphus: boolean; autoContinue: boolean; at: string }
|
|
10
|
-
| { type: "goal_focused"; goalId: string; reason: string; at: string }
|
|
11
|
-
| { type: "goal_unfocused"; reason: string; at: string }
|
|
12
|
-
| { type: "goal_paused"; goalId: string; reason: string; suggestedAction?: string; status?: "paused"; at: string }
|
|
13
|
-
| { type: "goal_resumed"; goalId: string; reason: string; at: string }
|
|
14
|
-
| { type: "goal_tweaked"; goalId: string; changeSummary: string; at: string }
|
|
15
|
-
| { type: "completion_requested"; goalId: string; summary?: string; at: string }
|
|
16
|
-
| { type: "audit_started"; goalId: string; provider?: string; model?: string; thinkingLevel?: string; at: string }
|
|
17
|
-
| { type: "audit_result"; goalId: string; verdict: "approved" | "disapproved" | "error"; report: string; at: string }
|
|
18
|
-
| { type: "audit_skipped"; goalId: string; reason: "disabled" | "user_aborted"; provider?: string; model?: string; thinkingLevel?: string; at: string }
|
|
19
|
-
| { type: "goal_completed"; goalId: string; archivePath?: string; at: string }
|
|
20
|
-
| { type: "goal_aborted"; goalId: string; reason: string; archivePath?: string; at: string }
|
|
21
|
-
| { type: "task_list_set"; goalId: string; taskCount: number; blockCompletion: boolean; at: string }
|
|
22
|
-
| { type: "task_complete"; goalId: string; taskId: string; evidence?: string; at: string }
|
|
23
|
-
| { type: "task_skipped"; goalId: string; taskId: string; reason: string; at: string };
|
|
24
|
-
|
|
25
|
-
export interface GoalLedgerContext {
|
|
26
|
-
cwd: string;
|
|
27
|
-
/** Current Pi session id. When present, ledger storage is session-scoped. */
|
|
28
|
-
sessionId?: string;
|
|
29
|
-
/** Pi's runtime session manager; used when callers pass the full ExtensionContext. */
|
|
30
|
-
sessionManager?: { getSessionId?: () => string };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface GoalLedgerReadResult {
|
|
34
|
-
events: GoalLedgerEvent[];
|
|
35
|
-
malformed: number;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface ReconstructedGoalState {
|
|
39
|
-
goalId: string;
|
|
40
|
-
latestStatus: "active" | "paused" | "complete" | "aborted" | "unknown";
|
|
41
|
-
latestFocus: boolean;
|
|
42
|
-
latestPauseReason?: string;
|
|
43
|
-
latestPauseSuggestedAction?: string;
|
|
44
|
-
latestAuditorResult?: { verdict: "approved" | "disapproved" | "error"; report: string; at: string };
|
|
45
|
-
createdAt?: string;
|
|
46
|
-
completedAt?: string;
|
|
47
|
-
abortedAt?: string;
|
|
48
|
-
tweakedAt?: string;
|
|
49
|
-
resumedAt?: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface ReconstructedLedgerState {
|
|
53
|
-
focusedGoalId: string | null;
|
|
54
|
-
goals: Map<string, ReconstructedGoalState>;
|
|
55
|
-
terminalGoals: Map<string, ReconstructedGoalState>;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function safeGoalId(value: string): string {
|
|
59
|
-
return safeIdPart(value);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function legacyGoalLedgerPath(ctx: GoalLedgerContext): string {
|
|
63
|
-
return path.resolve(ctx.cwd, normalizeRelPath(GOAL_LEDGER_FILE));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function goalLedgerPath(ctx: GoalLedgerContext): string {
|
|
67
|
-
const sessionId = ctx.sessionId ?? ctx.sessionManager?.getSessionId?.();
|
|
68
|
-
if (sessionId) {
|
|
69
|
-
return path.resolve(ctx.cwd, SESSION_GOAL_LEDGER_DIR, safeIdPart(sessionId), "goal_events.jsonl");
|
|
70
|
-
}
|
|
71
|
-
return legacyGoalLedgerPath(ctx);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function appendGoalEvent(ctx: GoalLedgerContext, event: GoalLedgerEvent): void {
|
|
75
|
-
const filePath = goalLedgerPath(ctx);
|
|
76
|
-
const dir = path.dirname(filePath);
|
|
77
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
78
|
-
|
|
79
|
-
const line = JSON.stringify(event) + "\n";
|
|
80
|
-
const tempPath = `${filePath}.${process.pid}.${Date.now()}.tmp`;
|
|
81
|
-
let appended = false;
|
|
82
|
-
try {
|
|
83
|
-
fs.writeFileSync(tempPath, line, { flag: "wx", encoding: "utf8" });
|
|
84
|
-
fs.appendFileSync(filePath, fs.readFileSync(tempPath, "utf8"), "utf8");
|
|
85
|
-
appended = true;
|
|
86
|
-
} catch {
|
|
87
|
-
// If temp write fails, try direct append as fallback.
|
|
88
|
-
// Skip fallback only if the primary append already succeeded.
|
|
89
|
-
if (!appended) {
|
|
90
|
-
try {
|
|
91
|
-
fs.appendFileSync(filePath, line, "utf8");
|
|
92
|
-
appended = true;
|
|
93
|
-
} catch {
|
|
94
|
-
// Ledger append failure should not crash the transaction.
|
|
95
|
-
// Callers that need strict durability can check the return.
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
} finally {
|
|
99
|
-
try {
|
|
100
|
-
fs.unlinkSync(tempPath);
|
|
101
|
-
} catch {
|
|
102
|
-
// Temp file may not exist; ignore cleanup failure.
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function readGoalLedgerFile(filePath: string): GoalLedgerReadResult {
|
|
108
|
-
let content: string;
|
|
109
|
-
try {
|
|
110
|
-
content = fs.readFileSync(filePath, "utf8");
|
|
111
|
-
} catch {
|
|
112
|
-
return { events: [], malformed: 0 };
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const events: GoalLedgerEvent[] = [];
|
|
116
|
-
let malformed = 0;
|
|
117
|
-
for (const line of content.split("\n")) {
|
|
118
|
-
const trimmed = line.trim();
|
|
119
|
-
if (!trimmed) continue;
|
|
120
|
-
try {
|
|
121
|
-
const parsed = JSON.parse(trimmed) as unknown;
|
|
122
|
-
if (isValidLedgerEvent(parsed)) {
|
|
123
|
-
events.push(sanitizeEvent(parsed));
|
|
124
|
-
} else {
|
|
125
|
-
malformed++;
|
|
126
|
-
}
|
|
127
|
-
} catch {
|
|
128
|
-
malformed++;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return { events, malformed };
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export function readGoalLedger(ctx: GoalLedgerContext): GoalLedgerReadResult {
|
|
135
|
-
return readGoalLedgerFile(goalLedgerPath(ctx));
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function readLegacyGoalLedger(ctx: GoalLedgerContext): GoalLedgerReadResult {
|
|
139
|
-
return readGoalLedgerFile(legacyGoalLedgerPath(ctx));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function isValidLedgerEvent(value: unknown): value is GoalLedgerEvent {
|
|
143
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
144
|
-
const obj = value as Record<string, unknown>;
|
|
145
|
-
if (typeof obj.type !== "string") return false;
|
|
146
|
-
if (typeof obj.at !== "string") return false;
|
|
147
|
-
const type = obj.type as GoalLedgerEvent["type"];
|
|
148
|
-
switch (type) {
|
|
149
|
-
case "goal_created":
|
|
150
|
-
return typeof obj.goalId === "string" && typeof obj.objective === "string" && typeof obj.sisyphus === "boolean" && typeof obj.autoContinue === "boolean";
|
|
151
|
-
case "goal_focused":
|
|
152
|
-
return typeof obj.goalId === "string" && typeof obj.reason === "string";
|
|
153
|
-
case "goal_unfocused":
|
|
154
|
-
return typeof obj.reason === "string";
|
|
155
|
-
case "goal_paused":
|
|
156
|
-
return typeof obj.goalId === "string" && typeof obj.reason === "string" && (obj.suggestedAction === undefined || typeof obj.suggestedAction === "string") && (obj.status === undefined || obj.status === "paused");
|
|
157
|
-
case "goal_resumed":
|
|
158
|
-
return typeof obj.goalId === "string" && typeof obj.reason === "string";
|
|
159
|
-
case "goal_tweaked":
|
|
160
|
-
return typeof obj.goalId === "string" && typeof obj.changeSummary === "string";
|
|
161
|
-
case "completion_requested":
|
|
162
|
-
return typeof obj.goalId === "string" && (obj.summary === undefined || typeof obj.summary === "string");
|
|
163
|
-
case "audit_started":
|
|
164
|
-
return typeof obj.goalId === "string" && (obj.provider === undefined || typeof obj.provider === "string") && (obj.model === undefined || typeof obj.model === "string") && (obj.thinkingLevel === undefined || typeof obj.thinkingLevel === "string");
|
|
165
|
-
case "audit_result":
|
|
166
|
-
return typeof obj.goalId === "string" && (obj.verdict === "approved" || obj.verdict === "disapproved" || obj.verdict === "error") && typeof obj.report === "string";
|
|
167
|
-
case "audit_skipped":
|
|
168
|
-
return typeof obj.goalId === "string" && (obj.reason === "disabled" || obj.reason === "user_aborted") && (obj.provider === undefined || typeof obj.provider === "string") && (obj.model === undefined || typeof obj.model === "string") && (obj.thinkingLevel === undefined || typeof obj.thinkingLevel === "string");
|
|
169
|
-
case "goal_completed":
|
|
170
|
-
return typeof obj.goalId === "string" && (obj.archivePath === undefined || typeof obj.archivePath === "string");
|
|
171
|
-
case "goal_aborted":
|
|
172
|
-
return typeof obj.goalId === "string" && typeof obj.reason === "string" && (obj.archivePath === undefined || typeof obj.archivePath === "string");
|
|
173
|
-
case "task_list_set":
|
|
174
|
-
return typeof obj.goalId === "string" && typeof obj.taskCount === "number" && typeof obj.blockCompletion === "boolean";
|
|
175
|
-
case "task_complete":
|
|
176
|
-
return typeof obj.goalId === "string" && typeof obj.taskId === "string" && (obj.evidence === undefined || typeof obj.evidence === "string");
|
|
177
|
-
case "task_skipped":
|
|
178
|
-
return typeof obj.goalId === "string" && typeof obj.taskId === "string" && typeof obj.reason === "string";
|
|
179
|
-
default:
|
|
180
|
-
return false;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function sanitizeEvent(event: GoalLedgerEvent): GoalLedgerEvent {
|
|
185
|
-
switch (event.type) {
|
|
186
|
-
case "goal_created":
|
|
187
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
188
|
-
case "goal_focused":
|
|
189
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
190
|
-
case "goal_paused":
|
|
191
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
192
|
-
case "goal_resumed":
|
|
193
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
194
|
-
case "goal_tweaked":
|
|
195
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
196
|
-
case "completion_requested":
|
|
197
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
198
|
-
case "audit_started":
|
|
199
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
200
|
-
case "audit_result":
|
|
201
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
202
|
-
case "audit_skipped":
|
|
203
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
204
|
-
case "goal_completed":
|
|
205
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
206
|
-
case "goal_aborted":
|
|
207
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
208
|
-
case "task_list_set":
|
|
209
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
210
|
-
case "task_complete":
|
|
211
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
212
|
-
case "task_skipped":
|
|
213
|
-
return { ...event, goalId: safeGoalId(event.goalId) };
|
|
214
|
-
case "goal_unfocused":
|
|
215
|
-
return event;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export function reconstructGoalLedger(events: GoalLedgerEvent[]): ReconstructedLedgerState {
|
|
220
|
-
const goals = new Map<string, ReconstructedGoalState>();
|
|
221
|
-
const terminalGoals = new Map<string, ReconstructedGoalState>();
|
|
222
|
-
let focusedGoalId: string | null = null;
|
|
223
|
-
|
|
224
|
-
for (const event of events) {
|
|
225
|
-
switch (event.type) {
|
|
226
|
-
case "goal_created": {
|
|
227
|
-
const state: ReconstructedGoalState = {
|
|
228
|
-
goalId: event.goalId,
|
|
229
|
-
latestStatus: "active",
|
|
230
|
-
latestFocus: false,
|
|
231
|
-
createdAt: event.at,
|
|
232
|
-
};
|
|
233
|
-
goals.set(event.goalId, state);
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
case "goal_focused": {
|
|
237
|
-
focusedGoalId = event.goalId;
|
|
238
|
-
for (const g of goals.values()) g.latestFocus = false;
|
|
239
|
-
for (const g of terminalGoals.values()) g.latestFocus = false;
|
|
240
|
-
const state = goals.get(event.goalId) ?? terminalGoals.get(event.goalId);
|
|
241
|
-
if (state) state.latestFocus = true;
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
case "goal_unfocused": {
|
|
245
|
-
focusedGoalId = null;
|
|
246
|
-
for (const g of goals.values()) g.latestFocus = false;
|
|
247
|
-
for (const g of terminalGoals.values()) g.latestFocus = false;
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
case "goal_paused": {
|
|
251
|
-
const state = goals.get(event.goalId);
|
|
252
|
-
if (state) {
|
|
253
|
-
state.latestStatus = event.status ?? "paused";
|
|
254
|
-
state.latestPauseReason = event.reason;
|
|
255
|
-
state.latestPauseSuggestedAction = event.suggestedAction;
|
|
256
|
-
}
|
|
257
|
-
break;
|
|
258
|
-
}
|
|
259
|
-
case "goal_resumed": {
|
|
260
|
-
const state = goals.get(event.goalId);
|
|
261
|
-
if (state) {
|
|
262
|
-
state.latestStatus = "active";
|
|
263
|
-
state.resumedAt = event.at;
|
|
264
|
-
delete state.latestPauseReason;
|
|
265
|
-
delete state.latestPauseSuggestedAction;
|
|
266
|
-
}
|
|
267
|
-
break;
|
|
268
|
-
}
|
|
269
|
-
case "goal_tweaked": {
|
|
270
|
-
const state = goals.get(event.goalId);
|
|
271
|
-
if (state) state.tweakedAt = event.at;
|
|
272
|
-
break;
|
|
273
|
-
}
|
|
274
|
-
case "completion_requested": {
|
|
275
|
-
// No status change until audit_result or goal_completed
|
|
276
|
-
break;
|
|
277
|
-
}
|
|
278
|
-
case "audit_started": {
|
|
279
|
-
// No state change
|
|
280
|
-
break;
|
|
281
|
-
}
|
|
282
|
-
case "audit_skipped": {
|
|
283
|
-
// audit was skipped; goal continues as-is
|
|
284
|
-
break;
|
|
285
|
-
}
|
|
286
|
-
case "audit_result": {
|
|
287
|
-
const state = goals.get(event.goalId) ?? terminalGoals.get(event.goalId);
|
|
288
|
-
if (state) {
|
|
289
|
-
state.latestAuditorResult = { verdict: event.verdict, report: event.report, at: event.at };
|
|
290
|
-
}
|
|
291
|
-
break;
|
|
292
|
-
}
|
|
293
|
-
case "goal_completed": {
|
|
294
|
-
let state = goals.get(event.goalId);
|
|
295
|
-
if (!state) {
|
|
296
|
-
state = { goalId: event.goalId, latestStatus: "complete", latestFocus: false }; }
|
|
297
|
-
state.latestStatus = "complete";
|
|
298
|
-
state.completedAt = event.at;
|
|
299
|
-
terminalGoals.set(event.goalId, state);
|
|
300
|
-
goals.delete(event.goalId);
|
|
301
|
-
break;
|
|
302
|
-
}
|
|
303
|
-
case "goal_aborted": {
|
|
304
|
-
let state = goals.get(event.goalId);
|
|
305
|
-
if (!state) {
|
|
306
|
-
state = { goalId: event.goalId, latestStatus: "aborted", latestFocus: false }; }
|
|
307
|
-
state.latestStatus = "aborted";
|
|
308
|
-
state.abortedAt = event.at;
|
|
309
|
-
terminalGoals.set(event.goalId, state);
|
|
310
|
-
goals.delete(event.goalId);
|
|
311
|
-
break;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
// If the focused goal was moved to terminal (e.g., aborted/completed), clear focus.
|
|
317
|
-
if (focusedGoalId && !goals.has(focusedGoalId)) {
|
|
318
|
-
focusedGoalId = null;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
return { focusedGoalId, goals, terminalGoals };
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
export function latestAuditorResultForGoal(events: GoalLedgerEvent[], goalId: string): { verdict: "approved" | "disapproved" | "error"; report: string; at: string } | undefined {
|
|
325
|
-
for (let i = events.length - 1; i >= 0; i--) {
|
|
326
|
-
const event = events[i];
|
|
327
|
-
if (event.type === "audit_result" && event.goalId === goalId) {
|
|
328
|
-
return { verdict: event.verdict, report: event.report, at: event.at };
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
return undefined;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
export function latestEventsForGoal(events: GoalLedgerEvent[], goalId: string, limit = 10): GoalLedgerEvent[] {
|
|
335
|
-
const result: GoalLedgerEvent[] = [];
|
|
336
|
-
for (let i = events.length - 1; i >= 0; i--) {
|
|
337
|
-
const event = events[i];
|
|
338
|
-
if ("goalId" in event && event.goalId === goalId) {
|
|
339
|
-
result.unshift(event);
|
|
340
|
-
if (result.length >= limit) break;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
return result;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
export function latestGoalLifecycleEvent(events: GoalLedgerEvent[], goalId: string): GoalLedgerEvent | undefined {
|
|
347
|
-
for (let i = events.length - 1; i >= 0; i--) {
|
|
348
|
-
const event = events[i];
|
|
349
|
-
if ("goalId" in event && event.goalId === goalId) {
|
|
350
|
-
return event;
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
return undefined;
|
|
354
|
-
}
|