@hasna/hooks 0.13.0 → 0.13.2

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.
@@ -323,16 +323,16 @@ async function handleReadinessSessionStart(provider, event, dependencies = {}) {
323
323
  return readinessSessionStartOutput(await runTrashReadiness(provider, dependencies));
324
324
  }
325
325
  export {
326
- TRASH_READINESS_DEFAULT_TIMEOUT_MS,
327
- TRASH_READINESS_MAX_TIMEOUT_MS,
328
- TRASH_READINESS_NATIVE_TIMEOUT_SECONDS,
329
- checkTrashReadinessBinding,
330
- handleReadinessSessionStart,
331
- isReadinessProvider,
332
- parseTrashReadinessBinding,
333
- readTrashReadinessBindingFile,
334
- readinessContext,
335
- readinessSessionStartOutput,
326
+ trashReadinessBindingPath,
336
327
  runTrashReadiness,
337
- trashReadinessBindingPath
328
+ readinessSessionStartOutput,
329
+ readinessContext,
330
+ readTrashReadinessBindingFile,
331
+ parseTrashReadinessBinding,
332
+ isReadinessProvider,
333
+ handleReadinessSessionStart,
334
+ checkTrashReadinessBinding,
335
+ TRASH_READINESS_NATIVE_TIMEOUT_SECONDS,
336
+ TRASH_READINESS_MAX_TIMEOUT_MS,
337
+ TRASH_READINESS_DEFAULT_TIMEOUT_MS
338
338
  };
@@ -588,14 +588,14 @@ async function verifyNativeSafetyExecutionRegistration(home = homedir2()) {
588
588
  return { path: before.path, sha256: before.sha256 };
589
589
  }
590
590
  export {
591
- NativeSafetyError,
592
- evaluateNativeSafety,
593
- evaluateNativeSafetyForExecution,
594
- nativeSafetyBindingIntegrityReason,
595
- nativeSafetyRecordText,
596
- readNativeSafetyRegistration,
597
- registerNativeSafety,
598
- verifyNativeSafetyCommand,
591
+ verifyNativeSafetyRegistration,
599
592
  verifyNativeSafetyExecutionRegistration,
600
- verifyNativeSafetyRegistration
593
+ verifyNativeSafetyCommand,
594
+ registerNativeSafety,
595
+ readNativeSafetyRegistration,
596
+ nativeSafetyRecordText,
597
+ nativeSafetyBindingIntegrityReason,
598
+ evaluateNativeSafetyForExecution,
599
+ evaluateNativeSafety,
600
+ NativeSafetyError
601
601
  };
@@ -385,10 +385,10 @@ var opencode_stop_plugin_default = {
385
385
  server: async (input, options) => createOpenCodeStopReviewPlugin(options).server(input)
386
386
  };
387
387
  export {
388
- OPENCODE_STOP_HOOK,
389
- OPENCODE_STOP_PLUGIN_ID,
390
- OPENCODE_STOP_PLUGIN_SCHEMA,
391
- createOpenCodeStopReviewPlugin,
388
+ isOpenCodeStopReviewCapability,
392
389
  opencode_stop_plugin_default as default,
393
- isOpenCodeStopReviewCapability
390
+ createOpenCodeStopReviewPlugin,
391
+ OPENCODE_STOP_PLUGIN_SCHEMA,
392
+ OPENCODE_STOP_PLUGIN_ID,
393
+ OPENCODE_STOP_HOOK
394
394
  };
package/dist/sdk/index.js CHANGED
@@ -1508,12 +1508,12 @@ function createHooksClient(options = {}) {
1508
1508
  return new HooksClient(authority, { fetch: options.fetch });
1509
1509
  }
1510
1510
  export {
1511
- HooksClient,
1512
- HooksRegistryApiClient,
1513
- ApiError as HooksRegistryApiError,
1514
- createHooksClient,
1515
- createHooksRegistryApiClient,
1516
- hooksRegistryApiClientFor,
1511
+ resolveHooksSdkAuthority,
1517
1512
  hooksRegistryOrigin,
1518
- resolveHooksSdkAuthority
1513
+ hooksRegistryApiClientFor,
1514
+ createHooksRegistryApiClient,
1515
+ createHooksClient,
1516
+ ApiError as HooksRegistryApiError,
1517
+ HooksRegistryApiClient,
1518
+ HooksClient
1519
1519
  };
