|
1 | 1 | #!/usr/bin/env python3 |
2 | | -"""With dev stopped: python3 scripts/seed-issue-21397.py [matching|absent|unrelated]. |
| 2 | +"""With dev stopped: python3 scripts/seed-issue-21397.py --replace [matching|absent|unrelated] [--now [ISO]]. |
3 | 3 |
|
4 | | -Replaces isolated projection state with a synthetic stopped usage-limit run. |
5 | | -Run dev once to initialize migrations first. Never targets the live T3 home. |
| 4 | +Replaces ALL thread/project projections in this worktree's .t3 database with a |
| 5 | +synthetic stopped usage-limit run, so --replace is required. Run dev once to |
| 6 | +initialize migrations first. Never targets the live T3 home. |
| 7 | +
|
| 8 | +Fixture times are frozen around the evidence clock, 2026-10-22T19:00:00Z (usage |
| 9 | +limit hit; reset and matching snooze at 2026-10-23T08:50:00Z). By default they |
| 10 | +are seeded unchanged, reproducing the PR captures. After that reset, pass --now |
| 11 | +(current time) or --now ISO to shift every timestamp so the failure happens then. |
6 | 12 | """ |
| 13 | +import argparse |
| 14 | +import datetime |
7 | 15 | import json |
8 | 16 | import pathlib |
| 17 | +import re |
9 | 18 | import sqlite3 |
10 | | -import sys |
| 19 | + |
| 20 | +EVIDENCE_CLOCK = datetime.datetime(2026, 10, 22, 19, tzinfo=datetime.timezone.utc) |
| 21 | +ISO = re.compile(r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$') |
| 22 | + |
| 23 | + |
| 24 | +def parse_iso(value): |
| 25 | + parsed = datetime.datetime.fromisoformat(value.replace('Z', '+00:00')) |
| 26 | + return parsed if parsed.tzinfo else parsed.replace(tzinfo=datetime.timezone.utc) |
| 27 | + |
| 28 | + |
| 29 | +def shift(value, delta): |
| 30 | + if isinstance(value, dict): |
| 31 | + return {k: shift(v, delta) for k, v in value.items()} |
| 32 | + if isinstance(value, str) and ISO.match(value): |
| 33 | + moved = parse_iso(value) + delta |
| 34 | + return moved.strftime('%Y-%m-%dT%H:%M:%S.') + f'{moved.microsecond // 1000:03d}Z' |
| 35 | + return value |
| 36 | + |
| 37 | + |
| 38 | +parser = argparse.ArgumentParser() |
| 39 | +parser.add_argument('mode', nargs='?', default='matching', choices=('matching', 'absent', 'unrelated')) |
| 40 | +parser.add_argument('--replace', action='store_true', help='confirm deleting existing worktree projections') |
| 41 | +parser.add_argument('--now', nargs='?', const='now', help='shift fixture times so the limit is hit at this ISO time') |
| 42 | +args = parser.parse_args() |
| 43 | +mode = args.mode |
| 44 | +now = None if args.now is None else datetime.datetime.now(datetime.timezone.utc) if args.now == 'now' else parse_iso(args.now) |
| 45 | +delta = datetime.timedelta() if now is None else now - EVIDENCE_CLOCK |
11 | 46 |
|
12 | 47 | root = pathlib.Path(__file__).resolve().parent.parent |
13 | 48 | path = root / '.t3/userdata/statev2.sqlite' |
14 | 49 | if not path.is_file() or path.resolve() != path.absolute(): |
15 | 50 | raise SystemExit('Expected a migrated, isolated worktree .t3 database') |
16 | | -mode = sys.argv[1] if len(sys.argv) > 1 else 'matching' |
17 | | -if mode not in ('matching', 'absent', 'unrelated'): |
18 | | - raise SystemExit('Unknown fixture mode') |
| 51 | +if not args.replace: |
| 52 | + raise SystemExit(f'Refusing to replace thread and project projections in {path} without --replace') |
19 | 53 | fixture = json.loads((root / 'scripts/fixtures/issue-21397.json').read_text()) |
20 | 54 | with sqlite3.connect(path) as db: |
21 | 55 | # Prevent the old V1 import from bringing unrelated conversations back. |
|
28 | 62 | db.execute("UPDATE orchestration_v2_effect_outbox SET status='cancelled' WHERE status IN ('pending','running')") |
29 | 63 | for table, rows in fixture.items(): |
30 | 64 | for row in rows: |
31 | | - row = dict(row) |
| 65 | + row = shift(dict(row), delta) |
32 | 66 | if table == 'projection_projects': |
33 | 67 | row['workspace_root'] = str(root) |
34 | 68 | if table == 'orchestration_v2_projection_threads': |
|
37 | 71 | payload['snoozedUntil'] = None |
38 | 72 | elif mode == 'unrelated': |
39 | 73 | payload['snoozedUntil'] = '2026-10-24T08:50:00.000Z' |
40 | | - row['payload_json'] = json.dumps(payload) |
| 74 | + row['payload_json'] = json.dumps(shift(payload, delta)) |
| 75 | + elif delta and 'payload_json' in row: |
| 76 | + row['payload_json'] = json.dumps(shift(json.loads(row['payload_json']), delta)) |
41 | 77 | columns = ','.join(f'"{k}"' for k in row) |
42 | 78 | placeholders = ','.join('?' for _ in row) |
43 | 79 | db.execute(f'INSERT INTO "{table}" ({columns}) VALUES ({placeholders})', list(row.values())) |
44 | 80 | settings_path = root / '.t3/userdata/settings.json' |
45 | 81 | settings = json.loads(settings_path.read_text()) if settings_path.exists() else {} |
46 | 82 | settings.update(snoozeLimitedThreads=False, autoResumeLimitedThreads=False) |
47 | 83 | settings_path.write_text(json.dumps(settings, indent=2) + '\n') |
48 | | -print(f'Seeded synthetic {mode} fixture in {path}') |
| 84 | +print(f'Seeded synthetic {mode} fixture in {path} (limit hit at {now or EVIDENCE_CLOCK:%Y-%m-%dT%H:%M:%SZ})') |
0 commit comments