@miphamai/cli 0.85.38 → 0.85.40
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/README.md +1 -1
- package/package.json +1 -1
- package/skills/standard/code-review.SKILL.md +17 -0
- package/skills/standard/web-access/references/cdp-api.md +4 -1
- package/skills/standard/web-access/scripts/cdp-proxy.mjs +40 -7
- package/skills/standard/web-access/scripts/check-deps.mjs +55 -2
- package/skills/standard/web-access.SKILL.md +5 -0
- package/skills/standard/web-search.SKILL.md +13 -4
- package/src/core/crsi-modify.ts +41 -6
- package/src/core/crsi-sandbox.ts +5 -0
- package/src/core/engine.ts +6 -0
- package/src/core/eval-harness.ts +106 -0
- package/src/core/improvement-track.ts +91 -0
- package/src/core/proposal-integration.ts +149 -0
- package/src/core/task-performance.ts +41 -3
- package/src/mcp/registry.ts +32 -0
- package/src/shared/package-info.ts +2 -2
- package/src/skills/bundled-skill-assets.ts +3 -3
- package/src/skills/bundled-skills.ts +3 -3
- package/src/tools/network/web-search.ts +1 -4
- package/src/ui/app.tsx +8 -0
- package/src/ui/commands.ts +216 -34
- package/src/ui/input-debug.ts +164 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Mipham Code v0.85.
|
|
1
|
+
# Mipham Code v0.85.40
|
|
2
2
|
|
|
3
3
|
**Multi-model open-core intelligent coding terminal** — 12 AI providers, 139 commands, 31 tools, 28 skills + marketplace, self-update. AI-assisted code generation, security auditing, MCP protocol, and extensible skills — in a single CLI.
|
|
4
4
|
|
package/package.json
CHANGED
|
@@ -8,6 +8,23 @@ version: 2.0.0
|
|
|
8
8
|
|
|
9
9
|
Analyzes code changes for bugs, security risks, performance issues, and code quality. Generates structured review reports.
|
|
10
10
|
|
|
11
|
+
## Scope (what `/code-review` reports)
|
|
12
|
+
|
|
13
|
+
`/code-review` reports **two things only**: defects (correctness, security) and
|
|
14
|
+
CLAUDE.md violations. Reuse, simplification, efficiency, architecture and
|
|
15
|
+
test-coverage cleanups belong to `/simplify` (`self-review` skill) — reporting
|
|
16
|
+
them here dilutes the one question the review is answering.
|
|
17
|
+
|
|
18
|
+
Every finding carries exactly one **attribution severity**:
|
|
19
|
+
|
|
20
|
+
- `important` — this change introduced or worsened it
|
|
21
|
+
- `pre_existing` — it was already there before this change
|
|
22
|
+
- `nit` — minor
|
|
23
|
+
|
|
24
|
+
Attribution is not risk ranking: the reader first needs to know whether they
|
|
25
|
+
broke it this time. End the summary with one suggested fix. When there is
|
|
26
|
+
nothing to report, say exactly: `Code review found no issues.`
|
|
27
|
+
|
|
11
28
|
## Review Dimensions
|
|
12
29
|
|
|
13
30
|
### 1. Correctness (Bug Detection)
|
|
@@ -12,16 +12,19 @@
|
|
|
12
12
|
|
|
13
13
|
### GET /health
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
健康检查。`status` 为 `ok` **仅当已连上 Chrome**;未连接时为 `disconnected`,`lastError` 说明原因(多半是 Chrome 远程调试未授权)。`connected` 是布尔值。
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
18
|
curl -s http://localhost:3456/health
|
|
19
|
+
# {"status":"ok","connected":true,"lastError":null,"chromePort":9222,"lastSeenChromePort":9222,...}
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
### GET /targets
|
|
22
23
|
|
|
23
24
|
列出所有已打开的页面 tab。返回数组,每项含 `targetId`、`title`、`url`。
|
|
24
25
|
|
|
26
|
+
连不上 Chrome 时返回 HTTP 500,body 含 `error`(原因)与 `hint`(下一步)。**不要**把这种情况读成「localhost 不通」:能收到这个 500 就说明代理可达。
|
|
27
|
+
|
|
25
28
|
```bash
|
|
26
29
|
curl -s http://localhost:3456/targets
|
|
27
30
|
```
|
|
@@ -13,6 +13,11 @@ import crypto from 'node:crypto'
|
|
|
13
13
|
|
|
14
14
|
const PORT = parseInt(process.env.CDP_PROXY_PORT || '3456')
|
|
15
15
|
let ws = null
|
|
16
|
+
// 诊断用:Chrome 连不上时,/health 与 /targets 都要能说出原因,
|
|
17
|
+
// 否则调用方只能看到 `connected: null` + `{"error":"连接失败"}`,
|
|
18
|
+
// 很容易把「Chrome 远程调试未授权」误判成「localhost 不通」。
|
|
19
|
+
let lastConnectError = null
|
|
20
|
+
let lastSeenChromePort = null
|
|
16
21
|
let cmdId = 0
|
|
17
22
|
const pending = new Map() // id -> {resolve, timer}
|
|
18
23
|
const sessions = new Map() // targetId -> sessionId
|
|
@@ -196,6 +201,7 @@ async function connect() {
|
|
|
196
201
|
chromePort = discovered.port
|
|
197
202
|
chromeWsPath = discovered.wsPath
|
|
198
203
|
}
|
|
204
|
+
lastSeenChromePort = chromePort
|
|
199
205
|
|
|
200
206
|
const wsUrl = getWebSocketUrl(chromePort, chromeWsPath)
|
|
201
207
|
if (!wsUrl) throw new Error('无法获取 Chrome WebSocket URL')
|
|
@@ -206,6 +212,7 @@ async function connect() {
|
|
|
206
212
|
const onOpen = () => {
|
|
207
213
|
cleanup()
|
|
208
214
|
connectingPromise = null
|
|
215
|
+
lastConnectError = null
|
|
209
216
|
console.log(`[CDP Proxy] 已连接 Chrome (端口 ${chromePort})`)
|
|
210
217
|
resolve()
|
|
211
218
|
}
|
|
@@ -215,12 +222,18 @@ async function connect() {
|
|
|
215
222
|
ws = null
|
|
216
223
|
chromePort = null
|
|
217
224
|
chromeWsPath = null
|
|
218
|
-
|
|
225
|
+
// 原生 WebSocket 的 error 事件常常没有 message(Chrome 未授权时握手被拒),
|
|
226
|
+
// 落到「连接失败」四个字上等于什么都没说。
|
|
227
|
+
const msg = e.message || e.error?.message || 'Chrome 拒绝了调试连接(远程调试尚未授权)'
|
|
228
|
+
lastConnectError = msg
|
|
219
229
|
console.error('[CDP Proxy] 连接错误:', msg, '(端口缓存已清除,下次将重新发现)')
|
|
220
230
|
reject(new Error(msg))
|
|
221
231
|
}
|
|
222
232
|
const onClose = () => {
|
|
223
233
|
console.log('[CDP Proxy] 连接断开')
|
|
234
|
+
// 握手失败时 onError 先给出可行动的原因,onClose 紧随其后把状态清空。
|
|
235
|
+
// 不要用「连接已断开」盖掉那句话 —— 它才是调用方能据以行动的东西。
|
|
236
|
+
if (!lastConnectError) lastConnectError = 'Chrome 连接已断开'
|
|
224
237
|
ws = null
|
|
225
238
|
chromePort = null // 重置端口缓存,下次连接重新发现
|
|
226
239
|
chromeWsPath = null
|
|
@@ -414,16 +427,19 @@ const server = http.createServer(async (req, res) => {
|
|
|
414
427
|
res.setHeader('Content-Type', 'application/json; charset=utf-8')
|
|
415
428
|
|
|
416
429
|
try {
|
|
417
|
-
// /health 不需要连接 Chrome
|
|
430
|
+
// /health 不需要连接 Chrome,但**必须如实报告没连上**:
|
|
431
|
+
// 一个在坏掉时说 ok 的健康检查,只会把调用方推向错误的根因。
|
|
418
432
|
if (pathname === '/health') {
|
|
419
|
-
const connected = ws && (ws.readyState === WS.OPEN || ws.readyState === 1)
|
|
433
|
+
const connected = !!(ws && (ws.readyState === WS.OPEN || ws.readyState === 1))
|
|
420
434
|
res.end(
|
|
421
435
|
JSON.stringify({
|
|
422
|
-
status: 'ok',
|
|
436
|
+
status: connected ? 'ok' : 'disconnected',
|
|
423
437
|
connected,
|
|
438
|
+
lastError: connected ? null : lastConnectError,
|
|
439
|
+
chromePort,
|
|
440
|
+
lastSeenChromePort,
|
|
424
441
|
sessions: sessions.size,
|
|
425
442
|
managedTabs: managedTabs.size,
|
|
426
|
-
chromePort,
|
|
427
443
|
}),
|
|
428
444
|
)
|
|
429
445
|
return
|
|
@@ -437,7 +453,22 @@ const server = http.createServer(async (req, res) => {
|
|
|
437
453
|
return
|
|
438
454
|
}
|
|
439
455
|
|
|
440
|
-
|
|
456
|
+
try {
|
|
457
|
+
await connect()
|
|
458
|
+
} catch (e) {
|
|
459
|
+
// 调用方拿到的是「连不上」时唯一能据以行动的东西:原因 + 下一步。
|
|
460
|
+
res.statusCode = 500
|
|
461
|
+
res.end(
|
|
462
|
+
JSON.stringify({
|
|
463
|
+
error: `Chrome 未连接:${lastConnectError || e.message || '未知原因'}`,
|
|
464
|
+
hint: lastSeenChromePort
|
|
465
|
+
? `调试端口 ${lastSeenChromePort} 是开着的,但 Chrome 没有允许远程调试。请在 Chrome 打开 chrome://inspect/#remote-debugging 勾选 Allow remote debugging(或点弹窗的「允许」)后重试。`
|
|
466
|
+
: '未发现 Chrome 调试端口。请确认 Chrome 正在运行,并在 chrome://inspect/#remote-debugging 勾选 Allow remote debugging。',
|
|
467
|
+
chromePort: lastSeenChromePort,
|
|
468
|
+
}),
|
|
469
|
+
)
|
|
470
|
+
return
|
|
471
|
+
}
|
|
441
472
|
|
|
442
473
|
// GET /targets - 列出所有页面
|
|
443
474
|
if (pathname === '/targets') {
|
|
@@ -779,7 +810,9 @@ async function main() {
|
|
|
779
810
|
.get(`http://127.0.0.1:${PORT}/health`, { timeout: 2000 }, (res) => {
|
|
780
811
|
let d = ''
|
|
781
812
|
res.on('data', (c) => (d += c))
|
|
782
|
-
|
|
813
|
+
// 只要回的是本 proxy 的 JSON 就算同一实例 —— status 可能是
|
|
814
|
+
// 'ok' 或 'disconnected',后者照样说明端口上是 proxy 而不是别人。
|
|
815
|
+
res.on('end', () => resolve(d.includes('"status"')))
|
|
783
816
|
})
|
|
784
817
|
.on('error', () => resolve(false))
|
|
785
818
|
})
|
|
@@ -73,6 +73,24 @@ function activePortFiles() {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Chrome 的调试 HTTP 端点在「允许远程调试」之前会回 404/403。
|
|
78
|
+
* 所以 TCP 端口通 ≠ 可调试:只探端口会打印 `chrome: ok`,而代理永远连不上。
|
|
79
|
+
*/
|
|
80
|
+
async function probeChromeHttp(port, timeoutMs = 2000) {
|
|
81
|
+
try {
|
|
82
|
+
const res = await fetch(`http://127.0.0.1:${port}/json/version`, {
|
|
83
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
84
|
+
})
|
|
85
|
+
const body = await res.text()
|
|
86
|
+
if (!res.ok) return { ok: false, status: res.status }
|
|
87
|
+
JSON.parse(body) // 必须是真的 Chrome 调试端点,不是同端口上的别的东西
|
|
88
|
+
return { ok: true, status: res.status }
|
|
89
|
+
} catch {
|
|
90
|
+
return { ok: false, status: 0 }
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
76
94
|
async function detectChromePort() {
|
|
77
95
|
// 优先从 DevToolsActivePort 文件读取
|
|
78
96
|
for (const filePath of activePortFiles()) {
|
|
@@ -133,6 +151,18 @@ function startProxyDetached() {
|
|
|
133
151
|
fs.closeSync(logFd)
|
|
134
152
|
}
|
|
135
153
|
|
|
154
|
+
async function proxyHealth() {
|
|
155
|
+
try {
|
|
156
|
+
const res = await fetch(`http://127.0.0.1:${PROXY_PORT}/health`, {
|
|
157
|
+
signal: AbortSignal.timeout(2000),
|
|
158
|
+
})
|
|
159
|
+
if (!res.ok) return null
|
|
160
|
+
return await res.json()
|
|
161
|
+
} catch {
|
|
162
|
+
return null
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
136
166
|
async function ensureProxy() {
|
|
137
167
|
const targetsUrl = `http://127.0.0.1:${PROXY_PORT}/targets`
|
|
138
168
|
|
|
@@ -143,7 +173,15 @@ async function ensureProxy() {
|
|
|
143
173
|
return true
|
|
144
174
|
}
|
|
145
175
|
|
|
146
|
-
//
|
|
176
|
+
// proxy 活着但 Chrome 没连上:再拉起一个实例只会撞端口,
|
|
177
|
+
// 真正该做的是把原因说清楚(多半是远程调试还没授权)。
|
|
178
|
+
const health = await proxyHealth()
|
|
179
|
+
if (health) {
|
|
180
|
+
console.log(`proxy: running, but Chrome is not connected — ${health.lastError || '原因未知'}`)
|
|
181
|
+
return false
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// 未运行,启动并等待
|
|
147
185
|
console.log('proxy: connecting...')
|
|
148
186
|
startProxyDetached()
|
|
149
187
|
|
|
@@ -162,7 +200,12 @@ async function ensureProxy() {
|
|
|
162
200
|
await new Promise((r) => setTimeout(r, 1000))
|
|
163
201
|
}
|
|
164
202
|
|
|
165
|
-
|
|
203
|
+
const healthAfter = await proxyHealth()
|
|
204
|
+
console.log(
|
|
205
|
+
healthAfter
|
|
206
|
+
? `❌ Chrome 未连接:${healthAfter.lastError || '原因未知'}`
|
|
207
|
+
: '❌ CDP Proxy 未能启动,请检查日志',
|
|
208
|
+
)
|
|
166
209
|
console.log(` 日志:${path.join(os.tmpdir(), 'cdp-proxy.log')}`)
|
|
167
210
|
return false
|
|
168
211
|
}
|
|
@@ -179,6 +222,16 @@ async function main() {
|
|
|
179
222
|
)
|
|
180
223
|
process.exit(1)
|
|
181
224
|
}
|
|
225
|
+
// TCP 开着不代表能调试:Chrome 136+ 在「允许远程调试」前会让 /json/* 回 404/403。
|
|
226
|
+
const probe = await probeChromeHttp(chromePort)
|
|
227
|
+
if (!probe.ok) {
|
|
228
|
+
console.log(
|
|
229
|
+
`chrome: not authorized (port ${chromePort}${probe.status ? `, HTTP ${probe.status}` : ',无响应'})` +
|
|
230
|
+
' — 远程调试尚未允许。请在 Chrome 打开 chrome://inspect/#remote-debugging 勾选 ' +
|
|
231
|
+
'Allow remote debugging(或点弹窗的「允许」),然后重试。',
|
|
232
|
+
)
|
|
233
|
+
process.exit(1)
|
|
234
|
+
}
|
|
182
235
|
console.log(`chrome: ok (port ${chromePort})`)
|
|
183
236
|
|
|
184
237
|
const proxyOk = await ensureProxy()
|
|
@@ -26,6 +26,9 @@ node ~/.mipham/skills/web-access/scripts/check-deps.mjs
|
|
|
26
26
|
|
|
27
27
|
> Mipham Code 环境:`node` 不可用时可用 `bun` 替代(Bun 原生支持 WebSocket 与 node: 内建)。未通过时引导用户:Chrome 地址栏打开 `chrome://inspect/#remote-debugging`,勾选 "Allow remote debugging for this browser instance"。
|
|
28
28
|
|
|
29
|
+
> **判读输出**:`chrome: ok` = 调试端点已授权;`chrome: not authorized` = 端口开着但 Chrome 没允许远程调试(去 `chrome://inspect/#remote-debugging` 勾 Allow,或点弹窗「允许」);`proxy: running, but Chrome is not connected` = 代理活着、Chrome 没授权。
|
|
30
|
+
> **不要**把以上任何一种读成「Bash 没有网络」:`curl -s http://127.0.0.1:3456/health` 能回 200 就证明 localhost 是通的,真正原因在 `lastError` 字段里。
|
|
31
|
+
|
|
29
32
|
**必须向用户展示**:部分站点对浏览器自动化检测严格,存在账号封禁风险。已内置防护但无法完全避免,Agent 继续操作即视为接受。
|
|
30
33
|
|
|
31
34
|
## 工具选择
|
|
@@ -40,6 +43,8 @@ node ~/.mipham/skills/web-access/scripts/check-deps.mjs
|
|
|
40
43
|
|
|
41
44
|
浏览器 CDP 不要求 URL 已知;WebSearch/WebFetch/curl 均不处理登录态。
|
|
42
45
|
|
|
46
|
+
> 搜索引擎结果页(Google / Bing / DuckDuckGo / Brave / Mojeek)对 WebFetch/curl 普遍回 403/429 或验证码。需要搜索时**在 Chrome 里打开引擎页**再用 `/eval` 提取结果,不要用 WebFetch 抓结果页。
|
|
47
|
+
|
|
43
48
|
## 浏览器 CDP 模式
|
|
44
49
|
|
|
45
50
|
通过 CDP Proxy 直连用户日常 Chrome,天然携带登录态。**不主动操作用户已有 tab**,所有操作在自己创建的后台 tab 中进行,任务结束关闭自建 tab(保留用户原 tab)。
|
|
@@ -46,6 +46,17 @@ Question involves...
|
|
|
46
46
|
|
|
47
47
|
---
|
|
48
48
|
|
|
49
|
+
### No API key? Do not scrape search engines
|
|
50
|
+
|
|
51
|
+
If `WebSearch` answers "not configured", **do not** fetch search-engine result pages with
|
|
52
|
+
`WebFetch` or `curl`. Google / Bing / DuckDuckGo / Brave / Mojeek all answer automated
|
|
53
|
+
requests with 403, 429 or a captcha — five calls later you have nothing.
|
|
54
|
+
|
|
55
|
+
Go straight to the `web-access` skill: open the search engine **in Chrome** (logged-in
|
|
56
|
+
profile, real fingerprint), then read the result list with `/eval`.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
49
60
|
## Phase 1: Construct the Query
|
|
50
61
|
|
|
51
62
|
### Rules (apply in order)
|
|
@@ -170,7 +181,5 @@ If search returns "not configured":
|
|
|
170
181
|
2. Set: `export BRAVE_API_KEY="BSA..."`
|
|
171
182
|
3. Restart Mipham Code
|
|
172
183
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
- `TAVILY_API_KEY` — https://tavily.com
|
|
176
|
-
- `SERPAPI_API_KEY` — https://serpapi.com
|
|
184
|
+
Only `BRAVE_API_KEY` is read. There is no Tavily / SerpAPI path in this build — setting
|
|
185
|
+
those variables has no effect.
|
package/src/core/crsi-modify.ts
CHANGED
|
@@ -17,8 +17,12 @@ import { CrsiSandbox, validateBlastRadius, validateMergeConvergence } from './cr
|
|
|
17
17
|
import type { CrsiModificationResult } from './crsi-sandbox'
|
|
18
18
|
import {
|
|
19
19
|
appendEvalScore,
|
|
20
|
+
getContractHistory,
|
|
20
21
|
getLastEvalScore,
|
|
21
22
|
instrumentFailure,
|
|
23
|
+
directionalRegret,
|
|
24
|
+
softHurtGate,
|
|
25
|
+
formatRegretNote,
|
|
22
26
|
regressedAnchors,
|
|
23
27
|
} from './eval-harness'
|
|
24
28
|
import { mechanismSentinel, type RewardFn, type ScoreReport } from './reward-fn'
|
|
@@ -181,12 +185,43 @@ export async function runCrsiModification(
|
|
|
181
185
|
applied.error = `Anchor regression: ${anchors.join(', ')}`
|
|
182
186
|
return applied
|
|
183
187
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
188
|
+
// 第二道闸(GUARD,2611.23476):有逐契约历史时看**方向性 regret** ——
|
|
189
|
+
// 收益必须超过回归;没有历史(首跑)或契约集合变了(新增/移除)时方向性比较
|
|
190
|
+
// 不成立,退回标量分数闸(分母变了,分数不可比)。
|
|
191
|
+
const history = getContractHistory(rewardFn.name, 1)
|
|
192
|
+
const comparable =
|
|
193
|
+
report.results !== undefined && history.length > 0
|
|
194
|
+
? directionalRegret(report.results, history)
|
|
195
|
+
: null
|
|
196
|
+
const setChanged =
|
|
197
|
+
comparable !== null && (comparable.newContracts.length > 0 || comparable.gone.length > 0)
|
|
198
|
+
|
|
199
|
+
if (comparable && !setChanged) {
|
|
200
|
+
const gate = softHurtGate(comparable)
|
|
201
|
+
if (!gate.pass) {
|
|
202
|
+
sandbox.rollback()
|
|
203
|
+
applied.phase = 'failed'
|
|
204
|
+
applied.error = `Directional regret gate (${rewardFn.name}): ${gate.reason}`
|
|
205
|
+
return applied
|
|
206
|
+
}
|
|
207
|
+
// 过闸也要让人看见两侧的数:修好 3 条、弄坏 1 条是**放行**,但读者有权知道
|
|
208
|
+
// 那 1 条是什么这次弄坏的。两侧全零(或只有抖动)不占一行。
|
|
209
|
+
if (
|
|
210
|
+
comparable.newlyBroken.length +
|
|
211
|
+
comparable.newlyFixed.length +
|
|
212
|
+
comparable.stillFailing.length >
|
|
213
|
+
0
|
|
214
|
+
) {
|
|
215
|
+
applied.regretNote = formatRegretNote(comparable)
|
|
216
|
+
}
|
|
217
|
+
} else {
|
|
218
|
+
const last = getLastEvalScore(rewardFn.name)
|
|
219
|
+
if (last !== null && report.score < last) {
|
|
220
|
+
sandbox.rollback()
|
|
221
|
+
applied.phase = 'failed'
|
|
222
|
+
applied.error = `Reward regression (${rewardFn.name}): score ${report.score} < last ${last}`
|
|
223
|
+
return applied
|
|
224
|
+
}
|
|
190
225
|
}
|
|
191
226
|
appendEvalScore(rewardFn.name, report)
|
|
192
227
|
|
package/src/core/crsi-sandbox.ts
CHANGED
|
@@ -70,6 +70,11 @@ export interface CrsiModificationResult {
|
|
|
70
70
|
* 不静默跳过 —— 沉默与「anchor 全绿」在外部读数上同形。
|
|
71
71
|
*/
|
|
72
72
|
rewardNote?: string
|
|
73
|
+
/**
|
|
74
|
+
* 方向性 regret 的可见读数(GUARD 闸):新破坏 / 新修复 / 仍未解决。
|
|
75
|
+
* 过闸但两侧有数时必须出现 —— 否则「这次改动弄坏了什么」只有一个看不见的内部值。
|
|
76
|
+
*/
|
|
77
|
+
regretNote?: string
|
|
73
78
|
/** Current phase of the sandbox pipeline */
|
|
74
79
|
phase:
|
|
75
80
|
'pending' | 'applied' | 'testing' | 'passed' | 'failed' | 'approved' | 'merged' | 'rolled-back'
|
package/src/core/engine.ts
CHANGED
|
@@ -854,6 +854,12 @@ export class QueryEngine {
|
|
|
854
854
|
reasoningContent = ''
|
|
855
855
|
thinkingContent = ''
|
|
856
856
|
toolUses.length = 0
|
|
857
|
+
// Usage is part of "everything": a provider reports tokens as the
|
|
858
|
+
// stream runs, so the attempt that failed had already added its
|
|
859
|
+
// share before `restart` arrived. Keeping it charges the retried
|
|
860
|
+
// reply twice in `/usage` and in every per-tool attribution.
|
|
861
|
+
turnApiInputTokens = 0
|
|
862
|
+
turnApiOutputTokens = 0
|
|
857
863
|
}
|
|
858
864
|
|
|
859
865
|
if (chunk.type === 'error') {
|
package/src/core/eval-harness.ts
CHANGED
|
@@ -265,6 +265,112 @@ export function diffContractHistory(
|
|
|
265
265
|
return out
|
|
266
266
|
}
|
|
267
267
|
|
|
268
|
+
/**
|
|
269
|
+
* 方向性 regret(GUARD,2611.23476):把一次评估拆成「本次新弄坏的」与
|
|
270
|
+
* 「本来就有的缺口」,而不是揉成一个总分。
|
|
271
|
+
*
|
|
272
|
+
* 为什么需要它:标量分数把「新破坏」与「仍未解决」混在一起 —— 一个修好 1 条、
|
|
273
|
+
* 弄坏 1 条的改动与什么都没做的改动同分。GUARD 的判据是**收益必须超过回归**,
|
|
274
|
+
* 故两侧必须分开计数。抖动契约(历史里 true/false 都出现过)不计入任何一侧:
|
|
275
|
+
* 拿一次抖动的翻转当「新破坏」会误拦,当「新修复」会假放。
|
|
276
|
+
*/
|
|
277
|
+
export interface DirectionalRegret {
|
|
278
|
+
/** 上次 PASS、本次 FAIL —— 本次改动新弄坏的(不含抖动)。 */
|
|
279
|
+
newlyBroken: string[]
|
|
280
|
+
/** 上次 FAIL、本次仍 FAIL —— 本来就有的缺口,不是本次的账。 */
|
|
281
|
+
stillFailing: string[]
|
|
282
|
+
/** 上次 FAIL、本次 PASS。 */
|
|
283
|
+
newlyFixed: string[]
|
|
284
|
+
/** 近几次 true/false 都出现过 —— 抖动,不计入任何一侧。 */
|
|
285
|
+
flaky: string[]
|
|
286
|
+
/** 历史里没出现过(新增契约)。 */
|
|
287
|
+
newContracts: string[]
|
|
288
|
+
/** 历史有、本次没有(契约被移出)。 */
|
|
289
|
+
gone: string[]
|
|
290
|
+
/** newlyBroken 中 role==='anchor' 的子集(硬闸)。 */
|
|
291
|
+
anchorBroken: string[]
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function directionalRegret(
|
|
295
|
+
current: ContractResultRecord[],
|
|
296
|
+
history: ContractSnapshot[],
|
|
297
|
+
): DirectionalRegret {
|
|
298
|
+
const delta: DirectionalRegret = {
|
|
299
|
+
newlyBroken: [],
|
|
300
|
+
stillFailing: [],
|
|
301
|
+
newlyFixed: [],
|
|
302
|
+
flaky: [],
|
|
303
|
+
newContracts: [],
|
|
304
|
+
gone: [],
|
|
305
|
+
anchorBroken: [],
|
|
306
|
+
}
|
|
307
|
+
const seen = new Set<string>()
|
|
308
|
+
for (const c of current) {
|
|
309
|
+
seen.add(c.id)
|
|
310
|
+
const past = history.filter((h) => c.id in h).map((h) => h[c.id] === true)
|
|
311
|
+
if (past.length === 0) {
|
|
312
|
+
delta.newContracts.push(c.id)
|
|
313
|
+
continue
|
|
314
|
+
}
|
|
315
|
+
if (past.includes(true) && past.includes(false)) {
|
|
316
|
+
delta.flaky.push(c.id)
|
|
317
|
+
continue
|
|
318
|
+
}
|
|
319
|
+
const wasPassing = past[0] === true
|
|
320
|
+
if (wasPassing && !c.passed) {
|
|
321
|
+
delta.newlyBroken.push(c.id)
|
|
322
|
+
if (c.role === 'anchor') delta.anchorBroken.push(c.id)
|
|
323
|
+
} else if (!wasPassing && c.passed) {
|
|
324
|
+
delta.newlyFixed.push(c.id)
|
|
325
|
+
} else if (!c.passed) {
|
|
326
|
+
delta.stillFailing.push(c.id)
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
for (const h of history) {
|
|
330
|
+
for (const id of Object.keys(h)) {
|
|
331
|
+
if (seen.has(id)) continue
|
|
332
|
+
seen.add(id)
|
|
333
|
+
delta.gone.push(id)
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return delta
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export interface SoftHurtVerdict {
|
|
340
|
+
pass: boolean
|
|
341
|
+
reason?: string
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Soft-HURT 门:**收益必须超过回归**才算通过。
|
|
346
|
+
*
|
|
347
|
+
* - anchor 新破坏 ⇒ 直接拒(安全/机制不变量没有「以多换少」的余地);
|
|
348
|
+
* - 新增破坏 ≥ 新增修复 ⇒ 拒。等号也拒:修好一条、弄坏一条**不是**改进,
|
|
349
|
+
* 只是把工作挪了个位置;标量分数在这一格恰好同分,所以它看不见。
|
|
350
|
+
*
|
|
351
|
+
* `stillFailing` 不进判据 —— 它本来就是失败,不是这次改动造成的。
|
|
352
|
+
*/
|
|
353
|
+
export function softHurtGate(delta: DirectionalRegret): SoftHurtVerdict {
|
|
354
|
+
if (delta.anchorBroken.length > 0) {
|
|
355
|
+
return { pass: false, reason: `anchor 契约回退: ${delta.anchorBroken.join(', ')}` }
|
|
356
|
+
}
|
|
357
|
+
if (delta.newlyBroken.length > 0 && delta.newlyBroken.length >= delta.newlyFixed.length) {
|
|
358
|
+
return {
|
|
359
|
+
pass: false,
|
|
360
|
+
reason:
|
|
361
|
+
`新破坏 ${delta.newlyBroken.length} 条 ≥ 新修复 ${delta.newlyFixed.length} 条` +
|
|
362
|
+
`(收益未超过回归:${delta.newlyBroken.join(', ')})`,
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return { pass: true }
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** 方向性 regret 的一行读数(只描述,不判定)。 */
|
|
369
|
+
export function formatRegretNote(delta: DirectionalRegret): string {
|
|
370
|
+
const flaky = delta.flaky.length > 0 ? ` · 抖动 ${delta.flaky.length}(不计)` : ''
|
|
371
|
+
return `新破坏 ${delta.newlyBroken.length} · 新修复 ${delta.newlyFixed.length} · 仍未解决 ${delta.stillFailing.length}${flaky}`
|
|
372
|
+
}
|
|
373
|
+
|
|
268
374
|
/**
|
|
269
375
|
* 把 diffContractHistory 的结果渲染成展示行。纯函数——不做 I/O、不读时钟。
|
|
270
376
|
* 返回空数组表示「无变化」,调用方据此决定是否打印标题。
|
|
@@ -40,6 +40,20 @@ export interface ImprovementReport {
|
|
|
40
40
|
nullModelTotal?: number
|
|
41
41
|
/** 闸的结论:failed = 有题空程序也能过 ⇒ `improved` 不可归因。 */
|
|
42
42
|
nullModelGate?: 'passed' | 'failed'
|
|
43
|
+
/**
|
|
44
|
+
* SIM(Self-Improvement Memory):这条编辑针对的**复发问题**分组键。
|
|
45
|
+
* 同一 harness 问题被反复修时,记录才能按问题聚在一起,而不是按时间摊平。
|
|
46
|
+
* 缺席 = 旧记录 / 该调用点没有失败信号。
|
|
47
|
+
*/
|
|
48
|
+
problemKey?: string
|
|
49
|
+
/** SIM:为什么做这次编辑(人可读的一句)。缺席语义同 problemKey。 */
|
|
50
|
+
rationale?: string
|
|
51
|
+
/**
|
|
52
|
+
* RSI Tax:与分数数组逐项对齐的前/后测量 token。**只记录,不进任何门禁**
|
|
53
|
+
* (与耗时维同一条纪律)。两个数组同生同灭;缺席 = 未测,不是免费。
|
|
54
|
+
*/
|
|
55
|
+
baselineTokens?: number[]
|
|
56
|
+
postTokens?: number[]
|
|
43
57
|
}
|
|
44
58
|
|
|
45
59
|
export interface ImprovementRecord extends ImprovementReport {
|
|
@@ -70,10 +84,16 @@ function stdDev(xs: number[]): number {
|
|
|
70
84
|
return Math.sqrt(variance)
|
|
71
85
|
}
|
|
72
86
|
|
|
87
|
+
export interface ImprovementContext {
|
|
88
|
+
problemKey?: string
|
|
89
|
+
rationale?: string
|
|
90
|
+
}
|
|
91
|
+
|
|
73
92
|
export function buildImprovementReport(
|
|
74
93
|
sample: SkillDeltaSample,
|
|
75
94
|
changeSet: string[],
|
|
76
95
|
predicted?: number,
|
|
96
|
+
context?: ImprovementContext,
|
|
77
97
|
): ImprovementReport {
|
|
78
98
|
const deltaMean = mean(sample.postScores) - mean(sample.baselineScores)
|
|
79
99
|
const noise = stdDev(sample.baselineScores)
|
|
@@ -123,7 +143,78 @@ export function buildImprovementReport(
|
|
|
123
143
|
nullModelGate,
|
|
124
144
|
}
|
|
125
145
|
: {}),
|
|
146
|
+
// SIM 两键各自独立:有理由而没有分组键(或反过来)都是有意义的状态,
|
|
147
|
+
// 强行绑定只会让调用方为了写一个而编另一个。
|
|
148
|
+
...(context?.problemKey ? { problemKey: context.problemKey } : {}),
|
|
149
|
+
...(context?.rationale ? { rationale: context.rationale } : {}),
|
|
150
|
+
// RSI Tax 与耗时维同一纪律:两条同生同灭,缺席即未测。
|
|
151
|
+
...(sample.baselineTokens !== undefined && sample.postTokens !== undefined
|
|
152
|
+
? { baselineTokens: sample.baselineTokens, postTokens: sample.postTokens }
|
|
153
|
+
: {}),
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** 问题分组键的规范化:去空白/大小写差异,截断到 120 字符(防止把整段散文当键)。 */
|
|
158
|
+
export function normalizeProblemKey(text: string): string {
|
|
159
|
+
return text.trim().toLowerCase().replace(/\s+/g, ' ').slice(0, 120)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export interface ProblemGroup {
|
|
163
|
+
key: string
|
|
164
|
+
total: number
|
|
165
|
+
improved: number
|
|
166
|
+
meanDelta: number
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 按 `problemKey` 聚合台账(SIM 的读侧)。没有分组键的旧记录不进入任何组,
|
|
171
|
+
* 它们仍在总台账里 —— 分组是视图,不是过滤器。
|
|
172
|
+
*/
|
|
173
|
+
export function groupImprovementsByProblem(records: ImprovementRecord[]): ProblemGroup[] {
|
|
174
|
+
const groups = new Map<string, ImprovementRecord[]>()
|
|
175
|
+
for (const r of records) {
|
|
176
|
+
if (!r.problemKey) continue
|
|
177
|
+
const bucket = groups.get(r.problemKey) ?? []
|
|
178
|
+
bucket.push(r)
|
|
179
|
+
groups.set(r.problemKey, bucket)
|
|
126
180
|
}
|
|
181
|
+
return [...groups.entries()]
|
|
182
|
+
.map(([key, rs]) => ({
|
|
183
|
+
key,
|
|
184
|
+
total: rs.length,
|
|
185
|
+
improved: rs.filter((r) => r.verdict === 'improved').length,
|
|
186
|
+
meanDelta: mean(rs.map((r) => r.deltaMean)),
|
|
187
|
+
}))
|
|
188
|
+
.sort((a, b) => b.total - a.total || a.key.localeCompare(b.key))
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** RSI Tax 的台账汇总:只统计带 token 量的记录。 */
|
|
192
|
+
export function measurementTaxSummary(records: ImprovementRecord[]): {
|
|
193
|
+
records: number
|
|
194
|
+
totalTokens: number
|
|
195
|
+
meanTokens: number
|
|
196
|
+
} {
|
|
197
|
+
const measured = records.filter(
|
|
198
|
+
(r) => r.baselineTokens !== undefined && r.postTokens !== undefined,
|
|
199
|
+
)
|
|
200
|
+
const totalTokens = measured.reduce(
|
|
201
|
+
(sum, r) => sum + [...r.baselineTokens!, ...r.postTokens!].reduce((a, b) => a + b, 0),
|
|
202
|
+
0,
|
|
203
|
+
)
|
|
204
|
+
return {
|
|
205
|
+
records: measured.length,
|
|
206
|
+
totalTokens,
|
|
207
|
+
meanTokens: measured.length > 0 ? Math.round(totalTokens / measured.length) : 0,
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** RSI Tax 的只读展示;缺席 → null(调用方整行不打印)。 */
|
|
212
|
+
export function formatRsiTaxLine(report: ImprovementReport): string | null {
|
|
213
|
+
const { baselineTokens: before_, postTokens: after_ } = report
|
|
214
|
+
if (before_ === undefined || after_ === undefined) return null
|
|
215
|
+
const before = before_.reduce((a, b) => a + b, 0)
|
|
216
|
+
const after = after_.reduce((a, b) => a + b, 0)
|
|
217
|
+
return `💸 验证 token(RSI Tax): 前 ${before.toLocaleString()} → 后 ${after.toLocaleString()}(合计 ${(before + after).toLocaleString()})`
|
|
127
218
|
}
|
|
128
219
|
|
|
129
220
|
/** 空模型闸失败时给出一行可见读数;闸通过则不出声(不占显示行)。 */
|