@lmzhen/dsh-evolution-state-domain 0.3.63 → 0.3.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +28 -12
  2. package/package.json +3 -3
package/lib/index.js CHANGED
@@ -116,16 +116,22 @@ function apply(ctx) {
116
116
  const provider = {
117
117
  name: PROVIDER_DOMAIN,
118
118
  async loadReviewState(sessionId) {
119
- return (await ensure()).table(REVIEW_STATE_TABLE).get(sessionId) ?? null;
119
+ const record = (await ensure()).table(REVIEW_STATE_TABLE).get(sessionId);
120
+ return record === void 0 ? null : structuredClone(record);
120
121
  },
121
122
  async saveReviewState(sessionId, record) {
122
- await (await ensure()).table(REVIEW_STATE_TABLE).put(sessionId, record);
123
+ const parsed = reviewStateSchema.safeParse(record);
124
+ if (!parsed.success) throw new Error(`evolution-state-domain: refusing to persist an invalid review-state record: ${parsed.error.issues[0]?.message ?? "schema mismatch"}`);
125
+ await (await ensure()).table(REVIEW_STATE_TABLE).put(sessionId, parsed.data);
123
126
  },
124
127
  async loadCuratorState() {
125
- return (await ensure()).table(CURATOR_STATE_TABLE).get(CURATOR_STATE_KEY) ?? null;
128
+ const record = (await ensure()).table(CURATOR_STATE_TABLE).get(CURATOR_STATE_KEY);
129
+ return record === void 0 ? null : structuredClone(record);
126
130
  },
127
131
  async saveCuratorState(record) {
128
- await (await ensure()).table(CURATOR_STATE_TABLE).put(CURATOR_STATE_KEY, record);
132
+ const parsed = curatorStateSchema.safeParse(record);
133
+ if (!parsed.success) throw new Error(`evolution-state-domain: refusing to persist an invalid curator-state record: ${parsed.error.issues[0]?.message ?? "schema mismatch"}`);
134
+ await (await ensure()).table(CURATOR_STATE_TABLE).put(CURATOR_STATE_KEY, parsed.data);
129
135
  },
130
136
  async transactCuratorState(task) {
131
137
  const table = (await ensure()).table(CURATOR_STATE_TABLE);
@@ -146,10 +152,16 @@ function apply(ctx) {
146
152
  else await table.delete(CURATOR_STATE_KEY);
147
153
  },
148
154
  async listPending(status = "pending") {
149
- return [...(await ensure()).table(PENDING_TABLE).entries()].map(([, value]) => value).filter((record) => record.status === status);
155
+ return [...(await ensure()).table(PENDING_TABLE).entries()].map(([, value]) => structuredClone(value)).filter((record) => record.status === status);
150
156
  },
151
157
  async savePending(record) {
152
- await (await ensure()).table(PENDING_TABLE).put(record.id, record);
158
+ if (!Object.prototype.hasOwnProperty.call(record, "args")) throw new Error("evolution-state-domain: refusing to persist a pending record without the required \"args\" key");
159
+ const parsed = pendingSchema.safeParse({
160
+ ...record,
161
+ args: record.args
162
+ });
163
+ if (!parsed.success) throw new Error(`evolution-state-domain: refusing to persist an invalid pending record: ${parsed.error.issues[0]?.message ?? "schema mismatch"}`);
164
+ await (await ensure()).table(PENDING_TABLE).put(record.id, parsed.data);
153
165
  },
154
166
  async claimPending(id, claimId) {
155
167
  const table = (await ensure()).table(PENDING_TABLE);
@@ -166,7 +178,7 @@ function apply(ctx) {
166
178
  };
167
179
  return slot.record;
168
180
  });
169
- return slot.record === null ? null : { ...slot.record };
181
+ return slot.record === null ? null : structuredClone(slot.record);
170
182
  } catch (error) {
171
183
  if (error instanceof DomainError && error.code === "missing-key") return null;
172
184
  throw error;
@@ -216,17 +228,21 @@ function apply(ctx) {
216
228
  };
217
229
  resolvedEntries.sort((a, b) => resolvedAtMs(a.record) - resolvedAtMs(b.record));
218
230
  const evicted = resolvedEntries.slice(0, Math.max(0, resolvedEntries.length - PENDING_RESOLVED_CAP));
219
- for (const entry of evicted) await table.delete(entry.key).catch((error) => {
220
- ctx.logger.warn(`evolution-state-domain: pending-cap eviction for "${entry.key}" failed (will retry on the next resolve): ${error instanceof Error ? error.message : String(error)}`);
221
- });
231
+ for (const entry of evicted) {
232
+ const current = table.get(entry.key);
233
+ if (!(current !== void 0 && (current.status === "approved" || current.status === "rejected") && current.resolvedAt === entry.record.resolvedAt)) continue;
234
+ await table.delete(entry.key).catch((error) => {
235
+ ctx.logger.warn(`evolution-state-domain: pending-cap eviction for "${entry.key}" failed (will retry on the next resolve): ${error instanceof Error ? error.message : String(error)}`);
236
+ });
237
+ }
222
238
  }
223
239
  const rawRecord = record;
224
240
  if (resolved.record === null) return {
225
- record: rawRecord === null ? null : { ...rawRecord },
241
+ record: rawRecord === null ? null : structuredClone(rawRecord),
226
242
  applied: false
227
243
  };
228
244
  return {
229
- record: { ...resolved.record },
245
+ record: structuredClone(resolved.record),
230
246
  applied: true
231
247
  };
232
248
  } catch (error) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lmzhen/dsh-evolution-state-domain",
3
3
  "description": "storage-domain provider for evolution state (community build)",
4
- "version": "0.3.63",
4
+ "version": "0.3.64",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -36,13 +36,13 @@
36
36
  "@deepseek-ai/dsh-invariants": "^0.1.1-rc.2",
37
37
  "@deepseek-ai/cordis": "^4.0.1",
38
38
  "@deepseek-ai/dsh-storage-domain": "^0.1.1-rc.2",
39
- "@lmzhen/dsh-evolution-state-storage": "^0.3.63"
39
+ "@lmzhen/dsh-evolution-state-storage": "^0.3.64"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@deepseek-ai/dsh-invariants": "^0.1.1-rc.2",
43
43
  "@deepseek-ai/dsh-storage-domain": "^0.1.1-rc.2",
44
44
  "@deepseek-ai/dsh-storage": "^0.1.1-rc.2",
45
45
  "@deepseek-ai/dsh-storage-json": "^0.1.1-rc.2",
46
- "@lmzhen/dsh-evolution-state-storage": "^0.3.63"
46
+ "@lmzhen/dsh-evolution-state-storage": "^0.3.64"
47
47
  }
48
48
  }