@ultimat3/ai 2.0.0 → 3.0.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/CLAUDE.md +161 -3
- package/README.md +139 -1
- package/package.json +10 -9
- package/src/agent-facts.ts +70 -0
- package/src/agent-job.ts +97 -0
- package/src/agent-transcript.ts +94 -0
- package/src/agent.ts +170 -61
- package/src/errors.ts +2 -0
- package/src/gateway.ts +17 -9
- package/src/hive-errors.ts +30 -0
- package/src/hive-pool.ts +90 -0
- package/src/hive-result.ts +96 -0
- package/src/hive.ts +177 -0
- package/src/index.ts +11 -2
- package/src/openai-provider.ts +17 -3
- package/src/provider.ts +18 -3
- package/src/tools.ts +95 -8
package/CLAUDE.md
CHANGED
|
@@ -4,7 +4,12 @@ Tier 4. May import tier 0–3: `core schema i18n money time cache seo entity pol
|
|
|
4
4
|
query jobs realtime`. **Never** `mcp manifest render pwa ui admin testing cli`.
|
|
5
5
|
|
|
6
6
|
Declared today: `action` (the primitive `llm()` returns), `cache` (semantic cache), `core`,
|
|
7
|
-
`db` (pgvector), `money`, `policy`, `schema`, `time`.
|
|
7
|
+
`db` (pgvector), `jobs` (`agentJob()`), `money`, `policy`, `schema`, `time`.
|
|
8
|
+
|
|
9
|
+
**`jobs` was declared 2026-08, for `agentJob()`.** `packages/action/src/job-handle.ts`'s header
|
|
10
|
+
already named this package as the home: `isJobHandle` needs `kind === 'job'` plus membership of a
|
|
11
|
+
WeakMap only `job()` writes, and `action` and `jobs` are both tier 3, so the bridge has to live at
|
|
12
|
+
tier 4+. Downward edge, nothing new in the tier table.
|
|
8
13
|
|
|
9
14
|
`mcp` is the same tier, so the LLM-tool projection is restated structurally in `tools.ts`
|
|
10
15
|
rather than imported. Same contract, two wire formats — and the same *decision*: `toLlmTools` and
|
|
@@ -44,10 +49,16 @@ until 2026-08, naming a tool no catalog contained (`llm.test.ts`, `agent.test.ts
|
|
|
44
49
|
| `pg-vector-sql.ts` | every pgvector statement: DDL, upsert, cosine, FTS, RRF fusion |
|
|
45
50
|
| `pg-vector.ts` | `PgVectorStore` — the production store |
|
|
46
51
|
| `rag.ts` | chunker, retriever, reranker, budgeted context assembler |
|
|
47
|
-
| `tools.ts` | action → LLM tool definition; `runLlmToolCall` |
|
|
52
|
+
| `tools.ts` | action → LLM tool definition; the `AgentTool` union and `asProjectableAction`; `runLlmToolCall` |
|
|
48
53
|
| `llm.ts` | `llm()` — the model call, declared as an `action`; and what a streamed answer must satisfy |
|
|
49
54
|
| `llm-stream.ts` | `.stream()`'s plumbing: the sink, the ambient mark, the one-turn drive |
|
|
50
55
|
| `agent.ts` | `agent()` — the tool loop, declared as an `action` |
|
|
56
|
+
| `agent-transcript.ts` | what one turn leaves in the transcript: the assistant replay, the tool results, the correction |
|
|
57
|
+
| `agent-facts.ts` | `describeAgents()` — the agent registry and the row a manifest publishes |
|
|
58
|
+
| `agent-job.ts` | `agentJob()` — an agent as a real `JobHandle`, composed from `job()` |
|
|
59
|
+
| `hive.ts` | `hive()` — one action fanned out over many inputs, declared as an `action` |
|
|
60
|
+
| `hive-result.ts` | `HiveMember` / `HiveResult`, and the SCHEMA built from the member's own `output` |
|
|
61
|
+
| `hive-errors.ts` | the `X_HIVE_*` class; its code and title stay in `errors.ts` |
|
|
51
62
|
| `redaction.ts` | the one gate between `vars()` and the provider: a `Secret` never reaches a prompt |
|
|
52
63
|
| `eval-errors.ts` | the five `X_EVAL_*` classes; their codes and titles stay in `errors.ts` |
|
|
53
64
|
| `runtime.ts` | the ambient gateway / embedder / semantic caches an `llm()` reaches |
|
|
@@ -92,6 +103,12 @@ until 2026-08, naming a tool no catalog contained (`llm.test.ts`, `agent.test.ts
|
|
|
92
103
|
zero leaks the ceiling upward on every release.
|
|
93
104
|
- Cost is `Money` (integer minor units), rounded **up**. Never a float, never a division
|
|
94
105
|
that loses a fraction.
|
|
106
|
+
- **The gateway's two reads of a provider's throw are total.** A `Provider` is the APP's object, so
|
|
107
|
+
the value it rejects with is one the framework did not build: `isRetryable` indexes it (a getter,
|
|
108
|
+
or a `Proxy` trap) and fails closed if the read raises, and the failure line goes through core's
|
|
109
|
+
`renderThrowable` rather than `error.message` / `String(error)` — a renderer that throws replaces
|
|
110
|
+
`X_AI_PROVIDER_UNAVAILABLE` with a bare `TypeError` nothing catches by code, and it bounds a
|
|
111
|
+
provider's 1MB body out of the `cause`.
|
|
95
112
|
- Every non-2xx and every in-band `error` frame becomes `AiTransportError`, which carries a real
|
|
96
113
|
`status` field — that field IS the gateway's retry rule. A body parsed as a message would read
|
|
97
114
|
as an empty, successful answer, which is the one outcome nothing downstream can detect.
|
|
@@ -261,15 +278,156 @@ until 2026-08, naming a tool no catalog contained (`llm.test.ts`, `agent.test.ts
|
|
|
261
278
|
(`X_AGENT_MAX_TURNS`, never a partial answer), by `budget.tokensPerRun` (the ledger's `request`
|
|
262
279
|
scope, which accumulates across turns) and by `maxToolResultChars` — the transcript IS the
|
|
263
280
|
request, so an untruncated tool result is re-billed once per remaining turn.
|
|
281
|
+
- **`tools` takes the real `action()`, adapted at this package's edge — decided 2026-08 (issue
|
|
282
|
+
#124).** It took `ProjectableAction` alone, which no `action()` structurally satisfies (an
|
|
283
|
+
action is `as`/`tool`/`openapi`/`job`/`contract` and the callable, never `run`), so the shape
|
|
284
|
+
the README documents was a `TS2741` and every test here hand-built a stand-in — which is why
|
|
285
|
+
the suite was green over an API that did not compile. `AgentTool = AnyAction |
|
|
286
|
+
ProjectableAction` and `asProjectableAction` are the fix, the same union `@ultimat3/mcp`'s
|
|
287
|
+
`ListedPrimitive` already accepted. **Not** a `run` member on the action facade, which was the
|
|
288
|
+
obvious alternative and is wrong three times over: it duplicates `.as()` (axiom 1), it would
|
|
289
|
+
offer a tool named `''` for an action `registerActions` has not named yet, and it would NOT
|
|
290
|
+
collapse `mcp`'s adapter, whose `toWireSchema` narrows to the subset that server's arg
|
|
291
|
+
validator can enforce while this one publishes the Messages API's (`packages/mcp/src/
|
|
292
|
+
from-action.ts` header). Two wire formats, two projections, one `invoke`.
|
|
293
|
+
- Projection happens on the FIRST RUN, memoised — never at declaration. `agent()` is evaluated at
|
|
294
|
+
module scope and `registerAction` stamps a name at boot, so `actionName()` at declaration would
|
|
295
|
+
make the ordinary `export const publishPost = action(...)` beside it `X_ACTION_UNREGISTERED`.
|
|
296
|
+
- **An `agent()` is a tool of another `agent()`** — it returns an action, and an action is what a
|
|
297
|
+
tool is. That is the supervisor/sub-agent shape, with no `hive()` and no ninth primitive; the
|
|
298
|
+
sub-agent runs under the same actor, through its own policy.
|
|
264
299
|
- A tool listed in `agent({ tools })` that is not `mcp: { expose: true }` is
|
|
265
300
|
`X_AGENT_TOOL_UNEXPOSED` **at declaration**, not filtered at the call: a silently dropped tool
|
|
266
301
|
reads as offered and is not. `isMcpExposed` is the one predicate, so an in-app agent and an
|
|
267
|
-
external MCP client see the same catalogue.
|
|
302
|
+
external MCP client see the same catalogue. Asked of the DECLARATION, not of the projection,
|
|
303
|
+
because exposure needs no name and the name does not exist yet.
|
|
304
|
+
- **`ctx.signal` is read, at three points.** `throwIfAborted` at the top of every turn and before
|
|
305
|
+
every tool batch, and `GenerateRequest.signal` forwarded into `fetch` by both providers. The
|
|
306
|
+
transcript IS the request, so a loop that keeps going after the caller disconnects re-sends it
|
|
307
|
+
once per remaining turn, runs every remaining tool's side effect and discards the answer —
|
|
308
|
+
eight provider calls for an answer nothing reads. `X_ABORTED` is core's, already shipped; no
|
|
309
|
+
new code. `signal` is deliberately absent from `cacheKeyFor` and from every estimate: it says
|
|
310
|
+
whether a call was ABANDONED, never what it asked for.
|
|
311
|
+
- **The tools of ONE turn run concurrently**, unbounded within the turn, results paired by index
|
|
312
|
+
so a fast tool cannot be matched to a slow tool's `tool_use` id. Serial cost 5x wall clock for a
|
|
313
|
+
turn that asked for five tools and nothing in the types or the docs said so. No second ceiling
|
|
314
|
+
here: the batch is what one model turn asked for, each entry is an action with its own `policy`
|
|
315
|
+
and `rateLimit`, and a tool that calls a model still queues on the ledger's root turnstile.
|
|
316
|
+
Guarantee is "no tool STARTS after the abort" — a tool already in flight unwinds through its
|
|
317
|
+
own handler's reading of `ctx.signal`, which is the action's to make.
|
|
318
|
+
- **`onTurn` is the per-turn observation, and `.stream()` on `agent()` is deliberately not shipped
|
|
319
|
+
yet.** A 90-second multi-turn run emitted nothing until it returned. `onTurn` reports facts
|
|
320
|
+
only — turn, model, tool names, stop reason, usage, that turn's cost — never the transcript and
|
|
321
|
+
never the actor, and the same facts land on the span as an `agent.turn` event so a run
|
|
322
|
+
declaring no hook is still readable in a trace. A throw from it FAILS the run: an observer that
|
|
323
|
+
quietly stopped working reads exactly like one that is fine. Tokens on a screen is a different
|
|
324
|
+
contract from turns in a loop, and half-shipping it would be the second path axiom 1 refuses.
|
|
268
325
|
- **No semantic cache on `agent()`.** Similar prompts do not have similar answers once the answer
|
|
269
326
|
depends on what `lookupOrder` returned this second.
|
|
327
|
+
- **Every `tool_use` block the transcript replays is answered by a `tool_result` in the very next
|
|
328
|
+
message** — the Messages API's own rule, and `agent-transcript.ts` owns both halves of it for
|
|
329
|
+
that reason. Two paths broke it, both through `respond`, which is filtered out of the calls
|
|
330
|
+
that RUN and replayed like any other block: a turn emitting a tool call AND `respond` together
|
|
331
|
+
(ordinary parallel tool use), and a `respond` whose input failed the output schema, followed by
|
|
332
|
+
a plain user message. Both were a 400 (`tool_use ids were found without tool_result blocks`),
|
|
333
|
+
i.e. `X_AI_PROVIDER_UNAVAILABLE` in place of a completed run, and `agent.test.ts` never mixed
|
|
334
|
+
the two so neither shipped visible. An unaccepted `respond` now comes back as its own
|
|
335
|
+
`tool_result`, `is_error`, saying why — the answer is SUPERSEDED, not wrong: it was written
|
|
336
|
+
before the results of the tools the same turn asked for existed, so the loop continues and the
|
|
337
|
+
model answers again with them in hand. Discarding the block instead would have been the other
|
|
338
|
+
legal fix and loses the record; USING the speculative answer would skip the tool results the
|
|
339
|
+
model itself asked for, after those tools already ran.
|
|
340
|
+
- **A tool result is rendered totally.** `runLlmToolCall` returns `content: string` and the loop
|
|
341
|
+
TRUNCATES it, so `JSON.stringify`'s other two answers both have to be handled: `undefined` for
|
|
342
|
+
an action that returns nothing (`'null'`), and a throw on a bigint, a cycle or a `toJSON` of
|
|
343
|
+
the value's own — reported as "the tool ran and its result is not JSON", never as a failure,
|
|
344
|
+
because a model told the tool failed calls it again and buys its side effects twice. The throw
|
|
345
|
+
it catches is read with `stringField`, never `typeof error.code === 'string'`: the value is an
|
|
346
|
+
app's, so the probe is a getter call or a `Proxy` trap inside the catch block.
|
|
270
347
|
- `AiMessage.content` widened to `string | readonly AiContentBlock[]` for this: a `tool_result`
|
|
271
348
|
has to name the `tool_use` it answers and a string has nowhere to put the id. The block field
|
|
272
349
|
names are the Messages API's, so `body()` passes them through untouched.
|
|
350
|
+
- **`hive()` is a fan-out, and the FOURTH instance of the factory rule** (after `llm()`,
|
|
351
|
+
`backfill()` and `agent()`) — it returns an `action`, never a ninth primitive. It exists because
|
|
352
|
+
the alternative is a hand-rolled `Promise.all` over `agent()` calls, and that loop gets four
|
|
353
|
+
things wrong every time: the actor, the order, the difference between ran-and-failed and
|
|
354
|
+
never-ran, and the ceiling.
|
|
355
|
+
- **`HiveResult` is a SCHEMA, not an interface**, built from the member action's own `output` and
|
|
356
|
+
embedded in the `ok` arm. That is what makes a hive project to OpenAPI, the typed client, the
|
|
357
|
+
MCP `outputSchema` and the manifest like any other action; a hand-written interface would have
|
|
358
|
+
given the type and none of the six projections, which is the whole reason it is a factory.
|
|
359
|
+
- **Three arms — `ok` / `failed` / `skipped` — never two.** A member that ran and threw and a
|
|
360
|
+
member that never ran are different facts, and an aborted sibling is the second. Two arms make
|
|
361
|
+
"the hive stopped early" indistinguishable from "every remaining item is bad data", which is
|
|
362
|
+
the difference between retrying the tail and fixing the source. `skipped` gets its own counter
|
|
363
|
+
beside `ok` and `failed` for the same reason: three arms and two counters means every caller
|
|
364
|
+
writes `members.length - ok - failed` once, and writes it wrong once.
|
|
365
|
+
- **`members` is in SPLIT order, always**, filled by index rather than pushed on settle, with
|
|
366
|
+
`index` on every arm so a caller can join a result back to its row without depending on array
|
|
367
|
+
position surviving a filter.
|
|
368
|
+
- **The hive never names an actor.** `split` derives member inputs from `input` and `ctx` and
|
|
369
|
+
from nothing a model emitted; each member runs through its own callable, so `invoke` applies
|
|
370
|
+
the member's own `policy` with `ctx.actor` untouched. There is no `as(actor)` in the factory —
|
|
371
|
+
the same boundary `agent()` holds, and the reason both belong in the framework.
|
|
372
|
+
- **No hive-specific budget code, deliberately.** One derived ledger for the run, `withBudget`
|
|
373
|
+
around the pool, each member's `agent()` deriving again — and the ceiling holds under
|
|
374
|
+
parallelism because `reserve` DEBITS on the root's turnstile before the call. Three members
|
|
375
|
+
against a ceiling only one fits leave exactly one `ok`; that is asserted through the hive
|
|
376
|
+
rather than asserted about the ledger, because the ledger already promised it.
|
|
377
|
+
- **A member's throw is RECORDED, whatever it is.** `failureOf` reads it with `isThrownError` and
|
|
378
|
+
`stringField` from core, never `error instanceof Error` and `.message`: a member is an app's
|
|
379
|
+
action, so a `Proxy` makes `instanceof` run a `getPrototypeOf` trap, and a throw there takes
|
|
380
|
+
down the whole hive — the one outcome the three arms exist to prevent. `skipped` has two
|
|
381
|
+
reasons, because they are two facts: `SKIPPED_ABORTED` (a sibling failed under `'abort'`) and
|
|
382
|
+
`SKIPPED_NO_INPUT` (the split produced nothing at that index). One string for both sent a
|
|
383
|
+
caller to retry a tail that was never cut.
|
|
384
|
+
- **`onMemberError` is required.** `'abort'` stops and leaves the rest `skipped`; `'collect'`
|
|
385
|
+
harvests. Both are right for somebody, so neither may be inherited silently.
|
|
386
|
+
- `concurrency` defaults to 4 and `minMembers` to 2, and neither number is measured off any run —
|
|
387
|
+
the framework cannot know a provider's concurrency allowance. A below-floor split still runs
|
|
388
|
+
every input it produced, serially: dropping one would be silent data loss.
|
|
389
|
+
- An empty split is `X_HIVE_EMPTY`, never a successful run of zero members — "0 ok, 0 failed"
|
|
390
|
+
cannot be told apart from a query that returned no rows and nobody noticed.
|
|
391
|
+
- An aborted `ctx` unwinds the whole hive with `X_ABORTED`, which is a DIFFERENT event from
|
|
392
|
+
`onMemberError: 'abort'`: the latter is a completed run with a partial harvest worth returning,
|
|
393
|
+
the former has nobody left to hand it to.
|
|
394
|
+
- **`describeAgents()` publishes what an `ActionDescriptor` cannot.** An agent projects to the same
|
|
395
|
+
descriptor as any other action, and that descriptor knows nothing about turns, tools, models or
|
|
396
|
+
prompt hashes — so "how far can this loop and what may it call" had no answer outside the source.
|
|
397
|
+
Same shape as `describePrompts()` / `describeEvals()`, and deliberately NOT a new
|
|
398
|
+
`ActionDescriptor` field: `@ultimat3/action` is tier 3 and knows nothing about models.
|
|
399
|
+
The facts are a THUNK, resolved when asked: `agent()` runs at module scope beside the actions it
|
|
400
|
+
lists, and every name in a row is stamped by `registerAction` at boot. An agent still carrying no
|
|
401
|
+
name has no row — not a silent drop, but the absence of a capability: an action with no name
|
|
402
|
+
reaches no route, no tool catalogue and no queue. `named()` builds a TWIN where registration
|
|
403
|
+
names in place, so an agent renamed that way is absent for the same reason; register it instead.
|
|
404
|
+
- **`agentJob()` closes #125 for the agent case, by COMPOSING `job()`** — the returned value is one
|
|
405
|
+
`job()` seated in that package's own registry, so `.enqueue()`, the outbox, the worker's
|
|
406
|
+
cancellation, the dead-letter path, `x jobs show` and its manifest row arrive without a line here.
|
|
407
|
+
Never an imitation handle: `isJobHandle` needs `kind === 'job'` plus membership of a WeakMap only
|
|
408
|
+
`job()` writes, which is exactly what stops a second execution path existing.
|
|
409
|
+
- `name`, `tenant` and `retry` are REQUIRED, no defaults. `name` because a job name is the durable
|
|
410
|
+
queue key that queued, retrying and dead-lettered rows already carry — deriving it from the
|
|
411
|
+
export name would move delivery when somebody renames a variable. `tenant` and `retry` because
|
|
412
|
+
`jobs` states that every candidate default for `tenant` is a cross-tenant read waiting for the
|
|
413
|
+
first job that takes an org id in its input. Both are `TS2741` when omitted AND have runtime
|
|
414
|
+
backstops (`X_JOB_TENANT_REQUIRED`, the `retry.attempts` assert), for generated and JS callers.
|
|
415
|
+
- **Both reads of `target.job()` are LAZY**, and that is load-bearing: `actionName()` throws
|
|
416
|
+
`X_ACTION_UNREGISTERED` until boot stamps the export name, and `agentJob()` is evaluated at
|
|
417
|
+
module scope right beside the `agent()` it wraps. Same rule as `agent()`'s tool projection and
|
|
418
|
+
`describeAgents()`' thunk — third instance in this package.
|
|
419
|
+
- **The at-least-once trap is DOCUMENTED, not enforced, and that is a decision with evidence.**
|
|
420
|
+
`idempotencyKey` dedupes the ENQUEUE, never the ATTEMPT: a lost lease re-runs the agent from the
|
|
421
|
+
top, as does every page `backfill()` replays. So every tool an `agentJob()`'d agent may call has
|
|
422
|
+
to be idempotent. The framework cannot check it: `mutates` is not a fact an `action()` declares
|
|
423
|
+
— it exists only in `@ultimat3/mcp`, whose `projectable.ts` sets it to `true` for EVERY action —
|
|
424
|
+
so a read-only `lookupOrder` and a destructive `issueRefund` are indistinguishable, and
|
|
425
|
+
`ActionFacade`'s `Pick<>` carries no `idempotent` either (only `describe()` does, and that needs
|
|
426
|
+
a name). `isMutator` IS legible without a name, but `mutator()` is the local-first write
|
|
427
|
+
primitive: refusing only those would catch almost none of the risk while reading as if it caught
|
|
428
|
+
all of it. A wrong refusal is worse than a stated obligation, so the obligation is stated — in
|
|
429
|
+
`AgentJobOptions.idempotencyKey`'s doc comment and in the README, both naming the second refund.
|
|
430
|
+
|
|
273
431
|
- **`configureAi({ redact })` is the one seam between `vars()` and the provider.** `vars()` is the
|
|
274
432
|
one declared place a model call loads data, so it is the one place a redactor can see the row
|
|
275
433
|
before it leaves the process; the redactor sees the whole RENDERED prompt and the system prompt,
|
package/README.md
CHANGED
|
@@ -300,23 +300,161 @@ export const support = agent({
|
|
|
300
300
|
output: t.object({ answer: t.string }),
|
|
301
301
|
prompt: supportPrompt,
|
|
302
302
|
vars: ({ input }) => ({ orderId: input.orderId }),
|
|
303
|
-
tools: [lookupOrder, issueRefund], // actions, each mcp.expose
|
|
303
|
+
tools: [lookupOrder, issueRefund], // real actions, each mcp.expose
|
|
304
304
|
maxTurns: 6,
|
|
305
305
|
maxToolResultChars: 4_000,
|
|
306
306
|
budget: { tokensPerRun: 200_000, costPerCall: { minor: 50, currency: 'USD' } },
|
|
307
307
|
policy: can('order:support'),
|
|
308
|
+
onTurn: ({ turn, toolCalls, cost }) => progress.push({ turn, toolCalls, cost }),
|
|
308
309
|
});
|
|
309
310
|
```
|
|
310
311
|
|
|
312
|
+
`tools` takes the `action()` an app already wrote — `[lookupOrder, issueRefund]`, the imports
|
|
313
|
+
themselves. `As of 2026-08`: it took a hand-shaped `ProjectableAction` until then, so the line
|
|
314
|
+
above was a `TS2741` against every real action (issue #124) and the only thing that satisfied it
|
|
315
|
+
was a stand-in written for a test.
|
|
316
|
+
|
|
317
|
+
An `agent()` returns an action, so **an agent is a tool of another agent** — a supervisor lists a
|
|
318
|
+
sub-agent in its own `tools` and the sub-agent runs under the same actor, through the same policy.
|
|
319
|
+
No `hive()`, no supervisor primitive: it falls out of the factory rule.
|
|
320
|
+
|
|
311
321
|
| Rule | Why |
|
|
312
322
|
|---|---|
|
|
313
323
|
| the actor is **`ctx.actor`**, read once, never from the model | this is the mistake a hand-rolled loop ships, and the reason the loop belongs in the framework |
|
|
324
|
+
| an aborted `ctx` unwinds the run — at the top of every turn, before every tool batch, and on the socket | the transcript IS the request, so a loop that keeps going after the caller disconnects re-sends it once per remaining turn, runs every remaining side effect and discards the answer. `ctx.signal` rides on `GenerateRequest` too, so a call already in flight is cut rather than paid for |
|
|
325
|
+
| the tools of **one turn** run concurrently, results paired by `tool_use` id | a turn asking for five tools cost 5x wall clock and nothing said so. Order is positional, never by completion; the batch is bounded by what one turn asked for, and each tool is an action with its own `policy` and `rateLimit`, so a second ceiling here would be a throttle competing with those |
|
|
326
|
+
| `onTurn` reports each completed turn as it happens (and an `agent.turn` span event, always) | a 90-second run emitted nothing until it returned. Observation only — it cannot steer the loop, see the transcript or reach the actor — and a throw from it fails the run rather than being swallowed |
|
|
314
327
|
| a tool that is not `mcp: { expose: true }` is `X_AGENT_TOOL_UNEXPOSED` **at declaration** | a silently dropped tool reads as offered and is not; `isMcpExposed` is the one predicate, so an in-app agent and an external MCP client see the same catalogue |
|
|
315
328
|
| running out of turns is `X_AGENT_MAX_TURNS`, never a partial answer | a half-finished transcript returned as a result is working notes presented as a decision |
|
|
316
329
|
| `budget.tokensPerRun` caps the **whole run** | a single call is bounded by `maxTokens`; a loop is bounded by nothing until this is set |
|
|
317
330
|
| a tool result is truncated, and says so | the transcript IS the request, so an untruncated result is re-billed once per remaining turn |
|
|
318
331
|
| **no semantic cache** | similar prompts do not have similar answers once the answer depends on what `lookupOrder` returned this second |
|
|
319
332
|
|
|
333
|
+
## `hive()` — many members, one action
|
|
334
|
+
|
|
335
|
+
Fan an action out over many inputs. The fourth factory over a primitive, after `llm()`,
|
|
336
|
+
`backfill()` and `agent()`: a fan-out is still one server-authoritative operation with an input
|
|
337
|
+
schema, an output schema and a policy.
|
|
338
|
+
|
|
339
|
+
```ts
|
|
340
|
+
import { action, t } from '@ultimat3/action';
|
|
341
|
+
import { hive } from '@ultimat3/ai';
|
|
342
|
+
import { allow } from '@ultimat3/policy';
|
|
343
|
+
|
|
344
|
+
const summarisePost = action({
|
|
345
|
+
input: t.object({ postId: t.uuid }),
|
|
346
|
+
output: t.object({ summary: t.string }),
|
|
347
|
+
policy: allow(),
|
|
348
|
+
mcp: { expose: true },
|
|
349
|
+
handle: ({ input }) => ({ summary: input.postId }),
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
export const summariseBacklog = hive({
|
|
353
|
+
input: t.object({ postIds: t.array(t.uuid) }),
|
|
354
|
+
member: summarisePost,
|
|
355
|
+
split: ({ input }) => input.postIds.map((postId) => ({ postId })),
|
|
356
|
+
concurrency: 8,
|
|
357
|
+
minMembers: 2,
|
|
358
|
+
onMemberError: 'collect',
|
|
359
|
+
budget: { tokensPerRun: 500_000 },
|
|
360
|
+
policy: allow(),
|
|
361
|
+
});
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
`member` is any action — most usefully an `agent()`, which makes a hive a **supervisor over
|
|
365
|
+
sub-agents** with no supervisor primitive anywhere.
|
|
366
|
+
|
|
367
|
+
| Rule | Why |
|
|
368
|
+
|---|---|
|
|
369
|
+
| `members` comes back in **split order**, with `index` on every arm | a hand-rolled `Promise.all` reports in completion order, so joining a result back to the row it came from silently depends on nothing having failed |
|
|
370
|
+
| three arms — `ok`, `failed`, `skipped` — never two | *ran and threw* and *never ran* are different facts, and an aborted sibling is the second. Collapsing them makes "the hive stopped early" read as "every remaining item is bad data" |
|
|
371
|
+
| `onMemberError` is **required** | `'abort'` stops and leaves the rest `skipped`; `'collect'` harvests the rest. Both are right for somebody, so neither is a default |
|
|
372
|
+
| the hive **never names an actor** | `split` derives member inputs from `input` and `ctx` and from nothing a model emitted; each member runs through its own callable, so `invoke` applies the member's own policy with `ctx.actor` untouched |
|
|
373
|
+
| `concurrency` bounds the fan-out; one derived ledger bounds the spend | the ceiling holds under parallelism because the budget's root turnstile debits before the call, so three members against a ceiling only one fits leave exactly one `ok` — no hive-specific budget code exists |
|
|
374
|
+
| an empty split is `X_HIVE_EMPTY` | "0 ok, 0 failed" cannot be told apart from a query that returned no rows and nobody noticed |
|
|
375
|
+
| `minMembers` (default 2) stops fanning out, and **drops nothing** | a member's fixed cost dominates trivial work; below the floor every input still runs, serially |
|
|
376
|
+
| an aborted `ctx` unwinds the whole hive with `X_ABORTED` | distinct from `onMemberError: 'abort'`, which is a completed run with a partial harvest worth returning — here there is nobody left to hand it to |
|
|
377
|
+
|
|
378
|
+
## `agentJob()` — an agent as durable background work
|
|
379
|
+
|
|
380
|
+
Run an agent over a million rows as resumable, retried, budgeted queue work. `As of 2026-08` this
|
|
381
|
+
is the only way an agent reaches a queue at all: `.job()` hands back `kind: 'action-job'`, and
|
|
382
|
+
`isJobHandle` needs `kind === 'job'` plus membership of a `WeakMap` only `job()` writes, so nothing
|
|
383
|
+
externally shaped has ever reached the registry, the worker or the dead-letter path (issue #125).
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
import { t } from '@ultimat3/action';
|
|
387
|
+
import { agent, agentJob, definePrompt } from '@ultimat3/ai';
|
|
388
|
+
import { allow } from '@ultimat3/policy';
|
|
389
|
+
|
|
390
|
+
const summarisePost = agent({
|
|
391
|
+
input: t.object({ postId: t.uuid, orgId: t.uuid }),
|
|
392
|
+
output: t.object({ summary: t.string }),
|
|
393
|
+
prompt: definePrompt<{ postId: string }>({
|
|
394
|
+
id: 'summarise-post',
|
|
395
|
+
version: '1.0.0',
|
|
396
|
+
template: 'Summarise post {{postId}}.',
|
|
397
|
+
}),
|
|
398
|
+
vars: ({ input }) => ({ postId: input.postId }),
|
|
399
|
+
tools: [],
|
|
400
|
+
policy: allow(),
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
export const summariseBacklog = agentJob(summarisePost, {
|
|
404
|
+
name: 'summarise-backlog',
|
|
405
|
+
tenant: (input) => input.orgId,
|
|
406
|
+
retry: { attempts: 3, backoff: 'exponential' },
|
|
407
|
+
});
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
It composes `job()` rather than imitating a handle, so `.enqueue()`, the outbox, the worker's
|
|
411
|
+
cancellation, `x jobs show` and its manifest row all arrive for free. Pair it with `backfill()` for
|
|
412
|
+
the sweep and `hive()` for the fan-out inside one page.
|
|
413
|
+
|
|
414
|
+
| Rule | Why |
|
|
415
|
+
|---|---|
|
|
416
|
+
| `name` is required, and is the queue key | a job name is what queued, retrying and dead-lettered rows already carry, so renaming an export must not move where they are delivered |
|
|
417
|
+
| `tenant` and `retry` are required, no default | `jobs` states it: every candidate default for `tenant` is a cross-tenant read waiting for the first job that takes an org id in its input. `tenant: 'none'` is the explicit statement that it touches no scoped table |
|
|
418
|
+
| the action projection is read **lazily** | `agentJob()` runs at module scope beside the `agent()` it wraps, and names are stamped by `registerAction` at boot — reading `.job()` eagerly makes that ordinary file `X_ACTION_UNREGISTERED` |
|
|
419
|
+
| one execution path, and it is the action's | `run` is `invoke(agent, input, { surface: 'job', ctx })`, so the agent's policy, input parse, budget scope and span all apply — and the `ctx` is the worker's, so an attempt timing out aborts the agent's turn loop |
|
|
420
|
+
| the actor is the worker context's, never the model's | the job body runs with system authority and the org comes from the job's declared `tenant`; nothing a model emits can reach either |
|
|
421
|
+
|
|
422
|
+
### The at-least-once trap, said plainly
|
|
423
|
+
|
|
424
|
+
**`idempotencyKey` dedupes the ENQUEUE, never the ATTEMPT.** Two enqueues with the same payload are
|
|
425
|
+
one row. One row that a worker claims, half-runs and loses the lease on is claimed again, and **the
|
|
426
|
+
agent runs a second time from the top** — as does every page a `backfill()` replays, since its
|
|
427
|
+
`handle` is at-least-once by construction.
|
|
428
|
+
|
|
429
|
+
So every tool the agent may call has to be idempotent: an `upsertAll`, an `updateWhere`, a statement
|
|
430
|
+
whose second run changes nothing. Otherwise a replayed attempt issues a second refund.
|
|
431
|
+
|
|
432
|
+
**The framework does not check this, and the reason is worth knowing.** `mutates` is not a fact an
|
|
433
|
+
`action()` declares — it exists only in `@ultimat3/mcp`, which sets it to `true` for *every* action
|
|
434
|
+
it projects — so a read-only `lookupOrder` and a destructive `issueRefund` are indistinguishable
|
|
435
|
+
here. A rule refusing every tool that has not declared `idempotent: true` would refuse the reads
|
|
436
|
+
too, and a wrong refusal is worse than a stated obligation. `isMutator` is legible, but `mutator()`
|
|
437
|
+
is the local-first write primitive and catches almost none of the risk while reading as if it
|
|
438
|
+
caught all of it. This is a contract you keep, not one the compiler keeps for you.
|
|
439
|
+
|
|
440
|
+
## `describeAgents()` — what the manifest can say
|
|
441
|
+
|
|
442
|
+
```ts
|
|
443
|
+
import { describeAgents } from '@ultimat3/ai';
|
|
444
|
+
|
|
445
|
+
describeAgents();
|
|
446
|
+
// [{ name: 'supportAgent', prompt: 'support@1.0.0', promptHash: '…', model: 'claude-opus-5',
|
|
447
|
+
// maxTurns: 6, maxToolResultChars: 4000, tools: ['issueRefund', 'lookupOrder'],
|
|
448
|
+
// budget: { tokensIn: null, tokensPerRun: 200000, costPerCall: { minor: 50, currency: 'USD' } },
|
|
449
|
+
// mcp: true }]
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
An agent projects to an `ActionDescriptor` like any other action, and that descriptor knows nothing
|
|
453
|
+
about turns or tools — so "how far can this loop, and what may it call" had no answer outside the
|
|
454
|
+
source. Names are read when you ask, not when the agent was declared: `registerAction` stamps them
|
|
455
|
+
at boot, long after `agent()` ran at module scope. An agent nothing registered has no row, because
|
|
456
|
+
an action with no name reaches no route, no tool catalogue and no queue.
|
|
457
|
+
|
|
320
458
|
## Redaction: one declared seam
|
|
321
459
|
|
|
322
460
|
`vars()` is the one place a model call loads data, so it is the one place anything can sit between
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/ai",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "LLM gateway, versioned prompts, evals as tests, embeddings, hybrid vector search, RAG",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -31,13 +31,14 @@
|
|
|
31
31
|
"test": "bun test"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@ultimat3/action": "
|
|
35
|
-
"@ultimat3/cache": "
|
|
36
|
-
"@ultimat3/core": "
|
|
37
|
-
"@ultimat3/db": "
|
|
38
|
-
"@ultimat3/
|
|
39
|
-
"@ultimat3/
|
|
40
|
-
"@ultimat3/
|
|
41
|
-
"@ultimat3/
|
|
34
|
+
"@ultimat3/action": "3.0.0",
|
|
35
|
+
"@ultimat3/cache": "3.0.0",
|
|
36
|
+
"@ultimat3/core": "3.0.0",
|
|
37
|
+
"@ultimat3/db": "3.0.0",
|
|
38
|
+
"@ultimat3/jobs": "3.0.0",
|
|
39
|
+
"@ultimat3/money": "3.0.0",
|
|
40
|
+
"@ultimat3/policy": "3.0.0",
|
|
41
|
+
"@ultimat3/schema": "3.0.0",
|
|
42
|
+
"@ultimat3/time": "3.0.0"
|
|
42
43
|
}
|
|
43
44
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// What an `agent()` IS, published for the manifest — turns, tools, budget, model, prompt hash.
|
|
2
|
+
//
|
|
3
|
+
// Nothing agent-shaped was visible anywhere before this: an agent projects to `ActionDescriptor`
|
|
4
|
+
// like every other action, and that descriptor deliberately knows nothing about turns or tools,
|
|
5
|
+
// so "which agents does this app have, how far can each one loop, and what may it call" had no
|
|
6
|
+
// answer outside reading the source. Same shape as `describePrompts()` / `describeEvals()`, and
|
|
7
|
+
// deliberately NOT a new `ActionDescriptor` field: @ultimat3/action is tier 3 and knows nothing
|
|
8
|
+
// about models.
|
|
9
|
+
|
|
10
|
+
import type { AnyAction } from '@ultimat3/action';
|
|
11
|
+
import type { Money } from '@ultimat3/money';
|
|
12
|
+
|
|
13
|
+
/** The declared ceilings, flattened so a manifest row is plain JSON. `null` is "not declared". */
|
|
14
|
+
export interface AgentBudgetFact {
|
|
15
|
+
readonly tokensIn: number | null;
|
|
16
|
+
readonly tokensPerRun: number | null;
|
|
17
|
+
readonly costPerCall: Money | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AgentFact {
|
|
21
|
+
/** The export name registration stamped — the same name `.tool()` and `tools/call` answer to. */
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly prompt: string;
|
|
24
|
+
readonly promptId: string;
|
|
25
|
+
/**
|
|
26
|
+
* The prompt's content hash. An agent's behaviour is its prompt, so a row without one records
|
|
27
|
+
* which agent ran and not which agent it was — the same reason every eval result carries it.
|
|
28
|
+
*/
|
|
29
|
+
readonly promptHash: string;
|
|
30
|
+
readonly model: string;
|
|
31
|
+
readonly maxTurns: number;
|
|
32
|
+
readonly maxToolResultChars: number;
|
|
33
|
+
/** Tool names, sorted — the catalogue this agent may call, which is its blast radius. */
|
|
34
|
+
readonly tools: readonly string[];
|
|
35
|
+
readonly budget: AgentBudgetFact;
|
|
36
|
+
/** Whether the agent itself is offered as a tool, so a supervisor could call it. */
|
|
37
|
+
readonly mcp: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Keyed by the action, and the facts are a THUNK: every name in a row — the agent's and its
|
|
42
|
+
* tools' — is stamped by `registerAction` at boot, long after `agent()` ran at module scope.
|
|
43
|
+
* Reading them here rather than at declaration is what makes a row name what an app can call.
|
|
44
|
+
*/
|
|
45
|
+
const registry = new Map<AnyAction, () => Omit<AgentFact, 'name'>>();
|
|
46
|
+
|
|
47
|
+
export function registerAgentFact(target: AnyAction, facts: () => Omit<AgentFact, 'name'>): void {
|
|
48
|
+
registry.set(target, facts);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Every registered agent, by name.
|
|
53
|
+
*
|
|
54
|
+
* An agent still carrying no name is left out, and that is not a silent drop: a name is stamped by
|
|
55
|
+
* `registerAction`, an action without one reaches no route, no tool catalogue and no queue, so
|
|
56
|
+
* there is no capability for a row to describe. `named()` builds a TWIN rather than naming in
|
|
57
|
+
* place — registration names in place — so an agent renamed that way is absent for the same
|
|
58
|
+
* reason. Register it instead: `registerAction('supportAgent', support)`.
|
|
59
|
+
*/
|
|
60
|
+
export function describeAgents(): readonly AgentFact[] {
|
|
61
|
+
return [...registry.entries()]
|
|
62
|
+
.filter(([target]) => target.name !== '')
|
|
63
|
+
.map(([target, facts]) => ({ name: target.name, ...facts() }))
|
|
64
|
+
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Test-only reset. A module-level registry otherwise leaks between test files. */
|
|
68
|
+
export function resetAgents(): void {
|
|
69
|
+
registry.clear();
|
|
70
|
+
}
|
package/src/agent-job.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `agentJob()` — an agent as durable, resumable, budgeted background work.
|
|
3
|
+
*
|
|
4
|
+
* The bridge `packages/action/src/job-handle.ts` names in its own header and could not build:
|
|
5
|
+
* `isJobHandle` needs `kind === 'job'` PLUS membership of a WeakMap only `job()` writes, so no
|
|
6
|
+
* externally-shaped object reaches the registry, the queue or the worker — and `action` and `jobs`
|
|
7
|
+
* are both tier 3, so neither may import the other. This package is tier 4 and may import both,
|
|
8
|
+
* which is exactly where that header says the adapter belongs.
|
|
9
|
+
*
|
|
10
|
+
* It composes `job()` rather than re-implementing a handle: the returned value IS one `job()`
|
|
11
|
+
* seated, so `.enqueue()`, the outbox, the worker's cancellation, the dead-letter path,
|
|
12
|
+
* `x jobs show` and its manifest row all arrive without a line here.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { Action, ActionJobHandle } from '@ultimat3/action';
|
|
16
|
+
import type { JobHandle, JobTenant, RetryPolicy } from '@ultimat3/jobs';
|
|
17
|
+
import { job } from '@ultimat3/jobs';
|
|
18
|
+
import type { InferInput, InferOutput, StandardSchemaV1 } from '@ultimat3/schema';
|
|
19
|
+
|
|
20
|
+
export interface AgentJobOptions<I> {
|
|
21
|
+
/**
|
|
22
|
+
* The durable queue key. REQUIRED and never derived from the agent's export name: a job name is
|
|
23
|
+
* what queued, retrying and dead-lettered rows already carry, so renaming an export must not
|
|
24
|
+
* move where they are delivered.
|
|
25
|
+
*/
|
|
26
|
+
readonly name: string;
|
|
27
|
+
/**
|
|
28
|
+
* REQUIRED, no default, and the org this run's body acts under. `jobs` states why: every
|
|
29
|
+
* candidate default is a cross-tenant read waiting for the first job that takes an org id in its
|
|
30
|
+
* input. `tenant: 'none'` is the explicit statement that this agent touches no tenant-scoped
|
|
31
|
+
* table, and then every scoped read inside it fails closed.
|
|
32
|
+
*/
|
|
33
|
+
readonly tenant: JobTenant<I>;
|
|
34
|
+
/** REQUIRED, no default. A model call fails transiently; how many times is nobody else's guess. */
|
|
35
|
+
readonly retry: RetryPolicy;
|
|
36
|
+
readonly queue?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Defaults to the action projection's own key — `action:<name>:<fingerprint of input>` — which
|
|
39
|
+
* is stable across retries and derived from the payload alone.
|
|
40
|
+
*
|
|
41
|
+
* **It dedupes the ENQUEUE, never the ATTEMPT.** Two `enqueue` calls with the same payload are
|
|
42
|
+
* one row; one row that a worker claims, half-runs and loses the lease on is claimed again, and
|
|
43
|
+
* the agent runs a second time from the top. Combined with `backfill()`, whose `handle` is at
|
|
44
|
+
* least once by construction, a replayed page re-runs every agent on it.
|
|
45
|
+
*
|
|
46
|
+
* What that means for `tools`: **every tool the agent may call has to be idempotent** — an
|
|
47
|
+
* `upsertAll`, an `updateWhere`, a statement whose second run changes nothing — because a
|
|
48
|
+
* replayed attempt issues a second `issueRefund` otherwise. The framework does NOT check this and
|
|
49
|
+
* cannot: `mutates` is not a fact an `action()` declares (`@ultimat3/mcp` sets it to `true` for
|
|
50
|
+
* every action it projects), so a read-only `lookupOrder` and a destructive `issueRefund` are
|
|
51
|
+
* indistinguishable here, and a rule refusing both would be a wrong refusal. See the README.
|
|
52
|
+
*/
|
|
53
|
+
idempotencyKey?(input: I): string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Wrap an `agent()` — or any action — as a real job handle.
|
|
58
|
+
*
|
|
59
|
+
* Both reads of the underlying projection are LAZY, and that is load-bearing: `target.job()` calls
|
|
60
|
+
* `actionName()`, which throws `X_ACTION_UNREGISTERED` until `registerAction` stamps the export
|
|
61
|
+
* name at boot — and `agentJob()` is evaluated at module scope, right beside the `agent()` it
|
|
62
|
+
* wraps. The queue key comes from `options.name` for the same reason it is required.
|
|
63
|
+
*/
|
|
64
|
+
export function agentJob<TInput extends StandardSchemaV1, TOutput extends StandardSchemaV1>(
|
|
65
|
+
target: Action<TInput, TOutput>,
|
|
66
|
+
options: AgentJobOptions<InferOutput<TInput>>,
|
|
67
|
+
): JobHandle<InferOutput<TInput>> {
|
|
68
|
+
let projected: ActionJobHandle<TInput, TOutput> | undefined;
|
|
69
|
+
const bridge = (): ActionJobHandle<TInput, TOutput> => {
|
|
70
|
+
if (projected === undefined) projected = target.job();
|
|
71
|
+
return projected;
|
|
72
|
+
};
|
|
73
|
+
return job<InferOutput<TInput>>({
|
|
74
|
+
name: options.name,
|
|
75
|
+
input: target.input as StandardSchemaV1<unknown, InferOutput<TInput>>,
|
|
76
|
+
tenant: options.tenant,
|
|
77
|
+
retry: options.retry,
|
|
78
|
+
...(options.queue === undefined ? {} : { queue: options.queue }),
|
|
79
|
+
idempotencyKey: (input) =>
|
|
80
|
+
options.idempotencyKey?.(input) ?? bridge().idempotencyKey(asInput<TInput>(input)),
|
|
81
|
+
// ONE execution path, and it is the action's. `ActionJobHandle.invoke` is `invoke(target,
|
|
82
|
+
// input, { surface: 'job', ctx })`, so the agent's policy, its input parse, its budget scope
|
|
83
|
+
// and its span all apply — and `ctx` is the WORKER's, so `ctx.signal` aborting at the attempt
|
|
84
|
+
// timeout reaches the agent's turn loop.
|
|
85
|
+
run: ({ input, ctx }) => bridge().invoke(asInput<TInput>(input), ctx),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The job parsed with the action's OWN schema, so what it hands back is a value that schema
|
|
91
|
+
* accepts — `invoke` re-parses it regardless, which is what makes this safe rather than merely
|
|
92
|
+
* convenient. The cast exists because `InferOutput` and `InferInput` are different types wherever a
|
|
93
|
+
* field has a default, and nothing at this seam can prove they meet.
|
|
94
|
+
*/
|
|
95
|
+
function asInput<TInput extends StandardSchemaV1>(value: InferOutput<TInput>): InferInput<TInput> {
|
|
96
|
+
return value as InferInput<TInput>;
|
|
97
|
+
}
|