@tea-agent/loop-agent 0.33.4 → 0.33.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.
@@ -6,7 +6,7 @@
6
6
  <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
7
7
  <link rel="stylesheet" href="/inspect/operator-chrome.css" />
8
8
  <title>Loop 操作台 · Operator Console</title>
9
- <script type="module" crossorigin src="/assets/index-PzYzcuFG.js"></script>
9
+ <script type="module" crossorigin src="/assets/index-CteJFFL2.js"></script>
10
10
  <link rel="stylesheet" crossorigin href="/assets/index-CnUXAqxG.css">
11
11
  </head>
12
12
  <body>
@@ -37,9 +37,7 @@ export function createChatSseEventApplier(deps) {
37
37
  interviewSessionId: typeof payload.interviewSessionId === "string"
38
38
  ? payload.interviewSessionId
39
39
  : current?.interviewSessionId,
40
- taskId: typeof payload.taskId === "string"
41
- ? payload.taskId
42
- : current?.taskId,
40
+ taskId: typeof payload.taskId === "string" ? payload.taskId : current?.taskId,
43
41
  state: typeof payload.state === "string" ? payload.state : current?.state,
44
42
  question: payload.question && typeof payload.question === "object"
45
43
  ? payload.question
@@ -50,9 +48,7 @@ export function createChatSseEventApplier(deps) {
50
48
  if (eventName === "draft") {
51
49
  setInterview((current) => ({
52
50
  ...(current ?? {}),
53
- taskId: typeof payload.taskId === "string"
54
- ? payload.taskId
55
- : current?.taskId,
51
+ taskId: typeof payload.taskId === "string" ? payload.taskId : current?.taskId,
56
52
  draft: payload.draft && typeof payload.draft === "object"
57
53
  ? payload.draft
58
54
  : current?.draft,
@@ -95,15 +91,44 @@ export function createChatSseEventApplier(deps) {
95
91
  return;
96
92
  }
97
93
  if (eventName === "agent_start") {
94
+ // P2: a replayed agent_start must not resurrect an already-finished
95
+ // turn (aborted turns have no agent_settled in the ring, so replay
96
+ // would otherwise leave the UI streaming forever). Only the server's
97
+ // active turn may flip streaming on during replay; every other
98
+ // agent_start (including all of them when the server reports no
99
+ // active turn) is stale and ignored.
100
+ if (replay &&
101
+ turnId !== undefined &&
102
+ turnId !== refs.activeTurnIdRef.current) {
103
+ return;
104
+ }
105
+ if (turnId)
106
+ refs.activeTurnIdRef.current = turnId;
98
107
  setStreaming(true);
99
108
  setStreamingAssistantId(resolvedAssistantId);
100
109
  return;
101
110
  }
102
- if (eventName === "agent_settled") {
111
+ if (eventName === "agent_settled" || eventName === "agent_end") {
112
+ // Both are terminal: agent_end = SDK run finished, agent_settled =
113
+ // post-turn continuation idle. Clear streaming and drop an empty
114
+ // placeholder bubble so a settled turn never shows a stuck "…" row.
103
115
  setStreaming(false);
104
116
  setStreamingAssistantId(null);
105
- if (!replay && document.hidden)
117
+ if (eventName === "agent_settled" &&
118
+ !replay &&
119
+ typeof document !== "undefined" &&
120
+ document.hidden) {
106
121
  setUnread((current) => current + 1);
122
+ }
123
+ setMessages((m) => m.filter((msg) => !(msg.id === resolvedAssistantId &&
124
+ msg.role === "assistant" &&
125
+ !msg.text.trim())));
126
+ // Close out tool calls that never got a tool_result (abort/fail):
127
+ // without this the process rail keeps showing "运行中" forever even
128
+ // though the turn is over.
129
+ setToolCalls((prev) => prev.map((t) => t.status === "running" && t.assistantMessageId === resolvedAssistantId
130
+ ? { ...t, status: "error", finishedAt: Date.now() }
131
+ : t));
107
132
  return;
108
133
  }
109
134
  if (eventName === "operation-ref" || eventName === "operation-status") {
@@ -10,6 +10,7 @@ export function useChatRefs() {
10
10
  const seenEventIdsRef = useRef(new Set());
11
11
  const turnAssistantIdsRef = useRef(new Map());
12
12
  const reconnectAbortRef = useRef(null);
13
+ const activeTurnIdRef = useRef(null);
13
14
  const usageResponseKeysRef = useRef(new Set());
14
15
  const isComposingRef = useRef(false);
15
16
  const compositionEndedAtRef = useRef(0);
@@ -26,6 +27,7 @@ export function useChatRefs() {
26
27
  seenEventIdsRef,
27
28
  turnAssistantIdsRef,
28
29
  reconnectAbortRef,
30
+ activeTurnIdRef,
29
31
  usageResponseKeysRef,
30
32
  isComposingRef,
31
33
  compositionEndedAtRef,
@@ -40,6 +42,7 @@ export function useChatRefs() {
40
42
  seenEventIdsRef,
41
43
  turnAssistantIdsRef,
42
44
  reconnectAbortRef,
45
+ activeTurnIdRef,
43
46
  usageResponseKeysRef,
44
47
  isComposingRef,
45
48
  compositionEndedAtRef,
@@ -23,6 +23,7 @@ export function useChatSessions(params) {
23
23
  refs.lastEventIdRef.current = null;
24
24
  refs.seenEventIdsRef.current = new Set();
25
25
  refs.turnAssistantIdsRef.current = new Map();
26
+ refs.activeTurnIdRef.current = null;
26
27
  resetForNewSession();
27
28
  setInput("");
28
29
  setPendingImages([]);
@@ -125,6 +126,7 @@ export function useChatSessions(params) {
125
126
  };
126
127
  refs.sessionRef.current = next;
127
128
  refs.streamingRef.current = Boolean(body.session.activeTurnId);
129
+ refs.activeTurnIdRef.current = body.session.activeTurnId ?? null;
128
130
  refs.seenEventIdsRef.current = new Set();
129
131
  refs.turnAssistantIdsRef.current = new Map();
130
132
  refs.lastEventIdRef.current = null;
@@ -192,6 +194,7 @@ export function useChatSessions(params) {
192
194
  // from the durable store, but tool/gate/operation projections live
193
195
  // in the event ring and must replay once. applySseBlock dedups by
194
196
  // eventId and marks replay so usage/unread/sound do not double-count.
197
+ refs.activeTurnIdRef.current = body.session.activeTurnId ?? null;
195
198
  refs.seenEventIdsRef.current = new Set();
196
199
  refs.turnAssistantIdsRef.current = new Map();
197
200
  refs.lastEventIdRef.current = null;
@@ -123,7 +123,84 @@ export function useChatStream(params) {
123
123
  refs.streamingRef.current = false;
124
124
  setStreaming(false);
125
125
  setStreamingAssistantId(null);
126
- }, [refs, setStreaming, setStreamingAssistantId]);
126
+ // User-initiated stop: immediately drop the still-empty placeholder
127
+ // bubble (no text yet) instead of leaving a forever "…" row (pi-web
128
+ // hides empty assistant rows on abort).
129
+ setMessages((m) => m.filter((msg) => !(msg.id === refs.assistantIdRef.current &&
130
+ msg.role === "assistant" &&
131
+ !msg.text.trim())));
132
+ }, [refs, setStreaming, setStreamingAssistantId, setMessages]);
133
+ // Reconcile client streaming state with the server (pi-web get_state loop).
134
+ // When SSE events are missed (network drop, backgrounded tab, half-open
135
+ // connection) agent_end/agent_settled never arrive and the UI stays
136
+ // streaming forever. If the server reports no active turn while we still
137
+ // think a turn is streaming, force the same finish a terminal event would
138
+ // have produced. Also refreshes activeTurnIdRef so replayed agent_start
139
+ // frames are judged against current server truth.
140
+ useEffect(() => {
141
+ if (!session || !streaming)
142
+ return;
143
+ const sid = session.sessionId;
144
+ let cancelled = false;
145
+ const sync = async () => {
146
+ if (cancelled)
147
+ return;
148
+ try {
149
+ const res = await fetch(`${origin}/api/operator/v1/chat/sessions/${encodeURIComponent(sid)}/state`, { credentials: "include" });
150
+ if (!res.ok)
151
+ return;
152
+ const body = (await res.json());
153
+ const activeTurnId = body.state?.activeTurnId ?? null;
154
+ refs.activeTurnIdRef.current = activeTurnId;
155
+ if (activeTurnId !== null || !refs.streamingRef.current)
156
+ return;
157
+ // Server idle while the client still thinks a turn is streaming.
158
+ // Skip when an inline prompt request is still in flight — the
159
+ // server may not have created its turn lease yet (POST prompt
160
+ // creates the turn server-side after the request lands), and
161
+ // force-finishing then would kill a just-sent prompt.
162
+ if (refs.inlineStreamSessionIdsRef.current.has(sid))
163
+ return;
164
+ const pendingAssistantId = refs.assistantIdRef.current;
165
+ refs.streamingRef.current = false;
166
+ setStreaming(false);
167
+ setStreamingAssistantId(null);
168
+ refs.abortRef.current?.abort();
169
+ refs.abortRef.current = null;
170
+ refs.assistantIdRef.current = null;
171
+ setMessages((m) => m.filter((msg) => !(msg.id === pendingAssistantId &&
172
+ msg.role === "assistant" &&
173
+ !msg.text.trim())));
174
+ }
175
+ catch {
176
+ // Network still down — the next poll / visibility / online tick retries.
177
+ }
178
+ };
179
+ const onVisible = () => {
180
+ if (document.visibilityState === "visible")
181
+ void sync();
182
+ };
183
+ const onOnline = () => {
184
+ void sync();
185
+ };
186
+ const interval = setInterval(() => void sync(), 15_000);
187
+ document.addEventListener("visibilitychange", onVisible);
188
+ window.addEventListener("online", onOnline);
189
+ return () => {
190
+ cancelled = true;
191
+ clearInterval(interval);
192
+ document.removeEventListener("visibilitychange", onVisible);
193
+ window.removeEventListener("online", onOnline);
194
+ };
195
+ }, [
196
+ origin,
197
+ refs,
198
+ session,
199
+ streaming,
200
+ setStreaming,
201
+ setStreamingAssistantId,
202
+ setMessages,
203
+ ]);
127
204
  /** Open the reconnectable GET /events stream for a session and apply events
128
205
  * until it closes, then reopen with the latest Last-Event-ID (M1-T04/T05).
129
206
  *
@@ -194,7 +271,8 @@ export function useChatStream(params) {
194
271
  // leak a dangling connection. It resumes after settle/stop to catch the tail.
195
272
  useEffect(() => {
196
273
  if (!session ||
197
- (streaming && refs.inlineStreamSessionIdsRef.current.has(session.sessionId)))
274
+ (streaming &&
275
+ refs.inlineStreamSessionIdsRef.current.has(session.sessionId)))
198
276
  return;
199
277
  const ac = new AbortController();
200
278
  refs.reconnectAbortRef.current = ac;
@@ -5,6 +5,7 @@ import { access, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
5
5
  import { createWriteStream } from "node:fs";
6
6
  import path from "node:path";
7
7
  import { promisify } from "node:util";
8
+ import { isPriorityOnlyBackendTestModuleStem } from "./backend-test-module-stem.js";
8
9
  import { parseJacocoXml, } from "./backend-test-coverage-contract.js";
9
10
  import { backendTestCaseManifestSchema, computeCaseManifestCoverageSummary, } from "./backend-test-case-manifest.js";
10
11
  const execFileAsync = promisify(execFile);
@@ -398,9 +399,13 @@ function markdownSecretFindings(relativeFile, markdown) {
398
399
  export function hasBlockingBackendMarkdownSafetyFindings(report) {
399
400
  return /^## Safety Status\s*\r?\n\s*BLOCKED\s*$/m.test(report);
400
401
  }
402
+ export function hasBlockingBackendMarkdownModuleStemFindings(report) {
403
+ return /^## Module Stem Status\s*\r?\n\s*BLOCKED\s*$/m.test(report);
404
+ }
401
405
  export async function validateBackendMarkdownCases(input) {
402
406
  const findings = [];
403
407
  const safetyFindings = [];
408
+ const moduleStemFindings = [];
404
409
  let files = [];
405
410
  try {
406
411
  files = await markdownFiles(input.workspaceRoot);
@@ -419,6 +424,9 @@ export async function validateBackendMarkdownCases(input) {
419
424
  let caseCount = 0;
420
425
  for (const file of moduleFiles) {
421
426
  const relativeFile = path.relative(input.workspaceRoot, file).replaceAll(path.sep, "/");
427
+ if (isPriorityOnlyBackendTestModuleStem(path.basename(file, path.extname(file)))) {
428
+ moduleStemFindings.push(`priority-only-module-stem: ${relativeFile}; regenerate the README Module Index with a stable business resource/domain stem`);
429
+ }
422
430
  const markdown = await readFile(file, "utf8");
423
431
  safetyFindings.push(...markdownSecretFindings(relativeFile, markdown));
424
432
  if (!markdown.trim()) {
@@ -479,6 +487,7 @@ export async function validateBackendMarkdownCases(input) {
479
487
  if (missing.length > 0) {
480
488
  findings.push(`required AC IDs are not covered by final Markdown cases: ${missing.join(", ")}`);
481
489
  }
490
+ findings.push(...moduleStemFindings);
482
491
  const status = findings.length === 0 ? "PASS" : "FAIL";
483
492
  const report = [
484
493
  "# Backend Markdown Case Validation",
@@ -497,6 +506,14 @@ export async function validateBackendMarkdownCases(input) {
497
506
  "- Source-reference validity check: not applied",
498
507
  "- Markdown quality sensitive-information finding: not advisory; see Safety Status",
499
508
  "",
509
+ "## Module Stem Status",
510
+ "",
511
+ moduleStemFindings.length > 0 ? "BLOCKED" : "PASS",
512
+ "",
513
+ ...(moduleStemFindings.length > 0
514
+ ? moduleStemFindings.map((finding) => `- ${finding}`)
515
+ : ["- All Markdown modules use non-priority business stems."]),
516
+ "",
500
517
  "## Safety Status",
501
518
  "",
502
519
  safetyFindings.length > 0 ? "BLOCKED" : "PASS",
@@ -0,0 +1,21 @@
1
+ import path from "node:path";
2
+ const PRIORITY_ONLY_MODULE_STEMS = new Set(["p0", "p1", "p2"]);
3
+ export function normalizeBackendTestModuleStemCandidate(raw) {
4
+ return path.basename(raw)
5
+ .replace(/\.(?:md|py)$/i, "")
6
+ .toLowerCase()
7
+ .replace(/[^a-z0-9]+/g, "_")
8
+ .replace(/^_+|_+$/g, "")
9
+ .replace(/_+/g, "_");
10
+ }
11
+ export function isPriorityOnlyBackendTestModuleStem(raw) {
12
+ return PRIORITY_ONLY_MODULE_STEMS.has(normalizeBackendTestModuleStemCandidate(raw));
13
+ }
14
+ export function isPriorityOnlyBackendPytestScript(script) {
15
+ const basename = path.posix.basename(script.replaceAll("\\", "/"));
16
+ const match = /^test_(.+)\.py$/i.exec(basename);
17
+ return match ? isPriorityOnlyBackendTestModuleStem(match[1]) : false;
18
+ }
19
+ export function priorityOnlyBackendPytestScripts(paths) {
20
+ return [...new Set(paths.map((item) => item.replaceAll("\\", "/")).filter(isPriorityOnlyBackendPytestScript))].sort();
21
+ }
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
2
2
  import { readdir, readFile, writeFile, mkdir } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import { z } from "zod";
5
+ import { priorityOnlyBackendPytestScripts } from "./backend-test-module-stem.js";
5
6
  const SHA256 = /^[a-f0-9]{64}$/;
6
7
  const MAX_DIAGNOSTIC_CHARS = 12_000;
7
8
  export const backendPytestCollectionFindingSchema = z.object({
@@ -273,6 +274,41 @@ export function assessBackendPytestCollection(input) {
273
274
  fixtureStderrExcerpt: "",
274
275
  });
275
276
  }
277
+ export function assessPriorityOnlyBackendPytestModules(input) {
278
+ const invalidScripts = priorityOnlyBackendPytestScripts(input.inventory.assetFiles);
279
+ if (invalidScripts.length === 0)
280
+ return undefined;
281
+ return backendPytestCollectionFactsSchema.parse({
282
+ schemaId: "backend-test-pytest-collection-v3",
283
+ phase: input.phase,
284
+ status: "BLOCKED",
285
+ repairEligible: false,
286
+ repairAttempt: input.phase === "final" ? 1 : 0,
287
+ collectionAttempted: false,
288
+ fixtureResolutionAttempted: false,
289
+ fixtureResolutionStatus: "NOT_RUN",
290
+ fixtureResolutionExitCode: null,
291
+ repairPaths: [],
292
+ mappedScripts: [...input.mappedScripts],
293
+ existingMappedScripts: input.inventory.mappedScripts,
294
+ missingMappedScripts: [],
295
+ assetFiles: input.inventory.assetFiles,
296
+ inputHashes: input.inventory.inputHashes,
297
+ pytestExitCode: null,
298
+ collectedItemCount: 0,
299
+ collectedItemIds: [],
300
+ findings: invalidScripts.map((script) => ({
301
+ kind: "priority-only-pytest-module",
302
+ classification: "test-asset-defect",
303
+ repairability: "blocked",
304
+ detail: `priority-only pytest module is forbidden and cannot be auto-renamed: ${script}`,
305
+ })),
306
+ stdoutExcerpt: "",
307
+ stderrExcerpt: "",
308
+ fixtureStdoutExcerpt: "",
309
+ fixtureStderrExcerpt: "",
310
+ });
311
+ }
276
312
  export function assessMissingBackendPytestScripts(input) {
277
313
  return backendPytestCollectionFactsSchema.parse({
278
314
  schemaId: "backend-test-pytest-collection-v3",
@@ -408,6 +444,10 @@ export async function materializeBackendTestExecutionReadiness(input) {
408
444
  throw new Error("backend-test execution readiness requires effective collection and fixture-resolution PASS");
409
445
  }
410
446
  const current = await buildBackendPytestAssetInventory(input.workspaceRoot, input.effective.mappedScripts);
447
+ const priorityOnly = priorityOnlyBackendPytestScripts(current.assetFiles);
448
+ if (priorityOnly.length > 0) {
449
+ throw new Error(`priority-only-pytest-module: ${priorityOnly.join(", ")}`);
450
+ }
411
451
  assertSameInventory(input.effective, current);
412
452
  const status = input.scenarioParamStatus === "FAIL"
413
453
  ? "BLOCKED"
@@ -442,6 +482,10 @@ export async function assertBackendTestExecutionReadinessFresh(workspaceRoot, re
442
482
  throw new Error(`backend pytest execution readiness is ${readiness.status}`);
443
483
  }
444
484
  const current = await buildBackendPytestAssetInventory(workspaceRoot, readiness.mappedScripts);
485
+ const priorityOnly = priorityOnlyBackendPytestScripts(current.assetFiles);
486
+ if (priorityOnly.length > 0) {
487
+ throw new Error(`priority-only-pytest-module: ${priorityOnly.join(", ")}`);
488
+ }
445
489
  assertSameInventory({ mappedScripts: readiness.mappedScripts, assetFiles: Object.keys(readiness.assetHashes), inputHashes: readiness.assetHashes }, current);
446
490
  }
447
491
  export async function assertBackendPytestCollectionFresh(workspaceRoot, effective) {
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
2
2
  import { access, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import { z } from "zod";
5
+ import { isPriorityOnlyBackendTestModuleStem } from "./backend-test-module-stem.js";
5
6
  /**
6
7
  * Maps a backend-test generation writer task id to its writer-progress role.
7
8
  * Returns undefined for nodes that are not completeness-gated generators.
@@ -96,31 +97,26 @@ function hasMarkdownTable(section, headerNeedle) {
96
97
  * phantom missing modules when the model discusses forbidden filenames
97
98
  * ("do not create 1.md / 9.md") inside the README body.
98
99
  */
99
- function looksLikeValidModuleStem(raw) {
100
+ function moduleStemInvalidReason(raw) {
100
101
  if (!raw)
101
- return false;
102
+ return "invalid-syntax";
102
103
  const stem = normalizeBackendTestModuleStem(raw);
104
+ if (isPriorityOnlyBackendTestModuleStem(stem))
105
+ return "priority-only-module-stem";
103
106
  if (stem.toLowerCase() === "readme")
104
- return false;
107
+ return "reserved-module-stem";
105
108
  if (!/^[a-z][a-z0-9_]*$/.test(stem))
106
- return false;
109
+ return "invalid-syntax";
107
110
  if (/^(?:be|tp|ac|req|br)[_-]/i.test(stem))
108
- return false;
109
- return true;
111
+ return "case-like-module-stem";
112
+ return undefined;
110
113
  }
111
- /**
112
- * Exported for reuse by the manifest-shell node that materializes the
113
- * module list JSON consumed by the map_agent expansion. Kept deterministic
114
- * (same extractor as the completeness gate) so the shard set always matches
115
- * the README index the gate trusts.
116
- */
117
- export function extractModuleStemsFromReadme(readme) {
118
- const stems = [];
119
- // Only trust testcase/md/<stem>.md mentions that appear inside markdown
120
- // table rows (`| ... testcase/md/x.md ... |`) or as canonical relative
121
- // links (`[label](./x.md)`). Free-form prose mentions such as a recovery
122
- // note listing `testcase/md/1.md` must NOT be treated as authoritative
123
- // module references, otherwise the gate invents phantom missing modules.
114
+ function looksLikeValidModuleStem(raw) {
115
+ return moduleStemInvalidReason(raw) === undefined;
116
+ }
117
+ /** Inspect only authoritative README module-index candidates. */
118
+ export function inspectModuleStemsFromReadme(readme) {
119
+ const candidates = [];
124
120
  const tableRowLines = readme
125
121
  .replaceAll("\r\n", "\n")
126
122
  .replaceAll("\r", "\n")
@@ -128,19 +124,42 @@ export function extractModuleStemsFromReadme(readme) {
128
124
  .filter((line) => line.includes("|"));
129
125
  for (const line of tableRowLines) {
130
126
  for (const match of line.matchAll(/`?testcase\/md\/([A-Za-z0-9_.-]+)\.md`?/g)) {
131
- if (looksLikeValidModuleStem(match[1]))
132
- stems.push(match[1]);
127
+ candidates.push(match[1]);
133
128
  }
134
129
  for (const match of line.matchAll(/\|\s*`?([A-Za-z0-9_.-]+)`?\s*\|\s*`?testcase\/test_/g)) {
135
- if (looksLikeValidModuleStem(match[1]))
136
- stems.push(match[1]);
130
+ // This compatibility shape can also capture a numeric count column.
131
+ // Keep valid stems and the priority-only defect we diagnose, while
132
+ // ignoring non-stem incidental cells such as `1`.
133
+ if (looksLikeValidModuleStem(match[1]) || isPriorityOnlyBackendTestModuleStem(match[1])) {
134
+ candidates.push(match[1]);
135
+ }
137
136
  }
138
137
  }
139
138
  for (const match of readme.matchAll(/\[[^\]]+\]\(\.\/([A-Za-z0-9_.-]+)\.md\)/g)) {
140
- if (looksLikeValidModuleStem(match[1]))
141
- stems.push(match[1]);
139
+ candidates.push(match[1]);
142
140
  }
143
- return orderedUnique(stems.map((stem) => normalizeBackendTestModuleStem(stem)));
141
+ const uniqueCandidates = orderedUnique(candidates);
142
+ const invalid = uniqueCandidates.flatMap((raw) => {
143
+ const reasonCode = moduleStemInvalidReason(raw);
144
+ return reasonCode
145
+ ? [{ raw, normalized: normalizeBackendTestModuleStem(raw), reasonCode }]
146
+ : [];
147
+ });
148
+ return {
149
+ candidates: uniqueCandidates,
150
+ validStems: orderedUnique(uniqueCandidates
151
+ .filter(looksLikeValidModuleStem)
152
+ .map((stem) => normalizeBackendTestModuleStem(stem))),
153
+ invalid,
154
+ };
155
+ }
156
+ /**
157
+ * Exported for reuse by callers that only need the trusted shard set.
158
+ * Completeness gates must use inspectModuleStemsFromReadme so invalid
159
+ * authoritative candidates cannot be silently dropped.
160
+ */
161
+ export function extractModuleStemsFromReadme(readme) {
162
+ return inspectModuleStemsFromReadme(readme).validStems;
144
163
  }
145
164
  async function listMarkdownModules(workspaceRoot) {
146
165
  const root = path.join(workspaceRoot, "testcase", "md");
@@ -242,12 +261,80 @@ function moduleStructuralDetail(result) {
242
261
  return "module file is structurally complete";
243
262
  return `module file is empty or has structural issues: ${result.reasons.join("; ")}`;
244
263
  }
264
+ function pythonStructureOutsideStrings(source) {
265
+ const input = source.replace(/\r\n/g, "\n");
266
+ let output = "";
267
+ let index = 0;
268
+ let quote;
269
+ let triple = false;
270
+ let comment = false;
271
+ while (index < input.length) {
272
+ const char = input[index];
273
+ if (comment) {
274
+ if (char === "\n") {
275
+ comment = false;
276
+ output += "\n";
277
+ }
278
+ else
279
+ output += " ";
280
+ index += 1;
281
+ continue;
282
+ }
283
+ if (quote) {
284
+ if (char === "\\") {
285
+ output += " ";
286
+ if (index + 1 < input.length)
287
+ output += input[index + 1] === "\n" ? "\n" : " ";
288
+ index += 2;
289
+ continue;
290
+ }
291
+ if (triple && input.slice(index, index + 3) === quote.repeat(3)) {
292
+ output += " ";
293
+ index += 3;
294
+ quote = undefined;
295
+ triple = false;
296
+ continue;
297
+ }
298
+ if (!triple && char === quote) {
299
+ output += " ";
300
+ index += 1;
301
+ quote = undefined;
302
+ continue;
303
+ }
304
+ if (!triple && char === "\n")
305
+ return { text: output, stringsClosed: false };
306
+ output += char === "\n" ? "\n" : " ";
307
+ index += 1;
308
+ continue;
309
+ }
310
+ if (char === "#") {
311
+ comment = true;
312
+ output += " ";
313
+ index += 1;
314
+ continue;
315
+ }
316
+ if (char === "'" || char === '"') {
317
+ quote = char;
318
+ triple = input.slice(index, index + 3) === char.repeat(3);
319
+ output += triple ? " " : " ";
320
+ index += triple ? 3 : 1;
321
+ continue;
322
+ }
323
+ output += char;
324
+ index += 1;
325
+ }
326
+ return { text: output, stringsClosed: quote === undefined };
327
+ }
245
328
  function pythonParseable(source) {
246
- const text = source.replace(/\r\n/g, "\n");
247
- if (!text.trim())
329
+ const normalized = source.replace(/\r\n/g, "\n");
330
+ if (!normalized.trim())
248
331
  return false;
249
- if (/^\s*(?:def|class|import|from)\b/m.test(text) === false)
332
+ if (/^\s*(?:def|class|import|from)\b/m.test(normalized) === false)
333
+ return false;
334
+ const structural = pythonStructureOutsideStrings(normalized);
335
+ if (!structural.stringsClosed)
250
336
  return false;
337
+ const text = structural.text;
251
338
  const openParens = (text.match(/\(/g) ?? []).length;
252
339
  const closeParens = (text.match(/\)/g) ?? []).length;
253
340
  const openBrackets = (text.match(/\[/g) ?? []).length;
@@ -260,15 +347,6 @@ function pythonParseable(source) {
260
347
  return false;
261
348
  if (openBraces !== closeBraces)
262
349
  return false;
263
- if (/("""|''')[\s\S]*$/.test(text)) {
264
- const triples = text.match(/("""|''')/g) ?? [];
265
- if (triples.length % 2 !== 0)
266
- return false;
267
- }
268
- // The balanced-bracket counts above already catch a genuinely truncated
269
- // file (an unclosed def/call leaves unbalanced parens). The previous
270
- // per-line "def ... ( ... $" heuristic was a false-positive source for
271
- // legal multi-line definitions such as `def f(\n x,\n):` — removed.
272
350
  return true;
273
351
  }
274
352
  /**
@@ -318,12 +396,7 @@ function looksLikeValidPythonModule(source) {
318
396
  return false;
319
397
  if (/^\s*(?:def|class|import|from)\b/m.test(text) === false)
320
398
  return false;
321
- if (/("""|''')[\s\S]*$/.test(text)) {
322
- const triples = text.match(/("""|''')/g) ?? [];
323
- if (triples.length % 2 !== 0)
324
- return false;
325
- }
326
- return true;
399
+ return pythonStructureOutsideStrings(text).stringsClosed;
327
400
  }
328
401
  export function buildOutputLimitRecoveryPrompt(input) {
329
402
  return buildOutputLimitRecoverySection(input);
@@ -842,7 +915,19 @@ export async function assessBackendTestMdPlanCompleteness(workspaceRoot) {
842
915
  // The README must advertise a non-empty module index so the downstream
843
916
  // manifest shell has stems to shard; the indexed module files themselves
844
917
  // are produced by map_agent children and are NOT required here.
845
- const indexedStems = extractModuleStemsFromReadme(readme);
918
+ const stemInspection = inspectModuleStemsFromReadme(readme);
919
+ const indexedStems = stemInspection.validStems;
920
+ for (const invalid of stemInspection.invalid) {
921
+ if (!brokenPaths.includes("testcase/md/README.md")) {
922
+ brokenPaths.push("testcase/md/README.md");
923
+ }
924
+ issues.push({
925
+ code: "T5",
926
+ path: "testcase/md/README.md",
927
+ detail: `${invalid.reasonCode}: ${invalid.normalized}; use a stable lowercase business resource/domain stem`,
928
+ recoverable: true,
929
+ });
930
+ }
846
931
  if (indexedStems.length === 0) {
847
932
  if (!brokenPaths.includes("testcase/md/README.md")) {
848
933
  brokenPaths.push("testcase/md/README.md");