@polycode-projects/the-mechanical-code-talker 6.0.3 → 6.0.5

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": "@polycode-projects/the-mechanical-code-talker",
3
- "version": "6.0.3",
3
+ "version": "6.0.5",
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.",
@@ -187,10 +187,10 @@
187
187
  "claim:news": "node scripts/news-rig.mjs",
188
188
  "roll": "node scripts/roll.mjs",
189
189
  "build:ask-bundle": "node scripts/build-ask-bundle.mjs",
190
- "build:row-service": "esbuild server/row-service/handler.mjs --bundle --platform=node --target=node20 --external:@aws-sdk/lib-dynamodb --external:@aws-sdk/client-dynamodb --external:@aws-sdk/client-lambda --outfile=server/row-service/dist/handler.js",
191
- "build:turn-service": "esbuild server/turn-service/handler.mjs --bundle --platform=node --target=node20 --external:@aws-sdk/lib-dynamodb --external:@aws-sdk/client-dynamodb --external:@aws-sdk/client-lambda --outfile=server/turn-service/dist/handler.js",
190
+ "build:row-service": "rm -rf server/row-service/dist && esbuild server/row-service/handler.mjs --banner:js=\"import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);\" --bundle --platform=node --target=node20 --external:@aws-sdk/lib-dynamodb --external:@aws-sdk/client-dynamodb --external:@aws-sdk/client-lambda --format=esm --outfile=server/row-service/dist/handler.mjs",
191
+ "build:turn-service": "rm -rf server/turn-service/dist && esbuild server/turn-service/handler.mjs --banner:js=\"import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);\" --bundle --platform=node --target=node20 --external:@aws-sdk/lib-dynamodb --external:@aws-sdk/client-dynamodb --external:@aws-sdk/client-lambda --format=esm --outfile=server/turn-service/dist/handler.mjs",
192
192
  "build:turn-seed": "node server/turn-service/build-seed.mjs",
193
- "build:news-worker": "esbuild server/news-worker/handler.mjs --bundle --platform=node --target=node20 --external:@aws-sdk/lib-dynamodb --external:@aws-sdk/client-dynamodb --external:@aws-sdk/client-lambda --outfile=server/news-worker/dist/handler.js",
193
+ "build:news-worker": "rm -rf server/news-worker/dist && esbuild server/news-worker/handler.mjs --banner:js=\"import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);\" --bundle --platform=node --target=node20 --external:@aws-sdk/lib-dynamodb --external:@aws-sdk/client-dynamodb --external:@aws-sdk/client-lambda --format=esm --outfile=server/news-worker/dist/handler.mjs",
194
194
  "build:chat-bundle": "node scripts/build-chat-bundle.mjs",
195
195
  "build:spider-fly-bundle": "node scripts/build-spider-fly-bundle.mjs",
196
196
  "build:plan-bundle": "node scripts/build-plan-bundle.mjs",
