gatelight 0.0.0-stage → 0.17.9

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.
Files changed (108) hide show
  1. package/.antigravity-plugin/plugin.json +9 -0
  2. package/.claude-plugin/plugin.json +11 -0
  3. package/.codex-plugin/plugin.json +18 -0
  4. package/.mcp.json +8 -0
  5. package/bin/modwall +8 -0
  6. package/bin/node.sh +14 -0
  7. package/bin/testcraft +8 -0
  8. package/bin/testpace +8 -0
  9. package/entry/command +44 -0
  10. package/hooks/antigravity.sh +24 -0
  11. package/hooks/codex.json +50 -0
  12. package/hooks/codex.sh +7 -0
  13. package/hooks/hooks.json +43 -0
  14. package/hooks/testpace.ts +25 -0
  15. package/hooks.json +16 -0
  16. package/mcp_config.json +8 -0
  17. package/package.json +21 -4
  18. package/setup.mjs +99 -0
  19. package/skills/merge/SKILL.md +42 -0
  20. package/skills/modular/SKILL.md +75 -0
  21. package/skills/setup/SKILL.md +101 -0
  22. package/skills/setup-modwall/SKILL.md +112 -0
  23. package/skills/setup-testcraft/SKILL.md +23 -0
  24. package/skills/setup-testcraft/checklist/dev-login.md +20 -0
  25. package/skills/write-tests/SKILL.md +153 -0
  26. package/src/base/agent.ts +100 -0
  27. package/src/base/baseline.ts +54 -0
  28. package/src/base/commandline.ts +40 -0
  29. package/src/base/commands.ts +94 -0
  30. package/src/base/confirm.ts +161 -0
  31. package/src/base/contract.ts +94 -0
  32. package/src/base/coverage.ts +46 -0
  33. package/src/base/entry.ts +90 -0
  34. package/src/base/excerpt.ts +46 -0
  35. package/src/base/execute.ts +70 -0
  36. package/src/base/exit.ts +6 -0
  37. package/src/base/format.ts +27 -0
  38. package/src/base/full.ts +160 -0
  39. package/src/base/gate.ts +251 -0
  40. package/src/base/git.ts +361 -0
  41. package/src/base/glob.ts +38 -0
  42. package/src/base/hook.ts +297 -0
  43. package/src/base/hookformat.ts +257 -0
  44. package/src/base/judging.ts +27 -0
  45. package/src/base/layermap.ts +69 -0
  46. package/src/base/module.ts +88 -0
  47. package/src/base/process-group.ts +101 -0
  48. package/src/base/records.ts +66 -0
  49. package/src/base/repo.ts +155 -0
  50. package/src/base/sessions.ts +55 -0
  51. package/src/base/shell.ts +176 -0
  52. package/src/base/stop.ts +100 -0
  53. package/src/base/store.ts +348 -0
  54. package/src/base/version.ts +24 -0
  55. package/src/cli.ts +186 -0
  56. package/src/craft/change.ts +24 -0
  57. package/src/craft/checklist.ts +157 -0
  58. package/src/craft/commands.ts +111 -0
  59. package/src/craft/config.ts +15 -0
  60. package/src/craft/full.ts +30 -0
  61. package/src/craft/index.ts +12 -0
  62. package/src/craft/session.ts +21 -0
  63. package/src/mcp.ts +79 -0
  64. package/src/modules.ts +10 -0
  65. package/src/modwall.ts +8 -0
  66. package/src/pace/commands.ts +281 -0
  67. package/src/pace/config.ts +103 -0
  68. package/src/pace/consent.ts +81 -0
  69. package/src/pace/focused.ts +208 -0
  70. package/src/pace/full.ts +38 -0
  71. package/src/pace/index.ts +16 -0
  72. package/src/pace/record.ts +84 -0
  73. package/src/pace/report.ts +335 -0
  74. package/src/pace/results.ts +243 -0
  75. package/src/pace/run/node-test-reporter.mjs +34 -0
  76. package/src/pace/run/node-test.ts +26 -0
  77. package/src/pace/run/playwright-reporter.mjs +48 -0
  78. package/src/pace/run/playwright.ts +34 -0
  79. package/src/pace/run/runner.ts +157 -0
  80. package/src/pace/run/targets.ts +130 -0
  81. package/src/pace/run/vitest-launcher.mjs +101 -0
  82. package/src/pace/run/vitest.ts +22 -0
  83. package/src/pace/session.ts +35 -0
  84. package/src/pace/testlist.ts +91 -0
  85. package/src/pace/tests.ts +22 -0
  86. package/src/pace/verify.ts +95 -0
  87. package/src/testcraft.ts +9 -0
  88. package/src/wall/check.ts +258 -0
  89. package/src/wall/commands.ts +279 -0
  90. package/src/wall/config.ts +394 -0
  91. package/src/wall/database.ts +136 -0
  92. package/src/wall/dbcheck.ts +389 -0
  93. package/src/wall/dbrules.ts +213 -0
  94. package/src/wall/edit.ts +64 -0
  95. package/src/wall/files.ts +35 -0
  96. package/src/wall/full.ts +30 -0
  97. package/src/wall/index.ts +15 -0
  98. package/src/wall/init.ts +149 -0
  99. package/src/wall/layermap.ts +124 -0
  100. package/src/wall/layout.ts +231 -0
  101. package/src/wall/loosen.ts +157 -0
  102. package/src/wall/miniprogram.ts +80 -0
  103. package/src/wall/project.ts +88 -0
  104. package/src/wall/rules.ts +400 -0
  105. package/src/wall/session.ts +198 -0
  106. package/src/wall/sql.ts +510 -0
  107. package/src/wall/store.ts +39 -0
  108. package/README.md +0 -3
