replen 1.0.0 → 1.0.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.
package/dist/commands.js CHANGED
@@ -254,6 +254,31 @@ export async function runCheckNew(argv) {
254
254
  // correct response (calm-cadence principle).
255
255
  const scope = r.scopedTo ? ` for ${r.scopedTo}` : "";
256
256
  console.log(`No new actionable matches${scope} since you last engaged.`);
257
+ return;
258
+ }
259
+ // Hook mode + hasNew=false: fall back to inventory state. check-new
260
+ // is cursor-based and goes silent the moment ANY prior call (including
261
+ // our own validation probes or a previous session) bumped the cursor
262
+ // past existing matches. That's right behaviour for "did anything
263
+ // change?", wrong for "should I tell the agent there's a queue worth
264
+ // surfacing?" — the user might never have seen these candidates.
265
+ //
266
+ // Resolve the cwd's GitHub remote, query the inventory scoped to it,
267
+ // and print a one-line status if anything's there. Output goes
268
+ // verbatim into the agent's opening context via Claude Code's
269
+ // SessionStart-hook stdout injection.
270
+ const cwdRepo = await detectCwdRepo();
271
+ if (!cwdRepo)
272
+ return; // not in a git repo, or no GitHub remote — silent
273
+ try {
274
+ const inv = await fetchInventoryStatus(cfg, cwdRepo);
275
+ if (inv && inv.count > 0) {
276
+ const top = inv.topRepo ? ` Top: ${inv.topRepo}${inv.topSimilarity ? ` (~${inv.topSimilarity}% match)` : ""}.` : "";
277
+ console.log(`Replen has ${inv.count} candidate${inv.count === 1 ? "" : "s"} queued for ${cwdRepo}.${top} Run /replen-match for full triage.`);
278
+ }
279
+ }
280
+ catch {
281
+ // Inventory query failed — silent, never disrupt a session.
257
282
  }
258
283
  return;
259
284
  }
@@ -408,3 +433,61 @@ function handleApiError(e) {
408
433
  function sleep(ms) {
409
434
  return new Promise((r) => setTimeout(r, ms));
410
435
  }
436
+ // Resolve the cwd's git origin remote into a GitHub owner/name, if the
437
+ // remote is a GitHub URL (HTTPS, standard SSH, or a multi-account SSH
438
+ // alias like github-personal). Returns null if not in a git repo or the
439
+ // remote isn't GitHub. Used by the SessionStart hook to scope inventory
440
+ // queries to the project the user just opened Claude Code in.
441
+ async function detectCwdRepo() {
442
+ try {
443
+ const { execSync } = await import("node:child_process");
444
+ const url = execSync("git remote get-url origin", {
445
+ stdio: ["ignore", "pipe", "ignore"],
446
+ encoding: "utf8",
447
+ timeout: 1500,
448
+ }).trim();
449
+ const m = url.match(/(?:github\.com|github-[a-z0-9_-]+)[:/]([^/]+)\/([^/?#]+?)(?:\.git)?$/i);
450
+ if (!m)
451
+ return null;
452
+ return `${m[1]}/${m[2]}`;
453
+ }
454
+ catch {
455
+ return null;
456
+ }
457
+ }
458
+ async function fetchInventoryStatus(cfg, repo) {
459
+ // Hook mode is on the session-open critical path; cap latency hard.
460
+ const ctrl = new AbortController();
461
+ const timer = setTimeout(() => ctrl.abort(), 3000);
462
+ try {
463
+ const url = new URL(cfg.base + "/api/inventory/today");
464
+ url.searchParams.set("repo", repo);
465
+ url.searchParams.set("limit", "5");
466
+ url.searchParams.set("days", "14");
467
+ const res = await fetch(url, {
468
+ headers: { "x-digest-token": cfg.token, accept: "application/json" },
469
+ signal: ctrl.signal,
470
+ });
471
+ if (!res.ok)
472
+ return null;
473
+ const data = (await res.json());
474
+ const cands = data.candidates ?? [];
475
+ if (cands.length === 0)
476
+ return null;
477
+ const top = cands[0];
478
+ // Pull the cosine % out of whyShortlisted if present
479
+ // (format: "...; semantic similarity: 58%").
480
+ const simMatch = top.whyShortlisted?.match(/semantic similarity:\s*(\d+)%/);
481
+ return {
482
+ count: cands.length,
483
+ topRepo: top.repo ?? null,
484
+ topSimilarity: simMatch ? Number(simMatch[1]) : null,
485
+ };
486
+ }
487
+ catch {
488
+ return null;
489
+ }
490
+ finally {
491
+ clearTimeout(timer);
492
+ }
493
+ }
@@ -1,9 +1,12 @@
1
1
  // Local-filesystem project discovery for day-1 onboarding.
2
2
  //
3
- // Walks the user's conventional repo roots (~/github/, ~/code/,
4
- // ~/projects/) for git repos, then for each one extracts:
3
+ // Recursively walks a list of root dirs (provided by discover-roots.ts),
4
+ // finds every git repo, and for each one extracts:
5
5
  // - The repo's `owner/name` from `git remote get-url origin`
6
- // - A slug (from the directory basename, normalised)
6
+ // - A slug derived from the GITHUB repo name (so a local folder named
7
+ // "drone" whose remote is acme/aegis registers with slug "aegis",
8
+ // matching what shows on GitHub). Falls back to dirname for repos
9
+ // without a GitHub remote.
7
10
  // - A name (from package.json's `name` field if present, else slug)
8
11
  // - Auto-suggested tags from the project's manifests
9
12
  // - The primary language (best-effort, from manifest type)
@@ -11,16 +14,54 @@
11
14
  // Output is shaped for POST /api/projects/bulk on the server. No
12
15
  // network or LLM calls — pure local filesystem.
13
16
  //
14
- // Why local-FS instead of asking GitHub via PAT: skill-mode's whole
15
- // pitch is "no API keys to share with us." Auto-detect via GitHub
16
- // API requires a PAT; auto-detect via local git remotes requires
17
- // nothing the user doesn't already have. PAT becomes optional, only
18
- // needed if/when the user wants server-side handoff PRs.
17
+ // Why local-FS instead of asking GitHub via PAT: the whole pitch is
18
+ // "no API keys to share with us." Auto-detect via GitHub API requires
19
+ // a PAT; auto-detect via local git remotes requires nothing the user
20
+ // doesn't already have. PAT becomes optional, only needed if/when the
21
+ // user wants server-side handoff PRs.
19
22
  import { readFileSync, readdirSync, existsSync, statSync } from "node:fs";
20
23
  import { execSync } from "node:child_process";
21
- import { homedir } from "node:os";
22
- import { join } from "node:path";
23
- const SCAN_ROOTS = ["github", "code", "projects"]; // immediate children of ~
24
+ import { join, basename } from "node:path";
25
+ // Hard maximum directory depth to walk from each root. depth=0 is the
26
+ // root itself, depth=1 its immediate children, etc. 4 covers the
27
+ // "workspace dir with sibling repos" pattern (e.g. ~/projects/drone/
28
+ // containing ~/projects/drone/flight-controller/.git, where the
29
+ // flight-controller dir is at depth 2 from ~/projects).
30
+ const MAX_DEPTH = 4;
31
+ // Directory names to skip outright during the walk. Mix of: build
32
+ // artifacts that pollute repo-counting, package caches that can be
33
+ // huge, and macOS / Linux home subdirs that never contain user code.
34
+ const EXCLUDE_NAMES = new Set([
35
+ "node_modules",
36
+ ".next",
37
+ "dist",
38
+ "build",
39
+ "target",
40
+ "vendor",
41
+ ".cache",
42
+ ".npm",
43
+ ".yarn",
44
+ ".pnpm-store",
45
+ ".turbo",
46
+ ".terraform",
47
+ ".venv",
48
+ "venv",
49
+ "__pycache__",
50
+ // macOS system / media dirs
51
+ "Library",
52
+ "Applications",
53
+ "System",
54
+ "Pictures",
55
+ "Movies",
56
+ "Music",
57
+ "Downloads",
58
+ "Public",
59
+ "Desktop",
60
+ // Linux equivalents
61
+ ".local",
62
+ ".config",
63
+ "snap",
64
+ ]);
24
65
  // Manifest-derived tag mappings. Each pattern matches a dep name (or
25
66
  // a substring) and yields one or more tags. Ordered by specificity:
26
67
  // more-specific patterns first so e.g. "next" doesn't accidentally
@@ -84,45 +125,131 @@ const DEP_TO_TAGS = [
84
125
  { match: /^albumentations$/, tags: ["augmentation"] },
85
126
  { match: /^scikit-learn$/, tags: ["ml"] },
86
127
  ];
87
- export function discoverProjects() {
88
- const out = [];
89
- const home = homedir();
90
- for (const root of SCAN_ROOTS) {
91
- const rootPath = join(home, root);
92
- if (!existsSync(rootPath))
93
- continue;
94
- let entries;
95
- try {
96
- entries = readdirSync(rootPath);
97
- }
98
- catch {
99
- continue;
100
- }
101
- for (const dirName of entries) {
102
- if (dirName.startsWith(".") || dirName === "node_modules")
128
+ /**
129
+ * Walk the given roots recursively (depth-capped, excluded dirs skipped)
130
+ * and return every git repo found, partitioned by whether it has a
131
+ * GitHub origin remote. Repos without a GitHub remote are counted but
132
+ * not registered — they're surfaced to the user as transparency rather
133
+ * than silently dropped.
134
+ *
135
+ * Deduplicates by absolute `localPath` (so overlapping roots like
136
+ * `~/projects` and `~/projects/drone` don't double-register the inner
137
+ * repos) and by `githubFullName` (so cloning the same repo to two
138
+ * paths doesn't create two project rows).
139
+ *
140
+ * Slug = the local directory basename (normalised). When two repos in
141
+ * the discovery result would share a slug (e.g. `flight-controller`
142
+ * under both `~/projects/drone/` and `~/work/sandbox/`), the second+
143
+ * gets `-<owner>` appended to disambiguate. Keeps slugs short for the
144
+ * common case while preventing server-side `uniq_profile_user_slug`
145
+ * collisions.
146
+ *
147
+ * Identity is `githubFullName` on the server side; slug is just the
148
+ * URL-safe display label. So a local `~/projects/drone/` whose remote
149
+ * is `acme/aegis` keeps slug `drone` (matching how you think of it
150
+ * locally) while still registering correctly against `acme/aegis` on
151
+ * the dashboard.
152
+ */
153
+ export function discoverProjects(roots) {
154
+ const seenPaths = new Set();
155
+ const seenGithub = new Set();
156
+ const projects = [];
157
+ let nonGithubSkipped = 0;
158
+ const scannedRoots = Array.from(new Set(roots.filter(existsAndIsDir)));
159
+ for (const root of scannedRoots) {
160
+ for (const repoPath of walkForGitRepos(root, 0)) {
161
+ if (seenPaths.has(repoPath))
103
162
  continue;
104
- const localPath = join(rootPath, dirName);
105
- try {
106
- if (!statSync(localPath).isDirectory())
107
- continue;
108
- if (!existsSync(join(localPath, ".git")))
109
- continue;
110
- }
111
- catch {
163
+ seenPaths.add(repoPath);
164
+ const githubFullName = readGitRemote(repoPath);
165
+ const dirName = basename(repoPath);
166
+ if (!githubFullName) {
167
+ nonGithubSkipped++;
112
168
  continue;
113
169
  }
114
- const githubFullName = readGitRemote(localPath);
115
- if (!githubFullName)
116
- continue; // No GitHub remote → skip; can't register
117
- const { name, tags, primaryLanguage } = extractMetadata(localPath, dirName);
118
- const slug = normaliseSlug(dirName);
119
- out.push({ localPath, slug, name, githubFullName, tags, primaryLanguage });
170
+ if (seenGithub.has(githubFullName))
171
+ continue;
172
+ seenGithub.add(githubFullName);
173
+ const { name, tags, primaryLanguage } = extractMetadata(repoPath, dirName);
174
+ projects.push({
175
+ localPath: repoPath,
176
+ slug: normaliseSlug(dirName),
177
+ name,
178
+ githubFullName,
179
+ tags,
180
+ primaryLanguage,
181
+ });
120
182
  }
121
183
  }
122
- return out;
184
+ // In-discovery slug disambiguation. Same-name dirs under different
185
+ // parents (e.g. `~/projects/drone/flight-controller` +
186
+ // `~/work/flight-controller`) would otherwise collide on the server's
187
+ // `uniq_profile_user_slug` index. Suffix the second+ occurrence with
188
+ // the GitHub owner so it remains unique per user.
189
+ disambiguateSlugs(projects);
190
+ return { projects, nonGithubSkipped, scannedRoots };
191
+ }
192
+ function disambiguateSlugs(projects) {
193
+ const counts = new Map();
194
+ for (const p of projects)
195
+ counts.set(p.slug, (counts.get(p.slug) ?? 0) + 1);
196
+ const used = new Set();
197
+ for (const p of projects) {
198
+ if ((counts.get(p.slug) ?? 0) <= 1 && !used.has(p.slug)) {
199
+ used.add(p.slug);
200
+ continue;
201
+ }
202
+ const owner = (p.githubFullName ?? "").split("/")[0] ?? "";
203
+ let candidate = normaliseSlug(`${p.slug}-${owner}`);
204
+ let n = 2;
205
+ while (used.has(candidate)) {
206
+ candidate = normaliseSlug(`${p.slug}-${owner}-${n++}`);
207
+ }
208
+ p.slug = candidate;
209
+ used.add(candidate);
210
+ }
211
+ }
212
+ /**
213
+ * Generator: yields the absolute path of every git repo found under
214
+ * `dir`, up to `MAX_DEPTH`. Stops recursing into a directory once a
215
+ * `.git/` is found there (treats it as a repo boundary — submodules
216
+ * and nested-clone edge cases aren't worth complicating the walker).
217
+ */
218
+ function* walkForGitRepos(dir, depth) {
219
+ if (depth > MAX_DEPTH)
220
+ return;
221
+ let entries;
222
+ try {
223
+ entries = readdirSync(dir, { withFileTypes: true });
224
+ }
225
+ catch {
226
+ return;
227
+ }
228
+ // Is this dir itself a repo? Yield + stop recursing.
229
+ if (entries.some((e) => e.name === ".git" && (e.isDirectory() || e.isSymbolicLink()))) {
230
+ yield dir;
231
+ return;
232
+ }
233
+ for (const entry of entries) {
234
+ if (!entry.isDirectory() && !entry.isSymbolicLink())
235
+ continue;
236
+ if (entry.name.startsWith("."))
237
+ continue;
238
+ if (EXCLUDE_NAMES.has(entry.name))
239
+ continue;
240
+ yield* walkForGitRepos(join(dir, entry.name), depth + 1);
241
+ }
242
+ }
243
+ function existsAndIsDir(p) {
244
+ try {
245
+ return statSync(p).isDirectory();
246
+ }
247
+ catch {
248
+ return false;
249
+ }
123
250
  }
124
- function normaliseSlug(dirName) {
125
- return dirName
251
+ function normaliseSlug(name) {
252
+ return name
126
253
  .toLowerCase()
127
254
  .replace(/[^a-z0-9_-]/g, "-")
128
255
  .replace(/^-+|-+$/g, "")
@@ -144,9 +271,17 @@ function readGitRemote(repoPath) {
144
271
  catch {
145
272
  return null;
146
273
  }
147
- // Match both HTTPS (https://github.com/owner/name[.git]) and SSH
148
- // (git@github.com:owner/name[.git]) formats.
149
- const m = url.match(/github\.com[:/]([^/]+)\/([^/?#]+?)(?:\.git)?$/i);
274
+ // Match three URL shapes that all resolve to GitHub:
275
+ // 1. HTTPS: https://github.com/owner/name[.git]
276
+ // 2. Standard SSH: git@github.com:owner/name[.git]
277
+ // 3. SSH config alias for github: git@github-<alias>:owner/name[.git]
278
+ // Case (3) is a common multi-account-GitHub pattern (one ssh alias
279
+ // per identity, e.g. `github-personal`, `github-work`); the host
280
+ // portion is opaque to git, the ssh layer resolves it to github.com.
281
+ // Without this branch, those repos register as "non-GitHub" and get
282
+ // silently skipped — a confusing failure mode for users with that
283
+ // convention. Owner/name parsing is identical across all three shapes.
284
+ const m = url.match(/(?:github\.com|github-[a-z0-9_-]+)[:/]([^/]+)\/([^/?#]+?)(?:\.git)?$/i);
150
285
  if (!m)
151
286
  return null;
152
287
  return `${m[1]}/${m[2]}`;
@@ -0,0 +1,169 @@
1
+ // Layered project-root discovery. Tries cheap inference first, falls back
2
+ // to interactive prompt if nothing works. Result is one or more root dirs
3
+ // that the recursive walker in discover-projects.ts will then scan for
4
+ // git repos.
5
+ //
6
+ // Order (cheapest / most-targeted first):
7
+ // 1. Explicit --root flag(s) → user-specified
8
+ // 2. REPLEN_PROJECT_ROOTS env var → scripted / dotfiles
9
+ // 3. Saved ~/.replen/config.json roots → previously confirmed
10
+ // 4. cwd walk-up → "I'm in a repo right now"
11
+ // 5. ~/.claude.json mining → "I use Claude Code regularly"
12
+ // 6. Hardcoded ~/github, ~/code, ~/projects → conventional layout
13
+ // 7. Interactive prompt → ask the user directly
14
+ //
15
+ // The orchestration in sync-projects.ts tries strategies in order and
16
+ // keeps going until at least one git repo is found, then stops looking.
17
+ import { readFileSync, existsSync, statSync } from "node:fs";
18
+ import { homedir } from "node:os";
19
+ import { join, dirname, resolve } from "node:path";
20
+ import { createInterface } from "node:readline/promises";
21
+ import { readConfig } from "./config.js";
22
+ /** From explicit `--root <path>` CLI flag(s), or empty if not passed. */
23
+ export function rootsFromFlag(flagValues) {
24
+ return flagValues.map(expandTilde).filter(existsAndIsDir);
25
+ }
26
+ /** From `REPLEN_PROJECT_ROOTS` env var (colon-separated). */
27
+ export function rootsFromEnv() {
28
+ const env = process.env.REPLEN_PROJECT_ROOTS;
29
+ if (!env)
30
+ return [];
31
+ return env.split(":").map((s) => s.trim()).filter(Boolean).map(expandTilde).filter(existsAndIsDir);
32
+ }
33
+ /** From the user's persisted ~/.replen/config.json `projectRoots`. */
34
+ export async function rootsFromConfig() {
35
+ const cfg = await readConfig();
36
+ if (!cfg?.projectRoots?.length)
37
+ return [];
38
+ return cfg.projectRoots.map(expandTilde).filter(existsAndIsDir);
39
+ }
40
+ /**
41
+ * If `npx replen` was invoked from inside a git repo (or one of its
42
+ * subdirs), walk up to find the repo root, then return its parent dir
43
+ * as a candidate root. This catches the common "I'm sitting in one of
44
+ * my projects when I install Replen" case.
45
+ *
46
+ * Stops at `homedir()` and at `/` to avoid walking into system dirs.
47
+ */
48
+ export function rootsFromCwdWalkUp() {
49
+ const cwd = process.cwd();
50
+ const home = homedir();
51
+ let dir = cwd;
52
+ // Walk up until we find a .git/ or hit home/root.
53
+ while (dir !== "/" && dir !== home && dir.length > 1) {
54
+ if (existsSync(join(dir, ".git"))) {
55
+ const parent = dirname(dir);
56
+ // Only suggest the parent if it's still inside home — never offer
57
+ // "/" or anything above $HOME as a scan root.
58
+ if (parent.startsWith(home) && parent !== home)
59
+ return [parent];
60
+ return [];
61
+ }
62
+ dir = dirname(dir);
63
+ }
64
+ return [];
65
+ }
66
+ /**
67
+ * Read ~/.claude.json (Claude Code's per-user config) and extract the
68
+ * project cwds it tracks. Group by parent dir. Return parents that
69
+ * contain ≥2 Claude Code projects — these are very likely the user's
70
+ * actual repo roots.
71
+ *
72
+ * Why this is high-signal: Claude Code only adds an entry when the user
73
+ * opens it inside a real project, so the cwd list is curated by their
74
+ * actual usage. Far more accurate than guessing dir names.
75
+ */
76
+ export function rootsFromClaudeJson() {
77
+ const claudePath = join(homedir(), ".claude.json");
78
+ if (!existsSync(claudePath))
79
+ return [];
80
+ let data;
81
+ try {
82
+ data = JSON.parse(readFileSync(claudePath, "utf8"));
83
+ }
84
+ catch {
85
+ return [];
86
+ }
87
+ if (!data || typeof data !== "object")
88
+ return [];
89
+ const projects = data.projects;
90
+ if (!projects || typeof projects !== "object")
91
+ return [];
92
+ const parentCounts = new Map();
93
+ for (const path of Object.keys(projects)) {
94
+ if (!path.startsWith("/"))
95
+ continue;
96
+ const parent = dirname(path);
97
+ if (!parent.startsWith(homedir()))
98
+ continue;
99
+ parentCounts.set(parent, (parentCounts.get(parent) ?? 0) + 1);
100
+ }
101
+ return Array.from(parentCounts.entries())
102
+ .filter(([, n]) => n >= 2)
103
+ .sort((a, b) => b[1] - a[1])
104
+ .slice(0, 5)
105
+ .map(([parent]) => parent)
106
+ .filter(existsAndIsDir);
107
+ }
108
+ /** Conventional layout: ~/github, ~/code, ~/projects. */
109
+ export function rootsFromHardcoded() {
110
+ return ["github", "code", "projects", "Code", "Projects", "dev", "src", "work"]
111
+ .map((n) => join(homedir(), n))
112
+ .filter(existsAndIsDir);
113
+ }
114
+ /**
115
+ * Last-resort interactive prompt. Asks the user where they keep their
116
+ * code, with a sensible default. Returns null if the user is in a
117
+ * non-interactive context (stdin not a TTY) or hits Ctrl-C.
118
+ */
119
+ export async function promptForRoot() {
120
+ if (!process.stdin.isTTY)
121
+ return null;
122
+ const home = homedir();
123
+ // Suggest the most common parent of any folder we know about, falling
124
+ // back to home itself.
125
+ const suggestion = pickPromptSuggestion() ?? home;
126
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
127
+ try {
128
+ const answer = (await rl.question(`\n Where do you keep your code? [${suggestion}]: `)).trim();
129
+ const chosen = answer || suggestion;
130
+ const expanded = expandTilde(chosen);
131
+ if (!existsAndIsDir(expanded)) {
132
+ console.warn(` ✗ Not a directory: ${expanded}`);
133
+ return null;
134
+ }
135
+ return expanded;
136
+ }
137
+ catch {
138
+ return null;
139
+ }
140
+ finally {
141
+ rl.close();
142
+ }
143
+ }
144
+ function pickPromptSuggestion() {
145
+ const fromCwd = rootsFromCwdWalkUp();
146
+ if (fromCwd[0])
147
+ return fromCwd[0];
148
+ const fromClaude = rootsFromClaudeJson();
149
+ if (fromClaude[0])
150
+ return fromClaude[0];
151
+ const fromHard = rootsFromHardcoded();
152
+ if (fromHard[0])
153
+ return fromHard[0];
154
+ return null;
155
+ }
156
+ function expandTilde(p) {
157
+ if (p === "~" || p.startsWith("~/")) {
158
+ return p === "~" ? homedir() : join(homedir(), p.slice(2));
159
+ }
160
+ return resolve(p);
161
+ }
162
+ function existsAndIsDir(p) {
163
+ try {
164
+ return statSync(p).isDirectory();
165
+ }
166
+ catch {
167
+ return false;
168
+ }
169
+ }
@@ -0,0 +1,140 @@
1
+ // First-time ingest streamer used by `npx replen` after install.
2
+ //
3
+ // Triggers a pipeline run and tails progress until the discovered pool
4
+ // is ready (event signal: `fetch_done` with "Discovered pool" message,
5
+ // or candidates count crosses 0). Then exits cleanly. Stages 1+2 and
6
+ // the scouted-pool fetcher continue server-side in the background;
7
+ // the user can do `npx replen progress` later if they want to watch
8
+ // the rest, or just open Claude Code and the agent will find whatever
9
+ // landed.
10
+ //
11
+ // Why exit early instead of waiting for full pipeline completion: a
12
+ // fresh install with 30+ projects can take 5-10 min for Stage 1+2
13
+ // per-project LLM work to finish. The user shouldn't sit at a blank
14
+ // terminal — they want to know "is there something I can look at now?"
15
+ // and the answer becomes yes once the discovered pool lands (typically
16
+ // 30-60s in).
17
+ //
18
+ // Tolerant of rate-limit and in-flight responses; never fails the
19
+ // install flow on its own — worst case prints a "you can run
20
+ // `npx replen progress` later" hint and returns.
21
+ import { apiGet, apiPost } from "./api.js";
22
+ const POLL_INTERVAL_MS = 2500;
23
+ // Absolute cap on how long we sit in the streamer. Discovered pool
24
+ // usually lands inside 60s but giving generous headroom for slow
25
+ // fetcher tails. If we hit this, we exit with a hint rather than
26
+ // blocking the install flow.
27
+ const MAX_WAIT_MS = 180_000;
28
+ export async function runFirstIngest(cfg) {
29
+ console.log("");
30
+ console.log(" Pulling first batch of candidates from your sources…");
31
+ console.log(" (This takes about a minute. You can `^C` here — ingest continues server-side.)");
32
+ console.log("");
33
+ // Step 1: trigger a run. Tolerant of in-flight / rate-limit.
34
+ let triggered = null;
35
+ try {
36
+ triggered = await apiPost(cfg, "/api/mcp/run-now", {});
37
+ }
38
+ catch (e) {
39
+ // Surface but don't fail install — the cron scheduler will pick
40
+ // up the new projects on its next tick.
41
+ console.log(` · Couldn't trigger an immediate run (${e.message.slice(0, 80)}).`);
42
+ console.log(` The cron scheduler will catch up shortly. Open Claude Code in a tracked repo`);
43
+ console.log(` in a few minutes and the agent will mention any new matches.`);
44
+ return;
45
+ }
46
+ if (!triggered.ok && triggered.status !== "in_flight") {
47
+ console.log(` · Run not started: ${triggered.reason ?? triggered.status}`);
48
+ console.log(` The next scheduled run will pick up the new projects.`);
49
+ return;
50
+ }
51
+ // Step 2: poll status, print events, exit when discovered pool ready.
52
+ let since = 0;
53
+ let firstTick = true;
54
+ let lastCandidates = 0;
55
+ let startedAt = Date.now();
56
+ let discoveredReadyAnnounced = false;
57
+ while (true) {
58
+ if (Date.now() - startedAt > MAX_WAIT_MS) {
59
+ console.log("");
60
+ console.log(" · Ingest still going — exiting the streamer so you're not blocked.");
61
+ console.log(" Run `npx replen progress` to keep watching, or just open Claude Code.");
62
+ return;
63
+ }
64
+ let status;
65
+ try {
66
+ status = await apiGet(cfg, "/api/mcp/status", since ? { since } : undefined);
67
+ }
68
+ catch (e) {
69
+ console.log(` · Lost connection to status endpoint: ${e.message.slice(0, 80)}`);
70
+ console.log(` Ingest still running. Open Claude Code shortly to see matches.`);
71
+ return;
72
+ }
73
+ if (firstTick) {
74
+ const phase = status.phase ?? "starting";
75
+ console.log(` Run #${status.runId ?? "?"} · ${status.inFlight ? `running · phase=${phase}` : "idle"}`);
76
+ firstTick = false;
77
+ }
78
+ for (const ev of status.events ?? []) {
79
+ console.log(` ${marker(ev.kind)} ${ev.message}`);
80
+ if (ev.id > since)
81
+ since = ev.id;
82
+ // The Phase-1 completion signal: pipeline emits a `fetch_done`
83
+ // event whose message starts with "Discovered pool:". Once we
84
+ // see that, the discovered candidates are persisted and visible
85
+ // to replen_match.
86
+ if (ev.kind === "fetch_done" &&
87
+ ev.message.startsWith("Discovered pool:") &&
88
+ !discoveredReadyAnnounced) {
89
+ discoveredReadyAnnounced = true;
90
+ // Don't exit immediately — let the next poll catch any inline
91
+ // events so the user sees a coherent end state, then bail.
92
+ }
93
+ }
94
+ // Fallback signal: if candidates count crossed 0, the discovered
95
+ // pool is functionally ready even if the event ordering didn't
96
+ // emit the expected marker. Belt + suspenders.
97
+ if (!discoveredReadyAnnounced && (status.candidates ?? 0) > lastCandidates && (status.candidates ?? 0) > 0) {
98
+ discoveredReadyAnnounced = true;
99
+ }
100
+ lastCandidates = status.candidates ?? lastCandidates;
101
+ // If the whole pipeline already finished (e.g. existing user with
102
+ // cached vectors — runs end fast), exit cleanly.
103
+ if (!status.inFlight) {
104
+ console.log("");
105
+ console.log(` ✓ Ingest complete · ${status.candidates ?? 0} candidate(s) in inventory.`);
106
+ return;
107
+ }
108
+ // If discovered pool is ready, exit and let the rest run server-
109
+ // side. Stage 1+2 + scouted pool take much longer; no point making
110
+ // the user watch them.
111
+ if (discoveredReadyAnnounced) {
112
+ console.log("");
113
+ console.log(` ✓ First candidates ready (${status.candidates ?? "?"} so far).`);
114
+ console.log(` Per-project relevance refinement continues in the background.`);
115
+ console.log(` Run \`npx replen progress\` later to watch, or just open Claude Code.`);
116
+ return;
117
+ }
118
+ await sleep(POLL_INTERVAL_MS);
119
+ }
120
+ }
121
+ function marker(kind) {
122
+ switch (kind) {
123
+ case "fetch_start":
124
+ case "fetch_done":
125
+ return "›";
126
+ case "scan":
127
+ return "·";
128
+ case "match":
129
+ return "✓";
130
+ case "skip":
131
+ return "·";
132
+ case "error":
133
+ return "✗";
134
+ default:
135
+ return "·";
136
+ }
137
+ }
138
+ function sleep(ms) {
139
+ return new Promise((resolve) => setTimeout(resolve, ms));
140
+ }