@narumitw/pi-goal 0.54.4 → 0.54.5

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.
Files changed (39) hide show
  1. package/README.md +41 -166
  2. package/dist/chunks/{chunk-QWIUWKRW.js → chunk-VJ5LR26S.js} +57 -77
  3. package/dist/chunks/chunk-VJ5LR26S.js.map +7 -0
  4. package/dist/chunks/{chunk-UQRRW4CY.js → chunk-WQIA35U2.js} +6 -5
  5. package/dist/chunks/chunk-WQIA35U2.js.map +7 -0
  6. package/dist/chunks/{menu-WEFDREGC.js → menu-JMGWQZIT.js} +7 -22
  7. package/dist/chunks/menu-JMGWQZIT.js.map +7 -0
  8. package/dist/chunks/{settings-ui-MVYTTBHW.js → settings-ui-JG3PFRB5.js} +6 -18
  9. package/dist/chunks/settings-ui-JG3PFRB5.js.map +7 -0
  10. package/dist/index.ts +92 -126
  11. package/dist/index.ts.map +2 -2
  12. package/docs/managing-goals.md +47 -0
  13. package/docs/settings.md +91 -0
  14. package/package.json +55 -55
  15. package/src/accounting.ts +62 -66
  16. package/src/command-registration.ts +104 -102
  17. package/src/command.ts +101 -112
  18. package/src/commands.ts +465 -501
  19. package/src/errors.ts +127 -122
  20. package/src/goal-contract.ts +64 -64
  21. package/src/goal.ts +25 -25
  22. package/src/lifecycle.ts +615 -628
  23. package/src/markers.ts +6 -6
  24. package/src/menu.ts +565 -591
  25. package/src/persistence.ts +177 -192
  26. package/src/prompts.ts +53 -67
  27. package/src/run-protocol.ts +299 -325
  28. package/src/runtime.ts +1422 -1495
  29. package/src/safety.ts +51 -51
  30. package/src/settings-ui.ts +366 -393
  31. package/src/settings.ts +134 -147
  32. package/src/tool-policy.ts +6 -6
  33. package/src/tools.ts +360 -371
  34. package/src/wait.ts +47 -51
  35. package/src/workflow-mutex.ts +68 -68
  36. package/dist/chunks/chunk-QWIUWKRW.js.map +0 -7
  37. package/dist/chunks/chunk-UQRRW4CY.js.map +0 -7
  38. package/dist/chunks/menu-WEFDREGC.js.map +0 -7
  39. package/dist/chunks/settings-ui-MVYTTBHW.js.map +0 -7
package/src/errors.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
- isContextOverflow,
3
- isRetryableAssistantError,
4
- type AssistantMessage as PiAssistantMessage,
5
- type Usage,
2
+ isContextOverflow,
3
+ isRetryableAssistantError,
4
+ type AssistantMessage as PiAssistantMessage,
5
+ type Usage,
6
6
  } from "@earendil-works/pi-ai";
7
7
  import { stripTerminalSequences } from "@earendil-works/pi-tui";
8
8
  import { assistantUsageTokens, nonNegativeFiniteNumber } from "./accounting.js";
@@ -10,167 +10,172 @@ import { assistantUsageTokens, nonNegativeFiniteNumber } from "./accounting.js";
10
10
  export type AgentStopReason = "stop" | "length" | "toolUse" | "error" | "aborted";
11
11
 
12
12
  export interface AssistantMessageLike {
13
- role: "assistant";
14
- stopReason?: AgentStopReason;
15
- errorMessage?: string;
16
- content?: PiAssistantMessage["content"];
17
- api?: PiAssistantMessage["api"];
18
- provider?: PiAssistantMessage["provider"];
19
- model?: string;
20
- usage?: Usage;
21
- timestamp?: number;
13
+ role: "assistant";
14
+ stopReason?: AgentStopReason;
15
+ errorMessage?: string;
16
+ content?: PiAssistantMessage["content"];
17
+ api?: PiAssistantMessage["api"];
18
+ provider?: PiAssistantMessage["provider"];
19
+ model?: string;
20
+ usage?: Usage;
21
+ timestamp?: number;
22
22
  }
23
23
 
24
24
  const USAGE_LIMIT_GOAL_ERROR_PATTERNS = [
25
- /usage[_\s-]*(?:limit|cap)|chatgpt.{0,32}usage/i,
26
- /quota.{0,32}(?:reached|exceeded|exhausted|depleted)|(?:reached|exceeded|exhausted|depleted).{0,32}quota/i,
27
- /insufficient[_\s-]*(?:quota|credits?)|out of credits|out of budget|available balance|payment required/i,
28
- /(?:credit|balance).{0,32}(?:low|exhausted|depleted)|billing/i,
25
+ /usage[_\s-]*(?:limit|cap)|chatgpt.{0,32}usage/i,
26
+ /quota.{0,32}(?:reached|exceeded|exhausted|depleted)|(?:reached|exceeded|exhausted|depleted).{0,32}quota/i,
27
+ /insufficient[_\s-]*(?:quota|credits?)|out of credits|out of budget|available balance|payment required/i,
28
+ /(?:credit|balance).{0,32}(?:low|exhausted|depleted)|billing/i,
29
29
  ] as const;
30
- const NON_RETRYABLE_GOAL_ERROR_RE =
31
- /multi-auth rotation failed|credentials tried|unauthori[sz]ed|invalid api key/i;
30
+ const NON_RETRYABLE_GOAL_ERROR_RE = /multi-auth rotation failed|credentials tried|unauthori[sz]ed|invalid api key/i;
32
31
  const RETRYABLE_GOAL_ERROR_PATTERNS = [
33
- /overloaded|rate.?limit|too many requests|\b(?:429|500|502|503|504)\b|service.?unavailable|server.?error|internal.?error/i,
34
- /provider.?returned.?error|you can retry your request|try your request again|please retry your request/i,
35
- /network.?error|connection.?(?:error|refused|lost)|other side closed|fetch failed|upstream.?connect|reset before headers|socket hang up/i,
36
- /timed? out|timeout|terminated|websocket.?(?:closed|error)|ended without|stream ended before message_stop|http2 request did not get a response|retry delay/i,
37
- /context[_\s-]*length[_\s-]*exceeded|input exceeds the context window/i,
32
+ /overloaded|rate.?limit|too many requests|\b(?:429|500|502|503|504)\b|service.?unavailable|server.?error|internal.?error/i,
33
+ /provider.?returned.?error|you can retry your request|try your request again|please retry your request/i,
34
+ /network.?error|connection.?(?:error|refused|lost)|other side closed|fetch failed|upstream.?connect|reset before headers|socket hang up/i,
35
+ /timed? out|timeout|terminated|websocket.?(?:closed|error)|ended without|stream ended before message_stop|http2 request did not get a response|retry delay/i,
36
+ /context[_\s-]*length[_\s-]*exceeded|input exceeds the context window/i,
38
37
  ] as const;
39
38
 
40
39
  export function safeTerminalText(value: string) {
41
- return [...stripTerminalSequences(value)]
42
- .map((character) => {
43
- const codePoint = character.codePointAt(0) ?? 0;
44
- if (character === "\n") return character;
45
- return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f) ? " " : character;
46
- })
47
- .join("")
48
- .trim();
40
+ return [...stripTerminalSequences(value)]
41
+ .map((character) => {
42
+ const codePoint = character.codePointAt(0) ?? 0;
43
+ if (character === "\n") return character;
44
+ return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f) ? " " : character;
45
+ })
46
+ .join("")
47
+ .trim();
49
48
  }
50
49
 
51
50
  export function safeGoalMenuText(value: string, maxCharacters = 120) {
52
- const sanitized = safeTerminalText(value).replace(/\s+/gu, " ").trim();
53
- const characters = [...sanitized];
54
- return characters.length <= maxCharacters
55
- ? sanitized
56
- : `${characters.slice(0, maxCharacters).join("")}…`;
51
+ const sanitized = safeTerminalText(value).replace(/\s+/gu, " ").trim();
52
+ const characters = [...sanitized];
53
+ return characters.length <= maxCharacters ? sanitized : `${characters.slice(0, maxCharacters).join("")}…`;
57
54
  }
58
55
 
