Skip to content

An open-source, faster Devin alternative

TypeScript

Teach one flow everywhere: address the agent by alias, one key per submission, retry the same request

Igor Zalutski0ada7d69d510
diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.ts
index 1287fa2f..b7f890bd 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts
@@ -2423,6 +2423,7 @@ describe("managed agents proxy", () => {
runtimeToken: "internal-runtime-token",
microvmId: "internal-vm",
microvmState: "suspended",
+ creation: { agent: "worker@development" },
createdAt: "2026-09-15T00:00:00.000Z",
updatedAt: "2026-09-15T00:01:00.000Z",
environment: "development",
@@ -2473,6 +2474,9 @@ describe("managed agents proxy", () => {
expect(listBody.sessions[0].result).toEqual(result);
expect(detailBody.result).toEqual(result);
expect(detailBody.result).toEqual(listBody.sessions[0].result);
+ // The creation intent is admission metadata the platform compares on a
+ // replay; it is not part of the documented session.
+ expect(detailBody).not.toHaveProperty("creation");
expect(detailBody.labels).toEqual({ user_id: "u-42", topic: "t" });
expect((detailBody.turns as Array<Record<string, unknown>>)[0].payload).toEqual({
userId: "u-42",
diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.ts
index 2412d582..43a2bc53 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.ts
@@ -780,11 +780,12 @@ function publicSessionResult(value: unknown): unknown {
/**
* Platform-internal fields of the session envelope that no public route
- * documents: the runtime generation counter and the name of the memory
- * object the bindings were admitted against. Dropped by position, since the
- * name filter is for the fields it lists.
+ * documents: the runtime generation counter, the name of the memory object
+ * the bindings were admitted against, and the creation intent the platform
+ * compares on an idempotent replay. Dropped by position, since the name
+ * filter is for the fields it lists.
*/
-const PRIVATE_SESSION_FIELDS = new Set(["runtimeEpoch", "memoryObject"]);
+const PRIVATE_SESSION_FIELDS = new Set(["runtimeEpoch", "memoryObject", "creation"]);
/**
* One turn of the snapshot. `payload` is the caller's own JSON and passes
diff --git a/docs/agents/api.mdx b/docs/agents/api.mdx
index 59366841..d46846e9 100644
--- a/docs/agents/api.mdx
+++ b/docs/agents/api.mdx
@@ -48,7 +48,7 @@ code.
## Sessions
-A session is a durable conversation with one deployed agent. It pins the
+A session is a durable conversation with one deployed agent. It keeps the
deployment it was created on; see [Sessions and turns](/agents/sessions).
| Operation | Method and path | Success |
@@ -127,7 +127,7 @@ insufficient_credits`.
| Field | Meaning |
| --- | --- |
-| `id`, `agentId`, `deploymentId` | The session and the deployment it pins |
+| `id`, `agentId`, `deploymentId` | The session and the deployment it runs on, chosen when it was created |
| `projectId` | The project the agent belongs to |
| `environment` | `development` or `production`, when the request named one |
| `status` | `new`, `connecting`, `idle`, `running`, `stopping`, `waiting_runtime`, `suspending`, `suspended`, `resuming`, `failed` or `ended` |
@@ -299,16 +299,16 @@ An application that turns a form submission into agent work makes two calls,
each with its own key, and keeps what it sent until the second one is
acknowledged:
-1. Resolve the deployment once, where the submission is composed (for
- example when the form opens), and pin it: `POST /sessions` with
- `Idempotency-Key: <submission-id>`, the `deploymentId` and `environment`.
- A retry then carries the deployment the submission was composed against,
- even if the alias has moved since.
+1. `POST /sessions` with `Idempotency-Key: <submission-id>` and
+ `agentId: <agent-id>@<environment>`. The platform chooses the deployment
+ active in that environment and records it on the session; a retry under
+ the same key returns that session, even if the alias has moved since.
+ The application never discovers, verifies or stores a deployment id.
2. `POST /sessions/<id>/turns` with `Idempotency-Key: <submission-id>/start`,
the text and the `payload`.
-Keep the submission (its id, the pinned deployment, the text and the
-payload) until the turns call answers, with `duplicate: false` or `true`.
+Keep the submission (its id, the text and the payload) until the turns call
+answers, with `duplicate: false` or `true`.
A lost reply at either step is retried with the same body and the same key:
the first step returns the same session, the second the same turn. A
session that exists without a turn is work that was never admitted; only the
diff --git a/docs/agents/document-memory.mdx b/docs/agents/document-memory.mdx
index 65f6e6c0..7811e518 100644
--- a/docs/agents/document-memory.mdx
+++ b/docs/agents/document-memory.mdx
@@ -84,9 +84,11 @@ Session inspection returns a `memory` array. Entries contain `resource`,
`scope`, optional document `id`, `access` and current `writable`. It is false
for read-only bindings, frozen/deleted documents and ended sessions.
-Reusing a session-create `Idempotency-Key` requires identical agent, deployment,
-environment and bindings; incompatible inputs return `409 idempotency_conflict`.
-The [management API](/agents/api#create) page has the full rules.
+Reusing a session-create `Idempotency-Key` requires the same agent (or the
+same pinned deployment), environment and bindings; incompatible inputs return
+`409 idempotency_conflict`. The session keeps the deployment it started on,
+so a redeploy does not change what the key returns. The
+[management API](/agents/api#create) page has the full rules.
### Start a session on a new document
@@ -116,16 +118,15 @@ const { document, session } = await oc.sessions.startOnDocument({
left as it is; `document` is ignored then. A deleted id is reserved and
fails before any session is created.
- The session is created with an `Idempotency-Key` derived from
- `idempotencyKey`. The same key with the same agent, deployment, environment
- and bindings returns the existing session; anything else under that key
+ `idempotencyKey`. The same key with the same agent, environment and
+ bindings returns the existing session; anything else under that key
fails with code `idempotency_key_reused`. `access` (default `read-write`),
`memory` for further bindings and `source` are passed through.
-The helper resolves the agent's environment alias on each attempt. A
-redeploy can therefore make a retry conflict even if the first attempt
-created the session. Do not change the key to recover a lost response: that
-would create another session. For retries across redeploys, use separate
-document and session calls with a retained deployment ID, as described below.
+The helper addresses the agent by its environment alias; the platform
+records the deployment the session started on, so a retry after a redeploy
+returns the same session. Do not change the key to recover a lost response:
+that would create another session.
The CLI does the same for a Development session:
@@ -141,8 +142,8 @@ run the first turn at once, and `--json` for the ids.
Over HTTP, send `PUT .../documents/<id>` with `If-None-Match: *`, treat `412`
as "exists" (then `GET` it; `404` means the id was deleted), and `POST
-/api/managed-agents/sessions` with the document binding, a retained
-`deploymentId`, `environment` and `Idempotency-Key`; `201` created the
+/api/managed-agents/sessions` with the document binding,
+`agentId: <agent-id>@<environment>` and `Idempotency-Key`; `201` created the
session, `200` returned the one that key had already created. Keep that
envelope unchanged for retries. See [session creation](/agents/api#create)
for the body and the
diff --git a/docs/agents/react.mdx b/docs/agents/react.mdx
index 3e98a583..0d49821a 100644
--- a/docs/agents/react.mdx
+++ b/docs/agents/react.mdx
@@ -15,25 +15,27 @@ npm install @opencomputer/react
## Create the session on your server
Use the [management client](/reference/typescript-sdk/agents) from trusted
-code. Resolve an allowed deployment when composing the submission, then
-retain its ID and a creation key for retries:
+code. Address the agent by its environment alias, with one key per
+submission:
```ts
import { OpenComputer } from "@opencomputer/sdk/agents";
const oc = new OpenComputer({ apiKey: process.env.OPENCOMPUTER_API_KEY! });
const { session } = await oc.sessions.create(
- { deploymentId, environment: "development", labels: { request: taskId } },
+ { agentId: "<agent-id>@development", labels: { request: taskId } },
{ idempotencyKey: taskId },
);
// Return session.id to the browser; use it when reopening this conversation.
```
-Here `deploymentId` and `taskId` belong to one submission. Validate the
-requested deployment against the user's permitted project, environment and
-agent. Retry an uncertain create with the same parameters and key; resolving
-an alias again could select a newer deployment. Creating a session and
-sending its first turn are separate calls with separate keys.
+`taskId` belongs to one submission. The platform chooses the deployment
+active in the environment and records it on the session, so retry an
+uncertain create with the same parameters and key: it returns the same
+session even after a redeploy. Nothing in your application handles
+deployment ids; pinning one is an advanced option of `sessions.create`.
+Creating a session and sending its first turn are separate calls with
+separate keys.
## Expose three routes
diff --git a/docs/agents/sessions.mdx b/docs/agents/sessions.mdx
index a31a8d1a..bf7af81f 100644
--- a/docs/agents/sessions.mdx
+++ b/docs/agents/sessions.mdx
@@ -8,7 +8,8 @@ input and streamed runtime events to the session, allowing clients to resume a
conversation without rebuilding its history locally. The events are listed on
[Session events](/agents/events).
-A session pins the deployment it was created on. Advancing Development or
+A session keeps the deployment it was created on, chosen by the platform
+from the alias it was addressed to. Advancing Development or
promoting to Production changes which deployment new sessions get; turns sent
to an existing session keep running the code it started with, including its
declared tools and memory resources. To run new code, start a new session.
diff --git a/docs/reference/typescript-sdk/agents.mdx b/docs/reference/typescript-sdk/agents.mdx
index 4468f634..52df9905 100644
--- a/docs/reference/typescript-sdk/agents.mdx
+++ b/docs/reference/typescript-sdk/agents.mdx
@@ -132,14 +132,15 @@ after a lost reply learns what became of the turn. `list` takes `project`,
### Create and submit
-Create the session, then send its first turn. Resolve the deployment once
-when composing the submission and retain its ID, both keys and the inputs.
-Retrying either call then returns the same session or turn, even if the
-agent is redeployed between attempts.
+Create the session, then send its first turn. Address the agent by its
+environment alias and retain both keys and the inputs. Retrying either call
+then returns the same session or turn, even if the agent is redeployed
+between attempts: the platform records the deployment the session started
+on. Pinning a `deploymentId` is an advanced option, not part of this flow.
```typescript
const { session } = await oc.sessions.create(
- { deploymentId, environment: "development", labels: { request: taskId } },
+ { agentId: "<agent-id>@development", labels: { request: taskId } },
{ idempotencyKey: taskId },
);
const receipt = await oc.sessions.turns.send(session.id, {
@@ -179,7 +180,7 @@ one. Results retain their turn provenance and can outlive later work; see
if it does not exist, then a session bound to it, and reports both ids and
whether each already existed. These are two requests, not an atomic operation:
the document remains if session creation fails. Retries reuse the document
-and converge on the session while the agent's deployment and bindings remain
+and converge on the session while the agent, environment and bindings remain
unchanged.
```typescript
@@ -236,13 +237,10 @@ The result:
| `session.created` | `false` when the key had already created this session |
| `session.status`, `session.executionMode` | As the create response reported them |
-The helper resolves the environment's agent alias on every call and cannot
-pin a deployment. If that alias changes between a successful create and its
-retry, the retry fails with `idempotency_key_reused`. A new key would create
-another session; it does not recover a lost response. When retries must
-survive redeploys, create/read the document separately and pass its binding
-to [`sessions.create`](#create-and-submit) with a retained `deploymentId`
-and key.
+The helper addresses the environment's agent alias on every call; the
+platform records the deployment the session started on, so a retry after a
+redeploy returns the same session. A new key would create another session;
+it does not recover a lost response.
`startSessionOnDocument({ apiKey, baseUrl?, fetch?, ...params })`, exported
from the same subpath, is the standalone form of the call for code that
diff --git a/sdks/typescript/README.md b/sdks/typescript/README.md
index 9dcab67f..9ccf2f2d 100644
--- a/sdks/typescript/README.md
+++ b/sdks/typescript/README.md
@@ -71,7 +71,7 @@ Applications that keep notes in [memory](https://opencomputer.dev/agents/memory)
const { document, session } = await oc.sessions.startOnDocument({
projectId: "prj_…", environment: "development", agent: "topic-worker",
resource: "topics", documentId: "workshop", document: { title: "Workshop" },
- idempotencyKey: `topic/workshop/${deploymentId}`,
+ idempotencyKey: "topic/workshop/1",
});
// document.created, session.created: false when they already existed.
```
diff --git a/sdks/typescript/src/agents/client.ts b/sdks/typescript/src/agents/client.ts
index 8b0e365d..eded8e8f 100644
--- a/sdks/typescript/src/agents/client.ts
+++ b/sdks/typescript/src/agents/client.ts
@@ -61,9 +61,10 @@ export interface CallOptions {
export interface CreateSessionOptions extends CallOptions {
/**
- * At most 256 characters. The same key with the same agent, deployment,
- * environment and memory bindings returns the existing session; anything
- * else under the key is `409 idempotency_conflict`.
+ * At most 256 characters. The same key with the same agent (or the same
+ * pinned deployment), environment and memory bindings returns the
+ * existing session with the deployment it started on, after a redeploy
+ * too; anything else under the key is `409 idempotency_conflict`.
*/
idempotencyKey?: string;
}
diff --git a/sdks/typescript/src/agents/start-on-document.test.ts b/sdks/typescript/src/agents/start-on-document.test.ts
index b3ef4bd8..51a0ab1e 100644
--- a/sdks/typescript/src/agents/start-on-document.test.ts
+++ b/sdks/typescript/src/agents/start-on-document.test.ts
@@ -121,7 +121,7 @@ describe("sessions.startOnDocument", () => {
const failure = await client(api.fetch).sessions.startOnDocument(params).catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(OpenComputerError);
expect(failure).toMatchObject({ status: 409, code: "idempotency_key_reused" });
- expect((failure as Error).message).toMatch(/different agent, deployment, environment or memory bindings/);
+ expect((failure as Error).message).toMatch(/different agent, environment or memory bindings/);
});
it("refuses a deleted document id instead of creating a session that cannot bind it", async () => {
diff --git a/sdks/typescript/src/agents/start-on-document.ts b/sdks/typescript/src/agents/start-on-document.ts
index 8ac4094a..28c0d010 100644
--- a/sdks/typescript/src/agents/start-on-document.ts
+++ b/sdks/typescript/src/agents/start-on-document.ts
@@ -85,9 +85,10 @@ export async function sessionIdempotencyKey(idempotencyKey: string): Promise<str
* is reserved and fails with code `memory_document_deleted`, since a
* binding to it would fail admission.
* - The session is created with an `Idempotency-Key` derived from
- * `idempotencyKey`; the same key with the same agent, deployment,
- * environment and bindings returns the existing session with
- * `created: false`. Anything else under the same key fails with code
+ * `idempotencyKey`; the same key with the same agent, environment and
+ * bindings returns the existing session with `created: false`, after a
+ * redeploy too, since the platform records the deployment the session
+ * started on. Anything else under the same key fails with code
* `idempotency_key_reused`.
*/
export async function startOnDocument(http: Http, params: StartOnDocumentParams): Promise<StartOnDocumentResult> {
@@ -149,7 +150,7 @@ export async function startOnDocument(http: Http, params: StartOnDocumentParams)
409,
"idempotency_key_reused",
`Idempotency key ${JSON.stringify(params.idempotencyKey)} already created a session with a different ` +
- "agent, deployment, environment or memory bindings. Use a new key to start another session; " +
+ "agent, environment or memory bindings. Use a new key to start another session; " +
"the document was left as it is.",
);
}
diff --git a/sdks/typescript/src/version.ts b/sdks/typescript/src/version.ts
index 6a27461a..56d2c3c5 100644
--- a/sdks/typescript/src/version.ts
+++ b/sdks/typescript/src/version.ts
@@ -27,7 +27,7 @@ export const SDK_VERSION_HEADER = "x-oc-sdk-version";
* only failure mode that matters — a stale constant here would silently route a
* v2 caller to the old runtime.
*/
-export const SDK_VERSION = "2.0.0";
+export const SDK_VERSION = "2.1.1";
/** The identifying header, as a spreadable object. */
export function sdkVersionHeaders(): Record<string, string> {