Skip to content

Conversation

@ulivz
Copy link
Member

@ulivz ulivz commented Oct 10, 2025

Summary

Comprehensive refactoring of @tarko/agent-snapshot package to improve code quality, maintainability, and architectural consistency while preserving all public APIs.

Key Improvements

🔧 Code Quality & Architecture

  • Eliminated 620+ lines of duplicate code by extracting ToolCallTracker utility class
  • Removed custom diff implementation (200+ lines) in favor of existing snapshot-diff library
  • Enhanced type safety by replacing all any types with unknown
  • Simplified hook management with helper methods in AgentHookBase

📦 Bundle Optimization

  • ESM size: 61.1 kB → 56.5 kB (-7.5%)
  • CJS size: 85.2 kB → 82.7 kB (-2.9%)
  • Total code reduction: ~33% (1200+ → 800 lines)

🧹 Code Cleanup

  • Removed redundant "parrot" comments that only repeated method names
  • Streamlined CLI command handling in AgentSnapshotRunner
  • Improved error handling consistency across hooks
  • Better separation of concerns with utility classes

Technical Details

New Components

  • ToolCallTracker: Shared utility for managing tool call lifecycle across hooks
  • Improved hook management with storeOriginalHooks() and installNewHooks() helpers

Refactored Components

  • AgentSnapshotNormalizer: Uses existing snapshot-diff instead of custom implementation
  • AgentHookBase: Simplified hook lifecycle management
  • AgentSnapshotRunner: Cleaner CLI command processing

Type Safety Improvements

  • All any types replaced with unknown for better type safety
  • Stricter interface definitions without over-broad types
  • Enhanced WeakMap typing for circular reference detection

Testing

  • ✅ Build passes successfully
  • ✅ All existing APIs preserved (no breaking changes)
  • ✅ Bundle size optimized
  • ✅ Type checking improved

Checklist

  • Added or updated necessary tests (Optional).
  • Updated documentation to align with changes (Optional).
  • Verified no breaking changes, or prepared solutions for any occurring breaking changes (Optional).
  • My change does not involve the above items.

@netlify
Copy link

netlify bot commented Oct 10, 2025

Deploy Preview for agent-tars-docs ready!

Name Link
🔨 Latest commit d542b75
🔍 Latest deploy log https://app.netlify.com/projects/agent-tars-docs/deploys/68ecf53075b08b00087ec357
😎 Deploy Preview https://deploy-preview-1709--agent-tars-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link

netlify bot commented Oct 10, 2025

Deploy Preview for tarko ready!

Name Link
🔨 Latest commit d542b75
🔍 Latest deploy log https://app.netlify.com/projects/tarko/deploys/68ecf530d2f16f00087564b9
😎 Deploy Preview https://deploy-preview-1709--tarko.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@ulivz ulivz force-pushed the refactor/agent-snapshot-optimization branch 2 times, most recently from dc6c5de to acaf9e8 Compare October 11, 2025 06:26
ulivz added 5 commits October 13, 2025 19:57
- Remove complex prototype chain manipulation from AgentSnapshot constructor
- Simplify AgentSnapshot to focus on core snapshot generation and replay functionality
- Add generic verification method to reduce code duplication in SnapshotManager
- Consolidate repeated file path operations with helper methods
- Improve README with comprehensive documentation and examples
- Remove unnecessary type assertions and comments
- Maintain API compatibility while improving internal structure

Breaking Changes: None - all public APIs remain unchanged
…cation

- Remove custom diff implementation, use existing snapshot-diff library
- Extract ToolCallTracker utility to eliminate code duplication between hooks
- Improve type safety by replacing 'any' with 'unknown' types
- Simplify hook management with helper methods in AgentHookBase
- Remove redundant comments and improve code clarity
- Optimize CLI command handling in AgentSnapshotRunner
- Maintain all existing Public APIs without breaking changes
- Fix loop count mismatch in streaming mode by fully consuming streams
- Track agent_run_end events to ensure complete execution
- Add wait time for background processing and cleanup handlers
- Improve error handling and logging for streaming responses

Resolves issue where AgentSnapshot.replay() didn't wait for complete
agent execution in streaming mode
@ulivz ulivz force-pushed the refactor/agent-snapshot-optimization branch from 51e7657 to debf493 Compare October 13, 2025 12:42
- Use proper type casting for async iterator checks
- Add unknown type conversion to satisfy TypeScript compiler
- Improve type safety in stream consumption logic

Fixes build errors in agent-snapshot package
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants