@capybara-hq/device 0.1.0-rc.17 → 0.1.0-rc.19

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.
Files changed (107) hide show
  1. package/README.md +143 -32
  2. package/dist/activity.d.ts +31 -0
  3. package/dist/activity.js +62 -0
  4. package/dist/api.d.ts +3 -3
  5. package/dist/args.d.ts +25 -4
  6. package/dist/args.js +82 -21
  7. package/dist/cli.js +14 -3
  8. package/dist/client.js +1 -1
  9. package/dist/commands/ceiling.d.ts +3 -2
  10. package/dist/commands/ceiling.js +60 -20
  11. package/dist/commands/keep-awake.d.ts +19 -0
  12. package/dist/commands/keep-awake.js +77 -0
  13. package/dist/commands/login.d.ts +15 -11
  14. package/dist/commands/login.js +75 -49
  15. package/dist/commands/policy.js +16 -10
  16. package/dist/commands/service.d.ts +3 -1
  17. package/dist/commands/service.js +35 -2
  18. package/dist/commands/start.d.ts +18 -3
  19. package/dist/commands/start.js +144 -9
  20. package/dist/commands/status.d.ts +8 -1
  21. package/dist/commands/status.js +59 -7
  22. package/dist/control/client.d.ts +12 -0
  23. package/dist/control/client.js +63 -0
  24. package/dist/control/protocol.d.ts +58 -0
  25. package/dist/control/protocol.js +16 -0
  26. package/dist/control/server.d.ts +28 -0
  27. package/dist/control/server.js +254 -0
  28. package/dist/exec/docker-operations.d.ts +14 -2
  29. package/dist/exec/docker-operations.js +32 -5
  30. package/dist/exec/linux-agent.d.ts +13 -3
  31. package/dist/exec/linux-agent.js +450 -43
  32. package/dist/exec/linux-operation-backend.d.ts +27 -6
  33. package/dist/exec/linux-operation-backend.js +30 -9
  34. package/dist/exec/linux-operation-worker.d.ts +51 -3
  35. package/dist/exec/linux-operation-worker.js +55 -3
  36. package/dist/exec/linux-runtime.d.ts +3 -2
  37. package/dist/exec/linux-runtime.js +4 -2
  38. package/dist/exec/mac-operation-backend.d.ts +12 -2
  39. package/dist/exec/mac-operation-backend.js +19 -10
  40. package/dist/exec/mac-write-policy.d.ts +2 -0
  41. package/dist/exec/mac-write-policy.js +1 -1
  42. package/dist/exec/operation-supervisor.d.ts +6 -0
  43. package/dist/exec/operation-supervisor.js +10 -0
  44. package/dist/exec/operation-types.d.ts +35 -5
  45. package/dist/exec/operation-types.js +73 -23
  46. package/dist/exec/operation-worker.d.ts +80 -1
  47. package/dist/exec/operation-worker.js +197 -14
  48. package/dist/exec/protected-targets.d.ts +16 -0
  49. package/dist/exec/protected-targets.js +53 -0
  50. package/dist/exec/shared.d.ts +7 -3
  51. package/dist/exec/shared.js +7 -5
  52. package/dist/index.d.ts +2 -0
  53. package/dist/index.js +1 -0
  54. package/dist/keep-awake.d.ts +39 -0
  55. package/dist/keep-awake.js +104 -0
  56. package/dist/node_modules/@anthropic-ai/sandbox-runtime/CAPYBARA-MODIFICATIONS.patch +343 -7
  57. package/dist/node_modules/@anthropic-ai/sandbox-runtime/dist/sandbox/resolved-address-guard.js +220 -6
  58. package/dist/node_modules/@anthropic-ai/sandbox-runtime/dist/sandbox/sandbox-config.js +17 -0
  59. package/dist/node_modules/@anthropic-ai/sandbox-runtime/dist/sandbox/sandbox-manager.js +11 -0
  60. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/device-protocol.d.ts +77 -46
  61. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/device-protocol.js +206 -83
  62. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/index.d.ts +5 -2
  63. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/index.js +5 -2
  64. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/network-classes.d.ts +98 -0
  65. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/network-classes.js +291 -0
  66. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/permissions.js +3 -1
  67. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-linux-runtime-source.js +460 -28
  68. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-native-runtime-source.js +143 -3
  69. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-operation.d.ts +36 -4
  70. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-operation.js +21 -4
  71. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-runtime-source.js +2 -2
  72. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-write-denies.d.ts +87 -8
  73. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-write-denies.js +249 -10
  74. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox.d.ts +11 -22
  75. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/shell-capabilities.d.ts +80 -0
  76. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/shell-capabilities.js +13 -0
  77. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/shell-environment.d.ts +57 -0
  78. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/shell-environment.js +16 -0
  79. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/ui-slots.d.ts +12 -0
  80. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/ui-slots.js +23 -0
  81. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/ui.d.ts +6 -1
  82. package/dist/node_modules/@capybara-hq/plugin-sdk/package.json +1 -1
  83. package/dist/owner-report.d.ts +1 -1
  84. package/dist/owner-report.js +5 -4
  85. package/dist/policy-store.d.ts +2 -1
  86. package/dist/policy-store.js +2 -1
  87. package/dist/policy.d.ts +23 -14
  88. package/dist/policy.js +28 -21
  89. package/dist/runtime-manifest.json +1 -1
  90. package/dist/server-ops.d.ts +33 -5
  91. package/dist/server-ops.js +138 -85
  92. package/dist/service/launchd.d.ts +2 -0
  93. package/dist/service/launchd.js +10 -4
  94. package/dist/service/manager.d.ts +2 -0
  95. package/dist/service/manager.js +16 -2
  96. package/dist/service/systemd.d.ts +2 -0
  97. package/dist/service/systemd.js +8 -2
  98. package/dist/staged-connect.d.ts +10 -1
  99. package/dist/staged-connect.js +25 -3
  100. package/dist/staged-device.d.ts +1 -1
  101. package/dist/staged-device.js +13 -5
  102. package/dist/staged-session.d.ts +1 -1
  103. package/dist/state.d.ts +50 -21
  104. package/dist/state.js +111 -46
  105. package/dist/wake.d.ts +23 -0
  106. package/dist/wake.js +29 -0
  107. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -2,6 +2,7 @@ import { homedir } from "node:os";
