midah commited on
Commit
4582bc5
·
verified ·
1 Parent(s): 58e82bb

feat: read precomputed spine store (all-traces), live-ingest fallback

Browse files
Files changed (3) hide show
  1. __pycache__/app.cpython-313.pyc +0 -0
  2. app.py +70 -5
  3. requirements.txt +2 -0
__pycache__/app.cpython-313.pyc CHANGED
Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ
 
app.py CHANGED
@@ -25,6 +25,13 @@ Design decisions (benefit / price):
25
  static essay's query box exactly.
26
  Benefit: one query language across the paper and the live demo.
27
  Price: the spine drops argument-level detail, by design in procgrep.
 
 
 
 
 
 
 
28
  """
29
 
30
  from __future__ import annotations
@@ -50,6 +57,8 @@ MAX_DATASETS = 6 # cached datasets before LRU eviction (design decision 2)
50
  INGEST_TIMEOUT_S = 60.0
51
  HIT_SAMPLE = 50 # matched traces returned to the client
52
  STATIC = Path(__file__).parent / "static"
 
 
53
 
54
  # A short, curated starting set; the client may query any dataset id.
55
  SUGGESTED = (
@@ -90,17 +99,67 @@ class CachedTrace:
90
  _CACHE: OrderedDict[str, list[CachedTrace]] = OrderedDict()
91
  _META: dict[str, dict] = {} # dataset id -> {adapter, n_traces, truncated, n_models}
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  def _load(dataset: str) -> list[CachedTrace]:
95
- """Return cached canonicalized traces for ``dataset``, ingesting on a miss.
96
 
97
- Ingest is bounded by MAX_TRACES and a timeout; results are cached under an
98
- LRU of size MAX_DATASETS (design decisions 2 and 3).
 
99
  """
100
  if dataset in _CACHE:
101
  _CACHE.move_to_end(dataset)
102
  return _CACHE[dataset]
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  traces, plan = ingest(dataset, limit=MAX_TRACES, timeout=INGEST_TIMEOUT_S)
105
  cached = [
106
  CachedTrace(
@@ -165,9 +224,15 @@ class QueryRequest(BaseModel):
165
 
166
  @app.get("/datasets")
167
  def datasets() -> JSONResponse:
168
- """Suggested datasets plus which ones are already warm in the cache."""
 
 
 
 
 
 
169
  return JSONResponse(
170
- {"suggested": list(SUGGESTED), "cached": list(_CACHE.keys()), "meta": _META}
171
  )
172
 
173
 
 
25
  static essay's query box exactly.
26
  Benefit: one query language across the paper and the live demo.
27
  Price: the spine drops argument-level detail, by design in procgrep.
28
+ 5. Prefer a precomputed spine store (HF dataset midah/procgrep-spines) over
29
+ live ingest, falling back to live ingest when the store is missing a dataset
30
+ or cannot be reached.
31
+ Benefit: warm datasets answer instantly with no per-query streaming or
32
+ canonicalization, and coverage can grow on CI rather than at request time.
33
+ Price: store-backed datasets are only as fresh as the last refresh build;
34
+ the weekly action keeps them current.
35
  """
36
 
37
  from __future__ import annotations
 
57
  INGEST_TIMEOUT_S = 60.0
58
  HIT_SAMPLE = 50 # matched traces returned to the client
59
  STATIC = Path(__file__).parent / "static"
60
+ SPINE_REPO = "midah/procgrep-spines" # precomputed store (design decision 5)
61
+ SPINE_FILE = "procgrep_spines.parquet"
62
 
63
  # A short, curated starting set; the client may query any dataset id.
