@capybara-hq/kernel 0.1.0-rc.130 → 0.1.0-rc.132
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/admin/account-erasures.d.ts +37 -0
- package/dist/admin/account-erasures.js +119 -0
- package/dist/admin/admin.controller.d.ts +35 -2
- package/dist/admin/admin.controller.js +75 -18
- package/dist/admin/admin.module.js +10 -0
- package/dist/admin/admin.schemas.d.ts +51 -0
- package/dist/admin/admin.schemas.js +20 -0
- package/dist/admin/erasure-dispositions.d.ts +23 -0
- package/dist/admin/erasure-dispositions.js +76 -0
- package/dist/admin/health.service.d.ts +6 -0
- package/dist/admin/health.service.js +27 -1
- package/dist/admin/usage.service.d.ts +5 -0
- package/dist/admin/usage.service.js +21 -2
- package/dist/agents/agents.service.d.ts +1 -1
- package/dist/catalog/plugin-import.d.ts +3 -1
- package/dist/catalog/plugin-import.js +19 -15
- package/dist/channels/connections.service.d.ts +1 -1
- package/dist/client/client-reports.service.js +12 -0
- package/dist/config/schema.d.ts +9 -1
- package/dist/config/schema.js +51 -1
- package/dist/conversations/conversations.service.d.ts +9 -1
- package/dist/conversations/conversations.service.js +112 -15
- package/dist/conversations/event-log.d.ts +2 -0
- package/dist/conversations/event-log.js +77 -0
- package/dist/conversations/events.d.ts +11 -0
- package/dist/conversations/events.js +13 -1
- package/dist/conversations/prompt-suggestions.js +4 -0
- package/dist/conversations/titles.js +3 -2
- package/dist/ctx/create-ctx.d.ts +3 -2
- package/dist/ctx/create-ctx.js +10 -7
- package/dist/db/extension-db.d.ts +30 -0
- package/dist/db/extension-db.js +91 -0
- package/dist/db/extension-schema-transfer.d.ts +6 -0
- package/dist/db/extension-schema-transfer.js +141 -0
- package/dist/db/kernel-schema.d.ts +73 -12
- package/dist/db/migrator.d.ts +6 -0
- package/dist/db/migrator.js +39 -4
- package/dist/db/pool.d.ts +4 -0
- package/dist/db/pool.js +36 -1
- package/dist/devices/devices.service.d.ts +13 -1
- package/dist/devices/devices.service.js +122 -9
- package/dist/erasure/account-batches.d.ts +9 -0
- package/dist/erasure/account-batches.js +567 -0
- package/dist/erasure/account-files.d.ts +34 -0
- package/dist/erasure/account-files.js +217 -0
- package/dist/erasure/account-residual.d.ts +15 -0
- package/dist/erasure/account-residual.js +149 -0
- package/dist/erasure/account-worker.d.ts +32 -0
- package/dist/erasure/account-worker.js +392 -0
- package/dist/erasure/device-cleanup.d.ts +23 -0
- package/dist/erasure/device-cleanup.js +221 -0
- package/dist/erasure/erasure.module.d.ts +2 -0
- package/dist/{conversations/event-org-backfill.module.js → erasure/erasure.module.js} +6 -10
- package/dist/erasure/external-evidence.d.ts +4 -0
- package/dist/erasure/external-evidence.js +28 -0
- package/dist/erasure/jobs.d.ts +5 -0
- package/dist/erasure/jobs.js +20 -0
- package/dist/erasure/org-batches.d.ts +11 -0
- package/dist/erasure/org-batches.js +159 -0
- package/dist/erasure/org-catalog.d.ts +2 -0
- package/dist/erasure/org-catalog.js +34 -0
- package/dist/erasure/org-rules.d.ts +14 -0
- package/dist/erasure/org-rules.js +227 -0
- package/dist/erasure/org-worker.d.ts +38 -0
- package/dist/erasure/org-worker.js +501 -0
- package/dist/erasure/requests.d.ts +61 -0
- package/dist/erasure/requests.js +180 -0
- package/dist/erasure/sweep.job.d.ts +19 -0
- package/dist/erasure/sweep.job.js +130 -0
- package/dist/erasure/worker.d.ts +17 -0
- package/dist/erasure/worker.js +110 -0
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js +4 -0
- package/dist/evals/judge.d.ts +5 -0
- package/dist/evals/judge.js +16 -1
- package/dist/evals/run-replay.js +12 -1
- package/dist/evals/step-replay.d.ts +5 -3
- package/dist/evals/step-replay.js +9 -5
- package/dist/evals/worker.d.ts +2 -0
- package/dist/evals/worker.js +60 -16
- package/dist/extensions/admin-ops.d.ts +3 -1
- package/dist/extensions/admin-ops.js +11 -7
- package/dist/extensions/extensions.module.js +3 -0
- package/dist/extensions/host.d.ts +4 -1
- package/dist/extensions/host.js +29 -6
- package/dist/extensions/instance.d.ts +3 -0
- package/dist/extensions/instance.js +3 -1
- package/dist/extensions/migrate-lock.d.ts +4 -1
- package/dist/extensions/migrate-lock.js +19 -2
- package/dist/files/blob-objects.d.ts +7 -3
- package/dist/files/blob-objects.js +39 -22
- package/dist/files/blob-sweep.job.d.ts +11 -0
- package/dist/files/blob-sweep.job.js +67 -6
- package/dist/http/api.module.js +5 -1
- package/dist/identity/accounts.js +3 -0
- package/dist/identity/api-tokens.js +5 -0
- package/dist/identity/auth.guard.d.ts +1 -0
- package/dist/identity/auth.guard.js +1 -0
- package/dist/identity/erasure-admission.d.ts +3 -0
- package/dist/identity/erasure-admission.js +14 -0
- package/dist/identity/external.js +5 -0
- package/dist/identity/sessions.js +5 -0
- package/dist/identity/settings.d.ts +86 -1
- package/dist/identity/settings.js +38 -4
- package/dist/infra/config/kernel-config.service.js +1 -0
- package/dist/infra/metrics/kernel-metrics.d.ts +18 -0
- package/dist/infra/metrics/kernel-metrics.js +54 -0
- package/dist/infra/metrics/operational-metrics.d.ts +31 -0
- package/dist/infra/metrics/operational-metrics.js +188 -0
- package/dist/infra/outbox/registry.d.ts +23 -14
- package/dist/infra/outbox/registry.js +12 -15
- package/dist/infra/throttler/credential-throttler.js +8 -1
- package/dist/infra/throttler/named-throttlers.d.ts +6 -0
- package/dist/infra/throttler/named-throttlers.js +6 -0
- package/dist/infra/throttler/principal-throttler.guard.d.ts +10 -0
- package/dist/infra/throttler/principal-throttler.guard.js +50 -0
- package/dist/infra/throttler/redis-throttler-storage.d.ts +1 -0
- package/dist/infra/throttler/redis-throttler-storage.js +27 -5
- package/dist/infra/throttler/throttler.guard.d.ts +2 -3
- package/dist/infra/throttler/throttler.guard.js +2 -3
- package/dist/infra/throttler/throttler.module.js +3 -2
- package/dist/kernel.js +4 -0
- package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
- package/dist/lifecycle/kernel-lifecycle.js +26 -21
- package/dist/models/budget-admission.d.ts +31 -0
- package/dist/models/budget-admission.js +149 -0
- package/dist/models/budget-counters.d.ts +28 -0
- package/dist/models/budget-counters.js +117 -0
- package/dist/models/budget-policy.d.ts +88 -0
- package/dist/models/budget-policy.js +78 -0
- package/dist/models/embeddings.js +13 -2
- package/dist/models/images.js +12 -2
- package/dist/models/purposes.d.ts +5 -2
- package/dist/models/purposes.js +9 -5
- package/dist/orgs/invitations.service.d.ts +1 -1
- package/dist/orgs/members.service.d.ts +28 -1
- package/dist/orgs/members.service.js +74 -11
- package/dist/orgs/offboarding.js +4 -6
- package/dist/orgs/org-owner.d.ts +2 -0
- package/dist/orgs/org-owner.js +21 -6
- package/dist/orgs/orgs.controller.d.ts +22 -0
- package/dist/orgs/orgs.controller.js +24 -0
- package/dist/orgs/orgs.module.js +4 -0
- package/dist/orgs/orgs.schemas.js +7 -0
- package/dist/orgs/orgs.service.d.ts +5 -1
- package/dist/orgs/orgs.service.js +28 -3
- package/dist/orgs/settings.controller.js +6 -1
- package/dist/orgs/settings.service.d.ts +58 -1
- package/dist/orgs/settings.service.js +74 -9
- package/dist/platform/exclusions.js +20 -1
- package/dist/platform/ops/accounts.ops.js +58 -1
- package/dist/platform/ops/admin.ops.js +13 -5
- package/dist/platform/ops/org-settings.ops.js +11 -4
- package/dist/platform/ops/orgs.ops.js +28 -0
- package/dist/platform/ops/retention.d.ts +16 -0
- package/dist/platform/ops/retention.js +21 -0
- package/dist/retention/jobs.d.ts +21 -0
- package/dist/retention/jobs.js +48 -0
- package/dist/retention/policy.d.ts +70 -0
- package/dist/retention/policy.js +103 -0
- package/dist/retention/purge-batch.d.ts +17 -0
- package/dist/retention/purge-batch.js +355 -0
- package/dist/retention/report-obligations.d.ts +12 -0
- package/dist/retention/report-obligations.js +71 -0
- package/dist/retention/sweep.job.d.ts +17 -0
- package/dist/retention/sweep.job.js +95 -0
- package/dist/retention/worker.d.ts +29 -0
- package/dist/retention/worker.js +306 -0
- package/dist/roles/worker.module.js +12 -8
- package/dist/runtime/maintenance.d.ts +5 -3
- package/dist/runtime/maintenance.js +19 -12
- package/dist/runtime/model-call.d.ts +3 -1
- package/dist/runtime/model-call.js +25 -3
- package/dist/runtime/providers.d.ts +7 -2
- package/dist/runtime/providers.js +76 -10
- package/dist/runtime/quota.d.ts +12 -12
- package/dist/runtime/quota.js +42 -37
- package/dist/runtime/run-finish.js +2 -0
- package/dist/runtime/run-principal.d.ts +9 -0
- package/dist/runtime/run-principal.js +18 -0
- package/dist/runtime/run-worker.js +48 -10
- package/dist/runtime/usage-report-jobs.d.ts +4 -1
- package/dist/runtime/usage-report-jobs.js +36 -13
- package/dist/runtime/usage-report.d.ts +5 -3
- package/dist/runtime/usage-report.js +37 -19
- package/dist/sandbox/review/classifier.d.ts +2 -2
- package/dist/sandbox/review/classifier.js +6 -2
- package/dist/sandbox/sandbox.service.js +18 -7
- package/dist/storage/purge.js +5 -1
- package/dist/storage/storage.service.d.ts +17 -1
- package/dist/storage/storage.service.js +75 -4
- package/dist/tools/builtins/vision/read-image.js +3 -0
- package/dist/tools/builtins/web/extract.js +3 -0
- package/dist/tools/pipeline.js +5 -1
- package/package.json +3 -1
- package/src/migrations/0216_model_budget_counters.sql +24 -0
- package/src/migrations/0217_retention.sql +44 -0
- package/src/migrations/0218_data_erasure.sql +41 -0
- package/src/migrations/0219_event_org_constraint_check.sql +29 -0
- package/src/migrations/0220_require_event_org.sql +18 -0
- package/src/migrations/0221_drop_connector_declaration_policy.sql +2 -0
- package/dist/conversations/event-org-backfill.d.ts +0 -111
- package/dist/conversations/event-org-backfill.js +0 -264
- package/dist/conversations/event-org-backfill.module.d.ts +0 -3
package/dist/evals/worker.d.ts
CHANGED
|
@@ -107,6 +107,8 @@ export declare class EvalWorker {
|
|
|
107
107
|
* - 回放出错 → 结果 error 并计入熔断;停机 → 任务回到队列,接手的执行从头重跑。
|
|
108
108
|
*/
|
|
109
109
|
private runWhole;
|
|
110
|
+
/** Step eval and judge calls have no conversation usage event. Settle them with eval usage. */
|
|
111
|
+
private consumeDirectUsage;
|
|
110
112
|
/**
|
|
111
113
|
* 写一条单步回放的结果:只在仍由本次执行认领时写结果(stalled 后被接手的旧执行不覆盖结果)。用量照样计入(请求确实
|
|
112
114
|
* 发出且已付费,与失败尝试一样不以认领为条件),走同一个原子 consumeTokens。
|
package/dist/evals/worker.js
CHANGED
|
@@ -24,6 +24,9 @@ import { asPlatform, withOrgScope } from "../infra/database/org-scope.js";
|
|
|
24
24
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
25
25
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
26
26
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
27
|
+
import { ModelBudgetRefused } from "../models/budget-admission.js";
|
|
28
|
+
import { recordSettledModelUsage } from "../models/budget-counters.js";
|
|
29
|
+
import { readPriceSnapshot } from "../models/price-snapshot.js";
|
|
27
30
|
import { ModelPurposesService, PurposeAttemptError } from "../models/purposes.js";
|
|
28
31
|
import { KyselyCheckpointSaver } from "../runtime/checkpointer.js";
|
|
29
32
|
import { evalDeniedAudit } from "../runtime/hook-calls.js";
|
|
@@ -101,6 +104,14 @@ export function replayLoopDeps(stepDeps, overrides) {
|
|
|
101
104
|
}
|
|
102
105
|
/** 回放出错的分类(与运行时的降级梯子同一套码)。 */
|
|
103
106
|
function classify(err) {
|
|
107
|
+
if (err instanceof ModelBudgetRefused || err instanceof RunError) {
|
|
108
|
+
return {
|
|
109
|
+
retryable: false,
|
|
110
|
+
code: err.code,
|
|
111
|
+
message: err.message,
|
|
112
|
+
...(err.budgetDetail && { budget: err.budgetDetail }),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
104
115
|
if (err instanceof ModelRequestDenied) {
|
|
105
116
|
return { retryable: false, code: err.code, message: err.message, denial: err.verdict };
|
|
106
117
|
}
|
|
@@ -115,9 +126,6 @@ function classify(err) {
|
|
|
115
126
|
if (err instanceof MediaUnavailableError) {
|
|
116
127
|
return { retryable: true, code: "run.storage_unavailable", message: err.message };
|
|
117
128
|
}
|
|
118
|
-
// 整轮回放的 loop 失败(降级梯子已在 loop 内重试过):沿用运行时的码
|
|
119
|
-
if (err instanceof RunError)
|
|
120
|
-
return { retryable: false, code: err.code, message: err.message };
|
|
121
129
|
// 供应商插件在请求途中被撤销 / 升级 / 重新加载:与运行时一致按可重试处理,重试时实时取到新实例
|
|
122
130
|
if (isPluginUnloaded(err) || err instanceof ProviderListError) {
|
|
123
131
|
return {
|
|
@@ -492,14 +500,15 @@ let EvalWorker = class EvalWorker {
|
|
|
492
500
|
return;
|
|
493
501
|
}
|
|
494
502
|
try {
|
|
495
|
-
out = await replayStep(this.d.replay, s, call, resolved, signal);
|
|
503
|
+
out = await replayStep(this.d.replay, s, call, resolved, signal, { evalRunId: runId });
|
|
496
504
|
break;
|
|
497
505
|
}
|
|
498
506
|
catch (err) {
|
|
499
507
|
// 已发出的请求没有正常收完:按估算计入预算(与成功的请求走同一个原子累加,越限同样置 capped)。
|
|
500
508
|
// 不以认领为条件:钱已经花出去了
|
|
501
|
-
if (err instanceof ReplayAttemptError)
|
|
502
|
-
await
|
|
509
|
+
if (err instanceof ReplayAttemptError) {
|
|
510
|
+
await this.consumeDirectUsage(data.orgId, runId, err.tokens, resolved.model, err.usage);
|
|
511
|
+
}
|
|
503
512
|
if (signal.aborted)
|
|
504
513
|
throw new Aborted();
|
|
505
514
|
const f = classify(err instanceof ReplayAttemptError ? err.cause : err);
|
|
@@ -514,7 +523,7 @@ let EvalWorker = class EvalWorker {
|
|
|
514
523
|
if (!out) {
|
|
515
524
|
const f = failure;
|
|
516
525
|
const entry = f.denial && evalDeniedAudit({ ...data, resultId: r.id, evaluation: "step" }, f.denial);
|
|
517
|
-
if (await this.resultError(data, r.id, claim, { code: f.code, message: f.message }, entry)) {
|
|
526
|
+
if (await this.resultError(data, r.id, claim, { code: f.code, message: f.message, ...(f.budget && { budget: f.budget }) }, entry)) {
|
|
518
527
|
return;
|
|
519
528
|
}
|
|
520
529
|
continue;
|
|
@@ -524,7 +533,7 @@ let EvalWorker = class EvalWorker {
|
|
|
524
533
|
const b = replyParts(result.baseline.content);
|
|
525
534
|
const c = replyParts(result.candidate.content);
|
|
526
535
|
const diff = structuralDiff({ ...b, stopReason: result.baseline.stopReason }, { ...c, stopReason: result.candidate.stopReason });
|
|
527
|
-
const outcome = await this.writeStepResultIn(
|
|
536
|
+
const outcome = await this.writeStepResultIn(data, r.id, claim, result, diff, grade, tokens);
|
|
528
537
|
// 失去认领:这个用例已由别的执行接手,本次停下
|
|
529
538
|
if (outcome === "lost")
|
|
530
539
|
return;
|
|
@@ -591,12 +600,14 @@ let EvalWorker = class EvalWorker {
|
|
|
591
600
|
denial = verdict;
|
|
592
601
|
},
|
|
593
602
|
maxOutputTokens: this.d.config.get().evals.judgeMaxOutputTokens,
|
|
603
|
+
budgetSite: { evalRunId: data.runId },
|
|
594
604
|
}, data.orgId, input, signal);
|
|
595
605
|
}
|
|
596
606
|
catch (err) {
|
|
597
607
|
// 中止时已发出的评委请求:按估算计费(与失败的回放尝试一致)
|
|
598
|
-
if (err instanceof PurposeAttemptError)
|
|
599
|
-
await
|
|
608
|
+
if (err instanceof PurposeAttemptError) {
|
|
609
|
+
await this.consumeDirectUsage(data.orgId, data.runId, err.tokens, err, err.usage);
|
|
610
|
+
}
|
|
600
611
|
if (signal.aborted)
|
|
601
612
|
throw new Aborted();
|
|
602
613
|
throw err;
|
|
@@ -607,7 +618,7 @@ let EvalWorker = class EvalWorker {
|
|
|
607
618
|
attempt >= EVAL_MAX_RETRIES) {
|
|
608
619
|
break;
|
|
609
620
|
}
|
|
610
|
-
const { capped } = await
|
|
621
|
+
const { capped } = await this.consumeDirectUsage(data.orgId, data.runId, v.tokens, v, v.usage);
|
|
611
622
|
if (capped || !(await runActive(db, data.runId))) {
|
|
612
623
|
await db
|
|
613
624
|
.updateTable("eval_results")
|
|
@@ -620,10 +631,12 @@ let EvalWorker = class EvalWorker {
|
|
|
620
631
|
await sleep(RETRY_DELAYS_MS[attempt] ?? 4000, signal);
|
|
621
632
|
}
|
|
622
633
|
const tokens = "error" in v ? v.tokens : usageTokens(v.usage);
|
|
623
|
-
const { capped } = await
|
|
634
|
+
const { capped } = await this.consumeDirectUsage(data.orgId, data.runId, tokens, v, v.usage);
|
|
624
635
|
const stored = "error" in v
|
|
625
636
|
? {
|
|
626
637
|
error: v.error,
|
|
638
|
+
...(v.message && { message: v.message }),
|
|
639
|
+
...(v.budget && { budget: v.budget }),
|
|
627
640
|
...(v.provider !== undefined && { provider: v.provider }),
|
|
628
641
|
...(v.model !== undefined && { model: v.model }),
|
|
629
642
|
}
|
|
@@ -720,7 +733,11 @@ let EvalWorker = class EvalWorker {
|
|
|
720
733
|
if (!isReplayFailure(err))
|
|
721
734
|
throw err;
|
|
722
735
|
const f = classify(err);
|
|
723
|
-
await this.resultError(data, resultId, claim, {
|
|
736
|
+
await this.resultError(data, resultId, claim, {
|
|
737
|
+
code: f.code,
|
|
738
|
+
message: f.message,
|
|
739
|
+
...(f.budget && { budget: f.budget }),
|
|
740
|
+
});
|
|
724
741
|
return;
|
|
725
742
|
}
|
|
726
743
|
if (signal.aborted)
|
|
@@ -761,11 +778,35 @@ let EvalWorker = class EvalWorker {
|
|
|
761
778
|
return;
|
|
762
779
|
await this.judgeResult(data, resultId, claim, signal, judgeInputOf(s, "run", null, baseline, out, grade.rubric));
|
|
763
780
|
}
|
|
781
|
+
/** Step eval and judge calls have no conversation usage event. Settle them with eval usage. */
|
|
782
|
+
async consumeDirectUsage(orgId, runId, tokens, ref, usage) {
|
|
783
|
+
const db = this.d.db;
|
|
784
|
+
const result = await consumeTokens(db, runId, tokens);
|
|
785
|
+
if (!usage || !ref.provider || !ref.model)
|
|
786
|
+
return result;
|
|
787
|
+
const run = await db
|
|
788
|
+
.selectFrom("eval_runs")
|
|
789
|
+
.select(["org_id", "created_by", sql `now()`.as("settled_at")])
|
|
790
|
+
.where("id", "=", runId)
|
|
791
|
+
.where("org_id", "=", orgId)
|
|
792
|
+
.executeTakeFirst();
|
|
793
|
+
if (run) {
|
|
794
|
+
await recordSettledModelUsage(db, {
|
|
795
|
+
orgId: run.org_id,
|
|
796
|
+
accountId: run.created_by,
|
|
797
|
+
usageSourceId: runId,
|
|
798
|
+
at: run.settled_at,
|
|
799
|
+
usage,
|
|
800
|
+
snapshot: await readPriceSnapshot(db, { provider: ref.provider, model: ref.model }),
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
return result;
|
|
804
|
+
}
|
|
764
805
|
/**
|
|
765
806
|
* 写一条单步回放的结果:只在仍由本次执行认领时写结果(stalled 后被接手的旧执行不覆盖结果)。用量照样计入(请求确实
|
|
766
807
|
* 发出且已付费,与失败尝试一样不以认领为条件),走同一个原子 consumeTokens。
|
|
767
808
|
*/
|
|
768
|
-
async writeStepResultIn(runId, resultId, claim, result, diff, grade, tokens) {
|
|
809
|
+
async writeStepResultIn({ orgId, runId }, resultId, claim, result, diff, grade, tokens) {
|
|
769
810
|
const db = this.d.db;
|
|
770
811
|
const w = await db
|
|
771
812
|
.updateTable("eval_results")
|
|
@@ -785,11 +826,11 @@ let EvalWorker = class EvalWorker {
|
|
|
785
826
|
.where("claimed_by", "=", claim)
|
|
786
827
|
.executeTakeFirst();
|
|
787
828
|
if (Number(w.numUpdatedRows) === 0) {
|
|
788
|
-
await
|
|
829
|
+
await this.consumeDirectUsage(orgId, runId, tokens, result.candidate, result.candidate.usage);
|
|
789
830
|
return "lost";
|
|
790
831
|
}
|
|
791
832
|
await recordOutcome(db, runId, true, this.d.config.get().evals.maxConsecutiveErrors);
|
|
792
|
-
const { capped } = await
|
|
833
|
+
const { capped } = await this.consumeDirectUsage(orgId, runId, tokens, result.candidate, result.candidate.usage);
|
|
793
834
|
return capped ? "capped" : "ok";
|
|
794
835
|
}
|
|
795
836
|
/** 写一条整轮回放的结果(仍由本次执行认领时);false = 已失去认领。 */
|
|
@@ -913,6 +954,9 @@ __decorate([
|
|
|
913
954
|
__decorate([
|
|
914
955
|
Transactional({ propagation: "requires_new" })
|
|
915
956
|
], EvalWorker.prototype, "writeJudgeResultIn", null);
|
|
957
|
+
__decorate([
|
|
958
|
+
Transactional()
|
|
959
|
+
], EvalWorker.prototype, "consumeDirectUsage", null);
|
|
916
960
|
__decorate([
|
|
917
961
|
Transactional({ propagation: "requires_new" })
|
|
918
962
|
], EvalWorker.prototype, "writeStepResultIn", null);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ExtensionDbIdentity } from "../db/extension-db.js";
|
|
1
2
|
import { ApiError } from "../errors.js";
|
|
2
3
|
import { SuperAdminLock } from "../identity/super-admin.js";
|
|
3
4
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
@@ -64,6 +65,7 @@ export declare class ExtensionAdminOps {
|
|
|
64
65
|
/** Reads packages (a downgrade checks the target version's migration files). */
|
|
65
66
|
private readonly blobs;
|
|
66
67
|
private readonly config;
|
|
68
|
+
private readonly extensionDbIdentity;
|
|
67
69
|
/** Publishes `kernel.plugins.changed`. */
|
|
68
70
|
private readonly bus;
|
|
69
71
|
/**
|
|
@@ -91,7 +93,7 @@ export declare class ExtensionAdminOps {
|
|
|
91
93
|
/** The config-declared extension names (they cannot be installed / disabled / uninstalled, C15). */
|
|
92
94
|
registry: ExtensionRegistry,
|
|
93
95
|
/** Reads packages (a downgrade checks the target version's migration files). */
|
|
94
|
-
blobs: ExtensionBlobs, config: KernelConfigService,
|
|
96
|
+
blobs: ExtensionBlobs, config: KernelConfigService, extensionDbIdentity: ExtensionDbIdentity,
|
|
95
97
|
/** Publishes `kernel.plugins.changed`. */
|
|
96
98
|
bus: PublishBus,
|
|
97
99
|
/**
|
|
@@ -14,6 +14,7 @@ import semver from "semver";
|
|
|
14
14
|
import { itemRemovedError } from "../catalog/ownership.js";
|
|
15
15
|
import { oweDeployedExtensionSync } from "../catalog/scope-jobs.js";
|
|
16
16
|
import { pluginSchemaName } from "../ctx/create-ctx.js";
|
|
17
|
+
import { ConfiguredExtensionDbIdentity } from "../db/extension-db.js";
|
|
17
18
|
import { revokeSiteGrantsOfExtensionEverywhere } from "../device-access/site-grants.js";
|
|
18
19
|
import { ApiError } from "../errors.js";
|
|
19
20
|
import { audit } from "../identity/audit.js";
|
|
@@ -45,6 +46,7 @@ let ExtensionAdminOps = class ExtensionAdminOps {
|
|
|
45
46
|
registry;
|
|
46
47
|
blobs;
|
|
47
48
|
config;
|
|
49
|
+
extensionDbIdentity;
|
|
48
50
|
bus;
|
|
49
51
|
lifecycle;
|
|
50
52
|
log;
|
|
@@ -65,7 +67,7 @@ let ExtensionAdminOps = class ExtensionAdminOps {
|
|
|
65
67
|
/** The config-declared extension names (they cannot be installed / disabled / uninstalled, C15). */
|
|
66
68
|
registry,
|
|
67
69
|
/** Reads packages (a downgrade checks the target version's migration files). */
|
|
68
|
-
blobs, config,
|
|
70
|
+
blobs, config, extensionDbIdentity,
|
|
69
71
|
/** Publishes `kernel.plugins.changed`. */
|
|
70
72
|
bus,
|
|
71
73
|
/**
|
|
@@ -86,6 +88,7 @@ let ExtensionAdminOps = class ExtensionAdminOps {
|
|
|
86
88
|
this.registry = registry;
|
|
87
89
|
this.blobs = blobs;
|
|
88
90
|
this.config = config;
|
|
91
|
+
this.extensionDbIdentity = extensionDbIdentity;
|
|
89
92
|
this.bus = bus;
|
|
90
93
|
this.lifecycle = lifecycle;
|
|
91
94
|
this.log = log;
|
|
@@ -237,7 +240,7 @@ let ExtensionAdminOps = class ExtensionAdminOps {
|
|
|
237
240
|
return { changed: false, state: row.desired_state };
|
|
238
241
|
}
|
|
239
242
|
if (row && isDowngrade(pkg.version, row.version)) {
|
|
240
|
-
const unrun = await unrunMigrations(db, name, target.migrationSql);
|
|
243
|
+
const unrun = await unrunMigrations(db, name, target.migrationSql, this.extensionDbIdentity.resolve(name), this.config.get().db.connectTimeoutMs);
|
|
241
244
|
if (unrun.length > 0) {
|
|
242
245
|
throw new ApiError("extension.downgrade_unsupported", `Extension ${name} ${pkg.version} has migrations that were never run here (${unrun.join(", ")}); extension migrations only expand, so downgrade only to a version whose migrations have all been applied`, unrun.map((file) => ({ file })));
|
|
243
246
|
}
|
|
@@ -521,11 +524,12 @@ ExtensionAdminOps = __decorate([
|
|
|
521
524
|
__param(6, Inject(ExtensionRegistry)),
|
|
522
525
|
__param(7, Inject(ExtensionBlobs)),
|
|
523
526
|
__param(8, Inject(KernelConfigService)),
|
|
524
|
-
__param(9, Inject(
|
|
525
|
-
__param(10, Inject(
|
|
526
|
-
__param(11, Inject(
|
|
527
|
-
__param(12, Inject(
|
|
528
|
-
__param(13, Inject(
|
|
527
|
+
__param(9, Inject(ConfiguredExtensionDbIdentity)),
|
|
528
|
+
__param(10, Inject(KERNEL_BUS)),
|
|
529
|
+
__param(11, Inject(ExtensionLifecycle)),
|
|
530
|
+
__param(12, Inject(KERNEL_LOGGER)),
|
|
531
|
+
__param(13, Inject(SingleProviderGuard)),
|
|
532
|
+
__param(14, Inject(OutboxService))
|
|
529
533
|
], ExtensionAdminOps);
|
|
530
534
|
export { ExtensionAdminOps };
|
|
531
535
|
/**
|
|
@@ -5,6 +5,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
};
|
|
7
7
|
import { Module } from "@nestjs/common";
|
|
8
|
+
import { ConfiguredExtensionDbIdentity } from "../db/extension-db.js";
|
|
8
9
|
import { DialInRelayModule } from "../dial-in/relay.module.js";
|
|
9
10
|
import { SuperAdminLockModule } from "../identity/super-admin.js";
|
|
10
11
|
import { ModelsModule } from "../models/models.module.js";
|
|
@@ -71,6 +72,7 @@ ExtensionsModule = __decorate([
|
|
|
71
72
|
SingleProviderLock,
|
|
72
73
|
SingleProviderGuard,
|
|
73
74
|
ReplicaStatusStore,
|
|
75
|
+
ConfiguredExtensionDbIdentity,
|
|
74
76
|
ExtensionHost,
|
|
75
77
|
ExtensionLifecycle,
|
|
76
78
|
ExtensionAdmin,
|
|
@@ -91,6 +93,7 @@ ExtensionsModule = __decorate([
|
|
|
91
93
|
PluginInstallLock,
|
|
92
94
|
PluginApproveLock,
|
|
93
95
|
ReplicaStatusStore,
|
|
96
|
+
ConfiguredExtensionDbIdentity,
|
|
94
97
|
ExtensionHost,
|
|
95
98
|
ExtensionLifecycle,
|
|
96
99
|
ExtensionAdmin,
|
|
@@ -7,6 +7,7 @@ import type { BusSubscriber } from "../ctx/bus.js";
|
|
|
7
7
|
import { type DeviceAccessBackend, type InboxBackend, type PluginReadsBackend, type Route, type TriggersBackend } from "../ctx/create-ctx.js";
|
|
8
8
|
import { type ExtensionNetSeams } from "../ctx/net.js";
|
|
9
9
|
import type { Keyring } from "../ctx/secrets.js";
|
|
10
|
+
import { type ExtensionDbIdentity } from "../db/extension-db.js";
|
|
10
11
|
import { DEVICE_ACCESS_BACKEND } from "../device-access/token.js";
|
|
11
12
|
import { DialInAccess } from "../dial-in/access.js";
|
|
12
13
|
import type { PluginRouteTable } from "../http/plugin-dispatch.js";
|
|
@@ -133,6 +134,7 @@ export declare class ExtensionHost {
|
|
|
133
134
|
* org's point resolution (layering spec A §6).
|
|
134
135
|
*/
|
|
135
136
|
private readonly effective;
|
|
137
|
+
private readonly dbIdentity;
|
|
136
138
|
/**
|
|
137
139
|
* Only resolves extensions and runs migrations in dependency order: no deployment config check, no
|
|
138
140
|
* setup, no Redis (for `capybara migrate`). Like `load()` it checks secret markers first
|
|
@@ -145,6 +147,7 @@ export declare class ExtensionHost {
|
|
|
145
147
|
*/
|
|
146
148
|
static migrateOnly(specifiers: string[], opts: {
|
|
147
149
|
dbUrl: string;
|
|
150
|
+
extensionIdentity?: ExtensionDbIdentity;
|
|
148
151
|
resolveFrom?: string;
|
|
149
152
|
connectTimeoutMs?: number;
|
|
150
153
|
autoApprove?: boolean;
|
|
@@ -312,7 +315,7 @@ export declare class ExtensionHost {
|
|
|
312
315
|
}): PluginReadsBackend;
|
|
313
316
|
},
|
|
314
317
|
/** Tests only: `ctx.net`'s DNS and dispatcher seams (`EXTENSION_NET_SEAMS`). */
|
|
315
|
-
netSeams?: ExtensionNetSeams);
|
|
318
|
+
netSeams?: ExtensionNetSeams, dbIdentity?: ExtensionDbIdentity);
|
|
316
319
|
/** The startup config's `extensionConfig` (file / Nacos merged with env). */
|
|
317
320
|
private get extensionConfig();
|
|
318
321
|
/** `extensionPermissions.autoApprove` (development and trials only; see `planApprovals`). */
|
package/dist/extensions/host.js
CHANGED
|
@@ -18,6 +18,8 @@ import { TRIGGER_EVENTS_BACKEND } from "../automations/events/token.js";
|
|
|
18
18
|
import { changedPaths, configFields, getPath, secretValues, unreachableSecrets, } from "../config/fields.js";
|
|
19
19
|
import { pluginSchemaName, queryEnabledPlugins, } from "../ctx/create-ctx.js";
|
|
20
20
|
import { EXTENSION_NET_SEAMS } from "../ctx/net.js";
|
|
21
|
+
import { ConfiguredExtensionDbIdentity, extensionDbIdentity, extensionDbPool, } from "../db/extension-db.js";
|
|
22
|
+
import { transferKnownExtensionSchemas } from "../db/extension-schema-transfer.js";
|
|
21
23
|
import { kernelDb } from "../db/kernel-schema.js";
|
|
22
24
|
import { migrate } from "../db/migrator.js";
|
|
23
25
|
import { createDb } from "../db/pool.js";
|
|
@@ -181,12 +183,16 @@ function warnPending(log, p) {
|
|
|
181
183
|
* 无迁移时返回 undefined。迁移器在会话上持有按 schema 的咨询锁:多副本同时启动时只有一个在执行,其余等它结束后
|
|
182
184
|
* 发现都已应用。
|
|
183
185
|
*/
|
|
184
|
-
async function migratePlugin(
|
|
186
|
+
async function migratePlugin(connection, manifest, migrationsDir, connectTimeoutMs, signal) {
|
|
185
187
|
if (!migrationsDir)
|
|
186
188
|
return undefined;
|
|
187
189
|
const schema = pluginSchemaName(manifest.name);
|
|
188
190
|
try {
|
|
189
|
-
const { applied } = await migrate(
|
|
191
|
+
const { applied } = await migrate(connection.url, { schema, dir: migrationsDir }, {
|
|
192
|
+
connectTimeoutMs,
|
|
193
|
+
signal,
|
|
194
|
+
extensionLogin: connection.separate ? connection.login : undefined,
|
|
195
|
+
});
|
|
190
196
|
return { schema, applied };
|
|
191
197
|
}
|
|
192
198
|
catch (e) {
|
|
@@ -237,6 +243,7 @@ let ExtensionHost = class ExtensionHost {
|
|
|
237
243
|
config;
|
|
238
244
|
log;
|
|
239
245
|
effective;
|
|
246
|
+
dbIdentity;
|
|
240
247
|
/**
|
|
241
248
|
* Only resolves extensions and runs migrations in dependency order: no deployment config check, no
|
|
242
249
|
* setup, no Redis (for `capybara migrate`). Like `load()` it checks secret markers first
|
|
@@ -254,6 +261,10 @@ let ExtensionHost = class ExtensionHost {
|
|
|
254
261
|
const ordered = resolveExtensions(entries.map((e) => e.manifest), SDK_VERSION);
|
|
255
262
|
for (const manifest of ordered)
|
|
256
263
|
assertSecretsReachable(manifest);
|
|
264
|
+
const identity = opts.extensionIdentity ?? extensionDbIdentity({ url: opts.dbUrl });
|
|
265
|
+
if (opts.extensionIdentity) {
|
|
266
|
+
await transferKnownExtensionSchemas(opts.dbUrl, identity, dirs.keys(), opts.connectTimeoutMs);
|
|
267
|
+
}
|
|
257
268
|
let toMigrate = ordered;
|
|
258
269
|
if (!opts.autoApprove && ordered.length > 0) {
|
|
259
270
|
const sets = await permissionSets(ordered, dirs);
|
|
@@ -273,7 +284,7 @@ let ExtensionHost = class ExtensionHost {
|
|
|
273
284
|
}
|
|
274
285
|
const out = [];
|
|
275
286
|
for (const manifest of toMigrate) {
|
|
276
|
-
const r = await migratePlugin(
|
|
287
|
+
const r = await migratePlugin(identity.resolve(manifest.name), manifest, migrationsDirOf(manifest, dirs.get(manifest.name)), opts.connectTimeoutMs);
|
|
277
288
|
if (r)
|
|
278
289
|
out.push({ plugin: manifest.name, ...r });
|
|
279
290
|
}
|
|
@@ -443,7 +454,7 @@ let ExtensionHost = class ExtensionHost {
|
|
|
443
454
|
*/
|
|
444
455
|
modules,
|
|
445
456
|
/** Tests only: `ctx.net`'s DNS and dispatcher seams (`EXTENSION_NET_SEAMS`). */
|
|
446
|
-
netSeams) {
|
|
457
|
+
netSeams, dbIdentity = extensionDbIdentity(config.get().db)) {
|
|
447
458
|
this.registry = registry;
|
|
448
459
|
this.installs = installs;
|
|
449
460
|
this.approvals = approvals;
|
|
@@ -451,6 +462,7 @@ let ExtensionHost = class ExtensionHost {
|
|
|
451
462
|
this.config = config;
|
|
452
463
|
this.log = log;
|
|
453
464
|
this.effective = effective;
|
|
465
|
+
this.dbIdentity = dbIdentity;
|
|
454
466
|
const c = config.get();
|
|
455
467
|
this.hooks = new HookRegistry({
|
|
456
468
|
instances: () => this.registry.all(),
|
|
@@ -1410,7 +1422,7 @@ let ExtensionHost = class ExtensionHost {
|
|
|
1410
1422
|
// 安装记录:一个副本持锁迁移(C16),别的副本正在迁移时待定、旧实例照常;配置声明:按 schema 锁直接迁移
|
|
1411
1423
|
const migrating = a.migrate
|
|
1412
1424
|
? a.migrate()
|
|
1413
|
-
: migratePlugin(this.
|
|
1425
|
+
: migratePlugin(this.dbIdentity.resolve(name), manifest, a.migrationsDir, this.deps.connectTimeoutMs, this.abort.signal);
|
|
1414
1426
|
this.migrating = migrating;
|
|
1415
1427
|
try {
|
|
1416
1428
|
if ((await migrating) === "busy") {
|
|
@@ -1783,6 +1795,11 @@ let ExtensionHost = class ExtensionHost {
|
|
|
1783
1795
|
warnUpgrades(this.log, this.imports);
|
|
1784
1796
|
}
|
|
1785
1797
|
let ctx;
|
|
1798
|
+
const name = spec.manifest.name;
|
|
1799
|
+
const identity = this.dbIdentity.resolve(name);
|
|
1800
|
+
const extensionDb = identity.separate
|
|
1801
|
+
? extensionDbPool(identity, name, this.config.get().db, (err) => this.log.warn("extension database pool error", { plugin: name, err }))
|
|
1802
|
+
: undefined;
|
|
1786
1803
|
const p = setupInstance(this.loadDeps, spec.manifest, {
|
|
1787
1804
|
entryUrl: spec.entryUrl,
|
|
1788
1805
|
source: spec.source,
|
|
@@ -1790,12 +1807,17 @@ let ExtensionHost = class ExtensionHost {
|
|
|
1790
1807
|
config: spec.config,
|
|
1791
1808
|
configHash: spec.configHash,
|
|
1792
1809
|
permissions: spec.permissions,
|
|
1810
|
+
...(extensionDb && { extensionDb }),
|
|
1793
1811
|
onCtx: (c) => {
|
|
1794
1812
|
if (c)
|
|
1795
1813
|
ctx = c;
|
|
1796
1814
|
if (c || this.settingUp === ctx)
|
|
1797
1815
|
this.settingUp = c;
|
|
1798
1816
|
},
|
|
1817
|
+
}).catch(async (error) => {
|
|
1818
|
+
if (!ctx)
|
|
1819
|
+
await extensionDb?.destroy();
|
|
1820
|
+
throw error;
|
|
1799
1821
|
});
|
|
1800
1822
|
if (timeoutMs === undefined)
|
|
1801
1823
|
return p.then((inst) => this.logUiResolution(inst));
|
|
@@ -2476,6 +2498,7 @@ ExtensionHost = __decorate([
|
|
|
2476
2498
|
__param(20, Optional()),
|
|
2477
2499
|
__param(20, Inject(ModuleRef)),
|
|
2478
2500
|
__param(21, Optional()),
|
|
2479
|
-
__param(21, Inject(EXTENSION_NET_SEAMS))
|
|
2501
|
+
__param(21, Inject(EXTENSION_NET_SEAMS)),
|
|
2502
|
+
__param(22, Inject(ConfiguredExtensionDbIdentity))
|
|
2480
2503
|
], ExtensionHost);
|
|
2481
2504
|
export { ExtensionHost };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type DroppedUiContribution, type ExtensionManifest, type ExtensionPointRef, type ResolvedUiContribution, type StaticExtensionManifest } from "@capybara-hq/plugin-sdk";
|
|
2
2
|
import { type ExtensionPointAccess, type KernelDeps, type KernelExtensionContext, LiveConfig, type RegisteredSetupCheck, type RegisteredTool, type Route } from "../ctx/create-ctx.js";
|
|
3
|
+
import type { extensionDbPool } from "../db/extension-db.js";
|
|
3
4
|
import type { UiActionHandler, UiQueryHandler, UiRender } from "../ui/registry.js";
|
|
4
5
|
import type { RegisteredHook } from "./hooks.js";
|
|
5
6
|
import { InflightTracker } from "./inflight.js";
|
|
@@ -159,6 +160,7 @@ export declare function setupInstance(deps: LoadDeps, manifest: ExtensionManifes
|
|
|
159
160
|
permissions: PermissionSet;
|
|
160
161
|
/** 已建好 ctx、setup 进行中时回调(宿主据此在 close() 时立即关闭它)。 */
|
|
161
162
|
onCtx?: (ctx: KernelExtensionContext | undefined) => void;
|
|
163
|
+
extensionDb?: ReturnType<typeof extensionDbPool>;
|
|
162
164
|
}): Promise<ExtensionInstance>;
|
|
163
165
|
/**
|
|
164
166
|
* 加载一个插件实例:import → defineExtension → 有包内静态清单(`section`)时与 {@link staticManifestOf} 核对
|
|
@@ -179,4 +181,5 @@ export declare function loadInstance(deps: LoadDeps, input: {
|
|
|
179
181
|
manifest: StaticExtensionManifest;
|
|
180
182
|
};
|
|
181
183
|
onCtx?: (ctx: KernelExtensionContext | undefined) => void;
|
|
184
|
+
extensionDb?: ReturnType<typeof extensionDbPool>;
|
|
182
185
|
}): Promise<ExtensionInstance>;
|
|
@@ -2,6 +2,7 @@ import { dirname } from "node:path";
|
|
|
2
2
|
import { fileURLToPath } from "node:url";
|
|
3
3
|
import { defineExtension, ExtensionManifestError, staticManifestOf, } from "@capybara-hq/plugin-sdk";
|
|
4
4
|
import { createExtensionContext, LiveConfig, } from "../ctx/create-ctx.js";
|
|
5
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
5
6
|
import { InflightTracker } from "./inflight.js";
|
|
6
7
|
const errMsg = (e) => (e instanceof Error ? e.message : String(e));
|
|
7
8
|
/** 模块清单与审阅过的静态清单(包内 `capybara.manifest`)不一致。 */
|
|
@@ -299,10 +300,11 @@ export async function setupInstance(deps, manifest, input) {
|
|
|
299
300
|
const live = new LiveConfig(input.config);
|
|
300
301
|
const ctx = createExtensionContext(deps.kernel, manifest, live, deps.extensionPointAccess, {
|
|
301
302
|
sha256: input.sha256,
|
|
303
|
+
...(input.extensionDb && { db: input.extensionDb }),
|
|
302
304
|
});
|
|
303
305
|
input.onCtx?.(ctx);
|
|
304
306
|
try {
|
|
305
|
-
await manifest.setup(ctx);
|
|
307
|
+
await asPlatform("initialize an extension on this replica", () => manifest.setup(ctx));
|
|
306
308
|
ctx.sealTools();
|
|
307
309
|
for (const cap of manifest.provides) {
|
|
308
310
|
if (!ctx.provided.has(cap)) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ExtensionDbConnection } from "../db/extension-db.js";
|
|
1
2
|
import type { KernelSchemaDb } from "../db/kernel-schema.js";
|
|
2
3
|
/** 插件迁移完成(kernel bus 主题,频道 `cap:kernel:plugins.migrated`,payload `{ plugin, sha256 }`)。 */
|
|
3
4
|
export declare const PLUGINS_MIGRATED_TOPIC = "plugins.migrated";
|
|
@@ -8,6 +9,8 @@ export interface MigrateLockDeps {
|
|
|
8
9
|
db: KernelSchemaDb;
|
|
9
10
|
/** 迁移用独立连接(不走连接池)。 */
|
|
10
11
|
dbUrl: string;
|
|
12
|
+
/** The schema migrations authenticate independently of the install record lock. */
|
|
13
|
+
extensionDb?: ExtensionDbConnection;
|
|
11
14
|
connectTimeoutMs?: number | undefined;
|
|
12
15
|
/** 发布 `plugins.migrated`(kernel bus)。 */
|
|
13
16
|
bus: {
|
|
@@ -47,4 +50,4 @@ export declare function ensureMigrated(d: MigrateLockDeps, name: string, sha256:
|
|
|
47
50
|
export declare function unrunMigrations(db: KernelSchemaDb, name: string, files: {
|
|
48
51
|
file: string;
|
|
49
52
|
sql: string;
|
|
50
|
-
}[]): Promise<string[]>;
|
|
53
|
+
}[], identity?: ExtensionDbConnection, connectTimeoutMs?: number): Promise<string[]>;
|
|
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { sql } from "kysely";
|
|
3
3
|
import pg from "pg";
|
|
4
4
|
import { pluginSchemaName } from "../ctx/create-ctx.js";
|
|
5
|
+
import { extensionDbPool } from "../db/extension-db.js";
|
|
5
6
|
import { migrate } from "../db/migrator.js";
|
|
6
7
|
import { pgConnectOptions } from "../db/pool.js";
|
|
7
8
|
/** 插件迁移完成(kernel bus 主题,频道 `cap:kernel:plugins.migrated`,payload `{ plugin, sha256 }`)。 */
|
|
@@ -42,9 +43,10 @@ export async function ensureMigrated(d, name, sha256, dir) {
|
|
|
42
43
|
const timeout = d.timeoutMs === undefined ? undefined : AbortSignal.timeout(d.timeoutMs);
|
|
43
44
|
const signals = [d.signal, timeout].filter((x) => x !== undefined);
|
|
44
45
|
try {
|
|
45
|
-
await migrate(d.dbUrl, { schema: pluginSchemaName(name), dir }, {
|
|
46
|
+
await migrate(d.extensionDb?.url ?? d.dbUrl, { schema: pluginSchemaName(name), dir }, {
|
|
46
47
|
connectTimeoutMs: d.connectTimeoutMs,
|
|
47
48
|
signal: signals.length > 0 ? AbortSignal.any(signals) : undefined,
|
|
49
|
+
extensionLogin: d.extensionDb?.separate ? d.extensionDb.login : undefined,
|
|
48
50
|
...(d.timeoutMs !== undefined && {
|
|
49
51
|
statementTimeoutMs: d.timeoutMs,
|
|
50
52
|
lockTimeoutMs: Math.min(d.timeoutMs, MIGRATION_LOCK_TIMEOUT_MS),
|
|
@@ -83,9 +85,24 @@ export async function ensureMigrated(d, name, sha256, dir) {
|
|
|
83
85
|
* 按插件 schema 的 `_migrations` 表比对文件名与 checksum(与迁移器相同的 sha256);改过内容的也算未执行。
|
|
84
86
|
* 插件从没迁移过时返回全部文件。
|
|
85
87
|
*/
|
|
86
|
-
export async function unrunMigrations(db, name, files) {
|
|
88
|
+
export async function unrunMigrations(db, name, files, identity, connectTimeoutMs) {
|
|
87
89
|
if (files.length === 0)
|
|
88
90
|
return [];
|
|
91
|
+
if (!identity?.separate)
|
|
92
|
+
return readUnrunMigrations(db, name, files);
|
|
93
|
+
const pool = extensionDbPool(identity, name, {
|
|
94
|
+
rls: "off",
|
|
95
|
+
connectTimeoutMs,
|
|
96
|
+
extensionPoolMax: 1,
|
|
97
|
+
});
|
|
98
|
+
try {
|
|
99
|
+
return await readUnrunMigrations(pool, name, files);
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
await pool.destroy();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async function readUnrunMigrations(db, name, files) {
|
|
89
106
|
const schema = pluginSchemaName(name);
|
|
90
107
|
const exists = await sql `SELECT to_regclass(${`"${schema}"._migrations`})::text AS t`.execute(db);
|
|
91
108
|
const done = new Map();
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { ObjectStore } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
1
3
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
2
4
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
3
5
|
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
@@ -90,9 +92,10 @@ export declare class BlobObjects {
|
|
|
90
92
|
private readonly db;
|
|
91
93
|
readonly txHost: TransactionHost;
|
|
92
94
|
private readonly storage;
|
|
95
|
+
private readonly config;
|
|
93
96
|
private readonly outbox;
|
|
94
97
|
private readonly log;
|
|
95
|
-
constructor(db: KernelDb, txHost: TransactionHost, storage: Pick<StorageService, "kernel">, outbox: Pick<OutboxService, "add">, log: Pick<KernelLogger, "warn">);
|
|
98
|
+
constructor(db: KernelDb, txHost: TransactionHost, storage: Pick<StorageService, "kernel" | "verifyErasureObjects">, config: Pick<KernelConfigService, "get">, outbox: Pick<OutboxService, "add">, log: Pick<KernelLogger, "warn">);
|
|
96
99
|
/**
|
|
97
100
|
* Releases what a dropped (`deleted`) upload held: without a blob, its staging object (as
|
|
98
101
|
* {@link deleteStaging} does); with one, the blob when no other file or share points at it,
|
|
@@ -107,9 +110,10 @@ export declare class BlobObjects {
|
|
|
107
110
|
*/
|
|
108
111
|
releaseBlob(data: FileReleaseData): Promise<string | null>;
|
|
109
112
|
/** Deletes one staging object; see the class comment. */
|
|
110
|
-
deleteStaging(data: FileStagingDeleteData): Promise<void>;
|
|
113
|
+
deleteStaging(data: FileStagingDeleteData, store?: Pick<ObjectStore, "delete">): Promise<void>;
|
|
111
114
|
/** Deletes one `deleting` blob, its object first; see the class comment. */
|
|
112
|
-
deleteBlob(data: BlobDeleteData): Promise<void>;
|
|
115
|
+
deleteBlob(data: BlobDeleteData, store?: Pick<ObjectStore, "delete">): Promise<void>;
|
|
116
|
+
private deleteObject;
|
|
113
117
|
/**
|
|
114
118
|
* Under `FOR UPDATE` on the blob: when it is `deleting` (or `gone`: a retry) and nothing points
|
|
115
119
|
* at it, sets it `gone` and returns its object key; else null (logged when it is still
|