@@ -161,19 +161,24 @@ export function createDynamoRowBackend({
161
161
 
162
162
  async function readRows() {
163
163
  assertOpen();
164
- const expressionAttributeNames = { "#pk": names.pk, "#sk": names.sk };
165
- const filterClauses = ["NOT begins_with(#sk, :metaPrefix)"];
164
+ // Meta rows are excluded here in code, not in the FilterExpression:
165
+ // DynamoDB rejects any filter that references a key attribute, and the
166
+ // sort key is one.
167
+ const expressionAttributeNames = { "#pk": names.pk };
168
+ let filterExpression;
166
169
  if (softDelete) {
167
- filterClauses.push("attribute_not_exists(#deletedAt)");
170
+ filterExpression = "attribute_not_exists(#deletedAt)";
168
171
  expressionAttributeNames["#deletedAt"] = names.deletedAt;
169
172
  }
170
173
  const items = await query({
171
174
  keyConditionExpression: "#pk = :pk",
172
- filterExpression: filterClauses.join(" AND "),
175
+ filterExpression,
173
176
  expressionAttributeNames,
174
- expressionAttributeValues: { ":pk": sessionKey, ":metaPrefix": META_SK_PREFIX },
177
+ expressionAttributeValues: { ":pk": sessionKey },
175
178
  });
176
- return items.map(rowFromItem);
179
+ return items
180
+ .filter((item) => !String(item[names.sk] ?? "").startsWith(META_SK_PREFIX))
181
+ .map(rowFromItem);
177
182
  }
178
183
 
179
184
  async function readRowsByTerm(term) {
@@ -266,13 +266,13 @@ const WIKIDATA_RESEARCH_PROVENANCE_RE = /(?:^|:)research:wikidata:/;
266
266
  const WIKIDATA_QID_TERM_RE = /^q[1-9]\d*$/;
267
267
 
268
268
  // The two attached findings a row's own extraction may carry that disqualify
269
- // it from heading a card or anchoring novelty (PLAN_EXTRACTION_CONFIDENCE.md
270
- // section 2.5): an identifier-shaped token or a clause-fallback read is a
271
- // structural tell the row was mis-read, not a report a card should lead
272
- // with. `pronoun-carry` is deliberately absent — a subject carried from the
273
- // paragraph's own prose is not a mis-read, and barring it would suppress
274
- // real hubs. A row with no `extraction` at all (nothing recorded, or an old
275
- // row from before this plan) never matches.
269
+ // it from heading a card or anchoring novelty: an identifier-shaped token or
270
+ // a clause-fallback read is a structural tell the row was mis-read, not a
271
+ // report a card should lead with. `pronoun-carry` is deliberately absent — a
272
+ // subject carried from the paragraph's own prose is not a mis-read, and
273
+ // barring it would suppress real hubs. A row with no `extraction` at all
274
+ // (nothing recorded, or a row written before findings existed) never
275
+ // matches.
276
276
  const GATE_DECLINING_FINDINGS = new Set(["identifier-token", "clause-fallback"]);
277
277
 
278
278
  function rowCarriesGateDecliningFinding(row) {
@@ -14,11 +14,23 @@ import {
14
14
  } from "../../adapters/memory/row-backend.mjs";
15
15
 
16
16
  const SESSION_HEADER = "x-tmct-session";
17
+ const PAYLOAD_HASH_HEADER = "x-amz-content-sha256";
17
18
 
18
19
  function trimTrailingSlash(value) {
19
20
  return value.endsWith("/") ? value.slice(0, -1) : value;
20
21
  }
21
22
 
23
+ /** Hex SHA-256 of `text`, via Web Crypto (available as `globalThis.crypto.subtle`
24
+ * in both browsers and node >= 20, so this needs no node-only import and
25
+ * stays safe to bundle for the page). Every body-carrying `/api/*` request
26
+ * needs this as `x-amz-content-sha256`: Lambda URLs behind OAC 403 at the
27
+ * URL auth layer, before the handler ever runs, if a body-carrying request
28
+ * arrives without it. */
29
+ export async function sha256Hex(text) {
30
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
31
+ return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
32
+ }
33
+
22
34
  /** Reads whatever text the response carries, for the error's own message
23
35
  * only. The class decision uses the status code alone (the service's own
24
36
  * §3.8 mapping), so a consumer never has to parse a body to know whether a
@@ -94,7 +106,12 @@ export function createHttpRowBackend({ apiBase, sessionKey, fetchImpl = fetch }
94
106
  }
95
107
 
96
108
  async function mutate(path, { method, body }, { retryOn5xx = false } = {}) {
97
- const init = { method, headers: jsonHeaders, body: JSON.stringify(body) };
109
+ const serializedBody = JSON.stringify(body);
110
+ const init = {
111
+ method,
112
+ headers: { ...jsonHeaders, [PAYLOAD_HASH_HEADER]: await sha256Hex(serializedBody) },
113
+ body: serializedBody,
114
+ };
98
115
  const response = retryOn5xx
99
116
  ? await fetchWithOneRetryOn5xx(fetchImpl, `${base}${path}`, init, "could not reach the row service")
100
117
  : await runFetch(fetchImpl, `${base}${path}`, init, "could not reach the row service");
@@ -32,11 +32,12 @@
32
32
  // builds it fresh on every deploy, never committed, the same posture every
33
33
  // sibling *-browser-entry.mjs documents for its own output.
34
34
 
35
- import { createHttpRowBackend } from "./http-row-backend.mjs";
35
+ import { createHttpRowBackend, sha256Hex } from "./http-row-backend.mjs";
36
36
  import { publishTmctSurface } from "./tmct-surface.mjs";
37
37
  import { factSentence, findingCaveat } from "../../domain/fact-phrase.mjs";
38
38
 
39
39
  const SESSION_HEADER = "x-tmct-session";
40
+ const PAYLOAD_HASH_HEADER = "x-amz-content-sha256";
40
41
  const DEFAULT_CYCLE_POLL_MS = 400;
41
42
  const DEFAULT_CYCLE_WAIT_TIMEOUT_MS = 20_000;
42
43
 
@@ -296,10 +297,11 @@ export function createNewsSession({
296
297
 
297
298
  async function postTrigger(kind, body) {
298
299
  const key = ensureSessionKey();
300
+ const serializedBody = JSON.stringify(body || {});
299
301
  const response = await request(`/api/sessions/${key}/${kind}`, {
300
302
  method: "POST",
301
- headers: { "content-type": "application/json" },
302
- body: JSON.stringify(body || {}),
303
+ headers: { "content-type": "application/json", [PAYLOAD_HASH_HEADER]: await sha256Hex(serializedBody) },
304
+ body: serializedBody,
303
305
  });
304
306
  // A 409 means a cycle this session already had running is still going —
305
307
  // never this call's own fault. Falling through to the version-wait below
@@ -455,10 +457,11 @@ export function createNewsSession({
455
457
  const key = ensureSessionKey();
456
458
  const body = { text };
457
459
  if (fuzzy !== undefined) body.retrieval = { fuzzy: Boolean(fuzzy) };
460
+ const serializedBody = JSON.stringify(body);
458
461
  const response = await request(`/api/sessions/${key}/turn`, {
459
462
  method: "POST",
460
- headers: { "content-type": "application/json" },
461
- body: JSON.stringify(body),
463
+ headers: { "content-type": "application/json", [PAYLOAD_HASH_HEADER]: await sha256Hex(serializedBody) },
464
+ body: serializedBody,
462
465
  });
463
466
  if (response.status === 429) {
464
467
  throw new Error("this session has sent a lot of chat messages this hour — wait a bit and try again.");