@capybara-hq/kernel 0.1.0-rc.136 → 0.1.0-rc.138
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/audit-log.service.d.ts +96 -12
- package/dist/admin/audit-log.service.js +508 -44
- package/dist/admin/audit-targets.d.ts +17 -0
- package/dist/admin/audit-targets.js +84 -0
- package/dist/admin/audit.controller.d.ts +12 -9
- package/dist/admin/audit.controller.js +29 -8
- package/dist/admin/audit.schemas.d.ts +20 -4
- package/dist/admin/audit.schemas.js +42 -17
- package/dist/admin/extensions.controller.d.ts +5 -2
- package/dist/admin/extensions.controller.js +10 -1
- package/dist/admin/extensions.schemas.d.ts +4 -0
- package/dist/admin/extensions.schemas.js +9 -0
- package/dist/admin/health.service.d.ts +1 -1
- package/dist/admin/health.service.js +3 -3
- package/dist/agents/templates.js +3 -10
- package/dist/app.d.ts +3 -1
- package/dist/app.js +1 -0
- package/dist/automations/automations.service.js +4 -5
- package/dist/catalog/catalog-content.d.ts +6 -8
- package/dist/catalog/catalog-content.js +17 -22
- package/dist/catalog/catalog-items.d.ts +5 -10
- package/dist/catalog/catalog-items.js +27 -51
- package/dist/catalog/catalog-scope.service.d.ts +4 -2
- package/dist/catalog/catalog-scope.service.js +109 -145
- package/dist/catalog/catalog.module.js +7 -4
- package/dist/catalog/catalog.service.d.ts +10 -4
- package/dist/catalog/catalog.service.js +52 -10
- package/dist/catalog/cli.js +17 -0
- package/dist/catalog/component-promotion.js +8 -4
- package/dist/catalog/component-scope.service.d.ts +3 -1
- package/dist/catalog/component-scope.service.js +101 -140
- package/dist/catalog/component-state.d.ts +10 -8
- package/dist/catalog/component-state.js +42 -30
- package/dist/catalog/dependency-edges.d.ts +25 -0
- package/dist/catalog/dependency-edges.js +125 -0
- package/dist/catalog/effect-work.d.ts +27 -0
- package/dist/catalog/effect-work.js +151 -0
- package/dist/catalog/jobs.d.ts +4 -1
- package/dist/catalog/jobs.js +14 -4
- package/dist/catalog/org-catalog.service.d.ts +5 -1
- package/dist/catalog/org-catalog.service.js +101 -106
- package/dist/catalog/org-intent.d.ts +45 -0
- package/dist/catalog/org-intent.js +205 -0
- package/dist/catalog/owned-items.d.ts +6 -2
- package/dist/catalog/owned-items.js +27 -32
- package/dist/catalog/ownership.d.ts +2 -3
- package/dist/catalog/ownership.js +10 -28
- package/dist/catalog/platform-components.d.ts +3 -1
- package/dist/catalog/platform-components.js +9 -1
- package/dist/catalog/plugin-import.d.ts +4 -2
- package/dist/catalog/plugin-import.js +22 -21
- package/dist/catalog/plugin-install.d.ts +19 -54
- package/dist/catalog/plugin-install.js +221 -271
- package/dist/catalog/plugin-upgrade.d.ts +10 -5
- package/dist/catalog/plugin-upgrade.js +80 -67
- package/dist/catalog/request-rules.js +20 -8
- package/dist/catalog/scope-orgs.js +2 -7
- package/dist/catalog/scope-state.d.ts +6 -6
- package/dist/catalog/scope-state.js +24 -43
- package/dist/catalog/scope-sync-workers.d.ts +7 -11
- package/dist/catalog/scope-sync-workers.js +10 -26
- package/dist/catalog/scope-sync.d.ts +19 -116
- package/dist/catalog/scope-sync.js +228 -275
- package/dist/catalog/scope-writer.d.ts +87 -0
- package/dist/catalog/scope-writer.js +239 -0
- package/dist/catalog/template-connector-ensure.js +1 -1
- package/dist/catalog/unshare.js +9 -43
- package/dist/channels/connections.service.js +6 -6
- package/dist/channels/supervisor.js +3 -7
- package/dist/client/client-access.service.js +2 -12
- package/dist/config/schema.d.ts +6 -0
- package/dist/config/schema.js +32 -1
- package/dist/ctx/create-ctx.d.ts +2 -2
- package/dist/ctx/create-ctx.js +2 -60
- package/dist/ctx/guard-db.d.ts +6 -0
- package/dist/ctx/guard-db.js +55 -0
- package/dist/db/extension-handoff-schema.d.ts +1 -1
- package/dist/db/extension-handoff-schema.js +48 -10
- package/dist/db/extension-schema-transfer.js +5 -2
- package/dist/db/kernel-schema.d.ts +32 -6
- package/dist/device-access/host-services.d.ts +2 -2
- package/dist/device-access/host-services.js +5 -6
- package/dist/erasure/org-catalog.js +2 -2
- package/dist/erasure/org-rules.js +7 -0
- package/dist/extensions/admin-ops.d.ts +10 -6
- package/dist/extensions/admin-ops.js +53 -9
- package/dist/extensions/admin-view.d.ts +4 -1
- package/dist/extensions/admin-view.js +27 -14
- package/dist/extensions/bootstrap.d.ts +1 -1
- package/dist/extensions/bootstrap.js +6 -4
- package/dist/extensions/cli.js +1 -0
- package/dist/extensions/effective-config.js +1 -4
- package/dist/extensions/enabled.d.ts +183 -29
- package/dist/extensions/enabled.js +148 -62
- package/dist/extensions/host.d.ts +65 -45
- package/dist/extensions/host.js +446 -128
- package/dist/extensions/installs.d.ts +9 -0
- package/dist/extensions/installs.js +23 -0
- package/dist/extensions/isolated/admission.d.ts +10 -0
- package/dist/extensions/isolated/admission.js +206 -0
- package/dist/extensions/isolated/authorization.d.ts +45 -0
- package/dist/extensions/isolated/authorization.js +191 -0
- package/dist/extensions/isolated/broker.d.ts +15 -0
- package/dist/extensions/isolated/broker.js +471 -0
- package/dist/extensions/isolated/child-protocol.d.ts +84 -0
- package/dist/extensions/isolated/child-protocol.js +21 -0
- package/dist/extensions/isolated/child-registration.d.ts +22 -0
- package/dist/extensions/isolated/child-registration.js +152 -0
- package/dist/extensions/isolated/child-session.d.ts +6 -0
- package/dist/extensions/isolated/child-session.js +343 -0
- package/dist/extensions/isolated/child-streams.d.ts +4 -0
- package/dist/extensions/isolated/child-streams.js +142 -0
- package/dist/extensions/isolated/child.d.ts +1 -0
- package/dist/extensions/isolated/child.js +10 -0
- package/dist/extensions/isolated/configured-package.d.ts +15 -0
- package/dist/extensions/isolated/configured-package.js +73 -0
- package/dist/extensions/isolated/engine-binds.d.ts +9 -0
- package/dist/extensions/isolated/engine-binds.js +82 -0
- package/dist/extensions/isolated/engine-policy.d.ts +85 -0
- package/dist/extensions/isolated/engine-policy.js +75 -0
- package/dist/extensions/isolated/engine-proxy-main.d.ts +1 -0
- package/dist/extensions/isolated/engine-proxy-main.js +20 -0
- package/dist/extensions/isolated/engine-proxy.d.ts +7 -0
- package/dist/extensions/isolated/engine-proxy.js +408 -0
- package/dist/extensions/isolated/engine-vaults.d.ts +12 -0
- package/dist/extensions/isolated/engine-vaults.js +93 -0
- package/dist/extensions/isolated/lanes.d.ts +108 -0
- package/dist/extensions/isolated/lanes.js +726 -0
- package/dist/extensions/isolated/postgres-gate.d.ts +15 -0
- package/dist/extensions/isolated/postgres-gate.js +255 -0
- package/dist/extensions/isolated/revision.d.ts +32 -0
- package/dist/extensions/isolated/revision.js +359 -0
- package/dist/extensions/isolated/rpc-frame.d.ts +50 -0
- package/dist/extensions/isolated/rpc-frame.js +193 -0
- package/dist/extensions/isolated/rpc.d.ts +66 -0
- package/dist/extensions/isolated/rpc.js +314 -0
- package/dist/extensions/isolated/sandbox.d.ts +39 -0
- package/dist/extensions/isolated/sandbox.js +356 -0
- package/dist/extensions/isolated/tier.d.ts +6 -0
- package/dist/extensions/isolated/tier.js +45 -0
- package/dist/extensions/ledger.d.ts +1 -1
- package/dist/extensions/lifecycle-topics.d.ts +2 -0
- package/dist/extensions/lifecycle-topics.js +2 -0
- package/dist/extensions/lifecycle.d.ts +3 -1
- package/dist/extensions/lifecycle.js +53 -5
- package/dist/extensions/locks.d.ts +2 -0
- package/dist/extensions/locks.js +7 -0
- package/dist/extensions/org-config.d.ts +17 -27
- package/dist/extensions/org-config.js +114 -129
- package/dist/extensions/permission-view.d.ts +6 -1
- package/dist/extensions/plugin-guides.d.ts +5 -10
- package/dist/extensions/plugin-guides.js +18 -37
- package/dist/extensions/plugin-icons.d.ts +3 -21
- package/dist/extensions/plugin-icons.js +9 -44
- package/dist/extensions/plugin-items.d.ts +10 -30
- package/dist/extensions/plugin-items.js +32 -73
- package/dist/extensions/source-intent.d.ts +14 -0
- package/dist/extensions/source-intent.js +77 -0
- package/dist/inbox/platform-inbox.service.js +16 -27
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/infra/database/database.module.js +6 -1
- package/dist/infra/database/kernel-db.d.ts +3 -1
- package/dist/infra/database/kernel-db.js +4 -0
- package/dist/infra/events/kernel-events.d.ts +4 -0
- package/dist/infra/outbox/registry.d.ts +3 -36
- package/dist/infra/outbox/registry.js +4 -49
- package/dist/infra/paging/paged-read.d.ts +56 -0
- package/dist/infra/paging/paged-read.js +78 -0
- package/dist/infra/queue/queue-names.d.ts +1 -1
- package/dist/infra/queue/queue-names.js +1 -1
- package/dist/kernel.d.ts +3 -2
- package/dist/kernel.js +21 -2
- package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
- package/dist/lifecycle/kernel-lifecycle.js +22 -12
- package/dist/lifecycle/tokens.d.ts +8 -0
- package/dist/lifecycle/tokens.js +2 -0
- package/dist/orgs/directory-sync.js +1 -4
- package/dist/orgs/org-lock.d.ts +3 -1
- package/dist/orgs/org-lock.js +3 -1
- package/dist/orgs/orgs.service.d.ts +3 -1
- package/dist/orgs/orgs.service.js +15 -8
- package/dist/overrides.d.ts +0 -3
- package/dist/overrides.js +0 -2
- package/dist/platform/ops/admin.ops.js +14 -3
- package/dist/platform/ops/extensions-admin.ops.js +20 -3
- package/dist/platform/ops/org-plugins.ops.js +5 -2
- package/dist/platform/ops/orgs.ops.js +13 -3
- package/dist/platform/ops/plugins.ops.js +11 -10
- package/dist/setup/setup-checks.service.js +3 -2
- package/dist/skills/usable.js +41 -30
- package/dist/themes/theme-state.js +3 -3
- package/dist/tools/registry.d.ts +2 -2
- package/dist/tools/registry.js +7 -3
- package/package.json +2 -2
- package/src/migrations/0228_extension_org_switches.sql +56 -0
- package/src/migrations/0229_catalog_dependency_edges.sql +17 -0
- package/src/migrations/0230_audit_filter_idx.sql +18 -0
- package/src/migrations/0231_plugin_effect_work.sql +171 -0
- package/dist/catalog/install-status.d.ts +0 -6
- package/dist/catalog/install-status.js +0 -6
- package/dist/catalog/scope-jobs.d.ts +0 -59
- package/dist/catalog/scope-jobs.js +0 -78
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { assertEffectKey, } from "../../ctx/durable-effects.service.js";
|
|
4
|
+
import { asPlatform, withOrgScope } from "../../infra/database/org-scope.js";
|
|
5
|
+
import { ISOLATED_MODEL_RESULT_BYTES } from "./child-protocol.js";
|
|
6
|
+
import { IsolatedRpcError, RpcByteChunk } from "./rpc.js";
|
|
7
|
+
function refuse() {
|
|
8
|
+
throw new IsolatedRpcError("refused");
|
|
9
|
+
}
|
|
10
|
+
function record(value) {
|
|
11
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
12
|
+
return refuse();
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
function string(value) {
|
|
16
|
+
if (typeof value !== "string" || !value)
|
|
17
|
+
return refuse();
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
const CHUNK_BYTES = 64 * 1024;
|
|
21
|
+
const STREAMS_PER_CALL = 4;
|
|
22
|
+
const LOG_INPUT_MAX = 8_192;
|
|
23
|
+
const LOG_MESSAGE_MAX = 1_024;
|
|
24
|
+
const LOG_WINDOW_MS = 1_000;
|
|
25
|
+
const LOG_ORGS_PER_WINDOW = 64;
|
|
26
|
+
const LOG_METHODS = {
|
|
27
|
+
"log.info": "info",
|
|
28
|
+
"log.warn": "warn",
|
|
29
|
+
"log.error": "error",
|
|
30
|
+
};
|
|
31
|
+
const ACKNOWLEDGED_WRITES = new Set([
|
|
32
|
+
"bus.publish",
|
|
33
|
+
"inbox.post",
|
|
34
|
+
"inbox.resolve",
|
|
35
|
+
"kv.set",
|
|
36
|
+
"kv.del",
|
|
37
|
+
"kv.incr",
|
|
38
|
+
"outbox.add",
|
|
39
|
+
"outbox.stage",
|
|
40
|
+
"queue.add",
|
|
41
|
+
"storage.delete",
|
|
42
|
+
"storage.deletePrefix",
|
|
43
|
+
"storage.finishPut",
|
|
44
|
+
"triggers.emit",
|
|
45
|
+
"ui.invalidate",
|
|
46
|
+
]);
|
|
47
|
+
class Handles {
|
|
48
|
+
owners = new WeakMap();
|
|
49
|
+
add(scope, value, close) {
|
|
50
|
+
let entries = this.owners.get(scope);
|
|
51
|
+
if (!entries) {
|
|
52
|
+
entries = new Map();
|
|
53
|
+
this.owners.set(scope, entries);
|
|
54
|
+
}
|
|
55
|
+
if (scope.signal?.aborted || entries.size >= STREAMS_PER_CALL) {
|
|
56
|
+
void Promise.resolve()
|
|
57
|
+
.then(close)
|
|
58
|
+
.catch(() => { });
|
|
59
|
+
return refuse();
|
|
60
|
+
}
|
|
61
|
+
const id = randomUUID();
|
|
62
|
+
entries.set(id, { value, close });
|
|
63
|
+
scope.cleanup?.add(close);
|
|
64
|
+
return id;
|
|
65
|
+
}
|
|
66
|
+
get(scope, id) {
|
|
67
|
+
const entry = this.owners.get(scope)?.get(string(id));
|
|
68
|
+
if (!entry)
|
|
69
|
+
return refuse();
|
|
70
|
+
return entry.value;
|
|
71
|
+
}
|
|
72
|
+
async close(scope, id) {
|
|
73
|
+
const key = string(id);
|
|
74
|
+
const entries = this.owners.get(scope);
|
|
75
|
+
const entry = entries?.get(key);
|
|
76
|
+
if (!entry)
|
|
77
|
+
return;
|
|
78
|
+
entries?.delete(key);
|
|
79
|
+
scope.cleanup?.delete(entry.close);
|
|
80
|
+
await entry.close();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function wireCause(event, scope) {
|
|
84
|
+
if (!event.cause || !("call" in event.cause))
|
|
85
|
+
return event;
|
|
86
|
+
if (!scope.toolCall)
|
|
87
|
+
return refuse();
|
|
88
|
+
return {
|
|
89
|
+
...event,
|
|
90
|
+
cause: {
|
|
91
|
+
call: scope.toolCall,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function call(method, args) {
|
|
96
|
+
return method(...args);
|
|
97
|
+
}
|
|
98
|
+
const requestInit = z.strictObject({
|
|
99
|
+
method: z.string().max(12),
|
|
100
|
+
headers: z.array(z.tuple([z.string(), z.string()])).max(64),
|
|
101
|
+
redirect: z.enum(["error", "manual", "follow"]).optional(),
|
|
102
|
+
body: z
|
|
103
|
+
.string()
|
|
104
|
+
.max(4 * Math.ceil(CHUNK_BYTES / 3))
|
|
105
|
+
.optional(),
|
|
106
|
+
});
|
|
107
|
+
const leaseInput = z.strictObject({ ttlMs: z.number().int().min(100).max(60_000) });
|
|
108
|
+
const bytes = (value) => {
|
|
109
|
+
const text = string(value);
|
|
110
|
+
if (text.length > 4 * Math.ceil(CHUNK_BYTES / 3) || !/^[A-Za-z0-9+/]*={0,2}$/.test(text)) {
|
|
111
|
+
return refuse();
|
|
112
|
+
}
|
|
113
|
+
const data = Buffer.from(text, "base64");
|
|
114
|
+
if (data.length > CHUNK_BYTES)
|
|
115
|
+
return refuse();
|
|
116
|
+
return new Uint8Array(data);
|
|
117
|
+
};
|
|
118
|
+
function incrementIsolatedKv(ctx, [key, opts], org) {
|
|
119
|
+
return ctx.kv.incr(`${org}:${string(key)}`, z.strictObject({ ttlSeconds: z.number().int().min(1).max(604800) }).parse(opts));
|
|
120
|
+
}
|
|
121
|
+
/** The only route from an isolated process to kernel services. Authority comes from the live call. */
|
|
122
|
+
export function createIsolatedBroker(options) {
|
|
123
|
+
const { ctx } = options;
|
|
124
|
+
const loggedAt = new Map();
|
|
125
|
+
let modelRequests = 0;
|
|
126
|
+
const readers = new Handles();
|
|
127
|
+
const leases = new Handles();
|
|
128
|
+
const uploads = new Handles();
|
|
129
|
+
const own = (org, supplied) => {
|
|
130
|
+
if (org !== supplied)
|
|
131
|
+
return refuse();
|
|
132
|
+
};
|
|
133
|
+
const unary = (domain, name, orgAt) => (args, org) => {
|
|
134
|
+
if (orgAt === "first")
|
|
135
|
+
own(org, args[0]);
|
|
136
|
+
if (orgAt === "object")
|
|
137
|
+
own(org, record(args[0]).orgId);
|
|
138
|
+
const api = ctx[domain];
|
|
139
|
+
const method = api[name];
|
|
140
|
+
if (typeof method !== "function")
|
|
141
|
+
return refuse();
|
|
142
|
+
return Reflect.apply(method, api, args);
|
|
143
|
+
};
|
|
144
|
+
const openReader = (scope, body) => {
|
|
145
|
+
const reader = body.getReader();
|
|
146
|
+
return readers.add(scope, { reader, reading: false }, () => reader.cancel());
|
|
147
|
+
};
|
|
148
|
+
const modelResult = async (run, scope) => {
|
|
149
|
+
if (modelRequests >= 2)
|
|
150
|
+
throw new IsolatedRpcError("overloaded");
|
|
151
|
+
modelRequests++;
|
|
152
|
+
let released = false;
|
|
153
|
+
const release = () => {
|
|
154
|
+
if (released)
|
|
155
|
+
return;
|
|
156
|
+
released = true;
|
|
157
|
+
modelRequests--;
|
|
158
|
+
};
|
|
159
|
+
try {
|
|
160
|
+
const text = JSON.stringify(await run());
|
|
161
|
+
const size = Buffer.byteLength(text);
|
|
162
|
+
if (size > ISOLATED_MODEL_RESULT_BYTES)
|
|
163
|
+
throw new IsolatedRpcError("overloaded");
|
|
164
|
+
const data = Buffer.from(text);
|
|
165
|
+
let offset = 0;
|
|
166
|
+
const reader = new ReadableStream({
|
|
167
|
+
pull(controller) {
|
|
168
|
+
if (offset === data.length)
|
|
169
|
+
controller.close();
|
|
170
|
+
else {
|
|
171
|
+
const next = Math.min(data.length, offset + CHUNK_BYTES);
|
|
172
|
+
controller.enqueue(data.subarray(offset, next));
|
|
173
|
+
offset = next;
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
}).getReader();
|
|
177
|
+
return {
|
|
178
|
+
bytes: size,
|
|
179
|
+
stream: readers.add(scope, { reader, reading: false }, async () => {
|
|
180
|
+
try {
|
|
181
|
+
await reader.cancel();
|
|
182
|
+
}
|
|
183
|
+
finally {
|
|
184
|
+
release();
|
|
185
|
+
}
|
|
186
|
+
}),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
release();
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
const isCurrent = (signal) => !signal.aborted && options.current();
|
|
195
|
+
const fetch = async (urlValue, initValue, org, signal) => {
|
|
196
|
+
const url = new URL(string(urlValue));
|
|
197
|
+
if (!(await options.network(url, org)) || !isCurrent(signal))
|
|
198
|
+
return refuse();
|
|
199
|
+
const init = requestInit.parse(initValue);
|
|
200
|
+
return ctx.net.fetch(url.href, {
|
|
201
|
+
method: init.method,
|
|
202
|
+
headers: init.headers,
|
|
203
|
+
redirect: init.redirect ?? "error",
|
|
204
|
+
...(init.body !== undefined && { body: bytes(init.body) }),
|
|
205
|
+
signal,
|
|
206
|
+
}, org === undefined ? {} : { orgId: org });
|
|
207
|
+
};
|
|
208
|
+
const dispatch = {
|
|
209
|
+
"kv.get": ([key], org) => ctx.kv.get(`${org}:${string(key)}`),
|
|
210
|
+
"kv.set": ([key, value, opts], org) => ctx.kv.set(`${org}:${string(key)}`, string(value), z.strictObject({ ttlSeconds: z.number().int().min(1).max(604800) }).parse(opts)),
|
|
211
|
+
"kv.del": ([key], org) => ctx.kv.del(`${org}:${string(key)}`),
|
|
212
|
+
"kv.incr": (args, org) => incrementIsolatedKv(ctx, args, org),
|
|
213
|
+
"bus.publish": ([topic, payload], org) => ctx.bus.publish(string(topic), { orgId: org, payload }),
|
|
214
|
+
"queue.add": ([name, data, opts], org) => {
|
|
215
|
+
const queue = string(name);
|
|
216
|
+
if (!ctx.hasEffectQueue(queue))
|
|
217
|
+
return refuse();
|
|
218
|
+
const { jobId, delayMs } = z
|
|
219
|
+
.strictObject({
|
|
220
|
+
jobId: z.string().min(1).max(256).optional(),
|
|
221
|
+
delayMs: z
|
|
222
|
+
.number()
|
|
223
|
+
.int()
|
|
224
|
+
.min(0)
|
|
225
|
+
.max(365 * 24 * 60 * 60 * 1_000)
|
|
226
|
+
.optional(),
|
|
227
|
+
})
|
|
228
|
+
.parse(opts ?? {});
|
|
229
|
+
const idempotencyKey = jobId === undefined
|
|
230
|
+
? randomUUID()
|
|
231
|
+
: createHash("sha256")
|
|
232
|
+
.update(JSON.stringify([queue, jobId]))
|
|
233
|
+
.digest("hex");
|
|
234
|
+
return ctx.outbox.add({
|
|
235
|
+
kind: "queue",
|
|
236
|
+
orgId: org,
|
|
237
|
+
queue,
|
|
238
|
+
data,
|
|
239
|
+
...(delayMs !== undefined && { runAt: new Date(Date.now() + delayMs).toISOString() }),
|
|
240
|
+
}, { idempotencyKey });
|
|
241
|
+
},
|
|
242
|
+
"ui.invalidate": unary("ui", "invalidate", "object"),
|
|
243
|
+
"agents.usableBy": unary("agents", "usableBy", "object"),
|
|
244
|
+
"conversations.bySubject": unary("conversations", "bySubject", "object"),
|
|
245
|
+
"people.get": unary("people", "get", "object"),
|
|
246
|
+
"people.search": unary("people", "search", "object"),
|
|
247
|
+
"storage.head": unary("storage", "head", "first"),
|
|
248
|
+
"storage.delete": unary("storage", "delete", "first"),
|
|
249
|
+
"storage.deletePrefix": unary("storage", "deletePrefix", "first"),
|
|
250
|
+
"storage.list": unary("storage", "list", "first"),
|
|
251
|
+
"inbox.resolve": unary("inbox", "resolve", "object"),
|
|
252
|
+
"inbox.post": (args, org) => {
|
|
253
|
+
own(org, record(args[0]).orgId);
|
|
254
|
+
assertEffectKey(record(args[1]).idempotencyKey);
|
|
255
|
+
return call(ctx.inbox.post.bind(ctx.inbox), args.slice(0, 2));
|
|
256
|
+
},
|
|
257
|
+
"triggers.emit": (args, org, scope) => {
|
|
258
|
+
own(org, record(args[0]).orgId);
|
|
259
|
+
assertEffectKey(record(args[1]).idempotencyKey);
|
|
260
|
+
return call(ctx.triggers.emit.bind(ctx.triggers), [
|
|
261
|
+
wireCause(args[0], scope),
|
|
262
|
+
args[1],
|
|
263
|
+
]);
|
|
264
|
+
},
|
|
265
|
+
"triggers.causeOf": (args, _org, scope) => {
|
|
266
|
+
if (!scope.toolCall || args.length !== 0)
|
|
267
|
+
return refuse();
|
|
268
|
+
return call(ctx.triggers.causeOf.bind(ctx.triggers), [scope.toolCall]);
|
|
269
|
+
},
|
|
270
|
+
"outbox.add": (args, org) => {
|
|
271
|
+
own(org, record(args[0]).orgId);
|
|
272
|
+
assertEffectKey(record(args[1]).idempotencyKey);
|
|
273
|
+
return call(ctx.outbox.add.bind(ctx.outbox), args.slice(0, 2));
|
|
274
|
+
},
|
|
275
|
+
"outbox.stage": (args, org, scope) => {
|
|
276
|
+
const effect = args[0];
|
|
277
|
+
const key = args[1];
|
|
278
|
+
assertEffectKey(key);
|
|
279
|
+
switch (effect?.kind) {
|
|
280
|
+
case "trigger":
|
|
281
|
+
own(org, effect.data.orgId);
|
|
282
|
+
return options
|
|
283
|
+
.effects()
|
|
284
|
+
.prepare(options.plugin, org, key, { ...effect, data: wireCause(effect.data, scope) });
|
|
285
|
+
case "inbox.post":
|
|
286
|
+
own(org, effect.data.orgId);
|
|
287
|
+
break;
|
|
288
|
+
case "inbox.resolve":
|
|
289
|
+
case "queue":
|
|
290
|
+
own(org, effect.orgId);
|
|
291
|
+
break;
|
|
292
|
+
default:
|
|
293
|
+
return refuse();
|
|
294
|
+
}
|
|
295
|
+
return options.effects().prepare(options.plugin, org, key, effect);
|
|
296
|
+
},
|
|
297
|
+
"outbox.confirmKey": async ([id]) => (await options.keys().forVerification()).active === string(id),
|
|
298
|
+
"models.describe": unary("models", "describe", "object"),
|
|
299
|
+
"models.embed": ([input], org, scope, signal) => {
|
|
300
|
+
own(org, record(input).orgId);
|
|
301
|
+
return modelResult(() => call(ctx.models.embed.bind(ctx.models), [{ ...record(input), signal }]), scope);
|
|
302
|
+
},
|
|
303
|
+
"models.generateImage": ([input], org, scope, signal) => {
|
|
304
|
+
own(org, record(input).orgId);
|
|
305
|
+
return modelResult(() => call(ctx.models.generateImage.bind(ctx.models), [{ ...record(input), signal }]), scope);
|
|
306
|
+
},
|
|
307
|
+
"net.fetch": ([url, init, requested], org, _scope, signal) => {
|
|
308
|
+
if (requested !== undefined && requested !== null)
|
|
309
|
+
own(org, record(requested).orgId ?? org);
|
|
310
|
+
return fetch(url, init, org, signal);
|
|
311
|
+
},
|
|
312
|
+
"storage.get": async ([supplied, key, opts], org, scope, signal) => {
|
|
313
|
+
own(org, supplied);
|
|
314
|
+
const found = await ctx.storage.get(org, string(key), {
|
|
315
|
+
...record(opts ?? {}),
|
|
316
|
+
signal,
|
|
317
|
+
});
|
|
318
|
+
return found && { head: found.head, stream: openReader(scope, found.body) };
|
|
319
|
+
},
|
|
320
|
+
"storage.openPut": ([supplied, key, opts], org, scope, signal) => {
|
|
321
|
+
own(org, supplied);
|
|
322
|
+
const { contentType, size } = z
|
|
323
|
+
.strictObject({
|
|
324
|
+
contentType: z.string().min(1).max(256),
|
|
325
|
+
size: z.number().int().nonnegative().optional(),
|
|
326
|
+
})
|
|
327
|
+
.parse(opts);
|
|
328
|
+
const stream = new TransformStream();
|
|
329
|
+
const writer = stream.writable.getWriter();
|
|
330
|
+
const result = ctx.storage.put(org, string(key), stream.readable, {
|
|
331
|
+
contentType,
|
|
332
|
+
...(size !== undefined && { size }),
|
|
333
|
+
signal,
|
|
334
|
+
});
|
|
335
|
+
void result.catch(() => { });
|
|
336
|
+
return uploads.add(scope, { writer, result, writing: false }, () => writer.abort());
|
|
337
|
+
},
|
|
338
|
+
"storage.write": async ([id, chunk], _org, scope) => {
|
|
339
|
+
const upload = uploads.get(scope, id);
|
|
340
|
+
if (upload.writing || !(chunk instanceof Uint8Array) || chunk.length > CHUNK_BYTES) {
|
|
341
|
+
return refuse();
|
|
342
|
+
}
|
|
343
|
+
upload.writing = true;
|
|
344
|
+
try {
|
|
345
|
+
await upload.writer.write(chunk);
|
|
346
|
+
}
|
|
347
|
+
finally {
|
|
348
|
+
upload.writing = false;
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
"storage.finishPut": async ([id], _org, scope) => {
|
|
352
|
+
const upload = uploads.get(scope, id);
|
|
353
|
+
if (upload.writing)
|
|
354
|
+
return refuse();
|
|
355
|
+
upload.writing = true;
|
|
356
|
+
await upload.writer.close();
|
|
357
|
+
try {
|
|
358
|
+
return await upload.result;
|
|
359
|
+
}
|
|
360
|
+
finally {
|
|
361
|
+
await uploads.close(scope, id).catch(() => { });
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
"storage.abortPut": ([id], _org, scope) => uploads.close(scope, id),
|
|
365
|
+
"lock.acquire": async ([key, input], org, scope) => {
|
|
366
|
+
const lease = await ctx.lock.acquire(`${org}:${string(key)}`, leaseInput.parse(input));
|
|
367
|
+
return lease && leases.add(scope, lease, () => lease.release());
|
|
368
|
+
},
|
|
369
|
+
"lock.renew": ([id], _org, scope) => leases.get(scope, id).renew(),
|
|
370
|
+
"lock.release": ([id], _org, scope) => leases.close(scope, id),
|
|
371
|
+
};
|
|
372
|
+
const read = async ([id], _org, scope) => {
|
|
373
|
+
const stream = readers.get(scope, id);
|
|
374
|
+
if (stream.reading)
|
|
375
|
+
return refuse();
|
|
376
|
+
stream.reading = true;
|
|
377
|
+
try {
|
|
378
|
+
if (stream.remainder === undefined) {
|
|
379
|
+
const next = await stream.reader.read();
|
|
380
|
+
if (next.done) {
|
|
381
|
+
await readers.close(scope, id);
|
|
382
|
+
return { done: true };
|
|
383
|
+
}
|
|
384
|
+
stream.remainder = next.value;
|
|
385
|
+
}
|
|
386
|
+
const chunk = stream.remainder.subarray(0, CHUNK_BYTES);
|
|
387
|
+
stream.remainder =
|
|
388
|
+
stream.remainder.length > CHUNK_BYTES ? stream.remainder.subarray(CHUNK_BYTES) : undefined;
|
|
389
|
+
return new RpcByteChunk({ done: false }, chunk);
|
|
390
|
+
}
|
|
391
|
+
finally {
|
|
392
|
+
stream.reading = false;
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
dispatch["net.read"] = read;
|
|
396
|
+
dispatch["storage.read"] = read;
|
|
397
|
+
dispatch["models.read"] = read;
|
|
398
|
+
dispatch["models.close"] = ([id], _org, scope) => readers.close(scope, id);
|
|
399
|
+
dispatch["net.close"] = ([id], _org, scope) => readers.close(scope, id);
|
|
400
|
+
dispatch["storage.close"] = ([id], _org, scope) => readers.close(scope, id);
|
|
401
|
+
return async (method, args, scope, signal) => {
|
|
402
|
+
if (signal.aborted || args.length > 4 || !method.startsWith("broker."))
|
|
403
|
+
return refuse();
|
|
404
|
+
const org = scope.orgId;
|
|
405
|
+
const name = method.slice("broker.".length);
|
|
406
|
+
const run = async () => {
|
|
407
|
+
if (!isCurrent(signal))
|
|
408
|
+
return refuse();
|
|
409
|
+
if (Object.hasOwn(LOG_METHODS, name)) {
|
|
410
|
+
const [message] = args;
|
|
411
|
+
if (typeof message !== "string" || message.length > LOG_INPUT_MAX || args.length > 2)
|
|
412
|
+
return refuse();
|
|
413
|
+
const now = performance.now();
|
|
414
|
+
if (now - (loggedAt.get(org) ?? -Infinity) < LOG_WINDOW_MS)
|
|
415
|
+
return;
|
|
416
|
+
for (const [loggedOrg, at] of loggedAt) {
|
|
417
|
+
if (now - at < LOG_WINDOW_MS)
|
|
418
|
+
break;
|
|
419
|
+
loggedAt.delete(loggedOrg);
|
|
420
|
+
}
|
|
421
|
+
loggedAt.delete(org);
|
|
422
|
+
if (loggedAt.size >= LOG_ORGS_PER_WINDOW)
|
|
423
|
+
return;
|
|
424
|
+
loggedAt.set(org, now);
|
|
425
|
+
const level = LOG_METHODS[name];
|
|
426
|
+
const redacted = options.redactLog?.(message, scope.config) ?? message;
|
|
427
|
+
// The logger redacts organization secrets in structured values. Keep child text under a
|
|
428
|
+
// fixed field and discard arbitrary fields that could spoof kernel metadata.
|
|
429
|
+
ctx.log[level]("Isolated extension log event", {
|
|
430
|
+
extensionMessage: redacted
|
|
431
|
+
.replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/gu, " ")
|
|
432
|
+
.slice(0, LOG_MESSAGE_MAX),
|
|
433
|
+
});
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (scope.mode === "setupCheck") {
|
|
437
|
+
if (name !== "net.probe" || args.length !== 1)
|
|
438
|
+
return refuse();
|
|
439
|
+
const result = await fetch(args[0], { method: "HEAD", headers: [] }, org, signal);
|
|
440
|
+
await result.body?.cancel();
|
|
441
|
+
return { status: result.status };
|
|
442
|
+
}
|
|
443
|
+
if (scope.mode !== "org" || org === undefined || !Object.hasOwn(dispatch, name))
|
|
444
|
+
return refuse();
|
|
445
|
+
return dispatch[name]?.(args, org, scope, signal);
|
|
446
|
+
};
|
|
447
|
+
const result = await (org === undefined
|
|
448
|
+
? asPlatform("Dispatch an isolated deployment callback", run)
|
|
449
|
+
: withOrgScope(org, run));
|
|
450
|
+
if (name === "net.fetch") {
|
|
451
|
+
const response = result;
|
|
452
|
+
if (!isCurrent(signal) || scope.signal?.aborted) {
|
|
453
|
+
// The request was sent under a live grant. Acknowledge it without handing over its body
|
|
454
|
+
// or potentially sensitive response headers after revocation.
|
|
455
|
+
void response.body?.cancel().catch(() => { });
|
|
456
|
+
return { status: response.status, statusText: "", headers: [] };
|
|
457
|
+
}
|
|
458
|
+
return {
|
|
459
|
+
status: response.status,
|
|
460
|
+
statusText: response.statusText,
|
|
461
|
+
headers: [...response.headers],
|
|
462
|
+
...(response.body && { stream: openReader(scope, response.body) }),
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
// The scope was checked before dispatch. Reporting a completed write as refused can cause
|
|
466
|
+
// the caller to repeat it. Reads and live handles still require current authority.
|
|
467
|
+
if (!ACKNOWLEDGED_WRITES.has(name) && !isCurrent(signal))
|
|
468
|
+
return refuse();
|
|
469
|
+
return result;
|
|
470
|
+
};
|
|
471
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type AvailableHook, type HookOptions, type JsonSchema, type SetupGuidePlace, type StaticExtensionManifest, type ToolSpec, type TriggerEventDeclaration } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import type { RpcByteChunk } from "./rpc.js";
|
|
4
|
+
export declare const ISOLATED_PACKAGE_ROOT = "/app/extension";
|
|
5
|
+
export declare const ISOLATED_STREAM_CHUNK: number;
|
|
6
|
+
export declare const ISOLATED_MODEL_RESULT_BYTES: number;
|
|
7
|
+
export declare const ISOLATED_MAX_CALLBACKS = 256;
|
|
8
|
+
export declare const childInitSchema: z.ZodObject<{
|
|
9
|
+
entry: z.ZodString;
|
|
10
|
+
nonce: z.ZodString;
|
|
11
|
+
mode: z.ZodEnum<{
|
|
12
|
+
deployment: "deployment";
|
|
13
|
+
org: "org";
|
|
14
|
+
registration: "registration";
|
|
15
|
+
setupCheck: "setupCheck";
|
|
16
|
+
}>;
|
|
17
|
+
orgId: z.ZodOptional<z.ZodString>;
|
|
18
|
+
db: z.ZodOptional<z.ZodObject<{
|
|
19
|
+
login: z.ZodString;
|
|
20
|
+
url: z.ZodString;
|
|
21
|
+
}, z.core.$strict>>;
|
|
22
|
+
rls: z.ZodOptional<z.ZodEnum<{
|
|
23
|
+
enforce: "enforce";
|
|
24
|
+
off: "off";
|
|
25
|
+
scoped: "scoped";
|
|
26
|
+
}>>;
|
|
27
|
+
}, z.core.$strict>;
|
|
28
|
+
export type ChildInit = z.infer<typeof childInitSchema>;
|
|
29
|
+
export declare const childInvokeSchema: z.ZodObject<{
|
|
30
|
+
callback: z.ZodString;
|
|
31
|
+
invocation: z.ZodString;
|
|
32
|
+
args: z.ZodArray<z.ZodUnknown>;
|
|
33
|
+
config: z.ZodOptional<z.ZodUnknown>;
|
|
34
|
+
}, z.core.$strict>;
|
|
35
|
+
export type ChildInvoke = z.infer<typeof childInvokeSchema>;
|
|
36
|
+
export type ChildCallback = {
|
|
37
|
+
kind: "tool";
|
|
38
|
+
id: string;
|
|
39
|
+
spec: ToolSpec;
|
|
40
|
+
watchId?: string;
|
|
41
|
+
} | {
|
|
42
|
+
kind: "ui.view" | "ui.action" | "ui.query";
|
|
43
|
+
id: string;
|
|
44
|
+
target: string;
|
|
45
|
+
} | {
|
|
46
|
+
kind: "setup.check";
|
|
47
|
+
id: string;
|
|
48
|
+
target: string;
|
|
49
|
+
place: SetupGuidePlace;
|
|
50
|
+
} | {
|
|
51
|
+
kind: "queue";
|
|
52
|
+
id: string;
|
|
53
|
+
name: string;
|
|
54
|
+
concurrency?: number;
|
|
55
|
+
} | {
|
|
56
|
+
kind: "hook.observe";
|
|
57
|
+
id: string;
|
|
58
|
+
mount: AvailableHook;
|
|
59
|
+
options: HookOptions;
|
|
60
|
+
} | {
|
|
61
|
+
kind: "trigger-source";
|
|
62
|
+
id: "trigger-source";
|
|
63
|
+
events: TriggerEventDeclaration[];
|
|
64
|
+
visibleId: "trigger-source.visible";
|
|
65
|
+
subjectLinkId?: "trigger-source.subjectLink";
|
|
66
|
+
} | {
|
|
67
|
+
kind: "bus";
|
|
68
|
+
id: string;
|
|
69
|
+
topic: string;
|
|
70
|
+
};
|
|
71
|
+
export interface ChildRegistration {
|
|
72
|
+
nonce: string;
|
|
73
|
+
manifest: StaticExtensionManifest;
|
|
74
|
+
configSchemas?: {
|
|
75
|
+
deployment: JsonSchema;
|
|
76
|
+
};
|
|
77
|
+
callbacks: ChildCallback[];
|
|
78
|
+
}
|
|
79
|
+
export type ChildBrokerCall = (method: string, payload: {
|
|
80
|
+
invocation: string;
|
|
81
|
+
args: unknown[];
|
|
82
|
+
} | RpcByteChunk, options: {
|
|
83
|
+
signal: AbortSignal;
|
|
84
|
+
}) => Promise<unknown>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { isSafePackagePath, } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export const ISOLATED_PACKAGE_ROOT = "/app/extension";
|
|
4
|
+
export const ISOLATED_STREAM_CHUNK = 64 * 1024;
|
|
5
|
+
export const ISOLATED_MODEL_RESULT_BYTES = 16 * 1024 * 1024;
|
|
6
|
+
export const ISOLATED_MAX_CALLBACKS = 256;
|
|
7
|
+
const boundedId = z.string().min(1).max(256);
|
|
8
|
+
export const childInitSchema = z.strictObject({
|
|
9
|
+
entry: z.string().refine(isSafePackagePath),
|
|
10
|
+
nonce: boundedId,
|
|
11
|
+
mode: z.enum(["registration", "org", "deployment", "setupCheck"]),
|
|
12
|
+
orgId: boundedId.optional(),
|
|
13
|
+
db: z.strictObject({ login: boundedId, url: z.string().min(1).max(8192) }).optional(),
|
|
14
|
+
rls: z.enum(["off", "scoped", "enforce"]).optional(),
|
|
15
|
+
});
|
|
16
|
+
export const childInvokeSchema = z.strictObject({
|
|
17
|
+
callback: boundedId,
|
|
18
|
+
invocation: boundedId,
|
|
19
|
+
args: z.array(z.unknown()).max(8),
|
|
20
|
+
config: z.unknown().optional(),
|
|
21
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ExtensionContext } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { type ChildCallback } from "./child-protocol.js";
|
|
3
|
+
type Handler = (args: unknown[], signal: AbortSignal, config: unknown) => unknown | Promise<unknown>;
|
|
4
|
+
type Runners = Pick<ExtensionContext, "hooks" | "provide" | "setup" | "tools">;
|
|
5
|
+
/** Functions stay in the child. The kernel receives only validated registration data and IDs. */
|
|
6
|
+
export declare class ChildRegistrationLedger {
|
|
7
|
+
private readonly callbacks;
|
|
8
|
+
private readonly handlers;
|
|
9
|
+
private sealed;
|
|
10
|
+
private nextOrdinal;
|
|
11
|
+
get declarations(): ChildCallback[];
|
|
12
|
+
seal(): void;
|
|
13
|
+
handler(id: string): Handler;
|
|
14
|
+
private add;
|
|
15
|
+
private static withCall;
|
|
16
|
+
runners(): Runners;
|
|
17
|
+
ui<K extends "view" | "action" | "query">(key: K, target: string, handler: (arg: unknown) => unknown): void;
|
|
18
|
+
queue(name: string, handler: (data: unknown) => Promise<void>, concurrency?: number): void;
|
|
19
|
+
bus(topic: string, handler: (data: unknown) => void): string;
|
|
20
|
+
unsubscribe(id: string): void;
|
|
21
|
+
}
|
|
22
|
+
export {};
|