@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.
- package/README.md +117 -190
- package/dist/chunks/{chunk-QWIUWKRW.js → chunk-VJ5LR26S.js} +57 -77
- package/dist/chunks/chunk-VJ5LR26S.js.map +7 -0
- package/dist/chunks/{chunk-UQRRW4CY.js → chunk-WQIA35U2.js} +6 -5
- package/dist/chunks/chunk-WQIA35U2.js.map +7 -0
- package/dist/chunks/{menu-WEFDREGC.js → menu-JMGWQZIT.js} +7 -22
- package/dist/chunks/menu-JMGWQZIT.js.map +7 -0
- package/dist/chunks/{settings-ui-MVYTTBHW.js → settings-ui-JG3PFRB5.js} +6 -18
- package/dist/chunks/settings-ui-JG3PFRB5.js.map +7 -0
- package/dist/index.ts +111 -126
- package/dist/index.ts.map +2 -2
- package/docs/managing-goals.md +47 -0
- package/docs/settings.md +91 -0
- package/package.json +55 -58
- package/src/accounting.ts +62 -66
- package/src/command-registration.ts +104 -102
- package/src/command.ts +101 -112
- package/src/commands.ts +465 -501
- package/src/errors.ts +127 -122
- package/src/goal-contract.ts +64 -64
- package/src/goal.ts +25 -25
- package/src/lifecycle.ts +615 -628
- package/src/markers.ts +6 -6
- package/src/menu.ts +565 -591
- package/src/persistence.ts +177 -192
- package/src/prompts.ts +53 -67
- package/src/run-protocol.ts +299 -325
- package/src/runtime.ts +1422 -1495
- package/src/safety.ts +51 -51
- package/src/settings-ui.ts +366 -393
- package/src/settings.ts +134 -147
- package/src/tool-policy.ts +6 -6
- package/src/tools.ts +371 -347
- package/src/wait.ts +47 -51
- package/src/workflow-mutex.ts +68 -68
- package/dist/chunks/chunk-QWIUWKRW.js.map +0 -7
- package/dist/chunks/chunk-UQRRW4CY.js.map +0 -7
- package/dist/chunks/menu-WEFDREGC.js.map +0 -7
- 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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
22
|
-
|
|
21
|
+
rpc: { enabled: false },
|
|
22
|
+
continuationLimits: { automaticTurns: 25, noProgressTurns: 3 },
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
export type GoalSettingsLoadResult =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
)
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
90
|
+
settings: GoalSettings,
|
|
91
|
+
settingsPath = join(getAgentDir(), GOAL_SETTINGS_FILE),
|
|
92
|
+
overrides: Partial<GoalSettingsSaveFileSystem> = {},
|
|
99
93
|
) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
176
|
-
|
|
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
|
-
|
|
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
|
-
|
|
173
|
+
return error instanceof Error && "code" in error;
|
|
187
174
|
}
|
|
188
175
|
|
|
189
176
|
function formatError(error: unknown) {
|
|
190
|
-
|
|
177
|
+
return error instanceof Error ? error.message : String(error);
|
|
191
178
|
}
|
package/src/tool-policy.ts
CHANGED
|
@@ -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
|
-
|
|
11
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
}
|