@loopingai/core 0.5.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/a2a/caller-token.d.ts +38 -0
- package/dist/a2a/caller-token.js +61 -0
- package/dist/a2a/deliver.d.ts +49 -0
- package/dist/a2a/deliver.js +49 -0
- package/dist/a2a/index.d.ts +2 -0
- package/dist/a2a/index.js +2 -0
- package/dist/a2a/notify.d.ts +4 -3
- package/dist/a2a/notify.js +4 -3
- package/dist/agent/anthropic/index.d.ts +15 -0
- package/dist/agent/anthropic/index.js +19 -0
- package/dist/agent/anthropic/language-model.d.ts +59 -0
- package/dist/agent/anthropic/language-model.js +442 -0
- package/dist/agent/anthropic/prompt.d.ts +84 -0
- package/dist/agent/anthropic/prompt.js +541 -0
- package/dist/agent/anthropic/runtime.d.ts +79 -0
- package/dist/agent/anthropic/runtime.js +130 -0
- package/dist/agent/control.js +10 -9
- package/dist/agent/errors.d.ts +85 -0
- package/dist/agent/errors.js +64 -0
- package/dist/agent/final-reply.d.ts +14 -13
- package/dist/agent/final-reply.js +28 -11
- package/dist/agent/history.d.ts +3 -3
- package/dist/agent/history.js +2 -2
- package/dist/agent/index.d.ts +4 -2
- package/dist/agent/index.js +4 -2
- package/dist/agent/inference.d.ts +58 -1
- package/dist/agent/inference.js +44 -0
- package/dist/agent/model.d.ts +42 -25
- package/dist/agent/model.js +1 -48
- package/dist/agent/session.d.ts +6 -7
- package/dist/agent/session.js +3 -3
- package/dist/agent/workers-ai/index.d.ts +23 -0
- package/dist/agent/workers-ai/index.js +23 -0
- package/dist/agent/workers-ai/runtime.d.ts +42 -0
- package/dist/agent/workers-ai/runtime.js +63 -0
- package/dist/alarm/index.d.ts +77 -0
- package/dist/alarm/index.js +116 -0
- package/dist/config.d.ts +49 -15
- package/dist/config.js +30 -1
- package/dist/contract/plugin.d.ts +63 -3
- package/dist/contract/plugin.js +76 -0
- package/dist/contract/recipe.d.ts +16 -17
- package/dist/db/db.d.ts +0 -1
- package/dist/db/migrations/index.js +8 -1
- package/dist/db/models/subtasks.d.ts +24 -25
- package/dist/db/models/subtasks.js +33 -76
- package/dist/db/schema.d.ts +2 -21
- package/dist/db/schema.js +2 -4
- package/dist/host/agent.d.ts +58 -4
- package/dist/host/agent.js +63 -9
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/platform.d.ts +103 -11
- package/dist/platform.js +107 -13
- package/dist/round/agent.d.ts +36 -31
- package/dist/round/agent.js +61 -89
- package/dist/round/index.d.ts +3 -2
- package/dist/round/index.js +2 -2
- package/dist/round/policy.d.ts +2 -2
- package/dist/round/subagent.d.ts +19 -1
- package/dist/round/subagent.js +22 -5
- package/dist/round/turn.d.ts +32 -13
- package/dist/round/turn.js +83 -16
- package/dist/round/workflow.d.ts +23 -7
- package/dist/round/workflow.js +122 -110
- package/dist/runtime/index.d.ts +4 -2
- package/dist/runtime/index.js +6 -0
- package/dist/subagent/fingerprint.d.ts +2 -2
- package/dist/subagent/fingerprint.js +8 -17
- package/dist/subagent/index.d.ts +6 -4
- package/dist/subagent/index.js +8 -6
- package/dist/subagent/prompt.d.ts +4 -5
- package/dist/subagent/prompt.js +0 -8
- package/dist/subagent/run.d.ts +8 -1
- package/dist/subagent/run.js +59 -9
- package/dist/subtasks/catalog.d.ts +1 -1
- package/dist/subtasks/catalog.js +1 -1
- package/dist/subtasks/decomposition.d.ts +16 -20
- package/dist/subtasks/decomposition.js +27 -75
- package/dist/subtasks/delegate.d.ts +20 -1
- package/dist/subtasks/delegate.js +21 -16
- package/dist/subtasks/index.d.ts +1 -2
- package/dist/subtasks/index.js +1 -2
- package/dist/subtasks/subtask-types.d.ts +0 -8
- package/dist/subtasks/subtask-types.js +0 -7
- package/dist/subtasks/types.d.ts +45 -70
- package/dist/testing/do.d.ts +4 -4
- package/dist/testing/index.d.ts +1 -1
- package/dist/testing/index.js +5 -1
- package/dist/testing/mock-model.d.ts +35 -0
- package/dist/testing/mock-model.js +75 -0
- package/dist/testing/vcr-global-setup.d.ts +1 -3
- package/dist/testing/vcr-global-setup.js +1 -3
- package/dist/worker/index.d.ts +5 -12
- package/dist/worker/index.js +5 -12
- package/package.json +23 -1
- package/dist/subtasks/scheduler.d.ts +0 -48
- package/dist/subtasks/scheduler.js +0 -47
package/dist/db/schema.js
CHANGED
|
@@ -51,8 +51,8 @@ export const notifyTasks = sqliteTable("notify_tasks", {
|
|
|
51
51
|
*
|
|
52
52
|
* The integer primary key assigns a caller-local, monotonically increasing
|
|
53
53
|
* `SubtaskId` (autoincrement, so ids are never reused after cleanup deletes
|
|
54
|
-
* rows). References
|
|
55
|
-
*
|
|
54
|
+
* rows). References and result parts are stored as JSON text and parsed back
|
|
55
|
+
* into the `Subtask` contract by `models/subtasks.ts`.
|
|
56
56
|
* `recipe_id`/`recipe_version` are null until execution starts, then record the
|
|
57
57
|
* resolved recipe after the fact.
|
|
58
58
|
*/
|
|
@@ -71,8 +71,6 @@ export const subtasks = sqliteTable("subtasks", {
|
|
|
71
71
|
prompt: text("prompt").notNull(),
|
|
72
72
|
/** JSON `SubtaskReference[]` — verbatim role+text snapshots from decomposition. */
|
|
73
73
|
referencesJson: text("references_json").notNull(),
|
|
74
|
-
/** JSON `SubtaskId[]` — resolved dependency edges. */
|
|
75
|
-
dependsOnJson: text("depends_on_json").notNull(),
|
|
76
74
|
/** JSON `SubtaskParams` — the type's required inputs, validated at delegation. */
|
|
77
75
|
paramsJson: text("params_json").notNull().default("{}"),
|
|
78
76
|
status: text("status").notNull(),
|
package/dist/host/agent.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Agent, type Schedule } from "agents";
|
|
|
2
2
|
import { type Task } from "@a2a-js/sdk";
|
|
3
3
|
import { type AgentRuntime } from "../runtime/index.js";
|
|
4
4
|
import type { AgentPlugin } from "../contract/plugin.js";
|
|
5
|
-
import { type CoreConfig, type CoreConfigOverrides } from "../config.js";
|
|
5
|
+
import { type CoreConfig, type CoreConfigOverrides, type ModelConfig } from "../config.js";
|
|
6
6
|
import type { A2ASecretsEnv, AiEnv } from "../env.js";
|
|
7
7
|
import { AgentDB } from "../db/index.js";
|
|
8
8
|
import type { GatewayIdentity } from "../a2a/verify.js";
|
|
@@ -10,7 +10,7 @@ import type { PlainTask } from "../a2a/task.js";
|
|
|
10
10
|
import type { TaskListQuery } from "../a2a/agent-stub.js";
|
|
11
11
|
import { type PushChannel, type TurnPushContext } from "../a2a/push.js";
|
|
12
12
|
import { type SessionLike } from "../agent/session.js";
|
|
13
|
-
import {
|
|
13
|
+
import type { GatewayMetadata, ModelPair, ModelRuntime } from "../agent/model.js";
|
|
14
14
|
import type { PluginHost } from "./plugin-host.js";
|
|
15
15
|
/**
|
|
16
16
|
* The Durable Object body every Looping agent has, whatever loop it runs.
|
|
@@ -64,6 +64,9 @@ export declare abstract class LoopingAgent<TEnv extends Cloudflare.Env & AiEnv &
|
|
|
64
64
|
* `onStart` runs before any request, so it is not known when `agentPlugins()`
|
|
65
65
|
* is built — which is why anything per-caller takes a thunk. The DO is keyed
|
|
66
66
|
* 1:1 by this value, so it is constant once set.
|
|
67
|
+
*
|
|
68
|
+
* In-memory, and deliberately so — see {@link requireIdentityKey}, which does
|
|
69
|
+
* not depend on it surviving.
|
|
67
70
|
*/
|
|
68
71
|
private identityKey?;
|
|
69
72
|
/**
|
|
@@ -95,7 +98,30 @@ export declare abstract class LoopingAgent<TEnv extends Cloudflare.Env & AiEnv &
|
|
|
95
98
|
protected get config(): CoreConfig;
|
|
96
99
|
/** The agent's database (drizzle + migrations), built once per DO instance. */
|
|
97
100
|
protected get db(): AgentDB;
|
|
98
|
-
/**
|
|
101
|
+
/**
|
|
102
|
+
* Which provider this agent's loops run on. Defaults to Workers AI; override
|
|
103
|
+
* to run on something else.
|
|
104
|
+
*
|
|
105
|
+
* The seam is here rather than in `models` because `models` memoizes, and a
|
|
106
|
+
* subclass overriding a memoized getter has to remember to keep the caching —
|
|
107
|
+
* a trap that only shows up as a performance bug. This is called once.
|
|
108
|
+
*
|
|
109
|
+
* `ModelRuntime` is the whole contract: return anything satisfying it and
|
|
110
|
+
* every loop in core keeps working unchanged. Core ships two implementations,
|
|
111
|
+
* one directory each — {@link file://../agent/workers-ai/index.ts
|
|
112
|
+
* `agent/workers-ai`} (the default below) and `@loopingai/core/anthropic` —
|
|
113
|
+
* and a third provider is a third directory exporting one
|
|
114
|
+
* {@link file://../agent/model.ts ModelRuntimeFactory}, not a change to
|
|
115
|
+
* anything on this path.
|
|
116
|
+
*
|
|
117
|
+
* Takes the resolved {@link ModelConfig} rather than reading `this.config`, so
|
|
118
|
+
* that this signature matches
|
|
119
|
+
* {@link file://../round/subagent.ts RecipeSubagentHost.modelRuntime} — an
|
|
120
|
+
* agent and its facet **must** run the same provider, and identical seams are
|
|
121
|
+
* what let one factory serve both instead of two hand-copied bodies.
|
|
122
|
+
*/
|
|
123
|
+
protected modelRuntime(model: ModelConfig): ModelRuntime;
|
|
124
|
+
/** The model runtime for this instance, built lazily and memoized. */
|
|
99
125
|
protected get models(): ModelRuntime;
|
|
100
126
|
/**
|
|
101
127
|
* What this agent's plugins are handed. Built from
|
|
@@ -141,7 +167,35 @@ export declare abstract class LoopingAgent<TEnv extends Cloudflare.Env & AiEnv &
|
|
|
141
167
|
* announces the loss, and the runtime fans it out to every plugin that asked.
|
|
142
168
|
*/
|
|
143
169
|
getSession(identity: GatewayIdentity): SessionLike;
|
|
144
|
-
/**
|
|
170
|
+
/**
|
|
171
|
+
* The caller key, which is present on every path that can reach a plugin.
|
|
172
|
+
*
|
|
173
|
+
* ## Why this does not just read the field
|
|
174
|
+
*
|
|
175
|
+
* `identityKey` is set on the first turn and lives in the isolate. An isolate
|
|
176
|
+
* does not live as long as the work does: it can be evicted between two rounds
|
|
177
|
+
* of the same task, and it can be reset outright — "Durable Object connection
|
|
178
|
+
* closed because the object was reset" — while a Workflow step is mid-flight.
|
|
179
|
+
* The next call arrives on a fresh instance where the field is empty, and
|
|
180
|
+
* every per-caller thunk built off it throws.
|
|
181
|
+
*
|
|
182
|
+
* That failure is disproportionate to its cause. A plugin asking which caller
|
|
183
|
+
* it is serving gets an exception, mid-task, on an object whose entire purpose
|
|
184
|
+
* is to be that caller's — and because the throw happens inside a tool or a
|
|
185
|
+
* runtime resolution rather than at the edge, it surfaces as a failed branch
|
|
186
|
+
* rather than as anything an operator can read.
|
|
187
|
+
*
|
|
188
|
+
* So the object answers from itself. `define-agent` routes with
|
|
189
|
+
* `ns.get(ns.idFromName(identity.key))`, which means the caller key *is* this
|
|
190
|
+
* object's name and the platform hands it back on `ctx.id.name` — durable, free
|
|
191
|
+
* and correct by construction: an object cannot disagree with the name it was
|
|
192
|
+
* addressed by.
|
|
193
|
+
*
|
|
194
|
+
* The field still wins when it is set. `id.name` is undefined for an object
|
|
195
|
+
* addressed by `newUniqueId()` or a raw id string, so it is a fallback rather
|
|
196
|
+
* than the source of truth, and the throw is kept for the case where neither
|
|
197
|
+
* exists.
|
|
198
|
+
*/
|
|
145
199
|
protected requireIdentityKey(): string;
|
|
146
200
|
/** The gateway callback channel for one turn. See {@link PushChannel}. */
|
|
147
201
|
protected push(context: TurnPushContext): PushChannel;
|
package/dist/host/agent.js
CHANGED
|
@@ -6,7 +6,7 @@ import { AgentDB, stateOf } from "../db/index.js";
|
|
|
6
6
|
import { callerContext } from "../a2a/caller.js";
|
|
7
7
|
import { createPushChannel } from "../a2a/push.js";
|
|
8
8
|
import { buildAgentSession } from "../agent/session.js";
|
|
9
|
-
import {
|
|
9
|
+
import { workersAIModels } from "../agent/workers-ai/index.js";
|
|
10
10
|
/**
|
|
11
11
|
* The Durable Object body every Looping agent has, whatever loop it runs.
|
|
12
12
|
*
|
|
@@ -59,6 +59,9 @@ export class LoopingAgent extends Agent {
|
|
|
59
59
|
* `onStart` runs before any request, so it is not known when `agentPlugins()`
|
|
60
60
|
* is built — which is why anything per-caller takes a thunk. The DO is keyed
|
|
61
61
|
* 1:1 by this value, so it is constant once set.
|
|
62
|
+
*
|
|
63
|
+
* In-memory, and deliberately so — see {@link requireIdentityKey}, which does
|
|
64
|
+
* not depend on it surviving.
|
|
62
65
|
*/
|
|
63
66
|
identityKey;
|
|
64
67
|
/**
|
|
@@ -101,12 +104,34 @@ export class LoopingAgent extends Agent {
|
|
|
101
104
|
stores: this.runtime.stores
|
|
102
105
|
}));
|
|
103
106
|
}
|
|
104
|
-
/**
|
|
107
|
+
/**
|
|
108
|
+
* Which provider this agent's loops run on. Defaults to Workers AI; override
|
|
109
|
+
* to run on something else.
|
|
110
|
+
*
|
|
111
|
+
* The seam is here rather than in `models` because `models` memoizes, and a
|
|
112
|
+
* subclass overriding a memoized getter has to remember to keep the caching —
|
|
113
|
+
* a trap that only shows up as a performance bug. This is called once.
|
|
114
|
+
*
|
|
115
|
+
* `ModelRuntime` is the whole contract: return anything satisfying it and
|
|
116
|
+
* every loop in core keeps working unchanged. Core ships two implementations,
|
|
117
|
+
* one directory each — {@link file://../agent/workers-ai/index.ts
|
|
118
|
+
* `agent/workers-ai`} (the default below) and `@loopingai/core/anthropic` —
|
|
119
|
+
* and a third provider is a third directory exporting one
|
|
120
|
+
* {@link file://../agent/model.ts ModelRuntimeFactory}, not a change to
|
|
121
|
+
* anything on this path.
|
|
122
|
+
*
|
|
123
|
+
* Takes the resolved {@link ModelConfig} rather than reading `this.config`, so
|
|
124
|
+
* that this signature matches
|
|
125
|
+
* {@link file://../round/subagent.ts RecipeSubagentHost.modelRuntime} — an
|
|
126
|
+
* agent and its facet **must** run the same provider, and identical seams are
|
|
127
|
+
* what let one factory serve both instead of two hand-copied bodies.
|
|
128
|
+
*/
|
|
129
|
+
modelRuntime(model) {
|
|
130
|
+
return workersAIModels(this.env, model);
|
|
131
|
+
}
|
|
132
|
+
/** The model runtime for this instance, built lazily and memoized. */
|
|
105
133
|
get models() {
|
|
106
|
-
return (this._models ??=
|
|
107
|
-
ai: this.env.AI,
|
|
108
|
-
config: this.config.model
|
|
109
|
-
}));
|
|
134
|
+
return (this._models ??= this.modelRuntime(this.config.model));
|
|
110
135
|
}
|
|
111
136
|
/**
|
|
112
137
|
* What this agent's plugins are handed. Built from
|
|
@@ -193,12 +218,41 @@ export class LoopingAgent extends Agent {
|
|
|
193
218
|
onMessagesDisplaced: this.runtime.onMessagesDisplaced
|
|
194
219
|
}));
|
|
195
220
|
}
|
|
196
|
-
/**
|
|
221
|
+
/**
|
|
222
|
+
* The caller key, which is present on every path that can reach a plugin.
|
|
223
|
+
*
|
|
224
|
+
* ## Why this does not just read the field
|
|
225
|
+
*
|
|
226
|
+
* `identityKey` is set on the first turn and lives in the isolate. An isolate
|
|
227
|
+
* does not live as long as the work does: it can be evicted between two rounds
|
|
228
|
+
* of the same task, and it can be reset outright — "Durable Object connection
|
|
229
|
+
* closed because the object was reset" — while a Workflow step is mid-flight.
|
|
230
|
+
* The next call arrives on a fresh instance where the field is empty, and
|
|
231
|
+
* every per-caller thunk built off it throws.
|
|
232
|
+
*
|
|
233
|
+
* That failure is disproportionate to its cause. A plugin asking which caller
|
|
234
|
+
* it is serving gets an exception, mid-task, on an object whose entire purpose
|
|
235
|
+
* is to be that caller's — and because the throw happens inside a tool or a
|
|
236
|
+
* runtime resolution rather than at the edge, it surfaces as a failed branch
|
|
237
|
+
* rather than as anything an operator can read.
|
|
238
|
+
*
|
|
239
|
+
* So the object answers from itself. `define-agent` routes with
|
|
240
|
+
* `ns.get(ns.idFromName(identity.key))`, which means the caller key *is* this
|
|
241
|
+
* object's name and the platform hands it back on `ctx.id.name` — durable, free
|
|
242
|
+
* and correct by construction: an object cannot disagree with the name it was
|
|
243
|
+
* addressed by.
|
|
244
|
+
*
|
|
245
|
+
* The field still wins when it is set. `id.name` is undefined for an object
|
|
246
|
+
* addressed by `newUniqueId()` or a raw id string, so it is a fallback rather
|
|
247
|
+
* than the source of truth, and the throw is kept for the case where neither
|
|
248
|
+
* exists.
|
|
249
|
+
*/
|
|
197
250
|
requireIdentityKey() {
|
|
198
|
-
|
|
251
|
+
const key = this.identityKey ?? this.ctx.id.name;
|
|
252
|
+
if (!key) {
|
|
199
253
|
throw new Error("identity.key is required for per-caller isolation");
|
|
200
254
|
}
|
|
201
|
-
return this.identityKey;
|
|
255
|
+
return (this.identityKey = key);
|
|
202
256
|
}
|
|
203
257
|
/** The gateway callback channel for one turn. See {@link PushChannel}. */
|
|
204
258
|
push(context) {
|
package/dist/index.d.ts
CHANGED
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
* production bundle.
|
|
10
10
|
*/
|
|
11
11
|
export { createAgentRuntime, RuntimeSetupError, buildRecipeTools, collectToolFamilies, type AgentRuntime, type CreateAgentRuntimeOptions } from "./runtime/index.js";
|
|
12
|
-
export { PLUGIN_CONTRACT_VERSION, definePlugin, type AgentPlugin, type EmitProgress, type EnrichResultContext, type MainAgentToolContext, type PluginRequirements, type RecipeToolSet, type ResolveRuntimeContext, type ToolFamilyBuilder, type ToolFamilyContext, type TurnGateContext } from "./contract/plugin.js";
|
|
12
|
+
export { PLUGIN_CONTRACT_VERSION, definePlugin, restrictMainAgentTools, type AgentPlugin, type RestrictMainAgentToolsOptions, type EmitProgress, type EnrichResultContext, type MainAgentToolContext, type PluginRequirements, type RecipeToolSet, type ResolveRuntimeContext, type ToolFamilyBuilder, type ToolFamilyContext, type TurnGateContext } from "./contract/plugin.js";
|
|
13
13
|
export type { DelegationNames, RecipeLimits, ResolvedRecipe, SubtaskParams, SubtaskParamsSchema, SubtaskParamsShape, SubtaskTypeSpec, ValidatedRecipe } from "./contract/recipe.js";
|
|
14
14
|
export { RecipeValidationError, resolveLimits, validateRecipe, type RecipePolicy } from "./contract/validation.js";
|
|
15
15
|
export { ConfigError, DEFAULT_CORE_CONFIG, resolveConfig, type AgentLimits, type CoreConfig, type CoreConfigOverrides, type ModelConfig, type SessionConfig } from "./config.js";
|
|
16
16
|
export { parseGatewayOrigins, type A2ASecretsEnv, type AiEnv, type CoreEnv } from "./env.js";
|
|
17
|
-
export { CHUNK_SOFT_MS, MAX_CHUNKS_PER_BRANCH, STEP_TIMEOUT_MS, STEPS_PER_INSTANCE } from "./platform.js";
|
|
17
|
+
export { CHUNK_SOFT_MS, CHUNK_STEP, MAX_CHUNKS_PER_BRANCH, MAX_TOOL_CALL_MS, STEP_TIMEOUT_MS, STEPS_PER_INSTANCE } from "./platform.js";
|
|
18
18
|
export type { PluginStore } from "./db/db.js";
|
|
19
19
|
export { makeWorkspaceHandle, memoryWorkspaceBacking, WorkspaceLimitError, WORKSPACE_MAX_FILES, WORKSPACE_MAX_FILE_BYTES, type WorkspaceBacking, type WorkspaceEntry, type WorkspaceHandle } from "./subagent/workspace.js";
|
package/dist/index.js
CHANGED
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
* production bundle.
|
|
10
10
|
*/
|
|
11
11
|
export { createAgentRuntime, RuntimeSetupError, buildRecipeTools, collectToolFamilies } from "./runtime/index.js";
|
|
12
|
-
export { PLUGIN_CONTRACT_VERSION, definePlugin } from "./contract/plugin.js";
|
|
12
|
+
export { PLUGIN_CONTRACT_VERSION, definePlugin, restrictMainAgentTools } from "./contract/plugin.js";
|
|
13
13
|
export { RecipeValidationError, resolveLimits, validateRecipe } from "./contract/validation.js";
|
|
14
14
|
export { ConfigError, DEFAULT_CORE_CONFIG, resolveConfig } from "./config.js";
|
|
15
15
|
export { parseGatewayOrigins } from "./env.js";
|
|
16
|
-
export { CHUNK_SOFT_MS, MAX_CHUNKS_PER_BRANCH, STEP_TIMEOUT_MS, STEPS_PER_INSTANCE } from "./platform.js";
|
|
16
|
+
export { CHUNK_SOFT_MS, CHUNK_STEP, MAX_CHUNKS_PER_BRANCH, MAX_TOOL_CALL_MS, STEP_TIMEOUT_MS, STEPS_PER_INSTANCE } from "./platform.js";
|
|
17
17
|
export { makeWorkspaceHandle, memoryWorkspaceBacking, WorkspaceLimitError, WORKSPACE_MAX_FILES, WORKSPACE_MAX_FILE_BYTES } from "./subagent/workspace.js";
|
package/dist/platform.d.ts
CHANGED
|
@@ -1,23 +1,42 @@
|
|
|
1
|
+
import type { WorkflowStepConfig } from "cloudflare:workers";
|
|
1
2
|
/**
|
|
2
3
|
* What the Cloudflare Workflows runtime imposes, and the two numbers derived from
|
|
3
4
|
* it. Nothing here is a budget or a preference — see {@link file://./config.ts}
|
|
4
5
|
* for those, and note that no Recipe can reach these. They change when the
|
|
5
6
|
* platform changes, and for no other reason.
|
|
6
7
|
*
|
|
7
|
-
* The distinction is worth keeping sharp, because collapsing it
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* The distinction is worth keeping sharp, because collapsing it produces a
|
|
9
|
+
* specific bug: using a *turn count* to keep a step under the step timeout only
|
|
10
|
+
* works if you can predict how long a turn takes. You cannot. Time bounds time
|
|
11
|
+
* here; turns bound cost, over in `config.ts`.
|
|
11
12
|
*/
|
|
12
13
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
14
|
+
* The step timeout **we configure**, passed as `WorkflowStepConfig.timeout` on
|
|
15
|
+
* every step that can hold a model call or a container command.
|
|
16
|
+
*
|
|
17
|
+
* This is the one value in this file that is **not** a platform fact, and
|
|
18
|
+
* treating it as one is the mistake to avoid: ten minutes is Workflows'
|
|
19
|
+
* *default* step timeout, not its ceiling, and a step that never passes a config
|
|
20
|
+
* inherits it silently. Sizing {@link CHUNK_SOFT_MS} against that inherited
|
|
21
|
+
* default is what once ran a production task as ten four-minute slices, one of
|
|
22
|
+
* which blew the ten minutes anyway and was retried whole.
|
|
23
|
+
*
|
|
24
|
+
* The genuine platform facts are narrower and do not bind us: wall-clock time per
|
|
25
|
+
* step is effectively unlimited, and a step is bounded by **CPU** time. Measured
|
|
26
|
+
* over a 59-minute task, the chunk steps used ~100 ms of CPU against 3,146 s of
|
|
27
|
+
* wall — this ceiling is nowhere near the real one.
|
|
28
|
+
*
|
|
29
|
+
* It is still a ceiling worth having, because it is what turns a hung container
|
|
30
|
+
* into a retry rather than a task that never ends. {@link CHUNK_SOFT_MS} is sized
|
|
31
|
+
* against it, and that relationship is asserted in `platform.spec.ts`.
|
|
15
32
|
*/
|
|
16
33
|
export declare const STEP_TIMEOUT_MS: number;
|
|
17
34
|
/**
|
|
18
|
-
* Platform fact: a single Workflow instance may run
|
|
19
|
-
*
|
|
20
|
-
*
|
|
35
|
+
* Platform fact: a single Workflow instance may run 10,000 steps by default on the
|
|
36
|
+
* paid plan. Cloudflare will raise it to 25,000 on request — worth knowing, and
|
|
37
|
+
* worth not relying on: {@link MAX_CHUNKS_PER_BRANCH} is sized against the default
|
|
38
|
+
* so nothing here needs an account-level exception to be correct. See the
|
|
39
|
+
* worst-case product asserted in `platform.spec.ts`.
|
|
21
40
|
*/
|
|
22
41
|
export declare const STEPS_PER_INSTANCE = 10000;
|
|
23
42
|
/**
|
|
@@ -28,8 +47,52 @@ export declare const STEPS_PER_INSTANCE = 10000;
|
|
|
28
47
|
* This is the *only* thing keeping a step under the timeout. A subagent otherwise
|
|
29
48
|
* runs until its turn or wall-clock budget is spent, however many turns that takes
|
|
30
49
|
* — which is the point: the runner no longer guesses at turn duration.
|
|
50
|
+
*
|
|
51
|
+
* ## Why this is 15 minutes and not 4
|
|
52
|
+
*
|
|
53
|
+
* A chunk boundary is not free. It checkpoints, returns through two RPC hops,
|
|
54
|
+
* starts a fresh step, and re-hydrates the subagent — and for a coding agent it
|
|
55
|
+
* also means the container connection is re-established. Four minutes bought a
|
|
56
|
+
* boundary roughly every third tool call: a task that edited one README line spent
|
|
57
|
+
* 59 minutes across **ten** chunks, and the model was idle for most of each one,
|
|
58
|
+
* blocked on a single `sb_exec` running the project's test gate.
|
|
59
|
+
*
|
|
60
|
+
* ## Why it is not larger, which is the part that bit us
|
|
61
|
+
*
|
|
62
|
+
* This is a **soft** deadline, checked between turns (`stopWhen` in
|
|
63
|
+
* `subagent/run.ts`). A turn that starts one millisecond before it trips still runs
|
|
64
|
+
* to completion, so the real worst case is:
|
|
65
|
+
*
|
|
66
|
+
* chunk wall ≤ CHUNK_SOFT_MS + one whole turn
|
|
67
|
+
*
|
|
68
|
+
* and one turn is a model call plus a tool call. The old pair ignored that: four
|
|
69
|
+
* minutes soft under a ten-minute timeout looked like six minutes of headroom, but
|
|
70
|
+
* a single `sb_exec` may run for {@link MAX_TOOL_CALL_MS}, so a turn could add ten.
|
|
71
|
+
* That is not a hypothetical — it is the `WorkflowTimeoutError` that cost a
|
|
72
|
+
* production task ten minutes and a full chunk replay.
|
|
73
|
+
*
|
|
74
|
+
* So the headroom is sized against a whole turn, not against a guess:
|
|
75
|
+
* `STEP_TIMEOUT_MS - CHUNK_SOFT_MS` is 15 minutes, covering
|
|
76
|
+
* {@link MAX_TOOL_CALL_MS} of tool call plus five minutes for the model call and
|
|
77
|
+
* its provider retries. Asserted in `platform.spec.ts` — raise the step timeout
|
|
78
|
+
* before raising this.
|
|
31
79
|
*/
|
|
32
80
|
export declare const CHUNK_SOFT_MS: number;
|
|
81
|
+
/**
|
|
82
|
+
* The longest a **single tool call** may run, and a contract rather than a
|
|
83
|
+
* mechanism: core has no way to enforce it, because core installs no tools.
|
|
84
|
+
*
|
|
85
|
+
* It exists because {@link CHUNK_SOFT_MS} cannot be reasoned about without it. The
|
|
86
|
+
* soft deadline is checked between turns, so a host that lets one tool block for
|
|
87
|
+
* longer than the headroom under {@link STEP_TIMEOUT_MS} reintroduces exactly the
|
|
88
|
+
* step-timeout kill this pair is sized to prevent — and it reintroduces it
|
|
89
|
+
* invisibly, in a plugin, a long way from this file.
|
|
90
|
+
*
|
|
91
|
+
* A host installing a tool that can block (a shell, a container command, a fetch
|
|
92
|
+
* with no ceiling of its own) must bound it at or below this. See the `timeoutMs`
|
|
93
|
+
* passed to `@loopingai/plugins/computer` in looping-starter.
|
|
94
|
+
*/
|
|
95
|
+
export declare const MAX_TOOL_CALL_MS: number;
|
|
33
96
|
/**
|
|
34
97
|
* Hard ceiling on durable chunk steps for one Subtask branch. A backstop, not a
|
|
35
98
|
* budget: the Workflow *fails* a branch that reaches it, so reaching it is a bug.
|
|
@@ -39,8 +102,37 @@ export declare const CHUNK_SOFT_MS: number;
|
|
|
39
102
|
* 1. It exceeds every Recipe's `maxTurns`. A chunk that yields always advanced at
|
|
40
103
|
* least one turn, so a run takes at most `maxTurns` chunks however short they
|
|
41
104
|
* are — and they do get short, because `CHUNK_SOFT_MS` and progress events both
|
|
42
|
-
* end one early.
|
|
43
|
-
* estimate
|
|
105
|
+
* end one early. Counting turns is what makes the bound survive that; any
|
|
106
|
+
* estimate of turns-per-chunk would not, since neither of those two is
|
|
107
|
+
* predictable.
|
|
44
108
|
* 2. The worst-case step product stays under {@link STEPS_PER_INSTANCE}.
|
|
45
109
|
*/
|
|
46
110
|
export declare const MAX_CHUNKS_PER_BRANCH = 40;
|
|
111
|
+
/**
|
|
112
|
+
* What a step holding a model call or a container command configures instead of
|
|
113
|
+
* inheriting Workflows' defaults. Both defaults were measured wrong for this
|
|
114
|
+
* workload.
|
|
115
|
+
*
|
|
116
|
+
* **`timeout`.** The default is ten minutes. A step here holds a model call and
|
|
117
|
+
* its provider retries, or a container command running a project's test suite;
|
|
118
|
+
* neither fits in ten minutes reliably, and neither uses meaningful CPU while it
|
|
119
|
+
* waits. Left inherited, that default silently became the ceiling
|
|
120
|
+
* {@link CHUNK_SOFT_MS} was sized against.
|
|
121
|
+
*
|
|
122
|
+
* **`retries`.** The default is five attempts with exponential backoff from ten
|
|
123
|
+
* seconds. Against a fault that is not transient — a severed Durable Object stub
|
|
124
|
+
* — that produced five failures in under 10ms each, spread across 160 seconds of
|
|
125
|
+
* backoff that bought nothing. Three attempts still cover a genuinely transient
|
|
126
|
+
* fault, since the model call has its own provider-level retry underneath this,
|
|
127
|
+
* and a flat five-second delay stops a fast permanent failure being paid for at
|
|
128
|
+
* exponential rates.
|
|
129
|
+
*
|
|
130
|
+
* Here rather than in `/round` because the agent that most needs it may not be a
|
|
131
|
+
* round agent: a single-inference agent runs one model call in one step and has
|
|
132
|
+
* the same two problems, and importing this from `/round` would put the whole
|
|
133
|
+
* delegation engine in its bundle.
|
|
134
|
+
*
|
|
135
|
+
* For the **short** bookkeeping steps — `working`, `complete`, `notify` and
|
|
136
|
+
* friends — the defaults are fine and a shared config would only hide that.
|
|
137
|
+
*/
|
|
138
|
+
export declare const CHUNK_STEP: WorkflowStepConfig;
|
package/dist/platform.js
CHANGED
|
@@ -4,20 +4,38 @@
|
|
|
4
4
|
* for those, and note that no Recipe can reach these. They change when the
|
|
5
5
|
* platform changes, and for no other reason.
|
|
6
6
|
*
|
|
7
|
-
* The distinction is worth keeping sharp, because collapsing it
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* The distinction is worth keeping sharp, because collapsing it produces a
|
|
8
|
+
* specific bug: using a *turn count* to keep a step under the step timeout only
|
|
9
|
+
* works if you can predict how long a turn takes. You cannot. Time bounds time
|
|
10
|
+
* here; turns bound cost, over in `config.ts`.
|
|
11
11
|
*/
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
13
|
+
* The step timeout **we configure**, passed as `WorkflowStepConfig.timeout` on
|
|
14
|
+
* every step that can hold a model call or a container command.
|
|
15
|
+
*
|
|
16
|
+
* This is the one value in this file that is **not** a platform fact, and
|
|
17
|
+
* treating it as one is the mistake to avoid: ten minutes is Workflows'
|
|
18
|
+
* *default* step timeout, not its ceiling, and a step that never passes a config
|
|
19
|
+
* inherits it silently. Sizing {@link CHUNK_SOFT_MS} against that inherited
|
|
20
|
+
* default is what once ran a production task as ten four-minute slices, one of
|
|
21
|
+
* which blew the ten minutes anyway and was retried whole.
|
|
22
|
+
*
|
|
23
|
+
* The genuine platform facts are narrower and do not bind us: wall-clock time per
|
|
24
|
+
* step is effectively unlimited, and a step is bounded by **CPU** time. Measured
|
|
25
|
+
* over a 59-minute task, the chunk steps used ~100 ms of CPU against 3,146 s of
|
|
26
|
+
* wall — this ceiling is nowhere near the real one.
|
|
27
|
+
*
|
|
28
|
+
* It is still a ceiling worth having, because it is what turns a hung container
|
|
29
|
+
* into a retry rather than a task that never ends. {@link CHUNK_SOFT_MS} is sized
|
|
30
|
+
* against it, and that relationship is asserted in `platform.spec.ts`.
|
|
15
31
|
*/
|
|
16
|
-
export const STEP_TIMEOUT_MS =
|
|
32
|
+
export const STEP_TIMEOUT_MS = 30 * 60_000;
|
|
17
33
|
/**
|
|
18
|
-
* Platform fact: a single Workflow instance may run
|
|
19
|
-
*
|
|
20
|
-
*
|
|
34
|
+
* Platform fact: a single Workflow instance may run 10,000 steps by default on the
|
|
35
|
+
* paid plan. Cloudflare will raise it to 25,000 on request — worth knowing, and
|
|
36
|
+
* worth not relying on: {@link MAX_CHUNKS_PER_BRANCH} is sized against the default
|
|
37
|
+
* so nothing here needs an account-level exception to be correct. See the
|
|
38
|
+
* worst-case product asserted in `platform.spec.ts`.
|
|
21
39
|
*/
|
|
22
40
|
export const STEPS_PER_INSTANCE = 10_000;
|
|
23
41
|
/**
|
|
@@ -28,8 +46,52 @@ export const STEPS_PER_INSTANCE = 10_000;
|
|
|
28
46
|
* This is the *only* thing keeping a step under the timeout. A subagent otherwise
|
|
29
47
|
* runs until its turn or wall-clock budget is spent, however many turns that takes
|
|
30
48
|
* — which is the point: the runner no longer guesses at turn duration.
|
|
49
|
+
*
|
|
50
|
+
* ## Why this is 15 minutes and not 4
|
|
51
|
+
*
|
|
52
|
+
* A chunk boundary is not free. It checkpoints, returns through two RPC hops,
|
|
53
|
+
* starts a fresh step, and re-hydrates the subagent — and for a coding agent it
|
|
54
|
+
* also means the container connection is re-established. Four minutes bought a
|
|
55
|
+
* boundary roughly every third tool call: a task that edited one README line spent
|
|
56
|
+
* 59 minutes across **ten** chunks, and the model was idle for most of each one,
|
|
57
|
+
* blocked on a single `sb_exec` running the project's test gate.
|
|
58
|
+
*
|
|
59
|
+
* ## Why it is not larger, which is the part that bit us
|
|
60
|
+
*
|
|
61
|
+
* This is a **soft** deadline, checked between turns (`stopWhen` in
|
|
62
|
+
* `subagent/run.ts`). A turn that starts one millisecond before it trips still runs
|
|
63
|
+
* to completion, so the real worst case is:
|
|
64
|
+
*
|
|
65
|
+
* chunk wall ≤ CHUNK_SOFT_MS + one whole turn
|
|
66
|
+
*
|
|
67
|
+
* and one turn is a model call plus a tool call. The old pair ignored that: four
|
|
68
|
+
* minutes soft under a ten-minute timeout looked like six minutes of headroom, but
|
|
69
|
+
* a single `sb_exec` may run for {@link MAX_TOOL_CALL_MS}, so a turn could add ten.
|
|
70
|
+
* That is not a hypothetical — it is the `WorkflowTimeoutError` that cost a
|
|
71
|
+
* production task ten minutes and a full chunk replay.
|
|
72
|
+
*
|
|
73
|
+
* So the headroom is sized against a whole turn, not against a guess:
|
|
74
|
+
* `STEP_TIMEOUT_MS - CHUNK_SOFT_MS` is 15 minutes, covering
|
|
75
|
+
* {@link MAX_TOOL_CALL_MS} of tool call plus five minutes for the model call and
|
|
76
|
+
* its provider retries. Asserted in `platform.spec.ts` — raise the step timeout
|
|
77
|
+
* before raising this.
|
|
31
78
|
*/
|
|
32
|
-
export const CHUNK_SOFT_MS =
|
|
79
|
+
export const CHUNK_SOFT_MS = 15 * 60_000;
|
|
80
|
+
/**
|
|
81
|
+
* The longest a **single tool call** may run, and a contract rather than a
|
|
82
|
+
* mechanism: core has no way to enforce it, because core installs no tools.
|
|
83
|
+
*
|
|
84
|
+
* It exists because {@link CHUNK_SOFT_MS} cannot be reasoned about without it. The
|
|
85
|
+
* soft deadline is checked between turns, so a host that lets one tool block for
|
|
86
|
+
* longer than the headroom under {@link STEP_TIMEOUT_MS} reintroduces exactly the
|
|
87
|
+
* step-timeout kill this pair is sized to prevent — and it reintroduces it
|
|
88
|
+
* invisibly, in a plugin, a long way from this file.
|
|
89
|
+
*
|
|
90
|
+
* A host installing a tool that can block (a shell, a container command, a fetch
|
|
91
|
+
* with no ceiling of its own) must bound it at or below this. See the `timeoutMs`
|
|
92
|
+
* passed to `@loopingai/plugins/computer` in looping-starter.
|
|
93
|
+
*/
|
|
94
|
+
export const MAX_TOOL_CALL_MS = 10 * 60_000;
|
|
33
95
|
/**
|
|
34
96
|
* Hard ceiling on durable chunk steps for one Subtask branch. A backstop, not a
|
|
35
97
|
* budget: the Workflow *fails* a branch that reaches it, so reaching it is a bug.
|
|
@@ -39,8 +101,40 @@ export const CHUNK_SOFT_MS = 4 * 60_000;
|
|
|
39
101
|
* 1. It exceeds every Recipe's `maxTurns`. A chunk that yields always advanced at
|
|
40
102
|
* least one turn, so a run takes at most `maxTurns` chunks however short they
|
|
41
103
|
* are — and they do get short, because `CHUNK_SOFT_MS` and progress events both
|
|
42
|
-
* end one early.
|
|
43
|
-
* estimate
|
|
104
|
+
* end one early. Counting turns is what makes the bound survive that; any
|
|
105
|
+
* estimate of turns-per-chunk would not, since neither of those two is
|
|
106
|
+
* predictable.
|
|
44
107
|
* 2. The worst-case step product stays under {@link STEPS_PER_INSTANCE}.
|
|
45
108
|
*/
|
|
46
109
|
export const MAX_CHUNKS_PER_BRANCH = 40;
|
|
110
|
+
/**
|
|
111
|
+
* What a step holding a model call or a container command configures instead of
|
|
112
|
+
* inheriting Workflows' defaults. Both defaults were measured wrong for this
|
|
113
|
+
* workload.
|
|
114
|
+
*
|
|
115
|
+
* **`timeout`.** The default is ten minutes. A step here holds a model call and
|
|
116
|
+
* its provider retries, or a container command running a project's test suite;
|
|
117
|
+
* neither fits in ten minutes reliably, and neither uses meaningful CPU while it
|
|
118
|
+
* waits. Left inherited, that default silently became the ceiling
|
|
119
|
+
* {@link CHUNK_SOFT_MS} was sized against.
|
|
120
|
+
*
|
|
121
|
+
* **`retries`.** The default is five attempts with exponential backoff from ten
|
|
122
|
+
* seconds. Against a fault that is not transient — a severed Durable Object stub
|
|
123
|
+
* — that produced five failures in under 10ms each, spread across 160 seconds of
|
|
124
|
+
* backoff that bought nothing. Three attempts still cover a genuinely transient
|
|
125
|
+
* fault, since the model call has its own provider-level retry underneath this,
|
|
126
|
+
* and a flat five-second delay stops a fast permanent failure being paid for at
|
|
127
|
+
* exponential rates.
|
|
128
|
+
*
|
|
129
|
+
* Here rather than in `/round` because the agent that most needs it may not be a
|
|
130
|
+
* round agent: a single-inference agent runs one model call in one step and has
|
|
131
|
+
* the same two problems, and importing this from `/round` would put the whole
|
|
132
|
+
* delegation engine in its bundle.
|
|
133
|
+
*
|
|
134
|
+
* For the **short** bookkeeping steps — `working`, `complete`, `notify` and
|
|
135
|
+
* friends — the defaults are fine and a shared config would only hide that.
|
|
136
|
+
*/
|
|
137
|
+
export const CHUNK_STEP = {
|
|
138
|
+
timeout: STEP_TIMEOUT_MS,
|
|
139
|
+
retries: { limit: 3, delay: 5_000, backoff: "constant" }
|
|
140
|
+
};
|