Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | 密钥环境变量 | 模型 |
| ------------------- | -------------------------- | -------------------------------------------------------------------------------------------------- |
Expand All @@ -100,7 +100,7 @@ SDK 宿主也可通过 `MossAgentConfig.observeUserIntent` 注入观察器,或

### 单条任务中的 Jev 判定

以下开关均从启动环境读取。默认 `off`。`shadow` 记录判定,`active` 将判定用于当前 run。
以下开关均从启动环境读取。完成检查默认 `active`,其余判定默认 `off`。`shadow` 记录判定,`active` 将判定用于当前 run。

| 环境变量 | 触发场景 |
| --------------------- | --------------------------------------------------------------------------- |
Expand Down
2 changes: 1 addition & 1 deletion docs/specs/moss-jev-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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、建议动作、采纳与否、降级原因及耗时。

Expand Down
55 changes: 55 additions & 0 deletions scripts/verify/jev-runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/cli/config-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
Expand Down
25 changes: 18 additions & 7 deletions src/cli/jev-intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ function readChoice<T extends Record<string, string>>(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';
}

Expand All @@ -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<MossAgentConfig, 'observeUserIntent' | 'decideSteeringIntent' | 'runtimeJudge'> {
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');
Expand All @@ -127,15 +131,22 @@ 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;
}

async function ask(state: JudgeState, questions: JudgeQuestions, signal: AbortSignal) {
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 } : {}),
Expand Down
Loading