Skip to content

refactor: deduplicate make_hook_env / make_tool_hook_env between agent_loop.rs and tier_loop.rs #4015

@bug-ops

Description

@bug-ops

Description

Two near-identical helper functions exist in different crates/modules for constructing the hook environment map:

  • crates/zeph-subagent/src/agent_loop.rs:32make_hook_env(task_id, agent_name, tool_name, tool_input)
  • crates/zeph-core/src/agent/tool_execution/tier_loop.rs:27make_tool_hook_env(tool_name, tool_input, session_id)

Both functions: serialize tool_input to JSON, apply the 64 KiB truncation with floor_char_boundary, and insert ZEPH_TOOL_NAME + ZEPH_TOOL_ARGS_JSON. The differences are:

  • agent_loop.rs version adds ZEPH_AGENT_ID and ZEPH_AGENT_NAME (subagent-specific)
  • tier_loop.rs version adds ZEPH_SESSION_ID (main-agent-specific)

The truncation logic and constant (TOOL_ARGS_JSON_LIMIT = 64 * 1024) are duplicated byte-for-byte.

Expected Behavior

A single shared function in zeph-subagent (where HookDef and fire_hooks already live) should build the base env, with optional extra fields added by callers.

Actual Behavior

Two independent implementations that must be kept in sync when the limit or truncation logic changes.

Environment

Logs / Evidence

crates/zeph-subagent/src/agent_loop.rs:30-59   — make_hook_env
crates/zeph-core/src/agent/tool_execution/tier_loop.rs:21-54  — make_tool_hook_env (const TOOL_ARGS_JSON_LIMIT also duplicated)

Metadata

Metadata

Assignees

Labels

P3Research — medium-high complexityrefactorCode refactoring without functional changes

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions