@capybara-hq/kernel 0.1.0-rc.80 → 0.1.0-rc.81
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/outbox-dead.service.js +133 -120
- package/dist/background/store.js +78 -67
- package/dist/background/tasks.service.d.ts +6 -0
- package/dist/background/tasks.service.js +3 -0
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/plugin-import.js +45 -40
- package/dist/catalog/scope-sync-workers.js +3 -0
- package/dist/channels/inbound.d.ts +6 -1
- package/dist/channels/inbound.js +18 -1
- package/dist/client/client-reports.service.js +9 -6
- package/dist/config/schema.d.ts +5 -0
- package/dist/config/schema.js +6 -0
- package/dist/conversations/attachments.service.js +101 -96
- package/dist/conversations/events.d.ts +7 -0
- package/dist/conversations/events.js +9 -0
- package/dist/ctx/bus.js +5 -3
- package/dist/ctx/queue.js +4 -2
- package/dist/db/pool.d.ts +6 -0
- package/dist/db/pool.js +3 -2
- package/dist/devices/legacy-cleanup.job.js +73 -70
- package/dist/dial-in/gateway.d.ts +8 -0
- package/dist/dial-in/gateway.js +22 -2
- package/dist/extensions/admin-ops.js +22 -19
- package/dist/extensions/lifecycle-hooks.js +3 -1
- package/dist/http/app.js +3 -1
- package/dist/http/plugin-dispatch.js +8 -0
- package/dist/identity/auth.guard.js +13 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/infra/database/database.module.js +1 -0
- package/dist/infra/database/org-scope.d.ts +59 -0
- package/dist/infra/database/org-scope.js +68 -0
- package/dist/infra/database/rls-role.d.ts +11 -0
- package/dist/infra/database/rls-role.js +33 -0
- package/dist/infra/database/scoped-dialect.d.ts +18 -0
- package/dist/infra/database/scoped-dialect.js +245 -0
- package/dist/infra/outbox/outbox.dispatcher.js +98 -90
- package/dist/infra/outbox/outbox.service.js +29 -26
- package/dist/infra/queue/run-job.d.ts +8 -2
- package/dist/infra/queue/run-job.js +16 -3
- package/dist/infra/schedule/locked-tick.js +12 -4
- package/dist/infra/schedule/scheduled-runs.js +3 -1
- package/dist/lifecycle/kernel-lifecycle.d.ts +1 -0
- package/dist/lifecycle/kernel-lifecycle.js +16 -1
- package/dist/orgs/org.guard.js +4 -0
- package/dist/platform/sealed-reveals.js +9 -6
- package/dist/runtime/maintenance.js +6 -1
- package/dist/runtime/run-execution.d.ts +1 -0
- package/dist/runtime/run-execution.js +5 -0
- package/dist/runtime/run-orphan-check.job.js +20 -15
- package/dist/sandbox/legacy-retirement.js +438 -397
- package/dist/sandbox/modes.d.ts +10 -0
- package/dist/sandbox/modes.js +23 -0
- package/dist/sandbox/operation-reconcile.job.d.ts +14 -1
- package/dist/sandbox/operation-reconcile.job.js +35 -3
- package/dist/sandbox/permission-config-publication.js +83 -77
- package/dist/sandbox/sandbox.service.d.ts +24 -0
- package/dist/sandbox/sandbox.service.js +159 -62
- package/dist/schedules/store.js +17 -14
- package/dist/tools/builtins/sandbox/bash.js +16 -1
- package/dist/tools/drift.js +31 -24
- package/dist/tools/outputs.js +20 -17
- package/package.json +2 -2
- package/src/migrations/0182_rls_policies.sql +202 -0
|
@@ -9,6 +9,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
9
9
|
};
|
|
10
10
|
import { posix } from "node:path";
|
|
11
11
|
import { isDeviceOfflineError, isSandboxPermissionUnsupportedError, isSandboxScopeDeniedError, parseSandboxBrowseScope, parseSandboxExecutionRef, parseSandboxOperationScope, parseSandboxPreparationScope, SandboxOperationOutcomeUnknownError, SandboxPermissionUnsupportedError, SandboxScopeDeniedError, } from "@capybara-hq/plugin-sdk";
|
|
12
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
12
13
|
import { runOutsideTransaction } from "../infra/database/transaction-host.js";
|
|
13
14
|
import { createHeldOperations, withKernelGeneration, } from "./held-operations.js";
|
|
14
15
|
import { SandboxOperationAuthority } from "./operation-authority.js";
|
|
@@ -62,7 +63,7 @@ import { isKernelReadMethod, KernelReadNotAdmitted, KernelReadUnsupported, } fro
|
|
|
62
63
|
import { SandboxLeases, SandboxLockLostError, sandboxBusy, } from "./leases.js";
|
|
63
64
|
import { isKnownProvider, LegacyRetirementStore } from "./legacy-retirement.js";
|
|
64
65
|
import { ManagedDeviceContextFailed } from "./managed-device-context.js";
|
|
65
|
-
import { appliedLacksHost, appliedOf, appliedWider, instanceLimitsJson, orgEffectiveHosts, providerLimits, readInstanceLimits, sameApplied, sameAppliedExact, sameAppliedExactly, } from "./modes.js";
|
|
66
|
+
import { appliedLacksHost, appliedOf, appliedWider, installedWider, instanceLimitsJson, orgEffectiveHosts, providerLimits, readInstanceLimits, sameApplied, sameAppliedExact, sameAppliedExactly, } from "./modes.js";
|
|
66
67
|
import { PermissionFacts } from "./permission-facts.js";
|
|
67
68
|
import { reportOwed, supportOf } from "./permission-runtime-reports.js";
|
|
68
69
|
import { DeviceProviders, isInProcessName, servesDialIn, } from "./providers.js";
|
|
@@ -85,6 +86,8 @@ const RECONCILE_RETRY_SECONDS = 600;
|
|
|
85
86
|
const reconcileCursorKey = (plugin) => `capk:sandbox-reconcile-cursor:${plugin}`;
|
|
86
87
|
/** The bound on one cloud `permissionSupport` read under a lifecycle lease (B-15). */
|
|
87
88
|
const PERMISSION_SUPPORT_TIMEOUT_MS = 30_000;
|
|
89
|
+
/** The bound on one `appliedNetwork` read under a lifecycle lease (issue #766). */
|
|
90
|
+
const APPLIED_NETWORK_TIMEOUT_MS = 30_000;
|
|
88
91
|
/** How long the suspension sweep holds the intent on a sandbox past `maxRunningMinutes` (ms). */
|
|
89
92
|
const SWEEP_INTENT_WAIT_MS = 1000;
|
|
90
93
|
/** Rows one sweep handles at most, and the provider calls it runs at once. */
|
|
@@ -584,6 +587,51 @@ let SandboxService = class SandboxService {
|
|
|
584
587
|
return true;
|
|
585
588
|
return wider && !sameAppliedExact(rec.unapplied, target);
|
|
586
589
|
}
|
|
590
|
+
/**
|
|
591
|
+
* Under the exclusive lease, at a lifecycle step on the instance `handle` of `row` (issue #766):
|
|
592
|
+
* whether the network the provider reports it enforces (`appliedNetwork`) may allow more than
|
|
593
|
+
* the recorded limits ({@link installedWider}), so the desired limits must be applied again even
|
|
594
|
+
* though the record matches them. It closes the residual of a provider whose `update` cannot
|
|
595
|
+
* honor the fence: a stale holder's late `update` that landed after the newer holder's. Only a
|
|
596
|
+
* provider with both `update` and `appliedNetwork` is asked. An unknown answer, a failed read or
|
|
597
|
+
* an abort counts as wider (fail closed: applying the desired limits never widens anything).
|
|
598
|
+
* Never throws.
|
|
599
|
+
*/
|
|
600
|
+
async installedNetworkWider(id, p, row, handle, signal, want) {
|
|
601
|
+
const read = p.impl.appliedNetwork?.bind(p.impl);
|
|
602
|
+
if (!read || !p.impl.update)
|
|
603
|
+
return false;
|
|
604
|
+
const recorded = readInstanceLimits(row.instance_limits).applied;
|
|
605
|
+
let installed;
|
|
606
|
+
try {
|
|
607
|
+
const sig = AbortSignal.any([signal, AbortSignal.timeout(APPLIED_NETWORK_TIMEOUT_MS)]);
|
|
608
|
+
installed = await read(handle, sig);
|
|
609
|
+
}
|
|
610
|
+
catch (err) {
|
|
611
|
+
if (!signal.aborted)
|
|
612
|
+
this.log.warn("cannot read the instance's installed network; applying the desired limits again", { deviceId: id, provider: p.plugin, err });
|
|
613
|
+
return true;
|
|
614
|
+
}
|
|
615
|
+
try {
|
|
616
|
+
const wider = installedWider(installed, recorded, appliedOf(want));
|
|
617
|
+
if (wider) {
|
|
618
|
+
const kind = typeof installed === "object" && installed !== null
|
|
619
|
+
? installed.kind
|
|
620
|
+
: null;
|
|
621
|
+
this.log.warn("the instance's installed network allows more than its record; applying again", {
|
|
622
|
+
deviceId: id,
|
|
623
|
+
provider: p.plugin,
|
|
624
|
+
recorded: recorded?.network.kind ?? null,
|
|
625
|
+
installed: typeof kind === "string" ? kind : typeof kind,
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
return wider;
|
|
629
|
+
}
|
|
630
|
+
catch {
|
|
631
|
+
// a malformed answer the comparison cannot read (a sparse host list, say): fail closed
|
|
632
|
+
return true;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
587
635
|
/**
|
|
588
636
|
* The device's instance state as shown to people (read only, no lease): `running` /
|
|
589
637
|
* `suspended` and the last use. No instance, a destroyed one, or one stuck in `creating` (being
|
|
@@ -1764,29 +1812,31 @@ let SandboxService = class SandboxService {
|
|
|
1764
1812
|
await inPool(rows, SWEEP_CONCURRENCY, async (c) => {
|
|
1765
1813
|
const overdue = c.running_since !== null && c.running_since < runningBefore;
|
|
1766
1814
|
const done = await this.tryLocked(c.device_id, async (lease) => {
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
.
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1815
|
+
return asPlatform("deployment-wide suspension sweep", async () => {
|
|
1816
|
+
const id = c.device_id;
|
|
1817
|
+
// org-filter: deployment-wide suspension sweep (worker) over every org's idle instances,
|
|
1818
|
+
// re-read under each device's lease
|
|
1819
|
+
const row = await this.db
|
|
1820
|
+
.selectFrom("device_instances")
|
|
1821
|
+
.selectAll()
|
|
1822
|
+
.where("device_id", "=", id)
|
|
1823
|
+
.where(due)
|
|
1824
|
+
.executeTakeFirst();
|
|
1825
|
+
if (!row?.handle)
|
|
1826
|
+
return false;
|
|
1827
|
+
const fence = { gen: row.generation };
|
|
1828
|
+
await p.impl.suspend(row.handle, opSignal(lease));
|
|
1829
|
+
if (!(await lease.renew()))
|
|
1830
|
+
throw new SandboxLockLostError(id);
|
|
1831
|
+
await this.fenced(lease, id, fence, {
|
|
1832
|
+
status: "suspended",
|
|
1833
|
+
suspended_at: sql `now()`,
|
|
1834
|
+
running_since: null,
|
|
1835
|
+
sweep_failed_at: null,
|
|
1836
|
+
});
|
|
1837
|
+
this.log.info("sandbox suspended", { deviceId: id });
|
|
1838
|
+
return true;
|
|
1787
1839
|
});
|
|
1788
|
-
this.log.info("sandbox suspended", { deviceId: id });
|
|
1789
|
-
return true;
|
|
1790
1840
|
}, overdue);
|
|
1791
1841
|
if (done)
|
|
1792
1842
|
n++;
|
|
@@ -1823,18 +1873,20 @@ let SandboxService = class SandboxService {
|
|
|
1823
1873
|
let n = 0;
|
|
1824
1874
|
await inPool(await this.candidates(mine), SWEEP_CONCURRENCY, async ({ device_id: id }) => {
|
|
1825
1875
|
const done = await this.tryLocked(id, async (lease) => {
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
.
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1876
|
+
return asPlatform("deployment-wide destruction sweep", async () => {
|
|
1877
|
+
// org-filter: deployment-wide destruction sweep (worker) over every org's due instances,
|
|
1878
|
+
// re-read under each device's lease
|
|
1879
|
+
const row = await this.db
|
|
1880
|
+
.selectFrom("device_instances")
|
|
1881
|
+
.selectAll()
|
|
1882
|
+
.where("device_id", "=", id)
|
|
1883
|
+
.where(mine)
|
|
1884
|
+
.executeTakeFirst();
|
|
1885
|
+
if (!row)
|
|
1886
|
+
return false;
|
|
1887
|
+
await this.destroyRow(row, p, lease, mode);
|
|
1888
|
+
return true;
|
|
1889
|
+
});
|
|
1838
1890
|
});
|
|
1839
1891
|
if (done)
|
|
1840
1892
|
n++;
|
|
@@ -2032,8 +2084,8 @@ let SandboxService = class SandboxService {
|
|
|
2032
2084
|
if (!lease)
|
|
2033
2085
|
return "busy";
|
|
2034
2086
|
try {
|
|
2035
|
-
// org-filter: deviceId comes from the
|
|
2036
|
-
// not caller input
|
|
2087
|
+
// org-filter: deviceId comes from the retire-legacy-tasks worker's due list (kernel-held,
|
|
2088
|
+
// not caller input); the worker runs it in the platform scope
|
|
2037
2089
|
const row = await this.db
|
|
2038
2090
|
.selectFrom("device_instances")
|
|
2039
2091
|
.selectAll()
|
|
@@ -2073,8 +2125,8 @@ let SandboxService = class SandboxService {
|
|
|
2073
2125
|
if (!lease)
|
|
2074
2126
|
return "busy";
|
|
2075
2127
|
try {
|
|
2076
|
-
// org-filter: reads the device's own org; deviceId comes from the
|
|
2077
|
-
//
|
|
2128
|
+
// org-filter: reads the device's own org; deviceId comes from the reconcile sweep
|
|
2129
|
+
// (kernel-held, not caller input), which runs in the platform scope
|
|
2078
2130
|
const device = await this.db
|
|
2079
2131
|
.selectFrom("devices")
|
|
2080
2132
|
.select(["org_id", "provider"])
|
|
@@ -2207,7 +2259,7 @@ let SandboxService = class SandboxService {
|
|
|
2207
2259
|
const cursorKey = reconcileCursorKey(p.plugin);
|
|
2208
2260
|
// org-filter: deployment-wide reconcile sweep (worker) over every org's marked devices of
|
|
2209
2261
|
// this provider
|
|
2210
|
-
const page = (after, n) => this.db
|
|
2262
|
+
const page = (after, n) => asPlatform("deployment-wide reconcile sweep", () => this.db
|
|
2211
2263
|
.selectFrom("devices as d")
|
|
2212
2264
|
.innerJoin("device_instances as i", "i.device_id", "d.id")
|
|
2213
2265
|
.select("d.id")
|
|
@@ -2220,7 +2272,7 @@ let SandboxService = class SandboxService {
|
|
|
2220
2272
|
.where(sql `d.id COLLATE "C" > ${after}`)
|
|
2221
2273
|
.orderBy(sql `d.id COLLATE "C"`)
|
|
2222
2274
|
.limit(n)
|
|
2223
|
-
.execute();
|
|
2275
|
+
.execute());
|
|
2224
2276
|
// a keyset cursor shared by the replicas (the cron runs once per tick): each tick goes on
|
|
2225
2277
|
// after the last device the previous one visited and wraps around, so devices that stay
|
|
2226
2278
|
// marked never starve the ones after them
|
|
@@ -2643,16 +2695,18 @@ let SandboxService = class SandboxService {
|
|
|
2643
2695
|
}
|
|
2644
2696
|
/** Sweep candidates: those whose last sweep failed come last, the rest least recently used first. */
|
|
2645
2697
|
async candidates(due) {
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
.
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2698
|
+
return asPlatform("deployment-wide sweep candidates", async () => {
|
|
2699
|
+
// org-filter: deployment-wide sweep candidates (suspension and destruction workers) across
|
|
2700
|
+
// every org
|
|
2701
|
+
return await this.db
|
|
2702
|
+
.selectFrom("device_instances")
|
|
2703
|
+
.select(["device_id", "running_since"])
|
|
2704
|
+
.where(due)
|
|
2705
|
+
.orderBy(sql `sweep_failed_at ASC NULLS FIRST`)
|
|
2706
|
+
.orderBy("last_used_at")
|
|
2707
|
+
.limit(SWEEP_BATCH)
|
|
2708
|
+
.execute();
|
|
2709
|
+
});
|
|
2656
2710
|
}
|
|
2657
2711
|
/**
|
|
2658
2712
|
* One try at the exclusive lease: null → false; else runs `fn`. With `overdue` (past
|
|
@@ -2685,8 +2739,8 @@ let SandboxService = class SandboxService {
|
|
|
2685
2739
|
}
|
|
2686
2740
|
catch (err) {
|
|
2687
2741
|
this.log.warn("sandbox maintenance failed; will retry", { deviceId, err });
|
|
2688
|
-
// org-filter: sweep bookkeeping on the device
|
|
2689
|
-
// lease
|
|
2742
|
+
// org-filter: sweep bookkeeping on the device the calling sweep (platform scope) holds
|
|
2743
|
+
// under its lease
|
|
2690
2744
|
await this.db
|
|
2691
2745
|
.updateTable("device_instances")
|
|
2692
2746
|
.set({ sweep_failed_at: sql `now()` })
|
|
@@ -2770,12 +2824,18 @@ let SandboxService = class SandboxService {
|
|
|
2770
2824
|
const createdAt = row.created_at.getTime();
|
|
2771
2825
|
const fsResets = row.fs_resets;
|
|
2772
2826
|
if (handle && isInstance(row, gone) && row.status === "running") {
|
|
2773
|
-
const created = await this.
|
|
2827
|
+
const created = await this.createSettled(ctx, p, held, row, fence, signal, want);
|
|
2774
2828
|
return { ...created, fsResets, info: { fresh: true, restarted: true }, fence };
|
|
2775
2829
|
}
|
|
2776
2830
|
if (handle && row.status === "running") {
|
|
2777
|
-
|
|
2778
|
-
|
|
2831
|
+
const must = this.mustApply(row, p, want);
|
|
2832
|
+
const installed = !must && (await this.installedNetworkWider(id, p, row, handle, signal, want));
|
|
2833
|
+
// aborted while reading: the instance runs as it was (no quarantine of a healthy instance
|
|
2834
|
+
// for a cancel); the next exclusive step reads again
|
|
2835
|
+
if (installed && signal.aborted)
|
|
2836
|
+
throw abortReason(signal);
|
|
2837
|
+
if (must || installed) {
|
|
2838
|
+
const created = await this.applyLimits(ctx, p, held, row, handle, fence, signal, want, installed);
|
|
2779
2839
|
if (created)
|
|
2780
2840
|
return { ...created, fsResets, info: { fresh: true, restarted: true }, fence };
|
|
2781
2841
|
}
|
|
@@ -2787,10 +2847,13 @@ let SandboxService = class SandboxService {
|
|
|
2787
2847
|
if (r === "resumed") {
|
|
2788
2848
|
const incarnation = { provider: p.plugin, generation: String(createdAt) };
|
|
2789
2849
|
// the new limits first: the row says running only once the instance runs with them
|
|
2790
|
-
|
|
2850
|
+
// (and again when the instance's installed network allows more than the record: #766)
|
|
2851
|
+
const must = this.mustApply(row, p, want);
|
|
2852
|
+
const installed = !must && (await this.installedNetworkWider(id, p, row, handle, signal, want));
|
|
2853
|
+
if (must || installed) {
|
|
2791
2854
|
let created;
|
|
2792
2855
|
try {
|
|
2793
|
-
created = await this.applyLimits(ctx, p, held, row, handle, fence, signal, want);
|
|
2856
|
+
created = await this.applyLimits(ctx, p, held, row, handle, fence, signal, want, installed);
|
|
2794
2857
|
}
|
|
2795
2858
|
catch (err) {
|
|
2796
2859
|
// refused as stale: the provider changed nothing, and the instance (the same
|
|
@@ -2841,7 +2904,7 @@ let SandboxService = class SandboxService {
|
|
|
2841
2904
|
deviceId: id,
|
|
2842
2905
|
});
|
|
2843
2906
|
}
|
|
2844
|
-
const created = await this.
|
|
2907
|
+
const created = await this.createSettled(ctx, p, held, row, fence, signal, want);
|
|
2845
2908
|
// had_instance only ever becomes true: any earlier instance (even if a rebuild failed since)
|
|
2846
2909
|
// makes this one a restart on the kept files
|
|
2847
2910
|
return {
|
|
@@ -2860,6 +2923,26 @@ let SandboxService = class SandboxService {
|
|
|
2860
2923
|
throw err;
|
|
2861
2924
|
}
|
|
2862
2925
|
}
|
|
2926
|
+
/**
|
|
2927
|
+
* {@link create} from {@link ensure}, then, for a provider whose limits are fixed at its file
|
|
2928
|
+
* system (the new instance keeps the network its file system enforces, E2B), the installed
|
|
2929
|
+
* network check of {@link installedNetworkWider} on the new instance (issue #766): wider than
|
|
2930
|
+
* the record applies the desired limits again before the use. Any other provider's new instance
|
|
2931
|
+
* got the desired limits at `create`.
|
|
2932
|
+
*/
|
|
2933
|
+
async createSettled(ctx, p, held, row, fence, signal, want) {
|
|
2934
|
+
const created = await this.create(ctx, p, held, row, fence, signal, want);
|
|
2935
|
+
if (p.impl.limitsApplyAt !== "fileSystem")
|
|
2936
|
+
return created;
|
|
2937
|
+
// the row as the create committed it (its new instance generation and record)
|
|
2938
|
+
const fresh = await this.readRow(ctx);
|
|
2939
|
+
if (!fresh)
|
|
2940
|
+
return created;
|
|
2941
|
+
if (!(await this.installedNetworkWider(ctx.deviceId, p, fresh, created.handle, signal, want)))
|
|
2942
|
+
return created;
|
|
2943
|
+
const again = await this.applyLimits(ctx, p, held, fresh, created.handle, fence, signal, want, true);
|
|
2944
|
+
return again ?? created;
|
|
2945
|
+
}
|
|
2863
2946
|
/**
|
|
2864
2947
|
* Under the lease: fenced reset to `creating`, the file system when there is none
|
|
2865
2948
|
* (`createFileSystem`, its handle written fenced), then the instance on it, then a fenced write
|
|
@@ -2993,8 +3076,13 @@ let SandboxService = class SandboxService {
|
|
|
2993
3076
|
* instance create is recreated (warned); after an abort, or for a provider bound to its file
|
|
2994
3077
|
* system, the instance is suspended ({@link quarantine}) and the error thrown, so the next use
|
|
2995
3078
|
* resumes it and tries again.
|
|
3079
|
+
*
|
|
3080
|
+
* `installedWider`: the call narrows an instance whose installed network allows more than its
|
|
3081
|
+
* record (issue #766). A provider bound to its file system that cannot apply `want` then is
|
|
3082
|
+
* quarantined too, with the intent kept, instead of being recorded `unapplied` and used: the
|
|
3083
|
+
* instance would run wider than the record claims.
|
|
2996
3084
|
*/
|
|
2997
|
-
async applyLimits(ctx, p, held, row, handle, fence, signal, want) {
|
|
3085
|
+
async applyLimits(ctx, p, held, row, handle, fence, signal, want, installedWider = false) {
|
|
2998
3086
|
const id = ctx.deviceId;
|
|
2999
3087
|
const target = appliedOf(want);
|
|
3000
3088
|
const rec = readInstanceLimits(row.instance_limits);
|
|
@@ -3049,6 +3137,15 @@ let SandboxService = class SandboxService {
|
|
|
3049
3137
|
}
|
|
3050
3138
|
}
|
|
3051
3139
|
if (p.impl.limitsApplyAt === "fileSystem" && !gone) {
|
|
3140
|
+
if (installedWider) {
|
|
3141
|
+
// the instance enforces more than the record and the provider could not narrow it: stop
|
|
3142
|
+
// it, keep the intent (the next use resumes it and tries again); a file reset applies it
|
|
3143
|
+
await this.quarantine(held, id, handle, p, fence, {
|
|
3144
|
+
provider: p.plugin,
|
|
3145
|
+
generation: String(row.created_at.getTime()),
|
|
3146
|
+
});
|
|
3147
|
+
throw new Error("The device's sandbox enforces a wider network than its recorded limits and the provider could not narrow it; the sandbox is stopped and the next use tries again (resetting the device's files also applies the limits)");
|
|
3148
|
+
}
|
|
3052
3149
|
await this.fenced(held, id, fence, { instance_limits: instanceLimitsJson(was, target) });
|
|
3053
3150
|
this.log.info("sandbox limits changed; they apply when the device's files are reset", facts);
|
|
3054
3151
|
return null;
|
|
@@ -3274,8 +3371,8 @@ let SandboxService = class SandboxService {
|
|
|
3274
3371
|
}
|
|
3275
3372
|
/** The device was deleted, or its org was: the instance and the files go. */
|
|
3276
3373
|
deviceGone(eb) {
|
|
3277
|
-
// org-filter:
|
|
3278
|
-
//
|
|
3374
|
+
// org-filter: a sweep condition fragment (a deleted device of any org, correlated to the
|
|
3375
|
+
// instance row), run only by candidates(), inside its asPlatform
|
|
3279
3376
|
return eb.or([
|
|
3280
3377
|
eb.exists(eb
|
|
3281
3378
|
.selectFrom("devices")
|
package/dist/schedules/store.js
CHANGED
|
@@ -6,6 +6,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
6
6
|
};
|
|
7
7
|
import { Injectable } from "@nestjs/common";
|
|
8
8
|
import { sql } from "kysely";
|
|
9
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
9
10
|
const NON_TERMINAL = ["pending", "running"];
|
|
10
11
|
const date = (v) => (v instanceof Date ? v : new Date(v));
|
|
11
12
|
const dateOrNull = (v) => (v === null ? null : date(v));
|
|
@@ -148,7 +149,7 @@ let ScheduledTaskStore = class ScheduledTaskStore {
|
|
|
148
149
|
}
|
|
149
150
|
/** Patches a live task; null when it is missing or deleted. */
|
|
150
151
|
// org-filter: by the id of a task its caller loaded under an org filter (lockManaged / lockTask)
|
|
151
|
-
// or locked in the scan's
|
|
152
|
+
// or locked in the scan's due sweep (which runs in the platform scope)
|
|
152
153
|
async updateTask(t, id, patch) {
|
|
153
154
|
const row = await t
|
|
154
155
|
.updateTable("scheduled_tasks")
|
|
@@ -215,19 +216,21 @@ let ScheduledTaskStore = class ScheduledTaskStore {
|
|
|
215
216
|
*/
|
|
216
217
|
// org-filter: the scan's deployment-wide due-task sweep (LockedCron), each task fired in its own org
|
|
217
218
|
async lockDue(t, now, limit) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
219
|
+
return asPlatform("deployment-wide due-task sweep", async () => {
|
|
220
|
+
const rows = await t
|
|
221
|
+
.selectFrom("scheduled_tasks")
|
|
222
|
+
.selectAll()
|
|
223
|
+
.where("status", "=", "active")
|
|
224
|
+
.where("deleted_at", "is", null)
|
|
225
|
+
.where("next_run_at", "<=", now)
|
|
226
|
+
.orderBy("next_run_at")
|
|
227
|
+
.orderBy("id")
|
|
228
|
+
.limit(limit)
|
|
229
|
+
.forUpdate()
|
|
230
|
+
.skipLocked()
|
|
231
|
+
.execute();
|
|
232
|
+
return rows.map(toTask);
|
|
233
|
+
});
|
|
231
234
|
}
|
|
232
235
|
/**
|
|
233
236
|
* Inserts a task run; null when a non-trial run for the same tick already exists (the
|
|
@@ -259,7 +259,7 @@ export async function runInBackgroundManaged(deps, ctx, input) {
|
|
|
259
259
|
await announce();
|
|
260
260
|
const { task } = launch;
|
|
261
261
|
if (launch.kind === "unknown") {
|
|
262
|
-
|
|
262
|
+
return launchUnconfirmed(task.id, `Background task ${task.id} ("${description}") may have started, but its start was not confirmed; it is being reconciled under its original identity and will not be started again. Check TaskList before starting it again.`);
|
|
263
263
|
}
|
|
264
264
|
// a retry of a call whose acknowledged task has since ended reports the start it made; a ref
|
|
265
265
|
// attached to an owed cancellation (a task stopped by its operation revoke before the ref was
|
|
@@ -295,6 +295,9 @@ export async function runInBackgroundManaged(deps, ctx, input) {
|
|
|
295
295
|
if (!started && task.launchState === "cancel-pending") {
|
|
296
296
|
throw new ToolError(`Background task ${task.id} ("${description}") was stopped while it was starting; its cancellation is in progress. Call Bash again if it is still needed.`);
|
|
297
297
|
}
|
|
298
|
+
if (!started && task.launchState === "dispatched") {
|
|
299
|
+
return launchUnconfirmed(task.id, `Background task ${task.id} ("${description}") may have started, but its start was not confirmed; it will not be started again. Check TaskList before starting it again.`);
|
|
300
|
+
}
|
|
298
301
|
if (!started) {
|
|
299
302
|
throw new ToolError(`Background task ${task.id} ("${description}") may have started, but its start was not confirmed; it will not be started again. Check TaskList before starting it again.`, { kind: "unavailable" });
|
|
300
303
|
}
|
|
@@ -314,6 +317,18 @@ export async function runInBackgroundManaged(deps, ctx, input) {
|
|
|
314
317
|
display: display(`后台运行命令:${shown}`, `Started in the background: ${shown}`),
|
|
315
318
|
};
|
|
316
319
|
}
|
|
320
|
+
/**
|
|
321
|
+
* The result of a start the kernel sent but could not confirm: the same error text the model got
|
|
322
|
+
* as a thrown `ToolError`, plus the display-only `launch_unconfirmed` view, so the thread shows the
|
|
323
|
+
* row as reconciling until the task settles (never in a request).
|
|
324
|
+
*/
|
|
325
|
+
function launchUnconfirmed(taskId, text) {
|
|
326
|
+
return {
|
|
327
|
+
content: [{ type: "text", text }],
|
|
328
|
+
isError: true,
|
|
329
|
+
view: { kind: "launch_unconfirmed", taskId },
|
|
330
|
+
};
|
|
331
|
+
}
|
|
317
332
|
/**
|
|
318
333
|
* Why a managed start was refused, in the kernel's own words (never the provider's message):
|
|
319
334
|
* `why` is the clause recorded after `launch refused: `; `text`, the complete sentence for the
|
package/dist/tools/drift.js
CHANGED
|
@@ -15,6 +15,7 @@ import { ApiError } from "../errors.js";
|
|
|
15
15
|
import { audit } from "../identity/audit.js";
|
|
16
16
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
17
17
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
18
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
18
19
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
19
20
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
20
21
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
@@ -536,30 +537,36 @@ let ToolDrift = class ToolDrift {
|
|
|
536
537
|
// org-filter: deployment-wide sweep across all orgs (LockedInterval in ToolJobs); each job
|
|
537
538
|
// carries its connector's own org_id
|
|
538
539
|
async scan() {
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
.
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
540
|
+
return asPlatform("deployment-wide sweep across all orgs", async () => {
|
|
541
|
+
const interval = this.intervalMs();
|
|
542
|
+
const bucket = Math.floor(Date.now() / interval);
|
|
543
|
+
const apps = await this.db
|
|
544
|
+
.selectFrom("connectors")
|
|
545
|
+
.select(["id", "org_id"])
|
|
546
|
+
.where("deleted_at", "is", null)
|
|
547
|
+
.where("status", "=", "active")
|
|
548
|
+
.where((eb) => eb.or([
|
|
549
|
+
eb("tools_synced_at", "is", null),
|
|
550
|
+
eb("tools_synced_at", "<", sql `now() - ${Math.floor(interval / 2)}::bigint * interval '1 millisecond'`),
|
|
551
|
+
]))
|
|
552
|
+
.orderBy("id")
|
|
553
|
+
.execute();
|
|
554
|
+
for (const a of apps) {
|
|
555
|
+
if (a.id.includes(":"))
|
|
556
|
+
continue;
|
|
557
|
+
await this.queue.add(DRIFT_CONNECTOR_JOB, {
|
|
558
|
+
orgId: a.org_id,
|
|
559
|
+
connectorId: a.id,
|
|
560
|
+
trigger: "schedule",
|
|
561
|
+
}, {
|
|
562
|
+
jobId: driftScanJobId(a.id, bucket),
|
|
563
|
+
attempts: 1,
|
|
564
|
+
removeOnComplete: { age: Math.ceil(interval / 1000) },
|
|
565
|
+
removeOnFail: KEEP_FAILED,
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
return apps.length;
|
|
569
|
+
});
|
|
563
570
|
}
|
|
564
571
|
};
|
|
565
572
|
__decorate([
|
package/dist/tools/outputs.js
CHANGED
|
@@ -13,6 +13,7 @@ import { Inject, Injectable } from "@nestjs/common";
|
|
|
13
13
|
import { sql } from "kysely";
|
|
14
14
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
15
15
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
16
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
16
17
|
/** 文本行数(按 `\n` 切分;空串算 1 行,与按行分页读取一致)。 */
|
|
17
18
|
export function countLines(s) {
|
|
18
19
|
let n = 1;
|
|
@@ -80,23 +81,25 @@ export const OUTPUT_CLEANUP_BATCH = 1000;
|
|
|
80
81
|
// org-filter: deployment-wide cleanup of expired archives across all orgs (a worker job), by
|
|
81
82
|
// expiry only
|
|
82
83
|
export async function deleteExpiredOutputs(db, batch = OUTPUT_CLEANUP_BATCH) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
84
|
+
return asPlatform("deployment-wide cleanup of expired archives across all orgs", async () => {
|
|
85
|
+
let total = 0;
|
|
86
|
+
for (;;) {
|
|
87
|
+
const r = await db
|
|
88
|
+
.deleteFrom("tool_outputs")
|
|
89
|
+
.where("id", "in", db
|
|
90
|
+
.selectFrom("tool_outputs")
|
|
91
|
+
.select("id")
|
|
92
|
+
.where("expires_at", "<", sql `now()`)
|
|
93
|
+
.limit(batch)
|
|
94
|
+
.forUpdate()
|
|
95
|
+
.skipLocked())
|
|
96
|
+
.executeTakeFirst();
|
|
97
|
+
const n = Number(r.numDeletedRows);
|
|
98
|
+
total += n;
|
|
99
|
+
if (n < batch)
|
|
100
|
+
return total;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
100
103
|
}
|
|
101
104
|
/** 存档不存在 / 过期 / 不属于本会话(模型可见,英文;不区分几种情况,不泄露别处的存档是否存在)。 */
|
|
102
105
|
export const outputNotFoundText = (id) => `Output ${id} not found or expired. Run the tool that produced it again to get a new output id.`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capybara-hq/kernel",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.81",
|
|
4
4
|
"description": "Capybara kernel: config, database, plugin host and HTTP API of the Capybara agent platform",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"yaml": "^2.9.1",
|
|
67
67
|
"yauzl": "3.4.0",
|
|
68
68
|
"zod": "^4.6.7",
|
|
69
|
-
"@capybara-hq/plugin-sdk": "^0.1.0-rc.
|
|
69
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.67"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@langchain/langgraph-checkpoint-validation": "^1.2.0",
|