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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
//! Shared best-effort "ensure this project is indexed by trusty-search" entry
//! point, hoisted out of trusty-mpm so a second crate (trusty-code) can reuse
//! the ONE implementation instead of duplicating it.
//!
//! Why: the register-and-populate logic (derive the canonical index id, then
//! find-or-create the daemon-side index and best-effort trigger a
//! freshness-gated reindex) originally lived only in trusty-mpm's
//! `core::session_launch::search_index::register_project_index` (issues #1373 /
//! #1908). trusty-code now wants the same behaviour at task start so a tcode
//! run's working project is discoverable via trusty-search while the agent
//! loop proceeds. Per the workspace's common-entry-point rule (CLAUDE.md), a
//! capability used by two crates must be one shared function in trusty-common —
//! not copy-pasted — so the two call sites can never silently diverge.
//!
//! What: [`ensure_project_indexed`] resolves the git-root, derives the index id
//! via [`crate::resolve_project_root`] / [`crate::derive_index_id`], and — when
//! the daemon is discoverable — best-effort registers the index (`POST
//! /indexes`, ~1s cap) then best-effort triggers a freshness-gated reindex
//! (`POST /indexes/{id}/reindex`, ~2s cap, skipped when the index already holds
//! chunks indexed within the last hour). Every step is fail-open in the sense
//! that failures are logged at warn/debug and never propagated, so the caller (a
//! session launch or a task run) is never blocked or aborted by an
//! unreachable/slow search daemon. What it is NOT (#5091) is fail-open in its
//! RETURN: the id comes back only when the daemon confirmed the index, so a
//! failed create cannot advance a caller's pin. The blocking HTTP calls run on dedicated OS
//! threads so the function is safe to call from inside a tokio runtime.
//!
//! Mid-task incremental re-indexing: [`ensure_project_indexed`] runs once, at
//! task start — for a greenfield project that starts EMPTY, that means
//! `search_code` finds nothing the engineer writes DURING the task.
//! [`index_files_best_effort`] complements it: called after each successful
//! file write/edit, it POSTs just that file's fresh content to the daemon's
//! cheap per-file `POST /indexes/{id}/index-file` endpoint (never a full
//! reindex walk), so the growing codebase stays searchable within the same
//! task. Same fail-open contract, and non-blocking by construction (hands the
//! work to a background pool rather than relying on the caller to wrap it,
//! since its call sites are tcode's tool executors, not a one-shot task-start
//! hook). That pool is BOUNDED (issue #2798) — see [`crate::index_dispatch`]
//! for the sizes and for what happens to a batch submitted when it is full.
//!
//! `allow_sensitive_path` (issue #2914 — ephemeral index leak): earlier
//! revisions hardcoded `allow_sensitive_path: true` on every `POST /indexes`
//! this module issued, unconditionally bypassing the daemon's
//! `SENSITIVE_PATH_PREFIXES` denylist (`/tmp`, `/private/tmp`, `/var/folders`,
//! `/private/var/folders`) for BOTH callers. That bypass is only meaningful
//! for trusty-code, whose `directory`-bound working project can legitimately
//! live under an OS-temp prefix (issue #2747: a tcode scratch/bake-off
//! project). trusty-mpm's session-launch caller never has a legitimate reason
//! to index an OS-temp path — a real session workspace is always either the
//! user's checked-out repo or a `.worktrees/<uuid>` leaf INSIDE it — so for
//! that caller the bypass was a pure liability: any test exercising the
//! session-launch pipeline with a `tempfile`-backed workspace stand-in (e.g.
//! trusty-mpm's own `*-selfheal-ws`/`*-stale-heal-ws` fixtures) silently
//! registered that throwaway tempdir against whatever REAL trusty-search
//! daemon happened to be discoverable, because the denylist's one guard
//! against exactly that was switched off unconditionally. [`ensure_project_indexed`]
//! now takes `allow_sensitive_path` as an explicit parameter so each caller
//! states its own intent instead of inheriting trusty-code's opt-in for free.
//!
//! Test: `create_rejected_by_the_daemon_withholds_the_pinnable_id`,
//! `ensure_project_indexed_withholds_id_when_nothing_was_registered`,
//! `ensure_project_indexed_none_for_root`,
//! `ensure_project_indexed_refuses_the_real_home_directory`,
//! `index_files_inner_refuses_the_real_home_directory`,
//! the `index_is_fresh_*` predicate
//! tests, the `index_files_inner_*` / `relative_index_path_*` /
//! `index_file_request_body_*` tests, and the incremental-hardening tests
//! `retry_backoff_is_bounded_and_increasing` /
//! `post_index_file_retries_transient_send_failure` /
//! `post_index_file_exhausts_retries_and_returns_send_failed` in the `tests`
//! module below, plus the #2798 saturation test
//! `index_files_best_effort_drops_the_batch_when_the_shared_pool_is_saturated`.
//!
//! [`ensure_project_indexed`]: crate::search_index::ensure_project_indexed
//! [`index_files_best_effort`]: crate::search_index::index_files_best_effort
use Path;
// #6864: basename-collision recovery lives in a sibling file so this one stays
// under the 500-SLOC production cap. Same child-module rule as `tests` below.
use CreateOutcome;
/// Find-or-create the trusty-search index for `project_root`, best-effort
/// trigger a reindex so it is actually populated, and return its id (issues
/// #1373, #1908).
///
/// Why: pinning a session/task to an index id is only useful if that index
/// actually exists in the daemon — otherwise a query against it returns nothing
/// and the LLM falls back to guessing (the very bug #1373 fixes). Callers
/// therefore derive the project's canonical index id (the same rule
/// trusty-search's `detect_project` uses, via [`crate::derive_index_id`]) and
/// best-effort register it with the running daemon. The daemon's `POST
/// /indexes` is idempotent (returns `created: false` for an existing id), so a
/// re-register is safe and cheap. Issue #1908: `POST /indexes` alone only
/// registers an EMPTY index and starts a future-changes file watcher — it never
/// walks the existing tree — so a reindex is triggered right after, in the same
/// reachable-daemon branch, sharing one "is the daemon up" check.
///
/// `allow_sensitive_path` (issue #2914): forwarded verbatim to `POST
/// /indexes`' `allow_sensitive_path` field (see
/// [`create_index_request_body`]). Pass `true` ONLY when the caller's
/// `project_root` may legitimately be a deliberately-bound OS-temp path (e.g.
/// tcode's `directory` binding — issue #2747); pass `false` for any caller
/// whose root is always a real, persistent project directory (e.g.
/// trusty-mpm's session workspaces), so an accidental OS-temp root — most
/// commonly a `tempfile`-backed fixture standing in for that workspace in a
/// test — is refused by the daemon's `SENSITIVE_PATH_PREFIXES` denylist
/// instead of silently registered against whatever daemon happens to be
/// discoverable.
/// What: resolves the git-root for `project_root`, derives the index id, and —
/// when the id is non-empty AND the trusty-search daemon address is discoverable
/// — POSTs `{id, root_path, allow_sensitive_path}` to `/indexes` then
/// best-effort triggers a reindex (skipping it when the index is already
/// fresh; see [`best_effort_trigger_reindex`]). Returns the id ONLY when that
/// POST came back 2xx — see [`pinnable_index_id`] for why an unconfirmed create
/// must yield `None`. Errors still never propagate: a refusing or absent daemon
/// is logged at warn and the caller makes progress unindexed.
/// Test: `create_rejected_by_the_daemon_withholds_the_pinnable_id`,
/// `ensure_project_indexed_withholds_id_when_nothing_was_registered`,
/// `ensure_project_indexed_none_for_root`,
/// `ensure_project_indexed_sends_allow_sensitive_path_through_to_create_body`.
/// Per-call knobs for [`ensure_project_indexed_with`] (#5060).
///
/// Why: [`ensure_project_indexed`] grew a second orthogonal dimension when
/// trusty-mpm began registering an index for a git WORKTREE at the moment the
/// worktree is created. A worktree differs from its base checkout by a small
/// diff: its exact text (BM25) and its symbol graph (KG) are branch-specific
/// and must be worktree-accurate, but conceptual similarity is not — it does
/// not change because a branch moved a few functions. So the expensive lane
/// (embedding) is built once on the base checkout and the cheap lanes are
/// built per worktree. A struct rather than a third positional `bool` keeps
/// the two flags from being silently transposed at a call site.
/// What: a plain options bag whose `Default` reproduces the pre-#5060
/// behaviour exactly (`allow_sensitive_path: false`, `skip_vector: false`), so
/// [`ensure_project_indexed`] stays a one-line wrapper and no existing caller
/// changes behaviour.
///
/// `#[non_exhaustive]`: this crate is published, and the daemon already carries
/// a third orthogonal flag this bag does not yet expose (`skip_kg`), so a third
/// field is expected. Without the attribute, adding it would break every
/// external struct-literal construction — a SemVer break this crate has taken
/// before. The attribute bars struct expressions from other crates outright
/// (functional-update syntax does not exempt them), so external callers build
/// with [`IndexOptions::default`] plus the `with_*` setters:
/// `IndexOptions::default().with_skip_vector(true)`.
/// Test: `index_options_default_matches_legacy_ensure_call`,
/// `create_index_request_body_sets_skip_vector`,
/// `index_options_builders_match_field_construction`.
/// What the daemon-side half of an `ensure_project_indexed*` call achieved
/// (#5065 review).
///
/// Why: [`ensure_project_indexed_with`] returns the derived id unconditionally,
/// so its caller cannot tell "the daemon confirmed this index exists" from "the
/// daemon was down and nothing was sent". trusty-mpm's worktree hook was
/// logging `worktree index registered` for the second case — announcing a
/// success it never observed, in the one code path whose stated purpose is to
/// make outcomes distinguishable. Reporting the registration outcome beside the
/// id fixes that without making the call fallible: nothing here ever propagates
/// an error. #5091 then wired this enum into the return rather than leaving it
/// advisory: the id-only entry points hand back an id only when it says
/// `Confirmed` (see [`pinnable_index_id`]), while this report still carries the
/// derived id in every case.
/// What: the four terminal states of the `POST /indexes` attempt. Only
/// `Confirmed` means the index is known to exist daemon-side.
/// Test: `reporting_says_skipped_under_test_harness`,
/// `reporting_says_daemon_unreachable_when_no_daemon_is_discoverable`.
/// The derived index id plus what actually happened daemon-side (#5065 review).
///
/// Why: see [`IndexRegistration`]. A struct rather than a tuple so a third
/// reported quantity can be added without breaking callers.
/// What: `index_id` is `None` only when derivation yielded an empty string, in
/// which case nothing was attempted and `registration` is `NotConfirmed`.
/// Test: `reporting_says_skipped_under_test_harness`.
/// [`ensure_project_indexed`] with explicit per-call [`IndexOptions`] (#5060).
///
/// Why: see [`IndexOptions`]. Kept as a thin wrapper over
/// [`ensure_project_indexed_reporting`] so the register-then-populate sequence
/// can never drift between the session-launch, task-start, and
/// worktree-creation callers.
/// What: identical to [`ensure_project_indexed`] in every respect except that
/// `opts.skip_vector` is threaded into the `POST /indexes` body. Failures are
/// still logged and swallowed rather than propagated, and the returned id is
/// still gated on a confirmed registration ([`pinnable_index_id`]). A caller
/// that needs the DERIVED id whether or not the daemon confirmed it — to name
/// the index in a log line, or to GC it — must use
/// [`ensure_project_indexed_reporting`], where the adjacent `registration`
/// field makes ignoring the failure a visible choice.
/// Test: `create_rejected_by_the_daemon_withholds_the_pinnable_id`,
/// `ensure_project_indexed_none_for_root`,
/// `create_index_request_body_sets_skip_vector`.
/// The id a caller may PIN, or `None` when nothing observed the index (#5091).
///
/// Why: `POST /indexes` can fail — a non-2xx, a transport error, a daemon that
/// is not running — and the id-only entry points used to hand the derived id
/// back anyway. Session launch writes that id into `.mcp.json` as
/// `trusty-search serve --index <id>`, so a create that silently failed left the
/// session pinned to an index the daemon has never heard of: every `search`
/// answers `404 unknown index` for the life of the session, while
/// `search_health` and the `search` doctor probe both stay green because they
/// ask about the daemon, not the pin (#5045 measured 4 of 75 live worktrees
/// actually indexed). Withholding the id leaves the pin unadvanced, which is the
/// one outcome that cannot lie: an unpinned stub is visibly unpinned, and
/// `tm doctor`'s `search_index_pin` check says so.
/// What: returns `report.index_id` for [`IndexRegistration::Confirmed`] — the
/// only variant that means the daemon acknowledged the index, and since `POST
/// /indexes` is find-or-create it covers "already existed" too. Every other
/// variant, including the #4255 test-harness suppression (which sends nothing,
/// so it registers nothing), logs at warn and returns `None`.
/// Test: `create_rejected_by_the_daemon_withholds_the_pinnable_id`,
/// `ensure_project_indexed_withholds_id_when_nothing_was_registered`.
/// [`ensure_project_indexed_with`], but reporting what the daemon actually did
/// (#5065 review).
///
/// Why: the id-only return cannot distinguish a confirmed registration from a
/// silent no-op against a down daemon — see [`IndexRegistration`]. This is the
/// ONE implementation; the two id-only entry points delegate here, so no third
/// copy of the register-then-populate sequence exists.
/// What: resolves the git-root, refuses an unindexable root (#6550), derives
/// the id, and — when the id is non-empty, this is not a test process, and a
/// daemon address resolves — issues the find-or-create `POST /indexes` followed
/// by the freshness-gated reindex trigger. Returns the id in every case except
/// a refusal or empty derivation, alongside the registration outcome. Still
/// fail-open: no step propagates an error.
///
/// The refusal is the one place this function is deliberately NOT best-effort.
/// `resolve_project_root` falls back to the start path when nothing above it is
/// a git repository, so a caller that passed `$HOME` got index `masa` — a
/// plausible id naming the operator, which a later reindex then repointed at a
/// real repository (#6550). Registering the wrong index is worse than
/// registering none, so the root is refused and no id comes back.
/// Test: `reporting_says_skipped_under_test_harness`,
/// `reporting_says_daemon_unreachable_when_no_daemon_is_discoverable`,
/// `ensure_project_indexed_none_for_root`,
/// `ensure_project_indexed_refuses_the_real_home_directory`.
/// Should this process refuse to mutate a real trusty-search daemon?
///
/// Why (issue #4255): both mutating entry points in this module talk to
/// whatever daemon is discoverable on the machine. Under `cargo test` that is
/// the OPERATOR's daemon, so a test exercising a session launch or a task run
/// against a `tempfile` fixture registered that throwaway directory in the
/// live `indexes.toml` — the dead roots then stall warm boot for the timeout,
/// once per entry. Issue #2914 narrowed this by making the temp-dir denylist
/// bypass opt-in, and trusty-code's tests added a per-test
/// `isolate_ambient_daemons()` call, but both leave the safety to whoever
/// writes the next test. The live registry carried five `.tmpXXXXXX` roots
/// proving that was forgotten. Deciding it here, in the shared helper, is the
/// version nobody can forget.
/// What: returns `true` — and logs why — when
/// [`crate::running_under_test_harness`] says this is a test process. A test
/// that genuinely wants the real daemon sets `TRUSTY_ALLOW_PRODUCTION_STATE=1`
/// (see [`crate::test_harness::ALLOW_PRODUCTION_ENV`]). Reads are untouched:
/// this gates only the writes.
/// Test: `ensure_project_indexed_never_writes_to_a_daemon_under_test`,
/// `index_files_inner_never_writes_to_a_daemon_under_test`.
/// Best-effort, non-blocking incremental re-index of specific files into an
/// ALREADY-REGISTERED trusty-search index (mid-task incremental re-indexing).
///
/// Why: [`ensure_project_indexed`] runs once at task start, when a greenfield
/// project is often EMPTY — so `search_code` finds nothing the engineer goes
/// on to write during the task. Re-registering (or fully reindexing) the
/// whole project after every write would mean a full-tree walk per file
/// (expensive); the daemon's per-file `POST /indexes/{id}/index-file`
/// endpoint lets a caller add or update ONE file's chunks cheaply, so the
/// growing codebase stays searchable within the same task.
/// What: submits ONE job to the shared bounded pool ([`crate::index_dispatch`])
/// and returns immediately — the caller (a tool executor mid-turn) must never
/// block or fail because trusty-search is unreachable or slow. On a worker,
/// [`index_files_inner`] derives the same `(root, index_id)`
/// [`ensure_project_indexed`] would (so this always targets the same index a
/// task-start call already created) and POSTs each of `paths` to the daemon. A
/// no-op with zero work submitted when `paths` is empty.
///
/// Saturation (issue #2798): the pool runs at most
/// [`crate::index_dispatch::MAX_INDEX_WORKERS`] batches at once with at most
/// [`crate::index_dispatch::INDEX_QUEUE_CAPACITY`] more queued. A batch
/// submitted when both are full is **DROPPED, not blocked and not queued** —
/// the alternative, blocking the caller, would turn a slow daemon into a
/// stalled agent task. The drop is not silent: it is logged at `warn` naming
/// the file count, the project root, the first path, and the running
/// process-wide drop total — and it is readable as state via
/// [`index_drop_stats`], which trusty-code's `GET /health` publishes so a
/// saturation episode changes the health answer rather than only a log line.
/// Losing an incremental update degrades mid-task search freshness until the
/// next write or reindex covers the file; it does not lose the file, and it
/// does not fail the tool call.
///
/// Sensitive-path note (issue #2747): unlike `POST /indexes`, the per-file
/// `index-file` endpoint does NOT re-run the sensitive-path denylist — it
/// looks the index up by id in the daemon's in-memory registry
/// (`crates/trusty-search/src/service/server/files.rs`'s `index_file_handler`
/// calls `state.registry.get(&index_id)`, never `allowlist::is_denied`), so
/// an index created under the #2747 `allow_sensitive_path` bypass (a tempdir
/// root) accepts incremental updates unconditionally. No bypass flag is
/// threaded through here because none is needed.
/// Test: `index_files_best_effort_drops_the_batch_when_the_shared_pool_is_saturated`
/// covers the submit/reject half; the work itself is [`index_files_inner`],
/// which the `index_files_inner_*` tests below exercise directly
/// (synchronously, off any worker) for determinism.
/// How many incremental index batches this process has dropped, and when the
/// last one happened (#2798 review).
///
/// Why: the bound is only acceptable because the loss it creates is visible.
/// A `warn!` line nobody greps is not visibility, so this is the read surface a
/// health check consumes — trusty-code's `GET /health` publishes it as
/// `incremental_index`.
///
/// The four fields are two pairs, and both pairs are needed. Within a pair, the
/// count says whether the loss has EVER happened and the age says whether it is
/// happening NOW — a monotonic total alone cannot distinguish a wedged daemon
/// right now from one episode an hour ago. Between the pairs, a DROP means the
/// pool refused the batch outright and none of it ran, while a TRUNCATION means
/// the pool accepted and started the batch and then
/// [`BATCH_INDEX_BUDGET`] cut it short partway. Different causes, different
/// fixes, so they are never summed: an episode where every batch is accepted
/// and then truncated leaves files unindexed while `dropped_batches` reads `0`
/// forever.
/// What: both counts are monotonic for the life of the process; each age is
/// `None` until that loss first happens, then the age of the most recent one
/// (saturating at 0 if the wall clock moved backwards). All four read the
/// shared pool, so they cover every caller in the process.
/// Test: `index_files_best_effort_drops_the_batch_when_the_shared_pool_is_saturated`
/// (asserts the drop pair right after a real drop),
/// `a_truncated_batch_is_counted_separately_from_a_dropped_one`,
/// `a_fresh_pool_reports_no_drop_ever`.
/// Snapshot the shared pool's loss counters — see [`IndexDropStats`].
///
/// Test: `index_files_best_effort_drops_the_batch_when_the_shared_pool_is_saturated`,
/// `a_truncated_batch_is_counted_separately_from_a_dropped_one`.
/// Age in seconds of a unix-second stamp, saturating at 0 if the clock moved
/// backwards.
/// Wall-clock budget one batch may spend indexing before it stops early.
///
/// Why (#2798 review): a job is a whole `write_files` BATCH, and that tool caps
/// nothing — a scaffold write is one job. At [`MAX_INDEX_ATTEMPTS`]'s ~6.2s
/// worst case per file against a degraded daemon, a 30-file batch would hold
/// one of the four workers for over three minutes, and the queue-depth
/// reasoning behind [`crate::index_dispatch::INDEX_QUEUE_CAPACITY`] collapses.
/// Capping the batch in TIME is what makes worker turnover derivable: no job
/// occupies a worker for more than this budget plus the one file already in
/// flight (~36s), so a full 64-slot queue drains in ~10 minutes worst case
/// rather than an unbounded time.
/// What: 30s, checked before each file — never mid-request, so an in-flight
/// POST always finishes. Files the batch had not reached when the budget ran
/// out are abandoned; the loss is counted as
/// [`IndexDropStats::truncated_batches`], separately from a pool rejection.
/// Test: `batch_budget_is_exhausted_at_and_past_the_cap`.
pub const BATCH_INDEX_BUDGET: Duration = from_secs;
/// Has this batch spent its [`BATCH_INDEX_BUDGET`]?
///
/// Test: `batch_budget_is_exhausted_at_and_past_the_cap`.
/// Should this batch stop here? Counts and logs the truncation when it should.
///
/// Why: the decision and the accounting are one function so a batch cannot stop
/// without being counted. When the `break` only logged, a sustained episode in
/// which every batch was accepted and then truncated reported
/// `dropped_batches: 0` in `GET /health` for as long as it lasted, while files
/// went unindexed batch after batch — the same single-reader blind spot the
/// rejection counter was added to close, reintroduced on the other loss path.
/// Splitting "decide" from "record" is what would let it come back.
/// What: returns `true` once [`batch_budget_exhausted`], and on that edge
/// records the truncation on `pool` (readable as
/// [`IndexDropStats::truncated_batches`], distinct from a drop) and warns with
/// how many of the batch's files were reached and how many are abandoned.
/// Returns `false` and records nothing while the budget holds. `pool` is a
/// parameter rather than a reach for [`crate::index_dispatch::global`] so the
/// negative case can assert an untouched counter absolutely, on an isolated
/// pool, instead of a delta against a process-wide one a sibling test also
/// writes — the same reason [`crate::index_dispatch::BoundedDispatcher`]'s own
/// tests build small instances rather than racing the shared pool.
/// Test: `a_truncated_batch_is_counted_separately_from_a_dropped_one`,
/// `an_unexhausted_budget_records_no_truncation`.
/// Synchronous body of [`index_files_best_effort`], run on a pool worker (or
/// called directly by tests for determinism).
///
/// Why: split out so tests can exercise the fail-open branches (empty index
/// id, undiscoverable daemon) synchronously, without waiting on — or racing
/// — a spawned thread.
/// What: derives `(root, index_id)` via [`crate::resolve_project_root`] /
/// [`crate::derive_index_id`]; returns early (logged at debug) when the id is
/// empty or [`crate::resolve_daemon_base_url`] finds no running daemon;
/// otherwise builds ONE pooled HTTP client for the whole batch (issue #2785:
/// so multiple files in a `write_files` batch reuse keep-alive connections
/// instead of a fresh TCP connect per file) and, for each path, resolves it
/// against `root`, reads its current content from disk (an unreadable file —
/// e.g. deleted since the write — is logged at debug and skipped, not fatal to
/// the batch), and POSTs it via [`best_effort_index_one_file`] (which itself
/// retries transient send failures with backoff). Every step fails open. The
/// loop also stops early once [`BATCH_INDEX_BUDGET`] is spent (#2798) — a batch
/// has no size limit, so without that a single large write pins a pool worker
/// for minutes. Stopping goes through [`stop_batch_for_budget`], which counts
/// the truncation into [`index_drop_stats`] as well as logging it; the files it
/// had not reached are abandoned, never retried from here.
/// Test: `index_files_inner_is_noop_for_empty_paths`,
/// `index_files_inner_skips_when_index_id_empty`,
/// `index_files_inner_skips_gracefully_when_daemon_down`,
/// `index_files_inner_refuses_the_real_home_directory`.
/// Resolve `abs` to the path string the corpus stores for a file under `root`.
///
/// Why: the reindex walker stores every chunk's `file` field relative to the
/// index root (`crates/trusty-search/src/service/walker.rs` strips the
/// canonical root prefix); posting an absolute path here would create a
/// duplicate, differently-keyed corpus entry for the same file instead of
/// updating the one the walker already produced.
/// What: strips `root` as a prefix and forward-slash-normalises the
/// remainder; falls back to `abs` itself (lossy) when it does not live under
/// `root` — should not happen for a working-directory-scoped tool write, but
/// fails safe rather than panicking or silently dropping the update.
/// Test: `relative_index_path_strips_root_prefix`,
/// `relative_index_path_falls_back_for_paths_outside_root`.
/// Build the pooled blocking HTTP client used for incremental index updates.
///
/// Why: extracted so [`index_files_inner`] builds exactly ONE client per batch
/// (issue #2785 connection reuse) and so the retry test can construct an
/// identically-configured client.
/// What: a `reqwest::blocking::Client` with a 2s overall / 750ms connect
/// timeout — tight caps because this runs on a mid-task detached thread and
/// must never stall a long task when the daemon is slow. reqwest maintains an
/// idle-connection pool per client, so reusing the returned client across a
/// batch's files amortises TCP/handshake setup.
/// Test: covered indirectly by `post_index_file_retries_transient_send_failure`
/// (which builds and drives one), and by the daemon-down fail-open path in
/// `index_files_inner_skips_gracefully_when_daemon_down`.
/// Max attempts (initial try + retries) for a single per-file index POST.
///
/// Why: issue #2785 — under sustained mid-task load the per-file HTTP call sees
/// transient send failures (connection resets / connect races under rapid
/// repeated writes). A tiny bounded retry recovers the vast majority of them.
/// What: 3 total attempts.
/// Latency note: the SLEEP this adds beyond a single attempt is only
/// [`retry_backoff`]'s sum (~200ms across 3 attempts) — cheap when failures
/// are the fast connect-refused/reset kind this fix targets. But that is NOT
/// the worst-case TOTAL latency: each attempt still carries
/// [`build_index_client`]'s own per-call timeout (2s overall / 750ms connect),
/// and a *slow-but-reachable* daemon can consume the full 2s on every attempt
/// before erroring or hanging up. Worst case against such a daemon is
/// therefore ~3 × 2s + ~200ms backoff ≈ **6.2s for a single file**, on the
/// batch's detached thread — never on the tool-executor's return path, but
/// worth knowing before shrinking timeouts or raising `MAX_INDEX_ATTEMPTS`.
/// Test: `retry_backoff_is_bounded_and_increasing`.
const MAX_INDEX_ATTEMPTS: u32 = 3;
/// Backoff to sleep BEFORE retry `attempt` (1-based) of a per-file index POST.
///
/// Why: a transient send failure under load often clears within tens of
/// milliseconds once the daemon drains the burst; a short exponential backoff
/// spaces retries without materially slowing the task. Kept as a pure function
/// so the schedule is unit-testable without any I/O.
/// What: `50ms * 3^(attempt-1)`, capped at 1s — i.e. 50ms before the 2nd try,
/// 150ms before the 3rd. Saturating arithmetic keeps it panic-free for any
/// `attempt`.
/// Test: `retry_backoff_is_bounded_and_increasing`.
/// Outcome of a per-file index POST, surfaced so tests can assert the
/// retry-then-succeed AND retry-exhaustion paths without scraping logs.
///
/// Why: [`post_index_file_with_retries`] is otherwise pure I/O; returning a
/// small enum lets tests prove both that a transient send failure is retried
/// and ultimately succeeds, and that persistent failure is reported (not
/// silently hung or panicked) once attempts are exhausted.
/// What: `Indexed` (2xx), `HttpStatus` (non-2xx — not retried; a 4xx/404 for an
/// unknown index won't fix itself), or `SendFailed` (transport error on every
/// attempt).
/// Test: `post_index_file_retries_transient_send_failure`,
/// `post_index_file_exhausts_retries_and_returns_send_failed`.
/// POST a single file's `{path, content}` to `url`, retrying transient send
/// failures with [`retry_backoff`] up to [`MAX_INDEX_ATTEMPTS`] times.
///
/// Why: issue #2785 — a single transport-level `send()` failure (connection
/// reset/connect race under rapid concurrent writes) previously dropped the
/// update entirely. Retrying transport errors (but NOT HTTP non-2xx, which
/// will not self-heal) recovers those transient failures.
/// What: reuses the caller-supplied pooled `client`; on a transport `Err` it
/// sleeps [`retry_backoff`] and retries (until attempts are exhausted → returns
/// `SendFailed`); a 2xx returns `Indexed` immediately; any other status returns
/// `HttpStatus` immediately (no retry). Never panics, never propagates. See
/// [`MAX_INDEX_ATTEMPTS`]'s doc comment for the latency distinction between
/// the ~200ms of added backoff SLEEP and the much larger (~6.2s) worst-case
/// TOTAL wall time this function can spend against a slow-but-up daemon,
/// since each of the 3 attempts carries its own 2s/750ms client timeout.
/// Test: `post_index_file_retries_transient_send_failure`,
/// `post_index_file_exhausts_retries_and_returns_send_failed`.
/// POST `/indexes/{id}/index-file` for a single file; failures are logged,
/// never propagated.
///
/// Why: mirrors [`best_effort_create_index`]'s fail-open contract for the
/// per-file endpoint, hardened for issue #2785 (retry + connection reuse).
/// What: delegates to [`post_index_file_with_retries`] using the pooled
/// `client` [`index_files_inner`] built once for the batch (so rapid writes
/// reuse keep-alive connections). Unlike [`best_effort_create_index`], this
/// does NOT spawn-and-join its own nested OS thread: it is only ever reached
/// from inside [`index_files_inner`] running on a [`crate::index_dispatch`]
/// pool worker (submitted by [`index_files_best_effort`]), a plain
/// `std::thread` that is already off any tokio runtime, so a
/// direct blocking call here cannot trigger the "cannot drop a runtime in a
/// context where blocking is not allowed" panic. A non-2xx response (including
/// 404 for an unregistered/unknown index — e.g. the daemon restarted since task
/// start) is logged at warn; a transport error surviving all retries is logged
/// at warn. Both are swallowed.
/// Test: exercised via `index_files_inner_skips_gracefully_when_daemon_down`
/// (daemon-down path, never reaches this function) and
/// `post_index_file_retries_transient_send_failure` (retry path); the live HTTP
/// success path is covered by integration use.
/// Build the JSON body for the `POST /indexes/{id}/index-file` call.
///
/// Why: extracted so the request shape is unit-testable without a live
/// daemon or a spawned thread — mirrors [`create_index_request_body`].
/// What: `{path, content}` — the exact shape the per-file endpoint's
/// `IndexFileRequest` expects (`crates/trusty-search/src/service/server/router.rs`).
/// No `allow_sensitive_path` field: see [`index_files_best_effort`]'s doc
/// comment for why the per-file endpoint needs no such opt-in.
/// Test: `index_file_request_body_targets_relative_path_and_content`.
/// Build the JSON body for the `POST /indexes` find-or-create call.
///
/// Why: extracted from `best_effort_create_index` so the request shape —
/// specifically, whether `allow_sensitive_path` is set — is unit-testable
/// without a live daemon or a spawned thread.
/// What: `allow_sensitive_path` (explicit-index-sensitive-path-bypass) is
/// forwarded verbatim from the caller (issue #2914 — it is NOT unconditionally
/// `true` any more). When `true`, this is the "explicit request" case the
/// daemon-side flag exists for: it lets trusty-search index a bake-off scratch
/// project living under an OS-temp prefix (e.g. `/var/folders/…`) instead of
/// hard-rejecting it with 400 (issue #2747 — tcode's `directory` binding).
/// When `false`, an OS-temp root (most commonly an accidental `tempfile`
/// fixture standing in for a real project in a test) is refused by the
/// daemon's `SENSITIVE_PATH_PREFIXES` denylist instead of silently registered.
/// Harmless either way for ordinary project roots (trusty-mpm worktrees,
/// checked-out repos): none of those live under `SENSITIVE_PATH_PREFIXES`, so
/// the flag is a no-op for them. It never bypasses the OTHER denylist checks
/// (credential dirs, sensitive file names, top-level home dirs) — see
/// `trusty-search::allowlist::is_denied_allowing_sensitive_path`'s doc comment
/// for exactly what stays enforced.
///
/// `skip_vector` (#5060) asks the daemon to register the index with its vector
/// lane permanently suppressed — see [`IndexOptions::skip_vector`]. It is sent
/// unconditionally (as `false` for every pre-#5060 caller) because the
/// daemon's `CreateIndexRequest` field is `Option<bool>` with `None` and
/// `Some(false)` both meaning "build the vector lane": an explicit `false` is
/// byte-for-byte equivalent to omitting it, and keeping the field present
/// makes the request shape uniform across callers.
/// Test: `create_index_request_body_respects_allow_sensitive_path_param`,
/// `create_index_request_body_sets_skip_vector`.
/// Extract the tree a `POST /indexes` response says the index is registered at,
/// but ONLY when the daemon reported it did not create anything.
///
/// Why: `created: true` means the daemon adopted the root that was just sent, so
/// there is nothing to cross-check. `created: false` means an entry already
/// existed, and THAT is the case where the registered tree can differ from the
/// requested one. Returning `None` for every other shape keeps the caller's
/// behaviour identical against a daemon too old to report `root_path` — the
/// check strengthens the verdict where it can and never invents a failure where
/// it cannot.
/// What: parses `body` as JSON and returns `root_path` when it is a string and
/// `created` is exactly `false`. Any parse failure, absent field, or wrong type
/// yields `None`.
/// Test: `registered_root_from_response_reads_the_already_exists_root`,
/// `registered_root_from_response_ignores_a_fresh_create`,
/// `registered_root_from_response_tolerates_a_daemon_that_omits_it`.
/// POST `/indexes` to find-or-create `index_id`; failures are logged, never
/// propagated (issue #1373).
///
/// Why: registration is best-effort — a daemon that is briefly unreachable, or
/// an HTTP hiccup, must NOT abort the caller. Isolating the blocking HTTP call
/// here keeps [`ensure_project_indexed`] readable and the error handling in one
/// place.
/// What: issues a short-timeout blocking `POST {base}/indexes` with body
/// `{id, root_path, allow_sensitive_path}` (built by
/// [`create_index_request_body`]) ON A DEDICATED OS THREAD. Callers are
/// frequently inside a tokio runtime; creating `reqwest::blocking`'s internal
/// runtime directly there panics with "Cannot drop a runtime in a context
/// where blocking is not allowed". Running the blocking client on a
/// freshly-spawned `std::thread` (joined here) keeps that nested runtime
/// entirely off the async worker, so the call is safe from both sync and
/// async callers. A non-2xx response or transport error is logged at
/// warn/debug and swallowed; the daemon endpoint is idempotent so re-creates
/// are harmless. The client uses a tight ~1s overall timeout (750 ms connect)
/// so the joined thread returns quickly: this call sits on a hot path and
/// must NOT stall when the daemon is slow or unreachable.
///
/// Returns [`CreateOutcome::Confirmed`] ONLY for a 2xx response (#5065
/// review): a non-2xx other than `409`, a transport error, and a panicked worker
/// thread are all `NotConfirmed`. They are still logged and swallowed — the
/// return value gives the caller something honest to report, it does not make
/// the call fallible.
///
/// A 2xx is no longer sufficient on its own. The daemon answers a find-or-create
/// for an id it already holds with `200 {created: false}`, and reading only the
/// status made that indistinguishable from a real create — so a caller whose
/// tree differed from the registered one was told the registration succeeded and
/// then had every query answered from the OTHER tree, with no error and no
/// warning. The response now carries the registered `root_path` and a mismatch
/// downgrades the verdict to [`CreateOutcome::Conflict`].
///
/// #6864: a `409` is that same conflict said out loud — the daemon refuses to
/// re-register an id over a second tree (`root_path_mismatch_response`), and
/// refuses a second id over one tree (`root_path_collision_response`, which
/// names the owning `existing_id`). Both are reported as `Conflict` rather than
/// `NotConfirmed` because an index for the requested tree may well exist under
/// another id; [`reconcile::create_and_reconcile`] is what goes and finds it.
/// Test: `ensure_project_indexed_withholds_id_when_nothing_was_registered`
/// (daemon-down path), `registered_root_from_response_*` (the body contract),
/// `create_index_response_for_a_different_tree_reports_a_conflict` (the 2xx
/// conflict arm), `registration_matches_an_existing_index_by_root_path` (the
/// `409` arm, end to end).
/// Best-effort, non-blocking trigger of a trusty-search reindex for `index_id`
/// (issue #1908).
///
/// Why: [`best_effort_create_index`] only find-or-creates an EMPTY index — the
/// daemon's `POST /indexes` handler registers the id and starts a
/// future-changes file watcher but never walks the existing tree. Without an
/// explicit reindex trigger, a freshly registered index stays empty until
/// *something* changes on disk, so the very first `search`/`grep` query silently
/// returns nothing. `POST /indexes/{id}/reindex` is fire-and-forget server-side
/// — it `tokio::spawn`s the walk and returns almost instantly — so triggering it
/// here does not risk a long stall; the short dedicated-thread timeout guards
/// the (much rarer) case where even the initial HTTP round trip is slow.
/// What: on a dedicated OS thread (mirroring [`best_effort_create_index`]) with
/// a ~2s overall / 750ms connect timeout: first does a cheap `GET
/// {base}/indexes/{id}/status` freshness probe (see [`index_is_fresh`]) and
/// skips the reindex entirely when the index already has chunks and was indexed
/// within the last hour; otherwise POSTs `{base}/indexes/{id}/reindex`. A failed
/// status probe is treated as "not fresh" (fail-open toward reindexing). Every
/// outcome — skipped, triggered, non-2xx, transport error, panicked thread — is
/// logged at warn/debug and swallowed; the daemon-side reindex is itself
/// idempotent, so calling it redundantly is harmless, and the caller must never
/// block or fail because trusty-search is unreachable or slow.
/// Test: `index_is_fresh_true_when_recently_indexed_with_chunks`,
/// `index_is_fresh_false_when_no_chunks`, `index_is_fresh_false_when_stale`,
/// `index_is_fresh_false_when_last_indexed_missing_or_malformed`; the live-HTTP
/// trigger path is exercised the same way `best_effort_create_index` is
/// (daemon-down graceful path via
/// `ensure_project_indexed_withholds_id_when_nothing_was_registered`).
/// Whether a `GET /indexes/{id}/status` response body represents an index
/// fresh enough that [`best_effort_trigger_reindex`] should skip reindexing
/// (issue #1908).
///
/// Why: pure predicate over the JSON body so the freshness rule is unit
/// testable without a live daemon — [`best_effort_trigger_reindex`] is
/// otherwise pure I/O. Skipping redundant reindexes avoids reindex spam on
/// every launch/run of an already-fresh workspace.
/// What: returns `true` when `chunk_count` is a positive integer AND
/// `last_indexed` parses as an RFC3339 timestamp no more than one hour in the
/// past (clock skew that makes it appear in the future is also treated as not
/// fresh, out of caution). Any missing/malformed/zero field returns `false`
/// (fail-open toward reindexing, never toward skipping).
/// Test: `index_is_fresh_true_when_recently_indexed_with_chunks`,
/// `index_is_fresh_false_when_no_chunks`, `index_is_fresh_false_when_stale`,
/// `index_is_fresh_false_when_last_indexed_missing_or_malformed`.
// Tests are in a sibling file to keep this file under the 500-SLOC production
// cap (issue #2914 split). The submodule can access private items via
// `super::` (Rust child-module rule).