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
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// 当前在哪个 agent 里:Claude Code、Codex、Antigravity(桌面 App),以及以后要接的 DeepSeek Harness(dsh,现在只认出来,没有接入)。
|
|
2
|
+
// 各 agent 的差别都记在 AGENTS 这张表里,别处只从表里取:认 agent、合并门的钩子看哪些变量、hook 要补充哪些前提、hook 的输入输出用哪种格式。
|
|
3
|
+
// 命令行靠命令环境里的变量认;hook 由入口参数说明是哪个 agent,不靠环境变量(见 hooks/testpace.ts)。
|
|
4
|
+
// 一个 agent 从另一个里启动时会继承外层的变量(比如 Codex 从 Claude Code 里启动时带着 CLAUDECODE 和
|
|
5
|
+
// CLAUDE_CODE_SESSION_ID),所以表里非 Claude 的排在前面:有 CODEX_THREAD_ID 就算 Codex。
|
|
6
|
+
// 光看环境变量分不出嵌套顺序:Claude Code 从 Codex 里启动时会被认成 Codex,这是已知限制。
|
|
7
|
+
// Antigravity 排在 Codex 后、Claude Code 前:从 Antigravity 里起的 Codex 算 Codex,从 Claude Code 里起的 Antigravity 算 Antigravity;
|
|
8
|
+
// 三个互相嵌套的其余情况一样分不出来。
|
|
9
|
+
|
|
10
|
+
import { ANTIGRAVITY_HOOKS, ANTIGRAVITY_SERVER, CLAUDE_HOOKS, type HookFormat } from './hookformat.ts'
|
|
11
|
+
|
|
12
|
+
export type AgentKind = 'claude' | 'codex' | 'antigravity' | 'dsh'
|
|
13
|
+
|
|
14
|
+
interface AgentProfile {
|
|
15
|
+
name: string
|
|
16
|
+
/** 命令环境里有其中一个就是这个 agent。 */
|
|
17
|
+
markers: string[]
|
|
18
|
+
/** 已经接入:合并门的钩子看它的标记变量,拦它的命令。还没接入的只认出来。 */
|
|
19
|
+
connected: boolean
|
|
20
|
+
/** 会话号所在的变量:停止门只管起整组的会话。 */
|
|
21
|
+
session: string
|
|
22
|
+
/** 有这个变量表示命令在 agent 的沙箱里跑:测试可能因为端口、网络被拒跑出假失败。没有沙箱、或沙箱里没有这样的变量的不填。 */
|
|
23
|
+
sandbox?: string
|
|
24
|
+
/**
|
|
25
|
+
* 有沙箱的 agent:会话说明里告诉它怎么申请到沙箱外跑(接在"每次"后面)。testpace 的命令在沙箱里跑不了,有没有标记沙箱的变量都要说。
|
|
26
|
+
* 没有沙箱的不填。
|
|
27
|
+
*/
|
|
28
|
+
leaveSandbox?: string
|
|
29
|
+
/** 插件的 bin 目录不在命令的 PATH 上,要靠固定命令入口找到插件的命令。 */
|
|
30
|
+
needsEntry: boolean
|
|
31
|
+
/** hook 的输入怎么读、结论怎么写(hookformat.ts)。 */
|
|
32
|
+
hooks: HookFormat
|
|
33
|
+
/**
|
|
34
|
+
* 说给 agent 的话里技能叫什么:话里写的是 Claude Code 的全名(插件名:技能名),这个 agent 里叫法不一样的填一个换法。
|
|
35
|
+
* 没填的照原样。
|
|
36
|
+
*/
|
|
37
|
+
skill?(name: string): string
|
|
38
|
+
/** 确认工具(confirm.ts)在这个 agent 里怎么调,接在"<插件名> 的 <工具名> 工具"后面。按工具名直接调的不填。 */
|
|
39
|
+
confirmCall?(tool: string): string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 按认的先后排(非 Claude 的在前)。dsh 还没接入,只认出来:合并门不拦它,沙箱、固定入口接入时实测再填。 */
|
|
43
|
+
export const AGENTS: Record<AgentKind, AgentProfile> = {
|
|
44
|
+
codex: {
|
|
45
|
+
name: 'Codex',
|
|
46
|
+
markers: ['CODEX_THREAD_ID'],
|
|
47
|
+
connected: true,
|
|
48
|
+
session: 'CODEX_THREAD_ID',
|
|
49
|
+
sandbox: 'CODEX_SANDBOX',
|
|
50
|
+
leaveSandbox: '申请提权在沙箱外跑',
|
|
51
|
+
needsEntry: true,
|
|
52
|
+
hooks: CLAUDE_HOOKS,
|
|
53
|
+
},
|
|
54
|
+
// 桌面 App 实测:ANTIGRAVITY_AGENT 沙箱内外的命令里都有,hook 的环境里没有(hook 不靠环境变量认 agent);会话号和 hook 输入的 conversationId 同值。
|
|
55
|
+
// 沙箱里没有标记它的变量:在沙箱里时固定入口找不到插件(看不见 Antigravity 的配置目录),由入口拒跑并说明(entry/command)。
|
|
56
|
+
// 技能名前面不带插件名(实测是光名字),撞名的可能先不管,话里说清楚是 gatelight 的。
|
|
57
|
+
antigravity: {
|
|
58
|
+
name: 'Antigravity',
|
|
59
|
+
markers: ['ANTIGRAVITY_AGENT'],
|
|
60
|
+
connected: true,
|
|
61
|
+
session: 'ANTIGRAVITY_CONVERSATION_ID',
|
|
62
|
+
leaveSandbox: '在 run_command 里加上 BypassSandbox 申请在沙箱外跑',
|
|
63
|
+
needsEntry: true,
|
|
64
|
+
hooks: ANTIGRAVITY_HOOKS,
|
|
65
|
+
skill: name => `gatelight 的 ${name}`,
|
|
66
|
+
// 系统提示里只列服务名和工具名,MCP 工具要经 call_mcp_tool 调(实测 agent 第一次没找到确认工具,直接说没有)
|
|
67
|
+
confirmCall: tool => `用 call_mcp_tool 调:ServerName 填 ${ANTIGRAVITY_SERVER},ToolName 填 ${tool},Arguments 填 repo、id、text`,
|
|
68
|
+
},
|
|
69
|
+
dsh: { name: 'DeepSeek Harness', markers: ['DSH_SHELL'], connected: false, session: 'DSH_SESSION_ID', needsEntry: false, hooks: CLAUDE_HOOKS },
|
|
70
|
+
// 桌面版里 Claude 往终端面板打的命令不带 CLAUDECODE,它开的标签带着 CLAUDE_CODE_LAUNCHED_BY_SESSION_IDS(实测)。
|
|
71
|
+
// 这个变量放的是开标签的会话号,可能不止一个,不拿来当会话号。用户自己开的标签没有它;用户在 Claude 开的标签里打的命令也算 Claude 的。
|
|
72
|
+
claude: { name: 'Claude Code', markers: ['CLAUDECODE', 'CLAUDE_CODE_LAUNCHED_BY_SESSION_IDS'], connected: true, session: 'CLAUDE_CODE_SESSION_ID', needsEntry: false, hooks: CLAUDE_HOOKS },
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const KINDS = Object.keys(AGENTS) as AgentKind[]
|
|
76
|
+
|
|
77
|
+
/** 已接入的 agent 的标记变量:合并门的钩子有其中一个才检查。排好序:钩子命令的正文不随表里的先后变。 */
|
|
78
|
+
export const AGENT_MARKERS: string[] = KINDS.filter(kind => AGENTS[kind].connected)
|
|
79
|
+
.flatMap(kind => AGENTS[kind].markers)
|
|
80
|
+
.sort()
|
|
81
|
+
|
|
82
|
+
export interface Agent {
|
|
83
|
+
kind: AgentKind
|
|
84
|
+
/** 会话号:停止门只管起整组的会话;没有时不记。 */
|
|
85
|
+
session?: string
|
|
86
|
+
/** 命令在 agent 的沙箱里跑。 */
|
|
87
|
+
sandboxed: boolean
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 命令环境里的 agent;用户自己在终端里跑的返回 undefined。 */
|
|
91
|
+
export function currentAgent(env: NodeJS.ProcessEnv = process.env): Agent | undefined {
|
|
92
|
+
const kind = KINDS.find(kind => AGENTS[kind].markers.some(marker => env[marker]))
|
|
93
|
+
if (!kind) return undefined
|
|
94
|
+
const { session, sandbox } = AGENTS[kind]
|
|
95
|
+
return { kind, ...(env[session] ? { session: env[session] } : {}), sandboxed: Boolean(sandbox && env[sandbox]) }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function isAgentKind(value: unknown): value is AgentKind {
|
|
99
|
+
return typeof value === 'string' && KINDS.includes(value as AgentKind)
|
|
100
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// 会话起点:会话第一次碰到一个工作区时,模块关心的那部分内容的样子。停止门只管这之后改的,靠它分清"这个会话改的"和"本来就这样"。
|
|
2
|
+
// 模块定记什么(value)、怎么算;怎么存、按什么认、没有起点时怎么办,都在这里,模块不各做各的。
|
|
3
|
+
// 按会话和工作区各记一份:记录文件是同一个仓库的所有工作区共用的,各工作区的内容却不一样,
|
|
4
|
+
// 拿一个工作区的起点去比另一个,那里别人早就改好的东西会被算成这个会话改的。
|
|
5
|
+
// format 是算法的名字:算法换了,旧起点和新算出来的样子没法比,当作没有。
|
|
6
|
+
// 没有起点(模块是会话中途才装上的、当时看不了改动、算法换了)就没有参照:把现在的样子记成起点,不说是这个会话改的。
|
|
7
|
+
// 这之前这个会话真改过的,这一次不拦;合并前的整组照样查。
|
|
8
|
+
// 起点只说"这之后变了",不说是谁变的:同一个仓库几个会话各在自己的工作区并行时,进去看过别人工作区的会话也有那里的起点。
|
|
9
|
+
// 停止门只为会话干活的工作区(所在目录的、用改文件的工具改过的)问模块,见 store.ts 的 SessionState.worked。
|
|
10
|
+
|
|
11
|
+
import { hash, ModuleRecords } from './records.ts'
|
|
12
|
+
import type { Repo } from './repo.ts'
|
|
13
|
+
|
|
14
|
+
interface Baseline<T> {
|
|
15
|
+
value: T
|
|
16
|
+
at: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class SessionBaselines<T> {
|
|
20
|
+
private readonly records: ModuleRecords<Baseline<T>>
|
|
21
|
+
private readonly worktree: string
|
|
22
|
+
private readonly format: string
|
|
23
|
+
|
|
24
|
+
/** module 是模块的名字;format 变了,以前记的起点都不算。 */
|
|
25
|
+
constructor(repo: Repo, module: string, format: string) {
|
|
26
|
+
this.records = new ModuleRecords(repo.store, module, 'baselines')
|
|
27
|
+
this.worktree = repo.root
|
|
28
|
+
this.format = format
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private key(sessionId: string): string {
|
|
32
|
+
return hash(this.format, sessionId, this.worktree)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 会话碰到这个工作区了:还没有起点就记下现在的样子(now 给不出来、比如没接入时,不记)。
|
|
37
|
+
* 已经有的不动:同一个会话恢复、压缩后还会再来,每条命令之前也会来,所以先看有没有,再算。
|
|
38
|
+
*/
|
|
39
|
+
note(sessionId: string, now: () => T | undefined): void {
|
|
40
|
+
const key = this.key(sessionId)
|
|
41
|
+
if (this.records.get(key)) return
|
|
42
|
+
const value = now()
|
|
43
|
+
if (value !== undefined) this.records.put(key, { value, at: Date.now() })
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 这个会话在这个工作区的起点;没有就把 now(现在的样子)记成起点,返回它。 */
|
|
47
|
+
since(sessionId: string, now: T): T {
|
|
48
|
+
const key = this.key(sessionId)
|
|
49
|
+
const found = this.records.get(key)
|
|
50
|
+
if (found) return found.value
|
|
51
|
+
this.records.put(key, { value: now, at: Date.now() })
|
|
52
|
+
return now
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// 三条命令(testpace、modwall、testcraft)的入口共用的一层:在 agent 的沙箱里跑不了的子命令事先拒绝,写入被拒时说清楚。
|
|
2
|
+
// 各命令只说哪些子命令在沙箱里跑不了、要做什么(outside);怎么认沙箱、怎么说、退出码是什么都在这里,三个入口都经过它。
|
|
3
|
+
|
|
4
|
+
import { AGENTS, currentAgent } from './agent.ts'
|
|
5
|
+
import { EXIT } from './exit.ts'
|
|
6
|
+
|
|
7
|
+
/** 跑这条命令的 agent:会话号、在不在沙箱里。用户在终端里跑的没有。 */
|
|
8
|
+
export const AGENT = currentAgent()
|
|
9
|
+
|
|
10
|
+
export interface CommandLine {
|
|
11
|
+
/** 命令名:提示里用。 */
|
|
12
|
+
name: string
|
|
13
|
+
/**
|
|
14
|
+
* 这次调用在 agent 的沙箱里跑不了时,说它要做什么(接在"它要"后面);跑得了的返回 undefined,没有这样的子命令的不填。
|
|
15
|
+
* 算跑不了的:要写项目目录以外的地方(.git/testpace/ 的记录、LayerMap 存在项目外面的地图),或者要跑项目的命令(常要端口、网络)。
|
|
16
|
+
* 沙箱里这些会被拒,得出的是假结论(测试假失败、检查假的没通过),所以不跑,不等它失败。
|
|
17
|
+
* 仓库在沙箱放行的位置(比如 /tmp)时记录目录也写得进去,所以不能只靠写不写得进去判断。
|
|
18
|
+
* git 钩子调用的子命令不算在里面:在这里拒绝不是检查结论,钩子会当成没检查而放行。
|
|
19
|
+
*/
|
|
20
|
+
outside?(argv: string[]): string | undefined
|
|
21
|
+
run(argv: string[]): number | Promise<number>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 跑一条命令,返回退出码。 */
|
|
25
|
+
export async function runCommandLine(line: CommandLine, argv: string[]): Promise<number> {
|
|
26
|
+
const needs = AGENT?.sandboxed ? line.outside?.(argv) : undefined
|
|
27
|
+
if (AGENT && needs) {
|
|
28
|
+
console.error(`${line.name} 要在 ${AGENTS[AGENT.kind].name} 的沙箱外跑:它要${needs},在沙箱里会被拒、结果不准。申请提权(在沙箱外)重新运行这条命令。`)
|
|
29
|
+
return EXIT.usage
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
return await line.run(argv)
|
|
33
|
+
} catch (error) {
|
|
34
|
+
// 写入被拒(记录目录 .git/testpace、项目里的文件):多半是在 agent 的沙箱里,说清楚,不只抛一句写入被拒
|
|
35
|
+
const code = (error as NodeJS.ErrnoException).code
|
|
36
|
+
if (code !== 'EPERM' && code !== 'EACCES' && code !== 'EROFS') throw error
|
|
37
|
+
console.error(`${line.name} 写不了 ${(error as NodeJS.ErrnoException).path ?? '记录目录'}(${code})。在 agent 的沙箱里时,申请提权(在沙箱外)重新运行这条命令。`)
|
|
38
|
+
return EXIT.usage
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// 底座的命令:借用工作区(hold、release)、这台机器配一次(install)、git 钩子调的 gate。
|
|
2
|
+
// 命令行入口(src/cli.ts)分发到这里;各模块的命令在各自的目录里。
|
|
3
|
+
|
|
4
|
+
import { readFileSync } from 'node:fs'
|
|
5
|
+
import { ENTRY_COMMANDS, entryDir, entryPath, installEntries } from './entry.ts'
|
|
6
|
+
import { EXIT } from './exit.ts'
|
|
7
|
+
import { checkRefUpdates, GATE_REJECT, GATE_UNCHECKED, gateProblem, installGate } from './gate.ts'
|
|
8
|
+
import { borrow, giveBack, openRepo, type Repo, runningText } from './repo.ts'
|
|
9
|
+
import { processAlive } from './store.ts'
|
|
10
|
+
|
|
11
|
+
/** 命令所在的仓库;不在 git 仓库里时说一句。 */
|
|
12
|
+
export function context(): Repo | undefined {
|
|
13
|
+
const repo = openRepo(process.cwd())
|
|
14
|
+
if (!repo) console.error('testpace 要在 git 仓库里运行。')
|
|
15
|
+
return repo
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** testpace hold:别的工具借用这个工作区(见 repo.ts 的 borrow)。占上了输出借用的 id,放开时用。 */
|
|
19
|
+
export function hold(args: string[], usage: string): number {
|
|
20
|
+
const [pid, ...what] = args
|
|
21
|
+
const by = what.join(' ').trim()
|
|
22
|
+
if (!pid || !/^[1-9]\d*$/.test(pid) || !by) {
|
|
23
|
+
console.error(usage)
|
|
24
|
+
return EXIT.usage
|
|
25
|
+
}
|
|
26
|
+
// 进程不在了的登记马上作废,调用方会以为占上了
|
|
27
|
+
if (!processAlive(Number(pid))) {
|
|
28
|
+
console.error(`进程 ${pid} 不在,借用要登记正在用工作区的那个进程。`)
|
|
29
|
+
return EXIT.usage
|
|
30
|
+
}
|
|
31
|
+
const ctx = context()
|
|
32
|
+
if (!ctx) return EXIT.usage
|
|
33
|
+
const held = borrow(ctx, Number(pid), by)
|
|
34
|
+
if ('busy' in held) {
|
|
35
|
+
console.log(`${runningText(held.busy)}。`)
|
|
36
|
+
return EXIT.busy
|
|
37
|
+
}
|
|
38
|
+
if (held.id) console.log(held.id)
|
|
39
|
+
return EXIT.ok
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** testpace release:放开 hold 占的工作区。 */
|
|
43
|
+
export function release(args: string[], usage: string): number {
|
|
44
|
+
const [id] = args
|
|
45
|
+
if (!id || args.length !== 1) {
|
|
46
|
+
console.error(usage)
|
|
47
|
+
return EXIT.usage
|
|
48
|
+
}
|
|
49
|
+
const ctx = context()
|
|
50
|
+
if (!ctx) return EXIT.usage
|
|
51
|
+
giveBack(ctx, id)
|
|
52
|
+
return EXIT.ok
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function install(): number {
|
|
56
|
+
const ctx = context()
|
|
57
|
+
if (!ctx) return EXIT.usage
|
|
58
|
+
const { ok, problem } = installGate(ctx.root)
|
|
59
|
+
if (!ok) {
|
|
60
|
+
console.error(`没有配好:${problem}`)
|
|
61
|
+
return EXIT.usage
|
|
62
|
+
}
|
|
63
|
+
console.log('合并门已配好:全局 git 配置里的 hook.testpace,这台机器上 agent 在所有仓库里移动默认分支时都检查(只管接入了 testpace 的仓库)。')
|
|
64
|
+
const rest = gateProblem(ctx.root)
|
|
65
|
+
if (rest) console.log(rest)
|
|
66
|
+
const { refused } = installEntries()
|
|
67
|
+
const placed = ENTRY_COMMANDS.filter(command => !refused.includes(command))
|
|
68
|
+
if (placed.length) console.log(`固定命令入口已放好:${entryDir()} 下的 ${placed.join('、')},运行时转给这台机器上装着的最高版本的插件里同名的命令。`)
|
|
69
|
+
if (refused.length) {
|
|
70
|
+
console.error(`固定命令入口没放上:${refused.map(entryPath).join('、')} 已经有别的文件,没有覆盖。确认不要它后删掉,再跑 testpace install。`)
|
|
71
|
+
return EXIT.usage
|
|
72
|
+
}
|
|
73
|
+
return EXIT.ok
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* git 的 reference-transaction 钩子调用:标准输入是要更新的引用。放行退出码 0;拒绝退出码 GATE_REJECT,
|
|
78
|
+
* git 就取消这次更新;这次没有检查、要当场说一句的退出码 GATE_UNCHECKED,钩子放行并转达。
|
|
79
|
+
* 检查时出错也拒绝:已经在检查了,确认不了要落地的代码测过就不放。
|
|
80
|
+
*/
|
|
81
|
+
export function gate(): number {
|
|
82
|
+
try {
|
|
83
|
+
const answer = checkRefUpdates(process.cwd(), readFileSync(0, 'utf8'))
|
|
84
|
+
if (!answer) return EXIT.ok
|
|
85
|
+
if ('unchecked' in answer) {
|
|
86
|
+
console.error(answer.unchecked)
|
|
87
|
+
return GATE_UNCHECKED
|
|
88
|
+
}
|
|
89
|
+
console.error(answer.reject)
|
|
90
|
+
} catch (error) {
|
|
91
|
+
console.error(`testpace 合并门:没能确认要落地的代码测过(${(error as Error).message.split('\n')[0]}),这次不更新。`)
|
|
92
|
+
}
|
|
93
|
+
return GATE_REJECT
|
|
94
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// 用户确认:检查里有要用户同意的改动(放松了测试、放宽了规则)时,模块把条目交到这里,不自己弹窗、不自己存同意记录。
|
|
2
|
+
// 同意只能来自确认工具(src/mcp.ts 的 confirm_with_user):每次调用都要用户在弹窗里同意(Claude Code 靠工具上的标记,
|
|
3
|
+
// Antigravity 靠 hook 的 force_ask),agent 自己说"用户同意了"不算。
|
|
4
|
+
//
|
|
5
|
+
// 同意按项记:一项是"哪个模块的、改之前是什么、改成什么、用户当时看到的文字",四样都一样才是同一项。
|
|
6
|
+
// 几个分支合到一起,没再动过的那一项不用重新确认;文字相同但改之前的内容不同,是另一项,要重新确认。
|
|
7
|
+
//
|
|
8
|
+
// 两步:检查(askUser)把还没同意的条目按编号存好,输出编号和弹窗文字让 agent 去调工具;
|
|
9
|
+
// 工具(confirm)只按编号取存下的内容,核对传来的文字一字不差,记下同意。工具进程跟着会话开着、插件更新后还是旧代码,
|
|
10
|
+
// 所以它不打开改动、不做任何判断。
|
|
11
|
+
//
|
|
12
|
+
// 记录在 .git/testpace/confirm/ 下(所有 worktree 共用):pending.json 等用户过目的,confirmed.json 用户同意过的。
|
|
13
|
+
|
|
14
|
+
import { existsSync } from 'node:fs'
|
|
15
|
+
import { commonDir, repoRoot } from './git.ts'
|
|
16
|
+
import { hash, ModuleRecords } from './records.ts'
|
|
17
|
+
import { type Repo } from './repo.ts'
|
|
18
|
+
import { Store } from './store.ts'
|
|
19
|
+
import { PLUGIN_NAME } from './version.ts'
|
|
20
|
+
|
|
21
|
+
/** 确认工具的名字(src/mcp.ts)。 */
|
|
22
|
+
export const CONFIRM_TOOL = 'confirm_with_user'
|
|
23
|
+
|
|
24
|
+
/** 要用户同意的一项改动。 */
|
|
25
|
+
export interface ConfirmItem {
|
|
26
|
+
/** 这一项改之前是什么。写法由模块定(内容本身或它的摘要都行),同一项改动每次要写得一样。 */
|
|
27
|
+
before: string
|
|
28
|
+
/** 改成了什么。 */
|
|
29
|
+
after: string
|
|
30
|
+
/** 给用户看的文字:白话,用户不看代码也能判断。 */
|
|
31
|
+
text: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface Asked {
|
|
35
|
+
/** 用户同意过的。 */
|
|
36
|
+
confirmed: ConfirmItem[]
|
|
37
|
+
/** 还没同意的。 */
|
|
38
|
+
pending: ConfirmItem[]
|
|
39
|
+
/** 有没同意的条目时:存好的编号和弹窗文字,以及说给 agent 的话(怎么调工具)。 */
|
|
40
|
+
ask?: { id: string; text: string; lines: string[] }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface PendingRecord {
|
|
44
|
+
module: string
|
|
45
|
+
/** 弹窗里给用户看的文字:工具只核对传来的和它一字不差。 */
|
|
46
|
+
text: string
|
|
47
|
+
/** 这次弹窗里的各项:同意后按 key 各记一条。 */
|
|
48
|
+
items: Array<{ key: string; text: string }>
|
|
49
|
+
at: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface ConfirmRecord {
|
|
53
|
+
module: string
|
|
54
|
+
/** 用户同意时看到的这一项的文字。 */
|
|
55
|
+
text: string
|
|
56
|
+
at: number
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 文字比对用:每行去掉首尾空白,去掉空行,一样才算同一份。 */
|
|
60
|
+
function plain(text: string): string {
|
|
61
|
+
return text
|
|
62
|
+
.split('\n')
|
|
63
|
+
.map(line => line.trim())
|
|
64
|
+
.filter(Boolean)
|
|
65
|
+
.join('\n')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 一项的记号:模块、改之前、改成什么、给用户看的文字。 */
|
|
69
|
+
function itemKey(module: string, item: ConfirmItem): string {
|
|
70
|
+
return hash('item', module, item.before, item.after, plain(item.text))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 两份记录都用底座的模块记录存取(records.ts):90 天前的不再算,文件坏了当成没有。 */
|
|
74
|
+
const pendingOf = (store: Store): ModuleRecords<PendingRecord> => new ModuleRecords(store, 'confirm', 'pending')
|
|
75
|
+
const confirmedOf = (store: Store): ModuleRecords<ConfirmRecord> => new ModuleRecords(store, 'confirm', 'confirmed')
|
|
76
|
+
|
|
77
|
+
/** 弹窗里给用户看的文字:一项一段,带编号。 */
|
|
78
|
+
function popupText(heading: string, items: ConfirmItem[]): string {
|
|
79
|
+
const lines = [`${heading},要你确认的有 ${items.length} 项:`]
|
|
80
|
+
items.forEach((item, index) => {
|
|
81
|
+
const [first = '', ...rest] = item.text.split('\n').map(line => line.trim()).filter(Boolean)
|
|
82
|
+
lines.push('', `${index + 1}. ${first}`, ...rest.map(line => ` ${line}`))
|
|
83
|
+
})
|
|
84
|
+
lines.push('', '同意:按这样改。不同意:告诉 agent 改回去或换个做法。')
|
|
85
|
+
return lines.join('\n')
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 检查时调:这些条目里用户同意过哪些。有没同意的,把它们按编号存好,返回编号、弹窗文字和说给 agent 的话。
|
|
90
|
+
* module 是模块的名字,heading 是弹窗开头说这是什么的半句(比如"这次改动里放松测试的地方")。
|
|
91
|
+
* 有没同意的条目时,模块的检查不能算通过。
|
|
92
|
+
*/
|
|
93
|
+
export function askUser(repo: Repo, module: string, heading: string, items: ConfirmItem[]): Asked {
|
|
94
|
+
const records = confirmedOf(repo.store)
|
|
95
|
+
const seen = new Set<string>()
|
|
96
|
+
const confirmed: ConfirmItem[] = []
|
|
97
|
+
const pending: Array<{ key: string; item: ConfirmItem }> = []
|
|
98
|
+
for (const item of items) {
|
|
99
|
+
const key = itemKey(module, item)
|
|
100
|
+
if (seen.has(key)) continue
|
|
101
|
+
seen.add(key)
|
|
102
|
+
if (records.get(key)) confirmed.push(item)
|
|
103
|
+
else pending.push({ key, item })
|
|
104
|
+
}
|
|
105
|
+
if (pending.length === 0) return { confirmed, pending: [] }
|
|
106
|
+
const text = popupText(heading, pending.map(({ item }) => item))
|
|
107
|
+
// 编号按这一批没同意的条目算:条目变了就是另一个编号,旧编号的同意对它不算
|
|
108
|
+
const id = hash('ask', module, text, ...pending.map(({ key }) => key))
|
|
109
|
+
pendingOf(repo.store).put(id, { module, text, items: pending.map(({ key, item }) => ({ key, text: item.text })), at: Date.now() })
|
|
110
|
+
return {
|
|
111
|
+
confirmed,
|
|
112
|
+
pending: pending.map(({ item }) => item),
|
|
113
|
+
ask: {
|
|
114
|
+
id,
|
|
115
|
+
text,
|
|
116
|
+
lines: [
|
|
117
|
+
`这 ${pending.length} 项还没经用户确认。用户不会看命令输出,要调用 ${PLUGIN_NAME} 的 ${CONFIRM_TOOL} 工具,弹窗给用户看,用户同意了才算。`,
|
|
118
|
+
'调用之前,先在对话里用白话告诉用户每一项原来是什么样、现在改成什么样、为什么;用户不同意的,改回去或者换个做法。',
|
|
119
|
+
`repo:${repo.root}`,
|
|
120
|
+
`id:${id}`,
|
|
121
|
+
'text(下面两行分隔线之间的原样填,一字不改):',
|
|
122
|
+
'-----',
|
|
123
|
+
text,
|
|
124
|
+
'-----',
|
|
125
|
+
],
|
|
126
|
+
},
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface Confirmed {
|
|
131
|
+
ok: boolean
|
|
132
|
+
text: string
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 编号是 32 位十六进制。 */
|
|
136
|
+
const ID = /^[0-9a-f]{32}$/
|
|
137
|
+
|
|
138
|
+
/** 确认工具调:核对传来的文字就是检查给这个编号存下的弹窗内容,记下用户同意了里面的每一项。 */
|
|
139
|
+
export function confirm(repoPath: string, id: string, text: string): Confirmed {
|
|
140
|
+
const root = existsSync(repoPath) ? repoRoot(repoPath) : undefined
|
|
141
|
+
if (!root) return { ok: false, text: `${repoPath} 不在 git 仓库里` }
|
|
142
|
+
const missing = { ok: false, text: '没有这个编号要确认的内容:重跑要求确认的那条检查,照它输出的编号和文字填。' }
|
|
143
|
+
if (!ID.test(id)) return missing
|
|
144
|
+
const store = new Store(commonDir(root))
|
|
145
|
+
// 编号是外面传来的:模块记录只取文件里这个键自己的
|
|
146
|
+
const pending = pendingOf(store).get(id)
|
|
147
|
+
if (!pending) return missing
|
|
148
|
+
const whole =
|
|
149
|
+
typeof pending.text === 'string' &&
|
|
150
|
+
typeof pending.module === 'string' &&
|
|
151
|
+
Array.isArray(pending.items) &&
|
|
152
|
+
pending.items.every(item => typeof item?.key === 'string' && typeof item.text === 'string')
|
|
153
|
+
if (!whole) return { ok: false, text: '这个编号存下的内容坏了:重跑要求确认的那条检查,照它输出的编号和文字填。' }
|
|
154
|
+
if (plain(text) !== plain(pending.text)) {
|
|
155
|
+
return { ok: false, text: '传来的文字和检查给这个编号的不一致:照它输出的原样填,不要改写、增删。' }
|
|
156
|
+
}
|
|
157
|
+
const records = confirmedOf(store)
|
|
158
|
+
const at = Date.now()
|
|
159
|
+
for (const item of pending.items) records.put(item.key, { module: pending.module, text: item.text, at })
|
|
160
|
+
return { ok: true, text: `已记下:用户确认并同意了这 ${pending.items.length} 项。再跑一次要求确认的那条检查。` }
|
|
161
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// 项目的约定文件 testpace.json,底座看到的部分:接没接入、哪些路径不影响结果(ignore)、整个文件有没有问题。
|
|
2
|
+
// 别的字段归各模块(见 module.ts 的 fields、contractProblems),底座只把它们的问题合到一起。
|
|
3
|
+
|
|
4
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { defaultBranch, fileAt, refTip } from './git.ts'
|
|
7
|
+
import { modules } from './module.ts'
|
|
8
|
+
|
|
9
|
+
export const CONTRACT_FILE = 'testpace.json'
|
|
10
|
+
|
|
11
|
+
export interface Contract {
|
|
12
|
+
/** 这份约定里写的、不影响结果的路径规则。文件写坏了读不出来时为空(按没有 ignore 算:要内容完全一致的通过记录)。判断时认哪些见 effectiveIgnore。 */
|
|
13
|
+
ignore: string[]
|
|
14
|
+
/** 文件的内容;不是合法的 JSON 对象时没有。 */
|
|
15
|
+
raw?: Record<string, unknown>
|
|
16
|
+
/** 约定的问题:格式不对、未知字段、ignore 写错,和各模块对自己字段的检查。有问题时整组跑不了。 */
|
|
17
|
+
problems: string[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 工作区接入了:根目录有 testpace.json。写坏了也算接入,问题由命令和会话说明讲出来。 */
|
|
21
|
+
export function joined(root: string): boolean {
|
|
22
|
+
return existsSync(join(root, CONTRACT_FILE))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 工作区里的约定;没接入返回 undefined。 */
|
|
26
|
+
export function loadContract(root: string): Contract | undefined {
|
|
27
|
+
return joined(root) ? readContract(readFileSync(join(root, CONTRACT_FILE), 'utf8')) : undefined
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 一个提交自己带的约定(提交里的 testpace.json);提交里没有这个文件返回 undefined。
|
|
32
|
+
* 合并门、停止门判断落到默认分支的代码时用它,不看工作区里的文件:工作区里没提交的改动、
|
|
33
|
+
* 别的分支检出的旧文件都不该影响判断。
|
|
34
|
+
*/
|
|
35
|
+
export function contractAt(root: string, rev: string): Contract | undefined {
|
|
36
|
+
const text = fileAt(root, rev, CONTRACT_FILE)
|
|
37
|
+
return text === undefined ? undefined : readContract(text)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 默认分支上现在那份约定(本地的默认分支,没有就看 origin 上的);认不出默认分支、那里没有 testpace.json 时没有。 */
|
|
41
|
+
export function landedContract(root: string): Contract | undefined {
|
|
42
|
+
const branch = defaultBranch(root)
|
|
43
|
+
const tip = branch ? (refTip(root, `refs/heads/${branch}`) ?? refTip(root, `refs/remotes/origin/${branch}`)) : undefined
|
|
44
|
+
return tip ? contractAt(root, tip.commit) : undefined
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 判断"只改了不影响结果的路径"时认哪些 ignore 规则。ignore 是评判用的配置,走"合并后才生效"(见 judging.ts):
|
|
49
|
+
* 这份约定里有、默认分支上原来那份(landed)里也有的才认。分支上新加的规则这次不算,合并以后才生效;删掉的马上生效。
|
|
50
|
+
* 默认分支上还没有约定(首次接入)时一条也不认。
|
|
51
|
+
*/
|
|
52
|
+
export function effectiveIgnore(contract: Contract | undefined, landed: Contract | undefined): string[] {
|
|
53
|
+
if (!contract || !landed) return []
|
|
54
|
+
return contract.ignore.filter(rule => landed.ignore.includes(rule))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 工作区里这份约定现在认的 ignore 规则(rules),和写了但要合并后才生效的(deferred)。 */
|
|
58
|
+
export function countedIgnore(root: string, contract: Contract | undefined): { rules: string[]; deferred: string[] } {
|
|
59
|
+
const rules = effectiveIgnore(contract, landedContract(root))
|
|
60
|
+
return { rules, deferred: (contract?.ignore ?? []).filter(rule => !rules.includes(rule)) }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 从 testpace.json 的内容读约定(工作区里的文件,或某个提交里的那份)。 */
|
|
64
|
+
export function readContract(text: string): Contract {
|
|
65
|
+
let raw: unknown
|
|
66
|
+
try {
|
|
67
|
+
raw = JSON.parse(text)
|
|
68
|
+
} catch (error) {
|
|
69
|
+
return { ignore: [], problems: [`${CONTRACT_FILE} 不是合法的 JSON:${(error as Error).message}`] }
|
|
70
|
+
}
|
|
71
|
+
return parseContract(raw)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function parseContract(raw: unknown): Contract {
|
|
75
|
+
if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
|
|
76
|
+
return { ignore: [], problems: [`${CONTRACT_FILE} 应该是一个对象`] }
|
|
77
|
+
}
|
|
78
|
+
const record = raw as Record<string, unknown>
|
|
79
|
+
const problems: string[] = []
|
|
80
|
+
const known = new Set(['ignore', ...modules().flatMap(module => module.fields ?? [])])
|
|
81
|
+
for (const key of Object.keys(record)) {
|
|
82
|
+
if (!known.has(key)) problems.push(`未知字段 "${key}"`)
|
|
83
|
+
}
|
|
84
|
+
for (const module of modules()) problems.push(...(module.contractProblems?.(record) ?? []))
|
|
85
|
+
let ignore: string[] = []
|
|
86
|
+
if (record.ignore !== undefined) {
|
|
87
|
+
if (!Array.isArray(record.ignore) || record.ignore.some(item => typeof item !== 'string' || item === '')) {
|
|
88
|
+
problems.push('"ignore" 应该是路径规则的字符串数组')
|
|
89
|
+
} else {
|
|
90
|
+
ignore = record.ignore as string[]
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return { ignore, raw: record, problems }
|
|
94
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// 某份代码内容有没有整组通过的记录。
|
|
2
|
+
|
|
3
|
+
import type { RunRecord } from './store.ts'
|
|
4
|
+
|
|
5
|
+
export type Coverage =
|
|
6
|
+
| { kind: 'exact'; run: RunRecord }
|
|
7
|
+
| { kind: 'ignored'; run: RunRecord; changed: string[] }
|
|
8
|
+
| { kind: 'none' }
|
|
9
|
+
|
|
10
|
+
/** 能当通过记录的整组:通过,而且跑的过程中代码没改过。 */
|
|
11
|
+
export function isValidPass(run: RunRecord): boolean {
|
|
12
|
+
return run.kind === 'full' && run.result === 'pass' && run.tree === run.treeAtEnd
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** 只和最近这么多份通过的代码比较忽略路径,避免历史很长时逐个 diff。 */
|
|
16
|
+
const IGNORE_CANDIDATES = 20
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 先找内容完全一致的通过记录;没有的话,找相对它只改了 ignore 路径的最近一次。
|
|
20
|
+
* diff 由调用方给(git diff-tree),这里不碰 git,方便单测。
|
|
21
|
+
*/
|
|
22
|
+
export function findCoverage(
|
|
23
|
+
tree: string,
|
|
24
|
+
runs: RunRecord[],
|
|
25
|
+
isIgnored: (path: string) => boolean,
|
|
26
|
+
diff: (from: string, to: string) => string[],
|
|
27
|
+
): Coverage {
|
|
28
|
+
const passes = runs.filter(isValidPass).sort((a, b) => b.startedAt - a.startedAt)
|
|
29
|
+
const exact = passes.find(run => run.tree === tree)
|
|
30
|
+
if (exact) return { kind: 'exact', run: exact }
|
|
31
|
+
|
|
32
|
+
const seen = new Set<string>()
|
|
33
|
+
for (const run of passes) {
|
|
34
|
+
if (seen.has(run.tree)) continue
|
|
35
|
+
seen.add(run.tree)
|
|
36
|
+
if (seen.size > IGNORE_CANDIDATES) break
|
|
37
|
+
let changed: string[]
|
|
38
|
+
try {
|
|
39
|
+
changed = diff(run.tree, tree)
|
|
40
|
+
} catch {
|
|
41
|
+
continue // 树对象已被清理
|
|
42
|
+
}
|
|
43
|
+
if (changed.length > 0 && changed.every(isIgnored)) return { kind: 'ignored', run, changed }
|
|
44
|
+
}
|
|
45
|
+
return { kind: 'none' }
|
|
46
|
+
}
|