8000 refactor: apply scoped logger context to interim PRs by amikofalvy · Pull Request #3078 · inkeep/agents · GitHub
[go: up one dir, main page]

Skip to content

refactor: apply scoped logger context to interim PRs - #3078

Closed
amikofalvy wants to merge 2 commits into
mainfrom
fix/logger-interim-migration
Closed

amikofalvy wants to merge 2 commits into
mainfrom
fix/logger-interim-migration

Conversation

@amikofalvy
Copy link
Copy Markdown
Collaborator

Summary

Changes

  • tool-approval.ts: Wraps waitForToolApproval and parseAndCheckApproval in runWithLogContext({ toolCallId, toolName }), strips repeated toolName/toolCallId from 8 logger calls
  • tool-wrapper.ts: Wraps the execute closure in runWithLogContext({ toolName, toolCallId }), strips repeated fields from 3 logger calls
  • AGENTS.md: New "Logger: Scoped Context Pattern" section documenting getLogger, runWithLogContext, and .with() patterns
  • api-logging-guidelines skill: Updated to teach the ALS scoped context pattern as the primary approach

Test plan

  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm format passes
  • All 153 test files pass (2244 tests), including pending-approvals.test.ts
  • Pre-commit hooks pass (lint-staged + test runner + artifact validation)

🤖 Generated with Claude Code

These files were modified by PRs #3062 and #3064 which merged after the
logger migration in #3067 but were not updated to use the scoped context
pattern. Wraps tool-approval.ts and tool-wrapper.ts execute paths in
runWithLogContext({ toolCallId, toolName }), strips repeated ambient
fields from individual logger calls, and adds logger scoped context
guidance to AGENTS.md and the api-logging-guidelines skill.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@changeset-bot
changeset-bot Bot commented Apr 8, 2026 •
Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 57130f6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@inkeep/agents-api Patch
@inkeep/agents-manage-ui Patch
@inkeep/agents-cli Patch
@inkeep/agents-core Patch
@inkeep/agents-email Patch
@inkeep/agents-mcp Patch
@inkeep/agents-sdk Patch
@inkeep/agents-work-apps Patch
@inkeep/ai-sdk-provider Patch
@inkeep/create-agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
vercel Bot commented Apr 8, 2026 •
Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
agents-api Ready Ready Preview, Comment Apr 8, 2026 7:28pm
agents-docs Ready Ready Preview, Comment Apr 8, 2026 7:28pm
agents-manage-ui Ready Ready Preview, Comment Apr 8, 2026 7:28pm

Request Review

@pullfrog
pullfrog Bot commented Apr 8, 2026 •
Copy link
Copy Markdown
Contributor

TL;DR — Migrates the tool-execution layer (tool-approval.ts and tool-wrapper.ts) to use runWithLogContext() for ALS-backed scoped logger context, eliminating repetitive manual inclusion of toolName/toolCallId in every log call. Updates AGENTS.md and the api-logging-guidelines skill to codify this as the standard pattern.

Key changes

  • Wrap tool execution in runWithLogContext — both tool-wrapper.ts and tool-approval.ts now set toolName and toolCallId once at function entry, automatically propagating to all nested log calls via AsyncLocalStorage.
  • Strip ambient fields from individual log calls — toolName, toolCallId, and parentToolName removed from explicit logger bindings across ~15 call sites since ALS now provides them.
  • Split waitForToolApproval into outer wrapper + inner function — thin outer function sets logger context, delegates to waitForToolApprovalInner for the actual approval logic.
  • Add "Logger: Scoped Context Pattern" section to AGENTS.md — documents the three core APIs (getLogger, runWithLogContext, logger.with), usage patterns, anti-patterns, and a decision table.
  • Rewrite api-logging-guidelines skill Core Principle Update README.md #1 — replaces "Avoid Redundant Logging" with "Use Scoped Logger Context (ALS)" including migration steps and before/after examples.
  • Add changeset for @inkeep/agents-api — patch-level changeset documenting the scoped context migration.

Summary | 5 files | 2 commits | base: main ← fix/logger-interim-migration


Scoped logger context in tool execution

Before: every logger.info/warn/error call in tool-wrapper.ts and tool-approval.ts manually passed { toolName, toolCallId } (and sometimes parentToolName) as bindings — roughly 15 call sites with duplicated context fields.
After: a single runWithLogContext({ toolName, toolCallId }, ...) at each function's entry point injects those fields into ALS, and individual log calls only pass per-call data (e.g. { error }, { reason }) or use the string-only overload.

