feat: Add raw output logging and endpoint for debugging

- Introduced a new environment variable `AUTOMAKER_DEBUG_RAW_OUTPUT` to enable raw output logging for agent streams.
- Added a new endpoint `/raw-output` to retrieve raw JSONL output for debugging purposes.
- Implemented functionality in `AutoModeService` to log raw output events and save them to `raw-output.jsonl`.
- Enhanced `FeatureLoader` to provide access to raw output files.
- Updated UI components to clean fragmented streaming text for better log parsing.
This commit is contained in:
Shirone
2025-12-28 02:34:10 +01:00
parent 52b1dc98b8
commit e404262cb0
7 changed files with 250 additions and 21 deletions

View File

@@ -10,7 +10,7 @@ import { createGetHandler } from './routes/get.js';
import { createCreateHandler } from './routes/create.js';
import { createUpdateHandler } from './routes/update.js';
import { createDeleteHandler } from './routes/delete.js';
import { createAgentOutputHandler } from './routes/agent-output.js';
import { createAgentOutputHandler, createRawOutputHandler } from './routes/agent-output.js';
import { createGenerateTitleHandler } from './routes/generate-title.js';
export function createFeaturesRoutes(featureLoader: FeatureLoader): Router {
@@ -22,6 +22,7 @@ export function createFeaturesRoutes(featureLoader: FeatureLoader): Router {
router.post('/update', validatePathParams('projectPath'), createUpdateHandler(featureLoader));
router.post('/delete', validatePathParams('projectPath'), createDeleteHandler(featureLoader));
router.post('/agent-output', createAgentOutputHandler(featureLoader));
router.post('/raw-output', createRawOutputHandler(featureLoader));
router.post('/generate-title', createGenerateTitleHandler());
return router;

View File

@@ -1,5 +1,6 @@
/**
* POST /agent-output endpoint - Get agent output for a feature
* POST /raw-output endpoint - Get raw JSONL output for debugging
*/
import type { Request, Response } from 'express';
@@ -30,3 +31,31 @@ export function createAgentOutputHandler(featureLoader: FeatureLoader) {
}
};
}
/**
* Handler for getting raw JSONL output for debugging
*/
export function createRawOutputHandler(featureLoader: FeatureLoader) {
return async (req: Request, res: Response): Promise<void> => {
try {
const { projectPath, featureId } = req.body as {
projectPath: string;
featureId: string;
};
if (!projectPath || !featureId) {
res.status(400).json({
success: false,
error: 'projectPath and featureId are required',
});
return;
}
const content = await featureLoader.getRawOutput(projectPath, featureId);
res.json({ success: true, content });
} catch (error) {
logError(error, 'Get raw output failed');
res.status(500).json({ success: false, error: getErrorMessage(error) });
}
};
}

View File

