Skip to content

fix(showcase): call ctx.log.info/.warn in hook bodies — sandbox ctx.log is an object#3323

Merged
os-zhuang merged 1 commit into
mainfrom
claude/wizardly-golick-11e2fa
Jul 19, 2026
Merged

fix(showcase): call ctx.log.info/.warn in hook bodies — sandbox ctx.log is an object#3323
os-zhuang merged 1 commit into
mainfrom
claude/wizardly-golick-11e2fa

Conversation

@os-zhuang

Copy link
Copy Markdown
Contributor

背景

examples/app-showcase 的两个 hook body 用 if (typeof ctx.log === 'function') ctx.log(...) 守卫日志行,但沙箱 hook 运行时把 ctx.log 装成对象 { info, warn, error }(见 packages/runtime/src/sandbox/quickjs-runner.tsinstallCtxscript-runner.tsScriptContext.log),所以 typeof ctx.log === 'function' 恒为 false,这两行审计/告警日志是死代码,从不触发。

改动

改用正确的对象方法 API:

Hook 改后
AuditTaskCompletionHook(审计) ctx.log.info(...)
WarnOverBudgetHook(告警) ctx.log.warn(...)

两个 hook 都已声明 capabilities: ['log'],无需改能力。body 保持极简(去掉死守卫后更短,便于当文档阅读)。

说明:告警 hook 用了语义匹配的 .warn(hook 名 WarnOverBudget、描述 "Emits a warning"),它与 .info 同属正确 API,还顺带在 showcase 里演示了第二个日志级别,契合本文件"exercise the full hook designer surface / read as documentation"的定位。

ctx.log 的发射本身是 best-effort(转发到 ctx.log?.[level]?.(),hook 路径下 host logger 可能缺省 → 静默 no-op),所以本改动修的是"调对 API",不改变可观测性;未把它们改造成写数据的 hook(超出范围)。纯 bug 修复,无需 changeset(AGENTS.md §263)。

验证

  • os validate 通过(showcase 全量,仅有与本改动无关的预存权限/master-detail 警告)。
  • showcase vitest run:9 文件 / 55 测试全绿。
  • 契约核对:quickjs-runner.ts installCtx 把 ctx.log 装为对象、每 level 转发 ctx.log?.[level]?.(...);ScriptContext.log 类型为 { info, warn, error }

🤖 Generated with Claude Code

…og is an object

AuditTaskCompletion and WarnOverBudget guarded their log line with
`if (typeof ctx.log === 'function')`, but the sandboxed hook runtime installs
`ctx.log` as an object `{ info, warn, error }` (quickjs-runner installCtx /
ScriptContext.log), so the guard was always false and the lines were dead code.
Call the correct object-method API: `ctx.log.info(...)` for the audit hook and
`ctx.log.warn(...)` for the over-budget warning. Both already declare
capabilities: ['log'], so no capability change is needed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@vercel

vercel Bot commented Jul 19, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
spec Ready Ready Preview, Comment Jul 19, 2026 7:06pm

Request Review

@os-zhuang
os-zhuang merged commit 3e6622b into main Jul 19, 2026
13 of 15 checks passed
@os-zhuang
os-zhuang deleted the claude/wizardly-golick-11e2fa branch July 19, 2026 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant