Skip to content

feat: rag management - #2976

Merged
Chia1104 merged 2 commits into
developfrom
feat/rag-management
Aug 12, 2026
Merged

Chia1104 merged 2 commits into
developfrom
feat/rag-management

Conversation

@Chia1104

@Chia1104 Chia1104 commented Aug 12, 2026 •

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features

    • Added a RAG management area with overview dashboards, chunk search and inspection, indexing-run history, and maintenance tools.
    • Added embedding status details by locale, including coverage, chunk information, and reindex actions.
    • Added administrator controls for resource, feed, and full reindexing, plus stale-embedding cleanup.
    • Added progress tracking, previews, filtering, pagination, and loading states.
    • Added RAG navigation to the workspace sidebar.
  • Bug Fixes

    • Improved translation resource indexing in feed editing and metadata views.
    • Improved handling and reporting of indexing and chunk-loading errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Codex GPT-5.6 SOL <codex@openai.com>
@vercel

vercel Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
chia1104 Ready Ready Preview Aug 12, 2026 6:17pm

@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds RAG index-run persistence, indexing workflows, eleven RAG RPC endpoints, and dashboard views for overview metrics, chunks, runs, maintenance, and feed embedding status.

Changes

RAG management and indexing

Layer / File(s) Summary
Index-run persistence and statistics
packages/db/src/schemas/*, packages/db/src/libs/resources/*, packages/db/src/libs/feeds/index.ts, packages/db/.drizzle/migrations/*
Adds index-run storage, run repositories, embedding-state statistics, chunk queries, pagination, and stale-vector cleanup.
RPC contracts and routes
packages/api/orpc/*, packages/api/__tests__/rag.route.test.ts, packages/api/package.json
Adds eleven authenticated RAG procedures for reads, previews, indexing operations, run access, and embedding cleanup.
Indexing service and workflow
apps/service/src/services/rag-indexing.service.ts, apps/service/src/steps/resource-reindex.step.ts, apps/service/src/workflows/resource-reindex.workflow.ts, apps/service/src/routes/rpc.route.ts
Starts, reconciles, claims, tracks, and finalizes indexing runs through workflow execution.
Dashboard exploration
apps/dash/src/app/(workspace)/rag/*, apps/dash/src/components/rag/rag-overview.tsx, apps/dash/src/components/rag/rag-chunk-explorer.tsx, apps/dash/src/components/rag/rag-shared.tsx, apps/dash/src/components/rag/use-index-run.ts, apps/dash/src/shared/routes.tsx
Adds RAG pages, navigation, overview metrics, chunk search and filters, virtualized results, chunk details, and run polling.
Embedding and maintenance operations
apps/dash/src/components/rag/embedding-drawer.tsx, apps/dash/src/components/rag/rag-maintenance.tsx, apps/dash/src/components/feed/*, apps/dash/src/app/(workspace)/feed/(edit)/edit/[id]/page.tsx
Adds feed embedding status, indexing controls, reindex confirmation, progress tracking, and stale-vector cleanup.
Dashboard query-state wiring
apps/dash/src/components/commons/root-provider.tsx
Adds NuqsAdapter around the dashboard providers for URL-backed filter state.
Implementation record
plans/rag-dash-management-plan.md
Records implementation details, validation results, and migration status.

Estimated code review effort: 5 (Critical) | ~120 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the pull request's main change: adding RAG management functionality.
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 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/rag-management

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
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 10

🧹 Nitpick comments (8)
apps/dash/src/components/rag/rag-maintenance.tsx (2)

66-66: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Guard onOpenChange on the close transition.

onOpenChange receives the next open state. The handler ignores it and always calls onCancel(). If HeroUI ever reports an open transition here, the modal closes immediately.

♻️ Proposed change
-      <Modal.Backdrop isOpen={mode !== null} onOpenChange={() => onCancel()}>
+      <Modal.Backdrop
+        isOpen={mode !== null}
+        onOpenChange={(open) => {
+          if (!open) onCancel();
+        }}>
🤖 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 `@apps/dash/src/components/rag/rag-maintenance.tsx` at line 66, Update the
Modal.Backdrop onOpenChange handler in the rag maintenance component to inspect
the next open state and call onCancel only when the state indicates the modal is
closing; ignore open transitions so they do not cancel the operation.

117-128: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Cancel shows a pending spinner during the reindex mutation.

isPending={isPending} on the Cancel button makes Cancel look like it is doing work. Use isDisabled on Cancel instead, and keep isPending on the confirm button.

♻️ Proposed change
-              <Button isPending={isPending} variant="ghost" onPress={onCancel}>
+              <Button isDisabled={isPending} variant="ghost" onPress={onCancel}>
                 Cancel
               </Button>
🤖 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 `@apps/dash/src/components/rag/rag-maintenance.tsx` around lines 117 - 128,
Update the Cancel Button in the modal footer to use isDisabled={isPending}
instead of isPending={isPending}, while retaining isPending={isPending} on the
confirm Button.
packages/db/src/libs/resources/stats.ts (3)

286-286: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Escape LIKE wildcards in query.

The value is passed as a bound parameter, so there is no injection. But % and _ inside the operator input still act as pattern metacharacters, so a search for 100% matches unrelated content. Escape them before building the pattern.

♻️ Proposed escaping
+const likePattern = (value: string) =>
+  `%${value.replace(/[\\%_]/g, (char) => `\\${char}`)}%`;
+
@@
-          query ? ilike(chunks.content, `%${query}%`) : undefined,
+          query ? ilike(chunks.content, likePattern(query)) : undefined,
🤖 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 `@packages/db/src/libs/resources/stats.ts` at line 286, Update the query
pattern construction around ilike in the stats query to escape `%` and `_`
characters in the user-provided query before adding the surrounding wildcards,
while preserving bound-parameter usage and existing behavior for ordinary text.

211-248: 🚀 Performance & Scalability | 🔵 Trivial

Plan for the cost of the overview aggregates.

Each of the five queries scans resource_chunk and evaluates two correlated EXISTS subqueries per row. The dashboard overview calls all five on every load. As the corpus grows this becomes the most expensive read in the RAG surface.

Consider a partial index on resource_embedding (chunk_id, model, index_version) for the current-vector probe, and either short-lived caching or a materialized summary for the overview page.

🤖 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 `@packages/db/src/libs/resources/stats.ts` around lines 211 - 248, The
getRagOverview implementation issues five full aggregate queries, each repeating
expensive correlated vector checks; reduce this dashboard cost by reusing a
current-vector partial index on resource_embedding(chunk_id, model,
index_version) and adding either short-lived caching or a materialized summary
for the overview result, while preserving the existing RagOverview shape.

367-381: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Use rowCount instead of returning(). A PostgreSQL delete without .returning() returns the driver QueryResult; return { deletedCount: deleted.rowCount ?? 0 } to avoid materializing one row per deleted vector.

🤖 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 `@packages/db/src/libs/resources/stats.ts` around lines 367 - 381, Update
deleteStaleEmbeddings to remove the returning({ chunkId: embeddings.chunkId })
clause and use the delete query’s QueryResult instead; return deletedCount as
deleted.rowCount ?? 0.
packages/db/src/schemas/resources.schema.ts (1)

232-242: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Constrain scope and status at the database level.

$type<>() only narrows the TypeScript type. It adds no runtime or database validation. resourceChunks.kind at Line 87 uses text(..., { enum: [...] }), so this table is the outlier. A writer that supplies an unlisted value (for example a workflow status this codebase does not map) persists a row that no reader treats as active and no reader treats as terminal.

Add check() constraints, and derive the index predicates from RESOURCE_INDEX_RUN_ACTIVE_STATUSES so the literals at Lines 261, 266, and 271 cannot drift from the constant.

♻️ Proposed constraint and predicate reuse
+const ACTIVE_STATUS_SQL = sql.raw(
+  RESOURCE_INDEX_RUN_ACTIVE_STATUSES.map((status) => `'${status}'`).join(", ")
+);
+
 export const resourceIndexRuns = pgTable(
   "resource_index_run",
   {
@@
   (table) => [
+    check(
+      "resource_index_run_scope_valid",
+      sql`${table.scope} in ('resource', 'feed', 'all')`
+    ),
+    check(
+      "resource_index_run_status_valid",
+      sql`${table.status} in ('pending', 'running', 'completed', 'failed', 'cancelled')`
+    ),
     index("resource_index_run_source_idx").on(table.sourceType, table.sourceId),
🤖 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 `@packages/db/src/schemas/resources.schema.ts` around lines 232 - 242, Update
the resource index run schema around scope and status to add database-level
check constraints allowing only the defined ResourceIndexRunScope and
ResourceIndexRunStatus values. In the active, completed, and failed index
predicates, reuse RESOURCE_INDEX_RUN_ACTIVE_STATUSES instead of duplicating
status literals, while preserving the existing predicate behavior.
packages/db/.drizzle/migrations/20260812083824_resource_index_run/migration.sql (1)

4-8: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Add the enum CHECK constraints in this migration.

"scope" and "status" are unconstrained text. The application layer is the only validator today. Add CHECK constraints here so an unmapped status cannot be stored.

🛡️ Proposed addition
 ALTER TABLE "chia_resource_index_run" ADD CONSTRAINT "chia_resource_index_run_triggered_by_chia_user_id_fkey" FOREIGN KEY ("triggered_by") REFERENCES "chia_user"("id");
+--> statement-breakpoint
+ALTER TABLE "chia_resource_index_run" ADD CONSTRAINT "resource_index_run_scope_valid" CHECK ("scope" IN ('resource', 'feed', 'all'));--> statement-breakpoint
+ALTER TABLE "chia_resource_index_run" ADD CONSTRAINT "resource_index_run_status_valid" CHECK ("status" IN ('pending', 'running', 'completed', 'failed', 'cancelled'));
🤖 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
`@packages/db/.drizzle/migrations/20260812083824_resource_index_run/migration.sql`
around lines 4 - 8, Update the migration’s table definition to add database
CHECK constraints restricting “scope” and “status” to their supported enum
values, while preserving the existing defaults and NOT NULL behavior. Ensure
invalid or unmapped values cannot be inserted directly into the database.
apps/service/src/steps/resource-reindex.step.ts (1)

29-41: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Page the reindex target enumeration. listFeedTranslationIds(db, {}) intentionally includes unpublished and soft-deleted translations so chunk visibility remains available for BM25 filtering.

listReindexTargetsStep still returns the complete target array as one workflow-step output. Use paging or another bounded enumeration as the corpus grows.

🤖 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 `@apps/service/src/steps/resource-reindex.step.ts` around lines 29 - 41, Update
listReindexTargetsStep to enumerate feed translation IDs in bounded pages
instead of loading all results into one array, while preserving inclusion of
unpublished and soft-deleted translations for BM25 filtering. Use the existing
paging support exposed by listFeedTranslationIds or the database layer, then
emit or process each page without retaining the complete target collection in a
single workflow-step output.
🤖 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 `@apps/dash/src/components/rag/embedding-drawer.tsx`:
- Around line 269-274: Update the embedding drawer’s hasEmbedding initialization
to use the existing per-locale embedding values provided through meta.embedding,
then refine that value with statuses query counts once the drawer is open.
Preserve the current query-based detection after opening while ensuring the
trigger reflects existing embeddings before any resource:status query runs.
- Around line 168-190: The permission tooltip cannot open from disabled buttons;
update both Recompute controls in
apps/dash/src/components/rag/embedding-drawer.tsx at lines 168-190 and 338-354
to display the restriction message through a focusable or plain-text element,
following the existing rag-maintenance.tsx approach. Preserve the disabled
button behavior and apply the same change to both sites.

In `@apps/dash/src/components/rag/rag-chunk-explorer.tsx`:
- Around line 77-104: The chunk explorer currently shows a permanent spinner
when the chunk:get query fails because data is undefined after loading. Update
the useQuery result and Drawer.Body rendering to handle the query error after
the loading branch, rendering the error when error || !data while preserving the
spinner only for isLoading.

In `@apps/dash/src/components/rag/rag-maintenance.tsx`:
- Around line 252-259: Update the prune action in the component containing the
“Prune stale vectors” Button to require confirmation before calling
prune.mutate, using the existing confirmation pattern such as
ConfirmReindexModal. Also include isActive in the Button’s isDisabled condition
so pruning cannot start during an active index run, while preserving the
existing canTrigger and prune.isPending gates.

In `@apps/dash/src/components/rag/use-index-run.ts`:
- Around line 58-62: Update the return logic in the run-following hook around
run and query to treat query.error as inactive: set isActive to false whenever
run:get fails, while preserving the existing run-status behavior for successful
queries. Also expose query.error from the returned object so callers following a
run can display it.

In `@apps/service/src/services/rag-indexing.service.ts`:
- Around line 147-155: Validate workflow statuses end to end: in
apps/service/src/services/rag-indexing.service.ts:147-155, update the
finalization flow to map runtime status via an explicit switch to
ResourceIndexRunTerminalStatus, defaulting to RESOURCE_INDEX_RUN_STATUS.Failed;
in packages/db/src/schemas/resources.schema.ts:232-242, add check constraints
for scope and status and derive active-status index predicates from
RESOURCE_INDEX_RUN_ACTIVE_STATUSES; in
packages/db/.drizzle/migrations/20260812083824_resource_index_run/migration.sql:4-8,
add matching CHECK constraints for the permitted values.

In `@packages/db/src/libs/resources/index-run.ts`:
- Line 175: Both list repositories must normalize limits to positive integers
before applying the maximum cap. In
packages/db/src/libs/resources/index-run.ts:175-175, update the limit
calculation to truncate and clamp dto.limit, using a default of 20 and a minimum
of 1; apply the same change in packages/db/src/libs/resources/stats.ts:269-269,
using a default of 50.

In `@plans/rag-dash-management-plan.md`:
- Around line 363-365: Update the rollout plan around the “尚未執行” migration
status to apply and verify the generated resource-index migration before
enabling or deploying RAG trigger and runs routes; alternatively, explicitly
gate those routes until migration completion, while preserving read-only chunk
statistics.
- Line 331: Update the run coordination described in the “跨 scope 不互斥” section
so resource- and feed-scoped runs cannot process the same chunks concurrently.
Before starting either scope, claim a shared resource-level lease, or reuse an
already active overlapping run instead of submitting duplicate embedding work.
- Line 114: Specify the text language on all three fenced code blocks in
plans/rag-dash-management-plan.md: add text to the schema tree fence at lines
114-114, the directory tree fence at lines 261-261, and the workflow pseudocode
fence at lines 281-281.

---

Nitpick comments:
In `@apps/dash/src/components/rag/rag-maintenance.tsx`:
- Line 66: Update the Modal.Backdrop onOpenChange handler in the rag maintenance
component to inspect the next open state and call onCancel only when the state
indicates the modal is closing; ignore open transitions so they do not cancel
the operation.
- Around line 117-128: Update the Cancel Button in the modal footer to use
isDisabled={isPending} instead of isPending={isPending}, while retaining
isPending={isPending} on the confirm Button.

In `@apps/service/src/steps/resource-reindex.step.ts`:
- Around line 29-41: Update listReindexTargetsStep to enumerate feed translation
IDs in bounded pages instead of loading all results into one array, while
preserving inclusion of unpublished and soft-deleted translations for BM25
filtering. Use the existing paging support exposed by listFeedTranslationIds or
the database layer, then emit or process each page without retaining the
complete target collection in a single workflow-step output.

In
`@packages/db/.drizzle/migrations/20260812083824_resource_index_run/migration.sql`:
- Around line 4-8: Update the migration’s table definition to add database CHECK
constraints restricting “scope” and “status” to their supported enum values,
while preserving the existing defaults and NOT NULL behavior. Ensure invalid or
unmapped values cannot be inserted directly into the database.

In `@packages/db/src/libs/resources/stats.ts`:
- Line 286: Update the query pattern construction around ilike in the stats
query to escape `%` and `_` characters in the user-provided query before adding
the surrounding wildcards, while preserving bound-parameter usage and existing
behavior for ordinary text.
- Around line 211-248: The getRagOverview implementation issues five full
aggregate queries, each repeating expensive correlated vector checks; reduce
this dashboard cost by reusing a current-vector partial index on
resource_embedding(chunk_id, model, index_version) and adding either short-lived
caching or a materialized summary for the overview result, while preserving the
existing RagOverview shape.
- Around line 367-381: Update deleteStaleEmbeddings to remove the returning({
chunkId: embeddings.chunkId }) clause and use the delete query’s QueryResult
instead; return deletedCount as deleted.rowCount ?? 0.

In `@packages/db/src/schemas/resources.schema.ts`:
- Around line 232-242: Update the resource index run schema around scope and
status to add database-level check constraints allowing only the defined
ResourceIndexRunScope and ResourceIndexRunStatus values. In the active,
completed, and failed index predicates, reuse RESOURCE_INDEX_RUN_ACTIVE_STATUSES
instead of duplicating status literals, while preserving the existing predicate
behavior.
🪄 Autofix

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: 34c93e97-d6ff-41cd-952d-f92be139106a

📥 Commits

Reviewing files that changed from the base of the PR and between 5389a8a and 6440cef.

⛔ Files ignored due to path filters (2)
  • apps/dash/.next/dev/types/link.d.ts is excluded by !**/.next/**
  • apps/dash/.next/dev/types/routes.d.ts is excluded by !**/.next/**
📒 Files selected for processing (39)
  • apps/dash/src/app/(workspace)/feed/(edit)/edit/[id]/page.tsx
  • apps/dash/src/app/(workspace)/rag/chunks/page.tsx
  • apps/dash/src/app/(workspace)/rag/layout.tsx
  • apps/dash/src/app/(workspace)/rag/maintenance/page.tsx
  • apps/dash/src/app/(workspace)/rag/page.tsx
  • apps/dash/src/app/(workspace)/rag/runs/page.tsx
  • apps/dash/src/components/commons/app-sidebar.tsx
  • apps/dash/src/components/commons/root-provider.tsx
  • apps/dash/src/components/feed/edit-fields.tsx
  • apps/dash/src/components/feed/edit-form.tsx
  • apps/dash/src/components/feed/metadata-fields.tsx
  • apps/dash/src/components/rag/embedding-drawer.tsx
  • apps/dash/src/components/rag/rag-chunk-explorer.tsx
  • apps/dash/src/components/rag/rag-maintenance.tsx
  • apps/dash/src/components/rag/rag-overview.tsx
  • apps/dash/src/components/rag/rag-runs-table.tsx
  • apps/dash/src/components/rag/rag-shared.tsx
  • apps/dash/src/components/rag/use-index-run.ts
  • apps/dash/src/shared/routes.tsx
  • apps/service/src/routes/rpc.route.ts
  • apps/service/src/services/rag-indexing.service.ts
  • apps/service/src/steps/resource-reindex.step.ts
  • apps/service/src/workflows/resource-reindex.workflow.ts
  • packages/api/__tests__/rag.route.test.ts
  • packages/api/orpc/contracts/rag.contract.ts
  • packages/api/orpc/indexing.ts
  • packages/api/orpc/router.contract.ts
  • packages/api/orpc/router.ts
  • packages/api/orpc/routes/rag.route.ts
  • packages/api/package.json
  • packages/db/.drizzle/migrations/20260812083824_resource_index_run/migration.sql
  • packages/db/.drizzle/migrations/20260812083824_resource_index_run/snapshot.json
  • packages/db/package.json
  • packages/db/src/libs/feeds/index.ts
  • packages/db/src/libs/resources/index-run.ts
  • packages/db/src/libs/resources/stats.ts
  • packages/db/src/schemas/index.ts
  • packages/db/src/schemas/resources.schema.ts
  • plans/rag-dash-management-plan.md

Comment on lines +168 to +190
<Tooltip delay={300} isDisabled={canTrigger}>
<Tooltip.Trigger>
<Button
isDisabled={!canTrigger || isBusy}
isPending={isBusy}
size="sm"
variant="tertiary"
onPress={() =>
trigger.mutate({
sourceType: FEED_TRANSLATION_SOURCE_TYPE,
sourceId: resource.sourceId,
})
}>
<RefreshCwIcon className="size-3.5" />
Recompute
</Button>
</Tooltip.Trigger>
<Tooltip.Content>
<p className="text-xs">
Only the configured admin can trigger this
</p>
</Tooltip.Content>
</Tooltip>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

The permission tooltip never opens, because its trigger is disabled in the same state. Both controls enable the Tooltip only when canTrigger is false, and both disable the Button under that same condition. React Aria requires a hoverable and focusable trigger, so a disabled button does not open a tooltip. The user sees a greyed-out button with no reason.

  • apps/dash/src/components/rag/embedding-drawer.tsx#L168-L190: show the restriction text without depending on the disabled Recompute button, or wrap the button in a focusable element.
  • apps/dash/src/components/rag/embedding-drawer.tsx#L338-L354: apply the same change to the "Recompute whole feed" control in the drawer footer.

apps/dash/src/components/rag/rag-maintenance.tsx at Line 263 already renders this message as plain text. Reuse that approach for consistency.

📍 Affects 1 file
  • apps/dash/src/components/rag/embedding-drawer.tsx#L168-L190 (this comment)
  • apps/dash/src/components/rag/embedding-drawer.tsx#L338-L354
🤖 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 `@apps/dash/src/components/rag/embedding-drawer.tsx` around lines 168 - 190,
The permission tooltip cannot open from disabled buttons; update both Recompute
controls in apps/dash/src/components/rag/embedding-drawer.tsx at lines 168-190
and 338-354 to display the restriction message through a focusable or plain-text
element, following the existing rag-maintenance.tsx approach. Preserve the
disabled button behavior and apply the same change to both sites.

Comment on lines +269 to +274
const first = statuses[0]?.data;
const canTrigger = first?.canTrigger ?? false;
const isFeedBusy = feedRun.isActive || indexFeed.isPending;
const hasEmbedding = statuses.some(
(query) => (query.data?.counts.current ?? 0) > 0
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

hasEmbedding is always false until the drawer opens.

The resource:status queries use enabled: isOpen. hasEmbedding reads only those query results. Before the first open, every query is idle, so the trigger icon always renders in the muted color and never signals existing embeddings.

The feed page already resolves this data. apps/dash/src/app/(workspace)/feed/(edit)/edit/[id]/page.tsx passes t.hasEmbedding per locale in meta.embedding. Pass the same information into the drawer, and use the query results only as a refinement after opening.

🤖 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 `@apps/dash/src/components/rag/embedding-drawer.tsx` around lines 269 - 274,
Update the embedding drawer’s hasEmbedding initialization to use the existing
per-locale embedding values provided through meta.embedding, then refine that
value with statuses query counts once the drawer is open. Preserve the current
query-based detection after opening while ensuring the trigger reflects existing
embeddings before any resource:status query runs.

Comment on lines +147 to +155
const result =
status === "completed"
? await run.returnValue.catch(() => undefined)
: undefined;

return await finalize(db, row, status, {
result,
error: status === "completed" ? null : `Workflow run ${status}.`,
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

The run status is unvalidated end to end. The service forwards the workflow runtime status straight into the status column, and no layer restricts that column to the five known values. An unmapped value produces a row that isActive reports as inactive, that the partial unique indexes ignore, and that the dashboard cannot render.

  • apps/service/src/services/rag-indexing.service.ts#L147-L155: map the runtime status to ResourceIndexRunTerminalStatus with an explicit switch, and default to RESOURCE_INDEX_RUN_STATUS.Failed.
  • packages/db/src/schemas/resources.schema.ts#L232-L242: add check() constraints for scope and status, and derive the active-status index predicates from RESOURCE_INDEX_RUN_ACTIVE_STATUSES.
  • packages/db/.drizzle/migrations/20260812083824_resource_index_run/migration.sql#L4-L8: add the matching CHECK constraints so existing databases enforce the same values.
📍 Affects 3 files
  • apps/service/src/services/rag-indexing.service.ts#L147-L155 (this comment)
  • packages/db/src/schemas/resources.schema.ts#L232-L242
  • packages/db/.drizzle/migrations/20260812083824_resource_index_run/migration.sql#L4-L8
🤖 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 `@apps/service/src/services/rag-indexing.service.ts` around lines 147 - 155,
Validate workflow statuses end to end: in
apps/service/src/services/rag-indexing.service.ts:147-155, update the
finalization flow to map runtime status via an explicit switch to
ResourceIndexRunTerminalStatus, defaulting to RESOURCE_INDEX_RUN_STATUS.Failed;
in packages/db/src/schemas/resources.schema.ts:232-242, add check constraints
for scope and status and derive active-status index predicates from
RESOURCE_INDEX_RUN_ACTIVE_STATUSES; in
packages/db/.drizzle/migrations/20260812083824_resource_index_run/migration.sql:4-8,
add matching CHECK constraints for the permitted values.


放在 `packages/db/src/schemas/resources.schema.ts`,緊接 `resourceEmbeddings`。

```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Specify a language for each fenced code block.

markdownlint-cli2 reports MD040 at all three locations.

  • plans/rag-dash-management-plan.md#L114-L114: add text to the schema tree fence.
  • plans/rag-dash-management-plan.md#L261-L261: add text to the directory tree fence.
  • plans/rag-dash-management-plan.md#L281-L281: add text to the workflow pseudocode fence.
🧰 Tools
🪛 markdownlint-cli2 (0.23.2)

[warning] 114-114: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

📍 Affects 1 file
  • plans/rag-dash-management-plan.md#L114-L114 (this comment)
  • plans/rag-dash-management-plan.md#L261-L261
  • plans/rag-dash-management-plan.md#L281-L281
🤖 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 `@plans/rag-dash-management-plan.md` at line 114, Specify the text language on
all three fenced code blocks in plans/rag-dash-management-plan.md: add text to
the schema tree fence at lines 114-114, the directory tree fence at lines
261-261, and the workflow pseudocode fence at lines 281-281.

Source: Linters/SAST tools

- `meta-chip.tsx` 的 chip 是否改用三態語意(空間只有一個 icon)。目前維持「曾經嵌入過」。
- 檢索品質基準(`docs/rag-architecture.md` §9 的第一項)不在本規劃範圍。RAG 管理區塊將來是放評測結果的自然位置,但要先有評測腳本。
- `resource_index_run` 的保留期限 / 清理策略。目前資料量小,暫不處理。
- **跨 scope 不互斥**:`resource` 與 `feed` 兩個 scope 各有自己的 active unique index,所以「重算此語系」與「重算整篇」可以同時在跑,同一批 chunk 會被嵌入兩次。資料不會壞(`saveChunkEmbeddings` 是 upsert),但會多花額度。要擋需要跨 scope 的鎖,先不做。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚀 Performance & Scalability | 🟠 Major | 🏗️ Heavy lift

Prevent duplicate embedding work across run scopes.

Line 331 permits overlapping resource and feed runs for the same chunks. Both runs can submit identical chunks to the embedding provider. This consumes duplicate credits and can exhaust provider rate limits.

Claim a shared resource-level lease before starting any scope, or reuse the overlapping run.

🤖 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 `@plans/rag-dash-management-plan.md` at line 331, Update the run coordination
described in the “跨 scope 不互斥” section so resource- and feed-scoped runs cannot
process the same chunks concurrently. Before starting either scope, claim a
shared resource-level lease, or reuse an already active overlapping run instead
of submitting duplicate embedding work.

Comment on lines +363 to +365
### 尚未執行

**migration 未套用。** `packages/db/.drizzle/migrations/20260812083824_resource_index_run/` 已產生但沒有跑 `db:migrate` / `db:push`。在套用之前,所有觸發與 runs 頁都會因為 `chia_resource_index_run` 不存在而失敗;唯讀的 chunk 統計不受影響。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Apply the migration before enabling RAG triggers.

Line 365 states that chia_resource_index_run does not exist yet. Trigger and run routes query this table, so they fail until the migration completes.

Apply and verify the migration before deploying these routes, or gate the routes until rollout completes.

🤖 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 `@plans/rag-dash-management-plan.md` around lines 363 - 365, Update the rollout
plan around the “尚未執行” migration status to apply and verify the generated
resource-index migration before enabling or deploying RAG trigger and runs
routes; alternatively, explicitly gate those routes until migration completion,
while preserving read-only chunk statistics.

- confirm stale-vector pruning and prevent it during active runs
- surface run and chunk fetch failures in the dashboard
- keep unknown workflow statuses active and normalize list limits

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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 `@apps/dash/src/components/rag/rag-maintenance.tsx`:
- Around line 290-307: Update the pruning button’s disabled condition in the
maintenance component to also block when the tracked run polling error from
useIndexRun is present, even if isActive is false. Surface that polling error in
the existing UI error display, allowing pruning to become available again once
run status polling succeeds.
🪄 Autofix

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: 92a6c74b-12f4-4e3f-94bb-3b37a5b9e3dc

📥 Commits

Reviewing files that changed from the base of the PR and between 6440cef and add3c1c.

📒 Files selected for processing (7)
  • apps/dash/src/components/rag/embedding-drawer.tsx
  • apps/dash/src/components/rag/rag-chunk-explorer.tsx
  • apps/dash/src/components/rag/rag-maintenance.tsx
  • apps/dash/src/components/rag/use-index-run.ts
  • apps/service/src/services/rag-indexing.service.ts
  • packages/db/src/libs/resources/index-run.ts
  • packages/db/src/libs/resources/stats.ts
🚧 Files skipped from review as they are similar to previous changes (5)
  • apps/dash/src/components/rag/use-index-run.ts
  • apps/dash/src/components/rag/rag-chunk-explorer.tsx
  • packages/db/src/libs/resources/stats.ts
  • packages/db/src/libs/resources/index-run.ts
  • apps/service/src/services/rag-indexing.service.ts

Comment on lines +290 to +307
onPress={() => setAction("top-up")}>
<RefreshCwIcon className="size-4" />
Top up missing
</Button>
<Button
isDisabled={!canTrigger || isBusy}
isPending={isBusy}
variant="danger"
onPress={() => setAction("full")}>
<RefreshCwIcon className="size-4" />
Full reindex
</Button>
{/* `isActive` matters here: pruning mid-run would delete rows the run is still writing */}
<Button
isDisabled={!canTrigger || isActive || prune.isPending}
isPending={prune.isPending}
variant="tertiary"
onPress={() => setAction("prune")}>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Block pruning when run polling fails.