59
56
  export function notifyTerminal(
60
- ui: { notify: (message: string, level?: "info" | "warning" | "error") => void },
61
- message: string,
62
- level?: "info" | "warning" | "error",
57
+ ui: { notify: (message: string, level?: "info" | "warning" | "error") => void },
58
+ message: string,
59
+ level?: "info" | "warning" | "error",
63
60
  ) {
64
- ui.notify(safeTerminalText(message), level);
61
+ ui.notify(safeTerminalText(message), level);
62
+ }
63
+
64
+ /**
65
+ * True when an error came from an ExtensionContext invalidated by session
66
+ * replacement or reload. Pi throws this from every ctx getter and pi method on
67
+ * the stale runner, so deferred callbacks must treat it as cancellation.
68
+ */
69
+ export function isStaleContextError(error: unknown): boolean {
70
+ return (
71
+ error instanceof Error && error.message.includes("This extension ctx is stale after session replacement or reload")
72
+ );
65
73
  }
66
74
 
67
75
  export function formatError(error: unknown) {
68
- return truncateNotification(error instanceof Error ? error.message : String(error));
76
+ return truncateNotification(error instanceof Error ? error.message : String(error));
69
77
  }
70
78
 
71
79
  export function truncateNotification(value: string) {
72
- const safe = [...safeTerminalText(value)];
73
- return safe.length > 160 ? `${safe.slice(0, 157).join("")}...` : safe.join("");
80
+ const safe = [...safeTerminalText(value)];
81
+ return safe.length > 160 ? `${safe.slice(0, 157).join("")}...` : safe.join("");
74
82
  }
75
83
 
76
84
  export function isUsageLimitedGoalInterruption(assistant: AssistantMessageLike) {
77
- const errorMessage = assistant.errorMessage;
78
- return (
79
- assistant.stopReason === "error" &&
80
- typeof errorMessage === "string" &&
81
- USAGE_LIMIT_GOAL_ERROR_PATTERNS.some((pattern) => pattern.test(errorMessage))
82
- );
85
+ const errorMessage = assistant.errorMessage;
86
+ return (
87
+ assistant.stopReason === "error" &&
88
+ typeof errorMessage === "string" &&
89
+ USAGE_LIMIT_GOAL_ERROR_PATTERNS.some((pattern) => pattern.test(errorMessage))
90
+ );
83
91
  }
84
92
 
85
93
  export function isRetryableGoalInterruption(assistant: AssistantMessageLike) {
86
- if (assistant.stopReason !== "error" || !assistant.errorMessage) return false;
87
- if (
88
- isUsageLimitedGoalInterruption(assistant) ||
89
- NON_RETRYABLE_GOAL_ERROR_RE.test(assistant.errorMessage)
90
- ) {
91
- return false;
92
- }
93
- return (
94
- isGoalContextOverflow(assistant) ||
95
- isRetryableAssistantError(toPiAssistantMessage(assistant)) ||
96
- RETRYABLE_GOAL_ERROR_PATTERNS.some((pattern) => pattern.test(assistant.errorMessage ?? ""))
97
- );
94
+ if (assistant.stopReason !== "error" || !assistant.errorMessage) return false;
95
+ if (isUsageLimitedGoalInterruption(assistant) || NON_RETRYABLE_GOAL_ERROR_RE.test(assistant.errorMessage)) {
96
+ return false;
97
+ }
98
+ return (
99
+ isGoalContextOverflow(assistant) ||
100
+ isRetryableAssistantError(toPiAssistantMessage(assistant)) ||
101
+ RETRYABLE_GOAL_ERROR_PATTERNS.some((pattern) => pattern.test(assistant.errorMessage ?? ""))
102
+ );
98
103
  }
99
104
 
100
105
  export function isGoalContextOverflow(assistant: AssistantMessageLike) {
101
- return isContextOverflow(toPiAssistantMessage(assistant));
106
+ return isContextOverflow(toPiAssistantMessage(assistant));
102
107
  }
103
108
 
