coreprinciple Claude Opus 4.8 commited on
Commit
d336f72
·
1 Parent(s): 2b4da11

Trace: create the dataset repo before pushing (upload_file does not auto-create)

Browse files

Root cause of the missing trace dataset: upload_file 404s when the repo doesn't
exist; only the CLI auto-creates. Add an idempotent create_repo(exist_ok=True) on
first push + in the selftest, so the dataset is created and traces actually flow.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Files changed (1) hide show
  1. src/discoverroute/narrate/trace.py +24 -3
src/discoverroute/narrate/trace.py CHANGED
@@ -26,6 +26,25 @@ PLANS_PATH = LOG_DIR / "plans.jsonl"
26
  _seq_lock = threading.Lock()
27
  _seq = 0
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  def _next_seq() -> int:
31
  global _seq
@@ -59,6 +78,7 @@ def _push_hf_async(row: dict, kind: str) -> None:
59
  try:
60
  from huggingface_hub import HfApi
61
 
 
62
  api = HfApi(token=token)
63
  api.upload_file(
64
  path_or_fileobj=json.dumps(row, ensure_ascii=False).encode("utf-8"),
@@ -88,10 +108,11 @@ def selftest() -> None:
88
  "secret named exactly 'HF_TOKEN' to a write token to enable Hub push.",
89
  flush=True)
90
  return
91
- print(f"[trace] HF_TOKEN detected (len={len(token)}); testing write to "
92
  f"{config.TRACE_REPO} …", flush=True)
93
  try:
94
  from huggingface_hub import HfApi
 
95
  HfApi(token=token).upload_file(
96
  path_or_fileobj=b'{"selftest": true}',
97
  path_in_repo="_selftest/boot.json",
@@ -101,8 +122,8 @@ def selftest() -> None:
101
  print(f"[trace] ✅ push OK — {config.TRACE_REPO} is writable; traces will flow.",
102
  flush=True)
103
  except Exception as exc: # noqa: BLE001
104
- print(f"[trace] ❌ push FAILED ({type(exc).__name__}): {exc} — the token "
105
- "likely lacks WRITE access to the build-small-hackathon org.", flush=True)
106
 
107
 
108
  def log_trace(call_type: str, input_data: dict, output_data: dict,
 
26
  _seq_lock = threading.Lock()
27
  _seq = 0
28
 
29
+ # upload_file does NOT auto-create a missing repo (unlike the `hf upload` CLI), so
30
+ # the dataset must be created once before the first push or every push 404s. Guard
31
+ # the create with a flag so we only attempt it once per process.
32
+ _repo_lock = threading.Lock()
33
+ _repo_ready = False
34
+
35
+
36
+ def _ensure_repo(token: str) -> None:
37
+ """Create the trace dataset once (idempotent). Raises on a real auth failure."""
38
+ global _repo_ready
39
+ with _repo_lock:
40
+ if _repo_ready:
41
+ return
42
+ from huggingface_hub import HfApi
43
+ HfApi(token=token).create_repo(
44
+ repo_id=config.TRACE_REPO, repo_type="dataset", exist_ok=True, private=False
45
+ )
46
+ _repo_ready = True
47
+
48
 
49
  def _next_seq() -> int:
50
  global _seq
 
78
  try:
79
  from huggingface_hub import HfApi
80
 
81
+ _ensure_repo(token) # create the dataset on first push (no auto-create)
82
  api = HfApi(token=token)
83
  api.upload_file(
84
  path_or_fileobj=json.dumps(row, ensure_ascii=False).encode("utf-8"),
 
108
  "secret named exactly 'HF_TOKEN' to a write token to enable Hub push.",
109
  flush=True)
110
  return
111
+ print(f"[trace] HF_TOKEN detected (len={len(token)}); ensuring + testing write to "
112
  f"{config.TRACE_REPO} …", flush=True)
113
  try:
114
  from huggingface_hub import HfApi
115
+ _ensure_repo(token) # create the dataset if missing (the actual fix)
116
  HfApi(token=token).upload_file(
117
  path_or_fileobj=b'{"selftest": true}',
118
  path_in_repo="_selftest/boot.json",
 
122
  print(f"[trace] ✅ push OK — {config.TRACE_REPO} is writable; traces will flow.",
123
  flush=True)
124
  except Exception as exc: # noqa: BLE001
125
+ print(f"[trace] ❌ push FAILED ({type(exc).__name__}): {exc} — if 403, the "
126
+ "token lacks WRITE access to the build-small-hackathon org.", flush=True)
127
 
128
 
129
  def log_trace(call_type: str, input_data: dict, output_data: dict,