@karmaniverous/jeeves-watcher 0.17.10 → 0.18.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/README.md +128 -0
- package/config.schema.json +465 -176
- package/dist/cli/jeeves-watcher/{index-07J6R3B8.js → index-DtyqhT2s.js} +1590 -21
- package/dist/cli/jeeves-watcher/index.js +18 -60
- package/dist/index.d.ts +285 -5
- package/dist/index.js +1669 -142
- package/package.json +71 -72
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createConfigQueryHandler as createConfigQueryHandler$1, createStatusHandler, createConfigApplyHandler, DEFAULT_PORTS, getBindAddress } from '@karmaniverous/jeeves';
|
|
3
|
-
import {
|
|
3
|
+
import { a as normalizeSlashes, n as normalizeError, r as retry, G as GitignoreFilter, e as executeReindex, V as VALID_REINDEX_SCOPES, m as mergeInferenceRules, i as isPathWatched, g as getWatchRootBases, C as CONFIG_WATCH_VALID_SCOPES, s as sleep, l as loadConfig } from './index.js';
|
|
4
4
|
export { b as startFromConfig } from './index.js';
|
|
5
5
|
import { mkdirSync, existsSync, readFileSync, writeFileSync, rmSync, renameSync, readdirSync, statSync } from 'node:fs';
|
|
6
|
-
import { join, dirname, resolve, extname, basename, isAbsolute } from 'node:path';
|
|
6
|
+
import { join, dirname, resolve, extname, basename, relative, sep, isAbsolute } from 'node:path';
|
|
7
7
|
import Database from 'better-sqlite3';
|
|
8
8
|
import { z } from 'zod';
|
|
9
9
|
import Ajv from 'ajv';
|
|
10
10
|
import addFormats from 'ajv-formats';
|
|
11
11
|
import picomatch from 'picomatch';
|
|
12
12
|
import { deserialize, serialize } from 'node:v8';
|
|
13
|
+
import { normalizeWatchPaths, extractWatchPathStrings, inferenceRuleSchema, jeevesWatcherConfigSchema } from '@karmaniverous/jeeves-watcher-core';
|
|
14
|
+
import { execFile } from 'node:child_process';
|
|
15
|
+
import { promisify } from 'node:util';
|
|
16
|
+
import { homedir } from 'node:os';
|
|
17
|
+
import { access, rm, readFile, writeFile, appendFile, mkdir, stat } from 'node:fs/promises';
|
|
13
18
|
import { pathToFileURL, fileURLToPath } from 'node:url';
|
|
14
19
|
import { packageDirectorySync } from 'package-directory';
|
|
15
20
|
import { JsonMap } from '@karmaniverous/jsonmap';
|
|
@@ -24,12 +29,10 @@ import { unified } from 'unified';
|
|
|
24
29
|
import yaml from 'js-yaml';
|
|
25
30
|
import chokidar from 'chokidar';
|
|
26
31
|
import Fastify from 'fastify';
|
|
27
|
-
import { inferenceRuleSchema, jeevesWatcherConfigSchema } from '@karmaniverous/jeeves-watcher-core';
|
|
28
32
|
import crypto from 'crypto';
|
|
29
33
|
import https from 'node:https';
|
|
30
34
|
import { createHash } from 'node:crypto';
|
|
31
35
|
import pino from 'pino';
|
|
32
|
-
import { readFile, stat } from 'node:fs/promises';
|
|
33
36
|
import { v5 } from 'uuid';
|
|
34
37
|
import * as cheerio from 'cheerio';
|
|
35
38
|
import mammoth from 'mammoth';
|
|
@@ -744,6 +747,835 @@ class ValuesManager extends BinaryFileStore {
|
|
|
744
747
|
}
|
|
745
748
|
}
|
|
746
749
|
|
|
750
|
+
/**
|
|
751
|
+
* @module vcs/CommitMessageGenerator
|
|
752
|
+
* Generates AI-powered commit messages via the Anthropic Messages API.
|
|
753
|
+
*/
|
|
754
|
+
/** Maximum characters of diff to send to the AI. */
|
|
755
|
+
const MAX_DIFF_CHARS = 4000;
|
|
756
|
+
/** Maximum length of the generated commit message. */
|
|
757
|
+
const MAX_MESSAGE_LENGTH = 100;
|
|
758
|
+
/** Request timeout in milliseconds. */
|
|
759
|
+
const REQUEST_TIMEOUT_MS = 15000;
|
|
760
|
+
/** System prompt for the commit message generator. */
|
|
761
|
+
const SYSTEM_PROMPT = 'You are a commit message generator. Given file changes, produce a single concise one-line commit message. No prefix. No punctuation at end';
|
|
762
|
+
/**
|
|
763
|
+
* Generates AI commit messages using the Anthropic Messages API.
|
|
764
|
+
*/
|
|
765
|
+
class CommitMessageGenerator {
|
|
766
|
+
provider;
|
|
767
|
+
model;
|
|
768
|
+
apiKey;
|
|
769
|
+
logger;
|
|
770
|
+
constructor(provider, model, apiKey, logger) {
|
|
771
|
+
this.provider = provider;
|
|
772
|
+
this.model = model;
|
|
773
|
+
this.apiKey = apiKey;
|
|
774
|
+
this.logger = logger;
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Generate a commit message from file changes.
|
|
778
|
+
*
|
|
779
|
+
* @param files - List of changed file paths.
|
|
780
|
+
* @param diffSummary - Combined diff output (stat + patch).
|
|
781
|
+
* @returns A one-line commit message, or null on failure.
|
|
782
|
+
*/
|
|
783
|
+
async generate(files, diffSummary) {
|
|
784
|
+
if (!this.apiKey) {
|
|
785
|
+
this.logger.warn('No API key configured for commit message generation');
|
|
786
|
+
return null;
|
|
787
|
+
}
|
|
788
|
+
if (this.provider !== 'anthropic') {
|
|
789
|
+
this.logger.warn({ provider: this.provider }, 'Unsupported commit message provider; only "anthropic" is supported');
|
|
790
|
+
return null;
|
|
791
|
+
}
|
|
792
|
+
const truncatedDiff = diffSummary.slice(0, MAX_DIFF_CHARS);
|
|
793
|
+
const userPrompt = `Files changed:\n${files.join('\n')}\n\nDiff:\n${truncatedDiff}`;
|
|
794
|
+
try {
|
|
795
|
+
const body = JSON.stringify({
|
|
796
|
+
model: this.model,
|
|
797
|
+
max_tokens: 256,
|
|
798
|
+
system: SYSTEM_PROMPT,
|
|
799
|
+
messages: [{ role: 'user', content: userPrompt }],
|
|
800
|
+
});
|
|
801
|
+
const response = await fetch('https://api.anthropic.com/v1/messages', {
|
|
802
|
+
method: 'POST',
|
|
803
|
+
headers: {
|
|
804
|
+
'Content-Type': 'application/json',
|
|
805
|
+
'x-api-key': this.apiKey,
|
|
806
|
+
'anthropic-version': '2023-06-01',
|
|
807
|
+
},
|
|
808
|
+
body,
|
|
809
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
810
|
+
});
|
|
811
|
+
if (!response.ok) {
|
|
812
|
+
const text = await response.text();
|
|
813
|
+
this.logger.warn({ status: response.status, body: text }, 'Anthropic API returned non-OK status');
|
|
814
|
+
return null;
|
|
815
|
+
}
|
|
816
|
+
const data = (await response.json());
|
|
817
|
+
const textBlock = data.content?.find((b) => b.type === 'text');
|
|
818
|
+
if (!textBlock?.text) {
|
|
819
|
+
this.logger.warn('Anthropic API returned no text content');
|
|
820
|
+
return null;
|
|
821
|
+
}
|
|
822
|
+
const message = textBlock.text.trim().slice(0, MAX_MESSAGE_LENGTH);
|
|
823
|
+
this.logger.debug({ message }, 'AI commit message generated');
|
|
824
|
+
return message;
|
|
825
|
+
}
|
|
826
|
+
catch (error) {
|
|
827
|
+
this.logger.warn({ err: normalizeError(error) }, 'Failed to generate AI commit message');
|
|
828
|
+
return null;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* @module vcs/gitExec
|
|
835
|
+
* Shared git execution utilities.
|
|
836
|
+
*/
|
|
837
|
+
/** Promisified execFile for git commands. */
|
|
838
|
+
const execFileAsync = promisify(execFile);
|
|
839
|
+
/**
|
|
840
|
+
* Find the longest-prefix-match root for a normalized path.
|
|
841
|
+
* Roots must be sorted longest-first for correct nested matching.
|
|
842
|
+
*
|
|
843
|
+
* @param roots - Root paths sorted longest-first.
|
|
844
|
+
* @param normalizedPath - Normalized absolute path (forward slashes).
|
|
845
|
+
* @returns The matching root, or undefined.
|
|
846
|
+
*/
|
|
847
|
+
function findRootForPath(roots, normalizedPath) {
|
|
848
|
+
for (const root of roots) {
|
|
849
|
+
if (normalizedPath === root || normalizedPath.startsWith(root + '/')) {
|
|
850
|
+
return root;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
return undefined;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* @module vcs/resolveCommitMessageApiKey
|
|
858
|
+
* Resolves the API key for AI commit message generation.
|
|
859
|
+
*/
|
|
860
|
+
/**
|
|
861
|
+
* Resolve the API key for commit message generation.
|
|
862
|
+
* Checks config first, then falls back to OpenClaw gateway credentials.
|
|
863
|
+
*/
|
|
864
|
+
function resolveCommitMessageApiKey(provider, configApiKey, logger) {
|
|
865
|
+
if (configApiKey) {
|
|
866
|
+
logger.debug('Using commit message API key from config');
|
|
867
|
+
return configApiKey;
|
|
868
|
+
}
|
|
869
|
+
try {
|
|
870
|
+
const openclawPath = join(homedir(), '.openclaw', 'openclaw.json');
|
|
871
|
+
const raw = readFileSync(openclawPath, 'utf8');
|
|
872
|
+
const parsed = JSON.parse(raw);
|
|
873
|
+
const gatewayKey = parsed.models?.providers?.[provider]?.apiKey;
|
|
874
|
+
if (gatewayKey) {
|
|
875
|
+
logger.debug({ provider }, 'Using commit message API key from OpenClaw gateway config');
|
|
876
|
+
return gatewayKey;
|
|
877
|
+
}
|
|
878
|
+
logger.warn({ provider }, 'OpenClaw gateway config found but no API key for provider');
|
|
879
|
+
}
|
|
880
|
+
catch {
|
|
881
|
+
logger.warn('No commit message API key in config or OpenClaw gateway');
|
|
882
|
+
}
|
|
883
|
+
return undefined;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* @module vcs/SquashManager
|
|
888
|
+
* Handles scheduled squash retention for git-backed VCS roots.
|
|
889
|
+
*/
|
|
890
|
+
/**
|
|
891
|
+
* Parse a standard 5-field cron expression and check if the current time matches.
|
|
892
|
+
* Fields: minute hour dom month dow
|
|
893
|
+
* Supports: numbers, wildcards, lists (1,3,5), ranges (1-5), steps (star/5)
|
|
894
|
+
*/
|
|
895
|
+
function cronMatchesNow(cronExpr, now = new Date()) {
|
|
896
|
+
const fields = cronExpr.trim().split(/\s+/);
|
|
897
|
+
if (fields.length !== 5)
|
|
898
|
+
return false;
|
|
899
|
+
const minute = now.getMinutes();
|
|
900
|
+
const hour = now.getHours();
|
|
901
|
+
const dom = now.getDate();
|
|
902
|
+
const month = now.getMonth() + 1; // 1-based
|
|
903
|
+
const dow = now.getDay(); // 0=Sunday
|
|
904
|
+
return (fieldMatches(fields[0], minute, 0, 59) &&
|
|
905
|
+
fieldMatches(fields[1], hour, 0, 23) &&
|
|
906
|
+
fieldMatches(fields[2], dom, 1, 31) &&
|
|
907
|
+
fieldMatches(fields[3], month, 1, 12) &&
|
|
908
|
+
fieldMatches(fields[4], dow, 0, 7) // 7 also means Sunday
|
|
909
|
+
);
|
|
910
|
+
}
|
|
911
|
+
function fieldMatches(field, value, min, max) {
|
|
912
|
+
for (const part of field.split(',')) {
|
|
913
|
+
if (partMatches(part.trim(), value, min, max))
|
|
914
|
+
return true;
|
|
915
|
+
}
|
|
916
|
+
return false;
|
|
917
|
+
}
|
|
918
|
+
function partMatches(part, value, min, max) {
|
|
919
|
+
// Handle step: */N or range/N
|
|
920
|
+
const [rangePart, stepStr] = part.split('/');
|
|
921
|
+
const step = stepStr ? parseInt(stepStr, 10) : 1;
|
|
922
|
+
if (rangePart === '*') {
|
|
923
|
+
return (value - min) % step === 0;
|
|
924
|
+
}
|
|
925
|
+
// Handle range: N-M
|
|
926
|
+
if (rangePart.includes('-')) {
|
|
927
|
+
const [startStr, endStr] = rangePart.split('-');
|
|
928
|
+
const start = parseInt(startStr, 10);
|
|
929
|
+
const end = parseInt(endStr, 10);
|
|
930
|
+
if (value < start || value > end)
|
|
931
|
+
return false;
|
|
932
|
+
return (value - start) % step === 0;
|
|
933
|
+
}
|
|
934
|
+
// Plain number
|
|
935
|
+
const num = parseInt(rangePart, 10);
|
|
936
|
+
if (step === 1) {
|
|
937
|
+
// For dow, treat 7 as 0 (Sunday)
|
|
938
|
+
if (max === 7 && num === 7)
|
|
939
|
+
return value === 0;
|
|
940
|
+
return value === num;
|
|
941
|
+
}
|
|
942
|
+
// Number with step doesn't make sense for single values, treat as exact
|
|
943
|
+
return value === num;
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
* Manages scheduled squash retention for a single VCS root.
|
|
947
|
+
*
|
|
948
|
+
* Runs on a cron schedule (checked every 60s). When triggered, squashes
|
|
949
|
+
* commits older than the retention boundary (the tighter of maxAgeDays
|
|
950
|
+
* and maxVersions) into a single "historical baseline" orphan commit,
|
|
951
|
+
* then cherry-picks retained commits on top. Force-pushes to remote
|
|
952
|
+
* if configured (D13).
|
|
953
|
+
*/
|
|
954
|
+
class SquashManager {
|
|
955
|
+
rootPath;
|
|
956
|
+
retention;
|
|
957
|
+
remoteUrl;
|
|
958
|
+
accessToken;
|
|
959
|
+
logger;
|
|
960
|
+
intervalTimer;
|
|
961
|
+
running = false;
|
|
962
|
+
constructor(rootPath, retention, logger, remoteUrl, accessToken) {
|
|
963
|
+
this.rootPath = rootPath;
|
|
964
|
+
this.retention = retention;
|
|
965
|
+
this.logger = logger;
|
|
966
|
+
this.remoteUrl = remoteUrl;
|
|
967
|
+
this.accessToken = accessToken;
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Start the cron-based squash scheduler.
|
|
971
|
+
* Checks every 60 seconds if the cron expression matches.
|
|
972
|
+
*/
|
|
973
|
+
start() {
|
|
974
|
+
if (this.running)
|
|
975
|
+
return;
|
|
976
|
+
this.running = true;
|
|
977
|
+
this.intervalTimer = setInterval(() => {
|
|
978
|
+
if (cronMatchesNow(this.retention.squashCron)) {
|
|
979
|
+
void this.runSquash();
|
|
980
|
+
}
|
|
981
|
+
}, 60_000);
|
|
982
|
+
this.logger.info({ root: this.rootPath, cron: this.retention.squashCron }, 'SquashManager started');
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* Stop the cron scheduler.
|
|
986
|
+
*/
|
|
987
|
+
stop() {
|
|
988
|
+
this.running = false;
|
|
989
|
+
if (this.intervalTimer !== undefined) {
|
|
990
|
+
clearInterval(this.intervalTimer);
|
|
991
|
+
this.intervalTimer = undefined;
|
|
992
|
+
}
|
|
993
|
+
this.logger.info({ root: this.rootPath }, 'SquashManager stopped');
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Run the squash operation. Exposed publicly for testing.
|
|
997
|
+
*/
|
|
998
|
+
async runSquash() {
|
|
999
|
+
// Check for index.lock before starting multi-step operation
|
|
1000
|
+
const lockPath = join(this.rootPath, '.git', 'index.lock');
|
|
1001
|
+
try {
|
|
1002
|
+
await access(lockPath);
|
|
1003
|
+
// Lock exists — abort and retry next cycle
|
|
1004
|
+
this.logger.warn({ root: this.rootPath }, 'Squash aborted: index.lock exists, will retry next cycle');
|
|
1005
|
+
return { squashed: false, error: 'index.lock exists' };
|
|
1006
|
+
}
|
|
1007
|
+
catch {
|
|
1008
|
+
// No lock — proceed
|
|
1009
|
+
}
|
|
1010
|
+
try {
|
|
1011
|
+
const commits = await this.getCommitLog();
|
|
1012
|
+
if (commits.length <= 1) {
|
|
1013
|
+
return { squashed: false };
|
|
1014
|
+
}
|
|
1015
|
+
const boundaryIndex = this.calculateRetentionBoundary(commits);
|
|
1016
|
+
if (boundaryIndex <= 0) {
|
|
1017
|
+
// All commits are within retention or only one commit before boundary
|
|
1018
|
+
return { squashed: false };
|
|
1019
|
+
}
|
|
1020
|
+
// Perform the squash
|
|
1021
|
+
await this.performSquash(commits, boundaryIndex);
|
|
1022
|
+
const commitsRemoved = boundaryIndex;
|
|
1023
|
+
const commitsRetained = commits.length - boundaryIndex;
|
|
1024
|
+
this.logger.info({ root: this.rootPath, commitsRemoved, commitsRetained }, 'Squash retention completed');
|
|
1025
|
+
// Force push if remote configured
|
|
1026
|
+
await this.forcePushIfConfigured();
|
|
1027
|
+
return { squashed: true, commitsRemoved, commitsRetained };
|
|
1028
|
+
}
|
|
1029
|
+
catch (error) {
|
|
1030
|
+
const message = normalizeError(error).message;
|
|
1031
|
+
this.logger.error({ root: this.rootPath, err: normalizeError(error) }, 'Squash operation failed');
|
|
1032
|
+
return { squashed: false, error: message };
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Get all commits in chronological order (oldest first).
|
|
1037
|
+
*/
|
|
1038
|
+
async getCommitLog() {
|
|
1039
|
+
try {
|
|
1040
|
+
const { stdout } = await execFileAsync('git', ['log', '--format=%H %aI', '--reverse'], { cwd: this.rootPath });
|
|
1041
|
+
if (!stdout.trim())
|
|
1042
|
+
return [];
|
|
1043
|
+
return stdout
|
|
1044
|
+
.trim()
|
|
1045
|
+
.split('\n')
|
|
1046
|
+
.map((line) => {
|
|
1047
|
+
const spaceIdx = line.indexOf(' ');
|
|
1048
|
+
return {
|
|
1049
|
+
hash: line.slice(0, spaceIdx),
|
|
1050
|
+
date: new Date(line.slice(spaceIdx + 1)),
|
|
1051
|
+
};
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
catch {
|
|
1055
|
+
return [];
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
/**
|
|
1059
|
+
* Calculate the retention boundary index.
|
|
1060
|
+
* Returns the index of the oldest commit to KEEP.
|
|
1061
|
+
* All commits before this index will be squashed into a baseline.
|
|
1062
|
+
*
|
|
1063
|
+
* Strategy: find the tighter constraint between age and count.
|
|
1064
|
+
* - Age: keep commits newer than maxAgeDays
|
|
1065
|
+
* - Count: keep the last maxVersions commits
|
|
1066
|
+
* The boundary is the one that retains FEWER old commits (tighter).
|
|
1067
|
+
*/
|
|
1068
|
+
calculateRetentionBoundary(commits) {
|
|
1069
|
+
const now = new Date();
|
|
1070
|
+
const maxAgeMs = this.retention.maxAgeDays * 24 * 60 * 60 * 1000;
|
|
1071
|
+
const cutoffDate = new Date(now.getTime() - maxAgeMs);
|
|
1072
|
+
// Find the first commit that is within the age window (to keep)
|
|
1073
|
+
let ageBoundary = commits.length; // Default: all too old
|
|
1074
|
+
for (let i = 0; i < commits.length; i++) {
|
|
1075
|
+
if (commits[i].date >= cutoffDate) {
|
|
1076
|
+
ageBoundary = i;
|
|
1077
|
+
break;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
// Count boundary: keep the last maxVersions commits
|
|
1081
|
+
const countBoundary = Math.max(0, commits.length - this.retention.maxVersions);
|
|
1082
|
+
// The tighter constraint wins (keeps fewer old commits = higher boundary index)
|
|
1083
|
+
return Math.max(ageBoundary, countBoundary);
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* Perform the squash: create orphan with baseline + cherry-pick retained commits.
|
|
1087
|
+
*/
|
|
1088
|
+
async performSquash(commits, boundaryIndex) {
|
|
1089
|
+
// The commit just before boundary is the last one to squash
|
|
1090
|
+
const baselineHash = commits[boundaryIndex - 1].hash;
|
|
1091
|
+
// Get the current branch name
|
|
1092
|
+
const { stdout: branchOut } = await execFileAsync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: this.rootPath });
|
|
1093
|
+
const currentBranch = branchOut.trim();
|
|
1094
|
+
const orphanBranch = `__squash_orphan_${String(Date.now())}`;
|
|
1095
|
+
try {
|
|
1096
|
+
// 1. Create orphan branch with the tree state at the baseline commit
|
|
1097
|
+
await execFileAsync('git', ['checkout', '--orphan', orphanBranch], {
|
|
1098
|
+
cwd: this.rootPath,
|
|
1099
|
+
});
|
|
1100
|
+
// 2. Reset index to the baseline tree (the last squashed commit's tree)
|
|
1101
|
+
await execFileAsync('git', ['reset', '--hard', baselineHash], {
|
|
1102
|
+
cwd: this.rootPath,
|
|
1103
|
+
});
|
|
1104
|
+
// 3. Create the baseline commit with the same tree
|
|
1105
|
+
// Use commit-tree for a clean orphan commit
|
|
1106
|
+
const { stdout: treeOut } = await execFileAsync('git', ['rev-parse', `${baselineHash}^{tree}`], { cwd: this.rootPath });
|
|
1107
|
+
const treeHash = treeOut.trim();
|
|
1108
|
+
const { stdout: commitOut } = await execFileAsync('git', ['commit-tree', treeHash, '-m', 'historical baseline'], { cwd: this.rootPath });
|
|
1109
|
+
const baselineCommitHash = commitOut.trim();
|
|
1110
|
+
// 4. Reset orphan branch to this new baseline commit
|
|
1111
|
+
await execFileAsync('git', ['reset', '--hard', baselineCommitHash], {
|
|
1112
|
+
cwd: this.rootPath,
|
|
1113
|
+
});
|
|
1114
|
+
// 5. Cherry-pick boundary..HEAD (the retained commits)
|
|
1115
|
+
// Get the list of commits to cherry-pick (from boundary to the end)
|
|
1116
|
+
const commitsToCherry = commits.slice(boundaryIndex).map((c) => c.hash);
|
|
1117
|
+
if (commitsToCherry.length > 0) {
|
|
1118
|
+
await execFileAsync('git', ['cherry-pick', ...commitsToCherry], {
|
|
1119
|
+
cwd: this.rootPath,
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
// 6. Force-update the original branch to the orphan
|
|
1123
|
+
const { stdout: newHead } = await execFileAsync('git', ['rev-parse', 'HEAD'], { cwd: this.rootPath });
|
|
1124
|
+
await execFileAsync('git', ['branch', '-f', currentBranch, newHead.trim()], { cwd: this.rootPath });
|
|
1125
|
+
// 7. Switch back to original branch
|
|
1126
|
+
await execFileAsync('git', ['checkout', currentBranch], {
|
|
1127
|
+
cwd: this.rootPath,
|
|
1128
|
+
});
|
|
1129
|
+
// 8. Delete orphan branch
|
|
1130
|
+
await execFileAsync('git', ['branch', '-D', orphanBranch], {
|
|
1131
|
+
cwd: this.rootPath,
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1134
|
+
catch (error) {
|
|
1135
|
+
// Attempt cleanup: try to get back to original branch
|
|
1136
|
+
try {
|
|
1137
|
+
await execFileAsync('git', ['checkout', '-f', currentBranch], {
|
|
1138
|
+
cwd: this.rootPath,
|
|
1139
|
+
});
|
|
1140
|
+
await execFileAsync('git', ['branch', '-D', orphanBranch], {
|
|
1141
|
+
cwd: this.rootPath,
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
catch {
|
|
1145
|
+
// Cleanup failed — log but re-throw original error
|
|
1146
|
+
}
|
|
1147
|
+
// Remove index.lock if left behind
|
|
1148
|
+
try {
|
|
1149
|
+
const lockFile = join(this.rootPath, '.git', 'index.lock');
|
|
1150
|
+
await rm(lockFile, { force: true });
|
|
1151
|
+
}
|
|
1152
|
+
catch {
|
|
1153
|
+
// ignore
|
|
1154
|
+
}
|
|
1155
|
+
throw error;
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
/**
|
|
1159
|
+
* Force push to remote after squash (history rewrite requires force).
|
|
1160
|
+
*/
|
|
1161
|
+
async forcePushIfConfigured() {
|
|
1162
|
+
if (!this.remoteUrl)
|
|
1163
|
+
return;
|
|
1164
|
+
try {
|
|
1165
|
+
const pushUrl = this.accessToken
|
|
1166
|
+
? this.remoteUrl.replace(/^https:\/\//, `https://${encodeURIComponent(this.accessToken)}@`)
|
|
1167
|
+
: this.remoteUrl;
|
|
1168
|
+
await execFileAsync('git', ['push', '--force', pushUrl, 'HEAD'], {
|
|
1169
|
+
cwd: this.rootPath,
|
|
1170
|
+
});
|
|
1171
|
+
this.logger.info({ root: this.rootPath, remote: this.remoteUrl }, 'Squash force push succeeded');
|
|
1172
|
+
}
|
|
1173
|
+
catch (error) {
|
|
1174
|
+
this.logger.error({ root: this.rootPath, err: normalizeError(error) }, 'Squash force push failed');
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
/**
|
|
1180
|
+
* @module vcs/VcsManager
|
|
1181
|
+
* Per-root VCS manager for git-backed content versioning.
|
|
1182
|
+
*/
|
|
1183
|
+
/**
|
|
1184
|
+
* Per-root VCS manager for git-backed content versioning.
|
|
1185
|
+
*
|
|
1186
|
+
* Each VCS-enabled watch root gets its own VcsManager instance, which owns
|
|
1187
|
+
* a debounced commit pipeline: file changes are batched, staged, committed
|
|
1188
|
+
* (with optional AI-generated messages), and optionally pushed to a remote.
|
|
1189
|
+
*
|
|
1190
|
+
* Concurrency: only one commit is in-flight at a time per root. Index.lock
|
|
1191
|
+
* contention is handled with exponential backoff retries (D10).
|
|
1192
|
+
*/
|
|
1193
|
+
class VcsManager {
|
|
1194
|
+
config;
|
|
1195
|
+
rootPath;
|
|
1196
|
+
remoteUrl;
|
|
1197
|
+
accessToken;
|
|
1198
|
+
logger;
|
|
1199
|
+
commitMessageGenerator;
|
|
1200
|
+
pending = new Set();
|
|
1201
|
+
pendingReversions = [];
|
|
1202
|
+
_pushErrors = [];
|
|
1203
|
+
squashManager;
|
|
1204
|
+
debounceTimer;
|
|
1205
|
+
commitInFlight = Promise.resolve();
|
|
1206
|
+
started = false;
|
|
1207
|
+
_lastPushTime = null;
|
|
1208
|
+
constructor(rootPath, config, logger, commitMessageGenerator, remoteUrl, accessToken) {
|
|
1209
|
+
this.rootPath = rootPath;
|
|
1210
|
+
this.config = config;
|
|
1211
|
+
this.logger = logger;
|
|
1212
|
+
this.commitMessageGenerator = commitMessageGenerator;
|
|
1213
|
+
this.remoteUrl = remoteUrl;
|
|
1214
|
+
this.accessToken = accessToken;
|
|
1215
|
+
if (config.retention) {
|
|
1216
|
+
this.squashManager = new SquashManager(rootPath, config.retention, logger, remoteUrl, accessToken);
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
get lastPushTime() {
|
|
1220
|
+
return this._lastPushTime;
|
|
1221
|
+
}
|
|
1222
|
+
get pushErrors() {
|
|
1223
|
+
return this._pushErrors;
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Begin accepting file changes. Sets up the debounce mechanism.
|
|
1227
|
+
*/
|
|
1228
|
+
start() {
|
|
1229
|
+
this.started = true;
|
|
1230
|
+
this.squashManager?.start();
|
|
1231
|
+
this.logger.info({ root: this.rootPath }, 'VcsManager started');
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Add a file to the pending set and reset the debounce timer.
|
|
1235
|
+
* If the pending set exceeds maxBatchSize, flush immediately.
|
|
1236
|
+
*
|
|
1237
|
+
* @param filePath - Absolute path of the changed file.
|
|
1238
|
+
*/
|
|
1239
|
+
fileChanged(filePath) {
|
|
1240
|
+
if (!this.started)
|
|
1241
|
+
return;
|
|
1242
|
+
this.pending.add(filePath);
|
|
1243
|
+
if (this.pending.size >= this.config.maxBatchSize) {
|
|
1244
|
+
this.clearDebounce();
|
|
1245
|
+
const batch = this.takeBatch(this.config.maxBatchSize);
|
|
1246
|
+
this.commitInFlight = this.commitInFlight.then(() => this.commitBatch(batch));
|
|
1247
|
+
if (this.pending.size > 0) {
|
|
1248
|
+
this.resetDebounce();
|
|
1249
|
+
}
|
|
1250
|
+
return;
|
|
1251
|
+
}
|
|
1252
|
+
this.resetDebounce();
|
|
1253
|
+
}
|
|
1254
|
+
/**
|
|
1255
|
+
* Record reversion metadata so the next commit uses a revert-prefixed message.
|
|
1256
|
+
*
|
|
1257
|
+
* @param reversion - The reversion metadata to record.
|
|
1258
|
+
*/
|
|
1259
|
+
addPendingReversion(reversion) {
|
|
1260
|
+
this.pendingReversions.push(reversion);
|
|
1261
|
+
}
|
|
1262
|
+
/**
|
|
1263
|
+
* Stage a file deletion and add to pending set.
|
|
1264
|
+
* git add handles deleted files when the file is gone from disk.
|
|
1265
|
+
*
|
|
1266
|
+
* @param filePath - Absolute path of the deleted file.
|
|
1267
|
+
*/
|
|
1268
|
+
handleUnlink(filePath) {
|
|
1269
|
+
if (!this.started)
|
|
1270
|
+
return;
|
|
1271
|
+
this.fileChanged(filePath);
|
|
1272
|
+
}
|
|
1273
|
+
/**
|
|
1274
|
+
* Flush all pending files immediately.
|
|
1275
|
+
*/
|
|
1276
|
+
async flush() {
|
|
1277
|
+
this.clearDebounce();
|
|
1278
|
+
if (this.pending.size > 0) {
|
|
1279
|
+
const batch = [...this.pending];
|
|
1280
|
+
this.pending.clear();
|
|
1281
|
+
this.commitInFlight = this.commitInFlight.then(() => this.commitBatch(batch));
|
|
1282
|
+
}
|
|
1283
|
+
await this.commitInFlight;
|
|
1284
|
+
}
|
|
1285
|
+
/**
|
|
1286
|
+
* Stop accepting file changes, flush pending, and clean up timers.
|
|
1287
|
+
*/
|
|
1288
|
+
async stop() {
|
|
1289
|
+
this.started = false;
|
|
1290
|
+
this.squashManager?.stop();
|
|
1291
|
+
await this.flush();
|
|
1292
|
+
this.logger.info({ root: this.rootPath }, 'VcsManager stopped');
|
|
1293
|
+
}
|
|
1294
|
+
/**
|
|
1295
|
+
* Take up to N items from the pending set.
|
|
1296
|
+
*/
|
|
1297
|
+
takeBatch(n) {
|
|
1298
|
+
const batch = [];
|
|
1299
|
+
for (const item of this.pending) {
|
|
1300
|
+
if (batch.length >= n)
|
|
1301
|
+
break;
|
|
1302
|
+
batch.push(item);
|
|
1303
|
+
}
|
|
1304
|
+
for (const item of batch) {
|
|
1305
|
+
this.pending.delete(item);
|
|
1306
|
+
}
|
|
1307
|
+
return batch;
|
|
1308
|
+
}
|
|
1309
|
+
/**
|
|
1310
|
+
* Clear the debounce timer.
|
|
1311
|
+
*/
|
|
1312
|
+
clearDebounce() {
|
|
1313
|
+
if (this.debounceTimer !== undefined) {
|
|
1314
|
+
clearTimeout(this.debounceTimer);
|
|
1315
|
+
this.debounceTimer = undefined;
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
/**
|
|
1319
|
+
* Reset the debounce timer to fire flush() after commitDebounceMs.
|
|
1320
|
+
*/
|
|
1321
|
+
resetDebounce() {
|
|
1322
|
+
this.clearDebounce();
|
|
1323
|
+
this.debounceTimer = setTimeout(() => {
|
|
1324
|
+
this.debounceTimer = undefined;
|
|
1325
|
+
void this.flush();
|
|
1326
|
+
}, this.config.commitDebounceMs);
|
|
1327
|
+
}
|
|
1328
|
+
/**
|
|
1329
|
+
* Get the cached diff for staged files, for AI context.
|
|
1330
|
+
*/
|
|
1331
|
+
async getStagedDiff() {
|
|
1332
|
+
try {
|
|
1333
|
+
const { stdout: stat } = await execFileAsync('git', ['diff', '--cached', '--stat'], { cwd: this.rootPath });
|
|
1334
|
+
const { stdout: patch } = await execFileAsync('git', ['diff', '--cached'], { cwd: this.rootPath });
|
|
1335
|
+
return `${stat}\n${patch}`;
|
|
1336
|
+
}
|
|
1337
|
+
catch {
|
|
1338
|
+
return '';
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
/**
|
|
1342
|
+
* Build the template commit message (no AI).
|
|
1343
|
+
*/
|
|
1344
|
+
buildTemplateMessage(fileCount) {
|
|
1345
|
+
if (this.pendingReversions.length === 0) {
|
|
1346
|
+
const timestamp = new Date().toISOString();
|
|
1347
|
+
return `watcher: batch ${timestamp} (${String(fileCount)} files)`;
|
|
1348
|
+
}
|
|
1349
|
+
// Count total reverted files across all pending reversions
|
|
1350
|
+
const revertedPaths = new Set(this.pendingReversions.flatMap((r) => r.paths));
|
|
1351
|
+
const revertedCount = revertedPaths.size;
|
|
1352
|
+
const otherCount = fileCount - revertedCount;
|
|
1353
|
+
// Use the first reversion's glob and commit for the message
|
|
1354
|
+
const { glob, commit } = this.pendingReversions[0];
|
|
1355
|
+
const shortCommit = commit.slice(0, 7);
|
|
1356
|
+
let message = `revert: ${glob} to ${shortCommit} — restored ${String(revertedCount)} files`;
|
|
1357
|
+
if (otherCount > 0) {
|
|
1358
|
+
message += ` (+ ${String(otherCount)} other changes)`;
|
|
1359
|
+
}
|
|
1360
|
+
return message;
|
|
1361
|
+
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Build the commit message, using AI when available with template fallback.
|
|
1364
|
+
*/
|
|
1365
|
+
async buildCommitMessage(files) {
|
|
1366
|
+
const fileCount = files.length;
|
|
1367
|
+
const templateMessage = this.buildTemplateMessage(fileCount);
|
|
1368
|
+
if (!this.commitMessageGenerator) {
|
|
1369
|
+
return templateMessage;
|
|
1370
|
+
}
|
|
1371
|
+
try {
|
|
1372
|
+
const diffSummary = await this.getStagedDiff();
|
|
1373
|
+
const aiMessage = await this.commitMessageGenerator.generate(files, diffSummary);
|
|
1374
|
+
if (!aiMessage) {
|
|
1375
|
+
this.logger.warn('AI commit message generation returned null; using template');
|
|
1376
|
+
return templateMessage;
|
|
1377
|
+
}
|
|
1378
|
+
// For reversions: prefix with revert info + AI description
|
|
1379
|
+
if (this.pendingReversions.length > 0) {
|
|
1380
|
+
const { glob, commit } = this.pendingReversions[0];
|
|
1381
|
+
const shortCommit = commit.slice(0, 7);
|
|
1382
|
+
return `revert: ${glob} to ${shortCommit} — ${aiMessage}`;
|
|
1383
|
+
}
|
|
1384
|
+
return aiMessage;
|
|
1385
|
+
}
|
|
1386
|
+
catch (error) {
|
|
1387
|
+
this.logger.warn({ err: normalizeError(error) }, 'AI commit message generation failed; using template');
|
|
1388
|
+
return templateMessage;
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
/**
|
|
1392
|
+
* Commit a batch of files to the git repo with index.lock retry.
|
|
1393
|
+
*/
|
|
1394
|
+
async commitBatch(files) {
|
|
1395
|
+
if (files.length === 0)
|
|
1396
|
+
return;
|
|
1397
|
+
try {
|
|
1398
|
+
await retry(async (attempt) => {
|
|
1399
|
+
await execFileAsync('git', ['add', '--', ...files], {
|
|
1400
|
+
cwd: this.rootPath,
|
|
1401
|
+
});
|
|
1402
|
+
// Build message after staging so getStagedDiff can see the changes
|
|
1403
|
+
const message = attempt === 1
|
|
1404
|
+
? await this.buildCommitMessage(files)
|
|
1405
|
+
: this.buildTemplateMessage(files.length);
|
|
1406
|
+
this.pendingReversions.length = 0;
|
|
1407
|
+
await execFileAsync('git', ['commit', '-m', message], {
|
|
1408
|
+
cwd: this.rootPath,
|
|
1409
|
+
});
|
|
1410
|
+
const { stdout: hashOut } = await execFileAsync('git', ['rev-parse', '--short', 'HEAD'], { cwd: this.rootPath });
|
|
1411
|
+
const hash = hashOut.trim();
|
|
1412
|
+
this.logger.info({ root: this.rootPath, hash, fileCount: files.length }, 'VCS commit created');
|
|
1413
|
+
await this.pushIfConfigured();
|
|
1414
|
+
}, {
|
|
1415
|
+
attempts: 4,
|
|
1416
|
+
baseDelayMs: 500,
|
|
1417
|
+
maxDelayMs: 2000,
|
|
1418
|
+
onRetry: ({ attempt, delayMs }) => {
|
|
1419
|
+
this.logger.warn({ root: this.rootPath, attempt, delay: delayMs }, 'index.lock contention, retrying');
|
|
1420
|
+
},
|
|
1421
|
+
});
|
|
1422
|
+
}
|
|
1423
|
+
catch (error) {
|
|
1424
|
+
for (const f of files) {
|
|
1425
|
+
this.pending.add(f);
|
|
1426
|
+
}
|
|
1427
|
+
this.logger.warn({ root: this.rootPath, fileCount: files.length }, 'Re-queued files after commit failure');
|
|
1428
|
+
this.logger.error({ root: this.rootPath, err: normalizeError(error) }, 'VCS commit failed');
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
/**
|
|
1432
|
+
* Push to the configured remote if remoteUrl is set.
|
|
1433
|
+
* On failure: logs the error and records it in pushErrors; never throws.
|
|
1434
|
+
*/
|
|
1435
|
+
async pushIfConfigured() {
|
|
1436
|
+
if (!this.remoteUrl)
|
|
1437
|
+
return;
|
|
1438
|
+
try {
|
|
1439
|
+
// Build the authenticated URL if a token is available
|
|
1440
|
+
const pushUrl = this.accessToken
|
|
1441
|
+
? this.remoteUrl.replace(/^https:\/\//, `https://${encodeURIComponent(this.accessToken)}@`)
|
|
1442
|
+
: this.remoteUrl;
|
|
1443
|
+
await execFileAsync('git', ['push', pushUrl, 'HEAD'], {
|
|
1444
|
+
cwd: this.rootPath,
|
|
1445
|
+
});
|
|
1446
|
+
this._lastPushTime = new Date().toISOString();
|
|
1447
|
+
this.logger.info({ root: this.rootPath, remote: this.remoteUrl }, 'VCS push succeeded');
|
|
1448
|
+
}
|
|
1449
|
+
catch (error) {
|
|
1450
|
+
const pushError = {
|
|
1451
|
+
timestamp: new Date().toISOString(),
|
|
1452
|
+
message: normalizeError(error).message,
|
|
1453
|
+
};
|
|
1454
|
+
this._pushErrors.push(pushError);
|
|
1455
|
+
this.logger.error({ root: this.rootPath, err: normalizeError(error) }, 'VCS push failed');
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
/**
|
|
1461
|
+
* @module vcs/VcsCoordinator
|
|
1462
|
+
* Orchestrates VCS across all watch roots. Routes file events to the correct
|
|
1463
|
+
* root's VcsManager instance.
|
|
1464
|
+
*/
|
|
1465
|
+
/**
|
|
1466
|
+
* Orchestrates VCS across all VCS-enabled watch roots.
|
|
1467
|
+
*/
|
|
1468
|
+
class VcsCoordinator {
|
|
1469
|
+
managers = new Map();
|
|
1470
|
+
roots = [];
|
|
1471
|
+
logger;
|
|
1472
|
+
constructor(config, logger) {
|
|
1473
|
+
this.logger = logger;
|
|
1474
|
+
if (!config.vcs?.enabled)
|
|
1475
|
+
return;
|
|
1476
|
+
const normalized = normalizeWatchPaths(config.watch.paths);
|
|
1477
|
+
for (const entry of normalized) {
|
|
1478
|
+
const rootVcs = entry.vcs?.enabled ?? config.vcs.enabled;
|
|
1479
|
+
if (!rootVcs)
|
|
1480
|
+
continue;
|
|
1481
|
+
if (/[*?{[]/.test(entry.path)) {
|
|
1482
|
+
logger.warn({ path: entry.path }, 'Skipping VCS for watch path containing glob characters');
|
|
1483
|
+
continue;
|
|
1484
|
+
}
|
|
1485
|
+
const resolvedRoot = normalizeSlashes(resolve(entry.path));
|
|
1486
|
+
const mergedConfig = {
|
|
1487
|
+
...config.vcs,
|
|
1488
|
+
...entry.vcs,
|
|
1489
|
+
enabled: true,
|
|
1490
|
+
};
|
|
1491
|
+
// Create CommitMessageGenerator if AI commit messages are configured
|
|
1492
|
+
const cmConfig = mergedConfig.commitMessage;
|
|
1493
|
+
const rootLogger = logger.child({ vcsRoot: resolvedRoot });
|
|
1494
|
+
const resolvedApiKey = cmConfig?.enabled !== false
|
|
1495
|
+
? resolveCommitMessageApiKey(cmConfig?.provider ?? 'anthropic', cmConfig?.apiKey, rootLogger)
|
|
1496
|
+
: undefined;
|
|
1497
|
+
const generator = cmConfig?.enabled !== false && resolvedApiKey
|
|
1498
|
+
? new CommitMessageGenerator(cmConfig?.provider ?? 'anthropic', cmConfig?.model ?? 'claude-haiku-4-0', resolvedApiKey, rootLogger)
|
|
1499
|
+
: undefined;
|
|
1500
|
+
// Resolve remote config: per-root overrides fall back to root-level defaults
|
|
1501
|
+
const remoteUrl = entry.vcs?.remote;
|
|
1502
|
+
const accessToken = entry.vcs?.accessToken ?? config.vcs.defaultAccessToken;
|
|
1503
|
+
const manager = new VcsManager(resolvedRoot, mergedConfig, logger.child({ vcsRoot: resolvedRoot }), generator, remoteUrl, accessToken);
|
|
1504
|
+
this.managers.set(resolvedRoot, manager);
|
|
1505
|
+
this.roots.push(resolvedRoot);
|
|
1506
|
+
}
|
|
1507
|
+
// Sort roots longest-first so nested paths match before parents.
|
|
1508
|
+
this.roots.sort((a, b) => b.length - a.length);
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* Start all VcsManager instances.
|
|
1512
|
+
*/
|
|
1513
|
+
start() {
|
|
1514
|
+
this.managers.forEach((manager) => {
|
|
1515
|
+
manager.start();
|
|
1516
|
+
});
|
|
1517
|
+
this.logger.info({ rootCount: this.managers.size }, 'VcsCoordinator started');
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* Route a file event to the correct root's VcsManager.
|
|
1521
|
+
*
|
|
1522
|
+
* @param filePath - Absolute path of the changed file.
|
|
1523
|
+
* @param event - The event type: add, change, or unlink.
|
|
1524
|
+
*/
|
|
1525
|
+
onFileChange(filePath, event) {
|
|
1526
|
+
const normalizedPath = normalizeSlashes(resolve(filePath));
|
|
1527
|
+
const manager = this.findManagerForPath(normalizedPath);
|
|
1528
|
+
if (!manager)
|
|
1529
|
+
return;
|
|
1530
|
+
if (event === 'unlink') {
|
|
1531
|
+
manager.handleUnlink(normalizedPath);
|
|
1532
|
+
}
|
|
1533
|
+
else {
|
|
1534
|
+
manager.fileChanged(normalizedPath);
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
/**
|
|
1538
|
+
* Flush all managers and clean up.
|
|
1539
|
+
*/
|
|
1540
|
+
async stop() {
|
|
1541
|
+
const stopPromises = [];
|
|
1542
|
+
this.managers.forEach((manager) => {
|
|
1543
|
+
stopPromises.push(manager.stop());
|
|
1544
|
+
});
|
|
1545
|
+
await Promise.all(stopPromises);
|
|
1546
|
+
this.logger.info('VcsCoordinator stopped');
|
|
1547
|
+
}
|
|
1548
|
+
/**
|
|
1549
|
+
* Get the sorted root paths (longest-first).
|
|
1550
|
+
*/
|
|
1551
|
+
getRoots() {
|
|
1552
|
+
return this.roots;
|
|
1553
|
+
}
|
|
1554
|
+
/**
|
|
1555
|
+
* Get the VcsManager for a specific root path.
|
|
1556
|
+
*/
|
|
1557
|
+
getManager(root) {
|
|
1558
|
+
return this.managers.get(root);
|
|
1559
|
+
}
|
|
1560
|
+
/**
|
|
1561
|
+
* Get all managers as [root, manager] pairs.
|
|
1562
|
+
*/
|
|
1563
|
+
getAllManagers() {
|
|
1564
|
+
return this.managers;
|
|
1565
|
+
}
|
|
1566
|
+
/**
|
|
1567
|
+
* Find the VcsManager that owns the given normalized file path.
|
|
1568
|
+
* Uses longest-prefix-match against resolved watch roots.
|
|
1569
|
+
*
|
|
1570
|
+
* @param normalizedPath - Normalized absolute path (forward slashes).
|
|
1571
|
+
* @returns The matching VcsManager, or undefined.
|
|
1572
|
+
*/
|
|
1573
|
+
findManagerForPath(normalizedPath) {
|
|
1574
|
+
const root = findRootForPath(this.roots, normalizedPath);
|
|
1575
|
+
return root ? this.managers.get(root) : undefined;
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
|
|
747
1579
|
/**
|
|
748
1580
|
* @module helpers/loadModule
|
|
749
1581
|
* Shared utility for loading and namespace-prefixing exports from helper modules.
|
|
@@ -1925,7 +2757,164 @@ async function applyRules(compiledRules, attributes, options = {}) {
|
|
|
1925
2757
|
}
|
|
1926
2758
|
}
|
|
1927
2759
|
}
|
|
1928
|
-
return { metadata: merged, renderedContent, matchedRules, renderAs };
|
|
2760
|
+
return { metadata: merged, renderedContent, matchedRules, renderAs };
|
|
2761
|
+
}
|
|
2762
|
+
|
|
2763
|
+
/**
|
|
2764
|
+
* @module vcs/resolveWatchRoot
|
|
2765
|
+
* Resolves which VCS-enabled watch root(s) own a given absolute path or glob.
|
|
2766
|
+
*/
|
|
2767
|
+
/**
|
|
2768
|
+
* Resolve which VCS-enabled watch root owns an absolute path.
|
|
2769
|
+
*
|
|
2770
|
+
* @param coordinator - The VcsCoordinator with root information.
|
|
2771
|
+
* @param absolutePath - An absolute filesystem path.
|
|
2772
|
+
* @returns The matching root and relative path, or undefined if no root matches.
|
|
2773
|
+
*/
|
|
2774
|
+
function resolveWatchRoot(coordinator, absolutePath) {
|
|
2775
|
+
const normalized = normalizeSlashes(resolve(absolutePath));
|
|
2776
|
+
const root = findRootForPath(coordinator.getRoots(), normalized);
|
|
2777
|
+
if (!root)
|
|
2778
|
+
return undefined;
|
|
2779
|
+
return {
|
|
2780
|
+
root,
|
|
2781
|
+
relativePath: normalizeSlashes(relative(root, normalized)),
|
|
2782
|
+
};
|
|
2783
|
+
}
|
|
2784
|
+
/**
|
|
2785
|
+
* Resolve which VCS-enabled watch roots match a glob pattern.
|
|
2786
|
+
* For globs that could span multiple roots, returns all matching roots.
|
|
2787
|
+
*
|
|
2788
|
+
* @param coordinator - The VcsCoordinator with root information.
|
|
2789
|
+
* @param glob - An absolute glob pattern.
|
|
2790
|
+
* @returns Array of matching roots with relative glob patterns.
|
|
2791
|
+
*/
|
|
2792
|
+
function resolveWatchRootsForGlob(coordinator, glob) {
|
|
2793
|
+
const normalizedGlob = normalizeSlashes(glob);
|
|
2794
|
+
const roots = coordinator.getRoots();
|
|
2795
|
+
const results = [];
|
|
2796
|
+
for (const root of roots) {
|
|
2797
|
+
if (normalizedGlob.startsWith(root + '/') || normalizedGlob === root) {
|
|
2798
|
+
// Glob is under this root
|
|
2799
|
+
results.push({
|
|
2800
|
+
root,
|
|
2801
|
+
relativePath: normalizeSlashes(relative(root, normalizedGlob)),
|
|
2802
|
+
});
|
|
2803
|
+
}
|
|
2804
|
+
else if (root.startsWith(normalizedGlob.replace(/[*?[\]{}]/g, ''))) {
|
|
2805
|
+
// Root is under the glob's base path — include with root-relative glob
|
|
2806
|
+
results.push({
|
|
2807
|
+
root,
|
|
2808
|
+
relativePath: '.',
|
|
2809
|
+
});
|
|
2810
|
+
}
|
|
2811
|
+
}
|
|
2812
|
+
return results;
|
|
2813
|
+
}
|
|
2814
|
+
|
|
2815
|
+
/**
|
|
2816
|
+
* @module vcs/validateStateDirOverlap
|
|
2817
|
+
* Validates that stateDir does not overlap with any watch path.
|
|
2818
|
+
*/
|
|
2819
|
+
/**
|
|
2820
|
+
* Validate that stateDir does not overlap with any watch path.
|
|
2821
|
+
* Overlap means stateDir is a parent, child, or equal to a watch path.
|
|
2822
|
+
*
|
|
2823
|
+
* @param stateDir - The state directory path.
|
|
2824
|
+
* @param watchPaths - Array of watch path strings.
|
|
2825
|
+
* @throws Error if stateDir overlaps with any watch path.
|
|
2826
|
+
*/
|
|
2827
|
+
function validateStateDirOverlap(stateDir, watchPaths) {
|
|
2828
|
+
const resolvedStateDir = resolve(stateDir);
|
|
2829
|
+
for (const watchPath of watchPaths) {
|
|
2830
|
+
const resolvedWatch = resolve(watchPath);
|
|
2831
|
+
if (resolvedStateDir === resolvedWatch) {
|
|
2832
|
+
throw new Error(`stateDir "${stateDir}" is the same as watch path "${watchPath}". They must not overlap.`);
|
|
2833
|
+
}
|
|
2834
|
+
const stateDirWithSep = resolvedStateDir.endsWith(sep)
|
|
2835
|
+
? resolvedStateDir
|
|
2836
|
+
: resolvedStateDir + sep;
|
|
2837
|
+
const watchWithSep = resolvedWatch.endsWith(sep)
|
|
2838
|
+
? resolvedWatch
|
|
2839
|
+
: resolvedWatch + sep;
|
|
2840
|
+
if (resolvedWatch.startsWith(stateDirWithSep)) {
|
|
2841
|
+
throw new Error(`Watch path "${watchPath}" is inside stateDir "${stateDir}". They must not overlap.`);
|
|
2842
|
+
}
|
|
2843
|
+
if (resolvedStateDir.startsWith(watchWithSep)) {
|
|
2844
|
+
throw new Error(`stateDir "${stateDir}" is inside watch path "${watchPath}". They must not overlap.`);
|
|
2845
|
+
}
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
2848
|
+
|
|
2849
|
+
/**
|
|
2850
|
+
* @module vcs/vcsBootstrap
|
|
2851
|
+
* Static bootstrap utilities for VCS-managed watch roots.
|
|
2852
|
+
*/
|
|
2853
|
+
/** Always-on .gitignore entries for VCS-managed watch roots. */
|
|
2854
|
+
const ALWAYS_GITIGNORE_ENTRIES = [
|
|
2855
|
+
'.git/',
|
|
2856
|
+
'node_modules/',
|
|
2857
|
+
'.jeeves-watcher/',
|
|
2858
|
+
'.jeeves-metadata/',
|
|
2859
|
+
];
|
|
2860
|
+
/**
|
|
2861
|
+
* Check whether git is available on the system PATH.
|
|
2862
|
+
*
|
|
2863
|
+
* @returns true if git is available, false otherwise.
|
|
2864
|
+
*/
|
|
2865
|
+
async function checkGitAvailable() {
|
|
2866
|
+
try {
|
|
2867
|
+
await execFileAsync('git', ['--version']);
|
|
2868
|
+
return true;
|
|
2869
|
+
}
|
|
2870
|
+
catch {
|
|
2871
|
+
return false;
|
|
2872
|
+
}
|
|
2873
|
+
}
|
|
2874
|
+
/**
|
|
2875
|
+
* Initialize a git repository at the given path if none exists.
|
|
2876
|
+
* Idempotent — does nothing if .git/ already exists.
|
|
2877
|
+
*
|
|
2878
|
+
* @param rootPath - Directory to initialize as a git repo.
|
|
2879
|
+
*/
|
|
2880
|
+
async function initRepo(rootPath) {
|
|
2881
|
+
const gitDir = join(rootPath, '.git');
|
|
2882
|
+
try {
|
|
2883
|
+
await access(gitDir);
|
|
2884
|
+
// .git exists, nothing to do
|
|
2885
|
+
}
|
|
2886
|
+
catch {
|
|
2887
|
+
await execFileAsync('git', ['init'], { cwd: rootPath });
|
|
2888
|
+
}
|
|
2889
|
+
}
|
|
2890
|
+
/**
|
|
2891
|
+
* Ensure a .gitignore file exists at rootPath with all always-on entries.
|
|
2892
|
+
* Creates the file if missing. Appends only entries not already present.
|
|
2893
|
+
*
|
|
2894
|
+
* @param rootPath - Directory containing the .gitignore.
|
|
2895
|
+
* @param alwaysOnEntries - Additional always-on entries beyond the defaults.
|
|
2896
|
+
*/
|
|
2897
|
+
async function ensureGitignore(rootPath, alwaysOnEntries = []) {
|
|
2898
|
+
const gitignorePath = join(rootPath, '.gitignore');
|
|
2899
|
+
const requiredEntries = [...ALWAYS_GITIGNORE_ENTRIES, ...alwaysOnEntries];
|
|
2900
|
+
let existingContent = '';
|
|
2901
|
+
try {
|
|
2902
|
+
existingContent = await readFile(gitignorePath, 'utf8');
|
|
2903
|
+
}
|
|
2904
|
+
catch {
|
|
2905
|
+
// File doesn't exist yet
|
|
2906
|
+
}
|
|
2907
|
+
const existingLines = new Set(existingContent.split('\n').map((line) => line.trim()));
|
|
2908
|
+
const missing = requiredEntries.filter((entry) => !existingLines.has(entry));
|
|
2909
|
+
if (missing.length === 0)
|
|
2910
|
+
return;
|
|
2911
|
+
if (existingContent.length === 0) {
|
|
2912
|
+
await writeFile(gitignorePath, missing.join('\n') + '\n', 'utf8');
|
|
2913
|
+
}
|
|
2914
|
+
else {
|
|
2915
|
+
const prefix = existingContent.endsWith('\n') ? '' : '\n';
|
|
2916
|
+
await appendFile(gitignorePath, prefix + missing.join('\n') + '\n', 'utf8');
|
|
2917
|
+
}
|
|
1929
2918
|
}
|
|
1930
2919
|
|
|
1931
2920
|
/**
|
|
@@ -1995,10 +2984,10 @@ function createProcessorConfig(config, configDir, customMapLib) {
|
|
|
1995
2984
|
/**
|
|
1996
2985
|
* Create file system watcher with gitignore filtering.
|
|
1997
2986
|
*/
|
|
1998
|
-
function createWatcher(config, factories, queue, processor, logger, runtimeOptions, initialScanTracker, contentHashCache) {
|
|
2987
|
+
function createWatcher(config, factories, queue, processor, logger, runtimeOptions, initialScanTracker, contentHashCache, onVcsFileChange) {
|
|
1999
2988
|
const respectGitignore = config.watch.respectGitignore ?? true;
|
|
2000
2989
|
const gitignoreFilter = respectGitignore
|
|
2001
|
-
? new GitignoreFilter(config.watch.paths)
|
|
2990
|
+
? new GitignoreFilter(extractWatchPathStrings(config.watch.paths))
|
|
2002
2991
|
: undefined;
|
|
2003
2992
|
const watcher = factories.createFileSystemWatcher(config.watch, queue, processor, logger, {
|
|
2004
2993
|
maxRetries: config.maxRetries ?? runtimeOptions.maxRetries,
|
|
@@ -2007,6 +2996,7 @@ function createWatcher(config, factories, queue, processor, logger, runtimeOptio
|
|
|
2007
2996
|
gitignoreFilter,
|
|
2008
2997
|
initialScanTracker,
|
|
2009
2998
|
contentHashCache,
|
|
2999
|
+
onVcsFileChange,
|
|
2010
3000
|
});
|
|
2011
3001
|
return { watcher, gitignoreFilter };
|
|
2012
3002
|
}
|
|
@@ -2078,6 +3068,41 @@ function resolveVersion(referenceUrl) {
|
|
|
2078
3068
|
return 'unknown';
|
|
2079
3069
|
}
|
|
2080
3070
|
}
|
|
3071
|
+
/**
|
|
3072
|
+
* Initialize VCS for all watch roots where VCS is enabled.
|
|
3073
|
+
* Checks git availability, validates stateDir overlap, and initializes repos.
|
|
3074
|
+
*
|
|
3075
|
+
* @param config - The resolved configuration (mutated in place if git unavailable).
|
|
3076
|
+
* @param logger - Logger instance.
|
|
3077
|
+
* @returns The effective VCS enabled state (false if git unavailable).
|
|
3078
|
+
*/
|
|
3079
|
+
async function initVcs(config, logger) {
|
|
3080
|
+
if (!config.vcs?.enabled)
|
|
3081
|
+
return false;
|
|
3082
|
+
const gitAvailable = await checkGitAvailable();
|
|
3083
|
+
if (!gitAvailable) {
|
|
3084
|
+
logger.warn('git not found on PATH — VCS disabled for this session');
|
|
3085
|
+
return false;
|
|
3086
|
+
}
|
|
3087
|
+
const stateDir = config.stateDir ?? '.jeeves-metadata';
|
|
3088
|
+
const pathStrings = extractWatchPathStrings(config.watch.paths);
|
|
3089
|
+
const staticPaths = pathStrings.filter((p) => !/[*?{[]/.test(p));
|
|
3090
|
+
validateStateDirOverlap(stateDir, staticPaths);
|
|
3091
|
+
const normalized = normalizeWatchPaths(config.watch.paths);
|
|
3092
|
+
for (const entry of normalized) {
|
|
3093
|
+
const rootVcs = entry.vcs?.enabled ?? config.vcs.enabled;
|
|
3094
|
+
if (!rootVcs)
|
|
3095
|
+
continue;
|
|
3096
|
+
if (/[*?{[]/.test(entry.path)) {
|
|
3097
|
+
logger.warn({ path: entry.path }, 'Skipping VCS init for watch path containing glob characters');
|
|
3098
|
+
continue;
|
|
3099
|
+
}
|
|
3100
|
+
await initRepo(entry.path);
|
|
3101
|
+
await ensureGitignore(entry.path);
|
|
3102
|
+
logger.info({ root: entry.path }, 'VCS initialized for watch root');
|
|
3103
|
+
}
|
|
3104
|
+
return true;
|
|
3105
|
+
}
|
|
2081
3106
|
|
|
2082
3107
|
/**
|
|
2083
3108
|
* @module app/configReload
|
|
@@ -2283,7 +3308,9 @@ function createConfigMatchHandler(options) {
|
|
|
2283
3308
|
const config = getConfig();
|
|
2284
3309
|
if (config !== cachedConfig) {
|
|
2285
3310
|
compiledRules = compileRules(config.inferenceRules ?? []);
|
|
2286
|
-
watchMatcher = picomatch(config.watch.paths, {
|
|
3311
|
+
watchMatcher = picomatch(extractWatchPathStrings(config.watch.paths), {
|
|
3312
|
+
dot: true,
|
|
3313
|
+
});
|
|
2287
3314
|
ignoreMatcher = config.watch.ignored.length
|
|
2288
3315
|
? picomatch(config.watch.ignored, { dot: true })
|
|
2289
3316
|
: undefined;
|
|
@@ -2562,10 +3589,15 @@ function createConfigReindexHandler(deps) {
|
|
|
2562
3589
|
plan: result.plan,
|
|
2563
3590
|
});
|
|
2564
3591
|
}
|
|
2565
|
-
//
|
|
2566
|
-
|
|
3592
|
+
// Live prune: fire and forget without pre-computing plan (avoids double-scrolling the collection).
|
|
3593
|
+
if (validScope === 'prune') {
|
|
3594
|
+
void executeReindex(reindexDeps, 'prune', pathParam, false);
|
|
3595
|
+
return await reply
|
|
3596
|
+
.status(200)
|
|
3597
|
+
.send({ status: 'started', scope: 'prune' });
|
|
3598
|
+
}
|
|
3599
|
+
// Non-prune scopes: compute plan first then execute async.
|
|
2567
3600
|
const planResult = await executeReindex(reindexDeps, validScope, pathParam, true);
|
|
2568
|
-
// Now fire actual reindex async
|
|
2569
3601
|
void executeReindex(reindexDeps, validScope, pathParam, false);
|
|
2570
3602
|
return await reply
|
|
2571
3603
|
.status(200)
|
|
@@ -3175,7 +4207,7 @@ function createRenderHandler(deps) {
|
|
|
3175
4207
|
}
|
|
3176
4208
|
// Validate path is within watched scope (live config)
|
|
3177
4209
|
const watch = getWatch();
|
|
3178
|
-
if (!isPathWatched(filePath, watch.paths, watch.ignored)) {
|
|
4210
|
+
if (!isPathWatched(filePath, extractWatchPathStrings(watch.paths), watch.ignored)) {
|
|
3179
4211
|
return reply.status(403).send({ error: 'Path is outside watched scope' });
|
|
3180
4212
|
}
|
|
3181
4213
|
try {
|
|
@@ -3362,12 +4394,510 @@ function createSearchHandler(deps) {
|
|
|
3362
4394
|
const vectors = await deps.embeddingProvider.embed([query]);
|
|
3363
4395
|
const hybridConfig = deps.getHybridConfig?.();
|
|
3364
4396
|
if (hybridConfig?.enabled) {
|
|
3365
|
-
return deps.vectorStore.hybridSearch(vectors[0], query, limit, hybridConfig.textWeight, filter);
|
|
4397
|
+
return deps.vectorStore.hybridSearch(vectors[0], query, limit, hybridConfig.textWeight, filter, offset);
|
|
3366
4398
|
}
|
|
3367
4399
|
return deps.vectorStore.search(vectors[0], limit, filter, offset);
|
|
3368
4400
|
}, deps.logger, 'Search');
|
|
3369
4401
|
}
|
|
3370
4402
|
|
|
4403
|
+
/**
|
|
4404
|
+
* @module api/handlers/vcs/vcsCheckExclusion
|
|
4405
|
+
* Fastify route handler for GET /vcs/check-exclusion. Checks gitignore status of a path.
|
|
4406
|
+
*/
|
|
4407
|
+
/**
|
|
4408
|
+
* Create handler for GET /vcs/check-exclusion.
|
|
4409
|
+
*/
|
|
4410
|
+
function createVcsCheckExclusionHandler(deps) {
|
|
4411
|
+
return wrapHandler(async (request, reply) => {
|
|
4412
|
+
const { path } = request.query;
|
|
4413
|
+
if (!path) {
|
|
4414
|
+
void reply
|
|
4415
|
+
.status(400)
|
|
4416
|
+
.send({ error: 'Missing required query parameter: path' });
|
|
4417
|
+
return;
|
|
4418
|
+
}
|
|
4419
|
+
const resolved = resolveWatchRoot(deps.coordinator, path);
|
|
4420
|
+
if (!resolved) {
|
|
4421
|
+
void reply
|
|
4422
|
+
.status(404)
|
|
4423
|
+
.send({ error: 'Path does not match any VCS-enabled watch root' });
|
|
4424
|
+
return;
|
|
4425
|
+
}
|
|
4426
|
+
try {
|
|
4427
|
+
const { stdout } = await execFileAsync('git', ['check-ignore', '-v', resolved.relativePath], { cwd: resolved.root });
|
|
4428
|
+
// Output format: <source>:<linenum>:<pattern>\t<pathname>
|
|
4429
|
+
const trimmed = stdout.trim();
|
|
4430
|
+
const match = /^(.+):(\d+):(.+)\t/.exec(trimmed);
|
|
4431
|
+
if (match) {
|
|
4432
|
+
return {
|
|
4433
|
+
excluded: true,
|
|
4434
|
+
rule: match[3].trim(),
|
|
4435
|
+
source: match[1],
|
|
4436
|
+
};
|
|
4437
|
+
}
|
|
4438
|
+
return { excluded: true };
|
|
4439
|
+
}
|
|
4440
|
+
catch {
|
|
4441
|
+
// Exit code 1 means the path is NOT ignored
|
|
4442
|
+
return { excluded: false };
|
|
4443
|
+
}
|
|
4444
|
+
}, deps.logger, 'VcsCheckExclusion');
|
|
4445
|
+
}
|
|
4446
|
+
|
|
4447
|
+
/**
|
|
4448
|
+
* @module api/handlers/vcs/vcsDiff
|
|
4449
|
+
* Fastify route handler for GET /vcs/diff. Returns diff text for a glob between commits.
|
|
4450
|
+
*/
|
|
4451
|
+
/**
|
|
4452
|
+
* Create handler for GET /vcs/diff.
|
|
4453
|
+
*/
|
|
4454
|
+
function createVcsDiffHandler(deps) {
|
|
4455
|
+
return wrapHandler(async (request, reply) => {
|
|
4456
|
+
const { glob, commit, commitEnd } = request.query;
|
|
4457
|
+
if (!glob || !commit) {
|
|
4458
|
+
void reply
|
|
4459
|
+
.status(400)
|
|
4460
|
+
.send({ error: 'Missing required query parameters: glob, commit' });
|
|
4461
|
+
return;
|
|
4462
|
+
}
|
|
4463
|
+
const resolved = resolveWatchRootsForGlob(deps.coordinator, glob);
|
|
4464
|
+
if (resolved.length === 0) {
|
|
4465
|
+
void reply
|
|
4466
|
+
.status(404)
|
|
4467
|
+
.send({ error: 'No VCS-enabled watch root matches the given glob' });
|
|
4468
|
+
return;
|
|
4469
|
+
}
|
|
4470
|
+
const diffs = [];
|
|
4471
|
+
for (const { root, relativePath } of resolved) {
|
|
4472
|
+
const range = commitEnd ? `${commit}..${commitEnd}` : `${commit}..HEAD`;
|
|
4473
|
+
const args = ['diff', range, '--', relativePath || '.'];
|
|
4474
|
+
try {
|
|
4475
|
+
const { stdout } = await execFileAsync('git', args, { cwd: root });
|
|
4476
|
+
if (stdout.trim())
|
|
4477
|
+
diffs.push(stdout);
|
|
4478
|
+
}
|
|
4479
|
+
catch {
|
|
4480
|
+
// Invalid commit or path — skip
|
|
4481
|
+
}
|
|
4482
|
+
}
|
|
4483
|
+
void reply.header('content-type', 'text/plain').send(diffs.join('\n'));
|
|
4484
|
+
}, deps.logger, 'VcsDiff');
|
|
4485
|
+
}
|
|
4486
|
+
|
|
4487
|
+
/**
|
|
4488
|
+
* @module api/handlers/vcs/vcsExclude
|
|
4489
|
+
* Fastify route handler for POST /vcs/exclude. Manages .gitignore exclusions
|
|
4490
|
+
* with locality-based placement.
|
|
4491
|
+
*/
|
|
4492
|
+
/**
|
|
4493
|
+
* Determine the deepest directory for .gitignore placement and the pattern
|
|
4494
|
+
* to add. Splits the glob at the first wildcard character.
|
|
4495
|
+
*
|
|
4496
|
+
* @param relativeGlob - Glob relative to the watch root.
|
|
4497
|
+
* @returns The directory (relative to root) and the pattern for .gitignore.
|
|
4498
|
+
*/
|
|
4499
|
+
function splitGlobForLocality(relativeGlob) {
|
|
4500
|
+
// Find the first path segment containing a wildcard
|
|
4501
|
+
const segments = relativeGlob.split('/');
|
|
4502
|
+
const dirSegments = [];
|
|
4503
|
+
for (const segment of segments) {
|
|
4504
|
+
if (/[*?[\]{}]/.test(segment))
|
|
4505
|
+
break;
|
|
4506
|
+
dirSegments.push(segment);
|
|
4507
|
+
}
|
|
4508
|
+
// Last non-wildcard segment might be a filename, not a directory.
|
|
4509
|
+
// If no wildcards at all, the directory is the parent of the file.
|
|
4510
|
+
if (dirSegments.length === segments.length) {
|
|
4511
|
+
// No wildcards — the glob is a literal path, directory is parent
|
|
4512
|
+
const dir = dirSegments.slice(0, -1).join('/') || '.';
|
|
4513
|
+
const pattern = dirSegments[dirSegments.length - 1];
|
|
4514
|
+
return { dir, pattern };
|
|
4515
|
+
}
|
|
4516
|
+
const dir = dirSegments.join('/') || '.';
|
|
4517
|
+
const patternSegments = segments.slice(dirSegments.length);
|
|
4518
|
+
const pattern = patternSegments.join('/');
|
|
4519
|
+
return { dir, pattern };
|
|
4520
|
+
}
|
|
4521
|
+
/**
|
|
4522
|
+
* Create handler for POST /vcs/exclude.
|
|
4523
|
+
*/
|
|
4524
|
+
function createVcsExcludeHandler(deps) {
|
|
4525
|
+
return wrapHandler(async (request, reply) => {
|
|
4526
|
+
const { glob, root: requestedRoot, remove } = request.body;
|
|
4527
|
+
if (!glob) {
|
|
4528
|
+
void reply
|
|
4529
|
+
.status(400)
|
|
4530
|
+
.send({ error: 'Missing required body parameter: glob' });
|
|
4531
|
+
return;
|
|
4532
|
+
}
|
|
4533
|
+
// Resolve the root
|
|
4534
|
+
let root;
|
|
4535
|
+
if (requestedRoot) {
|
|
4536
|
+
const normalizedRoot = normalizeSlashes(requestedRoot);
|
|
4537
|
+
const manager = deps.coordinator.getManager(normalizedRoot);
|
|
4538
|
+
if (!manager) {
|
|
4539
|
+
void reply
|
|
4540
|
+
.status(404)
|
|
4541
|
+
.send({ error: 'Root is not a VCS-enabled watch root' });
|
|
4542
|
+
return;
|
|
4543
|
+
}
|
|
4544
|
+
root = normalizedRoot;
|
|
4545
|
+
}
|
|
4546
|
+
else {
|
|
4547
|
+
// Resolve root from the glob path
|
|
4548
|
+
const normalizedGlob = normalizeSlashes(glob);
|
|
4549
|
+
const basePath = normalizedGlob.replace(/[*?[\]{}].*/g, '');
|
|
4550
|
+
const matchedRoot = findRootForPath(deps.coordinator.getRoots(), basePath);
|
|
4551
|
+
if (!matchedRoot) {
|
|
4552
|
+
void reply
|
|
4553
|
+
.status(404)
|
|
4554
|
+
.send({ error: 'Glob does not match any VCS-enabled watch root' });
|
|
4555
|
+
return;
|
|
4556
|
+
}
|
|
4557
|
+
root = matchedRoot;
|
|
4558
|
+
}
|
|
4559
|
+
// Compute relative glob from root
|
|
4560
|
+
const normalizedGlob = normalizeSlashes(glob);
|
|
4561
|
+
let relativeGlob;
|
|
4562
|
+
if (normalizedGlob.startsWith(root + '/')) {
|
|
4563
|
+
relativeGlob = normalizedGlob.slice(root.length + 1);
|
|
4564
|
+
}
|
|
4565
|
+
else {
|
|
4566
|
+
// Glob is the root itself or doesn't start with root — use as-is
|
|
4567
|
+
relativeGlob = normalizedGlob;
|
|
4568
|
+
}
|
|
4569
|
+
// Determine .gitignore directory and pattern using locality principle
|
|
4570
|
+
const { dir, pattern } = splitGlobForLocality(relativeGlob);
|
|
4571
|
+
const gitignoreDir = dir === '.' ? root : normalizeSlashes(join(root, dir));
|
|
4572
|
+
const gitignorePath = normalizeSlashes(join(gitignoreDir, '.gitignore'));
|
|
4573
|
+
if (remove) {
|
|
4574
|
+
// Remove the pattern from .gitignore
|
|
4575
|
+
let content;
|
|
4576
|
+
try {
|
|
4577
|
+
content = await readFile(gitignorePath, 'utf8');
|
|
4578
|
+
}
|
|
4579
|
+
catch {
|
|
4580
|
+
void reply
|
|
4581
|
+
.status(404)
|
|
4582
|
+
.send({ error: 'No .gitignore found at the expected location' });
|
|
4583
|
+
return;
|
|
4584
|
+
}
|
|
4585
|
+
const lines = content.split('\n');
|
|
4586
|
+
const filtered = lines.filter((line) => line.trim() !== pattern);
|
|
4587
|
+
if (filtered.length === lines.length) {
|
|
4588
|
+
void reply
|
|
4589
|
+
.status(404)
|
|
4590
|
+
.send({ error: 'Pattern not found in .gitignore' });
|
|
4591
|
+
return;
|
|
4592
|
+
}
|
|
4593
|
+
await writeFile(gitignorePath, filtered.join('\n'), 'utf8');
|
|
4594
|
+
return { ok: true, gitignorePath, action: 'removed' };
|
|
4595
|
+
}
|
|
4596
|
+
// Add the pattern to .gitignore
|
|
4597
|
+
await mkdir(gitignoreDir, { recursive: true });
|
|
4598
|
+
let existingContent = '';
|
|
4599
|
+
try {
|
|
4600
|
+
existingContent = await readFile(gitignorePath, 'utf8');
|
|
4601
|
+
}
|
|
4602
|
+
catch {
|
|
4603
|
+
// File doesn't exist yet
|
|
4604
|
+
}
|
|
4605
|
+
// Check if pattern already exists
|
|
4606
|
+
const existingLines = new Set(existingContent.split('\n').map((line) => line.trim()));
|
|
4607
|
+
if (existingLines.has(pattern)) {
|
|
4608
|
+
return { ok: true, gitignorePath, action: 'added' };
|
|
4609
|
+
}
|
|
4610
|
+
if (existingContent.length === 0) {
|
|
4611
|
+
await writeFile(gitignorePath, pattern + '\n', 'utf8');
|
|
4612
|
+
}
|
|
4613
|
+
else {
|
|
4614
|
+
const prefix = existingContent.endsWith('\n') ? '' : '\n';
|
|
4615
|
+
await appendFile(gitignorePath, prefix + pattern + '\n', 'utf8');
|
|
4616
|
+
}
|
|
4617
|
+
return { ok: true, gitignorePath, action: 'added' };
|
|
4618
|
+
}, deps.logger, 'VcsExclude');
|
|
4619
|
+
}
|
|
4620
|
+
|
|
4621
|
+
/**
|
|
4622
|
+
* @module api/handlers/vcs/vcsHistory
|
|
4623
|
+
* Fastify route handler for GET /vcs/history. Returns commit history for a glob.
|
|
4624
|
+
*/
|
|
4625
|
+
/** Matches a commit header line: 40-hex-char hash, pipe, message, pipe, ISO timestamp. */
|
|
4626
|
+
const HEADER_RE = /^([0-9a-f]{40})\|(.+)\|(\d{4}-\d{2}-\d{2}T.+)$/;
|
|
4627
|
+
/**
|
|
4628
|
+
* Parse git log --format='%H|%s|%aI' --name-only output into structured entries.
|
|
4629
|
+
*
|
|
4630
|
+
* Output format per commit:
|
|
4631
|
+
* hash|subject|ISO-date
|
|
4632
|
+
* (empty line)
|
|
4633
|
+
* file1
|
|
4634
|
+
* file2
|
|
4635
|
+
* (empty line)
|
|
4636
|
+
*/
|
|
4637
|
+
function parseGitLog(stdout) {
|
|
4638
|
+
const entries = [];
|
|
4639
|
+
const lines = stdout.split('\n');
|
|
4640
|
+
let current;
|
|
4641
|
+
for (const line of lines) {
|
|
4642
|
+
const trimmed = line.trim();
|
|
4643
|
+
const headerMatch = HEADER_RE.exec(trimmed);
|
|
4644
|
+
if (headerMatch) {
|
|
4645
|
+
// Start of a new commit
|
|
4646
|
+
current = {
|
|
4647
|
+
commit: headerMatch[1],
|
|
4648
|
+
message: headerMatch[2],
|
|
4649
|
+
timestamp: headerMatch[3],
|
|
4650
|
+
files: [],
|
|
4651
|
+
};
|
|
4652
|
+
entries.push(current);
|
|
4653
|
+
}
|
|
4654
|
+
else if (trimmed.length > 0 && current) {
|
|
4655
|
+
current.files.push(trimmed);
|
|
4656
|
+
}
|
|
4657
|
+
}
|
|
4658
|
+
return entries;
|
|
4659
|
+
}
|
|
4660
|
+
/**
|
|
4661
|
+
* Create handler for GET /vcs/history.
|
|
4662
|
+
*/
|
|
4663
|
+
function createVcsHistoryHandler(deps) {
|
|
4664
|
+
return wrapHandler(async (request, reply) => {
|
|
4665
|
+
const { glob, since, until, limit: limitStr } = request.query;
|
|
4666
|
+
if (!glob) {
|
|
4667
|
+
void reply
|
|
4668
|
+
.status(400)
|
|
4669
|
+
.send({ error: 'Missing required query parameter: glob' });
|
|
4670
|
+
return;
|
|
4671
|
+
}
|
|
4672
|
+
const limit = limitStr ? parseInt(limitStr, 10) : 20;
|
|
4673
|
+
const resolved = resolveWatchRootsForGlob(deps.coordinator, glob);
|
|
4674
|
+
if (resolved.length === 0) {
|
|
4675
|
+
void reply
|
|
4676
|
+
.status(404)
|
|
4677
|
+
.send({ error: 'No VCS-enabled watch root matches the given glob' });
|
|
4678
|
+
return;
|
|
4679
|
+
}
|
|
4680
|
+
const allEntries = [];
|
|
4681
|
+
for (const { root, relativePath } of resolved) {
|
|
4682
|
+
const args = ['log', '--format=%H|%s|%aI', '--name-only'];
|
|
4683
|
+
if (since)
|
|
4684
|
+
args.push(`--since=${since}`);
|
|
4685
|
+
if (until)
|
|
4686
|
+
args.push(`--until=${until}`);
|
|
4687
|
+
args.push('--', relativePath || '.');
|
|
4688
|
+
try {
|
|
4689
|
+
const { stdout } = await execFileAsync('git', args, { cwd: root });
|
|
4690
|
+
allEntries.push(...parseGitLog(stdout));
|
|
4691
|
+
}
|
|
4692
|
+
catch {
|
|
4693
|
+
// No commits or invalid path — skip
|
|
4694
|
+
}
|
|
4695
|
+
}
|
|
4696
|
+
// Sort by timestamp descending
|
|
4697
|
+
allEntries.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime());
|
|
4698
|
+
return allEntries.slice(0, limit);
|
|
4699
|
+
}, deps.logger, 'VcsHistory');
|
|
4700
|
+
}
|
|
4701
|
+
|
|
4702
|
+
/**
|
|
4703
|
+
* @module api/handlers/vcs/vcsRevert
|
|
4704
|
+
* Fastify route handler for POST /vcs/revert. Restores files from a past commit.
|
|
4705
|
+
*/
|
|
4706
|
+
/**
|
|
4707
|
+
* Create handler for POST /vcs/revert.
|
|
4708
|
+
*/
|
|
4709
|
+
function createVcsRevertHandler(deps) {
|
|
4710
|
+
return wrapHandler(async (request, reply) => {
|
|
4711
|
+
const { glob, commit, existingOnly } = request.body;
|
|
4712
|
+
if (!glob || !commit) {
|
|
4713
|
+
void reply
|
|
4714
|
+
.status(400)
|
|
4715
|
+
.send({ error: 'Missing required body parameters: glob, commit' });
|
|
4716
|
+
return;
|
|
4717
|
+
}
|
|
4718
|
+
const resolved = resolveWatchRootsForGlob(deps.coordinator, glob);
|
|
4719
|
+
if (resolved.length === 0) {
|
|
4720
|
+
void reply
|
|
4721
|
+
.status(404)
|
|
4722
|
+
.send({ error: 'Glob does not match any VCS-enabled watch root' });
|
|
4723
|
+
return;
|
|
4724
|
+
}
|
|
4725
|
+
const allFiles = [];
|
|
4726
|
+
for (const { root, relativePath } of resolved) {
|
|
4727
|
+
const manager = deps.coordinator.getManager(root);
|
|
4728
|
+
if (!manager)
|
|
4729
|
+
continue;
|
|
4730
|
+
// List files matching the glob at the target commit
|
|
4731
|
+
let fileList;
|
|
4732
|
+
try {
|
|
4733
|
+
const args = relativePath === '' || relativePath === '.'
|
|
4734
|
+
? ['ls-tree', '-r', '--name-only', commit]
|
|
4735
|
+
: ['ls-tree', '-r', '--name-only', commit, '--', relativePath];
|
|
4736
|
+
const { stdout } = await execFileAsync('git', args, { cwd: root });
|
|
4737
|
+
fileList = stdout
|
|
4738
|
+
.trim()
|
|
4739
|
+
.split('\n')
|
|
4740
|
+
.filter((line) => line.length > 0);
|
|
4741
|
+
}
|
|
4742
|
+
catch {
|
|
4743
|
+
continue;
|
|
4744
|
+
}
|
|
4745
|
+
const restoredPaths = [];
|
|
4746
|
+
for (const relFile of fileList) {
|
|
4747
|
+
const absPath = normalizeSlashes(join(root, relFile));
|
|
4748
|
+
// If existingOnly, skip files that don't exist on disk
|
|
4749
|
+
if (existingOnly) {
|
|
4750
|
+
try {
|
|
4751
|
+
await access(absPath);
|
|
4752
|
+
}
|
|
4753
|
+
catch {
|
|
4754
|
+
continue;
|
|
4755
|
+
}
|
|
4756
|
+
}
|
|
4757
|
+
// Get file content at the target commit
|
|
4758
|
+
let content;
|
|
4759
|
+
try {
|
|
4760
|
+
const { stdout } = await execFileAsync('git', ['show', `${commit}:${relFile}`], { cwd: root });
|
|
4761
|
+
content = stdout;
|
|
4762
|
+
}
|
|
4763
|
+
catch {
|
|
4764
|
+
continue;
|
|
4765
|
+
}
|
|
4766
|
+
// Ensure parent directory exists
|
|
4767
|
+
const dir = dirname(absPath);
|
|
4768
|
+
await mkdir(dir, { recursive: true });
|
|
4769
|
+
// Write the file
|
|
4770
|
+
await writeFile(absPath, content, 'utf8');
|
|
4771
|
+
restoredPaths.push(absPath);
|
|
4772
|
+
// Notify VcsManager of the change (enters debounce cycle)
|
|
4773
|
+
manager.fileChanged(absPath);
|
|
4774
|
+
}
|
|
4775
|
+
if (restoredPaths.length > 0) {
|
|
4776
|
+
manager.addPendingReversion({
|
|
4777
|
+
glob,
|
|
4778
|
+
commit,
|
|
4779
|
+
paths: restoredPaths,
|
|
4780
|
+
});
|
|
4781
|
+
allFiles.push(...restoredPaths);
|
|
4782
|
+
}
|
|
4783
|
+
}
|
|
4784
|
+
return { restored: allFiles.length, files: allFiles };
|
|
4785
|
+
}, deps.logger, 'VcsRevert');
|
|
4786
|
+
}
|
|
4787
|
+
|
|
4788
|
+
/**
|
|
4789
|
+
* @module api/handlers/vcs/vcsShow
|
|
4790
|
+
* Fastify route handler for GET /vcs/show. Returns file content at a specific commit.
|
|
4791
|
+
*/
|
|
4792
|
+
const CONTENT_TYPES = {
|
|
4793
|
+
'.txt': 'text/plain',
|
|
4794
|
+
'.md': 'text/markdown',
|
|
4795
|
+
'.json': 'application/json',
|
|
4796
|
+
'.xml': 'application/xml',
|
|
4797
|
+
'.html': 'text/html',
|
|
4798
|
+
'.css': 'text/css',
|
|
4799
|
+
'.js': 'application/javascript',
|
|
4800
|
+
'.ts': 'text/plain',
|
|
4801
|
+
'.yaml': 'text/yaml',
|
|
4802
|
+
'.yml': 'text/yaml',
|
|
4803
|
+
'.csv': 'text/csv',
|
|
4804
|
+
};
|
|
4805
|
+
/**
|
|
4806
|
+
* Create handler for GET /vcs/show.
|
|
4807
|
+
*/
|
|
4808
|
+
function createVcsShowHandler(deps) {
|
|
4809
|
+
return wrapHandler(async (request, reply) => {
|
|
4810
|
+
const { path, commit } = request.query;
|
|
4811
|
+
if (!path || !commit) {
|
|
4812
|
+
void reply
|
|
4813
|
+
.status(400)
|
|
4814
|
+
.send({ error: 'Missing required query parameters: path, commit' });
|
|
4815
|
+
return;
|
|
4816
|
+
}
|
|
4817
|
+
const resolved = resolveWatchRoot(deps.coordinator, path);
|
|
4818
|
+
if (!resolved) {
|
|
4819
|
+
void reply
|
|
4820
|
+
.status(404)
|
|
4821
|
+
.send({ error: 'Path does not match any VCS-enabled watch root' });
|
|
4822
|
+
return;
|
|
4823
|
+
}
|
|
4824
|
+
try {
|
|
4825
|
+
const { stdout } = await execFileAsync('git', ['show', `${commit}:${resolved.relativePath}`], { cwd: resolved.root });
|
|
4826
|
+
const ext = extname(path).toLowerCase();
|
|
4827
|
+
const contentType = CONTENT_TYPES[ext] ?? 'application/octet-stream';
|
|
4828
|
+
void reply.header('content-type', contentType).send(stdout);
|
|
4829
|
+
}
|
|
4830
|
+
catch {
|
|
4831
|
+
void reply
|
|
4832
|
+
.status(404)
|
|
4833
|
+
.send({ error: 'File not found at the specified commit' });
|
|
4834
|
+
}
|
|
4835
|
+
}, deps.logger, 'VcsShow');
|
|
4836
|
+
}
|
|
4837
|
+
|
|
4838
|
+
/**
|
|
4839
|
+
* @module api/handlers/vcs/vcsStatus
|
|
4840
|
+
* Fastify route handler for GET /vcs/status. Returns VCS state for all roots.
|
|
4841
|
+
*/
|
|
4842
|
+
async function getLastCommit(cwd) {
|
|
4843
|
+
try {
|
|
4844
|
+
const { stdout } = await execFileAsync('git', ['log', '-1', '--format=%H|%s|%aI'], { cwd });
|
|
4845
|
+
const trimmed = stdout.trim();
|
|
4846
|
+
if (!trimmed)
|
|
4847
|
+
return null;
|
|
4848
|
+
const [hash, message, timestamp] = trimmed.split('|');
|
|
4849
|
+
return { hash, message, timestamp };
|
|
4850
|
+
}
|
|
4851
|
+
catch {
|
|
4852
|
+
return null;
|
|
4853
|
+
}
|
|
4854
|
+
}
|
|
4855
|
+
async function getTrackedCount(cwd) {
|
|
4856
|
+
try {
|
|
4857
|
+
const { stdout } = await execFileAsync('git', ['rev-list', '--count', 'HEAD'], { cwd });
|
|
4858
|
+
return parseInt(stdout.trim(), 10);
|
|
4859
|
+
}
|
|
4860
|
+
catch {
|
|
4861
|
+
return 0;
|
|
4862
|
+
}
|
|
4863
|
+
}
|
|
4864
|
+
async function getRemoteUrl(cwd) {
|
|
4865
|
+
try {
|
|
4866
|
+
const { stdout } = await execFileAsync('git', ['remote', 'get-url', 'origin'], { cwd });
|
|
4867
|
+
const url = stdout.trim();
|
|
4868
|
+
return url || null;
|
|
4869
|
+
}
|
|
4870
|
+
catch {
|
|
4871
|
+
return null;
|
|
4872
|
+
}
|
|
4873
|
+
}
|
|
4874
|
+
/**
|
|
4875
|
+
* Create handler for GET /vcs/status.
|
|
4876
|
+
*/
|
|
4877
|
+
function createVcsStatusHandler(deps) {
|
|
4878
|
+
return wrapHandler(async () => {
|
|
4879
|
+
const roots = deps.coordinator.getRoots();
|
|
4880
|
+
const enabled = roots.length > 0;
|
|
4881
|
+
const rootStatuses = await Promise.all(roots.map(async (root) => {
|
|
4882
|
+
const manager = deps.coordinator.getManager(root);
|
|
4883
|
+
const [tracked, lastCommit, remoteUrl] = await Promise.all([
|
|
4884
|
+
getTrackedCount(root),
|
|
4885
|
+
getLastCommit(root),
|
|
4886
|
+
getRemoteUrl(root),
|
|
4887
|
+
]);
|
|
4888
|
+
return {
|
|
4889
|
+
path: root,
|
|
4890
|
+
tracked,
|
|
4891
|
+
lastCommit,
|
|
4892
|
+
remoteUrl: manager?.remoteUrl ?? remoteUrl,
|
|
4893
|
+
lastPush: manager?.lastPushTime ?? null,
|
|
4894
|
+
pushErrors: manager?.pushErrors ?? [],
|
|
4895
|
+
};
|
|
4896
|
+
}));
|
|
4897
|
+
return { enabled, roots: rootStatuses };
|
|
4898
|
+
}, deps.logger, 'VcsStatus');
|
|
4899
|
+
}
|
|
4900
|
+
|
|
3371
4901
|
/**
|
|
3372
4902
|
* @module api/handlers/walk
|
|
3373
4903
|
* Fastify route handler for POST /walk. Enumerates watched files matching caller-provided globs
|
|
@@ -3676,7 +5206,7 @@ function createApiServer(options) {
|
|
|
3676
5206
|
logger,
|
|
3677
5207
|
}));
|
|
3678
5208
|
app.post('/walk', createWalkHandler({
|
|
3679
|
-
getWatchPaths: () => getConfig().watch.paths,
|
|
5209
|
+
getWatchPaths: () => extractWatchPathStrings(getConfig().watch.paths),
|
|
3680
5210
|
getFileSystemWatcher,
|
|
3681
5211
|
logger,
|
|
3682
5212
|
}));
|
|
@@ -3771,6 +5301,17 @@ function createApiServer(options) {
|
|
|
3771
5301
|
app.post('/points/delete', createPointsDeleteHandler({ vectorStore, logger }));
|
|
3772
5302
|
app.post('/rules/reapply', createRulesReapplyHandler({ processor, vectorStore, logger }));
|
|
3773
5303
|
}
|
|
5304
|
+
// VCS routes — only registered when a VcsCoordinator is provided
|
|
5305
|
+
if (options.vcsCoordinator) {
|
|
5306
|
+
const coordinator = options.vcsCoordinator;
|
|
5307
|
+
app.get('/vcs/status', createVcsStatusHandler({ coordinator, logger }));
|
|
5308
|
+
app.get('/vcs/history', createVcsHistoryHandler({ coordinator, logger }));
|
|
5309
|
+
app.get('/vcs/show', createVcsShowHandler({ coordinator, logger }));
|
|
5310
|
+
app.get('/vcs/diff', createVcsDiffHandler({ coordinator, logger }));
|
|
5311
|
+
app.get('/vcs/check-exclusion', createVcsCheckExclusionHandler({ coordinator, logger }));
|
|
5312
|
+
app.post('/vcs/revert', createVcsRevertHandler({ coordinator, logger }));
|
|
5313
|
+
app.post('/vcs/exclude', createVcsExcludeHandler({ coordinator, logger }));
|
|
5314
|
+
}
|
|
3774
5315
|
return app;
|
|
3775
5316
|
}
|
|
3776
5317
|
|
|
@@ -4332,6 +5873,8 @@ function computeLineOffsets(fullText, chunks) {
|
|
|
4332
5873
|
*/
|
|
4333
5874
|
/** Default number of points to upsert per batch (prevents OOM on large files). */
|
|
4334
5875
|
const DEFAULT_UPSERT_BATCH_SIZE = 50;
|
|
5876
|
+
/** Maximum chunks per Gemini batchEmbedContents request. */
|
|
5877
|
+
const EMBED_BATCH_SIZE = 100;
|
|
4335
5878
|
/**
|
|
4336
5879
|
* Embed text chunks, upsert to vector store, and clean up orphaned chunks.
|
|
4337
5880
|
*
|
|
@@ -4350,8 +5893,14 @@ async function embedAndUpsert(deps, text, filePath, metadata, existingPayload, f
|
|
|
4350
5893
|
const chunks = await splitter.splitText(text);
|
|
4351
5894
|
// Compute line offsets
|
|
4352
5895
|
const offsets = computeLineOffsets(text, chunks);
|
|
4353
|
-
// Embed
|
|
4354
|
-
const
|
|
5896
|
+
// Embed chunks in batches to respect Gemini's 100-doc limit
|
|
5897
|
+
const allVectors = [];
|
|
5898
|
+
for (let start = 0; start < chunks.length; start += EMBED_BATCH_SIZE) {
|
|
5899
|
+
const batch = chunks.slice(start, start + EMBED_BATCH_SIZE);
|
|
5900
|
+
const batchVectors = await embeddingProvider.embed(batch);
|
|
5901
|
+
allVectors.push(...batchVectors);
|
|
5902
|
+
}
|
|
5903
|
+
const vectors = allVectors;
|
|
4355
5904
|
// Build all points
|
|
4356
5905
|
const points = chunks.map((chunk, i) => ({
|
|
4357
5906
|
id: pointId(filePath, i),
|
|
@@ -5031,9 +6580,10 @@ async function ensureTextIndex(client, collectionName, fieldName, log) {
|
|
|
5031
6580
|
* @param limit - Maximum results to return.
|
|
5032
6581
|
* @param textWeight - Weight for text results in RRF (0–1).
|
|
5033
6582
|
* @param filter - Optional Qdrant filter.
|
|
6583
|
+
* @param offset - Optional result offset for pagination.
|
|
5034
6584
|
* @returns An array of search results.
|
|
5035
6585
|
*/
|
|
5036
|
-
async function hybridSearch(client, collectionName, vector, queryText, limit, textWeight, filter) {
|
|
6586
|
+
async function hybridSearch(client, collectionName, vector, queryText, limit, textWeight, filter, offset) {
|
|
5037
6587
|
const prefetchLimit = Math.max(limit * 3, 20);
|
|
5038
6588
|
const vectorWeight = 1 - textWeight;
|
|
5039
6589
|
const textFilter = {
|
|
@@ -5062,6 +6612,7 @@ async function hybridSearch(client, collectionName, vector, queryText, limit, te
|
|
|
5062
6612
|
},
|
|
5063
6613
|
limit,
|
|
5064
6614
|
with_payload: true,
|
|
6615
|
+
...(offset !== undefined ? { offset } : {}),
|
|
5065
6616
|
});
|
|
5066
6617
|
return result.points.map((p) => ({
|
|
5067
6618
|
id: String(p.id),
|
|
@@ -5359,10 +6910,11 @@ class VectorStoreClient {
|
|
|
5359
6910
|
* @param limit - Maximum results to return.
|
|
5360
6911
|
* @param textWeight - Weight for text results in RRF (0–1).
|
|
5361
6912
|
* @param filter - Optional Qdrant filter.
|
|
6913
|
+
* @param offset - Optional result offset for pagination.
|
|
5362
6914
|
* @returns An array of search results.
|
|
5363
6915
|
*/
|
|
5364
|
-
async hybridSearch(vector, queryText, limit, textWeight, filter) {
|
|
5365
|
-
return hybridSearch(this.client, this.collectionName, vector, queryText, limit, textWeight, filter);
|
|
6916
|
+
async hybridSearch(vector, queryText, limit, textWeight, filter, offset) {
|
|
6917
|
+
return hybridSearch(this.client, this.collectionName, vector, queryText, limit, textWeight, filter, offset);
|
|
5366
6918
|
}
|
|
5367
6919
|
/**
|
|
5368
6920
|
* Retrieve points with their vectors by ID.
|
|
@@ -5771,6 +7323,7 @@ class FileSystemWatcher {
|
|
|
5771
7323
|
gitignoreFilter;
|
|
5772
7324
|
initialScanTracker;
|
|
5773
7325
|
contentHashCache;
|
|
7326
|
+
onVcsFileChange;
|
|
5774
7327
|
moveCorrelator;
|
|
5775
7328
|
globMatches;
|
|
5776
7329
|
watcher;
|
|
@@ -5791,6 +7344,7 @@ class FileSystemWatcher {
|
|
|
5791
7344
|
this.gitignoreFilter = options.gitignoreFilter;
|
|
5792
7345
|
this.initialScanTracker = options.initialScanTracker;
|
|
5793
7346
|
this.contentHashCache = options.contentHashCache;
|
|
7347
|
+
this.onVcsFileChange = options.onVcsFileChange;
|
|
5794
7348
|
this.globMatches = () => true;
|
|
5795
7349
|
const healthOptions = {
|
|
5796
7350
|
maxRetries: options.maxRetries,
|
|
@@ -5808,7 +7362,7 @@ class FileSystemWatcher {
|
|
|
5808
7362
|
// Glob patterns are silently treated as literal strings, producing zero
|
|
5809
7363
|
// events. We extract static directory roots for chokidar to watch, then
|
|
5810
7364
|
// filter emitted events against the original globs via picomatch.
|
|
5811
|
-
const { roots, matches } = resolveWatchPaths(this.config.paths);
|
|
7365
|
+
const { roots, matches } = resolveWatchPaths(extractWatchPathStrings(this.config.paths));
|
|
5812
7366
|
this.globMatches = matches;
|
|
5813
7367
|
this.logger.info({ roots }, 'Resolved watch roots from globs');
|
|
5814
7368
|
// Chokidar v5's inline anymatch does exact string equality for string
|
|
@@ -5889,6 +7443,8 @@ class FileSystemWatcher {
|
|
|
5889
7443
|
this.initialScanTracker?.incrementEnqueued();
|
|
5890
7444
|
}
|
|
5891
7445
|
this.logger.debug({ path }, 'File added');
|
|
7446
|
+
if (initialScanComplete)
|
|
7447
|
+
this.onVcsFileChange?.(path, 'add');
|
|
5892
7448
|
if (correlator && initialScanComplete) {
|
|
5893
7449
|
void correlator.handleAdd(path);
|
|
5894
7450
|
}
|
|
@@ -5905,6 +7461,7 @@ class FileSystemWatcher {
|
|
|
5905
7461
|
if (this.isGitignored(path))
|
|
5906
7462
|
return;
|
|
5907
7463
|
this.logger.debug({ path }, 'File changed');
|
|
7464
|
+
this.onVcsFileChange?.(path, 'change');
|
|
5908
7465
|
this.queue.enqueue({ type: 'modify', path, priority: 'normal' }, () => this.wrapProcessing(() => this.processor.processFile(path)));
|
|
5909
7466
|
});
|
|
5910
7467
|
this.watcher.on('unlink', (path) => {
|
|
@@ -5914,6 +7471,7 @@ class FileSystemWatcher {
|
|
|
5914
7471
|
if (this.isGitignored(path))
|
|
5915
7472
|
return;
|
|
5916
7473
|
this.logger.debug({ path }, 'File removed');
|
|
7474
|
+
this.onVcsFileChange?.(path, 'unlink');
|
|
5917
7475
|
if (correlator) {
|
|
5918
7476
|
correlator.handleUnlink(path);
|
|
5919
7477
|
}
|
|
@@ -6065,6 +7623,7 @@ class JeevesWatcher {
|
|
|
6065
7623
|
gitignoreFilter;
|
|
6066
7624
|
enrichmentStore;
|
|
6067
7625
|
contentHashCache;
|
|
7626
|
+
vcsCoordinator;
|
|
6068
7627
|
initialScanTracker;
|
|
6069
7628
|
version;
|
|
6070
7629
|
/** Create a new JeevesWatcher instance. */
|
|
@@ -6087,6 +7646,7 @@ class JeevesWatcher {
|
|
|
6087
7646
|
async start() {
|
|
6088
7647
|
const logger = this.factories.createLogger(this.config.logging);
|
|
6089
7648
|
this.logger = logger;
|
|
7649
|
+
await initVcs(this.config, logger);
|
|
6090
7650
|
const { embeddingProvider, vectorStore } = await initEmbeddingAndStore(this.config, this.factories, logger);
|
|
6091
7651
|
this.embeddingProvider = embeddingProvider;
|
|
6092
7652
|
this.vectorStore = vectorStore;
|
|
@@ -6123,10 +7683,15 @@ class JeevesWatcher {
|
|
|
6123
7683
|
concurrency: this.config.embedding.concurrency ?? 5,
|
|
6124
7684
|
rateLimitPerMinute: this.config.embedding.rateLimitPerMinute,
|
|
6125
7685
|
});
|
|
6126
|
-
const
|
|
7686
|
+
const vcsCoordinator = new VcsCoordinator(this.config, logger);
|
|
7687
|
+
this.vcsCoordinator = vcsCoordinator;
|
|
7688
|
+
const { watcher, gitignoreFilter } = createWatcher(this.config, this.factories, this.queue, processor, logger, this.runtimeOptions, this.initialScanTracker, contentHashCache, (filePath, event) => {
|
|
7689
|
+
vcsCoordinator.onFileChange(filePath, event);
|
|
7690
|
+
});
|
|
6127
7691
|
this.watcher = watcher;
|
|
6128
7692
|
this.gitignoreFilter = gitignoreFilter;
|
|
6129
7693
|
this.server = await this.startApiServer();
|
|
7694
|
+
vcsCoordinator.start();
|
|
6130
7695
|
this.watcher.start();
|
|
6131
7696
|
this.startConfigWatch();
|
|
6132
7697
|
logger.info('jeeves-watcher started');
|
|
@@ -6136,6 +7701,9 @@ class JeevesWatcher {
|
|
|
6136
7701
|
*/
|
|
6137
7702
|
async stop() {
|
|
6138
7703
|
await this.stopConfigWatch();
|
|
7704
|
+
if (this.vcsCoordinator) {
|
|
7705
|
+
await this.vcsCoordinator.stop();
|
|
7706
|
+
}
|
|
6139
7707
|
if (this.watcher) {
|
|
6140
7708
|
await this.watcher.stop();
|
|
6141
7709
|
}
|
|
@@ -6183,6 +7751,7 @@ class JeevesWatcher {
|
|
|
6183
7751
|
fileSystemWatcher: this.watcher,
|
|
6184
7752
|
getFileSystemWatcher: () => this.watcher,
|
|
6185
7753
|
enrichmentStore: this.enrichmentStore,
|
|
7754
|
+
vcsCoordinator: this.vcsCoordinator,
|
|
6186
7755
|
});
|
|
6187
7756
|
await server.listen({
|
|
6188
7757
|
host: this.config.api?.host ?? getBindAddress('watcher'),
|