@prestack/thread 0.0.1 → 0.0.2
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/dist/core/actor.ts +20 -0
- package/dist/core/asking.d.ts +0 -1
- package/dist/core/asking.js +0 -1
- package/dist/core/asking.ts +1342 -0
- package/dist/core/attach.js +2 -2
- package/dist/core/attach.ts +585 -0
- package/dist/core/chain.js +8 -1
- package/dist/core/chain.ts +148 -0
- package/dist/core/cli.js +97 -18
- package/dist/core/cli.ts +2004 -0
- package/dist/core/condition.ts +227 -0
- package/dist/core/descend.d.ts +1 -0
- package/dist/core/descend.js +36 -0
- package/dist/core/descend.ts +1001 -0
- package/dist/core/finding.ts +38 -0
- package/dist/core/generators/meta.js +6 -0
- package/dist/core/generators/meta.ts +1006 -0
- package/dist/core/init.ts +170 -0
- package/dist/core/invoked.ts +47 -0
- package/dist/core/round.ts +57 -0
- package/dist/core/rows.ts +369 -0
- package/dist/core/scope.ts +18 -0
- package/dist/core/thread.ts +29 -0
- package/dist/core/tree.ts +67 -0
- package/dist/core/types.ts +350 -0
- package/dist/derivation/attach.js +1 -1
- package/dist/derivation/attach.ts +68 -0
- package/dist/derivation/generators/build.ts +232 -0
- package/dist/derivation/generators/check.ts +118 -0
- package/dist/derivation/generators/derivation.ts +347 -0
- package/dist/derivation/generators/determinism.ts +145 -0
- package/dist/derivation/generators/release.js +19 -9
- package/dist/derivation/generators/release.ts +213 -0
- package/dist/derivation/makers/release.ts +135 -0
- package/dist/derivation/release.ts +23 -0
- package/dist/integrations/attach.js +2 -2
- package/dist/integrations/attach.ts +207 -0
- package/dist/integrations/generators/read.ts +127 -0
- package/dist/integrations/generators/secret.ts +289 -0
- package/dist/integrations/generators/target.js +16 -6
- package/dist/integrations/generators/target.ts +944 -0
- package/dist/integrations/generators/vault.js +46 -14
- package/dist/integrations/generators/vault.ts +212 -0
- package/dist/integrations/{sdk.js → module.js} +4 -4
- package/dist/integrations/module.ts +189 -0
- package/dist/integrations/secret.ts +50 -0
- package/dist/integrations/target.d.ts +3 -1
- package/dist/integrations/target.js +14 -11
- package/dist/integrations/target.ts +340 -0
- package/dist/integrations/vault.d.ts +2 -0
- package/dist/integrations/vault.js +4 -2
- package/dist/integrations/vault.ts +136 -0
- package/dist/integrity/attach.ts +234 -0
- package/dist/integrity/check-threads.js +2 -26
- package/dist/integrity/check-threads.ts +166 -0
- package/dist/integrity/constraint.ts +1202 -0
- package/dist/integrity/generators/compiler.ts +161 -0
- package/dist/integrity/generators/gate.ts +201 -0
- package/dist/integrity/generators/issues.ts +160 -0
- package/dist/integrity/generators/publish.js +14 -5
- package/dist/integrity/generators/publish.ts +247 -0
- package/dist/integrity/generators/rule.ts +271 -0
- package/dist/integrity/generators/seal.ts +117 -0
- package/dist/integrity/generators/trial.ts +320 -0
- package/dist/integrity/lint.ts +227 -0
- package/dist/integrity/lsp/bin.ts +42 -0
- package/dist/integrity/lsp/diagnostics.ts +92 -0
- package/dist/integrity/lsp/findings.ts +63 -0
- package/dist/integrity/lsp/lint.ts +140 -0
- package/dist/integrity/lsp/protocol.ts +78 -0
- package/dist/integrity/lsp/server.ts +224 -0
- package/dist/integrity/sealed.js +2 -2
- package/dist/integrity/sealed.ts +56 -0
- package/dist/integrity/slopnator.ts +350 -0
- package/dist/integrity/slopnators/sl_broken_lineage.ts +36 -0
- package/dist/integrity/slopnators/sl_declared_elsewhere.ts +39 -0
- package/dist/integrity/slopnators/sl_derived_drift.ts +98 -0
- package/dist/integrity/slopnators/sl_hardcoded_style.ts +55 -0
- package/dist/integrity/slopnators/sl_inert_island.ts +91 -0
- package/dist/integrity/slopnators/sl_inert_slopnator.ts +93 -0
- package/dist/integrity/slopnators/sl_missing_row.ts +182 -0
- package/dist/integrity/slopnators/sl_no_barrel.ts +48 -0
- package/dist/integrity/slopnators/sl_no_child_process.ts +74 -0
- package/dist/integrity/slopnators/sl_no_comment.ts +43 -0
- package/dist/integrity/slopnators/sl_no_dispatch_bypass.ts +328 -0
- package/dist/integrity/slopnators/sl_no_import_cycle.ts +50 -0
- package/dist/integrity/slopnators/sl_no_such_command.ts +64 -0
- package/dist/integrity/slopnators/sl_only_takes_a_literal.ts +63 -0
- package/dist/integrity/slopnators/sl_secret_literal.ts +100 -0
- package/dist/integrity/slopnators/sl_stale_path.ts +94 -0
- package/dist/integrity/slopnators/sl_twice_meant.ts +31 -0
- package/dist/integrity/slopnators/sl_twice_parented.ts +37 -0
- package/dist/integrity/slopnators/sl_undeclared_crossing.ts +87 -0
- package/dist/integrity/slopnators/sl_undeclared_destination.ts +65 -0
- package/dist/integrity/slopnators/sl_undeclared_element.ts +44 -0
- package/dist/integrity/slopnators/sl_undeclared_node.ts +69 -0
- package/dist/integrity/slopnators/sl_undeclared_operation.ts +147 -0
- package/dist/integrity/slopnators/sl_undeclared_variant.ts +148 -0
- package/dist/integrity/slopnators/sl_undescribed.ts +65 -0
- package/dist/integrity/slopnators/sl_ungoverned_island.ts +61 -0
- package/dist/integrity/slopnators/sl_unkept_rows.ts +111 -0
- package/dist/integrity/slopnators/sl_unnamed_island.ts +41 -0
- package/dist/integrity/slopnators/sl_unpaired_member.ts +131 -0
- package/dist/integrity/slopnators/sl_unpaired_slopnator.ts +58 -0
- package/dist/integrity/slopnators/sl_unprefixed_member.ts +136 -0
- package/dist/integrity/slopnators/sl_unseamed_reach.ts +79 -0
- package/dist/integrity/slopnators/sl_unthreaded.ts +419 -0
- package/dist/integrity/slopnators/sl_untranslated_key.ts +164 -0
- package/dist/integrity/slopnators/sl_unversioned.ts +36 -0
- package/dist/integrity/sweep.ts +307 -0
- package/dist/knowledge/attach.ts +350 -0
- package/dist/knowledge/generators/docsgen.ts +174 -0
- package/dist/knowledge/generators/glossary.ts +73 -0
- package/dist/knowledge/generators/skills.ts +263 -0
- package/dist/knowledge/glossary.ts +6 -0
- package/dist/operations/access.ts +88 -0
- package/dist/operations/attach.ts +475 -0
- package/dist/operations/capabilities.ts +65 -0
- package/dist/operations/cost.ts +58 -0
- package/dist/operations/dispatch.ts +348 -0
- package/dist/operations/error-results.ts +31 -0
- package/dist/operations/errors/catalog.ts +168 -0
- package/dist/operations/errors/classes.ts +51 -0
- package/dist/operations/errors/errors.ts +314 -0
- package/dist/operations/generators/capability.js +29 -19
- package/dist/operations/generators/capability.ts +704 -0
- package/dist/operations/generators/compile.js +20 -1
- package/dist/operations/generators/compile.ts +643 -0
- package/dist/operations/generators/grant.ts +336 -0
- package/dist/operations/generators/operation.ts +972 -0
- package/dist/operations/generators/permits.ts +223 -0
- package/dist/operations/generators/seed.ts +347 -0
- package/dist/operations/generators/trial.js +4 -1
- package/dist/operations/generators/trial.ts +529 -0
- package/dist/operations/http.ts +164 -0
- package/dist/operations/makers/capability.ts +166 -0
- package/dist/operations/makers/trial.ts +520 -0
- package/dist/operations/memory.ts +470 -0
- package/dist/operations/metering.ts +328 -0
- package/dist/operations/nodes.ts +54 -0
- package/dist/operations/operations.ts +32 -0
- package/dist/operations/queue-policy.ts +10 -0
- package/dist/operations/result.ts +28 -0
- package/dist/operations/seams.d.ts +4 -4
- package/dist/operations/seams.ts +364 -0
- package/dist/operations/store.ts +29 -0
- package/package.json +8 -1
- package/dist/integrations/generators/sdk.d.ts +0 -1
- package/dist/integrations/generators/sdk.js +0 -148
- /package/dist/integrations/{sdk.d.ts → module.d.ts} +0 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { PlatformError } from './errors/catalog.ts'
|
|
2
|
+
import { DbRoundTripBudgetExceeded } from './errors/errors.ts'
|
|
3
|
+
|
|
4
|
+
import type { Actor } from '../core/actor.ts'
|
|
5
|
+
import type { PropertyValue, Row, Search, Seams, Store, Transactional, Views } from './seams.ts'
|
|
6
|
+
import { isTransactional } from './seams.ts'
|
|
7
|
+
|
|
8
|
+
export interface ActionBudget {
|
|
9
|
+
|
|
10
|
+
readonly roundTrips: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface WriteAmplification {
|
|
14
|
+
readonly entity: string
|
|
15
|
+
readonly id: string
|
|
16
|
+
|
|
17
|
+
readonly columns: readonly string[]
|
|
18
|
+
|
|
19
|
+
readonly bytes: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface DatabaseUsage {
|
|
23
|
+
|
|
24
|
+
readonly roundTrips: number
|
|
25
|
+
readonly rowsRead: number
|
|
26
|
+
readonly rowsWritten: number
|
|
27
|
+
readonly bytesRead: number
|
|
28
|
+
readonly bytesWritten: number
|
|
29
|
+
|
|
30
|
+
readonly amplified: readonly WriteAmplification[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class RoundTripBudgetExceededError extends PlatformError {
|
|
34
|
+
readonly budget: number
|
|
35
|
+
|
|
36
|
+
readonly refusedStatement: string
|
|
37
|
+
|
|
38
|
+
constructor(budget: number, statement: string) {
|
|
39
|
+
const detail =
|
|
40
|
+
`this operation declared a budget of ${budget} database round trip(s) and asked for ` +
|
|
41
|
+
`"${statement}" as number ${budget + 1}`
|
|
42
|
+
|
|
43
|
+
super(DbRoundTripBudgetExceeded.def, { detail, because: BUDGET_EXCEEDED_BECAUSE })
|
|
44
|
+
this.name = 'RoundTripBudgetExceededError'
|
|
45
|
+
|
|
46
|
+
this.message = detail
|
|
47
|
+
this.budget = budget
|
|
48
|
+
this.refusedStatement = statement
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const BUDGET_EXCEEDED_BECAUSE = 'round_trip_budget_exceeded'
|
|
53
|
+
|
|
54
|
+
export const DB_USAGE_PROPERTY = 'db'
|
|
55
|
+
|
|
56
|
+
export const DB_USAGE_ATTRIBUTES = {
|
|
57
|
+
roundTrips: 'thread.db.round_trips',
|
|
58
|
+
rowsRead: 'thread.db.rows_read',
|
|
59
|
+
rowsWritten: 'thread.db.rows_written',
|
|
60
|
+
bytesRead: 'thread.db.bytes_read',
|
|
61
|
+
bytesWritten: 'thread.db.bytes_written',
|
|
62
|
+
budgetRoundTrips: 'thread.db.budget_round_trips',
|
|
63
|
+
} as const
|
|
64
|
+
|
|
65
|
+
export const DB_AMPLIFICATION_PROPERTY = 'db_amplification'
|
|
66
|
+
|
|
67
|
+
export const DB_AMPLIFICATION_ATTRIBUTES = {
|
|
68
|
+
entities: 'thread.db.amplified_entities',
|
|
69
|
+
columns: 'thread.db.amplified_columns',
|
|
70
|
+
bytes: 'thread.db.amplified_bytes',
|
|
71
|
+
writes: 'thread.db.amplified_writes',
|
|
72
|
+
} as const
|
|
73
|
+
|
|
74
|
+
export function databaseUsageValues(
|
|
75
|
+
usage: DatabaseUsage,
|
|
76
|
+
budget?: ActionBudget,
|
|
77
|
+
): Readonly<Record<string, PropertyValue>> {
|
|
78
|
+
return {
|
|
79
|
+
[DB_USAGE_ATTRIBUTES.roundTrips]: usage.roundTrips,
|
|
80
|
+
[DB_USAGE_ATTRIBUTES.rowsRead]: usage.rowsRead,
|
|
81
|
+
[DB_USAGE_ATTRIBUTES.rowsWritten]: usage.rowsWritten,
|
|
82
|
+
[DB_USAGE_ATTRIBUTES.bytesRead]: usage.bytesRead,
|
|
83
|
+
[DB_USAGE_ATTRIBUTES.bytesWritten]: usage.bytesWritten,
|
|
84
|
+
...(budget === undefined ? {} : { [DB_USAGE_ATTRIBUTES.budgetRoundTrips]: budget.roundTrips }),
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function writeAmplificationValues(
|
|
89
|
+
usage: DatabaseUsage,
|
|
90
|
+
): Readonly<Record<string, PropertyValue>> | undefined {
|
|
91
|
+
if (usage.amplified.length === 0) return undefined
|
|
92
|
+
|
|
93
|
+
const entities = [...new Set(usage.amplified.map((write) => write.entity))]
|
|
94
|
+
const columns = [...new Set(usage.amplified.flatMap((write) => write.columns))]
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
[DB_AMPLIFICATION_ATTRIBUTES.entities]: entities.join(','),
|
|
98
|
+
[DB_AMPLIFICATION_ATTRIBUTES.columns]: columns.join(','),
|
|
99
|
+
[DB_AMPLIFICATION_ATTRIBUTES.bytes]: usage.amplified.reduce((total, write) => total + write.bytes, 0),
|
|
100
|
+
[DB_AMPLIFICATION_ATTRIBUTES.writes]: usage.amplified.length,
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const MAX_WALK_DEPTH = 24
|
|
105
|
+
|
|
106
|
+
function utf8Length(text: string): number {
|
|
107
|
+
let bytes = 0
|
|
108
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
109
|
+
const code = text.charCodeAt(index)
|
|
110
|
+
if (code < 0x80) bytes += 1
|
|
111
|
+
else if (code < 0x800) bytes += 2
|
|
112
|
+
else if (code >= 0xd800 && code <= 0xdbff && index + 1 < text.length) {
|
|
113
|
+
|
|
114
|
+
bytes += 4
|
|
115
|
+
index += 1
|
|
116
|
+
} else bytes += 3
|
|
117
|
+
}
|
|
118
|
+
return bytes
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function payloadBytes(value: unknown, depth = 0): number {
|
|
122
|
+
if (value === null || value === undefined) return 0
|
|
123
|
+
if (depth > MAX_WALK_DEPTH) return 0
|
|
124
|
+
|
|
125
|
+
switch (typeof value) {
|
|
126
|
+
case 'string':
|
|
127
|
+
return utf8Length(value)
|
|
128
|
+
case 'number':
|
|
129
|
+
case 'boolean':
|
|
130
|
+
case 'bigint':
|
|
131
|
+
|
|
132
|
+
return 8
|
|
133
|
+
case 'object':
|
|
134
|
+
break
|
|
135
|
+
default:
|
|
136
|
+
|
|
137
|
+
return 0
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (value instanceof Date) return 8
|
|
141
|
+
if (value instanceof Uint8Array) return value.byteLength
|
|
142
|
+
if (Array.isArray(value)) {
|
|
143
|
+
let total = 0
|
|
144
|
+
for (const item of value) total += payloadBytes(item, depth + 1)
|
|
145
|
+
return total
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let total = 0
|
|
149
|
+
for (const [key, nested] of Object.entries(value as Record<string, unknown>)) {
|
|
150
|
+
|
|
151
|
+
total += utf8Length(key) + payloadBytes(nested, depth + 1)
|
|
152
|
+
}
|
|
153
|
+
return total
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function sameStoredValue(left: unknown, right: unknown, depth = 0): boolean {
|
|
157
|
+
if (left === right) return true
|
|
158
|
+
if (depth > MAX_WALK_DEPTH) return false
|
|
159
|
+
|
|
160
|
+
if (left instanceof Date || right instanceof Date) {
|
|
161
|
+
return left instanceof Date && right instanceof Date && left.getTime() === right.getTime()
|
|
162
|
+
}
|
|
163
|
+
if (typeof left !== 'object' || typeof right !== 'object' || left === null || right === null) return false
|
|
164
|
+
if (Array.isArray(left) !== Array.isArray(right)) return false
|
|
165
|
+
|
|
166
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
167
|
+
return left.length === right.length && left.every((item, index) => sameStoredValue(item, right[index], depth + 1))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const a = left as Record<string, unknown>
|
|
171
|
+
const b = right as Record<string, unknown>
|
|
172
|
+
const keys = Object.keys(a)
|
|
173
|
+
if (keys.length !== Object.keys(b).length) return false
|
|
174
|
+
return keys.every((key) => key in b && sameStoredValue(a[key], b[key], depth + 1))
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface MeterableSeams {
|
|
178
|
+
readonly store?: Store
|
|
179
|
+
readonly search?: Search
|
|
180
|
+
readonly views?: Views
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export interface Metered<S> {
|
|
184
|
+
readonly seams: S
|
|
185
|
+
|
|
186
|
+
usage(): DatabaseUsage
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export type MeteredSeams = Metered<Seams>
|
|
190
|
+
|
|
191
|
+
export function meterDatabase<S extends MeterableSeams>(seams: S, budget?: ActionBudget): Metered<S> {
|
|
192
|
+
let roundTrips = 0
|
|
193
|
+
let rowsRead = 0
|
|
194
|
+
let rowsWritten = 0
|
|
195
|
+
let bytesRead = 0
|
|
196
|
+
let bytesWritten = 0
|
|
197
|
+
const amplified: WriteAmplification[] = []
|
|
198
|
+
|
|
199
|
+
const seen = new Map<string, Row>()
|
|
200
|
+
const remember = (entity: string, row: Row): void => {
|
|
201
|
+
seen.set(`${entity}:${row.id}`, row)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const spend = (statement: string): void => {
|
|
205
|
+
if (budget !== undefined && roundTrips >= budget.roundTrips) {
|
|
206
|
+
|
|
207
|
+
throw new RoundTripBudgetExceededError(budget.roundTrips, statement)
|
|
208
|
+
}
|
|
209
|
+
roundTrips += 1
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const readRows = (rows: readonly unknown[]): void => {
|
|
213
|
+
rowsRead += rows.length
|
|
214
|
+
for (const row of rows) bytesRead += payloadBytes(row)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const base = seams.store
|
|
218
|
+
|
|
219
|
+
const store: (Store & Partial<Transactional>) | undefined = base === undefined ? undefined : {
|
|
220
|
+
async insert(actor, entity, row) {
|
|
221
|
+
spend(`insert ${entity}`)
|
|
222
|
+
bytesWritten += payloadBytes(row)
|
|
223
|
+
rowsWritten += 1
|
|
224
|
+
const written = await base.insert(actor, entity, row)
|
|
225
|
+
remember(entity, written)
|
|
226
|
+
return written
|
|
227
|
+
},
|
|
228
|
+
|
|
229
|
+
async get(actor, entity, id, fields) {
|
|
230
|
+
spend(`get ${entity}`)
|
|
231
|
+
const row = await base.get(actor, entity, id, fields)
|
|
232
|
+
if (row !== null) {
|
|
233
|
+
rowsRead += 1
|
|
234
|
+
bytesRead += payloadBytes(row)
|
|
235
|
+
remember(entity, row)
|
|
236
|
+
}
|
|
237
|
+
return row
|
|
238
|
+
},
|
|
239
|
+
|
|
240
|
+
async list(actor, entity, opts) {
|
|
241
|
+
spend(`list ${entity}`)
|
|
242
|
+
const rows = await base.list(actor, entity, opts)
|
|
243
|
+
readRows(rows)
|
|
244
|
+
for (const row of rows) remember(entity, row)
|
|
245
|
+
return rows
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
async update(actor, entity, id, patch, expectedVersion) {
|
|
249
|
+
spend(`update ${entity}`)
|
|
250
|
+
recordAmplification(entity, id, patch)
|
|
251
|
+
bytesWritten += payloadBytes(patch)
|
|
252
|
+
rowsWritten += 1
|
|
253
|
+
const row = await base.update(actor, entity, id, patch, expectedVersion)
|
|
254
|
+
remember(entity, row)
|
|
255
|
+
return row
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
async remove(actor, entity, id) {
|
|
259
|
+
|
|
260
|
+
spend(`remove ${entity}`)
|
|
261
|
+
rowsWritten += 1
|
|
262
|
+
await base.remove(actor, entity, id)
|
|
263
|
+
seen.delete(`${entity}:${id}`)
|
|
264
|
+
},
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (base !== undefined && store !== undefined && isTransactional(base)) {
|
|
268
|
+
store.transaction = (actor, fn) => base.transaction(actor, fn)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function recordAmplification(entity: string, id: string | number, patch: Record<string, unknown>): void {
|
|
272
|
+
const stored = seen.get(`${entity}:${id}`)
|
|
273
|
+
if (stored === undefined) return
|
|
274
|
+
|
|
275
|
+
const columns: string[] = []
|
|
276
|
+
let bytes = 0
|
|
277
|
+
for (const [column, value] of Object.entries(patch)) {
|
|
278
|
+
if (!(column in stored)) continue
|
|
279
|
+
if (!sameStoredValue(value, stored[column])) continue
|
|
280
|
+
columns.push(column)
|
|
281
|
+
bytes += payloadBytes(value)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (columns.length === 0) return
|
|
285
|
+
amplified.push({ entity, id: String(id), columns, bytes })
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const views: Views | undefined =
|
|
289
|
+
seams.views === undefined
|
|
290
|
+
? undefined
|
|
291
|
+
: {
|
|
292
|
+
async query<T = Record<string, unknown>>(
|
|
293
|
+
actor: Actor,
|
|
294
|
+
sql: string,
|
|
295
|
+
params: readonly unknown[],
|
|
296
|
+
): Promise<readonly T[]> {
|
|
297
|
+
spend('view')
|
|
298
|
+
const rows = await (seams.views as Views).query<T>(actor, sql, params)
|
|
299
|
+
readRows(rows)
|
|
300
|
+
return rows
|
|
301
|
+
},
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const baseSearch = seams.search
|
|
305
|
+
const search: Search | undefined =
|
|
306
|
+
baseSearch === undefined
|
|
307
|
+
? undefined
|
|
308
|
+
: {
|
|
309
|
+
index: (actor, entity, row) => baseSearch.index(actor, entity, row),
|
|
310
|
+
async query(actor, entity, q) {
|
|
311
|
+
spend(`search ${entity}`)
|
|
312
|
+
const rows = await baseSearch.query(actor, entity, q)
|
|
313
|
+
readRows(rows)
|
|
314
|
+
return rows
|
|
315
|
+
},
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const metered: MeterableSeams = {
|
|
319
|
+
...(store === undefined ? {} : { store }),
|
|
320
|
+
...(search === undefined ? {} : { search }),
|
|
321
|
+
...(views === undefined ? {} : { views }),
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return {
|
|
325
|
+
seams: { ...seams, ...metered } as S,
|
|
326
|
+
usage: () => ({ roundTrips, rowsRead, rowsWritten, bytesRead, bytesWritten, amplified }),
|
|
327
|
+
}
|
|
328
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Threaded } from '../core/thread.ts'
|
|
2
|
+
|
|
3
|
+
import type { Operation } from './operations.ts'
|
|
4
|
+
import type { Handed, Reads, Supplied } from './seams.ts'
|
|
5
|
+
|
|
6
|
+
export interface Node extends Threaded {
|
|
7
|
+
readonly version: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type Held<I = unknown> = Parameters<Operation<I>['run']>[1]
|
|
11
|
+
|
|
12
|
+
export const STOPPED = Symbol.for('thread.operation.stopped')
|
|
13
|
+
|
|
14
|
+
export function stopped(thrown: unknown): { readonly value: unknown } | undefined {
|
|
15
|
+
if (typeof thrown !== 'object' || thrown === null) return undefined
|
|
16
|
+
if ((thrown as Record<symbol, unknown>)[STOPPED] !== true) return undefined
|
|
17
|
+
return { value: (thrown as { readonly value?: unknown }).value }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const nd_read = <I, R extends string = Supplied>(
|
|
21
|
+
block: (hands: Reads<Handed<R>>, held: Held<I>) => Promise<unknown>,
|
|
22
|
+
) => ({ type: 'nd_read' as const })
|
|
23
|
+
|
|
24
|
+
export const nd_action = <I, R extends string = Supplied>(
|
|
25
|
+
block: (hands: Handed<R>, held: Held<I>) => Promise<unknown>,
|
|
26
|
+
) => ({ type: 'nd_action' as const })
|
|
27
|
+
|
|
28
|
+
export const nd_branch = <I, R extends string = Supplied>(
|
|
29
|
+
choose: (hands: Reads<Handed<R>>, held: Held<I>) => Promise<string>,
|
|
30
|
+
ways: Readonly<Record<string, (hands: Handed<R>, held: Held<I>) => Promise<unknown>>>,
|
|
31
|
+
) => ({ type: 'nd_branch' as const })
|
|
32
|
+
|
|
33
|
+
export const nd_each = <I, R extends string = Supplied>(
|
|
34
|
+
over: (held: Held<I>) => readonly unknown[],
|
|
35
|
+
block: (item: unknown, hands: Handed<R>, held: Held<I>) => Promise<unknown>,
|
|
36
|
+
) => ({ type: 'nd_each' as const })
|
|
37
|
+
|
|
38
|
+
export const nd_parallel = <I, R extends string = Supplied>(
|
|
39
|
+
blocks: readonly ((hands: Handed<R>, held: Held<I>) => Promise<unknown>)[],
|
|
40
|
+
) => ({ type: 'nd_parallel' as const })
|
|
41
|
+
|
|
42
|
+
export const nd_repeat = <I, R extends string = Supplied>(
|
|
43
|
+
ceiling: number,
|
|
44
|
+
block: (
|
|
45
|
+
pass: number,
|
|
46
|
+
carried: unknown,
|
|
47
|
+
hands: Handed<R>,
|
|
48
|
+
held: Held<I>,
|
|
49
|
+
) => Promise<{ readonly done: boolean; readonly value: unknown }>,
|
|
50
|
+
) => ({ type: 'nd_repeat' as const })
|
|
51
|
+
|
|
52
|
+
export const nd_stop = <I, R extends string = Supplied>(
|
|
53
|
+
block: (hands: Reads<Handed<R>>, held: Held<I>) => Promise<unknown>,
|
|
54
|
+
) => ({ type: 'nd_stop' as const })
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
import type { Member } from '../core/thread.ts'
|
|
4
|
+
|
|
5
|
+
import type { Handed, Row, Supplied } from './seams.ts'
|
|
6
|
+
|
|
7
|
+
export const OPERATIONS_FILE = 'operations.ts'
|
|
8
|
+
|
|
9
|
+
export const TRIALS_AT = 'operations/_trials.json'
|
|
10
|
+
|
|
11
|
+
export const NODES_FILE = 'nodes.ts'
|
|
12
|
+
|
|
13
|
+
export const MEMBERS_LIST = 'MEMBERS'
|
|
14
|
+
|
|
15
|
+
export interface Operation<I = unknown, R extends string = Supplied> {
|
|
16
|
+
readonly id: string
|
|
17
|
+
|
|
18
|
+
readonly input: z.ZodType<I>
|
|
19
|
+
|
|
20
|
+
readonly requires: readonly string[]
|
|
21
|
+
|
|
22
|
+
readonly run: (
|
|
23
|
+
hands: Handed<R>,
|
|
24
|
+
held: { readonly input: I } & Readonly<Record<string, unknown>>,
|
|
25
|
+
) => Promise<unknown>
|
|
26
|
+
|
|
27
|
+
readonly estimate: number
|
|
28
|
+
|
|
29
|
+
readonly resource: (input: I) => Row | null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const OPERATIONS: readonly Member[] = []
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const MAX_ATTEMPTS = 5
|
|
2
|
+
|
|
3
|
+
export function backoffMs(attempts: number): number {
|
|
4
|
+
const base = 1_000 * 2 ** Math.max(0, attempts - 1)
|
|
5
|
+
return Math.min(base, 5 * 60_000)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function isExhausted(attempts: number): boolean {
|
|
9
|
+
return attempts >= MAX_ATTEMPTS
|
|
10
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ErrorCode, ErrorDef } from './errors/catalog.ts'
|
|
2
|
+
import type { ErrorClass } from './errors/classes.ts'
|
|
3
|
+
|
|
4
|
+
export type ResultCode = 'denied' | 'invalid' | 'rate_limited' | 'feature_denied' | 'quota_exceeded' | 'error'
|
|
5
|
+
|
|
6
|
+
export type FailureIdentity = Pick<ErrorDef, 'code' | 'class'>
|
|
7
|
+
|
|
8
|
+
export type Result<O> =
|
|
9
|
+
| { ok: true; value: O }
|
|
10
|
+
| {
|
|
11
|
+
ok: false
|
|
12
|
+
code: ResultCode
|
|
13
|
+
error: string
|
|
14
|
+
because?: string
|
|
15
|
+
errorCode?: ErrorCode
|
|
16
|
+
errorClass?: ErrorClass
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const ok = <O>(value: O): Result<O> => ({ ok: true, value })
|
|
20
|
+
export const fail = <O>(code: ResultCode, error: string, because?: string, identity?: FailureIdentity): Result<O> => ({
|
|
21
|
+
ok: false,
|
|
22
|
+
code,
|
|
23
|
+
error,
|
|
24
|
+
because,
|
|
25
|
+
...(identity === undefined ? {} : { errorCode: identity.code, errorClass: identity.class }),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
export class ValidationError extends Error {}
|
|
@@ -179,10 +179,10 @@ export interface Vault {
|
|
|
179
179
|
resolve(refs: readonly string[]): Promise<Readonly<Record<string, string>>>;
|
|
180
180
|
}
|
|
181
181
|
export interface Seams {
|
|
182
|
-
store
|
|
183
|
-
search
|
|
184
|
-
events
|
|
185
|
-
queue
|
|
182
|
+
store?: Store;
|
|
183
|
+
search?: Search;
|
|
184
|
+
events?: EventBus;
|
|
185
|
+
queue?: Queue;
|
|
186
186
|
telemetry?: Telemetry;
|
|
187
187
|
log: Log;
|
|
188
188
|
audit: Audit;
|