@polycode-projects/the-mechanical-code-talker 6.0.7 → 6.0.9
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 +1 -1
- package/src/services/corpus-loader.mjs +76 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polycode-projects/the-mechanical-code-talker",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.9",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "The Mechanical Code Talker (tmct) — a tolerant, offline, $0 chat surface that guides you toward precision queries about a software repository. ELIZA/PARRY-style but domain-obsessed with code. No model calls; indexes a repo on request (tmct index) or reads any producer's graph.",
|
|
@@ -33,10 +33,38 @@ import {
|
|
|
33
33
|
bandPartitionKey, bandSortKeyForRow, buildBandManifest, bandLicenseInfo, MANIFEST_SORT_KEY,
|
|
34
34
|
} from "../adapters/memory/corpus-bands.mjs";
|
|
35
35
|
import { BackendRejected, BackendUnavailable, assertValidRow } from "../adapters/memory/row-backend.mjs";
|
|
36
|
+
import { isSystemicFailure } from "../adapters/memory/systemic-failure.mjs";
|
|
36
37
|
import { termQueryOverDocumentClient } from "./subgraph-retrieval.mjs";
|
|
37
38
|
|
|
38
39
|
const DEFAULT_WRITE_CONCURRENCY = 25;
|
|
39
40
|
|
|
41
|
+
// A run against millions of rows meets an occasional transient 500 or
|
|
42
|
+
// throttle as a certainty, not a rare event, so every store call gets its
|
|
43
|
+
// own retry budget rather than treating the first one as fatal. Five
|
|
44
|
+
// attempts with exponential backoff from 100 ms, full jitter, so many
|
|
45
|
+
// concurrent writers recovering from the same outage don't retry in
|
|
46
|
+
// lockstep.
|
|
47
|
+
const DEFAULT_RETRY_ATTEMPTS = 5;
|
|
48
|
+
const RETRY_BASE_DELAY_MS = 100;
|
|
49
|
+
|
|
50
|
+
const defaultSleep = (ms) => new Promise((resolve) => { setTimeout(resolve, ms); });
|
|
51
|
+
const defaultRandom = () => Math.random();
|
|
52
|
+
|
|
53
|
+
/** Delay (ms) before the retry that follows a failed `attempt` (1-based):
|
|
54
|
+
* full jitter, a value drawn uniformly from `[0, baseDelayMs * 2^(attempt-1))`. */
|
|
55
|
+
function retryDelayMs(attempt, baseDelayMs, random) {
|
|
56
|
+
return random() * baseDelayMs * (2 ** (attempt - 1));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Build the retry knobs `call` reads from a caller's options object,
|
|
60
|
+
* defaulting to the production policy. */
|
|
61
|
+
function retryPolicyFrom({
|
|
62
|
+
retryAttempts = DEFAULT_RETRY_ATTEMPTS, retryBaseDelayMs = RETRY_BASE_DELAY_MS,
|
|
63
|
+
sleep = defaultSleep, random = defaultRandom,
|
|
64
|
+
} = {}) {
|
|
65
|
+
return { attempts: retryAttempts, baseDelayMs: retryBaseDelayMs, sleep, random };
|
|
66
|
+
}
|
|
67
|
+
|
|
40
68
|
/** Runs `fn` over `items` with at most `limit` calls in flight, mirroring the
|
|
41
69
|
* DynamoDB row backend's own bounded-concurrency write loop. */
|
|
42
70
|
async function mapWithConcurrency(items, limit, fn) {
|
|
@@ -55,13 +83,28 @@ async function mapWithConcurrency(items, limit, fn) {
|
|
|
55
83
|
|
|
56
84
|
/** Every store call funnels through here so a network failure or a
|
|
57
85
|
* 429/5xx-shaped rejection surfaces as `BackendUnavailable`, never a raw
|
|
58
|
-
* client error a caller would have to string-match.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
86
|
+
* client error a caller would have to string-match. A systemic failure — a
|
|
87
|
+
* throttle, a 5xx, a transient network fault — retries with exponential
|
|
88
|
+
* backoff up to `retry.attempts` times; a validation/schema rejection is
|
|
89
|
+
* not systemic and fails on the first try, because retrying it gets the
|
|
90
|
+
* same answer. The final error names how many attempts were made, so a
|
|
91
|
+
* genuinely down service reads clearly rather than looking like one bad
|
|
92
|
+
* call. */
|
|
93
|
+
async function call(fn, label, retry) {
|
|
94
|
+
for (let attempt = 1; attempt <= retry.attempts; attempt += 1) {
|
|
95
|
+
try {
|
|
96
|
+
return await fn();
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if (error instanceof BackendRejected || error instanceof BackendUnavailable) throw error;
|
|
99
|
+
if (!isSystemicFailure(error) || attempt === retry.attempts) {
|
|
100
|
+
const attemptsNote = attempt > 1 ? ` after ${attempt} attempts` : "";
|
|
101
|
+
throw new BackendUnavailable(
|
|
102
|
+
`corpus loader's ${label} call failed${attemptsNote}: ${error.message}`,
|
|
103
|
+
{ cause: error },
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
await retry.sleep(retryDelayMs(attempt, retry.baseDelayMs, retry.random));
|
|
107
|
+
}
|
|
65
108
|
}
|
|
66
109
|
}
|
|
67
110
|
|
|
@@ -99,11 +142,15 @@ async function* readWireRows(path) {
|
|
|
99
142
|
}
|
|
100
143
|
}
|
|
101
144
|
|
|
102
|
-
/** The manifest row for `band`, or null when the band has never been loaded.
|
|
103
|
-
|
|
145
|
+
/** The manifest row for `band`, or null when the band has never been loaded.
|
|
146
|
+
* `retryAttempts`/`retryBaseDelayMs`/`sleep`/`random` tune the retry policy
|
|
147
|
+
* every store call in this module shares; see `retryPolicyFrom`. */
|
|
148
|
+
export async function bandStatus({ client, tableName, band, ...retryOptions }) {
|
|
149
|
+
const retry = retryPolicyFrom(retryOptions);
|
|
104
150
|
const result = await call(
|
|
105
151
|
() => client.get({ TableName: tableName, Key: { pk: bandPartitionKey(band), sk: MANIFEST_SORT_KEY } }),
|
|
106
152
|
"get",
|
|
153
|
+
retry,
|
|
107
154
|
);
|
|
108
155
|
return result?.Item ? { ...result.Item } : null;
|
|
109
156
|
}
|
|
@@ -111,7 +158,7 @@ export async function bandStatus({ client, tableName, band }) {
|
|
|
111
158
|
/** Every item (fact rows and the manifest, if present) under a band's
|
|
112
159
|
* partition, paginated. Shared by `clearBand` and any caller that wants a
|
|
113
160
|
* full inventory rather than one term's rows. */
|
|
114
|
-
async function queryWholePartition({ client, tableName, band }) {
|
|
161
|
+
async function queryWholePartition({ client, tableName, band, retry }) {
|
|
115
162
|
const items = [];
|
|
116
163
|
let exclusiveStartKey;
|
|
117
164
|
do {
|
|
@@ -120,7 +167,7 @@ async function queryWholePartition({ client, tableName, band }) {
|
|
|
120
167
|
KeyConditionExpression: "pk = :pk",
|
|
121
168
|
ExpressionAttributeValues: { ":pk": bandPartitionKey(band) },
|
|
122
169
|
...(exclusiveStartKey ? { ExclusiveStartKey: exclusiveStartKey } : {}),
|
|
123
|
-
}), "query");
|
|
170
|
+
}), "query", retry);
|
|
124
171
|
items.push(...(response?.Items || []));
|
|
125
172
|
exclusiveStartKey = response?.LastEvaluatedKey || null;
|
|
126
173
|
} while (exclusiveStartKey);
|
|
@@ -130,18 +177,21 @@ async function queryWholePartition({ client, tableName, band }) {
|
|
|
130
177
|
/** Load `band` from a jsonl source of wire rows. Resolves to
|
|
131
178
|
* `{status: "unchanged" | "loaded" | "dry-run", band, rowCount, sourceDigest}`.
|
|
132
179
|
* `dryRun` computes the digest and reports what would happen without
|
|
133
|
-
* writing anything.
|
|
180
|
+
* writing anything. `retryAttempts`/`retryBaseDelayMs`/`sleep`/`random` tune
|
|
181
|
+
* the retry policy every store call below shares; see `retryPolicyFrom`. */
|
|
134
182
|
export async function loadBand({
|
|
135
183
|
client, tableName, band, source, dryRun = false,
|
|
136
184
|
writeConcurrency = DEFAULT_WRITE_CONCURRENCY, now = () => new Date().toISOString(),
|
|
185
|
+
retryAttempts, retryBaseDelayMs, sleep, random,
|
|
137
186
|
}) {
|
|
138
187
|
if (!client) throw new TypeError("loadBand needs a client");
|
|
139
188
|
if (!tableName) throw new TypeError("loadBand needs a tableName");
|
|
140
189
|
if (!band) throw new TypeError("loadBand needs a band");
|
|
141
190
|
if (!source) throw new TypeError("loadBand needs a source path");
|
|
142
191
|
|
|
192
|
+
const retry = retryPolicyFrom({ retryAttempts, retryBaseDelayMs, sleep, random });
|
|
143
193
|
const sourceDigest = await digestFile(source);
|
|
144
|
-
const existing = await bandStatus({ client, tableName, band });
|
|
194
|
+
const existing = await bandStatus({ client, tableName, band, retryAttempts, retryBaseDelayMs, sleep, random });
|
|
145
195
|
if (existing?.sourceDigest === sourceDigest) {
|
|
146
196
|
return { status: "unchanged", band, rowCount: existing.rowCount, sourceDigest };
|
|
147
197
|
}
|
|
@@ -159,7 +209,7 @@ export async function loadBand({
|
|
|
159
209
|
pk, sk: bandSortKeyForRow(row.rowClass, row.term, row.rowKey),
|
|
160
210
|
rowKey: row.rowKey, rowClass: row.rowClass, term: row.term, json: row.json,
|
|
161
211
|
};
|
|
162
|
-
await call(() => client.put({ TableName: tableName, Item: item }), "put");
|
|
212
|
+
await call(() => client.put({ TableName: tableName, Item: item }), "put", retry);
|
|
163
213
|
});
|
|
164
214
|
const manifest = buildBandManifest({
|
|
165
215
|
band, rowCount, loadedAt: now(), sourceDigest, license, notice,
|
|
@@ -167,7 +217,7 @@ export async function loadBand({
|
|
|
167
217
|
await call(() => client.put({
|
|
168
218
|
TableName: tableName,
|
|
169
219
|
Item: { pk, sk: MANIFEST_SORT_KEY, ...manifest },
|
|
170
|
-
}), "put");
|
|
220
|
+
}), "put", retry);
|
|
171
221
|
return { status: "loaded", band, rowCount, sourceDigest };
|
|
172
222
|
}
|
|
173
223
|
|
|
@@ -177,21 +227,27 @@ export async function loadBand({
|
|
|
177
227
|
|
|
178
228
|
/** Physically delete every item under `band`'s partition, manifest last.
|
|
179
229
|
* Idempotent: an empty partition (already cleared, or never loaded) is a
|
|
180
|
-
* no-op. Resolves to `{band, deleted}`.
|
|
181
|
-
|
|
230
|
+
* no-op. Resolves to `{band, deleted}`. `retryAttempts`/`retryBaseDelayMs`/
|
|
231
|
+
* `sleep`/`random` tune the retry policy every store call below shares;
|
|
232
|
+
* see `retryPolicyFrom`. */
|
|
233
|
+
export async function clearBand({
|
|
234
|
+
client, tableName, band, writeConcurrency = DEFAULT_WRITE_CONCURRENCY,
|
|
235
|
+
retryAttempts, retryBaseDelayMs, sleep, random,
|
|
236
|
+
}) {
|
|
182
237
|
if (!client) throw new TypeError("clearBand needs a client");
|
|
183
238
|
if (!tableName) throw new TypeError("clearBand needs a tableName");
|
|
184
239
|
if (!band) throw new TypeError("clearBand needs a band");
|
|
185
240
|
|
|
186
|
-
const
|
|
241
|
+
const retry = retryPolicyFrom({ retryAttempts, retryBaseDelayMs, sleep, random });
|
|
242
|
+
const items = await queryWholePartition({ client, tableName, band, retry });
|
|
187
243
|
const facts = items.filter((item) => item.sk !== MANIFEST_SORT_KEY);
|
|
188
244
|
const manifestItem = items.find((item) => item.sk === MANIFEST_SORT_KEY);
|
|
189
245
|
|
|
190
246
|
await mapWithConcurrency(facts, writeConcurrency, async (item) => {
|
|
191
|
-
await call(() => client.delete({ TableName: tableName, Key: { pk: item.pk, sk: item.sk } }), "delete");
|
|
247
|
+
await call(() => client.delete({ TableName: tableName, Key: { pk: item.pk, sk: item.sk } }), "delete", retry);
|
|
192
248
|
});
|
|
193
249
|
if (manifestItem) {
|
|
194
|
-
await call(() => client.delete({ TableName: tableName, Key: { pk: manifestItem.pk, sk: manifestItem.sk } }), "delete");
|
|
250
|
+
await call(() => client.delete({ TableName: tableName, Key: { pk: manifestItem.pk, sk: manifestItem.sk } }), "delete", retry);
|
|
195
251
|
}
|
|
196
252
|
return { band, deleted: items.length };
|
|
197
253
|
}
|