Expiring payloads also retires their frontend sequence identity. Delayed * batch retries cannot resurrect an old warning after routine/error retention. */
| 131 | /* Expiring payloads also retires their frontend sequence identity. Delayed |
| 132 | * batch retries cannot resurrect an old warning after routine/error retention. */ |
| 133 | static bool prune_logs(sqlite3 *db, int level) { |
| 134 | sqlite3_stmt *st = NULL; |
| 135 | const char *boundary = |
| 136 | level >= 2 |
| 137 | ? "SELECT id FROM daemon_logs WHERE level>=2 ORDER BY id DESC LIMIT 1 OFFSET 2000" |
| 138 | : "SELECT id FROM daemon_logs WHERE level<2 ORDER BY id DESC LIMIT 1 OFFSET 3000"; |
| 139 | if (sqlite3_prepare_v2(db, boundary, -1, &st, NULL) != SQLITE_OK) |
| 140 | return false; |
| 141 | int rc = sqlite3_step(st); |
| 142 | int64_t cutoff = rc == SQLITE_ROW ? sqlite3_column_int64(st, 0) : 0; |
| 143 | sqlite3_finalize(st); |
| 144 | if (rc == SQLITE_DONE) |
| 145 | return true; |
| 146 | if (rc != SQLITE_ROW || sqlite3_exec(db, "SAVEPOINT prune_logs", NULL, NULL, NULL) != SQLITE_OK) |
| 147 | return false; |
| 148 | bool ok = |
| 149 | sqlite3_prepare_v2( |
| 150 | db, |
| 151 | "INSERT INTO ui_log_retired(session,max_seq) " |
| 152 | "SELECT json_extract(message,'$.session'),max(json_extract(message,'$.seq')) " |
| 153 | "FROM daemon_logs WHERE id<=?1 AND (level>=2)=?2 AND origin='frontend' AND " |
| 154 | "CASE WHEN json_valid(message) THEN json_type(message,'$.session')='text' AND " |
| 155 | "length(json_extract(message,'$.session'))>0 AND json_type(message,'$.seq')='integer' " |
| 156 | "ELSE 0 END GROUP BY json_extract(message,'$.session') " |
| 157 | "ON CONFLICT(session) DO UPDATE SET max_seq=max(max_seq,excluded.max_seq)", |
| 158 | -1, &st, NULL) == SQLITE_OK; |
| 159 | if (ok) { |
| 160 | sqlite3_bind_int64(st, 1, cutoff); |
| 161 | sqlite3_bind_int(st, 2, level >= 2); |
| 162 | ok = sqlite3_step(st) == SQLITE_DONE; |
| 163 | } |
| 164 | sqlite3_finalize(st); |
| 165 | st = NULL; |
| 166 | if (ok) |
| 167 | ok = sqlite3_prepare_v2(db, "DELETE FROM daemon_logs WHERE id<=?1 AND (level>=2)=?2", -1, |
| 168 | &st, NULL) == SQLITE_OK; |
| 169 | if (ok) { |
| 170 | sqlite3_bind_int64(st, 1, cutoff); |
| 171 | sqlite3_bind_int(st, 2, level >= 2); |
| 172 | ok = sqlite3_step(st) == SQLITE_DONE; |
| 173 | } |
| 174 | sqlite3_finalize(st); |
| 175 | if (ok) |
| 176 | ok = sqlite3_exec(db, "RELEASE prune_logs", NULL, NULL, NULL) == SQLITE_OK; |
| 177 | if (!ok) |
| 178 | sqlite3_exec(db, "ROLLBACK TO prune_logs; RELEASE prune_logs", NULL, NULL, NULL); |
| 179 | return ok; |
| 180 | } |
| 181 | |
| 182 | /* Returns inserted / duplicate / failure. Frontend records are already bounded, |
| 183 | * sanitized JSON; retain their full shape so both APIs share source evidence. */ |