rivetkit-core 2.3.10

Core runtime primitives for RivetKit actor hosts
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
use std::fmt::Write as _;

use rusqlite::types::Value;
use rusqlite::{Connection, StatementStatus, params_from_iter};

use crate::actor::internal_storage::queries;
use crate::actor::internal_storage::schema::{CREATE_META_TABLE, MIGRATIONS};
use crate::actor::{internal_storage, schedule};
use crate::types::ListOpts;

// This suite covers SQL owned by rivetkit-core. User-provided SQL, including
// Actor Runtime Socket requests, is intentionally outside its scope.
const REPRESENTATIVE_ROWS: usize = 10_000;

#[derive(Clone, Copy, Debug)]
struct AllowedScan {
	table: &'static str,
	reason: &'static str,
	bound: &'static str,
}

#[derive(Clone, Debug, Default)]
struct QueryPlanExpectation {
	forbid_full_scan_of: &'static [&'static str],
	forbid_temp_sort: bool,
	forbid_auto_index: bool,
	expected_index: Option<&'static str>,
	allowed_scans: &'static [AllowedScan],
}

#[derive(Debug)]
struct QueryCase {
	id: &'static str,
	sql: String,
	params: Vec<Value>,
	expectation: QueryPlanExpectation,
}

#[derive(Clone, Copy, Debug)]
struct StatementMetrics {
	fullscan_steps: i32,
	sorts: i32,
	auto_indexes: i32,
	vm_steps: i32,
}

fn indexed(
	expected_index: Option<&'static str>,
	tables: &'static [&'static str],
) -> QueryPlanExpectation {
	QueryPlanExpectation {
		forbid_full_scan_of: tables,
		forbid_temp_sort: true,
		forbid_auto_index: true,
		expected_index,
		allowed_scans: &[],
	}
}

fn bounded_scan(allowed_scans: &'static [AllowedScan]) -> QueryPlanExpectation {
	QueryPlanExpectation {
		forbid_temp_sort: true,
		forbid_auto_index: true,
		allowed_scans,
		..QueryPlanExpectation::default()
	}
}

fn text(value: &str) -> Value {
	Value::Text(value.to_owned())
}

fn fixture(row_count: usize) -> Connection {
	let mut db = Connection::open_in_memory().expect("open efficiency fixture");
	db.execute_batch(CREATE_META_TABLE)
		.expect("create real metadata schema");
	for migration in MIGRATIONS {
		for sql in *migration {
			db.execute_batch(sql).expect("apply real internal schema");
		}
	}

	let tx = db.transaction().expect("begin fixture seed");
	tx.execute(
		"INSERT INTO _rivet_runtime (id, last_pushed_alarm, inspector_token, queue_next_id) VALUES (1, 42, 'token', ?)",
		[row_count as i64 + 1],
	)
	.expect("seed runtime");
	tx.execute(
		"INSERT INTO _rivet_actor (id, has_initialized, input) VALUES (1, 1, x'01')",
		[],
	)
	.expect("seed actor");
	tx.execute(
		"INSERT INTO _rivet_actor_state (id, state) VALUES (1, x'02')",
		[],
	)
	.expect("seed actor state");
	tx.execute(
		"INSERT INTO _rivet_meta (key, value) VALUES ('fixture', x'03')",
		[],
	)
	.expect("seed metadata");

	let mut conn_insert = tx
		.prepare("INSERT INTO _rivet_conns (conn_id, parameters, gateway_id, request_id, request_path, request_headers) VALUES (?, x'01', x'00000000', x'00000000', '/', x'01')")
		.expect("prepare connection seed");
	let mut conn_state_insert = tx
		.prepare("INSERT INTO _rivet_conn_state (conn_id, state, server_message_index, client_message_index, subscriptions) VALUES (?, x'01', 0, 0, x'01')")
		.expect("prepare connection state seed");
	let mut queue_insert = tx
		.prepare(
			"INSERT INTO _rivet_queue (id, name, body, created_at) VALUES (?, 'message', x'01', ?)",
		)
		.expect("prepare queue seed");
	let mut user_kv_insert = tx
		.prepare("INSERT INTO _rivet_user_kv (key, value) VALUES (?, x'01')")
		.expect("prepare user kv seed");
	let mut workflow_kv_insert = tx
		.prepare("INSERT INTO _rivet_wf_kv (key, value) VALUES (?, x'01')")
		.expect("prepare workflow kv seed");
	let mut schedule_insert = tx
		.prepare("INSERT INTO _rivet_schedule_events (event_id, trigger_at, action, args, kind, cron_expression, timezone, interval_ms, last_started_at, max_history) VALUES (?, ?, 'run', x'01', ?, NULL, NULL, NULL, NULL, 100)")
		.expect("prepare schedule seed");
	let mut history_insert = tx
		.prepare("INSERT INTO _rivet_schedule_history (schedule_id, action, scheduled_at, fired_at, finished_at, result) VALUES (?, 'run', ?, ?, ?, ?)")
		.expect("prepare history seed");
	for index in 0..row_count {
		let key = format!("{index:08}");
		conn_insert.execute([&key]).expect("seed connection");
		conn_state_insert
			.execute([&key])
			.expect("seed connection state");
		queue_insert
			.execute((index as i64 + 1, index as i64))
			.expect("seed queue");
		user_kv_insert
			.execute([key.as_bytes()])
			.expect("seed user kv");
		workflow_kv_insert
			.execute([key.as_bytes()])
			.expect("seed workflow kv");
		let event_id = if index % 3 == 0 {
			format!("at:{key}")
		} else {
			format!("cron:{key}")
		};
		schedule_insert
			.execute((&event_id, index as i64, (index % 3) as i64))
			.expect("seed schedule");
		let schedule_id = format!("cron:{:04}", index % 100);
		history_insert
			.execute((
				&schedule_id,
				index as i64,
				index as i64,
				index as i64,
				if index % 997 == 0 { 0_i64 } else { 1_i64 },
			))
			.expect("seed schedule history");
	}
	drop(conn_insert);
	drop(conn_state_insert);
	drop(queue_insert);
	drop(user_kv_insert);
	drop(workflow_kv_insert);
	drop(schedule_insert);
	drop(history_insert);
	tx.commit().expect("commit fixture seed");
	db.execute_batch("ANALYZE").expect("analyze fixture");
	db
}

fn explain_plan(db: &Connection, sql: &str, params: &[Value]) -> Vec<String> {
	let mut statement = db
		.prepare(&format!("EXPLAIN QUERY PLAN {sql}"))
		.expect("prepare query plan");
	statement
		.query_map(params_from_iter(params.iter()), |row| {
			row.get::<_, String>(3)
		})
		.expect("execute query plan")
		.map(|detail| normalize_plan_detail(&detail.expect("read query plan detail")))
		.collect()
}

fn normalize_plan_detail(detail: &str) -> String {
	detail
		.to_ascii_lowercase()
		.replace(['`', '"', '[', ']'], "")
		.split_whitespace()
		.collect::<Vec<_>>()
		.join(" ")
}

fn plan_has_scan(plan: &[String], table: &str) -> bool {
	let table = table.to_ascii_lowercase();
	plan.iter().any(|detail| {
		(detail.starts_with("scan ") || detail.contains(" scan "))
			&& detail.split_whitespace().any(|word| word == table)
	})
}

fn assert_query_plan(
	query_id: &str,
	plan: &[String],
	expectation: &QueryPlanExpectation,
) -> Result<(), String> {
	let rendered = plan.join("\n");
	for table in expectation.forbid_full_scan_of {
		if plan_has_scan(plan, table)
			&& !expectation
				.allowed_scans
				.iter()
				.any(|allowed| allowed.table == *table)
		{
			return Err(format!(
				"{query_id}: forbidden full scan of {table}\n{rendered}"
			));
		}
	}
	if expectation.forbid_temp_sort && rendered.contains("temp b-tree") {
		return Err(format!("{query_id}: temporary sort\n{rendered}"));
	}
	if expectation.forbid_auto_index && rendered.contains("automatic") {
		return Err(format!("{query_id}: automatic index\n{rendered}"));
	}
	if let Some(expected_index) = expectation.expected_index
		&& !rendered.contains(&expected_index.to_ascii_lowercase())
	{
		return Err(format!(
			"{query_id}: expected index {expected_index}\n{rendered}"
		));
	}
	for allowed in expectation.allowed_scans {
		assert!(
			!allowed.reason.is_empty(),
			"{query_id}: scan reason is empty"
		);
		assert!(!allowed.bound.is_empty(), "{query_id}: scan bound is empty");
	}
	Ok(())
}

