({
applyRows,
runRecord,
ledger,
runId,
}: {
applyRows: JsonValue;
runRecord?: JsonValue | null;
ledger: ClawSweeperEventLedger;
runId?: string | undefined;
})
| 98 | } |
| 99 | |
| 100 | export function collectClawSweeperEvents({ |
| 101 | applyRows, |
| 102 | runRecord, |
| 103 | ledger, |
| 104 | runId, |
| 105 | }: { |
| 106 | applyRows: JsonValue; |
| 107 | runRecord?: JsonValue | null; |
| 108 | ledger: ClawSweeperEventLedger; |
| 109 | runId?: string | undefined; |
| 110 | }): { considered: number; events: ClawSweeperEvent[]; skipped: JsonObject[] } { |
| 111 | const seen = new Set(ledger.notifications.map((entry) => entry.key)); |
| 112 | const events: ClawSweeperEvent[] = []; |
| 113 | const skipped: JsonObject[] = []; |
| 114 | let considered = 0; |
| 115 | |
| 116 | for (const raw of Array.isArray(applyRows) ? applyRows : []) { |
| 117 | const row = asJsonObject(raw); |
| 118 | if (runId && stringOrNull(row.run_id) !== runId) continue; |
| 119 | const event = buildApplyEvent(row); |
| 120 | if (!event) continue; |
| 121 | considered += 1; |
| 122 | if (seen.has(event.key)) { |
| 123 | skipped.push(skippedRow(event, "notification already sent")); |
| 124 | continue; |
| 125 | } |
| 126 | seen.add(event.key); |
| 127 | events.push(event); |
| 128 | } |
| 129 | |
| 130 | const record = asJsonObject(runRecord); |
| 131 | const recordRunId = stringOrNull(record.run_id); |
| 132 | if (Object.keys(record).length > 0 && (!runId || !recordRunId || recordRunId === runId)) { |
| 133 | for (const raw of Array.isArray(record.fix_actions) ? record.fix_actions : []) { |
| 134 | const event = buildFixEvent(asJsonObject(raw), record); |
| 135 | if (!event) continue; |
| 136 | considered += 1; |
| 137 | if (seen.has(event.key)) { |
| 138 | skipped.push(skippedRow(event, "notification already sent")); |
| 139 | continue; |
| 140 | } |
| 141 | seen.add(event.key); |
| 142 | events.push(event); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return { considered, events, skipped }; |
| 147 | } |
| 148 | |
| 149 | export function buildApplyEvent(row: JsonObject): ClawSweeperEvent | null { |
| 150 | const action = stringOrNull(row.action); |
no test coverage detected