@loomwork/middleware 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/NOTICE +8 -0
- package/README.md +82 -2
- package/dist/claimCheck.d.ts +179 -0
- package/dist/claimCheck.d.ts.map +1 -0
- package/dist/claimCheck.js +241 -0
- package/dist/claimCheck.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/index.js.map +1 -0
- package/dist/killSwitchRegistry.d.ts +74 -0
- package/dist/killSwitchRegistry.d.ts.map +1 -0
- package/dist/killSwitchRegistry.js +108 -0
- package/dist/killSwitchRegistry.js.map +1 -0
- package/dist/metering.d.ts +81 -0
- package/dist/metering.d.ts.map +1 -0
- package/dist/metering.js +153 -0
- package/dist/metering.js.map +1 -0
- package/dist/nestedBudget.d.ts +60 -0
- package/dist/nestedBudget.d.ts.map +1 -0
- package/dist/nestedBudget.js +134 -0
- package/dist/nestedBudget.js.map +1 -0
- package/dist/profile.d.ts +38 -0
- package/dist/profile.d.ts.map +1 -0
- package/dist/profile.js +70 -0
- package/dist/profile.js.map +1 -0
- package/dist/rateLimit.d.ts +40 -0
- package/dist/rateLimit.d.ts.map +1 -0
- package/dist/rateLimit.js +83 -0
- package/dist/rateLimit.js.map +1 -0
- package/dist/redaction.d.ts +66 -0
- package/dist/redaction.d.ts.map +1 -0
- package/dist/redaction.js +141 -0
- package/dist/redaction.js.map +1 -0
- package/dist/retry.d.ts +50 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +98 -0
- package/dist/retry.js.map +1 -0
- package/dist/safety.d.ts +93 -0
- package/dist/safety.d.ts.map +1 -0
- package/dist/safety.js +152 -0
- package/dist/safety.js.map +1 -0
- package/dist/streamTokenBudget.d.ts +58 -0
- package/dist/streamTokenBudget.d.ts.map +1 -0
- package/dist/streamTokenBudget.js +47 -0
- package/dist/streamTokenBudget.js.map +1 -0
- package/package.json +55 -3
- package/src/claimCheck.ts +410 -0
- package/src/index.ts +79 -0
- package/src/killSwitchRegistry.ts +168 -0
- package/src/metering.ts +226 -0
- package/src/nestedBudget.ts +200 -0
- package/src/profile.ts +86 -0
- package/src/rateLimit.ts +126 -0
- package/src/redaction.ts +184 -0
- package/src/retry.ts +133 -0
- package/src/safety.ts +235 -0
- package/src/streamTokenBudget.ts +98 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,83 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @loomwork/middleware
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Effect middleware: wrappers that run around every tool or model call. This package has the first-party
|
|
4
|
+
ones: retry with backoff, token and cost metering with budgets, behavioural safety validators, secret
|
|
5
|
+
redaction, rate limits, a scoped kill switch, claim-check offload of large results, and timing. Use it
|
|
6
|
+
when a policy belongs on the effect path (before a call runs or after it returns) rather than inside a
|
|
7
|
+
flow. Wire it through `defineConfig` or `new EffectRuntime(middleware)`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { defineConfig } from "@loomwork/app";
|
|
11
|
+
import {
|
|
12
|
+
denyPatterns,
|
|
13
|
+
llmUsageTokens,
|
|
14
|
+
meteringMiddleware,
|
|
15
|
+
retryMiddleware,
|
|
16
|
+
safetyMiddleware,
|
|
17
|
+
} from "@loomwork/middleware";
|
|
18
|
+
|
|
19
|
+
const app = defineConfig({
|
|
20
|
+
storage: { kind: "memory" },
|
|
21
|
+
execution: {
|
|
22
|
+
// The first entry is outermost: safety and quota refusals sit outside the retry loop.
|
|
23
|
+
effectMiddleware: [
|
|
24
|
+
safetyMiddleware({ validators: [denyPatterns([/ignore previous instructions/i])] }),
|
|
25
|
+
meteringMiddleware({
|
|
26
|
+
pricing: ({ result }) => ({ tokens: llmUsageTokens(result) }),
|
|
27
|
+
budget: { maxTokens: 200_000 },
|
|
28
|
+
}),
|
|
29
|
+
retryMiddleware({ maxRetries: 2, baseDelayMs: 200 }),
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Surface
|
|
36
|
+
|
|
37
|
+
- **Reliability.** `retryMiddleware`, `rateLimitMiddleware` (token bucket per execution, tenant,
|
|
38
|
+
workflow or process), `effectTimingMiddleware` (measures what effects cost).
|
|
39
|
+
- **Cost.** `meteringMiddleware` (records `RESOURCE_METERED`, enforces a `Budget`),
|
|
40
|
+
`nestedBudgetMiddleware` with `BudgetLedger` (tenant, workflow and execution ceilings together),
|
|
41
|
+
`streamTokenBudget` (a mid-stream guard), `QuotaExceededError`.
|
|
42
|
+
- **Safety.** `safetyMiddleware` with `denyPatterns`, `piiValidator`, `schemaValidator`, `killSwitch`;
|
|
43
|
+
`killSwitchMiddleware` with `InMemoryKillSwitch` (stop all, a tenant, or a workflow).
|
|
44
|
+
- **Data.** `redactionMiddleware` with `secretRef` and `SecretResolver`; `claimCheckMiddleware`
|
|
45
|
+
with `resolveClaimCheck` and `previewDigest`.
|
|
46
|
+
|
|
47
|
+
## Behaviour that surprises people
|
|
48
|
+
|
|
49
|
+
- **Order is outermost first, and it matters.** `defineConfig` itself puts, from the outside in: kill
|
|
50
|
+
switch, claim-check, redaction, the autonomy envelope (rate limit, budget), then your
|
|
51
|
+
`effectMiddleware`. A retry wrapper placed outside a refusing middleware would re-attempt the refusal.
|
|
52
|
+
- **Retry is in-memory and quiet.** Retries inside one `execute` write no events; use `onRetry` to
|
|
53
|
+
observe them. It never retries a `SuspendError` or an aborted execution. By default it also skips a
|
|
54
|
+
`TypedFailure` and any error with `retryable: false` (safety, quota, rate limit and kill switch
|
|
55
|
+
errors all set it). Defaults: 2 retries, 100 ms base, factor 2, 30 s cap, deterministic jitter.
|
|
56
|
+
- **A budget is checked before an effect, not during it.** Metering refuses the next effect once spend
|
|
57
|
+
is at or over the budget, so the call that crosses it still runs and is metered. Use
|
|
58
|
+
`streamTokenBudget` to cut a streamed call mid-flight.
|
|
59
|
+
- **Rate limits are per process.** Bucket state is local, wall-clock driven and never logged, so two
|
|
60
|
+
nodes each enforce their own limit. Buckets are LRU-bounded (10,000 by default).
|
|
61
|
+
- **The kill switch refuses the next effect.** It does not interrupt one already running, and it fails
|
|
62
|
+
closed if its registry throws.
|
|
63
|
+
- **Safety validators run on live execution only.** A replay serves completed steps from the log and
|
|
64
|
+
does not call them. A block is recorded as `SAFETY_BLOCKED`. For a streamed call, an output
|
|
65
|
+
validator without a delta form makes the runtime refuse streaming to every delta sink, and the delta
|
|
66
|
+
check sees only a trailing window (4096 characters by default), so a pattern longer than that can
|
|
67
|
+
slip through the live tail.
|
|
68
|
+
- **`piiValidator` is a few regular expressions** (email, US SSN, card-like digits), not a classifier.
|
|
69
|
+
- **Claim-check defaults differ.** The middleware's inline cap is 16 KiB; `defineConfig`'s
|
|
70
|
+
`execution.claimCheck` defaults to 1 KiB. An offloaded blob is TTL-reclaimable unless `retain` is set.
|
|
71
|
+
|
|
72
|
+
## See also
|
|
73
|
+
|
|
74
|
+
- API reference: section "`@loomwork/middleware` — effect middleware" in
|
|
75
|
+
[docs/api-reference.org](../../docs/api-reference.org).
|
|
76
|
+
- Guide: [quotas, supervision and cancellation](../../docs/user-guide/quotas-supervision-and-cancellation.md).
|
|
77
|
+
- ADRs: [0054 claim-check](../../docs/adr/0054-claim-check-middleware.md),
|
|
78
|
+
[0119 streaming deltas](../../docs/adr/0119-decide-scope-streaming-deltas.md),
|
|
79
|
+
[0140 claim-check for agent state](../../docs/adr/0140-agent-claim-check-opt-in.md).
|
|
80
|
+
|
|
81
|
+
## Install
|
|
82
|
+
|
|
83
|
+
Unpublished for now: inside this repo use `workspace:*`; from a sibling checkout use a `link:` override (see [AGENTS.md](../../AGENTS.md)).
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type { EffectMiddleware } from "@loomwork/effect-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* ADR 0143 - the execution a content operation is for (structural twin of `@loomwork/plugin-content`'s
|
|
4
|
+
* `ContentAccess`, ContentStore SPI 1.5.0). The middleware passes the running effect's execution on every
|
|
5
|
+
* `put`, so a store that seals per subject knows whose key to use; a plain store ignores it.
|
|
6
|
+
*/
|
|
7
|
+
export interface ClaimCheckAccess {
|
|
8
|
+
readonly executionId: string;
|
|
9
|
+
readonly workflowId: string;
|
|
10
|
+
}
|
|
11
|
+
/** Write-side view of the content store (structural — no plugin dependency). */
|
|
12
|
+
export interface ClaimCheckStore {
|
|
13
|
+
/** Content-addressed, idempotent by hash — the `ContentStore.put` contract (ADR 0046). */
|
|
14
|
+
put(bytes: Uint8Array, access?: ClaimCheckAccess): Promise<{
|
|
15
|
+
__loomContent: string;
|
|
16
|
+
}>;
|
|
17
|
+
/** OPTIONAL (ST-1, ADR 0059) — streaming ingestion; required only for stream-typed results. */
|
|
18
|
+
putStream?(stream: AsyncIterable<Uint8Array>, access?: ClaimCheckAccess): Promise<{
|
|
19
|
+
__loomContent: string;
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
22
|
+
/** Read-side view, for {@link resolveClaimCheck} OUTSIDE the determinism boundary. */
|
|
23
|
+
export interface ClaimCheckReader {
|
|
24
|
+
get(ref: {
|
|
25
|
+
__loomContent: string;
|
|
26
|
+
}, access?: ClaimCheckAccess): Promise<Uint8Array | null>;
|
|
27
|
+
/**
|
|
28
|
+
* OPTIONAL (TS-10, ADR 0062) — batched get, positionally aligned, null per
|
|
29
|
+
* absent entry. A `ContentStore` with `getMany` satisfies this structurally.
|
|
30
|
+
*/
|
|
31
|
+
getMany?(refs: readonly {
|
|
32
|
+
__loomContent: string;
|
|
33
|
+
}[], access?: ClaimCheckAccess): Promise<(Uint8Array | null)[]>;
|
|
34
|
+
}
|
|
35
|
+
/** What STEP_COMPLETED records in place of an over-cap effect result. */
|
|
36
|
+
export interface ClaimCheckEnvelope {
|
|
37
|
+
readonly __loomClaimCheck: true;
|
|
38
|
+
readonly ref: {
|
|
39
|
+
readonly __loomContent: string;
|
|
40
|
+
};
|
|
41
|
+
/** Byte length of the offloaded payload. */
|
|
42
|
+
readonly size: number;
|
|
43
|
+
/** The tool's declared deterministic projection of the result (CC-2), if any. */
|
|
44
|
+
readonly digest?: unknown;
|
|
45
|
+
/**
|
|
46
|
+
* How the offloaded bytes decode. Absent = JSON (the classic offload path);
|
|
47
|
+
* stream-typed results record their declared codec ("bytes" default) —
|
|
48
|
+
* resolve those with {@link resolveClaimCheckBytes}, not JSON parsing.
|
|
49
|
+
*/
|
|
50
|
+
readonly codec?: string;
|
|
51
|
+
/**
|
|
52
|
+
* ADR 0140 - `true`: the offloaded bytes are the ONLY copy of a value the log once held (the opt-in
|
|
53
|
+
* agent claim-check), so the content sweep treats the ref as LOAD-BEARING: kept while any live log,
|
|
54
|
+
* archive segment or snapshot cites it, freed only when that history is erased or discarded. Absent
|
|
55
|
+
* (the classic ADR 0054 telemetry offload): the ref is TTL-reclaimable (ADR 0055 D4).
|
|
56
|
+
*/
|
|
57
|
+
readonly retain?: true;
|
|
58
|
+
}
|
|
59
|
+
export interface StreamedResult {
|
|
60
|
+
readonly __loomStreamResult: true;
|
|
61
|
+
readonly stream: AsyncIterable<Uint8Array>;
|
|
62
|
+
/** The declared deterministic projection (CC-2). Pure w.r.t. the streamed content. */
|
|
63
|
+
readonly digest?: unknown;
|
|
64
|
+
/** Recorded on the envelope; default "bytes". */
|
|
65
|
+
readonly codec?: string;
|
|
66
|
+
}
|
|
67
|
+
/** Wrap a byte stream as a tool result; Node `Readable`s satisfy the iterable directly. */
|
|
68
|
+
export declare function streamResult(stream: AsyncIterable<Uint8Array>, opts?: {
|
|
69
|
+
digest?: unknown;
|
|
70
|
+
codec?: string;
|
|
71
|
+
}): StreamedResult;
|
|
72
|
+
export declare function isStreamResult(value: unknown): value is StreamedResult;
|
|
73
|
+
export declare function isClaimCheck(value: unknown): value is ClaimCheckEnvelope;
|
|
74
|
+
/** CC-3 — offload observability: what, where, how big. */
|
|
75
|
+
export interface ClaimCheckInfo {
|
|
76
|
+
toolId?: string;
|
|
77
|
+
stepId: string;
|
|
78
|
+
executionId: string;
|
|
79
|
+
workflowId: string;
|
|
80
|
+
size: number;
|
|
81
|
+
contentAddress: string;
|
|
82
|
+
}
|
|
83
|
+
export interface ClaimCheckOptions {
|
|
84
|
+
store: ClaimCheckStore;
|
|
85
|
+
/**
|
|
86
|
+
* Inline cap in bytes for the JSON-encoded effect result; results at or under
|
|
87
|
+
* the cap are recorded inline, untouched. Default 16 KiB.
|
|
88
|
+
*/
|
|
89
|
+
inlineCapBytes?: number;
|
|
90
|
+
/**
|
|
91
|
+
* CC-2 — `toolId → digest` declarations. A digest is the tool's DETERMINISTIC
|
|
92
|
+
* projection of its result: the one part of an offloaded payload that
|
|
93
|
+
* orchestration may branch on, recorded inline in the envelope. MUST be a pure
|
|
94
|
+
* function of the result. Tools without a declared digest record `{ ref, size }`
|
|
95
|
+
* only — their orchestration can branch on nothing but existence.
|
|
96
|
+
*/
|
|
97
|
+
digests?: Readonly<Record<string, (result: unknown) => unknown>>;
|
|
98
|
+
/**
|
|
99
|
+
* The projection used for an offloaded result whose tool has NO entry in {@link digests}
|
|
100
|
+
* (a per-tool digest always wins). Same contract as a digest: PURE w.r.t. the result, because it is
|
|
101
|
+
* recorded inline in the envelope. Unset (the default) keeps the classic behaviour: such a tool
|
|
102
|
+
* records `{ ref, size }` only. {@link previewDigest} is a ready-made one.
|
|
103
|
+
*/
|
|
104
|
+
defaultDigest?: (result: unknown, toolId: string | undefined) => unknown;
|
|
105
|
+
/** CC-3 — called after each offload (metrics/log-budget regression tracking). */
|
|
106
|
+
onOffload?: (info: ClaimCheckInfo) => void;
|
|
107
|
+
/**
|
|
108
|
+
* ADR 0140 - mark the envelope `retain: true` (see {@link ClaimCheckEnvelope.retain}): the offloaded
|
|
109
|
+
* blob is audit evidence the content sweep must not TTL-reclaim. A function decides per tool id. Default
|
|
110
|
+
* unset: the classic reclaimable telemetry offload.
|
|
111
|
+
*/
|
|
112
|
+
retain?: boolean | ((toolId: string | undefined) => boolean);
|
|
113
|
+
/**
|
|
114
|
+
* ADR 0140 - results this returns `true` for pass through UNTOUCHED, never offloaded (e.g. a media
|
|
115
|
+
* result, whose parts the loop renders itself). Runs before the cap.
|
|
116
|
+
*/
|
|
117
|
+
passThrough?: (result: unknown, toolId: string | undefined) => boolean;
|
|
118
|
+
/**
|
|
119
|
+
* ADR 0140 - treat a result that already LOOKS like an envelope as DATA, not as a ref this middleware
|
|
120
|
+
* made: it is force-offloaded like any other value, so the log never records a tool-forged
|
|
121
|
+
* `{ __loomClaimCheck, ref }` the sweep, the resolvers or the model would trust. Default unset: an
|
|
122
|
+
* existing envelope passes through (stacked wiring, never double-wrapped).
|
|
123
|
+
*/
|
|
124
|
+
envelopesAreData?: boolean;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* CC-1 — claim-check middleware on the ADR 0006 seam. Runs only when the effect
|
|
128
|
+
* actually executes (replayed/completed steps short-circuit upstream), so replay
|
|
129
|
+
* determinism is automatic: the envelope is the recorded value. Crash-recovery
|
|
130
|
+
* re-runs re-offload the same bytes to the same content address (`put` is
|
|
131
|
+
* idempotent by hash), reproducing an identical envelope.
|
|
132
|
+
*
|
|
133
|
+
* Results that JSON cannot encode (circular, undefined) pass through untouched —
|
|
134
|
+
* offload is impossible for what the journal could not record anyway.
|
|
135
|
+
*/
|
|
136
|
+
export declare function claimCheckMiddleware(options: ClaimCheckOptions): EffectMiddleware;
|
|
137
|
+
/** What {@link previewDigest} records: the head of the result's JSON text. */
|
|
138
|
+
export interface PreviewDigest {
|
|
139
|
+
/** The first `maxChars` UTF-16 code units of `JSON.stringify(result)`, never ending mid-surrogate-pair. */
|
|
140
|
+
readonly preview: string;
|
|
141
|
+
/** `true` when the result's JSON continues past the preview (the tail is NOT shown); absent when the preview is the whole text. */
|
|
142
|
+
readonly truncated?: true;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* A ready-made `defaultDigest` (CC-2): the first `maxChars` (default 256) characters of the result's
|
|
146
|
+
* JSON text, so the model sees what an offloaded tool result BEGAN with instead of a bare hash. Pure
|
|
147
|
+
* and deterministic (a function of the result only). It is NOT a summary: it is a head, and it can cut
|
|
148
|
+
* a JSON string anywhere.
|
|
149
|
+
*
|
|
150
|
+
* Untrusted content is never previewed: a result carrying an ADR 0085 taint marker ANYWHERE (a nested
|
|
151
|
+
* `{ __loomTainted }` wrapper, such as an A2A reply's `content`) yields no digest at all, because a
|
|
152
|
+
* digest rides into the model's prompt as ordinary text and would launder the taint. A result JSON cannot
|
|
153
|
+
* encode yields none either.
|
|
154
|
+
*/
|
|
155
|
+
export declare function previewDigest(maxChars?: number): (result: unknown, toolId?: string) => PreviewDigest | undefined;
|
|
156
|
+
/**
|
|
157
|
+
* CC-4 — resolve an envelope back to its payload, for use OUTSIDE the
|
|
158
|
+
* determinism boundary (rendering evidence in a UI, exporting a report).
|
|
159
|
+
* Returns `null` when the bytes were erased/never stored — the store's
|
|
160
|
+
* null-on-absence contract, surfaced as-is so callers decide loudness.
|
|
161
|
+
* Flow control must NOT branch on the resolved payload (CC-5 lint).
|
|
162
|
+
*/
|
|
163
|
+
export declare function resolveClaimCheck<T = unknown>(reader: ClaimCheckReader, envelope: ClaimCheckEnvelope, access?: ClaimCheckAccess): Promise<T | null>;
|
|
164
|
+
/**
|
|
165
|
+
* CC-4 for stream-offloaded content (ST-2): the raw bytes, `null` on absence.
|
|
166
|
+
* Same boundary rule as {@link resolveClaimCheck}: OUTSIDE the determinism
|
|
167
|
+
* boundary only.
|
|
168
|
+
*/
|
|
169
|
+
export declare function resolveClaimCheckBytes(reader: ClaimCheckReader, envelope: ClaimCheckEnvelope, access?: ClaimCheckAccess): Promise<Uint8Array | null>;
|
|
170
|
+
/**
|
|
171
|
+
* TS-10 (ADR 0062) — the batch twin of {@link resolveClaimCheck}, for
|
|
172
|
+
* interactive read paths (frozen-snapshot serving: a completed run's envelopes,
|
|
173
|
+
* one human waiting). One `getMany` round-trip when the reader supports it
|
|
174
|
+
* (coalesced parallel `get`s otherwise), deduped by content address; results
|
|
175
|
+
* positionally aligned with `envelopes`, `null` per erased/absent entry.
|
|
176
|
+
* Same boundary rule: OUTSIDE the determinism boundary only.
|
|
177
|
+
*/
|
|
178
|
+
export declare function resolveClaimChecks<T = unknown>(reader: ClaimCheckReader, envelopes: readonly ClaimCheckEnvelope[], access?: ClaimCheckAccess): Promise<(T | null)[]>;
|
|
179
|
+
//# sourceMappingURL=claimCheck.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claimCheck.d.ts","sourceRoot":"","sources":["../src/claimCheck.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAajE;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED,gFAAgF;AAChF,MAAM,WAAW,eAAe;IAC9B,0FAA0F;IAC1F,GAAG,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC;QAAE,aAAa,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACtF,+FAA+F;IAC/F,SAAS,CAAC,CACR,MAAM,EAAE,aAAa,CAAC,UAAU,CAAC,EACjC,MAAM,CAAC,EAAE,gBAAgB,GAC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