@netnodeag/kraftwerk 0.36.0 → 0.37.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 (46) hide show
  1. package/README.md +39 -1
  2. package/dist/cli/doctor.js +18 -1
  3. package/dist/cli/init.js +16 -9
  4. package/dist/cli/kraftwerk.js +3 -0
  5. package/dist/cli/projects.d.ts +14 -0
  6. package/dist/cli/projects.js +3 -2
  7. package/dist/cli/repos.d.ts +2 -0
  8. package/dist/cli/repos.js +115 -0
  9. package/dist/cli/routines.d.ts +10 -0
  10. package/dist/cli/routines.js +1 -1
  11. package/dist/cli/ui.js +2 -2
  12. package/dist/config.d.ts +35 -0
  13. package/dist/config.js +68 -1
  14. package/dist/inspector/chat/sessions.js +29 -2
  15. package/dist/inspector/git.d.ts +14 -0
  16. package/dist/inspector/git.js +11 -9
  17. package/dist/inspector/instances.js +7 -7
  18. package/dist/inspector/repos.d.ts +118 -0
  19. package/dist/inspector/repos.js +249 -0
  20. package/dist/inspector/server.js +43 -1
  21. package/dist/inspector/settings.d.ts +7 -1
  22. package/dist/inspector/settings.js +32 -1
  23. package/inspector/dist/assets/{editor-7ImnDC3D.js → editor-vM6JSj5h.js} +3 -3
  24. package/inspector/dist/assets/{index-DbmSSHvr.js → index-BAAjszPI.js} +1 -1
  25. package/inspector/dist/assets/{index-DjRBLY9N.js → index-BKcOp35r.js} +1 -1
  26. package/inspector/dist/assets/index-Ba8VU6Wz.js +217 -0
  27. package/inspector/dist/assets/{index-C3Ob1O7W.js → index-Bb08wK-l.js} +1 -1
  28. package/inspector/dist/assets/{index-Ct_jZSyD.js → index-BrCwDkJZ.js} +1 -1
  29. package/inspector/dist/assets/{index-DKrBjjGQ.js → index-Bs1U0rWW.js} +1 -1
  30. package/inspector/dist/assets/{index-BC7aIJ-h.js → index-C1UXlW32.js} +1 -1
  31. package/inspector/dist/assets/{index-CIjv7yxE.js → index-CQ2XKqmD.js} +1 -1
  32. package/inspector/dist/assets/{index-BuRajvjQ.js → index-CReepirH.js} +1 -1
  33. package/inspector/dist/assets/{index-YZtBJhWD.js → index-CUGfxoYt.js} +1 -1
  34. package/inspector/dist/assets/{index-B_CIhKa4.js → index-CaKmxcD6.js} +1 -1
  35. package/inspector/dist/assets/{index-BZeYO5wT.js → index-CtMGH62C.js} +1 -1
  36. package/inspector/dist/assets/{index-BA7nx4V5.css → index-D4_mLTSH.css} +1 -1
  37. package/inspector/dist/assets/{index-DPBAv_kj.js → index-D_11IZBV.js} +1 -1
  38. package/inspector/dist/assets/{index-CRAwmMfk.js → index-DfZT0IoM.js} +1 -1
  39. package/inspector/dist/assets/{index-DL_bJiOj.js → index-DykDNrYm.js} +1 -1
  40. package/inspector/dist/assets/{index-BIXYBiw7.js → index-MWv5H1oP.js} +1 -1
  41. package/inspector/dist/assets/{index-fWUFhIGR.js → index-oIQqfbwm.js} +1 -1
  42. package/inspector/dist/assets/{index--2I7vpcS.js → index-yGZsomcy.js} +1 -1
  43. package/inspector/dist/index.html +2 -2
  44. package/package.json +1 -1
  45. package/schema/kraftwerk.schema.json +15 -0
  46. package/inspector/dist/assets/index-B2ThRS2S.js +0 -217
@@ -1,7 +1,7 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { realpathSync } from "node:fs";
3
3
  import path from "node:path";
4
- import { resolveProject } from "../config.js";
4
+ import { reposRootFor, resolveProject } from "../config.js";
5
5
  import { ENV_FILE } from "../runner/docker.js";
6
6
  import { getOutputDir, getProjectRoot } from "./context.js";
7
7
  /**
@@ -44,7 +44,7 @@ const DENY = [
44
44
  const DEFAULTS = { remote: "origin", interval: 300, autosync: "pull" };
45
45
  /** Local operations are quick; anything touching the network gets longer. */
46
46
  const LOCAL_TIMEOUT = 15_000;
47
- const NET_TIMEOUT = 60_000;
47
+ export const NET_TIMEOUT = 60_000;
48
48
  const MAX_OUTPUT = 32 * 1024 * 1024;
49
49
  /** Longest diff the screen gets; a dumped database under knowledge/ is not something to read in a browser. */
50
50
  const MAX_DIFF = 512 * 1024;
@@ -70,7 +70,7 @@ const gitEnv = (extra) => ({
70
70
  GIT_OPTIONAL_LOCKS: "0",
71
71
  ...extra,
72
72
  });
73
- function git(args, cwd, timeoutMs = LOCAL_TIMEOUT, env) {
73
+ export function git(args, cwd, timeoutMs = LOCAL_TIMEOUT, env) {
74
74
  return new Promise((resolve) => {
75
75
  let child;
76
76
  try {
@@ -115,7 +115,7 @@ function git(args, cwd, timeoutMs = LOCAL_TIMEOUT, env) {
115
115
  * is left alone: whoever set it owns it.
116
116
  */
117
117
  const sshCache = new Map();
118
- async function sshCommandFor(repoRoot) {
118
+ export async function sshCommandFor(repoRoot) {
119
119
  if (process.env.GIT_SSH_COMMAND)
120
120
  return process.env.GIT_SSH_COMMAND;
121
121
  const hit = sshCache.get(repoRoot);
@@ -127,7 +127,7 @@ async function sshCommandFor(repoRoot) {
127
127
  return cmd;
128
128
  }
129
129
  /** A git call that may touch the network: longer timeout, non-interactive ssh. */
130
- async function gitNet(args, repoRoot) {
130
+ export async function gitNet(args, repoRoot) {
131
131
  return git(args, repoRoot, NET_TIMEOUT, { GIT_SSH_COMMAND: await sshCommandFor(repoRoot) });
132
132
  }
133
133
  /**
@@ -136,7 +136,7 @@ async function gitNet(args, repoRoot) {
136
136
  */
137
137
  const literal = (p) => `:(literal)${p}`;
138
138
  /** Repo root for a directory, or null when it is not inside a git repo. */
139
- async function repoRootFor(dir) {
139
+ export async function repoRootFor(dir) {
140
140
  const r = await git(["rev-parse", "--show-toplevel"], dir);
141
141
  return r.ok ? path.resolve(r.stdout.trim()) : null;
142
142
  }
@@ -220,10 +220,12 @@ function scopeFor({ project, repoRoot }) {
220
220
  project.configPath,
221
221
  ];
222
222
  const mapped = roots.filter((r) => !!r).map(rel).filter((r) => !!r);
223
- const outputRel = rel(getOutputDir());
223
+ // Clones under the repos root are other people's history, never this
224
+ // repo's: excluded like run artifacts, whatever the configured roots say.
225
+ const excluded = [getOutputDir(), reposRootFor(project)].filter((r) => !!r);
224
226
  return {
225
227
  include: [...new Set(mapped.filter((r) => r !== "."))],
226
- exclude: outputRel ? [outputRel] : [],
228
+ exclude: excluded.map(rel).filter((r) => !!r && r !== "."),
227
229
  wholeRepo: mapped.includes("."),
228
230
  };
229
231
  }
@@ -242,7 +244,7 @@ function reasonFor(file, code, scope) {
242
244
  if (DENY.some((re) => re.test(file)))
243
245
  return "never synced (secret or key)";
244
246
  if (scope.exclude.some((e) => within(file, e)))
245
- return "run artifacts, not synced";
247
+ return "run artifacts or repositories, not synced";
246
248
  if (!scope.include.some((r) => within(file, r)))
247
249
  return "outside the workspace paths";
248
250
  if (/[RC]/.test(code))
@@ -3,7 +3,7 @@ import { createHash } from "node:crypto";
3
3
  import fsSync, { promises as fs } from "node:fs";
4
4
  import os from "node:os";
5
5
  import path from "node:path";
6
- import { isDir, resolveProject } from "../config.js";
6
+ import { absolutePath, isDir, resolveProject } from "../config.js";
7
7
  import { selfCommand } from "./self-command.js";
8
8
  /**
9
9
  * Two registries under ~/.kraftwerk, two lifecycles:
@@ -31,7 +31,7 @@ const LOGS_DIR = path.join(HOME, "logs");
31
31
  const selfFile = () => path.join(INSTANCES_DIR, `${process.pid}.json`);
32
32
  let selfPort = null;
33
33
  let selfRoot = null;
34
- const projectKey = (root) => createHash("sha1").update(path.resolve(root)).digest("hex").slice(0, 16);
34
+ const projectKey = (root) => createHash("sha1").update(absolutePath(root)).digest("hex").slice(0, 16);
35
35
  const projectFile = (root) => path.join(PROJECTS_DIR, `${projectKey(root)}.json`);
36
36
  /** ~/… for display. */
37
37
  export const tildify = (p) => {
@@ -42,7 +42,7 @@ export const tildify = (p) => {
42
42
  /** Write this instance's registry file (call once the server listens). */
43
43
  export async function registerInstance(port, root) {
44
44
  selfPort = port;
45
- selfRoot = path.resolve(root);
45
+ selfRoot = absolutePath(root);
46
46
  try {
47
47
  await fs.mkdir(INSTANCES_DIR, { recursive: true });
48
48
  const rec = { pid: process.pid, port, startedAt: new Date().toISOString(), root: selfRoot };
@@ -151,7 +151,7 @@ async function readProject(file) {
151
151
  }
152
152
  /** Upsert the project record for a root (call on every inspector start). */
153
153
  export async function registerProject(root) {
154
- const abs = path.resolve(root);
154
+ const abs = absolutePath(root);
155
155
  const now = new Date().toISOString();
156
156
  try {
157
157
  await fs.mkdir(PROJECTS_DIR, { recursive: true });
@@ -395,7 +395,7 @@ export async function listWorkspacesDetailed() {
395
395
  * straight to it.
396
396
  */
397
397
  export async function startProject(root) {
398
- const abs = path.resolve(root);
398
+ const abs = absolutePath(root);
399
399
  const { name, port, exists } = await describeRoot(abs);
400
400
  if (!exists)
401
401
  return { ok: false, error: `not a project directory (missing or moved): ${abs}` };
@@ -464,12 +464,12 @@ export async function startProject(root) {
464
464
  * that registered no root. Waits up to ~5s for the port to go quiet.
465
465
  */
466
466
  export async function stopProject(target) {
467
- if (target.root && path.resolve(target.root) === selfRoot) {
467
+ if (target.root && absolutePath(target.root) === selfRoot) {
468
468
  return { ok: false, error: "that is this workspace — stop it from its own terminal or pid" };
469
469
  }
470
470
  cache = wsCache = null;
471
471
  const live = await discoverInstances();
472
- const inst = live.find((i) => (target.root && i.root === path.resolve(target.root)) || (target.url && i.url === target.url));
472
+ const inst = live.find((i) => (target.root && i.root === absolutePath(target.root)) || (target.url && i.url === target.url));
473
473
  if (!inst)
474
474
  return { ok: false, error: "not running" };
475
475
  try {
@@ -0,0 +1,118 @@
1
+ import { type Project } from "../config.js";
2
+ /**
3
+ * Repositories: the git clones an agent works on, kept under one root
4
+ * inside the project (`repos.root` in kraftwerk.yml, default repos/). The
5
+ * folder is the registry: whatever has a .git directory directly under the
6
+ * root is a repository, whoever put it there — this module, the CLI, or an
7
+ * agent running plain `git clone`. Everything shown about a clone is read
8
+ * from git on request, so nothing here can go stale.
9
+ *
10
+ * The root lives inside the project on purpose: chat sessions run in the
11
+ * project root, so every harness reaches the clones without extra grants.
12
+ * It must stay out of the workspace git: `kraftwerk init` ignores it, and
13
+ * the workspace sync never stages it.
14
+ *
15
+ * Like git.ts, every call passes an argument array (nothing reaches a
16
+ * shell), never prompts, and times out — a clone from a web UI must fail
17
+ * loudly, not park on a credential prompt nobody sees.
18
+ */
19
+ export interface RepoInfo {
20
+ /** Folder name under the root; the id everywhere. */
21
+ slug: string;
22
+ /** Absolute path of the clone. */
23
+ path: string;
24
+ /** origin url, absent when the clone has no origin. */
25
+ url?: string;
26
+ /** Checked-out branch, or "HEAD" when detached. */
27
+ branch?: string;
28
+ /** Short hash of HEAD. */
29
+ head?: string;
30
+ /** Subject of the HEAD commit. */
31
+ subject?: string;
32
+ /** ISO timestamp of the HEAD commit. */
33
+ committedAt?: string;
34
+ /** Modified, added, deleted or untracked paths (porcelain count). */
35
+ dirty: number;
36
+ /** Commits not on the upstream, or not on any remote when the branch tracks nothing. Absent when git could not tell. */
37
+ ahead?: number;
38
+ /** Commits on the upstream not here. Absent when the branch tracks nothing. */
39
+ behind?: number;
40
+ /** Set when git could not be read for this folder. */
41
+ error?: string;
42
+ }
43
+ export interface ReposView {
44
+ enabled: boolean;
45
+ root?: string;
46
+ repos: RepoInfo[];
47
+ /** Set when the feature is on but unusable, or off. */
48
+ error?: string;
49
+ }
50
+ export interface AddRepoInput {
51
+ url: string;
52
+ name?: string;
53
+ branch?: string;
54
+ /** Shallow clone with this many commits of history (`git clone --depth`). */
55
+ depth?: number;
56
+ }
57
+ /**
58
+ * A clone is the one call here that can legitimately take long: a large
59
+ * repository on a slow link is minutes, not seconds. Prompts are already
60
+ * disabled (git.ts), so a long timeout only ever cuts a transfer that is
61
+ * still running, and the partial clone is deleted when it does.
62
+ */
63
+ export declare const CLONE_TIMEOUT: number;
64
+ export declare function safeRepoSlug(slug: string): string;
65
+ /** "https://github.com/org/repo.git" → "repo"; "git@host:org/repo" → "repo". */
66
+ export declare function slugFromUrl(url: string): string;
67
+ /**
68
+ * The url git gets. `github:org/repo` is the same shorthand `--from`
69
+ * accepts. Anything starting with a dash would be read as an option, and
70
+ * empty is empty.
71
+ */
72
+ export declare function cloneUrl(input: string): string;
73
+ /** Resolve the project and its repos block. `off` when the feature is not on. */
74
+ export declare function openRepos(): Promise<{
75
+ project?: Project;
76
+ root?: string;
77
+ off?: boolean;
78
+ error?: string;
79
+ }>;
80
+ /** Every clone under the root, alphabetically. Folders without .git are skipped. */
81
+ export declare function listRepos(): Promise<ReposView>;
82
+ /**
83
+ * Clone into <root>/<slug>. The folder is claimed with a plain mkdir first:
84
+ * it fails when anything is there already, whatever it is, and two adds of
85
+ * the same name racing each other cannot both win. Only what this call
86
+ * created is deleted when the clone fails.
87
+ */
88
+ export declare function addRepo(input: AddRepoInput): Promise<RepoInfo>;
89
+ /**
90
+ * Fetch, then fast-forward when the clone is behind and clean. Never merges:
91
+ * a diverged or dirty clone is reported, and the agent (or a terminal)
92
+ * sorts it out inside the repo.
93
+ */
94
+ export declare function updateRepo(slug: string): Promise<{
95
+ ok: boolean;
96
+ error?: string;
97
+ off?: boolean;
98
+ repo?: RepoInfo;
99
+ }>;
100
+ /**
101
+ * Delete the clone. Refuses while it holds unpushed or uncommitted work, or
102
+ * while git cannot tell, unless forced: the clone may be the only copy.
103
+ */
104
+ export declare function removeRepo(slug: string, force?: boolean): Promise<{
105
+ ok: boolean;
106
+ error?: string;
107
+ off?: boolean;
108
+ conflict?: boolean;
109
+ }>;
110
+ /**
111
+ * Make sure the root exists and is ignored by the workspace git: a clone
112
+ * inside a tracked tree shows up as an untracked gitlink for everyone.
113
+ * Best effort — a missing .gitignore is created, one that already covers
114
+ * the root is left alone. Runs before every clone as well as when the
115
+ * feature is switched on, so a root enabled by editing kraftwerk.yml is
116
+ * ignored by the time the first clone lands.
117
+ */
118
+ export declare function ensureReposRoot(): Promise<void>;
@@ -0,0 +1,249 @@
1
+ import { promises as fs } from "node:fs";
2
+ import path from "node:path";
3
+ import { gitignoreHas, ignoreEntryFor, reposRootFor, resolveProject } from "../config.js";
4
+ import { getProjectRoot } from "./context.js";
5
+ import { git, gitNet, sshCommandFor } from "./git.js";
6
+ /**
7
+ * A clone is the one call here that can legitimately take long: a large
8
+ * repository on a slow link is minutes, not seconds. Prompts are already
9
+ * disabled (git.ts), so a long timeout only ever cuts a transfer that is
10
+ * still running, and the partial clone is deleted when it does.
11
+ */
12
+ export const CLONE_TIMEOUT = 30 * 60_000;
13
+ const SLUG_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
14
+ export function safeRepoSlug(slug) {
15
+ if (!SLUG_RE.test(slug) || slug === "." || slug === "..")
16
+ throw new Error(`invalid repository name "${slug}"`);
17
+ return slug;
18
+ }
19
+ /** "https://github.com/org/repo.git" → "repo"; "git@host:org/repo" → "repo". */
20
+ export function slugFromUrl(url) {
21
+ const last = url.replace(/[/:]+$/, "").split(/[/:]/).pop() ?? "";
22
+ const slug = last.replace(/\.git$/, "");
23
+ return safeRepoSlug(slug);
24
+ }
25
+ /**
26
+ * The url git gets. `github:org/repo` is the same shorthand `--from`
27
+ * accepts. Anything starting with a dash would be read as an option, and
28
+ * empty is empty.
29
+ */
30
+ export function cloneUrl(input) {
31
+ const url = input.trim();
32
+ if (!url)
33
+ throw new Error("url is required");
34
+ if (url.startsWith("-"))
35
+ throw new Error(`invalid url "${url}"`);
36
+ const gh = /^github:([\w.-]+\/[\w.-]+?)(?:\.git)?\/?$/.exec(url);
37
+ if (gh)
38
+ return `https://github.com/${gh[1]}.git`;
39
+ return url;
40
+ }
41
+ /** Resolve the project and its repos block. `off` when the feature is not on. */
42
+ export async function openRepos() {
43
+ let project;
44
+ try {
45
+ project = await resolveProject(getProjectRoot());
46
+ }
47
+ catch (err) {
48
+ return { error: err.message };
49
+ }
50
+ const root = reposRootFor(project);
51
+ if (!root)
52
+ return { off: true, error: "repositories are off (enable them in settings or add `repos:` to kraftwerk.yml)" };
53
+ return { project, root };
54
+ }
55
+ /**
56
+ * Everything about a clone in three git calls. `status --porcelain=v2
57
+ * --branch` carries the branch, its upstream, ahead/behind and the change
58
+ * list in one spawn; `log` the head; `remote` the origin. Only a branch
59
+ * without upstream costs one more call.
60
+ */
61
+ async function inspect(root, slug) {
62
+ const dir = path.join(root, slug);
63
+ const info = { slug, path: dir, dirty: 0 };
64
+ const [status, log, origin] = await Promise.all([
65
+ git(["status", "--porcelain=v2", "--branch", "--untracked-files=normal"], dir),
66
+ git(["log", "-1", "--format=%h%x00%s%x00%cI"], dir),
67
+ git(["remote", "get-url", "origin"], dir),
68
+ ]);
69
+ if (!status.ok)
70
+ return { ...info, error: status.stderr || "not a git repository" };
71
+ if (origin.ok)
72
+ info.url = origin.stdout.trim();
73
+ if (log.ok) {
74
+ const [head, subject, committedAt] = log.stdout.trim().split("\0");
75
+ info.head = head;
76
+ info.subject = subject;
77
+ info.committedAt = committedAt;
78
+ }
79
+ let hasUpstream = false;
80
+ for (const line of status.stdout.split("\n")) {
81
+ if (!line.trim())
82
+ continue;
83
+ if (!line.startsWith("# ")) {
84
+ info.dirty++;
85
+ continue;
86
+ }
87
+ const [key, ...rest] = line.slice(2).split(" ");
88
+ const value = rest.join(" ");
89
+ if (key === "branch.head")
90
+ info.branch = value === "(detached)" ? "HEAD" : value;
91
+ else if (key === "branch.upstream")
92
+ hasUpstream = true;
93
+ else if (key === "branch.ab") {
94
+ const m = /^\+(\d+) -(\d+)$/.exec(value);
95
+ if (m) {
96
+ info.ahead = Number(m[1]);
97
+ info.behind = Number(m[2]);
98
+ }
99
+ }
100
+ }
101
+ if (!hasUpstream || info.ahead === undefined) {
102
+ // No upstream (a branch made locally, a detached HEAD): count what no
103
+ // remote has, so the remove guard still knows about unpushed work.
104
+ const local = await git(["rev-list", "--count", "HEAD", "--not", "--remotes"], dir);
105
+ if (local.ok && /^\d+$/.test(local.stdout.trim()))
106
+ info.ahead = Number(local.stdout.trim());
107
+ }
108
+ return info;
109
+ }
110
+ /** Every clone under the root, alphabetically. Folders without .git are skipped. */
111
+ export async function listRepos() {
112
+ const opened = await openRepos();
113
+ if (!opened.root)
114
+ return { enabled: false, repos: [], error: opened.error };
115
+ const root = opened.root;
116
+ const entries = await fs.readdir(root, { withFileTypes: true }).catch(() => []);
117
+ const slugs = [];
118
+ for (const e of entries) {
119
+ if (!e.isDirectory() || !SLUG_RE.test(e.name))
120
+ continue;
121
+ if (await fs.stat(path.join(root, e.name, ".git")).catch(() => null))
122
+ slugs.push(e.name);
123
+ }
124
+ slugs.sort((a, b) => a.localeCompare(b));
125
+ const repos = await Promise.all(slugs.map((s) => inspect(root, s)));
126
+ return { enabled: true, root, repos };
127
+ }
128
+ const cloneError = (r) => (r.stderr || r.stdout || "git clone failed").split("\n").filter((l) => l.trim()).slice(-3).join(" ");
129
+ /**
130
+ * Clone into <root>/<slug>. The folder is claimed with a plain mkdir first:
131
+ * it fails when anything is there already, whatever it is, and two adds of
132
+ * the same name racing each other cannot both win. Only what this call
133
+ * created is deleted when the clone fails.
134
+ */
135
+ export async function addRepo(input) {
136
+ const opened = await openRepos();
137
+ if (!opened.root || !opened.project)
138
+ throw new Error(opened.error ?? "repositories are off");
139
+ const url = cloneUrl(input.url);
140
+ const slug = input.name?.trim() ? safeRepoSlug(input.name.trim()) : slugFromUrl(url);
141
+ const branch = input.branch?.trim();
142
+ if (branch && (branch.startsWith("-") || /\s/.test(branch)))
143
+ throw new Error(`invalid branch "${branch}"`);
144
+ if (input.depth !== undefined && (!Number.isInteger(input.depth) || input.depth < 1))
145
+ throw new Error(`invalid depth "${input.depth}"`);
146
+ const dest = path.join(opened.root, slug);
147
+ await prepareRoot(opened.project, opened.root);
148
+ try {
149
+ await fs.mkdir(dest);
150
+ }
151
+ catch (err) {
152
+ if (err.code === "EEXIST")
153
+ throw new Error(`"${slug}" already exists under ${opened.root}`);
154
+ throw err;
155
+ }
156
+ const args = [
157
+ "-c", "protocol.ext.allow=never", "clone",
158
+ ...(branch ? ["--branch", branch] : []),
159
+ ...(input.depth ? ["--depth", String(input.depth)] : []),
160
+ "--", url, dest,
161
+ ];
162
+ const r = await git(args, opened.root, CLONE_TIMEOUT, { GIT_SSH_COMMAND: await sshCommandFor(opened.project.root) });
163
+ if (!r.ok) {
164
+ await fs.rm(dest, { recursive: true, force: true }).catch(() => { });
165
+ throw new Error(cloneError(r));
166
+ }
167
+ return inspect(opened.root, slug);
168
+ }
169
+ /**
170
+ * Fetch, then fast-forward when the clone is behind and clean. Never merges:
171
+ * a diverged or dirty clone is reported, and the agent (or a terminal)
172
+ * sorts it out inside the repo.
173
+ */
174
+ export async function updateRepo(slug) {
175
+ safeRepoSlug(slug);
176
+ const opened = await openRepos();
177
+ if (!opened.root)
178
+ return { ok: false, off: true, error: opened.error };
179
+ const dir = path.join(opened.root, slug);
180
+ if (!(await fs.stat(path.join(dir, ".git")).catch(() => null)))
181
+ return { ok: false, error: `no repository "${slug}"` };
182
+ const fetched = await gitNet(["fetch", "--prune"], dir);
183
+ if (!fetched.ok)
184
+ return { ok: false, error: fetched.stderr || "git fetch failed", repo: await inspect(opened.root, slug) };
185
+ let repo = await inspect(opened.root, slug);
186
+ if (repo.behind && !repo.ahead && repo.dirty === 0) {
187
+ const pulled = await git(["merge", "--ff-only", "@{upstream}"], dir);
188
+ if (!pulled.ok)
189
+ return { ok: false, error: pulled.stderr || "fast-forward failed", repo };
190
+ repo = await inspect(opened.root, slug);
191
+ }
192
+ else if (repo.behind && (repo.ahead || repo.dirty)) {
193
+ return { ok: true, error: repo.ahead ? "diverged from upstream, not fast-forwarded" : "local changes, not fast-forwarded", repo };
194
+ }
195
+ return { ok: true, repo };
196
+ }
197
+ /**
198
+ * Delete the clone. Refuses while it holds unpushed or uncommitted work, or
199
+ * while git cannot tell, unless forced: the clone may be the only copy.
200
+ */
201
+ export async function removeRepo(slug, force = false) {
202
+ safeRepoSlug(slug);
203
+ const opened = await openRepos();
204
+ if (!opened.root)
205
+ return { ok: false, off: true, error: opened.error };
206
+ const dir = path.join(opened.root, slug);
207
+ if (!(await fs.stat(path.join(dir, ".git")).catch(() => null)))
208
+ return { ok: false, error: `no repository "${slug}"` };
209
+ if (!force) {
210
+ const repo = await inspect(opened.root, slug);
211
+ if (repo.error)
212
+ return { ok: false, conflict: true, error: `"${slug}" cannot be read (${repo.error}); remove it with --force` };
213
+ if (repo.dirty)
214
+ return { ok: false, conflict: true, error: `"${slug}" has ${repo.dirty} uncommitted change(s)` };
215
+ if (repo.ahead === undefined)
216
+ return { ok: false, conflict: true, error: `"${slug}": cannot tell whether it has unpushed commits; remove it with --force` };
217
+ if (repo.ahead)
218
+ return { ok: false, conflict: true, error: `"${slug}" has ${repo.ahead} unpushed commit(s)` };
219
+ }
220
+ await fs.rm(dir, { recursive: true, force: true });
221
+ return { ok: true };
222
+ }
223
+ /**
224
+ * Make sure the root exists and is ignored by the workspace git: a clone
225
+ * inside a tracked tree shows up as an untracked gitlink for everyone.
226
+ * Best effort — a missing .gitignore is created, one that already covers
227
+ * the root is left alone. Runs before every clone as well as when the
228
+ * feature is switched on, so a root enabled by editing kraftwerk.yml is
229
+ * ignored by the time the first clone lands.
230
+ */
231
+ export async function ensureReposRoot() {
232
+ const project = await resolveProject(getProjectRoot());
233
+ const root = reposRootFor(project);
234
+ if (!root)
235
+ return;
236
+ await prepareRoot(project, root);
237
+ }
238
+ async function prepareRoot(project, root) {
239
+ await fs.mkdir(root, { recursive: true });
240
+ const entry = ignoreEntryFor(project.root, root);
241
+ if (!entry)
242
+ return;
243
+ const ignorePath = path.join(project.root, ".gitignore");
244
+ const existing = await fs.readFile(ignorePath, "utf8").catch(() => null);
245
+ if (existing !== null && gitignoreHas(existing, entry))
246
+ return;
247
+ const line = `${entry}/\n`;
248
+ await fs.writeFile(ignorePath, existing === null ? line : `${existing}${existing.endsWith("\n") ? "" : "\n"}${line}`).catch(() => { });
249
+ }
@@ -13,6 +13,7 @@ import { deleteAgentSkill, getSkill, listAgentSkills, listSkills, readSkill, sav
13
13
  import { discoverWorkspaces, forgetProject, listWorkspacesDetailed, markProjectStopped, registerInstance, registerProject, startProject, stopProject, tildify, unregisterInstance, } from "./instances.js";
14
14
  import { gitCommit, gitDiff, gitFetch, gitPull, gitPush, gitStatus, startGitSync, } from "./git.js";
15
15
  import { getSettings, saveSettings } from "./settings.js";
16
+ import { addRepo, listRepos, openRepos, removeRepo, updateRepo } from "./repos.js";
16
17
  import { searchAgents } from "./search.js";
17
18
  import { deleteRoutine, routineStatuses, runRoutineNow, saveRoutine, startRoutineScheduler, } from "./routines.js";
18
19
  /**
@@ -215,6 +216,7 @@ async function handleApi(req, res, url) {
215
216
  projectRoot: project?.root ?? getProjectRoot(),
216
217
  projectRootLabel: tildify(project?.root ?? getProjectRoot()),
217
218
  git: (project?.config.git && project.config.git.enabled !== false) === true,
219
+ repos: (project?.config.repos && project.config.repos.enabled !== false) === true,
218
220
  switcher,
219
221
  });
220
222
  }
@@ -250,6 +252,46 @@ async function handleApi(req, res, url) {
250
252
  const result = await run();
251
253
  return json(res, result, result.ok ? 200 : 409);
252
254
  }
255
+ // GET /api/repos — every clone under the repos root, read live from git
256
+ if (seg.length === 2 && seg[1] === "repos" && method === "GET") {
257
+ return json(res, await listRepos());
258
+ }
259
+ // POST /api/repos {url, name?, branch?, depth?} — clone into the root
260
+ if (seg.length === 2 && seg[1] === "repos" && method === "POST") {
261
+ if ((await openRepos()).off)
262
+ return json(res, { error: "repositories are off" }, 409);
263
+ try {
264
+ const body = JSON.parse(await readBody(req));
265
+ const str = (v) => (typeof v === "string" ? v : undefined);
266
+ const depth = body.depth === undefined ? undefined : typeof body.depth === "number" ? body.depth : Number.NaN;
267
+ const repo = await addRepo({ url: str(body.url) ?? "", name: str(body.name), branch: str(body.branch), depth });
268
+ return json(res, repo, 201);
269
+ }
270
+ catch (err) {
271
+ return json(res, { error: err.message }, 400);
272
+ }
273
+ }
274
+ // POST /api/repos/<slug>/update — fetch, fast-forward when clean
275
+ if (seg.length === 4 && seg[1] === "repos" && seg[3] === "update" && method === "POST") {
276
+ try {
277
+ const result = await updateRepo(seg[2]);
278
+ return json(res, result, result.ok ? 200 : result.repo || result.off ? 409 : 404);
279
+ }
280
+ catch (err) {
281
+ return json(res, { error: err.message }, 400);
282
+ }
283
+ }
284
+ // DELETE /api/repos/<slug>[?force=1] — remove the clone; 409 while it holds unpushed or uncommitted work
285
+ if (seg.length === 3 && seg[1] === "repos" && method === "DELETE") {
286
+ try {
287
+ const force = ["1", "true"].includes(url.searchParams.get("force") ?? "");
288
+ const result = await removeRepo(seg[2], force);
289
+ return json(res, result, result.ok ? 200 : result.conflict || result.off ? 409 : 404);
290
+ }
291
+ catch (err) {
292
+ return json(res, { error: err.message }, 400);
293
+ }
294
+ }
253
295
  // GET /api/search/agents — active agents of every reachable workspace (the ⌘K palette)
254
296
  if (seg.length === 3 && seg[1] === "search" && seg[2] === "agents" && method === "GET") {
255
297
  return json(res, await searchAgents());
@@ -301,7 +343,7 @@ async function handleApi(req, res, url) {
301
343
  if (seg.length === 2 && seg[1] === "settings" && method === "GET") {
302
344
  return json(res, await getSettings());
303
345
  }
304
- // PUT /api/settings — write the UI-editable subset (name, icon, switcher, git) back
346
+ // PUT /api/settings — write the UI-editable subset (name, icon, switcher, git, repos) back
305
347
  if (seg.length === 2 && seg[1] === "settings" && method === "PUT") {
306
348
  try {
307
349
  const input = JSON.parse(await readBody(req));
@@ -1,7 +1,7 @@
1
1
  import { type ProjectConfig, type SwitcherEntry } from "../config.js";
2
2
  /**
3
3
  * Workspace settings: read kraftwerk.yml for the UI and write the
4
- * UI-editable subset (name, icon, switcher, git) back. Edits go through the
4
+ * UI-editable subset (name, icon, switcher, git, repos) back. Edits go through the
5
5
  * yaml Document API so comments and every other key survive untouched;
6
6
  * a missing kraftwerk.yml is created at the project root on first save.
7
7
  */
@@ -24,6 +24,12 @@ export interface SaveSettingsInput {
24
24
  switcher?: SwitcherEntry[];
25
25
  /** Omitted = untouched. See cleanGit for how the block is written. */
26
26
  git?: GitSettingsInput;
27
+ /** Omitted = untouched. Same rules as git: off with defaults = no block. */
28
+ repos?: ReposSettingsInput;
29
+ }
30
+ export interface ReposSettingsInput {
31
+ enabled: boolean;
32
+ root?: string;
27
33
  }
28
34
  /** The git form as the settings screen posts it. Every field but `enabled` may be blank. */
29
35
  export interface GitSettingsInput {
@@ -1,8 +1,9 @@
1
1
  import { promises as fs } from "node:fs";
2
2
  import path from "node:path";
3
3
  import { parseDocument } from "yaml";
4
- import { resolveProject } from "../config.js";
4
+ import { ignoreEntryFor, REPOS_DEFAULT_ROOT, resolveProject } from "../config.js";
5
5
  import { getProjectRoot } from "./context.js";
6
+ import { ensureReposRoot } from "./repos.js";
6
7
  const GIT_DEFAULTS = { remote: "origin", interval: 300, autosync: "pull" };
7
8
  export async function getSettings() {
8
9
  const project = await resolveProject(getProjectRoot());
@@ -69,6 +70,25 @@ function cleanGit(value) {
69
70
  return null;
70
71
  return { enabled: false, ...out };
71
72
  }
73
+ /** The repos block to write; null when the feature is off and nothing else is set. */
74
+ function cleanRepos(value, projectRoot) {
75
+ if (typeof value !== "object" || value === null)
76
+ throw new Error("repos must be an object");
77
+ if (typeof value.enabled !== "boolean")
78
+ throw new Error("repos.enabled must be true or false");
79
+ const out = {};
80
+ const root = typeof value.root === "string" ? value.root.trim().replace(/\/+$/, "") : "";
81
+ if (root && !ignoreEntryFor(projectRoot, root)) {
82
+ throw new Error('repos.root must be a directory inside the project (not ".", ".." or an absolute path outside it)');
83
+ }
84
+ if (root && root !== REPOS_DEFAULT_ROOT)
85
+ out.root = root;
86
+ if (value.enabled)
87
+ return out;
88
+ if (Object.keys(out).length === 0)
89
+ return null;
90
+ return { enabled: false, ...out };
91
+ }
72
92
  export async function saveSettings(input) {
73
93
  if (input.name !== undefined && typeof input.name !== "string")
74
94
  throw new Error("name must be a string");
@@ -99,6 +119,17 @@ export async function saveSettings(input) {
99
119
  else
100
120
  doc.set("git", git);
101
121
  }
122
+ if (input.repos !== undefined) {
123
+ const repos = cleanRepos(input.repos, project.root);
124
+ if (repos === null)
125
+ doc.delete("repos");
126
+ else
127
+ doc.set("repos", repos);
128
+ }
102
129
  await fs.writeFile(configPath, doc.toString());
130
+ // Turning repositories on prepares the folder so the first clone and the
131
+ // workspace git both find it in the right state.
132
+ if (input.repos?.enabled)
133
+ await ensureReposRoot().catch(() => { });
103
134
  return getSettings();
104
135
  }