@@ -260,6 +260,15 @@ struct cbm_pipeline {
260260 * "invalid_existing_db" (#1864). */
261261 bool final_existed ;
262262 bool existing_generation ;
263+ /* The copy into the stage is deferred until routing needs it: an
264+ * unchanged repository is proven current on the live generation
265+ * (live_db_path, read-only) and never copies, seals or renames a
266+ * database. backup_succeeded records the copy once it ran; live_noop
267+ * marks a run that ended on the live proof with nothing staged. */
268+ bool stage_backup_pending ;
269+ bool backup_succeeded ;
270+ bool live_noop ;
271+ const char * live_db_path ; /* borrowed from cbm_pipeline_run */
263272
264273 /* ADR (project_summaries) captured before a full-reindex DB delete, so it
265274 * can be restored after the rebuild. NULL when no ADR existed. Issue #516. */
@@ -848,7 +857,10 @@ cbm_pipeline_t *cbm_pipeline_new(const char *repo_path, const char *db_path,
848857 p -> repo_path = strdup (repo_path );
849858 p -> db_path = db_path ? strdup (db_path ) : NULL ;
850859 p -> project_name = cbm_project_name_from_path (repo_path );
851- (void )cbm_git_context_resolve (repo_path , & p -> git_ctx );
860+ /* The Git context is resolved once, when the run starts
861+ * (pipeline_refresh_git_context): a context resolved here was always
862+ * replaced before any use, at the cost of seven git subprocesses per
863+ * index call. Until then the branch QN names the working tree. */
852864 p -> branch_qn = cbm_git_context_branch_qn (p -> project_name , & p -> git_ctx );
853865 p -> requested_mode = mode ;
854866 p -> mode = mode ;
@@ -2738,12 +2750,69 @@ static int capture_existing_adr(cbm_pipeline_t *p, const char *db_path) {
27382750 return 0 ;
27392751}
27402752
2753+ static int remove_stage_files (const char * stage_path );
2754+
2755+ /* Copy the live generation into this run's stage: every route except the
2756+ * live no-op works on that copy. A failed copy behaves as the eager copy it
2757+ * replaces did: the stage files go (the name and its lock stay this run's,
2758+ * the rebuilt generation is renamed over it later) and the run has no
2759+ * previous generation, so it rebuilds in full. */
2760+ static void materialize_stage_backup (cbm_pipeline_t * p ) {
2761+ p -> stage_backup_pending = false;
2762+ p -> backup_succeeded = cbm_store_backup_path (p -> live_db_path , p -> db_path ) == CBM_STORE_OK ;
2763+ if (!p -> backup_succeeded ) {
2764+ cbm_log_warn ("pipeline.stage" , "action" , "backup_failed_full_rebuild" , "path" ,
2765+ p -> live_db_path );
2766+ (void )remove_stage_files (p -> db_path );
2767+ }
2768+ p -> existing_generation = p -> backup_succeeded ;
2769+ }
2770+
2771+ /* The live generation is provably current: read-only, it passes the shallow
2772+ * integrity and format checks the staged route applies to its copy, and the
2773+ * exact semantic-manifest test the staged route would answer no-op with. A
2774+ * copy of it would route to that no-op and publish the same database again,
2775+ * so the run ends here with nothing copied, sealed or renamed. Any doubt
2776+ * answers false and the run copies and routes exactly as before. */
2777+ static bool live_generation_current (cbm_pipeline_t * p , const cbm_file_hash_t * baseline_manifest ,
2778+ int baseline_count ) {
2779+ struct stat db_st ;
2780+ if (!p -> live_db_path || stat (p -> live_db_path , & db_st ) != 0 ) {
2781+ return false;
2782+ }
2783+ cbm_store_t * store = cbm_store_open_path_query (p -> live_db_path );
2784+ if (!store ) {
2785+ return false;
2786+ }
2787+ int fmt = 0 ;
2788+ bool current = cbm_store_check_integrity (store );
2789+ if (current ) {
2790+ cbm_store_get_format_version (store , & fmt );
2791+ current =
2792+ fmt == CBM_INDEX_FORMAT_VERSION &&
2793+ cbm_pipeline_incremental_generation_exact (p , store , baseline_manifest , baseline_count );
2794+ }
2795+ cbm_store_close (store );
2796+ return current ;
2797+ }
2798+
27412799/* Route an existing generation. Full rebuilds never delete the live DB here:
27422800 * publication owns the eventual atomic replacement after every pass and
27432801 * metadata write has succeeded. */
27442802static int try_incremental_or_delete_db (cbm_pipeline_t * p , cbm_file_info_t * files , int file_count ,
27452803 const cbm_file_hash_t * baseline_manifest ,
27462804 int baseline_count , bool force_full_on_mismatch ) {
2805+ if (p -> stage_backup_pending ) {
2806+ if (live_generation_current (p , baseline_manifest , baseline_count )) {
2807+ p -> stage_backup_pending = false;
2808+ p -> live_noop = true;
2809+ cbm_log_info ("pipeline.route" , "path" , "incremental_manifest" , "generation" , "live" );
2810+ cbm_log_info ("incremental.noop" , "reason" , "semantic_manifest_equal" , "generation" ,
2811+ "live" );
2812+ return 0 ;
2813+ }
2814+ materialize_stage_backup (p );
2815+ }
27472816 if (!p -> existing_generation ) {
27482817 /* Nothing to be incremental against: a first index, or a
27492818 * destination that could not be copied (already reported as
@@ -2846,12 +2915,15 @@ static bool promote_mode_to_existing_coverage(cbm_pipeline_t *p) {
28462915 if (!p || !p -> project_name ) {
28472916 return false;
28482917 }
2849- char * db_path = resolve_db_path (p );
2918+ /* Until routing copies it, the stage is an empty placeholder: the
2919+ * coverage the copy would carry is read from the live generation. */
2920+ char * resolved = p -> stage_backup_pending ? NULL : resolve_db_path (p );
2921+ const char * db_path = p -> stage_backup_pending ? p -> live_db_path : resolved ;
28502922 if (!db_path ) {
28512923 return false;
28522924 }
28532925 cbm_store_t * store = cbm_store_open_path_query (db_path );
2854- free (db_path );
2926+ free (resolved );
28552927 if (!store ) {
28562928 return false;
28572929 }
@@ -4542,6 +4614,29 @@ static void sweep_orphan_stages(const char *final_path) {
45424614 free (dir_path );
45434615}
45444616
4617+ /* End a run whose exact no-op was proven on the live generation. Nothing was
4618+ * staged, so nothing is sealed or renamed and the live database stays the
4619+ * current generation. The checks publication makes on the run's sources and
4620+ * destination still decide success, and the artifact is refreshed from the
4621+ * live database exactly as it would be from a republished one. */
4622+ static int finish_live_noop (cbm_pipeline_t * p , const char * final_path ) {
4623+ cbm_userconfig_t * fresh_config = cbm_userconfig_load_with_source (p -> repo_path );
4624+ bool source_matches = cbm_pipeline_project_source_matches (p , fresh_config );
4625+ cbm_userconfig_free (fresh_config );
4626+ struct stat final_st ;
4627+ int rc = 0 ;
4628+ if (!source_matches ) {
4629+ cbm_log_error ("pipeline.err" , "phase" , "test_declarations_source_changed" );
4630+ rc = CBM_PIPELINE_ABORT_PRESERVE_DB ;
4631+ } else if (stat (final_path , & final_st ) != 0 ) {
4632+ /* The destination vanished during the run: someone else owns it. */
4633+ rc = CBM_PIPELINE_ABORT_PRESERVE_DB ;
4634+ } else {
4635+ rc = export_after_publish (p , final_path );
4636+ }
4637+ return rc ;
4638+ }
4639+
45454640int cbm_pipeline_run (cbm_pipeline_t * p ) {
45464641 /* Per-index attribution: peaks and phase totals are about THIS index, not
45474642 * the process history, so they start clean here. The first mark opens
@@ -4571,21 +4666,12 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
45714666 return CBM_NOT_FOUND ;
45724667 }
45734668
4574- bool backup_succeeded = false;
4575- if (final_existed ) {
4576- backup_succeeded = cbm_store_backup_path (final_path , staging_path ) == CBM_STORE_OK ;
4577- if (!backup_succeeded ) {
4578- cbm_log_warn ("pipeline.stage" , "action" , "backup_failed_full_rebuild" , "path" ,
4579- final_path );
4580- /* The copy is gone but the NAME stays ours: the rebuilt
4581- * generation is renamed over it by the inner finalize and then
4582- * published from it below, so its lock is held to the end. */
4583- remove_stage_files (staging_path );
4584- }
4585- }
4586-
4669+ /* The existing generation is copied into the stage only when routing
4670+ * needs a copy (materialize_stage_backup): an exact no-op is proven on
4671+ * the live generation and copies nothing -- on a kernel-scale index the
4672+ * copy alone was a 19 GB write per unchanged re-index. */
45874673 p -> final_existed = final_existed ;
4588- p -> existing_generation = final_existed && backup_succeeded ;
4674+ p -> existing_generation = final_existed ;
45894675 char * configured_db_path = p -> db_path ;
45904676 p -> db_path = strdup (staging_path );
45914677 if (!p -> db_path ) {
@@ -4595,10 +4681,19 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
45954681 free (final_path );
45964682 return CBM_NOT_FOUND ;
45974683 }
4684+ p -> stage_backup_pending = final_existed ;
4685+ p -> backup_succeeded = false;
4686+ p -> live_noop = false;
4687+ p -> live_db_path = final_path ;
45984688 cbm_log_debug ("pipeline.setup" , "step" , "staged" );
45994689 int rc = cbm_pipeline_run_staged (p );
46004690 free (p -> db_path );
46014691 p -> db_path = configured_db_path ;
4692+ bool backup_succeeded = p -> backup_succeeded ;
4693+ bool live_noop = p -> live_noop ;
4694+ p -> stage_backup_pending = false;
4695+ p -> live_noop = false;
4696+ p -> live_db_path = NULL ;
46024697
46034698 /* Report WHY the run stopped. Everything below happens before the final
46044699 * rename, so the live database is still the previous generation and an
@@ -4612,12 +4707,17 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
46124707 pipeline_release_test_config (p );
46134708 return rc ;
46144709 }
4615- if (check_cancel (p )) {
4710+ /* A cancelled run and a live no-op both end here without publishing:
4711+ * the stage placeholder is discarded, and only the no-op goes on to
4712+ * finish on the live generation. */
4713+ bool cancelled = check_cancel (p );
4714+ if (cancelled || live_noop ) {
46164715 cleanup_staging_db (staging_path );
46174716 free (staging_path );
4717+ rc = cancelled ? CBM_PIPELINE_ABORT_PRESERVE_DB : finish_live_noop (p , final_path );
46184718 free (final_path );
46194719 pipeline_release_test_config (p );
4620- return CBM_PIPELINE_ABORT_PRESERVE_DB ;
4720+ return rc ;
46214721 }
46224722 if (seal_staging_db (staging_path ) != 0 ) {
46234723 cleanup_staging_db (staging_path );
0 commit comments