Skip to content

Commit 07f0809

Browse files
Pigbibicodex
andcommitted
fix: preserve nanosecond gateway epoch bounds
Co-Authored-By: Codex <noreply@openai.com>
1 parent b5afce3 commit 07f0809

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

scripts/recover_ib_gateway_ready.sh

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,21 @@ assessment_lower_bound() {
3333
import sys
3434
from datetime import datetime, timedelta, timezone
3535
stage, started_at, window = sys.argv[1:]
36-
start = datetime.fromisoformat(started_at.replace("Z", "+00:00"))
36+
def parse_rfc3339(value):
37+
base, fraction = value.rstrip("Z").split(".", 1) if "." in value else (value.rstrip("Z"), "")
38+
return (datetime.fromisoformat(base).replace(tzinfo=timezone.utc), int((fraction + "0" * 9)[:9]))
39+
40+
def render(value):
41+
moment, nanos = value
42+
return moment.strftime("%Y-%m-%dT%H:%M:%S") + ".%09dZ" % nanos
43+
44+
start = parse_rfc3339(started_at)
3745
if stage == "initial":
38-
lower = max(start, datetime.now(timezone.utc) - timedelta(seconds=int(window)))
46+
now = datetime.now(timezone.utc) - timedelta(seconds=int(window))
47+
lower = max(start, (now.replace(microsecond=0), now.microsecond * 1000))
3948
else:
4049
lower = start
41-
print(lower.isoformat(timespec="microseconds").replace("+00:00", "Z"))
50+
print(render(lower))
4251
PY
4352
}
4453

@@ -59,7 +68,13 @@ snapshot_decision() {
5968
} | python3 -c '
6069
import re, sys
6170
from datetime import datetime, timezone
62-
lower = datetime.fromisoformat(sys.argv[1].replace("Z", "+00:00"))
71+
def parse_stamp(value):
72+
match = re.fullmatch(r"(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d)(?:\.(\d{1,9}))?Z", value)
73+
if not match:
74+
raise ValueError("invalid timestamp")
75+
return (datetime.fromisoformat(match.group(1)).replace(tzinfo=timezone.utc), int(((match.group(2) or "") + "0" * 9)[:9]))
76+
77+
lower = parse_stamp(sys.argv[1])
6378
terminal = re.compile(r"IBC closing because login has not completed|(?:authentication|login).*(?:timeout|timed out|failed)", re.I)
6479
progress = re.compile(r"IBC: (Starting Gateway|Login attempt|Second Factor Authentication|Login has completed|Configuration tasks completed)|Authentication window found|Auto-fill submitted|Passed token authentication|Authentication completed|Security code:", re.I)
6580
seen_progress = False
@@ -69,7 +84,7 @@ for raw in sys.stdin:
6984
except ValueError: print("invalid"); raise SystemExit
7085
m = re.match(r"(\d{4}-\d\d-\d\d[T ]\d\d:\d\d:\d\d)(?:[.,:](\d{1,9}))?Z?", line)
7186
if not m: continue
72-
stamp = datetime.fromisoformat((m.group(1).replace(" ", "T") + "." + (m.group(2) or "0") + "+00:00"))
87+
stamp = (datetime.fromisoformat(m.group(1).replace(" ", "T")).replace(tzinfo=timezone.utc), int(((m.group(2) or "") + "0" * 9)[:9]))
7388
if stamp < lower: continue
7489
if terminal.search(line): print("terminal"); raise SystemExit
7590
if progress.search(line): seen_progress = True

tests/test_gateway_recovery_scripts.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ grep -Fq 'IB_GATEWAY_RECOVERY_PROGRESS_WAIT_SECONDS:-420' "$recover_script"
2626
grep -Fq 'IB_GATEWAY_RECOVERY_PROGRESS_EXTENSIONS:-2' "$recover_script"
2727
grep -Fq 'IB_GATEWAY_RECOVERY_INITIAL_SNAPSHOT_WINDOW_SECONDS:-420' "$recover_script"
2828
grep -Fq 'assessment_lower_bound()' "$recover_script"
29-
grep -Fq 'max(start, datetime.now(timezone.utc)' "$recover_script"
29+
grep -Fq 'lower = max(start, (now.replace(microsecond=0), now.microsecond * 1000))' "$recover_script"
30+
grep -Fq 'def parse_rfc3339(value):' "$recover_script"
31+
grep -Fq '".%09dZ" % nanos' "$recover_script"
32+
grep -Fq 'def parse_stamp(value):' "$recover_script"
3033
grep -Fq 'snapshot_decision()' "$recover_script"
3134
grep -Fq 'epoch_changed' "$recover_script"
3235
grep -Fq 'tail -n 400' "$recover_script"

0 commit comments

Comments
 (0)