@oakgreencc/graft 0.2.18 → 0.2.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oakgreencc/graft",
3
- "version": "0.2.18",
3
+ "version": "0.2.19",
4
4
  "description": "Let an agent work a GitHub project without the local git CLI's write path: Verified commits created by a GitHub App, the repo's git hooks run at the right moments, CODEOWNERS as the manifest of what needs a human, and a scoped maintainer mode for when one says yes.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "type": "module",
@@ -66,7 +66,7 @@ import { isAbsolute, resolve } from 'node:path'
66
66
  import { contextForPath } from '../context.mjs'
67
67
  import { currentSession, grantAdvice } from '../maintainer.mjs'
68
68
  import { judge, keyDiffers, normalise } from '../protected.mjs'
69
- import { executedCommands, segments, tokens } from './shell-text.mjs'
69
+ import { executedCommands, segments, statements, tokens } from './shell-text.mjs'
70
70
 
71
71
  /** Tools whose `file_path` (or `notebook_path`) input is a write. */
72
72
  const WRITE_TOOLS = new Set(['Write', 'Edit', 'NotebookEdit', 'MultiEdit'])
@@ -110,31 +110,89 @@ const REMOVER = verb('rm|unlink|truncate')
110
110
  * @returns {string[]}
111
111
  */
112
112
  export function writeTargets(command) {
113
- return [...new Set(executedCommands(command).flatMap(targetsOf))]
113
+ return [...new Set(writeTargetsIn(command).map((t) => t.path))]
114
+ }
115
+
116
+ /**
117
+ * Like `writeTargets`, each with the directory a preceding `cd <dir>` in the
118
+ * same command moved to (`dir`, as typed: relative to the event's `cwd`, or
119
+ * absolute), or `null` for none. `cd packages/x && sed -i … package.json`
120
+ * writes `packages/x/package.json`, not the root `package.json`. A `cd` whose
121
+ * target cannot be read (`cd`, `cd -`, `cd "$D"`) leaves the paths after it
122
+ * judged from `cwd`, as before. Nested `sh -c` payloads start from `cwd`.
123
+ *
124
+ * @param {string} command
125
+ * @returns {{ path: string, dir: string | null }[]}
126
+ */
127
+ export function writeTargetsIn(command) {
128
+ const seen = new Set()
129
+ const out = []
130
+ for (const t of executedCommands(command).flatMap(targetsOf)) {
131
+ const k = `${t.dir ?? ''}\0${t.path}`
132
+ if (seen.has(k)) continue
133
+ seen.add(k)
134
+ out.push(t)
135
+ }
136
+ return out
137
+ }
138
+
139
+ /** A `cd` target that can be read as typed: no expansion, not `-`, not `~`. */
140
+ const PLAIN_DIR = /^[\w./@+-]+$/
141
+
142
+ /**
143
+ * Where each statement of ONE command runs, by offset: the `cd <dir>`
144
+ * statements before it, chained. A `cd` inside a pipeline runs in a subshell
145
+ * and moves nothing.
146
+ *
147
+ * @param {{ raw: string, masked: string }} one
148
+ * @returns {(offset: number) => string | null}
149
+ */
150
+ function dirsOf({ raw, masked }) {
151
+ /** @type {{ start: number, dir: string | null }[]} */
152
+ const moves = []
153
+ let dir = /** @type {string | null} */ (null)
154
+ for (const st of statements(masked)) {
155
+ const toks = tokens(st, raw)
156
+ if (toks[0]?.masked !== 'cd') continue
157
+ if (segments(st.text).length > 1) continue
158
+ const arg = toks.length === 2 ? unquote(toks[1].raw) : null
159
+ const to = arg && PLAIN_DIR.test(arg) && arg !== '-' ? arg : null
160
+ dir = to === null ? null : isAbsolute(to) || dir === null ? to : `${dir}/${to}`
161
+ moves.push({ start: st.start + st.text.length, dir })
162
+ }
163
+ return (offset) => {
164
+ let at = null
165
+ for (const m of moves) if (m.start <= offset) at = m.dir
166
+ return at
167
+ }
114
168
  }
115
169
 
116
170
  /**
117
171
  * The write targets of ONE command, nested payloads not included.
118
172
  *
119
173
  * @param {{ raw: string, masked: string }} one
120
- * @returns {string[]}
174
+ * @returns {{ path: string, dir: string | null }[]}
121
175
  */
