@factiii/runner 0.10.2 → 0.11.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@factiii/runner",
3
- "version": "0.10.2",
3
+ "version": "0.11.1",
4
4
  "description": "Factiii Runner, run Board AI agents on a machine you control. Pairs with the Factiii web/mobile clients over WebRTC.",
5
5
  "license": "ISC",
6
6
  "keywords": [
@@ -0,0 +1,208 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * factiii-secrets - run a command with this space's stored secrets injected.
4
+ *
5
+ * A PROXY, never a carrier. This process asks the runner to run the command;
6
+ * the runner is what holds the decrypted values, execs the command, and sends
7
+ * back output already redacted against them. Nothing secret ever enters this
8
+ * process, so nothing secret is visible to whatever spawned it - which is the
9
+ * whole point, because what spawns it is an AI agent.
10
+ *
11
+ * Finding the runner doubles as the gate: this plugin installs globally, so
12
+ * outside a factiii space there is no socket and the command says so rather
13
+ * than hanging.
14
+ */
15
+ 'use strict';
16
+
17
+ const fs = require('fs');
18
+ const net = require('net');
19
+ const path = require('path');
20
+
21
+ const SOCK_NAME = 'secrets.sock';
22
+ /** Denial is not a command failure: a distinct code lets a script tell "the
23
+ * owner said no" from "the command ran and failed". */
24
+ const EXIT_DENIED = 77;
25
+ /** Nothing ran at all - no socket, no runner, bad usage. */
26
+ const EXIT_UNAVAILABLE = 3;
27
+
28
+ function usage() {
29
+ process.stderr.write(
30
+ [
31
+ 'usage:',
32
+ ' factiii-secrets run -- <command...> run it with the declared secrets injected',
33
+ ' factiii-secrets list print the declared key NAMES (never values)',
34
+ ''
35
+ ].join('\n')
36
+ );
37
+ }
38
+
39
+ /** `<spaceDir>/state/secrets.sock`. The env var is what the runner sets on
40
+ * every spawn; the walk-up covers a caller that changed the working
41
+ * directory to somewhere the runner did not choose. */
42
+ function findSocket() {
43
+ const fromEnv = process.env.FACTIII_SECRETS_SOCK;
44
+ if (fromEnv) return fromEnv;
45
+ let dir = process.cwd();
46
+ while (dir && dir !== path.dirname(dir)) {
47
+ const candidate = path.join(dir, 'state', SOCK_NAME);
48
+ try {
49
+ if (fs.statSync(candidate).isSocket()) return candidate;
50
+ } catch {
51
+ // Not here; keep walking.
52
+ }
53
+ dir = path.dirname(dir);
54
+ }
55
+ return null;
56
+ }
57
+
58
+ /**
59
+ * Rebuild one shell word.
60
+ *
61
+ * The runner runs the command through a login shell and shows it to the owner
62
+ * verbatim for authorization, so what we send has to mean exactly what the
63
+ * caller's argv meant. Single quotes make every character literal; the only
64
+ * thing that cannot appear inside them is a single quote, which is spliced.
65
+ * Words that need no quoting are left bare so the authorization card stays
66
+ * readable.
67
+ */
68
+ function shellQuote(word) {
69
+ if (/^[A-Za-z0-9_@%+=:,./-]+$/.test(word)) return word;
70
+ return `'${word.split("'").join(`'\\''`)}'`;
71
+ }
72
+
73
+ /** One NDJSON request, its stream of replies handled by `onMessage`. */
74
+ function request(socketPath, payload, onMessage) {
75
+ return new Promise((resolve) => {
76
+ const conn = net.createConnection(socketPath);
77
+ let buffer = '';
78
+ let settled = false;
79
+ const finish = (code) => {
80
+ if (settled) return;
81
+ settled = true;
82
+ conn.end();
83
+ resolve(code);
84
+ };
85
+
86
+ conn.on('connect', () => conn.write(`${JSON.stringify(payload)}\n`));
87
+ conn.on('data', (chunk) => {
88
+ buffer += chunk.toString('utf-8');
89
+ let newline = buffer.indexOf('\n');
90
+ while (newline !== -1) {
91
+ const line = buffer.slice(0, newline);
92
+ buffer = buffer.slice(newline + 1);
93
+ if (line.trim()) {
94
+ let message;
95
+ try {
96
+ message = JSON.parse(line);
97
+ } catch {
98
+ // A malformed frame is the runner's bug, not the caller's; there
99
+ // is nothing useful to do with half a message.
100
+ message = null;
101
+ }
102
+ if (message) {
103
+ const code = onMessage(message);
104
+ if (typeof code === 'number') finish(code);
105
+ }
106
+ }
107
+ newline = buffer.indexOf('\n');
108
+ }
109
+ });
110
+ conn.on('error', (err) => {
111
+ // The path can be known and still have nothing listening: the store is
112
+ // served only while a deploy run is in progress, and a creator session
113
+ // drafting a skill is the common case of asking outside one.
114
+ const why =
115
+ err.code === 'ENOENT' || err.code === 'ECONNREFUSED'
116
+ ? 'the secrets store is not being served right now. It is available during a deploy run'
117
+ : `cannot reach the runner (${err.message})`;
118
+ process.stderr.write(`factiii-secrets: ${why}.\n`);
119
+ finish(EXIT_UNAVAILABLE);
120
+ });
121
+ // Closed with no verdict: the runner died, or the deploy run was
122
+ // cancelled mid-command. Either way nothing was reported as finished.
123
+ conn.on('close', () => finish(EXIT_UNAVAILABLE));
124
+ });
125
+ }
126
+
127
+ async function main() {
128
+ const [op, ...rest] = process.argv.slice(2);
129
+
130
+ if (!op || op === '-h' || op === '--help') {
131
+ usage();
132
+ return op ? 0 : EXIT_UNAVAILABLE;
133
+ }
134
+ if (op !== 'run' && op !== 'list') {
135
+ process.stderr.write(`factiii-secrets: unknown command "${op}".\n`);
136
+ usage();
137
+ return EXIT_UNAVAILABLE;
138
+ }
139
+
140
+ const socketPath = findSocket();
141
+ if (!socketPath) {
142
+ process.stderr.write(
143
+ 'factiii-secrets: no secrets store is reachable here. This command works inside a factiii space while a deploy run is active.\n'
144
+ );
145
+ return EXIT_UNAVAILABLE;
146
+ }
147
+
148
+ if (op === 'list') {
149
+ return request(socketPath, { op: 'list' }, (message) => {
150
+ if (message.type === 'keys') {
151
+ process.stdout.write(`${message.keys.join('\n')}\n`);
152
+ return 0;
153
+ }
154
+ if (message.type === 'error') {
155
+ process.stderr.write(`factiii-secrets: ${message.message}\n`);
156
+ return EXIT_UNAVAILABLE;
157
+ }
158
+ });
159
+ }
160
+
161
+ // `--` is optional but documented: it is what stops the caller's own flags
162
+ // (-e, --prod) from being read as ours.
163
+ const argv = rest[0] === '--' ? rest.slice(1) : rest;
164
+ if (!argv.length) {
165
+ process.stderr.write('factiii-secrets: run needs a command.\n');
166
+ usage();
167
+ return EXIT_UNAVAILABLE;
168
+ }
169
+
170
+ const command = argv.map(shellQuote).join(' ');
171
+ return request(
172
+ socketPath,
173
+ { op: 'run', command, cwd: process.cwd() },
174
+ (message) => {
175
+ if (message.type === 'status') {
176
+ // stderr, so a caller capturing stdout gets the command's output and
177
+ // nothing of ours.
178
+ process.stderr.write(`factiii-secrets: ${message.text}\n`);
179
+ return;
180
+ }
181
+ if (message.type === 'output') {
182
+ process.stdout.write(message.chunk);
183
+ return;
184
+ }
185
+ if (message.type === 'denied') {
186
+ process.stderr.write(
187
+ 'factiii-secrets: the owner declined to run that command.\n'
188
+ );
189
+ return EXIT_DENIED;
190
+ }
191
+ if (message.type === 'error') {
192
+ process.stderr.write(`factiii-secrets: ${message.message}\n`);
193
+ return EXIT_UNAVAILABLE;
194
+ }
195
+ if (message.type === 'exit') return message.code;
196
+ }
197
+ );
198
+ }
199
+
200
+ main().then(
201
+ (code) => {
202
+ process.exitCode = code;
203
+ },
204
+ (err) => {
205
+ process.stderr.write(`factiii-secrets: ${err && err.message}\n`);
206
+ process.exitCode = EXIT_UNAVAILABLE;
207
+ }
208
+ );
@@ -0,0 +1,82 @@
1
+ ---
2
+ name: factiii-secrets
3
+ description: Run a command that needs this workspace's stored secrets, with factiii-secrets. TRIGGER when a command needs an API token, deploy credential, database URL or any other secret value, when writing or revising a deploy skill, or whenever an environment variable a step needs appears to be missing.
4
+ ---
5
+
6
+ # factiii-secrets
7
+
8
+ This machine keeps the workspace's secrets in an encrypted store the owner
9
+ manages. You never hold those values, and you never need to: one command runs
10
+ anything that needs them.
11
+
12
+ ```bash
13
+ factiii-secrets run -- <command...>
14
+ ```
15
+
16
+ It is already on your PATH. Nothing needs installing, and nothing needs
17
+ setting up before you use it.
18
+
19
+ ## What actually happens
20
+
21
+ You run the command. It blocks. The owner sees the exact command on their
22
+ board, with the keys it will receive, and enters their password to authorize
23
+ it. The runner then decrypts the store for that one command, runs it with the
24
+ values in its environment, and hands you back its output with every secret
25
+ value replaced by `[REDACTED]`. `factiii-secrets` exits with the command's own
26
+ exit code, so `&&`, `||` and `set -e` all behave normally.
27
+
28
+ The values reach the command and stop there. They are never in your
29
+ environment, never in your context, and never in anything you can read.
30
+
31
+ ## Using it
32
+
33
+ Write the command exactly as you would without the wrapper, referencing
34
+ secrets as ordinary `$NAME` variables:
35
+
36
+ ```bash
37
+ factiii-secrets run -- vercel deploy --prod --token "$VERCEL_TOKEN"
38
+ factiii-secrets run -- psql "$DATABASE_URL" -f migrations/latest.sql
39
+ factiii-secrets run -- ./scripts/publish.sh
40
+ ```
41
+
42
+ Quote the variables. They expand on the runner's side, once the values are
43
+ present - not in your shell, where they are empty.
44
+
45
+ Wrap **only** the commands that need a secret. Everything else - builds,
46
+ tests, git, file edits - runs normally, unwrapped, and stays fast because it
47
+ needs no authorization.
48
+
49
+ ```bash
50
+ factiii-secrets list
51
+ ```
52
+
53
+ prints the key NAMES the workspace has declared. Never their values; there is
54
+ no command that prints a value, on purpose.
55
+
56
+ ## Things that will waste your time
57
+
58
+ - **Do not check whether these variables are set.** They are unset in your
59
+ shell by design and will stay that way for the whole session. `env`,
60
+ `printenv`, `test -n "$TOKEN"` and echo-checks all tell you nothing.
61
+ - **Do not report them as missing or stop over them.** An unset declared
62
+ variable is the security model working. Route the command through
63
+ `factiii-secrets run` and it gets what it needs.
64
+ - **Do not ask the owner to export them,** write them to a `.env`, or put them
65
+ in a shell profile or CI config. They are already stored; that is what this
66
+ command reads.
67
+ - **Never print a value.** If output somehow contains one, do not repeat it.
68
+
69
+ ## Exit codes
70
+
71
+ - The command's own exit code, when it ran.
72
+ - `77` - the owner declined to authorize it. That is an answer, not a fault:
73
+ adapt or stop and explain where things stand. Do not retry the same command.
74
+ - `3` - nothing ran. No store is reachable here, or the runner went away.
75
+
76
+ ## Writing a deploy skill
77
+
78
+ A repo's deploy skill declares every key it needs in
79
+ `.agents/skills/deploy/required-variables.json`, then wraps each step that
80
+ needs one in `factiii-secrets run -- …`. Declare a key exactly when a command
81
+ uses it: never declare one the skill does not use, never use one it does not
82
+ declare. Put no secret value, and no key material, in any file.