In tool-wrapper.ts, the entire execute body is wrapped in one runWithLogContext call — the bulk of the diff is re-indentation, not logic changes. In tool-approval.ts, waitForToolApproval is split into a thin context-setting wrapper and a waitForToolApprovalInner implementation function, while parseAndCheckApproval wraps its body directly.

Why split waitForToolApproval into two functions?

The outer function exists solely to call runWithLogContext and forward all arguments — this keeps the ALS scope precisely aligned with the function's lifetime and avoids an extra indentation level in the ~80-line inner implementation.

tool-wrapper.ts · tool-approval.ts


Developer guidelines for scoped logger context

Before: no documented standard for how to propagate logger context in service functions — each module improvised its own approach.
After: AGENTS.md includes a dedicated section with three canonical patterns (route handlers, service functions, class members), a "What NOT to do" block, and a decision table. The api-logging-guidelines skill mirrors this guidance for AI coding agents.

The SKILL.md rewrite replaces the former "Avoid Redundant Logging" principle with "Use Scoped Logger Context (ALS)" and provides a concrete 4-step migration strategy: wrap in runWithLogContext, strip ambient fields, use string-only overloads where possible, use .with() for class members.

AGENTS.md · SKILL.md · back-white-warbler.md

Pullfrog  | View workflow run | Triggered by Pullfrog | Using Claude Opus | 𝕏

@pullfrog pullfrog Bot left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean migration — the ALS context changes are functionally correct and the documentation additions are well-structured. One numbering bug in the skill file and one architectural note about triple-nested scopes.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using Claude Opus | 𝕏

- ALS middleware provides tenant/project/agent IDs on every log line automatically
- Adding duplicate logs creates noise without value

### 2. Log Level Guidelines
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Section numbering is off: the new section 1 ("Use Scoped Logger Context") pushed everything down, but this header kept its old number. Sequence reads 1, 2, 2, 3 instead of 1, 2, 3, 4.

Suggested change
### 2. Log Level Guidelines
### 3. Log Level Guidelines

