An open-source, faster Devin alternative
TypeScript
fix(flue): tolerate additive repository responses
Igor Zalutskiee683ff46a8a
diff --git a/sdks/flue/src/cf-env.ts b/sdks/flue/src/cf-env.tsindex c29a6ad2..0215e28b 100644--- a/sdks/flue/src/cf-env.ts+++ b/sdks/flue/src/cf-env.ts@@ -1,8 +1,9 @@// Ambient Cloudflare-Workers env access (design 013 §4). On the `flue build --target cloudflare`-// build the real Worker bindings (`OC_GATEWAY`, `OC_SESSION_TOKEN`, `OC_SANDBOX_*`, `OC_INGEST`, …)-// live on the AMBIENT env exported by `cloudflare:workers` — the same one Flue's generated entry reads-// (`import { env } from 'cloudflare:workers'`). The per-agent `ctx.env` Flue threads into the-// initializer is EMPTY for these bindings, so OC helpers must read the ambient env instead.+// build, plain Worker bindings (`OC_GATEWAY`, `OC_SANDBOX_*`, `OC_INGEST`, …) are available on the+// AMBIENT env exported by `cloudflare:workers` — the same one Flue's generated entry reads+// (`import { env } from 'cloudflare:workers'`). Worker secrets such as `OC_SESSION_TOKEN` can instead+// arrive through the runtime-owned env reference at request time. Helpers therefore retain that+// fallback reference and resolve it lazily, layering ambient plain bindings over its current values.//// `cloudflare:workers` only resolves inside workerd, so importing it statically would break local// `flue dev` on the node target and the package's own vitest. Load it lazily + guarded: on CF the@@ -23,8 +24,8 @@ try {/*** Resolve the effective OC env: the Cloudflare ambient bindings layered over `fallback` (ambient wins).- * On CF this returns the real Worker bindings even though `ctx.env` is empty; off CF (local dev / node /- * tests) it returns `fallback` unchanged so an explicitly-passed env still works.+ * On CF this adds ambient plain Worker bindings without discarding request-time values from `fallback`;+ * off CF (local dev / node / tests) it returns `fallback` unchanged.*/export function ocResolveEnv<T extends Record<string, unknown>>(fallback: T | undefined): T {const base = (fallback ?? {}) as T;diff --git a/sdks/flue/src/repo.test.ts b/sdks/flue/src/repo.test.tsindex e6af11c9..7c225585 100644--- a/sdks/flue/src/repo.test.ts+++ b/sdks/flue/src/repo.test.ts@@ -135,7 +135,7 @@ describe("ocRepoTools contract", () => {});describe("list_working_repos HTTP fixtures", () => {- it("resolves request-time env and encodes the session and query", async () => {+ it("reads the captured env lazily at tool run and encodes the request", async () => {const env: OcRepoEnv = {};const list = tool("list_working_repos", env, "ses_/customer space");const fetchSpy = vi.fn(async () =>@@ -146,9 +146,11 @@ describe("list_working_repos HTTP fixtures", () => {id: "repo_1",full_name: "diggerhq/example app",default_branch: "main",+ visibility: "private",},],truncated: true,+ server_revision: 2,}),);vi.stubGlobal("fetch", fetchSpy);@@ -215,7 +217,19 @@ describe("list_working_repos HTTP fixtures", () => {: {}),},};- vi.stubGlobal("fetch", vi.fn(async () => json(body, status)));+ vi.stubGlobal(+ "fetch",+ vi.fn(async () =>+ json(+ {+ ...body,+ server_trace_id: "trace_1",+ error: { ...body.error, provider_detail: "ignored" },+ },+ status,+ ),+ ),+ );await expect(tool("list_working_repos").run({ input: {} }),@@ -223,6 +237,61 @@ describe("list_working_repos HTTP fixtures", () => {},);+ it("accepts additive response growth without freezing list or message size", async () => {+ const repositories = Array.from({ length: 51 }, (_, index) => ({+ id: `repo_${index}`,+ full_name: `owner/repository-${index}`,+ default_branch: "main",+ future_field: index,+ }));+ const list = tool("list_working_repos");+ vi.stubGlobal(+ "fetch",+ vi+ .fn()+ .mockResolvedValueOnce(+ json({+ ok: true,+ repositories,+ truncated: false,+ next_cursor: null,+ }),+ )+ .mockResolvedValueOnce(+ json(+ {+ ok: false,+ error: {+ code: "github_unavailable",+ message: "x".repeat(501),+ retryable: true,+ future_recovery: "retry",+ },+ },+ 503,+ ),+ ),+ );++ await expect(list.run({ input: {} })).resolves.toEqual({+ ok: true,+ repositories: repositories.map(({ id, full_name, default_branch }) => ({+ id,+ full_name,+ default_branch,+ })),+ truncated: false,+ });+ await expect(list.run({ input: {} })).resolves.toEqual({+ ok: false,+ error: {+ code: "github_unavailable",+ message: "x".repeat(501),+ retryable: true,+ },+ });+ });+it("rejects status, retryability, and retry-after contract drift", async () => {const list = tool("list_working_repos");const fetchSpy = vi@@ -330,6 +399,7 @@ describe("add_source HTTP fixture", () => {ref: "main",sha: "a".repeat(40),path: "/workspace/sources/app",+ materialized_at: "2026-07-21T00:00:00Z",}),);vi.stubGlobal("fetch", fetchSpy);@@ -418,7 +488,9 @@ describe("github_publish_pull_request HTTP fixtures", () => {pull_request: {number: 42,url: "https://github.com/diggerhq/example-app/pull/42",+ state: "open",},+ provider_request_id: "request_1",});}),);@@ -435,9 +507,11 @@ describe("github_publish_pull_request HTTP fixtures", () => {signal: controller.signal,});- expect(result).toMatchObject({+ expect(result).toEqual({ok: true,no_changes: false,+ branch: "oc/ses_1/document-setup",+ commit: "d".repeat(40),pull_request: {number: 42,url: "https://github.com/diggerhq/example-app/pull/42",@@ -462,7 +536,13 @@ describe("github_publish_pull_request HTTP fixtures", () => {it("returns no_changes as a successful terminal result", async () => {vi.stubGlobal("fetch",- vi.fn(async () => json({ ok: true, no_changes: true })),+ vi.fn(async () =>+ json({+ ok: true,+ no_changes: true,+ compared_commit: "a".repeat(40),+ }),+ ),);await expect(tool("github_publish_pull_request").run({diff --git a/sdks/flue/src/repo.ts b/sdks/flue/src/repo.tsindex 8cba61bc..d713575f 100644--- a/sdks/flue/src/repo.ts+++ b/sdks/flue/src/repo.ts@@ -47,21 +47,25 @@ export type OcRepoFailure = {error: OcRepoError;};-const RepoErrorSchema = v.strictObject({+// Platform responses are forward-extensible: validate and return the known+// semantic fields while ignoring additive fields from a newer server. Tool+// inputs below remain strict and bounded so the model cannot smuggle extra+// authority or unbounded work.+const RepoErrorSchema = v.object({code: v.picklist(ERROR_CODES),- message: v.pipe(v.string(), v.minLength(1), v.maxLength(500)),+ message: v.pipe(v.string(), v.minLength(1)),retryable: v.boolean(),retry_after_seconds: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(600)),),});-const RepoFailureSchema = v.strictObject({+const RepoFailureSchema = v.object({ok: v.literal(false),error: RepoErrorSchema,});-const WorkingRepositorySchema = v.strictObject({+const WorkingRepositorySchema = v.object({id: v.pipe(v.string(), v.minLength(1)),full_name: v.pipe(v.string(), v.minLength(3)),default_branch: v.pipe(v.string(), v.minLength(1)),@@ -73,12 +77,9 @@ export interface WorkingRepository {default_branch: string;}-const ListWorkingReposSuccessSchema = v.strictObject({+const ListWorkingReposSuccessSchema = v.object({ok: v.literal(true),- repositories: v.pipe(- v.array(WorkingRepositorySchema),- v.maxLength(50),- ),+ repositories: v.array(WorkingRepositorySchema),truncated: v.boolean(),});@@ -108,7 +109,7 @@ const PullRequestBaseSchema = v.pipe();const AddSourceSuccessSchema = v.pipe(- v.strictObject({+ v.object({ok: v.literal(true),name: SourceNameSchema,repository_id: v.pipe(v.string(), v.startsWith("repo_")),@@ -123,12 +124,12 @@ const AddSourceSuccessSchema = v.pipe(),);-const PublishNoChangesSchema = v.strictObject({+const PublishNoChangesSchema = v.object({ok: v.literal(true),no_changes: v.literal(true),});-const PublishPullRequestSuccessSchema = v.strictObject({+const PublishPullRequestSuccessSchema = v.object({ok: v.literal(true),no_changes: v.literal(false),branch: v.pipe(v.string(), v.minLength(1)),@@ -136,7 +137,7 @@ const PublishPullRequestSuccessSchema = v.strictObject({v.string(),v.regex(/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i),),- pull_request: v.strictObject({+ pull_request: v.object({number: v.pipe(v.number(), v.integer(), v.minValue(1)),url: v.pipe(v.string(), v.url()),}),@@ -429,9 +430,10 @@ function randomIdempotencyKey(): string {/*** Standard hosted-repository tools for an OpenComputer Flue session.*- * The initializer captures only the session id and environment reference.- * Managed bindings are resolved inside each tool run because Cloudflare- * secrets are request-scoped.+ * The initializer captures only the session id and the runtime-owned+ * environment reference, not a spread/snapshot. Managed bindings are resolved+ * inside each tool run so request-time secrets on that reference are visible;+ * ambient plain bindings are layered in by `ocResolveEnv`.*/export function ocRepoTools(ctx: AgentInitializerContext<OcRepoEnv>,diff --git a/sdks/flue/src/sandbox.ts b/sdks/flue/src/sandbox.tsindex ea1476cb..9dfc729a 100644--- a/sdks/flue/src/sandbox.ts+++ b/sdks/flue/src/sandbox.ts@@ -266,10 +266,10 @@ async function resolveSandboxId(* The optional OC-fleet sandbox factory. Add `sandbox: ocSandbox(env)` only when an agent needs a* Linux shell or durable files. The default starter intentionally omits it.*- * Reads the CF ambient env (`cloudflare:workers`), not the passed `ctx.env`: on the `--target- * cloudflare` build the real `OC_SANDBOX_*`/`OC_SESSION_TOKEN` bindings live on the ambient env and- * `ctx.env` is empty for them (same reason as `useOcGateway`). `createSessionEnv` validates local- * configuration and returns a lazy environment without fetching or provisioning anything.+ * Resolves the captured runtime env lazily and layers in plain bindings from the CF ambient env+ * (`cloudflare:workers`). The request-time `OC_SESSION_TOKEN` remains on the captured env reference;+ * it is not assumed to exist in the ambient snapshot. `createSessionEnv` validates local configuration+ * and returns a lazy environment without fetching or provisioning anything.*/export function ocSandbox(env: OcSandboxEnv,