gatelight 0.18.0 → 0.19.1

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.
@@ -0,0 +1,22 @@
1
+ // "代码规范"说给 agent 的话,经底座的钩子送出去(base/module.ts 的 brief):会话开始时说几句规范的约定。
2
+ // 没有改文件前的拦截和停止门:lint 自己会报,规范检查在落地前的整组里跑(见 full.ts)。
3
+
4
+ import { existsSync } from 'node:fs'
5
+ import { join } from 'node:path'
6
+ import type { Repo } from '../base/repo.ts'
7
+ import { CONFIG_PATH, KEEP_DIR } from './config.ts'
8
+
9
+ export function guide(label = '这个仓库'): string[] {
10
+ return [
11
+ `${label}用 codekeep 管代码规范(${KEEP_DIR}/):`,
12
+ `1. 类型检查、lint、格式检查由 codekeep 在整组(testpace full)里跑,命令和要比的配置写在 ${CONFIG_PATH}。`,
13
+ '2. 不改松规范:不关规则、不把 error 降成 warn、不调高长度和复杂度上限、不加忽略的路径、不关严格类型选项、不往批量豁免文件里加条目。确实要改,整组会要用户确认:先在对话里用白话讲清改了哪条、为什么只能这样。',
14
+ '3. 代码里"这里不查"的注释(eslint-disable、@ts-expect-error 这类)必须写理由,比如 // eslint-disable-next-line 规则名 -- 理由;能改代码就改代码,不加这种注释。',
15
+ '4. 提交里不放密钥、密码;测试用的假值写得一眼能看出是假的(比如 test-password-not-real)。',
16
+ ]
17
+ }
18
+
19
+ /** 会话说明:接入了"代码规范"的仓库(工作区里有 .codekeep/)才说。 */
20
+ export function brief(label: string, repo?: Repo): string[] {
21
+ return repo && existsSync(join(repo.root, KEEP_DIR)) ? guide(label) : []
22
+ }
@@ -0,0 +1,115 @@
1
+ // tsconfig 生效的选项:用项目自己装的 typescript 跑 tsc --showConfig(extends 合并好、strict 展开好),分叉点那一版和现在比。
2
+ // 算放松的:严格类的选项从开变成关,开了 skipLibCheck,include 少了、exclude 多了。别的选项不比。
3
+
4
+ import { execFileSync } from 'node:child_process'
5
+ import { readFileSync } from 'node:fs'
6
+ import { createRequire } from 'node:module'
7
+ import { dirname, join } from 'node:path'
8
+ import type { ConfirmItem } from '../base/confirm.ts'
9
+ import { stable } from './eslint.ts'
10
+
11
+ /** 严格类的选项和它查的是什么(白话)。strict 开着时没写的前几项跟着开。 */
12
+ const STRICT: Record<string, string> = {
13
+ strict: '严格类型检查的总开关',
14
+ noImplicitAny: '推断不出类型的地方不许悄悄当成 any',
15
+ strictNullChecks: '可能是空值(null、undefined)的地方要先判断',
16
+ strictFunctionTypes: '函数参数的类型要对得上',
17
+ strictBindCallApply: 'bind、call、apply 的参数要对得上',
18
+ strictPropertyInitialization: '类的属性要在构造时赋值',
19
+ strictBuiltinIteratorReturn: '内置迭代器的返回值按严格的类型算',
20
+ noImplicitThis: 'this 的类型要明确',
21
+ alwaysStrict: '按 JavaScript 严格模式解析',
22
+ useUnknownInCatchVariables: 'catch 到的错误当成 unknown,用之前要先判断',
23
+ noUncheckedIndexedAccess: '按下标、键取值可能取不到,要先判断',
24
+ noImplicitReturns: '函数的每个分支都要 return',
25
+ noFallthroughCasesInSwitch: 'switch 的分支不许漏写 break',
26
+ noUnusedLocals: '不许有没用的变量',
27
+ noUnusedParameters: '不许有没用的参数',
28
+ noImplicitOverride: '覆盖父类的方法要写 override',
29
+ exactOptionalPropertyTypes: '可选属性不许随手赋 undefined',
30
+ }
31
+ /** strict 开着时跟着开的(没单独写时)。 */
32
+ const STRICT_FAMILY = [
33
+ 'noImplicitAny',
34
+ 'strictNullChecks',
35
+ 'strictFunctionTypes',
36
+ 'strictBindCallApply',
37
+ 'strictPropertyInitialization',
38
+ 'strictBuiltinIteratorReturn',
39
+ 'noImplicitThis',
40
+ 'alwaysStrict',
41
+ 'useUnknownInCatchVariables',
42
+ ]
43
+
44
+ /** 拿来比的那几样。 */
45
+ export interface TsSettings {
46
+ /** 开着的严格类选项。 */
47
+ strict: string[]
48
+ skipLibCheck: boolean
49
+ include: string[]
50
+ exclude: string[]
51
+ }
52
+
53
+ /**
54
+ * 项目自己装的 tsc:从工作区里这个 tsconfig 所在的目录往上找 typescript,按它 package.json 的 bin 找到 tsc。
55
+ * 不直接找 typescript/bin/tsc:TypeScript 7 的 package.json 写了 exports,没导出 bin/ 下的文件。找不到时抛错。
56
+ */
57
+ export function findTsc(root: string, tsconfig: string): string {
58
+ let manifest: string
59
+ try {
60
+ manifest = createRequire(join(root, dirname(tsconfig), 'noop.js')).resolve('typescript/package.json')
61
+ } catch {
62
+ throw new Error('项目里没装 typescript(从 tsconfig 所在的目录往上找不到),先装好依赖')
63
+ }
64
+ const { bin } = JSON.parse(readFileSync(manifest, 'utf8')) as { bin?: string | Record<string, string> }
65
+ const tsc = typeof bin === 'string' ? bin : bin?.tsc
66
+ if (!tsc) throw new Error(`${manifest} 里没写 tsc 命令(bin)`)
67
+ return join(dirname(manifest), tsc)
68
+ }
69
+
70
+ const strings = (value: unknown): string[] => (Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : [])
71
+
72
+ /** 在 tree(仓库或分叉点的临时 worktree)里算一个 tsconfig 生效的选项。算不出来时抛错。 */
73
+ export function tsSettings(tsc: string, tree: string, tsconfig: string): TsSettings {
74
+ let out: string
75
+ try {
76
+ out = execFileSync(process.execPath, [tsc, '--showConfig', '-p', join(tree, tsconfig)], { cwd: tree, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 120_000, maxBuffer: 256 * 1024 * 1024 })
77
+ } catch (error) {
78
+ const output = `${(error as { stdout?: string }).stdout ?? ''}${(error as { stderr?: string }).stderr ?? ''}`.trim()
79
+ throw new Error(`tsc --showConfig 没跑成:${output.split('\n')[0] || (error as Error).message}`)
80
+ }
81
+ const shown = JSON.parse(out) as { compilerOptions?: Record<string, unknown>; include?: unknown; exclude?: unknown }
82
+ const options = shown.compilerOptions ?? {}
83
+ const on = (name: string): boolean => {
84
+ const value = options[name]
85
+ if (typeof value === 'boolean') return value
86
+ return STRICT_FAMILY.includes(name) && options.strict === true
87
+ }
88
+ return {
89
+ strict: Object.keys(STRICT).filter(on),
90
+ skipLibCheck: options.skipLibCheck === true,
91
+ include: strings(shown.include),
92
+ exclude: strings(shown.exclude),
93
+ }
94
+ }
95
+
96
+ /** 一个 tsconfig 的放松合成一项,一行一处。 */
97
+ export function tsconfigItem(tsconfig: string, before: TsSettings, now: TsSettings): ConfirmItem | undefined {
98
+ const lines: string[] = []
99
+ const off = before.strict.filter(name => !now.strict.includes(name))
100
+ // 关掉 strict 时跟着关掉的几项并成一行说
101
+ const along = off.includes('strict') ? off.filter(name => STRICT_FAMILY.includes(name)) : []
102
+ if (off.includes('strict')) lines.push(`关掉了 strict(${STRICT.strict})${along.length ? `,跟着关掉了 ${along.join('、')}` : ''}:以后这些类型问题都不再报`)
103
+ for (const name of off.filter(name => name !== 'strict' && !along.includes(name))) lines.push(`关掉了 ${name}(${STRICT[name]}):以后这类问题类型检查不再报`)
104
+ if (now.skipLibCheck && !before.skipLibCheck) lines.push('开了 skipLibCheck:以后不检查依赖库和 .d.ts 文件里的类型声明')
105
+ const dropped = before.include.filter(rule => !now.include.includes(rule))
106
+ if (dropped.length) lines.push(`include 去掉了 ${dropped.map(rule => `"${rule}"`).join('、')}:这些文件可能不再做类型检查`)
107
+ const excluded = now.exclude.filter(rule => !before.exclude.includes(rule))
108
+ if (excluded.length) lines.push(`exclude 新加了 ${excluded.map(rule => `"${rule}"`).join('、')}:这些文件可能不再做类型检查`)
109
+ if (lines.length === 0) return undefined
110
+ return {
111
+ before: stable(['tsconfig', tsconfig, before]),
112
+ after: stable(['tsconfig', tsconfig, now]),
113
+ text: [`类型检查的配置 ${tsconfig} 改松了:`, ...lines].join('\n'),
114
+ }
115
+ }
package/src/mcp.ts CHANGED
@@ -1,14 +1,20 @@
1
- // 确认工具的 MCP 服务:只有一个工具 confirm_with_user,把检查要用户同意的改动给用户确认。
2
- // 工具标了 anthropic/requiresUserInteraction:Claude Code 每次都弹窗,任何权限模式、允许规则都跳不过,
3
- // 用户在弹窗里看到 text 的原文(检查写好的白话),同意了工具才会执行、记下确认。用标准输入输出收发一行一条的 JSON-RPC。
4
- // Antigravity 不认这个标记:它从插件根上的 mcp_config.json 起这个服务,每次调用前由插件的 hook 输出 force_ask 弹窗(hookformat.ts),
5
- // "总是允许"在那个弹窗里选不了;hook 出错时拒绝这次调用(hooks/antigravity.sh)。
1
+ // 确认工具的 MCP 服务:只有一个工具 confirm_with_user,把检查要用户同意的改动给用户确认。用标准输入输出收发一行一条的 JSON-RPC。
2
+ // 三个 agent 怎么保证每次都是用户本人在弹窗里同意(用户看到的都是检查存下的原文,同意了工具才记下):
3
+ // - Claude Code:工具标了 anthropic/requiresUserInteraction,每次都弹窗,任何权限模式、允许规则都跳不过。
4
+ // - Antigravity:不认这个标记。它从插件根上的 mcp_config.json 起这个服务,每次调用前由插件的 hook 输出 force_ask 弹窗(hookformat.ts),
5
+ // "总是允许"在那个弹窗里选不了;hook 出错时拒绝这次调用(hooks/antigravity.sh)。
6
+ // - Codex:也不认这个标记,hook 也要不了弹窗,工具审批还可能交给自动审查(AI)。它从 .codex-plugin/mcp.json 起这个服务、
7
+ // 参数带 codex:服务核对编号和文字后,自己向 Codex 发一张表单(MCP 的 elicitation),表单到用户面前、用户点同意才记下;
8
+ // 拒绝、取消(不能交互的 codex exec 直接取消)都不记。不支持表单的客户端不记,说明原因。
6
9
  // 这个进程跟着会话开着,插件更新了也不换代码,所以工具只认检查存下的编号和文字(见 base/confirm.ts),不引用任何模块、不做判断。
