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
spec-context.ts6.9 KB · 262 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
/**
 * Spec context reader for spec-to-PR.
 *
 * Given a bare repo on disk and a natural-language spec, produce a bounded
 * "prompt context" package: a capped file list plus the highest-scoring
 * source files (by keyword overlap with the spec), each content-truncated.
 *
 * Design:
 * - All git access is via `Bun.spawn(["git", "-C", repoDiskPath, ...])` —
 *   argv form, no shell.
 * - We never throw: any git/IO failure is returned as `{ok:false, error}`.
 * - Scoring is deliberately cheap (token overlap + a couple of small boosts)
 *   because this runs synchronously before an LLM call on every request and
 *   must stay predictable/cheap.
 * - Binary files are skipped by the classic NUL-byte heuristic so we don't
 *   waste token budget on images/archives.
 */
export type SpecContext = {
  fileList: string[];
  relevantFiles: Array<{ path: string; content: string }>;
  defaultBranch: string;
  totalSizeBytes: number;
};

export type BuildSpecContextArgs = {
  repoDiskPath: string;
  spec: string;
  defaultBranch?: string;
  maxRelevantFiles?: number;
  maxFileBytes?: number;
};

export type BuildSpecContextResult =
  | { ok: true; context: SpecContext }
  | { ok: false; error: string };

const STOP_WORDS = new Set([
  "add",
  "the",
  "and",
  "for",
  "from",
  "with",
  "this",
  "that",
]);

const CODE_EXTENSIONS = new Set([
  "ts",
  "tsx",
  "js",
  "jsx",
  "py",
  "go",
  "rs",
  "rb",
  "java",
  "php",
]);

const BOOST_NAMES = new Set(["readme", "index", "main", "app"]);

const MAX_FILE_LIST = 500;
const DEFAULT_MAX_RELEVANT = 20;
const DEFAULT_MAX_BYTES = 3000;
const BINARY_SNIFF_BYTES = 8000;

/** Tokenize a spec into lowercase alphanumeric words of length ≥3, stop-words removed. */
function tokenize(spec: string): string[] {
  const out: string[] = [];
  const seen = new Set<string>();
  for (const raw of spec.toLowerCase().split(/[^a-z0-9]+/)) {
    if (raw.length < 3) continue;
    if (STOP_WORDS.has(raw)) continue;
    if (seen.has(raw)) continue;
    seen.add(raw);
    out.push(raw);
  }
  return out;
}

/**
 * Score a path against a set of spec tokens. Higher = more relevant.
 *
 * Exported so the unit tests can exercise scoring without building a real
 * git repo on disk.
 */
export function scoreFile(path: string, tokens: string[]): number {
  const lower = path.toLowerCase();
  const parts = lower.split(/[\/\\._-]+/).filter(Boolean);
  let score = 0;
  for (const tok of tokens) {
    for (const part of parts) {
      if (part === tok) score += 2;
      else if (part.includes(tok)) score += 1;
    }
  }
  // Boost well-known "entry-point"-ish filenames.
  for (const part of parts) {
    if (BOOST_NAMES.has(part)) {
      score += 1;
      break;
    }
  }
  // Boost common code-file extensions.
  const dot = lower.lastIndexOf(".");
  if (dot !== -1) {
    const ext = lower.slice(dot + 1);
    if (CODE_EXTENSIONS.has(ext)) score += 0.5;
  }
  return score;
}

async function runGit(
  repoDiskPath: string,
  args: string[]
): Promise<{ ok: true; stdout: string } | { ok: false; error: string }> {
  try {
    const proc = Bun.spawn(["git", "-C", repoDiskPath, ...args], {
      stdout: "pipe",
      stderr: "pipe",
    });
    const [stdout, stderr] = await Promise.all([
      new Response(proc.stdout).text(),
      new Response(proc.stderr).text(),
    ]);
    const exitCode = await proc.exited;
    if (exitCode !== 0) {
      return {
        ok: false,
        error: (stderr || `git ${args[0]} exited ${exitCode}`).trim(),
      };
    }
    return { ok: true, stdout };
  } catch (err) {
    return {
      ok: false,
      error: err instanceof Error ? err.message : String(err),
    };
  }
}

