Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
claude-web-session.ts12.4 KB · 427 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
 * Claude-on-the-web runtime (Block CW).
 *
 * Drives one turn of an interactive Claude Code session running on the
 * gluecron web server. Each turn:
 *   1. Resolves (and lazily clones) the session's working directory.
 *   2. Spawns `claude` as a subprocess pointed at that workdir, with
 *      --resume <session-uuid> for follow-up turns so prior context is
 *      preserved without us re-sending the transcript.
 *   3. Streams stdout chunks back via an AsyncIterable the route layer
 *      hands to an SSE response.
 *   4. Persists the assistant turn + updates the Claude session UUID +
 *      the last_active_at watermark.
 *
 * v1 design rules:
 *   - Admin-only — gated by the route, not this lib.
 *   - Single shared compute: every session shares the web server's CPU
 *     and disk. We cap a single turn at MAX_TURN_MS so a runaway prompt
 *     can't pin the box forever.
 *   - No container: workdir is a plain directory under CLAUDE_WEB_WORKDIR
 *     (default /var/lib/gluecron/claude-web). Caller is responsible for
 *     not pointing this at a tenant-shared filesystem.
 *   - Anthropic creds: we DO NOT pass ANTHROPIC_API_KEY through the
 *     environment if the operator has logged in `claude` interactively
 *     (the CLI manages its own creds in ~/.claude). If the env var is
 *     set, we pass it through unchanged.
 *
 * Test seam: `__setSpawnForTests` lets unit tests intercept the spawn
 * without actually running the Claude CLI.
 */

import { mkdir, stat } from "fs/promises";
import { join } from "path";
import { db } from "../db";
import {
  claudeWebMessages,
  claudeWebSessions,
  type ClaudeWebSession,
} from "../db/schema";
import { and, eq } from "drizzle-orm";
import { getRepoPath } from "../git/repository";

const MAX_TURN_MS = 5 * 60_000;

export function claudeWebRoot(): string {
  return (
    process.env.CLAUDE_WEB_WORKDIR ||
    "/var/lib/gluecron/claude-web"
  ).replace(/\/$/, "");
}

export function claudeBinary(): string {
  return process.env.CLAUDE_BIN || "claude";
}

// ---------------------------------------------------------------------------
// Spawn seam
// ---------------------------------------------------------------------------

export interface SpawnHandle {
  /** Async iterable of utf-8 stdout chunks. */
  stdout: AsyncIterable<string>;
  /** Resolves to the final exit code + collected stderr after stdout ends. */
  done: Promise<{ exitCode: number; stderr: string }>;
  /** Kills the underlying subprocess. */
  kill: () => void;
}

export type SpawnFn = (
  cmd: string[],
  opts: { cwd: string; env: Record<string, string> }
) => SpawnHandle;

const defaultSpawn: SpawnFn = (cmd, opts) => {
  const proc = Bun.spawn(cmd, {
    cwd: opts.cwd,
    env: opts.env,
    stdin: "ignore",
    stdout: "pipe",
    stderr: "pipe",
  });
  const stdout = (async function* () {
    const reader = (proc.stdout as ReadableStream<Uint8Array>).getReader();
    const dec = new TextDecoder();
    try {
      while (true) {
        const { done, value } = await reader.read();
        if (done) break;
        if (value && value.length) yield dec.decode(value, { stream: true });
      }
      const tail = dec.decode();
      if (tail) yield tail;
    } finally {
      reader.releaseLock();
    }
  })();
  const done = (async () => {
    const [exitCode, stderr] = await Promise.all([
      proc.exited,
      new Response(proc.stderr).text(),
    ]);
    return { exitCode, stderr };
  })();
  return { stdout, done, kill: () => proc.kill() };
};

let _spawn: SpawnFn = defaultSpawn;
export function __setSpawnForTests(fn: SpawnFn | null): void {
  _spawn = fn ?? defaultSpawn;
}

// ---------------------------------------------------------------------------
// Workdir
// ---------------------------------------------------------------------------

export function sessionWorkdir(sessionId: string): string {
  return join(claudeWebRoot(), sessionId);
}

/**
 * Make sure the session's working dir exists as a fresh clone of the
 * repo's bare store at the given branch. Idempotent — if the dir already
 * has a `.git` folder, we leave it alone and the operator's existing
 * working state is preserved across turns.
 *
 * Uses `git clone --branch <branch> --depth 1 <bare> <workdir>` so the
 * initial materialisation is fast even on huge repos. Operators can
 * `git fetch --unshallow` from inside the session if they need history.
 */