64
  SUGGESTED = (
 
99
  _CACHE: OrderedDict[str, list[CachedTrace]] = OrderedDict()
100
  _META: dict[str, dict] = {} # dataset id -> {adapter, n_traces, truncated, n_models}
101
 
102
+ # Precomputed spine store, loaded once and shared (design decision 5). None until
103
+ # the first load attempt; an empty dict means "tried, nothing usable" so every
104
+ # dataset falls through to live ingest.
105
+ _STORE: dict[str, list[CachedTrace]] | None = None
106
+
107
+
108
+ def _load_store() -> dict[str, list[CachedTrace]]:
109
+ """Download and parse the precomputed spine store, grouped by dataset.
110
+
111
+ Reconstructs atoms from the space-joined spine (lossless: atoms carry no
112
+ internal spaces). Any failure (no repo, offline, bad file) yields an empty
113
+ store so callers transparently fall back to live ingest.
114
+ """
115
+ global _STORE
116
+ if _STORE is not None:
117
+ return _STORE
118
+ try:
119
+ import pandas as pd
120
+ from huggingface_hub import hf_hub_download
121
+
122
+ path = hf_hub_download(SPINE_REPO, SPINE_FILE, repo_type="dataset")
123
+ df = pd.read_parquet(path)
124
+ store: dict[str, list[CachedTrace]] = {}
125
+ for row in df.itertuples(index=False):
126
+ spine = str(row.spine)
127
+ atoms = tuple(spine.split())
128
+ store.setdefault(str(row.dataset), []).append(
129
+ CachedTrace(str(row.trace_id), str(row.agent), atoms, spine, str(row.task))
130
+ )
131
+ _STORE = store
132
+ except Exception:
133
+ _STORE = {}
134
+ return _STORE
135
+
136
 
137
  def _load(dataset: str) -> list[CachedTrace]:
138
+ """Return canonicalized traces for ``dataset``.
139
 
140
+ Prefers the precomputed spine store (design decision 5); on a store miss,
141
+ ingests live, bounded by MAX_TRACES and a timeout and cached under an LRU of
142
+ size MAX_DATASETS (design decisions 2 and 3).
143
  """
144
  if dataset in _CACHE:
145
  _CACHE.move_to_end(dataset)
146
  return _CACHE[dataset]
147
 
148
+ store = _load_store()
149
+ if dataset in store:
150
+ cached = store[dataset]
151
+ _CACHE[dataset] = cached
152
+ _META[dataset] = {
153
+ "adapter": "spine-store",
154
+ "n_traces": len(cached),
155
+ "truncated": False,
156
+ **_stats(cached),
157
+ }
158
+ while len(_CACHE) > MAX_DATASETS:
159
+ evicted, _ = _CACHE.popitem(last=False)
160
+ _META.pop(evicted, None)
161
+ return cached
162
+
163
  traces, plan = ingest(dataset, limit=MAX_TRACES, timeout=INGEST_TIMEOUT_S)
164
  cached = [
165
  CachedTrace(
 
224
 
225
  @app.get("/datasets")
226
  def datasets() -> JSONResponse:
227
+ """Suggested datasets plus which ones are already warm in the cache.
228
+
229
+ Store-backed datasets are surfaced first (they answer instantly), followed
230
+ by the curated suggestions, deduped in that order.
231
+ """
232
+ store_ids = list(_load_store().keys())
233
+ suggested = list(dict.fromkeys([*store_ids, *SUGGESTED]))
234
  return JSONResponse(
235
+ {"suggested": suggested, "cached": list(_CACHE.keys()), "meta": _META}
236
  )
237
 
238
 
requirements.txt CHANGED
@@ -2,4 +2,6 @@ fastapi>=0.110
2
  uvicorn[standard]>=0.29
3
  datasets>=2.19
4
  huggingface_hub>=0.23
 
 
5
  procgrep @ git+https://github.com/hamidahoderinwale/procgrep@main
 
2
  uvicorn[standard]>=0.29
3
  datasets>=2.19
4
  huggingface_hub>=0.23
5
+ pandas>=2.0
6
+ pyarrow>=15.0
7
  procgrep @ git+https://github.com/hamidahoderinwale/procgrep@main