Skip to content

[REPL-36898] feat(codemode): support .then, .catch and .finally on promises - #76

Draft
xinreplo wants to merge 1 commit into
devfrom
codemode-promise-chaining
Draft

xinreplo wants to merge 1 commit into
devfrom
codemode-promise-chaining

Conversation

@xinreplo

@xinreplo xinreplo commented Dec 2, 2026 •

Copy link
Copy Markdown

The agent's code sandbox now runs programs that use .then(), .catch() or .finally() on a tool call instead of rejecting them. When a customer asks Replo's agent for store data, the agent writes a short JavaScript program that calls integrations such as Shopify; about 70 of those calls from about 50 people failed in Nov 3–30 only because the model wrote .catch(() => null), which the sandbox refused. A prompt rule against it has been in the agent's instructions since Sep 27 and the model still writes it, so the sandbox should accept it.

Context

  • Linear: REPL-36898 — the widest app integration failure in the Nov 30 MCP & API report (50 people, more than the 31 hit by Shopify sales totals).
  • Proposal with the evidence: https://admin.replo.app/team-agent-artifacts/37645238-fbe8-496e-aa20-11659de45340
  • Next: release this as a patch tag and bump the three OPENCODE_VERSION pins in andytown (.agents/skills/agent-tooling/release-opencode.md). A companion andytown PR makes the daily product report stop hiding this error so any return shows up.

How it works

A .then/.catch/.finally call returns a derived promise: a promise whose handler runs once, when something first waits on it (await, Promise.all, or the end-of-program drain that already waits for fire-and-forget calls). Handlers do not get their own fibers (the Effect library's lightweight threads), because the interpreter keeps one shared stack of variable scopes and two handlers interleaving would read each other's variables. Tool calls a handler starts still run in parallel on their own fibers.

Case Before After Why
await tools.x().catch(() => null) UnsupportedSyntax error data, or null on failure the pattern models write
Promise.all(items.map(i => tools.a(i).then(r => tools.b(r)))) error all b calls overlap handlers run for every item before joining
Promise.race over chains error races each chain's first call, then runs only the winner's handlers keeps handlers sequential
.catch on an un-awaited call error counts as handled, no "unhandled rejection" matches JS

Review closely: chainPromise and the race change in packages/codemode/src/interpreter/runtime.ts. Known gap: handlers run when awaited rather than on a microtask, so a program that reads a variable a never-awaited .then writes, before the program ends, sees the old value. Real JS has the same race in practice since the tool call is asynchronous.

Testing Done

  • bun test in packages/codemode: 285 pass, including 15 new tests for .then, .catch, .finally, chains, assimilation of a returned tool call, run-once memoization, Promise.all/allSettled/race, fire-and-forget chains, and a non-function handler.
  • bun typecheck across the repo (pre-push hook) passes; oxlint packages/codemode reports 0 errors.
  • Not yet run in a real sandbox: that needs a release build and the pin bump.

🤖 Generated with Claude Code


Summary by cubic

The agent's code sandbox now runs programs that use .then(), .catch() and .finally() on tool‑call promises instead of rejecting them with an UnsupportedSyntax error.

Promises chained this way behave like JS: handlers run once when the promise is first awaited or joined, Promise.all/allSettled start every chain's handler before joining so second‑stage tool calls overlap, and Promise.race races each chain's root call, running only the winner's handlers. A handler that returns a tool call is assimilated, missing handlers pass the settlement through, and a .catch attached to an un‑awaited call counts as handled. This addresses REPL-36898, the widest app integration failure in the Nov 30 MCP & API report.

Known gap

  • Handlers run when awaited, not on a microtask, so a never‑awaited chain's side effects are only visible by the run's end; real JS has the same race because tool calls are async.

Written for commit 63ad5ab. Summary will update on new commits.

View guided diff Turn on auto-fix

Models keep writing tools.x(...).catch(() => null) despite a prompt rule against it,
and the sandbox rejected every such program. Chained promises now run like JS:
handlers run once when the promise is first observed, Promise.all/allSettled start
every member's handler before joining so second-stage calls overlap, and race
races each chain's root call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Hey! Your PR title [REPL-36898] feat(codemode): support .then, .catch and .finally on promises doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant