akm-cli 0.9.3 → 0.9.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/CHANGELOG.md +233 -1
- package/README.md +1 -1
- package/SECURITY.md +1 -1
- package/STABILITY.md +1 -1
- package/dist/akm +2 -2
- package/dist/akm-migrate +2 -2
- package/dist/cli.js +5 -5
- package/dist/commands/health/improve-metrics.js +17 -0
- package/dist/commands/health/windows.js +2 -2
- package/dist/commands/health.js +2 -2
- package/dist/commands/improve/anti-collapse.js +4 -91
- package/dist/commands/improve/preparation.js +8 -1
- package/dist/commands/lint/index.js +3 -7
- package/dist/commands/proposal/validators/proposal-validators.js +12 -0
- package/dist/commands/read/search.js +14 -24
- package/dist/commands/tasks/tasks-cli.js +81 -3
- package/dist/commands/tasks/tasks.js +117 -2
- package/dist/core/adapter/adapters/akm-adapter.js +23 -14
- package/dist/core/adapter/adapters/akm-lint.js +3 -2
- package/dist/core/adapter/adapters/akm-task-adapter.js +9 -6
- package/dist/core/adapter/adapters/dotenv-adapter.js +13 -11
- package/dist/core/adapter/recognize-match.js +1 -20
- package/dist/core/asset/asset-placement.js +21 -2
- package/dist/core/common.js +21 -1
- package/dist/core/config/config-version-shim.js +101 -0
- package/dist/core/config/config.js +6 -6
- package/dist/core/improve-result.js +35 -14
- package/dist/execution/guarded-source.js +0 -10
- package/dist/indexer/lookup/adapter-concept-owner.js +6 -89
- package/dist/indexer/passes/metadata.js +12 -4
- package/dist/indexer/scan/doc-to-entry.js +2 -0
- package/dist/indexer/search/db-search.js +6 -0
- package/dist/indexer/search/search-fields.js +16 -1
- package/dist/indexer/walk/matchers.js +0 -22
- package/dist/output/shapes/helpers.js +19 -1
- package/dist/output/shapes/passthrough.js +18 -5
- package/dist/output/text/command-format.js +4 -0
- package/dist/registry/pinned-request-helper.js +2 -2
- package/dist/registry/pinned-transport.js +6 -6
- package/dist/scripts/akm-migrate-node.js +12678 -12601
- package/dist/scripts/akm-migrate.js +12678 -12601
- package/dist/storage/repositories/proposals-repository.js +65 -7
- package/dist/storage/repositories/task-history-repository.js +22 -10
- package/dist/tasks/run/task-history.js +23 -3
- package/dist/tasks/scheduler-binding.js +15 -5
- package/dist/tasks/scheduler-sync-preview.js +45 -0
- package/dist/tasks/scheduler-sync.js +77 -41
- package/dist/tasks/source/bounded-document.js +1 -1
- package/dist/tasks/source/parse-task-source.js +77 -11
- package/dist/tasks/source/task-source-v3-frozen.js +428 -0
- package/dist/tasks/source/task-to-v3.js +512 -0
- package/dist/tasks/source/task-to-v4.js +457 -0
- package/docs/reference/cli.md +33 -6
- package/docs/reference/configuration.md +27 -6
- package/docs/reference/tasks.md +10 -0
- package/package.json +4 -4
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
/** Pure, byte-producing legacy-task to task-v3 migration planner. */
|
|
5
|
+
import crypto from "node:crypto";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { LineCounter, parseDocument, stringify as stringifyYaml } from "yaml";
|
|
8
|
+
import { bundleRefToString, parseBundleRef } from "../../core/asset/asset-ref.js";
|
|
9
|
+
import { formatExtraParamsIssue, validateExtraParams } from "../../core/extra-params.js";
|
|
10
|
+
import { WORKFLOW_ENV_VAR_NAME_PATTERN, WORKFLOW_MAX_EXEC_PASS_ENV, WORKFLOW_MAX_RETRIES, WORKFLOW_MAX_TIMEOUT_MS, } from "../../workflows/resource-limits.js";
|
|
11
|
+
import { validateTaskId } from "../task-id.js";
|
|
12
|
+
import { assertBoundedTaskYamlDocument, TASK_V3_MAX_SOURCE_BYTES } from "./bounded-document.js";
|
|
13
|
+
import { classifyTaskV3Uses, parseTaskV3Yaml } from "./task-source-v3-frozen.js";
|
|
14
|
+
import { parseTaskSourceV4 } from "./task-source-v4.js";
|
|
15
|
+
const V2_KEYS = new Set([
|
|
16
|
+
"version",
|
|
17
|
+
"name",
|
|
18
|
+
"description",
|
|
19
|
+
"when_to_use",
|
|
20
|
+
"tags",
|
|
21
|
+
"schedule",
|
|
22
|
+
"enabled",
|
|
23
|
+
"workflow",
|
|
24
|
+
"prompt",
|
|
25
|
+
"command",
|
|
26
|
+
"params",
|
|
27
|
+
"engine",
|
|
28
|
+
"model",
|
|
29
|
+
"timeoutMs",
|
|
30
|
+
"maxSteps",
|
|
31
|
+
"maxRetries",
|
|
32
|
+
"llm",
|
|
33
|
+
"redact",
|
|
34
|
+
]);
|
|
35
|
+
const V2_SHARED_KEYS = new Set([
|
|
36
|
+
"version",
|
|
37
|
+
"name",
|
|
38
|
+
"description",
|
|
39
|
+
"when_to_use",
|
|
40
|
+
"tags",
|
|
41
|
+
"schedule",
|
|
42
|
+
"enabled",
|
|
43
|
+
"redact",
|
|
44
|
+
]);
|
|
45
|
+
const V2_LLM_KEYS = new Set([
|
|
46
|
+
"temperature",
|
|
47
|
+
"maxTokens",
|
|
48
|
+
"supportsJsonSchema",
|
|
49
|
+
"extraParams",
|
|
50
|
+
"contextLength",
|
|
51
|
+
"enableThinking",
|
|
52
|
+
"reasoningEffort",
|
|
53
|
+
]);
|
|
54
|
+
const SAFE_V2_COMMAND_TOKEN = /^[A-Za-z0-9_./:=+,-]+$/;
|
|
55
|
+
const SHELL_ASSIGNMENT_WORD = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
56
|
+
/**
|
|
57
|
+
* V2 executed argv directly, while v3 `run:` enters a host shell. An explicit
|
|
58
|
+
* path bypasses shell aliases/builtins; `akm` is the one bare executable whose
|
|
59
|
+
* v3 runtime resolution is contractually pinned to the current installation.
|
|
60
|
+
*/
|
|
61
|
+
function shellStableV2Executable(executable) {
|
|
62
|
+
return executable === "akm" || executable.includes("/");
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* `env NAME=value... cmd args...` is env(1) itself resolving and exec'ing
|
|
66
|
+
* `cmd` via its own PATH search — that lookup happens inside env's execvp()
|
|
67
|
+
* regardless of whether env was launched by direct execve (v2) or by a host
|
|
68
|
+
* shell (v3 `run:`). The shell-vs-argv divergence `shellStableV2Executable`
|
|
69
|
+
* guards against (bare names shadowed by shell aliases/builtins/functions)
|
|
70
|
+
* therefore does not apply to whatever env ultimately invokes, so skip past
|
|
71
|
+
* a leading `env` and its `NAME=value` assignments to find the real target.
|
|
72
|
+
* Returns the original tokens, unchanged, when there is no such target
|
|
73
|
+
* (e.g. `env` with nothing after its assignments).
|
|
74
|
+
*/
|
|
75
|
+
function skipEnvAssignmentPrefix(tokens) {
|
|
76
|
+
if (tokens[0] !== "env")
|
|
77
|
+
return { tokens, envWrapped: false };
|
|
78
|
+
let index = 1;
|
|
79
|
+
while (index < tokens.length && SHELL_ASSIGNMENT_WORD.test(tokens[index]))
|
|
80
|
+
index += 1;
|
|
81
|
+
if (index >= tokens.length)
|
|
82
|
+
return { tokens, envWrapped: false };
|
|
83
|
+
return { tokens: tokens.slice(index), envWrapped: true };
|
|
84
|
+
}
|
|
85
|
+
const KNOWN_PROMPT_REF_FAMILIES = new Set([
|
|
86
|
+
"agents",
|
|
87
|
+
"commands",
|
|
88
|
+
"env",
|
|
89
|
+
"facts",
|
|
90
|
+
"instructions",
|
|
91
|
+
"knowledge",
|
|
92
|
+
"lessons",
|
|
93
|
+
"memories",
|
|
94
|
+
"scripts",
|
|
95
|
+
"secrets",
|
|
96
|
+
"sessions",
|
|
97
|
+
"skills",
|
|
98
|
+
"tasks",
|
|
99
|
+
"workflows",
|
|
100
|
+
]);
|
|
101
|
+
function hash(bytes) {
|
|
102
|
+
return crypto.createHash("sha256").update(bytes).digest("hex");
|
|
103
|
+
}
|
|
104
|
+
function base(input) {
|
|
105
|
+
return {
|
|
106
|
+
filePath: input.filePath,
|
|
107
|
+
before: Buffer.from(input.bytes),
|
|
108
|
+
beforeHash: hash(input.bytes),
|
|
109
|
+
mode: input.mode,
|
|
110
|
+
writable: input.writable,
|
|
111
|
+
...(input.onDiskWritable !== undefined ? { onDiskWritable: input.onDiskWritable } : {}),
|
|
112
|
+
...(input.containmentRoot ? { containmentRoot: input.containmentRoot } : {}),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function blocked(input, reason, detail) {
|
|
116
|
+
return Object.freeze({ status: "blocked", ...base(input), reason, ...(detail ? { detail } : {}) });
|
|
117
|
+
}
|
|
118
|
+
function plainRecord(value, label) {
|
|
119
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
120
|
+
throw new Error(`${label} must be a mapping`);
|
|
121
|
+
const prototype = Object.getPrototypeOf(value);
|
|
122
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
123
|
+
throw new Error(`${label} must use a plain or null prototype`);
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
function exactString(value, label, nonempty = false) {
|
|
127
|
+
if (typeof value !== "string" || (nonempty && value.trim().length === 0)) {
|
|
128
|
+
throw new Error(`${label} must be ${nonempty ? "a non-empty " : "a "}string`);
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
function optionalString(value, label) {
|
|
133
|
+
if (value === undefined || value === null)
|
|
134
|
+
return undefined;
|
|
135
|
+
return exactString(value, label);
|
|
136
|
+
}
|
|
137
|
+
function parseLegacyTaskYaml(input) {
|
|
138
|
+
if (input.bytes.byteLength > TASK_V3_MAX_SOURCE_BYTES) {
|
|
139
|
+
throw new Error(`task YAML exceeds the 1 MiB (${TASK_V3_MAX_SOURCE_BYTES}-byte) source resource limit`);
|
|
140
|
+
}
|
|
141
|
+
let source;
|
|
142
|
+
try {
|
|
143
|
+
source = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(input.bytes);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
throw new Error("task YAML contains invalid UTF-8 bytes");
|
|
147
|
+
}
|
|
148
|
+
const lineCounter = new LineCounter();
|
|
149
|
+
let document;
|
|
150
|
+
try {
|
|
151
|
+
document = parseDocument(source, { lineCounter, uniqueKeys: true });
|
|
152
|
+
}
|
|
153
|
+
catch (cause) {
|
|
154
|
+
throw new Error(`invalid YAML: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
155
|
+
}
|
|
156
|
+
const [parseError] = document.errors;
|
|
157
|
+
if (parseError)
|
|
158
|
+
throw new Error(`invalid YAML: ${parseError.message.split("\n")[0]}`);
|
|
159
|
+
const [parseWarning] = document.warnings;
|
|
160
|
+
if (parseWarning)
|
|
161
|
+
throw new Error(`unsupported YAML construct: ${parseWarning.message}`);
|
|
162
|
+
assertBoundedTaskYamlDocument(document, {
|
|
163
|
+
filePath: input.filePath,
|
|
164
|
+
sourceLabel: "task v2 migration source",
|
|
165
|
+
lineCounter,
|
|
166
|
+
});
|
|
167
|
+
return { data: plainRecord(document.toJS({ maxAliasCount: 0 }), "task YAML"), source };
|
|
168
|
+
}
|
|
169
|
+
function validateCommonV2(data) {
|
|
170
|
+
const unknown = Object.keys(data).filter((key) => !V2_KEYS.has(key));
|
|
171
|
+
if (unknown.length > 0)
|
|
172
|
+
throw new Error(`unknown v2 field(s): ${unknown.join(", ")}`);
|
|
173
|
+
exactString(data.schedule, "schedule", true);
|
|
174
|
+
if (data.enabled !== undefined && typeof data.enabled !== "boolean")
|
|
175
|
+
throw new Error("enabled must be a boolean");
|
|
176
|
+
for (const key of ["name", "description", "when_to_use"])
|
|
177
|
+
optionalString(data[key], key);
|
|
178
|
+
if (data.tags !== undefined && data.tags !== null) {
|
|
179
|
+
if (!Array.isArray(data.tags) || data.tags.some((entry) => typeof entry !== "string" || entry.length === 0)) {
|
|
180
|
+
throw new Error("tags must be an array of non-empty strings");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (data.timeoutMs !== undefined && data.timeoutMs !== null) {
|
|
184
|
+
if (!Number.isInteger(data.timeoutMs) ||
|
|
185
|
+
data.timeoutMs < 1 ||
|
|
186
|
+
data.timeoutMs > WORKFLOW_MAX_TIMEOUT_MS) {
|
|
187
|
+
throw new Error(`timeoutMs must be null or an integer from 1 through ${WORKFLOW_MAX_TIMEOUT_MS}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (data.redact !== undefined && data.redact !== null) {
|
|
191
|
+
if (!Array.isArray(data.redact) ||
|
|
192
|
+
data.redact.length > WORKFLOW_MAX_EXEC_PASS_ENV ||
|
|
193
|
+
data.redact.some((entry) => typeof entry !== "string" || !WORKFLOW_ENV_VAR_NAME_PATTERN.test(entry))) {
|
|
194
|
+
throw new Error("redact must contain only bounded environment variable names");
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function validateTargetFields(data, allowed) {
|
|
199
|
+
const targetFields = new Set([...allowed, "workflow", "prompt", "command"]);
|
|
200
|
+
const invalid = Object.keys(data).filter((key) => !V2_SHARED_KEYS.has(key) && !targetFields.has(key));
|
|
201
|
+
if (invalid.length > 0)
|
|
202
|
+
throw new Error(`field(s) not valid for this target: ${invalid.join(", ")}`);
|
|
203
|
+
}
|
|
204
|
+
function validateV2Llm(value) {
|
|
205
|
+
if (value === undefined)
|
|
206
|
+
return undefined;
|
|
207
|
+
const llm = plainRecord(value, "llm");
|
|
208
|
+
const unknown = Object.keys(llm).filter((key) => !V2_LLM_KEYS.has(key));
|
|
209
|
+
if (unknown.length > 0)
|
|
210
|
+
throw new Error(`llm has unknown field(s): ${unknown.join(", ")}`);
|
|
211
|
+
if (llm.temperature !== undefined && (typeof llm.temperature !== "number" || !Number.isFinite(llm.temperature))) {
|
|
212
|
+
throw new Error("llm.temperature must be a finite number");
|
|
213
|
+
}
|
|
214
|
+
for (const key of ["maxTokens", "contextLength"]) {
|
|
215
|
+
if (llm[key] !== undefined && (!Number.isInteger(llm[key]) || llm[key] <= 0)) {
|
|
216
|
+
throw new Error(`llm.${key} must be a positive integer`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
for (const key of ["supportsJsonSchema", "enableThinking"]) {
|
|
220
|
+
if (llm[key] !== undefined && typeof llm[key] !== "boolean")
|
|
221
|
+
throw new Error(`llm.${key} must be a boolean`);
|
|
222
|
+
}
|
|
223
|
+
if (llm.reasoningEffort !== undefined && (typeof llm.reasoningEffort !== "string" || !llm.reasoningEffort.trim())) {
|
|
224
|
+
throw new Error("llm.reasoningEffort must be a non-empty string");
|
|
225
|
+
}
|
|
226
|
+
if (llm.extraParams !== undefined) {
|
|
227
|
+
const issue = validateExtraParams(llm.extraParams)[0];
|
|
228
|
+
if (issue)
|
|
229
|
+
throw new Error(formatExtraParamsIssue("llm.extraParams", issue));
|
|
230
|
+
}
|
|
231
|
+
return llm;
|
|
232
|
+
}
|
|
233
|
+
function commonAkm(data) {
|
|
234
|
+
const akm = {
|
|
235
|
+
schedule: exactString(data.schedule, "schedule", true),
|
|
236
|
+
enabled: data.enabled === undefined ? true : data.enabled,
|
|
237
|
+
};
|
|
238
|
+
for (const key of ["description", "when_to_use", "tags"]) {
|
|
239
|
+
if (data[key] !== undefined && data[key] !== null)
|
|
240
|
+
akm[key] = data[key];
|
|
241
|
+
}
|
|
242
|
+
return akm;
|
|
243
|
+
}
|
|
244
|
+
function addRuntimeOverrides(data, akm) {
|
|
245
|
+
for (const key of ["engine", "model"]) {
|
|
246
|
+
const value = optionalString(data[key], key);
|
|
247
|
+
if (value)
|
|
248
|
+
akm[key] = value;
|
|
249
|
+
}
|
|
250
|
+
const llm = validateV2Llm(data.llm);
|
|
251
|
+
if (llm !== undefined)
|
|
252
|
+
akm.inference = llm;
|
|
253
|
+
if (data.timeoutMs !== undefined)
|
|
254
|
+
akm.timeout = data.timeoutMs;
|
|
255
|
+
if (data.redact !== undefined && data.redact !== null) {
|
|
256
|
+
akm.redact = [...new Set(data.redact)];
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function addSharedNonPromptOverrides(data, akm) {
|
|
260
|
+
if (data.timeoutMs !== undefined)
|
|
261
|
+
akm.timeout = data.timeoutMs;
|
|
262
|
+
if (data.redact !== undefined && data.redact !== null) {
|
|
263
|
+
akm.redact = [...new Set(data.redact)];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function promptSourceKind(raw) {
|
|
267
|
+
const trimmed = raw.trim();
|
|
268
|
+
if (trimmed.startsWith("./") ||
|
|
269
|
+
trimmed.startsWith("../") ||
|
|
270
|
+
path.isAbsolute(trimmed) ||
|
|
271
|
+
/^[A-Za-z]:[\\/]/.test(trimmed)) {
|
|
272
|
+
return "file";
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
const parsed = parseBundleRef(trimmed);
|
|
276
|
+
const family = parsed.conceptId.split("/", 1)[0] ?? "";
|
|
277
|
+
if (bundleRefToString(parsed) !== trimmed || !parsed.conceptId.includes("/")) {
|
|
278
|
+
return "inline";
|
|
279
|
+
}
|
|
280
|
+
if (!KNOWN_PROMPT_REF_FAMILIES.has(family))
|
|
281
|
+
return parsed.bundle === undefined ? "inline" : "other-ref";
|
|
282
|
+
if (family === "agents")
|
|
283
|
+
return "agent";
|
|
284
|
+
if (family === "commands")
|
|
285
|
+
return "command";
|
|
286
|
+
return "other-ref";
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
return "inline";
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
function migratedObject(data) {
|
|
293
|
+
validateCommonV2(data);
|
|
294
|
+
const targets = ["workflow", "prompt", "command"].filter((key) => Object.hasOwn(data, key) && data[key] !== null && data[key] !== "");
|
|
295
|
+
if (targets.length !== 1)
|
|
296
|
+
throw new Error("v2 task must declare exactly one of workflow, prompt, or command");
|
|
297
|
+
const output = { version: 3 };
|
|
298
|
+
if (data.name !== undefined && data.name !== null)
|
|
299
|
+
output.name = data.name;
|
|
300
|
+
const akm = commonAkm(data);
|
|
301
|
+
if (targets[0] === "workflow") {
|
|
302
|
+
validateTargetFields(data, ["params", "timeoutMs", "maxSteps", "maxRetries"]);
|
|
303
|
+
const ref = exactString(data.workflow, "workflow", true).trim();
|
|
304
|
+
let target;
|
|
305
|
+
try {
|
|
306
|
+
target = classifyTaskV3Uses(ref);
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
throw new Error("workflow is not a canonical v3 asset ref");
|
|
310
|
+
}
|
|
311
|
+
if (target.kind !== "workflow")
|
|
312
|
+
throw new Error("workflow target is not a workflows/ ref");
|
|
313
|
+
output.uses = ref;
|
|
314
|
+
if (data.params !== undefined && data.params !== null) {
|
|
315
|
+
const params = plainRecord(data.params, "params");
|
|
316
|
+
output.with = params;
|
|
317
|
+
}
|
|
318
|
+
if (data.maxSteps !== undefined && data.maxSteps !== null) {
|
|
319
|
+
if (!Number.isSafeInteger(data.maxSteps) || data.maxSteps < 1)
|
|
320
|
+
throw new Error("maxSteps must be positive");
|
|
321
|
+
akm.maxSteps = data.maxSteps;
|
|
322
|
+
}
|
|
323
|
+
if (data.maxRetries !== undefined && data.maxRetries !== null) {
|
|
324
|
+
if (!Number.isSafeInteger(data.maxRetries) ||
|
|
325
|
+
data.maxRetries < 0 ||
|
|
326
|
+
data.maxRetries > WORKFLOW_MAX_RETRIES) {
|
|
327
|
+
throw new Error(`maxRetries must be from 0 through ${WORKFLOW_MAX_RETRIES}`);
|
|
328
|
+
}
|
|
329
|
+
akm.maxRetries = data.maxRetries;
|
|
330
|
+
}
|
|
331
|
+
addSharedNonPromptOverrides(data, akm);
|
|
332
|
+
}
|
|
333
|
+
else if (targets[0] === "prompt") {
|
|
334
|
+
validateTargetFields(data, ["engine", "model", "timeoutMs", "llm"]);
|
|
335
|
+
const prompt = exactString(data.prompt, "prompt", true).trim();
|
|
336
|
+
const kind = promptSourceKind(prompt);
|
|
337
|
+
if (kind === "file")
|
|
338
|
+
return "dynamic-file-read-cannot-be-inlined-without-changing-semantics";
|
|
339
|
+
if (kind === "agent")
|
|
340
|
+
return "agent-ref-has-persona-but-no-command-work";
|
|
341
|
+
if (kind === "other-ref")
|
|
342
|
+
return "non-command-asset-has-no-v3-command-ref-equivalent";
|
|
343
|
+
if (kind === "command")
|
|
344
|
+
output.uses = prompt;
|
|
345
|
+
else {
|
|
346
|
+
output.uses = "akm/command";
|
|
347
|
+
output.with = { content: prompt };
|
|
348
|
+
}
|
|
349
|
+
addRuntimeOverrides(data, akm);
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
validateTargetFields(data, ["timeoutMs"]);
|
|
353
|
+
if (Array.isArray(data.command))
|
|
354
|
+
return "argv-array-has-no-portable-shell-string";
|
|
355
|
+
const command = exactString(data.command, "command", true).trim();
|
|
356
|
+
if (/['"\\]/.test(command))
|
|
357
|
+
return "shell-quoting-changes-v2-whitespace-split-semantics";
|
|
358
|
+
const tokens = command.split(/\s+/).filter(Boolean);
|
|
359
|
+
if (tokens.length === 0 || tokens.some((token) => !SAFE_V2_COMMAND_TOKEN.test(token))) {
|
|
360
|
+
return "shell-operators-change-v2-literal-argv-semantics";
|
|
361
|
+
}
|
|
362
|
+
const { tokens: targetTokens, envWrapped } = skipEnvAssignmentPrefix(tokens);
|
|
363
|
+
const executable = targetTokens[0];
|
|
364
|
+
if (SHELL_ASSIGNMENT_WORD.test(executable) || (!envWrapped && !shellStableV2Executable(executable))) {
|
|
365
|
+
return "shell-command-resolution-changes-v2-literal-argv-semantics";
|
|
366
|
+
}
|
|
367
|
+
output.run = tokens.join(" ");
|
|
368
|
+
addSharedNonPromptOverrides(data, akm);
|
|
369
|
+
}
|
|
370
|
+
output.akm = akm;
|
|
371
|
+
return output;
|
|
372
|
+
}
|
|
373
|
+
function isReason(value) {
|
|
374
|
+
return typeof value === "string";
|
|
375
|
+
}
|
|
376
|
+
/** Convert one already-normalized legacy record directly to final task v3. */
|
|
377
|
+
export function planLegacyTaskDataToV3(input, data) {
|
|
378
|
+
if (data.version !== 2) {
|
|
379
|
+
return blocked(input, "unsupported-task-version", `expected normalized legacy version 2, got ${String(data.version)}`);
|
|
380
|
+
}
|
|
381
|
+
if (!input.writable || input.onDiskWritable === false) {
|
|
382
|
+
return blocked(input, "read-only-source", !input.writable ? "the owning source is not writable" : "the source file or publication directory is read-only");
|
|
383
|
+
}
|
|
384
|
+
try {
|
|
385
|
+
validateTaskId(path.basename(input.filePath, ".yml"));
|
|
386
|
+
}
|
|
387
|
+
catch (cause) {
|
|
388
|
+
return blocked(input, "invalid-v2-task", cause instanceof Error ? cause.message : String(cause));
|
|
389
|
+
}
|
|
390
|
+
let migrated;
|
|
391
|
+
try {
|
|
392
|
+
migrated = migratedObject(data);
|
|
393
|
+
}
|
|
394
|
+
catch (cause) {
|
|
395
|
+
return blocked(input, "invalid-v2-task", cause instanceof Error ? cause.message : String(cause));
|
|
396
|
+
}
|
|
397
|
+
if (isReason(migrated))
|
|
398
|
+
return blocked(input, migrated);
|
|
399
|
+
const after = Buffer.from(stringifyYaml(migrated), "utf8");
|
|
400
|
+
try {
|
|
401
|
+
parseTaskV3Yaml({
|
|
402
|
+
yaml: after.toString("utf8"),
|
|
403
|
+
filePath: input.filePath,
|
|
404
|
+
...(input.containmentRoot ? { workspaceRoot: input.containmentRoot } : {}),
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
catch (cause) {
|
|
408
|
+
return blocked(input, "generated-v3-validation-failed", cause instanceof Error ? cause.message : String(cause));
|
|
409
|
+
}
|
|
410
|
+
return Object.freeze({
|
|
411
|
+
status: "changed",
|
|
412
|
+
...base(input),
|
|
413
|
+
reason: "task-converted",
|
|
414
|
+
after,
|
|
415
|
+
afterHash: hash(after),
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
/** Plan exactly one source file without touching disk. */
|
|
419
|
+
export function planTaskToV3File(input) {
|
|
420
|
+
let data;
|
|
421
|
+
let source;
|
|
422
|
+
try {
|
|
423
|
+
({ data, source } = parseLegacyTaskYaml(input));
|
|
424
|
+
}
|
|
425
|
+
catch (cause) {
|
|
426
|
+
return blocked(input, "invalid-task-yaml", cause instanceof Error ? cause.message : String(cause));
|
|
427
|
+
}
|
|
428
|
+
if (data.version === 3) {
|
|
429
|
+
try {
|
|
430
|
+
parseTaskV3Yaml({
|
|
431
|
+
yaml: source,
|
|
432
|
+
filePath: input.filePath,
|
|
433
|
+
...(input.containmentRoot ? { workspaceRoot: input.containmentRoot } : {}),
|
|
434
|
+
});
|
|
435
|
+
return Object.freeze({ status: "skipped", ...base(input), reason: "already-v3" });
|
|
436
|
+
}
|
|
437
|
+
catch (cause) {
|
|
438
|
+
return blocked(input, "invalid-v3-task", cause instanceof Error ? cause.message : String(cause));
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (data.version === 4) {
|
|
442
|
+
try {
|
|
443
|
+
parseTaskSourceV4({
|
|
444
|
+
yaml: source,
|
|
445
|
+
filePath: input.filePath,
|
|
446
|
+
...(input.containmentRoot ? { workspaceRoot: input.containmentRoot } : {}),
|
|
447
|
+
});
|
|
448
|
+
return Object.freeze({ status: "skipped", ...base(input), reason: "already-v4" });
|
|
449
|
+
}
|
|
450
|
+
catch (cause) {
|
|
451
|
+
return blocked(input, "invalid-v4-task", cause instanceof Error ? cause.message : String(cause));
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
if (data.version !== 2) {
|
|
455
|
+
return blocked(input, "unsupported-task-version", `expected version 2, 3, or 4, got ${String(data.version)}`);
|
|
456
|
+
}
|
|
457
|
+
return planLegacyTaskDataToV3(input, data);
|
|
458
|
+
}
|
|
459
|
+
function generationFor(files) {
|
|
460
|
+
const digest = crypto.createHash("sha256");
|
|
461
|
+
digest.update("akm-task-to-v3-plan-v1\0");
|
|
462
|
+
for (const file of files) {
|
|
463
|
+
digest.update(file.filePath);
|
|
464
|
+
digest.update("\0");
|
|
465
|
+
digest.update(file.status);
|
|
466
|
+
digest.update("\0");
|
|
467
|
+
digest.update(file.reason);
|
|
468
|
+
digest.update("\0");
|
|
469
|
+
digest.update(String(file.mode));
|
|
470
|
+
digest.update("\0");
|
|
471
|
+
digest.update(file.writable ? "writable" : "read-only");
|
|
472
|
+
digest.update("\0");
|
|
473
|
+
digest.update(file.onDiskWritable === false ? "disk-read-only" : "disk-writable-or-unspecified");
|
|
474
|
+
digest.update("\0");
|
|
475
|
+
if (file.containmentRoot)
|
|
476
|
+
digest.update(file.containmentRoot);
|
|
477
|
+
digest.update("\0");
|
|
478
|
+
digest.update(file.beforeHash);
|
|
479
|
+
digest.update("\0");
|
|
480
|
+
if (file.status === "changed")
|
|
481
|
+
digest.update(file.afterHash);
|
|
482
|
+
digest.update("\0");
|
|
483
|
+
if (file.detail)
|
|
484
|
+
digest.update(file.detail);
|
|
485
|
+
digest.update("\0");
|
|
486
|
+
}
|
|
487
|
+
return digest.digest("hex");
|
|
488
|
+
}
|
|
489
|
+
/** Build/fingerprint a plan from already-derived immutable outcomes. */
|
|
490
|
+
export function taskToV3PlanFromOutcomes(outcomes) {
|
|
491
|
+
const files = [...outcomes].sort((left, right) => left.filePath < right.filePath ? -1 : left.filePath > right.filePath ? 1 : 0);
|
|
492
|
+
for (let index = 1; index < files.length; index += 1) {
|
|
493
|
+
const previous = files[index - 1];
|
|
494
|
+
const current = files[index];
|
|
495
|
+
if (previous && current && path.resolve(previous.filePath) === path.resolve(current.filePath)) {
|
|
496
|
+
throw new Error(`duplicate task migration file path: ${current.filePath}`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return Object.freeze({ schemaVersion: 1, generation: generationFor(files), files: Object.freeze(files) });
|
|
500
|
+
}
|
|
501
|
+
/** Plan a complete, stable file set. Input order cannot change the result. */
|
|
502
|
+
export function planTaskToV3Migration(inputs) {
|
|
503
|
+
const sorted = [...inputs].sort((left, right) => left.filePath < right.filePath ? -1 : left.filePath > right.filePath ? 1 : 0);
|
|
504
|
+
let previous;
|
|
505
|
+
for (const current of sorted) {
|
|
506
|
+
if (previous && path.resolve(previous.filePath) === path.resolve(current.filePath)) {
|
|
507
|
+
throw new Error(`duplicate task migration file path: ${current.filePath}`);
|
|
508
|
+
}
|
|
509
|
+
previous = current;
|
|
510
|
+
}
|
|
511
|
+
return taskToV3PlanFromOutcomes(sorted.map(planTaskToV3File));
|
|
512
|
+
}
|