@lmzhen/dsh-evolution-approval 0.39.2 → 0.40.1

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/lib/index.js CHANGED
@@ -321,6 +321,18 @@ var EvolutionApproval = class EvolutionApproval extends Service {
321
321
  state() {
322
322
  return this.ctx.evolutionState;
323
323
  }
324
+ /** Serialize one approval's claim with the family run lease when the authority service is mounted. */
325
+ async withApprovalLease(id, task) {
326
+ const authority = this.ctx.get("evolutionMutationAuthority");
327
+ if (authority === void 0) return await task();
328
+ const acquired = await authority.lease.acquire("foreground", "approval:" + id);
329
+ if (!acquired.ok) return void 0;
330
+ try {
331
+ return await task();
332
+ } finally {
333
+ await authority.lease.release(acquired.token);
334
+ }
335
+ }
324
336
  /** Fail-closed capability adapters need to distinguish "allowed" from "enabled". */
325
337
  /** Host write capability exposed to settings/Facts; mirrors the live enabled switch. */
326
338
  get platformWrite() {
@@ -450,9 +462,21 @@ var EvolutionApproval = class EvolutionApproval extends Service {
450
462
  for (const record of await this.state().listPending("pending")) {
451
463
  const stagedAt = Date.parse(record.createdAt);
452
464
  if (!Number.isFinite(stagedAt) || stagedAt > cutoff) continue;
453
- const claimId = this.newClaimId();
454
- if (!await this.state().claimPending(record.id, claimId)) continue;
455
- if ((await this.state().tryResolvePending(record.id, "rejected", claimId)).applied) expired.push(record.id);
465
+ await this.withApprovalLease(record.id, async () => {
466
+ const claimId = this.newClaimId();
467
+ if (!await this.state().claimPending(record.id, claimId)) return;
468
+ let applied = false;
469
+ try {
470
+ applied = (await this.state().tryResolvePending(record.id, "rejected", claimId)).applied;
471
+ if (applied) expired.push(record.id);
472
+ } catch (error) {
473
+ this.ctx.logger.warn(error);
474
+ } finally {
475
+ if (!applied) await this.state().releasePendingClaim(record.id, claimId).catch((error) => {
476
+ this.ctx.logger.warn(error);
477
+ });
478
+ }
479
+ });
456
480
  }
457
481
  if (expired.length > 0) this.ctx.logger.warn(`evolution-approval: ${expired.length} staged write(s) passed pendingTtlMs=${ttl} and were closed as rejected WITHOUT executing: ${expired.join(", ")} — stage them again if they are still wanted`);
458
482
  return expired;
@@ -463,7 +487,10 @@ var EvolutionApproval = class EvolutionApproval extends Service {
463
487
  }
464
488
  async approve(id, audit) {
465
489
  await this.expireStale();
466
- return await this.dedupe(`approve:${id}`, () => this.doApprove(id, audit));
490
+ return await this.withApprovalLease(id, () => this.dedupe(`approve:${id}`, () => this.doApprove(id, audit))) ?? {
491
+ ok: false,
492
+ message: "待批准写入「" + id + "」正在被另一个批准/过期进程处理:lease 被占用。"
493
+ };
467
494
  }
468
495
  /** S2-P2-22 (0.3.80): return an ORPHANED executing record to the pending
469
496
  * window. "Orphaned" is DECIDED here, never assumed: the older rationale
@@ -530,7 +557,7 @@ var EvolutionApproval = class EvolutionApproval extends Service {
530
557
  };
531
558
  }
532
559
  async reject(id, audit) {
533
- return await this.dedupe(`reject:${id}`, async () => {
560
+ return await this.withApprovalLease(id, () => this.dedupe(`reject:${id}`, async () => {
534
561
  const claimId = this.newClaimId();
535
562
  if (!await this.state().claimPending(id, claimId)) {
536
563
  if ((await this.state().listPending("executing")).find((item) => item.id === id)) return (await this.state().tryResolvePending(id, "rejected")).applied ? {
@@ -558,7 +585,10 @@ var EvolutionApproval = class EvolutionApproval extends Service {
558
585
  ok: true,
559
586
  message: `已拒绝 ${resolution.record.kind} 写入「${id}」。`
560
587
  };
561
- });
588
+ })) ?? {
589
+ ok: false,
590
+ message: "待批准写入「" + id + "」正在被另一个批准/过期进程处理:lease 被占用。"
591
+ };
562
592
  }
563
593
  /**
564
594
  * 0.3.17 (S3.1): platform-side session policy derivation — the platform's
@@ -210,6 +210,8 @@ export declare class EvolutionApproval extends Service {
210
210
  private readonly inFlight;
211
211
  constructor(ctx: Context, config?: Config);
212
212
  private state;
213
+ /** Serialize one approval's claim with the family run lease when the authority service is mounted. */
214
+ private withApprovalLease;
213
215
  /** Fail-closed capability adapters need to distinguish "allowed" from "enabled". */
214
216
  /** Host write capability exposed to settings/Facts; mirrors the live enabled switch. */
215
217
  get platformWrite(): boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lmzhen/dsh-evolution-approval",
3
3
  "description": "Stage/pending approval service for Hermes-style self-evolution writes (community build)",
4
- "version": "0.39.2",
4
+ "version": "0.40.1",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -27,18 +27,18 @@
27
27
  "license": "MIT",
28
28
  "dependencies": {
29
29
  "@deepseek-ai/schemastery": "^3.18.5",
30
- "@lmzhen/dsh-evolution-core": "^0.39.2"
30
+ "@lmzhen/dsh-evolution-core": "^0.40.1"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@deepseek-ai/cordis": "^4.0.5",
34
- "@lmzhen/dsh-evolution-core": "^0.39.2",
35
- "@lmzhen/dsh-evolution-state": "^0.39.2",
36
- "@lmzhen/dsh-evolution-state-storage": "^0.39.2"
34
+ "@lmzhen/dsh-evolution-core": "^0.40.1",
35
+ "@lmzhen/dsh-evolution-state": "^0.40.1",
36
+ "@lmzhen/dsh-evolution-state-storage": "^0.40.1"
37
37
  },
38
38
  "devDependencies": {
39
- "@lmzhen/dsh-evolution-core": "^0.39.2",
40
- "@lmzhen/dsh-evolution-state": "^0.39.2",
41
- "@lmzhen/dsh-evolution-state-storage": "^0.39.2"
39
+ "@lmzhen/dsh-evolution-core": "^0.40.1",
40
+ "@lmzhen/dsh-evolution-state": "^0.40.1",
41
+ "@lmzhen/dsh-evolution-state-storage": "^0.40.1"
42
42
  },
43
43
  "engines": {
44
44
  "dsh": ">=0.2.2-rc.0 <0.2.3-0"