@harrylabsj/kiwi 0.12.31 → 0.12.32
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/a2a/node.d.ts +1 -0
- package/dist/a2a/node.js +170 -2
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/reception/agent-runtime.d.ts +32 -1
- package/dist/a2a/reception/agent-runtime.js +420 -11
- package/dist/a2a/reception/agent-runtime.js.map +1 -1
- package/dist/a2a/reception/customer-memory-tools.d.ts +73 -0
- package/dist/a2a/reception/customer-memory-tools.js +17 -0
- package/dist/a2a/reception/customer-memory-tools.js.map +1 -0
- package/dist/a2a/reception/engine.d.ts +12 -0
- package/dist/a2a/reception/engine.js +42 -1
- package/dist/a2a/reception/engine.js.map +1 -1
- package/dist/a2a/reception/publish.d.ts +14 -1
- package/dist/a2a/reception/publish.js +62 -1
- package/dist/a2a/reception/publish.js.map +1 -1
- package/dist/a2a/reception/question-spec.d.ts +91 -0
- package/dist/a2a/reception/question-spec.js +260 -0
- package/dist/a2a/reception/question-spec.js.map +1 -0
- package/dist/a2a/reception/reply-outbox.d.ts +6 -0
- package/dist/a2a/reception/reply-outbox.js +44 -0
- package/dist/a2a/reception/reply-outbox.js.map +1 -1
- package/dist/a2a/reception/responder.d.ts +13 -1
- package/dist/a2a/reception/responder.js +26 -3
- package/dist/a2a/reception/responder.js.map +1 -1
- package/dist/a2a/reception/skill-catalog.d.ts +72 -0
- package/dist/a2a/reception/skill-catalog.js +85 -0
- package/dist/a2a/reception/skill-catalog.js.map +1 -0
- package/dist/a2a/reception/skill-contract.d.ts +70 -0
- package/dist/a2a/reception/skill-contract.js +183 -0
- package/dist/a2a/reception/skill-contract.js.map +1 -0
- package/dist/a2a/reception/types.d.ts +12 -0
- package/dist/a2a/server/customer-identity-gateway.d.ts +111 -0
- package/dist/a2a/server/customer-identity-gateway.js +441 -0
- package/dist/a2a/server/customer-identity-gateway.js.map +1 -0
- package/dist/a2a/server/merchant-handler.d.ts +34 -0
- package/dist/a2a/server/merchant-handler.js +439 -19
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/pipeline.js +74 -29
- package/dist/a2a/server/pipeline.js.map +1 -1
- package/dist/a2a/server/types.d.ts +2 -0
- package/dist/a2a/unit-alias.d.ts +3 -3
- package/dist/a2a/unit-alias.js +9 -5
- package/dist/a2a/unit-alias.js.map +1 -1
- package/dist/auth/merchant-sessions.d.ts +3 -0
- package/dist/auth/merchant-sessions.js +21 -5
- package/dist/auth/merchant-sessions.js.map +1 -1
- package/dist/buyer-core/a2a-knp.d.ts +4 -2
- package/dist/buyer-core/a2a-knp.js +5 -2
- package/dist/buyer-core/a2a-knp.js.map +1 -1
- package/dist/buyer-core/a2a-negotiator.d.ts +1 -0
- package/dist/buyer-core/a2a-negotiator.js +18 -11
- package/dist/buyer-core/a2a-negotiator.js.map +1 -1
- package/dist/buyer-core/a2a-quote-fetcher.d.ts +25 -0
- package/dist/buyer-core/a2a-quote-fetcher.js +82 -4
- package/dist/buyer-core/a2a-quote-fetcher.js.map +1 -1
- package/dist/buyer-core/a2a-quote-history.d.ts +94 -0
- package/dist/buyer-core/a2a-quote-history.js +149 -0
- package/dist/buyer-core/a2a-quote-history.js.map +1 -0
- package/dist/buyer-core/build-service.d.ts +19 -0
- package/dist/buyer-core/build-service.js +30 -1
- package/dist/buyer-core/build-service.js.map +1 -1
- package/dist/buyer-core/errors.d.ts +1 -1
- package/dist/buyer-core/errors.js +1 -0
- package/dist/buyer-core/errors.js.map +1 -1
- package/dist/buyer-core/open-inquiry-signer.d.ts +1 -0
- package/dist/buyer-core/open-inquiry-signer.js +5 -2
- package/dist/buyer-core/open-inquiry-signer.js.map +1 -1
- package/dist/buyer-core/protocol-recovery.d.ts +2 -1
- package/dist/buyer-core/protocol-recovery.js +14 -4
- package/dist/buyer-core/protocol-recovery.js.map +1 -1
- package/dist/buyer-core/service.d.ts +57 -5
- package/dist/buyer-core/service.js +275 -50
- package/dist/buyer-core/service.js.map +1 -1
- package/dist/buyer-core/store.d.ts +8 -0
- package/dist/buyer-core/store.js +37 -7
- package/dist/buyer-core/store.js.map +1 -1
- package/dist/cli.js +13 -2
- package/dist/cli.js.map +1 -1
- package/dist/cloud/bootstrap.js +141 -7
- package/dist/cloud/bootstrap.js.map +1 -1
- package/dist/commerce/receipt/dispatcher.d.ts +42 -0
- package/dist/commerce/receipt/dispatcher.js +68 -0
- package/dist/commerce/receipt/dispatcher.js.map +1 -0
- package/dist/commerce/receipt/receipts.d.ts +140 -0
- package/dist/commerce/receipt/receipts.js +247 -0
- package/dist/commerce/receipt/receipts.js.map +1 -0
- package/dist/config/profile.d.ts +2 -2
- package/dist/handoff/order-receipts.d.ts +62 -0
- package/dist/handoff/order-receipts.js +71 -0
- package/dist/handoff/order-receipts.js.map +1 -0
- package/dist/handoff/ucp-checkout/cart-channel.d.ts +2 -1
- package/dist/handoff/ucp-checkout/cart-channel.js +1 -0
- package/dist/handoff/ucp-checkout/cart-channel.js.map +1 -1
- package/dist/handoff/ucp-checkout/channel.d.ts +6 -0
- package/dist/handoff/ucp-checkout/channel.js +44 -0
- package/dist/handoff/ucp-checkout/channel.js.map +1 -1
- package/dist/http/cli.js +6 -0
- package/dist/http/cli.js.map +1 -1
- package/dist/http/merchant-management/api.d.ts +10 -0
- package/dist/http/merchant-management/api.js +443 -0
- package/dist/http/merchant-management/api.js.map +1 -1
- package/dist/http/merchant-management/draft-store.js +8 -6
- package/dist/http/merchant-management/draft-store.js.map +1 -1
- package/dist/http/merchant-management/page.js +2 -2
- package/dist/mcp/cli.js +6 -0
- package/dist/mcp/cli.js.map +1 -1
- package/dist/mcp/merchant-runtime-assembly.d.ts +20 -0
- package/dist/mcp/merchant-runtime-assembly.js +93 -3
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
- package/dist/mcp/merchant-server.d.ts +5 -0
- package/dist/mcp/merchant-server.js +12 -0
- package/dist/mcp/merchant-server.js.map +1 -1
- package/dist/mcp/tools.js +7 -1
- package/dist/mcp/tools.js.map +1 -1
- package/dist/merchant/application/actor.d.ts +1 -1
- package/dist/merchant/application/actor.js +15 -0
- package/dist/merchant/application/actor.js.map +1 -1
- package/dist/merchant/workbench-server.d.ts +1 -0
- package/dist/merchant/workbench-server.js +176 -2
- package/dist/merchant/workbench-server.js.map +1 -1
- package/dist/merchant-core/budget/background-budget.d.ts +132 -0
- package/dist/merchant-core/budget/background-budget.js +308 -0
- package/dist/merchant-core/budget/background-budget.js.map +1 -0
- package/dist/merchant-core/context-governance/actor-context.d.ts +56 -0
- package/dist/merchant-core/context-governance/actor-context.js +76 -0
- package/dist/merchant-core/context-governance/actor-context.js.map +1 -0
- package/dist/merchant-core/context-governance/authority.d.ts +39 -0
- package/dist/merchant-core/context-governance/authority.js +28 -0
- package/dist/merchant-core/context-governance/authority.js.map +1 -0
- package/dist/merchant-core/context-governance/context-guard.d.ts +104 -0
- package/dist/merchant-core/context-governance/context-guard.js +194 -0
- package/dist/merchant-core/context-governance/context-guard.js.map +1 -0
- package/dist/merchant-core/context-governance/data-use-grant.d.ts +91 -0
- package/dist/merchant-core/context-governance/data-use-grant.js +211 -0
- package/dist/merchant-core/context-governance/data-use-grant.js.map +1 -0
- package/dist/merchant-core/context-governance/feature-flags.d.ts +32 -0
- package/dist/merchant-core/context-governance/feature-flags.js +83 -0
- package/dist/merchant-core/context-governance/feature-flags.js.map +1 -0
- package/dist/merchant-core/context-governance/pilot-readiness.d.ts +39 -0
- package/dist/merchant-core/context-governance/pilot-readiness.js +141 -0
- package/dist/merchant-core/context-governance/pilot-readiness.js.map +1 -0
- package/dist/merchant-core/context-governance/source-governance.d.ts +157 -0
- package/dist/merchant-core/context-governance/source-governance.js +492 -0
- package/dist/merchant-core/context-governance/source-governance.js.map +1 -0
- package/dist/merchant-core/context-governance/types.d.ts +62 -0
- package/dist/merchant-core/context-governance/types.js +92 -0
- package/dist/merchant-core/context-governance/types.js.map +1 -0
- package/dist/merchant-core/customer-delivery/binding.d.ts +55 -0
- package/dist/merchant-core/customer-delivery/binding.js +121 -0
- package/dist/merchant-core/customer-delivery/binding.js.map +1 -0
- package/dist/merchant-core/customer-delivery/channel.d.ts +75 -0
- package/dist/merchant-core/customer-delivery/channel.js +157 -0
- package/dist/merchant-core/customer-delivery/channel.js.map +1 -0
- package/dist/merchant-core/customer-delivery/consent-repository.d.ts +50 -0
- package/dist/merchant-core/customer-delivery/consent-repository.js +160 -0
- package/dist/merchant-core/customer-delivery/consent-repository.js.map +1 -0
- package/dist/merchant-core/customer-delivery/dispatcher-loop.d.ts +68 -0
- package/dist/merchant-core/customer-delivery/dispatcher-loop.js +132 -0
- package/dist/merchant-core/customer-delivery/dispatcher-loop.js.map +1 -0
- package/dist/merchant-core/customer-delivery/dispatcher.d.ts +118 -0
- package/dist/merchant-core/customer-delivery/dispatcher.js +166 -0
- package/dist/merchant-core/customer-delivery/dispatcher.js.map +1 -0
- package/dist/merchant-core/customer-delivery/email-adapter.d.ts +31 -0
- package/dist/merchant-core/customer-delivery/email-adapter.js +90 -0
- package/dist/merchant-core/customer-delivery/email-adapter.js.map +1 -0
- package/dist/merchant-core/customer-delivery/gate.d.ts +37 -0
- package/dist/merchant-core/customer-delivery/gate.js +196 -0
- package/dist/merchant-core/customer-delivery/gate.js.map +1 -0
- package/dist/merchant-core/customer-delivery/reconciliation.d.ts +13 -0
- package/dist/merchant-core/customer-delivery/reconciliation.js +70 -0
- package/dist/merchant-core/customer-delivery/reconciliation.js.map +1 -0
- package/dist/merchant-core/customer-delivery/repository.d.ts +55 -0
- package/dist/merchant-core/customer-delivery/repository.js +235 -0
- package/dist/merchant-core/customer-delivery/repository.js.map +1 -0
- package/dist/merchant-core/customer-delivery/smtp-adapter.d.ts +30 -0
- package/dist/merchant-core/customer-delivery/smtp-adapter.js +183 -0
- package/dist/merchant-core/customer-delivery/smtp-adapter.js.map +1 -0
- package/dist/merchant-core/customer-delivery/stream-gate.d.ts +45 -0
- package/dist/merchant-core/customer-delivery/stream-gate.js +123 -0
- package/dist/merchant-core/customer-delivery/stream-gate.js.map +1 -0
- package/dist/merchant-core/customer-delivery/types.d.ts +103 -0
- package/dist/merchant-core/customer-delivery/types.js +32 -0
- package/dist/merchant-core/customer-delivery/types.js.map +1 -0
- package/dist/merchant-core/customer-delivery/weixin-adapter.d.ts +35 -0
- package/dist/merchant-core/customer-delivery/weixin-adapter.js +56 -0
- package/dist/merchant-core/customer-delivery/weixin-adapter.js.map +1 -0
- package/dist/merchant-core/customer-memory/auto-identity.d.ts +43 -0
- package/dist/merchant-core/customer-memory/auto-identity.js +163 -0
- package/dist/merchant-core/customer-memory/auto-identity.js.map +1 -0
- package/dist/merchant-core/customer-memory/grants.d.ts +72 -0
- package/dist/merchant-core/customer-memory/grants.js +224 -0
- package/dist/merchant-core/customer-memory/grants.js.map +1 -0
- package/dist/merchant-core/customer-memory/history.d.ts +51 -0
- package/dist/merchant-core/customer-memory/history.js +252 -0
- package/dist/merchant-core/customer-memory/history.js.map +1 -0
- package/dist/merchant-core/customer-memory/identity.d.ts +66 -0
- package/dist/merchant-core/customer-memory/identity.js +249 -0
- package/dist/merchant-core/customer-memory/identity.js.map +1 -0
- package/dist/merchant-core/customer-memory/maintenance.d.ts +51 -0
- package/dist/merchant-core/customer-memory/maintenance.js +67 -0
- package/dist/merchant-core/customer-memory/maintenance.js.map +1 -0
- package/dist/merchant-core/customer-memory/memory.d.ts +119 -0
- package/dist/merchant-core/customer-memory/memory.js +502 -0
- package/dist/merchant-core/customer-memory/memory.js.map +1 -0
- package/dist/merchant-core/customer-memory/private-transport.d.ts +10 -0
- package/dist/merchant-core/customer-memory/private-transport.js +108 -0
- package/dist/merchant-core/customer-memory/private-transport.js.map +1 -0
- package/dist/merchant-core/customer-memory/quote-history.d.ts +160 -0
- package/dist/merchant-core/customer-memory/quote-history.js +392 -0
- package/dist/merchant-core/customer-memory/quote-history.js.map +1 -0
- package/dist/merchant-core/customer-memory/reception-provider.d.ts +69 -0
- package/dist/merchant-core/customer-memory/reception-provider.js +250 -0
- package/dist/merchant-core/customer-memory/reception-provider.js.map +1 -0
- package/dist/merchant-core/customer-memory/reception-writer.d.ts +19 -0
- package/dist/merchant-core/customer-memory/reception-writer.js +32 -0
- package/dist/merchant-core/customer-memory/reception-writer.js.map +1 -0
- package/dist/merchant-core/customer-memory/types.d.ts +111 -0
- package/dist/merchant-core/customer-memory/types.js +58 -0
- package/dist/merchant-core/customer-memory/types.js.map +1 -0
- package/dist/merchant-core/followup/repository.d.ts +2 -1
- package/dist/merchant-core/followup/repository.js +29 -23
- package/dist/merchant-core/followup/repository.js.map +1 -1
- package/dist/merchant-core/learning/evaluation-fixtures.d.ts +38 -0
- package/dist/merchant-core/learning/evaluation-fixtures.js +83 -0
- package/dist/merchant-core/learning/evaluation-fixtures.js.map +1 -0
- package/dist/merchant-core/learning/evaluation-suite-v1.d.ts +56 -0
- package/dist/merchant-core/learning/evaluation-suite-v1.js +129 -0
- package/dist/merchant-core/learning/evaluation-suite-v1.js.map +1 -0
- package/dist/merchant-core/learning/evaluation.d.ts +84 -0
- package/dist/merchant-core/learning/evaluation.js +581 -0
- package/dist/merchant-core/learning/evaluation.js.map +1 -0
- package/dist/merchant-core/learning/initial-skills.d.ts +36 -0
- package/dist/merchant-core/learning/initial-skills.js +67 -0
- package/dist/merchant-core/learning/initial-skills.js.map +1 -0
- package/dist/merchant-core/learning/precheck.d.ts +33 -0
- package/dist/merchant-core/learning/precheck.js +56 -0
- package/dist/merchant-core/learning/precheck.js.map +1 -0
- package/dist/merchant-core/learning/real-evaluation-runner.d.ts +52 -0
- package/dist/merchant-core/learning/real-evaluation-runner.js +167 -0
- package/dist/merchant-core/learning/real-evaluation-runner.js.map +1 -0
- package/dist/merchant-core/learning/reception-skill-catalog.d.ts +39 -0
- package/dist/merchant-core/learning/reception-skill-catalog.js +145 -0
- package/dist/merchant-core/learning/reception-skill-catalog.js.map +1 -0
- package/dist/merchant-core/learning/recovery-gateway.d.ts +27 -0
- package/dist/merchant-core/learning/recovery-gateway.js +106 -0
- package/dist/merchant-core/learning/recovery-gateway.js.map +1 -0
- package/dist/merchant-core/learning/repository.d.ts +130 -0
- package/dist/merchant-core/learning/repository.js +588 -0
- package/dist/merchant-core/learning/repository.js.map +1 -0
- package/dist/merchant-core/learning/service.d.ts +262 -0
- package/dist/merchant-core/learning/service.js +1498 -0
- package/dist/merchant-core/learning/service.js.map +1 -0
- package/dist/merchant-core/learning/test-dispatch-gateway.d.ts +43 -0
- package/dist/merchant-core/learning/test-dispatch-gateway.js +145 -0
- package/dist/merchant-core/learning/test-dispatch-gateway.js.map +1 -0
- package/dist/merchant-core/learning/types.d.ts +217 -0
- package/dist/merchant-core/learning/types.js +52 -0
- package/dist/merchant-core/learning/types.js.map +1 -0
- package/dist/merchant-core/migrations/ledgers.d.ts +40 -0
- package/dist/merchant-core/migrations/ledgers.js +824 -0
- package/dist/merchant-core/migrations/ledgers.js.map +1 -0
- package/dist/merchant-core/migrations/runner.d.ts +55 -0
- package/dist/merchant-core/migrations/runner.js +114 -0
- package/dist/merchant-core/migrations/runner.js.map +1 -0
- package/dist/merchant-core/operations-jobs/advice-runner.d.ts +63 -0
- package/dist/merchant-core/operations-jobs/advice-runner.js +199 -0
- package/dist/merchant-core/operations-jobs/advice-runner.js.map +1 -0
- package/dist/merchant-core/operations-jobs/pilot-evidence.d.ts +64 -0
- package/dist/merchant-core/operations-jobs/pilot-evidence.js +109 -0
- package/dist/merchant-core/operations-jobs/pilot-evidence.js.map +1 -0
- package/dist/merchant-core/operations-jobs/pilot-metrics.d.ts +70 -0
- package/dist/merchant-core/operations-jobs/pilot-metrics.js +98 -0
- package/dist/merchant-core/operations-jobs/pilot-metrics.js.map +1 -0
- package/dist/merchant-core/operations-jobs/projection.d.ts +41 -0
- package/dist/merchant-core/operations-jobs/projection.js +158 -0
- package/dist/merchant-core/operations-jobs/projection.js.map +1 -0
- package/dist/merchant-core/operations-jobs/scheduler.d.ts +68 -0
- package/dist/merchant-core/operations-jobs/scheduler.js +233 -0
- package/dist/merchant-core/operations-jobs/scheduler.js.map +1 -0
- package/dist/merchant-core/operations-jobs/service.d.ts +260 -0
- package/dist/merchant-core/operations-jobs/service.js +922 -0
- package/dist/merchant-core/operations-jobs/service.js.map +1 -0
- package/dist/merchant-core/ops/facade.d.ts +10 -0
- package/dist/merchant-core/ops/facade.js +52 -0
- package/dist/merchant-core/ops/facade.js.map +1 -1
- package/dist/merchant-core/ops/schema.d.ts +1 -1
- package/dist/merchant-core/ops/schema.js +119 -2
- package/dist/merchant-core/ops/schema.js.map +1 -1
- package/dist/merchant-core/ops/surface.d.ts +37 -0
- package/dist/merchant-core/ops/surface.js +240 -0
- package/dist/merchant-core/ops/surface.js.map +1 -1
- package/dist/merchant-core/ownership/instance-ownership.d.ts +122 -0
- package/dist/merchant-core/ownership/instance-ownership.js +236 -0
- package/dist/merchant-core/ownership/instance-ownership.js.map +1 -0
- package/dist/merchant-core/ownership/runtime.d.ts +175 -0
- package/dist/merchant-core/ownership/runtime.js +422 -0
- package/dist/merchant-core/ownership/runtime.js.map +1 -0
- package/dist/merchant-core/storage/transaction.d.ts +102 -7
- package/dist/merchant-core/storage/transaction.js +274 -3
- package/dist/merchant-core/storage/transaction.js.map +1 -1
- package/dist/merchant-credential-rotation.d.ts +4 -1
- package/dist/merchant-credential-rotation.js +12 -8
- package/dist/merchant-credential-rotation.js.map +1 -1
- package/dist/merchant-runtime/manager.d.ts +2 -0
- package/dist/merchant-runtime/manager.js +5 -0
- package/dist/merchant-runtime/manager.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import type { DatabaseSync } from "node:sqlite";
|
|
17
|
+
import type { SourceRefCheck } from "./context-guard.js";
|
|
18
|
+
/** 每个删除任务必须逐面登记的存储面(§12.2)。 */
|
|
19
|
+
export declare const DELETION_SURFACES: readonly ["source_payloads", "derived_objects", "caches", "in_flight_runs", "pending_outbound", "exports", "backups"];
|
|
20
|
+
export type DeletionSurface = (typeof DELETION_SURFACES)[number];
|
|
21
|
+
export declare const DELETION_JOB_STATES: readonly ["PENDING", "PROCESSING", "COMPLETED"];
|
|
22
|
+
export type DeletionJobState = (typeof DELETION_JOB_STATES)[number];
|
|
23
|
+
export declare const DELETION_SURFACE_STATUSES: readonly ["PENDING", "PURGED", "FAILED"];
|
|
24
|
+
export type DeletionSurfaceStatus = (typeof DELETION_SURFACE_STATUSES)[number];
|
|
25
|
+
/** 默认可恢复/重试窗口(天):支持在窗口内幂等重试与运维核对。 */
|
|
26
|
+
export declare const DEFAULT_DELETION_RECOVERABLE_DAYS = 7;
|
|
27
|
+
export interface DataScopeRecord {
|
|
28
|
+
readonly merchant_id: string;
|
|
29
|
+
readonly erase_scope_id: string;
|
|
30
|
+
readonly data_epoch: number;
|
|
31
|
+
readonly state: "ACTIVE" | "SEALED";
|
|
32
|
+
readonly sealed_at: string | null;
|
|
33
|
+
readonly updated_at: string;
|
|
34
|
+
}
|
|
35
|
+
export interface SourceRefRecord {
|
|
36
|
+
readonly merchant_id: string;
|
|
37
|
+
readonly object_id: string;
|
|
38
|
+
readonly version: number;
|
|
39
|
+
readonly payload_ref: string;
|
|
40
|
+
readonly erase_scope_id: string;
|
|
41
|
+
readonly observed_at: string;
|
|
42
|
+
readonly expires_at: string | null;
|
|
43
|
+
readonly erased_at: string | null;
|
|
44
|
+
readonly updated_at: string;
|
|
45
|
+
}
|
|
46
|
+
export interface DeletionSurfaceRecord {
|
|
47
|
+
readonly surface: DeletionSurface;
|
|
48
|
+
readonly status: DeletionSurfaceStatus;
|
|
49
|
+
readonly attempts: number;
|
|
50
|
+
readonly receipt_ref: string | null;
|
|
51
|
+
readonly last_error: string | null;
|
|
52
|
+
readonly updated_at: string;
|
|
53
|
+
}
|
|
54
|
+
export interface DeletionJobRecord {
|
|
55
|
+
readonly deletion_job_id: string;
|
|
56
|
+
readonly merchant_id: string;
|
|
57
|
+
readonly erase_scope_id: string;
|
|
58
|
+
readonly requested_by: string;
|
|
59
|
+
readonly reason: string;
|
|
60
|
+
readonly state: DeletionJobState;
|
|
61
|
+
readonly data_epoch: number;
|
|
62
|
+
readonly recoverable_until: string;
|
|
63
|
+
readonly created_at: string;
|
|
64
|
+
readonly updated_at: string;
|
|
65
|
+
readonly surfaces: readonly DeletionSurfaceRecord[];
|
|
66
|
+
}
|
|
67
|
+
export interface SourceGovernanceOptions {
|
|
68
|
+
db: DatabaseSync;
|
|
69
|
+
now: () => string;
|
|
70
|
+
}
|
|
71
|
+
export declare class SourceGovernanceService {
|
|
72
|
+
private readonly db;
|
|
73
|
+
private readonly now;
|
|
74
|
+
constructor(options: SourceGovernanceOptions);
|
|
75
|
+
/** 服务器时钟统一为定宽 UTC;不可解析即 fail-closed。 */
|
|
76
|
+
private nowUtc;
|
|
77
|
+
/** 当前数据域 epoch:仅 ACTIVE 返回(封存/不存在 = 旧快照失效)。 */
|
|
78
|
+
currentDataScopeEpoch(merchantId: string, eraseScopeId: string): number | undefined;
|
|
79
|
+
getDataScope(merchantId: string, eraseScopeId: string): DataScopeRecord | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* 登记来源对象版本。版本不可变:同 (object_id, version) 内容一致幂等;
|
|
82
|
+
* 内容不一致 version_conflict;已 erased 的版本不得复活(source_unavailable)。
|
|
83
|
+
* 首次登记会为 erase_scope 建立 ACTIVE data_scope(epoch 1)。
|
|
84
|
+
*/
|
|
85
|
+
registerSourceRef(input: {
|
|
86
|
+
merchant_id: string;
|
|
87
|
+
object_id: string;
|
|
88
|
+
version: number;
|
|
89
|
+
payload_ref: string;
|
|
90
|
+
erase_scope_id: string;
|
|
91
|
+
observed_at: string;
|
|
92
|
+
expires_at?: string;
|
|
93
|
+
}): SourceRefRecord;
|
|
94
|
+
getSourceRef(merchantId: string, objectId: string, version: number): SourceRefRecord | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* 来源可用性(ContextGuard `sourceAvailable`):权威值来自本库。
|
|
97
|
+
* 调用方提示(erased / expires_at)只用于提前拒绝,不用于放行:
|
|
98
|
+
* 存在且未 erased、未过期、版本匹配才可用。
|
|
99
|
+
*/
|
|
100
|
+
isAvailable(merchantId: string, ref: SourceRefCheck): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* 记录派生边(parent 来源版本 → child 业务对象)。parent 必须已登记且未删除;
|
|
103
|
+
* 边唯一(同边重复登记幂等,同边异 child_kind 为 version_conflict)。
|
|
104
|
+
*/
|
|
105
|
+
recordDerivationEdge(input: {
|
|
106
|
+
merchant_id: string;
|
|
107
|
+
parent_object_id: string;
|
|
108
|
+
parent_version: number;
|
|
109
|
+
child_object_id: string;
|
|
110
|
+
child_kind: string;
|
|
111
|
+
}): void;
|
|
112
|
+
/**
|
|
113
|
+
* 登记可恢复删除:先 DeletionJob → 封存 erase_scope(data_epoch+1)→
|
|
114
|
+
* 撤销该 scope 的 ACTIVE grant → 标记 source_refs.erased_at → 封存派生边
|
|
115
|
+
* → 建 PENDING 的各存储面。重复请求(同 scope 有开放任务)幂等返回原任务,
|
|
116
|
+
* 不重复递增 epoch。
|
|
117
|
+
*/
|
|
118
|
+
requestDeletion(input: {
|
|
119
|
+
merchant_id: string;
|
|
120
|
+
erase_scope_id: string;
|
|
121
|
+
requested_by: string;
|
|
122
|
+
reason: string;
|
|
123
|
+
expected_data_epoch?: number;
|
|
124
|
+
recoverable_days?: number;
|
|
125
|
+
}): DeletionJobRecord;
|
|
126
|
+
/** 存储面清理成功(幂等)。全部 PURGED 后任务 COMPLETED。 */
|
|
127
|
+
completeSurface(input: {
|
|
128
|
+
merchant_id: string;
|
|
129
|
+
deletion_job_id: string;
|
|
130
|
+
surface: string;
|
|
131
|
+
receipt_ref?: string;
|
|
132
|
+
}): DeletionJobRecord;
|
|
133
|
+
/** 存储面清理失败:记录原因并递增 attempts;任务保持可重试。 */
|
|
134
|
+
failSurface(input: {
|
|
135
|
+
merchant_id: string;
|
|
136
|
+
deletion_job_id: string;
|
|
137
|
+
surface: string;
|
|
138
|
+
reason: string;
|
|
139
|
+
}): DeletionJobRecord;
|
|
140
|
+
/** 失败存储面重新排队(幂等;PENDING/PURGED 不变)。 */
|
|
141
|
+
retrySurface(input: {
|
|
142
|
+
merchant_id: string;
|
|
143
|
+
deletion_job_id: string;
|
|
144
|
+
surface: string;
|
|
145
|
+
}): DeletionJobRecord;
|
|
146
|
+
getDeletionJob(merchantId: string, jobId: string): DeletionJobRecord | undefined;
|
|
147
|
+
/** 开放任务(PENDING/PROCESSING)——工作台/运维核对入口。 */
|
|
148
|
+
listOpenDeletionJobs(merchantId: string): DeletionJobRecord[];
|
|
149
|
+
private refreshJobState;
|
|
150
|
+
private requireJob;
|
|
151
|
+
private readJob;
|
|
152
|
+
private readOpenJob;
|
|
153
|
+
private readSurfaces;
|
|
154
|
+
private readSurface;
|
|
155
|
+
private readScope;
|
|
156
|
+
private readSourceRow;
|
|
157
|
+
}
|
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* 来源治理与可恢复删除(TOP1-3 设计 v1.1 §4.2 / §12.2 / §18.1 / §22.1 M1)。
|
|
18
|
+
*
|
|
19
|
+
* 本文件是 M1 共性控制中的「来源删除」基础:
|
|
20
|
+
* - `SourceGovernanceService.registerSourceRef` 登记来源对象版本(正文在受控
|
|
21
|
+
* payload,`payload_ref` 只存不透明引用);版本不可变;
|
|
22
|
+
* - `requestDeletion` 先登记 DeletionJob、封存 erase_scope、递增 data_epoch
|
|
23
|
+
* 并撤销相关 grant,再把来源引用标为 erased、封存派生边;具体载荷清理由
|
|
24
|
+
* 各存储面异步完成并逐面记录 purged / pending / failed(失败可幂等重试);
|
|
25
|
+
* - `isAvailable` / `currentDataScopeEpoch` 供 ContextGuard 的
|
|
26
|
+
* ContextVersionAuthority 接入(见 `authority.ts` 的注入点);
|
|
27
|
+
* - 查询一律带 merchant 作用域,不提供跨商家读取。
|
|
28
|
+
*
|
|
29
|
+
* 明确边界:
|
|
30
|
+
* - 本模块只做「立即失效」(封存/标记)与状态登记,不实现记忆、技能候选、
|
|
31
|
+
* 草稿等业务对象的清理——那是 M3 的删除传播;M1 保证旧快照/旧游标在删除
|
|
32
|
+
* 后立刻不可用,且删除任务可恢复(幂等重试)。
|
|
33
|
+
* - 不递归推断客户身份,不复活已 erased 的来源版本。
|
|
34
|
+
*/
|
|
35
|
+
import { randomUUID } from "node:crypto";
|
|
36
|
+
import { inImmediateTransaction } from "../storage/transaction.js";
|
|
37
|
+
import { ContextGovernanceError, assertOpaqueId } from "./types.js";
|
|
38
|
+
/** 每个删除任务必须逐面登记的存储面(§12.2)。 */
|
|
39
|
+
export const DELETION_SURFACES = [
|
|
40
|
+
"source_payloads",
|
|
41
|
+
"derived_objects",
|
|
42
|
+
"caches",
|
|
43
|
+
"in_flight_runs",
|
|
44
|
+
"pending_outbound",
|
|
45
|
+
"exports",
|
|
46
|
+
"backups",
|
|
47
|
+
];
|
|
48
|
+
export const DELETION_JOB_STATES = ["PENDING", "PROCESSING", "COMPLETED"];
|
|
49
|
+
export const DELETION_SURFACE_STATUSES = ["PENDING", "PURGED", "FAILED"];
|
|
50
|
+
/** 默认可恢复/重试窗口(天):支持在窗口内幂等重试与运维核对。 */
|
|
51
|
+
export const DEFAULT_DELETION_RECOVERABLE_DAYS = 7;
|
|
52
|
+
function scopeToRecord(row) {
|
|
53
|
+
return {
|
|
54
|
+
merchant_id: row.merchant_id,
|
|
55
|
+
erase_scope_id: row.erase_scope_id,
|
|
56
|
+
data_epoch: row.data_epoch,
|
|
57
|
+
state: row.state,
|
|
58
|
+
sealed_at: row.sealed_at,
|
|
59
|
+
updated_at: row.updated_at,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function sourceToRecord(row) {
|
|
63
|
+
return {
|
|
64
|
+
merchant_id: row.merchant_id,
|
|
65
|
+
object_id: row.object_id,
|
|
66
|
+
version: row.version,
|
|
67
|
+
payload_ref: row.payload_ref,
|
|
68
|
+
erase_scope_id: row.erase_scope_id,
|
|
69
|
+
observed_at: row.observed_at,
|
|
70
|
+
expires_at: row.expires_at,
|
|
71
|
+
erased_at: row.erased_at,
|
|
72
|
+
updated_at: row.updated_at,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function surfaceToRecord(row) {
|
|
76
|
+
return {
|
|
77
|
+
surface: row.surface,
|
|
78
|
+
status: row.status,
|
|
79
|
+
attempts: row.attempts,
|
|
80
|
+
receipt_ref: row.receipt_ref,
|
|
81
|
+
last_error: row.last_error,
|
|
82
|
+
updated_at: row.updated_at,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function jobToRecord(row, surfaces) {
|
|
86
|
+
return {
|
|
87
|
+
deletion_job_id: row.deletion_job_id,
|
|
88
|
+
merchant_id: row.merchant_id,
|
|
89
|
+
erase_scope_id: row.erase_scope_id,
|
|
90
|
+
requested_by: row.requested_by,
|
|
91
|
+
reason: row.reason,
|
|
92
|
+
state: row.state,
|
|
93
|
+
data_epoch: row.data_epoch,
|
|
94
|
+
recoverable_until: row.recoverable_until,
|
|
95
|
+
created_at: row.created_at,
|
|
96
|
+
updated_at: row.updated_at,
|
|
97
|
+
surfaces: surfaces.map(surfaceToRecord),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** 时间列统一 UTC RFC3339(Z 结尾);存储前规范化为 toISOString 定宽形式。 */
|
|
101
|
+
function normalizeUtc(value, field) {
|
|
102
|
+
if (!value.endsWith("Z")) {
|
|
103
|
+
throw new ContextGovernanceError("validation", `${field}_not_utc`, `${field} 必须是 UTC RFC3339(Z 结尾),收到 ${value}`);
|
|
104
|
+
}
|
|
105
|
+
const ms = Date.parse(value);
|
|
106
|
+
if (!Number.isFinite(ms)) {
|
|
107
|
+
throw new ContextGovernanceError("validation", `${field}_invalid`, `${field} 必须是 RFC3339 时间`);
|
|
108
|
+
}
|
|
109
|
+
return new Date(ms).toISOString();
|
|
110
|
+
}
|
|
111
|
+
function requirePositiveInt(value, field) {
|
|
112
|
+
if (!Number.isSafeInteger(value) || value < 1) {
|
|
113
|
+
throw new ContextGovernanceError("validation", `${field}_invalid`, `${field} 必须是正整数`);
|
|
114
|
+
}
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
function requireSurface(value) {
|
|
118
|
+
if (!DELETION_SURFACES.includes(value)) {
|
|
119
|
+
throw new ContextGovernanceError("validation", "surface_unknown", `surface "${value}" 不在固定存储面注册表中`);
|
|
120
|
+
}
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
/** 删除原因/操作者是运维字段:非空、有界、无控制字符(可含中文)。 */
|
|
124
|
+
function requireFreeText(value, field, maxLength) {
|
|
125
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
126
|
+
throw new ContextGovernanceError("validation", `${field}_empty`, `${field} 必须是非空字符串`);
|
|
127
|
+
}
|
|
128
|
+
if (value.length > maxLength) {
|
|
129
|
+
throw new ContextGovernanceError("validation", `${field}_too_long`, `${field} 长度超过 ${maxLength}`);
|
|
130
|
+
}
|
|
131
|
+
// eslint-disable-next-line no-control-regex
|
|
132
|
+
if (/[\u0000-\u001f\u007f]/.test(value)) {
|
|
133
|
+
throw new ContextGovernanceError("validation", `${field}_invalid`, `${field} 含控制字符`);
|
|
134
|
+
}
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
export class SourceGovernanceService {
|
|
138
|
+
db;
|
|
139
|
+
now;
|
|
140
|
+
constructor(options) {
|
|
141
|
+
this.db = options.db;
|
|
142
|
+
this.now = options.now;
|
|
143
|
+
}
|
|
144
|
+
/** 服务器时钟统一为定宽 UTC;不可解析即 fail-closed。 */
|
|
145
|
+
nowUtc() {
|
|
146
|
+
const ms = Date.parse(this.now());
|
|
147
|
+
if (!Number.isFinite(ms)) {
|
|
148
|
+
throw new ContextGovernanceError("validation", "now_invalid", "时钟输出必须是可解析的 RFC3339 时间");
|
|
149
|
+
}
|
|
150
|
+
return new Date(ms).toISOString();
|
|
151
|
+
}
|
|
152
|
+
/** 当前数据域 epoch:仅 ACTIVE 返回(封存/不存在 = 旧快照失效)。 */
|
|
153
|
+
currentDataScopeEpoch(merchantId, eraseScopeId) {
|
|
154
|
+
const row = this.readScope(merchantId, eraseScopeId);
|
|
155
|
+
return row !== undefined && row.state === "ACTIVE" ? row.data_epoch : undefined;
|
|
156
|
+
}
|
|
157
|
+
getDataScope(merchantId, eraseScopeId) {
|
|
158
|
+
const row = this.readScope(merchantId, eraseScopeId);
|
|
159
|
+
return row === undefined ? undefined : scopeToRecord(row);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 登记来源对象版本。版本不可变:同 (object_id, version) 内容一致幂等;
|
|
163
|
+
* 内容不一致 version_conflict;已 erased 的版本不得复活(source_unavailable)。
|
|
164
|
+
* 首次登记会为 erase_scope 建立 ACTIVE data_scope(epoch 1)。
|
|
165
|
+
*/
|
|
166
|
+
registerSourceRef(input) {
|
|
167
|
+
const merchantId = assertOpaqueId(input.merchant_id, "merchant_id");
|
|
168
|
+
const objectId = assertOpaqueId(input.object_id, "object_id");
|
|
169
|
+
const version = requirePositiveInt(input.version, "version");
|
|
170
|
+
const payloadRef = assertOpaqueId(input.payload_ref, "payload_ref");
|
|
171
|
+
const eraseScopeId = assertOpaqueId(input.erase_scope_id, "erase_scope_id");
|
|
172
|
+
const observedAt = normalizeUtc(input.observed_at, "observed_at");
|
|
173
|
+
const expiresAt = input.expires_at === undefined ? null : normalizeUtc(input.expires_at, "expires_at");
|
|
174
|
+
return inImmediateTransaction(this.db, () => {
|
|
175
|
+
const scope = this.readScope(merchantId, eraseScopeId);
|
|
176
|
+
if (scope === undefined) {
|
|
177
|
+
this.db
|
|
178
|
+
.prepare(`INSERT INTO data_scopes
|
|
179
|
+
(merchant_id, erase_scope_id, data_epoch, state, sealed_at, updated_at)
|
|
180
|
+
VALUES (?, ?, 1, 'ACTIVE', NULL, ?)`)
|
|
181
|
+
.run(merchantId, eraseScopeId, this.nowUtc());
|
|
182
|
+
}
|
|
183
|
+
else if (scope.state === "SEALED") {
|
|
184
|
+
throw new ContextGovernanceError("source_unavailable", "erase_scope_sealed", `erase_scope ${eraseScopeId} 已封存(删除已登记);不得再登记来源`);
|
|
185
|
+
}
|
|
186
|
+
const existing = this.readSourceRow(merchantId, objectId, version);
|
|
187
|
+
if (existing !== undefined) {
|
|
188
|
+
if (existing.erased_at !== null) {
|
|
189
|
+
throw new ContextGovernanceError("source_unavailable", "source_ref_erased", "来源版本已删除,不能复活;请登记新版本");
|
|
190
|
+
}
|
|
191
|
+
if (existing.payload_ref !== payloadRef ||
|
|
192
|
+
existing.erase_scope_id !== eraseScopeId ||
|
|
193
|
+
existing.observed_at !== observedAt ||
|
|
194
|
+
existing.expires_at !== expiresAt) {
|
|
195
|
+
throw new ContextGovernanceError("version_conflict", "source_ref_immutable", `来源 ${objectId}@${version} 内容不一致;版本不可变`);
|
|
196
|
+
}
|
|
197
|
+
return sourceToRecord(existing);
|
|
198
|
+
}
|
|
199
|
+
const stamp = this.nowUtc();
|
|
200
|
+
this.db
|
|
201
|
+
.prepare(`INSERT INTO source_refs
|
|
202
|
+
(merchant_id, object_id, version, payload_ref, erase_scope_id,
|
|
203
|
+
observed_at, expires_at, erased_at, updated_at)
|
|
204
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, NULL, ?)`)
|
|
205
|
+
.run(merchantId, objectId, version, payloadRef, eraseScopeId, observedAt, expiresAt, stamp);
|
|
206
|
+
return sourceToRecord(this.readSourceRow(merchantId, objectId, version));
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
getSourceRef(merchantId, objectId, version) {
|
|
210
|
+
const row = this.readSourceRow(assertOpaqueId(merchantId, "merchant_id"), assertOpaqueId(objectId, "object_id"), requirePositiveInt(version, "version"));
|
|
211
|
+
return row === undefined ? undefined : sourceToRecord(row);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* 来源可用性(ContextGuard `sourceAvailable`):权威值来自本库。
|
|
215
|
+
* 调用方提示(erased / expires_at)只用于提前拒绝,不用于放行:
|
|
216
|
+
* 存在且未 erased、未过期、版本匹配才可用。
|
|
217
|
+
*/
|
|
218
|
+
isAvailable(merchantId, ref) {
|
|
219
|
+
if (ref.erased === true)
|
|
220
|
+
return false;
|
|
221
|
+
const now = this.nowUtc();
|
|
222
|
+
if (ref.expires_at !== undefined) {
|
|
223
|
+
const expiresMs = Date.parse(ref.expires_at);
|
|
224
|
+
if (!Number.isFinite(expiresMs) || expiresMs <= Date.parse(now))
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
if (!Number.isSafeInteger(ref.version) || ref.version < 1)
|
|
228
|
+
return false;
|
|
229
|
+
const row = this.readSourceRow(merchantId, ref.object_id, ref.version);
|
|
230
|
+
if (row === undefined || row.erased_at !== null)
|
|
231
|
+
return false;
|
|
232
|
+
if (row.expires_at !== null && row.expires_at <= now)
|
|
233
|
+
return false;
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* 记录派生边(parent 来源版本 → child 业务对象)。parent 必须已登记且未删除;
|
|
238
|
+
* 边唯一(同边重复登记幂等,同边异 child_kind 为 version_conflict)。
|
|
239
|
+
*/
|
|
240
|
+
recordDerivationEdge(input) {
|
|
241
|
+
const merchantId = assertOpaqueId(input.merchant_id, "merchant_id");
|
|
242
|
+
const parentId = assertOpaqueId(input.parent_object_id, "parent_object_id");
|
|
243
|
+
const parentVersion = requirePositiveInt(input.parent_version, "parent_version");
|
|
244
|
+
const childId = assertOpaqueId(input.child_object_id, "child_object_id");
|
|
245
|
+
const childKind = requireFreeText(input.child_kind, "child_kind", 64);
|
|
246
|
+
inImmediateTransaction(this.db, () => {
|
|
247
|
+
const parent = this.readSourceRow(merchantId, parentId, parentVersion);
|
|
248
|
+
if (parent === undefined || parent.erased_at !== null) {
|
|
249
|
+
throw new ContextGovernanceError("source_unavailable", "parent_source_unavailable", "派生边的父来源不存在或已删除;拒绝登记迟到派生");
|
|
250
|
+
}
|
|
251
|
+
const existing = this.db
|
|
252
|
+
.prepare(`SELECT child_kind FROM derivation_edges
|
|
253
|
+
WHERE merchant_id = ? AND parent_object_id = ? AND parent_version = ?
|
|
254
|
+
AND child_object_id = ?`)
|
|
255
|
+
.get(merchantId, parentId, parentVersion, childId);
|
|
256
|
+
if (existing !== undefined) {
|
|
257
|
+
// 边唯一且不可变:同边异 kind 视为内容冲突,不静默覆盖。
|
|
258
|
+
if (existing.child_kind !== childKind) {
|
|
259
|
+
throw new ContextGovernanceError("version_conflict", "derivation_edge_immutable", "派生边已存在且 child_kind 不一致;边不可变");
|
|
260
|
+
}
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
this.db
|
|
264
|
+
.prepare(`INSERT INTO derivation_edges
|
|
265
|
+
(merchant_id, parent_object_id, parent_version, child_object_id,
|
|
266
|
+
child_kind, created_at, sealed_at)
|
|
267
|
+
VALUES (?, ?, ?, ?, ?, ?, NULL)`)
|
|
268
|
+
.run(merchantId, parentId, parentVersion, childId, childKind, this.nowUtc());
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* 登记可恢复删除:先 DeletionJob → 封存 erase_scope(data_epoch+1)→
|
|
273
|
+
* 撤销该 scope 的 ACTIVE grant → 标记 source_refs.erased_at → 封存派生边
|
|
274
|
+
* → 建 PENDING 的各存储面。重复请求(同 scope 有开放任务)幂等返回原任务,
|
|
275
|
+
* 不重复递增 epoch。
|
|
276
|
+
*/
|
|
277
|
+
requestDeletion(input) {
|
|
278
|
+
const merchantId = assertOpaqueId(input.merchant_id, "merchant_id");
|
|
279
|
+
const eraseScopeId = assertOpaqueId(input.erase_scope_id, "erase_scope_id");
|
|
280
|
+
const requestedBy = assertOpaqueId(input.requested_by, "requested_by");
|
|
281
|
+
const reason = requireFreeText(input.reason, "reason", 500);
|
|
282
|
+
const recoverableDays = input.recoverable_days ?? DEFAULT_DELETION_RECOVERABLE_DAYS;
|
|
283
|
+
if (!Number.isSafeInteger(recoverableDays) || recoverableDays < 0 || recoverableDays > 180) {
|
|
284
|
+
throw new ContextGovernanceError("validation", "recoverable_days_invalid", "recoverable_days 必须是 0..180 的整数");
|
|
285
|
+
}
|
|
286
|
+
return inImmediateTransaction(this.db, () => {
|
|
287
|
+
const open = this.readOpenJob(merchantId, eraseScopeId);
|
|
288
|
+
if (open !== undefined) {
|
|
289
|
+
// 幂等:同一 scope 已有开放任务(PENDING/PROCESSING)直接返回。
|
|
290
|
+
return this.readJob(open.deletion_job_id);
|
|
291
|
+
}
|
|
292
|
+
const scope = this.readScope(merchantId, eraseScopeId);
|
|
293
|
+
if (input.expected_data_epoch !== undefined &&
|
|
294
|
+
(scope?.data_epoch ?? 0) !== input.expected_data_epoch) {
|
|
295
|
+
throw new ContextGovernanceError("version_conflict", "data_epoch_mismatch", `erase_scope ${eraseScopeId} 的 data_epoch 已变化;回读后重试`);
|
|
296
|
+
}
|
|
297
|
+
const now = this.nowUtc();
|
|
298
|
+
const newEpoch = scope === undefined ? 1 : scope.data_epoch + 1;
|
|
299
|
+
if (scope === undefined) {
|
|
300
|
+
this.db
|
|
301
|
+
.prepare(`INSERT INTO data_scopes
|
|
302
|
+
(merchant_id, erase_scope_id, data_epoch, state, sealed_at, updated_at)
|
|
303
|
+
VALUES (?, ?, ?, 'SEALED', ?, ?)`)
|
|
304
|
+
.run(merchantId, eraseScopeId, newEpoch, now, now);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
this.db
|
|
308
|
+
.prepare(`UPDATE data_scopes
|
|
309
|
+
SET data_epoch = ?, state = 'SEALED', sealed_at = ?, updated_at = ?
|
|
310
|
+
WHERE merchant_id = ? AND erase_scope_id = ?`)
|
|
311
|
+
.run(newEpoch, now, now, merchantId, eraseScopeId);
|
|
312
|
+
}
|
|
313
|
+
// 撤销该 scope 的 ACTIVE 授权:逐条递增 grant_epoch(同 key 多行时
|
|
314
|
+
// 每次取当前最大值 +1,避免唯一索引冲突),状态置 REVOKED。
|
|
315
|
+
const activeGrants = this.db
|
|
316
|
+
.prepare(`SELECT grant_id, subject_id, purpose
|
|
317
|
+
FROM data_grants
|
|
318
|
+
WHERE merchant_id = ? AND resource_scope_id = ? AND status = 'ACTIVE'
|
|
319
|
+
ORDER BY grant_epoch ASC`)
|
|
320
|
+
.all(merchantId, eraseScopeId);
|
|
321
|
+
for (const grant of activeGrants) {
|
|
322
|
+
this.db
|
|
323
|
+
.prepare(`UPDATE data_grants
|
|
324
|
+
SET status = 'REVOKED', updated_at = ?,
|
|
325
|
+
grant_epoch = (SELECT COALESCE(MAX(grant_epoch), 0) + 1
|
|
326
|
+
FROM data_grants
|
|
327
|
+
WHERE merchant_id = ? AND subject_id = ? AND purpose = ?
|
|
328
|
+
AND resource_scope_id = ?)
|
|
329
|
+
WHERE merchant_id = ? AND grant_id = ? AND status = 'ACTIVE'`)
|
|
330
|
+
.run(now, merchantId, grant.subject_id, grant.purpose, eraseScopeId, merchantId, grant.grant_id);
|
|
331
|
+
}
|
|
332
|
+
// 来源立即不可读(旧索引/旧快照失效),载荷清理交给各存储面。
|
|
333
|
+
this.db
|
|
334
|
+
.prepare(`UPDATE source_refs SET erased_at = ?, updated_at = ?
|
|
335
|
+
WHERE merchant_id = ? AND erase_scope_id = ? AND erased_at IS NULL`)
|
|
336
|
+
.run(now, now, merchantId, eraseScopeId);
|
|
337
|
+
// 封存受影响的派生边:由这些边定位的记忆/候选/草稿由 M3 清理。
|
|
338
|
+
this.db
|
|
339
|
+
.prepare(`UPDATE derivation_edges SET sealed_at = ?
|
|
340
|
+
WHERE merchant_id = ? AND sealed_at IS NULL
|
|
341
|
+
AND parent_object_id IN (
|
|
342
|
+
SELECT object_id FROM source_refs
|
|
343
|
+
WHERE merchant_id = ? AND erase_scope_id = ?)`)
|
|
344
|
+
.run(now, merchantId, merchantId, eraseScopeId);
|
|
345
|
+
const jobId = `dlj_${randomUUID()}`;
|
|
346
|
+
const recoverableUntil = new Date(Date.parse(now) + recoverableDays * 24 * 60 * 60 * 1000).toISOString();
|
|
347
|
+
this.db
|
|
348
|
+
.prepare(`INSERT INTO deletion_jobs
|
|
349
|
+
(deletion_job_id, merchant_id, erase_scope_id, requested_by, reason,
|
|
350
|
+
state, data_epoch, recoverable_until, created_at, updated_at)
|
|
351
|
+
VALUES (?, ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?)`)
|
|
352
|
+
.run(jobId, merchantId, eraseScopeId, requestedBy, reason, newEpoch, recoverableUntil, now, now);
|
|
353
|
+
const insertSurface = this.db.prepare(`INSERT INTO deletion_job_surfaces
|
|
354
|
+
(deletion_job_id, surface, status, attempts, receipt_ref, last_error, updated_at)
|
|
355
|
+
VALUES (?, ?, 'PENDING', 0, NULL, NULL, ?)`);
|
|
356
|
+
for (const surface of DELETION_SURFACES) {
|
|
357
|
+
insertSurface.run(jobId, surface, now);
|
|
358
|
+
}
|
|
359
|
+
return this.readJob(jobId);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
/** 存储面清理成功(幂等)。全部 PURGED 后任务 COMPLETED。 */
|
|
363
|
+
completeSurface(input) {
|
|
364
|
+
const merchantId = assertOpaqueId(input.merchant_id, "merchant_id");
|
|
365
|
+
const jobId = assertOpaqueId(input.deletion_job_id, "deletion_job_id");
|
|
366
|
+
const surface = requireSurface(input.surface);
|
|
367
|
+
const receiptRef = input.receipt_ref === undefined ? null : assertOpaqueId(input.receipt_ref, "receipt_ref");
|
|
368
|
+
return inImmediateTransaction(this.db, () => {
|
|
369
|
+
const job = this.requireJob(merchantId, jobId);
|
|
370
|
+
if (job.state === "COMPLETED")
|
|
371
|
+
return this.readJob(jobId);
|
|
372
|
+
const current = this.readSurface(jobId, surface);
|
|
373
|
+
if (current === undefined) {
|
|
374
|
+
throw new ContextGovernanceError("validation", "surface_missing", "删除任务缺少该存储面");
|
|
375
|
+
}
|
|
376
|
+
if (current.status !== "PURGED") {
|
|
377
|
+
this.db
|
|
378
|
+
.prepare(`UPDATE deletion_job_surfaces
|
|
379
|
+
SET status = 'PURGED', receipt_ref = ?, last_error = NULL, updated_at = ?
|
|
380
|
+
WHERE deletion_job_id = ? AND surface = ?`)
|
|
381
|
+
.run(receiptRef, this.nowUtc(), jobId, surface);
|
|
382
|
+
}
|
|
383
|
+
return this.refreshJobState(merchantId, jobId);
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
/** 存储面清理失败:记录原因并递增 attempts;任务保持可重试。 */
|
|
387
|
+
failSurface(input) {
|
|
388
|
+
const merchantId = assertOpaqueId(input.merchant_id, "merchant_id");
|
|
389
|
+
const jobId = assertOpaqueId(input.deletion_job_id, "deletion_job_id");
|
|
390
|
+
const surface = requireSurface(input.surface);
|
|
391
|
+
const reason = requireFreeText(input.reason, "reason", 300);
|
|
392
|
+
return inImmediateTransaction(this.db, () => {
|
|
393
|
+
const job = this.requireJob(merchantId, jobId);
|
|
394
|
+
if (job.state === "COMPLETED")
|
|
395
|
+
return this.readJob(jobId);
|
|
396
|
+
this.db
|
|
397
|
+
.prepare(`UPDATE deletion_job_surfaces
|
|
398
|
+
SET status = 'FAILED', attempts = attempts + 1, last_error = ?, updated_at = ?
|
|
399
|
+
WHERE deletion_job_id = ? AND surface = ?`)
|
|
400
|
+
.run(reason, this.nowUtc(), jobId, surface);
|
|
401
|
+
return this.refreshJobState(merchantId, jobId);
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
/** 失败存储面重新排队(幂等;PENDING/PURGED 不变)。 */
|
|
405
|
+
retrySurface(input) {
|
|
406
|
+
const merchantId = assertOpaqueId(input.merchant_id, "merchant_id");
|
|
407
|
+
const jobId = assertOpaqueId(input.deletion_job_id, "deletion_job_id");
|
|
408
|
+
const surface = requireSurface(input.surface);
|
|
409
|
+
return inImmediateTransaction(this.db, () => {
|
|
410
|
+
const job = this.requireJob(merchantId, jobId);
|
|
411
|
+
if (job.state === "COMPLETED")
|
|
412
|
+
return this.readJob(jobId);
|
|
413
|
+
this.db
|
|
414
|
+
.prepare(`UPDATE deletion_job_surfaces
|
|
415
|
+
SET status = 'PENDING', updated_at = ?
|
|
416
|
+
WHERE deletion_job_id = ? AND surface = ? AND status = 'FAILED'`)
|
|
417
|
+
.run(this.nowUtc(), jobId, surface);
|
|
418
|
+
return this.refreshJobState(merchantId, jobId);
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
getDeletionJob(merchantId, jobId) {
|
|
422
|
+
return this.readJob(jobId, assertOpaqueId(merchantId, "merchant_id"));
|
|
423
|
+
}
|
|
424
|
+
/** 开放任务(PENDING/PROCESSING)——工作台/运维核对入口。 */
|
|
425
|
+
listOpenDeletionJobs(merchantId) {
|
|
426
|
+
const merchant = assertOpaqueId(merchantId, "merchant_id");
|
|
427
|
+
const rows = this.db
|
|
428
|
+
.prepare(`SELECT * FROM deletion_jobs
|
|
429
|
+
WHERE merchant_id = ? AND state IN ('PENDING','PROCESSING')
|
|
430
|
+
ORDER BY created_at ASC`)
|
|
431
|
+
.all(merchant);
|
|
432
|
+
return rows.map((row) => jobToRecord(row, this.readSurfaces(row.deletion_job_id)));
|
|
433
|
+
}
|
|
434
|
+
refreshJobState(merchantId, jobId) {
|
|
435
|
+
const surfaces = this.readSurfaces(jobId);
|
|
436
|
+
const state = surfaces.every((surface) => surface.status === "PURGED")
|
|
437
|
+
? "COMPLETED"
|
|
438
|
+
: "PROCESSING";
|
|
439
|
+
this.db
|
|
440
|
+
.prepare("UPDATE deletion_jobs SET state = ?, updated_at = ? WHERE deletion_job_id = ? AND merchant_id = ?")
|
|
441
|
+
.run(state, this.nowUtc(), jobId, merchantId);
|
|
442
|
+
return this.readJob(jobId);
|
|
443
|
+
}
|
|
444
|
+
requireJob(merchantId, jobId) {
|
|
445
|
+
const row = this.db
|
|
446
|
+
.prepare("SELECT * FROM deletion_jobs WHERE merchant_id = ? AND deletion_job_id = ?")
|
|
447
|
+
.get(merchantId, jobId);
|
|
448
|
+
if (row === undefined) {
|
|
449
|
+
throw new ContextGovernanceError("access_denied", "deletion_job_not_found", "删除任务不存在或不可见");
|
|
450
|
+
}
|
|
451
|
+
return row;
|
|
452
|
+
}
|
|
453
|
+
readJob(jobId, merchantId) {
|
|
454
|
+
const row = (merchantId === undefined
|
|
455
|
+
? this.db.prepare("SELECT * FROM deletion_jobs WHERE deletion_job_id = ?").get(jobId)
|
|
456
|
+
: this.db
|
|
457
|
+
.prepare("SELECT * FROM deletion_jobs WHERE deletion_job_id = ? AND merchant_id = ?")
|
|
458
|
+
.get(jobId, merchantId));
|
|
459
|
+
if (row === undefined)
|
|
460
|
+
return undefined;
|
|
461
|
+
return jobToRecord(row, this.readSurfaces(row.deletion_job_id));
|
|
462
|
+
}
|
|
463
|
+
readOpenJob(merchantId, eraseScopeId) {
|
|
464
|
+
return this.db
|
|
465
|
+
.prepare(`SELECT * FROM deletion_jobs
|
|
466
|
+
WHERE merchant_id = ? AND erase_scope_id = ? AND state IN ('PENDING','PROCESSING')
|
|
467
|
+
ORDER BY created_at DESC LIMIT 1`)
|
|
468
|
+
.get(merchantId, eraseScopeId);
|
|
469
|
+
}
|
|
470
|
+
readSurfaces(jobId) {
|
|
471
|
+
return this.db
|
|
472
|
+
.prepare(`SELECT * FROM deletion_job_surfaces
|
|
473
|
+
WHERE deletion_job_id = ? ORDER BY surface ASC`)
|
|
474
|
+
.all(jobId);
|
|
475
|
+
}
|
|
476
|
+
readSurface(jobId, surface) {
|
|
477
|
+
return this.db
|
|
478
|
+
.prepare("SELECT * FROM deletion_job_surfaces WHERE deletion_job_id = ? AND surface = ?")
|
|
479
|
+
.get(jobId, surface);
|
|
480
|
+
}
|
|
481
|
+
readScope(merchantId, eraseScopeId) {
|
|
482
|
+
return this.db
|
|
483
|
+
.prepare("SELECT * FROM data_scopes WHERE merchant_id = ? AND erase_scope_id = ?")
|
|
484
|
+
.get(merchantId, eraseScopeId);
|
|
485
|
+
}
|
|
486
|
+
readSourceRow(merchantId, objectId, version) {
|
|
487
|
+
return this.db
|
|
488
|
+
.prepare("SELECT * FROM source_refs WHERE merchant_id = ? AND object_id = ? AND version = ?")
|
|
489
|
+
.get(merchantId, objectId, version);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
//# sourceMappingURL=source-governance.js.map
|