{ approved: false; deniedResult: unknown } | { approved: true } | { approved: 'pending' }
> {
logger.info({ toolName, toolCallId, args }, 'Tool requires approval - waiting for user response');
return runWithLogContext({ toolCallId, toolName }, () =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both parseAndCheckApproval (line 249) and this function wrap in runWithLogContext({ toolCallId, toolName }). Since both callers (function-tools.ts:98, mcp-tools.ts:99) already go through wrapToolWithStreaming which sets the same scope at tool-wrapper.ts:108, tool executions end up with three nested pino child loggers carrying identical bindings.

This is functionally harmless (pino child nesting is cheap and ALS composes correctly), and keeping the wrappers here is defensively correct — if these functions are ever called outside wrapToolWithStreaming, they still get context. Just flagging for awareness; no change needed unless you want to eliminate the redundancy.

@claude claude Bot left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review Summary

(1) Total Issues | Risk: Low

🟡 Minor (1) 🟡

Inline Comments:

  • 🟡 Minor: .agents/skills/api-logging-guidelines/SKILL.md:50 Duplicate section numbering (### 2. appears twice)

💡 APPROVE WITH SUGGESTIONS

Summary: This is a clean refactoring PR that correctly migrates tool-approval.ts and tool-wrapper.ts to the scoped logger context pattern (ALS). The runWithLogContext wrappers appropriately set toolName and toolCallId context, and all logger calls correctly strip these now-ambient fields. The AGENTS.md documentation addition is clear and comprehensive. The only issue is a minor section numbering typo in the skill file that can be addressed in a follow-up.

Reviewers (2)
Reviewer Returned Main Findings Consider While You're Here Inline Comments Pending Recs Discarded
pr-review-standards 0 0 0 0 0 0 0
pr-review-docs 1 0 0 0 1 0 0
Total 1 0 0 0 1 0 0

- ALS middleware provides tenant/project/agent IDs on every log line automatically
- Adding duplicate logs creates noise without value

### 2. Log Level Guidelines
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Minor: Duplicate section numbering

Issue: This section is numbered "### 2." but line 43 already has "### 2. Avoid Redundant Logging". The sections should be numbered sequentially (1, 2, 3, 4, 5) but currently read 1, 2, 2, 3, 4.

Why: Contributors following this skill will be confused by duplicate numbering. When referencing "principle 2" it's ambiguous which section is meant.

Fix: (1-click apply)

Suggested change
### 2. Log Level Guidelines
### 3. Log Level Guidelines

Then renumber the subsequent sections accordingly (3→4, 4→5).

@github-actions github-actions Bot deleted a comment from claude Bot Apr 8, 2026
@github-actions
github-actions Bot commented Apr 8, 2026 •
Copy link
Copy Markdown
Contributor

Preview URLs

Use these stable preview aliases for testing this PR:

These point to the same Vercel preview deployment as the bot comment, but they stay stable and easier to find.

Raw Vercel deployment URLs

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@claude claude Bot left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review Summary

(0) New Issues | Risk: Low

Delta Analysis

The only change since the last automated review is the addition of a changeset file (.changeset/back-white-warbler.md). The changeset is correctly formatted:

  • ✅ Targets @inkeep/agents-api (the package containing tool-approval.ts and tool-wrapper.ts)
  • ✅ Uses patch level (appropriate for internal refactoring with no API changes)
  • ✅ Message follows changelog guidelines: "Migrate tool-approval and tool-wrapper logger calls to scoped context pattern"

No new issues found in the delta.

🕐 Pending Recommendations (1)


💡 APPROVE WITH SUGGESTIONS

Summary: The delta (changeset addition) is correctly formatted. The PR correctly migrates tool-approval.ts and tool-wrapper.ts to the scoped logger context pattern. One minor section numbering issue from the prior review remains unaddressed — see Pending Recommendations above for the inline comment link.

Reviewers (0)
Reviewer Returned Main Findings Consider While You're Here Inline Comments Pending Recs Discarded
Total 0 0 0 0 0 1 0

Note: Delta-only review — no sub-reviewers dispatched for changeset-only changes.

@itoqa
itoqa Bot commented Apr 8, 2026
Copy link
Copy Markdown

Ito Test Report ✅

8 test cases ran. 8 passed.

Overall, this unified local non-production QA run passed with 8/8 executed test cases and 0 failures (plus one code-first check that produced no verifiable feature outcome), and both runtime validation and route/path code review found no code-supported production defect in scope. The most important findings were that baseline chat streaming remained functional (with simulated model execution for deterministic local validation), approval flows behaved correctly and safely (mixed per-toolCallId approve/deny mapping, idempotent handling of replayed/unknown/forged IDs, and single-resolution under rapid repeated actions), and the UI/security behaviors were stable across mobile usability, script-injection rendering as inert text, and cross-tab conversation isolation.

✅ Passed (8)
Category Summary Screenshot
Adversarial Rapid repeated approval actions resolve exactly once, and subsequent repeats are treated as already processed. ADV-1
Adversarial Injection payload rendered as inert text; script content did not execute in the browser context. ADV-2
Adversarial Two tabs used distinct conversation IDs and maintained conversation-scoped isolation. ADV-3
Adversarial Forged unknown toolCallId approval-response was handled idempotently and did not resolve a valid approval. ADV-4
Edge Mixed approve/deny batch responses are processed independently per toolCallId and return the expected per-item status. EDGE-1
Edge Replayed or unknown approval responses are acknowledged idempotently with alreadyProcessed=true and no duplicate execution. EDGE-2
Edge Mobile viewport (390x844) playground remained usable; controls and chat input stayed accessible. EDGE-4
Happy-path Playground chat streamed and rendered assistant output successfully in the final local run. ROUTE-1

Commit: 57130f6

View Full Run


Tell us how we did: Give Ito Feedback

@github-actions
Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity.
It will be closed in 7 days if no further activity occurs.

If this PR is still relevant:

  • Rebase it on the latest main branch
  • Add a comment explaining its current status
  • Request a review if it's ready

Thank you for your contributions!

@github-actions github-actions Bot added the stale label Apr 16, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This pull request has been automatically closed due to inactivity.

If you'd like to continue working on this, please:

  1. Create a new branch from the latest main
  2. Cherry-pick your commits or rebase your changes
  3. Open a new pull request

Thank you for your understanding!

@github-actions github-actions Bot closed this Apr 23, 2026
auto-merge was automatically disabled April 23, 2026 00:48

Pull request was closed

@github-actions
github-actions Bot deleted the fix/logger-interim-migration branch April 23, 2026 00:48

This branch was successfully deployed

3 active deployments
Preview – agents-api — 57130f67 Deployed Apr 8, 2026 by vercel[bot]
Preview – agents-docs — 57130f67 Deployed Apr 8, 2026 by vercel[bot]
Preview – agents-manage-ui — 57130f67 Deployed Apr 8, 2026 by vercel[bot]
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

0