File size: 7,543 Bytes
097eaf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { v7 as uuidv7 } from "uuid";
import { logger as rootLogger } from "../../lib/logger";
import { getJobFromGCS } from "../../lib/gcs-jobs";
import {
  MonitorDiffArtifact,
  monitorDiffGcsKey,
  saveMonitorDiffArtifact,
} from "../../lib/gcs-monitoring";
import {
  diffMonitorJson,
  diffMonitorMarkdown,
  formatsRequestGitDiff,
  formatsRequestJsonExtraction,
} from "./diff";
import { judgeChange } from "./judgeChange";

type MonitorPageDiffStatus = "same" | "new" | "changed";

type Judgment = {
  meaningful: boolean;
  confidence: "high" | "medium" | "low";
  reason: string;
  meaningfulChanges: Array<{
    type: "added" | "removed" | "changed";
    before: string | null;
    after: string | null;
    reason: string;
  }>;
};

type MonitorPageDiffResult = {
  status: MonitorPageDiffStatus;
  diffGcsKey: string | null;
  diffTextBytes: number | null;
  diffJsonBytes: number | null;
  judgment?: Judgment;
  diffText?: string;
  diffJson?: Record<string, { previous: unknown; current: unknown }>;
};

type PreviousPageRef = {
  last_scrape_id: string | null;
  is_removed: boolean | null;
};

