// Debug: check where truncation happens - in the response or in saveFailedResponse

const fs = require('fs');

// Check the saved file - does it match what we expect?
const content = fs.readFileSync('temp/rich-outline-fail_57_1779014018760.txt', 'utf-8');
const marker = '========== AI 原始响应 ';
const idx = content.indexOf(marker);

// File says "AI 原始响应 (9867 chars)"
// Let's check what 9867 chars of actual response would be
const start = content.indexOf('\n', idx) + 1;
const end = content.indexOf('\n\n========== 响应结尾');
const savedResponse = content.substring(start, end).trim();

console.log('Saved response length:', savedResponse.length);
console.log('File header says: 9867 chars');
console.log('Match:', savedResponse.length === 9867);

console.log('\n--- Saved response end ---');
console.log(JSON.stringify(savedResponse.substring(savedResponse.length - 100)));

// The file also has a tail
const tailMarker = '========== 响应结尾500字符 ';
const tailIdx = content.indexOf(tailMarker);
const tailStart = content.indexOf('\n', tailIdx) + 1;
const tail = content.substring(tailStart).trim();
console.log('\n--- Tail (last 500 chars of original) ---');
console.log(JSON.stringify(tail.substring(0, 500)));

// Check if saved response ends mid-property
const lastPropMatch = savedResponse.match(/"estimatedWords":\s*(\d+)/);
console.log('\n--- Last complete property "estimatedWords" ---');
const allEstWords = savedResponse.match(/"estimatedWords":\s*\d+/g);
if (allEstWords) {
  console.log('All estimatedWords found:', allEstWords.length);
  console.log('Last one:', allEstWords[allEstWords.length - 1]);
} else {
  console.log('No complete estimatedWords properties');
}

// Is there a partial "estimatedWords" at the end?
const last500 = savedResponse.slice(-200);
console.log('\n--- Last 200 chars ---');
console.log(JSON.stringify(last500));