@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,16 @@
|
|
|
1
|
+
export declare const MAX_FILE_RANGE: number;
|
|
2
|
+
export declare function fileContentType(file: string): string;
|
|
3
|
+
export declare function rangeInteger(value: string | null, fallback: number): number;
|
|
4
|
+
/** Roots are selected by the server, never supplied unchecked by the phone.
|
|
5
|
+
* A zero length request checks existence and metadata without reading bytes.
|
|
6
|
+
* Reject every symlink component, matching the repository browser policy. */
|
|
7
|
+
export declare function readFileRange(root: string, requested: string, offset: number, length: number, expectedVersion?: string): Promise<{
|
|
8
|
+
path: string;
|
|
9
|
+
offset: number;
|
|
10
|
+
length: number;
|
|
11
|
+
total: number;
|
|
12
|
+
contentType: string;
|
|
13
|
+
version: string;
|
|
14
|
+
eof: boolean;
|
|
15
|
+
data: string;
|
|
16
|
+
}>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { lstat, open, realpath } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { BridgeError } from "./protocol.js";
|
|
5
|
+
export const MAX_FILE_RANGE = 4 * 1024 * 1024;
|
|
6
|
+
const types = {
|
|
7
|
+
mp4: "video/mp4", m4v: "video/mp4", mov: "video/quicktime", webm: "video/webm",
|
|
8
|
+
mp3: "audio/mpeg", m4a: "audio/mp4", aac: "audio/aac", wav: "audio/wav", aiff: "audio/aiff", flac: "audio/flac", ogg: "audio/ogg",
|
|
9
|
+
pdf: "application/pdf", json: "application/json", csv: "text/csv", md: "text/markdown", markdown: "text/markdown",
|
|
10
|
+
png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp",
|
|
11
|
+
heic: "image/heic", heif: "image/heif", tif: "image/tiff", tiff: "image/tiff", bmp: "image/bmp", svg: "image/svg+xml",
|
|
12
|
+
txt: "text/plain", log: "text/plain", yaml: "text/yaml", yml: "text/yaml", xml: "text/xml",
|
|
13
|
+
};
|
|
14
|
+
export function fileContentType(file) {
|
|
15
|
+
const ext = path.extname(file).slice(1).toLowerCase();
|
|
16
|
+
return types[ext] ?? (/^(ts|tsx|js|jsx|swift|py|rs|go|rb|sh|bash|zsh|c|h|cpp|hpp|cs|java|kt|html|css|scss|sql|toml|ini)$/.test(ext)
|
|
17
|
+
? "text/plain" : "application/octet-stream");
|
|
18
|
+
}
|
|
19
|
+
function version(stat) {
|
|
20
|
+
return `${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeMs}:${stat.ctimeMs}`;
|
|
21
|
+
}
|
|
22
|
+
export function rangeInteger(value, fallback) {
|
|
23
|
+
if (value === null)
|
|
24
|
+
return fallback;
|
|
25
|
+
if (!/^\d+$/.test(value) || !Number.isSafeInteger(Number(value)))
|
|
26
|
+
throw new BridgeError(400, "Invalid file range.");
|
|
27
|
+
return Number(value);
|
|
28
|
+
}
|
|
29
|
+
/** Roots are selected by the server, never supplied unchecked by the phone.
|
|
30
|
+
* A zero length request checks existence and metadata without reading bytes.
|
|
31
|
+
* Reject every symlink component, matching the repository browser policy. */
|
|
32
|
+
export async function readFileRange(root, requested, offset, length, expectedVersion) {
|
|
33
|
+
if (!requested || requested.length > 4096 || /[\x00-\x1f\x7f\\]/.test(requested)
|
|
34
|
+
|| requested.split("/").some(part => part === ".." || part === ".git"))
|
|
35
|
+
throw new BridgeError(400, "Invalid file path.");
|
|
36
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(length) || length < 0 || length > MAX_FILE_RANGE) {
|
|
37
|
+
throw new BridgeError(400, "File ranges must be between 0 and 4 MiB.");
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const base = await realpath(root);
|
|
41
|
+
const relative = path.isAbsolute(requested) ? path.relative(base, requested) : requested;
|
|
42
|
+
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
|
|
43
|
+
throw new BridgeError(403, "This file is outside the allowed folder.");
|
|
44
|
+
let file = base;
|
|
45
|
+
for (const part of relative.split(path.sep).filter(part => part && part !== ".")) {
|
|
46
|
+
file = path.join(file, part);
|
|
47
|
+
if ((await lstat(file)).isSymbolicLink())
|
|
48
|
+
throw new BridgeError(403, "Symbolic links cannot be opened.");
|
|
49
|
+
}
|
|
50
|
+
const resolved = await realpath(file);
|
|
51
|
+
if (!resolved.startsWith(base + path.sep))
|
|
52
|
+
throw new BridgeError(403, "This file is outside the allowed folder.");
|
|
53
|
+
const metadata = await lstat(resolved);
|
|
54
|
+
if (!metadata.isFile())
|
|
55
|
+
throw new BridgeError(400, "Only regular files can be opened.");
|
|
56
|
+
const handle = await open(resolved, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
57
|
+
try {
|
|
58
|
+
const current = await handle.stat();
|
|
59
|
+
// Recheck the name after opening, including parent directory replacements.
|
|
60
|
+
if (!current.isFile() || version(current) !== version(metadata) || await realpath(file) !== resolved) {
|
|
61
|
+
throw new BridgeError(409, "The file changed. Open it again.");
|
|
62
|
+
}
|
|
63
|
+
const revision = version(current);
|
|
64
|
+
if (expectedVersion !== undefined && expectedVersion !== revision)
|
|
65
|
+
throw new BridgeError(409, "The file changed. Open it again.");
|
|
66
|
+
if (offset > current.size)
|
|
67
|
+
throw new BridgeError(416, "The offset is past the end of this file.");
|
|
68
|
+
const bytes = Buffer.alloc(Math.min(length, current.size - offset));
|
|
69
|
+
let read = 0;
|
|
70
|
+
while (read < bytes.length) {
|
|
71
|
+
const next = await handle.read(bytes, read, bytes.length - read, offset + read);
|
|
72
|
+
if (!next.bytesRead)
|
|
73
|
+
break;
|
|
74
|
+
read += next.bytesRead;
|
|
75
|
+
}
|
|
76
|
+
if (version(await handle.stat()) !== revision || read !== bytes.length)
|
|
77
|
+
throw new BridgeError(409, "The file changed. Open it again.");
|
|
78
|
+
return { path: requested, offset, length: read, total: current.size, contentType: fileContentType(file),
|
|
79
|
+
version: revision, eof: offset + read === current.size, data: bytes.toString("base64") };
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
await handle.close();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (["ENOENT", "ENOTDIR"].includes(error.code ?? ""))
|
|
87
|
+
throw new BridgeError(404, "This file is no longer available.");
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Read-only browsing beneath a root selected and verified by the server. */
|
|
2
|
+
export declare function browseFiles(root: string, relative: string): Promise<{
|
|
3
|
+
path: string;
|
|
4
|
+
kind: string;
|
|
5
|
+
truncated: boolean;
|
|
6
|
+
entries: {
|
|
7
|
+
name: string;
|
|
8
|
+
path: string;
|
|
9
|
+
kind: string;
|
|
10
|
+
}[];
|
|
11
|
+
size?: undefined;
|
|
12
|
+
data?: undefined;
|
|
13
|
+
} | {
|
|
14
|
+
truncated?: undefined;
|
|
15
|
+
entries?: undefined;
|
|
16
|
+
path: string;
|
|
17
|
+
kind: string;
|
|
18
|
+
size: number;
|
|
19
|
+
data: string;
|
|
20
|
+
}>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { lstat, open, readdir, realpath } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { BridgeError } from "./protocol.js";
|
|
5
|
+
const MAX_BYTES = 2_097_152;
|
|
6
|
+
/** Read-only browsing beneath a root selected and verified by the server. */
|
|
7
|
+
export async function browseFiles(root, relative) {
|
|
8
|
+
if (relative.length > 4096 || relative.includes("\0") || relative.includes("\\") || path.isAbsolute(relative)
|
|
9
|
+
|| relative.split("/").some(part => part === ".." || part === ".git")) {
|
|
10
|
+
throw new BridgeError(400, "Invalid repository path.");
|
|
11
|
+
}
|
|
12
|
+
const base = await realpath(root);
|
|
13
|
+
let file = base;
|
|
14
|
+
for (const part of relative.split("/").filter(part => part && part !== ".")) {
|
|
15
|
+
file = path.join(file, part);
|
|
16
|
+
if ((await lstat(file)).isSymbolicLink())
|
|
17
|
+
throw new BridgeError(403, "Symbolic links cannot be browsed.");
|
|
18
|
+
}
|
|
19
|
+
const resolved = await realpath(file);
|
|
20
|
+
if (resolved !== base && !resolved.startsWith(base + path.sep))
|
|
21
|
+
throw new BridgeError(403, "The path is outside this repository.");
|
|
22
|
+
const metadata = await lstat(resolved);
|
|
23
|
+
if (metadata.isDirectory()) {
|
|
24
|
+
const entries = (await readdir(resolved, { withFileTypes: true }))
|
|
25
|
+
.filter(entry => entry.name !== ".git" && (entry.isDirectory() || entry.isFile()))
|
|
26
|
+
.sort((a, b) => Number(b.isDirectory()) - Number(a.isDirectory()) || a.name.localeCompare(b.name));
|
|
27
|
+
return { path: relative, kind: "directory", truncated: entries.length > 500,
|
|
28
|
+
entries: entries.slice(0, 500).map(entry => ({ name: entry.name, path: path.posix.join(relative, entry.name), kind: entry.isDirectory() ? "directory" : "file" })) };
|
|
29
|
+
}
|
|
30
|
+
if (!metadata.isFile())
|
|
31
|
+
throw new BridgeError(400, "Only regular files can be viewed.");
|
|
32
|
+
const handle = await open(resolved, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
33
|
+
try {
|
|
34
|
+
const current = await handle.stat();
|
|
35
|
+
if (!current.isFile() || current.ino !== metadata.ino || current.dev !== metadata.dev)
|
|
36
|
+
throw new BridgeError(409, "The file changed. Refresh and try again.");
|
|
37
|
+
if (current.size > MAX_BYTES)
|
|
38
|
+
throw new BridgeError(413, "Choose a file smaller than 2 MB.");
|
|
39
|
+
const bytes = Buffer.alloc(MAX_BYTES + 1);
|
|
40
|
+
let size = 0;
|
|
41
|
+
while (size < bytes.length) {
|
|
42
|
+
const result = await handle.read(bytes, size, bytes.length - size, size);
|
|
43
|
+
if (!result.bytesRead)
|
|
44
|
+
break;
|
|
45
|
+
size += result.bytesRead;
|
|
46
|
+
}
|
|
47
|
+
if (size > MAX_BYTES)
|
|
48
|
+
throw new BridgeError(413, "Choose a file smaller than 2 MB.");
|
|
49
|
+
return { path: relative, kind: "file", size, data: bytes.subarray(0, size).toString("base64") };
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
await handle.close();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Json } from "./protocol.js";
|
|
2
|
+
interface Run {
|
|
3
|
+
code: number | null;
|
|
4
|
+
output: string;
|
|
5
|
+
missing: boolean;
|
|
6
|
+
timedOut: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** One process with stdout and stderr interleaved in arrival order, bounded,
|
|
9
|
+
* never attached to a terminal and never prompting. */
|
|
10
|
+
export declare function runCombined(command: string, args: string[], cwd: string, timeout: number): Promise<Run>;
|
|
11
|
+
/** `git commit` of the index only: no `-a`, no `--no-verify`, no amend. A
|
|
12
|
+
* hook's refusal comes back as `{ ok: false, output }`, exactly as printed. */
|
|
13
|
+
export declare function gitCommit(cwd: string, message: unknown): Promise<Json>;
|
|
14
|
+
/** The current branch to its upstream, or to `origin` with the upstream set.
|
|
15
|
+
* Never forced: no `--force`, no `+` refspec, no `--no-verify`. The default
|
|
16
|
+
* branch needs the phone's explicit `confirmDefault`. */
|
|
17
|
+
export declare function gitPush(cwd: string, confirmDefault: unknown): Promise<Json>;
|
|
18
|
+
/** `gh pr create --fill` for the current branch. A missing or signed-out gh
|
|
19
|
+
* is `{ ok: false, reason: "missing" | "auth" }`; gh's own refusal is
|
|
20
|
+
* `reason: "failed"` with its output. A pull request that already exists for
|
|
21
|
+
* the branch comes back as success with `existing: true`. */
|
|
22
|
+
export declare function gitPullRequest(cwd: string, draft: unknown): Promise<Json>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { nonInteractiveGitEnv } from "../utils-helpers.js";
|
|
3
|
+
import { BridgeError } from "./protocol.js";
|
|
4
|
+
import { git, gitRoot } from "./projects.js";
|
|
5
|
+
import { countGit } from "./metrics.js";
|
|
6
|
+
import { defaultBranch, invalidateTree } from "./git.js";
|
|
7
|
+
/** Finishing a session from the phone: commit what is staged, push the
|
|
8
|
+
* current branch, open a pull request. Each acts on the repository the
|
|
9
|
+
* existing /v1/git/* routes resolve (the pane's, a child's worktree, or a
|
|
10
|
+
* listed worktree id). A refusal from Git itself (a hook, a rejected push,
|
|
11
|
+
* gh's own error) is a normal answer carrying the output verbatim, since the
|
|
12
|
+
* phone's error channel flattens and truncates text. */
|
|
13
|
+
const MAX_OUTPUT = 65_536;
|
|
14
|
+
const COMMIT_TIMEOUT_MS = 110_000;
|
|
15
|
+
const PUSH_TIMEOUT_MS = 110_000;
|
|
16
|
+
const PR_TIMEOUT_MS = 60_000;
|
|
17
|
+
const MAX_MESSAGE = 20_000;
|
|
18
|
+
/** One process with stdout and stderr interleaved in arrival order, bounded,
|
|
19
|
+
* never attached to a terminal and never prompting. */
|
|
20
|
+
export function runCombined(command, args, cwd, timeout) {
|
|
21
|
+
return new Promise(resolve => {
|
|
22
|
+
let output = "", truncated = false, timedOut = false, settled = false;
|
|
23
|
+
const finish = (run) => { if (!settled) {
|
|
24
|
+
settled = true;
|
|
25
|
+
resolve(run);
|
|
26
|
+
} };
|
|
27
|
+
const child = spawn(command, args, {
|
|
28
|
+
cwd, stdio: ["ignore", "pipe", "pipe"],
|
|
29
|
+
env: nonInteractiveGitEnv({ ...process.env, GIT_CONFIG_NOSYSTEM: "1", GIT_EDITOR: "true", GH_PROMPT_DISABLED: "1", GH_NO_UPDATE_NOTIFIER: "1", NO_COLOR: "1" }),
|
|
30
|
+
});
|
|
31
|
+
const append = (chunk) => {
|
|
32
|
+
if (truncated)
|
|
33
|
+
return;
|
|
34
|
+
output += chunk.toString("utf8");
|
|
35
|
+
if (output.length > MAX_OUTPUT) {
|
|
36
|
+
output = output.slice(0, MAX_OUTPUT) + "\n[output truncated]";
|
|
37
|
+
truncated = true;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
child.stdout.on("data", append);
|
|
41
|
+
child.stderr.on("data", append);
|
|
42
|
+
const timer = setTimeout(() => { timedOut = true; child.kill("SIGTERM"); }, timeout);
|
|
43
|
+
child.on("error", (error) => {
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
finish({ code: null, output: error.message, missing: error.code === "ENOENT", timedOut: false });
|
|
46
|
+
});
|
|
47
|
+
child.on("close", code => {
|
|
48
|
+
clearTimeout(timer);
|
|
49
|
+
finish({ code, output: output.trimEnd(), missing: false, timedOut });
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async function repository(cwd) {
|
|
54
|
+
const root = await gitRoot(cwd);
|
|
55
|
+
if (!root)
|
|
56
|
+
throw new BridgeError(409, "This pane is not in a Git repository.");
|
|
57
|
+
return root;
|
|
58
|
+
}
|
|
59
|
+
async function currentBranch(root) {
|
|
60
|
+
const branch = (await git(root, "branch", "--show-current")).trim();
|
|
61
|
+
if (!branch)
|
|
62
|
+
throw new BridgeError(409, "HEAD is detached. Check out a branch on the computer first.");
|
|
63
|
+
return branch;
|
|
64
|
+
}
|
|
65
|
+
async function config(root, key) {
|
|
66
|
+
return (await git(root, "config", "--get", key).catch(() => "")).trim() || undefined;
|
|
67
|
+
}
|
|
68
|
+
/** `git commit` of the index only: no `-a`, no `--no-verify`, no amend. A
|
|
69
|
+
* hook's refusal comes back as `{ ok: false, output }`, exactly as printed. */
|
|
70
|
+
export async function gitCommit(cwd, message) {
|
|
71
|
+
if (typeof message !== "string" || !message.trim())
|
|
72
|
+
throw new BridgeError(400, "Write a commit message first.");
|
|
73
|
+
if (message.length > MAX_MESSAGE || message.includes("\0"))
|
|
74
|
+
throw new BridgeError(400, "The commit message is too long.");
|
|
75
|
+
const root = await repository(cwd);
|
|
76
|
+
// `diff --cached --quiet` exits 1 when something is staged.
|
|
77
|
+
const staged = await git(root, "diff", "--cached", "--quiet").then(() => false, () => true);
|
|
78
|
+
if (!staged)
|
|
79
|
+
throw new BridgeError(409, "Nothing is staged. Stage the files to commit first.");
|
|
80
|
+
invalidateTree(root);
|
|
81
|
+
try {
|
|
82
|
+
countGit("projects");
|
|
83
|
+
const run = await runCombined("git", ["-C", root, "--no-pager", "commit", "--quiet", "--cleanup=strip", "-m", message], root, COMMIT_TIMEOUT_MS);
|
|
84
|
+
if (run.code !== 0) {
|
|
85
|
+
return { ok: false, output: run.timedOut ? `${run.output}\n[git commit did not finish within ${COMMIT_TIMEOUT_MS / 1000} seconds]`.trim() : run.output || "git commit failed." };
|
|
86
|
+
}
|
|
87
|
+
const [sha, short, subject] = (await git(root, "log", "-1", "--format=%H%x00%h%x00%s")).trim().split("\0");
|
|
88
|
+
return { ok: true, sha, short, subject, branch: (await git(root, "branch", "--show-current")).trim(), ...(run.output ? { output: run.output } : {}) };
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
invalidateTree(root);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** The current branch to its upstream, or to `origin` with the upstream set.
|
|
95
|
+
* Never forced: no `--force`, no `+` refspec, no `--no-verify`. The default
|
|
96
|
+
* branch needs the phone's explicit `confirmDefault`. */
|
|
97
|
+
export async function gitPush(cwd, confirmDefault) {
|
|
98
|
+
const root = await repository(cwd);
|
|
99
|
+
const branch = await currentBranch(root);
|
|
100
|
+
const upstreamRemote = await config(root, `branch.${branch}.remote`);
|
|
101
|
+
const merge = await config(root, `branch.${branch}.merge`);
|
|
102
|
+
const tracked = !!upstreamRemote && upstreamRemote !== "." && !!merge?.startsWith("refs/heads/");
|
|
103
|
+
const remote = tracked ? upstreamRemote : "origin";
|
|
104
|
+
const remotes = (await git(root, "remote").catch(() => "")).split("\n").map(line => line.trim()).filter(Boolean);
|
|
105
|
+
if (!remotes.includes(remote)) {
|
|
106
|
+
throw new BridgeError(409, tracked ? `The upstream remote ${remote} is not configured.` : "This branch has no upstream and the repository has no origin remote.");
|
|
107
|
+
}
|
|
108
|
+
const destination = tracked ? merge.slice("refs/heads/".length) : branch;
|
|
109
|
+
const fallback = await defaultBranch(root, remote);
|
|
110
|
+
if ((destination === fallback || branch === fallback) && confirmDefault !== true) {
|
|
111
|
+
throw new BridgeError(409, `${branch} is the default branch. Confirm to push it.`, { defaultBranch: fallback });
|
|
112
|
+
}
|
|
113
|
+
const args = ["-C", root, "--no-pager", "push", "--porcelain", ...(tracked ? [] : ["--set-upstream"]), remote, `refs/heads/${branch}:refs/heads/${destination}`];
|
|
114
|
+
countGit("projects");
|
|
115
|
+
const run = await runCombined("git", args, root, PUSH_TIMEOUT_MS);
|
|
116
|
+
invalidateTree(root);
|
|
117
|
+
if (run.code !== 0) {
|
|
118
|
+
return { ok: false, output: run.timedOut ? `${run.output}\n[git push did not finish within ${PUSH_TIMEOUT_MS / 1000} seconds]`.trim() : run.output || "git push failed." };
|
|
119
|
+
}
|
|
120
|
+
return { ok: true, branch, remote, upstream: `${remote}/${destination}`, setUpstream: !tracked, output: run.output };
|
|
121
|
+
}
|
|
122
|
+
const PULL_URL = /https:\/\/[^\s"'<>]+\/pull\/\d+/;
|
|
123
|
+
/** `gh pr create --fill` for the current branch. A missing or signed-out gh
|
|
124
|
+
* is `{ ok: false, reason: "missing" | "auth" }`; gh's own refusal is
|
|
125
|
+
* `reason: "failed"` with its output. A pull request that already exists for
|
|
126
|
+
* the branch comes back as success with `existing: true`. */
|
|
127
|
+
export async function gitPullRequest(cwd, draft) {
|
|
128
|
+
const root = await repository(cwd);
|
|
129
|
+
const branch = await currentBranch(root);
|
|
130
|
+
const version = await runCombined("gh", ["--version"], root, 10_000);
|
|
131
|
+
if (version.missing || version.code !== 0) {
|
|
132
|
+
return { ok: false, reason: "missing", message: "The GitHub CLI (gh) is not installed on this computer. Install it, then run gh auth login there." };
|
|
133
|
+
}
|
|
134
|
+
const auth = await runCombined("gh", ["auth", "status"], root, 15_000);
|
|
135
|
+
if (auth.code !== 0) {
|
|
136
|
+
return { ok: false, reason: "auth", message: "The GitHub CLI is not signed in on this computer. Run gh auth login there.", output: auth.output };
|
|
137
|
+
}
|
|
138
|
+
const run = await runCombined("gh", ["pr", "create", "--fill", "--head", branch, ...(draft === true ? ["--draft"] : [])], root, PR_TIMEOUT_MS);
|
|
139
|
+
const url = PULL_URL.exec(run.output)?.[0];
|
|
140
|
+
if (run.code === 0 && url)
|
|
141
|
+
return { ok: true, url, branch, draft: draft === true };
|
|
142
|
+
if (url && /already exists/i.test(run.output))
|
|
143
|
+
return { ok: true, url, branch, existing: true };
|
|
144
|
+
return { ok: false, reason: "failed", output: run.timedOut ? `${run.output}\n[gh did not finish within ${PR_TIMEOUT_MS / 1000} seconds]`.trim() : run.output || "gh pr create failed." };
|
|
145
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Json } from "./protocol.js";
|
|
2
|
+
export declare const worktreeIdPattern: RegExp;
|
|
3
|
+
/** Someone known to be working in a worktree: a fan-out job from its
|
|
4
|
+
* manifest, or a sub-agent whose own transcript runs there. */
|
|
5
|
+
export interface WorktreeWorker {
|
|
6
|
+
/** The worker's working directory; a worktree is matched when this is it or inside it. */
|
|
7
|
+
cwd: string;
|
|
8
|
+
label: string;
|
|
9
|
+
provider: string;
|
|
10
|
+
/** This conversation's public child id, when the worker is one of its agents. */
|
|
11
|
+
child?: string;
|
|
12
|
+
state?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function worktreeId(abs: string): string;
|
|
15
|
+
/** The pane repository's other worktrees with branch, HEAD, commits ahead of
|
|
16
|
+
* and behind this pane's HEAD, uncommitted file count and, when known, the
|
|
17
|
+
* worker editing there. */
|
|
18
|
+
export declare function gitWorktrees(cwd: string, workers?: WorktreeWorker[]): Promise<Json>;
|
|
19
|
+
/** The checkout for one listed worktree id of the pane's repository, or a
|
|
20
|
+
* 404; never a path the phone supplied. */
|
|
21
|
+
export declare function resolveWorktree(cwd: string, id: unknown): Promise<string>;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { realpath, stat } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { BridgeError } from "./protocol.js";
|
|
6
|
+
import { git, gitRoot } from "./projects.js";
|
|
7
|
+
/** Workers edit in their own worktrees, so the pane's repository diff never
|
|
8
|
+
* shows their work. These are the repository's other worktrees, exactly as
|
|
9
|
+
* `git worktree list` reports them, each addressed by an opaque id the phone
|
|
10
|
+
* sends back. A path the phone names is never trusted: an id resolves only to
|
|
11
|
+
* a worktree the listing reports for this pane's repository. */
|
|
12
|
+
const MAX_WORKTREES = 64;
|
|
13
|
+
export const worktreeIdPattern = /^[a-f0-9]{32}$/;
|
|
14
|
+
export function worktreeId(abs) {
|
|
15
|
+
return createHash("sha256").update(`worktree\0${abs}`).digest("hex").slice(0, 32);
|
|
16
|
+
}
|
|
17
|
+
/** `git worktree list --porcelain`: one block per worktree. Bare entries and
|
|
18
|
+
* worktrees missing on disk are left out; nothing there can be shown. */
|
|
19
|
+
async function listed(root) {
|
|
20
|
+
const output = await git(root, "worktree", "list", "--porcelain");
|
|
21
|
+
const blocks = [];
|
|
22
|
+
let current;
|
|
23
|
+
for (const line of output.split("\n")) {
|
|
24
|
+
if (!line) {
|
|
25
|
+
current = undefined;
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const space = line.indexOf(" ");
|
|
29
|
+
const key = space < 0 ? line : line.slice(0, space), value = space < 0 ? "" : line.slice(space + 1);
|
|
30
|
+
if (key === "worktree") {
|
|
31
|
+
current = { worktree: value };
|
|
32
|
+
blocks.push(current);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (current)
|
|
36
|
+
current[key] = value;
|
|
37
|
+
}
|
|
38
|
+
const worktrees = [];
|
|
39
|
+
let main;
|
|
40
|
+
for (const [index, block] of blocks.entries()) {
|
|
41
|
+
if (block.bare !== undefined || block.prunable !== undefined)
|
|
42
|
+
continue;
|
|
43
|
+
const abs = await realpath(block.worktree).catch(() => undefined);
|
|
44
|
+
if (!abs || !(await stat(abs).catch(() => undefined))?.isDirectory())
|
|
45
|
+
continue;
|
|
46
|
+
if (index === 0)
|
|
47
|
+
main = abs;
|
|
48
|
+
const branch = block.branch?.startsWith("refs/heads/") ? block.branch.slice("refs/heads/".length) : null;
|
|
49
|
+
worktrees.push({ abs, head: block.HEAD ?? "", branch, locked: block.locked !== undefined });
|
|
50
|
+
if (worktrees.length >= MAX_WORKTREES)
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
return { main, worktrees };
|
|
54
|
+
}
|
|
55
|
+
/** Relative to the repository when the worktree lives inside it (the usual
|
|
56
|
+
* `.claude/worktrees/agent-*`), otherwise with the home folder shortened. */
|
|
57
|
+
function displayPath(abs, roots) {
|
|
58
|
+
for (const root of roots) {
|
|
59
|
+
const rel = path.relative(root, abs);
|
|
60
|
+
if (rel && !rel.startsWith("..") && !path.isAbsolute(rel))
|
|
61
|
+
return rel.split(path.sep).join("/");
|
|
62
|
+
}
|
|
63
|
+
const home = homedir();
|
|
64
|
+
return abs === home ? "~" : abs.startsWith(home + path.sep) ? "~/" + path.relative(home, abs).split(path.sep).join("/") : abs;
|
|
65
|
+
}
|
|
66
|
+
function uncommitted(status) {
|
|
67
|
+
const tokens = status.split("\0");
|
|
68
|
+
let count = 0;
|
|
69
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
70
|
+
const record = tokens[index];
|
|
71
|
+
if (!record)
|
|
72
|
+
continue;
|
|
73
|
+
count++;
|
|
74
|
+
if (/[RC]/.test(record.slice(0, 2)))
|
|
75
|
+
index++;
|
|
76
|
+
}
|
|
77
|
+
return count;
|
|
78
|
+
}
|
|
79
|
+
async function workerFor(abs, workers, all = []) {
|
|
80
|
+
// The primary checkout holds `.claude/worktrees/*`: a worker in one of those
|
|
81
|
+
// belongs to that worktree, not to the checkout around it.
|
|
82
|
+
const nested = all.filter(other => other.startsWith(abs + path.sep));
|
|
83
|
+
let best;
|
|
84
|
+
for (const worker of workers) {
|
|
85
|
+
const cwd = await realpath(worker.cwd).catch(() => path.resolve(worker.cwd));
|
|
86
|
+
if (cwd !== abs && !cwd.startsWith(abs + path.sep))
|
|
87
|
+
continue;
|
|
88
|
+
if (nested.some(inner => cwd === inner || cwd.startsWith(inner + path.sep)))
|
|
89
|
+
continue;
|
|
90
|
+
// An exact checkout beats a directory inside it; this conversation's own
|
|
91
|
+
// agent beats a manifest that only names the checkout.
|
|
92
|
+
const score = (cwd === abs ? 2 : 0) + (worker.child ? 1 : 0);
|
|
93
|
+
if (!best || score > best.score)
|
|
94
|
+
best = { worker, score };
|
|
95
|
+
}
|
|
96
|
+
return best?.worker;
|
|
97
|
+
}
|
|
98
|
+
/** The pane repository's other worktrees with branch, HEAD, commits ahead of
|
|
99
|
+
* and behind this pane's HEAD, uncommitted file count and, when known, the
|
|
100
|
+
* worker editing there. */
|
|
101
|
+
export async function gitWorktrees(cwd, workers = []) {
|
|
102
|
+
const root = await gitRoot(cwd);
|
|
103
|
+
if (!root)
|
|
104
|
+
throw new BridgeError(409, "This pane is not in a Git repository.");
|
|
105
|
+
const { main, worktrees } = await listed(root);
|
|
106
|
+
const base = (await git(root, "rev-parse", "--verify", "HEAD").catch(() => "")).trim();
|
|
107
|
+
const roots = [root, ...(main && main !== root ? [main] : [])];
|
|
108
|
+
const others = worktrees.filter(worktree => worktree.abs !== root);
|
|
109
|
+
const rows = await Promise.all(others.map(async (worktree) => {
|
|
110
|
+
let ahead = 0, behind = 0;
|
|
111
|
+
if (base && worktree.head && worktree.head !== base) {
|
|
112
|
+
try {
|
|
113
|
+
const counts = (await git(root, "rev-list", "--left-right", "--count", `${base}...${worktree.head}`)).trim().split(/\s+/);
|
|
114
|
+
behind = Number(counts[0]) || 0;
|
|
115
|
+
ahead = Number(counts[1]) || 0;
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
ahead = 0;
|
|
119
|
+
behind = 0;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const changed = uncommitted(await git(worktree.abs, "status", "--porcelain=v1", "-z", "--untracked-files=normal").catch(() => ""));
|
|
123
|
+
const worker = await workerFor(worktree.abs, workers, worktrees.map(item => item.abs));
|
|
124
|
+
return {
|
|
125
|
+
id: worktreeId(worktree.abs), path: displayPath(worktree.abs, roots), branch: worktree.branch,
|
|
126
|
+
head: worktree.head, ahead, behind, changed,
|
|
127
|
+
...(worktree.abs === main ? { main: true } : {}), ...(worktree.locked ? { locked: true } : {}),
|
|
128
|
+
...(worker ? { worker: { label: worker.label.slice(0, 200), provider: worker.provider,
|
|
129
|
+
...(worker.child ? { child: worker.child } : {}), ...(worker.state ? { state: worker.state } : {}) } } : {}),
|
|
130
|
+
};
|
|
131
|
+
}));
|
|
132
|
+
// Git lists linked worktrees by their admin folder; the phone reads them
|
|
133
|
+
// the repository's own first, then by path.
|
|
134
|
+
const outside = (value) => value.startsWith("/") || value.startsWith("~") ? 1 : 0;
|
|
135
|
+
rows.sort((a, b) => outside(a.path) - outside(b.path) || a.path.localeCompare(b.path));
|
|
136
|
+
return { worktrees: rows };
|
|
137
|
+
}
|
|
138
|
+
/** The checkout for one listed worktree id of the pane's repository, or a
|
|
139
|
+
* 404; never a path the phone supplied. */
|
|
140
|
+
export async function resolveWorktree(cwd, id) {
|
|
141
|
+
if (typeof id !== "string" || !worktreeIdPattern.test(id))
|
|
142
|
+
throw new BridgeError(400, "Invalid worktree.");
|
|
143
|
+
const root = await gitRoot(cwd);
|
|
144
|
+
if (!root)
|
|
145
|
+
throw new BridgeError(409, "This pane is not in a Git repository.");
|
|
146
|
+
const match = (await listed(root)).worktrees.find(worktree => worktreeId(worktree.abs) === id);
|
|
147
|
+
if (!match)
|
|
148
|
+
throw new BridgeError(404, "That worktree is not part of this repository.");
|
|
149
|
+
return match.abs;
|
|
150
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type Json } from "./protocol.js";
|
|
2
|
+
/** The remote's default branch as its `HEAD` records it. With no recorded
|
|
3
|
+
* `HEAD`, `main` and `master` are treated as default so the guard errs on the
|
|
4
|
+
* side of asking. */
|
|
5
|
+
export declare function defaultBranch(root: string, remote?: string): Promise<string | null>;
|
|
6
|
+
export interface GitStatusFile {
|
|
7
|
+
path: string;
|
|
8
|
+
status: string;
|
|
9
|
+
staged: boolean;
|
|
10
|
+
additions: number;
|
|
11
|
+
deletions: number;
|
|
12
|
+
}
|
|
13
|
+
export interface GitStatus {
|
|
14
|
+
branch: string;
|
|
15
|
+
upstream: string | null;
|
|
16
|
+
ahead: number;
|
|
17
|
+
behind: number;
|
|
18
|
+
staged: number;
|
|
19
|
+
unstaged: number;
|
|
20
|
+
untracked: number;
|
|
21
|
+
additions: number;
|
|
22
|
+
deletions: number;
|
|
23
|
+
files: GitStatusFile[];
|
|
24
|
+
/** The branch a push guards: the upstream remote's `HEAD`, else `main`/`master`. */
|
|
25
|
+
defaultBranch: string | null;
|
|
26
|
+
}
|
|
27
|
+
/** Working-tree status, one record per section: a file edited in both the index
|
|
28
|
+
* and the working tree appears once with `staged: true` and once with `false`. */
|
|
29
|
+
export declare function gitStatus(cwd: string): Promise<GitStatus>;
|
|
30
|
+
/** Commits newest first, plus a one-line summary of what is not committed yet. */
|
|
31
|
+
export declare function gitLog(cwd: string, limit?: number, ref?: string): Promise<Json>;
|
|
32
|
+
/** Local branches with their upstream and ahead/behind counts, and remote refs. */
|
|
33
|
+
export declare function gitBranches(cwd: string): Promise<Json>;
|
|
34
|
+
/** A check rollup as one word: any failure fails, anything unfinished is
|
|
35
|
+
* pending, and only finished successes (or skips) pass. No checks is null. */
|
|
36
|
+
export declare function checkRollup(items: unknown): "passing" | "failing" | "pending" | null;
|
|
37
|
+
/** Open pull requests through `gh`, or `{ available: false }` when the tool is
|
|
38
|
+
* missing or not signed in. A failure is a normal answer, never an error.
|
|
39
|
+
* `current` is the checked-out branch's own pull request in any state (open,
|
|
40
|
+
* draft, merged or closed) with its checks, which the session card shows. */
|
|
41
|
+
export declare function gitPulls(cwd: string): Promise<Json>;
|
|
42
|
+
/** Drop the cached tree after a write (commit, push) outside this module. */
|
|
43
|
+
export declare function invalidateTree(root: string): void;
|
|
44
|
+
/** One lazy directory response from a bounded repo/HEAD/status-hash snapshot.
|
|
45
|
+
* Every request still validates the pane's repo and the requested path. A HEAD
|
|
46
|
+
* move invalidates immediately; external working-tree edits age out after 2s.
|
|
47
|
+
* Status refresh and phone mutations invalidate immediately as well. With
|
|
48
|
+
* `ignored`, git-ignored folders and files at that level are added, marked. */
|
|
49
|
+
export declare function gitTree(cwd: string, relPath?: unknown, ignored?: boolean): Promise<Json>;
|
|
50
|
+
export declare function gitStage(cwd: string, paths: unknown): Promise<Json>;
|
|
51
|
+
export declare function gitUnstage(cwd: string, paths: unknown): Promise<Json>;
|
|
52
|
+
/** Destructive: tracked files go back to the index, untracked files are
|
|
53
|
+
* removed. The phone confirms first; no `-d` (directories) and no `-x`. */
|
|
54
|
+
export declare function gitDiscard(cwd: string, paths: unknown): Promise<Json>;
|