@capybara-hq/plugin-device-local 0.1.0-rc.2 → 0.1.0-rc.4
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 +10 -0
- package/dist/index.mjs +67 -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.4",
|
|
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.4",
|
|
27
27
|
"sdk": "^0.1.0-rc.56",
|
|
28
28
|
"provides": [
|
|
29
29
|
"device-provider@1"
|
package/README.md
CHANGED
|
@@ -64,6 +64,16 @@ 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, or an unreadable list in a readable one, keeps them. The
|
|
76
|
+
line fits as many whole directories as it can, skipping one too long, then ` +N` for the rest.
|
|
67
77
|
- **Refused project paths.** A machine's `project_refused` outcome becomes
|
|
68
78
|
`ProjectPathRefusedError` with the fixed English and Chinese text of its refusal code
|
|
69
79
|
(`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,42 @@ 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?.sharedProjectRoots === void 0) 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
|
+
const rest = (n) => n > 0 ? ` +${n}` : "";
|
|
191
|
+
const kept = [];
|
|
192
|
+
for (const root of roots) {
|
|
193
|
+
const value = `${[...kept, root].join(", ")}${rest(roots.length - kept.length - 1)}`;
|
|
194
|
+
if (value.length <= max) kept.push(root);
|
|
195
|
+
}
|
|
196
|
+
if (kept.length > 0) return `${kept.join(", ")}${rest(roots.length - kept.length)}`;
|
|
197
|
+
const shortest = roots.reduce(
|
|
198
|
+
(best, r) => best === void 0 || r.length < best.length ? r : best,
|
|
199
|
+
void 0
|
|
200
|
+
);
|
|
201
|
+
const more = rest(roots.length - 1);
|
|
202
|
+
const chars = Array.from(shortest ?? "");
|
|
203
|
+
let tail = "";
|
|
204
|
+
for (let i = chars.length - 1; i >= 0; i--) {
|
|
205
|
+
const next = `${chars[i]}${tail}`;
|
|
206
|
+
if (`…${next}${more}`.length > max) break;
|
|
207
|
+
tail = next;
|
|
208
|
+
}
|
|
209
|
+
return `…${tail}${more}`;
|
|
210
|
+
}
|
|
172
211
|
var hosts = (n) => n === 1 ? { en: "1 allowed host", zh: "允许 1 个主机" } : { en: `${n} allowed hosts`, zh: `允许 ${n} 个主机` };
|
|
173
212
|
var effectiveReach = (mode, reach) => mode === "docker" ? "workspace" : reach;
|
|
174
213
|
var READS = {
|
|
@@ -231,6 +270,8 @@ function limitsOf(c, owner) {
|
|
|
231
270
|
label: { en: "Allowed hosts", zh: "允许的主机" },
|
|
232
271
|
value: String(owner.allowedDomains.length)
|
|
233
272
|
});
|
|
273
|
+
if (c.sharedProjectRoots.length > 0)
|
|
274
|
+
lines.push({ label: SHARED_LABEL, value: sharedRootsValue(c.sharedProjectRoots) });
|
|
234
275
|
return lines;
|
|
235
276
|
}
|
|
236
277
|
function deviceReportOf(c, b) {
|
|
@@ -361,6 +402,8 @@ function decideHello(s, deviceId, hello, conn, now) {
|
|
|
361
402
|
features: [...hello.features ?? []],
|
|
362
403
|
permissionSupport: hello.permissionSupport,
|
|
363
404
|
ownerReport: applied.next ?? stored?.ownerReport ?? null,
|
|
405
|
+
// a hello restarts what the machine said: an unreadable summary shows none
|
|
406
|
+
sharedProjectRoots: sharedRootsOf(hello.policy, []),
|
|
364
407
|
instanceGeneration: gen,
|
|
365
408
|
runtimeIdentity: hello.runtimeIdentity ?? null
|
|
366
409
|
};
|
|
@@ -399,8 +442,10 @@ function createDialInHandler(ctx, store, now = () => (/* @__PURE__ */ new Date()
|
|
|
399
442
|
const out = await store.change(deviceId, (s) => {
|
|
400
443
|
const c = fn(s);
|
|
401
444
|
if ("ok" in c) return { result: c };
|
|
402
|
-
|
|
403
|
-
|
|
445
|
+
moved = reportMoved(s, {
|
|
446
|
+
connection: c.connection ?? s.connection,
|
|
447
|
+
binding: c.binding ?? s.binding
|
|
448
|
+
});
|
|
404
449
|
return c;
|
|
405
450
|
});
|
|
406
451
|
if (moved) await tell(deviceId);
|
|
@@ -427,10 +472,13 @@ function createDialInHandler(ctx, store, now = () => (/* @__PURE__ */ new Date()
|
|
|
427
472
|
return { ok: true };
|
|
428
473
|
},
|
|
429
474
|
/**
|
|
430
|
-
* A `policy` event: the owner changed the machine's ceiling while connected.
|
|
431
|
-
* the owner-report rule (stale ignored, conflicting blocks);
|
|
432
|
-
*
|
|
433
|
-
*
|
|
475
|
+
* A `policy` event: the owner changed the machine's ceiling or policy while connected. The
|
|
476
|
+
* ceiling is recorded through the owner-report rule (stale ignored, conflicting blocks); the
|
|
477
|
+
* summary's shared project directories are taken only with a current ceiling (recorded or the
|
|
478
|
+
* same again; an unreadable summary keeps them), never from a stale or conflicting event. An
|
|
479
|
+
* invalid body is logged and dropped (the kernel keeps the connection; the machine's own
|
|
480
|
+
* enforcement stands), and so is a frame of any connection other than the one whose hello was
|
|
481
|
+
* recorded last.
|
|
434
482
|
*/
|
|
435
483
|
async onFrame(deviceId, frame, conn) {
|
|
436
484
|
let event;
|
|
@@ -450,7 +498,12 @@ function createDialInHandler(ctx, store, now = () => (/* @__PURE__ */ new Date()
|
|
|
450
498
|
);
|
|
451
499
|
if (applied.outcome === "conflict")
|
|
452
500
|
ctx.log.warn("a machine's owner report conflicts with the recorded one", { deviceId });
|
|
453
|
-
|
|
501
|
+
const current = applied.outcome === "recorded" || applied.outcome === "same";
|
|
502
|
+
const sharedProjectRoots = current ? sharedRootsOf(event.policy, s.connection.sharedProjectRoots) : s.connection.sharedProjectRoots;
|
|
503
|
+
return applied.next ? {
|
|
504
|
+
result: applied.outcome,
|
|
505
|
+
connection: { ...s.connection, ownerReport: applied.next, sharedProjectRoots }
|
|
506
|
+
} : { result: applied.outcome };
|
|
454
507
|
});
|
|
455
508
|
},
|
|
456
509
|
/**
|
|
@@ -4262,6 +4315,7 @@ function connectionOf(r) {
|
|
|
4262
4315
|
features: FEATURES.filter((f) => features.includes(f)),
|
|
4263
4316
|
permissionSupport,
|
|
4264
4317
|
ownerReport: storedOwnerReportOf(json(r.owner_report)),
|
|
4318
|
+
sharedProjectRoots: Array.isArray(r.shared_project_roots) ? r.shared_project_roots.filter((p) => typeof p === "string") : [],
|
|
4265
4319
|
instanceGeneration: typeof r.instance_generation === "string" ? r.instance_generation : null,
|
|
4266
4320
|
runtimeIdentity: identityOf(r.runtime_identity)
|
|
4267
4321
|
};
|
|
@@ -4287,6 +4341,7 @@ async function writeConnection(db, c) {
|
|
|
4287
4341
|
features: c.features,
|
|
4288
4342
|
permission_support: JSON.stringify(c.permissionSupport),
|
|
4289
4343
|
owner_report: c.ownerReport === null ? null : JSON.stringify(c.ownerReport),
|
|
4344
|
+
shared_project_roots: c.sharedProjectRoots,
|
|
4290
4345
|
instance_generation: c.instanceGeneration,
|
|
4291
4346
|
runtime_identity: c.runtimeIdentity === null ? null : JSON.stringify(c.runtimeIdentity),
|
|
4292
4347
|
updated_at: sql`now()`
|
|
@@ -4599,7 +4654,7 @@ function localHandle(h) {
|
|
|
4599
4654
|
const l = h;
|
|
4600
4655
|
return l.roots && l.limits && typeof l.generation === "string" ? h : void 0;
|
|
4601
4656
|
}
|
|
4602
|
-
var
|
|
4657
|
+
var sameRoots2 = (a, b) => a.workspace === b.workspace && a.skills === b.skills && a.tmp === b.tmp;
|
|
4603
4658
|
var bytesOf = (b64) => new Uint8Array(Buffer.from(b64, "base64"));
|
|
4604
4659
|
var b64Of = (data) => Buffer.from(data).toString("base64");
|
|
4605
4660
|
var refuseRaw = (method) => async (..._args) => {
|
|
@@ -4831,7 +4886,7 @@ function createDeviceLocalProvider(ctx, store, opts = {}) {
|
|
|
4831
4886
|
const current = await store.binding(l.id);
|
|
4832
4887
|
if (current?.generation !== l.generation) return "gone";
|
|
4833
4888
|
const roots = await machineCreate(l.id, l.limits, signal);
|
|
4834
|
-
if (!
|
|
4889
|
+
if (!sameRoots2(roots, l.roots)) return "gone";
|
|
4835
4890
|
return await bindings.rebind(l.id, l.generation, signal) === "bound" ? "resumed" : "gone";
|
|
4836
4891
|
},
|
|
4837
4892
|
async suspend(h, signal) {
|
|
@@ -4866,7 +4921,7 @@ function createDeviceLocalProvider(ctx, store, opts = {}) {
|
|
|
4866
4921
|
// src/index.ts
|
|
4867
4922
|
var plugin = defineExtension({
|
|
4868
4923
|
name: "device-local",
|
|
4869
|
-
version: "0.1.0-rc.
|
|
4924
|
+
version: "0.1.0-rc.4",
|
|
4870
4925
|
// ctx.dialIn.capacity and the handlers' connection argument are the rc.56 SDK's
|
|
4871
4926
|
sdk: "^0.1.0-rc.56",
|
|
4872
4927
|
provides: [DEVICE_PROVIDER_EXTENSION_POINT],
|
|
@@ -4922,7 +4977,9 @@ export {
|
|
|
4922
4977
|
projectRefusalError,
|
|
4923
4978
|
relayer,
|
|
4924
4979
|
reportFor,
|
|
4980
|
+
reportMoved,
|
|
4925
4981
|
revisionOf,
|
|
4982
|
+
sharedRootsValue,
|
|
4926
4983
|
storeOver,
|
|
4927
4984
|
storedOwnerReportOf,
|
|
4928
4985
|
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.4",
|
|
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",
|