@phren/cli 0.3.39 → 0.3.41
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/conductor-sets.d.ts +5 -0
- package/dist/bridge/conductor-sets.js +23 -3
- package/dist/bridge/enroll.js +22 -4
- package/dist/bridge/health.d.ts +15 -0
- package/dist/bridge/health.js +32 -2
- package/dist/bridge/server-routes.d.ts +1 -0
- package/dist/bridge/server-routes.js +9 -4
- package/dist/bridge/server.js +4 -1
- package/dist/bridge-hook.mjs +22014 -21397
- package/dist/cli/govern.js +8 -2
- package/dist/cli/session-git.js +1 -0
- package/dist/cli/session-start.js +18 -10
- package/dist/cli/session-stop.js +12 -1
- package/dist/governance/policy.d.ts +3 -0
- package/dist/governance/policy.js +4 -0
- package/dist/link/doctor.js +57 -0
- package/dist/modules/registry.js +2 -2
- package/dist/sync/auth-heal.d.ts +15 -0
- package/dist/sync/auth-heal.js +94 -0
- package/dist/sync/auth.js +19 -5
- package/dist/sync/conflict-resolve.d.ts +1 -1
- package/dist/sync/conflict-resolve.js +28 -8
- package/dist/sync/maintenance-writer.d.ts +7 -0
- package/dist/sync/maintenance-writer.js +37 -0
- package/dist/sync/pull.d.ts +2 -0
- package/dist/sync/pull.js +5 -3
- package/dist/sync/store-cleanup.d.ts +11 -0
- package/dist/sync/store-cleanup.js +117 -0
- package/dist/sync/store-merge.d.ts +4 -0
- package/dist/sync/store-merge.js +14 -1
- package/dist/sync/store-sync.d.ts +60 -0
- package/dist/sync/store-sync.js +182 -0
- package/dist/sync/task-merge.d.ts +5 -0
- package/dist/sync/task-merge.js +57 -15
- package/package.json +1 -1
package/dist/cli/govern.js
CHANGED
|
@@ -12,6 +12,7 @@ import { logger } from "../logger.js";
|
|
|
12
12
|
import { compactFindingJournals } from "../finding/journal.js";
|
|
13
13
|
import { resolveRuntimeProfile } from "../runtime-profile.js";
|
|
14
14
|
import { summarizeProject } from "../content/summarize.js";
|
|
15
|
+
import { claimMaintenanceWriter } from "../sync/maintenance-writer.js";
|
|
15
16
|
import { tidyDoneTasks } from "../data/tasks.js";
|
|
16
17
|
// ── Shared helpers ───────────────────────────────────────────────────────────
|
|
17
18
|
function targetProjects(projectArg) {
|
|
@@ -443,7 +444,10 @@ export async function handleBackgroundMaintenance(projectArg) {
|
|
|
443
444
|
// cheap; a model is only consulted from `phren maintain summarize --llm`.
|
|
444
445
|
let tidied = 0;
|
|
445
446
|
let summarized = 0;
|
|
446
|
-
|
|
447
|
+
// Only the lease holder rewrites generated files; every computer still
|
|
448
|
+
// folds its own journal into the findings above (maintenance-writer.ts).
|
|
449
|
+
const lease = claimMaintenanceWriter(getPhrenPath());
|
|
450
|
+
for (const project of lease.writer ? targetProjects(projectArg) : []) {
|
|
447
451
|
try {
|
|
448
452
|
const tidy = tidyDoneTasks(getPhrenPath(), project, 30);
|
|
449
453
|
if (tidy.ok && tidy.data.startsWith("Tidied"))
|
|
@@ -461,7 +465,9 @@ export async function handleBackgroundMaintenance(projectArg) {
|
|
|
461
465
|
logger.debug("maintenance", `summarize ${project}: ${errorMessage(err)}`);
|
|
462
466
|
}
|
|
463
467
|
}
|
|
464
|
-
const pruneResult =
|
|
468
|
+
const pruneResult = lease.writer
|
|
469
|
+
? pruneDeadMemories(getPhrenPath(), projectArg)
|
|
470
|
+
: { ok: true, data: { message: `prune skipped: ${lease.holder} maintains generated files` } };
|
|
465
471
|
const pruneMsg = pruneResult.ok ? pruneResult.data.message.replace(/\n+/g, " ") : pruneResult.error;
|
|
466
472
|
if (!pruneResult.ok) {
|
|
467
473
|
updateRuntimeHealth(getPhrenPath(), {
|
package/dist/cli/session-git.js
CHANGED
|
@@ -182,7 +182,8 @@ export async function handleHookSessionStart() {
|
|
|
182
182
|
lastPullAt: startedAt,
|
|
183
183
|
lastPullStatus: pull.ok ? "ok" : "error",
|
|
184
184
|
lastPullDetail: pull.ok ? (pull.output || "pull ok") : (pull.error || "pull failed"),
|
|
185
|
-
|
|
185
|
+
// A failed pull keeps the last success: it is how long the store has been stuck.
|
|
186
|
+
...(pull.ok && hasRemote ? { lastSuccessfulPullAt: startedAt } : {}),
|
|
186
187
|
unsyncedCommits,
|
|
187
188
|
...("counts" in pull && pull.counts ? pull.counts : {}),
|
|
188
189
|
},
|
|
@@ -212,20 +213,27 @@ export async function handleHookSessionStart() {
|
|
|
212
213
|
catch {
|
|
213
214
|
// store-registry not available or no stores — skip silently
|
|
214
215
|
}
|
|
215
|
-
//
|
|
216
|
+
// A store that cannot reach or merge with its remote is said at session
|
|
217
|
+
// start (the one hook whose output the agent reads), at most once a day.
|
|
216
218
|
try {
|
|
217
219
|
const syncPrefs = readInstallPreferences(phrenPath);
|
|
218
|
-
const
|
|
219
|
-
if (
|
|
220
|
+
const noRemote = syncPrefs.syncIntent === "sync" && !hasRemote;
|
|
221
|
+
if (noRemote || (hasRemote && !pull.ok)) {
|
|
220
222
|
const syncWarnPath = sessionMarker(phrenPath, SYNC_WARN_MARKER);
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
223
|
+
let warnedAt = Number.NaN;
|
|
224
|
+
try {
|
|
225
|
+
warnedAt = Date.parse(fs.readFileSync(syncWarnPath, "utf8").trim());
|
|
226
|
+
}
|
|
227
|
+
catch { /* never warned */ }
|
|
228
|
+
if (Number.isNaN(warnedAt) || Date.now() - warnedAt > 86_400_000) {
|
|
225
229
|
process.stdout.write([
|
|
226
230
|
"<phren-notice>",
|
|
227
|
-
|
|
228
|
-
|
|
231
|
+
noRemote
|
|
232
|
+
? "Sync is configured but no git remote is connected. Your phren data is local-only."
|
|
233
|
+
: `The phren store did not sync with its remote: ${(pull.error || "unknown error").split("\n")[0]}`,
|
|
234
|
+
noRemote
|
|
235
|
+
? `To fix: cd ${phrenPath} && git remote add origin <YOUR_REPO_URL> && git push -u origin main`
|
|
236
|
+
: "Tell the user. `phren doctor --fix` reconciles the store and says what it could not resolve.",
|
|
229
237
|
"<phren-notice>",
|
|
230
238
|
"",
|
|
231
239
|
].join("\n"));
|
package/dist/cli/session-stop.js
CHANGED
|
@@ -380,8 +380,19 @@ export async function handleHookStop() {
|
|
|
380
380
|
: { ...previousSync, lastPushAt: now, lastPushDetail: detail, unsyncedCommits: pending },
|
|
381
381
|
});
|
|
382
382
|
appendAuditLog(phrenPath, "hook_stop", `status=${inSync ? "clean" : "clean-unsynced"} pending=${pending}`);
|
|
383
|
-
if (!inSync)
|
|
383
|
+
if (!inSync) {
|
|
384
|
+
// Retry what an earlier turn failed to push (otherwise it waits for the
|
|
385
|
+
// next write), at most every ten minutes.
|
|
386
|
+
const retry = runtimeFile(phrenPath, "sync-retry");
|
|
387
|
+
let last = 0;
|
|
388
|
+
try {
|
|
389
|
+
last = fs.statSync(retry).mtimeMs;
|
|
390
|
+
}
|
|
391
|
+
catch { /* first retry */ }
|
|
392
|
+
if (Date.now() - last > 600_000 && scheduleBackgroundSync(phrenPath))
|
|
393
|
+
fs.writeFileSync(retry, `${now}\n`);
|
|
384
394
|
warnIfSyncDegraded(phrenPath);
|
|
395
|
+
}
|
|
385
396
|
return;
|
|
386
397
|
}
|
|
387
398
|
// Stage all changes first, then unstage any sensitive files that slipped
|
|
@@ -82,6 +82,9 @@ export interface RuntimeHealth {
|
|
|
82
82
|
/** Commits ahead of and behind the upstream tracking ref at the last sync outcome. */
|
|
83
83
|
ahead?: number;
|
|
84
84
|
behind?: number;
|
|
85
|
+
/** The last failure of a store sync in plain words, cleared by the next full success. */
|
|
86
|
+
lastError?: string;
|
|
87
|
+
lastErrorAt?: string;
|
|
85
88
|
};
|
|
86
89
|
}
|
|
87
90
|
export type SyncStatus = NonNullable<RuntimeHealth["lastSync"]>;
|
|
@@ -244,6 +244,10 @@ function normalizeRuntimeHealth(data) {
|
|
|
244
244
|
normalized.lastSync.ahead = data.lastSync.ahead;
|
|
245
245
|
if (isFiniteNumber(data.lastSync.behind))
|
|
246
246
|
normalized.lastSync.behind = data.lastSync.behind;
|
|
247
|
+
if (typeof data.lastSync.lastError === "string")
|
|
248
|
+
normalized.lastSync.lastError = data.lastSync.lastError;
|
|
249
|
+
if (typeof data.lastSync.lastErrorAt === "string")
|
|
250
|
+
normalized.lastSync.lastErrorAt = data.lastSync.lastErrorAt;
|
|
247
251
|
}
|
|
248
252
|
return normalized;
|
|
249
253
|
}
|
package/dist/link/doctor.js
CHANGED
|
@@ -648,6 +648,63 @@ export async function runDoctor(phrenPath, fix = false, checkData = false, confi
|
|
|
648
648
|
ok: autoSaveStatus === "saved-pushed" || autoSaveStatus === "saved-local" || autoSaveStatus === "clean",
|
|
649
649
|
detail: describeAutoSave({ status: autoSaveStatus, at: autoSaveAt, detail: typeof autoSaveObj?.["detail"] === "string" ? autoSaveObj["detail"] : undefined }, isRecord(runtime?.["lastSync"]) ? runtime["lastSync"] : undefined),
|
|
650
650
|
});
|
|
651
|
+
// Each store against its remote: diverged, unsynced for over an hour, or
|
|
652
|
+
// failing. --fix first collapses repeated task ids (the data layer refuses
|
|
653
|
+
// to edit them), then runs one full reconcile.
|
|
654
|
+
try {
|
|
655
|
+
const { resolveAllStores } = await import("../store-registry.js");
|
|
656
|
+
const { syncStore, storeSyncState } = await import("../sync/store-sync.js");
|
|
657
|
+
const { dedupeTaskIds } = await import("../sync/task-merge.js");
|
|
658
|
+
for (const store of resolveAllStores(phrenPath).filter((entry) => entry.available !== false && fs.existsSync(path.join(entry.path, ".git")))) {
|
|
659
|
+
const repeated = [];
|
|
660
|
+
for (const dir of fs.readdirSync(store.path, { withFileTypes: true })) {
|
|
661
|
+
if (!dir.isDirectory() || dir.name.startsWith("."))
|
|
662
|
+
continue;
|
|
663
|
+
const file = path.join(store.path, dir.name, "tasks.md");
|
|
664
|
+
let text;
|
|
665
|
+
try {
|
|
666
|
+
text = fs.readFileSync(file, "utf8");
|
|
667
|
+
}
|
|
668
|
+
catch {
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
const result = dedupeTaskIds(text);
|
|
672
|
+
if (!result.dropped)
|
|
673
|
+
continue;
|
|
674
|
+
if (fix)
|
|
675
|
+
fs.writeFileSync(file, result.content);
|
|
676
|
+
repeated.push(`${dir.name}/tasks.md (${result.dropped})`);
|
|
677
|
+
}
|
|
678
|
+
if (repeated.length) {
|
|
679
|
+
checks.push({
|
|
680
|
+
name: `task-ids:${store.name}`,
|
|
681
|
+
ok: fix,
|
|
682
|
+
detail: fix ? `collapsed repeated task ids in ${repeated.join(", ")}` : `repeated task ids in ${repeated.join(", ")}; edits to those tasks are refused. Run \`phren doctor --fix\`.`,
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
let synced = "";
|
|
686
|
+
if (fix) {
|
|
687
|
+
const outcome = await syncStore(store.path, { push: store.role !== "readonly" && store.sync !== "pull-only", source: "doctor" });
|
|
688
|
+
synced = `${outcome.status}: ${outcome.detail}${outcome.cleaned.length ? ` (${outcome.cleaned.join("; ")})` : ""}`;
|
|
689
|
+
}
|
|
690
|
+
const state = await storeSyncState(store.name, store.path);
|
|
691
|
+
if (!state.upstream && !synced)
|
|
692
|
+
continue;
|
|
693
|
+
const healthy = !state.diverged && !state.stale && !state.error;
|
|
694
|
+
checks.push({
|
|
695
|
+
name: `store-sync:${store.name}`,
|
|
696
|
+
ok: healthy,
|
|
697
|
+
detail: [
|
|
698
|
+
healthy ? `in sync${state.lastSyncedAt ? `, last synced ${state.lastSyncedAt}` : ""}` : state.warning ?? "not in sync",
|
|
699
|
+
synced && `doctor --fix: ${synced}`,
|
|
700
|
+
!healthy && !fix ? "run `phren doctor --fix` to reconcile" : "",
|
|
701
|
+
].filter(Boolean).join("; "),
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
catch (err) {
|
|
706
|
+
checks.push({ name: "store-sync", ok: false, detail: `could not check store sync: ${errorMessage(err)}` });
|
|
707
|
+
}
|
|
651
708
|
checks.push({
|
|
652
709
|
name: "runtime-prompt",
|
|
653
710
|
ok: Boolean(runtime?.["lastPromptAt"]),
|
package/dist/modules/registry.js
CHANGED
|
@@ -132,8 +132,8 @@ export const BUILTIN_MODULES = [
|
|
|
132
132
|
schemaVersion: 1, name: "conductor", version: VERSION, defaultEnabled: false, requires: ["memory", "hook"],
|
|
133
133
|
tools: full(["dispatch", "dispatch_returns", "dispatch_approve", "hand_off", "live_sessions", "account_usage", "authority", "dispatch_report", "owner_inbox", "move_session"]), cliCommands: ["dispatch", "dispatch status", "dispatch returns", "dispatch sessions", "dispatch usage", "dispatch report", "hand-off", "move", "conductor", "conductor grants", "conductor status", "conductor make", "conductor stop", "conductor sets", "conductor integrator", "owner-inbox", "authority", "bridge enroll-computer"], agentHooks: [],
|
|
134
134
|
hookRoutes: [...routes("GET", ["/v1/dispatch", "/v1/dispatch/capacity", "/v1/dispatch/arrival", "/v1/dispatch/brief", "/v1/conductor", "/v1/conductor/grants", "/v1/computers", "/v1/sets", "/v1/authority", "/v1/owner-inbox", "/v1/conductor/integrator"]),
|
|
135
|
-
...routes("POST", ["/v1/dispatch", "/v1/dispatch/workers", "/v1/dispatch/returns", "/v1/dispatch/approve", "/v1/dispatch/report", "/v1/dispatch/close", "/v1/owner-inbox", "/v1/conductor/integrator", "/v1/hand-off", "/v1/hand-off/status", "/v1/conductor/grants", "/v1/conductor/make", "/v1/conductor/stop", "/v1/sets/name", "/v1/authority", "/v1/authority/confirm"]), ...routes("DELETE", ["/v1/conductor/grants", "/v1/authority"])],
|
|
136
|
-
capabilities: ["dispatch", "conductorSets", "ownerInbox", "workerReports", "queuedHandOff"], storeFiles: ["global/skills/conductor/**"],
|
|
135
|
+
...routes("POST", ["/v1/dispatch", "/v1/dispatch/workers", "/v1/dispatch/returns", "/v1/dispatch/approve", "/v1/dispatch/report", "/v1/dispatch/close", "/v1/owner-inbox", "/v1/conductor/integrator", "/v1/hand-off", "/v1/hand-off/status", "/v1/conductor/grants", "/v1/conductor/make", "/v1/conductor/stop", "/v1/sets/name", "/v1/sets/link", "/v1/authority", "/v1/authority/confirm"]), ...routes("DELETE", ["/v1/conductor/grants", "/v1/authority"])],
|
|
136
|
+
capabilities: ["dispatch", "conductorSets", "setLink", "ownerInbox", "workerReports", "queuedHandOff"], storeFiles: ["global/skills/conductor/**"],
|
|
137
137
|
localFiles: ["<bridge>/hooks.yaml", "<bridge>/conductor.yaml", "<bridge>/conductor-role.json", "<bridge>/conductor/brief.md", "<bridge>/authority.yaml", "<bridge>/authority-confirmations.json", "<bridge>/dispatches/*.json", "<bridge>/hand-offs/*.json", "<bridge>/worker-reports/*.json", "<bridge>/closed-workers/*.json", "<bridge>/owner-inbox.json", "<bridge>/integrator.json"], phoneScreens: [], skills: ["conductor"],
|
|
138
138
|
},
|
|
139
139
|
{
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { RunStoreGit } from "./store-merge.js";
|
|
2
|
+
/** A failed switch is retried at most this often. */
|
|
3
|
+
export declare const AUTH_HEAL_RETRY_MS: number;
|
|
4
|
+
export interface AuthHealResult {
|
|
5
|
+
healed: boolean;
|
|
6
|
+
detail: string;
|
|
7
|
+
}
|
|
8
|
+
/** The HTTPS URL of a GitHub SSH remote (`git@github.com:o/r.git`, `ssh://git@github.com/o/r`), else undefined. */
|
|
9
|
+
export declare function githubHttpsUrl(remote: string): string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Called after a transport command failed authentication. Switches the named
|
|
12
|
+
* remote to HTTPS through gh when that answers, and otherwise says what to do.
|
|
13
|
+
* `git` is the unguarded runner, so the probe never re-enters the backoff.
|
|
14
|
+
*/
|
|
15
|
+
export declare function healStoreAuth(cwd: string, remoteName: string, remote: string, git: RunStoreGit, now?: number): Promise<AuthHealResult>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A GitHub store remote over SSH whose key GitHub refuses, on a computer
|
|
3
|
+
* where `gh` is signed in: the store switches to HTTPS with `gh` as its
|
|
4
|
+
* credential helper instead of failing quietly until someone looks. One
|
|
5
|
+
* computer's store sat diverged for two days that way while gh worked.
|
|
6
|
+
*/
|
|
7
|
+
import { execFile } from "child_process";
|
|
8
|
+
import * as fs from "fs";
|
|
9
|
+
import * as path from "path";
|
|
10
|
+
import { promisify } from "util";
|
|
11
|
+
import { atomicWriteText, runtimeFile } from "../phren-paths.js";
|
|
12
|
+
import { appendSyncLog } from "./outcome.js";
|
|
13
|
+
const exec = promisify(execFile);
|
|
14
|
+
/** A failed switch is retried at most this often. */
|
|
15
|
+
export const AUTH_HEAL_RETRY_MS = 60 * 60 * 1000;
|
|
16
|
+
/** The HTTPS URL of a GitHub SSH remote (`git@github.com:o/r.git`, `ssh://git@github.com/o/r`), else undefined. */
|
|
17
|
+
export function githubHttpsUrl(remote) {
|
|
18
|
+
const match = /^(?:ssh:\/\/)?git@github\.com[:/]([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+?)(?:\.git)?\/?$/.exec(remote.trim());
|
|
19
|
+
return match ? `https://github.com/${match[1]}/${match[2]}.git` : undefined;
|
|
20
|
+
}
|
|
21
|
+
/** gh from PATH, else where Homebrew and Linux packages put it (a service's PATH is often bare). */
|
|
22
|
+
async function signedInGh() {
|
|
23
|
+
const candidates = [
|
|
24
|
+
...(process.env.PATH ?? "").split(path.delimiter).filter(Boolean).map((dir) => path.join(dir, process.platform === "win32" ? "gh.exe" : "gh")),
|
|
25
|
+
"/opt/homebrew/bin/gh", "/usr/local/bin/gh", "/usr/bin/gh",
|
|
26
|
+
];
|
|
27
|
+
const gh = candidates.find((file) => { try {
|
|
28
|
+
return fs.statSync(file).isFile();
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
} });
|
|
33
|
+
if (!gh)
|
|
34
|
+
return undefined;
|
|
35
|
+
try {
|
|
36
|
+
// The token itself is discarded; only whether gh has one matters.
|
|
37
|
+
await exec(gh, ["auth", "token", "--hostname", "github.com"], { timeout: 10_000 });
|
|
38
|
+
return gh;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function readHealState(cwd) {
|
|
45
|
+
try {
|
|
46
|
+
const state = JSON.parse(fs.readFileSync(runtimeFile(cwd, "sync-auth-heal.json"), "utf8"));
|
|
47
|
+
return typeof state?.remote === "string" && Number.isFinite(state.at) && typeof state.detail === "string" ? state : undefined;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Called after a transport command failed authentication. Switches the named
|
|
55
|
+
* remote to HTTPS through gh when that answers, and otherwise says what to do.
|
|
56
|
+
* `git` is the unguarded runner, so the probe never re-enters the backoff.
|
|
57
|
+
*/
|
|
58
|
+
export async function healStoreAuth(cwd, remoteName, remote, git, now = Date.now()) {
|
|
59
|
+
const https = githubHttpsUrl(remote);
|
|
60
|
+
if (!https)
|
|
61
|
+
return { healed: false, detail: "" };
|
|
62
|
+
const previous = readHealState(cwd);
|
|
63
|
+
if (previous?.remote === remote && now - previous.at < AUTH_HEAL_RETRY_MS)
|
|
64
|
+
return { healed: false, detail: previous.detail };
|
|
65
|
+
const fail = (detail) => {
|
|
66
|
+
atomicWriteText(runtimeFile(cwd, "sync-auth-heal.json"), JSON.stringify({ remote, at: now, detail }) + "\n");
|
|
67
|
+
appendSyncLog(cwd, "auth", detail);
|
|
68
|
+
return { healed: false, detail };
|
|
69
|
+
};
|
|
70
|
+
const gh = await signedInGh();
|
|
71
|
+
if (!gh) {
|
|
72
|
+
return fail(`GitHub refused this computer's SSH key for ${remote}, and gh is not signed in here. Add the key to GitHub, or run 'gh auth login' and then 'phren doctor --fix'.`);
|
|
73
|
+
}
|
|
74
|
+
const helper = `!${gh.includes(" ") ? `"${gh}"` : gh} auth git-credential`;
|
|
75
|
+
// An empty helper first drops inherited ones (a stale keychain entry would answer before gh).
|
|
76
|
+
const probe = await git(cwd, ["-c", "credential.helper=", "-c", `credential.helper=${helper}`, "ls-remote", "--quiet", "--", https, "HEAD"]);
|
|
77
|
+
if (!probe.ok) {
|
|
78
|
+
return fail(`GitHub refused this computer's SSH key for ${remote}, and HTTPS through gh failed too: ${(probe.error ?? "").split("\n").find(Boolean)?.slice(0, 200) ?? "unknown error"}`);
|
|
79
|
+
}
|
|
80
|
+
const steps = [
|
|
81
|
+
["config", "--local", "--replace-all", "credential.https://github.com.helper", ""],
|
|
82
|
+
["config", "--local", "--add", "credential.https://github.com.helper", helper],
|
|
83
|
+
["remote", "set-url", "--", remoteName, https],
|
|
84
|
+
];
|
|
85
|
+
for (const step of steps) {
|
|
86
|
+
const result = await git(cwd, step);
|
|
87
|
+
if (!result.ok)
|
|
88
|
+
return fail(`Could not switch ${remoteName} to HTTPS: ${(result.error ?? "").split("\n").find(Boolean)?.slice(0, 200) ?? "git config failed"}`);
|
|
89
|
+
}
|
|
90
|
+
fs.rmSync(runtimeFile(cwd, "sync-auth-heal.json"), { force: true });
|
|
91
|
+
const detail = `GitHub refused this computer's SSH key for ${remote}; switched ${remoteName} to ${https} with gh as its credential helper.`;
|
|
92
|
+
appendSyncLog(cwd, "auth", detail);
|
|
93
|
+
return { healed: true, detail };
|
|
94
|
+
}
|
package/dist/sync/auth.js
CHANGED
|
@@ -3,6 +3,7 @@ import * as fs from "fs";
|
|
|
3
3
|
import { atomicWriteText, runtimeFile } from "../phren-paths.js";
|
|
4
4
|
import { tryFileLock, withFileLock } from "../governance/locks.js";
|
|
5
5
|
import { runGit } from "../utils-helpers.js";
|
|
6
|
+
import { healStoreAuth } from "./auth-heal.js";
|
|
6
7
|
export const AUTH_BACKOFF_INITIAL_MS = 60 * 60 * 1000;
|
|
7
8
|
export const AUTH_BACKOFF_MAX_MS = 24 * AUTH_BACKOFF_INITIAL_MS;
|
|
8
9
|
export const AUTH_UNREGISTER_AFTER_MS = 7 * AUTH_BACKOFF_MAX_MS;
|
|
@@ -86,20 +87,33 @@ export function withStoreAuthBackoff(git, now = Date.now) {
|
|
|
86
87
|
try {
|
|
87
88
|
const previous = readStoreAuthFailure(cwd);
|
|
88
89
|
const sameRemote = previous?.remote === remote.remote && previous.remoteName === remote.remoteName;
|
|
90
|
+
let healDetail = "";
|
|
89
91
|
if (sameRemote && authBackoffActive(previous, now())) {
|
|
90
|
-
|
|
92
|
+
// A GitHub SSH remote may still heal through gh (rate-limited on its own).
|
|
93
|
+
const healed = await healStoreAuth(cwd, remote.remoteName, remote.remote, git, now());
|
|
94
|
+
if (!healed.healed)
|
|
95
|
+
return { ok: false, output: "", error: `${storeAuthDetail(previous)}${healed.detail ? `. ${healed.detail}` : ""}` };
|
|
96
|
+
healDetail = healed.detail;
|
|
97
|
+
}
|
|
98
|
+
let result = await git(cwd, args);
|
|
99
|
+
if (!result.ok && isGitAuthFailure(result.error) && !healDetail) {
|
|
100
|
+
const healed = await healStoreAuth(cwd, remote.remoteName, remote.remote, git, now());
|
|
101
|
+
healDetail = healed.detail;
|
|
102
|
+
// The remote now points somewhere else, so any old block no longer applies.
|
|
103
|
+
if (healed.healed)
|
|
104
|
+
result = await git(cwd, args);
|
|
91
105
|
}
|
|
92
|
-
const result = await git(cwd, args);
|
|
93
106
|
// A push-only permission failure must not disable otherwise readable stores.
|
|
94
107
|
if (args[0] === "push")
|
|
95
|
-
return result;
|
|
108
|
+
return !result.ok && healDetail ? { ...result, error: `${result.error ?? "push failed"}\n${healDetail}` } : result;
|
|
96
109
|
if (result.ok) {
|
|
97
110
|
if (sameRemote)
|
|
98
111
|
clearStoreAuthFailure(cwd);
|
|
99
112
|
}
|
|
100
113
|
else if (isGitAuthFailure(result.error)) {
|
|
101
|
-
const
|
|
102
|
-
|
|
114
|
+
const current = storeSyncRemote(cwd, remote.remoteName) ?? remote;
|
|
115
|
+
const state = recordStoreAuthFailure(cwd, current.remoteName, current.remote, now());
|
|
116
|
+
return { ...result, error: `${storeAuthDetail(state)}${healDetail ? `. ${healDetail}` : ""}` };
|
|
103
117
|
}
|
|
104
118
|
else if (sameRemote) {
|
|
105
119
|
// The last failure was not authentication; do not age it toward removal.
|
|
@@ -2,7 +2,7 @@ export interface ConflictResolution {
|
|
|
2
2
|
resolved: string[];
|
|
3
3
|
unresolved: string[];
|
|
4
4
|
}
|
|
5
|
-
type Strategy = "tasks" | "findings" | "archive" | "topic" | "summary" | "union" | null;
|
|
5
|
+
type Strategy = "tasks" | "findings" | "archive" | "topic" | "summary" | "union" | "theirs" | null;
|
|
6
6
|
export declare function conflictStrategy(relFile: string): Strategy;
|
|
7
7
|
/** Resolves what it can in the store's in-progress merge and stages it. */
|
|
8
8
|
export declare function resolveStoreConflicts(cwd: string): ConflictResolution;
|
|
@@ -21,7 +21,7 @@ import { errorMessage } from "../utils.js";
|
|
|
21
21
|
import { nonInteractiveGitEnv } from "../utils-helpers.js";
|
|
22
22
|
import { mergeFindings, mergeTask } from "../content/validate.js";
|
|
23
23
|
import { cleanBlock, KNOWS_END, KNOWS_START, NOW_END, NOW_START, upsertBlock } from "../content/summarize.js";
|
|
24
|
-
import { mergeTasksByBid } from "./task-merge.js";
|
|
24
|
+
import { dedupeTaskIds, mergeTasksByBid } from "./task-merge.js";
|
|
25
25
|
export function conflictStrategy(relFile) {
|
|
26
26
|
const file = relFile.replace(/\\/g, "/");
|
|
27
27
|
const name = path.posix.basename(file).toLowerCase();
|
|
@@ -35,15 +35,20 @@ export function conflictStrategy(relFile) {
|
|
|
35
35
|
return "topic";
|
|
36
36
|
if (name === "summary.md")
|
|
37
37
|
return "summary";
|
|
38
|
-
if (name === "review.md")
|
|
38
|
+
if (name === "review.md" || name === "truths.md")
|
|
39
39
|
return "union";
|
|
40
|
+
// Coordination state Phren rewrites itself (maintenance-writer.ts): the remote's copy holds.
|
|
41
|
+
if (file === ".config/maintenance-writer.json")
|
|
42
|
+
return "theirs";
|
|
40
43
|
if (/(^|\/)(notes|journal)\/[^/]+\.md$/i.test(file))
|
|
41
44
|
return "union";
|
|
42
45
|
return null;
|
|
43
46
|
}
|
|
44
47
|
function git(cwd, args) {
|
|
45
48
|
return execFileSync("git", args, {
|
|
46
|
-
|
|
49
|
+
// The default 1 MiB buffer failed on a task archive that had outgrown it,
|
|
50
|
+
// and an unreadable side left the whole merge unresolved.
|
|
51
|
+
env: nonInteractiveGitEnv(), cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: EXEC_TIMEOUT_MS, maxBuffer: 256 * 1024 * 1024,
|
|
47
52
|
});
|
|
48
53
|
}
|
|
49
54
|
/** One index stage of a conflicted path (1 base, 2 local, 3 incoming), or null when that side has no file. */
|
|
@@ -73,7 +78,7 @@ function mergeText(base, ours, theirs) {
|
|
|
73
78
|
fs.writeFileSync(o, ours);
|
|
74
79
|
fs.writeFileSync(b, base);
|
|
75
80
|
fs.writeFileSync(t, theirs);
|
|
76
|
-
const result = spawnSync("git", ["merge-file", "-p", o, b, t], { encoding: "utf8", env: nonInteractiveGitEnv(), timeout: EXEC_TIMEOUT_MS });
|
|
81
|
+
const result = spawnSync("git", ["merge-file", "-p", o, b, t], { encoding: "utf8", env: nonInteractiveGitEnv(), timeout: EXEC_TIMEOUT_MS, maxBuffer: 256 * 1024 * 1024 });
|
|
77
82
|
return result.status === 0 ? result.stdout : null;
|
|
78
83
|
}
|
|
79
84
|
finally {
|
|
@@ -95,7 +100,7 @@ function unionText(base, ours, theirs) {
|
|
|
95
100
|
fs.writeFileSync(o, ours);
|
|
96
101
|
fs.writeFileSync(b, base);
|
|
97
102
|
fs.writeFileSync(t, theirs);
|
|
98
|
-
const result = spawnSync("git", ["merge-file", "--union", "-p", o, b, t], { encoding: "utf8", env: nonInteractiveGitEnv(), timeout: EXEC_TIMEOUT_MS });
|
|
103
|
+
const result = spawnSync("git", ["merge-file", "--union", "-p", o, b, t], { encoding: "utf8", env: nonInteractiveGitEnv(), timeout: EXEC_TIMEOUT_MS, maxBuffer: 256 * 1024 * 1024 });
|
|
99
104
|
return result.status === 0 ? result.stdout : null;
|
|
100
105
|
}
|
|
101
106
|
finally {
|
|
@@ -119,18 +124,33 @@ function mergeWithGeneratedBlock(base, ours, theirs, start, end, where, merge =
|
|
|
119
124
|
return rest;
|
|
120
125
|
return upsertBlock(rest, start, end, block, where);
|
|
121
126
|
}
|
|
127
|
+
const dedupe = (content) => content === null ? null : dedupeTaskIds(content).content;
|
|
128
|
+
function orUnion(merge, base, ours, theirs, after = (c) => c) {
|
|
129
|
+
try {
|
|
130
|
+
const merged = merge();
|
|
131
|
+
if (merged !== null)
|
|
132
|
+
return merged;
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
debugLog(`store merge fell back to a line union: ${errorMessage(err)}`);
|
|
136
|
+
}
|
|
137
|
+
return after(unionText(base, ours, theirs));
|
|
138
|
+
}
|
|
122
139
|
function resolveContent(strategy, base, ours, theirs) {
|
|
123
140
|
switch (strategy) {
|
|
124
|
-
|
|
125
|
-
|
|
141
|
+
// When a smarter merge refuses, a line union still keeps every record of
|
|
142
|
+
// both sides (repeated task ids collapsed): a knowledge file never blocks a sync.
|
|
143
|
+
case "tasks": return orUnion(() => mergeTasksByBid(base, ours, theirs), base, ours, theirs, dedupe);
|
|
144
|
+
case "findings": return orUnion(() => mergeFindings(ours, theirs), base, ours, theirs);
|
|
126
145
|
// Archived tasks only accumulate: keep both sides, the incoming version of any duplicate.
|
|
127
|
-
case "archive": return mergeTask(theirs, ours);
|
|
146
|
+
case "archive": return dedupe(orUnion(() => mergeTask(theirs, ours), base, ours, theirs));
|
|
128
147
|
case "topic": return mergeWithGeneratedBlock(base, ours, theirs, NOW_START, NOW_END, "top");
|
|
129
148
|
// Summaries are regenerated from the store: when the hand-written part
|
|
130
149
|
// doesn't merge cleanly, take the incoming file whole and let the next
|
|
131
150
|
// summarize pass fold in anything only this machine had.
|
|
132
151
|
case "summary": return mergeWithGeneratedBlock(base, ours, theirs, KNOWS_START, KNOWS_END, "bottom", mergeText) ?? theirs;
|
|
133
152
|
case "union": return unionText(base, ours, theirs);
|
|
153
|
+
case "theirs": return theirs;
|
|
134
154
|
}
|
|
135
155
|
}
|
|
136
156
|
/** Resolves what it can in the store's in-progress merge and stages it. */
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const MAINTENANCE_WRITER_FILE = ".config/maintenance-writer.json";
|
|
2
|
+
export declare const MAINTENANCE_LEASE_MS: number;
|
|
3
|
+
/** Whether this computer may rewrite generated files now; claims or renews the lease when it may. */
|
|
4
|
+
export declare function claimMaintenanceWriter(phrenPath: string, computer?: string, now?: number): {
|
|
5
|
+
writer: boolean;
|
|
6
|
+
holder: string;
|
|
7
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One computer rewrites a store's generated files (topic `## Now` blocks,
|
|
3
|
+
* summary.md's "What phren knows", archived tasks, pruned findings) at a
|
|
4
|
+
* time. When every computer's background maintenance rewrote them, each
|
|
5
|
+
* sync merged two rewrites of the same lines. The holder renews its lease
|
|
6
|
+
* daily as it runs; a holder that has stopped running loses it after three
|
|
7
|
+
* days to the next computer that does.
|
|
8
|
+
*/
|
|
9
|
+
import * as fs from "fs";
|
|
10
|
+
import * as path from "path";
|
|
11
|
+
import { getMachineName } from "../machine-identity.js";
|
|
12
|
+
import { atomicWriteText } from "../phren-paths.js";
|
|
13
|
+
export const MAINTENANCE_WRITER_FILE = ".config/maintenance-writer.json";
|
|
14
|
+
export const MAINTENANCE_LEASE_MS = 3 * 86_400_000;
|
|
15
|
+
const RENEW_AFTER_MS = 86_400_000;
|
|
16
|
+
function readLease(file) {
|
|
17
|
+
try {
|
|
18
|
+
const lease = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
19
|
+
return typeof lease?.computer === "string" && lease.computer && typeof lease.renewedAt === "string" && !Number.isNaN(Date.parse(lease.renewedAt))
|
|
20
|
+
? lease : undefined;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Whether this computer may rewrite generated files now; claims or renews the lease when it may. */
|
|
27
|
+
export function claimMaintenanceWriter(phrenPath, computer = getMachineName(), now = Date.now()) {
|
|
28
|
+
const file = path.join(phrenPath, MAINTENANCE_WRITER_FILE);
|
|
29
|
+
const lease = readLease(file);
|
|
30
|
+
const age = lease ? now - Date.parse(lease.renewedAt) : Number.POSITIVE_INFINITY;
|
|
31
|
+
if (lease && lease.computer !== computer && age < MAINTENANCE_LEASE_MS)
|
|
32
|
+
return { writer: false, holder: lease.computer };
|
|
33
|
+
if (!lease || lease.computer !== computer || age > RENEW_AFTER_MS) {
|
|
34
|
+
atomicWriteText(file, `${JSON.stringify({ computer, renewedAt: new Date(now).toISOString() }, null, 2)}\n`);
|
|
35
|
+
}
|
|
36
|
+
return { writer: true, holder: computer };
|
|
37
|
+
}
|
package/dist/sync/pull.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export declare function periodicPullEnabled(phrenPath: string): boolean;
|
|
|
8
8
|
export type RunGit = RunStoreGit;
|
|
9
9
|
export type { GitResult };
|
|
10
10
|
export declare const runPollGit: RunGit;
|
|
11
|
+
/** One Git command with its output, or its error once it fails or `timeout` passes. */
|
|
12
|
+
export declare function runGitWithin(timeout: number, cwd: string, args: string[]): Promise<GitResult>;
|
|
11
13
|
interface PollState {
|
|
12
14
|
checkedAt?: number;
|
|
13
15
|
failures?: number;
|
package/dist/sync/pull.js
CHANGED
|
@@ -42,10 +42,12 @@ export function periodicPullEnabled(phrenPath) {
|
|
|
42
42
|
&& resolveManagementCapabilities(phrenPath).lifecycleAutomations;
|
|
43
43
|
}
|
|
44
44
|
const execAsync = promisify(execFile);
|
|
45
|
-
export const runPollGit =
|
|
45
|
+
export const runPollGit = (cwd, args) => runGitWithin(10_000, cwd, args);
|
|
46
|
+
/** One Git command with its output, or its error once it fails or `timeout` passes. */
|
|
47
|
+
export async function runGitWithin(timeout, cwd, args) {
|
|
46
48
|
try {
|
|
47
49
|
const { stdout } = await execAsync("git", args, {
|
|
48
|
-
cwd, encoding: "utf8", timeout
|
|
50
|
+
cwd, encoding: "utf8", timeout, maxBuffer: 64 * 1024 * 1024,
|
|
49
51
|
env: nonInteractiveGitEnv({ ...process.env, GIT_OPTIONAL_LOCKS: "0" }),
|
|
50
52
|
});
|
|
51
53
|
return { ok: true, output: stdout.trim() };
|
|
@@ -53,7 +55,7 @@ export const runPollGit = async (cwd, args) => {
|
|
|
53
55
|
catch (err) {
|
|
54
56
|
return { ok: false, output: "", error: errorMessage(err) };
|
|
55
57
|
}
|
|
56
|
-
}
|
|
58
|
+
}
|
|
57
59
|
/**
|
|
58
60
|
* A missing state file is a first check. A corrupt one is logged and moved
|
|
59
61
|
* aside (kept for inspection) so the next check starts clean instead of
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type RunStoreGit } from "./store-merge.js";
|
|
2
|
+
/** Git state untouched this long is abandoned: no Phren step holds a merge open for minutes. */
|
|
3
|
+
export declare const STALE_GIT_STATE_MS: number;
|
|
4
|
+
export declare const STALE_INDEX_LOCK_MS: number;
|
|
5
|
+
export declare const BACKUP_REF_KEEP_DAYS = 14;
|
|
6
|
+
export interface CleanupOptions {
|
|
7
|
+
now?: number;
|
|
8
|
+
backupKeepDays?: number;
|
|
9
|
+
}
|
|
10
|
+
/** Removes abandoned sync leftovers and says what it removed, one line each. */
|
|
11
|
+
export declare function cleanupStore(store: string, git: RunStoreGit, options?: CleanupOptions): Promise<string[]>;
|