async function runGitBytes(
  repoDiskPath: string,
  args: string[]
): Promise<{ ok: true; bytes: Uint8Array } | { ok: false; error: string }> {
  try {
    const proc = Bun.spawn(["git", "-C", repoDiskPath, ...args], {
      stdout: "pipe",
      stderr: "pipe",
    });
    const [bytes, stderr] = await Promise.all([
      new Response(proc.stdout).arrayBuffer(),
      new Response(proc.stderr).text(),
    ]);
    const exitCode = await proc.exited;
    if (exitCode !== 0) {
      return {
        ok: false,
        error: (stderr || `git ${args[0]} exited ${exitCode}`).trim(),
      };
    }
    return { ok: true, bytes: new Uint8Array(bytes) };
  } catch (err) {
    return {
      ok: false,
      error: err instanceof Error ? err.message : String(err),
    };
  }
}

function looksBinary(bytes: Uint8Array): boolean {
  const n = Math.min(bytes.length, BINARY_SNIFF_BYTES);
  for (let i = 0; i < n; i++) {
    if (bytes[i] === 0) return true;
  }
  return false;
}

export async function buildSpecContext(
  args: BuildSpecContextArgs
): Promise<BuildSpecContextResult> {
  const {
    repoDiskPath,
    spec,
    maxRelevantFiles = DEFAULT_MAX_RELEVANT,
    maxFileBytes = DEFAULT_MAX_BYTES,
  } = args;

  // 1. Resolve default branch. We trust `defaultBranch` if passed, otherwise
  //    ask the repo for its symbolic HEAD.
  let defaultBranch = args.defaultBranch;
  if (!defaultBranch) {
    const head = await runGit(repoDiskPath, [
      "symbolic-ref",
      "--short",
      "HEAD",
    ]);
    if (!head.ok) return { ok: false, error: head.error };
    defaultBranch = head.stdout.trim();
    if (!defaultBranch) {
      return { ok: false, error: "could not resolve default branch" };
    }
  }

  // 2. Full file list on the branch. Cap before scoring to keep work bounded
  //    on monorepos.
  const tree = await runGit(repoDiskPath, [
    "ls-tree",
    "-r",
    defaultBranch,
    "--name-only",
  ]);
  if (!tree.ok) return { ok: false, error: tree.error };

  const allPaths = tree.stdout
    .split("\n")
    .map((s) => s.trim())
    .filter((s) => s.length > 0);
  const fileList = allPaths.slice(0, MAX_FILE_LIST);

  // 3. Score & rank. Ties broken by shorter path (likely more central).
  const tokens = tokenize(spec);
  const scored = fileList
    .map((path) => ({ path, score: scoreFile(path, tokens) }))
    .sort((a, b) => {
      if (b.score !== a.score) return b.score - a.score;
      return a.path.length - b.path.length;
    });

  // 4. Fetch content for the top N. Skip binaries, truncate oversized files.
  const relevantFiles: Array<{ path: string; content: string }> = [];
  let totalSizeBytes = 0;
  const TRUNC_MARKER = "\n...[truncated]";

  for (const { path } of scored) {
    if (relevantFiles.length >= maxRelevantFiles) break;
    const blob = await runGitBytes(repoDiskPath, [
      "show",
      `${defaultBranch}:${path}`,
    ]);
    if (!blob.ok) continue; // submodule, missing, etc. — just skip
    if (looksBinary(blob.bytes)) continue;

    let content = new TextDecoder("utf-8", { fatal: false }).decode(blob.bytes);
    if (content.length > maxFileBytes) {
      content = content.slice(0, maxFileBytes) + TRUNC_MARKER;
    }
    relevantFiles.push({ path, content });
    totalSizeBytes += content.length;
  }

  return {
    ok: true,
    context: {
      fileList,
      relevantFiles,
      defaultBranch,
      totalSizeBytes,
    },
  };
}