Python
Work items carry artifact refs; they ride transition and comment
Rohit C Prasad5c00cdfee40b
diff --git a/coworker/teams/store.py b/coworker/teams/store.pyindex ab22c154..770bcf14 100644--- a/coworker/teams/store.py+++ b/coworker/teams/store.py@@ -109,6 +109,7 @@ class TeamStore:state TEXT NOT NULL,assignee TEXT DEFAULT '',case_id TEXT DEFAULT '',+ refs TEXT NOT NULL DEFAULT '[]',created_ts TEXT NOT NULL,updated_seq INTEGER NOT NULL,PRIMARY KEY (space, id)@@ -389,7 +390,7 @@ class TeamStore:+ " ORDER BY id",params,).fetchall()- items = [dict(row) for row in rows]+ items = [_row_to_item(row) for row in rows]if actor.role == Role.WORKER:visible = self._worker_slice(space, actor.id)items = [item for item in items if item["id"] in visible]@@ -416,6 +417,7 @@ class TeamStore:to: str,*,comment: str = "",+ refs: Optional[list[str]] = None,taint: bool = False,) -> dict[str, Any]:target = ItemState(to)@@ -437,6 +439,7 @@ class TeamStore:"from": current.value,"to": target.value,"comment": comment,+ "refs": list(refs or []),},taint=taint,)@@ -449,6 +452,7 @@ class TeamStore:item_id: int,body: str,*,+ refs: Optional[list[str]] = None,taint: bool = False,) -> dict[str, Any]:if not (body or "").strip():@@ -468,7 +472,7 @@ class TeamStore:actor,item_id=item_id,case_id=item["case_id"] or None,- payload={"body": body},+ payload={"body": body, "refs": list(refs or [])},taint=taint,)@@ -673,8 +677,8 @@ class TeamStore:"""INSERT INTO team_items(space, id, title, description, criteria, state, assignee,- case_id, created_ts, updated_seq)- VALUES (?, ?, ?, ?, ?, ?, '', ?, ?, ?)+ case_id, refs, created_ts, updated_seq)+ VALUES (?, ?, ?, ?, ?, ?, '', ?, '[]', ?, ?)""",(space,@@ -700,6 +704,9 @@ class TeamStore:" WHERE space = ? AND id = ?",(payload.get("to"), seq, space, item_id),)+ self._merge_refs(space, item_id, payload.get("refs"))+ elif kind == ITEM_COMMENTED:+ self._merge_refs(space, item_id, payload.get("refs"))elif kind == ITEM_ASSIGNED:self._conn.execute("UPDATE team_items SET assignee = ?, updated_seq = ?"@@ -712,8 +719,27 @@ class TeamStore:" VALUES (?, ?, ?, ?)",(space, payload.get("src"), payload.get("kind"), payload.get("dst")),)- # Comments and journal entries have no materialized state: their- # projections read straight off the (indexed) log.+ # Comment bodies and journal entries have no materialized state: their+ # projections read straight off the (indexed) log. Only the artifact+ # refs a comment carries fold onto the item.++ def _merge_refs(+ self, space: str, item_id: Optional[int], refs: Optional[list]+ ) -> None:+ if not refs or item_id is None:+ return+ row = self._conn.execute(+ "SELECT refs FROM team_items WHERE space = ? AND id = ?",+ (space, item_id),+ ).fetchone()+ if row is None:+ return+ merged = json.loads(row["refs"] or "[]")+ merged.extend(str(ref) for ref in refs if str(ref) not in merged)+ self._conn.execute(+ "UPDATE team_items SET refs = ? WHERE space = ? AND id = ?",+ (json.dumps(merged), space, item_id),+ )def _check_transition_authority(self, actor: Actor, item: dict[str, Any], current: ItemState, target: ItemState@@ -812,7 +838,7 @@ class TeamStore:).fetchone()if row is None:raise BoardError(f"no item #{item_id} in space '{space}'")- return dict(row)+ return _row_to_item(row)def _next_item_id(self, space: str) -> int:row = self._conn.execute(@@ -844,6 +870,15 @@ def _hash(record: dict[str, Any]) -> str:return hashlib.sha256(material.encode("utf-8")).hexdigest()+def _row_to_item(row: sqlite3.Row) -> dict[str, Any]:+ item = dict(row)+ try:+ item["refs"] = json.loads(item.get("refs") or "[]")+ except json.JSONDecodeError:+ item["refs"] = []+ return item++def _row_to_event(row: sqlite3.Row) -> dict[str, Any]:event = dict(row)try:diff --git a/coworker/teams/tools.py b/coworker/teams/tools.pyindex 21e75727..2c965f8b 100644--- a/coworker/teams/tools.py+++ b/coworker/teams/tools.py@@ -94,18 +94,37 @@ def board_tools(except (BoardError, ValueError) as error:return {"error": str(error)}- def transition(item: int, to: str, comment: str = "") -> dict:+ def transition(+ item: int, to: str, comment: str = "", refs: Optional[list] = None+ ) -> dict:"""Move a work item to a new state. Workers move their own item toin_progress, blocked, or review (attach the blocker or a hand-off summary- as `comment`); done requires review verification first."""+ as `comment`, and artifact pointers — branch, report, session — as+ `refs`); done requires review verification first."""return _call(- store.transition, space, actor, item, to, comment=comment, taint=taint()+ store.transition,+ space,+ actor,+ item,+ to,+ comment=comment,+ refs=[str(ref) for ref in refs or []],+ taint=taint(),)- def comment(item: int, body: str) -> dict:+ def comment(item: int, body: str, refs: Optional[list] = None) -> dict:"""Add a comment to a work item. Comments are durable and attributed —- answers that matter belong here, not in chat."""- return _call(store.comment, space, actor, item, body, taint=taint())+ answers that matter belong here, not in chat. `refs` attach artifact+ pointers (branch, PR, report, file:line) to the item."""+ return _call(+ store.comment,+ space,+ actor,+ item,+ body,+ refs=[str(ref) for ref in refs or []],+ taint=taint(),+ )def assign(item: int, assignee: str) -> dict:"""Assign a work item to a worker coworker. The item itself becomes thediff --git a/tests/test_team_board.py b/tests/test_team_board.pyindex 02ce3e71..ee041319 100644--- a/tests/test_team_board.py+++ b/tests/test_team_board.py@@ -155,6 +155,20 @@ def test_blocked_by_shows_on_the_other_side(store):]+def test_artifact_refs_accumulate_on_the_item(store):+ item_id = assigned_item(store)+ store.transition(SPACE, WORKER, item_id, "in_progress")+ store.comment(SPACE, WORKER, item_id, "fix branch up", refs=["branch:fix/acl"])+ store.transition(+ SPACE, WORKER, item_id, "review",+ comment="done", refs=["branch:fix/acl", "report:posture.html"],+ )+ item = store.get_item(SPACE, item_id)+ assert item["refs"] == ["branch:fix/acl", "report:posture.html"] # deduped, ordered+ store.rebuild(SPACE)+ assert store.get_item(SPACE, item_id)["refs"] == item["refs"]++# ------------------------------------------------------------- worker visibilitydef test_worker_sees_only_its_slice(store):