worktrust 0.10.38 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +43 -18
  2. package/README.md +55 -2
  3. package/SECURITY.md +25 -0
  4. package/engine-release.json +1 -1
  5. package/log-session.mjs +6 -6
  6. package/package-release.json +1 -1
  7. package/package.json +43 -2
  8. package/remote-adapter-claude-code.mjs +113 -0
  9. package/remote-adapter-codex.mjs +159 -0
  10. package/remote-adapter-terminal.mjs +248 -0
  11. package/remote-approval-bridge.mjs +145 -0
  12. package/remote-approvals.mjs +134 -0
  13. package/remote-attachments.mjs +129 -0
  14. package/remote-cdp.mjs +60 -0
  15. package/remote-changes.mjs +94 -0
  16. package/remote-claude-events.mjs +212 -0
  17. package/remote-claude-progress.mjs +328 -0
  18. package/remote-cli.mjs +330 -0
  19. package/remote-coalesce.mjs +40 -0
  20. package/remote-codex-events.mjs +153 -0
  21. package/remote-daemon.mjs +220 -0
  22. package/remote-inventory.mjs +143 -0
  23. package/remote-journal.mjs +155 -0
  24. package/remote-live-preview.mjs +205 -0
  25. package/remote-pairing.mjs +104 -0
  26. package/remote-pipeline-config.mjs +177 -0
  27. package/remote-pipeline-parsers.mjs +121 -0
  28. package/remote-pipeline.mjs +234 -0
  29. package/remote-policy.mjs +199 -0
  30. package/remote-preview.mjs +146 -0
  31. package/remote-projects.mjs +87 -0
  32. package/remote-protocol.mjs +4010 -0
  33. package/remote-qr.mjs +175 -0
  34. package/remote-release-plans.mjs +88 -0
  35. package/remote-releases.mjs +297 -0
  36. package/remote-run.mjs +189 -0
  37. package/remote-screen-approvals.mjs +79 -0
  38. package/remote-screen-push.mjs +76 -0
  39. package/remote-screen-states.mjs +113 -0
  40. package/remote-service.mjs +101 -0
  41. package/remote-session-list.mjs +48 -0
  42. package/remote-sessions.mjs +337 -0
  43. package/remote-steered.mjs +114 -0
  44. package/remote-steering.mjs +97 -0
  45. package/remote-store.mjs +124 -0
  46. package/remote-terminal-cli.mjs +85 -0
  47. package/remote-terminal-sessions.mjs +291 -0
  48. package/remote-transport.mjs +88 -0
  49. package/worktrust.mjs +11 -4
@@ -0,0 +1,87 @@
1
+ /**
2
+ * ROOTS AND NEW PROJECTS (owner, 2026-11-07: the phone's projects, each with its sessions, and a "New session" sheet
3
+ * whose project may be New, with a storage location). docs/remote-agents/PROTOCOL.md "Projects and sessions".
4
+ *
5
+ * A ROOT is a parent folder the person added at the keyboard (`worktrust remote roots add <dir> [--git-init]`), held to
6
+ * the same rule as a granted project (canonical, every symlink resolved, never the disk's root, the home folder or above
7
+ * it, never a folder of secrets). A phone may ask for ONE new, empty folder inside a root (`project.create`): a single
8
+ * name of [A-Za-z0-9._-] that does not start with a dot and does not exist yet. The computer makes that folder and nothing
9
+ * else (no parents, no files), checks after making it that it sits directly inside the root, grants it as a project, and
10
+ * runs `git init` in it only when the person enabled that for the root. Nothing a phone sends names a path.
11
+ */
12
+ import { execFileSync } from "node:child_process";
13
+ import { createHmac } from "node:crypto";
14
+ import { existsSync, lstatSync, mkdirSync, realpathSync, rmdirSync } from "node:fs";
15
+ import { homedir } from "node:os";
16
+ import { basename, dirname, join, relative, sep } from "node:path";
17
+ import * as P from "./remote-protocol.mjs";
18
+ import { canonicalProject } from "./remote-policy.mjs";
19
+ import { projectIdOf } from "./remote-store.mjs";
20
+
21
+ export const MAX_ROOTS = 20;
22
+ /** The inventory lists 30 projects; a phone creates no more than that (review 2026-11-07, P3-9). */
23
+ export const MAX_PROJECTS = 30;
24
+
25
+ export const rootIdOf = (projectSecret, canonicalPath) =>
26
+ `rot_${P.toBase64Url(createHmac("sha256", Buffer.from(projectSecret)).update(`worktrust-remote/v1/root\n${canonicalPath}`).digest().subarray(0, 16))}`;
27
+
28
+ /** "Desktop/WorkTrust" for a folder under the home folder; else its last two segments. Sealed, for the phone's picker. */
29
+ export function rootLabel(path) {
30
+ const home = homedir();
31
+ const inside = path.startsWith(home + sep) ? relative(home, path) : path.split(sep).filter(Boolean).slice(-2).join("/");
32
+ return (inside || basename(path) || path).split(sep).join("/").slice(0, 256);
33
+ }
34
+
35
+ /** `roots add`: the folder's canonical path under the project rule, or why not. */
36
+ export function checkRoot(dir) {
37
+ const canonical = canonicalProject(dir);
38
+ return canonical.ok ? { ok: true, path: canonical.path } : { ok: false, reason: canonical.reason.replace("granted: grant one project folder", "a root: add the folder your projects go in") };
39
+ }
40
+
41
+ /**
42
+ * The main repository a project's folder is a git WORKTREE of (owner, 2026-11-08: a worktree's sessions sit under its
43
+ * main repository on the phone), or null: the main checkout itself, a folder outside git, or git not answering in time.
44
+ * `git rev-parse --git-common-dir` names the main repository's .git for a worktree and the folder's own for the main
45
+ * checkout. Asked once per folder per run of the daemon; never a path a phone sent.
46
+ */
47
+ const mains = new Map();
48
+ export function worktreeMain(path) {
49
+ if (mains.has(path)) return mains.get(path);
50
+ let main = null;
51
+ try {
52
+ const ask = (flag) => execFileSync("git", ["rev-parse", "--path-format=absolute", flag], { cwd: path, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 5_000, windowsHide: true }).trim();
53
+ const common = ask("--git-common-dir");
54
+ const top = realpathSync(ask("--show-toplevel"));
55
+ if (basename(common) === ".git") { const repo = realpathSync(dirname(common)); if (repo !== top) main = repo; }
56
+ } catch { main = null; }
57
+ mains.set(path, main);
58
+ return main;
59
+ }
60
+
61
+ /**
62
+ * project.create: → { ok: true, project, git } or { ok: false, reason } with a protocol reject reason. `o`: { name,
63
+ * root (a roots.json entry), projectSecret, projects, saveProjects, now }.
64
+ */
65
+ export function createProject(o) {
66
+ if (!P.projectNameOk(o.name)) return { ok: false, reason: "unsupported" };
67
+ if (!o.root) return { ok: false, reason: "not_found" };
68
+ if (o.projects().length >= MAX_PROJECTS) return { ok: false, reason: "busy" };
69
+ const root = checkRoot(o.root.path);
70
+ if (!root.ok || root.path !== o.root.path) return { ok: false, reason: "not_found" }; // moved, gone, or now a link elsewhere
71
+ const target = join(root.path, o.name);
72
+ let exists = existsSync(target);
73
+ try { lstatSync(target); exists = true; } catch { /* free */ }
74
+ if (exists) return { ok: false, reason: "conflict" };
75
+ try { mkdirSync(target, { mode: 0o755 }); } catch { return { ok: false, reason: "conflict" }; } // one folder, no parents
76
+ const made = realpathSync(target);
77
+ if (dirname(made) !== root.path || basename(made) !== o.name) { try { rmdirSync(target); } catch { /* left empty */ } return { ok: false, reason: "conflict" }; }
78
+ let git = false;
79
+ if (o.root.git_init === true) {
80
+ try { execFileSync("git", ["init", "-q"], { cwd: made, stdio: "ignore", timeout: 15_000, windowsHide: true }); git = true; } catch { git = false; }
81
+ }
82
+ const list = o.projects();
83
+ const project_id = projectIdOf(o.projectSecret, made); // the id `worktrust remote projects add` gives the folder
84
+ o.saveProjects([...list.filter((p) => p.project_id !== project_id), { project_id, path: made, added_at: o.now(), root_id: o.root.root_id }]);
85
+ return { ok: true, git, project: { project_id, label: o.name.slice(0, 256), path: made.slice(0, 4096), root_id: o.root.root_id } };
86
+ }
87
+