2
2
  import { parseCommandLine, USAGE, UsageError } from "./args.js";
3
3
  import { ceilingCommand } from "./commands/ceiling.js";
4
4
  import { drain, drainAndWait } from "./commands/drain.js";
5
+ import { keepAwakeCommand } from "./commands/keep-awake.js";
5
6
  import { login } from "./commands/login.js";
6
7
  import { logout } from "./commands/logout.js";
7
8
  import { policyCommand } from "./commands/policy.js";
@@ -22,7 +23,7 @@ async function askOnTerminal(question) {
22
23
  }
23
24
  }
24
25
  /**
25
- * The program's parts (device protocol 3): the binder with one operation backend per bound
26
+ * The program's parts (device protocol 4): the binder with one operation backend per bound
26
27
  * generation, built for this host (macOS, Linux) or the device's container (docker mode). Loaded
27
28
  * on demand: only start needs them.
28
29
  */
@@ -86,7 +87,10 @@ export async function runCli(argv, io = {}) {
86
87
  ...(io.ps && { ps: io.ps }),
87
88
  });
88
89
  case "status":
89
- return status(dir, cmd.json, out, err);
90
+ return status(dir, cmd.json, out, err, {
91
+ ...(io.kill && { kill: io.kill }),
92
+ ...(io.ps && { ps: io.ps }),
93
+ });
90
94
  case "start":
91
95
  return start({
92
96
  service: { home, platform },
@@ -129,6 +133,13 @@ export async function runCli(argv, io = {}) {
129
133
  ...((io.stdinIsTTY ?? process.stdin.isTTY === true) && { ask: io.ask ?? askOnTerminal }),
130
134
  ...(io.beforeCeilingWrite && { beforeWrite: io.beforeCeilingWrite }),
131
135
  });
136
+ case "keep-awake":
137
+ return keepAwakeCommand(cmd, {
138
+ stateDir: dir,
139
+ out,
140
+ err,
141
+ ...((io.stdinIsTTY ?? process.stdin.isTTY === true) && { ask: io.ask ?? askOnTerminal }),
142
+ });
132
143
  case "service":
133
144
  return serviceCommand(cmd.action, {
134
145
  stateDir: dir,
@@ -141,6 +152,6 @@ export async function runCli(argv, io = {}) {
141
152
  ...(io.sleep && { sleep: io.sleep }),
142
153
  ...(io.kill && { kill: io.kill }),
143
154
  ...(io.ps && { ps: io.ps }),
144
- });
155
+ }, { json: cmd.json === true });
145
156
  }
146
157
  }
package/dist/client.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * What the device program's connection shares: the messages it ends with, the reconnect backoff
3
- * and the running connection's interface. The connection itself is the protocol 3 one
3
+ * and the running connection's interface. The connection itself is the protocol 4 one
4
4
  * (`staged-connect.ts`).
5
5
  */
6
6
  import { DEVICE_PROTOCOL_VERSION } from "@capybara-hq/plugin-sdk";
