WeKnora/internal/handler
Leo Dawn 56d075e213
fix(sandbox): resolve a session's pinned sandbox in the workspace that owns it (#3471)
* fix(sandbox): resolve a session's pinned sandbox in the workspace that owns it

tenant_sandbox_configs is keyed by (tenant_id, id), so a config id is not an
address on its own. sessions.sandbox_config_id stored only the id and every
reader took the workspace from the ambient request context.

That holds inside a chat turn, which already runs under the agent's workspace
(WithExecutionTenant in the QA handler). It breaks for a shared agent
everywhere else, because a shared agent runs on ITS OWNER's sandbox config
while the session belongs to the borrower, and these paths run from plain
requests whose only tenant is the session owner's:

  * DeleteSession resolved nothing and returned after a warning. Cube and E2B
    create session sandboxes with onTimeout=pause, so the MicroVM was not
    reclaimed later either: it stayed paused, billing, with nobody holding its
    id. ReapOrphanSandboxes has no production caller, so nothing recovered it.
  * The terminal and desktop panels failed on both branches - attaching to the
    pinned sandbox and provisioning a new one - with ErrSandboxConfigNotFound,
    which terminalErrorFrame maps to a bare INTERNAL close reason. The panel is
    offered unconditionally and already forwards agent_source_tenant_id, so
    this was a dead end the user could reach in one click.
  * Fork read the pin through PinnedSessionSandbox, got no manager, and
    silently degraded to SNAPSHOT_UNSUPPORTED, losing the sandbox state.
  * The fork snapshot lease recorded the session's workspace next to the
    lending workspace's config id, so the reaper could not resolve it either.

Record the owning workspace beside the pin and make the pair travel together
as SandboxPin. Readers now take the workspace from the pin and fall back to the
session's own, which is what every non-borrowed pin (and every row written
before this column) resolves to - so behaviour for own agents is unchanged.

The provision path has no pin yet, so the WebSocket handler passes the agent's
workspace along with its config: terminalProvisionPin already resolved the
shared agent, it just discarded the tenant resolveAgent handed back.

Backfill in the migration is limited to sessions whose pinned config does not
exist in their own workspace - the only ones that can have borrowed it - using
the lending workspace recorded on their newest shared-agent message. Lite has
no shared spaces, so the SQLite mirror adds the column without a backfill.

* fix(sandbox): backfill the pin owner from the config row and self-heal legacy pins

Three gaps in the session sandbox pin owner:

Backfilling from messages.agent_tenant_id names the wrong workspace. The
pin is sticky to the session's FIRST sandbox, while the newest message
carrying agent_tenant_id may belong to a later shared agent from another
workspace. Writing that tenant leaves teardown resolving a config the
workspace does not own - the very failure the column exists to fix.
tenant_sandbox_configs.id is a global UUID primary key, so joining the
config row itself names the workspace that actually owns the pin.

The column was INTEGER while every other tenant id is BIGINT/uint64.
Postgres INTEGER is 32-bit signed, so a large tenant id would not fit.

Rows the SQL backfill cannot reach keep sandbox_config_tenant_id = 0
forever: the reader falls back to the caller's workspace but never
records what it learned. The next chat turn already runs under the
workspace that can resolve the config, so persisting it there is what
lets a later DELETE, panel open or fork find the sandbox. recordOwner
only writes when the lookup actually succeeded and the stored owner is
still unset - stamping the caller's tenant onto a failed lookup would
brand a sticky pin left by a previous shared agent with the wrong
workspace.

Also renames the lentSandboxConfg typo and corrects the fork degrade
reason in a comment: a lent config that resolves to nil fails
BoundSandboxID, so the fork degrades to SANDBOX_GONE, not
SNAPSHOT_UNSUPPORTED.

* fix(sandbox): require a named backend before recording a pin's owner

The self-heal wants to persist an owner "that actually resolved this config",
but a nil error does not prove that. resolveTenantSandboxForConfig returns a
disabled manager with no error twice before it ever reads the config row: for
the workspace kill switch, and for an empty or deployment-default config id.

That reopens the leak this column exists to close. The SQL backfill correctly
leaves sandbox_config_tenant_id at 0 whenever the pinned config belongs to the
session's own workspace, which is most existing rows. One turn of a shared
agent lent by a workspace whose admin has script execution turned off then
short-circuits on the kill switch, returns nil, and stamps that lending
workspace onto a config it does not own. The next DELETE resolves the pin there,
finds nothing, and strands the paused MicroVM.

The pin branch runs before agentConfigID is read, so the shared agent does not
even need a sandbox config of its own to trigger it.

A named backend (Cube/E2B/Docker) is the proof, and it is the same condition
that is already allowed to CREATE a pin further down: what may write a pin may
update whose it is. A config that exists but resolves to type "disabled" is
skipped as well - ownership is genuine there, but such a config never creates a
sandbox, so no teardown can go missing.

The deployment-default pin was inert either way (both workspaces short-circuit
to a manager with no DestroySession), but it is covered too so a later
relaxation of the proof cannot quietly reopen the kill-switch case.

Both tests fail without the guard.

---------

Co-authored-by: hailongzhao <hailongzhao@tencent.com>
2026-09-21 15:38:39 +08:00
..
dto fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475) 2026-09-21 13:02:34 +08:00
session fix(sandbox): resolve a session's pinned sandbox in the workspace that owns it (#3471) 2026-09-21 15:38:39 +08:00
audit_log.go Remove redundant Wiki operation log 2026-07-28 11:18:33 +08:00
audit_log_test.go feat(knowledge): add scoped activity audit trail 2026-07-23 10:50:32 +08:00
auth.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
auth_browser_preferences_test.go feat(browser): polish connection settings, open tasks in a new window, and streamline prompts (#3193) 2026-09-11 21:28:58 +08:00
auth_change_password_test.go feat: support complex password (#2929) 2026-09-01 11:50:10 +08:00
auth_desktop_security_test.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
auth_oidc_start_test.go feat(auth): add /auth/oidc/start endpoint for direct 302 OIDC login 2026-08-11 15:41:48 +08:00
auth_register_by_invite.go feat: support complex password (#2929) 2026-09-01 11:50:10 +08:00
auth_register_by_invite_tenant_test.go feat: support complex password (#2929) 2026-09-01 11:50:10 +08:00
auth_register_invite_only_test.go feat: support complex password (#2929) 2026-09-01 11:50:10 +08:00
chunk.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
chunker_debug.go fix(chunking): 修复内联 HTML 表格与保护模式导致的超长 chunk (#3404) 2026-09-18 21:10:17 +08:00
chunker_debug_test.go fix(chunking): normalize line endings before splitting / 在分块前统一换行符 2026-08-05 19:24:45 +08:00
custom_agent.go refactor(models): rebuild vendor integration on protocol + catalog + vendor layers (#3470) 2026-09-20 21:28:23 +08:00
custom_agent_api_key_scope_test.go feat(agent): enhance suggested questions API with tag scopes 2026-07-17 10:19:43 +08:00
custom_agent_avatar_test.go fix(security): close cross-workspace access paths in shared spaces (#3408) 2026-09-18 22:00:02 +08:00
custom_agent_reasoning_effort_test.go refactor(models): rebuild vendor integration on protocol + catalog + vendor layers (#3470) 2026-09-20 21:28:23 +08:00
custom_agent_sandbox_test.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
datasource.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
datasource_credentials.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
datasource_test.go Validate sync log limit parameter 2026-06-12 22:52:25 +08:00
deployment_capabilities.go feat(mcp): add built-in MCP server with per-workspace endpoints (#3371) 2026-09-17 22:40:05 +08:00
deployment_capabilities_test.go feat: require opt-in for the Docker sandbox backend (#2936) 2026-09-01 15:42:28 +08:00
document_write_scope_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
embed_allowed_origins_test.go fix(embed): enforce host origin allowlists on framed pages 2026-09-14 18:03:57 +08:00
embed_channel.go fix(security): close the low-severity shared-space findings (#3416) 2026-09-18 22:58:46 +08:00
embed_channel_exchange_test.go refactor(context): replace middleware.EmbedChannelContextKey with types.EmbedChannelContextKey 2026-08-13 18:00:02 +08:00
embed_flow_test.go fix(security): close the low-severity shared-space findings (#3416) 2026-09-18 22:58:46 +08:00
embed_resource_urls_test.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
embed_session_test.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
evaluation.go feat: 新增API Key认证支持,优化Swagger文档安全配置 2025-12-22 15:01:09 +08:00
faq.go feat(faq): filter entries by enabled status (#3157) 2026-09-10 14:42:59 +08:00
faq_enabled_filter_test.go feat(faq): filter entries by enabled status (#3157) 2026-09-10 14:42:59 +08:00
im.go fix(security): close the low-severity shared-space findings (#3416) 2026-09-18 22:58:46 +08:00
im_platform_test.go feat(im,datasource): support Lark, Feishu's international edition 2026-07-17 11:05:54 +08:00
initialization.go refactor(models): delete the internal/models/provider façade (#3506) 2026-09-21 14:41:48 +08:00
initialization_config_redaction_test.go fix(security): close cross-workspace access paths in shared spaces (#3408) 2026-09-18 22:00:02 +08:00
initialization_fill_secrets_test.go fix(initialization): pass stored ExtraConfig to model connection test 2026-09-07 11:43:44 +08:00
initialization_model_merge_test.go refactor(models): rebuild vendor integration on protocol + catalog + vendor layers (#3470) 2026-09-20 21:28:23 +08:00
initialization_model_tenant_test.go fix(initialization): stamp the KB's tenant onto created models 2026-09-17 13:09:34 +08:00
initialization_owner_test.go fix(security): close cross-workspace access paths in shared spaces (#3408) 2026-09-18 22:00:02 +08:00
kb_access.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
kb_access_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
knowledge.go refactor: remove unused private Go helpers 2026-09-17 15:51:51 +08:00
knowledge_api_key_scope_test.go fix(api-keys): close KB scope gaps in handlers and CloneContext 2026-07-07 12:00:13 +08:00
knowledge_download.go feat(knowledge): add batch file downloads (#3300) 2026-09-16 15:40:10 +08:00
knowledge_download_test.go feat(knowledge): add batch file downloads (#3300) 2026-09-16 15:40:10 +08:00
knowledge_folder_test.go feat(knowledge): store folder path for folder uploads and expose folder tree API 2026-08-04 20:45:29 +08:00
knowledge_move_gate_test.go fix: reject cross-store reuse_vectors knowledge move 2026-06-05 15:00:41 +08:00
knowledge_mutation_admission_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
knowledge_ownership_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
knowledge_preview_security_test.go fix(security): harden outbound requests and file previews 2026-07-02 19:15:46 +08:00
knowledge_spans_test.go fix(knowledge): recover tasks safely after server restart 2026-07-14 17:55:26 +08:00
knowledge_tag_ids_test.go fix(knowledge): harden multi-tag data integrity and UX follow-ups 2026-06-26 10:52:52 +08:00
knowledge_transfer_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
knowledgebase.go feat(knowledge): derive knowledge base descriptions from document profiles (#3370) 2026-09-17 22:21:55 +08:00
knowledgebase_copy_preflight_test.go Simplify KB duplicate API and improve naming/docs coverage. 2026-07-08 20:55:54 +08:00
knowledgebase_hybrid_search_resource_urls_test.go feat(api): honor resource_urls=public on hybrid-search 2026-08-26 17:40:54 +08:00
knowledgebase_hybrid_search_test.go feat(api): honor resource_urls=public on hybrid-search 2026-08-26 17:40:54 +08:00
knowledgebase_not_found_test.go feat(memory): add cross-session long-term memory 2026-08-13 18:00:02 +08:00
knowledgebase_pr3_response_test.go feat(knowledge-base): validate vector store bindings on create, copy, and delete 2026-05-18 15:58:46 +08:00
knowledgebase_pr5_list_test.go feat: expose KB ↔ vector store binding in list, editor, and detail UI 2026-05-22 17:40:10 +08:00
knowledgebase_request_test.go feat(knowledge-base): validate vector store bindings on create, copy, and delete 2026-05-18 15:58:46 +08:00
list_pagination.go fix(handler): validate knowledge search pagination 2026-08-24 14:16:35 +08:00
list_pagination_test.go fix(handler): validate knowledge search pagination 2026-08-24 14:16:35 +08:00
mcp_credentials.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
mcp_endpoint.go feat(mcp): add built-in MCP server with per-workspace endpoints (#3371) 2026-09-17 22:40:05 +08:00
mcp_metadata.go feat(mcp)!: persist tool catalogs and add guided configuration (#3118) 2026-09-09 13:34:56 +08:00
mcp_metadata_test.go feat(mcp)!: persist tool catalogs and add guided configuration (#3118) 2026-09-09 13:34:56 +08:00
mcp_oauth.go fix(mcp): coordinate OAuth token refresh and authorization polling 2026-07-23 15:35:24 +08:00
mcp_service.go fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475) 2026-09-21 13:02:34 +08:00
mcp_tool_approval_test.go feat(mcp): add per-tool enable toggle (#2989) 2026-09-05 17:19:20 +08:00
mcp_usage_instructions.go feat(mcp)!: streamline setup and use short routing handles 2026-09-09 16:11:52 +08:00
mcp_usage_instructions_test.go feat(mcp)!: streamline setup and use short routing handles 2026-09-09 16:11:52 +08:00
me_env_var.go feat(sandbox): inject per-user and workspace skill env vars at execution 2026-08-27 14:32:00 +08:00
me_env_var_test.go feat(sandbox): inject per-user and workspace skill env vars at execution 2026-08-27 14:32:00 +08:00
memory.go fix(memory): preserve extraction progress and atomic memory updates (#3224) 2026-09-13 00:04:16 +08:00
memory_consistency_test.go fix(memory): preserve extraction progress and atomic memory updates (#3224) 2026-09-13 00:04:16 +08:00
message.go fix(api): address resource_urls review follow-ups 2026-08-05 14:42:51 +08:00
message_resource_urls_test.go fix(api): close the resource_urls=public gaps found in review 2026-08-05 14:42:51 +08:00
message_session_not_found_test.go fix(chat): stabilize history pagination and keep message order 2026-05-20 21:03:39 +08:00
message_suggestion.go fix(sessions): 放行 embed 运行时读取自身会话,修复追问建议 500 与刷新丢对话 2026-07-30 17:51:34 +08:00
message_suggestion_test.go fix(sessions): 放行 embed 运行时读取自身会话,修复追问建议 500 与刷新丢对话 2026-07-30 17:51:34 +08:00
model.go refactor(asr): move the wire format into protocol packages, and cover every vendor whose docs offer ASR (#3503) 2026-09-21 13:25:27 +08:00
model_catalog.go refactor(embedding): move the wire formats into protocol packages, and correct the vendor facts (#3500) 2026-09-21 11:55:38 +08:00
model_catalog_visibility_test.go refactor(models): rebuild vendor integration on protocol + catalog + vendor layers (#3470) 2026-09-20 21:28:23 +08:00
model_credentials.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
model_secret_extra_test.go refactor(models): rebuild vendor integration on protocol + catalog + vendor layers (#3470) 2026-09-20 21:28:23 +08:00
model_test.go refactor(models): rebuild vendor integration on protocol + catalog + vendor layers (#3470) 2026-09-20 21:28:23 +08:00
organization.go fix(security): close the low-severity shared-space findings (#3416) 2026-09-18 22:58:46 +08:00
organization_discovery_security_test.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
organization_invite_test.go fix(security): close the low-severity shared-space findings (#3416) 2026-09-18 22:58:46 +08:00
organization_share_path_test.go fix(security): close cross-workspace access paths in shared spaces (#3408) 2026-09-18 22:00:02 +08:00
rbac_lookups.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
rbac_lookups_test.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
sandbox_check.go feat(sandbox): add tenant-configurable network policy (#2995) 2026-09-03 10:49:46 +08:00
sandbox_check_test.go feat(sandbox): add tenant-configurable network policy (#2995) 2026-09-03 10:49:46 +08:00
sandbox_config.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
sandbox_config_test.go feat(sandbox): pin Cube DNS on templates and freeze skill snapshot configs 2026-08-26 16:56:26 +08:00
sandbox_skill.go fix(skills): upgrade outdated sandbox skills without rolling back the catalog or taking them offline (#3406) 2026-09-18 20:27:43 +08:00
sandbox_skill_env_test.go feat(sandbox): inject per-user and workspace skill env vars at execution 2026-08-27 14:32:00 +08:00
sandbox_skill_test.go fix(skills): verify runtime prerequisites and support installer steering 2026-09-09 23:04:59 +08:00
shared_agent_access.go fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475) 2026-09-21 13:02:34 +08:00
shared_agent_access_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
shared_agent_picker_test.go fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475) 2026-09-21 13:02:34 +08:00
skill_catalog.go refactor(skills): enhance skill upload size management and error handling 2026-09-02 15:08:05 +08:00
skill_catalog_test.go fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475) 2026-09-21 13:02:34 +08:00
skill_handler.go fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475) 2026-09-21 13:02:34 +08:00
skill_handler_test.go fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475) 2026-09-21 13:02:34 +08:00
storage_allowlist.go fix(storage): align default provider fallback with STORAGE_ALLOW_LIST 2026-07-15 11:05:00 +08:00
storagebackend.go feat(storage): add storage backend management API and UI enhancements 2026-07-15 14:54:39 +08:00
system.go refactor(system): drop idempotent flag from create-user response body 2026-09-07 17:30:47 +08:00
system_admin_audit_test.go test(system): cover system.* audit constants + emitAdminAudit contract 2026-05-26 21:13:56 +08:00
system_parser_check_test.go fix(security): close init config and parser-check redaction gaps 2026-07-02 14:41:42 +08:00
system_password_reset_test.go feat(system): add admin password reset with session revocation 2026-07-13 20:11:00 +08:00
system_runtime_test.go fix(knowledge): recover stranded deleting rows and align the KB count (#3367) 2026-09-18 00:55:44 +08:00
system_storage_engine_test.go feat(storage): improve storage connectivity error handling and multi-instance support 2026-07-15 14:54:39 +08:00
system_storage_ssrf_test.go fix(security): close remaining GHSA gaps from security triage 2026-07-02 15:03:53 +08:00
system_user_create_test.go refactor(system): drop idempotent flag from create-user response body 2026-09-07 17:30:47 +08:00
tag.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
tag_delete_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
task_progress_auth.go fix(security): enforce KB access on wiki reads and related IDOR paths 2026-07-02 12:06:22 +08:00
task_progress_auth_test.go fix(security): enforce KB access on wiki reads and related IDOR paths 2026-07-02 12:06:22 +08:00
tenant.go feat(knowledge): derive knowledge base descriptions from document profiles (#3370) 2026-09-17 22:21:55 +08:00
tenant_api_key_validation_test.go feat: support updating API key configuration 2026-08-12 11:43:03 +08:00
tenant_invitation.go fix(invitation): harden auto-accept follow-ups from code review 2026-08-10 15:47:29 +08:00
tenant_invitation_accept_by_token_test.go feat: 邀请链接在 invite_only 模式下改为登录后加入空间 2026-08-10 14:57:38 +08:00
tenant_invitation_auto_accept_test.go fix(invitation): harden auto-accept follow-ups from code review 2026-08-10 15:47:29 +08:00
tenant_invite_link.go fix: prevent API keys from assigning owner role 2026-07-23 16:04:42 +08:00
tenant_member.go fix(invitation): harden auto-accept follow-ups from code review 2026-08-10 15:47:29 +08:00
tenant_member_test.go feat(memory): add cross-session long-term memory 2026-08-13 18:00:02 +08:00
tenant_parser_ssrf_test.go fix(security): redact IM list credentials and harden MinerU SSRF 2026-07-02 17:58:20 +08:00
tenant_policy.go feat(auth): support tenantless provisioning and gated self-service workspace creation 2026-07-13 18:13:51 +08:00
tenant_secret_disclosure_test.go refactor(api-keys): remove API key reset endpoint and update documentation 2026-07-07 12:00:13 +08:00
tenant_self_service_policy_test.go feat: support complex password (#2929) 2026-09-01 11:50:10 +08:00
upload_limit.go refactor(skills): improve skill bundle size handling and error management 2026-09-02 15:08:05 +08:00
upload_limit_test.go refactor(skills): improve skill bundle size handling and error management 2026-09-02 15:08:05 +08:00
user_resource_favorite.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
vectorstore.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
web_search.go docs(api): add and fix swag annotations on handlers 2026-05-12 13:16:58 +08:00
web_search_provider.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
web_search_provider_credentials.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
wechat_qrcode.go docs(api): add and fix swag annotations on handlers 2026-05-12 13:16:58 +08:00
weknoracloud.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
wiki_page.go fix(security): close the low-severity shared-space findings (#3416) 2026-09-18 22:58:46 +08:00