Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9c3d091
fix(materials): turn non-str keys into JSON keys where PLR data enter…
Xuwznln Nov 25, 2026
c7788fb
fix(host): hand drivers a device reference when a ResourceSlot names …
Xuwznln Nov 25, 2026
c888612
fix(host): put a device whose startup fails inside the Host into main…
Xuwznln Nov 25, 2026
cba165d
fix(materials): stop inferring site occupancy from equal coordinates
Xuwznln Nov 25, 2026
9f4eaa9
fix(host): reject a ResourceSlot that resolves to a device
Xuwznln Nov 25, 2026
69a1678
feat(workflow): declare allowed resource templates on @workflow
Xuwznln Nov 25, 2026
62b6096
docs(action-standard): redesign Material as declared material actions
Xuwznln Nov 26, 2026
e954063
feat(registry)!: declare material actions with Material.move / pick /…
Xuwznln Nov 26, 2026
33e7d16
feat(scheduler): resolve Material.move / pick / place by site uuid
Xuwznln Nov 26, 2026
1463a3a
feat(scheduler)!: lock DeviceSlot and SiteSlot parameters by type
Xuwznln Nov 26, 2026
9af7599
feat(workflow): check DeviceSlot values and device-owned site pickers
Xuwznln Nov 26, 2026
2d88a9c
docs(workflow): refer to Material.site_picker in site selection messages
Xuwznln Nov 26, 2026
3ea1e2e
docs: migrate guides and skills to the redesigned Material API
Xuwznln Nov 26, 2026
9080f5f
feat(scheduler): settle declared moves from the frozen plan without d…
Xuwznln Nov 26, 2026
d4065ac
feat(scheduler): resolve a SiteSlot label under its site_picker owner
Xuwznln Nov 26, 2026
d732992
feat(registry): let a site_picker SiteSlot carry a Site label
Xuwznln Nov 26, 2026
3e680d2
docs(action-standard): list the gripper role, receipt and site label …
Xuwznln Nov 26, 2026
5888d56
feat(registry)!: rename @action(material=) to materials= and mark it …
Xuwznln Nov 27, 2026
2774daf
docs: describe @action(materials=) as one experimental field
Xuwznln Nov 27, 2026
f8b01b0
test(registry): expect AST scan cache version 29
Xuwznln Nov 27, 2026
8d27a22
test(e2e): pin LabDeviceComplexWorkflowDemo to its materials= main
Xuwznln Nov 27, 2026
544633f
feat(registry)!: name the experimental resource-semantics field mater…
Xuwznln Nov 27, 2026
4f80ce2
docs: describe the experimental @action(material_contract=) field
Xuwznln Nov 27, 2026
3aeca29
test(e2e): pin LabDeviceComplexWorkflowDemo to its material_contract=…
Xuwznln Nov 27, 2026
3156de2
feat(registry)!: declare resource semantics with MaterialRole.xxx(...)
Xuwznln Nov 27, 2026
6c39635
docs: write resource semantics with MaterialRole.xxx(...)
Xuwznln Nov 27, 2026
3a15530
test(e2e): pin LabDeviceComplexWorkflowDemo to its MaterialRole main
Xuwznln Nov 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions .cursor/skills/add-device/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,22 @@ from unilabos.registry.decorators import action
auto_prefix=True, # 强制使用 auto- 前缀
parent=True, # 从父类 MRO 获取参数签名
)
@action(material=Material.load("sample", device="vessel")) # 资源语义:一个字段,见下
@action(material_contract=MaterialRole.move("plate", to_site="target_site")) # 资源语义(实验性):一个字段,见下
```

JSON 动作(不写 `action_type`)的参数与返回值只有一个标准:注册表按签名生成 schema,调度端与 Host 调用前按它校验参数,
调用后按返回注解校验返回值。写了类型的参数严格校验;没写类型或 `Any` 接受任意 JSON 值;没有默认值的参数必填。
资源语义用 `material=Material.load / unload / in_place / pick / place / transfer / aliquot / uses / order_sensitive / site(...)`,
多个写成列表;库位参数注解为 `SiteSlot`,它属于哪个物料参数写 `Material.site("site", owner="carrier", occupant="sample")`。
资源语义用 `material_contract=`,声明动作真实的物料动作 `MaterialRole.move / pick / place / dispense(...)`,多个写成列表;
不代表物料动作的 `MaterialRole.load / unload / in_place / site_picker / order_sensitive(...)` 也写在这里。整个 `material_contract=` 字段是实验性功能
(写了就按定义告警一次);改名前的 `material=` 与声明类 `Material`(现在叫 `MaterialRole`,区别于运行时的 `unilabos.resources.materials`)注册时报错。
字符串都是参数名:物料 `ResourceSlot`、库位 `SiteSlot`(库位 uuid,所属方由库位决定)、设备 `DeviceSlot`(省略即执行设备)。
库位选择器列出哪个所属方的库位写 `MaterialRole.site_picker("site", of="carrier", for_material="sample")`。
`DeviceSlot` 参数加设备锁、`SiteSlot` 参数加库位锁,不需要额外声明。
前端选择器:物料 `ResourceSlot`、设备 `DeviceSlot`、库位 `SiteSlot` 自动推出,其他(节点、人工确认指派人、扣减……)或想换一种
显示时写 `@action(placeholder_keys={"参数名": PLACEHOLDER_...})`,两者取并集、同一参数以手写为准,只改显示不改类型。
`contract=`、`resource_contract=`、JSON 动作的 `goal=` / `result=`,以及注解里的 `Placeholder(...)` / `SiteSelector(...)` 已删除,
写了报错。完整规则见 `docs/developer_guide/action_standard.md`。
`contract=`、`resource_contract=`、JSON 动作的 `goal=` / `result=`,注解里的 `Placeholder(...)` / `SiteSelector(...)`,
以及 `Material.transfer / aliquot / site / uses` 和带 `owner` / `site` / `carriers` 的 `Material.pick / place` 已删除,
写了报错并给出新写法。完整规则见 `docs/developer_guide/action_standard.md`。

**自动识别规则:**
- 带 `@action` 的公开方法 → 注册为动作(方法名即动作名)
Expand Down
61 changes: 40 additions & 21 deletions .cursor/skills/create-device-package/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ class <ClassName>:
- `always_free=True`:动作不占用设备 busy 队列,适合 `get_status`、`query_*`、`ping`、连接测试等轻量查询动作;会在 `action_value_mappings.<action>.always_free` 中写入 `true`。
- `feedback_interval`:动作反馈发布间隔,单位秒;AST 扫描默认补 `1.0`,长任务可调大,例如 `5.0`、`30.0`。
- `goal_default`:编辑器里的初始参数值,叠加在签名默认值上;它不让参数变成可选,没有签名默认值的参数仍然必填。
- `material`:资源语义(装载托管、原位处理、搬运、分装、顺序敏感、库位关系),一个字段,值是 `Material` 的一个方法或列表,见下文「资源语义」。
- `material_contract`(实验性,写了就按定义告警一次):资源语义(搬运、两步搬运、分装;以及装载托管、原位处理、库位选择器提示、顺序敏感),一个字段,值是 `MaterialRole` 的一个方法或列表,见下文「资源语义」。
- `placeholder_keys`:前端选择器 `{参数名: PLACEHOLDER_...}`。物料 `ResourceSlot`、设备 `DeviceSlot`、库位 `SiteSlot` 由类型自动推出;
节点、人工确认指派人、扣减等没有专门类型的选择器,或想把某个参数换一种显示,就在这里手写。两者取并集,同一参数以手写为准;
只改前端显示,参数类型与校验不变(兼容规则:手写成 `unilabos_resources` 的参数也加物料锁)。键必须是参数名。
- `handles`:动作级输入/输出端口,使用 `ActionInputHandle`、`ActionOutputHandle`;不写时按 schema 生成。
- 已删除、写了会报错的写法:`contract=`、`resource_contract=`、JSON 动作的 `goal=` / `result=` 重命名(参数名就是字段名),
以及注解里的 `Placeholder(...)`(改成 `placeholder_keys=`)与 `SiteSelector(...)`(改成 `SiteSlot` + `Material.site(...)`)。
- 已删除、写了会报错的写法:`contract=`、`resource_contract=`、改名前的 `material=`(改成 `material_contract=`,值不变)、改名前的声明类 `Material`(改成 `MaterialRole`,方法不变;`Material` 仍可导入,但 `Material.xxx(...)` 直接报错)、JSON 动作的 `goal=` / `result=` 重命名(参数名就是字段名),
注解里的 `Placeholder(...)`(改成 `placeholder_keys=`)与 `SiteSelector(...)`(改成 `SiteSlot` + `MaterialRole.site_picker(...)`),
以及 `Material.transfer / aliquot / site / uses`、带 `owner` / `site` / `carriers` 的 `Material.pick / place`(新写法见下文「资源语义」)。

参数与返回值只有一个标准,注册表按签名生成 schema,调度端与 Host 调用前校验参数,调用后按返回注解校验返回值:

Expand All @@ -143,33 +144,51 @@ class <ClassName>:

旧的 `@always_free` 仍可被 AST 扫描识别,但新设备包优先使用 `@action(always_free=True)`,把动作元数据集中在一个装饰器里。

#### 资源语义:`material=`
#### 资源语义:`material_contract=`(实验性)

```python
from unilabos.registry.decorators import Material, action
from unilabos.registry.placeholder_type import ResourceSlot
from unilabos.registry.decorators import MaterialRole, action
from unilabos.registry.placeholder_type import DeviceSlot, ResourceSlot, SiteSlot

@action(display_name="装载样品", material=Material.load("sample", device="vessel"))
def load_sample(self, sample: ResourceSlot, vessel: str) -> LoadResult: ...
@action(display_name="搬运板", material_contract=MaterialRole.move("plate", to_site="target_site"))
def move_plate(self, plate: ResourceSlot, target_site: SiteSlot) -> dict: ...

@action(display_name="原位搅拌", material=[Material.in_place("sample"), Material.order_sensitive()])
@action(display_name="抓取", material_contract=MaterialRole.pick("plate"))
def pick_plate(self, plate: ResourceSlot) -> dict: ...

@action(display_name="放下", material_contract=MaterialRole.place("plate", to_site="target_site"))
def place_plate(self, plate: ResourceSlot, target_site: SiteSlot) -> dict: ...

@action(display_name="原位搅拌", material_contract=[MaterialRole.in_place("sample"), MaterialRole.order_sensitive()])
def stir(self, sample: ResourceSlot, rpm: int = 300) -> None: ...
```

物料动作:

| 写法 | 含义 |
| --- | --- |
| `MaterialRole.move("plate", to_site="site", from_site=None, carrier_device=None)` | 一个动作把容器搬到 `to_site`;所属方由库位决定,搬运途中在搬运设备的 `gripper` 夹爪库位上 |
| `MaterialRole.pick("plate", from_site=None, carrier_device=None)` | 两步搬运第一步:抓到搬运设备的夹爪库位,任务托管搬运设备 |
| `MaterialRole.place("plate", to_site="site", carrier_device=None)` | 两步搬运第二步:从夹爪库位放到 `to_site`,结束托管 |
| `MaterialRole.dispense(from_material="source", to_materials=["target", ...])` | 按实测量在容器之间转移内容物 |

不代表物料动作的声明:

| 写法 | 含义 |
| --- | --- |
| `Material.load("sample", device="vessel")` / `Material.unload(...)` | 装进 / 取出设备,期间任务托管设备 |
| `Material.load("sample", device="b", from_device="a")` | 设备间转移 |
| `Material.in_place("sample")` | 物料必须仍在执行设备内 |
| `Material.pick(...)` / `Material.place("sample", owner=, site=, carriers=[...])` | 两步搬运 |
| `Material.transfer("plate", to=, site=, gripper=, ...)` | 一次完成的机械臂搬运 |
| `Material.aliquot(source=, targets=[...])` | 按实测量分装 |
| `Material.uses(device=[...], tool=[...], motion=[...], site=[...], material=[...])` | 额外占用的资源参数 |
| `Material.order_sensitive()` | 同一资源上的兄弟动作必须显式排序 |
| `Material.site("site", owner="carrier", occupant="sample", show_occupied=True, allow_occupied=False)` | 库位参数 `site`(`SiteSlot`)属于 `carrier`,放进去的是 `sample`;已占用库位是否列出 / 能否选 |

字符串都是参数名,注册时对照签名检查;物料参数必须是 `ResourceSlot`,设备参数可以是 `ResourceSlot` 或设备 ID 字符串;
`Material.site` 的库位参数必须是 `SiteSlot`,`owner` / `occupant` 必须是单个 `ResourceSlot`。
| `MaterialRole.load("sample", device=None, from_device=None)` / `MaterialRole.unload("sample", device=None)` | 装进 / 取出设备,期间任务托管设备;`device` 省略即执行设备,`from_device` 表示设备间转移 |
| `MaterialRole.in_place("sample")` | 物料必须仍在执行设备内 |
| `MaterialRole.site_picker("site", of=None, for_material=None, occupied="show")` | 库位选择器提示:`site` 列出 `of`(省略即执行设备根节点)的库位,放进去的是 `for_material`;`occupied` 取 `"hide"` / `"show"` / `"allow"` |
| `MaterialRole.order_sensitive()` | 同一资源上的兄弟动作必须显式排序 |

字符串都是参数名,注册时对照签名检查:物料(`material`、`from_material`、`to_materials`、`for_material`)必须是单个 `ResourceSlot`;
库位(`to_site`、`from_site`、`site_picker` 的库位参数)必须是 `SiteSlot`(值是库位 uuid);设备(`carrier_device`、`device`、
`from_device`)必须是 `DeviceSlot`(值是设备 id),省略就是执行设备;`site_picker` 的 `of` 是 `ResourceSlot` 或 `DeviceSlot`。
搬运设备的夹爪库位 `meta_data.role` 统一为 `gripper`(有且只有一个)。参数按类型加锁:`ResourceSlot` 物料锁、`DeviceSlot` 设备锁、
`SiteSlot` 库位锁。

用 `MaterialRole.move / pick / place` 声明了的搬运由核心按回执结算物料位置,驱动不要再对同一物料调用 `materials.transfer` /
`materials.assign`;`materials.*` 只用于没有声明的运行时变化(新建、写内容物、删除、启动对齐等)。

### 3.2 `@topic_config` 规则

Expand Down
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pytest tests/resources/test_resourcetreeset_groups.py::test_name # single test

### Core Layers

**Registry** (`unilabos/registry/`): Singleton `Registry` class discovers and catalogs all device types, resource types, and communication devices from YAML definitions. Device types live in `registry/devices/**/*.yaml`, resources in `registry/resources/`, comms in `registry/device_comms/` (all read recursively; a declared `category` is kept, the file stem is only the fallback). The registry resolves class paths to actual Python classes via `utils/import_manager.py`. Loading is ordered: files merge in sorted path order and entries in declaration order, independent of thread timing and the string hash seed (the first YAML file wins a duplicate id). Unless the backend is `ros2` (for library use and `--check_mode`: unless ROS 2 is installed), the registry build never imports `unilabos.client.host.ros2.*` or `unilabos.devices.ros_dev.*` and does not scan `devices/ros_dev`; the ROS 2 presets (`camera`, `serial`) stay registered so HostLink can mark such graph nodes unavailable (`registry.py:ros2_core_enabled`). Every JSON action (`@action()` without `action_type`, and `auto-` methods) gets one signature-derived schema (`registry/action_schema.py`: the AST scan builds a draft per parameter with the strict annotation parser, unknown annotations fall back to the registry's signature inference in `Registry._infer_annotation_schema`), carrying the `x-unilabos-action-contract` v2 envelope; the scheduler, run preflight and the Host validate arguments and results against it (`registry/typed_contract.py`). Resource semantics are one decorator field, `@action(material=Material.load(...) | [...])` (`registry/material_roles.py`); a site parameter is annotated `SiteSlot` and its relation to the material parameters is `Material.site(...)`, written into that field's `x-unilabos-site-selector` extension. Frontend selectors: the entry's `placeholder_keys` is the type-derived set (`ResourceSlot` / `DeviceSlot` / `SiteSlot`) united with `@action(placeholder_keys={...})`, the hand-written kind winning per parameter (`registry/placeholder_keys.py`); it changes only the display. The material lock is a union: `ResourceSlot` parameters plus every parameter whose merged selector is `unilabos_resources`, which is how signature-less YAML and ROS / protocol entries mark their materials. `contract=`, `resource_contract=`, JSON-action `goal=` / `result=` renames, the removed `Placeholder` / `SiteSelector` annotations and an `action_context` parameter without the `ActionContext` annotation are rejected by both the decorator and the scan (`registry/action_options.py`). The framework injects `ActionContext` by type: any parameter annotated `ActionContext`, `Optional[ActionContext]` or `ActionContext | None` gets it (the optional forms get `None` when there is no context, e.g. under ROS 2), and such parameters are excluded from the schema, handles, `placeholder_keys`, material locks and `goal_default` (`registry/action_context.py`, `client/host/runtime/action.py:bind_action_context`). Rules: `docs/developer_guide/action_standard.md`.
**Registry** (`unilabos/registry/`): Singleton `Registry` class discovers and catalogs all device types, resource types, and communication devices from YAML definitions. Device types live in `registry/devices/**/*.yaml`, resources in `registry/resources/`, comms in `registry/device_comms/` (all read recursively; a declared `category` is kept, the file stem is only the fallback). The registry resolves class paths to actual Python classes via `utils/import_manager.py`. Loading is ordered: files merge in sorted path order and entries in declaration order, independent of thread timing and the string hash seed (the first YAML file wins a duplicate id). Unless the backend is `ros2` (for library use and `--check_mode`: unless ROS 2 is installed), the registry build never imports `unilabos.client.host.ros2.*` or `unilabos.devices.ros_dev.*` and does not scan `devices/ros_dev`; the ROS 2 presets (`camera`, `serial`) stay registered so HostLink can mark such graph nodes unavailable (`registry.py:ros2_core_enabled`). Every JSON action (`@action()` without `action_type`, and `auto-` methods) gets one signature-derived schema (`registry/action_schema.py`: the AST scan builds a draft per parameter with the strict annotation parser, unknown annotations fall back to the registry's signature inference in `Registry._infer_annotation_schema`), carrying the `x-unilabos-action-contract` v2 envelope; the scheduler, run preflight and the Host validate arguments and results against it (`registry/typed_contract.py`). Resource semantics are one experimental decorator field, `@action(material_contract=MaterialRole.move(...) | [...])` (`registry/material_roles.py`; the factories live on the frozen `MaterialRole` dataclass they return, named apart from the runtime `unilabos.resources.materials`; any use warns once per definition; the old field name `material=`, the short-lived `materials=` and the old class name `Material` — still importable, but `Material.xxx(...)` fails at import and in the AST scan — are rejected with the new spelling): the material actions are `MaterialRole.move` / `pick` / `place` / `dispense` (sites are `SiteSlot` uuids whose owner the scheduler derives from the site, carriers are `DeviceSlot` parameters defaulting to the executing device and its `gripper` site); `load` / `unload` / `in_place` / `site_picker` / `order_sensitive` are the declarations that are not material actions. A site parameter is annotated `SiteSlot`; `MaterialRole.site_picker(...)` writes the selector hint into that field's `x-unilabos-site-selector` extension. `Material.transfer` / `aliquot` / `site` / `uses` and `pick` / `place` with `owner` / `site` / `carriers` are rejected with the new spelling. Parameters are locked by type: `ResourceSlot` material locks, `DeviceSlot` device locks, `SiteSlot` site locks. Frontend selectors: the entry's `placeholder_keys` is the type-derived set (`ResourceSlot` / `DeviceSlot` / `SiteSlot`) united with `@action(placeholder_keys={...})`, the hand-written kind winning per parameter (`registry/placeholder_keys.py`); it changes only the display. The material lock is a union: `ResourceSlot` parameters plus every parameter whose merged selector is `unilabos_resources`, which is how signature-less YAML and ROS / protocol entries mark their materials. `contract=`, `resource_contract=`, the old `material=` / `materials=`, JSON-action `goal=` / `result=` renames, the removed `Placeholder` / `SiteSelector` annotations and an `action_context` parameter without the `ActionContext` annotation are rejected by both the decorator and the scan (`registry/action_options.py`). The framework injects `ActionContext` by type: any parameter annotated `ActionContext`, `Optional[ActionContext]` or `ActionContext | None` gets it (the optional forms get `None` when there is no context, e.g. under ROS 2), and such parameters are excluded from the schema, handles, `placeholder_keys`, material locks and `goal_default` (`registry/action_context.py`, `client/host/runtime/action.py:bind_action_context`). Rules: `docs/developer_guide/action_standard.md`.

**Resource Tracking** (`unilabos/resources/resource_tracker.py`): Pydantic-based `ResourceDict` → `ResourceDictInstance` → `ResourceTreeSet` hierarchy. `ResourceTreeSet` is the canonical in-memory representation of all devices and resources, used throughout the system, and it owns every resource conversion: flat dicts (`from_raw_dict_list`, `dump`/`load`) and PLR (`from_plr_resources`, `to_plr_resources`). PLR drafts from a registry resource class come from `resources/materials.py:instantiate_registry_resource`. There is no separate graph I/O module: device graphs are node-link JSON only (GraphML is not supported) and are turned into runtime structures by the Graph Authority (`server/services/materials/graph.py:load_graph_payload`).

Expand Down
Loading
Loading