@@ -16,8 +16,9 @@ export interface CeilingDeps {
16
16
  /** True when `next` allows anything `cur` does not. */
17
17
  export declare function widens(cur: OwnerCeilingPolicy, next: OwnerCeilingPolicy): boolean;
18
18
  /**
19
- * `ceiling show` prints the owner ceiling, the restricted network baseline and the revision;
20
- * `ceiling set` writes both fields at once under the policy lock (converting an old-format file).
19
+ * `ceiling show` prints the owner ceiling, the sandbox allowlist and the revision; `ceiling set`
20
+ * changes the named fields under the policy lock, the others as they are then (converting an
21
+ * old-format file).
21
22
  * Narrowing applies directly; widening needs --yes or a confirmed terminal answer. Only the
22
23
  * machine's owner widens it: the deployment may only narrow.
23
24
  */
@@ -3,27 +3,55 @@ import { readStrictOwnerCeilingSync } from "../exec/operation-types.js";
3
3
  import { PolicyStore } from "../policy-store.js";
4
4
  import { policyPath, readCredential } from "../state.js";
5
5
  /** Printed before a widening is applied (by --yes or a confirmed prompt). */
6
- export const CEILING_WIDENING_TEXT = "Widening this machine's owner ceiling lets the deployment ask for more here. Full access lets " +
7
- "a conversation the owner switches to Full run commands as you, with your files, credentials " +
8
- "and network. The os-user project reach lets restricted operations and project browsing read " +
9
- "projects outside the device's workspace. Conversations need fresh Full consent either way.";
6
+ export const CEILING_WIDENING_TEXT = "Widening this machine's owner ceiling lets the deployment ask for more here. Commands always " +
7
+ "run in the sandbox under every profile, Full access included. Allowing outside the sandbox " +
8
+ "lets one approved call run as you, with your files, credentials and any network: the person, " +
9
+ "the review model, or a Full access auto-grant approves each such call, and each one is " +
10
+ "recorded. The open network lets sandboxed commands reach the public internet (private, local " +
11
+ "and metadata addresses stay closed). The os-user project reach lets sandboxed work and " +
12
+ "project browsing read projects outside the device's workspace. Conversations need fresh Full " +
13
+ "access consent either way.";
10
14
  export const CEILING_NEEDS_YES = "Nothing changed. To widen the owner ceiling, add --yes (or answer y at the prompt).";
11
15
  export const CEILING_PROMPT = "Widen this machine's owner ceiling? [y/N] ";
16
+ /** The strict reader's revision for the logged-in device, or null (logged out, unreadable). */
17
+ function strictRevision(stateDir, home) {
18
+ try {
19
+ const cred = readCredential(stateDir);
20
+ if (cred === undefined)
21
+ return null;
22
+ return readStrictOwnerCeilingSync({ stateDir, deviceId: cred.deviceId, dir: cred.dir, home })
23
+ .revision;
24
+ }
25
+ catch {
26
+ return null;
27
+ }
28
+ }
12
29
  /** Thrown inside the locked update when the policy now on disk makes the target a widening. */
13
30
  class WideningNeedsConfirmation extends Error {
14
31
  }
15
32
  const describe = (c) => [
16
- `Full access: ${c.fullAccess ? "allowed" : "denied"}`,
33
+ `Outside the sandbox: ${c.outsideSandbox ? "on" : "off"}`,
34
+ `Network: ${c.network}`,
17
35
  `Project reach: ${c.restrictedProjectReach}`,
18
36
  ];
19
37
  /** True when `next` allows anything `cur` does not. */
20
38
  export function widens(cur, next) {
21
- return ((!cur.fullAccess && next.fullAccess) ||
39
+ return ((!cur.outsideSandbox && next.outsideSandbox) ||
40
+ (cur.network === "allowlist" && next.network === "open") ||
22
41
  (cur.restrictedProjectReach === "workspace" && next.restrictedProjectReach === "os-user"));
23
42
  }
43
+ /** The target ceiling: the fields the command names, the others as they are in `cur`. */
44
+ function target(cmd, cur) {
45
+ return {
46
+ outsideSandbox: cmd.outsideSandbox ?? cur.outsideSandbox,
47
+ network: cmd.network ?? cur.network,
48
+ restrictedProjectReach: cmd.projectReach ?? cur.restrictedProjectReach,
49
+ };
50
+ }
24
51
  /**
25
- * `ceiling show` prints the owner ceiling, the restricted network baseline and the revision;
26
- * `ceiling set` writes both fields at once under the policy lock (converting an old-format file).
52
+ * `ceiling show` prints the owner ceiling, the sandbox allowlist and the revision; `ceiling set`
53
+ * changes the named fields under the policy lock, the others as they are then (converting an
54
+ * old-format file).
27
55
  * Narrowing applies directly; widening needs --yes or a confirmed terminal answer. Only the
28
56
  * machine's owner widens it: the deployment may only narrow.
29
57
  */
@@ -38,10 +66,22 @@ export async function ceilingCommand(cmd, deps) {
38
66
  return 1;
39
67
  }
40
68
  const cur = store.current();
69
+ if (cmd.action === "show" && cmd.json === true) {
70
+ out(JSON.stringify({
71
+ outsideSandbox: cur.ownerCeiling.outsideSandbox,
72
+ network: cur.ownerCeiling.network,
73
+ allowedDomains: cur.allowedDomains,
74
+ projectReach: cur.ownerCeiling.restrictedProjectReach,
75
+ // the strict reader's digest of the policy generation; null while logged out
76
+ revision: strictRevision(stateDir, deps.home),
77
+ }));
78
+ return 0;
79
+ }
41
80
  if (cmd.action === "show") {
42
81
  for (const line of describe(cur.ownerCeiling))
43
82
  out(line);
44
- out(`Restricted network baseline: ${cur.allowedDomains.length > 0 ? cur.allowedDomains.join(", ") : "none"}`);
83
+ out(`Sandbox allowlist: ${cur.allowedDomains.length > 0 ? cur.allowedDomains.join(", ") : "none"}` +
84
+ (cur.ownerCeiling.network === "open" ? " (used while the network is an allowlist)" : ""));
45
85
  const cred = (() => {
46
86
  try {
47
87
  return readCredential(stateDir);
@@ -64,16 +104,12 @@ export async function ceilingCommand(cmd, deps) {
64
104
  out(`Revision: unavailable (${e.message})`);
65
105
  }
66
106
  }
67
- out("The platform and organization Full access settings and this machine's runtime support " +
68
- "apply separately; the deployment's device page shows the effective permissions.");
107
+ out("The platform and organization sandbox settings and this machine's runtime support apply " +
108
+ "separately; the deployment's device page shows the effective permissions.");
69
109
  return 0;
70
110
  }
71
- const next = {
72
- fullAccess: cmd.fullAccess,
73
- restrictedProjectReach: cmd.projectReach,
74
- };
75
111
  let confirmed = cmd.yes === true;
76
- if (widens(cur.ownerCeiling, next)) {
112
+ if (widens(cur.ownerCeiling, target(cmd, cur.ownerCeiling))) {
77
113
  out(CEILING_WIDENING_TEXT);
78
114
  if (!confirmed) {
79
115
  if (deps.ask === undefined) {
@@ -94,9 +130,10 @@ export async function ceilingCommand(cmd, deps) {
94
130
  // decided again on the policy under the lock: a narrowing that landed since the read above
95
131
  // makes this a widening, which needs its own confirmation
96
132
  written = store.update((p) => {
133
+ const next = target(cmd, p.ownerCeiling);
97
134
  if (!confirmed && widens(p.ownerCeiling, next))
98
135
  throw new WideningNeedsConfirmation();
99
- return { ...p, ownerCeiling: { ...next } };
136
+ return { ...p, ownerCeiling: next };
100
137
  }).ownerCeiling;
101
138
  }
102
139
  catch (e) {
@@ -108,12 +145,15 @@ export async function ceilingCommand(cmd, deps) {
108
145
  err(`Cannot change ${policyPath(stateDir)}: ${e.message}`);
109
146
  return 1;
110
147
  }
111
- noteCliPolicyChange(stateDir, `ceiling set fullAccess ${written.fullAccess ? "allow" : "deny"}, projectReach ` +
112
- written.restrictedProjectReach);
148
+ noteCliPolicyChange(stateDir, `ceiling set outsideSandbox ${written.outsideSandbox ? "on" : "off"}, network ` +
149
+ `${written.network}, projectReach ${written.restrictedProjectReach}`);
113
150
  out("This machine's owner ceiling is now:");
114
151
  for (const line of describe(written))
115
152
  out(line);
153
+ if (written.network === "allowlist")
154
+ out("Sandboxed commands reach only the sandbox allowlist (capybara-device policy add " +
155
+ "allowedDomains <host>).");
116
156
  if (written.restrictedProjectReach === "os-user")
117
- out("Restricted operations may reach selected projects outside the device's workspace.");
157
+ out("Sandboxed work may reach selected projects outside the device's workspace.");
118
158
  return 0;
119
159
  }
@@ -0,0 +1,19 @@
1
+ import type { Command } from "../args.js";
2
+ export declare const KEEP_AWAKE_PROMPT = "Keep this computer awake more often? [y/N] ";
3
+ export declare const KEEP_AWAKE_NEEDS_YES = "Nothing changed. To keep this computer awake more often, add --yes (or answer y at the prompt).";
4
+ /** What keep-awake cannot prevent (spec §9.7), printed with the mode. */
5
+ export declare const KEEP_AWAKE_LIMITS: string;
6
+ export interface KeepAwakeDeps {
7
+ stateDir: string;
8
+ out: (line: string) => void;
9
+ err: (line: string) => void;
10
+ ask?: (question: string) => Promise<string>;
11
+ }
12
+ /**
13
+ * `keep-awake show` prints the mode; `keep-awake off|while-busy|connected` writes it under the
14
+ * policy lock. Lowering applies directly; raising needs --yes or a confirmed terminal answer. A
15
+ * running program applies the change without a restart.
16
+ */
17
+ export declare function keepAwakeCommand(cmd: Extract<Command, {
18
+ command: "keep-awake";
19
+ }>, deps: KeepAwakeDeps): Promise<number>;
@@ -0,0 +1,77 @@
1
+ import { noteCliPolicyChange } from "../command-log.js";
2
+ import { PolicyStore } from "../policy-store.js";
3
+ import { KEEP_AWAKE_MODES, policyPath } from "../state.js";
4
+ export const KEEP_AWAKE_PROMPT = "Keep this computer awake more often? [y/N] ";
5
+ export const KEEP_AWAKE_NEEDS_YES = "Nothing changed. To keep this computer awake more often, add --yes (or answer y at the prompt).";
6
+ /** What keep-awake cannot prevent (spec §9.7), printed with the mode. */
7
+ export const KEEP_AWAKE_LIMITS = "Keep-awake cannot prevent sleep when the lid of a Mac on battery is closed without an " +
8
+ "external display, when Sleep is chosen, at a critically low battery or in a thermal " +
9
+ "emergency, nor (on Linux) the lid switch. A sleeping computer is offline for the deployment.";
10
+ const RAISE_TEXT = "While the device is connected or busy, this computer will not go to idle sleep; it uses more " +
11
+ "power and a laptop in a bag can get warm.";
12
+ const rank = (m) => KEEP_AWAKE_MODES.indexOf(m);
13
+ class RaiseNeedsConfirmation extends Error {
14
+ }
15
+ /**
16
+ * `keep-awake show` prints the mode; `keep-awake off|while-busy|connected` writes it under the
17
+ * policy lock. Lowering applies directly; raising needs --yes or a confirmed terminal answer. A
18
+ * running program applies the change without a restart.
19
+ */
20
+ export async function keepAwakeCommand(cmd, deps) {
21
+ const { stateDir, out, err } = deps;
22
+ let store;
23
+ try {
24
+ store = new PolicyStore(stateDir);
25
+ }
26
+ catch (e) {
27
+ err(`Cannot read ${policyPath(stateDir)}: ${e.message}`);
28
+ return 1;
29
+ }
30
+ const cur = store.current().keepAwake;
31
+ if (cmd.action === "show") {
32
+ if (cmd.json === true)
33
+ out(JSON.stringify({ keepAwake: cur }));
34
+ else {
35
+ out(`Keep-awake: ${cur}`);
36
+ out(KEEP_AWAKE_LIMITS);
37
+ }
38
+ return 0;
39
+ }
40
+ const next = cmd.mode;
41
+ let confirmed = cmd.yes === true;
42
+ if (rank(next) > rank(cur)) {
43
+ out(RAISE_TEXT);
44
+ if (!confirmed) {
45
+ if (deps.ask === undefined) {
46
+ err(KEEP_AWAKE_NEEDS_YES);
47
+ return 2;
48
+ }
49
+ const answer = (await deps.ask(KEEP_AWAKE_PROMPT)).trim().toLowerCase();
50
+ if (answer !== "y" && answer !== "yes") {
51
+ err("Nothing changed.");
52
+ return 1;
53
+ }
54
+ confirmed = true;
55
+ }
56
+ }
57
+ try {
58
+ // decided again on the policy under the lock: a lowering that landed since the read above
59
+ // makes this a raise, which needs its own confirmation
60
+ store.update((p) => {
61
+ if (!confirmed && rank(next) > rank(p.keepAwake))
62
+ throw new RaiseNeedsConfirmation();
63
+ return { ...p, keepAwake: next };
64
+ });
65
+ }
66
+ catch (e) {
67
+ if (e instanceof RaiseNeedsConfirmation) {
68
+ err(`The keep-awake mode changed meanwhile. ${KEEP_AWAKE_NEEDS_YES}`);
69
+ return 2;
70
+ }
71
+ err(`Cannot change ${policyPath(stateDir)}: ${e.message}`);
72
+ return 1;
73
+ }
74
+ noteCliPolicyChange(stateDir, `keep-awake set ${next}`);
75
+ out(cmd.json === true ? JSON.stringify({ keepAwake: next }) : `Keep-awake is now: ${next}`);
76
+ return 0;
77
+ }
@@ -1,10 +1,12 @@
1
+ import type { DeviceNetworkMode } from "@capybara-hq/plugin-sdk";
1
2
  import { type FetchFn } from "../api.js";
2
3
  import type { Command } from "../args.js";
3
4
  /**
4
- * Shown once at login when the owner ceiling allows Full access in an organization that allows
5
- * it: the trust model of Full access is command review, not the OS sandbox.
5
+ * Shown once at login when the owner ceiling allows running outside the sandbox in an organization
6
+ * that allows it: an escalated call's trust model is its approval and the command review, not the
7
+ * OS sandbox.
6
8
  */
7
- export declare const FULL_ACCESS_RISK_TEXT: string;
9
+ export declare const OUTSIDE_SANDBOX_RISK_TEXT: string;
8
10
  /** The OS sandbox check `login` uses (default: the runtime's, loaded on demand). */
9
11
  export type SandboxCheck = () => {
10
12
  ok: true;
@@ -14,16 +16,18 @@ export type SandboxCheck = () => {
14
16
  };
15
17
  /** How the owner widens the ceiling, on this machine only (the deployment may only narrow it). */
16
18
  export declare const WIDEN_CEILING_HINT: string;
19
+ /** The sandbox network line of the login output. */
20
+ export declare function networkLoginLine(network: DeviceNetworkMode): string;
17
21
  /**
18
- * The line `login` prints for a restricted owner ceiling (the default of a new setup, B-14): no
19
- * Full access, restricted work in the device's workspace only.
22
+ * The line `login` prints when the owner ceiling keeps every call in the sandbox (the default of a
23
+ * new setup): commands run in the sandbox under every profile, Full access included.
20
24
  */
21
- export declare const RESTRICTED_CEILING_LOGIN_LINE: string;
22
- /** The line `login` prints when the owner ceiling allows Full access. */
23
- export declare const FULL_CEILING_LOGIN_LINE: string;
24
- /** Added when the organization does not allow Full access while the owner ceiling does. */
25
- export declare const FULL_ACCESS_NOT_ALLOWED_LOGIN_LINE = "This organization does not allow Full access, so agents use the restricted profiles here.";
26
- /** The line `login` prints when restricted work cannot run here (no OS sandbox). */
25
+ export declare const SANDBOX_ONLY_LOGIN_LINE: string;
26
+ /** The line `login` prints when the owner ceiling allows running outside the sandbox. */
27
+ export declare const OUTSIDE_SANDBOX_LOGIN_LINE: string;
28
+ /** Added when the organization does not allow running outside the sandbox while the owner does. */
29
+ export declare const OUTSIDE_SANDBOX_NOT_ALLOWED_LOGIN_LINE: string;
30
+ /** The line `login` prints when sandboxed work cannot run here (no OS sandbox). */
27
31
  export declare function restrictedUnavailableLoginLine(reason: string): string;
28
32
  export interface LoginDeps {
29
33
  stateDir: string;
@@ -9,13 +9,15 @@ import { resetOwnerReport } from "../owner-report.js";
9
9
  import { hasLoginService } from "../service/installed.js";
10
10
  import { DEFAULT_MAX_FRAME_BYTES, DEFAULT_POLICY, defaultWorkDir, policyPath, readCredential, readPolicy, withPolicyLock, writeCredential, writePolicy, } from "../state.js";
11
11
  /**
12
- * Shown once at login when the owner ceiling allows Full access in an organization that allows
13
- * it: the trust model of Full access is command review, not the OS sandbox.
12
+ * Shown once at login when the owner ceiling allows running outside the sandbox in an organization
13
+ * that allows it: an escalated call's trust model is its approval and the command review, not the
14
+ * OS sandbox.
14
15
  */
15
- export const FULL_ACCESS_RISK_TEXT = "With Full access, agent commands run on this computer as you: they can read and write all " +
16
- "your files, use your credentials (SSH keys, cloud accounts, keychain), Docker and any network, " +
17
- "and the deployment reviews every command before it runs. Prompt injection can make an agent " +
18
- "try to delete files or send your credentials away; the review blocks or asks about the " +
16
+ export const OUTSIDE_SANDBOX_RISK_TEXT = "A call approved to run outside the sandbox runs on this computer as you: it can read and " +
17
+ "write all your files, use your credentials (SSH keys, cloud accounts, keychain), Docker and " +
18
+ "any network. The person, the review model or a Full access auto-grant approves each such " +
19
+ "call, and the deployment reviews every command before it runs. Prompt injection can make an " +
20
+ "agent try to delete files or send your credentials away; the review blocks or asks about the " +
19
21
  "dangerous ones. Such commands can also read this program's own credential: if you suspect " +
20
22
  "misuse, run capybara-device logout and log in again.";
21
23
  async function checkSandbox(check) {
@@ -25,23 +27,32 @@ async function checkSandbox(check) {
25
27
  return sandboxAvailability();
26
28
  }
27
29
  /** How the owner widens the ceiling, on this machine only (the deployment may only narrow it). */
28
- export const WIDEN_CEILING_HINT = "Only you can widen it, on this machine: `capybara-device ceiling set --full-access allow " +
29
- "--project-reach os-user` (see `capybara-device ceiling show`).";
30
+ export const WIDEN_CEILING_HINT = "Only you can widen it, on this machine: `capybara-device ceiling set --outside-sandbox on` " +
31
+ "(see `capybara-device ceiling show`).";
32
+ /** The sandbox network line of the login output. */
33
+ export function networkLoginLine(network) {
34
+ return network === "open"
35
+ ? "Sandboxed commands reach the public internet (private, local and metadata addresses stay " +
36
+ "closed); `capybara-device ceiling set --network allowlist` limits them to allowedDomains."
37
+ : "Sandboxed commands reach only allowedDomains; open the network with " +
38
+ "`capybara-device ceiling set --network open`.";
39
+ }
30
40
  /**
31
- * The line `login` prints for a restricted owner ceiling (the default of a new setup, B-14): no
32
- * Full access, restricted work in the device's workspace only.
41
+ * The line `login` prints when the owner ceiling keeps every call in the sandbox (the default of a
42
+ * new setup): commands run in the sandbox under every profile, Full access included.
33
43
  */
34
- export const RESTRICTED_CEILING_LOGIN_LINE = "This machine's owner ceiling allows restricted work only: Full access is off, and restricted " +
35
- `work reaches the device's workspace. ${WIDEN_CEILING_HINT}`;
36
- /** The line `login` prints when the owner ceiling allows Full access. */
37
- export const FULL_CEILING_LOGIN_LINE = "This machine's owner ceiling allows Full access: a conversation set to Full access runs agent " +
38
- "commands as you. Narrow it with `capybara-device ceiling set --full-access deny " +
39
- "--project-reach workspace`.";
40
- /** Added when the organization does not allow Full access while the owner ceiling does. */
41
- export const FULL_ACCESS_NOT_ALLOWED_LOGIN_LINE = "This organization does not allow Full access, so agents use the restricted profiles here.";
42
- /** The line `login` prints when restricted work cannot run here (no OS sandbox). */
44
+ export const SANDBOX_ONLY_LOGIN_LINE = "This machine's owner ceiling keeps every command in the sandbox, Full access included: no " +
45
+ `call runs outside it. ${WIDEN_CEILING_HINT}`;
46
+ /** The line `login` prints when the owner ceiling allows running outside the sandbox. */
47
+ export const OUTSIDE_SANDBOX_LOGIN_LINE = "This machine's owner ceiling allows running outside the sandbox: commands still run in the " +
48
+ "sandbox, and one approved call may run as you. Turn it off with " +
49
+ "`capybara-device ceiling set --outside-sandbox off`.";
50
+ /** Added when the organization does not allow running outside the sandbox while the owner does. */
51
+ export const OUTSIDE_SANDBOX_NOT_ALLOWED_LOGIN_LINE = "This organization does not allow running outside the sandbox, so every call stays in the " +
52
+ "sandbox here.";
53
+ /** The line `login` prints when sandboxed work cannot run here (no OS sandbox). */
43
54
  export function restrictedUnavailableLoginLine(reason) {
44
- return (`The OS sandbox is unavailable on this machine (${reason}), so restricted work cannot run ` +
55
+ return (`The OS sandbox is unavailable on this machine (${reason}), so sandboxed work cannot run ` +
45
56
  `here yet: ${SANDBOX_INSTALL_HINT}; or log in again with --mode docker. ${WIDEN_CEILING_HINT}`);
46
57
  }
47
58
  const SLOW_DOWN_STEP_SECONDS = 5;
@@ -59,8 +70,8 @@ function defaultOpen(platform) {
59
70
  };
60
71
  }
61
72
  /**
62
- * A new setup writes the default policy (B-14): a restricted owner ceiling, no Full access and the
63
- * workspace as the restricted reach. Written under the policy lock, and only while no policy file
73
+ * A new setup writes the default policy: no running outside the sandbox, the open network and the
74
+ * workspace as the project reach. Written under the policy lock, and only while no policy file
64
75
  * exists: an existing policy is the owner's choice and is never changed here (an old format still
65
76
  * converts exactly at start). Returns whether this call created the file.
66
77
  */
@@ -91,8 +102,21 @@ export function normalizeUrl(raw) {
91
102
  }
92
103
  /** `login`: the device authorization flow. Returns the exit code. */
93
104
  export async function login(cmd, deps) {
94
- const out = deps.out ?? ((l) => console.log(l));
105
+ const stdout = deps.out ?? ((l) => console.log(l));
95
106
  const err = deps.err ?? ((l) => console.error(l));
107
+ const json = cmd.json === true;
108
+ // With --json stdout carries only JSON events; the human lines go to stderr.
109
+ const out = json ? err : stdout;
110
+ const emit = (event) => {
111
+ if (json)
112
+ stdout(JSON.stringify(event));
113
+ };
114
+ /** A failed login: the human line, plus the JSON error event. */
115
+ const fail = (code, message, exit) => {
116
+ err(message);
117
+ emit({ event: "error", code, message });
118
+ return exit;
119
+ };
96
120
  const fetchFn = deps.fetch ?? fetch;
97
121
  const sleep = deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
98
122
  const platform = deps.platform ?? process.platform;
@@ -102,17 +126,14 @@ export async function login(cmd, deps) {
102
126
  baseUrl = normalizeUrl(cmd.url);
103
127
  }
104
128
  catch (e) {
105
- err(e.message);
106
- return 2;
129
+ return fail("invalid_url", e.message, 2);
107
130
  }
108
131
  if (platform !== "darwin" && platform !== "linux") {
109
- err("capybara-device runs on macOS and Linux. On Windows use WSL or Docker.");
110
- return 1;
132
+ return fail("unsupported_platform", "capybara-device runs on macOS and Linux. On Windows use WSL or Docker.", 1);
111
133
  }
112
134
  try {
113
135
  if (readCredential(deps.stateDir) !== undefined) {
114
- err("This machine is already logged in. Run `capybara-device logout` first.");
115
- return 1;
136
+ return fail("already_logged_in", "This machine is already logged in. Run `capybara-device logout` first.", 1);
116
137
  }
117
138
  }
118
139
  catch (e) {
@@ -126,7 +147,13 @@ export async function login(cmd, deps) {
126
147
  programVersion: programVersion(),
127
148
  });
128
149
  out(`Open ${auth.verificationUriComplete} and confirm the code ${auth.userCode}`);
129
- if (cmd.browser)
150
+ emit({
151
+ event: "verification",
152
+ url: auth.verificationUriComplete,
153
+ userCode: auth.userCode,
154
+ expiresIn: auth.expiresIn,
155
+ });
156
+ if (cmd.browser && !json)
130
157
  (deps.openBrowser ?? defaultOpen(platform))(auth.verificationUriComplete);
131
158
  let interval = Number.isFinite(auth.interval) && auth.interval >= 1 ? auth.interval : 5;
132
159
  const expiresIn = Number.isFinite(auth.expiresIn) && auth.expiresIn > 0 ? auth.expiresIn : 600;
@@ -143,8 +170,8 @@ export async function login(cmd, deps) {
143
170
  : resolve(cmd.dir);
144
171
  // Recreates a lost directory, marker or root: the device starts afresh.
145
172
  prepareDeviceDir(dir);
146
- // A new setup gets the restricted default written before the credential makes the login
147
- // final; the printed lines follow the policy this machine actually has.
173
+ // A new setup gets the default written before the credential makes the login final; the
174
+ // printed lines follow the policy this machine actually has.
148
175
  let ceiling = { ...DEFAULT_POLICY.ownerCeiling };
149
176
  try {
150
177
  persistDefaultPolicy(deps.stateDir);
@@ -158,7 +185,7 @@ export async function login(cmd, deps) {
158
185
  catch (e) {
159
186
  err(`Cannot read the local policy in ${deps.stateDir}: ${e.message}`);
160
187
  }
161
- /** Why restricted work cannot run here: the OS sandbox is missing (OS mode only). */
188
+ /** Why sandboxed work cannot run here: the OS sandbox is missing (OS mode only). */
162
189
  let noSandbox;
163
190
  if (cmd.mode !== "docker") {
164
191
  const available = await checkSandbox(deps.sandboxAvailable);
@@ -189,19 +216,22 @@ export async function login(cmd, deps) {
189
216
  reconnectGraceSeconds: granted.reconnectGraceSeconds,
190
217
  maxFrameBytes: granted.maxFrameBytes ?? DEFAULT_MAX_FRAME_BYTES,
191
218
  });
219
+ emit({ event: "done", deviceId: granted.deviceId, orgId: granted.orgId, dir });
192
220
  out(`Logged in. Device ${granted.deviceId}, working directory ${dir}`);
193
- if (ceiling.fullAccess) {
194
- out(FULL_CEILING_LOGIN_LINE);
195
- if (granted.fullAccess === "off")
196
- out(FULL_ACCESS_NOT_ALLOWED_LOGIN_LINE);
197
- // the risk text is shown once at login, and only when Full access runs as the owner on
198
- // this machine (outside Docker)
221
+ if (ceiling.outsideSandbox) {
222
+ out(OUTSIDE_SANDBOX_LOGIN_LINE);
223
+ // the token's `outsideSandbox` is the platform and organization outside-sandbox setting
224
+ if (granted.outsideSandbox === "off")
225
+ out(OUTSIDE_SANDBOX_NOT_ALLOWED_LOGIN_LINE);
226
+ // the risk text is shown once at login, and only when an escalated call runs as the
227
+ // owner on this machine (outside Docker)
199
228
  else if (cmd.mode !== "docker")
200
- out(FULL_ACCESS_RISK_TEXT);
229
+ out(OUTSIDE_SANDBOX_RISK_TEXT);
201
230
  }
202
231
  else {
203
- out(RESTRICTED_CEILING_LOGIN_LINE);
232
+ out(SANDBOX_ONLY_LOGIN_LINE);
204
233
  }
234
+ out(networkLoginLine(ceiling.network));
205
235
  if (noSandbox !== undefined)
206
236
  out(restrictedUnavailableLoginLine(noSandbox));
207
237
  // An installed service stays stopped after a deliberate exit (not logged in, logged out):
@@ -222,24 +252,20 @@ export async function login(cmd, deps) {
222
252
  // Keep polling at the normal interval: pending, a network blip or a server error.
223
253
  }
224
254
  else if (e.code === "device_sign_in.expired") {
225
- err("The code expired before it was approved. Run `capybara-device login` again.");
226
- return 1;
255
+ return fail("device_sign_in.expired", "The code expired before it was approved. Run `capybara-device login` again.", 1);
227
256
  }
228
257
  else if (e.code === "device_sign_in.denied") {
229
- err("The login was denied in the browser.");
230
- return 1;
258
+ return fail("device_sign_in.denied", "The login was denied in the browser.", 1);
231
259
  }
232
260
  else
233
261
  throw e;
234
262
  if (waited >= expiresIn) {
235
- err("The code expired before it was approved. Run `capybara-device login` again.");
236
- return 1;
263
+ return fail("device_sign_in.expired", "The code expired before it was approved. Run `capybara-device login` again.", 1);
237
264
  }
238
265
  }
239
266
  }
240
267
  }
241
268
  catch (e) {
242
- err(`login failed: ${e.message}`);
243
- return 1;
269
+ return fail(e instanceof ApiError ? e.code : "login_failed", `login failed: ${e.message}`, 1);
244
270
  }
245
271
  }
@@ -5,10 +5,11 @@ import { policyPath, readCredential } from "../state.js";
5
5
  /** The fields `show` prints and `set` may replace. */
6
6
  const KEYS = ["allowedDomains", "allowCredentials", "cpu", "memoryMb"];
7
7
  /** What OS mode does not enforce yet, shown with `show` and with a limits change. */
8
- export const NOT_ENFORCED_NOTE = "Note: in OS mode cpu and memoryMb are not enforced yet; allowCredentials is ignored (Full " +
9
- "access already has the owner's credentials, restricted work gets none).";
8
+ export const NOT_ENFORCED_NOTE = "Note: in OS mode cpu and memoryMb are not enforced yet; allowCredentials is ignored (a call " +
9
+ "outside the sandbox already has the owner's credentials, sandboxed work gets none).";
10
10
  /** Where the owner ceiling is changed (the policy command never changes it). */
11
- const CEILING_COMMAND = "capybara-device ceiling set --full-access allow|deny --project-reach workspace|os-user";
11
+ const CEILING_COMMAND = "capybara-device ceiling set --outside-sandbox on|off --network open|allowlist " +
12
+ "--project-reach workspace|os-user";
12
13
  function list(value) {
13
14
  return value
14
15
  .split(",")
@@ -68,15 +69,19 @@ function edit(current, action, key, values, out) {
68
69
  }
69
70
  /** What `policy show` says the owner ceiling gives on this machine (English, one line each). */
70
71
  export function describeCeiling(policy, runMode = "os") {
71
- const { fullAccess, restrictedProjectReach } = policy.ownerCeiling;
72
+ const { outsideSandbox, network, restrictedProjectReach } = policy.ownerCeiling;
72
73
  const where = runMode === "docker" ? "in the device's container" : "as you on this machine";
73
74
  return [
74
- fullAccess
75
- ? `Owner ceiling: Full access allowed: a conversation set to Full access runs commands ${where}.`
76
- : "Owner ceiling: Full access denied: conversations here use the restricted profiles only.",
75
+ "Commands run in the sandbox under every profile, Full access included.",
76
+ outsideSandbox
77
+ ? `Owner ceiling: outside the sandbox on: one approved call may run ${where}.`
78
+ : "Owner ceiling: outside the sandbox off: no call runs outside the sandbox here.",
79
+ network === "open"
80
+ ? "Sandbox network: open (the public internet; private, local and metadata addresses stay closed)."
81
+ : "Sandbox network: allowlist (allowedDomains only).",
77
82
  restrictedProjectReach === "os-user"
78
- ? "Restricted work and project browsing may read projects outside the device's workspace."
79
- : "Restricted work and project browsing stay in the device's workspace.",
83
+ ? "Sandboxed work and project browsing may read projects outside the device's workspace."
84
+ : "Sandboxed work and project browsing stay in the device's workspace.",
80
85
  `Change the owner ceiling with: ${CEILING_COMMAND}`,
81
86
  ];
82
87
  }
@@ -176,7 +181,8 @@ export async function policyCommand(cmd, deps) {
176
181
  out(l);
177
182
  out(`Saved ${policyPath(stateDir)}. A running capybara-device applies it at the next operation.`);
178
183
  if (key === "allowedDomains") {
179
- out("Restricted work may reach these domains when the organization allows them too.");
184
+ out("Sandboxed work may reach these domains while the network is an allowlist, when the " +
185
+ "organization allows them too.");
180
186
  }
181
187
  if (key === "cpu" || key === "memoryMb" || key === "allowCredentials")
182
188
  out(NOT_ENFORCED_NOTE);
@@ -36,4 +36,6 @@ export declare const BOOTSTRAP_BACKOFF_MS: readonly [500, 1000, 2000];
36
36
  * `service install|uninstall|status`: runs `capybara-device start` at login (launchd) or boot
37
37
  * (systemd user), removes it, or reports whether it is loaded and running.
38
38
  */
39
- export declare function serviceCommand(action: ServiceAction, deps: ServiceDeps): Promise<number>;
39
+ export declare function serviceCommand(action: ServiceAction, deps: ServiceDeps, options?: {
40
+ json?: boolean;
41
+ }): Promise<number>;