Skip to content

refactor(knowledge): make lib/knowledge/orchestration the single implementation - #6154

Merged
TheodoreSpeaks merged 2 commits into
improvement/v2-endpointsfrom
improvement/knowledge-orchestration
Aug 1, 2026
Merged

refactor(knowledge): make lib/knowledge/orchestration the single implementation#6154
TheodoreSpeaks merged 2 commits into
improvement/v2-endpointsfrom
improvement/knowledge-orchestration

Conversation

@TheodoreSpeaks

Copy link
Copy Markdown
Collaborator

Summary

  • Extract lib/knowledge/orchestration/** so the internal route, v1, v2, and the copilot tool share one implementation — same shape as lib/table/orchestration. Services write, orchestration guards/audits/classifies.
  • One chunking default. The agent defaulted minSize to 1 against the API's 100, so identical input chunked differently depending on who created the KB. Agent path now chunks at 100.
  • Audit every successful mutation inside the orchestration function. recordAudit appeared 0 times in the copilot tool, so agent-created KBs, uploads, updates and deletes left no audit trail.
  • Classify failures by class, not message text. Knowledge errors are OrchestrationError subclasses; storage limits throw a shared StorageLimitExceededError. Removes four separate greps for "already exists" / "does not have permission" / "storage limit".
  • Fix delete_connector reporting the opposite of what happened — it reached the route via an internal HTTP self-call with no query string, so the keep-documents default always applied while the agent said documents were removed. Self-call deleted, all four connector ops run in-process, real counts returned.

Notes

  • OrchestrationErrorCode gains payload_too_large (413). Without it, dropping the storage-limit message match would have regressed the documented 413 on KB create + document upload to a 500.
  • messageForOrchestrationError keeps a driver's message off a 500 response.
  • v1/v2 KB update now forwards actorUserId (the service requires it for a workspace move; both omitted it).
  • Connector DELETE reads deleteDocuments through parseRequest; its contract said z.boolean(), which rejects the string a query param actually is.
  • Dropped the dead 409 from POST /api/v2/knowledge/{id}/documents in the OpenAPI spec — nothing on that path throws a conflict.

Behavior change: a v1/v2 PUT carrying only the workspaceId scope field and no updates now returns 400 instead of 200 with the unchanged KB.

Deferred: document update stays internal-only. performUpdateKnowledgeDocument makes exposing it a contract + route away, but that's new public surface, not consolidation.

Inherited CI failure: check:api-validation:strict fails on doubleCasts 9 vs baseline 8. Verified identical on improvement/v2-endpoints with none of this branch's changes — all 9 casts are in tools/hosting.ts, contracts/tables.ts, executor/utils/errors.ts, and the logger/SDK packages. Not touched here.

Type of Change

  • Refactor + bug fix

Testing

  • bunx vitest run lib/knowledge app/api/knowledge app/api/v1/knowledge app/api/v2/knowledge lib/copilot — 118 files, 1387 tests passing
  • bun run type-check — clean
  • bun run lint:check — clean
  • bun run check:api-validation — passes
  • Full audit suite green except the inherited check:api-validation:strict noted above
  • 41 new orchestration tests covering defaults, guards, error classification, and audit; route tests reduced to delegation + failure-class → status

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

…ementation

Knowledge base create was implemented four times — the internal route, v1, v2,
and the copilot tool — and the orchestration around the shared write had
drifted. Extract it the same way lib/table/orchestration was: services write,
orchestration decides which writes run, guards them, audits them, and returns a
transport-neutral failure.

Behavior converged, not preserved:

- One chunking default (DEFAULT_CHUNKING_CONFIG). The agent defaulted minSize to
  1 against the API's 100, so identical input produced differently-chunked
  knowledge bases depending on who created it. The agent path now chunks at 100.
- Every successful mutation is audited inside the orchestration function. The
  copilot tool called recordAudit zero times, so agent-created knowledge bases,
  document uploads, updates and deletes left no audit trail at all.
- Failures classify by class, not by message text. The knowledge service errors
  are OrchestrationError subclasses and storage-quota rejections throw a shared
  StorageLimitExceededError, replacing four separate message greps for
  "already exists" / "does not have permission" / "storage limit".

delete_connector reported the opposite of what happened. It reached the route
through an internal HTTP self-call that sent no query string, so the route's
keep-documents default always applied while the agent told the user the
documents had been removed. The self-call is gone — all four connector
operations run in-process — and the orchestration returns the real counts.

Also:

- OrchestrationErrorCode gains 'payload_too_large' (413 / PAYLOAD_TOO_LARGE).
  Without it, dropping the storage-limit message match would have regressed the
  documented 413 on knowledge base create and document upload to a 500.
- messageForOrchestrationError renders a route's own wording for an unclassified
  fault, so a driver's message no longer reaches the client on a 500.
- v1 and v2 knowledge base update now forward actorUserId, which the service
  requires for a workspace move; both omitted it.
- The connector DELETE route reads deleteDocuments through parseRequest. Its
  contract declared z.boolean(), which would have rejected the string a query
  param actually is.
- Drop the 409 from POST /api/v2/knowledge/{id}/documents in the OpenAPI spec.
  Nothing on the upload path throws a conflict; it was only ever reachable by
  the message match this change removes.

Behavior change worth noting: a v1/v2 PUT carrying only the workspaceId scope
field and no actual updates now returns 400 rather than 200 with the unchanged
knowledge base.

Deliberately deferred: document update remains internal-only. Extracting
performUpdateKnowledgeDocument makes exposing it on v1/v2 a contract and a route
away, but that is a new public surface rather than part of this consolidation.
@vercel

vercel Bot commented Aug 1, 2026

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Aug 1, 2026 6:58am

Request Review

@cursor

cursor Bot commented Aug 1, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Broad refactor across UI, public APIs, and agent paths for core knowledge data; behavior and status codes change in edge cases (empty updates, storage limits, connector delete messaging).

Overview
Moves knowledge-base, document, and connector mutations into lib/knowledge/orchestration, so internal routes, v1/v2 APIs, and the copilot tool delegate to the same perform* functions instead of duplicating service calls, audits, and error handling.

Orchestration layer owns guards, recordAudit, telemetry, and classified outcomes. Copilot connector ops run in-process (no internal HTTP self-calls); billing attribution is passed as a value on orchestration calls.

Failure handling uses OrchestrationError / StorageLimitExceededError with codes including unauthorized and payload_too_large. Routes map failures via messageForOrchestrationError and statusForOrchestrationError (v2 uses v2ErrorForOrchestration). Unclassified internal faults no longer leak driver messages to clients.

Defaults & contracts: DEFAULT_CHUNKING_CONFIG (minSize: 100) replaces per-caller literals so agent and API chunking match. Connector deleteDocuments query param is parsed as a boolean with default false. OpenAPI drops a dead 409 on v2 document upload.

Copilot fixes: KB delete distinguishes failed vs not found; delete_connector reports kept/deleted document counts accurately.

Behavior change: v1/v2 KB PUT with only workspaceId and no other fields returns 400 instead of 200.

Reviewed by Cursor Bugbot for commit 1ba0215. Bugbot is set up for automated code reviews on this repo. Configure here.

@greptile-apps

greptile-apps Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR consolidates knowledge-base, document, and connector mutations behind shared orchestration functions used by API versions and the copilot tool.

  • Moves billing attribution ahead of connector writes and sync audit effects.
  • Separates failed knowledge-base deletions from genuinely missing resources.
  • Centralizes mutation auditing, error classification, connector handling, and chunking defaults.
  • Removes connector HTTP self-calls from the copilot path and reports actual document-retention counts.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; both previously reported defects are fixed at the current head.

Important Files Changed

Filename Overview
apps/sim/lib/knowledge/orchestration/connectors.ts Centralizes connector create, update, delete, and sync behavior; the previously reported billing-attribution ordering defect is fixed before durable effects.
apps/sim/lib/copilot/tools/server/knowledge/knowledge-base.ts Replaces direct service calls and internal HTTP connector calls with orchestration and correctly separates non-not-found deletion failures.
apps/sim/app/api/knowledge/[id]/connectors/route.ts Delegates connector creation to shared orchestration while preserving route-level authentication and billing attribution.
apps/sim/app/api/knowledge/[id]/connectors/[connectorId]/sync/route.ts Delegates manual connector sync to orchestration with billing attribution resolved before audit and dispatch effects.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  UI[Internal and versioned API routes] --> O[Knowledge orchestration]
  Copilot[Copilot knowledge tool] --> O
  O --> Guards[Authorization, billing, and validation guards]
  Guards --> Services[Knowledge services and database mutations]
  Services --> Audit[Audit and telemetry]
  Services --> Queue[Document processing and connector sync queues]
Loading

Reviews (2): Last reviewed commit: "fix(knowledge): make connector create at..." | Re-trigger Greptile

Comment thread apps/sim/lib/knowledge/orchestration/connectors.ts Outdated
Comment thread apps/sim/lib/copilot/tools/server/knowledge/knowledge-base.ts
Comment thread apps/sim/lib/knowledge/orchestration/connectors.ts
Comment thread apps/sim/lib/knowledge/orchestration/connectors.ts
Comment thread apps/sim/lib/copilot/tools/server/knowledge/knowledge-base.ts Outdated
…ures

Review round 1 on #6154.

- Resolve the billing payer before the connector is committed, not after. A
  malformed attribution header rejected post-commit left a live connector behind
  a 500, and a retry created a duplicate plus duplicate sync work. Manual sync
  resolves before writing its audit for the same reason.
- Let the source-config validator carry its own failure class. Collapsing every
  rejection to `validation` flattened the connector PATCH route's 401 (stale
  stored credential) and 409 (missing workspace context) into a 400.
- Add `unauthorized` to OrchestrationErrorCode. It is the class that 401 was
  already expressing on this route, and the v2 vocabulary already had
  UNAUTHORIZED; only the shared union was missing it.
- Report a knowledge base that exists but failed to archive as failed, with the
  reason, rather than as not found. The copilot delete loop folded every
  non-not-found failure into `notFound`, telling the user it was never there.
- Route copilot failures through the same message helper the HTTP surfaces use,
  so an unclassified fault's raw text (a driver's failed SQL) no longer reaches
  the agent verbatim while the UI and public APIs get the generic wording.
@TheodoreSpeaks

Copy link
Copy Markdown
Collaborator Author

@greptile

@TheodoreSpeaks

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 1ba0215. Configure here.

@TheodoreSpeaks
TheodoreSpeaks merged commit edd9a06 into improvement/v2-endpoints Aug 1, 2026
5 checks passed
@TheodoreSpeaks
TheodoreSpeaks deleted the improvement/knowledge-orchestration branch August 1, 2026 08:02
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.

1 participant