export async function ensureWorkdir(
  session: ClaudeWebSession,
  ownerName: string,
  repoName: string,
  spawn: SpawnFn = _spawn
): Promise<{ ok: true } | { ok: false; error: string }> {
  const root = claudeWebRoot();
  try {
    await mkdir(root, { recursive: true });
  } catch (err) {
    return {
      ok: false,
      error: `mkdir ${root}: ${err instanceof Error ? err.message : err}`,
    };
  }
  const workdir = session.workdirPath;
  // If .git already exists in the workdir, the clone is done.
  try {
    const s = await stat(join(workdir, ".git"));
    if (s.isDirectory()) return { ok: true };
  } catch {
    /* fall through to clone */
  }

  const bare = getRepoPath(ownerName, repoName);
  const handle = spawn(
    ["git", "clone", "--branch", session.branch, "--depth", "1", bare, workdir],
    { cwd: root, env: scrubbedEnv() }
  );
  // Drain stdout (clone is quiet, but we still need to consume).
  for await (const _ of handle.stdout) {
    /* ignore */
  }
  const { exitCode, stderr } = await handle.done;
  if (exitCode !== 0) {
    return { ok: false, error: stderr.trim() || `git clone exit ${exitCode}` };
  }
  return { ok: true };
}

// ---------------------------------------------------------------------------
// Turn driver
// ---------------------------------------------------------------------------

export interface TurnInput {
  session: ClaudeWebSession;
  ownerName: string;
  repoName: string;
  prompt: string;
}

export interface TurnEvent {
  /** Streaming text chunk from Claude's stdout. */
  chunk?: string;
  /** Terminal event — call once, after stdout ends. */
  done?: {
    exitCode: number;
    durationMs: number;
    /** Claude CLI session UUID extracted from the run (when present). */
    claudeSessionId?: string;
    stderr: string;
  };
}

/**
 * Run a single conversational turn against Claude. Yields stdout chunks
 * as they arrive, then a single `done` event with metadata. The caller
 * (typically the SSE route) is responsible for serialising events onto
 * the wire and persisting the final assistant body.
 *
 * Honours `MAX_TURN_MS` — if the subprocess hasn't finished by then we
 * call kill() and emit done with exitCode=124 (matching `timeout`).
 */
export async function* runTurn(
  input: TurnInput,
  spawn: SpawnFn = _spawn
): AsyncGenerator<TurnEvent, void, void> {
  const start = Date.now();
  const cmd = [
    claudeBinary(),
    "--print",
    "--output-format",
    "stream-json",
    ...(input.session.claudeSessionId
      ? ["--resume", input.session.claudeSessionId]
      : []),
    input.prompt,
  ];

  const handle = spawn(cmd, {
    cwd: input.session.workdirPath,
    env: passthroughEnv(),
  });

  const timer = setTimeout(() => {
    try {
      handle.kill();
    } catch {
      /* ignore */
    }
  }, MAX_TURN_MS);

  let sessionId: string | undefined;
  try {
    for await (const chunk of handle.stdout) {
      // Best-effort parse for the Claude session UUID inside stream-json
      // events. The CLI emits a `"session_id":"<uuid>"` key on its init
      // event; we just grep the chunk text so format drift doesn't break.
      if (!sessionId) {
        const m = chunk.match(/"session_id"\s*:\s*"([0-9a-f-]{32,36})"/i);
        if (m) sessionId = m[1];
      }
      yield { chunk };
    }
  } finally {
    clearTimeout(timer);
  }

  const { exitCode, stderr } = await handle.done;
  yield {
    done: {
      exitCode,
      durationMs: Date.now() - start,
      claudeSessionId: sessionId,
      stderr,
    },
  };
}

/**
 * Env scrubbing for child subprocesses. Drops Postgres / SMTP / SSH
 * creds the Claude binary doesn't need. Keeps ANTHROPIC_API_KEY, PATH,
 * HOME, and the CLAUDE_* family.
 */
function passthroughEnv(): Record<string, string> {
  const out: Record<string, string> = {};
  const passlist = new Set([
    "PATH",
    "HOME",
    "USER",
    "LANG",
    "LC_ALL",
    "TERM",
    "ANTHROPIC_API_KEY",
    "ANTHROPIC_BASE_URL",
    "ANTHROPIC_AUTH_TOKEN",
  ]);
  for (const [k, v] of Object.entries(process.env)) {
    if (typeof v !== "string") continue;
    if (passlist.has(k) || k.startsWith("CLAUDE_")) out[k] = v;
  }
  return out;
}

