stageflow 0.1.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.
Files changed (180) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -0
  3. package/dist/agent/activity.d.ts +76 -0
  4. package/dist/agent/activity.js +89 -0
  5. package/dist/agent/activityObserver.d.ts +17 -0
  6. package/dist/agent/activityObserver.js +66 -0
  7. package/dist/agent/cursorExtension.d.ts +12 -0
  8. package/dist/agent/cursorExtension.js +88 -0
  9. package/dist/agent/cursorProvider.d.ts +14 -0
  10. package/dist/agent/cursorProvider.js +139 -0
  11. package/dist/agent/fakeAgent.d.ts +29 -0
  12. package/dist/agent/fakeAgent.js +295 -0
  13. package/dist/agent/oauthSessionManager.d.ts +33 -0
  14. package/dist/agent/oauthSessionManager.js +214 -0
  15. package/dist/agent/piAdapter.d.ts +128 -0
  16. package/dist/agent/piAdapter.js +967 -0
  17. package/dist/agent/port.d.ts +63 -0
  18. package/dist/agent/port.js +46 -0
  19. package/dist/agent/providerAuth.d.ts +101 -0
  20. package/dist/agent/providerAuth.js +282 -0
  21. package/dist/agent/providerSupport.d.ts +28 -0
  22. package/dist/agent/providerSupport.js +19 -0
  23. package/dist/cli/providersCommand.d.ts +11 -0
  24. package/dist/cli/providersCommand.js +275 -0
  25. package/dist/cli/terminalAuthInteraction.d.ts +8 -0
  26. package/dist/cli/terminalAuthInteraction.js +65 -0
  27. package/dist/cli/terminalSecret.d.ts +2 -0
  28. package/dist/cli/terminalSecret.js +67 -0
  29. package/dist/cli/validateCommand.d.ts +11 -0
  30. package/dist/cli/validateCommand.js +81 -0
  31. package/dist/cli/validateOutput.d.ts +8 -0
  32. package/dist/cli/validateOutput.js +75 -0
  33. package/dist/cli.d.ts +2 -0
  34. package/dist/cli.js +273 -0
  35. package/dist/config/createPipeline.d.ts +31 -0
  36. package/dist/config/createPipeline.js +255 -0
  37. package/dist/config/createStage.d.ts +25 -0
  38. package/dist/config/createStage.js +181 -0
  39. package/dist/config/listConfig.d.ts +33 -0
  40. package/dist/config/listConfig.js +195 -0
  41. package/dist/config/listExtensions.d.ts +26 -0
  42. package/dist/config/listExtensions.js +50 -0
  43. package/dist/config/listSkills.d.ts +29 -0
  44. package/dist/config/listSkills.js +55 -0
  45. package/dist/config/loadOutcome.d.ts +27 -0
  46. package/dist/config/loadOutcome.js +6 -0
  47. package/dist/config/loadPipeline.d.ts +25 -0
  48. package/dist/config/loadPipeline.js +196 -0
  49. package/dist/config/loadStage.d.ts +4 -0
  50. package/dist/config/loadStage.js +127 -0
  51. package/dist/config/loadTask.d.ts +7 -0
  52. package/dist/config/loadTask.js +69 -0
  53. package/dist/config/readYamlObject.d.ts +1 -0
  54. package/dist/config/readYamlObject.js +5 -0
  55. package/dist/config/resolvePipelineDag.d.ts +12 -0
  56. package/dist/config/resolvePipelineDag.js +282 -0
  57. package/dist/config/validateCatalog.d.ts +36 -0
  58. package/dist/config/validateCatalog.js +322 -0
  59. package/dist/envelope/check.d.ts +3 -0
  60. package/dist/envelope/check.js +52 -0
  61. package/dist/envelope/payloadSchema.d.ts +10 -0
  62. package/dist/envelope/payloadSchema.js +91 -0
  63. package/dist/hitl/qaTrail.d.ts +31 -0
  64. package/dist/hitl/qaTrail.js +65 -0
  65. package/dist/index.d.ts +13 -0
  66. package/dist/index.js +7 -0
  67. package/dist/mcp/projectRun.d.ts +37 -0
  68. package/dist/mcp/projectRun.js +57 -0
  69. package/dist/mcp/readArtifact.d.ts +2 -0
  70. package/dist/mcp/readArtifact.js +46 -0
  71. package/dist/mcp/server.d.ts +7 -0
  72. package/dist/mcp/server.js +26 -0
  73. package/dist/mcp/tools.d.ts +9 -0
  74. package/dist/mcp/tools.js +96 -0
  75. package/dist/package-meta.d.ts +1 -0
  76. package/dist/package-meta.js +1 -0
  77. package/dist/prompt/priorEnvelope.d.ts +2 -0
  78. package/dist/prompt/priorEnvelope.js +5 -0
  79. package/dist/runstore/catalog.d.ts +8 -0
  80. package/dist/runstore/catalog.js +16 -0
  81. package/dist/runstore/createStore.d.ts +15 -0
  82. package/dist/runstore/createStore.js +23 -0
  83. package/dist/runstore/disk/DiskRunStore.d.ts +30 -0
  84. package/dist/runstore/disk/DiskRunStore.js +238 -0
  85. package/dist/runstore/disk/catalogHelpers.d.ts +11 -0
  86. package/dist/runstore/disk/catalogHelpers.js +102 -0
  87. package/dist/runstore/layout.d.ts +22 -0
  88. package/dist/runstore/layout.js +58 -0
  89. package/dist/runstore/paths.d.ts +7 -0
  90. package/dist/runstore/paths.js +60 -0
  91. package/dist/runstore/pipelineDagSnapshot.d.ts +5 -0
  92. package/dist/runstore/pipelineDagSnapshot.js +43 -0
  93. package/dist/runstore/port.d.ts +146 -0
  94. package/dist/runstore/port.js +28 -0
  95. package/dist/runstore/runProjection.d.ts +6 -0
  96. package/dist/runstore/runProjection.js +102 -0
  97. package/dist/runstore/sqlite/SqliteRunStore.d.ts +48 -0
  98. package/dist/runstore/sqlite/SqliteRunStore.js +460 -0
  99. package/dist/runstore/sqlite/migrateFromDisk.d.ts +5 -0
  100. package/dist/runstore/sqlite/migrateFromDisk.js +108 -0
  101. package/dist/runstore/sqlite/schema.d.ts +1 -0
  102. package/dist/runstore/sqlite/schema.js +59 -0
  103. package/dist/runstore/stageExecution.d.ts +13 -0
  104. package/dist/runstore/stageExecution.js +22 -0
  105. package/dist/runstore/stageSnapshot.d.ts +2 -0
  106. package/dist/runstore/stageSnapshot.js +46 -0
  107. package/dist/runstore/syntheticStageSnapshot.d.ts +2 -0
  108. package/dist/runstore/syntheticStageSnapshot.js +10 -0
  109. package/dist/runstore/trackProjection.d.ts +7 -0
  110. package/dist/runstore/trackProjection.js +94 -0
  111. package/dist/runstore/workspaceLayout.d.ts +31 -0
  112. package/dist/runstore/workspaceLayout.js +172 -0
  113. package/dist/runtime/answerResume.d.ts +16 -0
  114. package/dist/runtime/answerResume.js +22 -0
  115. package/dist/runtime/credentialBinding.d.ts +16 -0
  116. package/dist/runtime/credentialBinding.js +80 -0
  117. package/dist/runtime/dagTraversal.d.ts +3 -0
  118. package/dist/runtime/dagTraversal.js +26 -0
  119. package/dist/runtime/envelopeRouting.d.ts +20 -0
  120. package/dist/runtime/envelopeRouting.js +59 -0
  121. package/dist/runtime/hitlSeams.d.ts +38 -0
  122. package/dist/runtime/hitlSeams.js +2 -0
  123. package/dist/runtime/pipelineRunner.d.ts +77 -0
  124. package/dist/runtime/pipelineRunner.js +141 -0
  125. package/dist/runtime/pipelineScheduler.d.ts +90 -0
  126. package/dist/runtime/pipelineScheduler.js +494 -0
  127. package/dist/runtime/pipelineValidationError.d.ts +6 -0
  128. package/dist/runtime/pipelineValidationError.js +8 -0
  129. package/dist/runtime/resumeReconstruct.d.ts +33 -0
  130. package/dist/runtime/resumeReconstruct.js +144 -0
  131. package/dist/runtime/runManager.d.ts +129 -0
  132. package/dist/runtime/runManager.js +818 -0
  133. package/dist/runtime/runRetryCoordinator.d.ts +85 -0
  134. package/dist/runtime/runRetryCoordinator.js +372 -0
  135. package/dist/runtime/settingsFile.d.ts +17 -0
  136. package/dist/runtime/settingsFile.js +88 -0
  137. package/dist/runtime/stageAttemptBootstrap.d.ts +40 -0
  138. package/dist/runtime/stageAttemptBootstrap.js +82 -0
  139. package/dist/runtime/stageAttemptContext.d.ts +11 -0
  140. package/dist/runtime/stageAttemptContext.js +26 -0
  141. package/dist/runtime/stageConcurrency.d.ts +10 -0
  142. package/dist/runtime/stageConcurrency.js +51 -0
  143. package/dist/runtime/stageHitl.d.ts +95 -0
  144. package/dist/runtime/stageHitl.js +198 -0
  145. package/dist/runtime/stageProcessLauncher.d.ts +45 -0
  146. package/dist/runtime/stageProcessLauncher.js +199 -0
  147. package/dist/runtime/stageRecovery.d.ts +10 -0
  148. package/dist/runtime/stageRecovery.js +43 -0
  149. package/dist/runtime/stageRoots.d.ts +22 -0
  150. package/dist/runtime/stageRoots.js +93 -0
  151. package/dist/runtime/stageRunner.d.ts +59 -0
  152. package/dist/runtime/stageRunner.js +221 -0
  153. package/dist/runtime/stageWorker.d.ts +6 -0
  154. package/dist/runtime/stageWorker.js +109 -0
  155. package/dist/runtime/stageWorkerProtocol.d.ts +27 -0
  156. package/dist/runtime/stageWorkerProtocol.js +16 -0
  157. package/dist/runtime/taskInput.d.ts +15 -0
  158. package/dist/runtime/taskInput.js +38 -0
  159. package/dist/server/http.d.ts +31 -0
  160. package/dist/server/http.js +561 -0
  161. package/dist/server/providerRoutes.d.ts +15 -0
  162. package/dist/server/providerRoutes.js +186 -0
  163. package/dist/tools/askOperator.d.ts +185 -0
  164. package/dist/tools/askOperator.js +409 -0
  165. package/dist/tools/emitStageEnvelope.d.ts +42 -0
  166. package/dist/tools/emitStageEnvelope.js +61 -0
  167. package/dist/tools/writeStageArtifact.d.ts +26 -0
  168. package/dist/tools/writeStageArtifact.js +103 -0
  169. package/dist/types/envelope.d.ts +12 -0
  170. package/dist/types/envelope.js +6 -0
  171. package/dist/types/pipeline.d.ts +25 -0
  172. package/dist/types/pipeline.js +1 -0
  173. package/dist/types/stage.d.ts +12 -0
  174. package/dist/types/stage.js +6 -0
  175. package/dist/types/task.d.ts +7 -0
  176. package/dist/types/task.js +1 -0
  177. package/dist/ui/assets/index-Cry0Tpfx.js +118 -0
  178. package/dist/ui/assets/index-DCsDopak.css +1 -0
  179. package/dist/ui/index.html +21 -0
  180. package/package.json +72 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tejas G
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Stageflow
2
+
3
+ CLI pipeline runtime for configurable SDLC stages on [Pi](https://github.com/badlogic/pi-mono), plus a local operator console.
4
+
5
+ Author YAML pipelines, stages, and tasks in a project directory. Stageflow runs each stage in a fresh Pi agent session, writes a structured handoff envelope, and keeps run state under `.stageflow/`. Bins: **`sf`** and **`stageflow`**.
6
+
7
+ Requires **Node.js ≥ 20**.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm i -g stageflow
13
+ # or
14
+ npx stageflow
15
+ ```
16
+
17
+ `better-sqlite3` ships prebuilds for common platforms. `--ignore-scripts` is fine when a prebuild exists. Benign `node-gyp` warnings during install can be ignored if `require("better-sqlite3")` works.
18
+
19
+ ## Quick start
20
+
21
+ In a project directory, author:
22
+
23
+ - `pipelines/` — ordered stage lists
24
+ - `stages/` — stage definitions (instructions, model, optional human gates)
25
+ - `tasks/` — work items (goal, pipeline, optional checkout)
26
+
27
+ ```bash
28
+ sf ui # operator console at http://127.0.0.1:3847
29
+ sf run --task tasks/foo.yaml --pipeline <pipeline-id>
30
+ ```
31
+
32
+ Connect model providers in the console (Settings → Providers) or via `sf providers …`. Stageflow is a thin Pi shell: reuse an existing Pi login (`pi_home`) or store credentials in an SF-owned file (`sf_owned`). You do not need Pi CLI `/login` as a hard prerequisite.
33
+
34
+ ## State
35
+
36
+ Runtime state lives in **`.stageflow/`** (SQLite + per-run workspaces under `.stageflow/runs/`). If `.stageflow` is missing and `.software-factory` exists from an older install, the next store open renames it to `.stageflow` once.
37
+
38
+ ## MCP
39
+
40
+ `sf ui` also serves a Streamable HTTP MCP endpoint at `http://127.0.0.1:3847/mcp` (URL printed on boot). Point a Cursor (or other) MCP client at that URL for tools like `list_pipelines`, `start_run`, and `get_run`.
41
+
42
+ ## Develop from source
43
+
44
+ ```bash
45
+ git clone https://github.com/tejasghutukade/stageflow.git
46
+ cd stageflow
47
+ npm i
48
+ npm run build && npm run ui:build # ui:build builds the UI and copies assets into dist/ui
49
+ sf ui
50
+ ```
51
+
52
+ ## License
53
+
54
+ MIT © Tejas G
55
+
56
+ Issues: https://github.com/tejasghutukade/stageflow/issues
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Pi-agnostic stage activity events.
3
+ *
4
+ * Adapters map provider/SDK events into this vocabulary. Disk, API, UI, and
5
+ * HITL control planes should speak these types — not Pi's AgentEvent.
6
+ * Lifecycle `waiting_for_input` / `resumed` are owned by stageRunner (not
7
+ * adapters).
8
+ *
9
+ * `operator_prompt` / `operator_answer` carry full T2 ask-operator payloads.
10
+ * ACTIVITY_TEXT_LIMIT applies only to tool/message *previews* — never to
11
+ * those Q&A bodies.
12
+ */
13
+ import type { AskOperatorAnswer, AskOperatorPrompt } from "../tools/askOperator.js";
14
+ export declare const ACTIVITY_TEXT_LIMIT = 500;
15
+ export type StageActivityEvent = {
16
+ event: "agent_start";
17
+ } | {
18
+ event: "agent_end";
19
+ } | {
20
+ event: "turn_start";
21
+ } | {
22
+ event: "tool_start";
23
+ toolName: string;
24
+ toolCallId?: string;
25
+ argsPreview?: string;
26
+ } | {
27
+ event: "tool_end";
28
+ toolName: string;
29
+ toolCallId?: string;
30
+ isError?: boolean;
31
+ resultPreview?: string;
32
+ } | {
33
+ event: "message";
34
+ role: string;
35
+ text?: string;
36
+ } | {
37
+ event: "operator_prompt";
38
+ prompt: AskOperatorPrompt;
39
+ } | {
40
+ event: "operator_answer";
41
+ promptId: string;
42
+ answer: AskOperatorAnswer;
43
+ };
44
+ export type OperatorPromptActivityEvent = Extract<StageActivityEvent, {
45
+ event: "operator_prompt";
46
+ }>;
47
+ export type OperatorAnswerActivityEvent = Extract<StageActivityEvent, {
48
+ event: "operator_answer";
49
+ }>;
50
+ /** Lifecycle lines written by stageRunner (not the adapter). */
51
+ export type StageLifecycleEvent = {
52
+ event: "started";
53
+ } | {
54
+ event: "waiting_for_input";
55
+ } | {
56
+ event: "resumed";
57
+ } | {
58
+ event: "succeeded";
59
+ } | {
60
+ event: "failed";
61
+ reason: string;
62
+ };
63
+ export type StageLogLine = StageActivityEvent | StageLifecycleEvent;
64
+ export declare function truncateActivityText(value: unknown, limit?: number): string | undefined;
65
+ export declare function extractMessageText(message: {
66
+ role?: string;
67
+ content?: unknown;
68
+ }): {
69
+ role: string;
70
+ text?: string;
71
+ };
72
+ /**
73
+ * Map a loose Pi/session event object into a StageActivityEvent.
74
+ * Returns null for events we intentionally ignore (e.g. text_delta streams).
75
+ */
76
+ export declare function mapSessionEventToActivity(event: Record<string, unknown>): StageActivityEvent | null;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Pi-agnostic stage activity events.
3
+ *
4
+ * Adapters map provider/SDK events into this vocabulary. Disk, API, UI, and
5
+ * HITL control planes should speak these types — not Pi's AgentEvent.
6
+ * Lifecycle `waiting_for_input` / `resumed` are owned by stageRunner (not
7
+ * adapters).
8
+ *
9
+ * `operator_prompt` / `operator_answer` carry full T2 ask-operator payloads.
10
+ * ACTIVITY_TEXT_LIMIT applies only to tool/message *previews* — never to
11
+ * those Q&A bodies.
12
+ */
13
+ export const ACTIVITY_TEXT_LIMIT = 500;
14
+ export function truncateActivityText(value, limit = ACTIVITY_TEXT_LIMIT) {
15
+ if (value === undefined || value === null)
16
+ return undefined;
17
+ let text;
18
+ if (typeof value === "string") {
19
+ text = value;
20
+ }
21
+ else {
22
+ try {
23
+ text = JSON.stringify(value);
24
+ }
25
+ catch {
26
+ text = String(value);
27
+ }
28
+ }
29
+ if (text.length <= limit)
30
+ return text;
31
+ return `${text.slice(0, limit)}…`;
32
+ }
33
+ export function extractMessageText(message) {
34
+ const role = typeof message.role === "string" ? message.role : "unknown";
35
+ const content = message.content;
36
+ if (typeof content === "string") {
37
+ return { role, text: truncateActivityText(content) };
38
+ }
39
+ if (!Array.isArray(content)) {
40
+ return { role };
41
+ }
42
+ const parts = [];
43
+ for (const block of content) {
44
+ if (!block || typeof block !== "object")
45
+ continue;
46
+ const b = block;
47
+ if (b.type === "text" && typeof b.text === "string") {
48
+ parts.push(b.text);
49
+ }
50
+ }
51
+ if (parts.length === 0)
52
+ return { role };
53
+ return { role, text: truncateActivityText(parts.join("")) };
54
+ }
55
+ /**
56
+ * Map a loose Pi/session event object into a StageActivityEvent.
57
+ * Returns null for events we intentionally ignore (e.g. text_delta streams).
58
+ */
59
+ export function mapSessionEventToActivity(event) {
60
+ const type = event.type;
61
+ if (type === "agent_start")
62
+ return { event: "agent_start" };
63
+ if (type === "agent_end")
64
+ return { event: "agent_end" };
65
+ if (type === "turn_start")
66
+ return { event: "turn_start" };
67
+ if (type === "tool_execution_start") {
68
+ return {
69
+ event: "tool_start",
70
+ toolName: String(event.toolName ?? "unknown"),
71
+ toolCallId: typeof event.toolCallId === "string" ? event.toolCallId : undefined,
72
+ argsPreview: truncateActivityText(event.args),
73
+ };
74
+ }
75
+ if (type === "tool_execution_end") {
76
+ return {
77
+ event: "tool_end",
78
+ toolName: String(event.toolName ?? "unknown"),
79
+ toolCallId: typeof event.toolCallId === "string" ? event.toolCallId : undefined,
80
+ isError: Boolean(event.isError),
81
+ resultPreview: truncateActivityText(event.result),
82
+ };
83
+ }
84
+ if (type === "message_end" && event.message && typeof event.message === "object") {
85
+ const { role, text } = extractMessageText(event.message);
86
+ return { event: "message", role, text };
87
+ }
88
+ return null;
89
+ }
@@ -0,0 +1,17 @@
1
+ import type { StageActivityEvent } from "./activity.js";
2
+ export type StageActivityObserver = {
3
+ onAssistantTextDelta(delta: string): void;
4
+ onStreamBoundary(): void;
5
+ onActivity(activity: StageActivityEvent): void;
6
+ dispose(): void;
7
+ };
8
+ export type CreateStageActivityObserverOptions = {
9
+ onActivity?: (event: StageActivityEvent) => void;
10
+ writeStderr?: boolean;
11
+ };
12
+ /**
13
+ * Observe StageActivityEvent milestones and optional assistant text deltas.
14
+ * Pi (or any adapter) maps provider events to StageActivityEvent before calling
15
+ * onActivity — Record-shaped SDK events never enter this module.
16
+ */
17
+ export declare function createStageActivityObserver(options?: CreateStageActivityObserverOptions): StageActivityObserver;
@@ -0,0 +1,66 @@
1
+ function writeActivityStderr(activity) {
2
+ switch (activity.event) {
3
+ case "agent_start":
4
+ process.stderr.write(" … agent started\n");
5
+ return;
6
+ case "agent_end":
7
+ process.stderr.write(" … agent settled\n");
8
+ return;
9
+ case "turn_start":
10
+ process.stderr.write(" … turn\n");
11
+ return;
12
+ case "tool_start":
13
+ process.stderr.write(` → ${activity.toolName}\n`);
14
+ return;
15
+ case "tool_end":
16
+ if (activity.isError) {
17
+ process.stderr.write(` ✕ ${activity.toolName}\n`);
18
+ }
19
+ return;
20
+ case "message":
21
+ return;
22
+ }
23
+ }
24
+ /**
25
+ * Observe StageActivityEvent milestones and optional assistant text deltas.
26
+ * Pi (or any adapter) maps provider events to StageActivityEvent before calling
27
+ * onActivity — Record-shaped SDK events never enter this module.
28
+ */
29
+ export function createStageActivityObserver(options = {}) {
30
+ const writeStderr = options.writeStderr !== false;
31
+ let inAssistantText = false;
32
+ const endTextLine = () => {
33
+ if (!inAssistantText) {
34
+ return;
35
+ }
36
+ if (writeStderr) {
37
+ process.stderr.write("\n");
38
+ }
39
+ inAssistantText = false;
40
+ };
41
+ return {
42
+ onAssistantTextDelta(delta) {
43
+ if (!writeStderr || !delta) {
44
+ return;
45
+ }
46
+ if (!inAssistantText) {
47
+ process.stderr.write(" ");
48
+ inAssistantText = true;
49
+ }
50
+ process.stderr.write(delta);
51
+ },
52
+ onStreamBoundary() {
53
+ endTextLine();
54
+ },
55
+ onActivity(activity) {
56
+ endTextLine();
57
+ options.onActivity?.(activity);
58
+ if (writeStderr) {
59
+ writeActivityStderr(activity);
60
+ }
61
+ },
62
+ dispose() {
63
+ endTextLine();
64
+ },
65
+ };
66
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Locate the pi-cursor-sdk extension entry without opening full global package
3
+ * discovery. Stages stay sealed; only this allowlisted path is loaded.
4
+ *
5
+ * Resolution order:
6
+ * 1. SOFTWARE_FACTORY_CURSOR_EXTENSION (absolute path to the extension .ts/.js)
7
+ * 2. Path package from ~/.pi/agent/settings.json (same source interactive pi uses)
8
+ * 3. npm install under ~/.pi/agent/npm/node_modules/pi-cursor-sdk
9
+ * 4. Sibling checkout at ../pi-cursor-sdk relative to this repo
10
+ */
11
+ export declare function resolveCursorExtensionPath(): string | undefined;
12
+ export declare function isCursorModelRef(modelRef: string): boolean;
@@ -0,0 +1,88 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ /**
6
+ * Locate the pi-cursor-sdk extension entry without opening full global package
7
+ * discovery. Stages stay sealed; only this allowlisted path is loaded.
8
+ *
9
+ * Resolution order:
10
+ * 1. SOFTWARE_FACTORY_CURSOR_EXTENSION (absolute path to the extension .ts/.js)
11
+ * 2. Path package from ~/.pi/agent/settings.json (same source interactive pi uses)
12
+ * 3. npm install under ~/.pi/agent/npm/node_modules/pi-cursor-sdk
13
+ * 4. Sibling checkout at ../pi-cursor-sdk relative to this repo
14
+ */
15
+ export function resolveCursorExtensionPath() {
16
+ const fromEnv = process.env.SOFTWARE_FACTORY_CURSOR_EXTENSION?.trim();
17
+ if (fromEnv && existsSync(fromEnv)) {
18
+ return path.resolve(fromEnv);
19
+ }
20
+ const fromSettings = resolveFromPiSettings();
21
+ if (fromSettings) {
22
+ return fromSettings;
23
+ }
24
+ const npmEntry = path.join(os.homedir(), ".pi", "agent", "npm", "node_modules", "pi-cursor-sdk", "src", "index.ts");
25
+ if (existsSync(npmEntry)) {
26
+ return npmEntry;
27
+ }
28
+ const here = path.dirname(fileURLToPath(import.meta.url));
29
+ const sibling = path.resolve(here, "../../../pi-cursor-sdk/src/index.ts");
30
+ if (existsSync(sibling)) {
31
+ return sibling;
32
+ }
33
+ return undefined;
34
+ }
35
+ export function isCursorModelRef(modelRef) {
36
+ const slash = modelRef.indexOf("/");
37
+ if (slash <= 0) {
38
+ return false;
39
+ }
40
+ return modelRef.slice(0, slash).toLowerCase() === "cursor";
41
+ }
42
+ function resolveFromPiSettings() {
43
+ const settingsPath = path.join(os.homedir(), ".pi", "agent", "settings.json");
44
+ if (!existsSync(settingsPath)) {
45
+ return undefined;
46
+ }
47
+ let settings;
48
+ try {
49
+ settings = JSON.parse(readFileSync(settingsPath, "utf8"));
50
+ }
51
+ catch {
52
+ return undefined;
53
+ }
54
+ if (!Array.isArray(settings.packages)) {
55
+ return undefined;
56
+ }
57
+ const agentDir = path.join(os.homedir(), ".pi", "agent");
58
+ for (const entry of settings.packages) {
59
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
60
+ continue;
61
+ }
62
+ const pkg = entry;
63
+ if (typeof pkg.source !== "string") {
64
+ continue;
65
+ }
66
+ if (!pkg.source.includes("pi-cursor-sdk")) {
67
+ continue;
68
+ }
69
+ const packageRoot = path.resolve(agentDir, pkg.source);
70
+ if (Array.isArray(pkg.extensions)) {
71
+ for (const ext of pkg.extensions) {
72
+ if (typeof ext !== "string") {
73
+ continue;
74
+ }
75
+ const rel = ext.replace(/^\+/, "");
76
+ const full = path.resolve(packageRoot, rel);
77
+ if (existsSync(full)) {
78
+ return full;
79
+ }
80
+ }
81
+ }
82
+ const declared = path.join(packageRoot, "src", "index.ts");
83
+ if (existsSync(declared)) {
84
+ return declared;
85
+ }
86
+ }
87
+ return undefined;
88
+ }
@@ -0,0 +1,14 @@
1
+ import { type StageProviderSupport } from "./providerSupport.js";
2
+ /**
3
+ * Locate the pi-cursor-sdk extension entry without opening full global package
4
+ * discovery. Stages stay sealed; only this allowlisted path is loaded.
5
+ *
6
+ * Resolution order:
7
+ * 1. STAGEFLOW_CURSOR_EXTENSION (absolute path to the extension .ts/.js)
8
+ * 2. Path package from ~/.pi/agent/settings.json (same source interactive pi uses)
9
+ * 3. npm install under ~/.pi/agent/npm/node_modules/pi-cursor-sdk
10
+ * 4. Sibling checkout at ../pi-cursor-sdk relative to this repo
11
+ */
12
+ export declare function resolveCursorExtensionPath(): string | undefined;
13
+ export declare function isCursorModelRef(modelRef: string): boolean;
14
+ export declare const cursorProviderSupport: StageProviderSupport;
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Cursor-only StageProviderSupport for pi-cursor-sdk.
3
+ *
4
+ * Loaded only when stage.model is `cursor/...`. Other providers never hit this
5
+ * module's prepare()/env sealing path.
6
+ */
7
+ import { existsSync, readFileSync } from "node:fs";
8
+ import os from "node:os";
9
+ import path from "node:path";
10
+ import { fileURLToPath } from "node:url";
11
+ import { registerProviderSupport, } from "./providerSupport.js";
12
+ const CURSOR_SETTING_SOURCES_ENV = "PI_CURSOR_SETTING_SOURCES";
13
+ /**
14
+ * Locate the pi-cursor-sdk extension entry without opening full global package
15
+ * discovery. Stages stay sealed; only this allowlisted path is loaded.
16
+ *
17
+ * Resolution order:
18
+ * 1. STAGEFLOW_CURSOR_EXTENSION (absolute path to the extension .ts/.js)
19
+ * 2. Path package from ~/.pi/agent/settings.json (same source interactive pi uses)
20
+ * 3. npm install under ~/.pi/agent/npm/node_modules/pi-cursor-sdk
21
+ * 4. Sibling checkout at ../pi-cursor-sdk relative to this repo
22
+ */
23
+ export function resolveCursorExtensionPath() {
24
+ const fromEnv = process.env.STAGEFLOW_CURSOR_EXTENSION?.trim();
25
+ if (fromEnv && existsSync(fromEnv)) {
26
+ return path.resolve(fromEnv);
27
+ }
28
+ const fromSettings = resolveFromPiSettings();
29
+ if (fromSettings) {
30
+ return fromSettings;
31
+ }
32
+ const npmEntry = path.join(os.homedir(), ".pi", "agent", "npm", "node_modules", "pi-cursor-sdk", "src", "index.ts");
33
+ if (existsSync(npmEntry)) {
34
+ return npmEntry;
35
+ }
36
+ const here = path.dirname(fileURLToPath(import.meta.url));
37
+ const sibling = path.resolve(here, "../../../pi-cursor-sdk/src/index.ts");
38
+ if (existsSync(sibling)) {
39
+ return sibling;
40
+ }
41
+ return undefined;
42
+ }
43
+ export function isCursorModelRef(modelRef) {
44
+ const slash = modelRef.indexOf("/");
45
+ if (slash <= 0) {
46
+ return false;
47
+ }
48
+ return modelRef.slice(0, slash).toLowerCase() === "cursor";
49
+ }
50
+ function sealCursorSettingSources() {
51
+ if (process.env[CURSOR_SETTING_SOURCES_ENV] !== undefined) {
52
+ return undefined;
53
+ }
54
+ process.env[CURSOR_SETTING_SOURCES_ENV] = "none";
55
+ return () => {
56
+ delete process.env[CURSOR_SETTING_SOURCES_ENV];
57
+ };
58
+ }
59
+ function missingExtensionReason(modelRef) {
60
+ return [
61
+ `Model "${modelRef}" requires pi-cursor-sdk, but no extension entry was found.`,
62
+ "Install with `pi install npm:pi-cursor-sdk`, or set STAGEFLOW_CURSOR_EXTENSION",
63
+ "to the absolute path of pi-cursor-sdk/src/index.ts.",
64
+ "Also ensure a Cursor SDK API key is available via Pi /login or CURSOR_API_KEY.",
65
+ ].join(" ");
66
+ }
67
+ function prepareCursor(modelRef) {
68
+ const extensionPath = resolveCursorExtensionPath();
69
+ if (!extensionPath) {
70
+ return { extensionPaths: [], error: missingExtensionReason(modelRef) };
71
+ }
72
+ return {
73
+ extensionPaths: [extensionPath],
74
+ // pi-cursor-sdk defaults to settingSources ["all"]. Seal ambient Cursor
75
+ // rules/plugins/MCP for isolated stages unless the operator already set it.
76
+ restore: sealCursorSettingSources(),
77
+ };
78
+ }
79
+ export const cursorProviderSupport = {
80
+ id: "cursor",
81
+ matches: isCursorModelRef,
82
+ prepare: prepareCursor,
83
+ emitToolHint(toolName) {
84
+ return [
85
+ `Required final action: call the MCP tool pi__${toolName} exactly once`,
86
+ `(pi name: ${toolName}) with status, summary, and artifacts (run-relative paths)`,
87
+ "and a payload matching the stage payload_schema when one is defined.",
88
+ "Do not stop after writing files — the stage only completes when that tool returns.",
89
+ ].join(" ");
90
+ },
91
+ };
92
+ registerProviderSupport(cursorProviderSupport);
93
+ function resolveFromPiSettings() {
94
+ const settingsPath = path.join(os.homedir(), ".pi", "agent", "settings.json");
95
+ if (!existsSync(settingsPath)) {
96
+ return undefined;
97
+ }
98
+ let settings;
99
+ try {
100
+ settings = JSON.parse(readFileSync(settingsPath, "utf8"));
101
+ }
102
+ catch {
103
+ return undefined;
104
+ }
105
+ if (!Array.isArray(settings.packages)) {
106
+ return undefined;
107
+ }
108
+ const agentDir = path.join(os.homedir(), ".pi", "agent");
109
+ for (const entry of settings.packages) {
110
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
111
+ continue;
112
+ }
113
+ const pkg = entry;
114
+ if (typeof pkg.source !== "string") {
115
+ continue;
116
+ }
117
+ if (!pkg.source.includes("pi-cursor-sdk")) {
118
+ continue;
119
+ }
120
+ const packageRoot = path.resolve(agentDir, pkg.source);
121
+ if (Array.isArray(pkg.extensions)) {
122
+ for (const ext of pkg.extensions) {
123
+ if (typeof ext !== "string") {
124
+ continue;
125
+ }
126
+ const rel = ext.replace(/^\+/, "");
127
+ const full = path.resolve(packageRoot, rel);
128
+ if (existsSync(full)) {
129
+ return full;
130
+ }
131
+ }
132
+ }
133
+ const declared = path.join(packageRoot, "src", "index.ts");
134
+ if (existsSync(declared)) {
135
+ return declared;
136
+ }
137
+ }
138
+ return undefined;
139
+ }
@@ -0,0 +1,29 @@
1
+ import type { StageRoots } from "../runtime/stageRoots.js";
2
+ import type { AgentPort, OpaqueAnswer, OpaqueWaitRequest, StageHandle, StageRunInput, StageRunResult } from "./port.js";
3
+ export type FakeAgentBehavior = {
4
+ type: "emit";
5
+ envelope: unknown;
6
+ } | {
7
+ type: "never_emit";
8
+ } | {
9
+ type: "throw";
10
+ message: string;
11
+ } | {
12
+ type: "wait_then_emit";
13
+ waitRequests: OpaqueWaitRequest[];
14
+ expectedAnswers?: OpaqueAnswer[];
15
+ envelope: unknown;
16
+ };
17
+ export type RecordedStageRoots = StageRoots;
18
+ export declare function fakeHitlResumePath(roots: Pick<StageRoots, "runWorkspaceDir">, stageId: string): string;
19
+ export declare class FakeAgent implements AgentPort {
20
+ private readonly behavior;
21
+ lastRoots?: RecordedStageRoots;
22
+ receivedAnswers: OpaqueAnswer[];
23
+ constructor(behavior: FakeAgentBehavior);
24
+ openStage(input: StageRunInput): StageHandle;
25
+ runStage(input: StageRunInput): Promise<StageRunResult>;
26
+ }
27
+ export declare function scriptedFakeAgent(behaviors: FakeAgentBehavior[]): AgentPort & {
28
+ recorded: RecordedStageRoots[];
29
+ };