Recorder saves thenable and cross-realm Promise tool results as synchronous empty objects

Open
#193 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@jihtsan is already working on this.

Since Oct 31, 2026.

  • #194 by @jihtsan — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
node.js, typescript

Research direction

Start at the createRecorder().tool() entry point and trace how tool results are classified, recorded, replayed, and tracked as in flight. Reproduce the listed native Promise, thenable, cross-realm Promise, sync, rejection, and overlap cases, then add controls for each behavior. Done means settled values and serialized rejections replay asynchronously, sync behavior is preserved, and concurrency markers remain correct; note that linked pull request #194 is already open.

Written by the indexing model from the issue text.

Description

Problem

On next (4c19918ea42e47d0e180a511961dd55c43aa614f), createRecorder().tool() misclassifies a custom thenable or a Promise from another realm as synchronous because runLive checks instanceof Promise.

Awaiting the live tool yields its result, but the recording saves {} with sync: true. A JSON round-trip followed by replay then yields {} instead of the original result. The same assumption in async tracking means overlapping tools can also lose their concurrent marker.

Reproduction

Node 24.22.1 and pinned pnpm 10.28.2. This was reproduced against the complete built @statelyai/agent/testing entry after a frozen-lockfile install and package build; the existing 30 equivalence tests pass on the unchanged base. No model calls, keys or network services are involved.

import { createRecorder } from '@statelyai/agent/testing';
import vm from 'node:vm';
for (const [name, fn] of [
 ['native', () => Promise.resolve({answer:42})],
 ['thenable', () => ({then(resolve) {resolve({answer:42});}})],
 ['other-realm-promise', () => vm.runInNewContext('Promise.resolve({answer:42})')]
]) {
 const record = createRecorder();
 const first = await record.tool('lookup', fn)();
 const recording = JSON.parse(JSON.stringify(record.recording));
 const replay = createRecorder({replay: recording});
 const second = await replay.tool('lookup', () => { throw new Error('must not execute'); })();
 console.log(JSON.stringify({name, first, second, recorded:recording.effects}));
}

Observed:

[
  {
    "name": "native",
    "first": {
      "answer": 42
    },
    "second": {
      "answer": 42
    },
    "recorded": [
      {
        "kind": "tool",
        "name": "lookup",
        "step": 0,
        "input": [],
        "output": {
          "answer": 42
        }
      }
    ]
  },
  {
    "name": "thenable",
    "first": {
      "answer": 42
    },
    "second": {},
    "recorded": [
      {
        "kind": "tool",
        "name": "lookup",
        "step": 0,
        "input": [],
        "sync": true,
        "output": {}
      }
    ]
  },
  {
    "name": "other-realm-promise",
    "first": {
      "answer": 42
    },
    "second": {},
    "recorded": [
      {
        "kind": "tool",
        "name": "lookup",
        "step": 0,
        "input": [],
        "sync": true,
        "output": {}
      }
    ]
  }
]

The native Promise control replays {answer:42}, while both other awaited tools replay {}. The module's record/replay contract says tools are answered from their recorded results without repeating side effects.

Expected scope

Record settled values and serialized rejections from promise-like tool results, including another realm's Promise, and replay those asynchronously. Preserve ordinary synchronous return/throw behavior and the existing error serialization rules. Count an asynchronous call as in-flight until settlement so overlap and later sequential calls retain correct markers. Include resolve/reject, native Promise, ordinary sync, and concurrency controls; do not make all tools async to solve the problem.

This concerns tool result classification and tracking; model stream-iteration errors are a separate behavior and not part of this proposal.

Dominant language
TypeScript
Stars
508
Forks
29
Avg merge
8h 54m
Merged PRs (30d)
74

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from statelyai/agent

All issues in statelyai/agent

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.