@phren/cli 0.2.15 → 0.3.0
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 +2 -2
- package/dist/agent-instructions.d.ts +12 -0
- package/dist/agent-instructions.js +52 -0
- package/dist/bridge/agent-hook-stores.d.ts +32 -0
- package/dist/bridge/agent-hook-stores.js +67 -0
- package/dist/bridge/agent-hooks.d.ts +195 -2
- package/dist/bridge/agent-hooks.js +1049 -38
- package/dist/bridge/canary.d.ts +27 -0
- package/dist/bridge/canary.js +189 -0
- package/dist/bridge/changes.d.ts +57 -0
- package/dist/bridge/changes.js +403 -0
- package/dist/bridge/client.d.ts +3 -0
- package/dist/bridge/client.js +36 -0
- package/dist/bridge/code-note.d.ts +23 -0
- package/dist/bridge/code-note.js +51 -0
- package/dist/bridge/code-routes.d.ts +151 -0
- package/dist/bridge/code-routes.js +442 -0
- package/dist/bridge/codex-threads.d.ts +27 -0
- package/dist/bridge/codex-threads.js +442 -0
- package/dist/bridge/command.d.ts +7 -0
- package/dist/bridge/command.js +50 -5
- package/dist/bridge/computer-names.d.ts +4 -0
- package/dist/bridge/computer-names.js +16 -0
- package/dist/bridge/computers.d.ts +9 -0
- package/dist/bridge/computers.js +111 -0
- package/dist/bridge/context.d.ts +14 -0
- package/dist/bridge/context.js +149 -0
- package/dist/bridge/dispatch-command.d.ts +3 -0
- package/dist/bridge/dispatch-command.js +93 -0
- package/dist/bridge/dispatch-hosts.d.ts +23 -0
- package/dist/bridge/dispatch-hosts.js +25 -0
- package/dist/bridge/dispatch-returns.d.ts +125 -0
- package/dist/bridge/dispatch-returns.js +283 -0
- package/dist/bridge/dispatch-tree.d.ts +54 -0
- package/dist/bridge/dispatch-tree.js +198 -0
- package/dist/bridge/dispatch.d.ts +210 -0
- package/dist/bridge/dispatch.js +286 -0
- package/dist/bridge/enroll.d.ts +31 -0
- package/dist/bridge/enroll.js +236 -0
- package/dist/bridge/fanout-messages.d.ts +74 -0
- package/dist/bridge/fanout-messages.js +230 -0
- package/dist/bridge/fanouts.d.ts +160 -0
- package/dist/bridge/fanouts.js +702 -0
- package/dist/bridge/file-range.d.ts +16 -0
- package/dist/bridge/file-range.js +90 -0
- package/dist/bridge/files.d.ts +20 -0
- package/dist/bridge/files.js +54 -0
- package/dist/bridge/git-publish.d.ts +23 -0
- package/dist/bridge/git-publish.js +145 -0
- package/dist/bridge/git-worktrees.d.ts +21 -0
- package/dist/bridge/git-worktrees.js +150 -0
- package/dist/bridge/git.d.ts +54 -0
- package/dist/bridge/git.js +552 -0
- package/dist/bridge/grants.d.ts +31 -0
- package/dist/bridge/grants.js +181 -0
- package/dist/bridge/hand-off.d.ts +79 -0
- package/dist/bridge/hand-off.js +151 -0
- package/dist/bridge/health.d.ts +128 -0
- package/dist/bridge/health.js +260 -0
- package/dist/bridge/herdr.d.ts +79 -6
- package/dist/bridge/herdr.js +292 -27
- package/dist/bridge/hook-main.js +2 -0
- package/dist/bridge/install.d.ts +28 -1
- package/dist/bridge/install.js +225 -38
- package/dist/bridge/launch-worktree.d.ts +21 -0
- package/dist/bridge/launch-worktree.js +70 -0
- package/dist/bridge/limits.d.ts +20 -0
- package/dist/bridge/limits.js +65 -0
- package/dist/bridge/locate.d.ts +16 -0
- package/dist/bridge/locate.js +75 -0
- package/dist/bridge/metrics.d.ts +29 -0
- package/dist/bridge/metrics.js +71 -0
- package/dist/bridge/model-switch.d.ts +35 -0
- package/dist/bridge/model-switch.js +250 -0
- package/dist/bridge/models.d.ts +46 -0
- package/dist/bridge/models.js +257 -0
- package/dist/bridge/opencode-approvals.d.ts +6 -0
- package/dist/bridge/opencode-approvals.js +60 -0
- package/dist/bridge/pane-text.d.ts +15 -0
- package/dist/bridge/pane-text.js +23 -0
- package/dist/bridge/peers.d.ts +27 -0
- package/dist/bridge/peers.js +130 -0
- package/dist/bridge/projects.d.ts +12 -1
- package/dist/bridge/projects.js +268 -20
- package/dist/bridge/protocol.d.ts +35 -2
- package/dist/bridge/protocol.js +61 -7
- package/dist/bridge/push.d.ts +83 -0
- package/dist/bridge/push.js +199 -0
- package/dist/bridge/questions.d.ts +38 -0
- package/dist/bridge/questions.js +205 -0
- package/dist/bridge/schedule-format.d.ts +103 -0
- package/dist/bridge/schedule-format.js +266 -0
- package/dist/bridge/schedule-launch.d.ts +11 -0
- package/dist/bridge/schedule-launch.js +169 -0
- package/dist/bridge/schedule-watch.d.ts +54 -0
- package/dist/bridge/schedule-watch.js +255 -0
- package/dist/bridge/schedules.d.ts +48 -0
- package/dist/bridge/schedules.js +205 -0
- package/dist/bridge/server-launch.d.ts +14 -0
- package/dist/bridge/server-launch.js +218 -0
- package/dist/bridge/server-overview.d.ts +45 -0
- package/dist/bridge/server-overview.js +102 -0
- package/dist/bridge/server-pane-routes.d.ts +36 -0
- package/dist/bridge/server-pane-routes.js +479 -0
- package/dist/bridge/server-routes.d.ts +108 -0
- package/dist/bridge/server-routes.js +556 -0
- package/dist/bridge/server-stream.d.ts +46 -0
- package/dist/bridge/server-stream.js +253 -0
- package/dist/bridge/server.d.ts +3 -15
- package/dist/bridge/server.js +162 -264
- package/dist/bridge/session-servers.d.ts +24 -0
- package/dist/bridge/session-servers.js +136 -0
- package/dist/bridge/side-questions.d.ts +74 -0
- package/dist/bridge/side-questions.js +252 -0
- package/dist/bridge/simulators.d.ts +28 -0
- package/dist/bridge/simulators.js +198 -0
- package/dist/bridge/speech-transcribe.d.ts +24 -0
- package/dist/bridge/speech-transcribe.js +124 -0
- package/dist/bridge/speech.d.ts +35 -0
- package/dist/bridge/speech.js +98 -0
- package/dist/bridge/steps.d.ts +13 -0
- package/dist/bridge/steps.js +222 -0
- package/dist/bridge/tab-activity.d.ts +18 -0
- package/dist/bridge/tab-activity.js +99 -0
- package/dist/bridge/terminal-choice.d.ts +64 -0
- package/dist/bridge/terminal-choice.js +281 -0
- package/dist/bridge/transcript-claude.d.ts +22 -0
- package/dist/bridge/transcript-claude.js +353 -0
- package/dist/bridge/transcript-codex.d.ts +30 -0
- package/dist/bridge/transcript-codex.js +282 -0
- package/dist/bridge/transcript-copilot.d.ts +4 -0
- package/dist/bridge/transcript-copilot.js +14 -0
- package/dist/bridge/transcript-opencode.d.ts +5 -0
- package/dist/bridge/transcript-opencode.js +26 -0
- package/dist/bridge/transcript-preview.d.ts +96 -0
- package/dist/bridge/transcript-preview.js +491 -0
- package/dist/bridge/transcripts.d.ts +81 -4
- package/dist/bridge/transcripts.js +332 -67
- package/dist/bridge/transport.d.ts +3 -0
- package/dist/bridge/transport.js +96 -15
- package/dist/bridge/uploads.d.ts +15 -0
- package/dist/bridge/uploads.js +44 -2
- package/dist/bridge/usage.d.ts +124 -0
- package/dist/bridge/usage.js +859 -0
- package/dist/bridge-hook.mjs +46545 -21051
- package/dist/cli/actions.js +14 -1
- package/dist/cli/config-policy.js +25 -0
- package/dist/cli/config-proactivity.d.ts +3 -1
- package/dist/cli/config-proactivity.js +35 -3
- package/dist/cli/config.js +4 -1
- package/dist/cli/extract.d.ts +0 -8
- package/dist/cli/extract.js +4 -7
- package/dist/cli/govern.js +5 -2
- package/dist/cli/hooks-globs.js +1 -1
- package/dist/cli/hooks.d.ts +1 -1
- package/dist/cli/hooks.js +9 -7
- package/dist/cli/namespaces-projects.js +12 -6
- package/dist/cli/namespaces-store.js +37 -6
- package/dist/cli/namespaces-tasks.js +1 -1
- package/dist/cli/schedules.d.ts +1 -0
- package/dist/cli/schedules.js +164 -0
- package/dist/cli/session-background.js +1 -1
- package/dist/cli/session-git.d.ts +8 -5
- package/dist/cli/session-git.js +45 -120
- package/dist/cli/session-start.js +3 -1
- package/dist/cli/session-stop.js +19 -8
- package/dist/cli/team.js +13 -10
- package/dist/cli-handlers.d.ts +1 -1
- package/dist/cli-handlers.js +8 -4
- package/dist/cli-help.d.ts +5 -4
- package/dist/cli-help.js +15 -15
- package/dist/cli-registry.d.ts +4 -1
- package/dist/cli-registry.js +119 -6
- package/dist/content/archive.js +2 -2
- package/dist/content/citation.d.ts +11 -0
- package/dist/content/citation.js +18 -0
- package/dist/content/learning.js +2 -0
- package/dist/content/summarize.d.ts +14 -15
- package/dist/content/summarize.js +60 -5
- package/dist/content/validate.d.ts +17 -4
- package/dist/content/validate.js +200 -122
- package/dist/core/project-registry.d.ts +26 -0
- package/dist/core/project-registry.js +573 -0
- package/dist/core/project.js +2 -3
- package/dist/data/access.d.ts +16 -1
- package/dist/data/access.js +31 -3
- package/dist/data/tasks.d.ts +18 -3
- package/dist/data/tasks.js +8 -7
- package/dist/entrypoint.js +44 -10
- package/dist/fanout/adapters/claude.d.ts +2 -0
- package/dist/fanout/adapters/claude.js +7 -0
- package/dist/fanout/adapters/codex.d.ts +2 -0
- package/dist/fanout/adapters/codex.js +9 -0
- package/dist/fanout/adapters/opencode.d.ts +4 -0
- package/dist/fanout/adapters/opencode.js +9 -0
- package/dist/fanout/adapters/types.d.ts +21 -0
- package/dist/fanout/adapters/types.js +3 -0
- package/dist/fanout/command.d.ts +2 -0
- package/dist/fanout/command.js +78 -0
- package/dist/fanout/launcher.d.ts +27 -0
- package/dist/fanout/launcher.js +156 -0
- package/dist/fanout/opencode-serve.d.ts +24 -0
- package/dist/fanout/opencode-serve.js +191 -0
- package/dist/fanout/picker.d.ts +91 -0
- package/dist/fanout/picker.js +51 -0
- package/dist/fanout/usage.d.ts +5 -0
- package/dist/fanout/usage.js +51 -0
- package/dist/fanout/watchdog.d.ts +10 -0
- package/dist/fanout/watchdog.js +27 -0
- package/dist/filenames.d.ts +9 -0
- package/dist/filenames.js +11 -0
- package/dist/finding/context.js +4 -22
- package/dist/generated/memory-ui-graph.browser.js +525 -419
- package/dist/governance/policy-constants.d.ts +11 -0
- package/dist/governance/policy-constants.js +9 -0
- package/dist/governance/policy.d.ts +5 -8
- package/dist/governance/policy.js +11 -8
- package/dist/graph-core/camera.d.ts +24 -0
- package/dist/graph-core/camera.js +18 -0
- package/dist/graph-core/index.d.ts +2 -0
- package/dist/graph-core/index.js +2 -0
- package/dist/graph-core/labels.d.ts +77 -0
- package/dist/graph-core/labels.js +124 -0
- package/dist/graph-core/model.d.ts +13 -0
- package/dist/graph-core/model.js +33 -0
- package/dist/graph-core/types.d.ts +2 -0
- package/dist/home-paths.d.ts +7 -0
- package/dist/home-paths.js +21 -0
- package/dist/hooks.d.ts +3 -15
- package/dist/hooks.js +14 -1
- package/dist/index.js +24 -2
- package/dist/init/guard-globals.d.ts +1 -1
- package/dist/init/guard-globals.js +4 -4
- package/dist/init/init-configure.js +3 -3
- package/dist/init/init-uninstall.js +15 -5
- package/dist/init/init-walkthrough.js +1 -1
- package/dist/init/init.js +26 -6
- package/dist/init/management-preset.d.ts +1 -1
- package/dist/init/management-preset.js +2 -2
- package/dist/init/preferences.js +1 -1
- package/dist/init/self-wiring.js +1 -1
- package/dist/init/setup.d.ts +3 -26
- package/dist/init/setup.js +68 -593
- package/dist/init/shared.js +2 -0
- package/dist/init/store-gitignore.d.ts +1 -6
- package/dist/init/store-gitignore.js +2 -8
- package/dist/init/teardown.d.ts +1 -1
- package/dist/init/teardown.js +2 -2
- package/dist/link/context-imports.d.ts +15 -0
- package/dist/link/context-imports.js +101 -0
- package/dist/link/context.js +1 -1
- package/dist/link/doctor.d.ts +22 -1
- package/dist/link/doctor.js +128 -58
- package/dist/link/link.js +29 -61
- package/dist/link/refresh.js +2 -2
- package/dist/link/skills.js +26 -5
- package/dist/machine-identity.d.ts +3 -0
- package/dist/machine-identity.js +6 -0
- package/dist/mcp/profile.d.ts +2 -19
- package/dist/mcp/profile.js +64 -22
- package/dist/memory-ui-graph.runtime.js +525 -419
- package/dist/modules/code-package.d.ts +16 -0
- package/dist/modules/code-package.js +160 -0
- package/dist/modules/command.d.ts +2 -0
- package/dist/modules/command.js +67 -0
- package/dist/modules/config.d.ts +4 -0
- package/dist/modules/config.js +70 -0
- package/dist/modules/manifest.d.ts +41 -0
- package/dist/modules/manifest.js +1 -0
- package/dist/modules/provision.d.ts +3 -0
- package/dist/modules/provision.js +84 -0
- package/dist/modules/registry.d.ts +16 -0
- package/dist/modules/registry.js +250 -0
- package/dist/modules/runtime.d.ts +15 -0
- package/dist/modules/runtime.js +59 -0
- package/dist/package-metadata.js +10 -2
- package/dist/phren-dotenv.js +4 -2
- package/dist/phren-paths.d.ts +3 -2
- package/dist/phren-paths.js +4 -5
- package/dist/profile-store.js +43 -8
- package/dist/project-config.d.ts +19 -1
- package/dist/project-config.js +29 -3
- package/dist/project-topics.d.ts +1 -0
- package/dist/project-topics.js +13 -10
- package/dist/provider-adapters.js +1 -4
- package/dist/query-correlation.js +6 -6
- package/dist/session/artifacts.js +5 -5
- package/dist/session/checkpoints.js +5 -0
- package/dist/session/utils.d.ts +2 -2
- package/dist/session/utils.js +6 -6
- package/dist/shared/content.d.ts +1 -1
- package/dist/shared/content.js +1 -1
- package/dist/shared/data-utils.js +1 -1
- package/dist/shared/fragment-graph.d.ts +1 -1
- package/dist/shared/fragment-graph.js +5 -5
- package/dist/shared/index.js +48 -7
- package/dist/shared/retrieval.d.ts +18 -3
- package/dist/shared/retrieval.js +101 -12
- package/dist/shared/search-fallback.js +4 -6
- package/dist/shared/sqljs.d.ts +2 -0
- package/dist/shared/sqljs.js +4 -2
- package/dist/shared.d.ts +0 -1
- package/dist/shared.js +0 -1
- package/dist/shell/input.d.ts +1 -5
- package/dist/shell/input.js +16 -10
- package/dist/shell/palette.d.ts +2 -2
- package/dist/shell/palette.js +3 -7
- package/dist/shell/render.d.ts +3 -1
- package/dist/shell/render.js +4 -4
- package/dist/shell/shell.js +4 -1
- package/dist/shell/types.d.ts +1 -0
- package/dist/shell/types.js +4 -0
- package/dist/shell/view.d.ts +0 -4
- package/dist/shell/view.js +19 -27
- package/dist/skill/registry.js +25 -17
- package/dist/skill/state.d.ts +3 -0
- package/dist/skill/state.js +13 -7
- package/dist/status.js +38 -17
- package/dist/store-registry.d.ts +44 -18
- package/dist/store-registry.js +169 -60
- package/dist/store-weight.d.ts +1 -6
- package/dist/store-weight.js +4 -2
- package/dist/sync/auth-doctor.d.ts +8 -0
- package/dist/sync/auth-doctor.js +27 -0
- package/dist/sync/auth.d.ts +27 -0
- package/dist/sync/auth.js +114 -0
- package/dist/sync/conflict-resolve.d.ts +9 -0
- package/dist/sync/conflict-resolve.js +146 -0
- package/dist/sync/git-state.d.ts +4 -0
- package/dist/sync/git-state.js +49 -0
- package/dist/sync/outcome.d.ts +34 -0
- package/dist/sync/outcome.js +57 -0
- package/dist/sync/pull.d.ts +15 -7
- package/dist/sync/pull.js +84 -35
- package/dist/sync/store-merge.d.ts +29 -0
- package/dist/sync/store-merge.js +137 -0
- package/dist/sync/task-merge.d.ts +22 -0
- package/dist/sync/task-merge.js +158 -0
- package/dist/task/github.js +1 -1
- package/dist/task/lifecycle.d.ts +5 -0
- package/dist/task/lifecycle.js +67 -23
- package/dist/terminal-text.d.ts +5 -0
- package/dist/terminal-text.js +11 -0
- package/dist/test-global-setup.js +7 -0
- package/dist/tool-registry.d.ts +1 -1
- package/dist/tool-registry.js +3 -1
- package/dist/tools/code.d.ts +3 -0
- package/dist/tools/code.js +186 -0
- package/dist/tools/config.js +8 -0
- package/dist/tools/data.js +14 -7
- package/dist/tools/dispatch.d.ts +2 -0
- package/dist/tools/dispatch.js +66 -0
- package/dist/tools/finding.d.ts +11 -0
- package/dist/tools/finding.js +180 -123
- package/dist/tools/ops.js +5 -2
- package/dist/tools/search.js +21 -8
- package/dist/tools/session.js +21 -22
- package/dist/tools/tasks.js +8 -2
- package/dist/tools/types.d.ts +2 -2
- package/dist/tools/types.js +2 -2
- package/dist/ui/assets.js +1 -1
- package/dist/ui/data.js +14 -15
- package/dist/ui/deep-void.d.ts +1 -1
- package/dist/ui/deep-void.js +0 -1
- package/dist/ui/graph-chrome.d.ts +1 -1
- package/dist/ui/graph-chrome.js +51 -0
- package/dist/ui/page.js +5 -5
- package/dist/ui/scripts.js +67 -30
- package/dist/ui/server.js +52 -7
- package/dist/update.js +22 -11
- package/dist/utils-fts.js +1 -0
- package/dist/utils-helpers.d.ts +7 -0
- package/dist/utils-helpers.js +17 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -1
- package/package.json +49 -7
- package/plugins/opencode/phren-transcript.js +340 -0
- package/starter/README.md +4 -4
- package/starter/global/{CLAUDE.md → AGENTS.md} +2 -2
- package/starter/global/skills/conductor/SKILL.md +106 -0
- package/starter/global/skills/fanout/SKILL.md +20 -0
- package/starter/global/skills/phren-discover/SKILL.md +6 -6
- package/starter/global/skills/phren-init/SKILL.md +4 -4
- package/starter/global/skills/phren-sync/SKILL.md +3 -3
- package/starter/templates/frontend/reference/README.md +1 -1
- package/starter/templates/library/reference/README.md +1 -1
- package/starter/templates/monorepo/reference/README.md +1 -1
- package/starter/templates/python-project/reference/README.md +1 -1
- /package/starter/templates/frontend/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/library/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/monorepo/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/python-project/{CLAUDE.md → AGENTS.md} +0 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the content conflicts a store merge can settle without a person:
|
|
3
|
+
* tasks.md per task id against the merge base, generated blocks from the
|
|
4
|
+
* incoming side, and the record-oriented files that already union-merge.
|
|
5
|
+
* Everything else stays conflicted so the caller aborts and names it.
|
|
6
|
+
*/
|
|
7
|
+
import { execFileSync, spawnSync } from "child_process";
|
|
8
|
+
import * as crypto from "crypto";
|
|
9
|
+
import * as fs from "fs";
|
|
10
|
+
import * as os from "os";
|
|
11
|
+
import * as path from "path";
|
|
12
|
+
import { isTaskFileName } from "../filenames.js";
|
|
13
|
+
import { debugLog, EXEC_TIMEOUT_MS } from "../shared.js";
|
|
14
|
+
import { errorMessage } from "../utils.js";
|
|
15
|
+
import { nonInteractiveGitEnv } from "../utils-helpers.js";
|
|
16
|
+
import { mergeFindings, mergeTask } from "../content/validate.js";
|
|
17
|
+
import { KNOWS_END, KNOWS_START, NOW_END, NOW_START, upsertBlock } from "../content/summarize.js";
|
|
18
|
+
import { mergeTasksByBid } from "./task-merge.js";
|
|
19
|
+
export function conflictStrategy(relFile) {
|
|
20
|
+
const file = relFile.replace(/\\/g, "/");
|
|
21
|
+
const name = path.posix.basename(file).toLowerCase();
|
|
22
|
+
if (isTaskFileName(name))
|
|
23
|
+
return "tasks";
|
|
24
|
+
if (name === "findings.md")
|
|
25
|
+
return "findings";
|
|
26
|
+
if (/^\.config\/task-archive\/[^/]+\.md$/i.test(file))
|
|
27
|
+
return "archive";
|
|
28
|
+
if (/(^|\/)reference\/topics\/[^/]+\.md$/i.test(file))
|
|
29
|
+
return "topic";
|
|
30
|
+
if (name === "summary.md")
|
|
31
|
+
return "summary";
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
function git(cwd, args) {
|
|
35
|
+
return execFileSync("git", args, {
|
|
36
|
+
env: nonInteractiveGitEnv(), cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: EXEC_TIMEOUT_MS,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** One index stage of a conflicted path (1 base, 2 local, 3 incoming), or null when that side has no file. */
|
|
40
|
+
function stage(cwd, n, relFile) {
|
|
41
|
+
try {
|
|
42
|
+
return git(cwd, ["show", `:${n}:${relFile}`]);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const BLOCK_RE = (start, end) => new RegExp(`${start.replace("-->", "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}[^\\n]*-->[\\s\\S]*?${end.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\n*`);
|
|
49
|
+
function splitBlock(content, start, end) {
|
|
50
|
+
const match = BLOCK_RE(start, end).exec(content);
|
|
51
|
+
if (!match)
|
|
52
|
+
return { rest: content, block: null };
|
|
53
|
+
return { rest: content.slice(0, match.index) + content.slice(match.index + match[0].length), block: match[0].replace(/\n+$/, "") };
|
|
54
|
+
}
|
|
55
|
+
/** A line-level three-way merge through `git merge-file`; null when lines still conflict. */
|
|
56
|
+
function mergeText(base, ours, theirs) {
|
|
57
|
+
if (ours === theirs || ours === base)
|
|
58
|
+
return theirs;
|
|
59
|
+
if (theirs === base)
|
|
60
|
+
return ours;
|
|
61
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "phren-merge-"));
|
|
62
|
+
try {
|
|
63
|
+
const [o, b, t] = ["ours", "base", "theirs"].map((name) => path.join(dir, name));
|
|
64
|
+
fs.writeFileSync(o, ours);
|
|
65
|
+
fs.writeFileSync(b, base);
|
|
66
|
+
fs.writeFileSync(t, theirs);
|
|
67
|
+
const result = spawnSync("git", ["merge-file", "-p", o, b, t], { encoding: "utf8", env: nonInteractiveGitEnv(), timeout: EXEC_TIMEOUT_MS });
|
|
68
|
+
return result.status === 0 ? result.stdout : null;
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Generated blocks regenerate, so the incoming block wins; the rest of the
|
|
76
|
+
* file must still merge line by line.
|
|
77
|
+
*/
|
|
78
|
+
function mergeWithGeneratedBlock(base, ours, theirs, start, end, where) {
|
|
79
|
+
const b = splitBlock(base, start, end);
|
|
80
|
+
const o = splitBlock(ours, start, end);
|
|
81
|
+
const t = splitBlock(theirs, start, end);
|
|
82
|
+
const rest = mergeText(b.rest, o.rest, t.rest);
|
|
83
|
+
if (rest === null)
|
|
84
|
+
return null;
|
|
85
|
+
const block = t.block ?? o.block;
|
|
86
|
+
if (!block)
|
|
87
|
+
return rest;
|
|
88
|
+
return upsertBlock(rest, start, end, block, where);
|
|
89
|
+
}
|
|
90
|
+
function resolveContent(strategy, base, ours, theirs) {
|
|
91
|
+
switch (strategy) {
|
|
92
|
+
case "tasks": return mergeTasksByBid(base, ours, theirs);
|
|
93
|
+
case "findings": return mergeFindings(ours, theirs);
|
|
94
|
+
// Archived tasks only accumulate: keep both sides, the incoming version of any duplicate.
|
|
95
|
+
case "archive": return mergeTask(theirs, ours);
|
|
96
|
+
case "topic": return mergeWithGeneratedBlock(base, ours, theirs, NOW_START, NOW_END, "top");
|
|
97
|
+
case "summary": return mergeWithGeneratedBlock(base, ours, theirs, KNOWS_START, KNOWS_END, "bottom");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Resolves what it can in the store's in-progress merge and stages it. */
|
|
101
|
+
export function resolveStoreConflicts(cwd) {
|
|
102
|
+
let conflicted;
|
|
103
|
+
try {
|
|
104
|
+
conflicted = git(cwd, ["diff", "--name-only", "--diff-filter=U"]).split("\n").map((line) => line.trim()).filter(Boolean);
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
debugLog(`resolveStoreConflicts: cannot list conflicts: ${errorMessage(err)}`);
|
|
108
|
+
return { resolved: [], unresolved: [] };
|
|
109
|
+
}
|
|
110
|
+
const resolved = [];
|
|
111
|
+
const unresolved = [];
|
|
112
|
+
for (const relFile of conflicted) {
|
|
113
|
+
const strategy = conflictStrategy(relFile);
|
|
114
|
+
if (!strategy) {
|
|
115
|
+
unresolved.push(relFile);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
const base = stage(cwd, 1, relFile) ?? "";
|
|
120
|
+
const ours = stage(cwd, 2, relFile);
|
|
121
|
+
const theirs = stage(cwd, 3, relFile);
|
|
122
|
+
const fullPath = path.join(cwd, relFile);
|
|
123
|
+
if (ours === null && theirs === null) {
|
|
124
|
+
unresolved.push(relFile);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
// Removed on one side and edited on the other: keep the edited file.
|
|
128
|
+
const merged = ours === null ? theirs : theirs === null ? ours : resolveContent(strategy, base, ours, theirs);
|
|
129
|
+
if (merged === null) {
|
|
130
|
+
unresolved.push(relFile);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const tmp = `${fullPath}.tmp-${crypto.randomUUID()}`;
|
|
134
|
+
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
135
|
+
fs.writeFileSync(tmp, merged);
|
|
136
|
+
fs.renameSync(tmp, fullPath);
|
|
137
|
+
git(cwd, ["add", "--", relFile]);
|
|
138
|
+
resolved.push(relFile);
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
debugLog(`resolveStoreConflicts: ${relFile}: ${errorMessage(err)}`);
|
|
142
|
+
unresolved.push(relFile);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return { resolved, unresolved };
|
|
146
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type GitOperation = "rebase" | "merge" | "cherry-pick" | "revert" | "sequencer" | "index lock";
|
|
2
|
+
/** Detect user-owned Git state without running a command that could alter it. */
|
|
3
|
+
export declare function inProgressGitOperation(store: string): GitOperation | undefined;
|
|
4
|
+
export declare function gitOperationRecovery(operation: GitOperation): string;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
function gitDirectory(store) {
|
|
4
|
+
const dotGit = path.join(store, ".git");
|
|
5
|
+
try {
|
|
6
|
+
const stat = fs.statSync(dotGit);
|
|
7
|
+
if (stat.isDirectory())
|
|
8
|
+
return dotGit;
|
|
9
|
+
if (!stat.isFile())
|
|
10
|
+
return undefined;
|
|
11
|
+
const match = /^gitdir:\s*(.+)\s*$/i.exec(fs.readFileSync(dotGit, "utf8"));
|
|
12
|
+
if (!match)
|
|
13
|
+
return undefined;
|
|
14
|
+
return path.resolve(store, match[1]);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Detect user-owned Git state without running a command that could alter it. */
|
|
21
|
+
export function inProgressGitOperation(store) {
|
|
22
|
+
const gitDir = gitDirectory(store);
|
|
23
|
+
if (!gitDir)
|
|
24
|
+
return undefined;
|
|
25
|
+
if (fs.existsSync(path.join(gitDir, "rebase-merge")) || fs.existsSync(path.join(gitDir, "rebase-apply")))
|
|
26
|
+
return "rebase";
|
|
27
|
+
if (fs.existsSync(path.join(gitDir, "MERGE_HEAD")))
|
|
28
|
+
return "merge";
|
|
29
|
+
if (fs.existsSync(path.join(gitDir, "CHERRY_PICK_HEAD")))
|
|
30
|
+
return "cherry-pick";
|
|
31
|
+
if (fs.existsSync(path.join(gitDir, "REVERT_HEAD")))
|
|
32
|
+
return "revert";
|
|
33
|
+
if (fs.existsSync(path.join(gitDir, "sequencer")))
|
|
34
|
+
return "sequencer";
|
|
35
|
+
if (fs.existsSync(path.join(gitDir, "index.lock")))
|
|
36
|
+
return "index lock";
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
export function gitOperationRecovery(operation) {
|
|
40
|
+
if (operation === "rebase")
|
|
41
|
+
return "git rebase --abort";
|
|
42
|
+
if (operation === "merge")
|
|
43
|
+
return "git merge --abort";
|
|
44
|
+
if (operation === "cherry-pick")
|
|
45
|
+
return "git cherry-pick --abort";
|
|
46
|
+
if (operation === "revert")
|
|
47
|
+
return "git revert --abort";
|
|
48
|
+
return "git status";
|
|
49
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
type RunGit = (cwd: string, args: string[]) => Promise<{
|
|
2
|
+
ok: boolean;
|
|
3
|
+
output?: string;
|
|
4
|
+
error?: string;
|
|
5
|
+
}>;
|
|
6
|
+
export interface AheadBehind {
|
|
7
|
+
ahead: number;
|
|
8
|
+
behind: number;
|
|
9
|
+
}
|
|
10
|
+
/** Commits on each side of the upstream, from the last fetched tracking ref. Undefined without an upstream. */
|
|
11
|
+
export declare function aheadBehind(cwd: string, git: RunGit): Promise<AheadBehind | undefined>;
|
|
12
|
+
/** The first non-empty line of an error, bounded, for logs and one-line status text. */
|
|
13
|
+
export declare function firstLine(text: string | undefined, max?: number): string;
|
|
14
|
+
export declare function formatCounts(counts: Partial<AheadBehind> | undefined): string;
|
|
15
|
+
/** Appends `[time] <source>: ok|failed <reason> (ahead N, behind M)` to the store's background-sync.log. */
|
|
16
|
+
export declare function logSyncOutcome(phrenPath: string, source: string, outcome: {
|
|
17
|
+
ok: boolean;
|
|
18
|
+
detail?: string;
|
|
19
|
+
counts?: AheadBehind;
|
|
20
|
+
}): void;
|
|
21
|
+
export declare function appendSyncLog(phrenPath: string, source: string, detail: string): void;
|
|
22
|
+
/**
|
|
23
|
+
* What doctor's runtime-auto-save check says: a failure names its first
|
|
24
|
+
* error line and the ahead/behind counts instead of only "sync-failed".
|
|
25
|
+
*/
|
|
26
|
+
export declare function describeAutoSave(autoSave: {
|
|
27
|
+
status?: string;
|
|
28
|
+
detail?: string;
|
|
29
|
+
at?: string;
|
|
30
|
+
} | undefined, sync: {
|
|
31
|
+
ahead?: number;
|
|
32
|
+
behind?: number;
|
|
33
|
+
} | undefined): string;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** One line per sync outcome in background-sync.log, and the words doctor and status show for it. */
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { runtimeFile } from "../phren-paths.js";
|
|
5
|
+
import { debugLog } from "../shared.js";
|
|
6
|
+
import { errorMessage } from "../utils.js";
|
|
7
|
+
/** Commits on each side of the upstream, from the last fetched tracking ref. Undefined without an upstream. */
|
|
8
|
+
export async function aheadBehind(cwd, git) {
|
|
9
|
+
try {
|
|
10
|
+
const counts = await git(cwd, ["rev-list", "--left-right", "--count", "HEAD...@{u}"]);
|
|
11
|
+
const match = counts.ok ? /^(\d+)\s+(\d+)$/.exec((counts.output ?? "").trim()) : null;
|
|
12
|
+
return match ? { ahead: Number(match[1]), behind: Number(match[2]) } : undefined;
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
debugLog(`ahead/behind: ${errorMessage(err)}`);
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** The first non-empty line of an error, bounded, for logs and one-line status text. */
|
|
20
|
+
export function firstLine(text, max = 200) {
|
|
21
|
+
return (text ?? "").split(/\r?\n/).map((line) => line.trim()).find(Boolean)?.slice(0, max) ?? "";
|
|
22
|
+
}
|
|
23
|
+
export function formatCounts(counts) {
|
|
24
|
+
return counts && typeof counts.ahead === "number" && typeof counts.behind === "number"
|
|
25
|
+
? ` (ahead ${counts.ahead}, behind ${counts.behind})` : "";
|
|
26
|
+
}
|
|
27
|
+
/** Appends `[time] <source>: ok|failed <reason> (ahead N, behind M)` to the store's background-sync.log. */
|
|
28
|
+
export function logSyncOutcome(phrenPath, source, outcome) {
|
|
29
|
+
// Long enough for a conflict detail to keep its whole file list.
|
|
30
|
+
const reason = firstLine(outcome.detail, 1000);
|
|
31
|
+
appendSyncLog(phrenPath, source, `${outcome.ok ? "ok" : "failed"}${reason ? ` ${reason}` : ""}${formatCounts(outcome.counts)}`);
|
|
32
|
+
}
|
|
33
|
+
export function appendSyncLog(phrenPath, source, detail) {
|
|
34
|
+
try {
|
|
35
|
+
const logPath = runtimeFile(phrenPath, "background-sync.log");
|
|
36
|
+
fs.mkdirSync(path.dirname(logPath), { recursive: true });
|
|
37
|
+
fs.appendFileSync(logPath, `[${new Date().toISOString()}] ${source}: ${detail}\n`);
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
debugLog(`${source} log: ${errorMessage(err)}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* What doctor's runtime-auto-save check says: a failure names its first
|
|
45
|
+
* error line and the ahead/behind counts instead of only "sync-failed".
|
|
46
|
+
*/
|
|
47
|
+
export function describeAutoSave(autoSave, sync) {
|
|
48
|
+
if (!autoSave?.status)
|
|
49
|
+
return "no auto-save runtime record yet";
|
|
50
|
+
const at = autoSave.at ? ` @ ${autoSave.at}` : "";
|
|
51
|
+
if (autoSave.status === "sync-failed" || autoSave.status === "error") {
|
|
52
|
+
const reason = firstLine(autoSave.detail);
|
|
53
|
+
const label = autoSave.status === "sync-failed" ? "sync failed" : "auto-save failed";
|
|
54
|
+
return `${label}${reason ? `: ${reason}` : ""}${formatCounts(sync)}${at}`;
|
|
55
|
+
}
|
|
56
|
+
return `last auto-save: ${autoSave.status}${at}`;
|
|
57
|
+
}
|
package/dist/sync/pull.d.ts
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
1
|
+
import { type GitResult, type RunStoreGit } from "./store-merge.js";
|
|
1
2
|
export declare const DEFAULT_PULL_INTERVAL_SECONDS = 0;
|
|
2
3
|
export declare const MIN_PULL_INTERVAL_SECONDS = 30;
|
|
3
4
|
export declare const MAX_PULL_INTERVAL_SECONDS = 86400;
|
|
4
5
|
export declare function parsePullInterval(value: unknown): number | undefined;
|
|
5
6
|
export declare function resolvePullInterval(phrenPath: string, env?: NodeJS.ProcessEnv): number;
|
|
6
7
|
export declare function periodicPullEnabled(phrenPath: string): boolean;
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
output: string;
|
|
10
|
-
error?: string;
|
|
11
|
-
}
|
|
12
|
-
export type RunGit = (cwd: string, args: string[]) => Promise<GitResult>;
|
|
8
|
+
export type RunGit = RunStoreGit;
|
|
9
|
+
export type { GitResult };
|
|
13
10
|
export declare const runPollGit: RunGit;
|
|
11
|
+
interface PollState {
|
|
12
|
+
checkedAt?: number;
|
|
13
|
+
failures?: number;
|
|
14
|
+
status?: string;
|
|
15
|
+
detail?: string;
|
|
16
|
+
}
|
|
14
17
|
export interface PullResult {
|
|
15
18
|
status: "unchanged" | "updated" | "deferred" | "error" | "not-due";
|
|
16
19
|
detail: string;
|
|
17
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* A missing state file is a first check. A corrupt one is logged and moved
|
|
23
|
+
* aside (kept for inspection) so the next check starts clean instead of
|
|
24
|
+
* silently resetting the backoff on every poll.
|
|
25
|
+
*/
|
|
26
|
+
export declare function readPollState(phrenPath: string): PollState;
|
|
18
27
|
/** Shared timestamps + a process lock give all MCP clients one check per store/interval. */
|
|
19
28
|
export declare function pollStore(phrenPath: string, seconds: number, git?: RunGit, now?: number): Promise<PullResult>;
|
|
20
29
|
interface PollingOptions {
|
|
@@ -26,4 +35,3 @@ interface PollingOptions {
|
|
|
26
35
|
export declare function startPullPolling(phrenPath: string, options: PollingOptions): {
|
|
27
36
|
stop: () => Promise<void>;
|
|
28
37
|
};
|
|
29
|
-
export {};
|
package/dist/sync/pull.js
CHANGED
|
@@ -3,14 +3,19 @@ import { execFile } from "child_process";
|
|
|
3
3
|
import * as fs from "fs";
|
|
4
4
|
import * as path from "path";
|
|
5
5
|
import { promisify } from "util";
|
|
6
|
-
import { atomicWriteText, readRootManifest, runtimeFile } from "../phren-paths.js";
|
|
7
|
-
import { readInstallPreferences } from "../init/preferences.js";
|
|
8
|
-
import { resolveManagementCapabilities } from "../init/management-preset.js";
|
|
9
6
|
import { tryFileLock } from "../governance/locks.js";
|
|
10
|
-
import { getNonPrimaryStores } from "../store-registry.js";
|
|
11
7
|
import { updateRuntimeHealth } from "../governance/policy.js";
|
|
8
|
+
import { resolveManagementCapabilities } from "../init/management-preset.js";
|
|
9
|
+
import { readInstallPreferences } from "../init/preferences.js";
|
|
10
|
+
import { atomicWriteText, readRootManifest, runtimeFile } from "../phren-paths.js";
|
|
12
11
|
import { debugLog } from "../shared.js";
|
|
12
|
+
import { getNonPrimaryStores } from "../store-registry.js";
|
|
13
13
|
import { errorMessage } from "../utils.js";
|
|
14
|
+
import { nonInteractiveGitEnv } from "../utils-helpers.js";
|
|
15
|
+
import { activeStoreAuthFailure, authBackoffActive, storeAuthDetail, withStoreAuthBackoff } from "./auth.js";
|
|
16
|
+
import { inProgressGitOperation } from "./git-state.js";
|
|
17
|
+
import { aheadBehind, appendSyncLog, logSyncOutcome } from "./outcome.js";
|
|
18
|
+
import { mergeStoreUpstream } from "./store-merge.js";
|
|
14
19
|
export const DEFAULT_PULL_INTERVAL_SECONDS = 0;
|
|
15
20
|
export const MIN_PULL_INTERVAL_SECONDS = 30;
|
|
16
21
|
export const MAX_PULL_INTERVAL_SECONDS = 86_400;
|
|
@@ -41,7 +46,7 @@ export const runPollGit = async (cwd, args) => {
|
|
|
41
46
|
try {
|
|
42
47
|
const { stdout } = await execAsync("git", args, {
|
|
43
48
|
cwd, encoding: "utf8", timeout: 10_000, maxBuffer: 1024 * 1024,
|
|
44
|
-
env: { ...process.env,
|
|
49
|
+
env: nonInteractiveGitEnv({ ...process.env, GIT_OPTIONAL_LOCKS: "0" }),
|
|
45
50
|
});
|
|
46
51
|
return { ok: true, output: stdout.trim() };
|
|
47
52
|
}
|
|
@@ -49,22 +54,52 @@ export const runPollGit = async (cwd, args) => {
|
|
|
49
54
|
return { ok: false, output: "", error: errorMessage(err) };
|
|
50
55
|
}
|
|
51
56
|
};
|
|
52
|
-
|
|
57
|
+
/**
|
|
58
|
+
* A missing state file is a first check. A corrupt one is logged and moved
|
|
59
|
+
* aside (kept for inspection) so the next check starts clean instead of
|
|
60
|
+
* silently resetting the backoff on every poll.
|
|
61
|
+
*/
|
|
62
|
+
export function readPollState(phrenPath) {
|
|
63
|
+
const file = runtimeFile(phrenPath, "pull-poll.json");
|
|
64
|
+
let text;
|
|
53
65
|
try {
|
|
54
|
-
|
|
55
|
-
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
66
|
+
text = fs.readFileSync(file, "utf8");
|
|
56
67
|
}
|
|
57
|
-
catch {
|
|
68
|
+
catch (err) {
|
|
69
|
+
if (err.code !== "ENOENT")
|
|
70
|
+
logPeriodicSync(phrenPath, `poll state unreadable: ${errorMessage(err).split("\n")[0]}`);
|
|
58
71
|
return {};
|
|
59
72
|
}
|
|
73
|
+
let reason;
|
|
74
|
+
try {
|
|
75
|
+
const value = JSON.parse(text);
|
|
76
|
+
if (value && typeof value === "object" && !Array.isArray(value))
|
|
77
|
+
return value;
|
|
78
|
+
reason = "not a JSON object";
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
reason = errorMessage(err).split("\n")[0];
|
|
82
|
+
}
|
|
83
|
+
const aside = `${file}.corrupt-${Date.now()}`;
|
|
84
|
+
try {
|
|
85
|
+
fs.renameSync(file, aside);
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
debugLog(`poll state rename: ${errorMessage(err)}`);
|
|
89
|
+
}
|
|
90
|
+
logPeriodicSync(phrenPath, `poll state was corrupt (${reason}); moved it to ${path.basename(aside)} and started fresh`);
|
|
91
|
+
return {};
|
|
60
92
|
}
|
|
61
93
|
/** Includes user-owned operations: polling never continues or aborts these. */
|
|
62
94
|
async function worktreeBusy(cwd, git) {
|
|
63
95
|
const dir = await git(cwd, ["rev-parse", "--absolute-git-dir"]);
|
|
64
96
|
if (!dir.ok)
|
|
65
97
|
return true;
|
|
66
|
-
return
|
|
67
|
-
|
|
98
|
+
return inProgressGitOperation(cwd) !== undefined;
|
|
99
|
+
}
|
|
100
|
+
/** The periodic pull writes its decisions to the same log a background sync uses. */
|
|
101
|
+
function logPeriodicSync(phrenPath, detail) {
|
|
102
|
+
appendSyncLog(phrenPath, "periodic-pull", detail);
|
|
68
103
|
}
|
|
69
104
|
/** Shared timestamps + a process lock give all MCP clients one check per store/interval. */
|
|
70
105
|
export async function pollStore(phrenPath, seconds, git = runPollGit, now = Date.now()) {
|
|
@@ -75,23 +110,33 @@ export async function pollStore(phrenPath, seconds, git = runPollGit, now = Date
|
|
|
75
110
|
if (!releasePoll)
|
|
76
111
|
return skipped;
|
|
77
112
|
try {
|
|
113
|
+
const auth = activeStoreAuthFailure(phrenPath);
|
|
114
|
+
if (authBackoffActive(auth, now))
|
|
115
|
+
return { status: "not-due", detail: storeAuthDetail(auth) };
|
|
116
|
+
git = withStoreAuthBackoff(git, () => now);
|
|
78
117
|
const previous = readPollState(phrenPath);
|
|
79
118
|
const failures = Math.min(10, Math.max(0, Number(previous.failures) || 0));
|
|
80
119
|
const delay = Math.min(seconds * 2 ** failures, Math.max(seconds, 1800)) * 1000;
|
|
81
120
|
if (typeof previous.checkedAt === "number" && now >= previous.checkedAt && now - previous.checkedAt < delay)
|
|
82
121
|
return skipped;
|
|
83
|
-
const finish = (result, verifiedRemote = false) => {
|
|
122
|
+
const finish = async (result, verifiedRemote = false) => {
|
|
84
123
|
atomicWriteText(runtimeFile(phrenPath, "pull-poll.json"), JSON.stringify({
|
|
85
124
|
checkedAt: now, failures: result.status === "error" ? failures + 1 : 0, ...result,
|
|
86
125
|
}) + "\n");
|
|
87
126
|
if (verifiedRemote || result.status === "updated" || result.status === "error" || result.status === "deferred") {
|
|
88
127
|
const at = new Date(now).toISOString();
|
|
89
128
|
const ok = result.status === "updated" || result.status === "unchanged";
|
|
129
|
+
const counts = await aheadBehind(phrenPath, git);
|
|
90
130
|
updateRuntimeHealth(phrenPath, { lastSync: {
|
|
91
131
|
lastPullAt: at, lastPullStatus: ok ? "ok" : "error",
|
|
92
132
|
lastPullDetail: result.detail,
|
|
93
133
|
...(ok ? { lastSuccessfulPullAt: at } : {}),
|
|
134
|
+
...(counts ?? {}),
|
|
94
135
|
} });
|
|
136
|
+
// A quiet "unchanged" poll is logged only when it follows a different outcome.
|
|
137
|
+
if (result.status !== "unchanged" || previous.status !== "unchanged") {
|
|
138
|
+
logSyncOutcome(phrenPath, "periodic-pull", { ok, detail: `${result.status}: ${result.detail}`, counts });
|
|
139
|
+
}
|
|
95
140
|
}
|
|
96
141
|
return result;
|
|
97
142
|
};
|
|
@@ -116,7 +161,12 @@ export async function pollStore(phrenPath, seconds, git = runPollGit, now = Date
|
|
|
116
161
|
const head = await git(phrenPath, ["rev-parse", "HEAD"]);
|
|
117
162
|
if (!head.ok)
|
|
118
163
|
return failed("Cannot read the store's current commit.");
|
|
119
|
-
|
|
164
|
+
const status = await git(phrenPath, ["status", "--porcelain"]);
|
|
165
|
+
if (!status.ok)
|
|
166
|
+
return failed("Cannot read the store's working tree state.");
|
|
167
|
+
const remoteUnchanged = remoteHead === head.output;
|
|
168
|
+
// The common poll (clean tree, remote already at HEAD) needs no lock.
|
|
169
|
+
if (remoteUnchanged && !status.output)
|
|
120
170
|
return finish({ status: "unchanged", detail: "Remote is unchanged." }, true);
|
|
121
171
|
const releaseGit = tryFileLock(runtimeFile(phrenPath, "git-op"));
|
|
122
172
|
if (!releaseGit)
|
|
@@ -126,29 +176,23 @@ export async function pollStore(phrenPath, seconds, git = runPollGit, now = Date
|
|
|
126
176
|
if (await worktreeBusy(phrenPath, git))
|
|
127
177
|
return deferred("Periodic pull deferred: a Git operation is in progress.");
|
|
128
178
|
const currentBranch = await git(phrenPath, ["symbolic-ref", "--quiet", "HEAD"]);
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return deferred("Periodic pull deferred: the branch changed or the store has uncommitted edits.");
|
|
132
|
-
}
|
|
133
|
-
const tracking = await git(phrenPath, ["rev-parse", "--verify", trackingRef]);
|
|
134
|
-
if (!tracking.ok || tracking.output !== remoteHead) {
|
|
135
|
-
const fetched = await git(phrenPath, ["fetch", "--quiet", "--no-tags", "--no-recurse-submodules", "--", remote, `${remoteRef}:${trackingRef}`]);
|
|
136
|
-
if (!fetched.ok)
|
|
137
|
-
return failed(`Periodic fetch failed: ${fetched.error}`);
|
|
138
|
-
}
|
|
139
|
-
const target = await git(phrenPath, ["rev-parse", "--verify", trackingRef]);
|
|
140
|
-
if (!target.ok)
|
|
141
|
-
return failed("Cannot read the fetched tracking branch.");
|
|
142
|
-
if ((await git(phrenPath, ["merge-base", "--is-ancestor", target.output, "HEAD"])).ok) {
|
|
143
|
-
return finish({ status: "unchanged", detail: "The store already contains the remote changes." }, true);
|
|
179
|
+
if (!currentBranch.ok || currentBranch.output !== branch.output) {
|
|
180
|
+
return deferred("Periodic pull deferred: the branch changed while checking.");
|
|
144
181
|
}
|
|
145
|
-
|
|
146
|
-
|
|
182
|
+
const merged = await mergeStoreUpstream(phrenPath, {
|
|
183
|
+
git,
|
|
184
|
+
advertisedHead: remoteHead,
|
|
185
|
+
commitMessage: "auto-save phren (periodic pull)",
|
|
186
|
+
});
|
|
187
|
+
if (merged.committedLocalWrites)
|
|
188
|
+
logPeriodicSync(phrenPath, "committed uncommitted store writes before pull");
|
|
189
|
+
if (merged.status === "busy" || merged.status === "conflict") {
|
|
190
|
+
const reason = merged.status === "conflict" ? `local and remote history diverged. ${merged.detail}` : merged.detail;
|
|
191
|
+
return deferred(`Periodic pull deferred: ${reason}`);
|
|
147
192
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
: deferred(`Periodic fast-forward deferred: ${merged.error}`);
|
|
193
|
+
if (merged.status === "error")
|
|
194
|
+
return failed(`Periodic pull failed: ${merged.detail}`);
|
|
195
|
+
return finish({ status: merged.status, detail: merged.detail }, true);
|
|
152
196
|
}
|
|
153
197
|
finally {
|
|
154
198
|
releaseGit();
|
|
@@ -183,12 +227,17 @@ export function startPullPolling(phrenPath, options) {
|
|
|
183
227
|
if (!before.ok)
|
|
184
228
|
return;
|
|
185
229
|
const previous = heads.get(store) ?? before.output;
|
|
230
|
+
// The remote check itself runs once per configured interval
|
|
231
|
+
// (pollStore keeps the shared timestamp); the 5 s tick only notices
|
|
232
|
+
// another client's pull, so HEAD is read again only after a check.
|
|
233
|
+
let after = before;
|
|
186
234
|
if (Date.now() - startedAt >= seconds * 1000) {
|
|
187
235
|
const result = await pollStore(store, seconds, git);
|
|
188
236
|
if (result.status === "error")
|
|
189
237
|
debugLog(result.detail);
|
|
238
|
+
if (result.status !== "not-due")
|
|
239
|
+
after = await git(store, ["rev-parse", "HEAD"]);
|
|
190
240
|
}
|
|
191
|
-
const after = await git(store, ["rev-parse", "HEAD"]);
|
|
192
241
|
if (!after.ok)
|
|
193
242
|
return;
|
|
194
243
|
if (after.output !== previous)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface GitResult {
|
|
2
|
+
ok: boolean;
|
|
3
|
+
output: string;
|
|
4
|
+
error?: string;
|
|
5
|
+
}
|
|
6
|
+
export type RunStoreGit = (cwd: string, args: string[]) => Promise<GitResult>;
|
|
7
|
+
export type StoreMergeStatus = "unchanged" | "updated" | "conflict" | "busy" | "error";
|
|
8
|
+
export interface StoreMergeResult {
|
|
9
|
+
status: StoreMergeStatus;
|
|
10
|
+
detail: string;
|
|
11
|
+
conflicts?: string[];
|
|
12
|
+
committedLocalWrites?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface StoreMergeOptions {
|
|
15
|
+
git: RunStoreGit;
|
|
16
|
+
/** A head advertised by ls-remote. Lets polling avoid an unnecessary fetch. */
|
|
17
|
+
advertisedHead?: string;
|
|
18
|
+
/** Callers that just made their own commit can skip the auto-save commit. */
|
|
19
|
+
commitLocalWrites?: boolean;
|
|
20
|
+
commitMessage?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Fetch and integrate a store's configured upstream without rebasing local
|
|
24
|
+
* commits. Conflicts in tasks.md (per task id against the merge base), the
|
|
25
|
+
* generated summary and topic blocks, findings and the task archive resolve
|
|
26
|
+
* automatically (see conflict-resolve.ts). Any other conflict aborts the
|
|
27
|
+
* merge and names its files.
|
|
28
|
+
*/
|
|
29
|
+
export declare function mergeStoreUpstream(cwd: string, options: StoreMergeOptions): Promise<StoreMergeResult>;
|