fn execute_with_status(db: &Connection, sql: &str, params: &[Value]) -> StatementMetrics {
	let mut statement = db.prepare(sql).expect("prepare measured statement");
	for status in [
		StatementStatus::FullscanStep,
		StatementStatus::Sort,
		StatementStatus::AutoIndex,
		StatementStatus::VmStep,
	] {
		statement.reset_status(status);
	}
	if statement.readonly() {
		let mut rows = statement
			.query(params_from_iter(params.iter()))
			.expect("execute measured query");
		while rows.next().expect("read measured row").is_some() {}
	} else {
		statement
			.execute(params_from_iter(params.iter()))
			.expect("execute measured statement");
	}
	StatementMetrics {
		fullscan_steps: statement.get_status(StatementStatus::FullscanStep),
		sorts: statement.get_status(StatementStatus::Sort),
		auto_indexes: statement.get_status(StatementStatus::AutoIndex),
		vm_steps: statement.get_status(StatementStatus::VmStep),
	}
}

fn query_catalog() -> Vec<QueryCase> {
	let all_schedules = &["_rivet_schedule_events"];
	let all_history = &["_rivet_schedule_history"];
	let all_user_kv = &["_rivet_user_kv"];
	vec![
		QueryCase {
			id: "actor.snapshot",
			sql: internal_storage::LOAD_ACTOR_SNAPSHOT_SQL.into(),
			params: vec![],
			expectation: indexed(None, &["_rivet_actor", "_rivet_actor_state"]),
		},
		QueryCase {
			id: "connection.list",
			sql: internal_storage::LOAD_CONNECTIONS_SQL.into(),
			params: vec![],
			expectation: bounded_scan(&[AllowedScan {
				table: "c",
				reason: "actor startup restores every live connection",
				bound: "rows are lifecycle-bounded to currently live or hibernated connections and are deleted on disconnect",
			}]),
		},
		QueryCase {
			id: "migration.reset_schedules",
			sql: internal_storage::RESET_SCHEDULES_FOR_LEGACY_IMPORT_SQL.into(),
			params: vec![],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_schedule_events",
				reason: "one-time legacy import replaces the complete pre-import schedule set",
				bound: "the legacy actor snapshot containing the source schedule vector is capped at 256 KiB",
			}]),
		},
		QueryCase {
			id: "queue.next_id",
			sql: internal_storage::LOAD_QUEUE_NEXT_ID_SQL.into(),
			params: vec![],
			expectation: indexed(None, &["_rivet_runtime"]),
		},
		QueryCase {
			id: "queue.stats",
			sql: internal_storage::LOAD_QUEUE_STATS_SQL.into(),
			params: vec![],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_queue",
				reason: "startup validates persisted queue cardinality and maximum id",
				bound: "ActorConfig.max_queue_size caps queue rows (default 1,000)",
			}]),
		},
		QueryCase {
			id: "queue.list",
			sql: internal_storage::LOAD_QUEUE_MESSAGES_SQL.into(),
			params: vec![],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_queue",
				reason: "startup restores queued messages in primary-key order",
				bound: "ActorConfig.max_queue_size caps queue rows (default 1,000)",
			}]),
		},
		QueryCase {
			id: "queue.receive",
			sql: internal_storage::LOAD_QUEUE_MESSAGES_LIMITED_SQL.into(),
			params: vec![5_i64.into()],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_queue",
				reason: "unfiltered queue receive walks global enqueue order",
				bound: "LIMIT caps returned message bodies; ActorConfig.max_queue_size caps the queue",
			}]),
		},
		QueryCase {
			id: "queue.receive_name",
			sql: internal_storage::LOAD_QUEUE_MESSAGES_FOR_NAME_SQL.into(),
			params: vec![text("message"), 5_i64.into()],
			expectation: indexed(Some("_rivet_queue_name_id"), &["_rivet_queue"]),
		},
		QueryCase {
			id: "queue.available_name",
			sql: internal_storage::HAS_QUEUE_MESSAGES_FOR_NAME_SQL.into(),
			params: vec![text("message")],
			expectation: indexed(Some("_rivet_queue_name_id"), &["_rivet_queue"]),
		},
		QueryCase {
			id: "queue.filter_metadata_page",
			sql: internal_storage::LOAD_QUEUE_MESSAGE_METADATA_PAGE_SQL.into(),
			params: vec![0_i64.into(), 128_i64.into()],
			expectation: indexed(None, &["_rivet_queue"]),
		},
		QueryCase {
			id: "queue.load_ids",
			sql: internal_storage::load_queue_messages_by_ids_sql(3),
			params: vec![1_i64.into(), 2_i64.into(), 3_i64.into()],
			expectation: indexed(None, &["_rivet_queue"]),
		},
		QueryCase {
			id: "queue.available",
			sql: internal_storage::HAS_QUEUE_MESSAGES_SQL.into(),
			params: vec![],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_queue",
				reason: "unfiltered availability checks stop at the first queue row",
				bound: "LIMIT 1",
			}]),
		},
		QueryCase {
			id: "queue.delete",
			sql: internal_storage::DELETE_QUEUE_MESSAGE_SQL.into(),
			params: vec![1_i64.into()],
			expectation: indexed(None, &["_rivet_queue"]),
		},
		QueryCase {
			id: "queue.reset",
			sql: internal_storage::RESET_QUEUE_SQL.into(),
			params: vec![],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_queue",
				reason: "explicit reset removes the complete logical queue",
				bound: "ActorConfig.max_queue_size caps queue rows (default 1,000)",
			}]),
		},
		QueryCase {
			id: "user_kv.batch_get",
			sql: internal_storage::user_kv_batch_get_sql(3),
			params: vec![
				Value::Blob(b"00000001".to_vec()),
				Value::Blob(b"00005000".to_vec()),
				Value::Blob(b"00009999".to_vec()),
			],
			expectation: indexed(None, all_user_kv),
		},
		QueryCase {
			id: "user_kv.delete",
			sql: internal_storage::DELETE_USER_KV_SQL.into(),
			params: vec![Value::Blob(b"00000001".to_vec())],
			expectation: indexed(None, all_user_kv),
		},
		QueryCase {
			id: "user_kv.delete_range",
			sql: internal_storage::DELETE_USER_KV_RANGE_SQL.into(),
			params: vec![
				Value::Blob(b"00000010".to_vec()),
				Value::Blob(b"00000020".to_vec()),
			],
			expectation: indexed(None, all_user_kv),
		},
		QueryCase {
			id: "user_kv.list_range",
			sql: internal_storage::user_kv_list_sql("WHERE key >= ? AND key < ?", false, true),
			params: vec![
				Value::Blob(b"00000010".to_vec()),
				Value::Blob(b"00000100".to_vec()),
				20_i64.into(),
			],
			expectation: indexed(None, all_user_kv),
		},
		QueryCase {
			id: "user_kv.list_prefix_open",
			sql: internal_storage::user_kv_list_sql("WHERE key >= ?", true, true),
			params: vec![Value::Blob(b"00009900".to_vec()), 20_i64.into()],
			expectation: indexed(None, all_user_kv),
		},
		QueryCase {
			id: "user_kv.list_prefix",
			sql: internal_storage::user_kv_list_sql("WHERE key >= ? AND key < ?", false, false),
			params: vec![
				Value::Blob(b"000000".to_vec()),
				Value::Blob(b"000001".to_vec()),
			],
			expectation: indexed(None, all_user_kv),
		},
		QueryCase {
			id: "user_kv.list_all",
			sql: internal_storage::user_kv_list_sql("", false, false),
			params: vec![],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_user_kv",
				reason: "the explicit list-all API returns the complete actor KV namespace in primary-key order",
				bound: "callers can set ListOpts.limit when they do not want to materialize the complete namespace",
			}]),
		},
		QueryCase {
			id: "connection.delete_state",
			sql: internal_storage::DELETE_CONN_STATE_SQL.into(),
			params: vec![text("00000001")],
			expectation: indexed(None, &["_rivet_conn_state"]),
		},
		QueryCase {
			id: "connection.delete",
			sql: internal_storage::DELETE_CONN_SQL.into(),
			params: vec![text("00000001")],
			expectation: indexed(None, &["_rivet_conns"]),
		},
		QueryCase {
			id: "runtime.last_alarm",
			sql: internal_storage::LOAD_LAST_PUSHED_ALARM_SQL.into(),
			params: vec![],
			expectation: indexed(None, &["_rivet_runtime"]),
		},
		QueryCase {
			id: "runtime.inspector_token",
			sql: internal_storage::LOAD_INSPECTOR_TOKEN_SQL.into(),
			params: vec![],
			expectation: indexed(None, &["_rivet_runtime"]),
		},
		QueryCase {
			id: "meta.get",
			sql: internal_storage::LOAD_META_TEXT_SQL.into(),
			params: vec![text("fixture")],
			expectation: indexed(None, &["_rivet_meta"]),
		},
		QueryCase {
			id: "schedule.cancel",
			sql: queries::CANCEL_SCHEDULE_SQL.into(),
			params: vec![text("at:00000000"), 0_i64.into()],
			expectation: indexed(None, all_schedules),
		},
		QueryCase {
			id: "schedule.get_one_shot",
			sql: queries::GET_SCHEDULED_EVENT_SQL.into(),
			params: vec![text("at:00000000"), 0_i64.into()],
			expectation: indexed(None, all_schedules),
		},
		QueryCase {
			id: "schedule.list_one_shots",
			sql: queries::LIST_SCHEDULED_EVENTS_SQL.into(),
			params: vec![0_i64.into()],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_schedule_events",
				reason: "the API enumerates all one-shot schedules in trigger order",
				bound: "ActorConfig.max_schedules caps all schedules (default 1,000)",
			}]),
		},
		QueryCase {
			id: "schedule.delete_cron_history",
			sql: queries::DELETE_CRON_HISTORY_SQL.into(),
			params: vec![text("cron:00000001"), text("cron:00000001"), 0_i64.into()],
			expectation: indexed(Some("_rivet_schedule_history_schedule"), all_history),
		},
		QueryCase {
			id: "schedule.delete_cron",
			sql: queries::DELETE_CRON_SQL.into(),
			params: vec![text("cron:00000001"), 0_i64.into()],
			expectation: indexed(None, all_schedules),
		},
		QueryCase {
			id: "schedule.delete_cron_if_action",
			sql: queries::DELETE_CRON_IF_ACTION_SQL.into(),
			params: vec![text("cron:00000001"), 0_i64.into(), text("run")],
			expectation: indexed(None, all_schedules),
		},
		QueryCase {
			id: "schedule.list_recurring",
			sql: queries::LIST_CRONS_SQL.into(),
			params: vec![0_i64.into()],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_schedule_events",
				reason: "the API enumerates all recurring schedules in trigger order",
				bound: "ActorConfig.max_schedules caps all schedules (default 1,000)",
			}]),
		},
		QueryCase {
			id: "schedule.history",
			sql: queries::CRON_HISTORY_SQL.into(),
			params: vec![text("cron:0001"), 20_i64.into()],
			expectation: indexed(Some("_rivet_schedule_history_schedule"), all_history),
		},
		QueryCase {
			id: "schedule.get",
			sql: queries::LOAD_SCHEDULE_SQL.into(),
			params: vec![text("cron:00000001")],
			expectation: indexed(None, all_schedules),
		},
		QueryCase {
			id: "schedule.count",
			sql: queries::COUNT_SCHEDULES_SQL.into(),
			params: vec![],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_schedule_events",
				reason: "SQLite represents its optimized COUNT(*) opcode as a covering-index scan in the query plan",
				bound: "runtime VM-step scaling verifies row-count-independent work; ActorConfig.max_schedules also caps rows",
			}]),
		},
		QueryCase {
			id: "schedule.due",
			sql: queries::TAKE_DUE_SCHEDULES_SQL.into(),
			params: vec![5_i64.into()],
			expectation: indexed(Some("_rivet_schedule_events_trigger_at"), all_schedules),
		},
		QueryCase {
			id: "schedule.claim_one_shots",
			sql: queries::claim_one_shots_sql(3),
			params: vec![
				0_i64.into(),
				text("at:00000000"),
				0_i64.into(),
				text("at:00000003"),
				3_i64.into(),
				text("at:00000006"),
				6_i64.into(),
			],
			expectation: indexed(None, all_schedules),
		},
		QueryCase {
			id: "schedule.advance_skipped",
			sql: queries::ADVANCE_SKIPPED_SCHEDULE_SQL.into(),
			params: vec![20_000_i64.into(), text("cron:00000001")],
			expectation: indexed(None, all_schedules),
		},
		QueryCase {
			id: "schedule.advance",
			sql: queries::ADVANCE_SCHEDULE_SQL.into(),
			params: vec![20_000_i64.into(), 10_000_i64.into(), text("cron:00000002")],
			expectation: indexed(None, all_schedules),
		},
		QueryCase {
			id: "schedule.finish_history",
			sql: queries::FINISH_HISTORY_SQL.into(),
			params: vec![
				20_000_i64.into(),
				1_i64.into(),
				Value::Null,
				Value::Null,
				Value::Null,
				Value::Null,
				1_i64.into(),
				0_i64.into(),
			],
			expectation: indexed(None, all_history),
		},
		QueryCase {
			id: "schedule.recover_history",
			sql: queries::RECOVER_HISTORY_SQL.into(),
			params: vec![
				20_000_i64.into(),
				2_i64.into(),
				text("schedule"),
				text("interrupted"),
				text("interrupted"),
				Value::Null,
			],
			expectation: indexed(Some("_rivet_schedule_history_running"), all_history),
		},
		QueryCase {
			id: "schedule.next_future",
			sql: queries::NEXT_FUTURE_SCHEDULE_SQL.into(),
			params: vec![5_000_i64.into()],
			expectation: indexed(Some("_rivet_schedule_events_trigger_at"), all_schedules),
		},
		QueryCase {
			id: "schedule.next",
			sql: queries::NEXT_SCHEDULE_SQL.into(),
			params: vec![],
			expectation: indexed(Some("_rivet_schedule_events_trigger_at"), all_schedules),
		},
		QueryCase {
			id: "schedule.prune",
			sql: queries::PRUNE_SCHEDULE_HISTORY_SQL.into(),
			params: vec![text("cron:0001"), 20_i64.into()],
			expectation: indexed(Some("_rivet_schedule_history_schedule"), all_history),
		},
		QueryCase {
			id: "schedule.prune_global",
			sql: queries::PRUNE_GLOBAL_HISTORY_SQL.into(),
			params: vec![schedule::GLOBAL_HISTORY_RETAINED_ROWS.into()],
			expectation: QueryPlanExpectation {
				forbid_full_scan_of: all_history,
				forbid_temp_sort: true,
				forbid_auto_index: true,
				expected_index: Some("_rivet_schedule_history_fired_at"),
				allowed_scans: &[AllowedScan {
					table: "_rivet_schedule_history",
					reason: "global pruning walks the history ordering index to skip retained rows and delete only the overflow",
					bound: "periodic pruning retains 9,900 rows, leaving headroom beneath MAX_ACTOR_HISTORY",
				}],
			},
		},
		QueryCase {
			id: "cleanup.select_chunk",
			sql: internal_storage::clear_table_select_sql(
				"_rivet_user_kv",
				"key",
				"length(key) + length(value)",
				internal_storage::KV_TX_MAX_ROWS,
			),
			params: vec![],
			expectation: bounded_scan(&[AllowedScan {
				table: "_rivet_user_kv",
				reason: "interrupted legacy import cleanup walks one primary-key chunk",
				bound: "each statement is limited to KV_TX_MAX_ROWS (128)",
			}]),
		},
		QueryCase {
			id: "cleanup.delete_row",
			sql: internal_storage::clear_table_delete_sql("_rivet_user_kv", "key"),
			params: vec![Value::Blob(b"00000001".to_vec())],
			expectation: indexed(None, all_user_kv),
		},
	]
}

#[test]
fn sql_efficiency_production_queries_use_expected_plans() {
	let db = fixture(REPRESENTATIVE_ROWS);
	let mut failures = String::new();
	for case in query_catalog() {
		let plan = explain_plan(&db, &case.sql, &case.params);
		if let Err(error) = assert_query_plan(case.id, &plan, &case.expectation) {
			let _ = writeln!(failures, "{error}\n");
		}
	}
	assert!(failures.is_empty(), "query plan failures:\n{failures}");
}

#[test]
fn sql_efficiency_indexed_queries_have_no_runtime_scan_sort_or_auto_index() {
	let db = fixture(REPRESENTATIVE_ROWS);
	let mut failures = String::new();
	for case in query_catalog()
		.into_iter()
		.filter(|case| case.expectation.allowed_scans.is_empty())
	{
		let metrics = execute_with_status(&db, &case.sql, &case.params);
		if metrics.fullscan_steps != 0 || metrics.sorts != 0 || metrics.auto_indexes != 0 {
			let plan = explain_plan(&db, &case.sql, &case.params);
			let _ = writeln!(
				failures,
				"{}: metrics={metrics:?}\n{}\n",
				case.id,
				plan.join("\n")
			);
		}
	}
	assert!(
		failures.is_empty(),
		"runtime efficiency failures:\n{failures}"
	);
}

fn measured_vm_steps(row_count: usize, sql: &str, params: &[Value]) -> i32 {
	let db = fixture(row_count);
	execute_with_status(&db, sql, params).vm_steps
}

#[test]
fn sql_efficiency_hot_paths_do_not_scale_with_unrelated_rows() {
	let cases = [
		(
			"connection.delete",
			internal_storage::DELETE_CONN_SQL.to_owned(),
			vec![Value::Text("00000050".to_owned())],
		),
		(
			"queue.delete",
			internal_storage::DELETE_QUEUE_MESSAGE_SQL.to_owned(),
			vec![Value::Integer(50)],
		),
		(
			"user_kv.batch_get",
			internal_storage::user_kv_batch_get_sql(1),
			vec![Value::Blob(b"00000050".to_vec())],
		),
		(
			"schedule.get",
			queries::LOAD_SCHEDULE_SQL.to_owned(),
			vec![Value::Text("cron:00000050".to_owned())],
		),
		(
			"schedule.due",
			queries::TAKE_DUE_SCHEDULES_SQL.to_owned(),
			vec![Value::Integer(5)],
		),
	];
	for (id, sql, params) in cases {
		let small = measured_vm_steps(100, &sql, &params);
		let large = measured_vm_steps(REPRESENTATIVE_ROWS, &sql, &params);
		assert!(
			large <= small.saturating_mul(3).saturating_add(100),
			"{id} scales with unrelated cardinality: small={small}, large={large}"
		);
	}
}

