Skip to content

fix(history): 继承失败原因源批次支持远程子串搜索 - #15

Merged
jacksontwu merged 1 commit into
openBitFun:masterfrom
azi44eo:ztx0729
Jul 29, 2026
Merged

fix(history): 继承失败原因源批次支持远程子串搜索#15
jacksontwu merged 1 commit into
openBitFun:masterfrom
azi44eo:ztx0729

Conversation

@azi44eo

@azi44eo azi44eo commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

默认仍返回最近 100 批;输入关键词后按 start_time 全库检索,避免更早轮次搜不到。

Summary by CodeRabbit

  • New Features
    • Added remote search for source batches in the “Inherit Failure Reason” dialog.
    • Search results update as you type and support substring matching across available batches.
    • The batch list defaults to the 100 most recent eligible options and excludes the current batch.
  • Documentation
    • Updated the batch option API specification, including search and filtering behavior.

默认仍返回最近 100 批;输入关键词后按 start_time 全库检索,避免更早轮次搜不到。

Co-authored-by: Cursor <cursoragent@cursor.com>
@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The inherit-batch options flow now supports an optional q search parameter. The backend safely filters batches by substring, the frontend performs debounced remote searching, and the specification documents the updated endpoint behavior.

Changes

Remote batch option search

Layer / File(s) Summary
Backend search contract and filtering
backend/api/v1/history.py, backend/services/inherit_failure_reason_service.py
The endpoint accepts q, and the service applies escaped substring matching with a maximum result limit while preserving batch exclusion and ordering rules.
Frontend remote search integration
frontend/src/services/index.ts, frontend/src/pages/history/HistoryPage.tsx
The API client forwards trimmed search text, while the history page debounces searches, loads options remotely, and manages dropdown and modal lifecycle behavior.
Batch option behavior specification
spec/10_inherit_failure_reason_spec.md
The specification documents the endpoint, filtering rules, default results, ordering, exclusion, prefix constraint, and 100-item limit.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant HistoryPage
  participant historyApi
  participant historyEndpoint
  participant get_inherit_batch_options

  HistoryPage->>HistoryPage: Debounce source-batch input
  HistoryPage->>historyApi: Request options with currentBatch and q
  historyApi->>historyEndpoint: GET /history/inherit-batch-options
  historyEndpoint->>get_inherit_batch_options: Apply exclusion and search
  get_inherit_batch_options-->>historyEndpoint: Return batch options
  historyEndpoint-->>historyApi: Return response
  historyApi-->>HistoryPage: Update source-batch dropdown
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了本次改动的核心:历史记录中继承失败原因的源批次支持远程子串搜索。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
backend/services/inherit_failure_reason_service.py (1)

75-98: 🚀 Performance & Scalability | 🔵 Trivial

Validate the cost of full-library substring search.

The pattern begins with %, so a normal B-tree index on start_time cannot efficiently support this lookup. Combined with debounced frontend requests, short queries may repeatedly scan and sort a large history table. Run EXPLAIN on production-sized data and consider a minimum query length or an appropriate substring-search index if needed.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/services/inherit_failure_reason_service.py` around lines 75 - 98,
Validate the q-filter path in get_inherit_batch_options with EXPLAIN against
production-sized data, focusing on the leading-wildcard start_time substring
search and its scan/sort cost. Based on the measured impact, either enforce an
appropriate minimum q length before executing the search or add/use a suitable
substring-search index, while preserving the existing filtering, ordering,
distinctness, and result limit.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@frontend/src/pages/history/HistoryPage.tsx`:
- Around line 318-319: Update the inherit-batch search flow around
inheritBatchSearchTimerRef to clear any pending timer during component unmount
cleanup and whenever the modal closes after successful confirmation, not only in
the cancel handler. Preserve the existing debounce behavior while preventing
callbacks from issuing requests or updating state after closure or unmount.
- Around line 960-983: Update fetchInheritBatchOptions and
handleInheritBatchSearch to track a request generation (or abort the previous
request) so stale remote responses cannot update inheritBatchOptions or loading
state after a newer search begins. Guard both successful and error/finally state
updates, while preserving the existing debounce and error-display behavior for
the latest request.

---

Nitpick comments:
In `@backend/services/inherit_failure_reason_service.py`:
- Around line 75-98: Validate the q-filter path in get_inherit_batch_options
with EXPLAIN against production-sized data, focusing on the leading-wildcard
start_time substring search and its scan/sort cost. Based on the measured
impact, either enforce an appropriate minimum q length before executing the
search or add/use a suitable substring-search index, while preserving the
existing filtering, ordering, distinctness, and result limit.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 61e314b3-b8da-49a1-b0d4-58a3302bc0ce

📥 Commits

Reviewing files that changed from the base of the PR and between 488d1ac and 83f3b16.

📒 Files selected for processing (5)
  • backend/api/v1/history.py
  • backend/services/inherit_failure_reason_service.py
  • frontend/src/pages/history/HistoryPage.tsx
  • frontend/src/services/index.ts
  • spec/10_inherit_failure_reason_spec.md

Comment on lines +318 to +319
/** 源批次远程搜索防抖 */
const inheritBatchSearchTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Clean up pending search timers on every close path.