104
109
  export function findFinalAssistantMessage(messages: unknown[]): AssistantMessageLike | undefined {
105
- for (let index = messages.length - 1; index >= 0; index--) {
106
- const message = messages[index];
107
- if (!message || typeof message !== "object") continue;
108
- const candidate = message as Record<string, unknown>;
109
- if (candidate.role !== "assistant") continue;
110
- const assistant: AssistantMessageLike = {
111
- role: "assistant",
112
- stopReason: isAgentStopReason(candidate.stopReason) ? candidate.stopReason : undefined,
113
- errorMessage: typeof candidate.errorMessage === "string" ? candidate.errorMessage : undefined,
114
- };
115
- if (Array.isArray(candidate.content)) {
116
- assistant.content = candidate.content as PiAssistantMessage["content"];
117
- }
118
- if (typeof candidate.api === "string") assistant.api = candidate.api;
119
- if (typeof candidate.provider === "string") assistant.provider = candidate.provider;
120
- if (typeof candidate.model === "string") assistant.model = candidate.model;
121
- if (typeof candidate.timestamp === "number") assistant.timestamp = candidate.timestamp;
122
- const usage = normalizeUsage(candidate.usage);
123
- if (usage) assistant.usage = usage;
124
- return assistant;
125
- }
126
- return undefined;
110
+ for (let index = messages.length - 1; index >= 0; index--) {
111
+ const message = messages[index];
112
+ if (!message || typeof message !== "object") continue;
113
+ const candidate = message as Record<string, unknown>;
114
+ if (candidate.role !== "assistant") continue;
115
+ const assistant: AssistantMessageLike = {
116
+ role: "assistant",
117
+ stopReason: isAgentStopReason(candidate.stopReason) ? candidate.stopReason : undefined,
118
+ errorMessage: typeof candidate.errorMessage === "string" ? candidate.errorMessage : undefined,
119
+ };
120
+ if (Array.isArray(candidate.content)) {
121
+ assistant.content = candidate.content as PiAssistantMessage["content"];
122
+ }
123
+ if (typeof candidate.api === "string") assistant.api = candidate.api;
124
+ if (typeof candidate.provider === "string") assistant.provider = candidate.provider;
125
+ if (typeof candidate.model === "string") assistant.model = candidate.model;
126
+ if (typeof candidate.timestamp === "number") assistant.timestamp = candidate.timestamp;
127
+ const usage = normalizeUsage(candidate.usage);
128
+ if (usage) assistant.usage = usage;
129
+ return assistant;
130
+ }
131
+ return undefined;
127
132
  }
128
133
 
129
134
  function toPiAssistantMessage(assistant: AssistantMessageLike): PiAssistantMessage {
130
- return {
131
- role: "assistant",
132
- content: assistant.content ?? [],
133
- api: assistant.api ?? "openai-responses",
134
- provider: assistant.provider ?? "unknown",
135
- model: assistant.model ?? "unknown",
136
- usage: assistant.usage ?? zeroUsage(),
137
- stopReason: assistant.stopReason ?? "error",
138
- errorMessage: assistant.errorMessage,
139
- timestamp: assistant.timestamp ?? Date.now(),
140
- };
135
+ return {
136
+ role: "assistant",
137
+ content: assistant.content ?? [],
138
+ api: assistant.api ?? "openai-responses",
139
+ provider: assistant.provider ?? "unknown",
140
+ model: assistant.model ?? "unknown",
141
+ usage: assistant.usage ?? zeroUsage(),
142
+ stopReason: assistant.stopReason ?? "error",
143
+ errorMessage: assistant.errorMessage,
144
+ timestamp: assistant.timestamp ?? Date.now(),
145
+ };
141
146
  }
142
147
 
143
148
  function zeroUsage(): Usage {
144
- return {
145
- input: 0,
146
- output: 0,
147
- cacheRead: 0,
148
- cacheWrite: 0,
149
- totalTokens: 0,
150
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
151
- };
149
+ return {
150
+ input: 0,
151
+ output: 0,
152
+ cacheRead: 0,
153
+ cacheWrite: 0,
154
+ totalTokens: 0,
155
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
156
+ };
152
157
  }
153
158
 
154
159
  function isAgentStopReason(value: unknown): value is AgentStopReason {
155
- return ["stop", "length", "toolUse", "error", "aborted"].includes(String(value));
160
+ return ["stop", "length", "toolUse", "error", "aborted"].includes(String(value));
156
161
  }
157
162
 
