espnet3.bin.atlas.core.state.StateStore
espnet3.bin.atlas.core.state.StateStore
class espnet3.bin.atlas.core.state.StateStore(path: Path)
Bases: object
Persist state in one CSV file per record type.
Each record type has a CSV file with one column per field; cells are JSON encoded to retain nulls, numbers, and nested values. A single advisory lock protects read/modify/write operations; failed non-blocking lock attempts retry every second. Rewrites use a temporary file and atomic rename.
Open (or create) a study’s CSV state store.
- Parameters:path – The study directory.
append_event(study_id: str, event_type: str, payload: dict) → None
Append one timestamped event row to the study’s event log.
- Parameters:
- study_id – Study the event belongs to.
- event_type – Short event name, e.g.
"node_started","study_completed". - payload – Event-specific JSON-serializable detail.
- Returns: None.
Example
>>> store.append_event("my_study", "node_started", {"run_id": "run_1"})begin_node_run(run_id: str, pid: int | None = None) → None
Mark a node run "running" and record its start time/local pid.
- Parameters:
- run_id – The node run to mark as started.
- pid – Local process id executing the node, or
Noneif not running as a tracked local subprocess.
- Returns: None.
Example
>>> store.begin_node_run("run_1")compute_trial_gpu_hours(study_id: str) → float
Sum wall-clock GPU hours used across all of a study’s trial node runs.
For each trial-scoped node run with a recorded start time, adds the elapsed time from started_at to finished_at (or now, if still running).
- Parameters:study_id – Study to compute GPU hours for.
- Returns: Total elapsed hours across all trial node runs.
- Return type: float
Example
>>> store.compute_trial_gpu_hours("my_study")
12.5create_job(job: JobRecord) → None
Persist a new job record.
- Parameters:job – The job record to store.
- Returns: None.
Example
>>> store.create_job(job_record)create_node_run(run_id: str, study_id: str, node_name: str, trial_id: str | None = None, attempt: int = 0) → None
Create a new node run record with status "pending".
- Parameters:
- run_id – Unique id for this node run.
- study_id – Study the node run belongs to.
- node_name – Graph node to run.
- trial_id – Trial the node run operates on, or
Nonefor a study-level node. - attempt – Attempt number, for a retried
run_trialnode run.
- Returns: None.
Example
>>> store.create_node_run("run_1", "my_study", "research_context")create_or_load_study(study_id: str, study_dir: Path) → Study
Return the existing study record, or create one if none exists yet.
- Parameters:
- study_id – The study’s name/key.
- study_dir – The study’s directory, recorded only when creating.
- Returns: The existing or newly created study record.
- Return type:Study
Example
>>> study = store.create_or_load_study("s", Path("exp/atlas/s"))
>>> study.status
'initialized'Persist a new trial and refresh the study’s trials.csv report.
- Parameters:trial – The trial record to store.
- Returns: The same trial, unchanged (returned for chaining).
- Return type:Trial
Example
>>> store.create_trial(trial).trial_id
'trial_001'find_job_by_node_run(node_run_id: str) → JobRecord | None
Return the most recently created job submitted for a node run, if any.
- Parameters:node_run_id – The node run to look up.
- Returns: The latest matching job, or
Noneif no job has been submitted for this node run. - Return type:JobRecord | None
Example
>>> store.find_job_by_node_run("run_1").status
'running'finish_node_run(run_id: str, result: StageResult) → None
Mark a node run "completed" or "failed" and record its result.
- Parameters:
- run_id – The node run to mark as finished.
- result – The stage’s result; maps
statusin{"failure", "timeout", "retry"}to node-run status"failed", anything else to"completed".
- Returns: None.
Example
>>> store.finish_node_run("run_1", StageResult(status="success"))get_agent_session_id(study_id: str) → str | None
Return a study’s shared coding-agent session/thread id, if any.
- Parameters:study_id – The study’s name/key.
- Returns: The persisted session id, or
Noneif the agent has not been called yet. - Return type: str | None
Example
>>> store.get_agent_session_id("my_study")
'codex-session-abc123'get_best_trial(study_id: str, metric: str, mode: str, exclude_trial_id: str | None = None) → Trial | None
Return the trial with the best score for a given metric.
- Parameters:
- study_id – Study whose trials to search.
- metric – Score-name to compare (only trials whose
score_namematches are considered). - mode –
"max"to pick the highest score, anything else to pick the lowest. - exclude_trial_id – Trial to leave out, for example to ask what the best was before that trial.
- Returns: The best-scoring matching trial, or
Noneif no trial has a score for this metric yet. - Return type:Trial | None
Example
>>> store.get_best_trial("my_study", "wer", "min").trial_id
'trial_002'get_node_run(run_id: str) → dict | None
Return one node run’s row by id, or None if it does not exist.
- Parameters:run_id – The node run’s id.
- Returns: The matching node-run row, or
None. - Return type: dict | None
Example
>>> store.get_node_run("run_1")["node_name"]
'research_context'get_pending_node_runs(study_id: str) → list[dict]
List a study’s not-yet-started node runs, ordered by run id.
- Parameters:study_id – Study whose node runs to list.
- Returns: Pending node-run rows (raw dicts, not a dataclass).
- Return type: list[dict]
Example
>>> len(store.get_pending_node_runs("my_study"))
1get_study(study_id: str) → Study
Return a study’s current record.
- Parameters:study_id – The study’s name/key.
- Returns: The study’s record.
- Return type:Study
- Raises:StopIteration – If no study with this id exists.
Example
>>> store.get_study("my_study").status
'running'get_trial(trial_id: str) → Trial
Return one trial by id.
- Parameters:trial_id – The trial’s id.
- Returns: The matching trial record.
- Return type:Trial
- Raises:StopIteration – If no trial with this id exists.
Example
>>> store.get_trial("trial_001").status
'running'is_agent_context_initialized(study_id: str) → bool
Whether the agent has already been given this study’s full context.
- Parameters:study_id – The study’s name/key.
- Returns:
Trueonce a successful response has set this flag. - Return type: bool
Example
>>> store.is_agent_context_initialized("my_study")
Falselist_active_jobs(study_id: str) → list[JobRecord]
List a study’s jobs still in a non-terminal status.
- Parameters:study_id – Study whose jobs to list.
- Returns: Jobs with status in
{"submitted", "pending", "running", "unknown"}. - Return type: list[JobRecord]
Example
>>> len(store.list_active_jobs("my_study"))
1list_jobs(study_id: str, job_type: str | None = None) → list[JobRecord]
List study jobs, optionally filtered by type.
- Parameters:
- study_id – Study whose jobs should be returned.
- job_type – Optional job type such as
tickortrial.
- Returns: Matching jobs ordered by creation time.
- Return type: list[JobRecord]
Example
>>> store.list_jobs("study-1", job_type="tick")
[]list_node_runs(study_id: str) → list[dict]
List all of a study’s node runs, ordered by run id.
- Parameters:study_id – Study whose node runs to list.
- Returns: All node-run rows for the study.
- Return type: list[dict]
Example
>>> len(store.list_node_runs("my_study"))
3list_trials(study_id: str, status: str | None = None) → list[Trial]
List a study’s trials, ordered by creation time then trial id.
- Parameters:
- study_id – Study whose trials to list.
- status – Optional status to filter by (e.g.
"running"), orNonefor all trials.
- Returns: Matching trials.
- Return type: list[Trial]
Example
>>> len(store.list_trials("my_study", status="running"))
1mark_agent_context_initialized(study_id: str) → None
Record that the agent has now seen this study’s full context.
- Parameters:study_id – The study’s name/key.
- Returns: None.
Example
>>> store.mark_agent_context_initialized("my_study")set_agent_session_id(study_id: str, session_id: str) → None
Persist a study’s shared coding-agent session/thread id.
- Parameters:
- study_id – The study’s name/key.
- session_id – The session/thread id to persist.
- Returns: None.
Example
>>> store.set_agent_session_id("my_study", "codex-session-abc123")set_best_trial(study_id: str, trial_id: str) → None
Record a study’s current best trial id.
- Parameters:
- study_id – The study’s name/key.
- trial_id – The trial id to record as current best.
- Returns: None.
Example
>>> store.set_best_trial("my_study", "trial_003")update_job_status(job_id: str, status: str, external_id: str | None = None) → None
Update a job’s status (and, once known, its backend-native id).
- Parameters:
- job_id – The job to update.
- status – New status, e.g.
"running","completed","failed". - external_id – Backend-native job id to set, or
Noneto leave the existing value unchanged.
- Returns: None.
Example
>>> store.update_job_status("job_1", "completed")update_study_status(study_id: str, status: str) → None
Set a study’s lifecycle status.
- Parameters:
- study_id – The study’s name/key.
- status – New status, e.g.
"running","stopped","completed".
- Returns: None.
Example
>>> store.update_study_status("my_study", "stopped")update_trial(trial: Trial) → None
Persist a trial’s current field values, bumping its updated_at.
- Parameters:trial – The trial record to store (mutated in place: its
updated_atis set to now before saving). - Returns: None.
Example
>>> trial.status = "accepted"
>>> store.update_trial(trial)