Skip to content

Conversation

@Pahari47
Copy link
Contributor

Description

closes #13
Implements a message compression system with manual /compact command and automatic context management via auto-compact. Reduces token usage by compressing conversation history while preserving essential information, enabling longer conversations within model context limits.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Features implemented

Manual compression command (/compact)

  1. Compress message history on demand with compression statistics
  2. Multiple modes: --aggressive, --conservative, --default, --preview
  3. Session overrides: --auto-on, --auto-off, --threshold <n>
  4. In memory backup/restore
  5. Preserves recent messages at full detail
  6. Tool result compression: keeps facts, drops noise (logs, verbose output)
  7. Never re-runs tools from compressed context (tool results are history, not instructions)

Auto-compact feature

  1. Automatic compression when context usage reaches configurable threshold
  2. Configurable via /compact --threshold <n> and agents.config:
{
    "nanocoder": {
      "autoCompact": {
        "enabled": true,
        "threshold": 60,
        "mode": "conservative",
        "notifyUser": true
      }
    }
  }
  1. Default: enabled at 60% threshold with conservative mode
  2. Runs asynchronously without blocking conversation
  3. Shows notifications when auto-compact triggers

Status command enhancement

  1. Displays current context usage (tokens/percentage)
  2. Shows auto-compact settings (enabled, threshold, mode)
  3. Indicates session overrides when active
  4. /compact
Screenshot 2026-01-15 171413
  1. /status
Screenshot 2026-01-15 171438
  1. /compact --preview
Screenshot 2026-01-15 171350

Compression strategy

  1. Smart tool result compression: extracts key facts, error details (type, file, line), success/failure status
  2. Message summarization for verbose content
  3. Preserves conversation state separately (ConversationStateManager untouched)
  4. Maintains conversation continuity and context flow

Testing

Automated Tests

  • All existing tests pass (pnpm test:all completes successfully)
  • New features include passing tests in .spec.ts/tsx files
    Note: Test coverage for compression utilities can be added
  • Code follows project style guidelines (Biome formatting applied)

Manual Testing

  • Tested manual /compact command with different modes
  • Tested auto-compact triggering at threshold
  • Tested session overrides (--auto-on, --auto-off, --threshold)
  • Tested /status command showing context usage
  • Verified tool result compression preserves essential information
  • Verified conversation state is preserved separately from messages

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Documentation updated (if needed)
  • No breaking changes (or clearly documented)
  • Appropriate logging added using structured logging (see CONTRIBUTING.md)

@Pahari47 Pahari47 marked this pull request as draft January 15, 2026 13:29
function loadAutoCompactConfig(): AutoCompactConfig {
const defaults: AutoCompactConfig = {
enabled: true,
threshold: 60,
Copy link
Member

Choose a reason for hiding this comment

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

@Pahari47 I see that the "conservative" threshold value is set at 60%. Where have you defined the default threshold values for "aggressive" & the other modes ? Also what's the value for "default" mode ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hii @Avtrkrb, All modes use the same threshold (60%) to trigger, they differ in how much compression is applied.

Summary comparison
Aspect Conservative Default Aggressive
Tool output First 3 lines Key info/summary Success/error only
User messages No change Summarize (>500 chars) Heavily summarize (>500 chars)
Assistant messages No change Summarize (>500 chars) Heavily summarize (>500 chars)
Token savings ~20-30% ~40-50% ~60-70%
Context preserved High Medium Low

Copy link
Member

Choose a reason for hiding this comment

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

Alright that makes sense. This might be a dumb questions, but in issue #13 there was a discussion about allowing the users to set the threshold value in agents.config.json, but I don't think I saw any code which was reading it ? Correct me if I'm wrong. I did see some logic for setting the threshold value at a session level, which is good though.. Could you clarify that please ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does read the agent config. In config/index.ts const projectConfigPath = join(process.cwd(), 'agents.config.json');

also check this one loadAppConfig()

Copy link
Member

Choose a reason for hiding this comment

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

What about the global agents.config.json ? I'm assuming it reads that as well ?

@Avtrkrb
Copy link
Member

Avtrkrb commented Jan 15, 2026

Also @Pahari47 you seem to have quite a few checks failing, please resolve them when you get the time.

@Pahari47
Copy link
Contributor Author

Also @Pahari47 you seem to have quite a few checks failing, please resolve them when you get the time.

actually There is problem in source/test-utils/cli-test-harness.ts this getCurrentStdout() function defined two time with same name, thats why everything failing, @Avtrkrb, can you check which one should keep.

@Avtrkrb
Copy link
Member

Avtrkrb commented Jan 15, 2026

Also @Pahari47 you seem to have quite a few checks failing, please resolve them when you get the time.

actually There is problem in source/test-utils/cli-test-harness.ts this getCurrentStdout() function defined two time with same name, thats why everything failing, @Avtrkrb, can you check which one should keep.

Sure let me take a look. @will-lamerton FYI

@Avtrkrb
Copy link
Member

Avtrkrb commented Jan 15, 2026

Also @Pahari47 you seem to have quite a few checks failing, please resolve them when you get the time.

actually There is problem in source/test-utils/cli-test-harness.ts this getCurrentStdout() function defined two time with same name, thats why everything failing, @Avtrkrb, can you check which one should keep.

@Pahari47
Removing the getCurrentStdout() defined in lines 294 to 298 should do it I guess: https://github.com/Pahari47/nanocoder/blob/0799be5fe99f3eac838048b71425ef6cec58352e/source/test-utils/cli-test-harness.ts#L294

Since the definition below it is much neater.. I feel the definition in lines 294 to 298 can be removed.

@will-lamerton
Copy link
Member

Hey @Pahari47, you might want to merge in upstream changes as these test cases were fixed and I can see you branch is behind them :)

@Pahari47
Copy link
Contributor Author

Hey @Pahari47, you might want to merge in upstream changes as these test cases were fixed and I can see you branch is behind them :)

I will fix it then Open the PR

@Pahari47 Pahari47 marked this pull request as ready for review January 16, 2026 15:39
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.

Feature Request: Add "/compact" Command for Context Compression

3 participants