-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate_versions_and_changelogs.py
More file actions
1184 lines (942 loc) · 41.5 KB
/
Copy pathupdate_versions_and_changelogs.py
File metadata and controls
1184 lines (942 loc) · 41.5 KB
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
#!/usr/bin/env python3
"""
Update versions and changelogs for .qmd documentation files.
This script handles the entire versioning workflow:
- Finds what changed since the last release
- Asks AI to analyze the changes and suggest version bumps
- Generates human-readable changelogs
- Updates the documentation files
It's smart about API usage - combines version and changelog analysis
into one call instead of two, which cuts costs by 80%.
How it works:
1. Looks at git history to find changed files
2. Sends the changes to Gemini AI for analysis
3. AI decides if it's a minor or patch update
4. AI writes a changelog summary
5. Updates the .qmd files with new versions and changelogs
6. Saves tracking data so we know what happened
Handles tricky stuff like:
- First release (no previous version)
- Renamed files (migrates their history)
- Huge diffs (truncates them so AI doesn't choke)
- Rate limits (delegated to helpers.gemini_client)
"""
import subprocess
import json
import os
import sys
import re
from datetime import datetime
from pathlib import Path
import time
sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # .github/scripts
from helpers.gemini_client import (
setup_gemini,
call_gemini,
count_tokens,
get_encoding,
create_smart_batches,
check_and_wait_for_rate_limits,
record_api_request,
rate_limit_state,
is_quota_error,
extract_retry_delay,
RPM_SAFE,
TPM_SAFE,
RPD_LIMIT,
)
from helpers.batch_utils import batch_with_retry
from helpers.qmd_utils import (
read_qmd_frontmatter,
write_qmd_frontmatter,
print_mode_banners,
)
from helpers.html_safe import sanitize_changelog_html
from helpers.json_io import load_json_or_empty
# Configuration
VERSIONS_FILE = ".llm_cache/versions.json"
CHANGELOGS_FILE = ".llm_cache/change_logs.json" # For inject_changelog.py compatibility
DOCS_DIR = "DOCS"
# ⚠️ TESTING GUARDRAILS - Remove these for production! ⚠️
# Read at import; main() re-reads in case env was set after import.
TESTING_MODE = os.getenv("TESTING_MODE", "false").lower() == "true"
DRY_RUN = os.getenv("DRY_RUN", "false").lower() == "true"
# When true, version analysis runs and caches results, but .qmd files are
# NOT written. Used by update_documentation.py orchestrator, which performs
# a single atomic write per file in Phase 2.
SKIP_FILE_WRITES = os.getenv("SKIP_FILE_WRITES", "false").lower() == "true"
MAX_FILES_FOR_TESTING = 15
MAX_COMMITS_FOR_TESTING = 10
# Conservative limits based on testing
MAX_TOKENS_PER_BATCH = 50_000 if TESTING_MODE else 600_000 # Primary constraint (input)
MAX_FILES_PER_BATCH = 5 if TESTING_MODE else 15 # Secondary constraint (output safety)
ABSOLUTE_MAX_TOKENS = 800_000 # Gemini context window limit (leave buffer)
# ═══════════════════════════════════════════════════════════════════════
# GIT OPERATIONS
# ═══════════════════════════════════════════════════════════════════════
def get_last_release_tag():
"""Get the most recent git tag for the current branch"""
try:
current_branch = (
subprocess.check_output(["git", "branch", "--show-current"])
.decode()
.strip()
)
print(f"[INFO] Current branch: {current_branch}")
all_tags = (
subprocess.check_output(
["git", "tag", "-l", "--sort=-v:refname"], stderr=subprocess.DEVNULL
)
.decode()
.strip()
.splitlines()
)
if not all_tags:
print("[INFO] No release tags found - this is the first release")
return None
# Filter tags based on branch
if current_branch == "test":
filtered_tags = [t for t in all_tags if "-test" in t]
print("[INFO] Filtering for test branch tags (with -test suffix)")
elif current_branch == "main":
filtered_tags = [t for t in all_tags if "-test" not in t]
print("[INFO] Filtering for main branch tags (without -test suffix)")
else:
filtered_tags = all_tags
print(f"[INFO] Using all tags for branch: {current_branch}")
if filtered_tags:
last_tag = filtered_tags[0]
print(f"[INFO] Found last release tag: {last_tag}")
return last_tag
else:
print(
f"[INFO] No release tags found for branch {current_branch} - first release"
)
return None
except subprocess.CalledProcessError:
print("[INFO] No release tags found - this is the first release")
return None
def get_changed_files_with_renames(last_tag):
"""
Get changed files and detect renames.
Returns: (changed_files, renames)
"""
if not last_tag:
# First release: every .qmd counts as changed. Sorted for deterministic order.
return sorted(get_all_current_files()), []
# Use --name-status to detect renames
cmd = ["git", "diff", "--name-status", last_tag, "HEAD"]
try:
output = subprocess.check_output(cmd).decode().strip().splitlines()
except subprocess.CalledProcessError:
return [], []
changed_files = []
renames = []
for line in output:
if not line.strip():
continue
parts = line.split("\t")
status = parts[0]
if status.startswith("R"): # Rename (R100, R095, etc.)
if len(parts) >= 3:
old_path = parts[1]
new_path = parts[2]
if new_path.startswith("DOCS/") and new_path.endswith(".qmd"):
renames.append(
{"old": old_path, "new": new_path, "similarity": status}
)
changed_files.append(new_path)
elif status in ["M", "A"]: # Modified or Added
if len(parts) >= 2:
filepath = parts[1]
if filepath.startswith("DOCS/") and filepath.endswith(".qmd"):
changed_files.append(filepath)
# Ignore 'D' (deleted)
return changed_files, renames
def get_git_diff_for_file(filepath, since_tag):
"""
Get git diff for a specific file.
Returns: diff string, None (no changes), or False (error)
"""
if not since_tag:
return None # First release, no baseline
try:
if TESTING_MODE:
# Limit to last N commits
log_cmd = [
"git",
"log",
"--format=%H",
"-n",
str(MAX_COMMITS_FOR_TESTING),
"--",
filepath,
]
commits = subprocess.check_output(log_cmd).decode().strip().splitlines()
if commits:
oldest_commit = commits[-1]
diff_cmd = ["git", "diff", f"{oldest_commit}^", "HEAD", "--", filepath]
else:
return None
else:
diff_cmd = ["git", "diff", since_tag, "HEAD", "--", filepath]
diff = subprocess.check_output(diff_cmd, stderr=subprocess.PIPE).decode()
if not diff or diff.strip() == "":
return None
return diff
except subprocess.CalledProcessError as e:
stderr = e.stderr.decode() if e.stderr else "unknown error"
print(f"[ERROR] Failed to get diff for {filepath}: {stderr}")
return False
def clean_diff_for_ai(diff):
"""Remove binary file markers and noise from diff"""
# Replace binary file markers
diff = re.sub(
r"^Binary files .* differ$",
"# [Binary file (image/media) was updated]",
diff,
flags=re.MULTILINE,
)
return diff
def truncate_large_diff(diff, filepath, max_tokens=ABSOLUTE_MAX_TOKENS):
"""If diff is too large, truncate to fit within token limit"""
encoding = get_encoding()
if not encoding:
return diff
tokens = len(encoding.encode(diff))
if tokens <= max_tokens:
return diff
print(f"[WARNING] {filepath}: Diff too large ({tokens:,} tokens)")
print(f" Truncating to {max_tokens:,} tokens (keeping beginning + end)")
keep_tokens = max_tokens - 1000
half_tokens = keep_tokens // 2
lines = diff.splitlines(keepends=True)
beginning = []
beginning_tokens = 0
for line in lines:
line_tokens = len(encoding.encode(line))
if beginning_tokens + line_tokens > half_tokens:
break
beginning.append(line)
beginning_tokens += line_tokens
ending = []
ending_tokens = 0
for line in reversed(lines):
line_tokens = len(encoding.encode(line))
if ending_tokens + line_tokens > half_tokens:
break
ending.insert(0, line)
ending_tokens += line_tokens
skipped_lines = len(lines) - len(beginning) - len(ending)
truncated = "".join(
beginning
) + f"\n\n... [{skipped_lines:,} lines omitted due to size - " f"file was extensively modified] ...\n\n" + "".join(
ending
)
return truncated
# ═══════════════════════════════════════════════════════════════════════
# METADATA OPERATIONS
# ═══════════════════════════════════════════════════════════════════════
def load_versions_metadata():
"""Load existing version metadata"""
return load_json_or_empty(VERSIONS_FILE, label="version metadata")
def save_versions_metadata(metadata):
"""Save version metadata"""
os.makedirs(os.path.dirname(VERSIONS_FILE), exist_ok=True)
with open(VERSIONS_FILE, "w") as f:
json.dump(metadata, f, indent=2, sort_keys=True)
print(f"[INFO] Saved version metadata to {VERSIONS_FILE}")
# ═══════════════════════════════════════════════════════════════════════
# PROJECT MANIFEST OPERATIONS (for restore feature)
# ═══════════════════════════════════════════════════════════════════════
def extract_project_id(filepath):
"""
Extract project ID from file path.
Convention: DOCS/<project_id>/file.qmd
"""
parts = Path(filepath).parts
if len(parts) >= 2 and parts[0] == "DOCS":
return parts[1]
return None
def get_current_commit_hash():
"""
Get the current commit hash (short form).
Used to track which commit a version was released in.
"""
try:
commit = (
subprocess.check_output(["git", "rev-parse", "--short=12", "HEAD"])
.decode()
.strip()
)
return commit
except subprocess.CalledProcessError:
return "unknown"
def load_project_file_versions(project_id):
"""Load version history for all files in a project, or create new structure"""
versions_path = Path(".version-history") / project_id / "versions.json"
return load_json_or_empty(
versions_path, label=f"version history for {project_id}"
)
def save_project_file_versions(project_id, versions):
"""Save version history for all files in a project"""
versions_dir = Path(".version-history") / project_id
versions_dir.mkdir(parents=True, exist_ok=True)
versions_path = versions_dir / "versions.json"
with open(versions_path, "w") as f:
json.dump(versions, f, indent=2, sort_keys=True)
print(f"[INFO] Updated versions: .version-history/{project_id}/versions.json")
def get_all_current_files():
"""Get set of all current .qmd files in DOCS/ directory"""
current_files = set()
for root, dirs, files in os.walk(DOCS_DIR):
for file in files:
if file.endswith(".qmd"):
filepath = os.path.join(root, file)
current_files.add(filepath)
return current_files
def detect_deleted_files():
"""
Detect files that have history in change_logs.json but no longer exist.
Returns dict of {filepath: last_known_version}
"""
if not os.path.exists(CHANGELOGS_FILE):
return {}
changelogs = load_json_or_empty(
CHANGELOGS_FILE, label="changelogs to detect deletions"
)
if not changelogs:
return {}
current_files = get_all_current_files()
deleted_files = {}
for filepath in changelogs.keys():
if filepath not in current_files:
# File has history but doesn't exist - it was deleted
# Get last known version
history = changelogs[filepath]
if history:
last_entry = history[-1] # Most recent entry
deleted_files[filepath] = last_entry.get("version", "unknown")
return deleted_files
def update_project_versions():
"""
Update project version history with new version information.
Called after versions have been updated in change_logs.json.
Stores version number and release timestamp for each file version.
"""
if DRY_RUN:
print("\n[DRY RUN] Would update project version history (skipped)")
return
print("\n" + "=" * 70)
print("📦 Updating Project Version History")
print("=" * 70)
# Load change logs to see what was updated
if not os.path.exists(CHANGELOGS_FILE):
print("[WARNING] No change_logs.json found - skipping version history update")
return
changelogs = load_json_or_empty(CHANGELOGS_FILE, label="change_logs.json")
if not changelogs:
return
# Current timestamp in ISO 8601 format
timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
# Current commit hash (optional - for tracking)
commit_hash = get_current_commit_hash()
# Group files by project
projects_to_update = {}
for filepath, history in changelogs.items():
project_id = extract_project_id(filepath)
if not project_id:
continue
if project_id not in projects_to_update:
projects_to_update[project_id] = []
projects_to_update[project_id].append(
{"filepath": filepath, "history": history}
)
# Detect deletions
deleted_files = detect_deleted_files()
if deleted_files:
print(f"\n📋 Detected {len(deleted_files)} deleted file(s)")
for filepath in deleted_files:
print(f" • {filepath}")
project_id = extract_project_id(filepath)
if project_id:
if project_id not in projects_to_update:
projects_to_update[project_id] = []
projects_to_update[project_id].append(
{
"filepath": filepath,
"history": changelogs.get(filepath, []),
"deleted": True,
}
)
# Update each project's version history
updated_count = 0
for project_id, files in projects_to_update.items():
print(f"\n📁 Project: {project_id} ({len(files)} file(s))")
versions = load_project_file_versions(project_id)
for file_info in files:
filepath = file_info["filepath"]
history = file_info["history"]
is_deleted = file_info.get("deleted", False)
if not history:
continue
# Get latest version from history
latest_entry = history[-1]
latest_version = latest_entry.get("version")
if not latest_version:
continue
# Initialize file entry if doesn't exist
if filepath not in versions:
versions[filepath] = {
"versions": [],
"latest": latest_version,
"status": "deleted" if is_deleted else "active",
}
file_versions = versions[filepath]
# Check if this version already exists
existing_versions = [
v.get("version") for v in file_versions.get("versions", [])
]
if latest_version not in existing_versions:
# Add new version entry
version_entry = {
"version": latest_version,
"released": timestamp,
"commit": commit_hash,
}
file_versions["versions"].append(version_entry)
file_versions["latest"] = latest_version
print(f" ✓ {filepath}: {latest_version} (released {timestamp})")
# Update status for deleted files
if is_deleted:
file_versions["status"] = "deleted"
file_versions["deleted_at"] = timestamp
print(f" [DELETED at {timestamp}]")
else:
file_versions["status"] = "active"
# Remove deleted_at if file was restored
file_versions.pop("deleted_at", None)
# Save updated version history
save_project_file_versions(project_id, versions)
updated_count += 1
print("\n" + "=" * 70)
print(f"✅ Updated {updated_count} project version history file(s)")
print("=" * 70)
def save_changelogs_for_injection(changelog_entries):
"""Merge new changelog entries ({filepath: entry}) into change_logs.json,
preserving history from previous releases for inject_changelog.py to consume."""
existing_changelogs = load_json_or_empty(
CHANGELOGS_FILE, label="existing changelogs"
)
if existing_changelogs:
print(
f"[INFO] Loaded existing changelog history for {len(existing_changelogs)} files"
)
# Add new entries for changed files (prepend to history)
# Keep DOCS/ prefix to match inject_changelog.py expectations
for filepath, new_entry in changelog_entries.items():
# Ensure DOCS/ prefix
normalized_path = (
filepath if filepath.startswith("DOCS/") else f"DOCS/{filepath}"
)
if normalized_path not in existing_changelogs:
existing_changelogs[normalized_path] = []
# Check if this version already exists (avoid duplicates)
existing_versions = [
entry.get("version") for entry in existing_changelogs[normalized_path]
]
if new_entry["version"] in existing_versions:
# Update existing entry instead of adding duplicate
for i, entry in enumerate(existing_changelogs[normalized_path]):
if entry.get("version") == new_entry["version"]:
# Update date and summary
existing_changelogs[normalized_path][i] = new_entry
print(
f"[INFO] Updated existing changelog entry for {normalized_path} v{new_entry['version']}"
)
break
else:
# Prepend new entry (most recent first)
existing_changelogs[normalized_path].insert(0, new_entry)
# Keep only last 20 entries per file (limit history size)
existing_changelogs[normalized_path] = existing_changelogs[normalized_path][:20]
os.makedirs(os.path.dirname(CHANGELOGS_FILE), exist_ok=True)
with open(CHANGELOGS_FILE, "w") as f:
json.dump(existing_changelogs, f, indent=2, sort_keys=True)
print(
f"[INFO] Saved {len(changelog_entries)} new changelog entries to {CHANGELOGS_FILE}"
)
def migrate_rename_metadata(renames, versions_metadata):
"""Copy version metadata from old filename to new filename"""
for rename in renames:
old_path = rename["old"]
new_path = rename["new"]
if old_path in versions_metadata:
print(f"[RENAME] Migrating metadata: {old_path} → {new_path}")
# Copy metadata
versions_metadata[new_path] = versions_metadata[old_path].copy()
versions_metadata[new_path]["renamed_from"] = old_path
versions_metadata[new_path]["renamed_at"] = datetime.now().isoformat()
versions_metadata[old_path]["renamed_to"] = new_path
else:
print(
f"[RENAME] No metadata for {old_path}, treating {new_path} as new file"
)
def extract_major_version(filename):
"""Extract major version from filename (e.g., file_v2.qmd → 2)"""
match = re.search(r"_v(\d+)\.qmd$", filename)
if match:
return int(match.group(1))
else:
raise ValueError(
"Filename must end with _vX.qmd where X is the major version number"
)
# ═══════════════════════════════════════════════════════════════════════
# AI ANALYSIS
# ═══════════════════════════════════════════════════════════════════════
def get_combined_prompt(file_list):
"""Return the comprehensive combined prompt for version + changelog"""
template_path = os.path.join(
os.path.dirname(__file__),
"prompt_templates",
"update_versions_changelog_prompt.txt",
)
with open(template_path, "r") as f:
template = f.read()
# Create numbered file list to help AI track completeness
file_list_text = "\n".join([f"{i+1}. {f}" for i, f in enumerate(file_list)])
num_files = str(len(file_list))
# Simple string replacement (no f-string complexity)
prompt = template.replace("{{NUM_FILES}}", num_files)
prompt = prompt.replace("{{FILE_LIST}}", file_list_text)
return prompt
def process_single_batch(batch_files, batch_num, total_batches):
"""Process one batch of files with Gemini AI"""
batch_input = "=== BATCH ANALYSIS (GIT DIFFS) ===\n\n"
if TESTING_MODE:
file_sizes = {}
for filepath, diff in batch_files.items():
batch_input += f"### FILE: {filepath}\n"
batch_input += "=== GIT DIFF ===\n"
batch_input += f"{diff}\n\n"
batch_input += "---\n\n"
if TESTING_MODE:
file_sizes[filepath] = {
"bytes": len(diff),
"tokens": count_tokens(diff),
}
input_tokens = count_tokens(batch_input)
batch_size_kb = len(batch_input) / 1024
print(
f"[AI] Batch {batch_num}/{total_batches}: Analyzing {len(batch_files)} files ({input_tokens:,} tokens, {batch_size_kb:.2f} KB)"
)
if TESTING_MODE:
print(" Per-file breakdown:")
for filepath, sizes in file_sizes.items():
print(
f" • {os.path.basename(filepath)}: {sizes['bytes']:,} bytes, {sizes['tokens']:,} tokens"
)
if DRY_RUN or TESTING_MODE:
print("\n" + "=" * 70)
print(f"🔍 DEBUG: AI Input for Batch {batch_num}/{total_batches}")
print("=" * 70)
for filepath, diff in batch_files.items():
print(f"\n📄 FILE: {filepath}")
print("-" * 70)
diff_lines = diff.splitlines()
preview_lines = min(50, len(diff_lines))
print(f"Diff preview (first {preview_lines} of {len(diff_lines)} lines):")
print("\n".join(diff_lines[:preview_lines]))
if len(diff_lines) > preview_lines:
print(
f"\n... ({len(diff_lines) - preview_lines} more lines truncated in preview)"
)
print("-" * 70)
print("\n" + "=" * 70)
print("End of AI Input Debug")
print("=" * 70 + "\n")
if DRY_RUN:
print(f"[DRY RUN] Skipping API call for batch {batch_num}/{total_batches}")
print(f"[DRY RUN] Would send {input_tokens:,} tokens to Gemini API")
mock_results = {}
for filepath in batch_files.keys():
mock_results[filepath] = {
"version": {
"bump": "patch",
"reason": "[DRY RUN] Mock result - no actual analysis performed",
"confidence": "mock",
},
"changelog": {
"format": "paragraph",
"summary": "[DRY RUN] Mock changelog - no actual analysis performed",
},
}
return mock_results
check_and_wait_for_rate_limits(input_tokens)
# Get prompt with explicit file list to prevent AI from "forgetting" files
file_list = list(batch_files.keys())
prompt = get_combined_prompt(file_list)
full_prompt = prompt + "\n\n" + batch_input
def _do_call():
return call_gemini(None, full_prompt)
try:
try:
result_text = _do_call()
except Exception as e:
error_str = str(e)
if is_quota_error(error_str):
delay = extract_retry_delay(error_str, default_wait=60)
sleep_secs = delay + 1 if delay > 0 else 60
print(f"[QUOTA] 429 — sleeping {sleep_secs:.0f}s then retrying once")
time.sleep(sleep_secs)
result_text = _do_call()
else:
raise
record_api_request(input_tokens)
if TESTING_MODE:
response_tokens = count_tokens(result_text)
print(
f" Response: {response_tokens:,} tokens, {len(result_text):,} bytes"
)
print(
f" Rate limit status: {len(rate_limit_state['requests_minute'])}/{RPM_SAFE} req/min, "
f"{rate_limit_state['tokens_minute']:,}/{TPM_SAFE:,} tokens/min, "
f"{rate_limit_state['requests_today']}/{RPD_LIMIT} req/day"
)
print("\n" + "=" * 70)
print("🔍 DEBUG: Raw LLM Response")
print("=" * 70)
print(result_text)
print("=" * 70 + "\n")
# call_gemini already strips fences; parse JSON directly
results = json.loads(result_text)
# Validate we got results for ALL files in the batch
expected_files = set(batch_files.keys())
returned_files = set(results.keys())
missing_files = expected_files - returned_files
if missing_files:
print(
f"\n❌ ERROR: AI did not return results for all files in batch {batch_num}/{total_batches}"
)
print(f" Expected: {len(expected_files)} files")
print(f" Received: {len(returned_files)} files")
print(" Missing files:")
for filepath in missing_files:
print(f" • {filepath}")
print(
"\n This usually means the AI response was truncated or incomplete."
)
print(" Try reducing MAX_FILES_PER_BATCH or MAX_TOKENS_PER_BATCH.")
raise Exception(
f"Batch {batch_num}/{total_batches} incomplete: {len(missing_files)} files missing from AI response"
)
if TESTING_MODE:
print("🔍 DEBUG: Parsed JSON Results")
print("=" * 70)
for filepath, decision in results.items():
print(f" {os.path.basename(filepath)}:")
print(f" Version: {decision.get('version', {}).get('bump', 'N/A')}")
print(f" Reason: {decision.get('version', {}).get('reason', 'N/A')}")
print(
f" Changelog Format: {decision.get('changelog', {}).get('format', 'N/A')}"
)
summary = decision.get("changelog", {}).get("summary", "N/A")
print(f" Changelog: {summary[:100]}...")
print("=" * 70 + "\n")
print(
f" ✅ Successfully analyzed {len(results)} files in batch {batch_num}/{total_batches}"
)
return results
except json.JSONDecodeError as e:
print(
f"\n❌ ERROR: Failed to parse JSON response in batch {batch_num}/{total_batches}"
)
print(f" JSON Error: {e}")
print("\n" + "=" * 70)
print("🔍 DEBUG: Problematic LLM Response")
print("=" * 70)
print(result_text)
print("=" * 70)
# Empty -> batch_with_retry splits and retries the halves instead of aborting.
return {}
except Exception as e:
print(f"\n❌ ERROR: Batch {batch_num}/{total_batches} processing failed")
print(f" Error: {e}")
raise Exception(f"Batch {batch_num}/{total_batches} failed: {e}")
def analyze_version_bumps_and_changelogs_batch(file_diffs):
"""
Analyze multiple files for version bump and changelog using smart batching.
Requires Gemini API - fails if API unavailable (no fallback).
Uses helpers.batch_utils.batch_with_retry for partial-failure recovery
(recursive halving on incomplete batch responses).
"""
batches = create_smart_batches(
file_diffs,
max_tokens=MAX_TOKENS_PER_BATCH,
max_files=MAX_FILES_PER_BATCH,
)
total_batches = len(batches)
if total_batches > 1:
print(f"\n📦 Split {len(file_diffs)} files into {total_batches} batches")
all_results = {}
for i, batch in enumerate(batches, 1):
def _process(sub_batch, _i=i):
return process_single_batch(sub_batch, _i, total_batches)
batch_results = batch_with_retry(batch, _process, max_retries=2)
all_results.update(batch_results)
print(f"\n✅ All batches completed: {len(all_results)} files analyzed")
return all_results
# ═══════════════════════════════════════════════════════════════════════
# FILE OPERATIONS
# ═══════════════════════════════════════════════════════════════════════
def calculate_new_version(current_version, bump_type, major_from_filename):
"""Calculate new version based on bump type"""
# First published version is 1.0.0, so a _v0 filename floors to major 1.
major_from_filename = max(major_from_filename, 1)
try:
parts = current_version.split(".")
major = int(parts[0])
minor = int(parts[1])
patch = int(parts[2])
# Verify major version matches filename
if major != major_from_filename:
print(
f"[WARNING] Major version mismatch: {current_version} vs filename v{major_from_filename}"
)
print(f" Resetting to {major_from_filename}.0.0")
return f"{major_from_filename}.0.0"
if bump_type == "minor":
minor += 1
patch = 0
elif bump_type == "patch":
patch += 1
else:
print(f"[WARNING] Unknown bump type '{bump_type}', defaulting to patch")
patch += 1
return f"{major}.{minor}.{patch}"
except Exception as e:
print(f"[ERROR] Version calculation failed: {e}")
return f"{major_from_filename}.0.0"
def update_qmd_version_only(filepath, new_version):
"""Update only version field in YAML frontmatter (not changelog - that's in change_logs.json)"""
if DRY_RUN:
print(f"[DRY RUN] Would update {filepath}: version → {new_version}")
return True
if SKIP_FILE_WRITES:
print(f"[SKIP] {filepath}: version → {new_version} (deferred to orchestrator)")
return True
try:
path = Path(filepath)
yaml_data, lines = read_qmd_frontmatter(path)
if not yaml_data and (not lines or lines[0].strip() != "---"):
print(f"[WARNING] {filepath}: No YAML frontmatter found, skipping")
return False
yaml_data["version"] = new_version
if not write_qmd_frontmatter(path, yaml_data, lines):
print(f"[WARNING] {filepath}: Invalid YAML frontmatter, skipping")
return False
print(f"[SUCCESS] Updated {filepath}: version → {new_version}")
return True
except Exception as e:
print(f"[ERROR] Failed to update {filepath}: {e}")
return False
# ═══════════════════════════════════════════════════════════════════════
# FIRST RELEASE LOGIC
# ═══════════════════════════════════════════════════════════════════════
def initialize_first_release(all_files):
"""Initialize versions for first release (no previous tags)"""
print("\n" + "=" * 70)
print("🆕 First Release - Initializing Versions")
print("=" * 70)
versions_metadata = load_versions_metadata()
changelog_entries = {}
today = datetime.now().strftime("%Y-%m-%d") # YYYY-MM-DD format
for filepath in all_files:
filename = os.path.basename(filepath)
try:
major_version = extract_major_version(filename)
except ValueError as e:
print(f"[ERROR] {filepath}: {e}")
continue
initial_version = f"{max(major_version, 1)}.0.0"
update_qmd_version_only(filepath, initial_version)
# Store changelog entry for change_logs.json
changelog_entries[filepath] = {
"version": initial_version,
"date": today,
"summary": "Initial release",
}
versions_metadata[filepath] = {
"current_version": initial_version,
"major_from_filename": major_version,
"last_updated": today,
"last_release_tag": "initial",
"last_bump": "initial",
"last_bump_reason": "First release",
}
save_versions_metadata(versions_metadata)
save_changelogs_for_injection(changelog_entries)
# Update project version history for restore feature
update_project_versions()
print(f"✅ First release initialization complete: {len(all_files)} files")
# ═══════════════════════════════════════════════════════════════════════
# MAIN LOGIC
# ═══════════════════════════════════════════════════════════════════════
def main():
"""Main version and changelog update workflow"""
global TESTING_MODE, DRY_RUN, SKIP_FILE_WRITES, MAX_TOKENS_PER_BATCH, MAX_FILES_PER_BATCH
# Re-read env in case it was set after module import
TESTING_MODE = os.getenv("TESTING_MODE", "false").lower() == "true"
DRY_RUN = os.getenv("DRY_RUN", "false").lower() == "true"
SKIP_FILE_WRITES = os.getenv("SKIP_FILE_WRITES", "false").lower() == "true"
MAX_TOKENS_PER_BATCH = 50_000 if TESTING_MODE else 600_000
MAX_FILES_PER_BATCH = 5 if TESTING_MODE else 15
print_mode_banners(
testing_mode=TESTING_MODE,
dry_run=DRY_RUN,
testing_extra_lines=[
f"Max files: {MAX_FILES_FOR_TESTING}",