@@ -1917,11 +1917,49 @@ This mock response was generated because AUTOMAKER_MOCK_AGENT=true was set.
// Note: We use projectPath here, not workDir, because workDir might be a worktree path
const featureDirForOutput = getFeatureDir(projectPath, featureId);
const outputPath = path.join(featureDirForOutput, 'agent-output.md');
const rawOutputPath = path.join(featureDirForOutput, 'raw-output.jsonl');
// Raw output logging is configurable via environment variable
// Set AUTOMAKER_DEBUG_RAW_OUTPUT=true to enable raw stream event logging
const enableRawOutput =
process.env.AUTOMAKER_DEBUG_RAW_OUTPUT === 'true' ||
process.env.AUTOMAKER_DEBUG_RAW_OUTPUT === '1';
// Incremental file writing state
let writeTimeout: ReturnType<typeof setTimeout> | null = null;
const WRITE_DEBOUNCE_MS = 500; // Batch writes every 500ms
// Raw output accumulator for debugging (NDJSON format)
let rawOutputLines: string[] = [];
let rawWriteTimeout: ReturnType<typeof setTimeout> | null = null;
// Helper to append raw stream event for debugging (only when enabled)
const appendRawEvent = (event: unknown): void => {
if (!enableRawOutput) return;
try {
const timestamp = new Date().toISOString();
const rawLine = JSON.stringify({ timestamp, event }, null, 4); // Pretty print for readability
rawOutputLines.push(rawLine);
// Debounced write of raw output
if (rawWriteTimeout) {
clearTimeout(rawWriteTimeout);
}
rawWriteTimeout = setTimeout(async () => {
try {
await secureFs.mkdir(path.dirname(rawOutputPath), { recursive: true });
await secureFs.appendFile(rawOutputPath, rawOutputLines.join('\n') + '\n');
rawOutputLines = []; // Clear after writing
} catch (error) {
console.error(`[AutoMode] Failed to write raw output for ${featureId}:`, error);
}
}, WRITE_DEBOUNCE_MS);
} catch {
// Ignore serialization errors
}
};
// Helper to write current responseText to file
const writeToFile = async (): Promise<void> => {
try {
@@ -1943,19 +1981,65 @@ This mock response was generated because AUTOMAKER_MOCK_AGENT=true was set.
}, WRITE_DEBOUNCE_MS);
};
// Track last text block for deduplication (Cursor sends duplicates)
let lastTextBlock = '';
streamLoop: for await (const msg of stream) {
// Log raw stream event for debugging
appendRawEvent(msg);
if (msg.type === 'assistant' && msg.message?.content) {
for (const block of msg.message.content) {
if (block.type === 'text') {
// Add separator before new text if we already have content and it doesn't end with newlines
if (responseText.length > 0 && !responseText.endsWith('\n\n')) {
if (responseText.endsWith('\n')) {
responseText += '\n';
} else {
const newText = block.text || '';
// Skip empty text
if (!newText) continue;
// Cursor-specific: Skip duplicate consecutive text blocks
// Cursor often sends the same text twice in a row
if (newText === lastTextBlock) {
continue;
}
// Cursor-specific: Skip final accumulated text block
// At the end, Cursor sends one large block containing ALL previous text
// Detect by checking if this block contains most of responseText
if (
responseText.length > 100 &&
newText.length > responseText.length * 0.8 &&
responseText.trim().length > 0
) {
// Check if this looks like accumulated text (contains our existing content)
const normalizedResponse = responseText.replace(/\s+/g, ' ').trim();
const normalizedNew = newText.replace(/\s+/g, ' ').trim();
if (normalizedNew.includes(normalizedResponse.slice(0, 100))) {
// This is the final accumulated block, skip it
continue;
}
}
lastTextBlock = newText;
// Only add separator when we're at a natural paragraph break:
// - Previous text ends with sentence terminator AND new text starts a new thought
// - Don't add separators mid-word or mid-sentence (for streaming providers like Cursor)
if (responseText.length > 0 && newText.length > 0) {
const lastChar = responseText.slice(-1);
const endsWithSentence = /[.!?:]\s*$/.test(responseText);
const endsWithNewline = /\n\s*$/.test(responseText);
const startsNewParagraph = /^[\n#\-*>]/.test(newText);
// Add paragraph break only at natural boundaries
if (
!endsWithNewline &&
(endsWithSentence || startsNewParagraph) &&
!/[a-zA-Z0-9]/.test(lastChar) // Not mid-word
) {
responseText += '\n\n';
}
}
responseText += block.text || '';
responseText += newText;
// Check for authentication errors in the response
if (
@@ -2431,6 +2515,21 @@ Implement all the changes described in the plan above.`;
}
// Final write - ensure all accumulated content is saved
await writeToFile();
// Flush remaining raw output (only if enabled)
if (enableRawOutput) {
if (rawWriteTimeout) {
clearTimeout(rawWriteTimeout);
}
if (rawOutputLines.length > 0) {
try {
await secureFs.mkdir(path.dirname(rawOutputPath), { recursive: true });
await secureFs.appendFile(rawOutputPath, rawOutputLines.join('\n') + '\n');
} catch (error) {
console.error(`[AutoMode] Failed to write final raw output for ${featureId}:`, error);
}
}
}
}
private async executeFeatureWithContext(

View File

@@ -158,6 +158,13 @@ export class FeatureLoader {
return path.join(this.getFeatureDir(projectPath, featureId), 'agent-output.md');
}
/**
* Get the path to a feature's raw-output.jsonl file
*/
getRawOutputPath(projectPath: string, featureId: string): string {
return path.join(this.getFeatureDir(projectPath, featureId), 'raw-output.jsonl');
}
/**
* Generate a new feature ID
*/
@@ -357,6 +364,23 @@ export class FeatureLoader {
}
}
/**
* Get raw output for a feature (JSONL format for debugging)
*/
async getRawOutput(projectPath: string, featureId: string): Promise<string | null> {
try {
const rawOutputPath = this.getRawOutputPath(projectPath, featureId);
const content = (await secureFs.readFile(rawOutputPath, 'utf-8')) as string;
return content;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
return null;
}
logger.error(`[FeatureLoader] Failed to get raw output for ${featureId}:`, error);
throw error;
}
}
/**
* Save agent output for a feature
*/