@narumitw/pi-goal 0.54.3 → 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 +117 -190
  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 +111 -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 -58
  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 +371 -347
  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/settings.ts CHANGED
@@ -8,184 +8,171 @@ export const GOAL_SETTINGS_FILE = "pi-goal.json";
8
8
  export type ContinuationLimit = number | null;
9
9
 
10
10
  export interface GoalSettings {
11
- rpc: {
12
- enabled: boolean;
13
- };
14
- continuationLimits: {
15
- automaticTurns: ContinuationLimit;
16
- noProgressTurns: ContinuationLimit;
17
- };
11
+ rpc: {
12
+ enabled: boolean;
13
+ };
14
+ continuationLimits: {
15
+ automaticTurns: ContinuationLimit;
16
+ noProgressTurns: ContinuationLimit;
17
+ };
18
18
  }
19
19
 
20
20
  export const DEFAULT_GOAL_SETTINGS: GoalSettings = {
21
- rpc: { enabled: false },
22
- continuationLimits: { automaticTurns: 25, noProgressTurns: 3 },
21
+ rpc: { enabled: false },
22
+ continuationLimits: { automaticTurns: 25, noProgressTurns: 3 },
23
23
  };
24
24
 
25
25
  export type GoalSettingsLoadResult =
26
- | { kind: "missing"; legacyExperimentalGoals: false }
27
- | { kind: "invalid"; reason: string }
28
- | { kind: "loaded"; settings: GoalSettings; legacyExperimentalGoals: boolean };
26
+ | { kind: "missing"; legacyExperimentalGoals: false }
27
+ | { kind: "invalid"; reason: string }
28
+ | { kind: "loaded"; settings: GoalSettings; legacyExperimentalGoals: boolean };
29
29
 
30
30
  export type GoalSettingsLoadIssue = Extract<GoalSettingsLoadResult, { kind: "invalid" }>;
31
31
 
32
32
  interface GoalSettingsSaveFileSystem {
33
- mkdirSync: typeof mkdirSync;
34
- writeFileSync: typeof writeFileSync;
35
- renameSync: typeof renameSync;
36
- rmSync: typeof rmSync;
33
+ mkdirSync: typeof mkdirSync;
34
+ writeFileSync: typeof writeFileSync;
35
+ renameSync: typeof renameSync;
36
+ rmSync: typeof rmSync;
37
37
  }
38
38
 
