@mnemonik/scanner 5.136.2 → 5.151.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mnemonik/scanner",
3
- "version": "5.136.2",
3
+ "version": "5.151.0",
4
4
  "description": "Automatic codebase indexing daemon for Mnemonik",
5
5
  "type": "module",
6
6
  "bin": {
@@ -20,6 +20,6 @@
20
20
  "devDependencies": {
21
21
  "typescript": "^5.3.3",
22
22
  "@types/node": "^20.19.43",
23
- "vitest": "^4.1.9"
23
+ "vitest": "^4.1.10"
24
24
  }
25
25
  }
package/src/client.ts CHANGED
@@ -53,6 +53,16 @@ export interface GitMiningMeta {
53
53
 
54
54
  const SCAN_PUSH_BATCH_DELAY_MS = 500;
55
55
 
56
+ /**
57
+ * Exponential backoff (base 2000ms × attempt) with ±25% jitter, to avoid a
58
+ * fleet of scanners retrying in lockstep after a shared 5xx/network blip
59
+ * (thundering herd). Base values unchanged from the pre-jitter constants.
60
+ */
61
+ function backoffWithJitter(attempt: number): number {
62
+ const base = 2000 * (attempt + 1);
63
+ return Math.round(base * (0.75 + Math.random() * 0.5));
64
+ }
65
+
56
66
  export class MnemonikClient {
57
67
  constructor(
58
68
  private serverUrl: string,
@@ -83,7 +93,7 @@ export class MnemonikClient {
83
93
  continue;
84
94
  }
85
95
  if (res.status >= 500 && attempt < retries) {
86
- await new Promise((r) => setTimeout(r, 2000 * (attempt + 1)));
96
+ await new Promise((r) => setTimeout(r, backoffWithJitter(attempt)));
87
97
  continue;
88
98
  }
89
99
  throw new Error(`${res.status} ${res.statusText}: ${text}`);
@@ -92,7 +102,7 @@ export class MnemonikClient {
92
102
  return res.json() as Promise<T>;
93
103
  } catch (err) {
94
104
  if (attempt < retries && (err as Error).message?.includes('fetch failed')) {
95
- await new Promise((r) => setTimeout(r, 2000 * (attempt + 1)));
105
+ await new Promise((r) => setTimeout(r, backoffWithJitter(attempt)));
96
106
  continue;
97
107
  }
98
108
  throw err;
package/dist/client.d.ts DELETED
@@ -1,99 +0,0 @@
1
- export interface ScanPushFile {
2
- path: string;
3
- hash: string;
4
- chunks: Array<{
5
- content: string;
6
- startLine: number;
7
- endLine: number;
8
- chunkType: string;
9
- language: string;
10
- contentHash: string;
11
- metadata: {
12
- fileName: string;
13
- extension: string;
14
- size: number;
15
- };
16
- }>;
17
- /**
18
- * Optional raw file content. Sent by the scanner daemon for ALL files
19
- * (code and authority/manifest alike) so the doc-truth worker can run
20
- * claim extraction against whole documents without filesystem access.
21
- * Omitted when the file exceeds the server's 5 MB schema cap or when
22
- * the raw content could not be read (chunk-based fallback path).
23
- */
24
- content?: string;
25
- }
26
- export interface ScanPushCommit {
27
- sha: string;
28
- author: string;
29
- date: string;
30
- message: string;
31
- files: string[];
32
- }
33
- export interface ScanPushPayload {
34
- projectId: string;
35
- files: ScanPushFile[];
36
- commits?: ScanPushCommit[];
37
- }
38
- export interface ScanStatusResponse {
39
- files: Array<{
40
- path: string;
41
- hash: string;
42
- }>;
43
- gitMining?: {
44
- enabled: boolean;
45
- lastMinedCommit: string | null;
46
- };
47
- }
48
- export interface GitMiningMeta {
49
- enabled: boolean;
50
- lastMinedCommit: string | null;
51
- }
52
- export declare class MnemonikClient {
53
- private serverUrl;
54
- private apiKey;
55
- constructor(serverUrl: string, apiKey: string);
56
- private request;
57
- /**
58
- * Fetch per-file hashes for dedup, plus git-mining metadata.
59
- * The daemon uses `gitMining.enabled` to decide whether to collect commits
60
- * this cycle, and `lastMinedCommit` as the lower bound of `git log`.
61
- */
62
- getStatus(projectId: string): Promise<{
63
- fileHashes: Map<string, string>;
64
- gitMining: GitMiningMeta;
65
- }>;
66
- /**
67
- * Push file chunks in batches. When `commits` is supplied (non-empty) it is
68
- * attached to the first batch only — BullMQ's idempotent jobId means a
69
- * duplicate would collapse anyway, but one payload saves bandwidth.
70
- *
71
- * When `files` is empty and `commits` is non-empty, a single commit-only
72
- * push is made — the server accepts `files=[]` since If both are
73
- * empty, no request is sent.
74
- */
75
- pushFiles(projectId: string, files: ScanPushFile[], commits?: ScanPushCommit[]): Promise<{
76
- success: boolean;
77
- }>;
78
- sendHeartbeat(projectId: string, scanner: {
79
- scope: 'global';
80
- version?: string;
81
- }): Promise<void>;
82
- /**
83
- * Notify the server of files that have been removed since the daemon's
84
- * previous scan of this project. Server deprecates exactly those code
85
- * memories. Empty arrays are accepted as no-ops so the daemon can call
86
- * this every tick regardless of whether anything was removed.
87
- *
88
- * The caller is responsible for computing the removal set locally — the
89
- * old inventory-diff shape that asked the server to derive removals from
90
- * a "known files" list has been removed because a small/malformed list
91
- * would mass-deprecate. The narrow `removedFiles` shape cannot exhibit
92
- * that failure mode by construction.
93
- */
94
- reportRemovedFiles(projectId: string, removedFiles: string[]): Promise<{
95
- deprecated: number;
96
- couplingsRemoved: number;
97
- }>;
98
- healthCheck(): Promise<boolean>;
99
- }
package/dist/client.js DELETED
@@ -1,131 +0,0 @@
1
- const SCAN_PUSH_BATCH_DELAY_MS = 500;
2
- export class MnemonikClient {
3
- serverUrl;
4
- apiKey;
5
- constructor(serverUrl, apiKey) {
6
- this.serverUrl = serverUrl;
7
- this.apiKey = apiKey;
8
- }
9
- async request(path, body, retries = 3) {
10
- const url = `${this.serverUrl}${path}`;
11
- for (let attempt = 0; attempt <= retries; attempt++) {
12
- try {
13
- const res = await fetch(url, {
14
- method: 'POST',
15
- headers: {
16
- 'Content-Type': 'application/json',
17
- Authorization: `Bearer ${this.apiKey}`,
18
- },
19
- body: JSON.stringify(body),
20
- });
21
- if (!res.ok) {
22
- const text = await res.text().catch(() => 'Unknown error');
23
- if (res.status === 401 || res.status === 403) {
24
- throw new Error(`Auth failed (${res.status}). Check your API key.`);
25
- }
26
- if (res.status === 503 && attempt < retries) {
27
- const retryAfter = parseInt(res.headers.get('Retry-After') || '5', 10);
28
- await new Promise((r) => setTimeout(r, retryAfter * 1000));
29
- continue;
30
- }
31
- if (res.status >= 500 && attempt < retries) {
32
- await new Promise((r) => setTimeout(r, 2000 * (attempt + 1)));
33
- continue;
34
- }
35
- throw new Error(`${res.status} ${res.statusText}: ${text}`);
36
- }
37
- return res.json();
38
- }
39
- catch (err) {
40
- if (attempt < retries && err.message?.includes('fetch failed')) {
41
- await new Promise((r) => setTimeout(r, 2000 * (attempt + 1)));
42
- continue;
43
- }
44
- throw err;
45
- }
46
- }
47
- throw new Error(`Request to ${path} failed after ${retries} retries`);
48
- }
49
- /**
50
- * Fetch per-file hashes for dedup, plus git-mining metadata.
51
- * The daemon uses `gitMining.enabled` to decide whether to collect commits
52
- * this cycle, and `lastMinedCommit` as the lower bound of `git log`.
53
- */
54
- async getStatus(projectId) {
55
- const result = await this.request('/api/v1/scan/status', { projectId });
56
- return {
57
- fileHashes: new Map(result.files.map((f) => [f.path, f.hash])),
58
- gitMining: result.gitMining ?? { enabled: false, lastMinedCommit: null },
59
- };
60
- }
61
- /**
62
- * Push file chunks in batches. When `commits` is supplied (non-empty) it is
63
- * attached to the first batch only — BullMQ's idempotent jobId means a
64
- * duplicate would collapse anyway, but one payload saves bandwidth.
65
- *
66
- * When `files` is empty and `commits` is non-empty, a single commit-only
67
- * push is made — the server accepts `files=[]` since If both are
68
- * empty, no request is sent.
69
- */
70
- async pushFiles(projectId, files, commits) {
71
- const hasCommits = !!commits && commits.length > 0;
72
- if (files.length === 0) {
73
- if (hasCommits) {
74
- await this.request('/api/v1/scan/push', { projectId, files: [], commits });
75
- }
76
- return { success: true };
77
- }
78
- const batchSize = 25;
79
- let attachedCommits = false;
80
- for (let i = 0; i < files.length; i += batchSize) {
81
- const batch = files.slice(i, i + batchSize);
82
- const body = { projectId, files: batch };
83
- if (!attachedCommits && hasCommits) {
84
- body.commits = commits;
85
- attachedCommits = true;
86
- }
87
- await this.request('/api/v1/scan/push', body);
88
- if (i + batchSize < files.length) {
89
- await sleep(SCAN_PUSH_BATCH_DELAY_MS);
90
- }
91
- }
92
- return { success: true };
93
- }
94
- async sendHeartbeat(projectId, scanner) {
95
- await this.request('/api/v1/scan/heartbeat', { projectId, scanner });
96
- }
97
- /**
98
- * Notify the server of files that have been removed since the daemon's
99
- * previous scan of this project. Server deprecates exactly those code
100
- * memories. Empty arrays are accepted as no-ops so the daemon can call
101
- * this every tick regardless of whether anything was removed.
102
- *
103
- * The caller is responsible for computing the removal set locally — the
104
- * old inventory-diff shape that asked the server to derive removals from
105
- * a "known files" list has been removed because a small/malformed list
106
- * would mass-deprecate. The narrow `removedFiles` shape cannot exhibit
107
- * that failure mode by construction.
108
- */
109
- async reportRemovedFiles(projectId, removedFiles) {
110
- const result = await this.request('/api/v1/scan/reconcile', { projectId, removedFiles });
111
- return {
112
- deprecated: result.deprecated ?? 0,
113
- couplingsRemoved: result.couplingsRemoved ?? 0,
114
- };
115
- }
116
- async healthCheck() {
117
- try {
118
- const res = await fetch(`${this.serverUrl}/api/v1/health`, {
119
- headers: { Authorization: `Bearer ${this.apiKey}` },
120
- });
121
- return res.ok;
122
- }
123
- catch {
124
- return false;
125
- }
126
- }
127
- }
128
- function sleep(ms) {
129
- return new Promise((resolve) => setTimeout(resolve, ms));
130
- }
131
- //# sourceMappingURL=client.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAqDA,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAErC,MAAM,OAAO,cAAc;IAEf;IACA;IAFV,YACU,SAAiB,EACjB,MAAc;QADd,cAAS,GAAT,SAAS,CAAQ;QACjB,WAAM,GAAN,MAAM,CAAQ;IACrB,CAAC;IAEI,KAAK,CAAC,OAAO,CAAI,IAAY,EAAE,IAAa,EAAE,OAAO,GAAG,CAAC;QAC/D,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,SAAS,GAAG,IAAI,EAAE,CAAC;QACvC,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC;YACpD,IAA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MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAI9B,wBAAwB,EAAE,EAAE,SAAS,EAAE,YAAY,EAAE,CAAC,CAAC;QAC1D,OAAO;YACL,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,CAAC;YAClC,gBAAgB,EAAE,MAAM,CAAC,gBAAgB,IAAI,CAAC;SAC/C,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,WAAW;QACf,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,SAAS,gBAAgB,EAAE;gBACzD,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,IAAI,CAAC,MAAM,EAAE,EAAE;aACpD,CAAC,CAAC;YACH,OAAO,GAAG,CAAC,EAAE,CAAC;QAChB,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;CACF;AAED,SAAS,KAAK,CAAC,EAAU;IACvB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC"}
package/dist/daemon.d.ts DELETED
@@ -1,51 +0,0 @@
1
- export interface DaemonConfig {
2
- serverUrl: string;
3
- apiKey: string;
4
- roots: string[];
5
- refreshIntervalMs?: number;
6
- maxConcurrentScans?: number;
7
- }
8
- export declare class ScannerDaemon {
9
- private config;
10
- private client;
11
- private scanner;
12
- private projects;
13
- private refreshTimer;
14
- private heartbeatTimer;
15
- private discovery;
16
- private refreshIntervalMs;
17
- private maxConcurrentScans;
18
- private scannerVersion;
19
- constructor(config: DaemonConfig);
20
- start(): Promise<void>;
21
- stop(): Promise<void>;
22
- private sendHeartbeats;
23
- private getScannerVersion;
24
- getWatchedProjects(): Array<{
25
- projectId: string;
26
- path: string;
27
- name?: string;
28
- }>;
29
- /**
30
- * Discover projects from configured roots and reconcile with current watch list.
31
- */
32
- refreshProjects(): Promise<void>;
33
- private addProject;
34
- private waitForServer;
35
- private initialScan;
36
- /**
37
- * Send the explicit list of paths that vanished since the previous scan.
38
- * Empty lists short-circuit so we don't pay an HTTP round-trip when
39
- * nothing was removed this tick.
40
- */
41
- private sendRemovedFiles;
42
- /**
43
- * Fetch commits since the last mine. Swallows errors — a git failure must
44
- * not block the file scan push.
45
- */
46
- private collectCommits;
47
- private handleChanges;
48
- private startRetryLoop;
49
- private collectAuthorityPushFiles;
50
- private groupChunksByFile;
51
- }