WeKnora/internal/middleware
nullptr-error 8dab7550f5
feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507)
* refactor(sandbox): move workspace layout and path rules into a leaf package

internal/sandbox/workspace owns the remote layout (root, input, output,
GitDir, skills root) and the lexical path rules. The sandbox root package
re-exports them through aliases, so callers keep their imports. No
behaviour change.

* feat(sandbox): add the allocation domain model

Pure types for placing a session: session/user mode, the user-mode
admission whitelist (web account or API external user only, with a fixed
fallback reason otherwise), the shared-machine identity and its digest,
the empty-retention bounds and the deployment gate.

* feat(sandbox): add machine and session admission leases

Registry of in-flight operations per shared machine and per session, with
Redis and in-memory implementations. Leases renew only while still live,
report loss at a local deadline measured from the request send time, and
Bind cancels a context when any held lease or barrier is lost. Holding or
renewing only ever extends the scope key's lifetime.

* feat(sandbox): add allocation_mode and empty retention to sandbox configs

Configs gain allocation_mode (session|user, default session) and
user_sandbox_empty_retention_days (0 = default 7, up to 90). Updates that
omit them keep the stored values. Switching a config to user requires the
deployment gate (WEKNORA_SANDBOX_USER_ALLOCATION plus Redis), exposed as
the settings.sandbox.user_allocation capability.

The gate stays closed in this build regardless of the env var: the session
manager does not place sessions by their assignment yet, so a user-mode
placement would be recorded for a session that still runs on its own
machine.

* feat(session): record the server-verified owner auth source

sessions.owner_auth_source records how user_id was authenticated when the
session was created or its owner first bound: web, API direct header or
signed token, API key, MCP endpoint, embed, IM, internal. The auth
middleware puts the source on the context, CreateSession stamps it and
ignores client input, owner binding (MCP ask, embed backfill) records the
entry's own source, and a fork inherits it. Rows from before the column
stay empty, which means unknown.

Per-user sandbox allocation reads it to tell a verified person from a
shared entry; the owner's kind follows from the source and user_id, so it
is not stored separately.

* feat(sandbox): persist session placements and shared-machine registry

session_sandbox_assignments records each session's placement once: its
config pair, the shared machine digest ('' for a machine of its own),
when it was released and when its directory cleanup is due. Mode and
layout follow from the digest. sandbox_user_machines holds the identity
behind each digest and the empty-retention clock.

ClaimPlacement writes the assignment and projects the session pin in one
transaction that locks the session row, then the machine row, before
inserting. RetireSessions releases assignments under the same locks and
writes a tombstone for a session that was never placed, so no placement
lands after a deletion began. The repository is also exercised on the
schema the shipped SQLite migrations create.

* feat(sandbox): place sessions through the allocation service

sandboxalloc decides a session's placement on first remote use and records
it with the pin: sessions that already used a per-session sandbox stay on
it, maintenance sessions are always dedicated, and only an eligible owner
on a user-mode config with the gate open shares a machine. A placed
session resolves straight from its assignment; a released one is refused.
Skill maintenance sessions record a maintenance placement and are removed
if that fails.

* fix(sandbox): retry placement when the sandbox config cannot be read

A failed config load was stored as session mode, so the session could never join a shared machine after the error cleared.

* fix(sandbox): satisfy golangci-lint on the allocation foundations

Wrap lines past 120 characters, comment exported names, and clear the two staticcheck findings so the pull request lint job can pass.

---------

Co-authored-by: hailongzhao <hailongzhao@tencent.com>
2026-10-20 12:28:30 +08:00
..
asynqdl fix(asynqdl): truncate dead-letter errors on a rune boundary (#3964) 2026-10-08 01:28:59 +08:00
access.go feat(assistant): side panel, access levels and role-aware operations (#4342) 2026-10-15 18:18:22 +08:00
access_test.go refactor(tenant_member): simplify tenant ID resolution in handlers 2026-05-18 17:28:58 +08:00
api_key_gate.go feat(api-keys): add platform-scoped API keys for control-plane automation 2026-07-21 17:21:35 +08:00
api_key_gate_test.go feat(api-keys): add platform-scoped API keys for control-plane automation 2026-07-21 17:21:35 +08:00
audit_provider.go feat(rbac): per-tenant audit log for RBAC events (PR 6, #1303) 2026-05-18 17:28:58 +08:00
auth.go feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
auth_api_principal_test.go feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
auth_context.go feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
auth_context_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
auth_internal_dispatch_test.go feat(agent): add the built-in WeKnora assistant with an operation catalog (#4302) 2026-10-15 12:18:30 +08:00
auth_platform_api_key_test.go feat(api-keys): add platform-scoped API keys for control-plane automation 2026-07-21 17:21:35 +08:00
auth_public_ratelimit.go feat(embed): add backend embed channels with auth and rate limiting (#1657) 2026-06-12 23:10:39 +08:00
auth_role_test.go refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
auth_tenantless_test.go refactor(auth): unify OIDC provisioning under shared default_tenant_mode policy 2026-07-13 19:14:49 +08:00
embed_auth.go feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
embed_auth_test.go feat(assistant): expand management operations and polish interaction UI (#4427) 2026-10-17 14:35:58 +08:00
error_handler.go init commit 2025-08-05 15:08:07 +08:00
error_handler_model_usage_test.go fix(models): show usage details when deletion is blocked (#2975) 2026-09-02 17:20:37 +08:00
kb_access.go fix(security): close the low-severity shared-space findings (#3416) 2026-09-18 22:58:46 +08:00
kb_access_test.go fix(security): close the low-severity shared-space findings (#3416) 2026-09-18 22:58:46 +08:00
language.go fix(i18n): use WEKNORA_LANGUAGE env for prompt language fallback instead of hardcoded zh-CN 2026-04-02 12:01:25 +08:00
logger.go feat(mcp): let callers without a browser obtain MCP OAuth tokens (#4229) 2026-10-13 16:37:24 +08:00
logger_test.go feat(mcp): let callers without a browser obtain MCP OAuth tokens (#4229) 2026-10-13 16:37:24 +08:00
mcp_endpoint_auth.go feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
mcp_endpoint_auth_test.go feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
multipart_cleanup.go fix(upload): clean multipart temporary files 2026-09-22 17:40:56 +08:00
multipart_cleanup_test.go fix(upload): clean multipart temporary files 2026-09-22 17:40:56 +08:00
rbac.go feat(assistant): side panel, access levels and role-aware operations (#4342) 2026-10-15 18:18:22 +08:00
rbac_api_key_shortcircuit_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
rbac_audit_test.go refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
rbac_test.go feat(models): let system admins edit builtin model config 2026-07-14 15:56:02 +08:00
recovery.go feat(memory): add cross-session long-term memory 2026-08-13 18:00:02 +08:00
ws_auth.go feat(sandbox): add an interactive terminal and files tab beside chat (#3110) 2026-09-08 17:43:15 +08:00