39
39
  export function normalizeGoalSettings(value: unknown): GoalSettings | undefined {
40
- if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
41
-
42
- const rpcValue = Object.hasOwn(value, "rpc") ? Reflect.get(value, "rpc") : undefined;
43
- if (
44
- rpcValue !== undefined &&
45
- (typeof rpcValue !== "object" || rpcValue === null || Array.isArray(rpcValue))
46
- ) {
47
- return undefined;
48
- }
49
- const rpcEnabled =
50
- rpcValue && Object.hasOwn(rpcValue, "enabled")
51
- ? Reflect.get(rpcValue, "enabled")
52
- : DEFAULT_GOAL_SETTINGS.rpc.enabled;
53
- if (typeof rpcEnabled !== "boolean") return undefined;
54
-
55
- const continuationLimitsValue = Object.hasOwn(value, "continuationLimits")
56
- ? Reflect.get(value, "continuationLimits")
57
- : undefined;
58
- if (
59
- continuationLimitsValue !== undefined &&
60
- (typeof continuationLimitsValue !== "object" ||
61
- continuationLimitsValue === null ||
62
- Array.isArray(continuationLimitsValue))
63
- ) {
64
- return undefined;
65
- }
66
- const automaticTurns = continuationLimitsValue
67
- ? normalizeContinuationLimit(
68
- Reflect.get(continuationLimitsValue, "automaticTurns"),
69
- DEFAULT_GOAL_SETTINGS.continuationLimits.automaticTurns,
70
- )
71
- : DEFAULT_GOAL_SETTINGS.continuationLimits.automaticTurns;
72
- const noProgressTurns = continuationLimitsValue
73
- ? normalizeContinuationLimit(
74
- Reflect.get(continuationLimitsValue, "noProgressTurns"),
75
- DEFAULT_GOAL_SETTINGS.continuationLimits.noProgressTurns,
76
- )
77
- : DEFAULT_GOAL_SETTINGS.continuationLimits.noProgressTurns;
78
- if (automaticTurns === undefined || noProgressTurns === undefined) return undefined;
79
-
80
- return {
81
- rpc: { enabled: rpcEnabled },
82
- continuationLimits: { automaticTurns, noProgressTurns },
83
- };
40
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
41
+
42
+ const rpcValue = Object.hasOwn(value, "rpc") ? Reflect.get(value, "rpc") : undefined;
43
+ if (rpcValue !== undefined && (typeof rpcValue !== "object" || rpcValue === null || Array.isArray(rpcValue))) {
44
+ return undefined;
45
+ }
46
+ const rpcEnabled =
47
+ rpcValue && Object.hasOwn(rpcValue, "enabled")
48
+ ? Reflect.get(rpcValue, "enabled")
49
+ : DEFAULT_GOAL_SETTINGS.rpc.enabled;
50
+ if (typeof rpcEnabled !== "boolean") return undefined;
51
+
52
+ const continuationLimitsValue = Object.hasOwn(value, "continuationLimits")
53
+ ? Reflect.get(value, "continuationLimits")
54
+ : undefined;
55
+ if (
56
+ continuationLimitsValue !== undefined &&
57
+ (typeof continuationLimitsValue !== "object" ||
58
+ continuationLimitsValue === null ||
59
+ Array.isArray(continuationLimitsValue))
60
+ ) {
61
+ return undefined;
62
+ }
63
+ const automaticTurns = continuationLimitsValue
64
+ ? normalizeContinuationLimit(
65
+ Reflect.get(continuationLimitsValue, "automaticTurns"),
66
+ DEFAULT_GOAL_SETTINGS.continuationLimits.automaticTurns,
67
+ )
68
+ : DEFAULT_GOAL_SETTINGS.continuationLimits.automaticTurns;
69
+ const noProgressTurns = continuationLimitsValue
70
+ ? normalizeContinuationLimit(
71
+ Reflect.get(continuationLimitsValue, "noProgressTurns"),
72
+ DEFAULT_GOAL_SETTINGS.continuationLimits.noProgressTurns,
73
+ )
74
+ : DEFAULT_GOAL_SETTINGS.continuationLimits.noProgressTurns;
75
+ if (automaticTurns === undefined || noProgressTurns === undefined) return undefined;
76
+
77
+ return {
78
+ rpc: { enabled: rpcEnabled },
79
+ continuationLimits: { automaticTurns, noProgressTurns },
80
+ };
84
81
  }
85
82
 