function isPlainObject(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

/**
 * Compute the page diff for a single monitored URL and persist the diff
 * artifact to GCS when content changed. Branches on whether the monitor
 * asked for JSON extraction (in which case the diff is over `doc.json`) or
 * the default markdown text diff.
 *
 * Returns the new status + GCS key/sizes for the caller to write into the
 * monitor pages row.
 */
export async function computeAndPersistPageDiff(params: {
  teamId: string;
  monitorId: string;
  checkId: string;
  url: string;
  scrapeId: string;
  doc: any;
  previous: PreviousPageRef | null;
  formats: unknown;
  goal?: string | null;
  extractionPrompt?: string | null;
}): Promise<MonitorPageDiffResult> {
  const {
    teamId,
    monitorId,
    checkId,
    url,
    scrapeId,
    doc,
    previous,
    formats,
    goal,
    extractionPrompt,
  } = params;

  const wantsJson = formatsRequestJsonExtraction(formats);
  const wantsGitDiff = formatsRequestGitDiff(formats);

  if (!previous?.last_scrape_id || previous.is_removed) {
    return {
      status: "new",
      diffGcsKey: null,
      diffTextBytes: null,
      diffJsonBytes: null,
    };
  }

  const previousDoc = (await getJobFromGCS(previous.last_scrape_id))?.[0];

  if (wantsJson) {
    const previousJson = isPlainObject(previousDoc?.json)
      ? (previousDoc!.json as Record<string, unknown>)
      : undefined;
    const currentJson = isPlainObject(doc?.json)
      ? (doc.json as Record<string, unknown>)
      : undefined;

    // If the current scrape didn't produce a JSON document we can't
    // compute a JSON diff; treat as `changed` to be safe (matches the
    // markdown-missing branch's behavior).
    if (!currentJson) {
      return {
        status: "changed",
        diffGcsKey: null,
        diffTextBytes: null,
        diffJsonBytes: null,
      };
    }

    const result = diffMonitorJson(previousJson, currentJson);

    // Mixed-mode: also compute the markdown diff so a monitor that asked
    // for both ["json","git-diff"] modes reports `changed` whenever either
    // surface changed, with both diffs persisted side by side.
    let markdownSidecar: { text: string; json: unknown } | undefined;
    if (wantsGitDiff) {
      const previousMarkdown = previousDoc?.markdown;
      const currentMarkdown = doc?.markdown;
      if (previousMarkdown && currentMarkdown) {
        const md = diffMonitorMarkdown(previousMarkdown, currentMarkdown);
        if (md.status === "changed") {
          markdownSidecar = { text: md.text, json: md.json };
        }
      }
    }

    if (result.status === "same" && !markdownSidecar) {
      return {
        status: "same",
        diffGcsKey: null,
        diffTextBytes: null,
        diffJsonBytes: null,
      };
    }

    const diffGcsKey = monitorDiffGcsKey({
      teamId,
      monitorId,
      checkId,
      pageId: uuidv7(),
    });
    const artifact: MonitorDiffArtifact = {
      kind: "json",
      url,
      previousScrapeId: previous.last_scrape_id,
      currentScrapeId: scrapeId,
      generatedAt: new Date().toISOString(),
      // When only markdown changed, the per-field JSON diff is empty.
      json: result.status === "changed" ? result.json : {},
      snapshot: currentJson,
      ...(markdownSidecar ? { markdown: markdownSidecar } : {}),
    };
    const sizes = await saveMonitorDiffArtifact(diffGcsKey, artifact);
    const judgment = goal
      ? await runJudge({
          goal,
          extractionPrompt,
          jsonDiff: result.status === "changed" ? result.json : undefined,
          markdownDiff: markdownSidecar
            ? {
                diffText: markdownSidecar.text,
              }
            : undefined,
        })
      : undefined;
    return {
      status: "changed",
      diffGcsKey,
      diffTextBytes: sizes.textBytes,
      diffJsonBytes: sizes.jsonBytes,
      ...(judgment ? { judgment } : {}),
      ...(result.status === "changed" ? { diffJson: result.json } : {}),
      ...(markdownSidecar ? { diffText: markdownSidecar.text } : {}),
    };
  }

  // Markdown path (existing behavior).
  const previousMarkdown = previousDoc?.markdown;
  const currentMarkdown = doc?.markdown;

  // Only treat genuinely missing markdown as the fallback "changed" case.
  // An empty string is a valid scrape result (e.g. a page that renders no
  // textual content) and should still flow through diffMonitorMarkdown so
  // we can correctly report "same" when both runs returned "".
  if (previousMarkdown == null || currentMarkdown == null) {
    return {
      status: "changed",
      diffGcsKey: null,
      diffTextBytes: null,
      diffJsonBytes: null,
    };
  }

  const diff = diffMonitorMarkdown(previousMarkdown, currentMarkdown);
  if (diff.status === "same") {
    return {
      status: "same",
      diffGcsKey: null,
      diffTextBytes: null,
      diffJsonBytes: null,
    };
  }

  const diffGcsKey = monitorDiffGcsKey({
    teamId,
    monitorId,
    checkId,
    pageId: uuidv7(),
  });
  const artifact: MonitorDiffArtifact = {
    kind: "markdown",
    url,
    previousScrapeId: previous.last_scrape_id,
    currentScrapeId: scrapeId,
    generatedAt: new Date().toISOString(),
    text: diff.text,
    json: diff.json,
  };
  const sizes = await saveMonitorDiffArtifact(diffGcsKey, artifact);
  const judgment = goal
    ? await runJudge({
        goal,
        extractionPrompt,
        markdownDiff: {
          diffText: diff.text,
        },
      })
    : undefined;
  return {
    status: "changed",
    diffGcsKey,
    diffTextBytes: sizes.textBytes,
    diffJsonBytes: sizes.jsonBytes,
    ...(judgment ? { judgment } : {}),
    diffText: diff.text,
  };
}

async function runJudge(args: {
  goal: string;
  extractionPrompt?: string | null;
  jsonDiff?: Record<string, { previous: unknown; current: unknown }>;
  markdownDiff?: {
    diffText?: string;
  };
}): Promise<Judgment | undefined> {
  try {
    return await judgeChange({
      logger: rootLogger.child({ module: "monitoring-judge" }),
      goal: args.goal,
      extractionPrompt: args.extractionPrompt ?? undefined,
      jsonDiff: args.jsonDiff,
      markdownDiff: args.markdownDiff,
    });
  } catch (error) {
    rootLogger.error("Judge call failed", { error });
    return undefined;
  }
}