7
10
 
8
11
  import { createInterface } from 'node:readline'
9
- import { confirm, CONFIRM_TOOL } from './base/confirm.ts'
12
+ import { check, CONFIRM_TOOL, type Confirmed } from './base/confirm.ts'
10
13
  import { PLUGIN_NAME, VERSION } from './base/version.ts'
11
14
 
15
+ /** 服务自己向客户端发表单问用户(Codex):启动参数里带 codex。 */
16
+ const ASK_BY_FORM = process.argv.slice(2).includes('codex')
17
+
12
18
  const TOOL = {
13
19
  name: CONFIRM_TOOL,
14
20
  description:
@@ -25,55 +31,108 @@ const TOOL = {
25
31
  _meta: { 'anthropic/requiresUserInteraction': true },
26
32
  }
27
33
 
28
- interface Request {
34
+ type Id = number | string
35
+
36
+ interface Message {
29
37
  jsonrpc: '2.0'
30
- id?: number | string
31
- method: string
38
+ id?: Id
39
+ method?: string
32
40
  params?: Record<string, unknown>
41
+ result?: Record<string, unknown>
42
+ error?: { code: number; message: string }
43
+ }
44
+
45
+ function send(message: Record<string, unknown>): void {
46
+ process.stdout.write(`${JSON.stringify({ jsonrpc: '2.0', ...message })}\n`)
47
+ }
48
+
49
+ function reply(id: Message['id'], body: { result: unknown } | { error: { code: number; message: string } }): void {
50
+ send({ id, ...body })
51
+ }
52
+
53
+ /** 客户端在 initialize 里说了支持表单(elicitation 的 form;只写了 elicitation 的按老版本,也是表单)。 */
54
+ let formSupported = false
55
+
56
+ /** 服务发给客户端、还在等回答的请求。 */
57
+ const waiting = new Map<Id, (answer: Message) => void>()
58
+ let nextAsk = 0
59
+
60
+ /** 向客户端发表单问用户,返回用户的选择(accept、decline、cancel),客户端出错时是 undefined。 */
61
+ function askByForm(message: string): Promise<string | undefined> {
62
+ const id = `${PLUGIN_NAME}-ask-${++nextAsk}`
63
+ return new Promise(resolve => {
64
+ waiting.set(id, answer => resolve(typeof answer.result?.action === 'string' ? answer.result.action : undefined))
65
+ send({ id, method: 'elicitation/create', params: { message, requestedSchema: { type: 'object', properties: {} } } })
66
+ })
33
67
  }
34
68
 
35
- function reply(id: Request['id'], body: { result: unknown } | { error: { code: number; message: string } }): void {
36
- process.stdout.write(`${JSON.stringify({ jsonrpc: '2.0', id, ...body })}\n`)
69
+ const text = (body: string, isError: boolean) => ({ result: { content: [{ type: 'text', text: body }], isError } })
70
+
71
+ async function callTool(id: Id, args: Record<string, unknown>): Promise<void> {
72
+ if (typeof args.repo !== 'string' || typeof args.id !== 'string' || typeof args.text !== 'string') {
73
+ return reply(id, text('repo、id、text 都要填文字', true))
74
+ }
75
+ const checked = check(args.repo, args.id, args.text)
76
+ if (!checked.ok) return reply(id, text(checked.text, true))
77
+ let done: Confirmed
78
+ if (!ASK_BY_FORM) done = checked.record()
79
+ else if (!formSupported) {
80
+ done = { ok: false, text: '这个客户端不支持向用户弹表单(MCP elicitation),没法让用户本人确认,没有记下。升级客户端,或在 Claude Code 里确认。' }
81
+ } else {
82
+ const action = await askByForm(`${PLUGIN_NAME} 要你确认:\n\n${checked.popup}`)
83
+ done =
84
+ action === 'accept'
85
+ ? checked.record()
86
+ : { ok: false, text: action === 'decline' ? '用户拒绝了,没有记下:照用户的意思改回去或换个做法。' : '用户没有确认(表单取消了或弹不出来,比如不能交互的 codex exec),没有记下。' }
87
+ }
88
+ return reply(id, text(done.text, !done.ok))
37
89
  }
38
90
 
39
- function handle(request: Request): void {
91
+ function handle(message: Message): void {
92
+ // 客户端对服务发出的请求(表单)的回答
93
+ if (message.method === undefined) {
94
+ if (message.id !== undefined) {
95
+ waiting.get(message.id)?.(message)
96
+ waiting.delete(message.id)
97
+ }
98
+ return
99
+ }
40
100
  // 通知(没有 id)不用回
41
- if (request.id === undefined) return
42
- const params = request.params ?? {}
43
- switch (request.method) {
44
- case 'initialize':
45
- return reply(request.id, {
101
+ if (message.id === undefined) return
102
+ const params = message.params ?? {}
103
+ switch (message.method) {
104
+ case 'initialize': {
105
+ const elicitation = (params.capabilities as { elicitation?: { form?: unknown; url?: unknown } } | undefined)?.elicitation
106
+ formSupported = elicitation !== undefined && (elicitation.form !== undefined || elicitation.url === undefined)
107
+ return reply(message.id, {
46
108
  result: {
47
109
  protocolVersion: typeof params.protocolVersion === 'string' ? params.protocolVersion : '2025-06-18',
48
110
  capabilities: { tools: {} },
49
111
  serverInfo: { name: PLUGIN_NAME, version: VERSION },
50
112
  },
51
113
  })
114
+ }
52
115
  case 'ping':
53
- return reply(request.id, { result: {} })
116
+ return reply(message.id, { result: {} })
54
117
  case 'tools/list':
55
- return reply(request.id, { result: { tools: [TOOL] } })
118
+ return reply(message.id, { result: { tools: [TOOL] } })
56
119
  case 'tools/call': {
57
- const args = (params.arguments ?? {}) as Record<string, unknown>
58
- if (params.name !== TOOL.name) return reply(request.id, { error: { code: -32602, message: `没有这个工具:${String(params.name)}` } })
59
- if (typeof args.repo !== 'string' || typeof args.id !== 'string' || typeof args.text !== 'string') {
60
- return reply(request.id, { result: { content: [{ type: 'text', text: 'repo、id、text 都要填文字' }], isError: true } })
61
- }
62
- const confirmed = confirm(args.repo, args.id, args.text)
63
- return reply(request.id, { result: { content: [{ type: 'text', text: confirmed.text }], isError: !confirmed.ok } })
120
+ if (params.name !== TOOL.name) return reply(message.id, { error: { code: -32602, message: `没有这个工具:${String(params.name)}` } })
121
+ void callTool(message.id, (params.arguments ?? {}) as Record<string, unknown>)
122
+ return
64
123
  }
65
124
  default:
66
- return reply(request.id, { error: { code: -32601, message: `不支持 ${request.method}` } })
125
+ return reply(message.id, { error: { code: -32601, message: `不支持 ${message.method}` } })
67
126
  }
68
127
  }
69
128
 
70
129
  createInterface({ input: process.stdin }).on('line', line => {
71
130
  if (line.trim() === '') return
72
- let request: Request
131
+ let message: Message
73
132
  try {
74
- request = JSON.parse(line) as Request
133
+ message = JSON.parse(line) as Message
75
134
  } catch {
76
135
  return reply(undefined, { error: { code: -32700, message: '不是合法的 JSON' } })
77
136
  }
78
- handle(request)
137
+ handle(message)
79
138
  })
package/src/modules.ts CHANGED
@@ -1,10 +1,11 @@
1
- // 装配:把这个插件里的检查模块登记给底座。命令行入口(cli.ts、testcraft.ts、modwall.ts)、钩子入口(hooks/testpace.ts)和测试都先引用这个文件。
1
+ // 装配:把这个插件里的检查模块登记给底座。命令行入口(cli.ts、testcraft.ts、modwall.ts、codekeep.ts)、钩子入口(hooks/testpace.ts)和测试都先引用这个文件。
2
2
  // 底座(base/)不引用模块,模块之间也不互相引用;知道有哪些模块的只有这里。
3
3
  // 顺序就是整组里各段的先后(检查在前、全部测试在后)和会话说明里各模块的先后。
4
4
 
5
5
  import { registerModules } from './base/module.ts'
6
6
  import { craft } from './craft/index.ts'
7
+ import { keep } from './keep/index.ts'
7
8
  import { pace } from './pace/index.ts'
8
9
  import { wall } from './wall/index.ts'
9
10
 
10
- registerModules([wall, craft, pace])
11
+ registerModules([wall, craft, keep, pace])