private extractDiffFromOutput()

in app/lib/server/processors/ApiJobExecutor.ts [52:135]


  private extractDiffFromOutput(output: string, jobId: string) {
    const delimiter =
      "================================================================================";

    console.log(
      `🔍 Extracting diff from output (${output.length} chars) for job ${jobId}`
    );

    // Count total occurrences for debugging
    const delimiterCount = (
      output.match(new RegExp(delimiter.replace(/=/g, "\\="), "g")) || []
    ).length;
    console.log(`📊 Found ${delimiterCount} delimiter occurrences`);

    // Find the last occurrence of the delimiter (end marker)
    const endIndex = output.lastIndexOf(delimiter);
    if (endIndex === -1) {
      console.warn(
        "❌ No end delimiter found in output, generating empty diff"
      );
      // Log a sample of the output for debugging
      console.log(
        "📄 Output sample (first 500 chars):",
        output.substring(0, 500)
      );
      return {
        jobId,
        files: [],
        summary: { totalAdditions: 0, totalDeletions: 0, totalFiles: 0 },
      };
    }

    console.log(`📍 End delimiter found at position: ${endIndex}`);

    // Find the second-to-last occurrence of the delimiter (start marker)
    // by searching backwards from the position before the end delimiter
    const searchUpTo = endIndex - 1;
    const startIndex = output.lastIndexOf(delimiter, searchUpTo);

    console.log(
      `📍 Start delimiter search up to position ${searchUpTo}, found at: ${startIndex}`
    );

    if (startIndex !== -1 && startIndex !== endIndex) {
      const diffContent = output
        .substring(startIndex + delimiter.length, endIndex)
        .trim();
      console.log(`📝 Extracted diff content (${diffContent.length} chars)`);
      console.log(`📄 Diff preview:`, diffContent.substring(0, 200));

      if (diffContent) {
        return this.parseDiff(diffContent, jobId);
      } else {
        console.warn("⚠️ Diff content is empty after trimming");
      }
    } else if (startIndex === -1) {
      console.warn(
        "⚠️ No start delimiter found - only one delimiter in output"
      );
      // If there's only one delimiter, maybe the diff is after it?
      const contentAfterDelimiter = output
        .substring(endIndex + delimiter.length)
        .trim();
      if (contentAfterDelimiter) {
        console.log(
          `🔄 Trying content after single delimiter (${contentAfterDelimiter.length} chars)`
        );
        console.log(
          `📄 Content preview:`,
          contentAfterDelimiter.substring(0, 200)
        );
        return this.parseDiff(contentAfterDelimiter, jobId);
      }
    } else {
      console.warn("⚠️ Start and end delimiters are the same position");
    }

    console.warn("❌ No valid diff found in output, generating empty diff");
    return {
      jobId,
      files: [],
      summary: { totalAdditions: 0, totalDeletions: 0, totalFiles: 0 },
    };
  }