86
- function normalizeContinuationLimit(
87
- value: unknown,
88
- fallback: ContinuationLimit,
89
- ): ContinuationLimit | undefined {
90
- if (value === undefined) return fallback;
91
- if (value === null) return null;
92
- return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined;
83
+ function normalizeContinuationLimit(value: unknown, fallback: ContinuationLimit): ContinuationLimit | undefined {
84
+ if (value === undefined) return fallback;
85
+ if (value === null) return null;
86
+ return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined;
93
87
  }
94
88
 
95
89
  export function saveGoalSettings(
96
- settings: GoalSettings,
97
- settingsPath = join(getAgentDir(), GOAL_SETTINGS_FILE),
98
- overrides: Partial<GoalSettingsSaveFileSystem> = {},
90
+ settings: GoalSettings,
91
+ settingsPath = join(getAgentDir(), GOAL_SETTINGS_FILE),
92
+ overrides: Partial<GoalSettingsSaveFileSystem> = {},
99
93
  ) {
100
- const normalized = normalizeGoalSettings(settings);
101
- if (!normalized) throw new Error("Refusing to save invalid pi-goal settings.");
102
-
103
- let raw: Record<string, unknown> = {};
104
- try {
105
- const contents = readFileSync(settingsPath, "utf8");
106
- const parsed = JSON.parse(contents) as unknown;
107
- if (!normalizeGoalSettings(parsed)) {
108
- throw new Error(`${settingsPath}: invalid settings shape`);
109
- }
110
- raw = ownRecord(parsed) ?? {};
111
- } catch (error) {
112
- if (!isNodeError(error) || error.code !== "ENOENT") {
113
- throw new Error(`Cannot save invalid settings file: ${formatError(error)}`);
114
- }
115
- }
116
-
117
- const rpc = ownRecord(raw.rpc) ?? {};
118
- const continuationLimits = ownRecord(raw.continuationLimits) ?? {};
119
- const document = `${JSON.stringify(
120
- {
121
- ...raw,
122
- rpc: { ...rpc, enabled: normalized.rpc.enabled },
123
- continuationLimits: {
124
- ...continuationLimits,
125
- automaticTurns: normalized.continuationLimits.automaticTurns,
126
- noProgressTurns: normalized.continuationLimits.noProgressTurns,
127
- },
128
- },
129
- null,
130
- 2,
131
- )}\n`;
132
- const fs = { mkdirSync, writeFileSync, renameSync, rmSync, ...overrides };
133
- const temporaryPath = join(
134
- dirname(settingsPath),
135
- `.${basename(settingsPath)}.${randomUUID()}.tmp`,
136
- );
137
- try {
138
- fs.mkdirSync(dirname(settingsPath), { recursive: true });
139
- fs.writeFileSync(temporaryPath, document, { encoding: "utf8", flag: "wx" });
140
- fs.renameSync(temporaryPath, settingsPath);
141
- } finally {
142
- try {
143
- fs.rmSync(temporaryPath, { force: true });
144
- } catch {
145
- // Best-effort cleanup must not replace the save result.
146
- }
147
- }
94
+ const normalized = normalizeGoalSettings(settings);
95
+ if (!normalized) throw new Error("Refusing to save invalid pi-goal settings.");
96
+
97
+ let raw: Record<string, unknown> = {};
98
+ try {
99
+ const contents = readFileSync(settingsPath, "utf8");
100
+ const parsed = JSON.parse(contents) as unknown;
101
+ if (!normalizeGoalSettings(parsed)) {
102
+ throw new Error(`${settingsPath}: invalid settings shape`);
103
+ }
104
+ raw = ownRecord(parsed) ?? {};
105
+ } catch (error) {
106
+ if (!isNodeError(error) || error.code !== "ENOENT") {
107
+ throw new Error(`Cannot save invalid settings file: ${formatError(error)}`);
108
+ }
109
+ }
110
+
111
+ const rpc = ownRecord(raw.rpc) ?? {};
112
+ const continuationLimits = ownRecord(raw.continuationLimits) ?? {};
113
+ const document = `${JSON.stringify(
114
+ {
115
+ ...raw,
116
+ rpc: { ...rpc, enabled: normalized.rpc.enabled },
117
+ continuationLimits: {
118
+ ...continuationLimits,
119
+ automaticTurns: normalized.continuationLimits.automaticTurns,
120
+ noProgressTurns: normalized.continuationLimits.noProgressTurns,
121
+ },
122
+ },
123
+ null,
124
+ 2,
125
+ )}\n`;
126
+ const fs = { mkdirSync, writeFileSync, renameSync, rmSync, ...overrides };
127
+ const temporaryPath = join(dirname(settingsPath), `.${basename(settingsPath)}.${randomUUID()}.tmp`);
128
+ try {
129
+ fs.mkdirSync(dirname(settingsPath), { recursive: true });
130
+ fs.writeFileSync(temporaryPath, document, { encoding: "utf8", flag: "wx" });
131
+ fs.renameSync(temporaryPath, settingsPath);
132
+ } finally {
133
+ try {
134
+ fs.rmSync(temporaryPath, { force: true });
135
+ } catch {
136
+ // Best-effort cleanup must not replace the save result.
137
+ }
138
+ }
148
139
  }
149
140
 
150
- export function readGoalSettings(
151
- settingsPath = join(getAgentDir(), GOAL_SETTINGS_FILE),
152
- ): GoalSettingsLoadResult {
153
- let contents: string;
154
- try {
155
- contents = readFileSync(settingsPath, "utf8");
156
- } catch (error: unknown) {
157
- if (isNodeError(error) && error.code === "ENOENT") {
158
- return { kind: "missing", legacyExperimentalGoals: false };
159
- }
160
- return { kind: "invalid", reason: `${settingsPath}: ${formatError(error)}` };
161
- }
162
-
163
- try {
164
- const parsed = JSON.parse(contents) as unknown;
165
- const settings = normalizeGoalSettings(parsed);
166
- return settings
167
- ? { kind: "loaded", settings, legacyExperimentalGoals: hasLegacyExperimentalGoals(parsed) }
168
- : { kind: "invalid", reason: `${settingsPath}: invalid settings shape` };
169
- } catch (error: unknown) {
170
- return { kind: "invalid", reason: `${settingsPath}: ${formatError(error)}` };
171
- }
141
+ export function readGoalSettings(settingsPath = join(getAgentDir(), GOAL_SETTINGS_FILE)): GoalSettingsLoadResult {
142
+ let contents: string;
143
+ try {
144
+ contents = readFileSync(settingsPath, "utf8");
145
+ } catch (error: unknown) {
146
+ if (isNodeError(error) && error.code === "ENOENT") {
147
+ return { kind: "missing", legacyExperimentalGoals: false };
148
+ }
149
+ return { kind: "invalid", reason: `${settingsPath}: ${formatError(error)}` };
150
+ }
151
+
152
+ try {
153
+ const parsed = JSON.parse(contents) as unknown;
154
+ const settings = normalizeGoalSettings(parsed);
155
+ return settings
156
+ ? { kind: "loaded", settings, legacyExperimentalGoals: hasLegacyExperimentalGoals(parsed) }
157
+ : { kind: "invalid", reason: `${settingsPath}: invalid settings shape` };
158
+ } catch (error: unknown) {
159
+ return { kind: "invalid", reason: `${settingsPath}: ${formatError(error)}` };
160
+ }
172
161
  }
173
162
 
174
163
  function hasLegacyExperimentalGoals(value: unknown) {
175
- const experimental = ownRecord(ownRecord(value)?.experimental);
176
- return experimental?.goals === true;
164
+ const experimental = ownRecord(ownRecord(value)?.experimental);
165
+ return experimental?.goals === true;
177
166
  }
178
167
 
179
168
  function ownRecord(value: unknown): Record<string, unknown> | undefined {
180
- return value && typeof value === "object" && !Array.isArray(value)
181
- ? (value as Record<string, unknown>)
182
- : undefined;
169
+ return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : undefined;
183
170
  }
184
171
 
185
172
  function isNodeError(error: unknown): error is NodeJS.ErrnoException {
186
- return error instanceof Error && "code" in error;
173
+ return error instanceof Error && "code" in error;
187
174
  }
188
175
 
189
176
  function formatError(error: unknown) {
190
- return error instanceof Error ? error.message : String(error);
177
+ return error instanceof Error ? error.message : String(error);
191
178
  }
@@ -7,13 +7,13 @@ export const GOAL_TOOL_NAMES = [GOAL_COMPLETE_TOOL, GOAL_BLOCKED_TOOL, GOAL_WAIT
7
7
  const REQUIRED_GOAL_TOOL_NAMES = [GOAL_COMPLETE_TOOL, GOAL_BLOCKED_TOOL] as const;
8
8
 
9
9
  export function goalToolsAvailable(pi: Pick<ExtensionAPI, "getActiveTools">) {
10
- const active = new Set(pi.getActiveTools());
11
- return REQUIRED_GOAL_TOOL_NAMES.every((name) => active.has(name));
10
+ const active = new Set(pi.getActiveTools());
11
+ return REQUIRED_GOAL_TOOL_NAMES.every((name) => active.has(name));
12
12
  }
13
13
 
14
14
  export function assertGoalToolsAvailable(pi: Pick<ExtensionAPI, "getActiveTools">) {
15
- if (goalToolsAvailable(pi)) return;
16
- throw new Error(
17
- "goal_complete and goal_blocked are unavailable; include them in the active tool allowlist or leave the restrictive tool mode first.",
18
- );
15
+ if (goalToolsAvailable(pi)) return;
16
+ throw new Error(
17
+ "goal_complete and goal_blocked are unavailable; include them in the active tool allowlist or leave the restrictive tool mode first.",
18
+ );
19
19
  }