@phren/cli 0.3.65 → 0.3.67
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/dist/bridge/account-choice.d.ts +12 -3
- package/dist/bridge/account-choice.js +133 -19
- package/dist/bridge/account-last-known.d.ts +27 -0
- package/dist/bridge/account-last-known.js +124 -0
- package/dist/bridge/changes.d.ts +22 -0
- package/dist/bridge/changes.js +323 -82
- package/dist/bridge/claude-accounts.d.ts +27 -0
- package/dist/bridge/claude-accounts.js +63 -8
- package/dist/bridge/dispatch-returns.d.ts +8 -0
- package/dist/bridge/dispatch-returns.js +20 -2
- package/dist/bridge/files.d.ts +1 -1
- package/dist/bridge/harnesses.js +8 -2
- package/dist/bridge/helper.d.ts +3 -2
- package/dist/bridge/helper.js +5 -4
- package/dist/bridge/machine-health-alerts.d.ts +20 -0
- package/dist/bridge/machine-health-alerts.js +47 -0
- package/dist/bridge/machine-health.d.ts +42 -0
- package/dist/bridge/machine-health.js +124 -0
- package/dist/bridge/machine-probes.d.ts +58 -0
- package/dist/bridge/machine-probes.js +306 -0
- package/dist/bridge/push.d.ts +10 -0
- package/dist/bridge/push.js +15 -0
- package/dist/bridge/resources.d.ts +5 -0
- package/dist/bridge/resources.js +16 -2
- package/dist/bridge/schedule-launch.js +2 -2
- package/dist/bridge/server-launch.js +3 -1
- package/dist/bridge/server.js +5 -1
- package/dist/bridge-hook.mjs +29235 -28083
- package/dist/computers/read.d.ts +18 -0
- package/dist/computers/read.js +18 -0
- package/dist/governance/policy.js +4 -3
- package/dist/governance/scores.js +2 -1
- package/dist/modules/registry.js +1 -1
- package/dist/tools/dispatch.js +16 -0
- package/dist/tools/helper.js +1 -1
- package/package.json +1 -1
- package/starter/global/skills/conductor/SKILL.md +6 -0
|
@@ -67,12 +67,21 @@ export declare function setChoiceReaders(readers: {
|
|
|
67
67
|
usage?: UsageReader;
|
|
68
68
|
inventory?: () => Promise<HarnessInventory | undefined>;
|
|
69
69
|
}): void;
|
|
70
|
+
/** Whose pools a launch keeps to: its project (by name, else the project whose checkout holds `cwd`). */
|
|
71
|
+
export interface PickScope {
|
|
72
|
+
project?: string;
|
|
73
|
+
cwd?: string;
|
|
74
|
+
phrenPath?: string;
|
|
75
|
+
}
|
|
70
76
|
/**
|
|
71
77
|
* The account a Claude launch that names none runs under on this computer, or
|
|
72
|
-
* undefined to launch as before (`default`)
|
|
73
|
-
* signed in here and their room can
|
|
78
|
+
* undefined to launch as before (`default`). With no pool, role or Console login
|
|
79
|
+
* in play: only when more than one account is signed in here and their room can
|
|
80
|
+
* be read. Otherwise as dispatch picks (account-policy.ts): the project's and this
|
|
81
|
+
* computer's pools, never `off`, `last-resort` last; 409 `account_pool` when no
|
|
82
|
+
* account fits. Logged with the reason.
|
|
74
83
|
*/
|
|
75
|
-
export declare function pickClaudeAccount(what: string, computer?: string, now?: number): Promise<AccountChoice | undefined>;
|
|
84
|
+
export declare function pickClaudeAccount(what: string, scope?: PickScope, computer?: string, now?: number): Promise<AccountChoice | undefined>;
|
|
76
85
|
/** Whether a dispatched Claude worker stopped at its usage limit continues on another signed-in account.
|
|
77
86
|
* Off unless the owner opts in with `phren config account-failover on` or PHREN_ACCOUNT_FAILOVER=on. */
|
|
78
87
|
export declare function resolveAccountFailover(env?: NodeJS.ProcessEnv, store?: string | null): {
|
|
@@ -9,9 +9,14 @@ import { z } from "zod";
|
|
|
9
9
|
import { readInstallPreferences } from "../init/preferences.js";
|
|
10
10
|
import { logger } from "../logger.js";
|
|
11
11
|
import { findPhrenPath } from "../phren-paths.js";
|
|
12
|
-
import { claudeHomes, DEFAULT_ACCOUNT } from "./claude-accounts.js";
|
|
12
|
+
import { claudeAuthStatus, claudeHomes, claudeLoginFingerprint, DEFAULT_ACCOUNT } from "./claude-accounts.js";
|
|
13
|
+
import { knownLogin, readLastKnown, rememberLogin, rememberPolicy } from "./account-last-known.js";
|
|
13
14
|
import { harnessInventoryWithin } from "./harnesses.js";
|
|
14
|
-
import { atomic, bridgeRoot } from "./protocol.js";
|
|
15
|
+
import { atomic, BridgeError, bridgeRoot } from "./protocol.js";
|
|
16
|
+
import { choosePolicyAccount, computerRule, policyApplies, projectRule, withPolicy } from "./account-policy.js";
|
|
17
|
+
import { accountPolicy } from "./account-pools.js";
|
|
18
|
+
import { phrenStoreRoot } from "./transcripts.js";
|
|
19
|
+
import { detectProject } from "../shared/index.js";
|
|
15
20
|
/** Claude's 5-hour session window and its 7-day window over all models. */
|
|
16
21
|
export const FIVE_HOUR = "five_hour", WEEK = "seven_day";
|
|
17
22
|
function windowRoom(window, now) {
|
|
@@ -126,33 +131,142 @@ async function within(ms, value) {
|
|
|
126
131
|
clearTimeout(timer);
|
|
127
132
|
}
|
|
128
133
|
}
|
|
134
|
+
function scopeRule(scope) {
|
|
135
|
+
const store = scope.phrenPath ?? phrenStoreRoot();
|
|
136
|
+
let project = scope.project;
|
|
137
|
+
if (!project && scope.cwd) {
|
|
138
|
+
try {
|
|
139
|
+
project = detectProject(store, scope.cwd) ?? undefined;
|
|
140
|
+
}
|
|
141
|
+
catch { /* No store here: no project rule. */ }
|
|
142
|
+
}
|
|
143
|
+
return project ? projectRule(store, project) : undefined;
|
|
144
|
+
}
|
|
145
|
+
const AUTH_BUDGET_MS = 2_500;
|
|
146
|
+
/** A home's login type: the probe's definitive answer, else the last one seen. Never waits past the budget. */
|
|
147
|
+
async function loginKind(home, last) {
|
|
148
|
+
// The fingerprint names the login this probe is about: a later one makes this answer stale.
|
|
149
|
+
const fingerprint = claudeLoginFingerprint(home);
|
|
150
|
+
const status = claudeAuthStatus(home);
|
|
151
|
+
// A definitive answer still records the classification when it lands after the budget.
|
|
152
|
+
void status.then(answer => {
|
|
153
|
+
if (answer.unknown !== true && answer.signedIn)
|
|
154
|
+
return rememberLogin(home, fingerprint, answer.console ? "console" : "subscription");
|
|
155
|
+
}).catch(() => undefined);
|
|
156
|
+
const answer = await within(AUTH_BUDGET_MS, status);
|
|
157
|
+
if (answer && answer.unknown !== true)
|
|
158
|
+
return answer.signedIn && answer.console ? "console" : "subscription";
|
|
159
|
+
return knownLogin(last, home);
|
|
160
|
+
}
|
|
129
161
|
/**
|
|
130
162
|
* The account a Claude launch that names none runs under on this computer, or
|
|
131
|
-
* undefined to launch as before (`default`)
|
|
132
|
-
* signed in here and their room can
|
|
163
|
+
* undefined to launch as before (`default`). With no pool, role or Console login
|
|
164
|
+
* in play: only when more than one account is signed in here and their room can
|
|
165
|
+
* be read. Otherwise as dispatch picks (account-policy.ts): the project's and this
|
|
166
|
+
* computer's pools, never `off`, `last-resort` last; 409 `account_pool` when no
|
|
167
|
+
* account fits. Logged with the reason.
|
|
133
168
|
*/
|
|
134
|
-
export async function pickClaudeAccount(what, computer = "this computer", now = Date.now()) {
|
|
169
|
+
export async function pickClaudeAccount(what, scope = {}, computer = "this computer", now = Date.now()) {
|
|
135
170
|
if (!usageReader)
|
|
136
171
|
return undefined;
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
//
|
|
140
|
-
|
|
141
|
-
|
|
172
|
+
const project = scopeRule(scope);
|
|
173
|
+
const last = await readLastKnown();
|
|
174
|
+
// This computer's accounts.yaml roles and pools, read here rather than only through the inventory: a slow or
|
|
175
|
+
// failed inventory read must not hide a restriction it would have applied. When the file can't be read, keep
|
|
176
|
+
// its last successfully parsed policy, but only when that policy restricted something.
|
|
177
|
+
const local = localPolicy();
|
|
178
|
+
let effective;
|
|
179
|
+
if (local.policy) {
|
|
180
|
+
effective = { settings: local.policy.settings, ...(local.policy.allowedPools ? { allowedPools: local.policy.allowedPools } : {}), restricted: restricts(local.policy) };
|
|
181
|
+
void rememberPolicy({ settings: local.policy.settings, ...(local.policy.allowedPools ? { allowedPools: local.policy.allowedPools } : {}) });
|
|
182
|
+
}
|
|
183
|
+
else if (last.policy && restricts(last.policy)) {
|
|
184
|
+
effective = { settings: last.policy.settings, ...(last.policy.allowedPools ? { allowedPools: last.policy.allowedPools } : {}), restricted: true };
|
|
185
|
+
logger.warn("accounts", "accounts.yaml can't be read; keeping its last-known Claude roles and pools.");
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
effective = { settings: {}, restricted: false };
|
|
189
|
+
logger.warn("accounts", "accounts.yaml can't be read; launching as before because no Claude role or pool was ever set.");
|
|
190
|
+
}
|
|
191
|
+
// One Claude home and no rule that could refuse it means nothing to choose. Checking the folders first keeps a
|
|
192
|
+
// launch from running `claude auth status` against the config it is about to pretrust: that process can write
|
|
193
|
+
// .claude.json back over the new trust entry. A Console (API-billed) login is the exception: it is never chosen
|
|
194
|
+
// automatically without a role, so the sole account's login is confirmed before taking the shortcut.
|
|
195
|
+
const homes = claudeHomes();
|
|
196
|
+
const knownKind = (account) => {
|
|
197
|
+
const home = homes.find(item => item.id === account);
|
|
198
|
+
return home ? knownLogin(last, home) : undefined;
|
|
199
|
+
};
|
|
200
|
+
if (!inventoryReader && homes.length < 2 && !project?.pools && !effective.restricted) {
|
|
201
|
+
if (await loginKind(homes[0], last) !== "console")
|
|
202
|
+
return undefined;
|
|
203
|
+
throw new BridgeError(409, `The only Claude account on ${computer} is an API (Console) login with no role, so it is not used automatically. Give it a role or name it by hand.`.slice(0, 500), { code: "account_pool", skipped: [`${homes[0].id}: an API account with no role (chosen only by hand)`] });
|
|
204
|
+
}
|
|
142
205
|
const inventory = await within(2_500, (inventoryReader ?? (() => harnessInventoryWithin(2_500)))());
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
206
|
+
const entry = inventory?.harnesses.find(item => item.source === "claude");
|
|
207
|
+
// A restriction is only enforceable against a listing of this computer's accounts. With none, refuse rather
|
|
208
|
+
// than launch under default, which may be exactly the account the restriction forbids.
|
|
209
|
+
if (effective.restricted && !entry?.accounts?.length) {
|
|
210
|
+
throw new BridgeError(409, `This computer's Claude account roles and pools${local.policy ? "" : " (its last-known ones)"} can't be evaluated because the harness inventory listed no accounts; refusing rather than launch under an account they may forbid.`.slice(0, 500), { code: "account_pool" });
|
|
211
|
+
}
|
|
212
|
+
const rules = [...(project ? [project] : []), computerRule(computer, effective.allowedPools)];
|
|
213
|
+
// An unanswered sign-in check leaves console off the listing: use the login last seen for that home, so a
|
|
214
|
+
// Console account is never chosen automatically just because this probe did not answer.
|
|
215
|
+
const policyRooms = (usage) => claudeRooms(inventory, usage, now).map(room => {
|
|
216
|
+
const listed = entry?.accounts?.find(item => item.id === room.account);
|
|
217
|
+
const unknown = Boolean(listed && !listed.signedIn && listed.usable);
|
|
218
|
+
const console = listed?.console === true || (unknown && knownKind(room.account) === "console");
|
|
219
|
+
return withPolicy(room, { ...listed, ...(console ? { console: true } : {}), ...effective.settings[room.account] });
|
|
220
|
+
});
|
|
221
|
+
// Falling back to `default` is also a choice: never launch a last-known Console login without a role.
|
|
222
|
+
const defaultListed = entry?.accounts?.find(item => item.id === DEFAULT_ACCOUNT);
|
|
223
|
+
const defaultUnknown = Boolean(defaultListed && !defaultListed.signedIn && defaultListed.usable);
|
|
224
|
+
const defaultConsole = defaultListed?.console === true || ((!defaultListed || defaultUnknown) && knownKind(DEFAULT_ACCOUNT) === "console");
|
|
225
|
+
const fallback = (choice) => {
|
|
226
|
+
if (choice)
|
|
227
|
+
return choice;
|
|
228
|
+
if (defaultConsole)
|
|
229
|
+
throw new BridgeError(409, `The default Claude account on ${computer} is an API (Console) login with no role, so it is not used automatically. Give it a role or name it by hand.`.slice(0, 500), { code: "account_pool", skipped: [`${DEFAULT_ACCOUNT}: an API account with no role (chosen only by hand)`] });
|
|
148
230
|
return undefined;
|
|
149
|
-
|
|
150
|
-
if (
|
|
151
|
-
|
|
152
|
-
|
|
231
|
+
};
|
|
232
|
+
if (!policyApplies(policyRooms([]), rules)) {
|
|
233
|
+
if (policyRooms([]).filter(room => room.usable !== false).length < 2)
|
|
234
|
+
return fallback();
|
|
235
|
+
const usage = await within(2_500, usageReader());
|
|
236
|
+
if (!usage)
|
|
237
|
+
return fallback();
|
|
238
|
+
const { choice, skipped } = chooseAccount(claudeRooms(inventory, usage, now), limitedBy(await readAccountLimits(now), computer));
|
|
239
|
+
if (choice) {
|
|
240
|
+
logger.info("accounts", `${what}: Claude account ${choice.account}, ${choice.reason}.`);
|
|
241
|
+
return choice;
|
|
242
|
+
}
|
|
243
|
+
if (defaultConsole)
|
|
244
|
+
return fallback();
|
|
153
245
|
logger.info("accounts", `${what}: no Claude account has room (${skipped.join("; ")}); launching under default.`);
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
// Unknown room ranks last, so a slow usage read still leaves the policy to choose.
|
|
249
|
+
const usage = await within(2_500, usageReader()) ?? [];
|
|
250
|
+
const { choice, skipped } = choosePolicyAccount(policyRooms(usage), rules, limitedBy(await readAccountLimits(now), computer));
|
|
251
|
+
if (!choice) {
|
|
252
|
+
throw new BridgeError(409, `No Claude account on ${computer} may do this work${skipped.length ? `: ${skipped.join("; ")}` : ""}.`.slice(0, 500), { code: "account_pool", skipped });
|
|
253
|
+
}
|
|
254
|
+
logger.info("accounts", `${what}: Claude account ${choice.account}, ${choice.reason}.`);
|
|
154
255
|
return choice;
|
|
155
256
|
}
|
|
257
|
+
/** This computer's accounts.yaml policy, or that it can't be read. */
|
|
258
|
+
function localPolicy() {
|
|
259
|
+
try {
|
|
260
|
+
return { policy: accountPolicy(), unreadable: false };
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
return { unreadable: true };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/** Whether a policy sets a pool, role or allowed pools. */
|
|
267
|
+
function restricts(policy) {
|
|
268
|
+
return Boolean(policy?.allowedPools) || Object.values(policy?.settings ?? {}).some(item => item.pool || item.role);
|
|
269
|
+
}
|
|
156
270
|
// ── The switch ───────────────────────────────────────────────────────────────
|
|
157
271
|
const TRUE = new Set(["1", "true", "on", "yes"]), FALSE = new Set(["0", "false", "off", "no"]);
|
|
158
272
|
/** Whether a dispatched Claude worker stopped at its usage limit continues on another signed-in account.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type ClaudeHome } from "./claude-accounts.js";
|
|
2
|
+
import { type AccountRole } from "./account-pools.js";
|
|
3
|
+
export type LoginKind = "console" | "subscription";
|
|
4
|
+
export interface KnownLogin {
|
|
5
|
+
kind: LoginKind;
|
|
6
|
+
fingerprint: string;
|
|
7
|
+
}
|
|
8
|
+
export interface KnownSettings {
|
|
9
|
+
pool?: string;
|
|
10
|
+
role?: AccountRole;
|
|
11
|
+
}
|
|
12
|
+
export interface KnownPolicy {
|
|
13
|
+
settings: Record<string, KnownSettings>;
|
|
14
|
+
allowedPools?: string[];
|
|
15
|
+
}
|
|
16
|
+
export interface LastKnown {
|
|
17
|
+
logins?: Record<string, KnownLogin>;
|
|
18
|
+
policy?: KnownPolicy;
|
|
19
|
+
}
|
|
20
|
+
/** The last-known logins and policy, or empty when there is no readable file. */
|
|
21
|
+
export declare function readLastKnown(): Promise<LastKnown>;
|
|
22
|
+
/** A home's last-known login type: only when it belongs to the current login, or the config names none. */
|
|
23
|
+
export declare function knownLogin(last: LastKnown, home: ClaudeHome): LoginKind | undefined;
|
|
24
|
+
/** Remembers a home's login type once Claude answered for the fingerprint seen before the probe. */
|
|
25
|
+
export declare function rememberLogin(home: ClaudeHome, seenFingerprint: string | undefined, kind: LoginKind): Promise<void>;
|
|
26
|
+
/** Remembers the last accounts.yaml policy that parsed. */
|
|
27
|
+
export declare function rememberPolicy(policy: KnownPolicy): Promise<void>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// What a launch last knew about this computer's Claude accounts: each home's login type (Console or
|
|
2
|
+
// subscription), bound to a privacy-preserving login fingerprint, and the last accounts.yaml policy
|
|
3
|
+
// that parsed. Kept in the bridge dir so a slow probe or an unreadable file does not force a launch
|
|
4
|
+
// back to default. See docs/accounts.md.
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { claudeLoginFingerprint } from "./claude-accounts.js";
|
|
9
|
+
import { withFileLock } from "../governance/locks.js";
|
|
10
|
+
import { isAccountRole, isPoolTag } from "./account-pools.js";
|
|
11
|
+
import { bridgeRoot } from "./protocol.js";
|
|
12
|
+
const SLUG = /^[a-z0-9][a-z0-9-]{0,31}$/;
|
|
13
|
+
const FINGERPRINT = /^[a-f0-9]{64}$/;
|
|
14
|
+
const MAX_POOLS = 32;
|
|
15
|
+
const file = () => path.join(bridgeRoot(), "account-last-known.json");
|
|
16
|
+
function loginsOf(raw) {
|
|
17
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
18
|
+
return undefined;
|
|
19
|
+
const out = {};
|
|
20
|
+
for (const [id, value] of Object.entries(raw)) {
|
|
21
|
+
if (!SLUG.test(id) || !value || typeof value !== "object" || Array.isArray(value))
|
|
22
|
+
continue;
|
|
23
|
+
const { kind, fingerprint } = value;
|
|
24
|
+
if ((kind === "console" || kind === "subscription") && typeof fingerprint === "string" && FINGERPRINT.test(fingerprint)) {
|
|
25
|
+
out[id] = { kind, fingerprint };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return Object.keys(out).length ? out : undefined;
|
|
29
|
+
}
|
|
30
|
+
function policyOf(raw) {
|
|
31
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
32
|
+
return undefined;
|
|
33
|
+
const value = raw;
|
|
34
|
+
const rawSettings = value.settings && typeof value.settings === "object" && !Array.isArray(value.settings)
|
|
35
|
+
? value.settings : {};
|
|
36
|
+
const settings = {};
|
|
37
|
+
for (const [id, item] of Object.entries(rawSettings)) {
|
|
38
|
+
if (!SLUG.test(id) || !item || typeof item !== "object" || Array.isArray(item))
|
|
39
|
+
continue;
|
|
40
|
+
const { pool, role } = item;
|
|
41
|
+
const next = { ...(isPoolTag(pool) ? { pool } : {}), ...(isAccountRole(role) ? { role } : {}) };
|
|
42
|
+
if (Object.keys(next).length)
|
|
43
|
+
settings[id] = next;
|
|
44
|
+
}
|
|
45
|
+
const allowedPools = Array.isArray(value.allowedPools)
|
|
46
|
+
? [...new Set(value.allowedPools.filter(isPoolTag))].slice(0, MAX_POOLS) : undefined;
|
|
47
|
+
return { settings, ...(allowedPools ? { allowedPools } : {}) };
|
|
48
|
+
}
|
|
49
|
+
function parse(raw) {
|
|
50
|
+
const value = raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
51
|
+
const known = {};
|
|
52
|
+
const logins = loginsOf(value.logins);
|
|
53
|
+
if (logins)
|
|
54
|
+
known.logins = logins;
|
|
55
|
+
const policy = policyOf(value.policy);
|
|
56
|
+
if (policy)
|
|
57
|
+
known.policy = policy;
|
|
58
|
+
return known;
|
|
59
|
+
}
|
|
60
|
+
function readSync() {
|
|
61
|
+
try {
|
|
62
|
+
return parse(JSON.parse(readFileSync(file(), "utf8")));
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** The last-known logins and policy, or empty when there is no readable file. */
|
|
69
|
+
export async function readLastKnown() { return readSync(); }
|
|
70
|
+
/** A home's last-known login type: only when it belongs to the current login, or the config names none. */
|
|
71
|
+
export function knownLogin(last, home) {
|
|
72
|
+
const entry = last.logins?.[home.id];
|
|
73
|
+
if (!entry)
|
|
74
|
+
return undefined;
|
|
75
|
+
const current = claudeLoginFingerprint(home);
|
|
76
|
+
// No identity to compare against: keep the previous classification rather than discarding it.
|
|
77
|
+
return current === undefined || entry.fingerprint === current ? entry.kind : undefined;
|
|
78
|
+
}
|
|
79
|
+
function writeSync(value) {
|
|
80
|
+
const target = file();
|
|
81
|
+
mkdirSync(path.dirname(target), { recursive: true, mode: 0o700 });
|
|
82
|
+
const temporary = `${target}.${randomUUID()}.tmp`;
|
|
83
|
+
try {
|
|
84
|
+
writeFileSync(temporary, JSON.stringify(value), { mode: 0o600, flag: "wx" });
|
|
85
|
+
renameSync(temporary, target);
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
rmSync(temporary, { force: true });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
let writes = Promise.resolve();
|
|
92
|
+
/** Read, change and write under the file's shared lock, one change at a time; a failure never rejects. */
|
|
93
|
+
function update(change) {
|
|
94
|
+
const run = writes.then(() => {
|
|
95
|
+
try {
|
|
96
|
+
withFileLock(file(), () => {
|
|
97
|
+
const value = readSync();
|
|
98
|
+
const before = JSON.stringify(value);
|
|
99
|
+
change(value);
|
|
100
|
+
if (JSON.stringify(value) !== before)
|
|
101
|
+
writeSync(value);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
catch { /* Best effort: a lost record only costs one more background probe. */ }
|
|
105
|
+
});
|
|
106
|
+
writes = run;
|
|
107
|
+
return run;
|
|
108
|
+
}
|
|
109
|
+
/** Remembers a home's login type once Claude answered for the fingerprint seen before the probe. */
|
|
110
|
+
export function rememberLogin(home, seenFingerprint, kind) {
|
|
111
|
+
// No identity to bind the answer to: keep the previous classification instead of storing an unbound one.
|
|
112
|
+
if (!SLUG.test(home.id) || !seenFingerprint)
|
|
113
|
+
return Promise.resolve();
|
|
114
|
+
return update(value => {
|
|
115
|
+
// A later login already changed the home: this older answer is not about it.
|
|
116
|
+
if (claudeLoginFingerprint(home) !== seenFingerprint)
|
|
117
|
+
return;
|
|
118
|
+
value.logins = { ...value.logins, [home.id]: { kind, fingerprint: seenFingerprint } };
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/** Remembers the last accounts.yaml policy that parsed. */
|
|
122
|
+
export function rememberPolicy(policy) {
|
|
123
|
+
return update(value => { value.policy = policy; });
|
|
124
|
+
}
|
package/dist/bridge/changes.d.ts
CHANGED
|
@@ -36,6 +36,17 @@ export declare class ToolChanges {
|
|
|
36
36
|
private controllers;
|
|
37
37
|
private claims;
|
|
38
38
|
private failures;
|
|
39
|
+
/** Conversation and repository root pairs already shown that Git could not capture changes. */
|
|
40
|
+
private reported;
|
|
41
|
+
private repositories;
|
|
42
|
+
private scratches;
|
|
43
|
+
private closing;
|
|
44
|
+
/** The failure to show on this call's row, if any. A capture that ran out of
|
|
45
|
+
* time shows no changes rather than an error: a busy machine is not
|
|
46
|
+
* something to act on per call, and repository Changes still has the
|
|
47
|
+
* status. A broken capture (Git missing, a corrupt repository) shows on the
|
|
48
|
+
* first call it hits in a conversation, not on every later one. A per-call
|
|
49
|
+
* limit (too many files) shows on its own call. */
|
|
39
50
|
private failed;
|
|
40
51
|
/** A hook callback's wall-clock cap on Git work; past it the call records
|
|
41
52
|
* no change. Tests about what is captured raise it, since a loaded Windows
|
|
@@ -48,6 +59,17 @@ export declare class ToolChanges {
|
|
|
48
59
|
private budget;
|
|
49
60
|
private discard;
|
|
50
61
|
close(): Promise<void>;
|
|
62
|
+
/** The repository holding `target` (a file, a folder, or something not yet
|
|
63
|
+
* created), confined to the user's home; undefined otherwise. Git is asked
|
|
64
|
+
* once per checkout every few minutes, not once per path a command names. */
|
|
65
|
+
private repositoryOf;
|
|
66
|
+
/** Wait for a shared repository lookup, dropping this caller's interest when
|
|
67
|
+
* it gives up; the lookup is stopped only when no caller is left. */
|
|
68
|
+
private awaitRepository;
|
|
69
|
+
/** The scratch a new snapshot of `repository` reads into. */
|
|
70
|
+
private scratchFor;
|
|
71
|
+
/** Delete a retired scratch once nothing reads it or holds a tree in it. */
|
|
72
|
+
private sweep;
|
|
51
73
|
before(conversation: string, toolUseId: string, cwd: string, command: string, input?: Json): Promise<void>;
|
|
52
74
|
/** Forget the snapshot of a tool call whose PreToolUse callback gave up
|
|
53
75
|
* before the snapshot was taken: the tool may have run while it was read,
|