npm.io
0.4.1-main.20261128T204842Z-935a3ab • Published 41m ago

iterate

Licence
Apache-2.0
Version
0.4.1-main.20261128T204842Z-935a3ab
Deps
0
Vulns
0
Weekly
0
Stars
3

iterate

The SDK for Iterate: context APIs, stream processors, reactive clients, React bindings, and OAuth app sessions, under iterate/*. The package exports source in this workspace and compiled JavaScript with declarations when packed. Its iterate bin is the CLI (src/cli/README.md).

npx iterate deploy puts a mind into your own Cloudflare account. The CLI has no erase command: the stack's script erases a mind, from the source and the config that the deploy left in ${XDG_CACHE_HOME:-~/.cache}/iterate (core/core/alchemy and deploys/<stage>/config.json): ITERATE_MIND_CONFIG=<that config> pnpm run destroy --stage <stage> --yes in that folder, with a Cloudflare token of the mind's account. It cannot be undone. The CLI README's "Erase a mind" has the steps and what a destroy leaves.

The SDK/platform line

The SDK holds what a mind's own code and apps run or speak: a loaded worker, a facet, a processor, a browser or Node client, and the wire contract between them and the platform. The Workers of a mind's stack (core/*-worker, with their shared kernel in core/itx) are its first user. Each subpath in package.json's exports is one public module; nothing else is importable.

Code that only a Worker of the stack runs belongs in that Worker or in core/itx, not here. Nothing in core/ imports outside core/ (core/AGENTS.md; lint/oxlintrc-core-boundary.test.ts pins it).

One path per symbol

Code the platform loads for a mind (the config entrypoint, a facet, a worker behind a rewrite rule) imports each symbol from one path, and the loader links this deployment's own build of it:

Path What it holds
iterate/sdk The workerd hosts: IterateConfigEntrypoint, FacetDurableObject, StreamProcessorDurableObject, their types, and capnweb's constructors
iterate/stream/processor A processor's surface: StreamProcessor, defineProcessorContract, LiveState, the event and contract types. It runs in Node too
iterate/stream/contract defineProcessorContract and the contract types alone, which iterate/stream/processor re-exports. Code a browser loads imports it: the engine needs node:async_hooks
iterate/email The email facet's contract, email/received and email/sent and the threads they fold into; and how a config asks for a received mail to be sent on (email/forward-requested, answered by email/forwarded or email/forward-failed)
iterate/agents The agents app: the collection's class, which a config's agents.ts re-exports and tells which agent to start; iterate/agents/install installs it; iterate/agents/kinds is kinds of agent (its README)
zod zod, one copy per isolate, so a schema user code makes is the one the SDK checks
import { StreamProcessorDurableObject } from "iterate/sdk";
import { StreamProcessor, defineProcessorContract } from "iterate/stream/processor";
import { z } from "zod";

const Contract = defineProcessorContract({
  slug: "hoarder",
  version: "1.0.0",
  description: "keeps every blob it reduces",
  stateSchema: z.object({ blobs: z.array(z.string()).default([]) }),
  consumes: ["blob"],
  emits: [],
});
class Hoarder extends StreamProcessor {
  contract = Contract;
  reduce({ event, state }) {
    return { blobs: [...state.blobs, event.payload.blob] };
  }
}
export class HoarderDurableObject extends StreamProcessorDurableObject {
  processor = new Hoarder();
}

Reaching the context from loaded code

Loaded code reaches its context with using itx = this.getItx(): when the block ends, the scope, every call made through it and every handle it awaited are released. Every loaded WorkerEntrypoint has getItx, and so do the SDK's hosts (IterateConfigEntrypoint, FacetDurableObject, StreamProcessorDurableObject).

import { IterateConfigEntrypoint } from "iterate/sdk";

export default class extends IterateConfigEntrypoint {
  async fetch() {
    using itx = this.getItx();
    const { mindSlug } = await itx.whoami();
    return new Response(`Homepage of ${mindSlug}`);
  }
}

Bind each scope with using in the smallest block that holds its calls, await every call inside it, and hand data, not handles, out of it: a kept scope, step or handle keeps the context, and any facet holding it, resident after the mind goes idle. An object that needs reach takes an accessor (() => this.getItx()), never a scope; work that outlives the call runs under a processor's runInBackground claim. Lint refuses a raw ITX.get() and a getItx() no using binds in this repository (iterate/no-raw-itx-get).

A config worker's getItx() is the root's scope whoever called it: every context linked to the root inherits the root's rules, so a script in /child that calls itx.voice.setupVoiceAgent(…) runs the worker as the root. Inside any method but fetch, this.callerPath() names the context the call came from (/child), as the platform stamps it, so what the worker creates for the caller can hold no more than the caller does:

async setupVoiceAgent({ streamPath }) {
  using itx = this.getItx();
  await itx.agents.at(this.callerPath()).create(streamPath); // linked to the caller, not to `/`
}

Who wrote an event

Anyone in a mind can append any event to any context, and every event says where it came from. The platform stamps source.origin, the context whose code or session wrote it, beside source.principal (the member) and source.platform. A writer's own values are dropped. The one field a writer keeps is source.processor, the engine's label for which processor wrote it.

// A script in /agents/a:
await itx.cd("/agents/b").append({
  type: "events.iterate.com/agent/context-added",
  payload: { role: "user", content: "hello" },
});
// Stamped { origin: "/agents/a" }. Agent b's model reads "[from /agents/a] hello",
// and the same from `itx.agents.get("/agents/b").message("hello")`.

Words from / (a member's session, the dash) read as a person's, with no sender. origin is the context whose code ran, not who asked it to run (core/itx/src/caller.ts stampCaller says why that makes it advisory). Batch writes: append(...events) is one commit, however many events it carries.

Runaway reactions stop on their own: code that reacts to other code's output faster than the clock — two agents answering each other at once, a handler that appends what it handles — is stopped with one error naming where the chain of reactions began, and the context records one itx/loop-limit fact. Reading still works. What counts is speed: each hand-off adds a step and every two minutes takes one away, so a job that waits, like one that re-arms itself for tomorrow, never trips it.

The exception is a jail, a context with a bare itx ⇒ null row plus the grants beside it. A bare jail is closed both ways: its code appends nowhere, and no code appends into it. Grant it itx.append and every context can append into it too; its own code still cannot lift the null. A jail confines the code that runs in it, not the contexts it creates through a grant: jail those too. No jail's rule names a host of the mind's own zones: code that may fetch one states the depth that the app's handler there works at, and an event that the app stores at the limit can cost the events beside it their reactions. A redirect that leads a jailed agent's request there arrives as nobody and begins a chain.

Testing a processor

iterate/stream/test-support (Node) is the harness the SDK's own engine tests use:

import { reduceProcessor } from "iterate/stream/test-support";

// durable ticks are reduced, ephemeral pokes are not
const state = reduceProcessor(new PresenceProcessor(), [{ type: "tick" }, { type: "poke" }]);
// state.ticks === 1

memoryStream, memoryStorage and settle drive a whole ProcessorEngine against an in-memory log (src/stream/processor.test.ts shows how).

A multi-step run

A processor whose work takes steps is still one contract, one state, one reduce and one processEvent.

  1. The request is a fact. A verb appends <x>-requested and answers its offset, which is the run's id. The fact holds every input the run needs, or names what the facet keeps for it in its own storage: the next incarnation reads the log and that storage, never the call.
  2. Each step lands a fact, keyed by the request's offset and the attempt (<slug>/<fact>:<requestOffset>:<attempt>), so a repeated append lands once. A keyed payload holds no key whose value is undefined: the stream compares a repeated append with the stored JSON, which has no such key.
  3. The state says where the run stands: the request, the attempt, the last step and the offset of its fact. snapshot() shows it, and a page can watch it live.
  4. processEvent advances from the state at head. At delivery.caughtUp, a run that no work of this incarnation drives gets one driver in runInBackground. The driver's claim revives the facet if the facet dies. A run found mid-attempt lost its incarnation, or its driver's own fact did not land, so the driver starts the next attempt at once: if anything goes wrong, the work is run again.
  5. Every step is safe to repeat. An attempt starts again from its first step, and each effect accepts its own earlier success. Facts record progress; they do not skip work that dies with an incarnation (/tmp, an open plan).
  6. Attempts are counted in facts and bounded. An attempt-started fact lands before each attempt. An attempt that ends settles the run at once, whatever its outcome: no retry waits inside the processor, the caller runs it again. Only an attempt that ended before it settled the run (its incarnation died) is run again, a bounded number of times, under the engine's five deaths, so that a run whose attempts all die is still revived to settle.
  7. One terminal fact, <x>-settled { requestOffset, status, error? }, keyed by the request. Its status is one of four: succeeded; refused, an expected outcome; failed, any other error (run it again); unavailable, the platform's failure (run it again). A request that opens no run is settled refused while it is processed, in blockProcessorWhile: no state remembers the request, so no later pass could settle it. A caller follows with waitForEvent({ type, payload: { requestOffset }, afterOffset }).
  8. Test it as tables: events through reduceProcessor, states through processEvent with a fake effect on a fake clock, and a re-reduce row.

A new version of a context

A context path has a version, a number that starts at 1. itx.newVersion(reason?) ends the current version and starts the next one at the same path, in the same Durable Object. The log goes on: an offset names one event of the path for ever.

K     itx/run-settled   (each open run, `interrupted`)
K+n   itx/started-over { version: 1, reason?, callerPath?, app?, held: { schedules, subscriptions, rewriteRules } }
K+n+1 itx/created       { path, version: 2 }
      (the object restarts)
K+n+2 itx/woken, then the host's birth events and its wake events: the version's birth

A log that took a new version under the event's earlier name holds itx/version-ended in the place of itx/started-over: a reader of the border takes both (STARTED_OVER_TYPES, client/new-version.ts).

The answer waits until no other isolate holds the earlier version's rules (5 s at most). An append that arrives between the capstone and the restart is refused as UNAVAILABLE and may be sent again. The next incarnation empties what the earlier version left: the subscriptions, schedules, fetch routes, pause and open runs, every facet with its storage, every cursor and delivery, and the kernel's own kv rows. held names the schedules, subscriptions and rules that the version held. What holds the context's code stays: a mask (a rule with a null target, a jail's bare itx ⇒ null among them), and a rule at one of the context's own verbs (itx.cd pointed at a broker, itx.run at a jail). Loaded code can remove neither. Every other rule goes: the parent link, and a rule at any other name. Under a jail every rule with a target goes, at an own verb too: there each is a grant. So the config writes a jail's grants again. A context that masks a name takes a new version only from a member's session.

A subscriber whose target is a client's live callback gets the capstone (itx/started-over and itx/created) as its last batch, whatever it consumes: its row goes with the version. An event that its row does not take comes as the bare border, with the version alone. iterate/client then subscribes again and reads the new version from its start. The limits:

  • Each call that runs starts a version. A second call that reaches the context after the restart starts another: call once for a path, and do not send the call again after an error.

  • A WebSocket that a client holds through the context to a provider (a fetch upgrade) dials again after the restart, as it does after itx.abort(), and goes on when its expression still resolves in the new version.

  • A name that a client lent the earlier version (provide) goes with it. The client is not told, and provides again by its own call.

  • A write that another context makes in reaction to the earlier version, and that arrives after the capstone, lands in the new version: it is sent again like any write. A schedule or a subscription of another context that appends here goes on.

  • A handle made with facets.get(name, spec) names a spec; called after a new version, it hosts that spec under the name again.

  • What another context keeps about this one is that context's: a catalog row on a parent keeps its title and description. Each copy of an event of the current version names that version (source.copiedFrom.version, from 2 on), so a catalog can start a row over when it rises.

  • A rule at a name that is none of the context's own verbs goes with the other rules, also when it narrowed what the parent link gives. Under a mask, write it again before code runs there.

  • The kit (@iterate-com/ui) keeps what it last drew for a moment after the border.

The log stays, and so does what other objects keep under the path: child contexts, the parent's catalog row, secrets, the mind's kv, files and telemetry. A child announces itself to its ancestors once, so a new version of an ancestor holds no itx/child-created for the children that exist, and a catalog that it folds from those facts (/agents) starts empty. A read from the start (afterOffset 0) begins at the current version; allVersions: true reads from the log's start. A read from a positive offset is taken as given: it answers the event at that offset whatever its version, and reads on into the current version. An idempotency key counts within its version. The root / and a context that hosts a first-party facet refuse a new version.

An agent's kind sends the agent's start facts, keyed, with every input. After a new version those keys are free again. So the next input starts whatever implementation the config has now, on an empty version: itx.cd(path).newVersion() starts an agent over.

Node connections

iterate/node connects a Node script to a mind at its address, with the same protocol, sign-in and cleanup as the CLI. ITERATE_MIND_TOKEN, when set, is used in place of a sign-in:

import { connectMind } from "iterate/node";

using connection = await connectMind({ address: "iterate.example.com" });
console.log(await connection.itx.whoami());

address is the mind's hostname as it is. For an address that a person typed, where a bare example.com means iterate.example.com, read it once with addressOf(typed) first.

A client of your own dials https://<address>/api with capnweb over a WebSocket: the API has no other transport. The root of that session is small (ControlPlaneApi in iterate/api): authenticate(token) answers a session, and the session's mind is the mind's itx as the token's member. Call it at once and do not await it, so that capnweb sends your first calls in the same round trip:

const api = newWebSocketRpcSession(socket);
const itx = api.authenticate(token).mind.dup(); // a stub, usable now
console.log(await itx.whoami());

With no token, authenticate() takes what the socket's own request carried: its Authorization bearer, or, from a page of the mind's control-plane hostname alone, the browser's session cookie. The socket closes with 1008 when the credential ends.

Event types

A platform event type is events.iterate.com/<namespace>/<event>: one namespace segment and one event segment, both lowercase kebab-case, and never a third segment.

Every type under events.iterate.com/ follows these rules, test types included. A type without that prefix belongs to whoever appends it and is opaque to the platform: tests use types like demo/ping on purpose, and a mind may use its own domain (events.garple.com/sales/…).

A platform fact that is appended after it happened carries recordedLate: { happenedAt } in its payload, so that a reader does not take the append's time for the event's: today the mail's facts that the boundary kept for a mind that runs elsewhere while it was away (iterate/email).

Namespaces
  • itx holds the context engine's own events: everything the core contract (core/itx/src/stream/core-processor.ts) reduces, validates or refuses, plus the records the Stream, the context Durable Object and the SDK processor host write themselves. Where the schema and the reduce live decides it, not which contexts hold the event: fetch routes and the apex ingress target are core state, so they are itx even though only a mind root's copy is read. A domain processor may consume an itx event (the agent consumes itx/run-*, the Mind processor itx/ingress-configured); it names the core's catalog in its processorDeps rather than defining the event itself. The core's checkpoint slug is core: it is a storage key, not a type prefix.
  • A domain namespace is the singular name of the kind of context whose log the event belongs to, which is the defining contract's slug when there is one: account, organization, mind, repo, workspace, sandbox, secret, approval, agent, voice-agent, page (a page's views, on /pages) and exception (what broke, on /exceptions). A fact cross-posted to another log keeps its own namespace: repo/created on / is still a repo fact.
  • An integration uses its own name as its namespace, for example chrome, slack, google, github.
  • An app of a mind uses its own name too: ai-subscriptions (the Claude and Codex accounts' app, internal-packages/ai-subscriptions, and apps/ai-subscriptions/ of a mind's config repo) and tasks (the Tasks app, apps/tasks/: the change feed of a repo's task files).
  • A channel of an agent uses its own name, as an integration does: agent-ui is the web and mobile chat (agent-ui/message-received is what its composer creates, and agent-ui/message-sent is the agent's words for it), beside slack.
  • A protocol between processors uses its own name. llm-request holds the facts by which one processor asks a model and another makes the request. Each request maker has a namespace of its own, named after the LLM API it speaks, for what only it knows (the exact bytes of an attempt, the provider's raw frames): openai-responses, anthropic-messages, chat-completions, google-generate-content. turn-decider is the processor beside an agent that decides when it must answer now.
  • test holds types that only tests append. Production code never matches a test/* type. A test contract may keep a slug of its own (counter), but its events go under test/. A test must not borrow a production namespace for a type that does not exist.
Event names
  • A fact is past tense: <object>-<verb-ed>, or a bare <verb-ed> when the object is the namespace's own subject (itx/created is the context, agent/paused is the agent). The object comes first and is singular.
  • Spell words out. Clipped words are not allowed (spk); a real word is (mic), and so is an acronym the API already spells (llm, rpc, itx).
  • Asking and answering. <x>-requested asks, and its offset identifies the ask. The answer takes one of three shapes:
    • <x>-settled is the one terminal fact when the asker reads a result. It names requestOffset and carries the outcome: succeeded, failed or cancelled, a status, or an error. Examples: itx/run-*, agent/llm-request-*.
    • <verb-ed> or <verb>-failed is used when success is a fact that other logs wait on, like a certificate: create-requested → created or create-failed, delete-requested → deleted. A failure that is retried rather than reported gets no -failed fact.
    • An answer that is also a fact of its own names the ask's offset: the assistant's agent/context-added carries the llmRequestOffset of the llm-request-requested it answers.
  • One verb pair per kind of change:
    • added / removed for membership in a set: account/member-added, account/key-added.
    • created / deleted for an entity with a lifecycle: minds, repos, workspaces, sandboxes, agents.
    • set / deleted for a keyed value: secret/*.
    • set / cancelled for a schedule: itx/schedule-*. Each occurrence is fired or failed.
    • -configured for one fact that sets a row or clears it with null (itx/subscription-configured, itx/rewrite-rule-configured, itx/fetch-route-configured), sets a singleton (itx/ingress-configured), or merges a partial configuration (agent/configured: omitted keys keep their values).
  • Things the platform does on its own are plain facts about the object: itx/schedule-fired, itx/schedule-failed, itx/subscription-delivery-halted.
  • Ephemeral events. An ephemeral event that records something happening is named like any other fact: itx/rpc-stub-attached, itx/live-state-changed, chrome/navigated. Three kinds may be singular nouns: a sequenced slice of a live stream is a <stream>-frame (voice-agent/mic-frame, agent/llm-response-frame), a heartbeat (voice-agent/keepalive), and a diagnostic record (itx/alarm-trace). A durable event is never a noun.
  • Families and prefixes. Code matches some families by prefix: …/itx/run-, …/itx/subscription-, …/itx/schedule-. Before naming a new type, check it doesn't join one of these families by accident. Never match …/itx/ as a whole: it is not a permission boundary, and it catches live-state deltas, stub presence and child announcements.
  • Code follows the type. A constant, schema, test fixture or idempotency key built from a type follows its name (itx/child-created:<path>). Broader concepts, modules and Workers log event names keep theirs: the Stream, scheduled appends, core, scheduled-append.completed.
  • Renaming. A rename has to serve one of these rules, not taste. If a type is stored outside the platform's Durable Objects (device firmware, a published SDK, a mind's config repo), rename it only in a change that migrates that store too.
Namespace Defined in
itx core/itx/src/stream/core-processor.ts (and its leaf event catalog), stream.ts, scheduled-appends.ts, subscription-delivery.ts, core/itx/src/context/built-ins.ts, core/itx/src/fetch-routes.ts, core/itx/src/iterate-context-durable-object.ts, core/lib/src/stream/{run,processor}.ts
account core/auth-worker/src/account-facts.ts
organization none: the namespace is allowed, and no code defines one
mind, repo, workspace, sandbox core/mind-worker/src/mind/contract.ts and core/mind-worker/src/<name>/contract.ts (repo, workspace and sandbox also use mind/entity-lifecycle.ts)
secret core/secrets-worker/src/secret/contract.ts
approval core/lib/src/acts.ts (the acts a gate holds for a member's yes, and their rules)
mind-meld core/auth-worker/src/minds.ts (the rule that lets another mind add events here)
agent core/lib/src/agents/contract.ts
voice-agent packages/voice/src/voice-agent.ts
chrome packages/browser-extension/public/panel.js
email core/lib/src/email.ts
mcp core/mind-worker/src/integrations/mcp.ts (a delivery to a context's MCP webhook) and core/mind-worker/src/library/mcp.ts (what the connection of itx.connectToMcp tells its context: a subscription that ended, a task that settled)
webhook core/mind-worker/src/integrations/webhooks.ts (a delivery to a webhook context of a mind stack)
cloud core/auth-worker/src/device/protocol.ts (the statement the iterate Cloud signs to ask to be let in: shown with the request, never appended)
integration core/lib/src/integrations.ts (the registry a mind's packages write into; its fold is core/mind-worker/src/integrations/registry.ts)
test tests only

note/added is only an example in the Agents composer; no contract defines note.

Keywords