function scrubbedEnv(): Record<string, string> {
  // For `git clone` we want even fewer — just PATH + HOME so git can
  // find its config and ssh wrappers.
  const out: Record<string, string> = {};
  for (const k of ["PATH", "HOME", "USER", "LANG"]) {
    if (typeof process.env[k] === "string") out[k] = process.env[k] as string;
  }
  // Disable any interactive prompt — clone should be non-interactive.
  out.GIT_TERMINAL_PROMPT = "0";
  return out;
}

// ---------------------------------------------------------------------------
// Transcript persistence
// ---------------------------------------------------------------------------

export async function listMessages(sessionId: string) {
  return db
    .select()
    .from(claudeWebMessages)
    .where(eq(claudeWebMessages.sessionId, sessionId))
    .orderBy(claudeWebMessages.createdAt);
}

export async function appendMessage(input: {
  sessionId: string;
  role: "user" | "assistant" | "system";
  body: string;
  exitCode?: number;
  durationMs?: number;
}): Promise<void> {
  await db.insert(claudeWebMessages).values({
    sessionId: input.sessionId,
    role: input.role,
    body: input.body,
    exitCode: input.exitCode ?? null,
    durationMs: input.durationMs ?? null,
  });
}

export async function touchSession(input: {
  sessionId: string;
  claudeSessionId?: string;
  status?: string;
}): Promise<void> {
  const set: Record<string, unknown> = { lastActiveAt: new Date() };
  if (input.claudeSessionId) set.claudeSessionId = input.claudeSessionId;
  if (input.status) set.status = input.status;
  await db
    .update(claudeWebSessions)
    .set(set)
    .where(eq(claudeWebSessions.id, input.sessionId));
}

export async function createSession(input: {
  repositoryId: string;
  ownerUserId: string;
  title?: string;
  branch?: string;
}): Promise<ClaudeWebSession> {
  // Insert first so we have the id for the workdir path.
  const [row] = await db
    .insert(claudeWebSessions)
    .values({
      repositoryId: input.repositoryId,
      ownerUserId: input.ownerUserId,
      title: input.title ?? "New session",
      branch: input.branch ?? "main",
      workdirPath: "pending",
      status: "cold",
    })
    .returning();
  if (!row) throw new Error("createSession: insert returned no row");
  const workdir = sessionWorkdir(row.id);
  const [updated] = await db
    .update(claudeWebSessions)
    .set({ workdirPath: workdir })
    .where(eq(claudeWebSessions.id, row.id))
    .returning();
  return updated ?? { ...row, workdirPath: workdir };
}

export async function getSession(
  id: string,
  ownerUserId?: string
): Promise<ClaudeWebSession | null> {
  const conds = ownerUserId
    ? and(
        eq(claudeWebSessions.id, id),
        eq(claudeWebSessions.ownerUserId, ownerUserId)
      )
    : eq(claudeWebSessions.id, id);
  const [row] = await db
    .select()
    .from(claudeWebSessions)
    .where(conds)
    .limit(1);
  return row ?? null;
}

export async function listSessionsForRepo(
  repositoryId: string,
  limit = 50
): Promise<ClaudeWebSession[]> {
  return db
    .select()
    .from(claudeWebSessions)
    .where(eq(claudeWebSessions.repositoryId, repositoryId))
    .limit(limit);
}

/**
 * List sessions for a specific user in a specific repo, ordered by most
 * recently active first. Used by the customer-facing /:owner/:repo/claude
 * page so each user sees only their own sessions.
 */
export async function listSessionsForUser(
  repositoryId: string,
  ownerUserId: string,
  limit = 50
): Promise<ClaudeWebSession[]> {
  return db
    .select()
    .from(claudeWebSessions)
    .where(
      and(
        eq(claudeWebSessions.repositoryId, repositoryId),
        eq(claudeWebSessions.ownerUserId, ownerUserId)
      )
    )
    .orderBy(claudeWebSessions.lastActiveAt)
    .limit(limit);
}

export async function deleteSession(id: string): Promise<void> {
  await db.delete(claudeWebSessions).where(eq(claudeWebSessions.id, id));
}

/** Test-only access to internals. */
export const __test = {
  passthroughEnv,
  scrubbedEnv,
  MAX_TURN_MS,
};