Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackforge/api",
"version": "0.1.0",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions apps/api/src/services/generate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
selectWorkflow,
StepExecutionError,
type LLMProvider,
type AgentStepExecutor,
} from "@stackforge/agents";
import {
createJob,
Expand Down Expand Up @@ -40,6 +41,8 @@ export type RuntimeStatus = {

export type GenerateProjectOptions = {
execution?: GenerateExecution;
/** Replaces provider-backed stage execution (offline tests). */
executor?: AgentStepExecutor;
};

class AgentRoutingProvider implements LLMProvider {
Expand Down Expand Up @@ -350,6 +353,7 @@ async function startOrchestration(
baseProviderName: string,
codegenProviderName: string | undefined,
execution?: GenerateExecution,
executor?: AgentStepExecutor,
): Promise<void> {
const emit = buildEmitter(jobId, expectedAgentsTotal, baseProviderName, codegenProviderName);
const jobStart = Date.now();
Expand All @@ -365,6 +369,7 @@ async function startOrchestration(
emit,
provider: getOrchestrationProvider(execution),
cache,
...(executor !== undefined ? { executor } : {}),
});

const now = new Date().toISOString();
Expand Down Expand Up @@ -431,6 +436,7 @@ export function generateProject(
baseProviderName,
codegenProviderName,
options?.execution,
options?.executor,
);

return job;
Expand Down
55 changes: 55 additions & 0 deletions apps/api/test/smoke-offline.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/// <reference types="bun" />
import { describe, expect, it } from "bun:test";
import { MockStepExecutor } from "@stackforge/agents";
import { BlueprintSchema } from "@stackforge/shared";
import { generateProject } from "../src/services/generate.service.js";
import { getJob, type StoredJob } from "../src/store/job.store.js";

async function waitForTerminal(jobId: string, timeoutMs = 5000): Promise<StoredJob> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const job = getJob(jobId);
if (job !== undefined && (job.status === "completed" || job.status === "failed")) {
return job;
}
await new Promise((resolve) => setTimeout(resolve, 10));
}
throw new Error(`job ${jobId} did not finish within ${timeoutMs}ms`);
}

describe("offline v1 smoke through the API job store", () => {
it("completes a full run and records every stage", async () => {
const job = generateProject("Build a task management app with teams", "smoke-api", {
executor: new MockStepExecutor(),
});
expect(job.status).toBe("queued");

const done = await waitForTerminal(job.id);
expect(done.status).toBe("completed");
expect(done.agentsCompleted).toEqual([
"planner", "schema", "api", "frontend", "devops", "reviewer", "codegen",
]);
expect(done.currentStage).toBeUndefined();
expect(done.failure).toBeUndefined();
expect(BlueprintSchema.safeParse(done.blueprint).success).toBe(true);
expect(done.events.some((e) => e.type === "job_completed")).toBe(true);
});

it("records a structured step failure when a mid-pipeline stage fails", async () => {
const job = generateProject("Build a task management app with teams", "smoke-api-fail", {
executor: new MockStepExecutor({ failAt: "api" }),
});

const done = await waitForTerminal(job.id);
expect(done.status).toBe("failed");
expect(done.agentsCompleted).toEqual(["planner", "schema"]);
expect(done.currentStage).toBe("api");
expect(done.failure).toEqual({ stage: "api", stepId: "step-3", message: "mock failure at stage 'api'" });
expect(done.error).toBe("mock failure at stage 'api'");
expect(done.blueprint).toBeUndefined();

const failedEvent = done.events.find((e) => e.type === "job_failed");
expect(failedEvent).toBeDefined();
expect(failedEvent?.type === "job_failed" && failedEvent.payload.failure?.stage).toBe("api");
});
});
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackforge/web",
"version": "0.1.0",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "stackforge",
"private": true,
"version": "0.1.0",
"version": "1.0.0",
"packageManager": "bun@1.3.0",
"workspaces": [
"apps/*",
Expand Down
2 changes: 1 addition & 1 deletion packages/agents/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackforge/agents",
"version": "0.1.0",
"version": "1.0.0",
"private": true,
"type": "module",
"main": "./dist/index.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/agents/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type { WorkflowEngineOptions, WorkflowRunMeta } from "./workflow/engine.j
export { ProviderStepExecutor } from "./workflow/provider-step-executor.js";
export type { ProviderStepExecutorOptions } from "./workflow/provider-step-executor.js";
export { StepExecutionError } from "./workflow/step-executor.js";
export { MockStepExecutor } from "./workflow/mock-step-executor.js";
export type { MockStepExecutorOptions } from "./workflow/mock-step-executor.js";
export type {
AgentStepExecutor,
StepExecutionRequest,
Expand Down
77 changes: 77 additions & 0 deletions packages/agents/src/workflow/mock-step-executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { AgentName, SSEEvent } from "@stackforge/shared";
import {
buildApiOutput,
buildCodegenOutput,
buildDevopsOutput,
buildFrontendOutput,
buildPlannerOutput,
buildReviewerOutput,
buildSchemaOutput,
} from "../provider/mock.data.js";
import type { AgentStepExecutor, StepExecutionRequest, StepExecutionResult } from "./step-executor.js";

export type MockStepExecutorOptions = {
/** Stage that throws instead of returning output. */
failAt?: AgentName;
};

function outputFor(agent: AgentName, projectName: string): unknown {
switch (agent) {
case "planner": return buildPlannerOutput(projectName);
case "schema": return buildSchemaOutput();
case "api": return buildApiOutput();
case "frontend": return buildFrontendOutput();
case "devops": return buildDevopsOutput(projectName);
case "reviewer": return buildReviewerOutput();
case "codegen": return buildCodegenOutput(projectName);
}
}

/**
* Deterministic, offline AgentStepExecutor. No provider, no delays, no
* network. Emits the same stage events as ProviderStepExecutor so job and
* telemetry handling run as in production.
*/
export class MockStepExecutor implements AgentStepExecutor {
readonly executedStages: AgentName[] = [];

constructor(private readonly options: MockStepExecutorOptions = {}) {}

async execute({ jobId, step, state, emit }: StepExecutionRequest): Promise<StepExecutionResult> {
const now = new Date().toISOString();
emit({ type: "agent_started", jobId, agent: step.agent, timestamp: now, payload: {} });
this.executedStages.push(step.agent);

if (this.options.failAt === step.agent) {
const message = `mock failure at stage '${step.agent}'`;
emit({ type: "agent_failed", jobId, agent: step.agent, timestamp: now, payload: { error: message } });
throw new Error(message);
}

const rawOutput = outputFor(step.agent, state["projectName"] ?? "mock-project");
const tokensUsed = 100;
const completed: SSEEvent = {
type: "agent_completed",
jobId,
agent: step.agent,
timestamp: now,
payload: {
durationMs: 0,
cached: false,
inputTokens: 60,
outputTokens: 40,
totalTokens: tokensUsed,
tokensUsed,
estimatedInputTokens: 60,
compressionPasses: 0,
providerInputTokens: 60,
providerOutputTokens: 40,
model: "mock",
},
};
emit({ type: "agent_complete", jobId, agentId: step.agent, fullOutput: rawOutput, timestamp: Date.now() });
emit(completed);

return { rawOutput, compressedOutput: JSON.stringify(rawOutput), tokensUsed };
}
}
65 changes: 65 additions & 0 deletions packages/agents/test/smoke-offline.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/// <reference types="bun" />
import { describe, expect, it } from "bun:test";
import { BlueprintSchema, type SSEEvent } from "@stackforge/shared";
import { AgentCache } from "../src/cache/agent.cache.js";
import { runOrchestrator } from "../src/orchestrator/index.js";
import type { LLMProvider } from "../src/provider/provider.interface.js";
import { MockStepExecutor } from "../src/workflow/mock-step-executor.js";
import { StepExecutionError } from "../src/workflow/step-executor.js";

// Any provider call means the executor boundary leaked.
const forbiddenProvider: LLMProvider = {
name: "forbidden",
call: () => Promise.reject(new Error("provider must not be called in offline smoke test")),
};

const SCAFFOLD_PROMPT = "Build a task management app with teams and PostgreSQL";

describe("offline v1 smoke (MockStepExecutor)", () => {
it("runs all 7 stages and produces a schema-valid blueprint", async () => {
const executor = new MockStepExecutor();
const events: SSEEvent[] = [];

const blueprint = await runOrchestrator({
jobId: "smoke-success",
prompt: SCAFFOLD_PROMPT,
projectName: "smoke-app",
emit: (event) => events.push(event),
provider: forbiddenProvider,
cache: new AgentCache(),
executor,
});

expect(executor.executedStages).toEqual([
"planner", "schema", "api", "frontend", "devops", "reviewer", "codegen",
]);
expect(BlueprintSchema.safeParse(blueprint).success).toBe(true);
expect(blueprint.projectName).toBe("smoke-app");
expect(blueprint.generatedSourceFiles?.length ?? 0).toBeGreaterThan(0);
expect(events.filter((e) => e.type === "agent_completed")).toHaveLength(7);
expect(events.some((e) => e.type === "agent_failed")).toBe(false);
});

it("stops at the failing stage and reports it", async () => {
const executor = new MockStepExecutor({ failAt: "api" });
const events: SSEEvent[] = [];

const error = await runOrchestrator({
jobId: "smoke-failure",
prompt: SCAFFOLD_PROMPT,
projectName: "smoke-app",
emit: (event) => events.push(event),
provider: forbiddenProvider,
cache: new AgentCache(),
executor,
}).catch((err: unknown) => err);

expect(error).toBeInstanceOf(StepExecutionError);
expect((error as StepExecutionError).stage).toBe("api");
expect((error as StepExecutionError).stepId).toBe("step-3");
expect(executor.executedStages).toEqual(["planner", "schema", "api"]);
expect(events.filter((e) => e.type === "agent_completed").map((e) => (e as { agent: string }).agent))
.toEqual(["planner", "schema"]);
expect(events.filter((e) => e.type === "agent_failed")).toHaveLength(1);
});
});
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackforge/shared",
"version": "0.1.0",
"version": "1.0.0",
"private": true,
"type": "module",
"main": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackforge/ui",
"version": "0.1.0",
"version": "1.0.0",
"private": true,
"type": "module",
"main": "./src/index.ts",
Expand Down
Loading