158
163
  function normalizeUsage(value: unknown): Usage | undefined {
159
- if (!value || typeof value !== "object") return undefined;
160
- const usage = value as Partial<Usage>;
161
- if (typeof usage.input !== "number" || typeof usage.output !== "number") return undefined;
162
- return {
163
- input: nonNegativeFiniteNumber(usage.input),
164
- output: nonNegativeFiniteNumber(usage.output),
165
- cacheRead: nonNegativeFiniteNumber(usage.cacheRead),
166
- cacheWrite: nonNegativeFiniteNumber(usage.cacheWrite),
167
- totalTokens: assistantUsageTokens(usage),
168
- cost: {
169
- input: usage.cost?.input ?? 0,
170
- output: usage.cost?.output ?? 0,
171
- cacheRead: usage.cost?.cacheRead ?? 0,
172
- cacheWrite: usage.cost?.cacheWrite ?? 0,
173
- total: usage.cost?.total ?? 0,
174
- },
175
- };
164
+ if (!value || typeof value !== "object") return undefined;
165
+ const usage = value as Partial<Usage>;
166
+ if (typeof usage.input !== "number" || typeof usage.output !== "number") return undefined;
167
+ return {
168
+ input: nonNegativeFiniteNumber(usage.input),
169
+ output: nonNegativeFiniteNumber(usage.output),
170
+ cacheRead: nonNegativeFiniteNumber(usage.cacheRead),
171
+ cacheWrite: nonNegativeFiniteNumber(usage.cacheWrite),
172
+ totalTokens: assistantUsageTokens(usage),
173
+ cost: {
174
+ input: usage.cost?.input ?? 0,
175
+ output: usage.cost?.output ?? 0,
176
+ cacheRead: usage.cost?.cacheRead ?? 0,
177
+ cacheWrite: usage.cost?.cacheWrite ?? 0,
178
+ total: usage.cost?.total ?? 0,
179
+ },
180
+ };
176
181
  }
@@ -5,111 +5,111 @@ export const GOAL_CONTRACT_MESSAGE_TYPE = "goal-contract";
5
5
  export const GOAL_CONTRACT_VERSION = 2;
6
6
 
7
7
  const INACTIVE_GOAL_CONTRACT_CONTENT = [
8
- "Goal mode is inactive.",
9
- "This Goal contract supersedes every earlier goal-contract message.",
10
- "Do not treat an earlier Goal objective, goal_id, Goal-mode rule, or summary of them as current unless a later Goal contract explicitly reactivates Goal mode.",
8
+ "Goal mode is inactive.",
9
+ "This Goal contract supersedes every earlier goal-contract message.",
10
+ "Do not treat an earlier Goal objective, goal_id, Goal-mode rule, or summary of them as current unless a later Goal contract explicitly reactivates Goal mode.",
11
11
  ].join("\n");
12
12
 
13
13
  interface ContractMessage {
14
- role?: string;
15
- customType?: string;
16
- content?: unknown;
14
+ role?: string;
15
+ customType?: string;
16
+ content?: unknown;
17
17
  }
18
18
 
19
19
  interface ContractSessionEntry extends ContractMessage {
20
- type?: string;
21
- message?: unknown;
20
+ type?: string;
21
+ message?: unknown;
22
22
  }
23
23
 
24
24
  export function createGoalContextContract(goal: GoalPromptContext) {
25
- return {
26
- role: "custom" as const,
27
- customType: GOAL_CONTRACT_MESSAGE_TYPE,
28
- content: [
29
- "This Goal contract supersedes every earlier goal-contract message.",
30
- "Only the objective and goal_id in this latest Goal contract are current.",
31
- buildGoalContextPrompt(goal),
32
- ].join("\n\n"),
33
- display: false,
34
- details: { version: GOAL_CONTRACT_VERSION, state: "active", goalId: goal.id },
35
- timestamp: 0,
36
- };
25
+ return {
26
+ role: "custom" as const,
27
+ customType: GOAL_CONTRACT_MESSAGE_TYPE,
28
+ content: [
29
+ "This Goal contract supersedes every earlier goal-contract message.",
30
+ "Only the objective and goal_id in this latest Goal contract are current.",
31
+ buildGoalContextPrompt(goal),
32
+ ].join("\n\n"),
33
+ display: false,
34
+ details: { version: GOAL_CONTRACT_VERSION, state: "active", goalId: goal.id },
35
+ timestamp: 0,
36
+ };
37
37
  }
