@blackbelt-technology/pi-dashboard-flows-plugin 0.5.3 → 0.6.0
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/package.json +48 -4
- package/src/AGENTS.md +8 -0
- package/src/__tests__/FlowGraph.test.ts +151 -5
- package/src/__tests__/FlowInputWiring.test.tsx +97 -0
- package/src/__tests__/FlowSummary.test.tsx +331 -0
- package/src/__tests__/FlowYamlPreview.test.tsx +75 -0
- package/src/__tests__/FlowsSessionStateContext.test.tsx +223 -0
- package/src/__tests__/FlowsUiStateContext.test.tsx +140 -0
- package/src/__tests__/authoring-renderers.test.tsx +190 -0
- package/src/__tests__/automation-actions.test.ts +117 -0
- package/src/__tests__/flow-agent-card-code-source.test.tsx +154 -0
- package/src/__tests__/flow-agent-cost.test.tsx +160 -0
- package/src/__tests__/flow-agent-error.test.ts +60 -0
- package/src/__tests__/flow-collapse-storage.test.ts +90 -0
- package/src/__tests__/flow-edges.test.ts +195 -0
- package/src/__tests__/flow-inputs.test.ts +69 -0
- package/src/__tests__/flow-reducer-bridge-contract.test.ts +108 -0
- package/src/__tests__/flow-reducer-seed-on-start.test.ts +61 -0
- package/src/__tests__/flow-yaml-parse.test.ts +124 -0
- package/src/__tests__/flows-for-cwd.test.ts +51 -0
- package/src/__tests__/flowsAvailability.test.ts +79 -0
- package/src/__tests__/manifest-discoverability.test.ts +97 -0
- package/src/__tests__/node-kind-reducer.test.ts +135 -0
- package/src/__tests__/plugin-action-handler.test.ts +54 -0
- package/src/__tests__/render-actions.test.ts +63 -0
- package/src/__tests__/session-flow-actions-edit.test.tsx +98 -0
- package/src/bridge/AGENTS.md +8 -0
- package/src/bridge/flow-question-adapter.ts +93 -0
- package/src/bridge/index.ts +30 -0
- package/src/client/AGENTS.md +32 -0
- package/src/client/FlowActivityBadge.tsx +41 -11
- package/src/client/FlowAgentCard.tsx +321 -61
- package/src/client/FlowAgentDetail.tsx +103 -154
- package/src/client/FlowAgentsToolRenderer.tsx +228 -0
- package/src/client/FlowAuthorPromptDialog.tsx +92 -0
- package/src/client/FlowDashboard.tsx +271 -43
- package/src/client/FlowGraph.tsx +425 -276
- package/src/client/FlowInputWiring.tsx +243 -0
- package/src/client/FlowLaunchDialog.tsx +52 -52
- package/src/client/FlowQuestionCard.tsx +216 -0
- package/src/client/FlowQuestionTranscriptPill.tsx +58 -0
- package/src/client/FlowSummary.tsx +285 -80
- package/src/client/FlowWriteToolRenderer.tsx +127 -0
- package/src/client/FlowYamlPopoverButton.tsx +110 -0
- package/src/client/FlowYamlPreview.tsx +67 -0
- package/src/client/FlowsCommandRoutes.tsx +237 -0
- package/src/client/FlowsSessionStateContext.tsx +142 -0
- package/src/client/FlowsSettings.tsx +68 -0
- package/src/client/FlowsUiStateContext.tsx +169 -0
- package/src/client/SessionFlowActions.tsx +172 -65
- package/src/client/flow-collapse-storage.ts +79 -0
- package/src/client/flow-edges.ts +173 -0
- package/src/client/flow-yaml-parse.ts +140 -0
- package/src/client/flowsAvailability.ts +44 -0
- package/src/client/index.tsx +95 -13
- package/src/client/shouldRender.ts +38 -0
- package/src/configSchema.json +14 -0
- package/src/flow-reducer.ts +94 -13
- package/src/i18n.ts +90 -0
- package/src/reducer.ts +5 -5
- package/src/server/AGENTS.md +10 -0
- package/src/server/automation-actions.ts +162 -0
- package/src/server/flow-inputs.ts +58 -0
- package/src/server/index.ts +171 -0
- package/src/server/render-actions.ts +88 -0
- package/src/server/state-store.ts +74 -0
- package/src/__tests__/ArchitectInputPrompt.test.tsx +0 -81
- package/src/__tests__/architect-reducer.test.ts +0 -307
- package/src/architect-reducer.ts +0 -386
- package/src/client/FlowArchitect.tsx +0 -622
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-agent USD cost surfacing (change surface-flow-agent-cost).
|
|
3
|
+
*
|
|
4
|
+
* Pins: reducer stores `result.cost` verbatim onto `FlowAgentState.cost`
|
|
5
|
+
* (undefined when absent), the `formatCost` precision rule (>= 1 → 2dp,
|
|
6
|
+
* < 1 → 4dp), the card stats-line `$` segment (shown when > 0, suppressed
|
|
7
|
+
* when 0/undefined), and the detail header cost value.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
createUiPrimitiveRegistry,
|
|
12
|
+
registerUiPrimitive,
|
|
13
|
+
UiPrimitiveProvider,
|
|
14
|
+
} from "@blackbelt-technology/dashboard-plugin-runtime";
|
|
15
|
+
import { withUiPrimitiveProvider } from "@blackbelt-technology/dashboard-plugin-runtime/test-support";
|
|
16
|
+
import { UI_PRIMITIVE_KEYS } from "@blackbelt-technology/pi-dashboard-shared/dashboard-plugin/ui-primitives.js";
|
|
17
|
+
import type {
|
|
18
|
+
DashboardEvent,
|
|
19
|
+
FlowAgentState,
|
|
20
|
+
FlowState,
|
|
21
|
+
} from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
22
|
+
import { cleanup, render } from "@testing-library/react";
|
|
23
|
+
import type React from "react";
|
|
24
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
25
|
+
import { FlowAgentCard, formatCost } from "../client/FlowAgentCard.js";
|
|
26
|
+
import { FlowAgentDetail } from "../client/FlowAgentDetail.js";
|
|
27
|
+
import { reduceFlowEvent } from "../reducer.js";
|
|
28
|
+
|
|
29
|
+
function ev(type: string, data: Record<string, unknown>): DashboardEvent {
|
|
30
|
+
return { seq: 1, timestamp: 0, sessionId: "s1", eventType: type, data } as unknown as DashboardEvent;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function fold(events: Array<[string, Record<string, unknown>]>): FlowState | null {
|
|
34
|
+
let s: FlowState | null = null;
|
|
35
|
+
for (const [t, d] of events) s = reduceFlowEvent(s, ev(t, d));
|
|
36
|
+
return s;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const started: Array<[string, Record<string, unknown>]> = [
|
|
40
|
+
["flow_started", {
|
|
41
|
+
flowName: "f", task: "t",
|
|
42
|
+
steps: [{ id: "a", stepType: "agent", agent: "a", blockedBy: [] }],
|
|
43
|
+
}],
|
|
44
|
+
["flow_agent_started", { agentName: "a", stepId: "a", nodeKind: "agent" }],
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
describe("cost reducer (task 1.1, 1.2)", () => {
|
|
48
|
+
it("stores result.cost verbatim onto FlowAgentState.cost", () => {
|
|
49
|
+
const s = fold([
|
|
50
|
+
...started,
|
|
51
|
+
["flow_agent_complete", { agentName: "a", stepId: "a", result: { success: true, cost: 0.0142 } }],
|
|
52
|
+
])!;
|
|
53
|
+
expect(s.agents.get("a")?.cost).toBe(0.0142);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("leaves cost undefined when the completion event omits it", () => {
|
|
57
|
+
const s = fold([
|
|
58
|
+
...started,
|
|
59
|
+
["flow_agent_complete", { agentName: "a", stepId: "a", result: { success: true } }],
|
|
60
|
+
])!;
|
|
61
|
+
expect(s.agents.get("a")?.cost).toBeUndefined();
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("formatCost helper (task 1.3)", () => {
|
|
66
|
+
it("uses two decimals at or above $1", () => {
|
|
67
|
+
expect(formatCost(1.2)).toBe("$1.20");
|
|
68
|
+
expect(formatCost(12)).toBe("$12.00");
|
|
69
|
+
expect(formatCost(1)).toBe("$1.00"); // exact >= 1 boundary
|
|
70
|
+
});
|
|
71
|
+
it("uses four decimals below $1", () => {
|
|
72
|
+
expect(formatCost(0.0142)).toBe("$0.0142");
|
|
73
|
+
expect(formatCost(0.5)).toBe("$0.5000");
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// --- Card render (task 1.4) ---
|
|
78
|
+
const cardRegistry = createUiPrimitiveRegistry();
|
|
79
|
+
registerUiPrimitive(
|
|
80
|
+
cardRegistry,
|
|
81
|
+
UI_PRIMITIVE_KEYS.agentCard,
|
|
82
|
+
(({ children, stats }: { children: React.ReactNode; stats?: React.ReactNode }) => (
|
|
83
|
+
<div data-testid="card"><div data-testid="stats">{stats}</div>{children}</div>
|
|
84
|
+
)) as never,
|
|
85
|
+
);
|
|
86
|
+
registerUiPrimitive(cardRegistry, UI_PRIMITIVE_KEYS.formatTokens, ((n: number) => String(n)) as never);
|
|
87
|
+
registerUiPrimitive(cardRegistry, UI_PRIMITIVE_KEYS.formatDuration, ((n: number) => `${n}ms`) as never);
|
|
88
|
+
registerUiPrimitive(cardRegistry, UI_PRIMITIVE_KEYS.dialog, (() => null) as never);
|
|
89
|
+
registerUiPrimitive(cardRegistry, UI_PRIMITIVE_KEYS.markdownContent, (() => null) as never);
|
|
90
|
+
|
|
91
|
+
function makeAgent(over: Partial<FlowAgentState>): FlowAgentState {
|
|
92
|
+
return {
|
|
93
|
+
agentName: "a", stepId: "a", status: "complete", blockedBy: [],
|
|
94
|
+
recentTools: [], detailHistory: [],
|
|
95
|
+
tokens: { input: 12000, output: 3000 }, duration: 4200,
|
|
96
|
+
...over,
|
|
97
|
+
} as FlowAgentState;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function renderCard(agent: FlowAgentState) {
|
|
101
|
+
return render(
|
|
102
|
+
<UiPrimitiveProvider value={cardRegistry}>
|
|
103
|
+
<FlowAgentCard agent={agent} />
|
|
104
|
+
</UiPrimitiveProvider>,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
describe("FlowAgentCard cost segment (task 1.4)", () => {
|
|
109
|
+
afterEach(cleanup);
|
|
110
|
+
|
|
111
|
+
it("shows a $ segment when cost > 0", () => {
|
|
112
|
+
const { getByTestId } = renderCard(makeAgent({ cost: 0.0142 }));
|
|
113
|
+
expect(getByTestId("stats").textContent).toContain("$0.0142");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("omits the segment when cost is 0 (no $, no dangling separator)", () => {
|
|
117
|
+
const { getByTestId } = renderCard(makeAgent({ cost: 0 }));
|
|
118
|
+
const text = getByTestId("stats").textContent ?? "";
|
|
119
|
+
expect(text).not.toContain("$");
|
|
120
|
+
expect(text).toContain("12000");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("omits the segment when cost is undefined", () => {
|
|
124
|
+
const { getByTestId } = renderCard(makeAgent({ cost: undefined }));
|
|
125
|
+
expect(getByTestId("stats").textContent ?? "").not.toContain("$");
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// --- Detail header (task 1.5) ---
|
|
130
|
+
function renderDetail(agent: FlowAgentState) {
|
|
131
|
+
return render(
|
|
132
|
+
withUiPrimitiveProvider(
|
|
133
|
+
{
|
|
134
|
+
"ui:markdown-content": (({ content }: { content: string }) => <div>{content}</div>) as never,
|
|
135
|
+
"ui:format-tokens": ((n: number) => String(n)) as never,
|
|
136
|
+
"ui:format-duration": ((n: number) => `${n}ms`) as never,
|
|
137
|
+
},
|
|
138
|
+
<FlowAgentDetail agent={agent} />,
|
|
139
|
+
),
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
describe("FlowAgentDetail cost in header (task 1.5)", () => {
|
|
144
|
+
afterEach(cleanup);
|
|
145
|
+
|
|
146
|
+
it("shows cost when present and > 0", () => {
|
|
147
|
+
const { container } = renderDetail(makeAgent({ cost: 0.0142 }));
|
|
148
|
+
expect(container.textContent).toContain("$0.0142");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("omits cost when 0", () => {
|
|
152
|
+
const { container } = renderDetail(makeAgent({ cost: 0 }));
|
|
153
|
+
expect(container.textContent).not.toContain("$");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("omits cost when absent", () => {
|
|
157
|
+
const { container } = renderDetail(makeAgent({ cost: undefined }));
|
|
158
|
+
expect(container.textContent).not.toContain("$");
|
|
159
|
+
});
|
|
160
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* flow_agent_error reducer case (change: replay-persisted-flow-runs).
|
|
3
|
+
* pi-flows now emits flow:agent-error { agentName, stepId, text } for
|
|
4
|
+
* step-level agent failures. The reducer appends { kind:"error", text } to
|
|
5
|
+
* the agent's detailHistory. The error variant of FlowDetailEntry already
|
|
6
|
+
* exists; this pins its producer case. Status stays owned by
|
|
7
|
+
* flow_agent_complete — the error case does not change it.
|
|
8
|
+
*/
|
|
9
|
+
import { describe, it, expect } from "vitest";
|
|
10
|
+
import { reduceFlowEvent } from "../reducer.js";
|
|
11
|
+
import type { DashboardEvent, FlowState } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
12
|
+
|
|
13
|
+
function ev(type: string, data: Record<string, unknown>): DashboardEvent {
|
|
14
|
+
return { seq: 1, timestamp: 0, sessionId: "s1", eventType: type, data } as unknown as DashboardEvent;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function startedWithAgent(): FlowState {
|
|
18
|
+
const s = reduceFlowEvent(
|
|
19
|
+
null,
|
|
20
|
+
ev("flow_started", { flowName: "f", task: "t", steps: [{ id: "research", agent: "researcher", blockedBy: [] }] }),
|
|
21
|
+
);
|
|
22
|
+
return reduceFlowEvent(s, ev("flow_agent_started", { agentName: "researcher", stepId: "research" }))!;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("reduceFlowEvent — flow_agent_error", () => {
|
|
26
|
+
it("appends an { kind: 'error', text } entry to the agent detailHistory", () => {
|
|
27
|
+
const s = startedWithAgent();
|
|
28
|
+
const next = reduceFlowEvent(
|
|
29
|
+
s,
|
|
30
|
+
ev("flow_agent_error", { agentName: "researcher", stepId: "research", text: "tool quota exceeded" }),
|
|
31
|
+
);
|
|
32
|
+
const agent = next!.agents.get("research")!;
|
|
33
|
+
const last = agent.detailHistory[agent.detailHistory.length - 1];
|
|
34
|
+
expect(last).toEqual({ kind: "error", text: "tool quota exceeded" });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("ignores events with empty text (no-op)", () => {
|
|
38
|
+
const s = startedWithAgent();
|
|
39
|
+
const before = s.agents.get("research")!.detailHistory.length;
|
|
40
|
+
const next = reduceFlowEvent(s, ev("flow_agent_error", { agentName: "researcher", stepId: "research", text: "" }));
|
|
41
|
+
expect(next!.agents.get("research")!.detailHistory.length).toBe(before);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("does not change the agent status (status owned by flow_agent_complete)", () => {
|
|
45
|
+
const s = startedWithAgent();
|
|
46
|
+
const statusBefore = s.agents.get("research")!.status;
|
|
47
|
+
const next = reduceFlowEvent(
|
|
48
|
+
s,
|
|
49
|
+
ev("flow_agent_error", { agentName: "researcher", stepId: "research", text: "boom" }),
|
|
50
|
+
);
|
|
51
|
+
expect(next!.agents.get("research")!.status).toBe(statusBefore);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("locates the agent by agentName when stepId is absent", () => {
|
|
55
|
+
const s = startedWithAgent();
|
|
56
|
+
const next = reduceFlowEvent(s, ev("flow_agent_error", { agentName: "researcher", text: "boom" }));
|
|
57
|
+
const agent = next!.agents.get("research")!;
|
|
58
|
+
expect(agent.detailHistory.some((e) => e.kind === "error" && e.text === "boom")).toBe(true);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-session collapse persistence. See change: fix-flow-ui-graph-zoom-summary.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { act, renderHook } from "@testing-library/react";
|
|
6
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
7
|
+
import { FLOW_SHOW_ERROR_ROUTES_KEY, flowCollapseKey, useFlowCollapsePersisted, usePersistedToggle } from "../client/flow-collapse-storage.js";
|
|
8
|
+
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
localStorage.clear();
|
|
11
|
+
vi.restoreAllMocks();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe("useFlowCollapsePersisted", () => {
|
|
15
|
+
it("defaults to expanded (false) for an untouched session", () => {
|
|
16
|
+
const { result } = renderHook(() => useFlowCollapsePersisted("sess-A", "summary"));
|
|
17
|
+
expect(result.current[0]).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("persists the collapse across remounts for the same session", () => {
|
|
21
|
+
const first = renderHook(() => useFlowCollapsePersisted("sess-A", "summary"));
|
|
22
|
+
act(() => first.result.current[1]()); // toggle → collapsed
|
|
23
|
+
expect(first.result.current[0]).toBe(true);
|
|
24
|
+
expect(localStorage.getItem(flowCollapseKey("summary", "sess-A"))).toBe("true");
|
|
25
|
+
|
|
26
|
+
// Fresh mount (same session) reads the persisted value.
|
|
27
|
+
const second = renderHook(() => useFlowCollapsePersisted("sess-A", "summary"));
|
|
28
|
+
expect(second.result.current[0]).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("isolates state per session id", () => {
|
|
32
|
+
const a = renderHook(() => useFlowCollapsePersisted("sess-A", "summary"));
|
|
33
|
+
act(() => a.result.current[1]()); // A collapsed
|
|
34
|
+
const b = renderHook(() => useFlowCollapsePersisted("sess-B", "summary"));
|
|
35
|
+
expect(b.result.current[0]).toBe(false); // B untouched → expanded
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("isolates state per kind for the same session", () => {
|
|
39
|
+
const summary = renderHook(() => useFlowCollapsePersisted("sess-A", "summary"));
|
|
40
|
+
act(() => summary.result.current[1]());
|
|
41
|
+
const dashboard = renderHook(() => useFlowCollapsePersisted("sess-A", "dashboard"));
|
|
42
|
+
expect(dashboard.result.current[0]).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("degrades to in-memory when localStorage.setItem throws", () => {
|
|
46
|
+
vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => {
|
|
47
|
+
throw new Error("quota");
|
|
48
|
+
});
|
|
49
|
+
const { result } = renderHook(() => useFlowCollapsePersisted("sess-A", "summary"));
|
|
50
|
+
expect(() => act(() => result.current[1]())).not.toThrow();
|
|
51
|
+
expect(result.current[0]).toBe(true); // toggle still works in-memory
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("is purely in-memory when sessionId is undefined", () => {
|
|
55
|
+
const setSpy = vi.spyOn(Storage.prototype, "setItem");
|
|
56
|
+
const { result } = renderHook(() => useFlowCollapsePersisted(undefined, "summary"));
|
|
57
|
+
act(() => result.current[1]());
|
|
58
|
+
expect(result.current[0]).toBe(true);
|
|
59
|
+
expect(setSpy).not.toHaveBeenCalled();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("usePersistedToggle (global error-route visibility)", () => {
|
|
64
|
+
it("defaults to the fallback when unset", () => {
|
|
65
|
+
const { result } = renderHook(() => usePersistedToggle(FLOW_SHOW_ERROR_ROUTES_KEY, false));
|
|
66
|
+
expect(result.current[0]).toBe(false);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("persists on/off across remounts (global key, no session)", () => {
|
|
70
|
+
const first = renderHook(() => usePersistedToggle(FLOW_SHOW_ERROR_ROUTES_KEY, false));
|
|
71
|
+
act(() => first.result.current[1]()); // on
|
|
72
|
+
expect(first.result.current[0]).toBe(true);
|
|
73
|
+
expect(localStorage.getItem(FLOW_SHOW_ERROR_ROUTES_KEY)).toBe("true");
|
|
74
|
+
|
|
75
|
+
const second = renderHook(() => usePersistedToggle(FLOW_SHOW_ERROR_ROUTES_KEY, false));
|
|
76
|
+
expect(second.result.current[0]).toBe(true); // remount reads persisted ON
|
|
77
|
+
|
|
78
|
+
act(() => second.result.current[1]()); // off
|
|
79
|
+
expect(localStorage.getItem(FLOW_SHOW_ERROR_ROUTES_KEY)).toBe("false");
|
|
80
|
+
const third = renderHook(() => usePersistedToggle(FLOW_SHOW_ERROR_ROUTES_KEY, false));
|
|
81
|
+
expect(third.result.current[0]).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("degrades to in-memory when localStorage throws", () => {
|
|
85
|
+
vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => { throw new Error("quota"); });
|
|
86
|
+
const { result } = renderHook(() => usePersistedToggle(FLOW_SHOW_ERROR_ROUTES_KEY, false));
|
|
87
|
+
expect(() => act(() => result.current[1]())).not.toThrow();
|
|
88
|
+
expect(result.current[0]).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { deriveFlowEdges, type FlowEdgeStep } from "../client/flow-edges.js";
|
|
3
|
+
|
|
4
|
+
const key = (e: { from: string; to: string }) => `${e.from}->${e.to}`;
|
|
5
|
+
|
|
6
|
+
describe("deriveFlowEdges", () => {
|
|
7
|
+
it("derives the four edge classes", () => {
|
|
8
|
+
const steps: FlowEdgeStep[] = [
|
|
9
|
+
{ id: "a", type: "agent", blockedBy: [] },
|
|
10
|
+
{ id: "b", type: "code", blockedBy: ["a"], onComplete: "d" }, // sequential a->b, route b->d
|
|
11
|
+
{ id: "d", type: "code-decision", blockedBy: ["b"], branches: { again: "b" } }, // branch d->b (back)
|
|
12
|
+
{ id: "sep", type: "fork", blockedBy: ["d"] },
|
|
13
|
+
{ id: "e", type: "agent", blockedBy: [] }, // implicit sep->e (no other incoming)
|
|
14
|
+
];
|
|
15
|
+
const edges = deriveFlowEdges(steps);
|
|
16
|
+
const byKey = new Map(edges.map((e) => [key(e), e]));
|
|
17
|
+
|
|
18
|
+
expect(byKey.get("a->b")?.kind).toBe("sequential");
|
|
19
|
+
expect(byKey.get("d->b")?.kind).toBe("branch");
|
|
20
|
+
expect(byKey.get("d->b")?.label).toBe("again");
|
|
21
|
+
expect(byKey.get("sep->e")?.kind).toBe("implicit");
|
|
22
|
+
expect(byKey.get("b->d")?.kind).toBe("route");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("does not serialize parallel siblings (no separator predecessor)", () => {
|
|
26
|
+
// Three roots with no blockedBy and no separator between them: the engine
|
|
27
|
+
// runs all three in wave 1 (parallel). No implicit chain should be drawn.
|
|
28
|
+
const steps: FlowEdgeStep[] = [
|
|
29
|
+
{ id: "a", type: "agent", blockedBy: [] },
|
|
30
|
+
{ id: "impl", type: "agent", blockedBy: [] },
|
|
31
|
+
{ id: "docs", type: "agent", blockedBy: [] },
|
|
32
|
+
];
|
|
33
|
+
const edges = deriveFlowEdges(steps);
|
|
34
|
+
expect(edges).toHaveLength(0);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("keeps the implicit edge across a separator boundary", () => {
|
|
38
|
+
// A step with no blockedBy immediately after a separator falls through from it.
|
|
39
|
+
const steps: FlowEdgeStep[] = [
|
|
40
|
+
{ id: "decide", type: "code-decision", blockedBy: [] },
|
|
41
|
+
{ id: "next", type: "agent", blockedBy: [] },
|
|
42
|
+
];
|
|
43
|
+
const edges = deriveFlowEdges(steps);
|
|
44
|
+
expect(edges.find((e) => key(e) === "decide->next")?.kind).toBe("implicit");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("fans all no-blockedBy roots out from the preceding separator (parallel)", () => {
|
|
48
|
+
// Two parallel roots after one separator: BOTH fall through from the
|
|
49
|
+
// separator (parallel), not chained b->c.
|
|
50
|
+
const steps: FlowEdgeStep[] = [
|
|
51
|
+
{ id: "sep", type: "fork", blockedBy: [] },
|
|
52
|
+
{ id: "b", type: "agent", blockedBy: [] },
|
|
53
|
+
{ id: "c", type: "agent", blockedBy: [] },
|
|
54
|
+
];
|
|
55
|
+
const edges = deriveFlowEdges(steps);
|
|
56
|
+
expect(edges.find((e) => key(e) === "sep->b")?.kind).toBe("implicit");
|
|
57
|
+
expect(edges.find((e) => key(e) === "sep->c")?.kind).toBe("implicit");
|
|
58
|
+
expect(edges.some((e) => key(e) === "b->c")).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("flags backward edges", () => {
|
|
62
|
+
const steps: FlowEdgeStep[] = [
|
|
63
|
+
{ id: "work", type: "agent", blockedBy: [] },
|
|
64
|
+
{ id: "gate", type: "code-decision", blockedBy: ["work"], branches: { again: "work", go: "done" } },
|
|
65
|
+
{ id: "done", type: "agent", blockedBy: [] },
|
|
66
|
+
];
|
|
67
|
+
const edges = deriveFlowEdges(steps);
|
|
68
|
+
const back = edges.find((e) => key(e) === "gate->work")!;
|
|
69
|
+
const fwd = edges.find((e) => key(e) === "gate->done")!;
|
|
70
|
+
expect(back.backward).toBe(true);
|
|
71
|
+
expect(fwd.backward).toBe(false);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("collapses duplicate {from,to}, preferring labeled branch", () => {
|
|
75
|
+
const steps: FlowEdgeStep[] = [
|
|
76
|
+
{ id: "a", type: "fork", blockedBy: [], branches: { x: "b" } },
|
|
77
|
+
{ id: "b", type: "agent", blockedBy: ["a"] }, // sequential a->b AND branch a->b
|
|
78
|
+
];
|
|
79
|
+
const edges = deriveFlowEdges(steps);
|
|
80
|
+
const ab = edges.filter((e) => key(e) === "a->b");
|
|
81
|
+
expect(ab).toHaveLength(1);
|
|
82
|
+
expect(ab[0].kind).toBe("branch");
|
|
83
|
+
expect(ab[0].label).toBe("x");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("emits route edges only when onComplete/onError present", () => {
|
|
87
|
+
const withRoute = deriveFlowEdges([
|
|
88
|
+
{ id: "a", type: "code", blockedBy: [], onComplete: "b" },
|
|
89
|
+
{ id: "b", type: "agent", blockedBy: [] },
|
|
90
|
+
]);
|
|
91
|
+
expect(withRoute.find((e) => key(e) === "a->b")?.kind).toBe("route");
|
|
92
|
+
|
|
93
|
+
const liveNoRoute = deriveFlowEdges([
|
|
94
|
+
{ id: "a", type: "code", blockedBy: [] },
|
|
95
|
+
{ id: "b", type: "agent", blockedBy: [] },
|
|
96
|
+
]);
|
|
97
|
+
expect(liveNoRoute.some((e) => e.kind === "route")).toBe(false);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("classifies an on_error route as returning when the handler rejoins the flow", () => {
|
|
101
|
+
const steps: FlowEdgeStep[] = [
|
|
102
|
+
{ id: "validate", type: "code-decision", blockedBy: [], onError: "fixup" },
|
|
103
|
+
{ id: "transform", type: "agent", blockedBy: ["validate"] },
|
|
104
|
+
{ id: "fixup", type: "agent", blockedBy: [], onComplete: "validate" }, // rejoins the spine
|
|
105
|
+
];
|
|
106
|
+
const r = deriveFlowEdges(steps).find((e) => key(e) === "validate->fixup")!;
|
|
107
|
+
expect(r.kind).toBe("route");
|
|
108
|
+
expect(r.label).toBe("on_error");
|
|
109
|
+
expect(r.routeTopology).toBe("returning");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("classifies an on_error route as terminal when the handler never rejoins", () => {
|
|
113
|
+
const steps: FlowEdgeStep[] = [
|
|
114
|
+
{ id: "finalize", type: "agent", blockedBy: [], onError: "notify" },
|
|
115
|
+
{ id: "notify", type: "agent", blockedBy: [] }, // sink: no forward edge back to the flow
|
|
116
|
+
];
|
|
117
|
+
const r = deriveFlowEdges(steps).find((e) => key(e) === "finalize->notify")!;
|
|
118
|
+
expect(r.kind).toBe("route");
|
|
119
|
+
expect(r.routeTopology).toBe("terminal");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("leaves routeTopology unset on non-on_error routes", () => {
|
|
123
|
+
const edges = deriveFlowEdges([
|
|
124
|
+
{ id: "a", type: "code", blockedBy: [], onComplete: "b" },
|
|
125
|
+
{ id: "b", type: "agent", blockedBy: [] },
|
|
126
|
+
]);
|
|
127
|
+
const oc = edges.find((e) => key(e) === "a->b")!;
|
|
128
|
+
expect(oc.kind).toBe("route");
|
|
129
|
+
expect(oc.routeTopology).toBeUndefined();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("skips edges with missing endpoints", () => {
|
|
133
|
+
const edges = deriveFlowEdges([
|
|
134
|
+
{ id: "a", type: "agent", blockedBy: ["ghost"], branches: { x: "nowhere" } },
|
|
135
|
+
]);
|
|
136
|
+
expect(edges).toHaveLength(0);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Regression: a flow wired entirely via on_complete (no blockedBy), with a
|
|
140
|
+
// routing step declared BEFORE any separator — the InvoiceBot `process` shape.
|
|
141
|
+
// The pre-separator root must NOT be orphaned, and a routing target must not
|
|
142
|
+
// also get a spurious implicit edge from the nearest separator.
|
|
143
|
+
// See change: fix-flow-ui-graph-zoom-summary.
|
|
144
|
+
it("on_complete-routed pre-separator root is connected (no orphan)", () => {
|
|
145
|
+
const steps: FlowEdgeStep[] = [
|
|
146
|
+
{ id: "load-state", type: "code", blockedBy: [], onComplete: "resume-gate", onError: "hold" },
|
|
147
|
+
{ id: "resume-gate", type: "code-decision", blockedBy: [], branches: { new: "intake" } },
|
|
148
|
+
{ id: "intake", type: "code", blockedBy: [], onComplete: "hold" },
|
|
149
|
+
{ id: "hold", type: "code", blockedBy: [] },
|
|
150
|
+
];
|
|
151
|
+
const edges = deriveFlowEdges(steps);
|
|
152
|
+
const byKey = new Map(edges.map((e) => [key(e), e]));
|
|
153
|
+
// load-state routes forward (not orphaned) and to its error sink.
|
|
154
|
+
expect(byKey.get("load-state->resume-gate")?.kind).toBe("route");
|
|
155
|
+
expect(byKey.get("load-state->hold")?.kind).toBe("route");
|
|
156
|
+
// load-state has at least one outgoing edge.
|
|
157
|
+
expect(edges.some((e) => e.from === "load-state")).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("routing target suppresses a spurious implicit edge", () => {
|
|
161
|
+
// `intake` has no blockedBy but is the on_complete target of `gate`; it must
|
|
162
|
+
// get the route edge and NOT also an implicit edge from the separator.
|
|
163
|
+
const steps: FlowEdgeStep[] = [
|
|
164
|
+
{ id: "gate", type: "code-decision", blockedBy: [], onComplete: "intake" },
|
|
165
|
+
{ id: "intake", type: "code", blockedBy: [] },
|
|
166
|
+
];
|
|
167
|
+
const edges = deriveFlowEdges(steps);
|
|
168
|
+
const incoming = edges.filter((e) => e.to === "intake");
|
|
169
|
+
expect(incoming).toHaveLength(1);
|
|
170
|
+
expect(incoming[0].kind).toBe("route");
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("omitting routing fields produces no route edges (backward compat)", () => {
|
|
174
|
+
const steps: FlowEdgeStep[] = [
|
|
175
|
+
{ id: "a", type: "code", blockedBy: [] },
|
|
176
|
+
{ id: "b", type: "code", blockedBy: [] },
|
|
177
|
+
];
|
|
178
|
+
const edges = deriveFlowEdges(steps);
|
|
179
|
+
expect(edges.some((e) => e.kind === "route")).toBe(false);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("live (branches only) and static (branches + routes) agree on shared classes", () => {
|
|
183
|
+
const base: FlowEdgeStep[] = [
|
|
184
|
+
{ id: "a", type: "agent", blockedBy: [] },
|
|
185
|
+
{ id: "fork", type: "fork", blockedBy: ["a"], branches: { p: "b", q: "c" } },
|
|
186
|
+
{ id: "b", type: "agent", blockedBy: [] },
|
|
187
|
+
{ id: "c", type: "agent", blockedBy: [] },
|
|
188
|
+
];
|
|
189
|
+
const live = deriveFlowEdges(base);
|
|
190
|
+
const staticEdges = deriveFlowEdges(base.map((s) => ({ ...s, onError: s.id === "b" ? "c" : undefined })));
|
|
191
|
+
const sharedLive = live.filter((e) => e.kind !== "route").map(key).sort();
|
|
192
|
+
const sharedStatic = staticEdges.filter((e) => e.kind !== "route").map(key).sort();
|
|
193
|
+
expect(sharedLive).toEqual(sharedStatic);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read-only flow input-schema reader tests.
|
|
3
|
+
* See change: wire-flow-inputs-in-automation.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
10
|
+
import { flowYamlPath, readFlowInputs } from "../server/flow-inputs.js";
|
|
11
|
+
|
|
12
|
+
function writeFlow(cwd: string, ns: string, name: string, yaml: string): void {
|
|
13
|
+
const dir = path.join(cwd, ".pi", "flows", "flows", ns, name);
|
|
14
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
15
|
+
fs.writeFileSync(path.join(dir, "flow.yaml"), yaml);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("readFlowInputs", () => {
|
|
19
|
+
let cwd: string;
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
cwd = fs.mkdtempSync(path.join(os.tmpdir(), "flow-inputs-"));
|
|
22
|
+
});
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
fs.rmSync(cwd, { recursive: true, force: true });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("returns declared typed inputs", () => {
|
|
28
|
+
writeFlow(
|
|
29
|
+
cwd,
|
|
30
|
+
"invoicebot",
|
|
31
|
+
"process",
|
|
32
|
+
`name: invoicebot:process\ninputs:\n invoice: { type: string, required: true }\n priority: { type: number }\nsteps: []\n`,
|
|
33
|
+
);
|
|
34
|
+
expect(readFlowInputs(cwd, "invoicebot:process")).toEqual([
|
|
35
|
+
{ name: "invoice", type: "string", required: true },
|
|
36
|
+
{ name: "priority", type: "number", required: false },
|
|
37
|
+
]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("returns [] for a flow with no inputs", () => {
|
|
41
|
+
writeFlow(cwd, "x", "y", `name: x:y\nsteps: []\n`);
|
|
42
|
+
expect(readFlowInputs(cwd, "x:y")).toEqual([]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("returns [] for a missing flow / unparseable id", () => {
|
|
46
|
+
expect(readFlowInputs(cwd, "no:such")).toEqual([]);
|
|
47
|
+
expect(readFlowInputs(cwd, "malformed-id")).toEqual([]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("skips invalid input rows, keeps valid ones", () => {
|
|
51
|
+
writeFlow(
|
|
52
|
+
cwd,
|
|
53
|
+
"a",
|
|
54
|
+
"b",
|
|
55
|
+
`name: a:b\ninputs:\n good: { type: boolean }\n bad: { type: nonsense }\n alsoBad: "notamapping"\nsteps: []\n`,
|
|
56
|
+
);
|
|
57
|
+
expect(readFlowInputs(cwd, "a:b")).toEqual([{ name: "good", type: "boolean", required: false }]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("does not write any flow file (read-only)", () => {
|
|
61
|
+
writeFlow(cwd, "a", "b", `name: a:b\ninputs:\n f: { type: string }\nsteps: []\n`);
|
|
62
|
+
const p = flowYamlPath(cwd, "a:b")!;
|
|
63
|
+
const before = fs.statSync(p).mtimeMs;
|
|
64
|
+
readFlowInputs(cwd, "a:b");
|
|
65
|
+
expect(fs.statSync(p).mtimeMs).toBe(before);
|
|
66
|
+
// no stray files created under the flow dir
|
|
67
|
+
expect(fs.readdirSync(path.dirname(p))).toEqual(["flow.yaml"]);
|
|
68
|
+
});
|
|
69
|
+
});
|