@gleapai/kai-bridge 0.6.0 → 0.7.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/src/workspace.mjs CHANGED
@@ -15,6 +15,7 @@
15
15
  import { execFileSync } from "node:child_process";
16
16
  import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
17
17
  import { dirname, join } from "node:path";
18
+ import { createHash } from 'node:crypto';
18
19
 
19
20
  import { seedNodeModules } from "./deps.mjs";
20
21
 
@@ -23,13 +24,18 @@ function git(cwd, args, opts = {}) {
23
24
  }
24
25
 
25
26
  export function sessionSlug(sessionId, title) {
27
+ if (process.env.KAI_HOSTED === '1') {
28
+ if (!/^[a-zA-Z0-9_-]{1,128}$/.test(String(sessionId))) throw new Error('Invalid session identity.');
29
+ return `session-${sessionId}`;
30
+ }
26
31
  const t = String(title || "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "").slice(0, 32);
27
32
  const id = String(sessionId || "").slice(-8);
28
33
  return t ? `${t}-${id}` : `session-${id}`;
29
34
  }
30
35
 
31
36
  export function worktreePath(kaiHome, repoName, slug) {
32
- return join(kaiHome, "worktrees", repoName, slug);
37
+ const repo = process.env.KAI_HOSTED === '1' ? createHash('sha256').update(String(repoName)).digest('hex') : repoName;
38
+ return join(kaiHome, "worktrees", repo, slug);
33
39
  }
34
40
 
35
41
  /**
@@ -81,7 +87,7 @@ export function materializeBinding({ kaiHome, repo, binding, sessionId, title, b
81
87
  // Resume: the worktree from the previous turn is the session state.
82
88
  return { cwd: dir, mode, branch, base, resumed: true };
83
89
  }
84
- mkdirSync(join(kaiHome, "worktrees", repo.name), { recursive: true });
90
+ mkdirSync(dirname(dir), { recursive: true });
85
91
  git(repo.primaryPath, ["fetch", "origin", base, "--quiet"]);
86
92
  git(repo.primaryPath, ["worktree", "add", "-b", branch, dir, `origin/${base}`]);
87
93
  // A fresh worktree has no node_modules; clone the primary checkout's
@@ -170,6 +176,12 @@ export function discardChanges(cwd) {
170
176
  export function removeWorktree({ kaiHome, repo, sessionId, title }) {
171
177
  const dir = worktreePath(kaiHome, repo.name, sessionSlug(sessionId, title));
172
178
  if (!existsSync(dir)) return false;
179
+ if (process.env.KAI_HOSTED === '1') {
180
+ // Never destroy dirty or unpushed work during session cleanup.
181
+ if (collectChanges(dir).files.length || git(dir, ['log', '--branches', '--not', '--remotes', '--oneline'])) return false;
182
+ git(repo.primaryPath, ['worktree', 'remove', dir]);
183
+ return true;
184
+ }
173
185
  try {
174
186
  git(repo.primaryPath, ["worktree", "remove", "--force", dir]);
175
187
  } catch {