gatelight 0.0.0-stage → 0.18.0
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/.antigravity-plugin/plugin.json +9 -0
- package/.claude-plugin/plugin.json +11 -0
- package/.codex-plugin/plugin.json +18 -0
- package/.mcp.json +8 -0
- package/LICENSE +21 -0
- package/README.md +29 -2
- package/bin/modwall +8 -0
- package/bin/node.sh +14 -0
- package/bin/testcraft +8 -0
- package/bin/testpace +8 -0
- package/entry/command +44 -0
- package/hooks/antigravity.sh +24 -0
- package/hooks/codex.json +50 -0
- package/hooks/codex.sh +7 -0
- package/hooks/hooks.json +43 -0
- package/hooks/testpace.ts +25 -0
- package/hooks.json +16 -0
- package/mcp_config.json +8 -0
- package/package.json +23 -4
- package/setup.mjs +99 -0
- package/skills/merge/SKILL.md +42 -0
- package/skills/modular/SKILL.md +75 -0
- package/skills/setup/SKILL.md +101 -0
- package/skills/setup-modwall/SKILL.md +112 -0
- package/skills/setup-testcraft/SKILL.md +23 -0
- package/skills/setup-testcraft/checklist/dev-login.md +20 -0
- package/skills/write-tests/SKILL.md +153 -0
- package/src/base/agent.ts +100 -0
- package/src/base/baseline.ts +54 -0
- package/src/base/commandline.ts +40 -0
- package/src/base/commands.ts +94 -0
- package/src/base/confirm.ts +161 -0
- package/src/base/contract.ts +94 -0
- package/src/base/coverage.ts +46 -0
- package/src/base/entry.ts +90 -0
- package/src/base/excerpt.ts +46 -0
- package/src/base/execute.ts +70 -0
- package/src/base/exit.ts +6 -0
- package/src/base/format.ts +27 -0
- package/src/base/full.ts +160 -0
- package/src/base/gate.ts +251 -0
- package/src/base/git.ts +361 -0
- package/src/base/glob.ts +38 -0
- package/src/base/hook.ts +297 -0
- package/src/base/hookformat.ts +257 -0
- package/src/base/judging.ts +27 -0
- package/src/base/layermap.ts +69 -0
- package/src/base/module.ts +88 -0
- package/src/base/process-group.ts +101 -0
- package/src/base/records.ts +66 -0
- package/src/base/repo.ts +155 -0
- package/src/base/sessions.ts +55 -0
- package/src/base/shell.ts +176 -0
- package/src/base/stop.ts +100 -0
- package/src/base/store.ts +348 -0
- package/src/base/version.ts +24 -0
- package/src/cli.ts +186 -0
- package/src/craft/change.ts +24 -0
- package/src/craft/checklist.ts +157 -0
- package/src/craft/commands.ts +111 -0
- package/src/craft/config.ts +15 -0
- package/src/craft/full.ts +30 -0
- package/src/craft/index.ts +12 -0
- package/src/craft/session.ts +21 -0
- package/src/mcp.ts +79 -0
- package/src/modules.ts +10 -0
- package/src/modwall.ts +8 -0
- package/src/pace/commands.ts +281 -0
- package/src/pace/config.ts +103 -0
- package/src/pace/consent.ts +81 -0
- package/src/pace/focused.ts +208 -0
- package/src/pace/full.ts +38 -0
- package/src/pace/index.ts +16 -0
- package/src/pace/record.ts +84 -0
- package/src/pace/report.ts +335 -0
- package/src/pace/results.ts +243 -0
- package/src/pace/run/node-test-reporter.mjs +34 -0
- package/src/pace/run/node-test.ts +26 -0
- package/src/pace/run/playwright-reporter.mjs +48 -0
- package/src/pace/run/playwright.ts +34 -0
- package/src/pace/run/runner.ts +157 -0
- package/src/pace/run/targets.ts +130 -0
- package/src/pace/run/vitest-launcher.mjs +101 -0
- package/src/pace/run/vitest.ts +22 -0
- package/src/pace/session.ts +35 -0
- package/src/pace/testlist.ts +91 -0
- package/src/pace/tests.ts +22 -0
- package/src/pace/verify.ts +95 -0
- package/src/testcraft.ts +9 -0
- package/src/wall/check.ts +258 -0
- package/src/wall/commands.ts +279 -0
- package/src/wall/config.ts +394 -0
- package/src/wall/database.ts +136 -0
- package/src/wall/dbcheck.ts +389 -0
- package/src/wall/dbrules.ts +213 -0
- package/src/wall/edit.ts +64 -0
- package/src/wall/files.ts +35 -0
- package/src/wall/full.ts +30 -0
- package/src/wall/index.ts +15 -0
- package/src/wall/init.ts +149 -0
- package/src/wall/layermap.ts +124 -0
- package/src/wall/layout.ts +231 -0
- package/src/wall/loosen.ts +157 -0
- package/src/wall/miniprogram.ts +80 -0
- package/src/wall/project.ts +88 -0
- package/src/wall/rules.ts +400 -0
- package/src/wall/session.ts +198 -0
- package/src/wall/sql.ts +510 -0
- package/src/wall/store.ts +39 -0
package/src/base/hook.ts
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// agent hook 的判断:会话开始时给节奏说明,执行命令之前补说节奏和软提示,改文件之前问各模块这次拦不拦,
|
|
2
|
+
// 命令执行完记下 agent 在前台看到了整组结果,要结束这一轮时跑停止门。
|
|
3
|
+
// 这里只看内部事件、只给结论(judge);各 agent 的输入输出长什么样在 hookformat.ts,这里不出现它们的字段名。
|
|
4
|
+
// 合并门不在这里:它是 git 的钩子(gate.ts),git 移动默认分支时检查。
|
|
5
|
+
// 入口在 plugin/hooks/testpace.ts,由它说明是哪个 agent。
|
|
6
|
+
|
|
7
|
+
import { realpathSync, statSync } from 'node:fs'
|
|
8
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path'
|
|
9
|
+
import { fileURLToPath } from 'node:url'
|
|
10
|
+
import { AGENTS, type AgentKind } from './agent.ts'
|
|
11
|
+
import { CONFIRM_TOOL } from './confirm.ts'
|
|
12
|
+
import { CONTRACT_FILE, joined, loadContract } from './contract.ts'
|
|
13
|
+
import { entriesNotPlaced, entryDir, refreshEntries } from './entry.ts'
|
|
14
|
+
import { ensureGate, gateProblem } from './gate.ts'
|
|
15
|
+
import { git } from './git.ts'
|
|
16
|
+
import type { HookEvent, HookVerdict } from './hookformat.ts'
|
|
17
|
+
import { modules } from './module.ts'
|
|
18
|
+
import { defaultTips, openRepo, type Repo } from './repo.ts'
|
|
19
|
+
import { noteSessionRepos, sessionRepos } from './sessions.ts'
|
|
20
|
+
import { invocations } from './shell.ts'
|
|
21
|
+
import { PLUGIN_NAME } from './version.ts'
|
|
22
|
+
import { stopCheck } from './stop.ts'
|
|
23
|
+
import type { SessionState } from './store.ts'
|
|
24
|
+
|
|
25
|
+
/** 落地的规矩:底座自己的一句,跟在各模块的会话说明后面。 */
|
|
26
|
+
export const LANDING_RULE =
|
|
27
|
+
'落到默认分支的代码必须有整组通过记录:合并门在 git 移动默认分支时检查,没测过的提交、合并、cherry-pick、reset 都会被取消。直接跑项目自己的测试、检查命令不留记录。'
|
|
28
|
+
|
|
29
|
+
/** 会话说明:各模块的规矩(module.ts 的 brief),加上落地的规矩。会话从仓库外(比如上一级目录)开始时,说明里写上是哪个仓库。 */
|
|
30
|
+
export function sessionBrief(label = '这个仓库', repo?: Repo): string {
|
|
31
|
+
return [...modules().flatMap(module => module.brief?.(label, repo) ?? []), LANDING_RULE].join('\n')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 会话碰到了这个工作区:让各模块记下自己要记的(会话起点,见 baseline.ts)。会话开始、每条命令之前、每次改文件之前都来,
|
|
36
|
+
* 模块只记第一次。哪个模块出错不挡别的,也不挡会话说明。
|
|
37
|
+
*/
|
|
38
|
+
function noteSession(repo: Repo, sessionId: string): void {
|
|
39
|
+
for (const module of modules()) {
|
|
40
|
+
try {
|
|
41
|
+
module.onSession?.(repo, sessionId)
|
|
42
|
+
} catch {
|
|
43
|
+
// 记不下只是停止门少一个参照,不挡 agent
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 这个会话在这个工作区干活(会话所在目录的工作区,或用改文件的工具改了那里的文件):记进会话状态,停止门只为这些工作区问各模块(stop.ts)。
|
|
50
|
+
* 起点照样在会话第一次碰到工作区时记(命令里 cd 进去也记):会话后来在那里动手改了,它进去之后命令里写的东西也算它的。
|
|
51
|
+
*/
|
|
52
|
+
function noteWorking(repo: Repo, sessionId: string, now: number): void {
|
|
53
|
+
const state = repo.store.session(sessionId, now, () => defaultTips(repo))
|
|
54
|
+
if (state.worked?.includes(repo.root)) return
|
|
55
|
+
repo.store.saveSession(sessionId, { ...state, worked: [...(state.worked ?? []), repo.root] })
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 新仓库最多这么多个提交:还没接入时,会话开始就提出接入。老仓库要先改造,不在这里提。 */
|
|
59
|
+
export const NEW_REPO_COMMITS = 20
|
|
60
|
+
|
|
61
|
+
/** 还没提交过,或提交很少的仓库。 */
|
|
62
|
+
function isNewRepo(root: string): boolean {
|
|
63
|
+
try {
|
|
64
|
+
return Number(git(root, ['rev-list', '--count', 'HEAD'])) <= NEW_REPO_COMMITS
|
|
65
|
+
} catch {
|
|
66
|
+
return true
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 新仓库还没接入时给 agent 的话:照接入说明配好,默认落地前跑全部测试,不用问用户。 */
|
|
71
|
+
export const NEW_REPO =
|
|
72
|
+
'这是个新仓库,还没接入 testpace(根目录没有 testpace.json)。建项目时一起接入:照 gatelight:setup 技能配好 testpace.json 和三条命令,' +
|
|
73
|
+
'落地前跑全部检查和全部测试,这是约定,不用问用户;配完跑 testpace verify 核对。'
|
|
74
|
+
|
|
75
|
+
/** 这个插件放命令的目录:没有固定入口时,插件 bin 目录不在 PATH 上的 agent 只能用完整路径。 */
|
|
76
|
+
const PLUGIN_BIN = fileURLToPath(new URL('../../bin', import.meta.url))
|
|
77
|
+
|
|
78
|
+
/** 各 agent 自己的前提(AGENTS 表里的沙箱、固定入口)。 */
|
|
79
|
+
function agentNotes(agent: AgentKind): string[] {
|
|
80
|
+
const { name, leaveSandbox, needsEntry, confirmCall } = AGENTS[agent]
|
|
81
|
+
const sandbox = leaveSandbox !== undefined
|
|
82
|
+
const notes: string[] = []
|
|
83
|
+
if (sandbox) notes.push(`在 ${name} 里:testpace 的命令要写 .git/testpace/、项目的测试常要开端口和网络,在沙箱里跑不了,每次${leaveSandbox}。`)
|
|
84
|
+
if (confirmCall) notes.push(`在 ${name} 里,${PLUGIN_NAME} 的 ${CONFIRM_TOOL} 工具${confirmCall(CONFIRM_TOOL)}。`)
|
|
85
|
+
const absent = needsEntry ? entriesNotPlaced() : []
|
|
86
|
+
if (absent.length) {
|
|
87
|
+
const gate = absent.includes('testpace') ? '合并门的 git 钩子也找不到 testpace(找不到就不检查)。' : ''
|
|
88
|
+
notes.push(
|
|
89
|
+
`这台机器还没放 ${absent.join('、')} 的固定命令入口(放在 ${entryDir()}),${name} 的命令里找不到${absent.length > 1 ? '它们' : '它'}。${gate}` +
|
|
90
|
+
`这是全局安装,先问用户;同意后${sandbox ? '在沙箱外' : ''}跑 ${join(PLUGIN_BIN, 'testpace')} install。在那之前用 ${PLUGIN_BIN} 下的同名文件代替。`,
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
return notes
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 结论里提到的技能(插件名:技能名)按这个 agent 的叫法写(AGENTS 表的 skill),没有换法的照原样。
|
|
98
|
+
* 钩子说的话都在这里换,判断各处照 Claude Code 的叫法写;命令自己的输出(合并门、modwall 的话)不经过这里,照原样。
|
|
99
|
+
*/
|
|
100
|
+
function inAgentWords(verdict: HookVerdict, agent: AgentKind): HookVerdict {
|
|
101
|
+
const skill = AGENTS[agent].skill
|
|
102
|
+
if (!skill) return verdict
|
|
103
|
+
const pattern = new RegExp(`\\b${PLUGIN_NAME}:([a-z][a-z-]*)`, 'g')
|
|
104
|
+
return Object.fromEntries(Object.entries(verdict).map(([key, text]) => [key, text.replace(pattern, (_: string, name: string) => skill(name))]))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** 给 agent 的会话说明;testpace.json 有问题、合并门没在把关时加上原因,再加各模块的提醒(比如时好时坏的测试)和这个 agent 自己的前提。 */
|
|
108
|
+
function told(repo: Repo, agent: AgentKind, label?: string): string {
|
|
109
|
+
const problems = loadContract(repo.root)?.problems ?? []
|
|
110
|
+
const broken = problems.length ? `${CONTRACT_FILE} 有问题:${problems.join(';')}。` : undefined
|
|
111
|
+
const gate = gateProblem(repo.root)
|
|
112
|
+
const alerts = modules().flatMap(module => module.alerts?.(repo) ?? [])
|
|
113
|
+
return [sessionBrief(label, repo), ...(broken ? [broken] : []), ...(gate ? [gate] : []), ...alerts, ...agentNotes(agent)].join('\n')
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** 这些目录所在的、接入了 testpace 的仓库(工作区根目录有 testpace.json),按根目录去重。 */
|
|
117
|
+
function contractRepos(dirs: Iterable<string>): Repo[] {
|
|
118
|
+
const roots = new Map<string, Repo>()
|
|
119
|
+
for (const dir of new Set(dirs)) {
|
|
120
|
+
const repo = openRepo(dir)
|
|
121
|
+
if (repo && !roots.has(repo.root) && joined(repo.root)) roots.set(repo.root, repo)
|
|
122
|
+
}
|
|
123
|
+
return [...roots.values()]
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 这条命令碰到的接入仓库(会话所在目录和命令里 cd 进去的目录;同一条命令里多数调用在同一个目录,按目录去重)。
|
|
128
|
+
* 每个仓库都记下会话,插件数据目录里记下会话碰过它;每个工作区都让各模块记下会话在那里的起点(节奏按仓库说一次,起点按工作区各记一份:
|
|
129
|
+
* 同一个仓库的另一个工作区不会再说节奏,起点照样要记)。会话所在目录的工作区记成它干活的地方;cd 进去的不算(noteWorking)。
|
|
130
|
+
* 返回还没给这个会话说过节奏的。
|
|
131
|
+
* 已知的缺口:会话没进某个工作区、从外面按路径往里写了文件(cp、重定向这类),这里看不到,那个工作区的起点要到会话进去时才记,
|
|
132
|
+
* 已经带着它写的东西;停止门不提醒这一部分,合并前的整组照样查(整体设计方案 9.4)。
|
|
133
|
+
*/
|
|
134
|
+
function untoldRepos(command: string, cwd: string, sessionId: string, now: number): Array<{ repo: Repo; state: SessionState }> {
|
|
135
|
+
const repos = contractRepos([cwd, ...invocations(command, cwd).map(call => call.cwd)])
|
|
136
|
+
noteSessionRepos(sessionId, repos.map(repo => repo.root), now)
|
|
137
|
+
const here = openRepo(cwd)?.root
|
|
138
|
+
const home = repos.find(repo => repo.root === here)
|
|
139
|
+
if (home) noteWorking(home, sessionId, now)
|
|
140
|
+
const untold: Array<{ repo: Repo; state: SessionState }> = []
|
|
141
|
+
for (const repo of repos) {
|
|
142
|
+
const state = repo.store.session(sessionId, now, () => defaultTips(repo))
|
|
143
|
+
noteSession(repo, sessionId)
|
|
144
|
+
if (!state.rhythm) untold.push({ repo, state })
|
|
145
|
+
}
|
|
146
|
+
return untold
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 会话要用改文件的工具改这个工作区的文件:改之前先记起点(先用改文件的工具改、后来才在这个工作区跑命令的,起点也得是改之前的样子),
|
|
151
|
+
* 记下会话碰过它(改的可以是会话所在目录以外的工作区,停止门才查得到那里),记成会话干活的工作区。
|
|
152
|
+
*/
|
|
153
|
+
function noteEditing(repo: Repo, sessionId: string, now: number): void {
|
|
154
|
+
noteSession(repo, sessionId)
|
|
155
|
+
noteSessionRepos(sessionId, [repo.root], now)
|
|
156
|
+
noteWorking(repo, sessionId, now)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 要改的文件在哪个接入了的仓库里、相对仓库根目录的路径。目标可以不在会话所在的仓库里,按它自己所在的仓库算;
|
|
161
|
+
* 还不存在的文件按最近的已有目录找仓库。不在接入的仓库里时没有。
|
|
162
|
+
*/
|
|
163
|
+
function editTarget(cwd: string, target: string): { repo: Repo; path: string } | undefined {
|
|
164
|
+
if (target === '') return undefined
|
|
165
|
+
const file = isAbsolute(target) ? target : resolve(cwd, target)
|
|
166
|
+
let dir = dirname(file)
|
|
167
|
+
while (!statSync(dir, { throwIfNoEntry: false })?.isDirectory()) {
|
|
168
|
+
const up = dirname(dir)
|
|
169
|
+
if (up === dir) return undefined
|
|
170
|
+
dir = up
|
|
171
|
+
}
|
|
172
|
+
const repo = openRepo(dir)
|
|
173
|
+
if (!repo || !joined(repo.root)) return undefined
|
|
174
|
+
// git 给的是真实路径(软链展开了,比如 macOS 的 /var 是 /private/var),目标路径也按真实路径算
|
|
175
|
+
const path = relative(repo.root, join(realpathSync(dir), relative(dir, file))).split('\\').join('/')
|
|
176
|
+
if (path === '' || path.startsWith('../')) return undefined
|
|
177
|
+
return { repo, path }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** 改文件之前:问各模块拦不拦(module.ts 的 beforeEdit),有一个说了原因就拒绝。text 是这次要写进文件的新文字。 */
|
|
181
|
+
function editRefusal({ repo, path }: { repo: Repo; path: string }, text: string | undefined): string | undefined {
|
|
182
|
+
for (const module of modules()) {
|
|
183
|
+
const reason = module.beforeEdit?.(repo, path, text)
|
|
184
|
+
if (reason) return reason
|
|
185
|
+
}
|
|
186
|
+
return undefined
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 判断:只看内部事件(hookformat.ts 的 HookEvent),只给结论。agent:hook 来自哪个 agent(入口参数说明),决定会话说明里要补充哪些前提。
|
|
191
|
+
* 什么都不用说、不用拦时返回 undefined。
|
|
192
|
+
*/
|
|
193
|
+
export function judge(event: HookEvent, now: number, agent: AgentKind): HookVerdict | undefined {
|
|
194
|
+
const { cwd, sessionId } = event
|
|
195
|
+
|
|
196
|
+
// 确认工具:用户当场在弹窗里看到原文、点了同意才执行。没填原文的不弹空窗,拒掉让 agent 照检查输出填
|
|
197
|
+
if (event.kind === 'confirm') {
|
|
198
|
+
return event.text?.trim() ? { ask: event.text } : { deny: `${CONFIRM_TOOL} 的 text 没填:照要求确认的那条检查输出的原样填。` }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (event.kind === 'start') {
|
|
202
|
+
// 旧版本放的固定入口先换成这一版的:下面的会话说明、之后的命令和合并门的钩子都靠它们找到命令
|
|
203
|
+
refreshEntries()
|
|
204
|
+
// 会话所在目录的仓库,加上会话之前碰过的仓库(压缩、恢复后重新开始时上下文里的节奏说明可能没了)。
|
|
205
|
+
const hereRepo = openRepo(cwd)
|
|
206
|
+
const here = hereRepo?.root
|
|
207
|
+
const repos = contractRepos([cwd, ...sessionRepos(sessionId)])
|
|
208
|
+
const fresh = hereRepo && !joined(hereRepo.root) && isNewRepo(hereRepo.root) ? hereRepo : undefined
|
|
209
|
+
if (repos.length === 0 && !fresh) return undefined
|
|
210
|
+
// 用得上合并门的会话里自动配上它,只告诉用户一声
|
|
211
|
+
const notice = ensureGate((repos[0] ?? fresh)!.root)
|
|
212
|
+
for (const repo of repos) {
|
|
213
|
+
const state = repo.store.session(sessionId, now, () => defaultTips(repo))
|
|
214
|
+
repo.store.saveSession(sessionId, { ...state, rhythm: true })
|
|
215
|
+
noteSession(repo, sessionId)
|
|
216
|
+
if (repo.root === here) noteWorking(repo, sessionId, now)
|
|
217
|
+
}
|
|
218
|
+
noteSessionRepos(sessionId, repos.map(repo => repo.root), now)
|
|
219
|
+
const context = [
|
|
220
|
+
...repos.map(repo => (repo.root === here ? told(repo, agent) : told(repo, agent, `${repo.root} 这个仓库`))),
|
|
221
|
+
...(fresh ? [NEW_REPO] : []),
|
|
222
|
+
].join('\n\n')
|
|
223
|
+
return { context, ...(notice ? { notice } : {}) }
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (event.kind === 'edit') {
|
|
227
|
+
const target = editTarget(cwd, event.file)
|
|
228
|
+
if (!target) return undefined
|
|
229
|
+
noteEditing(target.repo, sessionId, now)
|
|
230
|
+
const deny = editRefusal(target, event.text)
|
|
231
|
+
return deny ? { deny } : undefined
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// 补丁:只记下改到的工作区(起点、碰过、干活的地方)。改文件之前的检查(beforeEdit)不接:一段补丁里有几个文件,修改的部分只有差异
|
|
235
|
+
if (event.kind === 'patch') {
|
|
236
|
+
for (const path of event.files) {
|
|
237
|
+
const target = editTarget(cwd, path)
|
|
238
|
+
if (target) noteEditing(target.repo, sessionId, now)
|
|
239
|
+
}
|
|
240
|
+
return undefined
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (event.kind === 'command') {
|
|
244
|
+
// 记下会话第一次出现的时间:停止门只管这之后起的整组。会话不在仓库里开始(会话开始时没说节奏)时,
|
|
245
|
+
// 第一次碰到接入的仓库就补说一次。
|
|
246
|
+
const untold = untoldRepos(event.command, cwd, sessionId, now)
|
|
247
|
+
for (const { repo, state } of untold) repo.store.saveSession(sessionId, { ...state, rhythm: true })
|
|
248
|
+
const briefs = untold.map(({ repo }) => told(repo, agent, `${repo.root} 这个仓库`))
|
|
249
|
+
let hints = modules().flatMap(module => module.beforeCommand?.(event.command, cwd) ?? [])
|
|
250
|
+
if (AGENTS[agent].hooks.beforeCommand === 'refuse') {
|
|
251
|
+
// 只能拒绝的 agent:说过节奏的会话不会再补说,提示(比如直接跑 full 的命令)却每次都有,同一段提示一个会话只拦一次,
|
|
252
|
+
// 拦过的记在会话所在工作区的会话状态里;会话不在接入的仓库里时没处记,提示就不说(不拦)
|
|
253
|
+
const repo = openRepo(cwd)
|
|
254
|
+
const state = repo && joined(repo.root) ? repo.store.session(sessionId, now, () => defaultTips(repo)) : undefined
|
|
255
|
+
hints = state ? hints.filter(hint => !state.refused?.includes(hint)) : []
|
|
256
|
+
if (repo && state && hints.length > 0) repo.store.saveSession(sessionId, { ...state, refused: [...(state.refused ?? []), ...hints] })
|
|
257
|
+
const reason = [...briefs, ...hints].join('\n\n')
|
|
258
|
+
return reason ? { deny: `${reason}\n\n(这条命令被 gatelight 拦了一次,只为了把上面的话交给你;看过后照它做,要跑的话再跑一次,同样的话不会再拦。)` } : undefined
|
|
259
|
+
}
|
|
260
|
+
const context = [...briefs, ...hints].join('\n\n')
|
|
261
|
+
return context ? { context } : undefined
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (event.kind === 'ran') {
|
|
265
|
+
// 在前台跑完 testpace full,agent 已经看到了结果(失败的也是);放到后台的要等它看 testpace status
|
|
266
|
+
if (event.background) return undefined
|
|
267
|
+
for (const call of invocations(event.command, cwd)) {
|
|
268
|
+
if (basename(call.argv[0] ?? '') !== 'testpace' || call.argv[1] !== 'full') continue
|
|
269
|
+
const repo = openRepo(call.cwd)
|
|
270
|
+
if (repo) repo.store.markRead(sessionId, repo.root, now, () => defaultTips(repo))
|
|
271
|
+
}
|
|
272
|
+
return undefined
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// 要结束这一轮:会话所在目录的仓库和会话碰过的仓库都查;不是所在目录的,话里写上是哪个仓库。
|
|
276
|
+
const here = openRepo(cwd)?.root
|
|
277
|
+
const blocks: string[] = []
|
|
278
|
+
const warns: string[] = []
|
|
279
|
+
for (const repo of contractRepos([cwd, ...sessionRepos(sessionId)])) {
|
|
280
|
+
const result = stopCheck({ sessionId, cwd: repo.root, now, named: repo.root !== here })
|
|
281
|
+
if (result.block) blocks.push(result.block)
|
|
282
|
+
if (result.warn) warns.push(result.warn)
|
|
283
|
+
}
|
|
284
|
+
if (blocks.length === 0 && warns.length === 0) return undefined
|
|
285
|
+
return {
|
|
286
|
+
...(blocks.length ? { block: blocks.join('\n') } : {}),
|
|
287
|
+
...(warns.length ? { notice: warns.join('\n') } : {}),
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** 一次 hook:把 agent 的输入读成内部事件,判断,把结论写成这个 agent 的输出。读写用哪一组看 AGENTS 表。没有要输出的返回 undefined。 */
|
|
292
|
+
export function handle(input: unknown, now: number, agent: AgentKind): unknown {
|
|
293
|
+
const format = AGENTS[agent].hooks
|
|
294
|
+
const event = format.read(input)
|
|
295
|
+
const verdict = event && judge(event, now, agent)
|
|
296
|
+
return event && verdict ? format.write(event, inAgentWords(verdict, agent)) : undefined
|
|
297
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// 各 agent 的 hook 输入输出格式。hook 分三段:读(这里的 read)把 agent 的输入转成同一种内部事件,判断(hook.ts 的 judge)只看内部事件、
|
|
2
|
+
// 只给结论,写(这里的 write)把结论编成这个 agent 的输出。判断那一段不出现任何 agent 的字段名。
|
|
3
|
+
// 哪个 agent 用哪一组读写记在 AGENTS 表里(agent.ts),别处不判断 agent 的种类。
|
|
4
|
+
// Claude Code、Codex、dsh 共用一组:格式一样(Codex 实测:执行命令的工具也叫 Bash,输入是字符串的 command),
|
|
5
|
+
// 工具名的大小写不一样(dsh 是 bash),读的时候统一。Antigravity 另一组(字段是驼峰写法,工具名、怎么拒绝、怎么让 agent 继续都不同)。
|
|
6
|
+
|
|
7
|
+
import { PLUGIN_NAME } from './version.ts'
|
|
8
|
+
|
|
9
|
+
interface Session {
|
|
10
|
+
sessionId: string
|
|
11
|
+
/** 会话所在目录。 */
|
|
12
|
+
cwd: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** 内部事件:判断只认这几种。读不出其中一种的输入(别的事件、别的工具、缺了要用的字段)不是事件,不判断。 */
|
|
16
|
+
export type HookEvent = Session &
|
|
17
|
+
(
|
|
18
|
+
| { kind: 'start' }
|
|
19
|
+
/** 要用改文件的工具改一个文件:路径(相对会话所在目录,或绝对路径),和这次要写进去的新文字(拿得到时)。可以拒绝。 */
|
|
20
|
+
| { kind: 'edit'; file: string; text?: string }
|
|
21
|
+
/** 要用一段补丁改这几个文件(Codex 的 apply_patch):只记下改到的工作区,不检查(一段补丁里有几个文件,修改的部分只有差异)。 */
|
|
22
|
+
| { kind: 'patch'; files: string[] }
|
|
23
|
+
/** 要执行一条命令。 */
|
|
24
|
+
| { kind: 'command'; command: string }
|
|
25
|
+
/** 一条命令执行完了。放到后台的,agent 这时还没看到它的输出。 */
|
|
26
|
+
| { kind: 'ran'; command: string; background: boolean }
|
|
27
|
+
| { kind: 'stop' }
|
|
28
|
+
/**
|
|
29
|
+
* 要调 gatelight 自己的 MCP 服务(src/mcp.ts,只有确认工具 confirm_with_user):text 是要给用户看的原文(调用里填的,没填是 undefined)。
|
|
30
|
+
* 只有工具自己不能保证每次弹窗的 agent 才读出这种事件(Antigravity);Claude Code 靠工具自己的标记弹窗,不经过 hook。
|
|
31
|
+
*/
|
|
32
|
+
| { kind: 'confirm'; text?: string }
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
/** 判断的结论。什么都不用说、不用拦时没有结论(undefined),不是空对象。 */
|
|
36
|
+
export interface HookVerdict {
|
|
37
|
+
/** 给模型的话。 */
|
|
38
|
+
context?: string
|
|
39
|
+
/** 给用户看的话。 */
|
|
40
|
+
notice?: string
|
|
41
|
+
/** 拒绝这次工具调用的原因。 */
|
|
42
|
+
deny?: string
|
|
43
|
+
/** 不让结束这一轮的原因。 */
|
|
44
|
+
block?: string
|
|
45
|
+
/** 这次工具调用要用户当场同意才执行:给用户看的原文。只对确认工具的调用给(confirm 事件)。 */
|
|
46
|
+
ask?: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface HookFormat {
|
|
50
|
+
/** agent 的输入转成内部事件;不归 hook 管的输入返回 undefined。 */
|
|
51
|
+
read(input: unknown): HookEvent | undefined
|
|
52
|
+
/** 结论编成这个 agent 的输出(写到标准输出的 JSON);什么都不用输出时返回 undefined。 */
|
|
53
|
+
write(event: HookEvent, verdict: HookVerdict): unknown
|
|
54
|
+
/** hook 自己出错时的输出:不挡 agent,但要让人看得到。什么都不能输出的 agent 返回 undefined(错误只写到标准错误)。 */
|
|
55
|
+
failure(message: string): unknown
|
|
56
|
+
/**
|
|
57
|
+
* 执行命令之前给模型的话怎么送:note 是加在这次调用上、命令照跑;refuse 是这个 agent 没有只加一句话的写法,
|
|
58
|
+
* 只能拒绝这次调用、在原因里说(同一段话一个会话只拦一次,hook.ts)。
|
|
59
|
+
*/
|
|
60
|
+
beforeCommand: 'note' | 'refuse'
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface ClaudeInput {
|
|
64
|
+
hook_event_name?: string
|
|
65
|
+
session_id?: string
|
|
66
|
+
cwd?: string
|
|
67
|
+
tool_name?: string
|
|
68
|
+
tool_input?: { command?: unknown; run_in_background?: unknown; file_path?: unknown; content?: unknown; new_string?: unknown; edits?: unknown }
|
|
69
|
+
tool_response?: unknown
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 改文件的工具(Claude Code 的,和 hooks.json 里的 matcher 一样)。Codex 的 apply_patch 另算(patchFiles)。 */
|
|
73
|
+
const EDIT_TOOLS = new Set(['Edit', 'Write', 'MultiEdit'])
|
|
74
|
+
|
|
75
|
+
/** 改文件的工具这次要写进文件的新文字:Write 的整份内容,Edit、MultiEdit 替换进去的那几段。 */
|
|
76
|
+
function writtenText(input: ClaudeInput['tool_input']): string | undefined {
|
|
77
|
+
const parts: string[] = []
|
|
78
|
+
if (typeof input?.content === 'string') parts.push(input.content)
|
|
79
|
+
if (typeof input?.new_string === 'string') parts.push(input.new_string)
|
|
80
|
+
if (Array.isArray(input?.edits)) {
|
|
81
|
+
for (const item of input.edits as unknown[]) {
|
|
82
|
+
const text = (item as { new_string?: unknown } | null)?.new_string
|
|
83
|
+
if (typeof text === 'string') parts.push(text)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return parts.length > 0 ? parts.join('\n') : undefined
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Codex 的 apply_patch 补丁(tool_input.command 是补丁正文)要改的文件:新建、修改、删除的,和改名后的新路径。
|
|
91
|
+
* 路径相对会话所在目录,也可以是绝对路径。
|
|
92
|
+
*/
|
|
93
|
+
function patchFiles(patch: string): string[] {
|
|
94
|
+
return patch.split('\n').flatMap(line => /^\*\*\* (?:(?:Add|Update|Delete) File|Move to): (.+?)\s*$/.exec(line)?.[1] ?? [])
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 内部事件在 Claude Code 的格式里叫什么(hookSpecificOutput 要写明是哪个事件)。 */
|
|
98
|
+
const CLAUDE_EVENT: Record<HookEvent['kind'], string> = {
|
|
99
|
+
start: 'SessionStart',
|
|
100
|
+
edit: 'PreToolUse',
|
|
101
|
+
patch: 'PreToolUse',
|
|
102
|
+
command: 'PreToolUse',
|
|
103
|
+
ran: 'PostToolUse',
|
|
104
|
+
stop: 'Stop',
|
|
105
|
+
confirm: 'PreToolUse',
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Claude Code 的 hook 格式;Codex、dsh 也用它。 */
|
|
109
|
+
export const CLAUDE_HOOKS: HookFormat = {
|
|
110
|
+
read(raw) {
|
|
111
|
+
const input = raw as ClaudeInput
|
|
112
|
+
const session: Session = { sessionId: input.session_id ?? 'unknown', cwd: input.cwd ?? process.cwd() }
|
|
113
|
+
const event = input.hook_event_name
|
|
114
|
+
if (event === 'SessionStart') return { ...session, kind: 'start' }
|
|
115
|
+
if (event === 'Stop') return { ...session, kind: 'stop' }
|
|
116
|
+
const tool = input.tool_name ?? ''
|
|
117
|
+
if (event === 'PreToolUse' && EDIT_TOOLS.has(tool)) {
|
|
118
|
+
const file = input.tool_input?.file_path
|
|
119
|
+
if (typeof file !== 'string') return undefined
|
|
120
|
+
const text = writtenText(input.tool_input)
|
|
121
|
+
return { ...session, kind: 'edit', file, ...(text === undefined ? {} : { text }) }
|
|
122
|
+
}
|
|
123
|
+
// tool_input.command 是字符串的不都是命令:Codex 改文件的 apply_patch 输入里也有(补丁正文),不能当命令解析
|
|
124
|
+
const command = typeof input.tool_input?.command === 'string' ? input.tool_input.command : undefined
|
|
125
|
+
if (!command) return undefined
|
|
126
|
+
if (event === 'PreToolUse' && tool.toLowerCase() === 'apply_patch') return { ...session, kind: 'patch', files: patchFiles(command) }
|
|
127
|
+
if (tool.toLowerCase() !== 'bash') return undefined
|
|
128
|
+
if (event === 'PreToolUse') return { ...session, kind: 'command', command }
|
|
129
|
+
// 命令退出码非零时 Claude Code 发的是 PostToolUseFailure:失败的输出 agent 同样看到了
|
|
130
|
+
if (event === 'PostToolUse' || event === 'PostToolUseFailure') {
|
|
131
|
+
// 放到后台的:Claude Code 的 run_in_background,或者它自己把命令转到了后台(tool_response 里有后台任务号)
|
|
132
|
+
const background =
|
|
133
|
+
input.tool_input?.run_in_background === true ||
|
|
134
|
+
typeof (input.tool_response as { backgroundTaskId?: unknown } | undefined)?.backgroundTaskId === 'string'
|
|
135
|
+
return { ...session, kind: 'ran', command, background }
|
|
136
|
+
}
|
|
137
|
+
return undefined
|
|
138
|
+
},
|
|
139
|
+
write(event, verdict) {
|
|
140
|
+
const specific = verdict.deny
|
|
141
|
+
? { permissionDecision: 'deny', permissionDecisionReason: verdict.deny }
|
|
142
|
+
: verdict.context
|
|
143
|
+
? { additionalContext: verdict.context }
|
|
144
|
+
: undefined
|
|
145
|
+
return {
|
|
146
|
+
...(specific ? { hookSpecificOutput: { hookEventName: CLAUDE_EVENT[event.kind], ...specific } } : {}),
|
|
147
|
+
...(verdict.block ? { decision: 'block', reason: verdict.block } : {}),
|
|
148
|
+
...(verdict.notice ? { systemMessage: verdict.notice } : {}),
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
failure(message) {
|
|
152
|
+
return { systemMessage: message }
|
|
153
|
+
},
|
|
154
|
+
beforeCommand: 'note',
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
interface AntigravityInput {
|
|
158
|
+
hookEventName?: string
|
|
159
|
+
conversationId?: string
|
|
160
|
+
/** agent 的工作目录(它的命令默认在这里跑),不是 hook 进程的工作目录。 */
|
|
161
|
+
cwd?: string
|
|
162
|
+
toolCall?: {
|
|
163
|
+
name?: string
|
|
164
|
+
args?: { CommandLine?: unknown; Path?: unknown; Content?: unknown; Replacement?: unknown; ServerName?: unknown; Arguments?: unknown }
|
|
165
|
+
}
|
|
166
|
+
terminationReason?: string
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Antigravity 里 gatelight 的 MCP 服务叫什么:插件名_服务名(实测),服务名就是插件名(mcp_config.json)。
|
|
171
|
+
* 调确认工具要用 call_mcp_tool、ServerName 填它。这个服务只有确认工具,所以认调用只看服务名(hooks/antigravity.sh 出错时也这样认)。
|
|
172
|
+
*/
|
|
173
|
+
export const ANTIGRAVITY_SERVER = `${PLUGIN_NAME}_${PLUGIN_NAME}`
|
|
174
|
+
|
|
175
|
+
/** call_mcp_tool 的 Arguments 里的 text:实测是对象;万一是一段 JSON 文字也认。 */
|
|
176
|
+
function confirmText(args: unknown): string | undefined {
|
|
177
|
+
let value = args
|
|
178
|
+
if (typeof value === 'string') {
|
|
179
|
+
try {
|
|
180
|
+
value = JSON.parse(value)
|
|
181
|
+
} catch {
|
|
182
|
+
return undefined
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const text = (value as { text?: unknown } | null)?.text
|
|
186
|
+
return typeof text === 'string' ? text : undefined
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 给 Antigravity 的弹窗原文:它的弹窗把换行压成一段、不渲染 Markdown(实测),所以每行去掉首尾空白、去掉空行,用空格连成一段;
|
|
191
|
+
* 原文里每项本来就用"1.""2."开头,连起来也分得清。
|
|
192
|
+
*/
|
|
193
|
+
function oneParagraph(text: string): string {
|
|
194
|
+
return text
|
|
195
|
+
.split('\n')
|
|
196
|
+
.map(line => line.trim())
|
|
197
|
+
.filter(Boolean)
|
|
198
|
+
.join(' ')
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Antigravity(桌面 App)的 hook 格式,按 2026-11-24 的实测(Antigravity适配方案.md 1.1):
|
|
203
|
+
* - 跑命令是 run_command(args.CommandLine);改文件的专门工具只有 write_file(args.Path 是绝对路径、args.Content)和
|
|
204
|
+
* edit_file(args.Path、args.Replacement 是换进去的文字;它的输入没实际见过,照工具定义)。删文件、移动文件只能走 run_command,和 Claude Code 下走 Bash 一样不在改文件的检查里。
|
|
205
|
+
* - 不干预时什么都不能输出:PreToolUse 输出 {}、空的 decision、退出码非 0 都会把这次工具调用拦掉。所以这里没有要说的就返回 undefined,
|
|
206
|
+
* 出错也不输出(入口脚本保证退出码 0)。绝不输出 allow:那会替用户放行,绕过 Antigravity 自己的权限。
|
|
207
|
+
* - 命令执行完(PostToolUse)的输入里没有命令的输出,也分不出命令有没有自己转到后台(几分钟的命令一定会转),所以不接:
|
|
208
|
+
* agent 看没看到整组结果只认 testpace status。
|
|
209
|
+
* - 要结束这一轮(Stop)只管正常说完话的(terminationReason 是 NO_TOOL_CALL);用户取消、出错、超过上限的不拦。
|
|
210
|
+
* 只拦一次靠停止门自己记下拦过的(stop.ts),不看输入里的 continuationActive(实测一直是 false)。
|
|
211
|
+
*/
|
|
212
|
+
export const ANTIGRAVITY_HOOKS: HookFormat = {
|
|
213
|
+
read(raw) {
|
|
214
|
+
const input = raw as AntigravityInput
|
|
215
|
+
// 没有 agent 的工作目录时不管:hook 进程自己的目录是插件目录,拿它顶替会把会话当成在插件所在的仓库里干活
|
|
216
|
+
// (开发期插件是指向 gatelight 仓库的链接,那个仓库本身接入了 testpace)
|
|
217
|
+
const event = input.hookEventName
|
|
218
|
+
const tool = input.toolCall?.name
|
|
219
|
+
const args = input.toolCall?.args
|
|
220
|
+
// 确认工具的调用不看 cwd:缺了 cwd 也要弹窗,不能当成不归 hook 管而放过去
|
|
221
|
+
if (event === 'PreToolUse' && tool === 'call_mcp_tool' && args?.ServerName === ANTIGRAVITY_SERVER) {
|
|
222
|
+
const text = confirmText(args.Arguments)
|
|
223
|
+
return { sessionId: input.conversationId ?? 'unknown', cwd: typeof input.cwd === 'string' ? input.cwd : '', kind: 'confirm', ...(text === undefined ? {} : { text }) }
|
|
224
|
+
}
|
|
225
|
+
if (typeof input.cwd !== 'string' || input.cwd === '') return undefined
|
|
226
|
+
const session: Session = { sessionId: input.conversationId ?? 'unknown', cwd: input.cwd }
|
|
227
|
+
if (event === 'SessionStart') return { ...session, kind: 'start' }
|
|
228
|
+
if (event === 'Stop') return input.terminationReason === 'NO_TOOL_CALL' ? { ...session, kind: 'stop' } : undefined
|
|
229
|
+
if (event !== 'PreToolUse') return undefined
|
|
230
|
+
if (tool === 'run_command') return typeof args?.CommandLine === 'string' && args.CommandLine ? { ...session, kind: 'command', command: args.CommandLine } : undefined
|
|
231
|
+
if (tool === 'write_file' || tool === 'edit_file') {
|
|
232
|
+
if (typeof args?.Path !== 'string') return undefined
|
|
233
|
+
const text = tool === 'write_file' ? args.Content : args.Replacement
|
|
234
|
+
return { ...session, kind: 'edit', file: args.Path, ...(typeof text === 'string' ? { text } : {}) }
|
|
235
|
+
}
|
|
236
|
+
return undefined
|
|
237
|
+
},
|
|
238
|
+
write(event, verdict) {
|
|
239
|
+
if (event.kind === 'start') {
|
|
240
|
+
const steps = [
|
|
241
|
+
...(verdict.context ? [{ systemMessage: { systemMessage: verdict.context } }] : []),
|
|
242
|
+
// 只给用户看的话:模型那边是空的
|
|
243
|
+
...(verdict.notice ? [{ systemMessage: { systemMessage: '', metadata: { user_markdown: verdict.notice } } }] : []),
|
|
244
|
+
]
|
|
245
|
+
return steps.length > 0 ? { injectSteps: steps } : undefined
|
|
246
|
+
}
|
|
247
|
+
if (event.kind === 'stop') return verdict.block ? { decision: 'continue', reason: verdict.block } : undefined
|
|
248
|
+
// force_ask:每次都弹窗问用户,"总是允许"在弹窗里选不了(实测);用户拒绝时工具不执行
|
|
249
|
+
if (verdict.ask) return { decision: 'force_ask', reason: oneParagraph(verdict.ask) }
|
|
250
|
+
// 工具调用之前:只有拒绝这一种说法(给模型的话由判断改成了拒绝的原因,见 beforeCommand)
|
|
251
|
+
return verdict.deny ? { decision: 'deny', reason: verdict.deny } : undefined
|
|
252
|
+
},
|
|
253
|
+
failure() {
|
|
254
|
+
return undefined
|
|
255
|
+
},
|
|
256
|
+
beforeCommand: 'refuse',
|
|
257
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// 评判用的配置:决定检查怎么判的文件(testpace.json、各模块自己的配置和清单)。
|
|
2
|
+
// 原则只有一条:这次改动改不了评判它的检查。模块读这类文件只走这里,拿到分叉点那一版和工作区现在这一版,
|
|
3
|
+
// 对每个配置项二选一,没有第三条路:
|
|
4
|
+
// 合并后才生效:按 before 判,分支上的放松这次不算;
|
|
5
|
+
// 用户同意后生效:和 before 比,放松的列成条目交给用户确认(confirm.ts)。
|
|
6
|
+
// 加严的(新加一条规则、多认一类文件)可以马上按 now 算。
|
|
7
|
+
// 分叉点由调用的地方给(git.ts 的 forkPoint):算不出分叉点时没有 before 可比,模块要说"没做完",不能当成没放松。
|
|
8
|
+
|
|
9
|
+
import { fileAt, fileNow, filesAt, filesNow } from './git.ts'
|
|
10
|
+
|
|
11
|
+
export interface Judging {
|
|
12
|
+
/** 分叉点那一版的内容;那时没有这个文件为 undefined。 */
|
|
13
|
+
before: string | undefined
|
|
14
|
+
/** 工作区现在的内容(含没提交的改动);没有这个文件为 undefined。 */
|
|
15
|
+
now: string | undefined
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 一个评判用的文件的两版。fork 是分叉点的提交,path 相对仓库根目录。 */
|
|
19
|
+
export function judgingFile(root: string, fork: string, path: string): Judging {
|
|
20
|
+
return { before: fileAt(root, fork, path), now: fileNow(root, path) }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 一个评判用的目录(比如清单目录)里每个文件的两版,按路径排序:分叉点上有的和工作区里现有的都算。 */
|
|
24
|
+
export function judgingDir(root: string, fork: string, dir: string): Map<string, Judging> {
|
|
25
|
+
const paths = [...new Set([...filesAt(root, fork, dir), ...filesNow(root, dir)])].sort()
|
|
26
|
+
return new Map(paths.map(path => [path, judgingFile(root, fork, path)]))
|
|
27
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// 调 LayerMap:只在这里碰它的命令。找命令、跑、读 JSON、出了问题怎么说都在这一处;
|
|
2
|
+
// 输出里要哪些事实由用的模块自己读(各模块要的不一样),读不到要的字段就按版本太旧说(tooOld)。
|
|
3
|
+
// 不看版本号,按能力判断:没有要的命令、输出不是 JSON 或缺字段,就是版本太旧。版本下限只有这里的一个。
|
|
4
|
+
//
|
|
5
|
+
// 出问题时的规则只有一条:命令找不到、没跑成、版本太旧,都拿不到事实,用的模块要退回更严的一边
|
|
6
|
+
// (选测试退回跑整组,别的检查算没做完),不能当成"没有问题"。所以这里的结果要么是事实,要么是说明了原因的失败,没有空值。
|
|
7
|
+
//
|
|
8
|
+
// 底座不自己做代码分析:要 LayerMap 新出的事实,向它提需求、等它发版。
|
|
9
|
+
|
|
10
|
+
import { spawnSync } from 'node:child_process'
|
|
11
|
+
|
|
12
|
+
export const LAYERMAP_MINIMUM = '0.1.48'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 插件不把命令放进 PATH 的地方怎么有 layermap 命令:Claude Code 把 LayerMap 插件的命令放进它跑命令的 PATH,
|
|
16
|
+
* Codex 的命令、终端、git 钩子里没有,要用 LayerMap 自己放的固定入口(LayerMap 0.1.55 起,它运行装着的最高版本)。
|
|
17
|
+
* 入口由 LayerMap 放、归它管,这里只说怎么放;各处"找不到命令"的提示都接这一句。
|
|
18
|
+
*/
|
|
19
|
+
export const LAYERMAP_COMMAND = 'Codex 和终端里还要跑一次 npx layermap setup command,在 ~/.local/bin 放上 layermap 命令'
|
|
20
|
+
|
|
21
|
+
export type LayermapFailure = {
|
|
22
|
+
ok: false
|
|
23
|
+
/** missing:PATH 里没有 layermap 命令;old:版本太旧;failed:它跑了但没跑成。 */
|
|
24
|
+
kind: 'missing' | 'old' | 'failed'
|
|
25
|
+
problem: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type LayermapAnswer = { ok: true; value: unknown } | LayermapFailure
|
|
29
|
+
|
|
30
|
+
/** 版本太旧的说法:模块在输出里读不到要的字段时也用它。 */
|
|
31
|
+
export function tooOld(): LayermapFailure {
|
|
32
|
+
return {
|
|
33
|
+
ok: false,
|
|
34
|
+
kind: 'old',
|
|
35
|
+
problem: `layermap 版本太旧,要 ${LAYERMAP_MINIMUM} 以上:升级 layermap 插件(Claude Code 里是 claude plugin update layermap@layermap),开新会话后再跑`,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** layermap --version 的输出(给各模块的 status 看);找不到命令或它没跑成时为 undefined。 */
|
|
40
|
+
export function layermapVersion(): string | undefined {
|
|
41
|
+
const r = spawnSync('layermap', ['--version'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] })
|
|
42
|
+
return r.status === 0 ? r.stdout.trim() : undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 最多等多久:大仓库第一次建地图要几分钟。 */
|
|
46
|
+
const TIMEOUT_MS = 600_000
|
|
47
|
+
|
|
48
|
+
/** 在仓库根目录跑一条 layermap 命令(args 里要带 --json),读回它输出的 JSON。 */
|
|
49
|
+
export function layermap(root: string, args: string[]): LayermapAnswer {
|
|
50
|
+
const r = spawnSync('layermap', args, { cwd: root, encoding: 'utf8', maxBuffer: 1024 * 1024 * 1024, timeout: TIMEOUT_MS, stdio: ['ignore', 'pipe', 'pipe'] })
|
|
51
|
+
if (r.error) {
|
|
52
|
+
const code = (r.error as NodeJS.ErrnoException).code
|
|
53
|
+
if (code === 'ENOENT') return { ok: false, kind: 'missing', problem: '找不到 layermap 命令' }
|
|
54
|
+
if (code === 'ETIMEDOUT') return { ok: false, kind: 'failed', problem: `layermap 出错了:等了 ${TIMEOUT_MS / 60_000} 分钟没跑完` }
|
|
55
|
+
return { ok: false, kind: 'failed', problem: `layermap 出错了:${r.error.message}` }
|
|
56
|
+
}
|
|
57
|
+
if (r.status !== 0) {
|
|
58
|
+
// 旧版不认识的命令只打印用法(打在 stderr,stdout 不是 JSON)
|
|
59
|
+
if (/^Usage:/m.test(`${r.stdout}\n${r.stderr}`) && !r.stdout.trimStart().startsWith('{')) return tooOld()
|
|
60
|
+
const said = (r.stderr.trim() || r.stdout.trim()).split('\n').at(-1) ?? ''
|
|
61
|
+
const how = r.status === null ? `被信号 ${r.signal} 终止` : `退出码 ${r.status}`
|
|
62
|
+
return { ok: false, kind: 'failed', problem: `layermap 出错了${said ? `:${said}` : `(${how})`}` }
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
return { ok: true, value: JSON.parse(r.stdout) }
|
|
66
|
+
} catch {
|
|
67
|
+
return tooOld()
|
|
68
|
+
}
|
|
69
|
+
}
|