From 4ebb64aa851dcd605fa74ea47d818ca0114d4ffc Mon Sep 17 00:00:00 2001 From: Brendan Asselstine Date: Fri, 11 Sep 2026 16:39:33 -0700 Subject: [PATCH] Add flow workspaces and deployment automation --- .github/workflows/auto-merge.yml | 31 ++++++++++++++++++++++++++++ .github/workflows/ci.yml | 8 ++++++++ .github/workflows/deploy.yml | 35 ++++++++++++++++++++++++++++++++ src/tenant.ts | 23 +++++++++++++++------ src/types.ts | 2 +- src/ui.ts | 2 +- src/workspace.ts | 7 +++++++ test/ui.test.ts | 2 ++ test/workspace.test.ts | 7 ++++++- 9 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/auto-merge.yml create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/auto-merge.yml b/.github/workflows/auto-merge.yml new file mode 100644 index 00000000..c1c979b2 --- /dev/null +++ b/.github/workflows/auto-merge.yml @@ -0,0 +1,31 @@ +name: Auto-merge + +on: + pull_request_target: + branches: + - main + types: + - opened + - reopened + - synchronize + - ready_for_review + +permissions: + contents: read + +jobs: + enable: + name: Enable auto-merge + if: github.event.pull_request.draft == false + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + # This workflow intentionally does not check out or execute PR code. + - name: Merge after required checks pass + env: + # A fine-grained PAT or GitHub App token is required here. The built-in + # GITHUB_TOKEN cannot add PRs to a merge queue and can suppress + # downstream push-triggered workflows such as production deployment. + GH_TOKEN: ${{ secrets.AUTOMERGE_TOKEN }} + PR_URL: ${{ github.event.pull_request.html_url }} + run: gh pr merge --auto --squash "$PR_URL" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9e6e09b3..7b169036 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,7 +2,15 @@ name: CI on: push: + branches: + - main pull_request: + branches: + - main + # Run required checks again against GitHub's speculative merge-queue commit. + merge_group: + types: + - checks_requested workflow_dispatch: permissions: diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..cb02756c --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,35 @@ +name: Deploy to Cloudflare + +on: + push: + branches: + - main + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: production + cancel-in-progress: false + +jobs: + deploy: + name: Deploy Worker + runs-on: ubuntu-latest + timeout-minutes: 15 + environment: production + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + - run: npm ci + - run: npm run build:css + - name: Deploy Worker + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: deploy diff --git a/src/tenant.ts b/src/tenant.ts index da3f4a21..d65ab1a3 100644 --- a/src/tenant.ts +++ b/src/tenant.ts @@ -5,7 +5,7 @@ import { agentOutputCommand, agentStatusCommand, connectionCheckCommand, default import { LINEAR_ISSUE_PROJECT_QUERY, LINEAR_OPTION_QUERIES, LINEAR_PROJECTS_QUERY } from "./linear"; import { matchingIssue } from "./matcher"; import type { Env, ExeConnectionInput, FilterType, MatchRule, PipeInput, RunState } from "./types"; -import { workspaceNameFor } from "./workspace"; +import { workingDirectoryFor, workspaceNameFor } from "./workspace"; type Row = Record; const json = (value: unknown) => Response.json(value); @@ -191,13 +191,13 @@ export class Tenant extends DurableObject { if (!linear) throw new Error("Connect Linear first"); const exe = await this.exeConnection(input.exeConnectionId); if (!exe) throw new Error("Connect Herdr first"); - if (!input.cwd) throw new Error("Choose a working directory."); + const cwd = this.cwdTemplate(input.cwd, flowId); const workspaceName = flowId; // These legacy fields are retained for compatibility with the v1 Durable Object schema. // App-wide webhook verification is performed at the Worker edge before reaching this object. this.ctx.storage.sql.exec( "INSERT INTO pipes (id,name,project_id,team_id,filter_type,filter_target_id,max_concurrency,capability,webhook_id,signing_secret,workspace_name,agent_kind,context_template,match_rules,exe_connection_id,cwd,created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", - pipeId, input.name, input.projectId, "", rules[0].type, rules[0].targetId, maxConcurrency, "app-webhook", null, "app-webhook", workspaceName, exe.agentKind, this.contextTemplate(input.contextTemplate), JSON.stringify(rules), input.exeConnectionId || "default", input.cwd, now(), + pipeId, input.name, input.projectId, "", rules[0].type, rules[0].targetId, maxConcurrency, "app-webhook", null, "app-webhook", workspaceName, exe.agentKind, this.contextTemplate(input.contextTemplate), JSON.stringify(rules), input.exeConnectionId || "default", cwd, now(), ); return Response.json({ id: pipeId }, { status: 201 }); } @@ -212,10 +212,10 @@ export class Tenant extends DurableObject { const workspaceName = flowId; const exe = await this.exeConnection(input.exeConnectionId); if (!exe) throw new Error("Choose an exe.dev connection."); - if (!input.cwd) throw new Error("Choose a working directory."); + const cwd = this.cwdTemplate(input.cwd, flowId); this.ctx.storage.sql.exec( "UPDATE pipes SET name=?, project_id=?, filter_type=?, filter_target_id=?, max_concurrency=?, workspace_name=?, agent_kind=?, context_template=?, match_rules=?, exe_connection_id=?, cwd=? WHERE id=?", - input.name, input.projectId, rules[0].type, rules[0].targetId, maxConcurrency, workspaceName, exe.agentKind, this.contextTemplate(input.contextTemplate), JSON.stringify(rules), input.exeConnectionId || "default", input.cwd, pipeId, + input.name, input.projectId, rules[0].type, rules[0].targetId, maxConcurrency, workspaceName, exe.agentKind, this.contextTemplate(input.contextTemplate), JSON.stringify(rules), input.exeConnectionId || "default", cwd, pipeId, ); return json({ id: pipeId, ok: true }); } @@ -420,7 +420,7 @@ export class Tenant extends DurableObject { private async connectionForPipe(pipe: Row): Promise { const connection = await this.exeConnection(String(pipe.exe_connection_id || "default")); - return connection && pipe.cwd ? { ...connection, cwd: String(pipe.cwd) } : connection; + return connection && pipe.cwd ? { ...connection, cwd: workingDirectoryFor(String(pipe.cwd), String(pipe.workspace_name)) } : connection; } private async upsertMember(input: unknown): Promise { @@ -473,6 +473,17 @@ export class Tenant extends DurableObject { return value; } + private cwdTemplate(value: unknown, flowId: string): string { + if (typeof value !== "string" || !value.trim()) throw new Error("Choose a working directory."); + try { + Mustache.parse(value); + if (!workingDirectoryFor(value, flowId).trim()) throw new Error(); + } catch { + throw new Error("Working directory must be a valid Mustache template that renders to a path."); + } + return value; + } + private matchRules(input: PipeInput): MatchRule[] { const rules = Array.isArray(input.matchRules) && input.matchRules.length ? input.matchRules : input.filterType && input.filterTargetId ? [{ type: input.filterType, targetId: input.filterTargetId }] : []; const validTypes: FilterType[] = ["owner", "creator", "status", "label", "assignee"]; diff --git a/src/types.ts b/src/types.ts index f74c598f..119cae0e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,7 +30,7 @@ export interface PipeInput { contextTemplate?: string; /** Saved exe.dev connection selected for this flow. */ exeConnectionId?: string; - /** Working directory is flow-specific even when the VM is shared. */ + /** Mustache template for the working directory; receives `flowId`. */ cwd?: string; } diff --git a/src/ui.ts b/src/ui.ts index e908e76c..e4a85f0d 100644 --- a/src/ui.ts +++ b/src/ui.ts @@ -57,7 +57,7 @@ export const flowPage = (viewer: Viewer, editFlowId?: string) => viewer ? shell(
2

Exe.dev config

Checking…

Connect to the VM where your working directory lives.

-
+
diff --git a/src/workspace.ts b/src/workspace.ts index b0ed1bf9..5b7b6483 100644 --- a/src/workspace.ts +++ b/src/workspace.ts @@ -1,4 +1,11 @@ +import Mustache from "mustache"; + /** Turns a human flow name into a short, predictable Herdr workspace label. */ export function workspaceNameFor(value: string): string { return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 48) || "flow"; } + +/** Renders a flow-specific working directory while preserving literal paths. */ +export function workingDirectoryFor(template: string, flowId: string): string { + return Mustache.render(template, { flowId }); +} diff --git a/test/ui.test.ts b/test/ui.test.ts index c24bfe01..47cf4602 100644 --- a/test/ui.test.ts +++ b/test/ui.test.ts @@ -41,6 +41,8 @@ describe("pages", () => { expect(html).toContain('id="exe-connection"'); expect(html).toContain("Connections save an SSH destination and its restricted HTTPS token"); expect(html).toContain("This is chosen per flow"); + expect(html).toContain("/home/exedev/projects/{{{flowId}}}"); + expect(html).toContain("as a Mustache variable"); expect(html).toContain("Test Connection"); expect(html).toContain('id="exe-test-status"'); expect(html).toContain("Connection successful"); diff --git a/test/workspace.test.ts b/test/workspace.test.ts index 006864e8..5e953df5 100644 --- a/test/workspace.test.ts +++ b/test/workspace.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vitest"; import { agentStatusCommand, connectionCheckCommand, defaultAgentCommand, herdrAgentStatus, shellAtom, startAgentCommand } from "../src/exe"; -import { workspaceNameFor } from "../src/workspace"; +import { workingDirectoryFor, workspaceNameFor } from "../src/workspace"; describe("flow workspaces", () => { it("uses a harmless SSH round trip for connection tests", () => { @@ -12,6 +12,11 @@ describe("flow workspaces", () => { expect(workspaceNameFor("!!!")).toBe("flow"); }); + it("renders a flow-specific working directory from its flow ID", () => { + expect(workingDirectoryFor("/repos/{{{flowId}}}", "bug-fixes")).toBe("/repos/bug-fixes"); + expect(workingDirectoryFor("/repos/shared", "bug-fixes")).toBe("/repos/shared"); + }); + it("passes the saved flow workspace to Herdr", () => { const command = startAgentCommand("bug-fixes-1", { vmName: "vm", apiToken: "token", agentKind: "codex", cwd: "/repo" }, "fix it", "bug-fixes-1"); expect(command).toContain("--label 'bug-fixes-1'");