122
- function targetsOf({ raw: command, masked }) {
176
+ function targetsOf(one) {
177
+ const { raw: command, masked } = one
178
+ const dirAt = dirsOf(one)
179
+ /** @type {{ path: string, dir: string | null }[]} */
123
180
  const out = []
124
- const add = (t) => {
181
+ const add = (t, offset) => {
125
182
  const p = unquote(t ?? '')
126
- if (pathish(p)) out.push(p)
183
+ if (pathish(p)) out.push({ path: p, dir: dirAt(offset) })
127
184
  }
128
185
  const rawAt = (m, group) => command.slice(m.index + m[0].length - m[group].length, m.index + m[0].length)
129
186
 
130
187
  // `> path`, `>> path`, `2> path`, `&> path`.
131
- for (const m of masked.matchAll(/(?:^|[^0-9<>&])(?:[0-9]|&)?>>?\s*(['"]?[^\s;|&<>()]+['"]?)/g)) add(rawAt(m, 1))
188
+ for (const m of masked.matchAll(/(?:^|[^0-9<>&])(?:[0-9]|&)?>>?\s*(['"]?[^\s;|&<>()]+['"]?)/g))
189
+ add(rawAt(m, 1), m.index)
132
190
 
133
191
  // `dd of=path`
134
- for (const m of masked.matchAll(/\bdd\b[^\n;|&]*?\bof=(['"]?[^\s;|&]+['"]?)/g)) add(rawAt(m, 1))
192
+ for (const m of masked.matchAll(/\bdd\b[^\n;|&]*?\bof=(['"]?[^\s;|&]+['"]?)/g)) add(rawAt(m, 1), m.index)
135
193
 
136
194
  // `tee path`, `tee -a path`
137
- for (const m of masked.matchAll(/\btee\b\s+(?:-\S+\s+)*(['"]?[^\s;|&<>()]+['"]?)/g)) add(rawAt(m, 1))
195
+ for (const m of masked.matchAll(/\btee\b\s+(?:-\S+\s+)*(['"]?[^\s;|&<>()]+['"]?)/g)) add(rawAt(m, 1), m.index)
138
196
 
139
197
  for (const seg of segments(masked)) {
140
198
  const toks = tokens(seg, command)
@@ -145,7 +203,7 @@ function targetsOf({ raw: command, masked }) {
145
203
  if (editor >= 0) {
146
204
  const args = toks.slice(editor + 1)
147
205
  if (args.some((t) => isFlag(t.masked) && IN_PLACE_FLAG.test(t.masked))) {
148
- for (const t of args) if (!isFlag(t.masked) && !SED_EXPRESSION.test(t.masked)) add(t.raw)
206
+ for (const t of args) if (!isFlag(t.masked) && !SED_EXPRESSION.test(t.masked)) add(t.raw, seg.start)
149
207
  }
150
208
  }
151
209
 
@@ -154,20 +212,20 @@ function targetsOf({ raw: command, masked }) {
154
212
  const mover = verbIndex(toks, MOVER)
155
213
  if (mover >= 0) {
156
214
  const args = toks.slice(mover + 1).filter((t) => !isFlag(t.masked))
157
- if (toks[mover].masked.endsWith('mv')) for (const t of args) add(t.raw)
158
- else if (args.length >= 2) add(args[args.length - 1].raw)
215
+ if (toks[mover].masked.endsWith('mv')) for (const t of args) add(t.raw, seg.start)
216
+ else if (args.length >= 2) add(args[args.length - 1].raw, seg.start)
159
217
  }
160
218
 
161
219
  // Removal is a write.
162
220
  const remover = verbIndex(toks, REMOVER)
163
- if (remover >= 0) for (const t of toks.slice(remover + 1)) if (!isFlag(t.masked)) add(t.raw)
221
+ if (remover >= 0) for (const t of toks.slice(remover + 1)) if (!isFlag(t.masked)) add(t.raw, seg.start)
164
222
  }
165
223
 
166
224
  // Scripting one-liners that open a file for writing. Scanned RAW — the path
167
225
  // is the quoted string. A read (`open(path).read()`) is not matched.
168
- for (const m of command.matchAll(/\bopen\(\s*(['"])([^'"\n]+)\1\s*,\s*(?:mode\s*=\s*)?['"][wax]/g)) add(m[2])
169
- for (const m of command.matchAll(/\bPath\(\s*(['"])([^'"\n]+)\1\s*\)\.write_(?:text|bytes)\(/g)) add(m[2])
170
- for (const m of command.matchAll(/\b(?:writeFile|appendFile)(?:Sync)?\(\s*(['"])([^'"\n]+)\1/g)) add(m[2])
226
+ for (const m of command.matchAll(/\bopen\(\s*(['"])([^'"\n]+)\1\s*,\s*(?:mode\s*=\s*)?['"][wax]/g)) add(m[2], m.index)
227
+ for (const m of command.matchAll(/\bPath\(\s*(['"])([^'"\n]+)\1\s*\)\.write_(?:text|bytes)\(/g)) add(m[2], m.index)
228
+ for (const m of command.matchAll(/\b(?:writeFile|appendFile)(?:Sync)?\(\s*(['"])([^'"\n]+)\1/g)) add(m[2], m.index)
171
229
 
172
230
  return out
173
231
  }
@@ -322,8 +380,9 @@ export function bashVerdict({ command, cwd, lookup }) {
322
380
  ' ! graft maintainer grant "<why this session needs it>" --scope <scope>\n\n' +
323
381
  'Say which scope you need (protected, no-verify, merge) and why.'
324
382
  )
325
- for (const path of writeTargets(command)) {
326
- const reason = judgePath({ path, cwd, lookup, how: ' (named by a shell write)', keyChanged: () => false })
383
+ for (const { path, dir } of writeTargetsIn(command)) {
384
+ const at = dir === null ? cwd : resolve(cwd, dir)
385
+ const reason = judgePath({ path, cwd: at, lookup, how: ' (named by a shell write)', keyChanged: () => false })
327
386
  if (reason) return reason
328
387
  }
329
388
  return null