@lmzhen/dsh-evolution-approval 0.59.6 → 0.60.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 +107 -32
- package/lib/types/index.d.ts +7 -1
- package/lib/types/routes.d.ts +1 -1
- package/package.json +5 -7
package/lib/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { settingsPort } from "@lmzhen/dsh-evolution-
|
|
1
|
+
import { settingsPort } from "@lmzhen/dsh-evolution-platform/ports";
|
|
2
2
|
import { Service } from "@deepseek-ai/cordis";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
|
-
import {
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { clampOnce, evolutionHome, unwrapLive } from "@lmzhen/dsh-evolution-core";
|
|
5
6
|
import z from "@deepseek-ai/schemastery";
|
|
6
|
-
import { PENDING_STATUSES } from "@lmzhen/dsh-evolution-state";
|
|
7
|
+
import { PENDING_STATUSES } from "@lmzhen/dsh-evolution-core/state";
|
|
7
8
|
import { elapsedSince, textDiffFacts } from "@lmzhen/dsh-evolution-core/skills";
|
|
8
9
|
import { HOST_ROUTES, json, methodNotAllowed, readJsonObject, registerHostRoutes } from "@lmzhen/dsh-evolution-core/http-routes";
|
|
9
10
|
//#region lib/types/routes.js
|
|
@@ -322,6 +323,40 @@ var EvolutionApproval = class extends Service {
|
|
|
322
323
|
state() {
|
|
323
324
|
return this.ctx.evolutionState;
|
|
324
325
|
}
|
|
326
|
+
async refreshState() {
|
|
327
|
+
await this.state().reload?.();
|
|
328
|
+
}
|
|
329
|
+
/** A2: a durable executed marker is the cross-process idempotency barrier for one pending id. */
|
|
330
|
+
approvalIo() {
|
|
331
|
+
const registry = this.ctx.get("evolutionIo");
|
|
332
|
+
if (registry === void 0) return void 0;
|
|
333
|
+
try {
|
|
334
|
+
return registry.provider();
|
|
335
|
+
} catch {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
executedPath(id) {
|
|
340
|
+
return join(evolutionHome(), "approval", "executed", encodeURIComponent(id));
|
|
341
|
+
}
|
|
342
|
+
async alreadyExecuted(id) {
|
|
343
|
+
const io = this.approvalIo();
|
|
344
|
+
if (io === void 0) return false;
|
|
345
|
+
try {
|
|
346
|
+
return await io.exists(this.executedPath(id));
|
|
347
|
+
} catch {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
async markExecuted(id) {
|
|
352
|
+
const io = this.approvalIo();
|
|
353
|
+
if (io === void 0) return;
|
|
354
|
+
try {
|
|
355
|
+
await io.writeText(this.executedPath(id), (/* @__PURE__ */ new Date()).toISOString() + "\n");
|
|
356
|
+
} catch (error) {
|
|
357
|
+
this.ctx.logger.warn("evolution-approval: executed marker write failed; a second process may replay " + id + ": " + (error instanceof Error ? error.message : String(error)));
|
|
358
|
+
}
|
|
359
|
+
}
|
|
325
360
|
/** Serialize one approval's claim with the family run lease when the authority service is mounted. */
|
|
326
361
|
async withApprovalLease(id, task) {
|
|
327
362
|
const authority = this.ctx.get("evolutionMutationAuthority");
|
|
@@ -459,12 +494,19 @@ var EvolutionApproval = class extends Service {
|
|
|
459
494
|
async expireStale() {
|
|
460
495
|
const ttl = this.ttl();
|
|
461
496
|
if (ttl <= 0) return [];
|
|
497
|
+
await this.refreshState();
|
|
462
498
|
const cutoff = Date.now() - ttl;
|
|
463
499
|
const expired = [];
|
|
464
500
|
for (const record of await this.state().listPending("pending")) {
|
|
465
501
|
const stagedAt = Date.parse(record.createdAt);
|
|
466
502
|
if (!Number.isFinite(stagedAt) || stagedAt > cutoff) continue;
|
|
467
503
|
await this.withApprovalLease(record.id, async () => {
|
|
504
|
+
await this.refreshState();
|
|
505
|
+
if (await this.alreadyExecuted(record.id)) return;
|
|
506
|
+
const current = (await this.state().listPending("pending")).find((item) => item.id === record.id);
|
|
507
|
+
if (current === void 0) return;
|
|
508
|
+
const currentStagedAt = Date.parse(current.createdAt);
|
|
509
|
+
if (!Number.isFinite(currentStagedAt) || currentStagedAt > cutoff) return;
|
|
468
510
|
const claimId = this.newClaimId();
|
|
469
511
|
if (!await this.state().claimPending(record.id, claimId)) return;
|
|
470
512
|
let applied = false;
|
|
@@ -484,7 +526,9 @@ var EvolutionApproval = class extends Service {
|
|
|
484
526
|
return expired;
|
|
485
527
|
}
|
|
486
528
|
async list(status = "pending") {
|
|
529
|
+
const ttl = this.ttl();
|
|
487
530
|
await this.expireStale();
|
|
531
|
+
if (ttl <= 0) await this.refreshState();
|
|
488
532
|
return await this.state().listPending(status);
|
|
489
533
|
}
|
|
490
534
|
async listReadonly(status = "pending") {
|
|
@@ -492,10 +536,25 @@ var EvolutionApproval = class extends Service {
|
|
|
492
536
|
}
|
|
493
537
|
async approve(id, audit) {
|
|
494
538
|
await this.expireStale();
|
|
495
|
-
|
|
539
|
+
if (await this.alreadyExecuted(id)) return {
|
|
496
540
|
ok: false,
|
|
497
|
-
message: "待批准写入「" + id + "
|
|
541
|
+
message: "待批准写入「" + id + "」已执行过(跨进程记录),不会重复回放。"
|
|
498
542
|
};
|
|
543
|
+
return await this.dedupe(`approve:${id}`, async () => {
|
|
544
|
+
return await this.withApprovalLease(id, async () => {
|
|
545
|
+
await this.refreshState();
|
|
546
|
+
if (await this.alreadyExecuted(id)) return {
|
|
547
|
+
ok: false,
|
|
548
|
+
message: "待批准写入「" + id + "」已执行过(跨进程记录),不会重复回放。"
|
|
549
|
+
};
|
|
550
|
+
const resolved = await this.doApprove(id, audit);
|
|
551
|
+
if (resolved.ok) await this.markExecuted(id);
|
|
552
|
+
return resolved;
|
|
553
|
+
}) ?? {
|
|
554
|
+
ok: false,
|
|
555
|
+
message: "待批准写入「" + id + "」正在被另一个批准/过期进程处理:lease 被占用。"
|
|
556
|
+
};
|
|
557
|
+
});
|
|
499
558
|
}
|
|
500
559
|
/** S2-P2-22 (0.3.80): return an ORPHANED executing record to the pending
|
|
501
560
|
* window. "Orphaned" is DECIDED here, never assumed: the older rationale
|
|
@@ -562,35 +621,51 @@ var EvolutionApproval = class extends Service {
|
|
|
562
621
|
};
|
|
563
622
|
}
|
|
564
623
|
async reject(id, audit) {
|
|
565
|
-
return await this.withApprovalLease(id,
|
|
566
|
-
|
|
567
|
-
if (
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
624
|
+
return await this.withApprovalLease(id, async () => {
|
|
625
|
+
await this.refreshState();
|
|
626
|
+
if (await this.alreadyExecuted(id)) return {
|
|
627
|
+
ok: false,
|
|
628
|
+
message: "待批准写入「" + id + "」已执行过(跨进程记录),不会拒绝为未执行。"
|
|
629
|
+
};
|
|
630
|
+
return await this.dedupe(`reject:${id}`, async () => {
|
|
631
|
+
const claimId = this.newClaimId();
|
|
632
|
+
if (!await this.state().claimPending(id, claimId)) {
|
|
633
|
+
const stuck = (await this.state().listPending("executing")).find((item) => item.id === id);
|
|
634
|
+
if (stuck) {
|
|
635
|
+
const holder = typeof stuck.claimedBy === "string" ? stuck.claimedBy : "";
|
|
636
|
+
const holderPid = holder === "" ? null : claimHolderPid(holder);
|
|
637
|
+
if (holderPid !== null && holderPid > 0 && holderPid !== process.pid && isProcessAlive(holderPid)) return {
|
|
638
|
+
ok: false,
|
|
639
|
+
message: `待批准写入「${id}」被 pid ${holderPid} 占用且该进程仍存活:可能有另一个进程正在执行这次批准。不要拒绝它:改为核验写入效果,并让那次运行跑完。`
|
|
640
|
+
};
|
|
641
|
+
return (await this.state().tryResolvePending(id, "rejected")).applied ? {
|
|
642
|
+
ok: true,
|
|
643
|
+
message: `已拒绝执行中的写入「${id}」(未持有占用声明:这可能与在途的批准运行器抢跑;如果写入已落地,其效果按现状保留。请手动核验写入状态)。`
|
|
644
|
+
} : {
|
|
645
|
+
ok: false,
|
|
646
|
+
message: `待批准写入「${id}」无法拒绝:它并发地了结了。`
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
return {
|
|
650
|
+
ok: false,
|
|
651
|
+
message: `待批准写入「${id}」不在待批准窗口里(已轮转或已了结)。`
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
const resolution = await this.state().tryResolvePending(id, "rejected", claimId);
|
|
655
|
+
if (!resolution.applied || !resolution.record) {
|
|
656
|
+
await this.state().releasePendingClaim(id, claimId);
|
|
657
|
+
return {
|
|
658
|
+
ok: false,
|
|
659
|
+
message: `待批准写入「${id}」已不在待批准状态(已了结或不存在)。`
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
await this.recordDecision(resolution.record, audit);
|
|
583
663
|
return {
|
|
584
|
-
ok:
|
|
585
|
-
message:
|
|
664
|
+
ok: true,
|
|
665
|
+
message: `已拒绝 ${resolution.record.kind} 写入「${id}」。`
|
|
586
666
|
};
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
return {
|
|
590
|
-
ok: true,
|
|
591
|
-
message: `已拒绝 ${resolution.record.kind} 写入「${id}」。`
|
|
592
|
-
};
|
|
593
|
-
})) ?? {
|
|
667
|
+
});
|
|
668
|
+
}) ?? {
|
|
594
669
|
ok: false,
|
|
595
670
|
message: "待批准写入「" + id + "」正在被另一个批准/过期进程处理:lease 被占用。"
|
|
596
671
|
};
|
package/lib/types/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
import { Context, Service } from '@deepseek-ai/cordis';
|
|
20
20
|
import type { Volatile } from '@deepseek-ai/cordis';
|
|
21
21
|
import z from '@deepseek-ai/schemastery';
|
|
22
|
-
import type { PendingKind, PendingRecord, PendingStatus } from '@lmzhen/dsh-evolution-state';
|
|
22
|
+
import type { PendingKind, PendingRecord, PendingStatus } from '@lmzhen/dsh-evolution-core/state';
|
|
23
23
|
import type { DecisionAudit } from '@lmzhen/dsh-evolution-core/http-routes';
|
|
24
24
|
export type { PendingKind, PendingRecord, PendingStatus };
|
|
25
25
|
export type WriteRunner = (args: unknown, context?: {
|
|
@@ -214,6 +214,12 @@ export declare class EvolutionApproval extends Service {
|
|
|
214
214
|
private readonly inFlight;
|
|
215
215
|
constructor(ctx: Context, config?: Config);
|
|
216
216
|
private state;
|
|
217
|
+
private refreshState;
|
|
218
|
+
/** A2: a durable executed marker is the cross-process idempotency barrier for one pending id. */
|
|
219
|
+
private approvalIo;
|
|
220
|
+
private executedPath;
|
|
221
|
+
private alreadyExecuted;
|
|
222
|
+
private markExecuted;
|
|
217
223
|
/** Serialize one approval's claim with the family run lease when the authority service is mounted. */
|
|
218
224
|
private withApprovalLease;
|
|
219
225
|
/** Fail-closed capability adapters need to distinguish "allowed" from "enabled". */
|
package/lib/types/routes.d.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* the same methods.
|
|
15
15
|
* @module @lmzhen/dsh-evolution-approval/routes
|
|
16
16
|
*/
|
|
17
|
-
import type { PendingKind, PendingRecord, PendingStatus } from '@lmzhen/dsh-evolution-state';
|
|
17
|
+
import type { PendingKind, PendingRecord, PendingStatus } from '@lmzhen/dsh-evolution-core/state';
|
|
18
18
|
import { elapsedSince } from '@lmzhen/dsh-evolution-core/skills';
|
|
19
19
|
import type { ConnectionFetchRouteLike, DecisionAudit } from '@lmzhen/dsh-evolution-core/http-routes';
|
|
20
20
|
import type { WritePreview } from './index.ts';
|
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.
|
|
4
|
+
"version": "0.60.1",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -29,17 +29,15 @@
|
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@deepseek-ai/schemastery": "^3.18.5",
|
|
32
|
-
"@lmzhen/dsh-evolution-core": "0.
|
|
33
|
-
"@lmzhen/dsh-evolution-
|
|
32
|
+
"@lmzhen/dsh-evolution-core": "0.60.1",
|
|
33
|
+
"@lmzhen/dsh-evolution-platform": "0.60.1"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"@deepseek-ai/cordis": "^4.0.5",
|
|
37
|
-
"@lmzhen/dsh-evolution-core": "^0.
|
|
38
|
-
"@lmzhen/dsh-evolution-state": "^0.59.6"
|
|
37
|
+
"@lmzhen/dsh-evolution-core": "^0.60.1"
|
|
39
38
|
},
|
|
40
39
|
"devDependencies": {
|
|
41
|
-
"@lmzhen/dsh-evolution-core": "^0.
|
|
42
|
-
"@lmzhen/dsh-evolution-state": "^0.59.6"
|
|
40
|
+
"@lmzhen/dsh-evolution-core": "^0.60.1"
|
|
43
41
|
},
|
|
44
42
|
"engines": {
|
|
45
43
|
"dsh": "0.2.2-rc.5"
|