agentleFS
MicrosoftKnown publishermicrosoft/duroxide-node/.github/copilot-instructions.md

duroxide-node

Copilot instructions36 starsChanged 57 days agoMITView on GitHub

🚨 STOP: DO NOT git commit OR git push WITHOUT EXPLICIT USER PERMISSION 🚨 Always ask before committing or pushing. Never assume. When making changes to this SDK, always check ../duroxide-python for symmetry: 1. Bug fixes: If you fix a bug here, apply the same fix in duroxide-python (and vice versa) 2. API parity: Every public API in one SDK must have an equivalent in the other (adjusted for language idioms β€” camelCase in JS, snake_case in Python) 3. Test…

  • Reads credentials
  • Commits and pushes

The file

Show its rules and text

We split this copilot-instructions.md into the rules it gives an agent

16 rules. About 0 are common, 3 are distinctive, and 13 are very specific to this project.

  • β‰ˆ0

    Common Β· estimate

    Many files like this have a rule of this kind.

  • β‰ˆ3

    Distinctive Β· estimate

    This author's own choices. Worth a look.

  • 13

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When making changes to this SDK, always check ../duroxide-python for symmetry:

    line 9
  2. Distinctive:

    Bug fixes: If you fix a bug here, apply the same fix in duroxide-python (and vice versa)

    line 10
  3. Distinctive:

    API parity: Every public API in one SDK must have an equivalent in the other (adjusted for language idioms β€” camelCase in JS, snake_case in …

    line 11
  4. Distinctive:

    Test parity: Every test in one SDK must have a corresponding test in the other

    line 12
  5. Very specific:

    Orchestrations: use function* (generators) and yield for all durable operations

    line 45
  6. Very specific:

    Activities: use async function and await (normal async)

    line 46
  7. Very specific:

    Important: After any Rust code change (src/*.rs), you MUST run npx napi build --platform before running tests.

    line 95
  8. Very specific:

    After adding features: update docs/user-guide.md, relevant skills in .agents/skills/, and the ScheduledTask table above.

    line 184
  9. Very specific:

    Add variant to ScheduledTask enum in src/types.rs

    line 187
  10. Very specific:

    Add execution branch in execute_task() in src/handlers.rs

    line 188
  11. Very specific:

    Add JS method to OrchestrationContext class in lib/duroxide.js

    line 189
  12. Very specific:

    Add TypeScript type to index.d.ts

    line 190
  13. Very specific:

    Add test in __tests__/e2e.test.js

    line 191
  14. Very specific:

    Add #[napi] function in src/lib.rs (or relevant module)

    line 194
  15. Very specific:

    Import in lib/duroxide.js from the napi binding

    line 195
  16. Very specific:

    All JS callbacks run on the Node.js main thread (single-threaded)

    line 203

β‰ˆ β€œCommon” is an estimate: at least 1 in 5 of the 1,644 Copilot instructions files we read have a rule of the same kind, not the same words. β€œVery specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
copilot-instructions.md

duroxide-node β€” AI Coding Assistant Instructions

🚨 STOP: DO NOT git commit OR git push WITHOUT EXPLICIT USER PERMISSION 🚨

Always ask before committing or pushing. Never assume.

⚠️ SDK Symmetry β€” duroxide-node & duroxide-python MUST stay in sync

When making changes to this SDK, always check ../duroxide-python for symmetry:

  1. Bug fixes: If you fix a bug here, apply the same fix in duroxide-python (and vice versa)
  2. API parity: Every public API in one SDK must have an equivalent in the other (adjusted for language idioms β€” camelCase in JS, snake_case in Python)
  3. Test parity: Every test in one SDK must have a corresponding test in the other
  4. Feature parity: New duroxide features exposed in one SDK must be exposed in both
  5. Rust handler code (src/handlers.rs): Both SDKs share the same interop architecture β€” execute_task, make_select_future, make_join_future should be kept structurally identical

Project Overview

duroxide-node is a Node.js/TypeScript SDK for the Duroxide durable execution runtime. It wraps the Rust runtime via napi-rs, exposing generator-based orchestrations and async activities to JavaScript.

Key docs: docs/architecture.md, docs/user-guide.md.

Architecture

lib/duroxide.js          ← JS wrapper: generator driver, OrchestrationContext, ActivityContext
src/lib.rs               ← napi entry point, trace functions
src/handlers.rs          ← Core interop: orchestration handler loop, activity invocation,
                           global context maps, select/race/join, activity cancellation
src/types.rs             ← ScheduledTask enum (activity, timer, wait, orchestration, etc.)
src/runtime.rs           ← JsRuntime (wraps duroxide::Runtime)
src/client.rs            ← JsClient (wraps duroxide::Client, including admin/management APIs)
src/provider.rs          ← JsSqliteProvider
src/pg_provider.rs       ← JsPostgresProvider
__tests__/e2e.test.js    ← PostgreSQL e2e tests (24 tests + 1 SQLite smoketest)
__tests__/races.test.js  ← ctx.all() and ctx.race() with mixed task types + cancellation (7 tests)
__tests__/admin_api.test.js ← Admin/management API tests (14 tests)
__tests__/sessions.test.js ← Session support tests (6 tests, SQLite in-memory)
__tests__/scenarios/     ← Scenario tests modeling real-world patterns (toygres, 6 tests)

⚠️ Critical: Yield vs Await

This is the most important design constraint. Get this wrong and replay breaks.

  • Orchestrations use function* (generators) and yield for all durable operations
  • Activities use async function and await (normal async)
  • Tracing methods (ctx.traceInfo() etc.) are fire-and-forget β€” no yield needed
// βœ… Orchestration: generator, yield for durable operations
runtime.registerOrchestration('MyWorkflow', function* (ctx, input) {
  ctx.traceInfo('starting');                                     // no yield β€” fire-and-forget
  const result = yield ctx.scheduleActivity('Work', input);      // yield β€” durable
  return result;
});

// βœ… Activity: async function, await for I/O
runtime.registerActivity('Work', async (ctx, input) => {
  ctx.traceInfo(`processing ${input}`);
  const data = await fetch(input.url);
  return data;
});

Why generators? The Rust replay engine needs full control over each step β€” it calls poll_once() on DurableFutures and feeds cached results during replay. await would yield control to the JS event loop, breaking the replay engine's synchronous step-by-step model. See docs/architecture.md for the full explanation.

⚠️ Critical: Tracing Delegates to Rust

Tracing in both orchestrations and activities delegates to Rust contexts via global HashMaps. Do NOT reimplement tracing in JS.

  • Orchestration tracing: orchestrationTraceLog(ctxToken, level, message) β†’ looks up OrchestrationContext from ORCHESTRATION_CTXS map β†’ calls ctx.trace() which handles is_replaying suppression
  • Activity tracing: activityTraceLog(token, level, message) β†’ looks up ActivityContext from ACTIVITY_CTXS map β†’ calls ctx.trace_info() etc. with full structured fields

The is_replaying guard lives entirely on the Rust side. JS never knows about replay state.

Build & Test

# Build (MUST use napi build, not cargo build, for the .node binary)
npx napi build --platform              # Debug build
npx napi build --platform --release    # Release build

# Tests
npm test                               # PostgreSQL e2e (24 tests + 1 SQLite smoketest, needs DATABASE_URL in .env)
npm run test:races                     # Race/join composition tests (7 tests, needs DATABASE_URL)
npm run test:admin                     # Admin API tests (14 tests, needs DATABASE_URL)
npm run test:scenarios                 # Scenario tests (6 tests, needs DATABASE_URL)
npm run test:sessions                  # Session tests (6 tests, SQLite in-memory)
npm run test:all                       # Everything (58 tests)

# Lint the Rust side
cargo clippy --all-targets

Important: After any Rust code change (src/*.rs), you MUST run npx napi build --platform before running tests. The JS side loads a compiled .node binary, not Rust source.

Interop Model

The orchestration handler loop in src/handlers.rs:

  1. Rust calls JS createGenerator(payload) via ThreadsafeFunction (blocked with block_in_place)
  2. JS creates a generator, calls gen.next(), returns the first yielded ScheduledTask descriptor
  3. Rust executes the task via OrchestrationContext (real execution or replay)
  4. Rust calls JS nextStep(result) β€” feeds result back to generator
  5. Repeat until generator returns or throws

Activities are simpler: Rust calls JS async function via call_async, awaits the Promise.

block_in_place is required because the replay engine's poll_once() drops the handler future after one poll. A regular call_async future would be dropped before the JS callback fires.

ScheduledTask Protocol

The JS generator yields plain objects that map to Rust ScheduledTask variants:

JS DescriptorRust VariantNotes
{ type: 'activity', name, input }ActivityRuns an activity
{ type: 'activity', name, input, sessionId }ActivityRuns an activity on a session
{ type: 'activityWithRetry', name, input, retryPolicy }ActivityWithRetryWith retry policy
{ type: 'timer', delayMs }TimerDurable delay
{ type: 'wait', eventName }WaitEventWait for external event
{ type: 'subOrchestration', name, input }SubOrchestrationAwait child workflow
{ type: 'orchestration', name, instanceId, input }OrchestrationFire-and-forget
{ type: 'all', tasks }AllParallel execution
{ type: 'race', task1, task2 }RaceFirst-to-complete
{ type: 'utcNow' }UtcNowDeterministic timestamp
{ type: 'newGuid' }NewGuidDeterministic GUID
{ type: 'continueAsNew', input }ContinueAsNewRestart orchestration
{ type: 'continueAsNewVersioned', input, version }ContinueAsNewVersionedRestart as specific version

Key Patterns

Provider polymorphism β€” napi-rs can't use trait objects in constructors, so JsRuntime has a factory method:

// JS wrapper detects provider._type and calls the right constructor
if (provider._type === 'postgres') {
  this._native = JsRuntime.fromPostgres(provider._native, options);
} else {
  this._native = new JsRuntime(provider._native, options);
}

Global context maps for cross-thread tracing:

  • ACTIVITY_CTXS: HashMap<String, ActivityContext> β€” keyed by atomic token (act-0, act-1, ...)
  • ORCHESTRATION_CTXS: HashMap<String, OrchestrationContext> β€” keyed by atomic token (orch-0, orch-1, ...), one per invocation. Never key it by instance_id: two replays of one instance can be alive in the same process, and each must only reach its own context. JS gets the token in ctxInfo._ctxToken.
  • Contexts are inserted before calling JS, removed after completion

select/race uses make_select_future() which returns Pin<Box<dyn Future<Output = Result<String, String>>>> to handle all task types uniformly. select_result() returns { index, value } for a winner that succeeded, and the winner's own error for one that failed, so the yield throws.

join/all uses make_join_future() β€” similar to make_select_future() but normalizes output to {ok:v}/{err:e} JSON. Supports all task types: activities, timers, waits, sub-orchestrations (all variants).

Nested join/select rejection β€” Join and Select handlers reject nested Join/Select tasks to avoid recursive async issues.

Activity cancellation β€” ctx.isCancelled() checks the Rust CancellationToken via ACTIVITY_CTXS map. Cancellation is detected via lock renewal failure (latency = workerLockTimeoutMs / 2). A missing map entry also means cancelled: the runtime removes the entry when it gives up on a cancelled activity after the grace period, while the JS function is still running.

Activity client access β€” ctx.getClient() calls activityGetClient(token) napi function β†’ looks up ActivityContext in ACTIVITY_CTXS map β†’ calls ctx.get_client() β†’ wraps result as JsClient. Activities can use this to start orchestrations, raise events, etc.

Metrics snapshot β€” runtime.metricsSnapshot() returns a JsMetricsSnapshot with 17 counters (orch starts/completions/failures, activity results, dispatcher stats, provider errors). Returns null if observability is not enabled.

Observability options β€” JsRuntimeOptions includes logFormat, logLevel, serviceName, serviceVersion which map to duroxide::ObservabilityConfig.

Determinism Rules

Orchestration generators must be deterministic (same rules as Rust duroxide):

  • βœ… Use: yield ctx.utcNow(), yield ctx.newGuid(), ctx.traceInfo()
  • ❌ Avoid: Date.now(), Math.random(), crypto.randomUUID(), console.log(), I/O, fetch()

Activities have no restrictions β€” they run once, result is cached.

Crate Version Alignment

Release manifests must resolve duroxide and duroxide-pg from crates.io:

duroxide = { version = "0.1.30", features = ["sqlite"] }
duroxide-pg = "0.1.35"

Temporary local [patch.crates-io] sections or inline path = overrides are acceptable during cross-repository development, but remove them before release validation so CI and published packages use registry crates.

Adding Features

After adding features: update docs/user-guide.md, relevant skills in .agents/skills/, and the ScheduledTask table above.

New ScheduledTask type:

  1. Add variant to ScheduledTask enum in src/types.rs
  2. Add execution branch in execute_task() in src/handlers.rs
  3. Add JS method to OrchestrationContext class in lib/duroxide.js
  4. Add TypeScript type to index.d.ts
  5. Add test in __tests__/e2e.test.js

New napi function:

  1. Add #[napi] function in src/lib.rs (or relevant module)
  2. Import in lib/duroxide.js from the napi binding
  3. Rebuild: npx napi build --platform

Limitations

  • ctx.race() supports exactly 2 tasks (maps to Rust select2)
  • ctx.all() supports all task types: activities, timers, waits, sub-orchestrations (all variants). Nested all()/race() is rejected.
  • ctx.race() supports all task types. Nested all()/race() is rejected.
  • All JS callbacks run on the Node.js main thread (single-threaded)
  • No async function* generators β€” breaks replay model
  • SQLite may hit "database is locked" under concurrency (retried automatically)
  • Runtime.shutdown(timeoutMs) waits the full timeout (no short-circuit)
  • Platform-specific .node binary β€” cross-platform needs per-platform builds
  • Activity cancellation detection latency depends on workerLockTimeoutMs (default 30s β†’ ~15s detection)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.