@balchemyai/agent-sdk 0.3.11 → 0.6.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.
Files changed (154) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/LICENSE +21 -0
  3. package/README.md +152 -6
  4. package/dist/agent-loop/agent-loop.d.ts +19 -8
  5. package/dist/agent-loop/agent-loop.d.ts.map +1 -1
  6. package/dist/agent-loop/agent-loop.js +620 -120
  7. package/dist/agent-loop/agent-loop.js.map +1 -1
  8. package/dist/agent-loop/decision-handler.d.ts.map +1 -1
  9. package/dist/agent-loop/decision-handler.js +64 -3
  10. package/dist/agent-loop/decision-handler.js.map +1 -1
  11. package/dist/agent-loop/llm-adapters/anthropic.d.ts +4 -3
  12. package/dist/agent-loop/llm-adapters/anthropic.d.ts.map +1 -1
  13. package/dist/agent-loop/llm-adapters/anthropic.js +79 -40
  14. package/dist/agent-loop/llm-adapters/anthropic.js.map +1 -1
  15. package/dist/agent-loop/llm-adapters/openai.d.ts +4 -3
  16. package/dist/agent-loop/llm-adapters/openai.d.ts.map +1 -1
  17. package/dist/agent-loop/llm-adapters/openai.js +91 -36
  18. package/dist/agent-loop/llm-adapters/openai.js.map +1 -1
  19. package/dist/agent-loop/llm-adapters/transport.d.ts +5 -0
  20. package/dist/agent-loop/llm-adapters/transport.d.ts.map +1 -0
  21. package/dist/agent-loop/llm-adapters/transport.js +63 -0
  22. package/dist/agent-loop/llm-adapters/transport.js.map +1 -0
  23. package/dist/agent-loop/llm-cost-tracker.d.ts +4 -0
  24. package/dist/agent-loop/llm-cost-tracker.d.ts.map +1 -1
  25. package/dist/agent-loop/llm-cost-tracker.js +37 -5
  26. package/dist/agent-loop/llm-cost-tracker.js.map +1 -1
  27. package/dist/agent-loop/rule-checker.d.ts +1 -20
  28. package/dist/agent-loop/rule-checker.d.ts.map +1 -1
  29. package/dist/agent-loop/rule-checker.js +0 -2
  30. package/dist/agent-loop/rule-checker.js.map +1 -1
  31. package/dist/agent-loop/telemetry-reporter.d.ts +8 -14
  32. package/dist/agent-loop/telemetry-reporter.d.ts.map +1 -1
  33. package/dist/agent-loop/telemetry-reporter.js +74 -8
  34. package/dist/agent-loop/telemetry-reporter.js.map +1 -1
  35. package/dist/agent-loop/types.d.ts +25 -9
  36. package/dist/agent-loop/types.d.ts.map +1 -1
  37. package/dist/agent-loop/types.js +0 -7
  38. package/dist/agent-loop/types.js.map +1 -1
  39. package/dist/agent-loop/webhook-receiver.d.ts +21 -2
  40. package/dist/agent-loop/webhook-receiver.d.ts.map +1 -1
  41. package/dist/agent-loop/webhook-receiver.js +196 -33
  42. package/dist/agent-loop/webhook-receiver.js.map +1 -1
  43. package/dist/auth/onboarding.d.ts.map +1 -1
  44. package/dist/auth/onboarding.js +15 -0
  45. package/dist/auth/onboarding.js.map +1 -1
  46. package/dist/client/http-client.d.ts +4 -2
  47. package/dist/client/http-client.d.ts.map +1 -1
  48. package/dist/client/http-client.js +88 -16
  49. package/dist/client/http-client.js.map +1 -1
  50. package/dist/errors/agent-sdk-error.d.ts +6 -0
  51. package/dist/errors/agent-sdk-error.d.ts.map +1 -1
  52. package/dist/errors/agent-sdk-error.js +16 -0
  53. package/dist/errors/agent-sdk-error.js.map +1 -1
  54. package/dist/errors/failure-metadata.d.ts +19 -0
  55. package/dist/errors/failure-metadata.d.ts.map +1 -0
  56. package/dist/errors/failure-metadata.js +177 -0
  57. package/dist/errors/failure-metadata.js.map +1 -0
  58. package/dist/index.d.ts +33 -9
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +147 -15
  61. package/dist/index.js.map +1 -1
  62. package/dist/local-runner/assessment-client.d.ts +111 -0
  63. package/dist/local-runner/assessment-client.d.ts.map +1 -0
  64. package/dist/local-runner/assessment-client.js +417 -0
  65. package/dist/local-runner/assessment-client.js.map +1 -0
  66. package/dist/local-runner/control-client.d.ts +126 -0
  67. package/dist/local-runner/control-client.d.ts.map +1 -0
  68. package/dist/local-runner/control-client.js +370 -0
  69. package/dist/local-runner/control-client.js.map +1 -0
  70. package/dist/local-runner/decision-report-client.d.ts +64 -0
  71. package/dist/local-runner/decision-report-client.d.ts.map +1 -0
  72. package/dist/local-runner/decision-report-client.js +169 -0
  73. package/dist/local-runner/decision-report-client.js.map +1 -0
  74. package/dist/local-runner/execution-build-client.d.ts +207 -0
  75. package/dist/local-runner/execution-build-client.d.ts.map +1 -0
  76. package/dist/local-runner/execution-build-client.js +426 -0
  77. package/dist/local-runner/execution-build-client.js.map +1 -0
  78. package/dist/local-runner/execution-submit-client.d.ts +126 -0
  79. package/dist/local-runner/execution-submit-client.d.ts.map +1 -0
  80. package/dist/local-runner/execution-submit-client.js +287 -0
  81. package/dist/local-runner/execution-submit-client.js.map +1 -0
  82. package/dist/local-runner/llm-client.d.ts +20 -0
  83. package/dist/local-runner/llm-client.d.ts.map +1 -0
  84. package/dist/local-runner/llm-client.js +23 -0
  85. package/dist/local-runner/llm-client.js.map +1 -0
  86. package/dist/local-runner/owner-approval-client.d.ts +124 -0
  87. package/dist/local-runner/owner-approval-client.d.ts.map +1 -0
  88. package/dist/local-runner/owner-approval-client.js +261 -0
  89. package/dist/local-runner/owner-approval-client.js.map +1 -0
  90. package/dist/local-runner/owner-approval-message.d.ts +82 -0
  91. package/dist/local-runner/owner-approval-message.d.ts.map +1 -0
  92. package/dist/local-runner/owner-approval-message.js +174 -0
  93. package/dist/local-runner/owner-approval-message.js.map +1 -0
  94. package/dist/local-runner/owner-positions.d.ts +37 -0
  95. package/dist/local-runner/owner-positions.d.ts.map +1 -0
  96. package/dist/local-runner/owner-positions.js +94 -0
  97. package/dist/local-runner/owner-positions.js.map +1 -0
  98. package/dist/local-runner/pairing-client.d.ts +95 -0
  99. package/dist/local-runner/pairing-client.d.ts.map +1 -0
  100. package/dist/local-runner/pairing-client.js +186 -0
  101. package/dist/local-runner/pairing-client.js.map +1 -0
  102. package/dist/local-runner/research-client.d.ts +98 -0
  103. package/dist/local-runner/research-client.d.ts.map +1 -0
  104. package/dist/local-runner/research-client.js +344 -0
  105. package/dist/local-runner/research-client.js.map +1 -0
  106. package/dist/local-runner/runner-proof-transport.d.ts +55 -0
  107. package/dist/local-runner/runner-proof-transport.d.ts.map +1 -0
  108. package/dist/local-runner/runner-proof-transport.js +258 -0
  109. package/dist/local-runner/runner-proof-transport.js.map +1 -0
  110. package/dist/local-runner/runtime-client.d.ts +142 -0
  111. package/dist/local-runner/runtime-client.d.ts.map +1 -0
  112. package/dist/local-runner/runtime-client.js +306 -0
  113. package/dist/local-runner/runtime-client.js.map +1 -0
  114. package/dist/local-runner/sell-assessment-client.d.ts +113 -0
  115. package/dist/local-runner/sell-assessment-client.d.ts.map +1 -0
  116. package/dist/local-runner/sell-assessment-client.js +225 -0
  117. package/dist/local-runner/sell-assessment-client.js.map +1 -0
  118. package/dist/local-runner/signals-client.d.ts +91 -0
  119. package/dist/local-runner/signals-client.d.ts.map +1 -0
  120. package/dist/local-runner/signals-client.js +357 -0
  121. package/dist/local-runner/signals-client.js.map +1 -0
  122. package/dist/local-runner/solana-platform-account.d.ts +5 -0
  123. package/dist/local-runner/solana-platform-account.d.ts.map +1 -0
  124. package/dist/local-runner/solana-platform-account.js +66 -0
  125. package/dist/local-runner/solana-platform-account.js.map +1 -0
  126. package/dist/local-runner/trading-wallet-client.d.ts +67 -0
  127. package/dist/local-runner/trading-wallet-client.d.ts.map +1 -0
  128. package/dist/local-runner/trading-wallet-client.js +144 -0
  129. package/dist/local-runner/trading-wallet-client.js.map +1 -0
  130. package/dist/mcp/mcp-client.d.ts +31 -4
  131. package/dist/mcp/mcp-client.d.ts.map +1 -1
  132. package/dist/mcp/mcp-client.js +491 -83
  133. package/dist/mcp/mcp-client.js.map +1 -1
  134. package/dist/streaming/sse-event-stream.d.ts +14 -0
  135. package/dist/streaming/sse-event-stream.d.ts.map +1 -1
  136. package/dist/streaming/sse-event-stream.js +153 -13
  137. package/dist/streaming/sse-event-stream.js.map +1 -1
  138. package/dist/types.d.ts +11 -2
  139. package/dist/types.d.ts.map +1 -1
  140. package/dist/utils/package-version.d.ts +2 -0
  141. package/dist/utils/package-version.d.ts.map +1 -0
  142. package/dist/utils/package-version.js +54 -0
  143. package/dist/utils/package-version.js.map +1 -0
  144. package/dist/utils/redaction.d.ts +4 -0
  145. package/dist/utils/redaction.d.ts.map +1 -1
  146. package/dist/utils/redaction.js +54 -0
  147. package/dist/utils/redaction.js.map +1 -1
  148. package/dist/utils/retry.d.ts +22 -13
  149. package/dist/utils/retry.d.ts.map +1 -1
  150. package/dist/utils/retry.js +76 -50
  151. package/dist/utils/retry.js.map +1 -1
  152. package/docs/DEPLOYMENT.md +1 -1
  153. package/docs/error-retry-strategy.md +16 -6
  154. package/package.json +9 -9
package/CHANGELOG.md CHANGED
@@ -7,6 +7,82 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
7
7
 
8
8
  ---
9
9
 
10
+ ## [0.6.0] — 2026-11-XX
11
+
12
+ ### Added
13
+
14
+ - Owner first-enable approval protocol for the runner: `RunnerOwnerApprovalClient`,
15
+ `runnerFirstEnableApprovalMessage`, the digest, statement and check-code helpers, the
16
+ nonce/challenge/read-response validators and their types. The message takes the chain id
17
+ (`solana:mainnet`, `solana:mainnet-beta`, `solana:devnet`, `solana:testnet`) as a validated
18
+ parameter; the runner recomputes the check code from its own request and refuses a backend
19
+ code that differs.
20
+ - Execution request v2 for buys: `RunnerExecutionRequestV2`, `RunnerExecutionBuyRequest`,
21
+ `RunnerDailyCap` (`{ kind: "none" }` or `{ kind: "lamports" }`) and
22
+ `RunnerExecutionReservationV2`. v1 requests, digests and goldens are unchanged.
23
+ - `RUNNER_POSITION_NOTICES` and `RunnerPositionNotice`: the owner-positions validator accepts an
24
+ optional `notice` (`exits_unavailable`) on an item.
25
+ - `validateRunnerRuntimeNonce` takes an optional fourth argument `sentAt`. The three refusals that
26
+ only a clock difference explains carry `details.reason: "clock_skew"` (a lease receipt whose
27
+ server time is more than 5 s from this clock, a nonce issued more than 5 s ahead, an expired
28
+ nonce Balchemy issued more than 5 s before the request was sent). Codes, messages and every
29
+ accept or refuse outcome are unchanged.
30
+
31
+ ### Changed
32
+
33
+ - Released together with `balchemy@0.6.0`; `@balchemyai/agent-rules` stays at 1.1.0. Publish the
34
+ SDK first, then the CLI. Restart a running Balchemy runner after upgrading the CLI.
35
+ - Older SDK versions (0.4.x and 0.5.0) refuse an owner-positions item that carries `notice`
36
+ (closed validator, fails closed). Upgrade the SDK together with the CLI.
37
+
38
+ ---
39
+
40
+ ## [0.5.0] — 2026-11-11
41
+
42
+ ### Added
43
+
44
+ - `RunnerDecisionReportClient`: runner-signed, bounded per-decision reports for
45
+ Hub, with the closed action and hold-reason enums it validates.
46
+ - Runtime lease bodies and receipts accept an optional keystore observation
47
+ (`lock` and the build's fixed `exits` capability, no address or key material),
48
+ validated by `validateRunnerKeystoreObservation`; older bodies keep their
49
+ digest.
50
+
51
+ ### Changed
52
+
53
+ - Released together with `balchemy@0.5.0`; `@balchemyai/agent-rules` stays at
54
+ 1.1.0. Restart a running Balchemy runner after upgrading the CLI.
55
+
56
+ ## [0.4.0] — 2026-11-09
57
+
58
+ ### Added
59
+
60
+ - Local-runner protocol clients for a user-owned background runner: control,
61
+ signals, research, trading-wallet binding, execution build and execution
62
+ submit clients, exported alongside the existing MCP and onboarding clients.
63
+ - Runner-signed swap relay, submission journal and reconciliation client
64
+ support, and build-time decision provenance re-checks.
65
+ - Live-arming (`live_unarmed` / `live_armed`) and spend-limit support in the
66
+ runner protocol, plus the exits protocol (sell assessment, exit guard, owner
67
+ positions) for the runner.
68
+ - Dependency on the shared deterministic rules package
69
+ `@balchemyai/agent-rules` through the paired CLI.
70
+
71
+ ### Changed
72
+
73
+ - The telemetry reporter now reports the SDK version read from this package's
74
+ own `package.json` instead of a hard-coded constant.
75
+ - Added a `LICENSE` file (MIT) to the published package.
76
+
77
+ ### Notes
78
+
79
+ - The local-runner clients target the runner pairing and shadow-decision
80
+ protocol. Live trading is not available in this release; execution stays
81
+ non-executing until every deterministic contract gate is delivered and
82
+ verified.
83
+ - Publish order: `@balchemyai/agent-rules@1.1.0`, then
84
+ `@balchemyai/agent-sdk@0.4.0`, then `balchemy@0.4.0`.
85
+
10
86
  ## [0.3.11] — 2026-07-01
11
87
 
12
88
  ### Fixed
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Balchemy Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -10,12 +10,16 @@ keeps the record.
10
10
 
11
11
  ## Release Line
12
12
 
13
- This source tree is prepared for `@balchemyai/agent-sdk@0.3.11`. npm publish,
14
- public mirror sync, and live package installation proof happen in separate
15
- release steps; this README does not claim that `0.3.11` has already been
16
- published.
13
+ Version `0.6.0` is released together with `balchemy@0.6.0` and
14
+ `@balchemyai/agent-rules@1.1.0` (unchanged); publish the SDK first, then the CLI.
15
+ Version `0.4.0` added the local-runner protocol clients (see the changelog).
17
16
 
18
- `0.3.11` fixes the incomplete SDK root export surface shipped in `0.3.10`.
17
+ The approved ownership target keeps trading keys, model calls and private agent
18
+ context in the user's background runner. The existing onboarding and hosted
19
+ execution interfaces below describe legacy compatibility; local pairing and
20
+ signing are still being implemented and are not ready for live trading.
21
+
22
+ `0.3.11` fixed the incomplete SDK root export surface shipped in `0.3.10`.
19
23
 
20
24
  ## Install
21
25
 
@@ -23,6 +27,127 @@ published.
23
27
  npm install @balchemyai/agent-sdk
24
28
  ```
25
29
 
30
+ ## Unreleased local runner identity client
31
+
32
+ `RunnerPairingClient` is the candidate public client for fixed-purpose pairing
33
+ reads. It uses the configured Hub origin's `/api` proxy, rejects redirects, and
34
+ accepts no owner session token. It does not create wallets, issue execution
35
+ credentials, or enable trading.
36
+
37
+ Pin `frontendOrigin`, `apiBaseUrl` (`frontendOrigin + "/api"`), the runner's
38
+ Ed25519 public key and its locally generated intent. Request a nonce for exactly
39
+ one challenge or receipt read, validate its fixed-purpose bytes with
40
+ `runnerPairingReadMessage`, and sign those bytes with the dedicated local runner
41
+ identity. Do not use a trading-wallet key or expose a general supplied-text
42
+ signer. The CLI implements this private-key boundary; the SDK holds no keys.
43
+
44
+ Receipt reads require the binding digest durably saved at deliberate local
45
+ approval. `validateRunnerPairingBinding` validates a public binding only; it is
46
+ not proof that a request is still pending or that pairing committed.
47
+ `validateRunnerPairingResponse` separately checks the pending request or matching
48
+ committed receipt. A receipt can be recovered after the original request expires,
49
+ but its committed time must fall within that original validity window. Failed
50
+ reads are not automatically replayed; explicit recovery obtains a fresh nonce.
51
+
52
+ This candidate protocol must be released with its matching backend and CLI.
53
+ Source tests do not establish deployed availability or full local-agent readiness.
54
+
55
+ ## Unreleased local runner research client
56
+
57
+ `RunnerResearchClient` connects a durably paired runner to three public reports:
58
+ `market_brief`, `candidate_report` and `risk_report`, for Solana or Base. Requests
59
+ contain closed public fields such as the chain and exact asset address; they do
60
+ not accept chat, prompts, rules, credentials, arbitrary tools or URLs.
61
+
62
+ Pin the adopted origin, agent, binding digest, runner public key and current
63
+ instance. Call `query(request, epoch, signal)` only while the matching runner
64
+ lease remains current. Its signer must validate `runnerResearchNonceMessage`
65
+ against that exact target and request before signing with the dedicated local
66
+ identity. The CLI implements this check; a trading-wallet key is not used.
67
+
68
+ One deadline covers nonce, signing, request and body reading. Redirects and
69
+ automatic retries are disabled. A returned `result.ok` describes tool completion:
70
+ the report may still contain missing, stale or held evidence. Inspect its report
71
+ data and observation times; neither the report nor the lease authorizes a trade.
72
+ This candidate requires matching unreleased backend/CLI code. Published package
73
+ and deployed availability remain separate from the isolated interoperability tests.
74
+
75
+ ## Unreleased local Solana assessment client
76
+
77
+ `RunnerAssessmentClient` requests a read-only assessment for an exact SOL-input
78
+ plan: token, integer lamports, slippage and plan digest. The backend supplies the
79
+ existing global swap fee; the request cannot choose the fee or recipient. The
80
+ response binds fee arithmetic, quote amounts and token-safety observations to
81
+ that same request. It never contains a transaction or authorizes execution.
82
+
83
+ Pin the adopted origin, agent, binding, runner key and active lease instance.
84
+ Before signing, validate `runnerAssessmentNonceMessage` against that target,
85
+ epoch and exact request. Use the dedicated runner identity, never a trading key.
86
+ `query(request, epoch, signal)` has one bounded deadline and no automatic retry.
87
+ Private rules, prompts, model credentials and signing keys are not request fields.
88
+
89
+ `status: "complete"` means the required read sources returned usable observations;
90
+ the safety verdict can still be `block` or `require_approval`. Partial provider
91
+ failure remains `unavailable`, and every result has `executionAvailable: false`.
92
+ The client checks exact fee conservation, quote bounds, request/digest binding
93
+ and expiry. Fetch observation times do not establish an upstream publication time.
94
+ This candidate needs the matching unreleased backend and engine. Connection to
95
+ the private plan consumer, installed-package and deployed acceptance remain open.
96
+
97
+ ## Unreleased local signal receipt client
98
+
99
+ `RunnerSignalsClient` uses the same paired identity and current lease boundary.
100
+ Validate `runnerSignalsNonceMessage` against the exact target and request before
101
+ signing with the dedicated runner identity. `query({ kind: "poll" }, epoch)`
102
+ returns at most 16 unacknowledged public Solana observations from verified
103
+ Telegram/Discord sources. Historical observations remain deliverable; the
104
+ consumer must check freshness before evaluating them.
105
+
106
+ Persist each complete event locally before explicitly calling
107
+ `query({ kind: "ack", eventId, eventDigest }, epoch)`. The client never ACKs or
108
+ retries automatically. An ACK records receipt only; it is not a decision,
109
+ approval or trade. Retain the original event ID and digest across recovery.
110
+ This candidate requires the matching backend. The managed daemon consumer,
111
+ local source setup UI, onchain signals and Base delivery are not yet connected;
112
+ published availability and end-to-end automation are not established.
113
+
114
+ ## Unreleased local model client
115
+
116
+ `createLocalLlmAdapter` makes a text request directly from the caller's process to
117
+ the explicitly selected OpenAI or Anthropic provider. It requires an explicit
118
+ model; it does not select a cheaper model, retry, or fall back to Balchemy. Supply
119
+ the API key from the user's local secret source and never log the configuration.
120
+
121
+ ```ts
122
+ import { createLocalLlmAdapter } from '@balchemyai/agent-sdk';
123
+
124
+ const model = createLocalLlmAdapter({
125
+ provider: 'openai',
126
+ model: selectedModel,
127
+ apiKey: locallyLoadedApiKey,
128
+ timeoutMs: 30_000,
129
+ });
130
+ const result = await model.chat(localMessages, 1_000, { signal: shutdownSignal });
131
+ ```
132
+
133
+ OpenAI-compatible `baseUrl` requires HTTPS; plain HTTP is limited to numeric
134
+ loopback (`127.0.0.1` or `[::1]`). An explicitly supplied `fetchFn` receives private
135
+ messages and provider credentials and must remain in the user's trusted process.
136
+ Redirects are rejected. Timeout and cancellation cover response-body reading; an
137
+ aborted request may still have run or incurred cost at the provider, so this client
138
+ never retries automatically. Errors omit raw provider bodies and transport causes.
139
+
140
+ Only complete text responses are accepted. Truncation, refusal, tool calls and
141
+ unsupported content blocks fail closed. OpenAI `stop` and Anthropic `end_turn`
142
+ follow their respective [Chat Completions](https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create)
143
+ and [Messages](https://platform.claude.com/docs/en/api/messages/create) contracts.
144
+ The existing internal adapters keep their legacy default models; this new factory
145
+ requires the caller's choice. Provider classes and execution internals remain private.
146
+
147
+ This unreleased library boundary does not install a background service, attach a
148
+ private brain, persist memory, provision a wallet or authorize trading. It does not
149
+ establish deployed or real-provider acceptance for the owner-local runner.
150
+
26
151
  ## Create a Client
27
152
 
28
153
  ```ts
@@ -109,6 +234,7 @@ const { tools } = await mcp.listTools();
109
234
 
110
235
  const reply = await mcp.askBot({
111
236
  message: "What is the price of SOL?",
237
+ idempotency_key: crypto.randomUUID(),
112
238
  });
113
239
  ```
114
240
 
@@ -117,9 +243,29 @@ For other tools, call the runtime-advertised name returned by `tools/list`:
117
243
  ```ts
118
244
  const research = await mcp.callTool("ask_bot", {
119
245
  message: "Find low-risk Base setups with deep liquidity.",
246
+ idempotency_key: crypto.randomUUID(),
120
247
  });
121
248
  ```
122
249
 
250
+ Create one idempotency key per deliberate protected operation. Keep that key
251
+ with the operation until its outcome is reconciled. A timeout is not proof that
252
+ the operation failed; do not create a new key and blindly repeat it.
253
+
254
+ For a runtime-advertised manage tool, pass fresh owner step-up proof separately
255
+ from tool arguments:
256
+
257
+ ```ts
258
+ await mcp.callTool("agent_control", {
259
+ action: "pause",
260
+ idempotency_key: crypto.randomUUID(),
261
+ }, { stepUpToken: freshOwnerStepUpProof });
262
+ ```
263
+
264
+ Obtain that proof through authenticated Hub. The SDK sends it only on this
265
+ manage call, using `x-balchemy-step-up`; it does not store it or send it on
266
+ `tools/list`, status, or other read calls. Scope, ownership, idempotency, and
267
+ step-up validation remain backend requirements.
268
+
123
269
  The SDK does not expose typed swap/trade helper methods from the root client.
124
270
  Privileged tools require the backend-advertised tool surface, the right MCP key
125
271
  scope, replay/rate protection, and the approved product flow.
@@ -176,7 +322,7 @@ function toSdkFailure(err: unknown): SdkFailure | null {
176
322
  }
177
323
  ```
178
324
 
179
- Known codes include `auth_error`, `policy_error`, `rate_limit`,
325
+ Known codes include `auth_error`, `policy_error`, `rate_limit_error`,
180
326
  `provider_auth_error`, `network_error`, `execution_error`, and
181
327
  `invalid_response`.
182
328
 
@@ -14,15 +14,19 @@ export declare class AgentLoop {
14
14
  private status;
15
15
  private startedAt;
16
16
  private eventsReceived;
17
+ private eventsDropped;
17
18
  private decisionsExecuted;
18
19
  private tradesExecuted;
19
20
  private lastEventAt;
20
21
  private lastTradeAt;
21
- private seenTraceIds;
22
+ private readonly seenTraceIds;
23
+ private readonly pendingEvents;
24
+ private drainPromise;
25
+ private processingHalted;
26
+ private lifecycleGeneration;
22
27
  private portfolioCache;
23
28
  private rulesCache;
24
29
  /** Client-side behavior rule limits for pre-check enforcement. */
25
- private readonly ruleLimits;
26
30
  /** Trades executed in the current clock-hour (reset each hour). */
27
31
  private tradesThisHour;
28
32
  /** Timestamp (ms) of the hour boundary when tradesThisHour was last reset. */
@@ -30,6 +34,11 @@ export declare class AgentLoop {
30
34
  /** publicId extracted from the MCP endpoint path (last path segment). */
31
35
  private readonly publicId;
32
36
  private readonly sessionId;
37
+ private readonly userMessageIdempotencyNamespace;
38
+ private userMessageSequence;
39
+ private readonly maxPendingEvents;
40
+ private readonly maxEventAgeMs;
41
+ private readonly maxEvidenceAgeMs;
33
42
  constructor(config: AgentLoopConfig);
34
43
  start(): Promise<void>;
35
44
  stop(): Promise<void>;
@@ -49,12 +58,8 @@ export declare class AgentLoop {
49
58
  * so the decision loop continues with degraded context.
50
59
  */
51
60
  private fetchPortfolio;
52
- /**
53
- * Fetch compressed behavior rules from the MCP resource
54
- * `balchemy://behavior-rules/{publicId}`.
55
- * Result is cached for 5 minutes. On failure, returns empty string
56
- * so decisions still proceed without rule context.
57
- */
61
+ /** Fetch an exact immutable behavior-rule revision binding. Failures are cached
62
+ * briefly and return null so neither decisions nor submissions proceed unbound. */
58
63
  private fetchBehaviorRules;
59
64
  /**
60
65
  * Reset the hourly trade counter when the clock rolls to a new hour.
@@ -62,5 +67,11 @@ export declare class AgentLoop {
62
67
  */
63
68
  private resetHourlyCounterIfNeeded;
64
69
  private createLlmAdapter;
70
+ private startDrain;
71
+ private drainEvents;
72
+ private isGenerationCurrent;
73
+ private awaitCurrentDrain;
74
+ private notifyError;
75
+ private notifyStatus;
65
76
  }
66
77
  //# sourceMappingURL=agent-loop.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"agent-loop.d.ts","sourceRoot":"","sources":["../../src/agent-loop/agent-loop.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EACV,eAAe,EACf,WAAW,EAMZ,MAAM,SAAS,CAAC;AAmVjB,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkB;IACzC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAiB;IAC7C,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAa;IACjC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAkB;IAClD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAoB;IACxC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAqB;IACjD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAoB;IAC9C,OAAO,CAAC,eAAe,CAAgC;IACvD,OAAO,CAAC,SAAS,CAA+B;IAChD,OAAO,CAAC,cAAc,CAA6B;IAEnD,OAAO,CAAC,MAAM,CAA8B;IAC5C,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,cAAc,CAAK;IAC3B,OAAO,CAAC,iBAAiB,CAAK;IAC9B,OAAO,CAAC,cAAc,CAAK;IAC3B,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,YAAY,CAAqB;IAEzC,OAAO,CAAC,cAAc,CAA+B;IACrD,OAAO,CAAC,UAAU,CAA2B;IAE7C,kEAAkE;IAClE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAqB;IAEhD,mEAAmE;IACnE,OAAO,CAAC,cAAc,CAAK;IAC3B,8EAA8E;IAC9E,OAAO,CAAC,gBAAgB,CAAK;IAE7B,yEAAyE;IACzE,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO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