bcbd1bdb22
Integ / changes (push) Has been skipped
Pre-commit / pre-commit (push) Failing after 1s
CLI exit codes / changes (push) Has been skipped
Test (Install) / changes (push) Has been skipped
Test (Python) / changes (push) Has been skipped
Test (TypeScript) / changes (push) Has been skipped
CLI exit codes / cli-gate (push) Has been cancelled
Test (Install) / test-install-gate (push) Has been cancelled
Integ / integ-gate (push) Has been cancelled
Test (Python) / test-python-gate (push) Has been cancelled
Test (TypeScript) / test-typescript-gate (push) Has been cancelled
Test (Install) / python-minimal (3.12) (push) Has been cancelled
Test (Install) / python-minimal (3.11) (push) Has been cancelled
Test (Install) / python-extra (agno, mirage.agents.agno) (push) Has been cancelled
Test (Install) / python-extra (chroma, mirage.resource.chroma) (push) Has been cancelled
Test (Install) / python-extra (pdf, mirage.core.filetype.pdf) (push) Has been cancelled
Integ / integ (push) Has been cancelled
Integ / integ-database (push) Has been cancelled
Integ / integ-database-ts (push) Has been cancelled
Integ / integ-data (push) Has been cancelled
Integ / integ-ssh (push) Has been cancelled
Integ / integ-ssh-ts (push) Has been cancelled
Test (Python) / audit (push) Has been cancelled
Test (TypeScript) / test (push) Has been cancelled
Test (TypeScript) / python-fs-shim (push) Has been cancelled
CLI exit codes / Python CLI (push) Has been cancelled
CLI exit codes / TypeScript CLI (push) Has been cancelled
CLI exit codes / Cross-language snapshot interop (push) Has been cancelled
Test (Python) / test (push) Has been cancelled
Test (Python) / import-isolation (deepagents, openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Python) / import-isolation (deepagents, pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Integ / integ-ts (push) Has been cancelled
Integ / integ-fuse (push) Has been cancelled
Test (Install) / python-extra (databricks, mirage.resource.databricks_volume) (push) Has been cancelled
Test (Install) / python-extra (deepagents, mirage.agents.langchain) (push) Has been cancelled
Test (Install) / python-extra (email, mirage.resource.email) (push) Has been cancelled
Test (Install) / python-extra (fuse, mirage.fuse.mount) (push) Has been cancelled
Test (Install) / python-extra (hdf5, mirage.core.filetype.hdf5) (push) Has been cancelled
Test (Install) / python-extra (hf, mirage.resource.hf_buckets) (push) Has been cancelled
Test (Install) / python-extra (lancedb, mirage.resource.lancedb) (push) Has been cancelled
Test (Install) / python-extra (langfuse, mirage.resource.langfuse) (push) Has been cancelled
Test (Install) / python-extra (mongodb, mirage.resource.mongodb) (push) Has been cancelled
Test (Install) / python-extra (nextcloud, mirage.resource.nextcloud) (push) Has been cancelled
Test (Install) / python-extra (openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Install) / python-extra (openhands, mirage.agents.openhands, 3.12) (push) Has been cancelled
Test (Install) / python-extra (parquet, mirage.core.filetype.parquet) (push) Has been cancelled
Test (Install) / python-extra (postgres, mirage.resource.postgres) (push) Has been cancelled
Test (Install) / python-extra (pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Test (Install) / python-extra (qdrant, mirage.resource.qdrant) (push) Has been cancelled
Test (Install) / python-extra (redis, mirage.resource.redis) (push) Has been cancelled
Test (Install) / python-extra (s3, mirage.resource.s3) (push) Has been cancelled
Test (Install) / python-extra (ssh, mirage.resource.ssh) (push) Has been cancelled
Test (Install) / ts-minimal (push) Has been cancelled
175 lines
5.5 KiB
Python
175 lines
5.5 KiB
Python
# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
|
|
|
|
import asyncio
|
|
import time
|
|
from dataclasses import dataclass, field
|
|
from enum import Enum
|
|
|
|
from mirage.io.types import IOResult
|
|
from mirage.types import DEFAULT_SESSION_ID
|
|
from mirage.workspace.types import ExecutionNode
|
|
|
|
|
|
class JobStatus(str, Enum):
|
|
RUNNING = "running"
|
|
COMPLETED = "completed"
|
|
KILLED = "killed"
|
|
|
|
|
|
@dataclass
|
|
class Job:
|
|
id: int
|
|
command: str
|
|
task: asyncio.Task
|
|
cwd: str
|
|
status: JobStatus = JobStatus.RUNNING
|
|
stdout: bytes = b""
|
|
stderr: bytes = b""
|
|
exit_code: int = 0
|
|
execution_node: ExecutionNode | None = None
|
|
io_result: IOResult | None = None
|
|
created_at: float = field(default_factory=time.time)
|
|
agent: str = "unknown"
|
|
session_id: str = DEFAULT_SESSION_ID
|
|
|
|
|
|
class JobTable:
|
|
|
|
def __init__(self) -> None:
|
|
self._jobs: dict[int, Job] = {}
|
|
self._next_id: int = 1
|
|
|
|
def submit(
|
|
self,
|
|
command: str,
|
|
task: asyncio.Task,
|
|
cwd: str,
|
|
agent: str = "unknown",
|
|
session_id: str = DEFAULT_SESSION_ID,
|
|
) -> Job:
|
|
job = Job(id=self._next_id,
|
|
command=command,
|
|
task=task,
|
|
cwd=cwd,
|
|
agent=agent,
|
|
session_id=session_id)
|
|
self._jobs[job.id] = job
|
|
self._next_id += 1
|
|
return job
|
|
|
|
def get(self, job_id: int) -> Job | None:
|
|
job = self._jobs.get(job_id)
|
|
if job is not None:
|
|
self._refresh(job)
|
|
return job
|
|
|
|
def list_jobs(self) -> list[Job]:
|
|
jobs = list(self._jobs.values())
|
|
for j in jobs:
|
|
self._refresh(j)
|
|
return jobs
|
|
|
|
def running_jobs(self) -> list[Job]:
|
|
for j in self._jobs.values():
|
|
self._refresh(j)
|
|
return [
|
|
j for j in self._jobs.values() if j.status == JobStatus.RUNNING
|
|
]
|
|
|
|
def _refresh(self, job: Job) -> None:
|
|
"""Sync status from the underlying asyncio task without awaiting.
|
|
|
|
When the bg task has finished (normally, raised, or was cancelled)
|
|
but no one has called ``wait``, this updates the job's status,
|
|
exit_code, and captured streams from the task result. Lets
|
|
``list_jobs`` / ``running_jobs`` / ``get`` report fresh state.
|
|
Requires ``_run_bg`` to have already materialized stdout/stderr,
|
|
so reading the result is purely synchronous.
|
|
"""
|
|
if job.status != JobStatus.RUNNING:
|
|
return
|
|
if not job.task.done():
|
|
return
|
|
if job.task.cancelled():
|
|
job.status = JobStatus.KILLED
|
|
job.exit_code = 137
|
|
job.stderr = b"Killed"
|
|
return
|
|
exc = job.task.exception()
|
|
if exc is not None:
|
|
job.status = JobStatus.COMPLETED
|
|
job.exit_code = 1
|
|
job.stderr = str(exc).encode()
|
|
return
|
|
stdout, io_result, exec_node = job.task.result()
|
|
job.stdout = stdout if isinstance(stdout, bytes) else b""
|
|
job.io_result = io_result
|
|
job.execution_node = exec_node
|
|
io_result.sync_exit_code()
|
|
job.exit_code = io_result.exit_code
|
|
if isinstance(io_result.stderr, bytes):
|
|
job.stderr = io_result.stderr
|
|
elif io_result.stderr is None:
|
|
job.stderr = b""
|
|
job.status = JobStatus.COMPLETED
|
|
|
|
def kill(self, job_id: int) -> bool:
|
|
job = self._jobs.get(job_id)
|
|
if job is None:
|
|
return False
|
|
job.task.cancel()
|
|
job.status = JobStatus.KILLED
|
|
job.exit_code = 137
|
|
job.stderr = b"Killed"
|
|
return True
|
|
|
|
async def wait(self, job_id: int) -> Job:
|
|
job = self._jobs[job_id]
|
|
if job.status != JobStatus.RUNNING:
|
|
return job
|
|
try:
|
|
stdout, io_result, exec_node = await job.task
|
|
job.stdout = stdout if isinstance(stdout, bytes) else b""
|
|
job.io_result = io_result
|
|
job.execution_node = exec_node
|
|
io_result.sync_exit_code()
|
|
job.exit_code = io_result.exit_code
|
|
job.stderr = await io_result.materialize_stderr()
|
|
job.status = JobStatus.COMPLETED
|
|
except asyncio.CancelledError:
|
|
job.status = JobStatus.KILLED
|
|
job.exit_code = 137
|
|
job.stderr = b"Killed"
|
|
except Exception as exc:
|
|
job.status = JobStatus.COMPLETED
|
|
job.exit_code = 1
|
|
job.stderr = str(exc).encode()
|
|
return job
|
|
|
|
async def wait_all(self) -> list[Job]:
|
|
running = self.running_jobs()
|
|
for job in running:
|
|
await self.wait(job.id)
|
|
return running
|
|
|
|
def pop_completed(self) -> list[Job]:
|
|
"""Return completed/killed jobs and remove them from the table."""
|
|
completed = [
|
|
j for j in self._jobs.values() if j.status != JobStatus.RUNNING
|
|
]
|
|
for j in completed:
|
|
del self._jobs[j.id]
|
|
return completed
|