The timer is cleared only by the cancel handler. If the user confirms or navigates away before 300 ms, the callback still performs a request and may update state for a closed or unmounted page. Add unmount cleanup and clear the timer when the modal closes successfully as well.

Also applies to: 976-983

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/src/pages/history/HistoryPage.tsx` around lines 318 - 319, Update
the inherit-batch search flow around inheritBatchSearchTimerRef to clear any
pending timer during component unmount cleanup and whenever the modal closes
after successful confirmation, not only in the cancel handler. Preserve the
existing debounce behavior while preventing callbacks from issuing requests or
updating state after closure or unmount.

Comment on lines +960 to +983
const fetchInheritBatchOptions = async (q?: string, showError = false) => {
setInheritBatchOptionsLoading(true);
try {
const res = await historyApi.inheritBatchOptions(currentBatch, q);
setInheritBatchOptions(res.batches ?? []);
} catch (e: unknown) {
setInheritBatchOptions([]);
if (showError) {
const err = e as { response?: { data?: { detail?: string } }; message?: string };
message.error(err?.response?.data?.detail || err?.message || "获取批次选项失败");
}
} finally {
setInheritBatchOptionsLoading(false);
}
};

const handleInheritBatchSearch = (input: string) => {
if (inheritBatchSearchTimerRef.current) {
clearTimeout(inheritBatchSearchTimerRef.current);
}
inheritBatchSearchTimerRef.current = setTimeout(() => {
const trimmed = input.trim();
void fetchInheritBatchOptions(trimmed || undefined);
}, 300);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Ignore stale remote-search responses.

Debouncing does not cancel requests already in flight. If an older query resolves after a newer query, its results overwrite the current options; with filterOption={false}, users can see or select batches unrelated to the current search.

Track a request generation or abort the previous request, guarding both options and loading-state updates.

Proposed request-generation guard
+  const inheritBatchRequestIdRef = useRef(0);
+
   const fetchInheritBatchOptions = async (q?: string, showError = false) => {
+    const requestId = ++inheritBatchRequestIdRef.current;
     setInheritBatchOptionsLoading(true);
     try {
       const res = await historyApi.inheritBatchOptions(currentBatch, q);
+      if (requestId !== inheritBatchRequestIdRef.current) return;
       setInheritBatchOptions(res.batches ?? []);
     } catch (e: unknown) {
+      if (requestId !== inheritBatchRequestIdRef.current) return;
       setInheritBatchOptions([]);
       // existing error handling
     } finally {
-      setInheritBatchOptionsLoading(false);
+      if (requestId === inheritBatchRequestIdRef.current) {
+        setInheritBatchOptionsLoading(false);
+      }
     }
   };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const fetchInheritBatchOptions = async (q?: string, showError = false) => {
setInheritBatchOptionsLoading(true);
try {
const res = await historyApi.inheritBatchOptions(currentBatch, q);
setInheritBatchOptions(res.batches ?? []);
} catch (e: unknown) {
setInheritBatchOptions([]);
if (showError) {
const err = e as { response?: { data?: { detail?: string } }; message?: string };
message.error(err?.response?.data?.detail || err?.message || "获取批次选项失败");
}
} finally {
setInheritBatchOptionsLoading(false);
}
};
const handleInheritBatchSearch = (input: string) => {
if (inheritBatchSearchTimerRef.current) {
clearTimeout(inheritBatchSearchTimerRef.current);
}
inheritBatchSearchTimerRef.current = setTimeout(() => {
const trimmed = input.trim();
void fetchInheritBatchOptions(trimmed || undefined);
}, 300);
const inheritBatchRequestIdRef = useRef(0);
const fetchInheritBatchOptions = async (q?: string, showError = false) => {
const requestId = ++inheritBatchRequestIdRef.current;
setInheritBatchOptionsLoading(true);
try {
const res = await historyApi.inheritBatchOptions(currentBatch, q);
if (requestId !== inheritBatchRequestIdRef.current) return;
setInheritBatchOptions(res.batches ?? []);
} catch (e: unknown) {
if (requestId !== inheritBatchRequestIdRef.current) return;
setInheritBatchOptions([]);
if (showError) {
const err = e as { response?: { data?: { detail?: string } }; message?: string };
message.error(err?.response?.data?.detail || err?.message || "获取批次选项失败");
}
} finally {
if (requestId === inheritBatchRequestIdRef.current) {
setInheritBatchOptionsLoading(false);
}
}
};
const handleInheritBatchSearch = (input: string) => {
if (inheritBatchSearchTimerRef.current) {
clearTimeout(inheritBatchSearchTimerRef.current);
}
inheritBatchSearchTimerRef.current = setTimeout(() => {
const trimmed = input.trim();
void fetchInheritBatchOptions(trimmed || undefined);
}, 300);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/src/pages/history/HistoryPage.tsx` around lines 960 - 983, Update
fetchInheritBatchOptions and handleInheritBatchSearch to track a request
generation (or abort the previous request) so stale remote responses cannot
update inheritBatchOptions or loading state after a newer search begins. Guard
both successful and error/finally state updates, while preserving the existing
debounce and error-display behavior for the latest request.

@jacksontwu
jacksontwu merged commit 562c484 into openBitFun:master Jul 29, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants