@effect-agent/storage-memory 0.1.0-beta.35 → 0.1.0-beta.37
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/index.d.mts +6 -2
- package/dist/index.mjs +165 -7
- package/dist/index.mjs.map +1 -1
- package/dist/testing.d.mts +2 -2
- package/dist/testing.mjs +2 -2
- package/package.json +3 -3
- package/src/index.ts +1 -0
- package/src/memory-ledger.ts +7 -5
- package/src/memory-schedule-store.ts +284 -0
- package/src/testing.ts +6 -0
package/dist/testing.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ConversationStoreConformanceCase, ConversationStoreConformanceFailure, ConversationStoreConformanceViolation, SubmissionLedgerConformanceCase, SubmissionLedgerConformanceFailure, SubmissionLedgerConformanceViolation, conversationStoreConformanceCases, submissionLedgerConformanceCases } from "@effect-agent/session/testing";
|
|
2
|
-
export { type ConversationStoreConformanceCase, type ConversationStoreConformanceFailure, ConversationStoreConformanceViolation, type SubmissionLedgerConformanceCase, type SubmissionLedgerConformanceFailure, SubmissionLedgerConformanceViolation, conversationStoreConformanceCases, submissionLedgerConformanceCases };
|
|
1
|
+
import { ConversationStoreConformanceCase, ConversationStoreConformanceFailure, ConversationStoreConformanceViolation, ScheduleStoreConformanceCase, ScheduleStoreConformanceFailure, ScheduleStoreConformanceViolation, SubmissionLedgerConformanceCase, SubmissionLedgerConformanceFailure, SubmissionLedgerConformanceViolation, conversationStoreConformanceCases, scheduleStoreConformanceCases, submissionLedgerConformanceCases } from "@effect-agent/session/testing";
|
|
2
|
+
export { type ConversationStoreConformanceCase, type ConversationStoreConformanceFailure, ConversationStoreConformanceViolation, type ScheduleStoreConformanceCase, type ScheduleStoreConformanceFailure, ScheduleStoreConformanceViolation, type SubmissionLedgerConformanceCase, type SubmissionLedgerConformanceFailure, SubmissionLedgerConformanceViolation, conversationStoreConformanceCases, scheduleStoreConformanceCases, submissionLedgerConformanceCases };
|
package/dist/testing.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ConversationStoreConformanceViolation, SubmissionLedgerConformanceViolation, conversationStoreConformanceCases, submissionLedgerConformanceCases } from "@effect-agent/session/testing";
|
|
2
|
-
export { ConversationStoreConformanceViolation, SubmissionLedgerConformanceViolation, conversationStoreConformanceCases, submissionLedgerConformanceCases };
|
|
1
|
+
import { ConversationStoreConformanceViolation, ScheduleStoreConformanceViolation, SubmissionLedgerConformanceViolation, conversationStoreConformanceCases, scheduleStoreConformanceCases, submissionLedgerConformanceCases } from "@effect-agent/session/testing";
|
|
2
|
+
export { ConversationStoreConformanceViolation, ScheduleStoreConformanceViolation, SubmissionLedgerConformanceViolation, conversationStoreConformanceCases, scheduleStoreConformanceCases, submissionLedgerConformanceCases };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect-agent/storage-memory",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.37",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": {
|
|
6
6
|
"types": "./dist/index.d.mts",
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@effect-agent/core": "0.1.0-beta.
|
|
16
|
-
"@effect-agent/session": "0.1.0-beta.
|
|
15
|
+
"@effect-agent/core": "0.1.0-beta.37",
|
|
16
|
+
"@effect-agent/session": "0.1.0-beta.37",
|
|
17
17
|
"effect": "4.0.0-rc.111"
|
|
18
18
|
},
|
|
19
19
|
"description": "In-memory Conversation store and submission ledger reference adapters for Effect Agent tests and conformance suites.",
|
package/src/index.ts
CHANGED
package/src/memory-ledger.ts
CHANGED
|
@@ -175,13 +175,13 @@ interface StoredSubmission {
|
|
|
175
175
|
|
|
176
176
|
/**
|
|
177
177
|
* States in which `claim` never grants the head: the lane is host-owned (`joining`/`joined`)
|
|
178
|
-
* or durably
|
|
178
|
+
* or durably suspended rather than worker-claimable. Unknown heads are checked against abort
|
|
179
|
+
* intent separately: abort authorizes cleanup and settlement, never ordinary Tool replay.
|
|
179
180
|
*/
|
|
180
181
|
const BLOCKED_HEAD_STATES: ReadonlySet<SubmissionState> = new Set([
|
|
181
182
|
"joining",
|
|
182
183
|
"joined",
|
|
183
184
|
"suspended",
|
|
184
|
-
"unknown",
|
|
185
185
|
]);
|
|
186
186
|
|
|
187
187
|
interface LaneState {
|
|
@@ -597,9 +597,11 @@ const makeSubmissionLedger = (options: MemorySubmissionLedgerOptions = {}) =>
|
|
|
597
597
|
(current): readonly [Decision<Option.Option<Claim>, LedgerError>, LedgerState] => {
|
|
598
598
|
const head = findHead(current, request.conversationId);
|
|
599
599
|
if (head === undefined) return [success(Option.none()), current];
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
600
|
+
if (
|
|
601
|
+
BLOCKED_HEAD_STATES.has(head.row.state) ||
|
|
602
|
+
(head.row.state === "unknown" && head.abortIntent === undefined)
|
|
603
|
+
)
|
|
604
|
+
return [success(Option.none()), current];
|
|
603
605
|
if (
|
|
604
606
|
head.ownership !== undefined &&
|
|
605
607
|
head.ownership.leaseExpiresAtMillis > nowMillis &&
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ScheduleCapacityError,
|
|
3
|
+
ScheduleDueCursor,
|
|
4
|
+
defaultSchedulingLimits,
|
|
5
|
+
scheduleUsesCapacity,
|
|
6
|
+
ScheduleChange,
|
|
7
|
+
ScheduleConflict,
|
|
8
|
+
ScheduleFailpoint,
|
|
9
|
+
ScheduleKey,
|
|
10
|
+
ScheduleNotFound,
|
|
11
|
+
ScheduleOwner,
|
|
12
|
+
SchedulePageRequest,
|
|
13
|
+
ScheduleRecord,
|
|
14
|
+
ScheduleStorageError,
|
|
15
|
+
ScheduleStore,
|
|
16
|
+
applyScheduleChange,
|
|
17
|
+
compareScheduleKeys,
|
|
18
|
+
compareScheduleNames,
|
|
19
|
+
scheduleDeadline,
|
|
20
|
+
scheduleKeyString,
|
|
21
|
+
scheduleKeyOf,
|
|
22
|
+
scheduleOwnerKey,
|
|
23
|
+
} from "@effect-agent/session";
|
|
24
|
+
import { Effect, Layer, Ref, Result, Schema } from "effect";
|
|
25
|
+
|
|
26
|
+
interface MemoryScheduleState {
|
|
27
|
+
readonly records: ReadonlyMap<string, string>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const storageError = (operation: string, reason: "unavailable" | "corrupt") =>
|
|
31
|
+
ScheduleStorageError.make({ operation, reason });
|
|
32
|
+
|
|
33
|
+
const encodeRecord = (operation: string, record: ScheduleRecord) =>
|
|
34
|
+
Effect.try({
|
|
35
|
+
try: () => Schema.encodeSync(Schema.fromJsonString(ScheduleRecord))(record),
|
|
36
|
+
catch: () => storageError(operation, "corrupt"),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const decodeRecord = (operation: string, encoded: string) =>
|
|
40
|
+
Effect.try({
|
|
41
|
+
try: () => Schema.decodeSync(Schema.fromJsonString(ScheduleRecord))(encoded),
|
|
42
|
+
catch: () => storageError(operation, "corrupt"),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const decodeInput = <A, I>(operation: string, schema: Schema.Codec<A, I>, value: unknown) =>
|
|
46
|
+
Effect.try({
|
|
47
|
+
try: () => Schema.decodeUnknownSync(schema)(value),
|
|
48
|
+
catch: () => storageError(operation, "corrupt"),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const sameOwner = (record: ScheduleRecord, owner: ScheduleOwner): boolean =>
|
|
52
|
+
scheduleOwnerKey(record.owner) === scheduleOwnerKey(owner);
|
|
53
|
+
|
|
54
|
+
const makeScheduleStore = Effect.gen(function* () {
|
|
55
|
+
const state = yield* Ref.make<MemoryScheduleState>({ records: new Map() });
|
|
56
|
+
const failpoint = yield* ScheduleFailpoint;
|
|
57
|
+
|
|
58
|
+
const insert: ScheduleStore["Service"]["insert"] = Effect.fn("MemoryScheduleStore.insert")(
|
|
59
|
+
(record, ownerLimit) =>
|
|
60
|
+
Effect.gen(function* () {
|
|
61
|
+
const encoded = yield* encodeRecord("insert", record);
|
|
62
|
+
yield* failpoint.hit("schedule:insert:before");
|
|
63
|
+
const decision = yield* Effect.uninterruptible(
|
|
64
|
+
Ref.modify(
|
|
65
|
+
state,
|
|
66
|
+
(
|
|
67
|
+
current,
|
|
68
|
+
): readonly [
|
|
69
|
+
Result.Result<
|
|
70
|
+
ScheduleRecord,
|
|
71
|
+
ScheduleConflict | ScheduleCapacityError | ScheduleStorageError
|
|
72
|
+
>,
|
|
73
|
+
MemoryScheduleState,
|
|
74
|
+
] => {
|
|
75
|
+
const key = scheduleKeyString(record);
|
|
76
|
+
const existingText = current.records.get(key);
|
|
77
|
+
if (existingText !== undefined) {
|
|
78
|
+
const decoded = Result.try({
|
|
79
|
+
try: () => Schema.decodeSync(Schema.fromJsonString(ScheduleRecord))(existingText),
|
|
80
|
+
catch: () => storageError("insert", "corrupt"),
|
|
81
|
+
});
|
|
82
|
+
if (Result.isFailure(decoded)) {
|
|
83
|
+
return [Result.fail(decoded.failure), current];
|
|
84
|
+
}
|
|
85
|
+
if (decoded.success.creationFingerprint === record.creationFingerprint) {
|
|
86
|
+
return [Result.succeed(decoded.success), current];
|
|
87
|
+
}
|
|
88
|
+
return [
|
|
89
|
+
Result.fail(
|
|
90
|
+
ScheduleConflict.make({ reason: "creation", key: scheduleKeyOf(record) }),
|
|
91
|
+
),
|
|
92
|
+
current,
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let ownerCount = 0;
|
|
97
|
+
for (const text of current.records.values()) {
|
|
98
|
+
const decoded = Result.try({
|
|
99
|
+
try: () => Schema.decodeSync(Schema.fromJsonString(ScheduleRecord))(text),
|
|
100
|
+
catch: () => storageError("insert", "corrupt"),
|
|
101
|
+
});
|
|
102
|
+
if (Result.isFailure(decoded)) {
|
|
103
|
+
return [Result.fail(decoded.failure), current];
|
|
104
|
+
}
|
|
105
|
+
if (
|
|
106
|
+
sameOwner(decoded.success, record.owner) &&
|
|
107
|
+
scheduleUsesCapacity(decoded.success)
|
|
108
|
+
)
|
|
109
|
+
ownerCount += 1;
|
|
110
|
+
}
|
|
111
|
+
if (ownerCount >= ownerLimit) {
|
|
112
|
+
return [Result.fail(ScheduleCapacityError.make({ limit: ownerLimit })), current];
|
|
113
|
+
}
|
|
114
|
+
const records = new Map(current.records);
|
|
115
|
+
records.set(key, encoded);
|
|
116
|
+
const next = { records };
|
|
117
|
+
return [Result.succeed(record), next];
|
|
118
|
+
},
|
|
119
|
+
),
|
|
120
|
+
);
|
|
121
|
+
const inserted = yield* Effect.fromResult(decision);
|
|
122
|
+
yield* failpoint.hit("schedule:insert:after");
|
|
123
|
+
return yield* decodeRecord("insert", yield* encodeRecord("insert", inserted));
|
|
124
|
+
}),
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const get: ScheduleStore["Service"]["get"] = Effect.fn("MemoryScheduleStore.get")(
|
|
128
|
+
function* (key) {
|
|
129
|
+
const decodedKey = yield* decodeInput("get", ScheduleKey, key);
|
|
130
|
+
const text = (yield* Ref.get(state)).records.get(scheduleKeyString(decodedKey));
|
|
131
|
+
return text === undefined ? null : yield* decodeRecord("get", text);
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const list: ScheduleStore["Service"]["list"] = Effect.fn("MemoryScheduleStore.list")(
|
|
136
|
+
function* (request) {
|
|
137
|
+
const decodedRequest = yield* decodeInput("list", SchedulePageRequest, request);
|
|
138
|
+
const records: Array<ScheduleRecord> = [];
|
|
139
|
+
for (const text of (yield* Ref.get(state)).records.values()) {
|
|
140
|
+
const record = yield* decodeRecord("list", text);
|
|
141
|
+
if (
|
|
142
|
+
sameOwner(record, decodedRequest.owner) &&
|
|
143
|
+
(decodedRequest.after === undefined ||
|
|
144
|
+
compareScheduleNames(record.scheduleId, decodedRequest.after) > 0)
|
|
145
|
+
) {
|
|
146
|
+
records.push(record);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
records.sort((left, right) => compareScheduleNames(left.scheduleId, right.scheduleId));
|
|
150
|
+
const hasNext = records.length > decodedRequest.limit;
|
|
151
|
+
const items = records.slice(0, decodedRequest.limit);
|
|
152
|
+
return {
|
|
153
|
+
items,
|
|
154
|
+
next: hasNext ? (items.at(-1)?.scheduleId ?? null) : null,
|
|
155
|
+
};
|
|
156
|
+
},
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
const change: ScheduleStore["Service"]["change"] = Effect.fn("MemoryScheduleStore.change")(
|
|
160
|
+
(key, command, ownerLimit = defaultSchedulingLimits.maxSchedulesPerOwner) =>
|
|
161
|
+
Effect.gen(function* () {
|
|
162
|
+
const decodedKey = yield* decodeInput("change", ScheduleKey, key);
|
|
163
|
+
const decodedCommand = yield* decodeInput("change", ScheduleChange, command);
|
|
164
|
+
yield* failpoint.hit(`schedule:${decodedCommand._tag.toLowerCase()}:before`);
|
|
165
|
+
const decision = yield* Effect.uninterruptible(
|
|
166
|
+
Ref.modify(
|
|
167
|
+
state,
|
|
168
|
+
(
|
|
169
|
+
current,
|
|
170
|
+
): readonly [
|
|
171
|
+
Result.Result<
|
|
172
|
+
ScheduleRecord,
|
|
173
|
+
ScheduleConflict | ScheduleStorageError | ScheduleNotFound | ScheduleCapacityError
|
|
174
|
+
>,
|
|
175
|
+
MemoryScheduleState,
|
|
176
|
+
] => {
|
|
177
|
+
const storageKey = scheduleKeyString(decodedKey);
|
|
178
|
+
const text = current.records.get(storageKey);
|
|
179
|
+
if (text === undefined) {
|
|
180
|
+
return [Result.fail(ScheduleNotFound.make({ key: decodedKey })), current];
|
|
181
|
+
}
|
|
182
|
+
const decoded = Result.try({
|
|
183
|
+
try: () => Schema.decodeSync(Schema.fromJsonString(ScheduleRecord))(text),
|
|
184
|
+
catch: () => storageError("change", "corrupt"),
|
|
185
|
+
});
|
|
186
|
+
if (Result.isFailure(decoded)) {
|
|
187
|
+
return [Result.fail(decoded.failure), current];
|
|
188
|
+
}
|
|
189
|
+
const applied = applyScheduleChange(decoded.success, decodedCommand);
|
|
190
|
+
if (Result.isFailure(applied)) {
|
|
191
|
+
return [Result.fail(applied.failure), current];
|
|
192
|
+
}
|
|
193
|
+
if (applied.success === decoded.success) {
|
|
194
|
+
return [Result.succeed(decoded.success), current];
|
|
195
|
+
}
|
|
196
|
+
if (!scheduleUsesCapacity(decoded.success) && scheduleUsesCapacity(applied.success)) {
|
|
197
|
+
let count = 0;
|
|
198
|
+
for (const text of current.records.values()) {
|
|
199
|
+
const candidate = Schema.decodeUnknownResult(
|
|
200
|
+
Schema.fromJsonString(ScheduleRecord),
|
|
201
|
+
)(text);
|
|
202
|
+
if (Result.isFailure(candidate))
|
|
203
|
+
return [Result.fail(storageError("change", "corrupt")), current];
|
|
204
|
+
if (
|
|
205
|
+
sameOwner(candidate.success, decodedKey.owner) &&
|
|
206
|
+
scheduleUsesCapacity(candidate.success)
|
|
207
|
+
)
|
|
208
|
+
count += 1;
|
|
209
|
+
}
|
|
210
|
+
if (count >= ownerLimit)
|
|
211
|
+
return [Result.fail(ScheduleCapacityError.make({ limit: ownerLimit })), current];
|
|
212
|
+
}
|
|
213
|
+
const encoded = Result.try({
|
|
214
|
+
try: () =>
|
|
215
|
+
Schema.encodeSync(Schema.fromJsonString(ScheduleRecord))(applied.success),
|
|
216
|
+
catch: () => storageError("change", "corrupt"),
|
|
217
|
+
});
|
|
218
|
+
if (Result.isFailure(encoded)) {
|
|
219
|
+
return [Result.fail(encoded.failure), current];
|
|
220
|
+
}
|
|
221
|
+
const records = new Map(current.records);
|
|
222
|
+
records.set(storageKey, encoded.success);
|
|
223
|
+
const next = { records };
|
|
224
|
+
return [Result.succeed(applied.success), next];
|
|
225
|
+
},
|
|
226
|
+
),
|
|
227
|
+
);
|
|
228
|
+
const changed = yield* Effect.fromResult(decision);
|
|
229
|
+
yield* failpoint.hit(`schedule:${decodedCommand._tag.toLowerCase()}:after`);
|
|
230
|
+
return yield* decodeRecord("change", yield* encodeRecord("change", changed));
|
|
231
|
+
}),
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
const due: ScheduleStore["Service"]["due"] = Effect.fn("MemoryScheduleStore.due")(
|
|
235
|
+
function* (nowMillis, limit, owner, after) {
|
|
236
|
+
const decodedOwner =
|
|
237
|
+
owner === undefined ? undefined : yield* decodeInput("due", ScheduleOwner, owner);
|
|
238
|
+
const cursor =
|
|
239
|
+
after === undefined ? undefined : yield* decodeInput("due", ScheduleDueCursor, after);
|
|
240
|
+
const records: Array<ScheduleDueCursor> = [];
|
|
241
|
+
for (const text of (yield* Ref.get(state)).records.values()) {
|
|
242
|
+
const record = yield* decodeRecord("due", text);
|
|
243
|
+
const deadline = scheduleDeadline(record);
|
|
244
|
+
if (
|
|
245
|
+
deadline !== null &&
|
|
246
|
+
deadline <= nowMillis &&
|
|
247
|
+
(decodedOwner === undefined || sameOwner(record, decodedOwner)) &&
|
|
248
|
+
(cursor === undefined ||
|
|
249
|
+
deadline > cursor.deadlineAtMillis ||
|
|
250
|
+
(deadline === cursor.deadlineAtMillis && compareScheduleKeys(record, cursor) > 0))
|
|
251
|
+
) {
|
|
252
|
+
records.push({ ...scheduleKeyOf(record), deadlineAtMillis: deadline });
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
records.sort((left, right) => {
|
|
256
|
+
const byDeadline = left.deadlineAtMillis - right.deadlineAtMillis;
|
|
257
|
+
return byDeadline !== 0 ? byDeadline : compareScheduleKeys(left, right);
|
|
258
|
+
});
|
|
259
|
+
return records.slice(0, limit);
|
|
260
|
+
},
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const nextDeadline: ScheduleStore["Service"]["nextDeadline"] = Effect.fn(
|
|
264
|
+
"MemoryScheduleStore.nextDeadline",
|
|
265
|
+
)(function* (owner) {
|
|
266
|
+
const decodedOwner =
|
|
267
|
+
owner === undefined ? undefined : yield* decodeInput("nextDeadline", ScheduleOwner, owner);
|
|
268
|
+
let earliest: number | null = null;
|
|
269
|
+
for (const text of (yield* Ref.get(state)).records.values()) {
|
|
270
|
+
const record = yield* decodeRecord("nextDeadline", text);
|
|
271
|
+
if (decodedOwner !== undefined && !sameOwner(record, decodedOwner)) continue;
|
|
272
|
+
const deadline = scheduleDeadline(record);
|
|
273
|
+
if (deadline !== null && (earliest === null || deadline < earliest)) earliest = deadline;
|
|
274
|
+
}
|
|
275
|
+
return earliest;
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
return ScheduleStore.of({ insert, get, list, change, due, nextDeadline });
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
export const memoryScheduleStoreLayer = (): Layer.Layer<ScheduleStore> =>
|
|
282
|
+
Layer.effect(ScheduleStore, makeScheduleStore);
|
|
283
|
+
|
|
284
|
+
export const MemoryScheduleStoreLive: Layer.Layer<ScheduleStore> = memoryScheduleStoreLayer();
|
package/src/testing.ts
CHANGED
|
@@ -8,3 +8,9 @@ export {
|
|
|
8
8
|
type SubmissionLedgerConformanceCase,
|
|
9
9
|
type SubmissionLedgerConformanceFailure,
|
|
10
10
|
} from "@effect-agent/session/testing";
|
|
11
|
+
export {
|
|
12
|
+
scheduleStoreConformanceCases,
|
|
13
|
+
ScheduleStoreConformanceViolation,
|
|
14
|
+
type ScheduleStoreConformanceCase,
|
|
15
|
+
type ScheduleStoreConformanceFailure,
|
|
16
|
+
} from "@effect-agent/session/testing";
|