M8:天工阁、叩心与辰正——受托任务后台并行、叩心四约束、时态感知投影

- 天工阁域 domains/tiangong:backstage_tasks(委托包+验收标准+结果证据);有限分解(parent 链)与受控并行(各自后台 Run);子任务全终态级联父任务进入待验收;按证据 accept/reject
- kernel.start_backstage_run(kind=background_task):loop 后台分支不占前台席位、与前台对话并行;断言放宽容纳无人物功能任务;结果只经回调回呈任务——无身份写入路径
- 叩心域 domains/kouxin:knocking_candidates(event/time_anchor/manual 触发 + 草稿 + 裁决快照);四约束裁决(总开关→静默窗口【本地时区判定】→24h 频率→预算),dispatch 未过 429 且快照留痕;候选默认不自动发送;expire_stale 过期推进不触碰缘契
- 辰正 domains/zhengchen:temporal_context() 复用 time_sense 计算核(快照/农历/季节),产出【时态感知】投影段接入镜树生成 prompt(客观时间语境,不附情绪臆测——守一纪律)
- 退役:modules/goal_orchestrator|planner|perception|big_cycle、application/planning、services/governance|goal_persist、candidates 表链(imports 候选抽取 API 面)、goals/plans/agent_roster/perception_channels/big_cycles/旧候选表(迁移 009);前端 admin goals/plans/agents/perception/world 页与六组件;governance 只留 GenerationPreferences;admin 治理面收敛为司命+百工两枢;pivots 健康以域表计数重建
- 验收(test_m8_tiangong 五用例):后台任务无身份写入(记忆/魂籍/缘契零新增);后台与前台并行;分解级联;叩心四约束逐项;不回应不降低关系(缘契维度/状态/版本零变化)
- 四套件:后端 345、前端 203、BFF 4、神识 5 包全绿(2 个预存环境失败不变);迁移 009 净库干净
This commit is contained in:
chenxin committed 2026-08-28 09:13:39 +08:00
1 parent b419e0afde
commit c63b75152a
68 files changed
+1205 -4750

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
"""天工阁域:受托任务(分解/并行/验收,无魂籍)+ 后台 Run 编排(M8)。"""
from app.domains.tiangong import models, service # noqa: F401
__all__ = ["models", "service"]
+68
View File
@@ -0,0 +1,68 @@
"""天工阁 API:受托任务(分解/派发/验收)。"""
from fastapi import APIRouter, Depends, Request
from pydantic import BaseModel, Field
from app.api.v1.deps import get_current_user
from app.domains.tiangong import service
from app.schemas.auth import CurrentUser
task_router = APIRouter(prefix="/tasks", tags=["tiangong"])
class TaskCreateRequest(BaseModel):
character_id: str | None = None
title: str = Field(min_length=1, max_length=200)
brief: str = ""
acceptance_criteria: str = ""
parent_task_id: str | None = None
class DecomposeRequest(BaseModel):
subtasks: list[dict]
class AcceptRequest(BaseModel):
accepted: bool
@task_router.post("", status_code=201)
async def create_task_api(
payload: TaskCreateRequest, user: CurrentUser = Depends(get_current_user)
) -> dict:
return await service.create_task(
owner_scope=user.owner_scope,
character_id=payload.character_id,
title=payload.title,
brief=payload.brief,
acceptance_criteria=payload.acceptance_criteria,
parent_task_id=payload.parent_task_id,
)
@task_router.get("")
async def list_tasks_api(
user: CurrentUser = Depends(get_current_user), parent: str | None = "root"
) -> list[dict]:
return await service.list_tasks(user.owner_scope, parent=None if parent == "all" else parent)
@task_router.post("/{task_id}/decompose", status_code=201)
async def decompose_api(
task_id: str, payload: DecomposeRequest, user: CurrentUser = Depends(get_current_user)
) -> list[dict]:
return await service.decompose(task_id, user.owner_scope, payload.subtasks)
@task_router.post("/{task_id}/dispatch")
async def dispatch_api(
task_id: str, request: Request, user: CurrentUser = Depends(get_current_user)
) -> dict:
return await service.dispatch(task_id, user.owner_scope, request.app.state)
@task_router.post("/{task_id}/accept")
async def accept_api(
task_id: str, payload: AcceptRequest, user: CurrentUser = Depends(get_current_user)
) -> dict:
return await service.accept(task_id, user.owner_scope, accepted=payload.accepted)
+47
View File
@@ -0,0 +1,47 @@
"""天工阁表模型:后台受托任务(无魂籍的功能代理任务)。
设计依据 mandate-and-capability-model.md §7 与 PRD §9.5:
- 任务 Agent 是功能代理,没有魂籍、缘契、情态或独立长期身份;
- 承载受托任务、有限分解、受控并行与按证据验收;
- 后台 Run(runs.kind=background_task)不占前台席位、不构成第二意识。
"""
from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.core.ids import new_id
from app.core.time import utc_now
from app.db.base import Base
class BackstageTask(Base):
"""受托任务:委托包 + 验收标准 + 结果证据;可分解(parent 链)受控并行。"""
__tablename__ = "backstage_tasks"
id: Mapped[str] = mapped_column(String(80), primary_key=True, default=lambda: new_id("task"))
owner_scope: Mapped[str] = mapped_column(String(80), nullable=False, index=True)
# 委托对象人物(可空:纯功能任务无人物语境)
character_id: Mapped[str | None] = mapped_column(
String(80), ForeignKey("characters.id"), nullable=True, index=True
)
# 父任务(分解链:子任务并行执行,全部完成后父任务进入待验收)
parent_task_id: Mapped[str | None] = mapped_column(
String(80), ForeignKey("backstage_tasks.id"), nullable=True, index=True
)
title: Mapped[str] = mapped_column(String(200), nullable=False)
# 最小委托包(受托边界:达成目标与停止条件;任务 Agent 不扩使命)
brief: Mapped[str] = mapped_column(Text, nullable=False, default="")
# 按证据验收:明确的完成判据
acceptance_criteria: Mapped[str] = mapped_column(Text, nullable=False, default="")
# pending / running / awaiting_acceptance / accepted / rejected / failed
status: Mapped[str] = mapped_column(String(30), nullable=False, default="pending", index=True)
# 执行产出(净化摘要 + 证据引用;不构成身份/记忆写入)
result_digest: Mapped[str] = mapped_column(Text, nullable=False, default="")
evidence_json: Mapped[str] = mapped_column(Text, nullable=False, default="{}")
# 关联后台 Run(kind=background_task,不占前台席位)
run_id: Mapped[str | None] = mapped_column(String(80), index=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)
completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
+211
View File
@@ -0,0 +1,211 @@
"""天工阁应用服务:受托任务(分解/并行/验收)与后台 Run 编排。
铁律(identity-continuity.md §4 / mandate-and-capability-model.md §7):
- 后台受托 Run 不占前台席位(并行于前台对话);
- 任务 Run 不是第二意识:**无身份写入路径**——不触发记忆候选、不写魂籍/铸魂、
不建缘契(专项断言钉死);产出只作为任务结果证据回呈;
- 达到预算/期限或连续失败后保留证据、停止并请命(P1 以任务失败态承载)。
"""
from __future__ import annotations
import json
from sqlalchemy import select
from app.core.config import settings
from app.core.errors import AppError
from app.core.time import utc_now
from app.db.session import AsyncSessionLocal
from app.domains import one_qi
from app.domains.tiangong.models import BackstageTask
from app.models.character import Character
async def create_task(
*,
owner_scope: str,
character_id: str | None,
title: str,
brief: str,
acceptance_criteria: str,
parent_task_id: str | None,
) -> dict:
"""登记受托任务(pending;分解任务挂 parent 链)。"""
if character_id:
await _owned(character_id, owner_scope)
async with AsyncSessionLocal() as session:
task = BackstageTask(
owner_scope=owner_scope,
character_id=character_id,
parent_task_id=parent_task_id,
title=title,
brief=brief,
acceptance_criteria=acceptance_criteria,
)
session.add(task)
await session.commit()
session.expunge(task)
return _task_dict(task)
async def decompose(task_id: str, owner_scope: str, subtasks: list[dict]) -> list[dict]:
"""有限分解:登记子任务(受控并行由各自后台 Run 承载)。"""
parent = await _owned_task(task_id, owner_scope)
created = []
for sub in subtasks:
created.append(
await create_task(
owner_scope=owner_scope,
character_id=parent["character_id"],
title=str(sub.get("title", ""))[:200],
brief=str(sub.get("brief", "")),
acceptance_criteria=str(sub.get("acceptance_criteria", "")),
parent_task_id=task_id,
)
)
return created
async def dispatch(task_id: str, owner_scope: str, app_state) -> dict:
"""派发后台 Run(kind=background_task,不占前台席位)。
任务 Prompt = 最小委托包(目标+验收标准+停止条件);结果回填 result_digest,
任务进入 awaiting_acceptance。人物可为空——纯功能任务无人物语境。
"""
task = await _owned_task(task_id, owner_scope)
if task["status"] not in ("pending",):
raise AppError("invalid_request", "任务不在可派发状态。", status_code=409)
async with AsyncSessionLocal() as session:
row = await session.get(BackstageTask, task_id)
row.status = "running"
await session.commit()
from app.kernel import Kernel
kernel: Kernel | None = getattr(app_state, "kernel", None)
if kernel is None:
raise AppError("service_unavailable", "Kernel 未就绪。", status_code=503)
prompt = (
f"【受托任务】{task['title']}\n"
f"委托包:{task['brief']}\n"
f"验收标准:{task['acceptance_criteria'] or '按委托包目标验收'}\n"
f"停止条件:完成验收标准即止;不得扩展使命。"
)
run = await kernel.start_backstage_run(
character_id=task["character_id"],
prompt=prompt,
owner_id=owner_scope,
on_task_final=_make_finalizer(task_id, owner_scope),
)
async with AsyncSessionLocal() as session:
row = await session.get(BackstageTask, task_id)
row.run_id = run.id
await session.commit()
await one_qi.emit(
"task.dispatched", character_id=task["character_id"],
payload={"task_id": task_id, "run_id": run.id}, source="tiangong",
)
return {"task_id": task_id, "run_id": run.id, "status": "running"}
def _make_finalizer(task_id: str, owner_scope: str):
"""Run 完成回调:回填结果、进入待验收。"""
async def _finalize(content: str, status: str) -> None:
async with AsyncSessionLocal() as session:
row = await session.get(BackstageTask, task_id)
if row is None or row.owner_scope != owner_scope:
return
row.result_digest = (content or "")[:4000]
row.status = "awaiting_acceptance" if status == "completed" else "failed"
row.completed_at = utc_now()
await session.commit()
await _cascade_parent(task_id)
return _finalize
async def _cascade_parent(task_id: str) -> None:
async with AsyncSessionLocal() as session:
task = await session.get(BackstageTask, task_id)
if task is None or task.parent_task_id is None:
return
parent_id = task.parent_task_id
children = (
await session.execute(
select(BackstageTask).where(BackstageTask.parent_task_id == parent_id)
)
).scalars().all()
terminal = {"awaiting_acceptance", "accepted", "rejected", "failed"}
if children and all(c.status in terminal for c in children):
parent = await session.get(BackstageTask, parent_id)
if parent is not None and parent.status in ("pending", "running"):
parent.status = "awaiting_acceptance"
parent.result_digest = "\n".join(
f"〔{c.title}〕{c.result_digest[:400]}" for c in children
)
parent.completed_at = utc_now()
await session.commit()
async def accept(task_id: str, owner_scope: str, *, accepted: bool) -> dict:
"""按证据验收:accepted / rejected。"""
await _owned_task(task_id, owner_scope)
async with AsyncSessionLocal() as session:
row = await session.get(BackstageTask, task_id)
if row.status not in ("awaiting_acceptance", "failed"):
raise AppError("invalid_request", "任务不在待验收状态。", status_code=409)
row.status = "accepted" if accepted else "rejected"
await session.commit()
await one_qi.emit(
"task.accepted" if accepted else "task.rejected",
character_id=None, payload={"task_id": task_id}, source="tiangong",
)
return {"task_id": task_id, "status": row.status}
async def list_tasks(owner_scope: str, *, parent: str | None = "root") -> list[dict]:
"""任务清单(默认根任务;parent=None 全量)。"""
async with AsyncSessionLocal() as session:
stmt = select(BackstageTask).where(BackstageTask.owner_scope == owner_scope)
if parent == "root":
stmt = stmt.where(BackstageTask.parent_task_id.is_(None))
elif parent is not None:
stmt = stmt.where(BackstageTask.parent_task_id == parent)
rows = (await session.execute(stmt.order_by(BackstageTask.created_at.desc()))).scalars().all()
return [_task_dict(r) for r in rows]
async def _owned(character_id: str, owner_scope: str) -> None:
async with AsyncSessionLocal() as session:
character = await session.get(Character, character_id)
if character is None or character.owner_scope != owner_scope:
raise AppError("not_found", "人物不存在。", status_code=404)
async def _owned_task(task_id: str, owner_scope: str) -> dict:
async with AsyncSessionLocal() as session:
row = await session.get(BackstageTask, task_id)
if row is None or row.owner_scope != owner_scope:
raise AppError("not_found", "任务不存在。", status_code=404)
session.expunge(row)
return _task_dict(row)
def _task_dict(t: BackstageTask) -> dict:
return {
"id": t.id,
"character_id": t.character_id,
"parent_task_id": t.parent_task_id,
"title": t.title,
"brief": t.brief,
"acceptance_criteria": t.acceptance_criteria,
"status": t.status,
"result_digest": t.result_digest,
"evidence": json.loads(t.evidence_json or "{}"),
"run_id": t.run_id,
"created_at": t.created_at.isoformat() if t.created_at else None,
"completed_at": t.completed_at.isoformat() if t.completed_at else None,
}