@miphamai/cli 0.85.54 → 0.85.55
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/src/agent/sub-agent.ts +18 -2
- package/src/agent/types.ts +9 -0
- package/src/config/loader.ts +60 -0
- package/src/core/command-taps.ts +10 -0
- package/src/core/engine.ts +306 -2
- package/src/core/instructions.ts +1 -1
- package/src/core/review-gate.ts +192 -0
- package/src/core/task-performance-tasks.json +18 -0
- package/src/core/verification-baseline.ts +277 -0
- package/src/core/write-paths.ts +19 -0
- package/src/daemon/engine-capabilities.ts +22 -1
- package/src/i18n-core/locales/en-US.json +4 -0
- package/src/i18n-core/locales/zh-CN.json +4 -0
- package/src/index.tsx +25 -0
- package/src/shared/package-info.ts +2 -2
- package/src/shared/types.ts +11 -0
- package/src/skills/fork-executor.ts +3 -2
- package/src/tools/agent/skill.ts +1 -0
- package/src/tools/agent/workflow.ts +1 -0
- package/src/ui/commands.ts +8 -0
- package/src/workflow/primitives/agent.ts +4 -1
- package/src/workflow/runtime.ts +4 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Mipham Code v0.85.
|
|
1
|
+
# Mipham Code v0.85.55
|
|
2
2
|
|
|
3
3
|
**Multi-model open-core intelligent coding terminal** — 12 AI providers, 150 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
package/src/agent/sub-agent.ts
CHANGED
|
@@ -16,6 +16,8 @@ import { getWorkspaceTrust } from '../core/workspace-trust'
|
|
|
16
16
|
import { recordToolCall } from '../telemetry/index'
|
|
17
17
|
import type { ExperienceRuleEngine } from '../core/rule-engine.js'
|
|
18
18
|
import { runPreToolGate } from '../core/tool-gate'
|
|
19
|
+
import { writtenPathsFor } from '../core/write-paths'
|
|
20
|
+
import { commandFor } from '../core/command-taps'
|
|
19
21
|
import { buildRequest, sendInferenceCheck, isInferenceHookEnabled } from '../core/inference-hook'
|
|
20
22
|
import type { SubAgentServices } from './services'
|
|
21
23
|
|
|
@@ -320,8 +322,8 @@ export class SubAgent {
|
|
|
320
322
|
// The cap is never silent: say which agent asked for what and what it got instead.
|
|
321
323
|
reportAgentCapWarnings(gate, agentDef?.name)
|
|
322
324
|
|
|
323
|
-
// Resolve execution directory: worktree isolation or process cwd
|
|
324
|
-
const execCwd = options.worktreePath || process.cwd()
|
|
325
|
+
// Resolve execution directory: explicit cwd, worktree isolation, or process cwd
|
|
326
|
+
const execCwd = options.cwd || options.worktreePath || process.cwd()
|
|
325
327
|
|
|
326
328
|
// Check workspace trust for the execution directory.
|
|
327
329
|
// For worktree-isolated agents, auto-trust the worktree if the parent is trusted.
|
|
@@ -840,6 +842,20 @@ export class SubAgent {
|
|
|
840
842
|
try {
|
|
841
843
|
const result = await tool.execute(effectiveInput, toolContext)
|
|
842
844
|
|
|
845
|
+
// Second write path for the review gate: this loop bypasses
|
|
846
|
+
// `Engine.executeTool`, so the engine's own tracker never sees it.
|
|
847
|
+
if (result.success) {
|
|
848
|
+
for (const p of writtenPathsFor(tu.name, effectiveInput)) {
|
|
849
|
+
toolContext.onFileWrite?.(p)
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// Command tap for the verification baseline. Deliberately outside
|
|
854
|
+
// the success branch: a failed run is a reading, not evidence that
|
|
855
|
+
// the check never ran.
|
|
856
|
+
const command = commandFor(tu.name, effectiveInput)
|
|
857
|
+
if (command !== undefined) toolContext.onCommand?.(command, result.success)
|
|
858
|
+
|
|
843
859
|
if (sisFixSignature) {
|
|
844
860
|
this.services?.gate.preflight?.()?.recordOutcome(sisFixSignature, result.success)
|
|
845
861
|
sisOutcomeRecorded = true
|
package/src/agent/types.ts
CHANGED
|
@@ -71,6 +71,15 @@ export interface SubAgentOptions {
|
|
|
71
71
|
onTokenUsage?: (totalTokens: number) => void
|
|
72
72
|
/** When set, tool executions use this path as cwd (git worktree isolation). */
|
|
73
73
|
worktreePath?: string
|
|
74
|
+
/**
|
|
75
|
+
* Tool-execution cwd.
|
|
76
|
+
*
|
|
77
|
+
* Unlike `worktreePath` this does NOT enter the worktree branch and does NOT
|
|
78
|
+
* write a workspace-trust entry. Daemon sessions need it: their session cwd is
|
|
79
|
+
* not `process.cwd()`, and passing `worktreePath` there would persist a trust
|
|
80
|
+
* record for the session directory as a side effect of reviewing it.
|
|
81
|
+
*/
|
|
82
|
+
cwd?: string
|
|
74
83
|
/** Seed the sub-agent with a parent conversation prefix (e.g., fork inheritance). */
|
|
75
84
|
inheritContext?: { messages: Message[] }
|
|
76
85
|
/**
|
package/src/config/loader.ts
CHANGED
|
@@ -1023,6 +1023,66 @@ export function loadDisabledMcpServers(): Set<string> {
|
|
|
1023
1023
|
}
|
|
1024
1024
|
}
|
|
1025
1025
|
|
|
1026
|
+
export interface ReviewGateSettings {
|
|
1027
|
+
enabled: boolean
|
|
1028
|
+
maxRounds: number
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/** Review gate defaults: on, at most two reviews per turn. */
|
|
1032
|
+
export const DEFAULT_REVIEW_GATE: ReviewGateSettings = { enabled: true, maxRounds: 2 }
|
|
1033
|
+
|
|
1034
|
+
/**
|
|
1035
|
+
* Review gate config, read from the **user-level** settings.json.
|
|
1036
|
+
*
|
|
1037
|
+
* User level only, same reason as `loadDisabledMcpServers`: a project file must
|
|
1038
|
+
* not be able to silently turn the self-check off (that would be a widening).
|
|
1039
|
+
* A malformed file yields the defaults rather than throwing — this runs on the
|
|
1040
|
+
* startup path, where a throw takes the session down before it can be fixed.
|
|
1041
|
+
*/
|
|
1042
|
+
export function loadReviewGateConfig(): ReviewGateSettings {
|
|
1043
|
+
try {
|
|
1044
|
+
const doc = readSettingsDoc(settingsPathFor('user'))
|
|
1045
|
+
const raw = doc.reviewGate
|
|
1046
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return { ...DEFAULT_REVIEW_GATE }
|
|
1047
|
+
const obj = raw as Record<string, unknown>
|
|
1048
|
+
const enabled = typeof obj.enabled === 'boolean' ? obj.enabled : DEFAULT_REVIEW_GATE.enabled
|
|
1049
|
+
const maxRounds =
|
|
1050
|
+
typeof obj.maxRounds === 'number' && Number.isFinite(obj.maxRounds)
|
|
1051
|
+
? Math.min(5, Math.max(1, Math.trunc(obj.maxRounds)))
|
|
1052
|
+
: DEFAULT_REVIEW_GATE.maxRounds
|
|
1053
|
+
return { enabled, maxRounds }
|
|
1054
|
+
} catch {
|
|
1055
|
+
return { ...DEFAULT_REVIEW_GATE }
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
export interface VerificationSettings {
|
|
1060
|
+
enabled: boolean
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
/** Verification baseline defaults: on. */
|
|
1064
|
+
export const DEFAULT_VERIFICATION: VerificationSettings = { enabled: true }
|
|
1065
|
+
|
|
1066
|
+
/**
|
|
1067
|
+
* Verification baseline config, read from the **user-level** settings.json.
|
|
1068
|
+
*
|
|
1069
|
+
* User level only: a project file must not be able to turn the self-check off.
|
|
1070
|
+
* A malformed file yields the defaults rather than throwing — startup path.
|
|
1071
|
+
*/
|
|
1072
|
+
export function loadVerificationConfig(): VerificationSettings {
|
|
1073
|
+
try {
|
|
1074
|
+
const doc = readSettingsDoc(settingsPathFor('user'))
|
|
1075
|
+
const raw = doc.verification
|
|
1076
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return { ...DEFAULT_VERIFICATION }
|
|
1077
|
+
const obj = raw as Record<string, unknown>
|
|
1078
|
+
return {
|
|
1079
|
+
enabled: typeof obj.enabled === 'boolean' ? obj.enabled : DEFAULT_VERIFICATION.enabled,
|
|
1080
|
+
}
|
|
1081
|
+
} catch {
|
|
1082
|
+
return { ...DEFAULT_VERIFICATION }
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1026
1086
|
/**
|
|
1027
1087
|
* Persist the full disabled-server set (sorted, deduped). An empty set removes
|
|
1028
1088
|
* the key entirely, so "never disabled anything" and "edited it back" look the
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 从工具调用里提取「实际执行的 shell 命令」。只认 Bash 的 `command` 参数;
|
|
3
|
+
* 其余工具一律返回 undefined —— 验证基线只观察能对得上检查清单的命令。
|
|
4
|
+
*/
|
|
5
|
+
export function commandFor(toolName: string, params: Record<string, unknown>): string | undefined {
|
|
6
|
+
if (toolName !== 'Bash') return undefined
|
|
7
|
+
const command = params.command
|
|
8
|
+
if (typeof command !== 'string' || command.trim() === '') return undefined
|
|
9
|
+
return command
|
|
10
|
+
}
|
package/src/core/engine.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
StreamChunk,
|
|
3
3
|
ToolDefinition,
|
|
4
4
|
ToolResult,
|
|
5
|
+
ToolContext,
|
|
5
6
|
InferenceHookConfig,
|
|
6
7
|
CrsiConfig,
|
|
7
8
|
CrossSessionConfig,
|
|
@@ -28,6 +29,14 @@ import type { Skills } from '../skills/seam'
|
|
|
28
29
|
import { getBackgroundAgentRegistry } from '../agent/background-registry'
|
|
29
30
|
import { RulesLoader } from './rules-loader'
|
|
30
31
|
import { extractBashFileAccess } from './permission-rules'
|
|
32
|
+
import { writtenPathsFor, type ReviewGateConfig } from './review-gate'
|
|
33
|
+
import { commandFor } from './command-taps'
|
|
34
|
+
import { detectProjectBaseline, evaluateBaseline, hasSourceWrite } from './verification-baseline'
|
|
35
|
+
import type {
|
|
36
|
+
BaselineReport,
|
|
37
|
+
TurnCommand,
|
|
38
|
+
VerificationBaselineConfig,
|
|
39
|
+
} from './verification-baseline'
|
|
31
40
|
import { noteToolOutcome, reviewLedger } from './merge-gate'
|
|
32
41
|
import { unprovenCountClaims } from './number-provenance'
|
|
33
42
|
import type { InstructionsLoader } from './instructions'
|
|
@@ -461,6 +470,39 @@ export class QueryEngine {
|
|
|
461
470
|
private instructionsLoader?: InstructionsLoader
|
|
462
471
|
/** Files touched in the current turn (for rules matching). */
|
|
463
472
|
private touchedFiles: Set<string> = new Set()
|
|
473
|
+
/** Review gate: successful file-write operations this turn (two write paths feed this). */
|
|
474
|
+
private reviewWriteOps = 0
|
|
475
|
+
/** Review gate: the paths written this turn, for the reviewer's brief. */
|
|
476
|
+
private turnWrites: Set<string> = new Set()
|
|
477
|
+
/**
|
|
478
|
+
* Review gate: increments at the start of every turn. A write callback
|
|
479
|
+
* captured by a background/delegated tool keeps the generation it was
|
|
480
|
+
* created in, so a late write from a finished turn cannot open this turn's
|
|
481
|
+
* gate.
|
|
482
|
+
*/
|
|
483
|
+
private reviewTurnGeneration = 0
|
|
484
|
+
/** Review gate: config + runner. Absent means the gate is off. */
|
|
485
|
+
private reviewGate?: ReviewGateConfig
|
|
486
|
+
/** Review gate: reviews spent this turn. */
|
|
487
|
+
private reviewRounds = 0
|
|
488
|
+
/** Review gate: write generation already passed. -1 = nothing passed yet. */
|
|
489
|
+
private reviewedAtOps = -1
|
|
490
|
+
/** Review gate: this turn's user input (the reviewer's requirement). */
|
|
491
|
+
private turnPrompt = ''
|
|
492
|
+
/** Verification baseline: commands observed this turn (both tool paths). */
|
|
493
|
+
private turnCommands: TurnCommand[] = []
|
|
494
|
+
/** Verification baseline: writes with their order, to tell stale runs from fresh ones. */
|
|
495
|
+
private turnWriteEvents: Array<{ path: string; seq: number }> = []
|
|
496
|
+
/** Verification baseline: monotonic order shared by writes and commands. */
|
|
497
|
+
private turnOpSeq = 0
|
|
498
|
+
/** Verification baseline: config. Absent means the baseline is off. */
|
|
499
|
+
private verificationConfig?: VerificationBaselineConfig
|
|
500
|
+
/** Verification baseline: this turn's report (passed to the reviewer). */
|
|
501
|
+
private baselineReport?: BaselineReport
|
|
502
|
+
/** Verification baseline: blocked once already this turn. */
|
|
503
|
+
private verificationBlocked = false
|
|
504
|
+
/** Review gate: last FAIL findings, carried into the next review round. */
|
|
505
|
+
private lastReviewFindings?: string
|
|
464
506
|
/** A tool call failed since the last nudge check. */
|
|
465
507
|
private pendingToolFailure = false
|
|
466
508
|
/** 本轮用户输入 —— 数字出处判定里它是合法的出处之一。 */
|
|
@@ -547,6 +589,28 @@ export class QueryEngine {
|
|
|
547
589
|
}
|
|
548
590
|
}
|
|
549
591
|
|
|
592
|
+
/** Count a successful file write for the review gate. */
|
|
593
|
+
private recordWrite(path: string): void {
|
|
594
|
+
this.reviewWriteOps++
|
|
595
|
+
this.turnWrites.add(path)
|
|
596
|
+
this.turnWriteEvents.push({ path, seq: ++this.turnOpSeq })
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/** Record an observed Bash command (success or failure) for the verification baseline. */
|
|
600
|
+
private recordCommand(command: string, ok: boolean): void {
|
|
601
|
+
this.turnCommands.push({ command, ok, seq: ++this.turnOpSeq })
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/** Wire the verification baseline. Absent/disabled means it never runs. */
|
|
605
|
+
setVerificationBaseline(config: VerificationBaselineConfig): void {
|
|
606
|
+
this.verificationConfig = config
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/** Wire the review gate. Absent/disabled means the gate never runs. */
|
|
610
|
+
setReviewGate(config: ReviewGateConfig): void {
|
|
611
|
+
this.reviewGate = config
|
|
612
|
+
}
|
|
613
|
+
|
|
550
614
|
/**
|
|
551
615
|
* Inject path-scoped context for the files touched this round: matching rules
|
|
552
616
|
* (`.mipham/rules/*.md`) and any nested instruction files (`CLAUDE.md` and
|
|
@@ -774,6 +838,22 @@ export class QueryEngine {
|
|
|
774
838
|
// provider API (which would otherwise 400 on an empty/blank message).
|
|
775
839
|
if (!userInput.trim()) return
|
|
776
840
|
|
|
841
|
+
// Review gate state is per turn: a new user message starts a new write
|
|
842
|
+
// generation, so a gate that passed the previous turn runs again if this
|
|
843
|
+
// one writes files.
|
|
844
|
+
this.reviewTurnGeneration++
|
|
845
|
+
this.reviewWriteOps = 0
|
|
846
|
+
this.turnWrites.clear()
|
|
847
|
+
this.turnCommands = []
|
|
848
|
+
this.turnWriteEvents = []
|
|
849
|
+
this.turnOpSeq = 0
|
|
850
|
+
this.baselineReport = undefined
|
|
851
|
+
this.verificationBlocked = false
|
|
852
|
+
this.reviewRounds = 0
|
|
853
|
+
this.reviewedAtOps = -1
|
|
854
|
+
this.turnPrompt = userInput
|
|
855
|
+
this.lastReviewFindings = undefined
|
|
856
|
+
|
|
777
857
|
// Poll for cross-session messages before each turn
|
|
778
858
|
await this.pollCrossSessionInbox()
|
|
779
859
|
// Drain in-memory bus messages (same-process SendMessage + cross-session re-posts)
|
|
@@ -1669,6 +1749,7 @@ export class QueryEngine {
|
|
|
1669
1749
|
return { success: false, content: reason, error: reason }
|
|
1670
1750
|
}
|
|
1671
1751
|
|
|
1752
|
+
const writeGeneration = this.reviewTurnGeneration
|
|
1672
1753
|
const result = await tool.execute(effectiveParams, {
|
|
1673
1754
|
cwd,
|
|
1674
1755
|
sessionId: this.sessionId,
|
|
@@ -1690,8 +1771,29 @@ export class QueryEngine {
|
|
|
1690
1771
|
// A copy: the sub-agent seeds its own context from this prefix while the
|
|
1691
1772
|
// parent turn keeps appending to its list.
|
|
1692
1773
|
parentMessages: this.context.getMessages().slice(),
|
|
1774
|
+
// Ignore a callback that belongs to an earlier turn: a background
|
|
1775
|
+
// agent holding this function can write after its turn ended.
|
|
1776
|
+
onFileWrite: (p) => {
|
|
1777
|
+
if (writeGeneration === this.reviewTurnGeneration) this.recordWrite(p)
|
|
1778
|
+
},
|
|
1779
|
+
onCommand: (command, ok) => {
|
|
1780
|
+
if (writeGeneration === this.reviewTurnGeneration) this.recordCommand(command, ok)
|
|
1781
|
+
},
|
|
1693
1782
|
})
|
|
1694
1783
|
|
|
1784
|
+
// Review gate: count writes on the main tool funnel (the sub-agent loop
|
|
1785
|
+
// reports its own through `onFileWrite`).
|
|
1786
|
+
if (result.success && writeGeneration === this.reviewTurnGeneration) {
|
|
1787
|
+
for (const p of writtenPathsFor(name, effectiveParams)) this.recordWrite(p)
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
// Verification baseline: observe executed commands. Failure is recorded
|
|
1791
|
+
// too — "ran and failed" is a reading, not "did not run".
|
|
1792
|
+
const command = commandFor(name, effectiveParams)
|
|
1793
|
+
if (command !== undefined && writeGeneration === this.reviewTurnGeneration) {
|
|
1794
|
+
this.recordCommand(command, result.success)
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1695
1797
|
// Track touched files for rules matching
|
|
1696
1798
|
this.trackTouchedFile(name, effectiveParams)
|
|
1697
1799
|
|
|
@@ -2324,6 +2426,32 @@ export class QueryEngine {
|
|
|
2324
2426
|
}
|
|
2325
2427
|
}
|
|
2326
2428
|
|
|
2429
|
+
/** Read-only: are the review gate / verification baseline actually wired on? */
|
|
2430
|
+
getSelfCheckStatus(): { reviewGate: boolean; verification: boolean } {
|
|
2431
|
+
return {
|
|
2432
|
+
reviewGate: Boolean(this.reviewGate?.enabled),
|
|
2433
|
+
verification: Boolean(this.verificationConfig?.enabled),
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
|
|
2437
|
+
/**
|
|
2438
|
+
* Tool context for sub-agents spawned outside `executeTool` (workflow
|
|
2439
|
+
* agents). Only the review gate's write tap is shared; the rest of that
|
|
2440
|
+
* context is built per tool call. The generation is captured here so a
|
|
2441
|
+
* callback outliving this turn does not open the next turn's gate.
|
|
2442
|
+
*/
|
|
2443
|
+
getToolContext(): Partial<ToolContext> {
|
|
2444
|
+
const generation = this.reviewTurnGeneration
|
|
2445
|
+
return {
|
|
2446
|
+
onFileWrite: (p) => {
|
|
2447
|
+
if (generation === this.reviewTurnGeneration) this.recordWrite(p)
|
|
2448
|
+
},
|
|
2449
|
+
onCommand: (command, ok) => {
|
|
2450
|
+
if (generation === this.reviewTurnGeneration) this.recordCommand(command, ok)
|
|
2451
|
+
},
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2454
|
+
|
|
2327
2455
|
/**
|
|
2328
2456
|
* SIS Phase 0: Lazily-initialized PreFlightChecker singleton.
|
|
2329
2457
|
* Checks tool calls against ErrorSignatureDB + ExperienceRuleEngine
|
|
@@ -2532,7 +2660,7 @@ export class QueryEngine {
|
|
|
2532
2660
|
signal?: AbortSignal,
|
|
2533
2661
|
roundsLeft: number = MAX_TOOL_TURNS,
|
|
2534
2662
|
): AsyncGenerator<StreamChunk> {
|
|
2535
|
-
const { block, systemMessage } = await this.stopCheck()
|
|
2663
|
+
const { block, systemMessage } = await this.stopCheck(roundsLeft, signal)
|
|
2536
2664
|
if (systemMessage) yield { type: 'warning', content: systemMessage }
|
|
2537
2665
|
if (!block) return
|
|
2538
2666
|
|
|
@@ -2562,7 +2690,10 @@ export class QueryEngine {
|
|
|
2562
2690
|
* 两条来源,同一个后果:用户配置的 Stop 钩子(它先说话,先到先得),以及内置的
|
|
2563
2691
|
* 数字出处判定。后者不需要钩子引擎 —— 它是产品行为,不是可配置项。
|
|
2564
2692
|
*/
|
|
2565
|
-
private async stopCheck(
|
|
2693
|
+
private async stopCheck(
|
|
2694
|
+
roundsLeft: number = MAX_TOOL_TURNS,
|
|
2695
|
+
signal?: AbortSignal,
|
|
2696
|
+
): Promise<{
|
|
2566
2697
|
block: { source: string; text: string; reason: string } | null
|
|
2567
2698
|
systemMessage?: string
|
|
2568
2699
|
}> {
|
|
@@ -2583,6 +2714,16 @@ export class QueryEngine {
|
|
|
2583
2714
|
}
|
|
2584
2715
|
}
|
|
2585
2716
|
|
|
2717
|
+
const baseline = this.checkVerificationBaseline(roundsLeft)
|
|
2718
|
+
if (baseline.block) {
|
|
2719
|
+
return { block: baseline.block, systemMessage: baseline.systemMessage ?? systemMessage }
|
|
2720
|
+
}
|
|
2721
|
+
systemMessage = baseline.systemMessage ?? systemMessage
|
|
2722
|
+
|
|
2723
|
+
const gate = await this.checkReviewGate(roundsLeft, signal)
|
|
2724
|
+
if (gate.block) return { block: gate.block, systemMessage: gate.systemMessage ?? systemMessage }
|
|
2725
|
+
systemMessage = gate.systemMessage ?? systemMessage
|
|
2726
|
+
|
|
2586
2727
|
const claims = this.unprovenNumbers()
|
|
2587
2728
|
if (!claims) return { block: null, systemMessage }
|
|
2588
2729
|
// 提示语给模型,`reason` 只留数字本身 —— 预算用完时那条告警是给**人**看的,
|
|
@@ -2597,6 +2738,169 @@ export class QueryEngine {
|
|
|
2597
2738
|
}
|
|
2598
2739
|
}
|
|
2599
2740
|
|
|
2741
|
+
/**
|
|
2742
|
+
* Fourth stop source (after the Stop hook, before the review gate): did the
|
|
2743
|
+
* mechanical checks the project implies actually run after the last source
|
|
2744
|
+
* edit? Observation only — nothing is executed here. Missing checks block
|
|
2745
|
+
* once; the report then travels to the reviewer as a known gap.
|
|
2746
|
+
*/
|
|
2747
|
+
private checkVerificationBaseline(roundsLeft: number): {
|
|
2748
|
+
block: { source: string; text: string; reason: string } | null
|
|
2749
|
+
systemMessage?: string
|
|
2750
|
+
} {
|
|
2751
|
+
const config = this.verificationConfig
|
|
2752
|
+
if (!config || !config.enabled) return { block: null }
|
|
2753
|
+
|
|
2754
|
+
// Fail open: a deleted cwd makes `process.cwd()` throw on Node, and a
|
|
2755
|
+
// broken manifest read must not crash the stop path.
|
|
2756
|
+
let baseline
|
|
2757
|
+
try {
|
|
2758
|
+
const cwd = typeof config.cwd === 'function' ? config.cwd() : config.cwd
|
|
2759
|
+
baseline = detectProjectBaseline(cwd)
|
|
2760
|
+
} catch {
|
|
2761
|
+
return { block: null }
|
|
2762
|
+
}
|
|
2763
|
+
if (!baseline) return { block: null }
|
|
2764
|
+
if (
|
|
2765
|
+
!hasSourceWrite(
|
|
2766
|
+
baseline,
|
|
2767
|
+
this.turnWriteEvents.map((e) => e.path),
|
|
2768
|
+
)
|
|
2769
|
+
) {
|
|
2770
|
+
return { block: null }
|
|
2771
|
+
}
|
|
2772
|
+
|
|
2773
|
+
const sourceWrites = this.turnWriteEvents.filter((e) => hasSourceWrite(baseline, [e.path]))
|
|
2774
|
+
const lastSourceWriteSeq = sourceWrites.reduce((max, e) => Math.max(max, e.seq), -1)
|
|
2775
|
+
const report = evaluateBaseline(baseline, this.turnCommands, lastSourceWriteSeq)
|
|
2776
|
+
this.baselineReport = report
|
|
2777
|
+
|
|
2778
|
+
if (report.missing.length === 0) return { block: null }
|
|
2779
|
+
const missing = report.missing.join(', ')
|
|
2780
|
+
|
|
2781
|
+
if (roundsLeft <= 0) {
|
|
2782
|
+
return {
|
|
2783
|
+
block: null,
|
|
2784
|
+
systemMessage: t('errors.verification_budget_spent', { missing }),
|
|
2785
|
+
}
|
|
2786
|
+
}
|
|
2787
|
+
if (this.verificationBlocked) return { block: null }
|
|
2788
|
+
this.verificationBlocked = true
|
|
2789
|
+
return {
|
|
2790
|
+
block: {
|
|
2791
|
+
source: 'verification-baseline',
|
|
2792
|
+
text: t('errors.verification_missing', { missing }),
|
|
2793
|
+
reason: missing,
|
|
2794
|
+
},
|
|
2795
|
+
systemMessage: t('errors.verification_missing', { missing }),
|
|
2796
|
+
}
|
|
2797
|
+
}
|
|
2798
|
+
|
|
2799
|
+
/**
|
|
2800
|
+
* Third stop source: after a turn that wrote files, run one independent
|
|
2801
|
+
* review. FAIL blocks and feeds the findings back; a broken reviewer is
|
|
2802
|
+
* fail-open (a bad gate must not trap the turn).
|
|
2803
|
+
*
|
|
2804
|
+
* `roundsLeft` is checked here rather than in the caller: the caller's budget
|
|
2805
|
+
* warning would throw a FAIL away, and the longest turns are exactly where
|
|
2806
|
+
* files were written.
|
|
2807
|
+
*/
|
|
2808
|
+
private async checkReviewGate(
|
|
2809
|
+
roundsLeft: number,
|
|
2810
|
+
signal?: AbortSignal,
|
|
2811
|
+
): Promise<{
|
|
2812
|
+
block: { source: string; text: string; reason: string } | null
|
|
2813
|
+
systemMessage?: string
|
|
2814
|
+
}> {
|
|
2815
|
+
const gate = this.reviewGate
|
|
2816
|
+
if (!gate || !gate.enabled) return { block: null }
|
|
2817
|
+
if (this.reviewWriteOps === 0 || this.reviewedAtOps === this.reviewWriteOps) {
|
|
2818
|
+
return { block: null }
|
|
2819
|
+
}
|
|
2820
|
+
|
|
2821
|
+
if (this.reviewRounds >= gate.maxRounds) {
|
|
2822
|
+
this.reviewedAtOps = this.reviewWriteOps
|
|
2823
|
+
return {
|
|
2824
|
+
block: null,
|
|
2825
|
+
systemMessage: t('errors.review_gate_budget_spent', { max: String(gate.maxRounds) }),
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
|
|
2829
|
+
this.reviewRounds++
|
|
2830
|
+
let result
|
|
2831
|
+
try {
|
|
2832
|
+
result = await gate.runner({
|
|
2833
|
+
cwd: typeof gate.cwd === 'function' ? gate.cwd() : gate.cwd,
|
|
2834
|
+
userPrompt: this.turnPrompt,
|
|
2835
|
+
changedFiles: [...this.turnWrites],
|
|
2836
|
+
round: this.reviewRounds,
|
|
2837
|
+
maxRounds: gate.maxRounds,
|
|
2838
|
+
previousFindings: this.lastReviewFindings,
|
|
2839
|
+
authorResponse: this.lastAssistantText(),
|
|
2840
|
+
baseline: this.baselineReport,
|
|
2841
|
+
signal,
|
|
2842
|
+
})
|
|
2843
|
+
} catch (err) {
|
|
2844
|
+
result = {
|
|
2845
|
+
status: 'error' as const,
|
|
2846
|
+
message: err instanceof Error ? err.message : String(err),
|
|
2847
|
+
}
|
|
2848
|
+
}
|
|
2849
|
+
|
|
2850
|
+
if (result.status === 'pass') {
|
|
2851
|
+
this.reviewedAtOps = this.reviewWriteOps
|
|
2852
|
+
this.lastReviewFindings = undefined
|
|
2853
|
+
return { block: null }
|
|
2854
|
+
}
|
|
2855
|
+
|
|
2856
|
+
if (result.status === 'error') {
|
|
2857
|
+
this.reviewedAtOps = this.reviewWriteOps
|
|
2858
|
+
return {
|
|
2859
|
+
block: null,
|
|
2860
|
+
systemMessage: t('errors.review_gate_error', { message: result.message }),
|
|
2861
|
+
}
|
|
2862
|
+
}
|
|
2863
|
+
|
|
2864
|
+
this.lastReviewFindings = result.findings
|
|
2865
|
+
// No budget left: blocking would be discarded by the caller, but the
|
|
2866
|
+
// findings still reach the operator as a warning.
|
|
2867
|
+
if (roundsLeft <= 0) {
|
|
2868
|
+
this.reviewedAtOps = this.reviewWriteOps
|
|
2869
|
+
return {
|
|
2870
|
+
block: null,
|
|
2871
|
+
systemMessage: this.capHookContext(`[review gate] ${result.findings}`),
|
|
2872
|
+
}
|
|
2873
|
+
}
|
|
2874
|
+
|
|
2875
|
+
const firstLine = result.findings.split('\n', 1)[0]?.trim() || 'issues found'
|
|
2876
|
+
return {
|
|
2877
|
+
block: {
|
|
2878
|
+
source: 'review-gate',
|
|
2879
|
+
text: `[review gate ${this.reviewRounds}/${gate.maxRounds}]\n${result.findings}`,
|
|
2880
|
+
reason: `review gate round ${this.reviewRounds}/${gate.maxRounds}: ${firstLine}`,
|
|
2881
|
+
},
|
|
2882
|
+
}
|
|
2883
|
+
}
|
|
2884
|
+
|
|
2885
|
+
/** The author's last assistant text, for a round-2 review's rebuttal context. */
|
|
2886
|
+
private lastAssistantText(): string | undefined {
|
|
2887
|
+
const messages = this.context.getMessages()
|
|
2888
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
2889
|
+
const message = messages[i]!
|
|
2890
|
+
if (message.role !== 'assistant') continue
|
|
2891
|
+
const text =
|
|
2892
|
+
typeof message.content === 'string'
|
|
2893
|
+
? message.content
|
|
2894
|
+
: (message.content as Array<{ type: string; text?: string }>)
|
|
2895
|
+
.filter((block) => block.type === 'text')
|
|
2896
|
+
.map((block) => block.text ?? '')
|
|
2897
|
+
.join('\n')
|
|
2898
|
+
const trimmed = text.trim()
|
|
2899
|
+
return trimmed ? this.capHookContext(trimmed) : undefined
|
|
2900
|
+
}
|
|
2901
|
+
return undefined
|
|
2902
|
+
}
|
|
2903
|
+
|
|
2600
2904
|
/**
|
|
2601
2905
|
* 「这个数是你量出来的,还是你推出来的?」
|
|
2602
2906
|
*
|
package/src/core/instructions.ts
CHANGED
|
@@ -235,7 +235,7 @@ export function buildPermissionBlock(mode: string): string {
|
|
|
235
235
|
// staying stale makes it promise a keypress that does nothing. `bypassPermissions`
|
|
236
236
|
// is still reachable, but only by naming it in config; saying so is what keeps
|
|
237
237
|
// the model from offering it as a way out.
|
|
238
|
-
return `## Permission Context\n\n${description}\n\nWhen a tool is denied, do NOT retry it or any other approval-gated tool — Bash, WebSearch, network, and Workflow are all blocked in this mode.${escape} If the task genuinely needs a blocked tool, STOP retrying and ask the user to switch modes with Shift+Tab or add an allow rule (/permissions), then wait for the user's answer. Note that Shift+Tab's wheel does not reach bypassPermissions — that mode is set in config, so do not offer it as a keypress.`
|
|
238
|
+
return `## Permission Context\n\n${description}\n\nWhen a tool is denied, do NOT retry it or any other approval-gated tool — Bash, WebSearch, network, and Workflow are all blocked in this mode.${escape} If the task genuinely needs a blocked tool, STOP retrying and ask the user to switch modes with Shift+Tab or add an allow rule (/permissions), then wait for the user's answer. Note that Shift+Tab's wheel does not reach bypassPermissions — that mode is set in config, so do not offer it as a keypress. Independently of permissions, after a turn that writes files this session runs an independent review gate and a verification baseline unless they are turned off (both are on by default; /status reports their state): expect findings to fix, and report checks as Verified or Not verified.`
|
|
239
239
|
}
|
|
240
240
|
|
|
241
241
|
/** Level label used in each prompt part's provenance comment. */
|