@oneie/cli 4.1.0 → 4.2.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/README.md +77 -0
- package/dist/package.json +3 -3
- package/dist/src/agent.d.ts +64 -0
- package/dist/src/agent.js +240 -12
- package/dist/src/auth.js +3 -4
- package/dist/src/compiler.js +29 -1
- package/dist/src/doctor.js +61 -6
- package/dist/src/endpoint.d.ts +2 -0
- package/dist/src/endpoint.js +250 -0
- package/dist/src/eval.js +1 -0
- package/dist/src/index.js +8 -0
- package/dist/src/init.js +33 -3
- package/dist/src/lib/agent-env.js +7 -0
- package/dist/src/lib/client.js +16 -7
- package/dist/src/lib/keyfile.d.ts +130 -0
- package/dist/src/lib/keyfile.js +381 -0
- package/dist/src/lib/paths.d.ts +33 -0
- package/dist/src/lib/paths.js +77 -0
- package/dist/src/lib/shop-init.d.ts +62 -0
- package/dist/src/lib/shop-init.js +63 -0
- package/dist/src/lib/tasks-digest.d.ts +66 -0
- package/dist/src/lib/tasks-digest.js +200 -0
- package/dist/src/lib/tasks-graph.d.ts +78 -0
- package/dist/src/lib/tasks-graph.js +425 -0
- package/dist/src/lib/vaultfile.d.ts +73 -0
- package/dist/src/lib/vaultfile.js +184 -0
- package/dist/src/media.d.ts +2 -0
- package/dist/src/media.js +256 -0
- package/dist/src/shop.d.ts +10 -0
- package/dist/src/shop.js +73 -0
- package/dist/src/skill.js +7 -7
- package/dist/src/tasks.d.ts +2 -0
- package/dist/src/tasks.js +424 -0
- package/dist/src/templates.js +8 -0
- package/dist/src/wallet.js +172 -1
- package/package.json +3 -3
- package/dist/src/templates/_node/site/one.config.d.ts +0 -2
- package/dist/src/templates/_node/site/one.config.js +0 -22
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* keyfile.ts — the folder key ceremony.
|
|
3
|
+
*
|
|
4
|
+
* A folder key is a SPENDING CREDENTIAL sitting in a directory someone is about
|
|
5
|
+
* to `git init`. Everything here exists because of that one sentence.
|
|
6
|
+
*
|
|
7
|
+
* THE ORDER IS THE SECURITY. The ignore entry is written BEFORE the key file is
|
|
8
|
+
* created, never after. A key written first and ignored second is exposed for
|
|
9
|
+
* the width of the gap, and the gap is where `git add -A` lives. `writeKeyFolder`
|
|
10
|
+
* therefore refuses to write the key at all if it cannot first prove the path is
|
|
11
|
+
* ignored — an unignorable folder returns `{ ok: false }` rather than a key.
|
|
12
|
+
*
|
|
13
|
+
* WHY A FOLDER AND NOT ~/.config. `oneie init` writes one key to
|
|
14
|
+
* ~/.config/oneie/key, which every project on the machine then shares: one
|
|
15
|
+
* credential, machine-wide blast radius, and no way to revoke a single project
|
|
16
|
+
* without revoking them all. A per-folder key is narrower by construction — it
|
|
17
|
+
* is the project's key, it can be revoked alone, and deleting the project
|
|
18
|
+
* deletes it.
|
|
19
|
+
*
|
|
20
|
+
* WHY THE DOTFILE NAME MATTERS. The repo root .gitignore carries `*.key` (:37)
|
|
21
|
+
* but NOT a bare `key` — so ~/.config/oneie/key would be committed by a scaffold
|
|
22
|
+
* that copied it. The file here is `agent.key`, which `*.key` already covers,
|
|
23
|
+
* AND `.oneie/` is written into the local ignore anyway. Belt and braces, because
|
|
24
|
+
* the scaffold's .gitignore is a file the user is free to edit.
|
|
25
|
+
*
|
|
26
|
+
* WHAT IS DELIBERATELY NOT HERE: wallet key material. The anonymous provision
|
|
27
|
+
* door is safe precisely BECAUSE the identity it mints cannot receive value —
|
|
28
|
+
* `agent-wallet-provision.ts` writes `addressed:false` always ("no server-side
|
|
29
|
+
* key material, ever"), and `isCreditOnlyWallet` keeps such an actor on credits.
|
|
30
|
+
* Minting a fundable identity from an unauthenticated door would turn a free
|
|
31
|
+
* empty-workspace faucet into a wallet faucet. Addresses arrive later, from a
|
|
32
|
+
* separate and deliberate human ceremony: `one wallet keygen` (local BIP-39,
|
|
33
|
+
* mnemonic never uploaded) then `one wallet register` (public addresses only,
|
|
34
|
+
* through the four-key `addressesOnly()` whitelist). Keep the two apart.
|
|
35
|
+
*/
|
|
36
|
+
import { mkdirSync, writeFileSync, readFileSync, existsSync, chmodSync, renameSync, rmSync, realpathSync, lstatSync, openSync, writeSync, closeSync, constants as FS, } from 'node:fs';
|
|
37
|
+
import { randomBytes } from 'node:crypto';
|
|
38
|
+
import { join, resolve, relative, isAbsolute } from 'node:path';
|
|
39
|
+
import { CONFIG_DIR, MACHINE_ID_FILE, LEGACY_MACHINE_ID_FILE, firstExisting } from './paths.js';
|
|
40
|
+
/** The per-project directory holding the key, the vault and their metadata. */
|
|
41
|
+
export const KEY_DIR_NAME = '.one';
|
|
42
|
+
/**
|
|
43
|
+
* The pre-rename project directory. READ-ONLY — a folder minted before the
|
|
44
|
+
* rename keeps working; nothing is ever written here again. Same fallback shape
|
|
45
|
+
* as `paths.ts` uses for ~/.config, and for the same reason: a rename must not
|
|
46
|
+
* present as "my key stopped working".
|
|
47
|
+
*/
|
|
48
|
+
export const LEGACY_KEY_DIR_NAME = '.oneie';
|
|
49
|
+
/** `*.key` is already covered by the scaffold's root .gitignore. */
|
|
50
|
+
export const KEY_FILE_NAME = 'agent.key';
|
|
51
|
+
export const META_FILE_NAME = 'agent.json';
|
|
52
|
+
/** What the ignore file must contain before any key is written. */
|
|
53
|
+
const IGNORE_ENTRY = `${KEY_DIR_NAME}/`;
|
|
54
|
+
const IGNORE_BLOCK = [
|
|
55
|
+
'',
|
|
56
|
+
'# ONE — agent credential. Never commit.',
|
|
57
|
+
IGNORE_ENTRY,
|
|
58
|
+
'',
|
|
59
|
+
].join('\n');
|
|
60
|
+
/**
|
|
61
|
+
* True when `.gitignore` already ignores the key directory.
|
|
62
|
+
*
|
|
63
|
+
* Deliberately a literal scan, not a `git check-ignore` shell-out: the folder
|
|
64
|
+
* may not be a git repo yet (that is the common case — `npx oneie` runs before
|
|
65
|
+
* `git init`), and a check that silently passes on "not a repo" would be the
|
|
66
|
+
* false green this whole module exists to avoid.
|
|
67
|
+
*/
|
|
68
|
+
export function ignoresKeyDir(ignoreText) {
|
|
69
|
+
return ignoreText
|
|
70
|
+
.split('\n')
|
|
71
|
+
.map((l) => l.trim())
|
|
72
|
+
.some((l) => l === IGNORE_ENTRY || l === KEY_DIR_NAME || l === `/${IGNORE_ENTRY}` || l === `/${KEY_DIR_NAME}`);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Ensure `<dir>/.gitignore` ignores the key directory. Returns the path on
|
|
76
|
+
* success, or null when it could not be written — in which case the caller must
|
|
77
|
+
* NOT write a key.
|
|
78
|
+
*/
|
|
79
|
+
export function ensureIgnored(dir) {
|
|
80
|
+
const ignorePath = join(dir, '.gitignore');
|
|
81
|
+
try {
|
|
82
|
+
const existing = existsSync(ignorePath) ? readFileSync(ignorePath, 'utf8') : '';
|
|
83
|
+
if (ignoresKeyDir(existing))
|
|
84
|
+
return ignorePath;
|
|
85
|
+
// Append, never rewrite — the scaffold's own ignore rules are not ours to drop.
|
|
86
|
+
const next = existing.length && !existing.endsWith('\n') ? existing + '\n' + IGNORE_BLOCK : existing + IGNORE_BLOCK;
|
|
87
|
+
writeFileSync(ignorePath, next, { mode: 0o644 });
|
|
88
|
+
// Re-read rather than trust the write: a read-only mount or a full disk can
|
|
89
|
+
// fail without throwing on some platforms, and a key must never follow an
|
|
90
|
+
// ignore entry that is not actually on disk.
|
|
91
|
+
return ignoresKeyDir(readFileSync(ignorePath, 'utf8')) ? ignorePath : null;
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Canonical absolute path — symlinks resolved.
|
|
99
|
+
*
|
|
100
|
+
* `resolve()` alone is NOT enough for the path binding. On macOS `/tmp` is a
|
|
101
|
+
* symlink to `/private/tmp` and `/var` to `/private/var`, so a key minted at the
|
|
102
|
+
* path the user typed and read back after a `chdir` compares unequal and a
|
|
103
|
+
* legitimate credential is refused. Home directories and network mounts are
|
|
104
|
+
* symlinked on plenty of real machines too. Compare the REAL path on both sides
|
|
105
|
+
* or the check fails open on attackers and closed on users — the worst pairing.
|
|
106
|
+
*
|
|
107
|
+
* Falls back to `resolve()` when the path does not exist yet (realpath throws).
|
|
108
|
+
*/
|
|
109
|
+
export function canonicalDir(p) {
|
|
110
|
+
return canonical(p);
|
|
111
|
+
}
|
|
112
|
+
function canonical(p) {
|
|
113
|
+
try {
|
|
114
|
+
return realpathSync(resolve(p));
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return resolve(p);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* A stable, secret-ish id for THIS machine.
|
|
122
|
+
*
|
|
123
|
+
* The path binding alone is not enough where the path is predictable. A repo
|
|
124
|
+
* that ships `.oneie/agent.json` recording `dir:
|
|
125
|
+
* "/home/runner/work/<repo>/<repo>"` matches that path on EVERY GitHub Actions
|
|
126
|
+
* checkout of it — so the clone attack the binding closes for a laptop reopens
|
|
127
|
+
* for CI, which is exactly where nobody is watching. The machine id is a value
|
|
128
|
+
* the attacker cannot guess and cannot read from the victim's repo, so a key
|
|
129
|
+
* must be BOTH at the right path AND on the right machine.
|
|
130
|
+
*
|
|
131
|
+
* Stored 0600 beside the machine-wide key. Not a secret in the cryptographic
|
|
132
|
+
* sense — it authorises nothing on its own — so a read-only or unwritable home
|
|
133
|
+
* degrades to path-only binding rather than refusing all work.
|
|
134
|
+
*/
|
|
135
|
+
export function machineId() {
|
|
136
|
+
try {
|
|
137
|
+
// Read the legacy path too: this id is baked into every .oneie/agent.json
|
|
138
|
+
// already on disk, so minting a fresh one at the new path would flip every
|
|
139
|
+
// existing folder key to `foreign` at once — a mass logout caused by a rename.
|
|
140
|
+
const existing = firstExisting(MACHINE_ID_FILE, LEGACY_MACHINE_ID_FILE);
|
|
141
|
+
if (existing) {
|
|
142
|
+
const v = readFileSync(existing, 'utf8').trim();
|
|
143
|
+
if (v)
|
|
144
|
+
return v;
|
|
145
|
+
}
|
|
146
|
+
const dir = CONFIG_DIR;
|
|
147
|
+
const file = MACHINE_ID_FILE;
|
|
148
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
149
|
+
const v = randomBytes(16).toString('hex');
|
|
150
|
+
writeFileSync(file, v + '\n', { mode: 0o600 });
|
|
151
|
+
chmodSync(file, 0o600);
|
|
152
|
+
return v;
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Write `data` to `path` at 0600, refusing to follow a symlink.
|
|
160
|
+
*
|
|
161
|
+
* `writeFileSync` FOLLOWS symlinks. An attacker who can pre-plant
|
|
162
|
+
* `.oneie/agent.key.tmp` as a link to a file they control gets our chmod(0600)
|
|
163
|
+
* and our credential written to their target — the classic pre-plant, and the
|
|
164
|
+
* 0700 directory does not help because the link is created before we ever run.
|
|
165
|
+
* `O_NOFOLLOW` makes the kernel refuse, and `O_EXCL` means we never write onto
|
|
166
|
+
* an existing file at all.
|
|
167
|
+
*/
|
|
168
|
+
export function writeSecret(path, data) {
|
|
169
|
+
// Create the 0700 parent HERE rather than assuming a caller did. writeKeyFolder
|
|
170
|
+
// happened to mkdir first; writeVaultFolder did not, and every one of its writes
|
|
171
|
+
// failed with ENOENT until this line existed. A helper that silently depends on
|
|
172
|
+
// an unstated precondition is a helper that works for exactly one caller.
|
|
173
|
+
const parent = join(path, '..');
|
|
174
|
+
assertRealDir(parent);
|
|
175
|
+
mkdirSync(parent, { recursive: true, mode: 0o700 });
|
|
176
|
+
chmodSync(parent, 0o700);
|
|
177
|
+
if (existsSync(path) && !lstatSync(path).isSymbolicLink())
|
|
178
|
+
rmSync(path, { force: true });
|
|
179
|
+
writeNoFollow(path, data);
|
|
180
|
+
}
|
|
181
|
+
function writeNoFollow(path, data) {
|
|
182
|
+
const fd = openSync(path, FS.O_WRONLY | FS.O_CREAT | FS.O_EXCL | FS.O_NOFOLLOW, 0o600);
|
|
183
|
+
try {
|
|
184
|
+
writeSync(fd, data);
|
|
185
|
+
}
|
|
186
|
+
finally {
|
|
187
|
+
closeSync(fd);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/** Refuse a key directory that is a symlink — same pre-plant, one level up. */
|
|
191
|
+
function assertRealDir(p) {
|
|
192
|
+
if (!existsSync(p))
|
|
193
|
+
return;
|
|
194
|
+
const st = lstatSync(p);
|
|
195
|
+
if (st.isSymbolicLink())
|
|
196
|
+
throw new Error(`${p} is a symlink — refusing to write a credential through it`);
|
|
197
|
+
if (!st.isDirectory())
|
|
198
|
+
throw new Error(`${p} exists and is not a directory`);
|
|
199
|
+
}
|
|
200
|
+
/** Reject a directory outside the project — the key belongs to the folder, not above it. */
|
|
201
|
+
function insideProject(dir, target) {
|
|
202
|
+
const rel = relative(resolve(dir), resolve(target));
|
|
203
|
+
return rel !== '' && !rel.startsWith('..') && !isAbsolute(rel);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Write the key folder: ignore entry FIRST, then 0700 dir, then 0600 key via
|
|
207
|
+
* tmp+rename so a reader never observes a partial or world-readable file.
|
|
208
|
+
*
|
|
209
|
+
* Refuses (and writes nothing) when the ignore entry cannot be established.
|
|
210
|
+
*/
|
|
211
|
+
export function writeKeyFolder(input) {
|
|
212
|
+
const dir = canonical(input.dir);
|
|
213
|
+
if (!existsSync(dir))
|
|
214
|
+
return { ok: false, error: `no such directory: ${dir}` };
|
|
215
|
+
// 1. IGNORE FIRST. No ignore, no key.
|
|
216
|
+
const ignorePath = ensureIgnored(dir);
|
|
217
|
+
if (!ignorePath) {
|
|
218
|
+
return { ok: false, error: 'could not write .gitignore — refusing to write a credential that git can see' };
|
|
219
|
+
}
|
|
220
|
+
const keyDir = join(dir, KEY_DIR_NAME);
|
|
221
|
+
if (!insideProject(dir, keyDir))
|
|
222
|
+
return { ok: false, error: 'key path escapes the project directory' };
|
|
223
|
+
try {
|
|
224
|
+
// 2. 0700 — the directory itself is not listable by other local users.
|
|
225
|
+
// assertRealDir first: a pre-planted `.oneie` SYMLINK would otherwise be
|
|
226
|
+
// followed by every call below, landing the credential wherever it points.
|
|
227
|
+
assertRealDir(keyDir);
|
|
228
|
+
mkdirSync(keyDir, { recursive: true, mode: 0o700 });
|
|
229
|
+
chmodSync(keyDir, 0o700);
|
|
230
|
+
// 3. 0600 via tmp+rename. The mode is set on the tmp file BEFORE the rename,
|
|
231
|
+
// so the key is never briefly readable at the final path. chmod again
|
|
232
|
+
// because `mode` in writeFileSync is subject to the process umask.
|
|
233
|
+
const keyPath = join(keyDir, KEY_FILE_NAME);
|
|
234
|
+
const tmp = join(keyDir, `.${KEY_FILE_NAME}.tmp`);
|
|
235
|
+
// O_NOFOLLOW|O_EXCL — never follow a pre-planted link, never write onto an
|
|
236
|
+
// existing file. A stale tmp from a crashed run is removed first, by path,
|
|
237
|
+
// after the same symlink check.
|
|
238
|
+
if (existsSync(tmp) && !lstatSync(tmp).isSymbolicLink())
|
|
239
|
+
rmSync(tmp, { force: true });
|
|
240
|
+
writeNoFollow(tmp, input.apiKey + '\n');
|
|
241
|
+
renameSync(tmp, keyPath);
|
|
242
|
+
// 4. Metadata, 0600 as well — regKey is a credential too.
|
|
243
|
+
//
|
|
244
|
+
// BOTH keys land here, and they are not interchangeable. `apiKey` is
|
|
245
|
+
// workspace-scoped and is what an agent authenticates and trades with.
|
|
246
|
+
// `regKey` is uid-bound: receivers keyed on ctx.ownerSlug === uid — e.g.
|
|
247
|
+
// `lifecycle:mine`, which reads the actor's own onboarding progress —
|
|
248
|
+
// return ZERO ROWS for the apiKey, because ownerSlug resolves to the
|
|
249
|
+
// workspace aid and the fresh identity has no rows under it. A folder
|
|
250
|
+
// carrying only apiKey cannot read its own lifecycle.
|
|
251
|
+
const metaPath = join(keyDir, META_FILE_NAME);
|
|
252
|
+
const meta = {
|
|
253
|
+
workspace: input.workspace,
|
|
254
|
+
...(input.uid ? { uid: input.uid } : {}),
|
|
255
|
+
api: input.api,
|
|
256
|
+
// The ABSOLUTE path this key was minted for. readKeyFolder refuses to hand
|
|
257
|
+
// back a credential found at any other path — see its docblock.
|
|
258
|
+
dir,
|
|
259
|
+
// …and the machine. A CI checkout path is guessable; this is not.
|
|
260
|
+
...(machineId() ? { machine: machineId() } : {}),
|
|
261
|
+
...(input.regKey ? { regKey: input.regKey } : {}),
|
|
262
|
+
mintedAt: new Date().toISOString(),
|
|
263
|
+
...(input.expiresAt ? { expiresAt: new Date(input.expiresAt * 1000).toISOString() } : {}),
|
|
264
|
+
...(input.spendLimitUsd !== undefined ? { spendLimitUsd: input.spendLimitUsd } : {}),
|
|
265
|
+
note: 'Credential. Never commit. Revoke with `one auth revoke`.',
|
|
266
|
+
};
|
|
267
|
+
const metaTmp = join(keyDir, `.${META_FILE_NAME}.tmp`);
|
|
268
|
+
if (existsSync(metaTmp) && !lstatSync(metaTmp).isSymbolicLink())
|
|
269
|
+
rmSync(metaTmp, { force: true });
|
|
270
|
+
writeNoFollow(metaTmp, JSON.stringify(meta, null, 2) + '\n');
|
|
271
|
+
renameSync(metaTmp, metaPath);
|
|
272
|
+
return { ok: true, keyPath, metaPath, ignorePath };
|
|
273
|
+
}
|
|
274
|
+
catch (err) {
|
|
275
|
+
return { ok: false, error: `write failed: ${err.message}` };
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Read a folder key back, or null when the folder has none — or when the key
|
|
280
|
+
* found there was minted for a DIFFERENT directory.
|
|
281
|
+
*
|
|
282
|
+
* THE PATH BINDING IS A SECURITY CONTROL, not bookkeeping. `resolveKey` consults
|
|
283
|
+
* the current working directory, so without this check any directory you `cd`
|
|
284
|
+
* into can supply your credentials: clone a repo that ships a `.oneie/agent.key`,
|
|
285
|
+
* run one `one` command inside it, and you are authenticated as WHOEVER MINTED
|
|
286
|
+
* THAT KEY. The damage is not that your key leaks — it is that your next
|
|
287
|
+
* `one push` writes your agents and skills into THEIR workspace, and your next
|
|
288
|
+
* `one trade` spends against their ledger. That is a credential-confusion
|
|
289
|
+
* exfiltration, and the folder-key rung introduced it.
|
|
290
|
+
*
|
|
291
|
+
* `agent.json` records the absolute path the key was minted for, so a key that
|
|
292
|
+
* travelled — by clone, copy, or archive — refuses rather than authenticates.
|
|
293
|
+
* This is the same shape as git's `safe.directory`, decided per key instead of
|
|
294
|
+
* per user.
|
|
295
|
+
*
|
|
296
|
+
* A key with NO recorded `dir` is pre-binding and also refused: the whole point
|
|
297
|
+
* is that an unbound credential is the untrustworthy case, and treating a
|
|
298
|
+
* missing field as permission is how the check gets bypassed by simply deleting
|
|
299
|
+
* a line.
|
|
300
|
+
*/
|
|
301
|
+
export function readKeyFolder(dir) {
|
|
302
|
+
const root = canonical(dir);
|
|
303
|
+
// .one first, then the pre-rename .oneie — a folder minted before the rename
|
|
304
|
+
// keeps working rather than reading as "no key here".
|
|
305
|
+
const keyDir = existsSync(join(root, KEY_DIR_NAME, KEY_FILE_NAME))
|
|
306
|
+
? join(root, KEY_DIR_NAME)
|
|
307
|
+
: join(root, LEGACY_KEY_DIR_NAME);
|
|
308
|
+
const keyPath = join(keyDir, KEY_FILE_NAME);
|
|
309
|
+
if (!existsSync(keyPath))
|
|
310
|
+
return null;
|
|
311
|
+
try {
|
|
312
|
+
const apiKey = readFileSync(keyPath, 'utf8').trim();
|
|
313
|
+
if (!apiKey)
|
|
314
|
+
return null;
|
|
315
|
+
const metaPath = join(keyDir, META_FILE_NAME);
|
|
316
|
+
const meta = existsSync(metaPath)
|
|
317
|
+
? JSON.parse(readFileSync(metaPath, 'utf8'))
|
|
318
|
+
: {};
|
|
319
|
+
if (typeof meta.dir !== 'string' || canonical(meta.dir) !== root)
|
|
320
|
+
return null;
|
|
321
|
+
// The machine binding. Absent in the metadata means a key minted before this
|
|
322
|
+
// existed, or on a machine with no writable home — accepted, because the path
|
|
323
|
+
// binding still holds and refusing would break working setups. PRESENT and
|
|
324
|
+
// DIFFERENT is refused: that is a key that travelled.
|
|
325
|
+
const mine = machineId();
|
|
326
|
+
if (typeof meta.machine === 'string' && mine && meta.machine !== mine)
|
|
327
|
+
return null;
|
|
328
|
+
// Mode and ownership. A credential that is group- or world-readable, or owned
|
|
329
|
+
// by someone else, is not one we should be sending: either it leaked already
|
|
330
|
+
// or it was planted. Checked with lstat so a symlinked key is refused rather
|
|
331
|
+
// than followed to a file whose mode looks fine.
|
|
332
|
+
const st = lstatSync(keyPath);
|
|
333
|
+
if (st.isSymbolicLink())
|
|
334
|
+
return null;
|
|
335
|
+
if ((st.mode & 0o077) !== 0)
|
|
336
|
+
return null;
|
|
337
|
+
if (typeof process.getuid === 'function' && st.uid !== process.getuid())
|
|
338
|
+
return null;
|
|
339
|
+
return { apiKey, meta };
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Why a folder key present on disk was not used. Separate from `readKeyFolder`
|
|
347
|
+
* so a refusal can be EXPLAINED without the reader having to hold a credential:
|
|
348
|
+
* a silent null is right for `resolveKey` and useless for `doctor`.
|
|
349
|
+
*/
|
|
350
|
+
export function keyFolderStatus(dir) {
|
|
351
|
+
const root = canonical(dir);
|
|
352
|
+
const keyPath = existsSync(join(root, KEY_DIR_NAME, KEY_FILE_NAME))
|
|
353
|
+
? join(root, KEY_DIR_NAME, KEY_FILE_NAME)
|
|
354
|
+
: join(root, LEGACY_KEY_DIR_NAME, KEY_FILE_NAME);
|
|
355
|
+
if (!existsSync(keyPath))
|
|
356
|
+
return 'none';
|
|
357
|
+
try {
|
|
358
|
+
if (!readFileSync(keyPath, 'utf8').trim())
|
|
359
|
+
return 'unreadable';
|
|
360
|
+
const metaPath = join(keyPath, '..', META_FILE_NAME);
|
|
361
|
+
if (!existsSync(metaPath))
|
|
362
|
+
return 'foreign';
|
|
363
|
+
const meta = JSON.parse(readFileSync(metaPath, 'utf8'));
|
|
364
|
+
if (typeof meta.dir !== 'string' || canonical(meta.dir) !== root)
|
|
365
|
+
return 'foreign';
|
|
366
|
+
const mine = machineId();
|
|
367
|
+
if (typeof meta.machine === 'string' && mine && meta.machine !== mine)
|
|
368
|
+
return 'foreign';
|
|
369
|
+
const st = lstatSync(keyPath);
|
|
370
|
+
if (st.isSymbolicLink())
|
|
371
|
+
return 'unsafe';
|
|
372
|
+
if ((st.mode & 0o077) !== 0)
|
|
373
|
+
return 'unsafe';
|
|
374
|
+
if (typeof process.getuid === 'function' && st.uid !== process.getuid())
|
|
375
|
+
return 'unsafe';
|
|
376
|
+
return 'ok';
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
return 'unreadable';
|
|
380
|
+
}
|
|
381
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** `~/.config/one` — the current home for CLI state. */
|
|
2
|
+
export declare const CONFIG_DIR: string;
|
|
3
|
+
/**
|
|
4
|
+
* `~/.config/oneie` — where everything lived until the rename. READ-ONLY: kept
|
|
5
|
+
* so an existing install keeps working, never written to.
|
|
6
|
+
*/
|
|
7
|
+
export declare const LEGACY_CONFIG_DIR: string;
|
|
8
|
+
/** The machine-wide API key. Written here; also read from the legacy path. */
|
|
9
|
+
export declare const KEY_FILE: string;
|
|
10
|
+
export declare const LEGACY_KEY_FILE: string;
|
|
11
|
+
/**
|
|
12
|
+
* The per-machine binding id used by the folder-key guard (`keyfile.ts`).
|
|
13
|
+
*
|
|
14
|
+
* Kept on the SAME fallback ladder as the key, and for a sharper reason: this
|
|
15
|
+
* value is baked into every `.oneie/agent.json` already on disk. Reading only
|
|
16
|
+
* the new path would mint a fresh id, every existing folder key would compare
|
|
17
|
+
* `machine` mismatched, and they would all go `foreign` at once — a silent
|
|
18
|
+
* mass logout caused by a rename.
|
|
19
|
+
*/
|
|
20
|
+
export declare const MACHINE_ID_FILE: string;
|
|
21
|
+
export declare const LEGACY_MACHINE_ID_FILE: string;
|
|
22
|
+
/**
|
|
23
|
+
* First existing path from `candidates`, or null.
|
|
24
|
+
*
|
|
25
|
+
* Existence, not readability — a caller that needs the contents handles its own
|
|
26
|
+
* errors, and a path that exists but cannot be read is a different problem from
|
|
27
|
+
* one that was never there.
|
|
28
|
+
*/
|
|
29
|
+
export declare function firstExisting(...candidates: string[]): string | null;
|
|
30
|
+
/** The machine-wide key, new path first, legacy second. Empty string when absent. */
|
|
31
|
+
export declare function readMachineKey(): string;
|
|
32
|
+
/** Which path the machine-wide key actually came from — for `doctor`, never for auth. */
|
|
33
|
+
export declare function machineKeyPath(): string | null;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* paths.ts — the ONE place that knows where ONE keeps things on disk.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS FILE EXISTS. Six modules each carried their own
|
|
5
|
+
* `resolve(homedir(), '.config', 'oneie', 'key')` — `client.ts`, `doctor.ts`,
|
|
6
|
+
* `auth.ts`, `keyfile.ts` and `skill.ts` in three places. Six literals is six
|
|
7
|
+
* chances to change five of them: `login` writes one path, `skill push` reads
|
|
8
|
+
* another, and the user sees "I'm logged in but nothing works", which is the
|
|
9
|
+
* hardest class of bug to recognise because every individual command is correct.
|
|
10
|
+
*
|
|
11
|
+
* THE MIGRATION IS A READ FALLBACK, NOT A MOVE. The directory is now
|
|
12
|
+
* `~/.config/one/`. Anyone who authenticated before this change has a key at
|
|
13
|
+
* `~/.config/oneie/key`, and an upgrade that silently logs them out would look
|
|
14
|
+
* like a broken release. So: writes always go to the new path, reads try the new
|
|
15
|
+
* path and then the old one. Nothing is copied or deleted behind the user's
|
|
16
|
+
* back — `one auth login` rewrites at the new path on its own, and the old file
|
|
17
|
+
* is theirs to remove.
|
|
18
|
+
*/
|
|
19
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
20
|
+
import { homedir } from 'node:os';
|
|
21
|
+
import { resolve } from 'node:path';
|
|
22
|
+
/** `~/.config/one` — the current home for CLI state. */
|
|
23
|
+
export const CONFIG_DIR = resolve(homedir(), '.config', 'one');
|
|
24
|
+
/**
|
|
25
|
+
* `~/.config/oneie` — where everything lived until the rename. READ-ONLY: kept
|
|
26
|
+
* so an existing install keeps working, never written to.
|
|
27
|
+
*/
|
|
28
|
+
export const LEGACY_CONFIG_DIR = resolve(homedir(), '.config', 'oneie');
|
|
29
|
+
/** The machine-wide API key. Written here; also read from the legacy path. */
|
|
30
|
+
export const KEY_FILE = resolve(CONFIG_DIR, 'key');
|
|
31
|
+
export const LEGACY_KEY_FILE = resolve(LEGACY_CONFIG_DIR, 'key');
|
|
32
|
+
/**
|
|
33
|
+
* The per-machine binding id used by the folder-key guard (`keyfile.ts`).
|
|
34
|
+
*
|
|
35
|
+
* Kept on the SAME fallback ladder as the key, and for a sharper reason: this
|
|
36
|
+
* value is baked into every `.oneie/agent.json` already on disk. Reading only
|
|
37
|
+
* the new path would mint a fresh id, every existing folder key would compare
|
|
38
|
+
* `machine` mismatched, and they would all go `foreign` at once — a silent
|
|
39
|
+
* mass logout caused by a rename.
|
|
40
|
+
*/
|
|
41
|
+
export const MACHINE_ID_FILE = resolve(CONFIG_DIR, 'machine-id');
|
|
42
|
+
export const LEGACY_MACHINE_ID_FILE = resolve(LEGACY_CONFIG_DIR, 'machine-id');
|
|
43
|
+
/**
|
|
44
|
+
* First existing path from `candidates`, or null.
|
|
45
|
+
*
|
|
46
|
+
* Existence, not readability — a caller that needs the contents handles its own
|
|
47
|
+
* errors, and a path that exists but cannot be read is a different problem from
|
|
48
|
+
* one that was never there.
|
|
49
|
+
*/
|
|
50
|
+
export function firstExisting(...candidates) {
|
|
51
|
+
for (const p of candidates) {
|
|
52
|
+
try {
|
|
53
|
+
if (existsSync(p))
|
|
54
|
+
return p;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
/* unreadable home, permission denied — treat as absent */
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
/** The machine-wide key, new path first, legacy second. Empty string when absent. */
|
|
63
|
+
export function readMachineKey() {
|
|
64
|
+
const file = firstExisting(KEY_FILE, LEGACY_KEY_FILE);
|
|
65
|
+
if (!file)
|
|
66
|
+
return '';
|
|
67
|
+
try {
|
|
68
|
+
return readFileSync(file, 'utf8').trim();
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return '';
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Which path the machine-wide key actually came from — for `doctor`, never for auth. */
|
|
75
|
+
export function machineKeyPath() {
|
|
76
|
+
return firstExisting(KEY_FILE, LEGACY_KEY_FILE);
|
|
77
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `one shop init` — the terminal half of the shop pack.
|
|
3
|
+
*
|
|
4
|
+
* Pure on purpose: every round trip arrives through `deps.ask`, so the whole
|
|
5
|
+
* command is exercised without a network and without a key. `src/shop.ts` is
|
|
6
|
+
* the commander wiring and holds no logic of its own.
|
|
7
|
+
*
|
|
8
|
+
* TWO RECEIVERS, ONE OF WHICH MAY NOT BE THERE YET.
|
|
9
|
+
* `pages:instantiate-shop-pack` ships. `pages:fill-shop-pack` is C9.3 of
|
|
10
|
+
* `text/ecommerce-site-todo.md` and lands in parallel with this — and an
|
|
11
|
+
* unknown receiver DISSOLVES, which answers HTTP 200. So the fill leg is read
|
|
12
|
+
* for its outcome and reported separately: `seeded` and `filled` are two facts,
|
|
13
|
+
* and a shop whose pages exist but carry no facts must not report as either a
|
|
14
|
+
* clean success or a total failure.
|
|
15
|
+
*/
|
|
16
|
+
export interface AskEnvelope {
|
|
17
|
+
outcome?: string;
|
|
18
|
+
result?: {
|
|
19
|
+
ok?: boolean;
|
|
20
|
+
slug?: string;
|
|
21
|
+
error?: string;
|
|
22
|
+
pages?: Array<{
|
|
23
|
+
pageSlug?: string;
|
|
24
|
+
slug?: string;
|
|
25
|
+
}>;
|
|
26
|
+
filled?: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export interface ShopInitDeps {
|
|
30
|
+
ask(receiver: string, data: unknown): Promise<AskEnvelope>;
|
|
31
|
+
}
|
|
32
|
+
export interface ShopInitResult {
|
|
33
|
+
ok: boolean;
|
|
34
|
+
workspace: string;
|
|
35
|
+
/** The page slugs the seed reported, first one first. */
|
|
36
|
+
pages: string[];
|
|
37
|
+
/** Was a fill asked for, and did it happen. */
|
|
38
|
+
filled: boolean;
|
|
39
|
+
fillRequested: boolean;
|
|
40
|
+
error?: string;
|
|
41
|
+
fillError?: string;
|
|
42
|
+
}
|
|
43
|
+
/** One reading of an `/api/ask` answer. A dissolve and a refusal both arrive as 200. */
|
|
44
|
+
export declare function readEnvelope(env: AskEnvelope, receiver: string): {
|
|
45
|
+
ok: true;
|
|
46
|
+
result: NonNullable<AskEnvelope['result']>;
|
|
47
|
+
} | {
|
|
48
|
+
ok: false;
|
|
49
|
+
error: string;
|
|
50
|
+
};
|
|
51
|
+
/** Parse a facts file. A file that is not an object is refused by name, never coerced. */
|
|
52
|
+
export declare function parseFacts(raw: string): {
|
|
53
|
+
ok: true;
|
|
54
|
+
facts: Record<string, unknown>;
|
|
55
|
+
} | {
|
|
56
|
+
ok: false;
|
|
57
|
+
error: string;
|
|
58
|
+
};
|
|
59
|
+
export declare function shopInit(deps: ShopInitDeps, opts: {
|
|
60
|
+
workspace: string;
|
|
61
|
+
facts?: Record<string, unknown>;
|
|
62
|
+
}): Promise<ShopInitResult>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `one shop init` — the terminal half of the shop pack.
|
|
3
|
+
*
|
|
4
|
+
* Pure on purpose: every round trip arrives through `deps.ask`, so the whole
|
|
5
|
+
* command is exercised without a network and without a key. `src/shop.ts` is
|
|
6
|
+
* the commander wiring and holds no logic of its own.
|
|
7
|
+
*
|
|
8
|
+
* TWO RECEIVERS, ONE OF WHICH MAY NOT BE THERE YET.
|
|
9
|
+
* `pages:instantiate-shop-pack` ships. `pages:fill-shop-pack` is C9.3 of
|
|
10
|
+
* `text/ecommerce-site-todo.md` and lands in parallel with this — and an
|
|
11
|
+
* unknown receiver DISSOLVES, which answers HTTP 200. So the fill leg is read
|
|
12
|
+
* for its outcome and reported separately: `seeded` and `filled` are two facts,
|
|
13
|
+
* and a shop whose pages exist but carry no facts must not report as either a
|
|
14
|
+
* clean success or a total failure.
|
|
15
|
+
*/
|
|
16
|
+
/** One reading of an `/api/ask` answer. A dissolve and a refusal both arrive as 200. */
|
|
17
|
+
export function readEnvelope(env, receiver) {
|
|
18
|
+
if (env.outcome && env.outcome !== 'result')
|
|
19
|
+
return { ok: false, error: `${receiver} ${env.outcome}` };
|
|
20
|
+
const result = env.result;
|
|
21
|
+
if (!result?.ok)
|
|
22
|
+
return { ok: false, error: result?.error ?? `${receiver} returned no result` };
|
|
23
|
+
return { ok: true, result };
|
|
24
|
+
}
|
|
25
|
+
/** Parse a facts file. A file that is not an object is refused by name, never coerced. */
|
|
26
|
+
export function parseFacts(raw) {
|
|
27
|
+
let parsed;
|
|
28
|
+
try {
|
|
29
|
+
parsed = JSON.parse(raw);
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
return { ok: false, error: `facts file is not valid JSON: ${e.message}` };
|
|
33
|
+
}
|
|
34
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
35
|
+
return { ok: false, error: 'facts file must be a JSON object of ShopBusinessFacts' };
|
|
36
|
+
}
|
|
37
|
+
return { ok: true, facts: parsed };
|
|
38
|
+
}
|
|
39
|
+
export async function shopInit(deps, opts) {
|
|
40
|
+
const fillRequested = opts.facts !== undefined;
|
|
41
|
+
const base = { ok: false, workspace: opts.workspace, pages: [], filled: false, fillRequested };
|
|
42
|
+
if (!opts.workspace)
|
|
43
|
+
return { ...base, error: 'no workspace — pass --workspace <slug>' };
|
|
44
|
+
const seedEnv = await deps.ask('pages:instantiate-shop-pack', { slug: opts.workspace });
|
|
45
|
+
const seed = readEnvelope(seedEnv, 'pages:instantiate-shop-pack');
|
|
46
|
+
if (!seed.ok)
|
|
47
|
+
return { ...base, error: seed.error };
|
|
48
|
+
// Today the receiver answers with ONE page; C9.1 answers with the whole pack.
|
|
49
|
+
// Read both rather than making this command wait on that cycle.
|
|
50
|
+
const listed = Array.isArray(seed.result.pages) ? seed.result.pages : [];
|
|
51
|
+
const pages = listed.map(p => p.slug).filter((s) => typeof s === 'string' && s.length > 0);
|
|
52
|
+
if (!pages.length && seed.result.slug)
|
|
53
|
+
pages.push(seed.result.slug);
|
|
54
|
+
if (!pages.length)
|
|
55
|
+
return { ...base, error: 'the pack seeded no pages' };
|
|
56
|
+
if (!fillRequested)
|
|
57
|
+
return { ...base, ok: true, pages };
|
|
58
|
+
const fillEnv = await deps.ask('pages:fill-shop-pack', { slug: opts.workspace, facts: opts.facts });
|
|
59
|
+
const fill = readEnvelope(fillEnv, 'pages:fill-shop-pack');
|
|
60
|
+
if (!fill.ok)
|
|
61
|
+
return { ...base, ok: true, pages, fillError: fill.error };
|
|
62
|
+
return { ...base, ok: true, pages, filled: true };
|
|
63
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { BoardRes } from '@oneie/sdk/tasks-bulk';
|
|
2
|
+
export type Row = NonNullable<BoardRes['tasks']>[number];
|
|
3
|
+
export type Kind = 'work' | 'approval' | 'all';
|
|
4
|
+
export declare const CLOSED: ReadonlySet<string>;
|
|
5
|
+
export declare function kindOf(r: Row): 'work' | 'approval';
|
|
6
|
+
export declare function filterKind(rows: Row[], kind: Kind): {
|
|
7
|
+
rows: Row[];
|
|
8
|
+
excluded: {
|
|
9
|
+
approval: number;
|
|
10
|
+
work: number;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export declare function isStaleDone(r: Row): boolean;
|
|
14
|
+
export interface BlockedEntry {
|
|
15
|
+
tid: string;
|
|
16
|
+
name: string;
|
|
17
|
+
priority: number;
|
|
18
|
+
blockers: {
|
|
19
|
+
tid: string;
|
|
20
|
+
name?: string;
|
|
21
|
+
status?: string;
|
|
22
|
+
}[];
|
|
23
|
+
outside: number;
|
|
24
|
+
staleStatus: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface Move {
|
|
27
|
+
tid: string;
|
|
28
|
+
name: string;
|
|
29
|
+
action: string;
|
|
30
|
+
why: string;
|
|
31
|
+
}
|
|
32
|
+
export interface Digest {
|
|
33
|
+
kind: Kind;
|
|
34
|
+
rows: number;
|
|
35
|
+
excluded: {
|
|
36
|
+
approval: number;
|
|
37
|
+
work: number;
|
|
38
|
+
};
|
|
39
|
+
byStatus: Record<string, number>;
|
|
40
|
+
ready: Row[];
|
|
41
|
+
blocked: BlockedEntry[];
|
|
42
|
+
overdue: Row[];
|
|
43
|
+
unassigned: Row[];
|
|
44
|
+
orphans: Row[];
|
|
45
|
+
noNotes: Row[] | null;
|
|
46
|
+
staleDone: Row[];
|
|
47
|
+
picked: Row[];
|
|
48
|
+
nextMoves: Move[];
|
|
49
|
+
}
|
|
50
|
+
export declare function digest(rows: Row[], opts?: {
|
|
51
|
+
kind?: Kind;
|
|
52
|
+
}): Digest;
|
|
53
|
+
export interface BoardMeta {
|
|
54
|
+
total?: number;
|
|
55
|
+
asOf?: string;
|
|
56
|
+
pages?: number;
|
|
57
|
+
truncated?: {
|
|
58
|
+
rows?: number;
|
|
59
|
+
edges?: boolean;
|
|
60
|
+
tags?: number;
|
|
61
|
+
};
|
|
62
|
+
nextCursor?: string;
|
|
63
|
+
}
|
|
64
|
+
export declare function renderDigest(d: Digest, meta: BoardMeta, opts?: {
|
|
65
|
+
top?: number;
|
|
66
|
+
}): string;
|