#[test]
fn sql_efficiency_negative_control_detects_unindexed_access() {
	let db = fixture(REPRESENTATIVE_ROWS);
	let sql = "SELECT event_id FROM _rivet_schedule_events WHERE action = ? ORDER BY event_id";
	let params = vec![Value::Text("run".to_owned())];
	let plan = explain_plan(&db, sql, &params);
	let expectation = indexed(None, &["_rivet_schedule_events"]);
	let error = assert_query_plan("negative.unindexed_schedule_action", &plan, &expectation)
		.expect_err("negative control must detect a full scan");
	let metrics = execute_with_status(&db, sql, &params);
	assert!(error.contains("forbidden full scan"), "{error}");
	assert!(
		metrics.fullscan_steps > 0 || metrics.sorts > 0,
		"negative control must record inefficient work: {metrics:?}"
	);
}

#[tokio::test]
async fn sql_efficiency_unlimited_kv_listing_uses_one_ordered_query() {
	let ctx = crate::testing::actor_context("sql-efficiency-list", "actor", Vec::new(), "local");
	crate::actor::internal_storage::schema::ensure_internal_schema(ctx.sql())
		.await
		.expect("initialize KV listing fixture");
	let entries = (0..300)
		.map(|index| {
			(
				format!("prefix:{index:04}").into_bytes(),
				format!("value:{index:04}").into_bytes(),
			)
		})
		.collect::<Vec<_>>();
	let refs = entries
		.iter()
		.map(|(key, value)| (key.as_slice(), value.as_slice()))
		.collect::<Vec<_>>();
	internal_storage::user_kv_batch_put(ctx.sql(), &refs)
		.await
		.expect("seed KV listing fixture");

	let forward = internal_storage::user_kv_list_prefix(ctx.sql(), b"prefix:", ListOpts::default())
		.await
		.expect("list all KV values forward");
	assert_eq!(forward, entries);

	let reverse = internal_storage::user_kv_list_prefix(
		ctx.sql(),
		b"prefix:",
		ListOpts {
			reverse: true,
			limit: None,
		},
	)
	.await
	.expect("list all KV values in reverse");
	assert_eq!(reverse, entries.into_iter().rev().collect::<Vec<_>>());
}