Line 304 only blocks pruning when isActive is true. useIndexRun sets isActive to false when run:get fails. A known activeRunId can then still represent a running index job while this button enables pruning.

Keep pruning disabled when the tracked run has a polling error. Display that error so the user can retry after run status is available.

Proposed fix
-  const { run, isActive } = useIndexRun({ runId: activeRunId, onSettled });
+  const { run, isActive, error: runError } = useIndexRun({
+    runId: activeRunId,
+    onSettled,
+  });
...
-            isDisabled={!canTrigger || isActive || prune.isPending}
+            isDisabled={
+              !canTrigger || isActive || !!runError || prune.isPending
+            }
🤖 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 `@apps/dash/src/components/rag/rag-maintenance.tsx` around lines 290 - 307,
Update the pruning button’s disabled condition in the maintenance component to
also block when the tracked run polling error from useIndexRun is present, even
if isActive is false. Surface that polling error in the existing UI error
display, allowing pruning to become available again once run status polling
succeeds.

@Chia1104
Chia1104 merged commit 43b24e7 into develop Aug 12, 2026
21 checks passed
@Chia1104
Chia1104 deleted the feat/rag-management branch August 12, 2026 18:24

This branch was successfully deployed

1 active deployment
Preview — add3c1c1 Deployed Aug 12, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant