Metadata-Version: 2.5
Name: pyxis-harness-launcher
Version: 0.1.1.dev9
Summary: PyxisHarness launcher: the pyh command, the profile's uv project and environments, the supervisor
Project-URL: Homepage, https://github.com/Onenightcarnival/pyharness
Project-URL: Repository, https://github.com/Onenightcarnival/pyharness
Project-URL: Issues, https://github.com/Onenightcarnival/pyharness/issues
Project-URL: Documentation, https://github.com/Onenightcarnival/pyharness/tree/main/packages/launcher
Author: Onenightcarnival
License-Expression: MIT
License-File: LICENSE
Keywords: agent,deepagents,harness,langgraph,llm,plugin
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Requires-Python: >=3.12
Requires-Dist: uv==0.13.6
Description-Content-Type: text/markdown

# pyxis-harness-launcher

启动器：`pyh` 命令、profile 的 uv 项目与环境、supervisor。harness 自身是 profile 项目的一个依赖，装在 profile 的环境里。

| 项 | 值 |
|---|---|
| 分发包 | `pyxis-harness-launcher` |
| 模块 | `pyxis_harness_launcher` |
| 命令 | `pyh`（`pyxis_harness_launcher.cli:main`） |
| 依赖 | `uv`（版本锁定） |
| 插件 entry point | 无 |

## 使用

| 步骤 | 命令 |
|---|---|
| 安装 | 在一个 uv 项目里 `uv add pyxis-harness-launcher`；Python 3.12 及以上 |
| 创建 profile | `uv run pyh init`；首次联网，把 harness 装进 profile 的环境 |
| 启动 | `uv run pyh run`；浏览器打开 <http://127.0.0.1:8765> |
| 文档 | <https://github.com/Onenightcarnival/pyharness> |

## 文件

| 文件 | 内容 |
|---|---|
| `__init__.py` | 公开名称 |
| `protocol.py` | 启动器与环境内 harness 的约定 |
| `layout.py` | profile 的文件：路径、快照、last-known-good、锁对应的环境 |
| `project.py` | profile 的 `pyproject.toml`；harness 作为依赖的声明 |
| `environment.py` | uv 的调用：锁定、构建、wheel、迁移、清理 |
| `supervisor.py` | 一次运行一代 worker；重启与回滚 |
| `cli.py` | `pyh` 命令行 |

## 两处安装

| 安装 | 内容 | 版本 |
|---|---|---|
| 启动器所在的环境：用户的 uv 项目 | 本包与 `uv` | 该项目的 `uv.lock` |
| profile 的环境（`envs/<键>/`） | harness（`pyxis-harness`）、它的全部依赖、装进 profile 的包 | `uv.lock` 记录的版本 |

环境内的 harness 也依赖本包：`manage` 用同一组函数改声明、锁定、构建。两处的本包版本可以不同，靠 `protocol.py` 互通。

## 约定（`protocol.py`）

| 名称 | 值 | 含义 |
|---|---|---|
| `KERNEL` | `pyxis-harness` | profile 项目依赖的分发包：harness |
| `KERNEL_SOURCE_ENV` | `PYH_KERNEL_SOURCE` | harness 取自该检出目录，不取自索引 |
| `CLI_MODULE` | `pyxis_harness.cli` | `python -m`：harness 的全部命令 |
| `WORKER_MODULE` | `pyxis_harness.worker` | `python -m`：一代 |
| `PREFLIGHT_MODULE` | `pyxis_harness.preflight` | `python -m`：试启动；能启动时退出 0 |
| `BUNDLE_GROUP` | `pyxis_harness.bundles` | bundle 的 entry point 组 |
| `RESTART_EXIT_CODE` | 75 | worker：启动下一代 |
| `BOOT_FAILURE_EXIT_CODE` | 3 | worker：本代启动失败 |
| `ENV_READY` | `.pyxis-harness-ready` | 环境内的文件：已完整建成 |

文件上的约定：`pyproject.toml`、`uv.lock`、`envs/<键>/`、`wheels/<哈希>/`、`runtime/` 下的 ready 标记、`boot-error.json`、`rollback.json`、`supervisor.pid`、`last-known-good/`。

## Profile 的文件（`layout.py`）

`ProfileDir`：不读 YAML；harness 的 `Profile` 继承它。

| 路径 | 内容 |
|---|---|
| `pyproject.toml` | profile 的包：一个 uv 项目的声明 |
| `uv.lock` | 该声明解析出的版本与哈希 |
| `wheels/<哈希>/` | 从本地目录安装的包：构建出的 wheel |
| `envs/<键>/` | 按 `uv.lock` 构建的环境；键由锁文件内容与解释器版本算出 |

| 方法 | 返回 |
|---|---|
| `env()` | `uv.lock` 对应的已建成环境；没有时 `None` |
| `python()` | 该环境的解释器；没有环境时为当前解释器 |
| `site_packages()` | 该环境的 `site-packages`；旧版本的 `site/`；都没有时 `None` |

快照与 last-known-good 是同一组文件：`profile.yaml`、profile patch、`pyproject.toml`、`uv.lock`。

## 项目声明（`project.py`）

