WeKnora/internal/sandbox
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
..
admission feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
allocation feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
desktopscripts feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
workspace feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
capabilities.go feat(sandbox): run unpinned Lite sessions on the host OS sandbox (#3549) 2026-09-22 19:35:03 +08:00
check_client_guard_test.go feat(sandbox): 沙箱后端改为按空间配置,并补齐 E2B 模板与检测诊断 2026-08-11 15:42:02 +08:00
command_output.go feat(sandbox): stream live shell and install command output 2026-09-12 10:16:39 +08:00
config_identity.go feat(sandbox): Docker 后端支持会话级长驻容器与远程守护进程 2026-08-24 12:22:58 +08:00
config_identity_test.go feat(sandbox): remove the Local host-process backend 2026-08-26 17:48:21 +08:00
config_required.go feat(sandbox): run unpinned Lite sessions on the host OS sandbox (#3549) 2026-09-22 19:35:03 +08:00
config_sandboxes.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
config_sandboxes_test.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
cube_dns.go feat(sandbox): pin Cube DNS on templates and freeze skill snapshot configs 2026-08-26 16:56:26 +08:00
cube_dns_test.go feat(sandbox): pin Cube DNS on templates and freeze skill snapshot configs 2026-08-26 16:56:26 +08:00
cube_egress_integration_test.go feat(sandbox): pin Cube DNS on templates and freeze skill snapshot configs 2026-08-26 16:56:26 +08:00
cube_integration_test.go feat(sandbox): add tenant-configurable network policy (#2995) 2026-09-03 10:49:46 +08:00
cube_mock_test.go perf(sandbox): reduce redundant remote calls 2026-09-17 17:47:24 +08:00
cube_remote_client.go perf(sandbox): reduce redundant remote calls 2026-09-17 17:47:24 +08:00
cube_remote_client_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
cube_template_catalog_test.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
cube_terminal.go feat(sandbox): add an interactive terminal and files tab beside chat (#3110) 2026-09-08 17:43:15 +08:00
cube_terminal_test.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
desktop.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
desktop_scripts.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
desktop_test.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
docker_enabled.go feat: require opt-in for the Docker sandbox backend (#2936) 2026-09-01 15:42:28 +08:00
docker_enabled_test.go feat: require opt-in for the Docker sandbox backend (#2936) 2026-09-01 15:42:28 +08:00
docker_engine.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
docker_host.go feat(sandbox): Docker 后端支持会话级长驻容器与远程守护进程 2026-08-24 12:22:58 +08:00
docker_host_test.go feat(sandbox): Docker 后端支持会话级长驻容器与远程守护进程 2026-08-24 12:22:58 +08:00
docker_idle_sweeper.go feat(sandbox): run sandboxed execs as root by default (#3067) 2026-09-07 13:03:35 +08:00
docker_idle_sweeper_test.go feat(sandbox): run sandboxed execs as root by default (#3067) 2026-09-07 13:03:35 +08:00
docker_integration_test.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
docker_remote_client.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
docker_remote_client_test.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
docker_rpc_timeout.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
docker_snapshot.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
docker_snapshot_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
docker_template_catalog.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
docker_terminal.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
docker_terminal_test.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
e2b_compatible_integration_test.go feat(sandbox): remove the Local host-process backend 2026-08-26 17:48:21 +08:00
e2b_exec_timeout_test.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
e2b_integration_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
e2b_remote_client.go fix(sandbox): treat E2B in_progress template builds as building (#4108) 2026-10-10 11:28:44 +08:00
e2b_remote_client_test.go fix(sandbox): treat E2B in_progress template builds as building (#4108) 2026-10-10 11:28:44 +08:00
e2b_rpc_timeout.go feat(agent): unify sandbox tools and drop host preloaded skills (#3033) 2026-09-05 16:45:59 +08:00
e2b_terminal.go feat(sandbox): add an interactive terminal and files tab beside chat (#3110) 2026-09-08 17:43:15 +08:00
envd_compat_transport.go feat(agent): unify sandbox tools and drop host preloaded skills (#3033) 2026-09-05 16:45:59 +08:00
envd_compat_transport_test.go feat(sandbox): run sandboxed execs as root by default (#3067) 2026-09-07 13:03:35 +08:00
filesystem_identity.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
filesystem_identity_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
gateway_transport.go feat(security): add a whitelist-only outbound mode that refuses before DNS (#3393) 2026-09-23 12:02:06 +08:00
gateway_transport_test.go feat(sandbox): add tenant-configurable network policy (#2995) 2026-09-03 10:49:46 +08:00
inbound_token_registry.go fix(sandbox): always require inbound credentials 2026-09-03 13:34:40 +08:00
inbound_token_registry_test.go feat(sandbox): add tenant-configurable network policy (#2995) 2026-09-03 10:49:46 +08:00
langfuse.go perf(sandbox): reduce redundant remote calls 2026-09-17 17:47:24 +08:00
langfuse_test.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
manager.go feat(sandbox): remove the Local host-process backend 2026-08-26 17:48:21 +08:00
mkdir.go feat(agent): unify sandbox tools and drop host preloaded skills (#3033) 2026-09-05 16:45:59 +08:00
mkdir_test.go feat(agent): unify sandbox tools and drop host preloaded skills (#3033) 2026-09-05 16:45:59 +08:00
orphan_reaper.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
orphan_reaper_test.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
pty_input_coalesce.go feat(sandbox): add an interactive terminal and files tab beside chat (#3110) 2026-09-08 17:43:15 +08:00
pty_input_coalesce_test.go feat(sandbox): add an interactive terminal and files tab beside chat (#3110) 2026-09-08 17:43:15 +08:00
remote_client.go fix(sandbox): omit env overrides when creating Aliyun E2B sandboxes from snapshots (#4054) 2026-10-09 14:24:54 +08:00
remote_client_test.go feat(sandbox): add tenant-configurable network policy (#2995) 2026-09-03 10:49:46 +08:00
remote_errors.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
remote_errors_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
remote_fake_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
remote_sandbox.go feat(agent): write workspace scripts and run them with installed skills 2026-08-29 22:45:18 +08:00
remote_sandbox_test.go feat(agent): write workspace scripts and run them with installed skills 2026-08-29 22:45:18 +08:00
remote_snapshot_test.go fix(sandbox): keep skill image pointer out of sandbox config PUT 2026-08-24 16:00:23 +08:00
runtime_defaults.go feat(sandbox): Docker 后端支持会话级长驻容器与远程守护进程 2026-08-24 12:22:58 +08:00
sandbox.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
sandbox_test.go feat(sandbox): run unpinned Lite sessions on the host OS sandbox (#3549) 2026-09-22 19:35:03 +08:00
session_binding.go feat(session): in-place rewind with workspace checkpoint reset (#3469) 2026-09-20 21:01:00 +08:00
session_binding_redis.go feat(session): in-place rewind with workspace checkpoint reset (#3469) 2026-09-20 21:01:00 +08:00
session_binding_redis_test.go feat(session): in-place rewind with workspace checkpoint reset (#3469) 2026-09-20 21:01:00 +08:00
session_binding_tenant_test.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
session_binding_test.go feat(session): in-place rewind with workspace checkpoint reset (#3469) 2026-09-20 21:01:00 +08:00
session_bootstrapper.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
session_bootstrapper_test.go fix(sandbox): omit env overrides when creating Aliyun E2B sandboxes from snapshots (#4054) 2026-10-09 14:24:54 +08:00
session_connect.go perf(sandbox): reduce redundant remote calls 2026-09-17 17:47:24 +08:00
session_connect_test.go perf(sandbox): reduce redundant remote calls 2026-09-17 17:47:24 +08:00
session_file_operation.go perf(sandbox): reduce redundant remote calls 2026-09-17 17:47:24 +08:00
session_file_operation_test.go perf(sandbox): reduce redundant remote calls 2026-09-17 17:47:24 +08:00
session_lifecycle.go fix(sandbox): omit env overrides when creating Aliyun E2B sandboxes from snapshots (#4054) 2026-10-09 14:24:54 +08:00
session_lifecycle_test.go feat(session): fork conversations from a historical user message (#3304) 2026-09-17 14:20:18 +08:00
session_manager.go feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
session_manager_desktop_test.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
session_manager_terminal_test.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
session_manager_test.go fix(sandbox): omit env overrides when creating Aliyun E2B sandboxes from snapshots (#4054) 2026-10-09 14:24:54 +08:00
session_shell_snapshot_test.go perf(sandbox): reduce redundant remote calls 2026-09-17 17:47:24 +08:00
shell_quote.go feat(sandbox): add tenant skill install and remove service layer 2026-08-24 17:51:13 +08:00
shell_quote_test.go feat(sandbox): add tenant skill install and remove service layer 2026-08-24 17:51:13 +08:00
skill_image.go feat(sandbox): remove the Local host-process backend 2026-08-26 17:48:21 +08:00
skill_paths.go feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
skill_paths_test.go feat(lite): install and run skills on the host (#3661) 2026-09-24 21:30:34 +08:00
template_catalog.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
template_catalog_test.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
tenant_config.go fix(sandbox): omit env overrides when creating Aliyun E2B sandboxes from snapshots (#4054) 2026-10-09 14:24:54 +08:00
tenant_config_test.go fix(sandbox): omit env overrides when creating Aliyun E2B sandboxes from snapshots (#4054) 2026-10-09 14:24:54 +08:00
tenant_resolver.go feat(security): add a whitelist-only outbound mode that refuses before DNS (#3393) 2026-09-23 12:02:06 +08:00
tenant_resolver_test.go feat: require opt-in for the Docker sandbox backend (#2936) 2026-09-01 15:42:28 +08:00
terminal.go feat(sandbox): Docker 后端支持交互式终端 (#4023) 2026-10-10 21:38:03 +08:00
terminal_test.go feat(sandbox): add an interactive terminal and files tab beside chat (#3110) 2026-09-08 17:43:15 +08:00
terminal_ttl_test.go feat(sandbox): add an interactive terminal and files tab beside chat (#3110) 2026-09-08 17:43:15 +08:00
url_guard.go feat(security): add a whitelist-only outbound mode that refuses before DNS (#3393) 2026-09-23 12:02:06 +08:00
url_guard_test.go fix: share IP classification between SSRF and sandbox URL guards 2026-09-01 15:35:14 +08:00
url_guard_whitelist_only_test.go feat(security): add a whitelist-only outbound mode that refuses before DNS (#3393) 2026-09-23 12:02:06 +08:00
validator.go perf(sandbox): compile command-substitution regexps once 2026-06-16 19:51:32 +08:00
validator_test.go feat(sandbox) (#2301) 2026-08-10 21:28:20 +08:00
websocket_dialer.go feat(security): add a whitelist-only outbound mode that refuses before DNS (#3393) 2026-09-23 12:02:06 +08:00
websocket_dialer_test.go feat(sandbox): add a graphical desktop to session sandboxes (#3245) 2026-09-14 20:37:37 +08:00
workspace_failure_test.go feat(agent): unify sandbox tools and drop host preloaded skills (#3033) 2026-09-05 16:45:59 +08:00
workspace_layout.go feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00
workspace_layout_test.go feat(sandbox): run unpinned Lite sessions on the host OS sandbox (#3549) 2026-09-22 19:35:03 +08:00
workspace_path.go feat(sandbox): 双模式沙箱分配的基础(门控关闭,线上行为不变) (#4507) 2026-10-20 12:28:30 +08:00