@osolmaz/pi-workflows 0.10.0 → 0.11.1
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 +32 -14
- package/dist/builtins/autodoc.workflow.d.ts +58 -0
- package/dist/builtins/autodoc.workflow.js +266 -0
- package/dist/builtins/autodoc.workflow.js.map +1 -0
- package/dist/builtins/autoimplement.workflow.d.ts +212 -11
- package/dist/builtins/autoimplement.workflow.js +431 -22
- package/dist/builtins/autoimplement.workflow.js.map +1 -1
- package/dist/builtins/{autodevise.workflow.d.ts → autoplan.workflow.d.ts} +9 -9
- package/dist/builtins/{autodevise.workflow.js → autoplan.workflow.js} +20 -20
- package/dist/builtins/autoplan.workflow.js.map +1 -0
- package/dist/builtins/catalog.js +8 -4
- package/dist/builtins/catalog.js.map +1 -1
- package/dist/builtins/index.d.ts +6 -3
- package/dist/builtins/index.js +5 -2
- package/dist/builtins/index.js.map +1 -1
- package/dist/builtins/monitor.workflow.d.ts +4 -0
- package/dist/builtins/monitor.workflow.js +134 -9
- package/dist/builtins/monitor.workflow.js.map +1 -1
- package/dist/builtins/plan-approval.workflow.d.ts +83 -0
- package/dist/builtins/plan-approval.workflow.js +148 -0
- package/dist/builtins/plan-approval.workflow.js.map +1 -0
- package/dist/builtins/plan-presentation.d.ts +7 -0
- package/dist/builtins/plan-presentation.js +44 -0
- package/dist/builtins/plan-presentation.js.map +1 -0
- package/dist/extension/decision-channels.d.ts +132 -0
- package/dist/extension/decision-channels.js +1082 -0
- package/dist/extension/decision-channels.js.map +1 -0
- package/dist/extension/index.d.ts +1 -0
- package/dist/extension/index.js +426 -11
- package/dist/extension/index.js.map +1 -1
- package/dist/extension/widget.js +26 -2
- package/dist/extension/widget.js.map +1 -1
- package/dist/extension/workflow-tool.d.ts +1 -37
- package/dist/extension/workflow-tool.js +1 -42
- package/dist/extension/workflow-tool.js.map +1 -1
- package/dist/herdr/setup.d.ts +13 -1
- package/dist/herdr/setup.js +349 -36
- package/dist/herdr/setup.js.map +1 -1
- package/dist/host/rpc-bridge.js +4 -21
- package/dist/host/rpc-bridge.js.map +1 -1
- package/dist/render/graph-render.js +33 -8
- package/dist/render/graph-render.js.map +1 -1
- package/dist/viewer/cli.d.ts +1 -0
- package/dist/viewer/cli.js +21 -10
- package/dist/viewer/cli.js.map +1 -1
- package/dist/viewer/render.js +36 -2
- package/dist/viewer/render.js.map +1 -1
- package/dist/workflows/composition.js +15 -1
- package/dist/workflows/composition.js.map +1 -1
- package/dist/workflows/decision-presentation.d.ts +18 -0
- package/dist/workflows/decision-presentation.js +417 -0
- package/dist/workflows/decision-presentation.js.map +1 -0
- package/dist/workflows/engine.d.ts +2 -1
- package/dist/workflows/engine.js +86 -4
- package/dist/workflows/engine.js.map +1 -1
- package/dist/workflows/human-decision.d.ts +87 -0
- package/dist/workflows/human-decision.js +583 -0
- package/dist/workflows/human-decision.js.map +1 -0
- package/dist/workflows/index.d.ts +3 -1
- package/dist/workflows/index.js +2 -0
- package/dist/workflows/index.js.map +1 -1
- package/dist/workflows/schema.js +15 -0
- package/dist/workflows/schema.js.map +1 -1
- package/dist/workflows/store.js +7 -0
- package/dist/workflows/store.js.map +1 -1
- package/dist/workflows/tool-input.d.ts +72 -0
- package/dist/workflows/tool-input.js +141 -0
- package/dist/workflows/tool-input.js.map +1 -0
- package/dist/workflows/types.d.ts +223 -0
- package/docs/HUMAN_DECISIONS.md +371 -0
- package/docs/HUMAN_DECISION_PRESENTATIONS.md +323 -0
- package/docs/MONITOR.md +17 -6
- package/docs/WORKFLOW_COMPOSITION.md +13 -6
- package/docs/plans/2026-08-19-human-decision-gates-plan.md +296 -0
- package/docs/plans/2026-08-19-human-decision-presentations-plan.md +172 -0
- package/docs/plans/2026-08-19-provider-compatible-workflow-tool-schema-plan.md +45 -0
- package/docs/plans/2026-08-19-workflow-composition-plan.md +25 -17
- package/docs/plans/2026-08-20-autoimplement-blocker-challenge-plan.md +138 -0
- package/docs/plans/2026-08-20-herdr-plugin-sync-plan.md +104 -0
- package/docs/run-bundles.md +23 -2
- package/docs/workflows.md +47 -5
- package/examples/workflows/approved-plan.workflow.ts +58 -0
- package/examples/workflows/autoplan.workflow.ts +1 -0
- package/examples/workflows/human-decision.workflow.ts +62 -0
- package/herdr-plugin.toml +1 -1
- package/package.json +2 -1
- package/schemas/decision-presentation-v1.schema.json +83 -0
- package/schemas/human-decision-accepted-v1.schema.json +44 -0
- package/schemas/human-decision-accepted-v2.schema.json +50 -0
- package/schemas/human-decision-answer-attempt-v1.schema.json +37 -0
- package/schemas/human-decision-cancellation-v1.schema.json +15 -0
- package/schemas/human-decision-continuation-v1.schema.json +16 -0
- package/schemas/human-decision-delivery-v1.schema.json +28 -0
- package/schemas/human-decision-delivery-v2.schema.json +36 -0
- package/schemas/human-decision-receipt-v1.schema.json +33 -0
- package/schemas/human-decision-receipt-v2.schema.json +39 -0
- package/schemas/human-decision-request-v1.schema.json +59 -0
- package/schemas/human-decision-request-v2.schema.json +69 -0
- package/schemas/human-decision-resolution-v1.schema.json +27 -0
- package/schemas/human-decision-resolution-v2.schema.json +27 -0
- package/schemas/human-decision-settlement-v1.schema.json +28 -0
- package/skills/autodoc/SKILL.md +43 -0
- package/skills/autoimplement/SKILL.md +58 -0
- package/skills/autoimplement/agents/openai.yaml +4 -0
- package/skills/autoplan/SKILL.md +25 -0
- package/skills/monitor/SKILL.md +3 -1
- package/skills/pi-workflows/SKILL.md +3 -3
- package/src/builtins/autodoc.workflow.ts +325 -0
- package/src/builtins/autoimplement.workflow.ts +493 -23
- package/src/builtins/{autodevise.workflow.ts → autoplan.workflow.ts} +32 -32
- package/src/builtins/catalog.ts +8 -4
- package/src/builtins/index.ts +25 -6
- package/src/builtins/monitor.workflow.ts +162 -8
- package/src/builtins/plan-approval.workflow.ts +190 -0
- package/src/builtins/plan-presentation.ts +57 -0
- package/src/extension/decision-channels.ts +1533 -0
- package/src/extension/index.ts +499 -9
- package/src/extension/widget.ts +39 -2
- package/src/extension/workflow-tool.ts +5 -81
- package/src/herdr/setup.ts +429 -39
- package/src/host/rpc-bridge.ts +7 -30
- package/src/render/graph-render.ts +40 -3
- package/src/viewer/cli.ts +22 -10
- package/src/viewer/render.ts +56 -3
- package/src/workflows/composition.ts +16 -1
- package/src/workflows/decision-presentation.ts +501 -0
- package/src/workflows/engine.ts +108 -4
- package/src/workflows/human-decision.ts +819 -0
- package/src/workflows/index.ts +69 -0
- package/src/workflows/schema.ts +17 -0
- package/src/workflows/store.ts +8 -0
- package/src/workflows/tool-input.ts +202 -0
- package/src/workflows/types.ts +265 -0
- package/dist/builtins/autodevise.workflow.js.map +0 -1
- package/examples/workflows/autodevise.workflow.ts +0 -1
package/src/extension/widget.ts
CHANGED
|
@@ -297,8 +297,36 @@ function nodeRuntimeSegments(
|
|
|
297
297
|
}
|
|
298
298
|
|
|
299
299
|
if (state.waitingOn === nodeId) {
|
|
300
|
-
const
|
|
301
|
-
|
|
300
|
+
const human = snapshot.nodes[nodeId]?.humanDecision;
|
|
301
|
+
if (human !== undefined) {
|
|
302
|
+
const request =
|
|
303
|
+
state.finalOutput !== null && typeof state.finalOutput === "object"
|
|
304
|
+
? (state.finalOutput as {
|
|
305
|
+
schema?: unknown;
|
|
306
|
+
audience?: unknown;
|
|
307
|
+
presentationDigest?: unknown;
|
|
308
|
+
presentation?: { summary?: unknown };
|
|
309
|
+
})
|
|
310
|
+
: undefined;
|
|
311
|
+
const requestAudience =
|
|
312
|
+
(request?.schema === "pi-workflows.human-decision-request.v1" ||
|
|
313
|
+
request?.schema === "pi-workflows.human-decision-request.v2") &&
|
|
314
|
+
typeof request.audience === "string"
|
|
315
|
+
? request.audience
|
|
316
|
+
: human.audience;
|
|
317
|
+
segments.push(
|
|
318
|
+
`human decision · ${sanitizeText(requestAudience)}${typeof request?.presentation?.summary === "string" ? ` · ${sanitizeText(request.presentation.summary)}` : ""} · ${Object.values(
|
|
319
|
+
human.choices,
|
|
320
|
+
)
|
|
321
|
+
.map((choice) => sanitizeText(choice.label))
|
|
322
|
+
.join(
|
|
323
|
+
" / ",
|
|
324
|
+
)}${typeof request?.presentationDigest === "string" ? ` · ${request.presentationDigest.slice(7, 19)}` : ""}`,
|
|
325
|
+
);
|
|
326
|
+
} else {
|
|
327
|
+
const summary = snapshot.nodes[nodeId]?.summary;
|
|
328
|
+
segments.push(summary ? sanitizeText(summary) : "waiting");
|
|
329
|
+
}
|
|
302
330
|
return segments;
|
|
303
331
|
}
|
|
304
332
|
|
|
@@ -313,6 +341,15 @@ function nodeRuntimeSegments(
|
|
|
313
341
|
);
|
|
314
342
|
return segments;
|
|
315
343
|
}
|
|
344
|
+
const human = snapshot.nodes[nodeId]?.humanDecision;
|
|
345
|
+
if (
|
|
346
|
+
human !== undefined &&
|
|
347
|
+
state.humanDecision !== undefined &&
|
|
348
|
+
state.humanDecision.nodeId === nodeId
|
|
349
|
+
) {
|
|
350
|
+
const selected = human.choices[state.humanDecision.response.choice];
|
|
351
|
+
if (selected !== undefined) segments.push(`human: ${sanitizeText(selected.label)}`);
|
|
352
|
+
}
|
|
316
353
|
if (Number.isFinite(result.durationMs)) {
|
|
317
354
|
segments.push(formatDuration(result.durationMs));
|
|
318
355
|
}
|
|
@@ -1,81 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export const WorkflowToolParameters: TSchema = Type.Union([
|
|
7
|
-
Type.Object(
|
|
8
|
-
{
|
|
9
|
-
action: StringEnum(["list"] as const),
|
|
10
|
-
offset: Type.Optional(Type.Integer({ minimum: 0, description: "Workflow list offset" })),
|
|
11
|
-
},
|
|
12
|
-
noExtraProperties,
|
|
13
|
-
),
|
|
14
|
-
Type.Object(
|
|
15
|
-
{
|
|
16
|
-
action: StringEnum(["start"] as const),
|
|
17
|
-
workflow: Type.String({ description: "Discovered workflow name or workflow file path" }),
|
|
18
|
-
input: Type.Optional(Type.Unknown({ description: "Structured workflow input" })),
|
|
19
|
-
},
|
|
20
|
-
noExtraProperties,
|
|
21
|
-
),
|
|
22
|
-
Type.Object(
|
|
23
|
-
{
|
|
24
|
-
action: StringEnum(["status"] as const),
|
|
25
|
-
runId: Type.Optional(Type.String({ description: "Run id; omit for the active run" })),
|
|
26
|
-
},
|
|
27
|
-
noExtraProperties,
|
|
28
|
-
),
|
|
29
|
-
Type.Object({ action: StringEnum(["pause"] as const) }, noExtraProperties),
|
|
30
|
-
Type.Object({ action: StringEnum(["resume"] as const) }, noExtraProperties),
|
|
31
|
-
Type.Object({ action: StringEnum(["cancel"] as const) }, noExtraProperties),
|
|
32
|
-
Type.Object(
|
|
33
|
-
{
|
|
34
|
-
action: StringEnum(["answer"] as const),
|
|
35
|
-
input: Type.Unknown({ description: "Checkpoint answer" }),
|
|
36
|
-
runId: Type.Optional(Type.String({ description: "Waiting run id" })),
|
|
37
|
-
},
|
|
38
|
-
noExtraProperties,
|
|
39
|
-
),
|
|
40
|
-
Type.Object(
|
|
41
|
-
{
|
|
42
|
-
action: StringEnum(["update"] as const),
|
|
43
|
-
step: Type.String({ description: "Active step id" }),
|
|
44
|
-
attempt: Type.String({ description: "Active attempt id" }),
|
|
45
|
-
update: Type.Object(
|
|
46
|
-
{
|
|
47
|
-
type: Type.String(),
|
|
48
|
-
key: Type.String(),
|
|
49
|
-
data: Type.Record(Type.String(), Type.Unknown()),
|
|
50
|
-
},
|
|
51
|
-
noExtraProperties,
|
|
52
|
-
),
|
|
53
|
-
},
|
|
54
|
-
noExtraProperties,
|
|
55
|
-
),
|
|
56
|
-
Type.Object(
|
|
57
|
-
{
|
|
58
|
-
action: StringEnum(["submit"] as const),
|
|
59
|
-
step: Type.String({ description: "Step id from the workflow step contract" }),
|
|
60
|
-
attempt: Type.String({ description: "Attempt id from the workflow step contract" }),
|
|
61
|
-
output: Type.Unknown({ description: "Step output matching the expected shape" }),
|
|
62
|
-
},
|
|
63
|
-
noExtraProperties,
|
|
64
|
-
),
|
|
65
|
-
]);
|
|
66
|
-
|
|
67
|
-
export type WorkflowToolInput =
|
|
68
|
-
| { action: "list"; offset?: number }
|
|
69
|
-
| { action: "start"; workflow: string; input?: unknown }
|
|
70
|
-
| { action: "status"; runId?: string }
|
|
71
|
-
| { action: "pause" }
|
|
72
|
-
| { action: "resume" }
|
|
73
|
-
| { action: "cancel" }
|
|
74
|
-
| { action: "answer"; input: unknown; runId?: string }
|
|
75
|
-
| {
|
|
76
|
-
action: "update";
|
|
77
|
-
step: string;
|
|
78
|
-
attempt: string;
|
|
79
|
-
update: { type: string; key: string; data: Record<string, unknown> };
|
|
80
|
-
}
|
|
81
|
-
| { action: "submit"; step: string; attempt: string; output: unknown };
|
|
1
|
+
export {
|
|
2
|
+
parseWorkflowToolInput,
|
|
3
|
+
WorkflowToolParameters,
|
|
4
|
+
type WorkflowToolInput,
|
|
5
|
+
} from "../workflows/tool-input.js";
|
package/src/herdr/setup.ts
CHANGED
|
@@ -3,83 +3,469 @@ import fs from "node:fs";
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { HERDR_PLUGIN_ID } from "./constants.js";
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
const PACKAGE_NAME = "@osolmaz/pi-workflows";
|
|
7
|
+
const MANIFEST_NAME = "herdr-plugin.toml";
|
|
8
|
+
const VIEWER_PATH = "plugins/herdr/viewer.mjs";
|
|
9
|
+
const RESULT_SCHEMA = "pi-workflows.herdr-sync.v1" as const;
|
|
10
|
+
const MAX_COMMAND_OUTPUT_BYTES = 1024 * 1024;
|
|
11
|
+
|
|
12
|
+
export type HerdrSyncStatus = "linked" | "relinked" | "enabled" | "unchanged" | "unavailable";
|
|
13
|
+
|
|
14
|
+
export type HerdrSyncResult = {
|
|
15
|
+
schema: typeof RESULT_SCHEMA;
|
|
16
|
+
status: HerdrSyncStatus;
|
|
7
17
|
changed: boolean;
|
|
18
|
+
pluginId: string;
|
|
19
|
+
expectedVersion: string;
|
|
20
|
+
effectiveVersion: string | null;
|
|
21
|
+
enabled: boolean | null;
|
|
22
|
+
runningPiProcessesNeedReload: true;
|
|
8
23
|
message: string;
|
|
9
24
|
};
|
|
10
25
|
|
|
26
|
+
export type HerdrSetupResult = HerdrSyncResult;
|
|
27
|
+
|
|
11
28
|
type Spawn = (command: string, args: readonly string[]) => SpawnSyncReturns<string>;
|
|
12
29
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
30
|
+
type PluginPackage = {
|
|
31
|
+
root: string;
|
|
32
|
+
manifestPath: string;
|
|
33
|
+
version: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
type InstalledPlugin = {
|
|
37
|
+
root: string;
|
|
38
|
+
manifestPath: string;
|
|
39
|
+
version: string;
|
|
40
|
+
enabled: boolean;
|
|
41
|
+
warnings: string[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type Inspection = { available: true; plugin: InstalledPlugin | undefined } | { available: false };
|
|
45
|
+
|
|
46
|
+
type MutationResult = { ok: true } | { ok: false; error: string };
|
|
47
|
+
|
|
48
|
+
type RollbackTarget = { package: PluginPackage; enabled: boolean };
|
|
49
|
+
|
|
50
|
+
export function syncHerdrPlugin(packageRoot: string, spawn: Spawn = runCommand): HerdrSyncResult {
|
|
51
|
+
const expected = preflightPackage(packageRoot);
|
|
52
|
+
const initial = inspectHerdr(spawn, true);
|
|
53
|
+
if (!initial.available) {
|
|
54
|
+
return result("unavailable", false, expected.version, undefined);
|
|
18
55
|
}
|
|
19
56
|
|
|
20
|
-
const
|
|
21
|
-
if (
|
|
22
|
-
|
|
23
|
-
|
|
57
|
+
const installed = initial.plugin;
|
|
58
|
+
if (installed === undefined) {
|
|
59
|
+
const linked = mutate(spawn, "link", [expected.root]);
|
|
60
|
+
const adopted = verifyOrExplain(spawn, expected);
|
|
61
|
+
if (adopted.plugin !== undefined && matchesExpected(adopted.plugin, expected, true)) {
|
|
62
|
+
return result("linked", true, expected.version, adopted.plugin);
|
|
63
|
+
}
|
|
64
|
+
throw mutationError("link", linked, adopted.problem);
|
|
24
65
|
}
|
|
25
|
-
|
|
26
|
-
if (installed
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
66
|
+
|
|
67
|
+
if (matchesExpected(installed, expected, true)) {
|
|
68
|
+
return result("unchanged", false, expected.version, installed);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (matchesExpected(installed, expected, false) && !installed.enabled) {
|
|
72
|
+
const enabled = mutate(spawn, "enable", [HERDR_PLUGIN_ID]);
|
|
73
|
+
const adopted = verifyOrExplain(spawn, expected);
|
|
74
|
+
if (adopted.plugin !== undefined && matchesExpected(adopted.plugin, expected, true)) {
|
|
75
|
+
return result("enabled", true, expected.version, adopted.plugin);
|
|
31
76
|
}
|
|
32
|
-
|
|
33
|
-
|
|
77
|
+
throw mutationError("enable", enabled, adopted.problem);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const rollback = rollbackTarget(installed);
|
|
81
|
+
const unlinked = mutate(spawn, "unlink", [HERDR_PLUGIN_ID]);
|
|
82
|
+
const afterUnlink = inspectHerdr(spawn, false);
|
|
83
|
+
if (!afterUnlink.available) throw new Error("Herdr became unavailable during synchronization.");
|
|
84
|
+
if (afterUnlink.plugin !== undefined) {
|
|
85
|
+
if (matchesExpected(afterUnlink.plugin, expected, true)) {
|
|
86
|
+
return result("relinked", true, expected.version, afterUnlink.plugin);
|
|
34
87
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
88
|
+
throw mutationError(
|
|
89
|
+
"unlink the stale registration",
|
|
90
|
+
unlinked,
|
|
91
|
+
"Herdr still reports a conflicting registration.",
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const linked = mutate(spawn, "link", [expected.root]);
|
|
96
|
+
const adopted = verifyOrExplain(spawn, expected);
|
|
97
|
+
if (adopted.plugin !== undefined && matchesExpected(adopted.plugin, expected, true)) {
|
|
98
|
+
return result("relinked", true, expected.version, adopted.plugin);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const rollbackMessage = restorePrevious(spawn, expected, rollback);
|
|
102
|
+
throw new Error(
|
|
103
|
+
`${mutationError("link the current package", linked, adopted.problem).message} ${rollbackMessage}`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Compatibility alias for callers that used the original setup API. */
|
|
108
|
+
export function setupHerdrPlugin(packageRoot: string, spawn: Spawn = runCommand): HerdrSetupResult {
|
|
109
|
+
return syncHerdrPlugin(packageRoot, spawn);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function preflightPackage(packageRoot: string): PluginPackage {
|
|
113
|
+
const requestedRoot = path.resolve(packageRoot);
|
|
114
|
+
let root: string;
|
|
115
|
+
try {
|
|
116
|
+
root = fs.realpathSync(requestedRoot);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
throw new Error(`Pi Workflows package root is missing: ${requestedRoot}`, { cause: error });
|
|
119
|
+
}
|
|
120
|
+
if (!fs.statSync(root).isDirectory()) {
|
|
121
|
+
throw new Error(`Pi Workflows package root is not a directory: ${requestedRoot}`);
|
|
122
|
+
}
|
|
123
|
+
const packagePath = checkedRegularFile(root, "package.json");
|
|
124
|
+
const manifestPath = checkedRegularFile(root, MANIFEST_NAME);
|
|
125
|
+
const packageJson = parseJsonObject(fs.readFileSync(packagePath, "utf8"), "package.json");
|
|
126
|
+
if (packageJson["name"] !== PACKAGE_NAME) {
|
|
127
|
+
throw new Error(`Unexpected Pi Workflows package name in ${packagePath}.`);
|
|
128
|
+
}
|
|
129
|
+
const packageVersion = packageJson["version"];
|
|
130
|
+
if (typeof packageVersion !== "string" || packageVersion.length === 0) {
|
|
131
|
+
throw new Error(`Pi Workflows package version is missing in ${packagePath}.`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const manifest = fs.readFileSync(manifestPath, "utf8");
|
|
135
|
+
const topLevelManifest = manifest.split(/^\s*\[\[/mu, 1)[0] ?? "";
|
|
136
|
+
const pluginId = tomlString(topLevelManifest, "id", manifestPath);
|
|
137
|
+
const pluginVersion = tomlString(topLevelManifest, "version", manifestPath);
|
|
138
|
+
tomlString(topLevelManifest, "min_herdr_version", manifestPath);
|
|
139
|
+
const platforms = tomlStringArray(topLevelManifest, "platforms", manifestPath);
|
|
140
|
+
const command = tomlStringArray(manifest, "command", manifestPath);
|
|
141
|
+
if (pluginId !== HERDR_PLUGIN_ID) {
|
|
142
|
+
throw new Error(`Unexpected Herdr plugin ID in ${manifestPath}.`);
|
|
143
|
+
}
|
|
144
|
+
if (pluginVersion !== packageVersion) {
|
|
145
|
+
throw new Error(`Herdr plugin version does not match package version ${packageVersion}.`);
|
|
146
|
+
}
|
|
147
|
+
const platform = process.platform === "darwin" ? "macos" : process.platform;
|
|
148
|
+
if (!platforms.includes(platform)) {
|
|
149
|
+
throw new Error(`Herdr plugin does not support platform ${platform}.`);
|
|
150
|
+
}
|
|
151
|
+
if (command.length !== 2 || command[0] !== "node" || command[1] !== VIEWER_PATH) {
|
|
152
|
+
throw new Error(`Unexpected Herdr plugin viewer command in ${manifestPath}.`);
|
|
153
|
+
}
|
|
154
|
+
checkedRegularFile(root, VIEWER_PATH);
|
|
155
|
+
return { root, manifestPath, version: packageVersion };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function checkedRegularFile(root: string, relativePath: string): string {
|
|
159
|
+
const target = path.resolve(root, relativePath);
|
|
160
|
+
if (!isWithin(root, target)) {
|
|
161
|
+
throw new Error(`Package file escapes the package root: ${relativePath}`);
|
|
162
|
+
}
|
|
163
|
+
const stat = statOrThrow(target, `Package file ${relativePath}`);
|
|
164
|
+
if (!stat.isFile() || stat.isSymbolicLink()) {
|
|
165
|
+
throw new Error(`Package file is not a regular file: ${relativePath}`);
|
|
166
|
+
}
|
|
167
|
+
const real = fs.realpathSync(target);
|
|
168
|
+
if (!isWithin(root, real)) {
|
|
169
|
+
throw new Error(`Package file resolves outside the package root: ${relativePath}`);
|
|
170
|
+
}
|
|
171
|
+
return real;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function isWithin(root: string, target: string): boolean {
|
|
175
|
+
const relative = path.relative(root, target);
|
|
176
|
+
return relative === "" || (!relative.startsWith(`..${path.sep}`) && relative !== "..");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function statOrThrow(target: string, label: string): fs.Stats {
|
|
180
|
+
try {
|
|
181
|
+
return fs.lstatSync(target);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
throw new Error(`${label} is missing: ${target}`, { cause: error });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function parseJsonObject(text: string, label: string): Record<string, unknown> {
|
|
188
|
+
let value: unknown;
|
|
189
|
+
try {
|
|
190
|
+
value = JSON.parse(text) as unknown;
|
|
191
|
+
} catch (error) {
|
|
192
|
+
throw new Error(`${label} is not valid JSON.`, { cause: error });
|
|
193
|
+
}
|
|
194
|
+
if (!isRecord(value)) throw new Error(`${label} must contain a JSON object.`);
|
|
195
|
+
return value;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function tomlString(text: string, key: string, manifestPath: string): string {
|
|
199
|
+
const expressions = matches(
|
|
200
|
+
text,
|
|
201
|
+
new RegExp(String.raw`^\s*${escapeRegExp(key)}\s*=\s*("(?:[^"\\]|\\.)*")\s*(?:#.*)?$`, "gmu"),
|
|
202
|
+
);
|
|
203
|
+
if (expressions.length !== 1) {
|
|
204
|
+
throw new Error(`Herdr manifest must contain one ${key} string: ${manifestPath}`);
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
const value: unknown = JSON.parse(expressions[0] as string);
|
|
208
|
+
if (typeof value !== "string" || value.length === 0) throw new Error("empty string");
|
|
209
|
+
return value;
|
|
210
|
+
} catch (error) {
|
|
211
|
+
throw new Error(`Herdr manifest has an invalid ${key} string: ${manifestPath}`, {
|
|
212
|
+
cause: error,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function tomlStringArray(text: string, key: string, manifestPath: string): string[] {
|
|
218
|
+
const expressions = matches(
|
|
219
|
+
text,
|
|
220
|
+
new RegExp(
|
|
221
|
+
String.raw`^\s*${escapeRegExp(key)}\s*=\s*(\[(?:[^\]"\\]|"(?:[^"\\]|\\.)*")*\])\s*(?:#.*)?$`,
|
|
222
|
+
"gmu",
|
|
223
|
+
),
|
|
224
|
+
);
|
|
225
|
+
if (expressions.length !== 1) {
|
|
226
|
+
throw new Error(`Herdr manifest must contain one ${key} string array: ${manifestPath}`);
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
const value: unknown = JSON.parse(expressions[0] as string);
|
|
230
|
+
if (
|
|
231
|
+
!Array.isArray(value) ||
|
|
232
|
+
value.length === 0 ||
|
|
233
|
+
value.some((item) => typeof item !== "string")
|
|
234
|
+
) {
|
|
235
|
+
throw new Error("invalid string array");
|
|
41
236
|
}
|
|
42
|
-
return
|
|
237
|
+
return value as string[];
|
|
238
|
+
} catch (error) {
|
|
239
|
+
throw new Error(`Herdr manifest has an invalid ${key} string array: ${manifestPath}`, {
|
|
240
|
+
cause: error,
|
|
241
|
+
});
|
|
43
242
|
}
|
|
243
|
+
}
|
|
44
244
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
245
|
+
function matches(text: string, expression: RegExp): string[] {
|
|
246
|
+
const values: string[] = [];
|
|
247
|
+
for (const match of text.matchAll(expression)) {
|
|
248
|
+
const value = match[1];
|
|
249
|
+
if (value !== undefined) values.push(value);
|
|
49
250
|
}
|
|
50
|
-
return
|
|
251
|
+
return values;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function escapeRegExp(value: string): string {
|
|
255
|
+
return value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
51
256
|
}
|
|
52
257
|
|
|
53
|
-
function
|
|
258
|
+
function inspectHerdr(spawn: Spawn, allowUnavailable: boolean): Inspection {
|
|
259
|
+
const listed = spawn("herdr", ["plugin", "list", "--plugin", HERDR_PLUGIN_ID, "--json"]);
|
|
260
|
+
if (listed.error !== undefined) {
|
|
261
|
+
if (allowUnavailable && errorCode(listed.error) === "ENOENT") return { available: false };
|
|
262
|
+
throw new Error(`Could not run Herdr: ${listed.error.message}`);
|
|
263
|
+
}
|
|
264
|
+
if (listed.status !== 0) {
|
|
265
|
+
throw new Error(`Could not inspect Herdr plugins: ${bounded(listed.stderr || listed.stdout)}`);
|
|
266
|
+
}
|
|
267
|
+
return { available: true, plugin: installedPlugin(listed.stdout) };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function installedPlugin(stdout: string): InstalledPlugin | undefined {
|
|
54
271
|
let value: unknown;
|
|
55
272
|
try {
|
|
56
273
|
value = JSON.parse(stdout) as unknown;
|
|
57
274
|
} catch {
|
|
58
275
|
throw new Error("Herdr returned invalid plugin JSON.");
|
|
59
276
|
}
|
|
60
|
-
if (
|
|
277
|
+
if (
|
|
278
|
+
!isRecord(value) ||
|
|
279
|
+
!isRecord(value["result"]) ||
|
|
280
|
+
!Array.isArray(value["result"]["plugins"])
|
|
281
|
+
) {
|
|
61
282
|
throw new Error("Herdr returned an invalid plugin list.");
|
|
62
283
|
}
|
|
63
|
-
|
|
284
|
+
const candidates = value["result"]["plugins"].filter(
|
|
285
|
+
(plugin) => isRecord(plugin) && plugin["plugin_id"] === HERDR_PLUGIN_ID,
|
|
286
|
+
);
|
|
287
|
+
if (candidates.length > 1) {
|
|
288
|
+
throw new Error(`Herdr returned duplicate records for ${HERDR_PLUGIN_ID}.`);
|
|
289
|
+
}
|
|
290
|
+
const candidate = candidates[0];
|
|
291
|
+
if (candidate === undefined) return undefined;
|
|
292
|
+
if (
|
|
293
|
+
!isRecord(candidate) ||
|
|
294
|
+
typeof candidate["plugin_root"] !== "string" ||
|
|
295
|
+
typeof candidate["manifest_path"] !== "string" ||
|
|
296
|
+
typeof candidate["version"] !== "string" ||
|
|
297
|
+
typeof candidate["enabled"] !== "boolean"
|
|
298
|
+
) {
|
|
299
|
+
throw new Error(`Herdr returned an incomplete record for ${HERDR_PLUGIN_ID}.`);
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
root: normalizedPath(candidate["plugin_root"]),
|
|
303
|
+
manifestPath: normalizedPath(candidate["manifest_path"]),
|
|
304
|
+
version: candidate["version"],
|
|
305
|
+
enabled: candidate["enabled"],
|
|
306
|
+
warnings: pluginWarnings(candidate),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function pluginWarnings(plugin: Record<string, unknown>): string[] {
|
|
311
|
+
const warnings: string[] = [];
|
|
312
|
+
for (const key of ["warning", "manifest_warning"] as const) {
|
|
313
|
+
const value = plugin[key];
|
|
314
|
+
if (typeof value === "string" && value.trim().length > 0) warnings.push(value.trim());
|
|
315
|
+
}
|
|
316
|
+
const many = plugin["warnings"];
|
|
317
|
+
if (many !== undefined) {
|
|
318
|
+
if (!Array.isArray(many) || many.some((item) => typeof item !== "string")) {
|
|
319
|
+
throw new Error(`Herdr returned invalid warnings for ${HERDR_PLUGIN_ID}.`);
|
|
320
|
+
}
|
|
321
|
+
warnings.push(...many.filter((item) => item.trim().length > 0).map((item) => item.trim()));
|
|
322
|
+
}
|
|
323
|
+
return warnings;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function matchesExpected(
|
|
327
|
+
installed: InstalledPlugin,
|
|
328
|
+
expected: PluginPackage,
|
|
329
|
+
requireEnabled: boolean,
|
|
330
|
+
): boolean {
|
|
331
|
+
return (
|
|
332
|
+
installed.root === normalizedPath(expected.root) &&
|
|
333
|
+
installed.manifestPath === normalizedPath(expected.manifestPath) &&
|
|
334
|
+
installed.version === expected.version &&
|
|
335
|
+
(!requireEnabled || installed.enabled) &&
|
|
336
|
+
installed.warnings.length === 0
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function rollbackTarget(installed: InstalledPlugin): RollbackTarget | undefined {
|
|
341
|
+
try {
|
|
342
|
+
const previous = preflightPackage(installed.root);
|
|
343
|
+
return matchesExpected(installed, previous, installed.enabled)
|
|
344
|
+
? { package: previous, enabled: installed.enabled }
|
|
345
|
+
: undefined;
|
|
346
|
+
} catch {
|
|
347
|
+
return undefined;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function restorePrevious(
|
|
352
|
+
spawn: Spawn,
|
|
353
|
+
expected: PluginPackage,
|
|
354
|
+
previous: RollbackTarget | undefined,
|
|
355
|
+
): string {
|
|
356
|
+
if (previous === undefined) return "The previous registration could not be restored.";
|
|
357
|
+
try {
|
|
358
|
+
const current = inspectHerdr(spawn, false);
|
|
359
|
+
if (!current.available)
|
|
360
|
+
return "Herdr became unavailable; the previous registration was not restored.";
|
|
361
|
+
if (current.plugin !== undefined) {
|
|
362
|
+
if (!matchesExpected(current.plugin, expected, current.plugin.enabled)) {
|
|
363
|
+
return "A different registration appeared; the previous registration was not restored.";
|
|
364
|
+
}
|
|
365
|
+
const removed = mutate(spawn, "unlink", [HERDR_PLUGIN_ID]);
|
|
366
|
+
if (!removed.ok) return `The previous registration was not restored: ${removed.error}`;
|
|
367
|
+
}
|
|
368
|
+
const args = previous.enabled ? [previous.package.root] : [previous.package.root, "--disabled"];
|
|
369
|
+
const restored = mutate(spawn, "link", args);
|
|
370
|
+
if (!restored.ok) return `The previous registration was not restored: ${restored.error}`;
|
|
371
|
+
const verified = inspectHerdr(spawn, false);
|
|
64
372
|
if (
|
|
65
|
-
|
|
66
|
-
plugin
|
|
67
|
-
|
|
68
|
-
|
|
373
|
+
verified.available &&
|
|
374
|
+
verified.plugin !== undefined &&
|
|
375
|
+
matchesExpected(verified.plugin, previous.package, previous.enabled) &&
|
|
376
|
+
verified.plugin.enabled === previous.enabled
|
|
69
377
|
) {
|
|
70
|
-
return
|
|
378
|
+
return "The previous registration was restored.";
|
|
71
379
|
}
|
|
380
|
+
return "Herdr did not verify the restored registration.";
|
|
381
|
+
} catch (error) {
|
|
382
|
+
return `The previous registration was not restored: ${bounded(errorMessage(error))}`;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function verifyOrExplain(
|
|
387
|
+
spawn: Spawn,
|
|
388
|
+
expected: PluginPackage,
|
|
389
|
+
): { plugin?: InstalledPlugin; problem: string } {
|
|
390
|
+
try {
|
|
391
|
+
const inspected = inspectHerdr(spawn, false);
|
|
392
|
+
if (!inspected.available) return { problem: "Herdr became unavailable." };
|
|
393
|
+
if (inspected.plugin === undefined) return { problem: "Herdr reports no plugin registration." };
|
|
394
|
+
if (matchesExpected(inspected.plugin, expected, true)) {
|
|
395
|
+
return { plugin: inspected.plugin, problem: "" };
|
|
396
|
+
}
|
|
397
|
+
return {
|
|
398
|
+
plugin: inspected.plugin,
|
|
399
|
+
problem: "Herdr did not report the expected healthy registration.",
|
|
400
|
+
};
|
|
401
|
+
} catch (error) {
|
|
402
|
+
return { problem: errorMessage(error) };
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function mutate(spawn: Spawn, action: string, args: readonly string[]): MutationResult {
|
|
407
|
+
const changed = spawn("herdr", ["plugin", action, ...args]);
|
|
408
|
+
if (changed.error !== undefined) return { ok: false, error: changed.error.message };
|
|
409
|
+
if (changed.status !== 0) {
|
|
410
|
+
return { ok: false, error: bounded(changed.stderr || changed.stdout) };
|
|
72
411
|
}
|
|
73
|
-
return
|
|
412
|
+
return { ok: true };
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function mutationError(action: string, mutation: MutationResult, verification: string): Error {
|
|
416
|
+
const command = mutation.ok ? "The command completed" : `The command failed: ${mutation.error}`;
|
|
417
|
+
return new Error(`Could not ${action} the Herdr plugin. ${command}. ${verification}`);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function result(
|
|
421
|
+
status: HerdrSyncStatus,
|
|
422
|
+
changed: boolean,
|
|
423
|
+
expectedVersion: string,
|
|
424
|
+
effective: InstalledPlugin | undefined,
|
|
425
|
+
): HerdrSyncResult {
|
|
426
|
+
const messages: Record<HerdrSyncStatus, string> = {
|
|
427
|
+
linked: `Linked Herdr plugin ${HERDR_PLUGIN_ID}.`,
|
|
428
|
+
relinked: `Repaired Herdr plugin ${HERDR_PLUGIN_ID}.`,
|
|
429
|
+
enabled: `Enabled Herdr plugin ${HERDR_PLUGIN_ID}.`,
|
|
430
|
+
unchanged: `Herdr plugin ${HERDR_PLUGIN_ID} is current.`,
|
|
431
|
+
unavailable: "Herdr is not installed; plugin synchronization was skipped.",
|
|
432
|
+
};
|
|
433
|
+
return {
|
|
434
|
+
schema: RESULT_SCHEMA,
|
|
435
|
+
status,
|
|
436
|
+
changed,
|
|
437
|
+
pluginId: HERDR_PLUGIN_ID,
|
|
438
|
+
expectedVersion,
|
|
439
|
+
effectiveVersion: effective?.version ?? null,
|
|
440
|
+
enabled: effective?.enabled ?? null,
|
|
441
|
+
runningPiProcessesNeedReload: true,
|
|
442
|
+
message: messages[status],
|
|
443
|
+
};
|
|
74
444
|
}
|
|
75
445
|
|
|
76
446
|
function runCommand(command: string, args: readonly string[]): SpawnSyncReturns<string> {
|
|
77
447
|
return spawnSync(command, [...args], {
|
|
78
448
|
encoding: "utf8",
|
|
79
449
|
stdio: ["ignore", "pipe", "pipe"],
|
|
450
|
+
maxBuffer: MAX_COMMAND_OUTPUT_BYTES,
|
|
451
|
+
shell: false,
|
|
80
452
|
});
|
|
81
453
|
}
|
|
82
454
|
|
|
455
|
+
function errorCode(error: Error): string | undefined {
|
|
456
|
+
const value = error as Error & { code?: unknown };
|
|
457
|
+
return typeof value.code === "string" ? value.code : undefined;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function normalizedPath(value: string): string {
|
|
461
|
+
const resolved = path.resolve(value);
|
|
462
|
+
try {
|
|
463
|
+
return fs.realpathSync(resolved);
|
|
464
|
+
} catch {
|
|
465
|
+
return resolved;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
83
469
|
function bounded(value: string): string {
|
|
84
470
|
const compact = value
|
|
85
471
|
.replace(/[\r\n\t]+/gu, " ")
|
|
@@ -88,6 +474,10 @@ function bounded(value: string): string {
|
|
|
88
474
|
return compact.length <= 300 ? compact : `${compact.slice(0, 299)}…`;
|
|
89
475
|
}
|
|
90
476
|
|
|
477
|
+
function errorMessage(error: unknown): string {
|
|
478
|
+
return error instanceof Error ? error.message : String(error);
|
|
479
|
+
}
|
|
480
|
+
|
|
91
481
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
92
482
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
93
483
|
}
|