package/dist/storage.js CHANGED
@@ -166,7 +166,7 @@ function up(db) {
166
166
  db.exec(idx);
167
167
  }
168
168
  }
169
- var init__001_initial = __esm(() => {
169
+ var init_001_initial = __esm(() => {
170
170
  init_schema();
171
171
  });
172
172
 
@@ -195,7 +195,7 @@ function up2(db) {
195
195
  throw err;
196
196
  }
197
197
  }
198
- var init__002_session_events = __esm(() => {
198
+ var init_002_session_events = __esm(() => {
199
199
  init_schema();
200
200
  });
201
201
 
@@ -224,7 +224,7 @@ function up3(db) {
224
224
  throw err;
225
225
  }
226
226
  }
227
- var init__003_user_prompt_submit_event = __esm(() => {
227
+ var init_003_user_prompt_submit_event = __esm(() => {
228
228
  init_schema();
229
229
  });
230
230
 
@@ -248,7 +248,7 @@ var CREATE_HOOKS_TABLE = `
248
248
  last_verified_at TEXT
249
249
  )
250
250
  `, CREATE_HOOKS_INDEXES;
251
- var init__004_hooks_table = __esm(() => {
251
+ var init_004_hooks_table = __esm(() => {
252
252
  CREATE_HOOKS_INDEXES = [
253
253
  `CREATE INDEX IF NOT EXISTS idx_hooks_name ON hooks (name)`
254
254
  ];
@@ -279,7 +279,7 @@ function up5(db) {
279
279
  throw err;
280
280
  }
281
281
  }
282
- var init__005_subagent_start_event = __esm(() => {
282
+ var init_005_subagent_start_event = __esm(() => {
283
283
  init_schema();
284
284
  });
285
285
 
@@ -311,11 +311,11 @@ function runMigrations(db) {
311
311
  }
312
312
  var MIGRATIONS;
313
313
  var init_migrations = __esm(() => {
314
- init__001_initial();
315
- init__002_session_events();
316
- init__003_user_prompt_submit_event();
317
- init__004_hooks_table();
318
- init__005_subagent_start_event();
314
+ init_001_initial();
315
+ init_002_session_events();
316
+ init_003_user_prompt_submit_event();
317
+ init_004_hooks_table();
318
+ init_005_subagent_start_event();
319
319
  MIGRATIONS = [
320
320
  { version: "001_initial", up },
321
321
  { version: "002_session_events", up: up2 },
@@ -1047,29 +1047,29 @@ function coerceForSqlite(value) {
1047
1047
  return String(value);
1048
1048
  }
1049
1049
  export {
1050
- HOOKS_STORAGE_BACKEND_ENV,
1051
- HOOKS_STORAGE_BACKEND_FALLBACK_ENV,
1052
- HOOKS_STORAGE_ENV,
1053
- HOOKS_STORAGE_FALLBACK_ENV,
1054
- HOOKS_STORAGE_TABLES,
1055
- PG_MIGRATIONS,
1056
- PgAdapterAsync,
1057
- RETIRED_STORAGE_MODE_ENV,
1058
- STORAGE_BACKENDS,
1059
- STORAGE_BACKEND_ENV,
1060
- STORAGE_DATABASE_ENV,
1061
- STORAGE_TABLES,
1062
- getStorageBackend,
1063
- getStorageDatabaseEnv,
1064
- getStorageDatabaseEnvName,
1065
- getStorageDatabaseUrl,
1066
- getStoragePg,
1067
- getStorageStatus,
1068
- getSyncMetaAll,
1069
- parseStorageTables,
1070
- resolveTables,
1071
- runStorageMigrations,
1072
- storagePull,
1050
+ storageSync,
1073
1051
  storagePush,
1074
- storageSync
1052
+ storagePull,
1053
+ runStorageMigrations,
1054
+ resolveTables,
1055
+ parseStorageTables,
1056
+ getSyncMetaAll,
1057
+ getStorageStatus,
1058
+ getStoragePg,
1059
+ getStorageDatabaseUrl,
1060
+ getStorageDatabaseEnvName,
1061
+ getStorageDatabaseEnv,
1062
+ getStorageBackend,
1063
+ STORAGE_TABLES,
1064
+ STORAGE_DATABASE_ENV,
1065
+ STORAGE_BACKEND_ENV,
1066
+ STORAGE_BACKENDS,
1067
+ RETIRED_STORAGE_MODE_ENV,
1068
+ PgAdapterAsync,
1069
+ PG_MIGRATIONS,
1070
+ HOOKS_STORAGE_TABLES,
1071
+ HOOKS_STORAGE_FALLBACK_ENV,
1072
+ HOOKS_STORAGE_ENV,
1073
+ HOOKS_STORAGE_BACKEND_FALLBACK_ENV,
1074
+ HOOKS_STORAGE_BACKEND_ENV
1075
1075
  };
@@ -388,12 +388,12 @@ function createSumiStopReviewPlugin(options, dependencies = {}) {
388
388
  };
389
389
  }
390
390
  export {
391
- createSumiStopReviewPlugin,
392
- isParentStopReviewResult,
393
- isSumiStopReviewEnvelope,
394
- isSumiStopReviewResult,
395
- parentStopReviewPins,
396
- requestParentStopReview,
391
+ verifySumiStopReviewCommand,
397
392
  runSumiStopReviewCommand,
398
- verifySumiStopReviewCommand
393
+ requestParentStopReview,
394
+ parentStopReviewPins,
395
+ isSumiStopReviewResult,
396
+ isSumiStopReviewEnvelope,
397
+ isParentStopReviewResult,
398
+ createSumiStopReviewPlugin
399
399
  };
@@ -78,6 +78,8 @@ export type ProgramKind = "c-interp" | "awk" | "shell";
78
78
  export interface EmbeddedProgram {
79
79
  text: string;
80
80
  kind: ProgramKind;
81
+ /** The language of a `c-interp` program that has a reader of its own. */
82
+ language?: "python";
81
83
  }
82
84
 
83
85
  export interface ExecutorFinding {
@@ -152,23 +154,105 @@ const SSH_LOCAL_EXEC_OPTS = new Set(["proxycommand", "localcommand", "knownhosts
152
154
  const C_INTERP_DANGER = /(?:`|\$\()|\b(?:system|exec|execve|execvp|popen|spawn|posix_spawn|fork|subprocess|Popen|check_output|check_call|shell_exec|passthru|proc_open|pcntl_exec|qx|open|fopen|sysopen|unlink|remove|delete|rmtree|rmdir|mkdir|rename|truncate|ftruncate|chmod|chown|symlink|writeFile|createWriteStream|appendFile|child_process|FileUtils|shutil|Kernel|require|import|load|eval|os|sys|io|fs|File|IO|Dir|Path|pathlib|Deno|Bun)|%x\W/;
153
155
 
154
156
  /**
155
- * awk danger: `system()` or a pipe/redirect (`| "cmd"`, `print > "file"`), a
156
- * `getline` from a command or file, `close`/`fflush` on a stream, or a delete
157
- * verb. `>`/`|` are awk redirection/pipe operators, so a program using them is
158
- * not provably read-only (a bare comparison `$1 > 5` reads only, but is refused
159
- * here under a protected root — fail closed, in class).
157
+ * awk danger: `system`, `getline`, `close` and `fflush` in any use, matched
158
+ * anywhere, since awk reads `1system("x")` as a call; and
159
+ * a gawk `@` before anything but `@` or `/`: an indirect call (`@f()`, `@ f()`)
160
+ * can reach `system` by a name built from strings, and `@load` or `@include`
161
+ * brings in other code. `/^@@/` stays text.
160
162
  */
161
- const AWK_DANGER = /[|>]|\b(?:system|getline|close|fflush|print\w*\s*>>?)/;
163
+ const AWK_DANGER = /system|getline|close|fflush|@(?![@/])/;
164
+
165
+ /**
166
+ * Whether an awk program can only read. Output leaves it only through a `print`
167
+ * or `printf`, and a redirection follows its statement, so a `>` or `|` before
168
+ * the first print is an operator or a pattern. Past it, even a `>` in a string
169
+ * fails, because telling a string from a regexp or a comment takes a parser. A
170
+ * line continuation (awks differ on it) anywhere fails once a `>` or `|` is
171
+ * present.
172
+ */
173
+ function awkProgramIsReadOnly(program: string): boolean {
174
+ if (AWK_DANGER.test(program)) return false;
175
+ if (!/[|>]/.test(program)) return true;
176
+ if (/\\\r?\n/.test(program)) return false;
177
+ const print = program.search(/print/);
178
+ return print < 0 || !/[|>]/.test(program.slice(print));
179
+ }
180
+
181
+ /**
182
+ * A `.` followed by anything but reading in `json` and `sys`, or a method of a
183
+ * string, a container or a file. `write` fails on any file `open` may produce
184
+ * here. Nothing listed reaches a module, a code object or a buffer; what isn't
185
+ * listed is refused, which is why this can't go stale the way a list of dangers
186
+ * does. A `case` class pattern reads attributes with no `.`, so PYTHON_UNSAFE
187
+ * refuses `case`, and a string can't hide it (see pythonCode).
188
+ */
189
+ const PYTHON_UNLISTED_ATTRIBUTE = /\.\s*(?!(?:argv|std(?:in|out|err)|exit|write|loads?|dumps?|strip|split(?:lines)?|join|replace|(?:start|end)swith|lower|find|count|get|items|keys|values|append|sort|read)\b)[a-zA-Z_]/;
190
+
191
+ /**
192
+ * Only a builtin gives a bare name power: the only modules let in are `json`
193
+ * and `sys`, and their attributes are reached through a `.`, which
194
+ * PYTHON_UNLISTED_ATTRIBUTE checks. Refused here: the builtins that run code or
195
+ * return live objects (any name ending in `attr` among them), `case`, whose
196
+ * class pattern reads attributes with no `.`, and every name that starts with
197
+ * `_`, a lone `_` included. A name is refused even when assigned to, since
198
+ * `exec=exec` keeps the function.
199
+ */
200
+ const PYTHON_UNSAFE = /(?<![\w.])(?:breakpoint|case|compile|\w*attr|eval|exec|globals|help|locals|type|vars)\b|(?<!\w)_/;
201
+
202
+ /**
203
+ * A python program as python reads it, with comments gone and every string out
204
+ * of the way. A string made only of `r`, `b` and `t` may be a read mode, so it
205
+ * stays visible as `~`; every other string becomes `%`. Three quotes always open
206
+ * a triple-quoted string, as in python: `''''` is never read as two empty
207
+ * strings, so one left open stays a bare quote. A word is a token of its own, so
208
+ * only up to two of python's prefix letters (`r`, `b`, `u`, `f`, `t`) can be a
209
+ * string prefix and the end of a name can't: `case'x'` reads as `case%` and
210
+ * `if'x'` as `if%`, as python reads them. An `f` or `t` string holds code, so
211
+ * its fields stay. If a field holds a brace (it may hide a nested string) or a
212
+ * `#` (python 3.12 reads a comment there, and a `}` in it doesn't close the
213
+ * field), the whole string comes back as a bare quote. A backslash outside a string is a token of its
214
+ * own, so that an escaped quote can't open a string and scan to the end of the
215
+ * program each time; it stays in the result, and a backslash there is refused.
216
+ */
217
+ function pythonCode(source: string): string {
218
+ return source.replace(/#.*|\\[^]|([rRbBuUfFtT]{0,2})("""|'''|(["'])(?!\3\3))((?:\\[^]|(?!\2)[^\\])*)\2|\w+/g, (whole, prefix: string, fence, _, text: string) => {
219
+ if (!fence) return whole[0] === "#" ? "" : whole;
220
+ if (!/[ft]/i.test(prefix)) return /^[rbt]+$/.test(text) ? "~" : "%";
221
+ let fields = "";
222
+ const rest = text.replace(/\{\{|\}\}|\{([^{}#]*)\}/g, (_, field = "") => ((fields += `${pythonCode(field)},`), ""));
223
+ return /[{}]/.test(rest) ? "'" : `(${fields})`;
224
+ });
225
+ }
226
+
227
+ /**
228
+ * Whether an inline python program is provably read-only: a backtick or `$(`
229
+ * is refused, as is what can't be read as code (a leftover quote or backslash,
230
+ * or any non-ASCII character), nothing unsafe may be named, a statement may import only
231
+ * `json` and `sys`, `open` must read one plain path, and nothing unlisted may be
232
+ * reached through a `.`. A program over 8,192 characters is refused unread:
233
+ * past about 16,000 the scanner's regex slows sharply on an unclosed string.
234
+ */
235
+ function pythonProgramIsReadOnly(program: string): boolean {
236
+ if (program.length > 8192 || /`|\$\(/.test(program)) return false;
237
+ const code = pythonCode(program);
238
+ if (/['"\\]|[^\0-\x7f]/.test(code) || PYTHON_UNSAFE.test(code)) return false;
239
+ if (/\b(?:import|from)\b/.test(code.replace(/\bimport\s+(?:json|sys)(?:\s*,\s*(?:json|sys))*(?=\s*(?:;|$))/gm, ""))) return false;
240
+ if (/\bopen\b/.test(code.replace(/\bopen\([\w.[\]%]+(?:,\s*(?:mode=)?~)?(?:,\s*encoding=%)?\)/g, ""))) return false;
241
+ return !PYTHON_UNLISTED_ATTRIBUTE.test(code);
242
+ }
162
243
 
163
244
  /**
164
245
  * Whether an embedded program is provably read-only for its grammar. A `shell`
165
246
  * value is judged by the caller with its own command classifier (it is a shell
166
- * command); only `c-interp` and `awk` are judged here.
247
+ * command); only `c-interp` and `awk` are judged here. After the delete-verb
248
+ * check, Python 3 is judged by its allow-list in place of the danger words,
249
+ * since a program naming no danger word can still reach a debugger or a dunder
250
+ * chain.
167
251
  */
168
252
  export function embeddedProgramIsReadOnly(program: EmbeddedProgram): boolean {
169
253
  if (mentionsDeleteVerb(program.text)) return false;
170
- if (program.kind === "awk") return !AWK_DANGER.test(program.text);
171
- if (program.kind === "c-interp") return !C_INTERP_DANGER.test(program.text);
254
+ if (program.kind === "awk") return awkProgramIsReadOnly(program.text);
255
+ if (program.kind === "c-interp") return program.language === "python" ? pythonProgramIsReadOnly(program.text) : !C_INTERP_DANGER.test(program.text);
172
256
  // shell: never decided here.
173
257
  return false;
174
258
  }
@@ -177,11 +261,12 @@ export function embeddedProgramIsReadOnly(program: EmbeddedProgram): boolean {
177
261
  function interpPrograms(name: string, words: string[], index: number, opaque: (at: number) => boolean): ExecutorFinding | null {
178
262
  const spec = INTERP_PROGRAM_FLAGS[name] ?? (VERSIONED_INTERP.test(name) ? INTERP_PROGRAM_FLAGS[name.replace(/[0-9.]+$/, "")] : undefined);
179
263
  if (!spec) return null;
264
+ const inline = (text: string): EmbeddedProgram => (name.startsWith("python3") ? { text, kind: "c-interp", language: "python" } : { text, kind: "c-interp" });
180
265
  const programs: EmbeddedProgram[] = [];
181
266
  let unresolved = false;
182
267
  const takeNext = (at: number): number => {
183
268
  if (at + 1 >= words.length || opaque(at + 1)) { unresolved = true; return at; }
184
- programs.push({ text: words[at + 1], kind: "c-interp" });
269
+ programs.push(inline(words[at + 1]));
185
270
  return at + 1;
186
271
  };
187
272
  for (let at = index + 1; at < words.length; at++) {
@@ -193,7 +278,7 @@ function interpPrograms(name: string, words: string[], index: number, opaque: (a
193
278
  const eq = word.indexOf("=");
194
279
  const longName = eq < 0 ? word : word.slice(0, eq);
195
280
  if (spec.long.has(longName)) {
196
- if (eq >= 0) programs.push({ text: word.slice(eq + 1), kind: "c-interp" });
281
+ if (eq >= 0) programs.push(inline(word.slice(eq + 1)));
197
282
  else at = takeNext(at);
198
283
  }
199
284
  continue;
@@ -203,7 +288,7 @@ function interpPrograms(name: string, words: string[], index: number, opaque: (a
203
288
  const cluster = word.slice(1);
204
289
  for (let i = 0; i < cluster.length; i++) {
205
290
  if (!spec.letters.has(cluster[i])) continue;
206
- if (i < cluster.length - 1) programs.push({ text: cluster.slice(i + 1), kind: "c-interp" });
291
+ if (i < cluster.length - 1) programs.push(inline(cluster.slice(i + 1)));
207
292
  else at = takeNext(at);
208
293
  break;
209
294
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/hooks",
3
- "version": "0.13.0",
3
+ "version": "0.13.2",
4
4
  "description": "Open source hooks library for AI coding agents - Install safety, quality, and automation hooks with a single command",
5
5
  "type": "module",
6
6
  "bin": {