```toml
[project]
name = "pyxis-harness-profile"
version = "0"
requires-python = "==3.12.*"
dependencies = [
    "pyxis-harness==0.1.1",
    "pyh-todo",
]

[tool.uv]
package = false

[tool.uv.sources]
"pyh-todo" = { path = "wheels/c37b630f4ae3355d/pyh_todo-0.1.0-py3-none-any.whl" }
```

| 包的来源 | 声明 |
|---|---|
| 本地目录、源码包、wheel | 构建为 wheel，存入 `wheels/<哈希>/`；`tool.uv.sources` 指向它，锁文件记录其哈希 |
| 索引中的包 | 需求原文；`--index-url` 时另加一个 `explicit` 索引 |

harness 的声明（`declare_kernel`）：

| `kernel_source()` | 声明 |
|---|---|
| `PYH_KERNEL_SOURCE` 指向的检出目录 | `pyxis-harness`，以可编辑方式取自 `<检出目录>/packages/meta` |
| 本包以可编辑方式装自一个检出目录 | 同上，取自该检出目录 |
| 无 | `pyxis-harness==<本包的版本>` |

`write_project` 写入的项目总是声明 harness。

## 环境（`environment.py`）

| 文件 | 含义 | 写入 |
|---|---|---|
| `pyproject.toml` | 期望：装哪些包、各自的来源 | `write_project` |
| `uv.lock` | 事实：每个包的版本与哈希 | `uv lock` |
| `envs/<键>/` | 由 `uv.lock` 推导；可删除，可重建 | `uv sync --frozen` |

环境是不可变的：同一份锁对应同一个目录，变更产生新目录。

| 函数 | 效果 |
|---|---|
| `lock` | `uv lock`；已锁定的版本保持，先用缓存，缺文件时联网；`upgrade=<名称>` 把该包升到需求允许的最新版 |
| `build` | `uv sync --frozen` 到 `envs/<键>/`；已建成的环境保持 |
| `prune` | 删除当前文件与 last-known-good 都不指向的环境和 wheel |
| `prepare` | 一代启动之前：见下表 |
| `migrate` | 旧版本的 `site/` → 项目、锁、环境 |

| `prepare` 遇到 | 动作 |
|---|---|
| 旧版本的 `site/` | `migrate` |
| 没有 `pyproject.toml` | `bootstrap=True`：声明 harness，锁定、构建；否则无 |
| 项目没有声明 harness | 声明，锁定、构建 |
| harness 取自检出目录 | 锁定、构建 |
| 没有 `uv.lock` | 锁定、构建 |
| 没有 `uv.lock` 对应的环境 | 构建 |

`migrate`：

| 步骤 | 效果 |
|---|---|
| 1 | `site/` 中带 bundle 的每个分发包：按已安装的文件重新打包进 `wheels/`，写入声明 |
| 2 | 锁定、构建 |
| 3 | `site/` 改名为 `site.migrated/`；声明与锁并入 last-known-good |
| 失败 | 恢复原文件；`site/` 继续使用 |

联网的 uv 调用把输出写到 stderr（`echo`）：首次构建下载 harness 的全部依赖。

## Supervisor（`supervisor.py`）

一次运行一代 worker；每一代用 `ProfileDir.python()`：当时的 `uv.lock` 所对应环境的解释器。

每一代启动之前：`prepare`；未解决的问题记入日志与 `environment` 事件，该代照常启动。

| worker 结果 | supervisor 动作 |
|---|---|
| 写出 ready 标记 | 记录 `ready` |
| 退出 75 | 启动下一代 |
| 退出 0，或收到 SIGINT / SIGTERM | 停止 |
| 启动失败 | 恢复 last-known-good，写 `rollback.json`，再启动；仅一次 |
| 启动失败，无可恢复的文件 | 退出 3 |
| 启动中被转发的信号终止 | 再启动；不回滚 |
| 启动后其他退出 | 退避重启；`crash_window` 内超过 `max_crashes` 次则停止 |

恢复的文件包含 `uv.lock`：harness 的版本与装进 profile 的包随配置一起回到上一次能启动的状态。

| supervisor 收到的信号 | 效果 |
|---|---|
| SIGINT、SIGTERM | 停止 worker，退出 0 |
| SIGHUP | 转发；worker 在 hold 全部释放后重启 |

事件追加写入 `runtime/supervisor.log.jsonl`。

## 命令行（`cli.py`）

```
pyh [--home HOME] <command> [-p PROFILE] ...
```

| 命令 | 运行处 | 效果 |
|---|---|---|
| `run` | 启动器 | `prepare`；profile 不存在时在环境内 `init`；supervisor |
| `run --no-supervisor` | 环境 | 一代，不受管理 |
| `upgrade [--to VERSION]` | 启动器 | 把 profile 的 harness 换到本启动器的版本或指定版本 |
| 其余（`init`、`plugin`、`exec`、`preflight`、`dump-config`、`restart`） | 环境 | `prepare`，然后 `<环境的解释器> -m pyxis_harness.cli <原参数>` |

`upgrade`：

| 步骤 | 效果 |
|---|---|
| 1 | 声明 harness 的新版本 |
| 2 | 锁定（harness 升级，其余已锁定的版本保持）、构建新环境 |
| 3 | 在新环境内预检 |
| 4 | 能启动：保留，向运行中的 supervisor 发重启信号；不能：恢复原文件 |

启动器不自行改动 profile 的 harness 版本：两者不同时 `run` 打印一行提示。
