@allbluecn/web-app 0.27.23 → 0.27.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allbluecn/web-app",
3
- "version": "0.27.23",
3
+ "version": "0.27.24",
4
4
  "license": "AGPL-3.0-or-later",
5
5
  "type": "module",
6
6
  "files": [
@@ -112,7 +112,7 @@ export const SDK_GROUPS: SdkGroup[] = [
112
112
  "items": [
113
113
  {
114
114
  "name": "@allbluecn/web-app",
115
- "version": "v0.27.23",
115
+ "version": "v0.27.24",
116
116
  "lastUpdate": "Nov 10, 2026",
117
117
  "source": "apps/web/package.json",
118
118
  "docsUrl": "https://github.com/allbluecn/allblue"
@@ -25,6 +25,11 @@ vi.mock('@allbluecn/database', () => ({
25
25
  vi.mock('@/lib/session', () => ({ getCurrentSession }))
26
26
 
27
27
  import { harnessAccess } from '../access.server'
28
+ const syncMode = vi.fn(async () => {})
29
+ vi.mock('@/server/harness/permission-mode.server', () => ({
30
+ syncHarnessPermissionMode: (...args: unknown[]) => syncMode(...args),
31
+ }))
32
+
28
33
  import { trustedAcceptInput } from '../trusted-context.server'
29
34
  import { resolveToolIdentity } from '../tool-identity.server'
30
35
 
@@ -98,6 +103,8 @@ describe('harness 访问控制(T2.3 access.server)', () => {
98
103
  describe('可信上下文层(T2.3 trusted-context.server,安全用例)', () => {
99
104
  beforeEach(() => {
100
105
  findManyKb.mockReset()
106
+ syncMode.mockReset()
107
+ syncMode.mockImplementation(async () => {})
101
108
  })
102
109
 
103
110
  it('注入 context.userId=他人 → 被忽略,userId 恒取 access 层身份', async () => {
@@ -169,6 +176,35 @@ describe('可信上下文层(T2.3 trusted-context.server,安全用例)', (
169
176
  })
170
177
  })
171
178
 
179
+ it('权限 mode 对账:prompt 回合触发 syncHarnessPermissionMode(接线断言)', async () => {
180
+ const accept = trustedAcceptInput()
181
+ await accept(
182
+ { op: 'prompt', message: 'hi', context: {} },
183
+ { user: { id: 'u1' }, threadId: 't-9' },
184
+ )
185
+ expect(syncMode).toHaveBeenCalledWith('t-9', { id: 'u1' })
186
+ })
187
+
188
+ it('权限 mode 对账失败 → 只告警不阻断回合(fail-safe)', async () => {
189
+ syncMode.mockImplementation(async () => {
190
+ throw new Error('host down')
191
+ })
192
+ const warn = console.warn
193
+ console.warn = () => {}
194
+ try {
195
+ const accept = trustedAcceptInput()
196
+ const result = await accept(
197
+ { op: 'prompt', message: 'hi', context: { userId: 'victim' } },
198
+ { user: { id: 'u1' }, threadId: 't-9' },
199
+ )
200
+ expect(result).not.toHaveProperty('refuse')
201
+ const ctx = (result as { context: Record<string, unknown> }).context
202
+ expect(ctx.userId).toBe('u1') // 覆写仍生效
203
+ } finally {
204
+ console.warn = warn
205
+ }
206
+ })
207
+
172
208
  describe('resolveToolIdentity(T2.3 tool-identity.server)', () => {
173
209
  it('harness 路径:有 run → 只信 run.user.id(忽略 context.userId)', () => {
174
210
  const id = resolveToolIdentity(
@@ -0,0 +1,126 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // 权限 mode 对账(applyHarnessPermissionMode 零调用方缺口修复):
6
+ // diff → open + setPluginSetting('mode') + 记 indexMetadata;记录匹配跳过;
7
+ // 门控关闭直接返回(legacy 路径不经 harness mode)。DB 策略档非法回落默认档。
8
+
9
+ // @vitest-environment node
10
+
11
+ import { beforeEach, describe, expect, it, vi } from 'vitest'
12
+
13
+ const gateState = { enabled: false }
14
+ vi.mock('@/server/harness/gate.server', () => ({
15
+ isHarnessHostEnabled: () => gateState.enabled,
16
+ }))
17
+
18
+ const prismaState = {
19
+ thread: null as null | { sandboxPolicy: string; indexMetadata: unknown },
20
+ updated: [] as Array<Record<string, unknown>>,
21
+ }
22
+ vi.mock('@allbluecn/database', () => ({
23
+ prisma: {
24
+ chatThread: {
25
+ findUnique: vi.fn(async () => prismaState.thread),
26
+ update: vi.fn(async ({ data }: { data: Record<string, unknown> }) => {
27
+ prismaState.updated.push(data)
28
+ return data
29
+ }),
30
+ },
31
+ },
32
+ }))
33
+
34
+ const hostCalls = {
35
+ open: 0,
36
+ settings: [] as Array<{ key: string; value: unknown }>,
37
+ }
38
+ vi.mock('@/server/harness/host.server', () => ({
39
+ getHarnessHost: () => ({
40
+ open: async () => {
41
+ hostCalls.open += 1
42
+ return {
43
+ setPluginSetting: async (change: { key: string; value: unknown }) => {
44
+ hostCalls.settings.push(change)
45
+ return {}
46
+ },
47
+ close: async () => {},
48
+ }
49
+ },
50
+ }),
51
+ }))
52
+ vi.mock('@/server/harness/define.server', () => ({
53
+ allblueHarness: {},
54
+ }))
55
+
56
+ import { sandboxPolicyToPermissionMode, syncHarnessPermissionMode } from '../permission-mode.server'
57
+
58
+ const USER = { id: 'u1' }
59
+
60
+ beforeEach(() => {
61
+ gateState.enabled = true
62
+ hostCalls.open = 0
63
+ hostCalls.settings = []
64
+ prismaState.thread = null
65
+ prismaState.updated = []
66
+ })
67
+
68
+ describe('syncHarnessPermissionMode(档位变更 / 每回合对账)', () => {
69
+ it('diff(readonly 无记录)→ open + setPluginSetting(plan) + 记 indexMetadata', async () => {
70
+ prismaState.thread = { sandboxPolicy: 'readonly', indexMetadata: null }
71
+
72
+ await syncHarnessPermissionMode('t1', USER)
73
+
74
+ expect(hostCalls.open).toBe(1)
75
+ expect(hostCalls.settings).toMatchObject([{ key: 'mode', value: 'plan' }])
76
+ expect(prismaState.updated).toHaveLength(1)
77
+ // mock 的 update 直接返回 data(未包 {data} 包装)
78
+ expect(prismaState.updated[0]).toEqual({
79
+ indexMetadata: { harnessPermissionMode: 'plan' },
80
+ })
81
+ })
82
+
83
+ it('三档映射:workspace-write→default、full→acceptEdits', async () => {
84
+ prismaState.thread = { sandboxPolicy: 'full', indexMetadata: null }
85
+ await syncHarnessPermissionMode('t1', USER)
86
+ expect(hostCalls.settings).toMatchObject([{ key: 'mode', value: 'acceptEdits' }])
87
+
88
+ hostCalls.settings = []
89
+ prismaState.thread = { sandboxPolicy: 'workspace-write', indexMetadata: null }
90
+ prismaState.updated = []
91
+ await syncHarnessPermissionMode('t2', USER)
92
+ expect(hostCalls.settings).toMatchObject([{ key: 'mode', value: 'default' }])
93
+ })
94
+
95
+ it('记录匹配 → 零开销跳过(不 open、不写 DB)', async () => {
96
+ prismaState.thread = {
97
+ sandboxPolicy: 'readonly',
98
+ indexMetadata: { harnessPermissionMode: 'plan' },
99
+ }
100
+
101
+ await syncHarnessPermissionMode('t1', USER)
102
+
103
+ expect(hostCalls.open).toBe(0)
104
+ expect(prismaState.updated).toHaveLength(0)
105
+ })
106
+
107
+ it('门控关闭 → 直接返回(legacy 路径不触 harness)', async () => {
108
+ gateState.enabled = false
109
+ prismaState.thread = { sandboxPolicy: 'readonly', indexMetadata: null }
110
+
111
+ await syncHarnessPermissionMode('t1', USER)
112
+
113
+ expect(hostCalls.open).toBe(0)
114
+ expect(prismaState.updated).toHaveLength(0)
115
+ })
116
+
117
+ it('DB 策略档非法 → 回落默认档(fail-closed,不取更宽档)', async () => {
118
+ prismaState.thread = { sandboxPolicy: 'bogus', indexMetadata: null }
119
+
120
+ await syncHarnessPermissionMode('t1', USER)
121
+
122
+ expect(hostCalls.settings).toMatchObject([
123
+ { key: 'mode', value: sandboxPolicyToPermissionMode('readonly') },
124
+ ])
125
+ })
126
+ })
@@ -19,6 +19,12 @@
19
19
  // 供 serverFn(sandbox-policy 档位变更)与会话打开路径调用。
20
20
 
21
21
  /** 产品三档(与 ChatThread.sandboxPolicy / composer SandboxPolicyControl 一致)。 */
22
+ import { prisma } from '@allbluecn/database'
23
+ import {
24
+ SANDBOX_DEFAULT_POLICY_LEVEL,
25
+ isSandboxPolicyLevel,
26
+ } from '@/server/ai-gateway/sandbox/policy'
27
+
22
28
  export type SandboxPolicyLevel = 'readonly' | 'workspace-write' | 'full'
23
29
 
24
30
  /** 官方 permissions mode(PERMISSION_MODES 去 bypass)。 */
@@ -60,3 +66,51 @@ export async function applyHarnessPermissionMode(
60
66
  await session.close()
61
67
  }
62
68
  }
69
+
70
+ /** ChatThread.indexMetadata 里记录「已施加到 harness log 的 mode」的键。 */
71
+ const APPLIED_MODE_KEY = 'harnessPermissionMode'
72
+
73
+ /**
74
+ * 权限 mode 对账:按 ChatThread.sandboxPolicy 的当前档施加 mode(若尚未施加)。
75
+ *
76
+ * 两处调用:
77
+ * 1. setSandboxPolicyFn(档位变更,DB 更新后):diff → open + setPluginSetting
78
+ * 2. trustedAcceptInput(每回合 prompt/steer/followUp):diff → 同上(新线程首次
79
+ * 回合、修复前建的老线程等场景兜底)
80
+ *
81
+ * 已施加记录存 indexMetadata.harnessPermissionMode:setPluginSetting 是 log 命令
82
+ * (官方 "applies at the next turn",随 log 持久,重启 replay 保持),记录匹配即
83
+ * 跳过 open(热路径零开销)。**门控关闭(web/SaaS legacy 路径)直接返回**——
84
+ * legacy 沙箱不经 harness mode,getHarnessHost 会抛错。
85
+ *
86
+ * 失败语义由调用方定:档位变更路径让错误上抛(用户显式操作必须可见失败);
87
+ * acceptInput 路径 catch 告警不阻断回合(mode 回退官方默认档,fail-safe)。
88
+ */
89
+ export async function syncHarnessPermissionMode(
90
+ threadId: string,
91
+ user: { id: string },
92
+ ): Promise<void> {
93
+ const { isHarnessHostEnabled } = await import('@/server/harness/gate.server')
94
+ if (!isHarnessHostEnabled()) return
95
+
96
+ const thread = await prisma.chatThread.findUnique({
97
+ where: { id: threadId },
98
+ select: { sandboxPolicy: true, indexMetadata: true },
99
+ })
100
+ if (!thread) return
101
+
102
+ const policy = isSandboxPolicyLevel(thread.sandboxPolicy)
103
+ ? thread.sandboxPolicy
104
+ : SANDBOX_DEFAULT_POLICY_LEVEL
105
+ const desired = sandboxPolicyToPermissionMode(policy)
106
+
107
+ const metadata = (thread.indexMetadata ?? {}) as Record<string, unknown>
108
+ if (metadata[APPLIED_MODE_KEY] === desired) return
109
+
110
+ await applyHarnessPermissionMode(threadId, policy, user)
111
+ // 记录已施加(last-write-wins:竞态最多多施加一次 setPluginSetting,幂等无害)
112
+ await prisma.chatThread.update({
113
+ where: { id: threadId },
114
+ data: { indexMetadata: { ...metadata, [APPLIED_MODE_KEY]: desired } },
115
+ })
116
+ }
@@ -77,8 +77,21 @@ function carriesContext(
77
77
  * 原样放行(其越权防护在各 handler 的 expose 白名单与 access 层)。
78
78
  */
79
79
  export function trustedAcceptInput(): AcceptInput<HarnessHostUser> {
80
- return async (input, { user }) => {
80
+ return async (input, { user, threadId }) => {
81
81
  if (!carriesContext(input)) return input
82
+
83
+ // 权限 mode 对账(applyHarnessPermissionMode 零调用方缺口修复):按线程当前
84
+ // sandboxPolicy 施加 mode(新线程首回合 / 老线程兜底),记录匹配则零开销跳过。
85
+ // 失败只告警不阻断回合——mode 回退官方默认档(fail-safe),档位变更路径会重试。
86
+ if (user) {
87
+ try {
88
+ const { syncHarnessPermissionMode } = await import('@/server/harness/permission-mode.server')
89
+ await syncHarnessPermissionMode(threadId, user)
90
+ } catch (err) {
91
+ console.warn('[harness] 权限 mode 对账失败(回合按官方默认档继续):', err)
92
+ }
93
+ }
94
+
82
95
  const trustedContext = await buildTrustedContext(user, input.context)
83
96
  // 整体覆写:客户端 context 不残留任何键
84
97
  return { ...input, context: trustedContext } as HarnessInput
@@ -85,5 +85,12 @@ export const setSandboxPolicyFn = createServerFn({ method: "POST" })
85
85
  select: { sandboxPolicy: true },
86
86
  })
87
87
 
88
+ // 施加权限 mode(applyHarnessPermissionMode 零调用方缺口修复):档位变更后
89
+ // 立即 open + setPluginSetting('mode')(log 命令持久,重启 replay 保持)+ 记
90
+ // indexMetadata。错误上抛——用户显式操作必须可见失败(尤其 full→readonly 收紧)。
91
+ // 门控关闭(web/SaaS legacy)时 sync 内部直接返回。
92
+ const { syncHarnessPermissionMode } = await import("@/server/harness/permission-mode.server")
93
+ await syncHarnessPermissionMode(thread.id, { id: context.user.id })
94
+
88
95
  return updated.sandboxPolicy
89
96
  })