foldrun 26.7.0 → 26.7.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 +2 -2
- package/src/credentials.mjs +201 -47
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "foldrun",
|
|
3
|
-
"version": "26.7.
|
|
3
|
+
"version": "26.7.1",
|
|
4
4
|
"description": "Agents are just folders. Write them, check them, run them — on your machine.",
|
|
5
5
|
"license": "FSL-1.1-ALv2",
|
|
6
6
|
"type": "module",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
],
|
|
23
23
|
"scripts": {
|
|
24
24
|
"e2e": "FOLDRUN_E2E=1 FOLDRUN_DATA=$(mktemp -d /tmp/foldrun-e2e-XXXXXX) node --test --test-timeout=300000 tests/e2e.test.ts",
|
|
25
|
-
"test": "FOLDRUN_DATA=$(mktemp -d /tmp/foldrun-test-XXXXXX) node --test tests/cli-account.test.ts tests/model-credential.test.ts tests/cli-approvals.test.ts tests/cli-issues.test.ts tests/cli-capture.test.ts tests/cli-stop.test.ts tests/cli-account-settings.test.ts tests/cli-team-me.test.ts tests/cli-schedule.test.ts tests/cli-billing.test.ts tests/cli-storage.test.ts tests/cli-scaffold.test.ts tests/cli-secrets.test.ts tests/cli-site-login.test.ts tests/cli-check-library.test.ts tests/cli-check-timezone.test.ts tests/cli-check-frontmatter.test.ts tests/cli-agent-import.test.ts tests/cli-check-subagents.test.ts tests/cli-connect.test.ts tests/cli-accounts.test.ts tests/cli-workspaces-use.test.ts tests/cli-remote.test.ts tests/cli-stream.test.ts tests/cli-doctor.test.ts tests/cli-tool-test.test.ts tests/cli-agent-run.test.ts tests/cli-check-remote.test.ts tests/cli-shares.test.ts tests/cli-triggers.test.ts tests/cli-providers.test.ts tests/cli-rerun-put.test.ts tests/cli-guide.test.ts tests/cli-runtimes.test.ts tests/cli-deploy-safety.test.ts tests/cli-operator.test.ts tests/cli-flow-patterns.test.ts tests/cli-flow-rm-step.test.ts tests/cli-flow-dup-step.test.ts tests/cli-flow-write-stale.test.ts tests/cli-agent-link-stale.test.ts tests/cli-agent-link-isolated.test.ts tests/cli-parity.test.ts tests/cli-invoke-inputs.test.ts tests/cli-audit-keys.test.ts tests/cli-version.test.ts tests/cli-check-web-warnings.test.ts tests/cli-report-image.test.ts tests/cli-report-cache.test.ts tests/cli-check-workspace-paths.test.ts tests/cli-parity-dashboard.test.ts tests/cli-api-standards.test.ts tests/cli-backups-onboarding.test.ts tests/cli-check-limits.test.ts tests/cli-gallery.test.ts tests/cli-status-platform.test.ts tests/cli-webhooks-notifications.test.ts tests/cli-flags.test.ts tests/cli-api-coverage.test.ts tests/cli-hooks.test.ts tests/cli-release.test.ts tests/cli-terminal.test.ts tests/cli-network.test.ts tests/cli-assistant.test.ts tests/cli-check-scan.test.ts tests/cli-published.test.ts tests/cli-init-target.test.ts tests/cli-flow-filter.test.ts tests/cli-eval-grades.test.ts tests/cli-eval-compare.test.ts tests/cli-proposals.test.ts tests/cli-inbox.test.ts tests/cli-usage-compute.test.ts tests/cli-usage-meters.test.ts tests/cli-fol105.test.ts tests/cli-key-renew.test.ts tests/cli-wait-failed.test.ts tests/cli-mcp.test.ts tests/cli-a2a.test.ts tests/cli-gateway.test.ts tests/cli-switches.test.ts tests/cli-parity-runs.test.ts tests/cli-parity-b.test.ts tests/cli-parity-clears.test.ts tests/cli-parity-c.test.ts tests/cli-runs-paging.test.ts tests/cli-budget-invoices.test.ts tests/cli-observe-analysis.test.ts tests/cli-failure-retention.test.ts tests/cli-snapshot.test.ts tests/cli-code.test.ts tests/cli-run-money.test.ts tests/cli-talk.test.ts tests/cli-text.test.ts tests/cli-browser.test.ts tests/cli-read-concurrency.test.ts",
|
|
25
|
+
"test": "FOLDRUN_DATA=$(mktemp -d /tmp/foldrun-test-XXXXXX) node --test tests/cli-account.test.ts tests/model-credential.test.ts tests/cli-approvals.test.ts tests/cli-issues.test.ts tests/cli-capture.test.ts tests/cli-stop.test.ts tests/cli-account-settings.test.ts tests/cli-team-me.test.ts tests/cli-schedule.test.ts tests/cli-billing.test.ts tests/cli-storage.test.ts tests/cli-scaffold.test.ts tests/cli-secrets.test.ts tests/cli-site-login.test.ts tests/cli-check-library.test.ts tests/cli-check-timezone.test.ts tests/cli-check-frontmatter.test.ts tests/cli-agent-import.test.ts tests/cli-check-subagents.test.ts tests/cli-connect.test.ts tests/cli-accounts.test.ts tests/cli-workspaces-use.test.ts tests/cli-credentials-lock.test.ts tests/cli-remote.test.ts tests/cli-stream.test.ts tests/cli-doctor.test.ts tests/cli-tool-test.test.ts tests/cli-agent-run.test.ts tests/cli-check-remote.test.ts tests/cli-shares.test.ts tests/cli-triggers.test.ts tests/cli-providers.test.ts tests/cli-rerun-put.test.ts tests/cli-guide.test.ts tests/cli-runtimes.test.ts tests/cli-deploy-safety.test.ts tests/cli-operator.test.ts tests/cli-flow-patterns.test.ts tests/cli-flow-rm-step.test.ts tests/cli-flow-dup-step.test.ts tests/cli-flow-write-stale.test.ts tests/cli-agent-link-stale.test.ts tests/cli-agent-link-isolated.test.ts tests/cli-parity.test.ts tests/cli-invoke-inputs.test.ts tests/cli-audit-keys.test.ts tests/cli-version.test.ts tests/cli-check-web-warnings.test.ts tests/cli-report-image.test.ts tests/cli-report-cache.test.ts tests/cli-check-workspace-paths.test.ts tests/cli-parity-dashboard.test.ts tests/cli-api-standards.test.ts tests/cli-backups-onboarding.test.ts tests/cli-check-limits.test.ts tests/cli-gallery.test.ts tests/cli-status-platform.test.ts tests/cli-webhooks-notifications.test.ts tests/cli-flags.test.ts tests/cli-api-coverage.test.ts tests/cli-hooks.test.ts tests/cli-release.test.ts tests/cli-terminal.test.ts tests/cli-network.test.ts tests/cli-assistant.test.ts tests/cli-check-scan.test.ts tests/cli-published.test.ts tests/cli-init-target.test.ts tests/cli-flow-filter.test.ts tests/cli-eval-grades.test.ts tests/cli-eval-compare.test.ts tests/cli-proposals.test.ts tests/cli-inbox.test.ts tests/cli-usage-compute.test.ts tests/cli-usage-meters.test.ts tests/cli-fol105.test.ts tests/cli-key-renew.test.ts tests/cli-wait-failed.test.ts tests/cli-mcp.test.ts tests/cli-a2a.test.ts tests/cli-gateway.test.ts tests/cli-switches.test.ts tests/cli-parity-runs.test.ts tests/cli-parity-b.test.ts tests/cli-parity-clears.test.ts tests/cli-parity-c.test.ts tests/cli-runs-paging.test.ts tests/cli-budget-invoices.test.ts tests/cli-observe-analysis.test.ts tests/cli-failure-retention.test.ts tests/cli-snapshot.test.ts tests/cli-code.test.ts tests/cli-run-money.test.ts tests/cli-talk.test.ts tests/cli-text.test.ts tests/cli-browser.test.ts tests/cli-read-concurrency.test.ts",
|
|
26
26
|
"release": "node scripts/release.mjs",
|
|
27
27
|
"typecheck": "tsc -p tsconfig.json",
|
|
28
28
|
"docs:sync": "node scripts/sync-docs.mjs",
|
package/src/credentials.mjs
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
// CI job with a key in the environment never reads a file.
|
|
23
23
|
|
|
24
24
|
import fs from "node:fs";
|
|
25
|
+
import crypto from "node:crypto";
|
|
25
26
|
import os from "node:os";
|
|
26
27
|
import path from "node:path";
|
|
27
28
|
|
|
@@ -79,6 +80,151 @@ function nameFor(profiles, account, url) {
|
|
|
79
80
|
for (let i = 2; ; i++) if (!profiles[`${withHost}-${i}`]) return `${withHost}-${i}`;
|
|
80
81
|
}
|
|
81
82
|
|
|
83
|
+
/** One process at a time changes the file: each change reads it and writes
|
|
84
|
+
* it whole, so two at once (a renewal beside `workspaces use`, two logins)
|
|
85
|
+
* could each undo the other (Codex review, 19 Nov 2026). The lock is a file
|
|
86
|
+
* linked into place beside it already holding its owner's pid and a token.
|
|
87
|
+
* It is stale only when that process is gone (an empty one, left by an
|
|
88
|
+
* older CLI, after LOCK_EMPTY_STALE_MS); see takeOver for how a
|
|
89
|
+
* stale one changes hands. Released only by the owner whose token it holds. Synchronous, like the
|
|
90
|
+
* rest of this module. */
|
|
91
|
+
const LOCK_EMPTY_STALE_MS = 30_000;
|
|
92
|
+
const LOCK_WAIT_MS = 10_000;
|
|
93
|
+
const pause = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
94
|
+
const readLock = (p) => {
|
|
95
|
+
try {
|
|
96
|
+
return fs.readFileSync(p, "utf8");
|
|
97
|
+
} catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
function ownerGone(content, lockPath) {
|
|
102
|
+
const pid = Number(/^(\d+):/.exec(content ?? "")?.[1]);
|
|
103
|
+
if (!pid) {
|
|
104
|
+
try {
|
|
105
|
+
return Date.now() - fs.statSync(lockPath).mtimeMs > LOCK_EMPTY_STALE_MS;
|
|
106
|
+
} catch {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (pid === process.pid) return false;
|
|
111
|
+
try {
|
|
112
|
+
process.kill(pid, 0);
|
|
113
|
+
return false;
|
|
114
|
+
} catch (err) {
|
|
115
|
+
return err?.code === "ESRCH";
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function locked(fn) {
|
|
119
|
+
fs.mkdirSync(credentialsDir(), { recursive: true, mode: 0o700 });
|
|
120
|
+
const lock = `${file()}.lock`;
|
|
121
|
+
const token = `${process.pid}:${crypto.randomBytes(8).toString("hex")}`;
|
|
122
|
+
const until = Date.now() + LOCK_WAIT_MS;
|
|
123
|
+
sweepClaims(lock);
|
|
124
|
+
// The lock is written whole first, then hard-linked into place (atomic,
|
|
125
|
+
// EEXIST when held): never an empty lock to be taken for a crashed one
|
|
126
|
+
// while its maker is merely slow (Codex review).
|
|
127
|
+
const mine = `${lock}.${token.replace(":", "-")}.own`;
|
|
128
|
+
fs.writeFileSync(mine, token, { mode: 0o600 });
|
|
129
|
+
try {
|
|
130
|
+
for (;;) {
|
|
131
|
+
try {
|
|
132
|
+
fs.linkSync(mine, lock);
|
|
133
|
+
break;
|
|
134
|
+
} catch (err) {
|
|
135
|
+
if (err?.code !== "EEXIST") throw err;
|
|
136
|
+
}
|
|
137
|
+
if (takeOver(lock, token)) break;
|
|
138
|
+
if (Date.now() > until) throw new Error(`${lock} is held by another foldrun command — try again, or remove it if no foldrun is running`);
|
|
139
|
+
pause(25);
|
|
140
|
+
}
|
|
141
|
+
} finally {
|
|
142
|
+
fs.rmSync(mine, { force: true });
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
return fn();
|
|
146
|
+
} finally {
|
|
147
|
+
if (readLock(lock) === token) fs.rmSync(lock, { force: true });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Take over a lock whose owner is gone, without the path ever standing
|
|
152
|
+
* empty (Codex review): one claim file per dead lock (keyed by its content,
|
|
153
|
+
* inode and time), made with O_EXCL and holding the claimant's pid, so
|
|
154
|
+
* exactly one contender wins it; the winner checks the lock is still that
|
|
155
|
+
* dead one and renames its own lock file over it. The claim stays while its
|
|
156
|
+
* claimant lives, so a late contender that saw the same dead lock cannot win
|
|
157
|
+
* it again; a claim whose claimant is gone (crashed mid-takeover) is cleared.
|
|
158
|
+
* Limit, accepted: without kernel file locks (Node has none) a claimant
|
|
159
|
+
* paused between its check and its rename while its claim is cleared as dead
|
|
160
|
+
* could still overlap — it needs a crash during another crash's recovery. */
|
|
161
|
+
function takeOver(lock, token) {
|
|
162
|
+
let st;
|
|
163
|
+
const seen = readLock(lock);
|
|
164
|
+
try {
|
|
165
|
+
st = fs.statSync(lock);
|
|
166
|
+
} catch {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
if (seen === null || !ownerGone(seen, lock)) return false;
|
|
170
|
+
const key = crypto.createHash("sha256").update(`${seen}\0${st.ino}\0${st.mtimeMs}`).digest("hex").slice(0, 16);
|
|
171
|
+
const claim = `${lock}.claim-${key}`;
|
|
172
|
+
let fd;
|
|
173
|
+
try {
|
|
174
|
+
fd = fs.openSync(claim, "wx", 0o600);
|
|
175
|
+
} catch {
|
|
176
|
+
// Another contender has this dead lock — unless it died too.
|
|
177
|
+
const held = readLock(claim);
|
|
178
|
+
if (held !== null && ownerGone(held, claim)) fs.rmSync(claim, { force: true });
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
fs.writeSync(fd, `${process.pid}:claim`);
|
|
183
|
+
} finally {
|
|
184
|
+
fs.closeSync(fd);
|
|
185
|
+
}
|
|
186
|
+
let stillDead = false;
|
|
187
|
+
try {
|
|
188
|
+
stillDead = readLock(lock) === seen && fs.statSync(lock).ino === st.ino;
|
|
189
|
+
} catch {
|
|
190
|
+
/* gone: someone released it */
|
|
191
|
+
}
|
|
192
|
+
if (!stillDead) {
|
|
193
|
+
fs.rmSync(claim, { force: true });
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
const tmp = `${lock}.${token.replace(":", "-")}.tmp`;
|
|
197
|
+
try {
|
|
198
|
+
fs.writeFileSync(tmp, token, { mode: 0o600 });
|
|
199
|
+
fs.renameSync(tmp, lock);
|
|
200
|
+
return true;
|
|
201
|
+
} catch {
|
|
202
|
+
fs.rmSync(tmp, { force: true });
|
|
203
|
+
fs.rmSync(claim, { force: true });
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Claims whose claimant is gone go: nobody is taking that lock over. */
|
|
209
|
+
function sweepClaims(lock) {
|
|
210
|
+
const dir = path.dirname(lock);
|
|
211
|
+
const base = `${path.basename(lock)}.claim-`;
|
|
212
|
+
try {
|
|
213
|
+
for (const n of fs.readdirSync(dir)) {
|
|
214
|
+
if (!n.startsWith(base)) continue;
|
|
215
|
+
const p = path.join(dir, n);
|
|
216
|
+
try {
|
|
217
|
+
const held = readLock(p);
|
|
218
|
+
if (held !== null && ownerGone(held, p)) fs.rmSync(p, { force: true });
|
|
219
|
+
} catch {
|
|
220
|
+
/* gone meanwhile */
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
} catch {
|
|
224
|
+
/* an unreadable folder: the open below says so */
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
82
228
|
function write(current, profiles) {
|
|
83
229
|
// The old shape goes on being written for the newest profile on each URL,
|
|
84
230
|
// so a machine that moves between CLI versions is not signed out by the
|
|
@@ -120,42 +266,46 @@ function write(current, profiles) {
|
|
|
120
266
|
* @param {{ makeDefault?: boolean, name?: string }} [opts]
|
|
121
267
|
*/
|
|
122
268
|
export function saveCredential(url, entry, { makeDefault = true, name } = {}) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
269
|
+
return locked(() => {
|
|
270
|
+
const key = normaliseUrl(url);
|
|
271
|
+
const { current, profiles } = readCredentials();
|
|
272
|
+
const existing = name ?? Object.keys(profiles).find((n) => normaliseUrl(profiles[n].url) === key && profiles[n].account === entry.account);
|
|
273
|
+
const profileName = existing ?? nameFor(profiles, entry.account, key);
|
|
274
|
+
// The default project (`foldrun workspaces use`) is what is stored NOW for
|
|
275
|
+
// this account on this platform, never what the entry carries: a renewal
|
|
276
|
+
// writes back a profile read before a `use` or `--clear` changed it, and a
|
|
277
|
+
// re-login to the same account keeps it (Codex review, 19 Nov 2026).
|
|
278
|
+
const prev = profiles[profileName];
|
|
279
|
+
const kept = prev && normaliseUrl(prev.url) === key && prev.account === entry.account && typeof prev.workspace === "string" ? { workspace: prev.workspace } : {};
|
|
280
|
+
const { workspace: _carried, ...fields } = entry;
|
|
281
|
+
profiles[profileName] = { ...fields, ...kept, url: key, loggedInAt: new Date().toISOString() };
|
|
282
|
+
write(makeDefault || !current ? profileName : current, profiles);
|
|
283
|
+
return profileName;
|
|
284
|
+
});
|
|
137
285
|
}
|
|
138
286
|
|
|
139
287
|
/** Forget one. By name, else by URL (the newest there). Returns what was
|
|
140
288
|
* stored, for a best-effort revoke. */
|
|
141
289
|
export function removeCredential(urlOrName) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
290
|
+
return locked(() => {
|
|
291
|
+
const { current, profiles } = readCredentials();
|
|
292
|
+
const key = (() => {
|
|
293
|
+
if (profiles[urlOrName]) return urlOrName;
|
|
294
|
+
try {
|
|
295
|
+
const u = normaliseUrl(urlOrName);
|
|
296
|
+
const here = Object.keys(profiles).filter((n) => normaliseUrl(profiles[n].url) === u);
|
|
297
|
+
return here.sort((a, b) => String(profiles[a].loggedInAt ?? "").localeCompare(String(profiles[b].loggedInAt ?? ""))).at(-1) ?? null;
|
|
298
|
+
} catch {
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
})();
|
|
302
|
+
if (!key) return null;
|
|
303
|
+
const gone = profiles[key];
|
|
304
|
+
delete profiles[key];
|
|
305
|
+
const names = Object.keys(profiles);
|
|
306
|
+
write(current === key ? names[0] ?? null : current, profiles);
|
|
307
|
+
return gone;
|
|
308
|
+
});
|
|
159
309
|
}
|
|
160
310
|
|
|
161
311
|
/** The stored entry for a URL — the newest account signed in there. */
|
|
@@ -186,28 +336,32 @@ export function currentProfile() {
|
|
|
186
336
|
|
|
187
337
|
/** Make one the profile a bare command talks as. */
|
|
188
338
|
export function useProfile(name) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
339
|
+
return locked(() => {
|
|
340
|
+
const { profiles } = readCredentials();
|
|
341
|
+
if (!profiles[name]) return null;
|
|
342
|
+
write(name, profiles);
|
|
343
|
+
return { ...profiles[name], name };
|
|
344
|
+
});
|
|
193
345
|
}
|
|
194
346
|
|
|
195
347
|
/** Save (or, with null, forget) the project a profile's commands use when
|
|
196
348
|
* they name none — `foldrun workspaces use`. Kept on the profile, so it is
|
|
197
349
|
* one account on one platform's, and never follows a --token. */
|
|
198
350
|
export function setProfileWorkspace(name, workspace, expect = {}) {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
351
|
+
return locked(() => {
|
|
352
|
+
const { current, profiles } = readCredentials();
|
|
353
|
+
if (!profiles[name]) return null;
|
|
354
|
+
// Still the account and platform it was checked against: a sign-out or a
|
|
355
|
+
// new sign-in under the same name meanwhile saves nothing.
|
|
356
|
+
if (expect.url && normaliseUrl(profiles[name].url) !== normaliseUrl(expect.url)) return null;
|
|
357
|
+
if (expect.account && profiles[name].account !== expect.account) return null;
|
|
358
|
+
const next = { ...profiles[name] };
|
|
359
|
+
if (workspace) next.workspace = workspace;
|
|
360
|
+
else delete next.workspace;
|
|
361
|
+
profiles[name] = next;
|
|
362
|
+
write(current, profiles);
|
|
363
|
+
return { ...next, name };
|
|
364
|
+
});
|
|
211
365
|
}
|
|
212
366
|
|
|
213
367
|
/** All of them, newest sign-in last. */
|