@capybara-hq/plugin-sdk 0.1.0-rc.19 → 0.1.0-rc.20

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.
@@ -6,14 +6,15 @@ import { type ExecResult, type SandboxActor, type SandboxLimits, type SandboxRoo
6
6
  /** The protocol version carried in `X-Capybara-Device-Protocol` and the hello frame. */
7
7
  export declare const DEVICE_PROTOCOL_VERSION = 1;
8
8
  /** The operations the kernel asks a device to run. */
9
- export type DeviceOp = "create" | "exec" | "readFile" | "writeFile" | "stat" | "killTagged" | "killAll" | "stop" | "policy";
9
+ export type DeviceOp = "create" | "exec" | "readFile" | "writeFile" | "stat" | "killTagged" | "killAll" | "stop" | "policy" | "policyStatus";
10
10
  /**
11
- * Optional capabilities a device announces in its hello. A kernel sends the `policy` op only to a
12
- * device with `policyOp`; `networkReport` means exec results may carry `network`; `tiers` means the
13
- * device applies `limits.tier` / `defaultTier` / `devHosts` (without it the device behaves as
14
- * `strict`).
11
+ * Optional capabilities a device announces in its hello. `policyOp`: the device takes the `policy`
12
+ * op; `policyOpId`: it also logs each `policy` op's outcome by its `opId`, answers `policyStatus`
13
+ * and reports recent outcomes in its hello (`recentOps`); `networkReport` means exec results may
14
+ * carry `network`; `tiers` means the device applies `limits.tier` / `defaultTier` / `devHosts`
15
+ * (without it the device behaves as `strict`).
15
16
  */
16
- export type DeviceFeature = "networkReport" | "policyOp" | "tiers";
17
+ export type DeviceFeature = "networkReport" | "policyOp" | "policyOpId" | "tiers";
17
18
  /**
18
19
  * Whether the device accepts widening its local policy from the kernel (the `policy` op's `add`
19
20
  * and a higher `tier`, run and task grants): `owner` (default, decided by the device owner in a
@@ -45,6 +46,20 @@ export declare const BACKGROUND_LAUNCH_LINE = "# capybara: background task launc
45
46
  export declare function isBackgroundLaunch(command: string): boolean;
46
47
  /** The most blocked hosts an exec result reports; the parser cuts longer lists. */
47
48
  export declare const MAX_NETWORK_BLOCKED_HOSTS = 20;
49
+ /** The longest `opId` a `policy` / `policyStatus` op may carry. */
50
+ export declare const MAX_POLICY_OP_ID_LENGTH = 128;
51
+ /** The most entries a hello's `recentOps` carries; the parser keeps the newest. */
52
+ export declare const MAX_RECENT_POLICY_OPS = 100;
53
+ /**
54
+ * What a device recorded of a `policy` op: `started` (written before the change, so an op the
55
+ * device was stopped in the middle of stays `started`), then `applied` or `refused`.
56
+ */
57
+ export type DevicePolicyOpOutcome = "applied" | "refused" | "started";
58
+ /** One entry of a hello's `recentOps`. */
59
+ export interface DevicePolicyOpReport {
60
+ opId: string;
61
+ outcome: DevicePolicyOpOutcome;
62
+ }
48
63
  /** First frame from the device after the WebSocket opens. */
49
64
  export interface DeviceHello {
50
65
  type: "hello";
@@ -59,6 +74,12 @@ export interface DeviceHello {
59
74
  features?: DeviceFeature[];
60
75
  /** The device's local policy at connect time (absent on older devices). */
61
76
  policy?: DeviceLocalPolicySummary;
77
+ /**
78
+ * The outcomes of the latest `policy` ops by op id, oldest first, at most
79
+ * {@link MAX_RECENT_POLICY_OPS} (devices with `policyOpId`; malformed entries are dropped by
80
+ * the parser).
81
+ */
82
+ recentOps?: DevicePolicyOpReport[];
62
83
  }
63
84
  /** Error kinds a device reports for a failed request. */
64
85
  export type DeviceErrorKind = "gone" | "fs_gone" | "policy" | "error";
@@ -166,6 +187,12 @@ export interface DeviceStopArgs {
166
187
  * sandbox back on: turning it off is decided on the machine.
167
188
  */
168
189
  export interface DevicePolicyArgs {
190
+ /**
191
+ * The kernel's id of this change (non-empty, no `:`, at most {@link MAX_POLICY_OP_ID_LENGTH}
192
+ * characters): the device logs the op's outcome under it, for `policyStatus` and the hello's
193
+ * `recentOps`.
194
+ */
195
+ opId: string;
169
196
  /** Hosts to add to `allowedDomains`. */
170
197
  add?: string[];
171
198
  /** Hosts to remove from `allowedDomains`. */
@@ -186,10 +213,29 @@ export interface DevicePolicyArgs {
186
213
  export interface DevicePolicyResult {
187
214
  policy: DeviceLocalPolicySummary;
188
215
  }
216
+ /** Asks what became of the `policy` op with this id. Read-only on the device. */
217
+ export interface DevicePolicyStatusArgs {
218
+ opId: string;
219
+ }
220
+ /**
221
+ * What the device logged of a `policy` op: its {@link DevicePolicyOpOutcome}, or `unknown` when
222
+ * the id is not in its log (never received, or dropped from the log since). `policy` is the
223
+ * device's summary now; `message` the refusal's.
224
+ */
225
+ export interface DevicePolicyStatus {
226
+ outcome: DevicePolicyOpOutcome | "unknown";
227
+ policy?: DeviceLocalPolicySummary;
228
+ message?: string;
229
+ }
189
230
  /** A frame that is not valid protocol v1 (or came from the wrong side). */
190
231
  export declare class DeviceProtocolError extends Error {
191
232
  constructor(message: string);
192
233
  }
234
+ /**
235
+ * Whether `v` is a valid policy op id: non-empty, no `:`, at most
236
+ * {@link MAX_POLICY_OP_ID_LENGTH} characters.
237
+ */
238
+ export declare function isPolicyOpId(v: unknown): v is string;
193
239
  /** Result type per op. */
194
240
  export interface DeviceOpResults {
195
241
  create: DeviceCreateResult;
@@ -201,6 +247,7 @@ export interface DeviceOpResults {
201
247
  killAll: null;
202
248
  stop: null;
203
249
  policy: DevicePolicyResult;
250
+ policyStatus: DevicePolicyStatus;
204
251
  }
205
252
  /** Validates the `result` of a successful `res` for `op`; throws {@link DeviceProtocolError}. */
206
253
  export declare function parseOpResult<Op extends DeviceOp>(op: Op, result: unknown): DeviceOpResults[Op];
@@ -15,8 +15,14 @@ const DEVICE_OPS = [
15
15
  "killAll",
16
16
  "stop",
17
17
  "policy",
18
+ "policyStatus",
19
+ ];
20
+ const DEVICE_FEATURES = [
21
+ "networkReport",
22
+ "policyOp",
23
+ "policyOpId",
24
+ "tiers",
18
25
  ];
19
- const DEVICE_FEATURES = ["networkReport", "policyOp", "tiers"];
20
26
  /**
21
27
  * The first line of the kernel's background task launch command (`Bash` `run_in_background`), a
22
28
  * shell comment. A device kills what a foreground exec leaves running once the exec ends; an exec
@@ -31,6 +37,10 @@ export function isBackgroundLaunch(command) {
31
37
  export const MAX_NETWORK_BLOCKED_HOSTS = 20;
32
38
  /** The most hosts a `policy` op's `add` / `remove` or an exec's `grants` may carry. */
33
39
  const MAX_POLICY_HOSTS = 100;
40
+ /** The longest `opId` a `policy` / `policyStatus` op may carry. */
41
+ export const MAX_POLICY_OP_ID_LENGTH = 128;
42
+ /** The most entries a hello's `recentOps` carries; the parser keeps the newest. */
43
+ export const MAX_RECENT_POLICY_OPS = 100;
34
44
  /** A frame that is not valid protocol v1 (or came from the wrong side). */
35
45
  export class DeviceProtocolError extends Error {
36
46
  constructor(message) {
@@ -172,12 +182,46 @@ function parsePolicySummary(v) {
172
182
  out.tier = parseTier(o, "tier");
173
183
  return out;
174
184
  }
185
+ /**
186
+ * Whether `v` is a valid policy op id: non-empty, no `:`, at most
187
+ * {@link MAX_POLICY_OP_ID_LENGTH} characters.
188
+ */
189
+ export function isPolicyOpId(v) {
190
+ return (typeof v === "string" && v !== "" && v.length <= MAX_POLICY_OP_ID_LENGTH && !v.includes(":"));
191
+ }
192
+ function opId(o) {
193
+ const v = o.opId;
194
+ if (!isPolicyOpId(v)) {
195
+ throw new DeviceProtocolError(`field "opId" must be a non-empty string without ":" of at most ${MAX_POLICY_OP_ID_LENGTH} characters`);
196
+ }
197
+ return v;
198
+ }
199
+ const OP_OUTCOMES = ["applied", "refused", "started"];
200
+ /**
201
+ * The valid entries of a hello's `recentOps`, one per op id (the last listed), the newest
202
+ * {@link MAX_RECENT_POLICY_OPS}.
203
+ */
204
+ function parseRecentOps(v) {
205
+ // one entry per op id: the last one listed (the newest outcome), where it is listed
206
+ const byId = new Map();
207
+ for (const e of v) {
208
+ if (!isRecord(e) || !isPolicyOpId(e.opId))
209
+ continue;
210
+ if (!OP_OUTCOMES.includes(e.outcome))
211
+ continue;
212
+ byId.delete(e.opId);
213
+ byId.set(e.opId, { opId: e.opId, outcome: e.outcome });
214
+ }
215
+ return [...byId.values()].slice(-MAX_RECENT_POLICY_OPS);
216
+ }
175
217
  /** Optional hello fields: malformed values are ignored so old and new peers interoperate. */
176
218
  function optionalHelloFields(o) {
177
219
  const out = {};
178
220
  if (Array.isArray(o.features)) {
179
221
  out.features = DEVICE_FEATURES.filter((f) => o.features.includes(f));
180
222
  }
223
+ if (Array.isArray(o.recentOps))
224
+ out.recentOps = parseRecentOps(o.recentOps);
181
225
  return { ...out, ...optionalPolicy(o.policy) };
182
226
  }
183
227
  /** A summary field that is dropped when absent or unreadable. */
@@ -336,6 +380,7 @@ function validateArgs(op, a) {
336
380
  return;
337
381
  }
338
382
  case "policy": {
383
+ opId(a);
339
384
  if (a.add !== undefined)
340
385
  hostArray(a, "add");
341
386
  if (a.remove !== undefined)
@@ -355,6 +400,9 @@ function validateArgs(op, a) {
355
400
  str(src, "approvalId");
356
401
  return;
357
402
  }
403
+ case "policyStatus":
404
+ opId(a);
405
+ return;
358
406
  }
359
407
  }
360
408
  export function parseOpResult(op, result) {
@@ -380,6 +428,17 @@ export function parseOpResult(op, result) {
380
428
  }
381
429
  case "policy":
382
430
  return { policy: parsePolicySummary(obj(result, "policy result").policy) };
431
+ case "policyStatus": {
432
+ const r = obj(result, "policyStatus result");
433
+ const out = {
434
+ outcome: oneOf(r, "outcome", [...OP_OUTCOMES, "unknown"]),
435
+ };
436
+ if (r.policy !== undefined)
437
+ out.policy = parsePolicySummary(r.policy);
438
+ if (r.message !== undefined)
439
+ out.message = str(r, "message");
440
+ return out;
441
+ }
383
442
  case "readFile": {
384
443
  if (result === null)
385
444
  return null;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const SDK_VERSION = "0.1.0-rc.19";
1
+ export declare const SDK_VERSION = "0.1.0-rc.20";
2
2
  export * from "./agent-template.js";
3
3
  export * from "./bootstrap.js";
4
4
  export * from "./cards.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export const SDK_VERSION = "0.1.0-rc.19";
1
+ export const SDK_VERSION = "0.1.0-rc.20";
2
2
  export * from "./agent-template.js";
3
3
  export * from "./bootstrap.js";
4
4
  export * from "./cards.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/plugin-sdk",
3
- "version": "0.1.0-rc.19",
3
+ "version": "0.1.0-rc.20",
4
4
  "description": "Capybara plugin SDK: the types and pure functions plugins are written against",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {