WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

[Bug]: Own-workspace agent_source_tenant_id breaks chat (404 "Shared agent not found") and KB listing (403)

Open lsasw opened this issue 2 months ago • 2 comments

Affected Component

Backend Service & API

Bug Description

A request that carries agent_source_tenant_id equal to the caller's own tenant is rejected as a missing share, even though the agent belongs to the caller and does not need a share at all.

agentShareService.GetSharedAgentForTenant returns ErrAgentShareNotFound for that input (internal/application/service/agent_share.go:465-467):

if len(sourceTenantID) > 0 && sourceTenantID[0] != 0 {
	if sourceTenantID[0] == tenantID {
		return nil, ErrAgentShareNotFound
	}

session.Handler.resolveAgent only falls back to the local agent when sourceTenantID == 0 (internal/handler/session/qa.go:557-561), so it returns nil and the caller aborts (internal/handler/session/qa.go:190-193):

customAgent, effectiveTenantID, sharedAgentReadOnly := h.resolveAgent(ctx, c, request.AgentID, request.AgentSourceTenantID)
if request.AgentSourceTenantID != 0 && customAgent == nil {
	return nil, nil, errors.NewNotFoundError("Shared agent not found")
}

This is reachable because an own-workspace agent never has a share row for its own workspace: GetShareByAgentIDForTenant deliberately excludes source_tenant_id == tenantID. The client keeps that selector together with the conversation (deep link query / restored last_request_state) and resends it on every subsequent message, so once the value is set the conversation fails permanently until client-side state is cleared.

Steps to Reproduce

  1. Take an agent owned by tenant A — the caller's own agent, no share involved.
  2. Send a QA message and include the owning tenant as the source selector:
curl -X POST "$HOST/api/v1/agent-chat/$SESSION_ID" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"query":"hello","agent_id":"<agent-id>","agent_source_tenant_id":<tenant-A-id>}'
  1. The same selector breaks the @-mention knowledge base list:
curl "$HOST/api/v1/knowledge-bases?agent_id=<agent-id>&agent_source_tenant_id=<tenant-A-id>" \
  -H "Authorization: Bearer $TOKEN"

Note: omitting agent_source_tenant_id (or sending 0) works fine, which is what makes the failure look session-specific.

Expected Behavior

The caller's own agent runs in the caller's tenant: the chat request succeeds and uses the agent's configured model/KB scope, and the knowledge base list returns that agent's scope.

Actual Behavior

  • POST /api/v1/agent-chat/:session → HTTP 404, message Shared agent not found (internal/handler/session/qa.go:191). The request is rejected before any LLM call, so the conversation cannot proceed at all.
  • GET /api/v1/knowledge-bases?agent_id=…&agent_source_tenant_id=… → HTTP 403, message no permission for this shared agent (internal/handler/shared_agent_access.go:40).
  • POST /api/v1/sessions/:id/temporary-documents shares the same resolveAgent guard (internal/handler/session/temporary_document.go:47-51), so attachment upload fails the same way.
  • Net user-visible effect: sending a message in such a conversation keeps failing (the UI surfaces a generic error, e.g. code 1002), the @-mention KB picker is empty, and the agent picker silently falls back to the local builtin.

WeKnora Version

App Version: v0.8.0 (UI Version: v0.8.0), deployed with Docker Compose.

Also still present on main today (5db13a1): internal/application/service/agent_share.go:466 and internal/handler/session/qa.go:557-561 are unchanged, so the same request fails there too.

Deployment Method

Docker Compose

Operating System

Ubuntu 22.04 (Linux, Docker Compose)

Relevant Logs

POST /api/v1/agent-chat/<session_id>
  -> HTTP 404   message: "Shared agent not found"                      (internal/handler/session/qa.go:191)

GET  /api/v1/knowledge-bases?agent_id=<own-agent>&agent_source_tenant_id=<own-tenant>
  -> HTTP 403   message: "no permission for this shared agent"         (internal/handler/shared_agent_access.go:40)

POST /api/v1/sessions/<session_id>/temporary-documents
  -> HTTP 404   message: "Shared agent not found"                      (internal/handler/session/temporary_document.go:49)

Removing agent_source_tenant_id from the same requests returns 200, which isolates the selector as the trigger.

Confirmation

  • [x] I have searched existing issues and confirmed this is a new one

lsasw avatar Sep 10 '26 07:09 lsasw

Confirmed on current main — the mechanism is exactly as reported: GetSharedAgentForTenant returns ErrAgentShareNotFound for a self-referential source (sourceTenantID == callerTenant, agent_share.go:466-468), and resolveAgent only falls back to the local agent when sourceTenantID == 0, so the own-agent request dies with 404.

I'll take this. Planned fix: normalize the self-reference at resolveAgent entry (sourceTenantID == currentTenantID → treat as 0), so own agents resolve through the existing local path. The rejected-foreign-source guard stays untouched — a rejected foreign selector still must not silently fall back to a same-ID local agent.

vicfei avatar Sep 14 '26 13:09 vicfei

Fix is up as #3249 — the self-referential selector normalizes to the absent selector at resolveAgent entry, so own agents resolve through the local path while the rejected-foreign-source guard stays intact (pinned by existing + new regression tests). I noted the two sibling access surfaces in the PR as a possible follow-up.

vicfei avatar Sep 14 '26 14:09 vicfei