diff --git a/docs/cli-reference.md b/docs/cli-reference.md index 3bfada46..33a7c6ef 100644 --- a/docs/cli-reference.md +++ b/docs/cli-reference.md @@ -73,9 +73,9 @@ moss config set language auto # 默认:仅当系统区域以 zh 开 交互界面里 `/language`(别名 `/lang`)切换本会话;`/language zh save` 写入用户配置。系统区域为中文且还没选过时,首次 `moss setup` 用一行提示:按 `e` 切换为英语。 -## Jev 意图识别(实验) +## Jev 运行时判定 -选择服务商并显式开启 shadow 模式。TypeSafe 和 OpenRouter 需要对应的 API Key。 +配置受信任的 Jev 服务后,Moss 默认启用意图识别和完成检查。TypeSafe 和 OpenRouter 需要对应的 API Key。默认服务缺少凭据时,Moss 可离线运行。设置 `MOSS_JEV_ENABLED=0` 可关闭所有 Jev 判定。 | `MOSS_JEV_PROVIDER` | 密钥环境变量 | 模型 | | ------------------- | -------------------------- | -------------------------------------------------------------------------------------------------- | @@ -100,7 +100,7 @@ SDK 宿主也可通过 `MossAgentConfig.observeUserIntent` 注入观察器,或 ### 单条任务中的 Jev 判定 -以下开关均从启动环境读取。默认 `off`。`shadow` 记录判定,`active` 将判定用于当前 run。 +以下开关均从启动环境读取。完成检查默认 `active`,其余判定默认 `off`。`shadow` 记录判定,`active` 将判定用于当前 run。 | 环境变量 | 触发场景 | | --------------------- | --------------------------------------------------------------------------- | diff --git a/docs/specs/moss-jev-runtime.md b/docs/specs/moss-jev-runtime.md index 095bebcc..641e9816 100644 --- a/docs/specs/moss-jev-runtime.md +++ b/docs/specs/moss-jev-runtime.md @@ -121,7 +121,7 @@ ``` - **协议校验**。读取 `answers[questionId]`。`choice` 校验选项在 `criteria` 中、所选项概率有限且位于 0 到 1。`noul` 校验对应分值范围。缺字段、未知选项、非 JSON 和 HTTP 错误进入对应降级流程。本规范用 `choice` 和 `noul` 表达全部决策。 -- **启用**。每个判定点独立支持 `off`、`shadow`、`active`。`shadow` 记录原本会采用的判定,沿用当前执行流程。`active` 才把有效结果交给 MOSS 的执行策略。新判定点默认 `off`,部署者可以按点选择模式。已有 Jev 意图开关继续覆盖输入和 steering。 +- **启用**。每个判定点独立支持 `off`、`shadow`、`active`。`shadow` 记录原本会采用的判定,沿用当前执行流程。`active` 才把有效结果交给 MOSS 的执行策略。受信任的服务可用时,输入与完成检查默认 `active`,其余判定点默认 `off`。部署者可逐项覆盖,并用 `MOSS_JEV_ENABLED=0` 总关闭。Jev 意图开关覆盖输入和 steering。 - **预算初值**。输出筛选最多等待 4 秒,其他判定最多等待 2 秒。每 run 最多 12 个请求,其中保留 1 个给收尾。每条用户消息、具体工具调用或同一结果片段批次按稳定 ID 去重。初始所选项概率阈值为 0.8。日志筛选中,分值不高于 0.2 的普通块才可省略,后续用实际日志样本校准。 - **出站数据**。按判定点构造最小 `state`,截断并复用现有凭据脱敏。从目录约束使用相对路径,从证据使用测试目标和结果。二进制内容、环境变量、原始工具参数和完整会话留在本地。日志只记模型、判定点、事件 ID、建议动作、采纳与否、降级原因及耗时。 diff --git a/scripts/verify/jev-runtime.mjs b/scripts/verify/jev-runtime.mjs index 5b235c56..29c97b52 100644 --- a/scripts/verify/jev-runtime.mjs +++ b/scripts/verify/jev-runtime.mjs @@ -9,6 +9,61 @@ import { execTool } from '../../dist/tools/builtin.js'; import { readFileTool, writeFileTool } from '../../dist/tools/file-tools.js'; import { runTestsTool } from '../../dist/tools/harness-tools.js'; +const configured = { + MOSS_JEV_SYSTEMONE_URL: 'https://jev.example.test/v1/systemone', + MOSS_JEV_RELAY_KEY: 'fixture-jev-key', +}; +const defaults = createJevIntentRuntime(configured, { + async fetch(url, init) { + assert.equal(url, configured.MOSS_JEV_SYSTEMONE_URL); + assert.equal(new Headers(init.headers).get('Authorization'), 'Bearer fixture-jev-key'); + assert.equal(JSON.parse(init.body).questions.completion.type, 'choice'); + return Response.json({ + answers: { + completion: { type: 'choice', choice: 'verify', probabilities: { verify: 0.99 } }, + }, + }); + }, +}); +assert.deepEqual(defaults.runtimeJudge?.modes, { + input: 'active', + completion: 'active', + approval: 'off', + constraint: 'off', + output: 'off', + progress: 'off', +}); +assert.equal( + ( + await defaults.runtimeJudge.ask( + {}, + { + completion: { + type: 'choice', + instructions: 'Check completion', + criteria: { verify: 'Verify' }, + }, + }, + new AbortController().signal + ) + )?.completion?.choice, + 'verify' +); +assert.deepEqual(createJevIntentRuntime({}), {}); +assert.deepEqual( + createJevIntentRuntime({ ...configured, MOSS_JEV_ENABLED: '0', MOSS_JEV_INTENT: 'active' }), + {} +); +assert.equal( + createJevIntentRuntime({ ...configured, MOSS_JEV_INTENT: 'off' }).runtimeJudge?.modes.input, + 'off' +); +assert.equal( + createJevIntentRuntime({ ...configured, MOSS_JEV_COMPLETION: 'shadow' }).runtimeJudge?.modes + .completion, + 'shadow' +); + const live = process.argv.includes('--live'); const temp = await mkdtemp(path.join(os.tmpdir(), 'moss-jev-runtime-')); const workspace = await realpath(temp); diff --git a/src/cli/config-commands.ts b/src/cli/config-commands.ts index 1d812941..32c2b210 100644 --- a/src/cli/config-commands.ts +++ b/src/cli/config-commands.ts @@ -497,9 +497,11 @@ export const MOSS_ENV_REFERENCE: ReadonlyArray<{ group: string; vars: readonly s 'MOSS_NO_COLOR', 'MOSS_LOG_LEVEL', 'MOSS_LOG_JSON', - 'MOSS_JEV_INTENT (shadow observes; active applies live steering decisions; startup env only)', + 'MOSS_JEV_ENABLED (0 disables all Jev judgments; startup env only)', + 'MOSS_JEV_INTENT (default active with trusted service configuration; shadow observes)', 'MOSS_JEV_APPROVAL (shadow observes ordinary prompts; active is an observation-only alias)', - 'MOSS_JEV_CONSTRAINT / MOSS_JEV_OUTPUT / MOSS_JEV_PROGRESS / MOSS_JEV_COMPLETION (off|shadow|active; startup env only)', + 'MOSS_JEV_COMPLETION (default active with trusted service configuration)', + 'MOSS_JEV_CONSTRAINT / MOSS_JEV_OUTPUT / MOSS_JEV_PROGRESS (off|shadow|active; default off)', 'MOSS_JEV_SYSTEMONE_URL (full HTTPS endpoint for an explicit relay; startup env only)', 'MOSS_JEV_RELAY_KEY (credential dedicated to the explicit relay; startup env only)', 'MOSS_JEV_MODEL (relay model override; defaults to jev-latest)', diff --git a/src/cli/jev-intent.ts b/src/cli/jev-intent.ts index d1364087..bd293d90 100644 --- a/src/cli/jev-intent.ts +++ b/src/cli/jev-intent.ts @@ -70,7 +70,8 @@ function readChoice>(answer: unknown, criteria: }; } -function modeOf(value: string | undefined): JudgeMode { +function modeOf(value: string | undefined, fallback: JudgeMode = 'off'): JudgeMode { + if (value === undefined) return fallback; return value === 'active' || value === 'shadow' ? value : 'off'; } @@ -86,21 +87,24 @@ function sanitize(value: JudgeValue, key: string): JudgeValue { } export function createJevIntentRuntime( - env: NodeJS.ProcessEnv + env: NodeJS.ProcessEnv, + options: { fetch?: typeof fetch } = {} ): Pick { - const mode = modeOf(env.MOSS_JEV_INTENT); + if (env.MOSS_JEV_ENABLED?.trim() === '0') return {}; + const mode = modeOf(env.MOSS_JEV_INTENT, 'active'); const modes: RuntimeJudgeConfig['modes'] = { input: mode, approval: modeOf(env.MOSS_JEV_APPROVAL), constraint: modeOf(env.MOSS_JEV_CONSTRAINT), output: modeOf(env.MOSS_JEV_OUTPUT), progress: modeOf(env.MOSS_JEV_PROGRESS), - completion: modeOf(env.MOSS_JEV_COMPLETION), + completion: modeOf(env.MOSS_JEV_COMPLETION, 'active'), }; const runtimeEnabled = Object.values(modes).some((value) => value !== 'off'); if (!runtimeEnabled) return {}; const log = getRootLogger().child('jev-intent'); - const unavailable = mode === 'active' ? { decideSteeringIntent: async () => undefined } : {}; + const unavailable = + env.MOSS_JEV_INTENT === 'active' ? { decideSteeringIntent: async () => undefined } : {}; const provider = env.MOSS_JEV_PROVIDER?.trim() || 'typesafe'; if (provider !== 'typesafe' && provider !== 'openrouter' && provider !== 'opencode') { log.warn('MOSS_JEV_PROVIDER must be typesafe, openrouter, or opencode'); @@ -127,7 +131,14 @@ export function createJevIntentRuntime( const apiKeyEnv = parsed ? 'MOSS_JEV_RELAY_KEY' : spec.apiKeyEnv; const apiKey = env[apiKeyEnv]?.trim(); if (!apiKey && (parsed || provider !== 'opencode')) { - log.warn('Jev requires ' + apiKeyEnv + ' in the startup environment'); + const explicitlyConfigured = + endpoint || + env.MOSS_JEV_PROVIDER?.trim() || + Object.keys(modes).some((point) => { + const flag = point === 'input' ? 'INTENT' : point.toUpperCase(); + return modeOf(env['MOSS_JEV_' + flag]) !== 'off'; + }); + if (explicitlyConfigured) log.warn('Jev requires ' + apiKeyEnv + ' in the startup environment'); return unavailable; } @@ -135,7 +146,7 @@ export function createJevIntentRuntime( const safeState = sanitize(state, apiKey ?? ''); // An omitted suffix or a redacted operand can change what would be authorized. if (questions.approval && JSON.stringify(safeState) !== JSON.stringify(state)) return undefined; - const response = await fetch(url, { + const response = await (options.fetch ?? fetch)(url, { method: 'POST', headers: { ...(apiKey ? { Authorization: 'Bearer ' + apiKey } : {}),