38
38
 
39
39
  export function createInactiveGoalContextContract() {
40
- return {
41
- role: "custom" as const,
42
- customType: GOAL_CONTRACT_MESSAGE_TYPE,
43
- content: INACTIVE_GOAL_CONTRACT_CONTENT,
44
- display: false,
45
- details: { version: GOAL_CONTRACT_VERSION, state: "inactive" },
46
- timestamp: 0,
47
- };
40
+ return {
41
+ role: "custom" as const,
42
+ customType: GOAL_CONTRACT_MESSAGE_TYPE,
43
+ content: INACTIVE_GOAL_CONTRACT_CONTENT,
44
+ display: false,
45
+ details: { version: GOAL_CONTRACT_VERSION, state: "inactive" },
46
+ timestamp: 0,
47
+ };
48
48
  }
49
49
 
50
50
  export function reconcileGoalContextContract(messages: unknown[], goal: GoalPromptContext) {
51
- return reconcileContract(messages, createGoalContextContract(goal));
51
+ return reconcileContract(messages, createGoalContextContract(goal));
52
52
  }
53
53
 
54
54
  export function reconcileInactiveGoalContextContract(messages: unknown[]) {
55
- return reconcileContract(messages, createInactiveGoalContextContract());
55
+ return reconcileContract(messages, createInactiveGoalContextContract());
56
56
  }
57
57
 
58
58
  export function hasGoalContextContract(entries: unknown[], goal: GoalPromptContext) {
59
- return latestGoalContractContent(entries) === createGoalContextContract(goal).content;
59
+ return latestGoalContractContent(entries) === createGoalContextContract(goal).content;
60
60
  }
61
61
 
62
62
  export function hasInactiveGoalContextContract(entries: unknown[]) {
63
- return latestGoalContractContent(entries) === INACTIVE_GOAL_CONTRACT_CONTENT;
63
+ return latestGoalContractContent(entries) === INACTIVE_GOAL_CONTRACT_CONTENT;
64
64
  }
65
65
 
66
66
  export function hasGoalContextContractHistory(entries: unknown[]) {
67
- return entries.some(isGoalContextContract);
67
+ return entries.some(isGoalContextContract);
68
68
  }
69
69
 
70
70
  export function isGoalContextContract(message: unknown) {
71
- return unwrapMessage(message).customType === GOAL_CONTRACT_MESSAGE_TYPE;
71
+ return unwrapMessage(message).customType === GOAL_CONTRACT_MESSAGE_TYPE;
72
72
  }
73
73
 
74
74
  function reconcileContract(
75
- messages: unknown[],
76
- expected: {
77
- role: "custom";
78
- customType: string;
79
- content: string;
80
- display: boolean;
81
- details: object;
82
- timestamp: number;
83
- },
75
+ messages: unknown[],
76
+ expected: {
77
+ role: "custom";
78
+ customType: string;
79
+ content: string;
80
+ display: boolean;
81
+ details: object;
82
+ timestamp: number;
83
+ },
84
84
  ) {
85
- if (latestGoalContractContent(messages) === expected.content) return messages;
86
- const summaryBoundary = leadingSummaryBoundary(messages);
87
- if (!hasGoalContextContractHistory(messages) && summaryBoundary > 0) {
88
- return [...messages.slice(0, summaryBoundary), expected, ...messages.slice(summaryBoundary)];
89
- }
90
- return [...messages, expected];
85
+ if (latestGoalContractContent(messages) === expected.content) return messages;
86
+ const summaryBoundary = leadingSummaryBoundary(messages);
87
+ if (!hasGoalContextContractHistory(messages) && summaryBoundary > 0) {
88
+ return [...messages.slice(0, summaryBoundary), expected, ...messages.slice(summaryBoundary)];
89
+ }
90
+ return [...messages, expected];
91
91
  }
92
92
 
93
93
  function latestGoalContractContent(messages: readonly unknown[]) {
94
- for (let index = messages.length - 1; index >= 0; index -= 1) {
95
- const message = messages[index];
96
- if (isGoalContextContract(message)) return unwrapMessage(message).content;
97
- }
98
- return undefined;
94
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
95
+ const message = messages[index];
96
+ if (isGoalContextContract(message)) return unwrapMessage(message).content;
97
+ }
98
+ return undefined;
99
99
  }
