Skip to content

Commit 14969df

Browse files
igorcostaAutohand Evolve
andcommitted
Gate turn memory diagnostics behind debug mode
Route turn-memory reflection success and failure diagnostics through an explicit AUTOHAND_DEBUG check before writing to the live debug line surface. Co-authored-by: Autohand Evolve <code-noreply@autohand.ai>
1 parent ed78493 commit 14969df

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

src/core/agent.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { LLMProvider } from '../providers/LLMProvider.js';
1212
import { safeEmitKeypressEvents } from '../ui/inputPrompt.js';
1313

1414
import { safeSetRawMode } from '../ui/rawMode.js';
15-
import { writeAutohandDebugLine } from '../utils/debugLog.js';
15+
import { isAutohandDebugEnabled } from '../utils/debugLog.js';
1616
import type { UIManager } from '../ui/UIManager.js';
1717
import { GitIgnoreParser } from '../utils/gitIgnore.js';
1818
import { ConversationManager } from './conversationManager.js';
@@ -580,10 +580,7 @@ export class AutohandAgent {
580580
this.turnMemoryReflectionInFlight = this.runQueuedTurnMemoryReflection()
581581
.catch((error: unknown) => {
582582
const message = error instanceof Error ? error.message : String(error);
583-
writeAutohandDebugLine(
584-
`[memory] turn reflection failed: ${message}`,
585-
this.writeDebugLine.bind(this),
586-
);
583+
this.writeTurnMemoryDebugLine(`[memory] turn reflection failed: ${message}`);
587584
})
588585
.finally(() => {
589586
this.turnMemoryReflectionInFlight = null;
@@ -625,12 +622,19 @@ export class AutohandAgent {
625622
}
626623

627624
this.conversation.addSystemNote(formatTurnMemoryUpdate(saved), '[Auto Memory Update]');
628-
writeAutohandDebugLine(
625+
this.writeTurnMemoryDebugLine(
629626
`[memory] turn reflection saved ${saved.length} ${saved.length === 1 ? 'memory' : 'memories'}`,
630-
this.writeDebugLine.bind(this),
631627
);
632628
}
633629

630+
private writeTurnMemoryDebugLine(message: string): void {
631+
if (!isAutohandDebugEnabled()) {
632+
return;
633+
}
634+
635+
this.writeDebugLine(message);
636+
}
637+
634638
private async flushTurnMemoryReflection(timeoutMs = 1500): Promise<void> {
635639
if (!this.turnMemoryReflectionInFlight) {
636640
return;

tests/core/agent/TurnMemoryReflection.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
* Copyright 2025 Autohand AI LLC
44
* SPDX-License-Identifier: Apache-2.0
55
*/
6-
import { describe, expect, it, vi } from 'vitest';
6+
import { afterEach, describe, expect, it, vi } from 'vitest';
77
import { AutohandAgent } from '../../../src/core/agent.js';
88

9+
const originalDebug = process.env.AUTOHAND_DEBUG;
10+
11+
afterEach(() => {
12+
if (originalDebug === undefined) {
13+
delete process.env.AUTOHAND_DEBUG;
14+
} else {
15+
process.env.AUTOHAND_DEBUG = originalDebug;
16+
}
17+
});
18+
919
function createAgentHarness() {
1020
const agent = Object.create(AutohandAgent.prototype) as any;
1121
const memoryManager = {
@@ -75,13 +85,35 @@ describe('turn memory reflection', () => {
7585

7686
it('does not write a success notice into the live terminal after background reflection', async () => {
7787
const { agent } = createAgentHarness();
88+
delete process.env.AUTOHAND_DEBUG;
89+
90+
agent.scheduleTurnMemoryReflection(true);
91+
await agent.turnMemoryReflectionInFlight;
92+
93+
expect(agent.writeDebugLine).not.toHaveBeenCalled();
94+
});
95+
96+
it('does not write a failure notice into the live terminal unless debug logging is enabled', async () => {
97+
const { agent, llm } = createAgentHarness();
98+
llm.complete.mockRejectedValueOnce(new Error('memory unavailable'));
99+
delete process.env.AUTOHAND_DEBUG;
78100

79101
agent.scheduleTurnMemoryReflection(true);
80102
await agent.turnMemoryReflectionInFlight;
81103

82104
expect(agent.writeDebugLine).not.toHaveBeenCalled();
83105
});
84106

107+
it('writes turn memory diagnostics when AUTOHAND_DEBUG is enabled', async () => {
108+
const { agent } = createAgentHarness();
109+
process.env.AUTOHAND_DEBUG = '1';
110+
111+
agent.scheduleTurnMemoryReflection(true);
112+
await agent.turnMemoryReflectionInFlight;
113+
114+
expect(agent.writeDebugLine).toHaveBeenCalledWith('[memory] turn reflection saved 1 memory');
115+
});
116+
85117
it('does not run when auto-memory is disabled', () => {
86118
const { agent, llm } = createAgentHarness();
87119
agent.runtime.config.agent.autoMemory = false;

0 commit comments

Comments
 (0)