@@ -0,0 +1,510 @@
1
+ // 读 SQL 文本(迁移文件、库里函数的正文),只给出 R6 要用的事实,不判断:
2
+ // refs 带 schema 的全名(a.b 的 a 可能是 schema,也可能是别名、表名、变量,由规则按名字认)
3
+ // targets 这句建、改、删的对象(CREATE/ALTER/DROP 表、视图、函数、schema 等,挂触发器、规则、策略、索引的表,COMMENT、GRANT 的对象),
4
+ // tables 是其中建表和把表挪进别的 schema 的
5
+ // writes 这句写的表、视图(INSERT INTO、UPDATE、DELETE FROM、MERGE INTO、TRUNCATE)
6
+ // view 这句建的是视图
7
+ // function 建函数、过程:名字、SET search_path 写的值、是不是 SECURITY DEFINER;invoker 是 ALTER … SECURITY INVOKER 改回去的函数
8
+ // inFunction 这句写在函数、过程的正文里(DO 块的正文不算)
9
+ // aliases 表、列的别名,CTE 的名字(候选:规则只看和模块 schema 同名的)
10
+ // relations FROM/JOIN/UPDATE/INTO 后面没起别名的表名(不写别名时,表名就是限定列的前缀)
11
+ // setsSearchPath 这句改了 search_path(SET、RESET search_path,set_config('search_path', …))
12
+ // switchesRole 这句切了身份(SET ROLE、RESET ROLE、SET SESSION AUTHORIZATION、set_config('role', …),也包括角色、库上设的 role;建函数、改函数时设的不算)
13
+ // schemas 这句建的 schema(CREATE SCHEMA)
14
+ // 不解析完整语法:切词(注释、字符串、带引号的名字、$$ 正文)后按分号分句,在句子里认几种固定的写法。
15
+ // 函数、DO 块的正文当成 SQL 再读一遍,得到的句子单独列出(别名只在自己的句子里起作用)。
16
+ // 拼出来再执行的 SQL(EXECUTE '...')是字符串,读不到。
17
+
18
+ export type Token =
19
+ /** 名字和关键字:不带引号的转成小写,带双引号的照原样(quoted)。 */
20
+ | { kind: 'word'; text: string; line: number; quoted: boolean }
21
+ /** 字符串:单引号的(转义已还原)、$tag$ 的(dollar)。 */
22
+ | { kind: 'string'; text: string; line: number; dollar: boolean }
23
+ | { kind: 'punct'; text: string; line: number }
24
+
25
+ export interface Ref {
26
+ schema: string
27
+ name: string
28
+ line: number
29
+ /** 写在 REFERENCES 后面(外键指向的表)。 */
30
+ references: boolean
31
+ }
32
+
33
+ /** 一个对象的名字:schema 没写全名的为 undefined。 */
34
+ export interface Name {
35
+ schema?: string
36
+ name: string
37
+ line: number
38
+ }
39
+
40
+ export interface Statement {
41
+ /** 句子开头的行。 */
42
+ line: number
43
+ refs: Ref[]
44
+ /** 这句建、改、删的对象;建、改、删 schema 本身时 schema 和 name 都是那个 schema。 */
45
+ targets: Name[]
46
+ writes: Name[]
47
+ /** 建的表(CREATE TABLE)和挪了 schema 的表(ALTER TABLE … SET SCHEMA):schema 是表最后所在的 schema。 */
48
+ tables: Name[]
49
+ view: boolean
50
+ /** 建函数、过程:searchPath 是 SET search_path 写的值,没写时为 undefined。 */
51
+ function?: Name & { searchPath?: string[]; securityDefiner: boolean }
52
+ /** ALTER FUNCTION/PROCEDURE … SECURITY INVOKER:改成按调用者执行的函数。 */
53
+ invoker?: Name
54
+ inFunction: boolean
55
+ aliases: Array<{ name: string; line: number }>
56
+ relations: Array<{ name: string; line: number }>
57
+ setsSearchPath: boolean
58
+ switchesRole: boolean
59
+ /** CREATE SCHEMA 建的 schema。 */
60
+ schemas: Name[]
61
+ }
62
+
63
+ const WORD_START = /[A-Za-z_\u0080-\uffff]/
64
+ const WORD_PART = /[A-Za-z0-9_$\u0080-\uffff]/
65
+ const DOLLAR_TAG = /^\$(?:[A-Za-z_\u0080-\uffff][A-Za-z0-9_\u0080-\uffff]*)?\$/
66
+
67
+ export function tokenize(text: string, firstLine = 1): Token[] {
68
+ const tokens: Token[] = []
69
+ let line = firstLine
70
+ let i = 0
71
+ let escapeNext = false
72
+ const lines = (part: string): number => part.split('\n').length - 1
73
+ while (i < text.length) {
74
+ const char = text[i] as string
75
+ const next = text[i + 1]
76
+ if (char === '\n') {
77
+ line++
78
+ i++
79
+ } else if (/\s/.test(char)) {
80
+ i++
81
+ } else if (char === '-' && next === '-') {
82
+ while (i < text.length && text[i] !== '\n') i++
83
+ } else if (char === '/' && next === '*') {
84
+ // 注释可以嵌套
85
+ let depth = 0
86
+ do {
87
+ if (text.startsWith('/*', i)) {
88
+ depth++
89
+ i += 2
90
+ } else if (text.startsWith('*/', i)) {
91
+ depth--
92
+ i += 2
93
+ } else {
94
+ if (text[i] === '\n') line++
95
+ i++
96
+ }
97
+ } while (depth > 0 && i < text.length)
98
+ } else if (char === "'") {
99
+ const escapes = escapeNext
100
+ escapeNext = false
101
+ const start = line
102
+ let value = ''
103
+ i++
104
+ while (i < text.length) {
105
+ const c = text[i] as string
106
+ if (escapes && c === '\\') {
107
+ value += text[i + 1] ?? ''
108
+ i += 2
109
+ continue
110
+ }
111
+ if (c === "'") {
112
+ if (text[i + 1] === "'") {
113
+ value += "'"
114
+ i += 2
115
+ continue
116
+ }
117
+ i++
118
+ break
119
+ }
120
+ if (c === '\n') line++
121
+ value += c
122
+ i++
123
+ }
124
+ tokens.push({ kind: 'string', text: value, line: start, dollar: false })
125
+ } else if (char === '"') {
126
+ let value = ''
127
+ i++
128
+ while (i < text.length) {
129
+ const c = text[i] as string
130
+ if (c === '"') {
131
+ if (text[i + 1] === '"') {
132
+ value += '"'
133
+ i += 2
134
+ continue
135
+ }
136
+ i++
137
+ break
138
+ }
139
+ if (c === '\n') line++
140
+ value += c
141
+ i++
142
+ }
143
+ tokens.push({ kind: 'word', text: value, line, quoted: true })
144
+ } else if (char === '$' && DOLLAR_TAG.test(text.slice(i, i + 64))) {
145
+ const tag = (text.slice(i).match(DOLLAR_TAG) as RegExpMatchArray)[0]
146
+ const start = line
147
+ const end = text.indexOf(tag, i + tag.length)
148
+ const body = end === -1 ? text.slice(i + tag.length) : text.slice(i + tag.length, end)
149
+ tokens.push({ kind: 'string', text: body, line: start, dollar: true })
150
+ line += lines(body)
151
+ i = end === -1 ? text.length : end + tag.length
152
+ } else if (WORD_START.test(char)) {
153
+ let j = i + 1
154
+ while (j < text.length && WORD_PART.test(text[j] as string)) j++
155
+ const word = text.slice(i, j).toLowerCase()
156
+ // E'…':反斜杠转义的字符串,前缀不是词
157
+ if (word === 'e' && text[j] === "'") escapeNext = true
158
+ else tokens.push({ kind: 'word', text: word, line, quoted: false })
159
+ i = j
160
+ } else if (/[0-9]/.test(char)) {
161
+ let j = i + 1
162
+ while (j < text.length && /[0-9.eE]/.test(text[j] as string) && !(text[j] === '.' && text[j + 1] === '.')) j++
163
+ tokens.push({ kind: 'punct', text: text.slice(i, j), line })
164
+ i = j
165
+ } else if (char === ':' && next === ':') {
166
+ tokens.push({ kind: 'punct', text: '::', line })
167
+ i += 2
168
+ } else {
169
+ tokens.push({ kind: 'punct', text: char, line })
170
+ i++
171
+ }
172
+ }
173
+ return tokens
174
+ }
175
+
176
+ const isWord = (token: Token | undefined, ...texts: string[]): boolean => token?.kind === 'word' && !token.quoted && (texts.length === 0 || texts.includes(token.text))
177
+ const isPunct = (token: Token | undefined, text: string): boolean => token?.kind === 'punct' && token.text === text
178
+ const anyWord = (token: Token | undefined): token is Extract<Token, { kind: 'word' }> => token?.kind === 'word'
179
+
180
+ /** 关系后面的这些词不是别名,是句子接下来的部分。 */
181
+ const NOT_ALIAS = new Set([
182
+ 'where', 'join', 'left', 'right', 'inner', 'outer', 'full', 'cross', 'natural', 'on', 'using', 'group', 'order', 'limit', 'offset', 'having',
183
+ 'window', 'union', 'intersect', 'except', 'returning', 'set', 'values', 'for', 'fetch', 'into', 'select', 'default', 'tablesample', 'with', 'loop',
184
+ 'then', 'when', 'else', 'end', 'and', 'or', 'not', 'is', 'in', 'strict', 'do', 'overriding',
185
+ ])
186
+
187
+ /** 关系名后面的这些词不是关系:触发器的 UPDATE ON/OF,外键的 ON UPDATE CASCADE,FOR UPDATE 加锁等。 */
188
+ const NOT_RELATION = new Set(['on', 'of', 'or', 'cascade', 'restrict', 'set', 'no', 'nowait', 'skip', 'select', 'values', 'default', 'with'])
189
+
190
+ /** SQL 文本里的句子:按分号分开;函数、DO 块的正文再读一遍,接在后面。 */
191
+ export function statements(text: string, firstLine = 1): Statement[] {
192
+ const tokens = tokenize(text, firstLine)
193
+ const result: Statement[] = []
194
+ let start = 0
195
+ for (let i = 0; i <= tokens.length; i++) {
196
+ if (i < tokens.length && !isPunct(tokens[i], ';')) continue
197
+ if (i > start) result.push(...statement(tokens.slice(start, i)))
198
+ start = i + 1
199
+ }
200
+ return result
201
+ }
202
+
203
+ /** CREATE/ALTER/DROP 后面认的对象种类(MATERIALIZED VIEW 按 VIEW 认)。 */
204
+ const KINDS = new Set(['table', 'view', 'function', 'procedure', 'sequence', 'type', 'domain', 'aggregate', 'index', 'schema'])
205
+ /** CREATE 和种类之间可以有的词。 */
206
+ const MODIFIERS = new Set(['or', 'replace', 'global', 'local', 'temp', 'temporary', 'unlogged', 'foreign', 'recursive', 'materialized', 'unique', 'concurrently'])
207
+
208
+ function statement(tokens: Token[]): Statement[] {
209
+ const found: Statement = { line: tokens[0]?.line ?? 0, refs: [], targets: [], writes: [], tables: [], view: false, aliases: [], relations: [], setsSearchPath: false, switchesRole: false, schemas: [], inFunction: false }
210
+ const nested: Statement[] = []
211
+ const creates = isWord(tokens[0], 'create')
212
+ let at = 1
213
+ if (creates && isWord(tokens[at], 'or') && isWord(tokens[at + 1], 'replace')) at += 2
214
+ const isFunction = creates && isWord(tokens[at], 'function', 'procedure')
215
+ // DO 块的正文是句子里第一个字符串(DO $$…$$、DO LANGUAGE x '…')
216
+ let doBody = isWord(tokens[0], 'do')
217
+
218
+ for (let i = 0; i < tokens.length; i++) {
219
+ const token = tokens[i] as Token
220
+ const previous = tokens[i - 1]
221
+ const next = tokens[i + 1]
222
+
223
+ // 正文:CREATE FUNCTION … AS '…'/$$…$$,DO 块
224
+ if (token.kind === 'string' && ((isFunction && isWord(previous, 'as')) || doBody)) {
225
+ const inner = statements(token.text, token.line)
226
+ nested.push(...(doBody ? inner : inner.map(item => ({ ...item, inFunction: true }))))
227
+ doBody = false
228
+ continue
229
+ }
230
+
231
+ // a.b:前面也是点的(a.b.c 的 b)不算
232
+ if (anyWord(token) && isPunct(next, '.') && !isPunct(previous, '.')) {
233
+ const after = tokens[i + 2]
234
+ if (anyWord(after) || isPunct(after, '*')) {
235
+ found.refs.push({ schema: token.text, name: after?.text ?? '*', line: token.line, references: isWord(previous, 'references') })
236
+ }
237
+ }
238
+
239
+ // 别名:x AS 别名(后面不跟点和括号);CTE 名字 AS [NOT] [MATERIALIZED] (
240
+ if (isWord(token, 'as')) {
241
+ if (anyWord(next) && !isPunct(tokens[i + 2], '.') && !isPunct(tokens[i + 2], '(')) found.aliases.push({ name: next.text, line: next.line })
242
+ let k = i + 1
243
+ if (isWord(tokens[k], 'not')) k++
244
+ if (isWord(tokens[k], 'materialized')) k++
245
+ if (isPunct(tokens[k], '(') && anyWord(previous)) found.aliases.push({ name: previous.text, line: previous.line })
246
+ }
247
+
248
+ // 写入:INSERT INTO、MERGE INTO、DELETE FROM、UPDATE [ONLY]、TRUNCATE [TABLE] [ONLY] 后面的关系
249
+ const written =
250
+ isWord(token, 'insert', 'merge') && isWord(next, 'into') ? i + 2
251
+ : isWord(token, 'delete') && isWord(next, 'from') ? i + 2
252
+ : isWord(token, 'update') ? i + 1
253
+ : isWord(token, 'truncate') ? i + (isWord(next, 'table') ? 2 : 1)
254
+ : -1
255
+ if (written !== -1) {
256
+ const k = isWord(tokens[written], 'only') ? written + 1 : written
257
+ const name = isWord(tokens[k]) && NOT_RELATION.has((tokens[k] as Extract<Token, { kind: 'word' }>).text) ? undefined : qualified(tokens, k)
258
+ if (name) found.writes.push(name)
259
+ }
260
+
261
+ // FROM/JOIN/UPDATE/USING/INTO 后面的关系不写 AS 的别名:关系 别名[, 关系 别名…]
262
+ if (isWord(token, 'from', 'join', 'update', 'using', 'into')) {
263
+ let k = i + 1
264
+ for (;;) {
265
+ while (isWord(tokens[k], 'only', 'lateral')) k++
266
+ const first = tokens[k]
267
+ if (anyWord(first) && !(isWord(first) && NOT_RELATION.has(first.text))) {
268
+ k++
269
+ while (isPunct(tokens[k], '.') && anyWord(tokens[k + 1])) k += 2
270
+ } else if (!isPunct(first, '(')) break
271
+ const relation = anyWord(first) ? (tokens[k - 1] as Extract<Token, { kind: 'word' }>) : undefined
272
+ const call = isPunct(tokens[k], '(')
273
+ if (call) k = closing(tokens, k) + 1
274
+ const alias = tokens[k]
275
+ if (anyWord(alias) && !isWord(alias, 'as') && !(isWord(alias) && NOT_ALIAS.has(alias.text))) {
276
+ found.aliases.push({ name: alias.text, line: alias.line })
277
+ k++
278
+ } else if (relation && !call && !isWord(alias, 'as')) found.relations.push({ name: relation.text, line: relation.line })
279
+ if (!isPunct(tokens[k], ',')) break
280
+ k++
281
+ }
282
+ }
283
+ }
284
+
285
+ // 建、改、删的对象:CREATE [修饰] 种类 [IF NOT EXISTS] 名字;ALTER 种类 [IF EXISTS] [ONLY] 名字;DROP 种类 [IF EXISTS] 名字[, 名字…]
286
+ const action = isWord(tokens[0], 'create', 'alter', 'drop') ? (tokens[0] as Extract<Token, { kind: 'word' }>).text : undefined
287
+ if (action) {
288
+ let k = 1
289
+ while (action === 'create' && isWord(tokens[k]) && MODIFIERS.has((tokens[k] as Extract<Token, { kind: 'word' }>).text)) k++
290
+ if (action !== 'create' && isWord(tokens[k], 'materialized')) k++
291
+ const kind = isWord(tokens[k]) ? (tokens[k] as Extract<Token, { kind: 'word' }>).text : undefined
292
+ if (kind && KINDS.has(kind)) {
293
+ k++
294
+ if (isWord(tokens[k], 'if')) k += isWord(tokens[k + 1], 'not') ? 3 : 2
295
+ if (isWord(tokens[k], 'only')) k++
296
+ found.view = action === 'create' && kind === 'view'
297
+ for (;;) {
298
+ const name = qualified(tokens, k)
299
+ if (!name) break
300
+ const target = kind === 'schema' ? { schema: name.name, name: name.name, line: name.line } : name
301
+ found.targets.push(target)
302
+ if (action === 'create' && kind === 'table') found.tables.push(target)
303
+ if (action === 'create' && kind === 'schema') found.schemas.push(target)
304
+ k += name.schema === undefined ? 1 : 3
305
+ if (action !== 'drop' || !isPunct(tokens[k], ',')) break
306
+ k++
307
+ }
308
+ // 挪 schema:ALTER 种类 名字 SET SCHEMA 新 schema
309
+ const set = tokens.findIndex((item, index) => isWord(item, 'set') && isWord(tokens[index + 1], 'schema'))
310
+ const moved = tokens[set + 2]
311
+ const name = found.targets[0]
312
+ if (action === 'alter' && set !== -1 && anyWord(moved) && name) {
313
+ const target = { schema: moved.text, name: name.name, line: name.line }
314
+ found.targets.push(target)
315
+ if (kind === 'table') found.tables.push(target)
316
+ }
317
+ }
318
+ }
319
+
320
+ found.targets.push(...attachedTo(tokens))
321
+
322
+ // 改 search_path:SET [SESSION|LOCAL] search_path …(也包括 ALTER ROLE/DATABASE … SET search_path)、RESET search_path、set_config('search_path', …)。
323
+ // 建函数、改函数时的 SET search_path 是函数自己的设置,不算
324
+ const altersFunction = isWord(tokens[0], 'alter') && isWord(tokens[1], 'function', 'procedure', 'routine')
325
+ const ownSetting = isFunction || altersFunction
326
+ if (altersFunction && security(tokens) === 'invoker') {
327
+ const name = qualified(tokens, 2)
328
+ if (name) found.invoker = name
329
+ }
330
+ found.setsSearchPath = tokens.some((token, index) => {
331
+ if (isWord(token, 'reset') && isWord(tokens[index + 1], 'search_path') && !ownSetting) return true
332
+ if (isWord(token, 'set') && !ownSetting) {
333
+ const at = isWord(tokens[index + 1], 'session', 'local') ? index + 2 : index + 1
334
+ if (isWord(tokens[at], 'search_path')) return true
335
+ }
336
+ const value = tokens[index + 2]
337
+ return isWord(token, 'set_config') && isPunct(tokens[index + 1], '(') && value?.kind === 'string' && value.text === 'search_path'
338
+ })
339
+
340
+ // 切身份:SET [SESSION|LOCAL] ROLE、SET [SESSION|LOCAL] SESSION AUTHORIZATION、RESET ROLE、RESET SESSION AUTHORIZATION、set_config('role', …)。
341
+ // 角色、库上的 SET role 也算(登录时就切了)。建函数、改函数时的 SET role 是函数自己的设置,规则要求函数一律 SECURITY DEFINER,库会拒绝,不在这里算。
342
+ // UPDATE … SET role = … 是给叫 role 的列赋值,不算
343
+ const update = tokens.findIndex(token => isWord(token, 'update'))
344
+ found.switchesRole = tokens.some((token, index) => {
345
+ if (ownSetting && isWord(token, 'set', 'reset')) return false
346
+ if (update !== -1 && update < index && isWord(token, 'set', 'reset')) return false
347
+ if (isWord(token, 'reset')) return isWord(tokens[index + 1], 'role') || (isWord(tokens[index + 1], 'session') && isWord(tokens[index + 2], 'authorization'))
348
+ if (isWord(token, 'set')) {
349
+ const at = isWord(tokens[index + 1], 'local') || (isWord(tokens[index + 1], 'session') && !isWord(tokens[index + 2], 'authorization')) ? index + 2 : index + 1
350
+ return isWord(tokens[at], 'role') || (isWord(tokens[at], 'session') && isWord(tokens[at + 1], 'authorization'))
351
+ }
352
+ const value = tokens[index + 2]
353
+ return isWord(token, 'set_config') && isPunct(tokens[index + 1], '(') && value?.kind === 'string' && value.text.toLowerCase() === 'role'
354
+ })
355
+
356
+ if (isFunction) {
357
+ const name = qualified(tokens, at + 1)
358
+ if (name) found.function = { ...name, ...searchPath(tokens), securityDefiner: security(tokens) === 'definer' }
359
+ }
360
+ return [found, ...nested]
361
+ }
362
+
363
+ /** COMMENT ON、GRANT/REVOKE … ON 后面可以有的种类词。 */
364
+ const OBJECT_WORDS = new Set([
365
+ 'table', 'view', 'materialized', 'foreign', 'function', 'procedure', 'routine', 'sequence', 'type', 'domain', 'aggregate', 'index', 'schema', 'column',
366
+ 'large', 'object',
367
+ ])
368
+
369
+ /**
370
+ * 挂在别的对象上、或者改别的对象的句子,给出那个对象:CREATE TRIGGER/POLICY/INDEX … ON 名字、CREATE RULE … TO 名字;
371
+ * COMMENT ON [种类] 名字(COMMENT ON TRIGGER/POLICY/RULE/CONSTRAINT 名字 ON 表);GRANT/REVOKE … ON [种类] 名字[, …] 或 ON ALL … IN SCHEMA s。
372
+ */
373
+ function attachedTo(tokens: Token[]): Name[] {
374
+ const onAfter = (from: number): number => tokens.findIndex((token, index) => index >= from && isWord(token, 'on'))
375
+ const word = (k: number): string | undefined => (isWord(tokens[k]) ? (tokens[k] as Extract<Token, { kind: 'word' }>).text : undefined)
376
+ if (isWord(tokens[0], 'create')) {
377
+ let k = 1
378
+ while (word(k) && (MODIFIERS.has(word(k) as string) || word(k) === 'constraint')) k++
379
+ const kind = word(k)
380
+ if (kind === 'rule') {
381
+ const on = onAfter(k)
382
+ const to = tokens.findIndex((token, index) => index > on && on !== -1 && isWord(token, 'to'))
383
+ const name = to === -1 ? undefined : qualified(tokens, to + 1)
384
+ return name ? [name] : []
385
+ }
386
+ if (kind === 'trigger' || kind === 'policy' || kind === 'index') {
387
+ const on = onAfter(k)
388
+ const name = on === -1 ? undefined : qualified(tokens, isWord(tokens[on + 1], 'only') ? on + 2 : on + 1)
389
+ return name ? [name] : []
390
+ }
391
+ return []
392
+ }
393
+ if (isWord(tokens[0], 'comment') && isWord(tokens[1], 'on')) {
394
+ let k = 2
395
+ if (isWord(tokens[k], 'trigger', 'policy', 'rule', 'constraint')) {
396
+ const on = onAfter(k + 1)
397
+ const name = on === -1 ? undefined : qualified(tokens, on + 1)
398
+ return name ? [name] : []
399
+ }
400
+ const column = isWord(tokens[k], 'column')
401
+ const schema = isWord(tokens[k], 'schema')
402
+ while (word(k) && OBJECT_WORDS.has(word(k) as string) && !isPunct(tokens[k + 1], '.')) k++
403
+ const parts = dotted(tokens, k)
404
+ if (column) parts.pop()
405
+ if (schema && parts[0]) return [{ schema: parts[0].text, name: parts[0].text, line: parts[0].line }]
406
+ return parts.length >= 2 ? [{ schema: (parts[0] as Extract<Token, { kind: 'word' }>).text, name: (parts[1] as Extract<Token, { kind: 'word' }>).text, line: (parts[0] as Token).line }] : []
407
+ }
408
+ if (isWord(tokens[0], 'grant', 'revoke')) {
409
+ const on = onAfter(1)
410
+ if (on === -1) return []
411
+ const inSchema = tokens.findIndex((token, index) => index > on && isWord(token, 'in') && isWord(tokens[index + 1], 'schema'))
412
+ if (isWord(tokens[on + 1], 'all') && inSchema !== -1) {
413
+ const schema = tokens[inSchema + 2]
414
+ return anyWord(schema) ? [{ schema: schema.text, name: schema.text, line: schema.line }] : []
415
+ }
416
+ let k = on + 1
417
+ const schemaKind = isWord(tokens[k], 'schema')
418
+ while (word(k) && OBJECT_WORDS.has(word(k) as string) && !isPunct(tokens[k + 1], '.')) k++
419
+ const names: Name[] = []
420
+ for (;;) {
421
+ const name = qualified(tokens, k)
422
+ if (!name) break
423
+ names.push(schemaKind ? { schema: name.name, name: name.name, line: name.line } : name)
424
+ k += name.schema === undefined ? 1 : 3
425
+ if (isPunct(tokens[k], '(')) k = closing(tokens, k) + 1
426
+ if (!isPunct(tokens[k], ',')) break
427
+ k++
428
+ }
429
+ return names
430
+ }
431
+ return []
432
+ }
433
+
434
+ /** 点连起来的名字的各段:a.b.c。 */
435
+ function dotted(tokens: Token[], k: number): Array<Extract<Token, { kind: 'word' }>> {
436
+ const parts: Array<Extract<Token, { kind: 'word' }>> = []
437
+ let token = tokens[k]
438
+ while (anyWord(token)) {
439
+ parts.push(token)
440
+ if (!isPunct(tokens[k + 1], '.')) break
441
+ k += 2
442
+ token = tokens[k]
443
+ }
444
+ return parts
445
+ }
446
+
447
+ /** 名字或 schema.名字。 */
448
+ function qualified(tokens: Token[], k: number): { schema?: string; name: string; line: number } | undefined {
449
+ const first = tokens[k]
450
+ if (!anyWord(first)) return undefined
451
+ const second = tokens[k + 2]
452
+ if (isPunct(tokens[k + 1], '.') && anyWord(second)) return { schema: first.text, name: second.text, line: first.line }
453
+ return { name: first.text, line: first.line }
454
+ }
455
+
456
+ /** 建函数、改函数时写的 [EXTERNAL] SECURITY DEFINER / INVOKER。正文是字符串,不会误认。 */
457
+ function security(tokens: Token[]): 'definer' | 'invoker' | undefined {
458
+ const at = tokens.findIndex((item, index) => isWord(item, 'security') && isWord(tokens[index + 1], 'definer', 'invoker'))
459
+ return at === -1 ? undefined : ((tokens[at + 1] as Extract<Token, { kind: 'word' }>).text as 'definer' | 'invoker')
460
+ }
461
+
462
+ /** 函数定义里 SET search_path = / TO 后面的值(逗号分开)。FROM CURRENT 给出空列表。 */
463
+ function searchPath(tokens: Token[]): { searchPath?: string[] } {
464
+ const set = tokens.findIndex((item, index) => isWord(item, 'set') && isWord(tokens[index + 1], 'search_path'))
465
+ if (set === -1) return {}
466
+ let k = set + 2
467
+ if (!isPunct(tokens[k], '=') && !isWord(tokens[k], 'to')) return { searchPath: [] }
468
+ k++
469
+ const values: string[] = []
470
+ for (;;) {
471
+ const value = tokens[k]
472
+ if (isWord(value, 'from')) break
473
+ if (value?.kind !== 'word' && value?.kind !== 'string') break
474
+ values.push(value.text)
475
+ if (!isPunct(tokens[k + 1], ',')) break
476
+ k += 2
477
+ }
478
+ return { searchPath: values }
479
+ }
480
+
481
+ /** 从开括号找到配对的闭括号;没配上就到末尾。 */
482
+ function closing(tokens: Token[], open: number): number {
483
+ let depth = 0
484
+ for (let k = open; k < tokens.length; k++) {
485
+ if (isPunct(tokens[k], '(')) depth++
486
+ else if (isPunct(tokens[k], ')') && --depth === 0) return k
487
+ }
488
+ return tokens.length - 1
489
+ }
490
+
491
+ /** 库里存的 search_path 设置(proconfig 里 search_path= 后面那段):逗号分开,双引号里的照原样。 */
492
+ export function parseSearchPath(value: string): string[] {
493
+ const items: string[] = []
494
+ let current = ''
495
+ let quoted = false
496
+ for (let i = 0; i < value.length; i++) {
497
+ const c = value[i] as string
498
+ if (c === '"') {
499
+ if (quoted && value[i + 1] === '"') {
500
+ current += '"'
501
+ i++
502
+ } else quoted = !quoted
503
+ } else if (c === ',' && !quoted) {
504
+ items.push(current.trim())
505
+ current = ''
506
+ } else current += c
507
+ }
508
+ items.push(current.trim())
509
+ return items.filter(item => item !== '')
510
+ }
@@ -0,0 +1,39 @@
1
+ // "模块边界"要记的东西:检查的通过记录(按内容记)、每个分支上一次查全了的越界、每次检查的日志。会话起点归底座(base/baseline.ts),记什么见 session.ts。
2
+ // 都经底座存取(base/records.ts,文件在 .git/testpace/wall/ 下,所有 worktree 共用),这里只定记的是什么。
3
+ // 用户同意过的放宽项不在这里:归底座的用户确认(base/confirm.ts)。
4
+
5
+ import { hash, ModuleRecords } from '../base/records.ts'
6
+ import type { Repo } from '../base/repo.ts'
7
+ import { newRunId } from '../base/store.ts'
8
+
9
+ export interface PassRecord {
10
+ at: number
11
+ }
12
+
13
+ /** 一个分支上一次查全了的检查:下一次据此算改正了哪些越界。 */
14
+ export interface LastCheck {
15
+ violations: string[]
16
+ at: number
17
+ }
18
+
19
+ export interface Store {
20
+ /** modwall check 的通过记录,按当时的内容记,内容变了就对不上。 */
21
+ passes: ModuleRecords<PassRecord>
22
+ /** 按分支记。 */
23
+ last: ModuleRecords<LastCheck>
24
+ /** 新日志的路径(底座只留最近的若干份)。 */
25
+ logPath(name: string): string
26
+ }
27
+
28
+ export const MODULE = 'wall'
29
+
30
+ export function openStore(repo: Repo): Store {
31
+ return {
32
+ passes: new ModuleRecords(repo.store, MODULE, 'passes'),
33
+ last: new ModuleRecords(repo.store, MODULE, 'last'),
34
+ logPath: name => repo.store.logPath(`${newRunId(Date.now())}-${name}`),
35
+ }
36
+ }
37
+
38
+ /** 分支名当记录的键:取摘要,名字里有什么字符都不要紧。 */
39
+ export const branchKey = (branch: string): string => hash('branch', branch)
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.