100
100
 
101
101
  function leadingSummaryBoundary(messages: readonly unknown[]) {
102
- let index = 0;
103
- while (index < messages.length) {
104
- const role = unwrapMessage(messages[index]).role;
105
- if (role !== "compactionSummary" && role !== "branchSummary") break;
106
- index += 1;
107
- }
108
- return index;
102
+ let index = 0;
103
+ while (index < messages.length) {
104
+ const role = unwrapMessage(messages[index]).role;
105
+ if (role !== "compactionSummary" && role !== "branchSummary") break;
106
+ index += 1;
107
+ }
108
+ return index;
109
109
  }
110
110
 
111
111
  function unwrapMessage(message: unknown): ContractMessage {
112
- const entry = message as ContractSessionEntry | undefined;
113
- if (entry?.type === "custom_message") return entry;
114
- return (entry?.message ?? message ?? {}) as ContractMessage;
112
+ const entry = message as ContractSessionEntry | undefined;
113
+ if (entry?.type === "custom_message") return entry;
114
+ return (entry?.message ?? message ?? {}) as ContractMessage;
115
115
  }
package/src/goal.ts CHANGED
@@ -7,46 +7,46 @@ import { GoalRuntime } from "./runtime.js";
7
7
  import { registerGoalTools } from "./tools.js";
8
8
 
9
9
  interface GoalOptions {
10
- settingsPath?: string;
10
+ settingsPath?: string;
11
11
  }
12
12
 
13
13
  function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
14
- const runtime = new GoalRuntime(pi);
15
- const commands = new GoalCommandController(runtime);
16
- const runController = new GoalRunController(runtime, commands);
17
-
18
- // Keep registration order explicit: managed-run bus listeners exist before tools,
19
- // command routing, and session lifecycle bind the per-factory runtime.
20
- runController.register(pi);
21
- registerGoalTools(pi, runtime);
22
- registerGoalCommand(pi, runtime, commands, options);
23
- registerGoalLifecycle(pi, runtime, runController, options);
14
+ const runtime = new GoalRuntime(pi);
15
+ const commands = new GoalCommandController(runtime);
16
+ const runController = new GoalRunController(runtime, commands);
17
+
18
+ // Keep registration order explicit: managed-run bus listeners exist before tools,
19
+ // command routing, and session lifecycle bind the per-factory runtime.
20
+ runController.register(pi);
21
+ registerGoalTools(pi, runtime);
22
+ registerGoalCommand(pi, runtime, commands, options);
23
+ registerGoalLifecycle(pi, runtime, runController, options);
24
24
  }
25
25
 
26
26
  export default function goal(pi: ExtensionAPI, options: GoalOptions = {}) {
27
- registerGoalRuntime(pi, options);
27
+ registerGoalRuntime(pi, options);
28
28
  }
29
29
 
30
30
  export {
31
- assistantUsageTokens,
32
- cumulativeAssistantTokens,
33
- formatDuration,
34
- formatTokenCount,
31
+ assistantUsageTokens,
32
+ cumulativeAssistantTokens,
33
+ formatDuration,
34
+ formatTokenCount,
35
35
  } from "./accounting.js";
36
36
 
37
37
  export {
38
- completeGoalArguments,
39
- parseCommand,
40
- parseTokenBudget,
41
- validateObjective,
38
+ completeGoalArguments,
39
+ parseCommand,
40
+ parseTokenBudget,
41
+ validateObjective,
42
42
  } from "./command.js";
43
43
 
44
44
  export { buildGoalSystemPrompt } from "./prompts.js";
45
45
 
46
46
  export {
47
- findFinalAssistantMessage,
48
- formatStatus,
49
- isContradictoryCompletionSummary,
50
- isRetryableGoalInterruption,
51
- isUsageLimitedGoalInterruption,
47
+ findFinalAssistantMessage,
48
+ formatStatus,
49
+ isContradictoryCompletionSummary,
50
+ isRetryableGoalInterruption,
51
+ isUsageLimitedGoalInterruption,
52
52
  } from "./runtime.js";