@miphamai/cli 0.85.33 → 0.85.35
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/services.ts +21 -0
- package/src/agent/sub-agent.ts +129 -21
- package/src/commands/environment.ts +14 -11
- package/src/commands/project.ts +43 -12
- package/src/config/loader.ts +65 -35
- package/src/core/alignment-vocabulary.json +2 -2
- package/src/core/constitution-loader.ts +85 -24
- package/src/core/context.ts +22 -5
- package/src/core/engine.ts +62 -143
- package/src/core/hooks-config.ts +32 -0
- package/src/core/memory/memory-loader.ts +2 -2
- package/src/core/permission-rules.ts +67 -12
- package/src/core/session-log.ts +26 -7
- package/src/core/session-store.ts +4 -3
- package/src/core/tool-gate.ts +222 -0
- package/src/daemon/engine-capabilities.ts +124 -2
- package/src/daemon/remote-engine.ts +3 -0
- package/src/daemon/server.ts +13 -1
- package/src/i18n-core/locales/en-US.json +1 -0
- package/src/i18n-core/locales/zh-CN.json +1 -0
- package/src/mcp/client.ts +22 -2
- package/src/plugin/plugin-manager.ts +14 -0
- package/src/shared/package-info.ts +2 -2
- package/src/shared/types.ts +8 -0
- package/src/skills/fork-executor.ts +11 -1
- package/src/skills/marketplace.ts +44 -13
- package/src/skills/registry.ts +12 -1
- package/src/tools/agent/agent.ts +3 -0
- package/src/tools/agent/enter-plan.ts +22 -4
- package/src/tools/agent/exit-plan.ts +9 -1
- package/src/tools/agent/skill.ts +1 -0
- package/src/tools/agent/workflow.ts +1 -0
- package/src/tools/exec/git.ts +113 -69
- package/src/tools/index.ts +2 -2
- package/src/ui/app.tsx +15 -0
- package/src/ui/chat.tsx +7 -4
- package/src/ui/commands.ts +140 -20
- package/src/workflow/primitives/agent.ts +12 -1
- package/src/workflow/primitives/loop.ts +14 -0
- package/src/workflow/primitives/verify.ts +13 -1
- package/src/workflow/runtime.ts +1 -0
package/src/ui/commands.ts
CHANGED
|
@@ -19,6 +19,7 @@ import { McpClient } from '../mcp/client'
|
|
|
19
19
|
import { registerMcpServerTools, unregisterMcpServerTools } from '../mcp/registry'
|
|
20
20
|
import { buildCapabilityReport } from '../core/capability-inventory'
|
|
21
21
|
import { InstructionsLoader } from '../core/instructions'
|
|
22
|
+
import { UNDISPATCHED_EVENTS } from '../core/hooks-config'
|
|
22
23
|
import { findDerivableSections, DERIVABLE_HINTS } from '../core/claude-md-audit'
|
|
23
24
|
import { miphamHome, workflowScriptDir, workflowScriptDirs, worktreeRoot } from '../core/paths.ts'
|
|
24
25
|
import { fixDoctor, fixConfig, fixCache, selectRepoClaudeFiles } from '../core/fix'
|
|
@@ -647,6 +648,20 @@ const switchCmd: CommandHandler = (ctx, args) => {
|
|
|
647
648
|
return { content: t('commands.switch.usage') }
|
|
648
649
|
}
|
|
649
650
|
|
|
651
|
+
// 远程 attach 模式:`switchProvider` 在 RemoteEngine 上是 no-op。从前这里照样
|
|
652
|
+
// 回「✓ Switched」并把 nextProvider/nextModel 交给页脚 —— 页脚换了、daemon 还跑
|
|
653
|
+
// 着原来的模型,一次没发生的切换与一次成功的切换长得一模一样。
|
|
654
|
+
//
|
|
655
|
+
// 放在 provider/API key 检查**之前**:attach 时本地这份配置不是判据 ——
|
|
656
|
+
// 「这个 provider 没配 key」在这里既不真(key 在 daemon 那边)也不是用户能修的东西。
|
|
657
|
+
if (ctx.engine.isRemote) {
|
|
658
|
+
return {
|
|
659
|
+
content:
|
|
660
|
+
`Cannot switch to ${newProvider}/${newModel}: this session is attached to a daemon, ` +
|
|
661
|
+
'and the model is chosen on the daemon side. Change it there, or start a local session.',
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
650
665
|
const provider = ctx.config.providers.find((p) => p.id === newProvider)
|
|
651
666
|
if (!provider) {
|
|
652
667
|
return { content: t('commands.switch.unknown_provider', { provider: newProvider }) }
|
|
@@ -2245,8 +2260,16 @@ const changelogCmd: CommandHandler = async (ctx) => {
|
|
|
2245
2260
|
const lines: string[] = [t('commands.changelog.title'), '']
|
|
2246
2261
|
|
|
2247
2262
|
try {
|
|
2248
|
-
|
|
2249
|
-
|
|
2263
|
+
// A tag name is repository-controlled data, not a word we chose: a cloned
|
|
2264
|
+
// repository can contain `v1$(rm -rf …)`, and a shell string would run it.
|
|
2265
|
+
// `execFileSync` passes argv without a shell, so nothing in a tag is ever
|
|
2266
|
+
// interpreted. The log window is sliced here for the same reason — a
|
|
2267
|
+
// `| head -8` inside the command string is a second shell.
|
|
2268
|
+
const { execFileSync } = await import('node:child_process')
|
|
2269
|
+
const tags = execFileSync('git', ['tag', '--sort=-creatordate'], {
|
|
2270
|
+
encoding: 'utf-8',
|
|
2271
|
+
timeout: 5000,
|
|
2272
|
+
})
|
|
2250
2273
|
.trim()
|
|
2251
2274
|
.split('\n')
|
|
2252
2275
|
.filter(Boolean)
|
|
@@ -2264,13 +2287,14 @@ const changelogCmd: CommandHandler = async (ctx) => {
|
|
|
2264
2287
|
const nextTag = tags[i + 1]
|
|
2265
2288
|
try {
|
|
2266
2289
|
const range = nextTag ? `${nextTag}..${tag}` : tag
|
|
2267
|
-
const log =
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
)
|
|
2290
|
+
const log = execFileSync('git', ['log', '--oneline', '--no-merges', '--format=%s', range], {
|
|
2291
|
+
encoding: 'utf-8',
|
|
2292
|
+
timeout: 5000,
|
|
2293
|
+
})
|
|
2271
2294
|
.trim()
|
|
2272
2295
|
.split('\n')
|
|
2273
2296
|
.filter(Boolean)
|
|
2297
|
+
.slice(0, 8)
|
|
2274
2298
|
|
|
2275
2299
|
const isCurrent = tag === `v${ctx.version}`
|
|
2276
2300
|
lines.push(`### ${tag}${isCurrent ? t('commands.changelog.current_marker') : ''}`, '')
|
|
@@ -4103,6 +4127,57 @@ const cdCmd: CommandHandler = async (ctx, args) => {
|
|
|
4103
4127
|
try {
|
|
4104
4128
|
process.chdir(resolved)
|
|
4105
4129
|
|
|
4130
|
+
// `/cd` 只搬了 cwd,而**按目录装载的闸门**分两半:
|
|
4131
|
+
// • 能在这里重放的 —— `.mipham/rules/*.md` 与 settings.json 的 permission 规则;
|
|
4132
|
+
// • 启动时一次性装载、搬不动的 —— config.yml 的 permissionRules、hooks、
|
|
4133
|
+
// 指令文件(CLAUDE.md 链)。它们仍然属于**原来的**目录。
|
|
4134
|
+
// 后半段必须说出口:否则用户以为自己已经在新项目的策略下,而实际生效的
|
|
4135
|
+
// 是上一个仓库的那一套。重放前半段是纯收紧/纯重放,不会放宽任何东西。
|
|
4136
|
+
const notes: string[] = []
|
|
4137
|
+
try {
|
|
4138
|
+
const { loadSettingsJson } = await import('../config/loader')
|
|
4139
|
+
const { RulesLoader } = await import('../core/rules-loader')
|
|
4140
|
+
// attach 模式下 `getPermission()` 是个只有 `setMode`/`getMode` 的门面
|
|
4141
|
+
// (闸门在 daemon 那边),在本地重放规则既改不动任何东西,也会在
|
|
4142
|
+
// `listRules`/`allow` 上抛 —— 与其把这句内部报错印给用户,不如说清楚归属。
|
|
4143
|
+
if (ctx.engine.isRemote) {
|
|
4144
|
+
notes.push(
|
|
4145
|
+
'This session is attached to a daemon: permission rules and project rules ' +
|
|
4146
|
+
'belong to the daemon, so nothing was reloaded here.',
|
|
4147
|
+
)
|
|
4148
|
+
} else {
|
|
4149
|
+
const perm = ctx.engine.getPermission()
|
|
4150
|
+
const settings = loadSettingsJson(resolved)
|
|
4151
|
+
// `allow`/`deny` 是**追加**语义(`PermissionSystem.allow` 直接 push),
|
|
4152
|
+
// 而用户级 allow 规则启动时已经施加过一遍 —— 不查重的话,每 `/cd` 一次
|
|
4153
|
+
// 规则数组就长一截,`applied` 也会把「重放」报成「新施加」。
|
|
4154
|
+
const live = perm.listRules()
|
|
4155
|
+
let applied = 0
|
|
4156
|
+
for (const rule of settings.permissions.allow) {
|
|
4157
|
+
if (live.has(rule)) continue
|
|
4158
|
+
perm.allow(rule)
|
|
4159
|
+
live.set(rule, 'bypass')
|
|
4160
|
+
applied++
|
|
4161
|
+
}
|
|
4162
|
+
for (const rule of settings.permissions.deny) {
|
|
4163
|
+
if (live.has(rule)) continue
|
|
4164
|
+
perm.deny(rule)
|
|
4165
|
+
live.set(rule, 'ask')
|
|
4166
|
+
applied++
|
|
4167
|
+
}
|
|
4168
|
+
if (applied > 0)
|
|
4169
|
+
notes.push(`Applied ${applied} settings.json permission rule(s) from this directory.`)
|
|
4170
|
+
ctx.engine.setRulesLoader(new RulesLoader(resolved))
|
|
4171
|
+
notes.push('Project rules now read from this directory.')
|
|
4172
|
+
}
|
|
4173
|
+
} catch (err) {
|
|
4174
|
+
notes.push(`Could not reload this directory's policy: ${(err as Error).message}`)
|
|
4175
|
+
}
|
|
4176
|
+
notes.push(
|
|
4177
|
+
'Loaded at startup and still belonging to the original directory until restart: ' +
|
|
4178
|
+
'config.yml permissionRules, hooks, and instruction files (CLAUDE.md chain).',
|
|
4179
|
+
)
|
|
4180
|
+
|
|
4106
4181
|
try {
|
|
4107
4182
|
const { SessionStore } = await import('../core/session-store')
|
|
4108
4183
|
const saved = SessionStore.load(ctx.sessionId)
|
|
@@ -4126,7 +4201,7 @@ const cdCmd: CommandHandler = async (ctx, args) => {
|
|
|
4126
4201
|
/* session persistence is best-effort */
|
|
4127
4202
|
}
|
|
4128
4203
|
|
|
4129
|
-
return { content: t('commands.cd.content', { path: resolved }) }
|
|
4204
|
+
return { content: [t('commands.cd.content', { path: resolved }), '', ...notes].join('\n') }
|
|
4130
4205
|
} catch (err) {
|
|
4131
4206
|
return { content: t('commands.cd.failed', { error: (err as Error).message }) }
|
|
4132
4207
|
}
|
|
@@ -4183,13 +4258,23 @@ const hooksCmd: CommandHandler = async (ctx) => {
|
|
|
4183
4258
|
|
|
4184
4259
|
let count = 0
|
|
4185
4260
|
for (const [eventName, bucket] of byEvent) {
|
|
4261
|
+
// These events have no dispatch point, so `loadHookConfigs` drops them. This
|
|
4262
|
+
// listing reads the *config files*, not the engine's registry, so it has to
|
|
4263
|
+
// apply the same verdict here — otherwise the entry looks installed and the
|
|
4264
|
+
// count says it will run. Rendered, not hidden: the marker is the honest
|
|
4265
|
+
// version of "we saw your hook and it will not fire". Excluded from `count`
|
|
4266
|
+
// because `found N` must mean "these will run".
|
|
4267
|
+
const undispatchable = UNDISPATCHED_EVENTS.has(eventName)
|
|
4186
4268
|
for (const { entry, project } of bucket) {
|
|
4187
4269
|
for (const hook of entry.hooks) {
|
|
4188
|
-
count++
|
|
4270
|
+
if (!undispatchable) count++
|
|
4189
4271
|
const matcher = entry.matcher || '*'
|
|
4190
4272
|
const cmd = hook.type === 'command' && hook.command ? hook.command : hook.type
|
|
4191
4273
|
const source = t(project ? 'commands.hooks.source_project' : 'commands.hooks.source_user')
|
|
4192
|
-
const
|
|
4274
|
+
const markers: string[] = []
|
|
4275
|
+
if (project && gated) markers.push(t('commands.hooks.gated'))
|
|
4276
|
+
if (undispatchable) markers.push(t('commands.hooks.undispatched'))
|
|
4277
|
+
const suffix = markers.map((marker) => ` ${marker}`).join('')
|
|
4193
4278
|
lines.push(` [${source}] ${eventName} [${matcher}] → ${cmd}${suffix}`)
|
|
4194
4279
|
}
|
|
4195
4280
|
}
|
|
@@ -5603,6 +5688,7 @@ Background agents appear in the Agent View dashboard (/agents).`,
|
|
|
5603
5688
|
undefined,
|
|
5604
5689
|
undefined,
|
|
5605
5690
|
ctx.engine.getLlm(),
|
|
5691
|
+
ctx.engine.getSubAgentServices(),
|
|
5606
5692
|
)
|
|
5607
5693
|
try {
|
|
5608
5694
|
// The prompt came from the user typing `/bg …`, so it stays unframed —
|
|
@@ -5742,6 +5828,7 @@ const forkCmd: CommandHandler = async (ctx, args) => {
|
|
|
5742
5828
|
undefined,
|
|
5743
5829
|
undefined,
|
|
5744
5830
|
ctx.engine.getLlm(),
|
|
5831
|
+
ctx.engine.getSubAgentServices(),
|
|
5745
5832
|
)
|
|
5746
5833
|
const parentContext = ctx.engine.getContext()
|
|
5747
5834
|
// The row has to come back from `working` on both outcomes: the fork
|
|
@@ -5755,28 +5842,61 @@ const forkCmd: CommandHandler = async (ctx, args) => {
|
|
|
5755
5842
|
inheritContext: { messages: parentContext.getMessages() },
|
|
5756
5843
|
signal,
|
|
5757
5844
|
})
|
|
5845
|
+
// Same contract as the `worktree add` above: argv, no shell. This block
|
|
5846
|
+
// was the half that still built shell strings — and it interpolated both
|
|
5847
|
+
// `wtPath` (derived from the cwd) and `prompt` (the user's text), so a
|
|
5848
|
+
// directory or a task containing `"`, `$(…)` or `;` ran as commands.
|
|
5849
|
+
//
|
|
5850
|
+
// It also swallowed every failure (`/* best-effort */`) and still
|
|
5851
|
+
// reported "Fork done" with a branch name, so a commit that never
|
|
5852
|
+
// happened and a push that was rejected looked exactly like success.
|
|
5853
|
+
let publish: string
|
|
5758
5854
|
try {
|
|
5759
|
-
|
|
5760
|
-
|
|
5761
|
-
|
|
5855
|
+
execFileSync('git', ['add', '-A'], {
|
|
5856
|
+
cwd: wtPath,
|
|
5857
|
+
stdio: 'ignore',
|
|
5858
|
+
timeout: 10_000,
|
|
5859
|
+
env: internalGitEnv(),
|
|
5860
|
+
})
|
|
5861
|
+
const st = execFileSync('git', ['status', '--porcelain'], {
|
|
5862
|
+
cwd: wtPath,
|
|
5762
5863
|
encoding: 'utf-8',
|
|
5763
5864
|
timeout: 10_000,
|
|
5865
|
+
env: internalGitEnv(),
|
|
5764
5866
|
})
|
|
5765
|
-
if (st.trim()) {
|
|
5766
|
-
|
|
5767
|
-
|
|
5768
|
-
|
|
5769
|
-
|
|
5770
|
-
|
|
5867
|
+
if (!st.trim()) {
|
|
5868
|
+
publish = 'No changes to commit.'
|
|
5869
|
+
} else {
|
|
5870
|
+
execFileSync(
|
|
5871
|
+
'git',
|
|
5872
|
+
['commit', '-m', `feat: ${prompt.slice(0, 60)}\n\n${COAUTHOR_TRAILER}`],
|
|
5873
|
+
{ cwd: wtPath, stdio: 'ignore', timeout: 10_000, env: internalGitEnv() },
|
|
5874
|
+
)
|
|
5875
|
+
try {
|
|
5876
|
+
execFileSync('git', ['push', 'origin', branch], {
|
|
5877
|
+
cwd: wtPath,
|
|
5878
|
+
stdio: 'ignore',
|
|
5879
|
+
timeout: 30_000,
|
|
5880
|
+
env: internalGitEnv(),
|
|
5881
|
+
})
|
|
5882
|
+
publish = `Committed and pushed \`${branch}\` to origin.`
|
|
5883
|
+
} catch (pushErr) {
|
|
5884
|
+
publish =
|
|
5885
|
+
`Committed locally, but the **push failed** (${pushErr instanceof Error ? pushErr.message : String(pushErr)}). ` +
|
|
5886
|
+
`The commit is on \`${branch}\` in \`${wtPath}\` — push it yourself when the remote is reachable.`
|
|
5887
|
+
}
|
|
5771
5888
|
}
|
|
5772
|
-
} catch {
|
|
5773
|
-
|
|
5889
|
+
} catch (err) {
|
|
5890
|
+
publish =
|
|
5891
|
+
`**Auto-commit failed** (${err instanceof Error ? err.message : String(err)}). ` +
|
|
5892
|
+
`Changes are uncommitted in \`${wtPath}\`.`
|
|
5774
5893
|
}
|
|
5775
5894
|
const summary = [
|
|
5776
5895
|
`## Fork done: ${prompt}`,
|
|
5777
5896
|
'',
|
|
5778
5897
|
`Branch: \`${branch}\``,
|
|
5779
5898
|
`Worktree: \`${wtPath}\``,
|
|
5899
|
+
publish,
|
|
5780
5900
|
'',
|
|
5781
5901
|
result || '(no output)',
|
|
5782
5902
|
].join('\n')
|
|
@@ -4,6 +4,7 @@ import type { ProviderRegistry } from '../../providers/registry'
|
|
|
4
4
|
import type { Llm } from '../../providers/llm'
|
|
5
5
|
import type { ToolDefinition } from '../../shared/index.ts'
|
|
6
6
|
import type { PermissionSystem } from '../../core/permission'
|
|
7
|
+
import type { SubAgentServices } from '../../agent/services'
|
|
7
8
|
import { worktreeRoot } from '../../core/paths.ts'
|
|
8
9
|
import { internalGitEnv } from '../../shared/git-env'
|
|
9
10
|
import { validateJSONSchema, formatValidationErrors } from '../schema-validator'
|
|
@@ -37,6 +38,8 @@ export interface WorkflowAgentOpts {
|
|
|
37
38
|
maxRetries?: number
|
|
38
39
|
/** Permission system for sub-agent tool execution (optional). */
|
|
39
40
|
permissionSystem?: PermissionSystem
|
|
41
|
+
/** Hooks, gates and DLP handed down by the engine running the workflow. */
|
|
42
|
+
services?: SubAgentServices
|
|
40
43
|
/** Run the agent in an isolated git worktree (optional). */
|
|
41
44
|
isolation?: 'worktree'
|
|
42
45
|
}
|
|
@@ -115,7 +118,15 @@ export async function workflowAgent(
|
|
|
115
118
|
`Return ONLY the JSON object, no other text. Do not wrap in markdown code fences.`
|
|
116
119
|
}
|
|
117
120
|
|
|
118
|
-
const sub = new SubAgent(
|
|
121
|
+
const sub = new SubAgent(
|
|
122
|
+
registry,
|
|
123
|
+
toolRegistry,
|
|
124
|
+
opts.permissionSystem,
|
|
125
|
+
undefined,
|
|
126
|
+
undefined,
|
|
127
|
+
llm,
|
|
128
|
+
opts.services,
|
|
129
|
+
)
|
|
119
130
|
|
|
120
131
|
// ── Execute with optional schema validation + retry ──
|
|
121
132
|
let result: unknown = ''
|
|
@@ -58,6 +58,14 @@ export async function loopUntilConvergence<T>(
|
|
|
58
58
|
// FAN OUT: all finders run in parallel
|
|
59
59
|
const raw = await parallel(opts.finders.map((f) => () => f()))
|
|
60
60
|
|
|
61
|
+
// A round in which **every** finder failed is not "nothing new was found"
|
|
62
|
+
// — it is "nothing was searched for". Counting it as dry let two crashed
|
|
63
|
+
// finders report `converged: true` with an empty confirmed list. A partial
|
|
64
|
+
// failure is still tolerated (the survivors carry the round).
|
|
65
|
+
if (raw.length > 0 && raw.every((r) => r === null)) {
|
|
66
|
+
throw new Error(`loopUntilConvergence(): every finder failed in round ${rounds}`)
|
|
67
|
+
}
|
|
68
|
+
|
|
61
69
|
// EDGE LOGIC: flatMap + dedup (pure JS, zero tokens)
|
|
62
70
|
const items: T[] = []
|
|
63
71
|
for (const result of raw) {
|
|
@@ -84,6 +92,12 @@ export async function loopUntilConvergence<T>(
|
|
|
84
92
|
// VERIFY: optional quality gate
|
|
85
93
|
if (opts.verify) {
|
|
86
94
|
const judged = await parallel(fresh.map((item) => () => opts.verify!(item)))
|
|
95
|
+
// Same rule one level down: `parallel` turns a throwing verify into null,
|
|
96
|
+
// so "every verify call failed" would otherwise be read as "every finding
|
|
97
|
+
// was refuted" and the loop would converge on nothing.
|
|
98
|
+
if (judged.every((j) => j === null)) {
|
|
99
|
+
throw new Error(`loopUntilConvergence(): every verify call failed in round ${rounds}`)
|
|
100
|
+
}
|
|
87
101
|
for (const j of judged) {
|
|
88
102
|
if (j && j.survives) {
|
|
89
103
|
confirmed.push(j.finding as T)
|
|
@@ -164,7 +164,12 @@ export async function verify(finding: unknown, opts: VerifyOpts): Promise<Verify
|
|
|
164
164
|
|
|
165
165
|
const threshold = opts.threshold ?? defaultThreshold(mode, votes.length)
|
|
166
166
|
const realCount = votes.filter((v) => v.real).length
|
|
167
|
-
|
|
167
|
+
// A fan-out in which every voter failed is not a verdict. `defaultThreshold`
|
|
168
|
+
// returns 0 when there are no votes — correct for "all must agree" and for a
|
|
169
|
+
// majority of zero — so `0 >= 0` reported **survives: true** for adversarial
|
|
170
|
+
// and consensus while `score` said 0. Nothing confirmed the finding, so it is
|
|
171
|
+
// not confirmed.
|
|
172
|
+
const survives = votes.length > 0 && realCount >= threshold
|
|
168
173
|
|
|
169
174
|
return {
|
|
170
175
|
finding,
|
|
@@ -241,6 +246,13 @@ export async function judge(attempts: unknown[], opts: JudgeOpts): Promise<Judge
|
|
|
241
246
|
}
|
|
242
247
|
}
|
|
243
248
|
|
|
249
|
+
// No judge produced a score ⇒ there is no winner. Falling through left
|
|
250
|
+
// `winnerIndex` at its initial 0 and handed back `attempts[0]` as "the
|
|
251
|
+
// winner" — a fabricated verdict for a round in which every judge failed.
|
|
252
|
+
if (scores.length === 0 && scorePrompts.length > 0) {
|
|
253
|
+
throw new Error('judge(): every judge call failed — no attempt could be scored')
|
|
254
|
+
}
|
|
255
|
+
|
|
244
256
|
// Compute winner: highest average total across judges
|
|
245
257
|
const attemptTotals = new Map<number, number>()
|
|
246
258
|
const attemptCounts = new Map<number, number>()
|