@capybara-hq/plugin-device-local 0.1.0-rc.2 → 0.1.0-rc.3
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/.capybara-plugin/plugin.json +2 -2
- package/README.md +9 -0
- package/dist/index.mjs +61 -10
- package/migrations/0002_shared_project_roots.sql +4 -0
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 1,
|
|
3
3
|
"name": "device-local",
|
|
4
|
-
"version": "0.1.0-rc.
|
|
4
|
+
"version": "0.1.0-rc.3",
|
|
5
5
|
"publisher": "Capybara",
|
|
6
6
|
"description": {
|
|
7
7
|
"en": "Devices on people's own computers: a machine connected with capybara-device serves the device over the dial-in channel, and its owner limits it there",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"entry": "./dist/index.mjs",
|
|
24
24
|
"manifest": {
|
|
25
25
|
"name": "device-local",
|
|
26
|
-
"version": "0.1.0-rc.
|
|
26
|
+
"version": "0.1.0-rc.3",
|
|
27
27
|
"sdk": "^0.1.0-rc.56",
|
|
28
28
|
"provides": [
|
|
29
29
|
"device-provider@1"
|
package/README.md
CHANGED
|
@@ -64,6 +64,15 @@ generation under another runtime identity is refused (`local.invalid_hello`,
|
|
|
64
64
|
so a reconnect with the same facts keeps it. After a hello or event moved the revision the
|
|
65
65
|
provider calls `ctx.devices.reportChanged`; after a create or resume the kernel reads the report
|
|
66
66
|
again itself.
|
|
67
|
+
- **Shared project directories.** The machine's policy summary (hello and `policy` event) may carry
|
|
68
|
+
`sharedProjectRoots`: this connection's project directories another connection on the same
|
|
69
|
+
computer also uses, only those the owner listed in this connection's `projectPaths`, never naming
|
|
70
|
+
the other connection. The report lists them as "Also used by another connection on this
|
|
71
|
+
computer" (no line when none, or from an older device). They are left out of the revision: the
|
|
72
|
+
kernel moves the device authority epoch on every new revision and they change nothing the device
|
|
73
|
+
may do, so a change calls `ctx.devices.reportChanged` once with the revision kept and the kernel
|
|
74
|
+
stores the new text without invalidating approvals. A stale or conflicting `policy` event never
|
|
75
|
+
changes them; an unreadable summary keeps them.
|
|
67
76
|
- **Refused project paths.** A machine's `project_refused` outcome becomes
|
|
68
77
|
`ProjectPathRefusedError` with the fixed English and Chinese text of its refusal code
|
|
69
78
|
(`other-connection`, `outside-reach`, `denied`; `PROJECT_REFUSAL_TEXT`): the machine is not
|
package/dist/index.mjs
CHANGED
|
@@ -101,6 +101,9 @@ function applyOwnerReport(stored, incoming, via, now) {
|
|
|
101
101
|
|
|
102
102
|
// src/report.ts
|
|
103
103
|
import { createHash } from "node:crypto";
|
|
104
|
+
import {
|
|
105
|
+
DEVICE_REPORT_LIMITS
|
|
106
|
+
} from "@capybara-hq/plugin-sdk";
|
|
104
107
|
import {
|
|
105
108
|
parseSandboxPermissionSupport
|
|
106
109
|
} from "@capybara-hq/plugin-sdk/device-protocol";
|
|
@@ -169,6 +172,36 @@ function revisionFacts(c, b) {
|
|
|
169
172
|
};
|
|
170
173
|
}
|
|
171
174
|
var revisionOf = (c, b) => createHash("sha256").update(JSON.stringify(revisionFacts(c, b))).digest("hex");
|
|
175
|
+
function sharedRootsOf(policy, prev) {
|
|
176
|
+
if (!policy) return [...prev];
|
|
177
|
+
return [...new Set(policy.sharedProjectRoots ?? [])].sort();
|
|
178
|
+
}
|
|
179
|
+
var sameRoots = (a, b) => a.length === b.length && a.every((r, i) => r === b[i]);
|
|
180
|
+
var reportMoved = (before, after) => revisionOf(before.connection, before.binding) !== revisionOf(after.connection, after.binding) || !sameRoots(
|
|
181
|
+
before.connection?.sharedProjectRoots ?? [],
|
|
182
|
+
after.connection?.sharedProjectRoots ?? []
|
|
183
|
+
);
|
|
184
|
+
var SHARED_LABEL = {
|
|
185
|
+
en: "Also used by another connection on this computer",
|
|
186
|
+
zh: "本机的另一个连接也在使用"
|
|
187
|
+
};
|
|
188
|
+
function sharedRootsValue(roots) {
|
|
189
|
+
const max = DEVICE_REPORT_LIMITS.limitValueChars;
|
|
190
|
+
for (let k = roots.length; k >= 1; k--) {
|
|
191
|
+
const more2 = k < roots.length ? ` +${roots.length - k} more` : "";
|
|
192
|
+
const value = `${roots.slice(0, k).join(", ")}${more2}`;
|
|
193
|
+
if (value.length <= max) return value;
|
|
194
|
+
}
|
|
195
|
+
const more = roots.length > 1 ? ` +${roots.length - 1} more` : "";
|
|
196
|
+
const chars = Array.from(roots[0] ?? "");
|
|
197
|
+
let tail = "";
|
|
198
|
+
for (let i = chars.length - 1; i >= 0; i--) {
|
|
199
|
+
const next = `${chars[i]}${tail}`;
|
|
200
|
+
if (`…${next}${more}`.length > max) break;
|
|
201
|
+
tail = next;
|
|
202
|
+
}
|
|
203
|
+
return `…${tail}${more}`;
|
|
204
|
+
}
|
|
172
205
|
var hosts = (n) => n === 1 ? { en: "1 allowed host", zh: "允许 1 个主机" } : { en: `${n} allowed hosts`, zh: `允许 ${n} 个主机` };
|
|
173
206
|
var effectiveReach = (mode, reach) => mode === "docker" ? "workspace" : reach;
|
|
174
207
|
var READS = {
|
|
@@ -231,6 +264,8 @@ function limitsOf(c, owner) {
|
|
|
231
264
|
label: { en: "Allowed hosts", zh: "允许的主机" },
|
|
232
265
|
value: String(owner.allowedDomains.length)
|
|
233
266
|
});
|
|
267
|
+
if (c.sharedProjectRoots.length > 0)
|
|
268
|
+
lines.push({ label: SHARED_LABEL, value: sharedRootsValue(c.sharedProjectRoots) });
|
|
234
269
|
return lines;
|
|
235
270
|
}
|
|
236
271
|
function deviceReportOf(c, b) {
|
|
@@ -361,6 +396,8 @@ function decideHello(s, deviceId, hello, conn, now) {
|
|
|
361
396
|
features: [...hello.features ?? []],
|
|
362
397
|
permissionSupport: hello.permissionSupport,
|
|
363
398
|
ownerReport: applied.next ?? stored?.ownerReport ?? null,
|
|
399
|
+
// a hello restarts what the machine said: an unreadable summary shows none
|
|
400
|
+
sharedProjectRoots: sharedRootsOf(hello.policy, []),
|
|
364
401
|
instanceGeneration: gen,
|
|
365
402
|
runtimeIdentity: hello.runtimeIdentity ?? null
|
|
366
403
|
};
|
|
@@ -399,8 +436,10 @@ function createDialInHandler(ctx, store, now = () => (/* @__PURE__ */ new Date()
|
|
|
399
436
|
const out = await store.change(deviceId, (s) => {
|
|
400
437
|
const c = fn(s);
|
|
401
438
|
if ("ok" in c) return { result: c };
|
|
402
|
-
|
|
403
|
-
|
|
439
|
+
moved = reportMoved(s, {
|
|
440
|
+
connection: c.connection ?? s.connection,
|
|
441
|
+
binding: c.binding ?? s.binding
|
|
442
|
+
});
|
|
404
443
|
return c;
|
|
405
444
|
});
|
|
406
445
|
if (moved) await tell(deviceId);
|
|
@@ -427,10 +466,13 @@ function createDialInHandler(ctx, store, now = () => (/* @__PURE__ */ new Date()
|
|
|
427
466
|
return { ok: true };
|
|
428
467
|
},
|
|
429
468
|
/**
|
|
430
|
-
* A `policy` event: the owner changed the machine's ceiling while connected.
|
|
431
|
-
* the owner-report rule (stale ignored, conflicting blocks);
|
|
432
|
-
*
|
|
433
|
-
*
|
|
469
|
+
* A `policy` event: the owner changed the machine's ceiling or policy while connected. The
|
|
470
|
+
* ceiling is recorded through the owner-report rule (stale ignored, conflicting blocks); the
|
|
471
|
+
* summary's shared project directories are taken only with a current ceiling (recorded or the
|
|
472
|
+
* same again; an unreadable summary keeps them), never from a stale or conflicting event. An
|
|
473
|
+
* invalid body is logged and dropped (the kernel keeps the connection; the machine's own
|
|
474
|
+
* enforcement stands), and so is a frame of any connection other than the one whose hello was
|
|
475
|
+
* recorded last.
|
|
434
476
|
*/
|
|
435
477
|
async onFrame(deviceId, frame, conn) {
|
|
436
478
|
let event;
|
|
@@ -450,7 +492,12 @@ function createDialInHandler(ctx, store, now = () => (/* @__PURE__ */ new Date()
|
|
|
450
492
|
);
|
|
451
493
|
if (applied.outcome === "conflict")
|
|
452
494
|
ctx.log.warn("a machine's owner report conflicts with the recorded one", { deviceId });
|
|
453
|
-
|
|
495
|
+
const current = applied.outcome === "recorded" || applied.outcome === "same";
|
|
496
|
+
const sharedProjectRoots = current ? sharedRootsOf(event.policy, s.connection.sharedProjectRoots) : s.connection.sharedProjectRoots;
|
|
497
|
+
return applied.next ? {
|
|
498
|
+
result: applied.outcome,
|
|
499
|
+
connection: { ...s.connection, ownerReport: applied.next, sharedProjectRoots }
|
|
500
|
+
} : { result: applied.outcome };
|
|
454
501
|
});
|
|
455
502
|
},
|
|
456
503
|
/**
|
|
@@ -4262,6 +4309,7 @@ function connectionOf(r) {
|
|
|
4262
4309
|
features: FEATURES.filter((f) => features.includes(f)),
|
|
4263
4310
|
permissionSupport,
|
|
4264
4311
|
ownerReport: storedOwnerReportOf(json(r.owner_report)),
|
|
4312
|
+
sharedProjectRoots: Array.isArray(r.shared_project_roots) ? r.shared_project_roots.filter((p) => typeof p === "string") : [],
|
|
4265
4313
|
instanceGeneration: typeof r.instance_generation === "string" ? r.instance_generation : null,
|
|
4266
4314
|
runtimeIdentity: identityOf(r.runtime_identity)
|
|
4267
4315
|
};
|
|
@@ -4287,6 +4335,7 @@ async function writeConnection(db, c) {
|
|
|
4287
4335
|
features: c.features,
|
|
4288
4336
|
permission_support: JSON.stringify(c.permissionSupport),
|
|
4289
4337
|
owner_report: c.ownerReport === null ? null : JSON.stringify(c.ownerReport),
|
|
4338
|
+
shared_project_roots: c.sharedProjectRoots,
|
|
4290
4339
|
instance_generation: c.instanceGeneration,
|
|
4291
4340
|
runtime_identity: c.runtimeIdentity === null ? null : JSON.stringify(c.runtimeIdentity),
|
|
4292
4341
|
updated_at: sql`now()`
|
|
@@ -4599,7 +4648,7 @@ function localHandle(h) {
|
|
|
4599
4648
|
const l = h;
|
|
4600
4649
|
return l.roots && l.limits && typeof l.generation === "string" ? h : void 0;
|
|
4601
4650
|
}
|
|
4602
|
-
var
|
|
4651
|
+
var sameRoots2 = (a, b) => a.workspace === b.workspace && a.skills === b.skills && a.tmp === b.tmp;
|
|
4603
4652
|
var bytesOf = (b64) => new Uint8Array(Buffer.from(b64, "base64"));
|
|
4604
4653
|
var b64Of = (data) => Buffer.from(data).toString("base64");
|
|
4605
4654
|
var refuseRaw = (method) => async (..._args) => {
|
|
@@ -4831,7 +4880,7 @@ function createDeviceLocalProvider(ctx, store, opts = {}) {
|
|
|
4831
4880
|
const current = await store.binding(l.id);
|
|
4832
4881
|
if (current?.generation !== l.generation) return "gone";
|
|
4833
4882
|
const roots = await machineCreate(l.id, l.limits, signal);
|
|
4834
|
-
if (!
|
|
4883
|
+
if (!sameRoots2(roots, l.roots)) return "gone";
|
|
4835
4884
|
return await bindings.rebind(l.id, l.generation, signal) === "bound" ? "resumed" : "gone";
|
|
4836
4885
|
},
|
|
4837
4886
|
async suspend(h, signal) {
|
|
@@ -4866,7 +4915,7 @@ function createDeviceLocalProvider(ctx, store, opts = {}) {
|
|
|
4866
4915
|
// src/index.ts
|
|
4867
4916
|
var plugin = defineExtension({
|
|
4868
4917
|
name: "device-local",
|
|
4869
|
-
version: "0.1.0-rc.
|
|
4918
|
+
version: "0.1.0-rc.3",
|
|
4870
4919
|
// ctx.dialIn.capacity and the handlers' connection argument are the rc.56 SDK's
|
|
4871
4920
|
sdk: "^0.1.0-rc.56",
|
|
4872
4921
|
provides: [DEVICE_PROVIDER_EXTENSION_POINT],
|
|
@@ -4922,7 +4971,9 @@ export {
|
|
|
4922
4971
|
projectRefusalError,
|
|
4923
4972
|
relayer,
|
|
4924
4973
|
reportFor,
|
|
4974
|
+
reportMoved,
|
|
4925
4975
|
revisionOf,
|
|
4976
|
+
sharedRootsValue,
|
|
4926
4977
|
storeOver,
|
|
4927
4978
|
storedOwnerReportOf,
|
|
4928
4979
|
supportFor,
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
-- The project directories of the connection that another connection on the same machine also
|
|
2
|
+
-- uses, from the machine's last policy summary (only directories the owner listed for this
|
|
3
|
+
-- connection; never naming the other connection). Shown in the device report only.
|
|
4
|
+
ALTER TABLE connections ADD COLUMN IF NOT EXISTS shared_project_roots text[] NOT NULL DEFAULT '{}';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capybara-hq/plugin-device-local",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"description": "Devices on people's own computers: a machine connected with capybara-device serves the device over the dial-in channel, and its owner limits it there",
|