@swirls/looms 0.25.0 → 0.26.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/CHANGELOG.md +18 -0
- package/dist/actor/cell.d.ts +3 -1
- package/dist/actor/cell.js +1 -0
- package/dist/actor/host.d.ts +3 -1
- package/dist/actor/host.js +2 -1
- package/dist/actor/routing.d.ts +6 -3
- package/dist/actor/routing.js +6 -4
- package/dist/agent/agent-session.js +2 -2
- package/dist/agent/debugger/agent-chat.js +4 -3
- package/dist/agent/definitions.d.ts +3 -1
- package/dist/agent/effects.js +5 -3
- package/dist/agent/module.d.ts +730 -730
- package/dist/approval/steps.d.ts +3 -1
- package/dist/approval/steps.js +2 -2
- package/dist/client/client.d.ts +7 -1
- package/dist/client/client.js +5 -2
- package/dist/cloudflare/durable-object.d.ts +3 -1
- package/dist/cloudflare/durable-object.js +1 -0
- package/dist/cloudflare/queue-consumer.d.ts +3 -1
- package/dist/cloudflare/queue-consumer.js +2 -1
- package/dist/cloudflare/router.d.ts +3 -1
- package/dist/cloudflare/router.js +1 -1
- package/dist/core/compose.d.ts +8 -2
- package/dist/core/compose.js +6 -3
- package/dist/core/effects.d.ts +3 -0
- package/dist/core/envelope.d.ts +2 -0
- package/dist/core/envelope.js +2 -2
- package/dist/core/event-codec.d.ts +4 -3
- package/dist/core/event-codec.js +7 -7
- package/dist/core/fold.d.ts +3 -0
- package/dist/core/fold.js +3 -1
- package/dist/core/fork.js +1 -1
- package/dist/core/ids.d.ts +70 -5
- package/dist/core/ids.js +164 -41
- package/dist/core/snapshots.d.ts +2 -0
- package/dist/core/snapshots.js +2 -1
- package/dist/core/thread.d.ts +8 -0
- package/dist/debugger/app/assets/{index-GLtrulmj.js → index-D_FKxPrN.js} +2 -2
- package/dist/debugger/app/assets/routes-CkwzFejg.js +47 -0
- package/dist/debugger/app/index.html +1 -1
- package/dist/debugger/context.d.ts +4 -1
- package/dist/debugger/context.js +4 -4
- package/dist/debugger/shell/debugger-shell.d.ts +4 -1
- package/dist/debugger/shell/debugger-shell.js +2 -2
- package/dist/debugger/shell/use-start-run.d.ts +1 -1
- package/dist/debugger/shell/use-start-run.js +1 -2
- package/dist/evaluator/effects.d.ts +33 -33
- package/dist/evaluator/module.d.ts +66 -66
- package/dist/evaluator/steps.d.ts +2 -2
- package/dist/evaluator/steps.js +3 -3
- package/dist/react/react.d.ts +8 -3
- package/dist/react/react.js +15 -2
- package/dist/react/store.d.ts +5 -1
- package/dist/react/store.js +5 -4
- package/dist/runtime/cancellation.js +5 -5
- package/dist/runtime/deadline-transition-planner.d.ts +1 -0
- package/dist/runtime/deadline-transition-planner.js +8 -7
- package/dist/runtime/effect-admission.js +1 -0
- package/dist/runtime/helpers.d.ts +6 -5
- package/dist/runtime/helpers.js +8 -8
- package/dist/runtime/local-effect-executor.js +15 -10
- package/dist/runtime/looms.d.ts +6 -1
- package/dist/runtime/looms.js +4 -1
- package/dist/runtime/retry-policy.d.ts +2 -1
- package/dist/runtime/retry-policy.js +2 -1
- package/dist/runtime/runtime.js +10 -10
- package/dist/runtime/server/routes/runs.js +5 -5
- package/dist/runtime/types.d.ts +7 -1
- package/dist/runtime/wake-engine.js +10 -9
- package/dist/runtime/worker-callbacks.js +9 -8
- package/dist/runtime/worker-effect-dispatch.d.ts +3 -2
- package/dist/runtime/worker-effect-dispatch.js +7 -5
- package/dist/schedule/module.d.ts +36 -36
- package/dist/swarm/steps.d.ts +3 -3
- package/dist/swarm/steps.js +5 -5
- package/dist/swarm/tools.d.ts +3 -2
- package/dist/swarm/tools.js +13 -13
- package/dist/workflow/effects.js +6 -5
- package/dist/workflow/graph.d.ts +3 -3
- package/dist/workflow/graph.js +5 -5
- package/dist/workflow/module.d.ts +8 -2
- package/dist/workflow/threads.d.ts +14 -2
- package/dist/workflow/threads.js +54 -44
- package/package.json +1 -1
- package/dist/debugger/app/assets/routes-CECZJJob.js +0 -47
package/dist/core/ids.js
CHANGED
|
@@ -1,63 +1,186 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
counter += 1;
|
|
5
|
-
const timestamp = DateTime.toEpochMillis(DateTime.nowUnsafe());
|
|
6
|
-
// Synchronous ID factories need process-independent entropy without an Effect runtime.
|
|
7
|
-
// oxlint-disable-next-line effecttsgo/crypto-random-uuid
|
|
8
|
-
const entropy = globalThis.crypto.randomUUID().replaceAll('-', '');
|
|
9
|
-
return `${timestamp.toString(36)}_${counter.toString(36)}_${entropy}`;
|
|
1
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
2
|
+
import { Data, DateTime, Predicate } from 'effect';
|
|
3
|
+
export class InvalidIdFormatError extends Data.TaggedError('InvalidIdFormatError') {
|
|
10
4
|
}
|
|
5
|
+
const TYPEID_PREFIX = {
|
|
6
|
+
event: 'evt',
|
|
7
|
+
run: 'run',
|
|
8
|
+
thread: 'thr',
|
|
9
|
+
wait: 'wait',
|
|
10
|
+
};
|
|
11
|
+
const CROCKFORD = '0123456789abcdefghjkmnpqrstvwxyz';
|
|
12
|
+
/** The 26-character TypeID suffix for a UUID's 16 bytes: 128 bits, left-padded to 130. */
|
|
13
|
+
export function encodeTypeIdSuffix(uuid) {
|
|
14
|
+
if (uuid.length !== 16) {
|
|
15
|
+
throw new RangeError(`A TypeID suffix encodes 16 bytes, got ${uuid.length}`);
|
|
16
|
+
}
|
|
17
|
+
let value = 0n;
|
|
18
|
+
for (const byte of uuid) {
|
|
19
|
+
value = (value << 8n) | BigInt(byte);
|
|
20
|
+
}
|
|
21
|
+
let suffix = '';
|
|
22
|
+
for (let index = 0; index < 26; index++) {
|
|
23
|
+
suffix = CROCKFORD.charAt(Number(value & 31n)) + suffix;
|
|
24
|
+
value >>= 5n;
|
|
25
|
+
}
|
|
26
|
+
return suffix;
|
|
27
|
+
}
|
|
28
|
+
/** `<prefix>_<suffix>` for a UUID's 16 bytes, per TypeID 0.3. */
|
|
29
|
+
export function typeId(prefix, uuid) {
|
|
30
|
+
return `${prefix}_${encodeTypeIdSuffix(uuid)}`;
|
|
31
|
+
}
|
|
32
|
+
const RANDOM_BYTES = 10;
|
|
33
|
+
let lastTimestamp = -1;
|
|
34
|
+
const lastRandom = new Uint8Array(RANDOM_BYTES);
|
|
35
|
+
/** How many bits of each `lastRandom` byte the UUID carries: 4 + 8 for rand_a, 6 + 56 for rand_b. */
|
|
36
|
+
const RANDOM_MASKS = [0x0f, 0xff, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
|
|
37
|
+
function drawRandom() {
|
|
38
|
+
globalThis.crypto.getRandomValues(lastRandom);
|
|
39
|
+
lastRandom[2] = lastRandom[2] & 0x3f;
|
|
40
|
+
// Clear the top bit so a burst within one millisecond has room to count up.
|
|
41
|
+
lastRandom[0] = lastRandom[0] & 0x07;
|
|
42
|
+
}
|
|
43
|
+
/** Add one to the 74 random bits. Returns false when they wrap. */
|
|
44
|
+
function incrementRandom() {
|
|
45
|
+
for (let index = RANDOM_BYTES - 1; index >= 0; index--) {
|
|
46
|
+
lastRandom[index] = (lastRandom[index] + 1) & RANDOM_MASKS[index];
|
|
47
|
+
if (lastRandom[index] !== 0) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* UUIDv7 bytes (RFC 9562): a 48-bit millisecond timestamp, then random bits. Ids drawn in the same
|
|
55
|
+
* millisecond, or after the clock steps back, increment the previous random bits (section 6.2,
|
|
56
|
+
* method 2), so every id this process draws sorts after the one before it.
|
|
57
|
+
*/
|
|
58
|
+
export function uuidV7() {
|
|
59
|
+
const now = DateTime.toEpochMillis(DateTime.nowUnsafe());
|
|
60
|
+
if (now > lastTimestamp) {
|
|
61
|
+
lastTimestamp = now;
|
|
62
|
+
drawRandom();
|
|
63
|
+
}
|
|
64
|
+
else if (!incrementRandom()) {
|
|
65
|
+
lastTimestamp += 1;
|
|
66
|
+
drawRandom();
|
|
67
|
+
}
|
|
68
|
+
const bytes = new Uint8Array(16);
|
|
69
|
+
let timestamp = lastTimestamp;
|
|
70
|
+
for (let index = 5; index >= 0; index--) {
|
|
71
|
+
bytes[index] = timestamp % 256;
|
|
72
|
+
timestamp = Math.floor(timestamp / 256);
|
|
73
|
+
}
|
|
74
|
+
bytes[6] = 0x70 | lastRandom[0];
|
|
75
|
+
bytes[7] = lastRandom[1];
|
|
76
|
+
bytes[8] = 0x80 | lastRandom[2];
|
|
77
|
+
bytes.set(lastRandom.subarray(3), 9);
|
|
78
|
+
return bytes;
|
|
79
|
+
}
|
|
80
|
+
const encoder = new TextEncoder();
|
|
81
|
+
/**
|
|
82
|
+
* UUIDv8 bytes whose 122 free bits are the leading bits of SHA-256 over `kind` and `parts`. Parts
|
|
83
|
+
* are hashed as JSON, so `['a:b', 'c']` and `['a', 'b:c']` differ, and so do `1` and `'1'`.
|
|
84
|
+
*/
|
|
85
|
+
function derivedUuid(kind, parts) {
|
|
86
|
+
const bytes = sha256(encoder.encode(`${kind}\n${JSON.stringify(parts)}`)).slice(0, 16);
|
|
87
|
+
bytes[6] = 0x80 | (bytes[6] & 0x0f);
|
|
88
|
+
bytes[8] = 0x80 | (bytes[8] & 0x3f);
|
|
89
|
+
return bytes;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The default id format: [TypeID](https://github.com/jetify-com/typeid) 0.3. Ids are `evt_`,
|
|
93
|
+
* `run_`, `thr_`, or `wait_` followed by 26 characters. Drawn ids carry a UUIDv7 and sort by
|
|
94
|
+
* creation time within a process; derived ids carry a UUIDv8 hashed from their parts.
|
|
95
|
+
*/
|
|
96
|
+
export const typeIds = {
|
|
97
|
+
create: (kind) => typeId(TYPEID_PREFIX[kind], uuidV7()),
|
|
98
|
+
derive: (kind, parts) => typeId(TYPEID_PREFIX[kind], derivedUuid(kind, parts)),
|
|
99
|
+
};
|
|
100
|
+
function checkId(id, source) {
|
|
101
|
+
if (!Predicate.isString(id) || id.length === 0) {
|
|
102
|
+
throw new InvalidIdFormatError({
|
|
103
|
+
rule: 'non-empty',
|
|
104
|
+
id,
|
|
105
|
+
message: `Invalid id format: ${source} must return a non-empty string, got ${JSON.stringify(id)}`,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (id.includes(':')) {
|
|
109
|
+
throw new InvalidIdFormatError({
|
|
110
|
+
rule: 'no-colon',
|
|
111
|
+
id,
|
|
112
|
+
message: `Invalid id format: ${source} returned ${JSON.stringify(id)}, which contains ":". Ids must not contain ":" because effect ids are "<threadId>:<tag>"`,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return id;
|
|
116
|
+
}
|
|
117
|
+
const checked = new WeakSet([typeIds]);
|
|
118
|
+
const DRAWN_KINDS = ['event', 'run', 'thread', 'wait'];
|
|
119
|
+
const DERIVED_KINDS = ['run', 'thread'];
|
|
120
|
+
/**
|
|
121
|
+
* The format an `ids` option names, or {@link typeIds} without one. A custom format is tried once
|
|
122
|
+
* for every kind here, and every id it returns later is checked against the same rules; a
|
|
123
|
+
* violation throws {@link InvalidIdFormatError}.
|
|
124
|
+
*/
|
|
125
|
+
export function resolveIds(ids) {
|
|
126
|
+
if (ids === undefined) {
|
|
127
|
+
return typeIds;
|
|
128
|
+
}
|
|
129
|
+
if (checked.has(ids)) {
|
|
130
|
+
return ids;
|
|
131
|
+
}
|
|
132
|
+
const resolved = {
|
|
133
|
+
create: (kind) => checkId(ids.create(kind), `ids.create('${kind}')`),
|
|
134
|
+
derive: (kind, parts) => checkId(ids.derive(kind, parts), `ids.derive('${kind}', parts)`),
|
|
135
|
+
};
|
|
136
|
+
for (const kind of DRAWN_KINDS) {
|
|
137
|
+
resolved.create(kind);
|
|
138
|
+
}
|
|
139
|
+
for (const kind of DERIVED_KINDS) {
|
|
140
|
+
resolved.derive(kind, ['looms', 0]);
|
|
141
|
+
}
|
|
142
|
+
checked.add(resolved);
|
|
143
|
+
return resolved;
|
|
144
|
+
}
|
|
145
|
+
/** A fresh event id in the default format. A configured format is `ids.create('event')`. */
|
|
11
146
|
export function createEventId() {
|
|
12
|
-
return
|
|
147
|
+
return typeIds.create('event');
|
|
13
148
|
}
|
|
149
|
+
/** A fresh run id in the default format. A configured format is `ids.create('run')`. */
|
|
14
150
|
export function createRunId() {
|
|
15
|
-
return
|
|
151
|
+
return typeIds.create('run');
|
|
16
152
|
}
|
|
153
|
+
/** A fresh thread id in the default format. A configured format is `ids.create('thread')`. */
|
|
17
154
|
export function createThreadId() {
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
function fnv1a32(value, seed) {
|
|
21
|
-
let hash = seed >>> 0;
|
|
22
|
-
for (let index = 0; index < value.length; index++) {
|
|
23
|
-
hash ^= value.charCodeAt(index);
|
|
24
|
-
hash = Math.imul(hash, 0x01000193);
|
|
25
|
-
}
|
|
26
|
-
return (hash >>> 0).toString(16).padStart(8, '0');
|
|
155
|
+
return typeIds.create('thread');
|
|
27
156
|
}
|
|
28
|
-
/**
|
|
157
|
+
/**
|
|
158
|
+
* A stable run id in the default format, so a retry launches the same child run. A configured
|
|
159
|
+
* format is `ctx.ids.derive('run', parts)`.
|
|
160
|
+
*/
|
|
29
161
|
export function deterministicRunId(...parts) {
|
|
30
|
-
return
|
|
162
|
+
return typeIds.derive('run', parts);
|
|
31
163
|
}
|
|
32
164
|
/**
|
|
33
|
-
* A
|
|
34
|
-
* child thread every time.
|
|
165
|
+
* A thread id in the default format derived only from `parts`, so a reducer that replays the same
|
|
166
|
+
* events names the same child thread every time. A configured format is
|
|
167
|
+
* `ctx.ids.derive('thread', parts)`.
|
|
35
168
|
*/
|
|
36
169
|
export function deterministicThreadId(...parts) {
|
|
37
|
-
|
|
38
|
-
let reversed = '';
|
|
39
|
-
for (let index = source.length - 1; index >= 0; index--) {
|
|
40
|
-
reversed += source.charAt(index);
|
|
41
|
-
}
|
|
42
|
-
const hex = [
|
|
43
|
-
fnv1a32(source, 0x811c9dc5),
|
|
44
|
-
fnv1a32(source, 0x9e3779b9),
|
|
45
|
-
fnv1a32(reversed, 0x85ebca6b),
|
|
46
|
-
fnv1a32(reversed, 0xc2b2ae35),
|
|
47
|
-
].join('');
|
|
48
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-5${hex.slice(13, 16)}-${((Number.parseInt(hex.slice(16, 18), 16) & 0x3f) |
|
|
49
|
-
0x80)
|
|
50
|
-
.toString(16)
|
|
51
|
-
.padStart(2, '0')}${hex.slice(18, 20)}-${hex.slice(20)}`;
|
|
170
|
+
return typeIds.derive('thread', parts);
|
|
52
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* A wait id. With `scope` and `tag` it is the composed key `wait_<scope>_<tag>` and draws nothing.
|
|
174
|
+
* Without a tag it draws from the default format; a configured format is `ids.create('wait')`.
|
|
175
|
+
*/
|
|
53
176
|
export function createWaitId(scope, tag) {
|
|
54
177
|
if (scope !== undefined && tag !== undefined) {
|
|
55
178
|
return `wait_${scope}_${tag}`;
|
|
56
179
|
}
|
|
57
180
|
if (scope !== undefined) {
|
|
58
|
-
return `wait_${scope}_${
|
|
181
|
+
return `wait_${scope}_${encodeTypeIdSuffix(uuidV7())}`;
|
|
59
182
|
}
|
|
60
|
-
return
|
|
183
|
+
return typeIds.create('wait');
|
|
61
184
|
}
|
|
62
185
|
export function createEffectId(threadId, causingSeqOrTag, index) {
|
|
63
186
|
if (Predicate.isString(causingSeqOrTag)) {
|
package/dist/core/snapshots.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Schema } from 'effect';
|
|
2
2
|
import { type EventEnvelope } from './envelope';
|
|
3
3
|
import { type FoldRegistry } from './fold';
|
|
4
|
+
import type { LoomsIds } from './ids';
|
|
4
5
|
import { type RunState } from './state';
|
|
5
6
|
import { type JsonValue } from './types';
|
|
6
7
|
export declare const DEFAULT_SNAPSHOT_EVERY = 200;
|
|
@@ -23,6 +24,7 @@ export declare function buildSnapshotEvent(runId: string, events: readonly Event
|
|
|
23
24
|
export declare function buildSnapshotMarker(runId: string, cursor: number, stateHash: string, options?: {
|
|
24
25
|
state?: RunState;
|
|
25
26
|
maxInlineBytes?: number;
|
|
27
|
+
ids?: LoomsIds;
|
|
26
28
|
}): EventEnvelope;
|
|
27
29
|
export declare function foldFromSnapshots(events: readonly EventEnvelope[], registry: FoldRegistry, options?: {
|
|
28
30
|
runId?: string;
|
package/dist/core/snapshots.js
CHANGED
|
@@ -34,6 +34,7 @@ export function buildSnapshotEvent(runId, events, registry, options) {
|
|
|
34
34
|
const last = events[events.length - 1];
|
|
35
35
|
return buildSnapshotMarker(runId, last?.seq ?? 0, hashRunState(state), {
|
|
36
36
|
state: options?.includeState ? state : undefined,
|
|
37
|
+
ids: registry.ids,
|
|
37
38
|
});
|
|
38
39
|
}
|
|
39
40
|
/**
|
|
@@ -55,7 +56,7 @@ export function buildSnapshotMarker(runId, cursor, stateHash, options) {
|
|
|
55
56
|
payload,
|
|
56
57
|
threadId: null,
|
|
57
58
|
origin: { type: 'system' },
|
|
58
|
-
});
|
|
59
|
+
}, { ids: options?.ids });
|
|
59
60
|
}
|
|
60
61
|
export function foldFromSnapshots(events, registry, options) {
|
|
61
62
|
const runId = options?.runId ?? events[0]?.runId ?? 'unknown';
|
package/dist/core/thread.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RuntimeEffect } from './effects';
|
|
2
2
|
import type { EventEnvelope } from './envelope';
|
|
3
|
+
import type { LoomsIds } from './ids';
|
|
3
4
|
import type { InferDefinedSchema, InferSchemaOutput, SchemaInput } from './schema';
|
|
4
5
|
import type { ThreadStatus, JsonValue } from './types';
|
|
5
6
|
export interface StartContext<TInput = JsonValue> {
|
|
@@ -9,11 +10,18 @@ export interface StartContext<TInput = JsonValue> {
|
|
|
9
10
|
readonly definitionName: string;
|
|
10
11
|
readonly definitionVersion: string;
|
|
11
12
|
readonly input: TInput;
|
|
13
|
+
/** The id format this registry was composed with. */
|
|
14
|
+
readonly ids: LoomsIds;
|
|
12
15
|
}
|
|
13
16
|
export interface ThreadContext {
|
|
14
17
|
readonly runId: string;
|
|
15
18
|
readonly threadId: string;
|
|
16
19
|
readonly parentThreadId: string | null;
|
|
20
|
+
/**
|
|
21
|
+
* The id format this registry was composed with. Name a child with `ids.derive`, never
|
|
22
|
+
* `ids.create`: a reducer runs again on every replay.
|
|
23
|
+
*/
|
|
24
|
+
readonly ids: LoomsIds;
|
|
17
25
|
}
|
|
18
26
|
export interface ThreadDefinition<S = any> {
|
|
19
27
|
readonly kind: string;
|