9 Commits
22 changed files with 1210 additions and 15 deletions
+8
View File
@@ -15,3 +15,11 @@ HN_LISTEN_ADDRESS=0.0.0.0
HN_HTTPS_PORT=443
COMPOSE_PROJECT_NAME=hainuo-relay
HN_IMAGE=hainuo-relay:deploy-20261027
# Opt-in paired-client Work files; deployment defaults, not measured product promises.
HN_WORK_FILES_ENABLED=false
HN_WORK_FILE_MAX_BYTES=10485760
HN_WORK_FILE_BATCH_LIMIT=20
HN_WORK_FILE_RETENTION=86400
HN_WORK_FILE_OWNER_QUOTA_BYTES=536870912
HN_WORK_FILE_GLOBAL_QUOTA_BYTES=2147483648
+21
View File
@@ -0,0 +1,21 @@
# C 阶段服务端进度
2026-11-12,基线 2e0d598,分支 feat/remote-interaction-C-20261112。
首批实现:新增设备签名的 POST /work/v2/tasks/progress,持久记录状态及递增序号;旧 state 接口保持签名兼容;本人、设备、当前绑定、任务期限校验沿用既有授权。旧序号不覆盖新状态,重复状态不重复执行,同序号冲突返回 409,终态不回退。执行完成、结果接收、平台投递分别判断。
已实现状态卡片:原私聊消息下创建一张 interactive 卡片,PATCH 更新同一消息;5秒合并更新,每次扫描最多发送10次请求,单次扫描限100条最近一天内任务。卡片只含任务编号、执行状态与交付状态,不含正文和工具参数,不含远程批准按钮。任务编号与源消息从服务端账本取得。创建回执未知停止重建;已知消息ID的绝对状态更新失败可在后续周期更新到最新状态。卡片维护独立于结果投递任务,失败保留既有文字/附件交付。
尚未实现:流式正文、可信按钮回调与远程审批、持久下发队列和取消、完整故障恢复及多设备会话管理。当前状态表只存任务标识、枚举、序号和服务端时间,不存正文或凭据。
客户端配套分支同名,必须先升级 ECS 再升级客户端。当前未部署、未更改长连接订阅,也未新增平台权限;真实私有化平台卡片兼容性待后续探测和验收。
默认关闭卡片;测试部署使用 prepare-update --enable-task-cards,将 HN_WORK_TASK_CARDS_ENABLED=true 仅加到候选connector。回滚使用父计划原环境,绑定、密钥与存储不重建。此开关不代表已验证私有化平台支持 interactive 回复和消息 PATCH。旧客户端仍可用原 state 接口,但不能凭结果投递推断执行成功,缺失执行状态时卡片显示待核对。
## 验证记录
ECS卡片、签名状态、凭据和部署套件:125 passed,1 skipped(已有可选测试)。客户端任务账本/原生运行/契约:27 passed。新增联合源码测试:1 passed,真实客户端账本及轮询代码通过签名HTTP向ECS上报四次状态,创建一次卡片、更新三次,原生任务仅受理一次。平台响应和原生Agent使用合成适配;没有发送真实Work消息或操作业务数据,不能代替私有化平台真人验收。
已覆盖:乱序状态、终态回退、身份/解绑、5秒合并、创建回执丢失、更新失败恢复、跨worker占用、创建进程中断、过期任务、卡片故障不阻断文字结果、候选开关及回滚保留父环境。尚需上线验证平台卡片格式兼容、更新权限/限流及真实跨端显示。
断连超时补充:等待可信任务进度超过request_timeout(默认45秒)时记录独立的client_idle_timeout原因;卡片在后续5秒维护周期变红,显示“客户端失联,任务结果待核对”。硬性任务期限、一般unknown仍单独处理,不冒充客户端断连;已知执行终态不被失联覆盖。原因持久保存,后续经设备签名验证的当前序号进度可清除;不重新下发或执行任务。实际耗时受部署参数、卡片限流及平台可用性影响,45—50秒不是SLA。
+27
View File
@@ -0,0 +1,27 @@
# Work materials / artifact delivery (test deployment accepted)
Branch feat/work-files-20261110. Requires paired PC feat/client-foundation-20261110 and broker mode. Off by default. Existing users/keys/permissions are not migrated by this source change.
Flow: platform-verified private file messages stage encrypted materials; user sends all files, waits for receipts, then sends task instructions. The next task in that exact owner/device/binding/chat claims them. Device capability work-files-v1 is signed in its handshake; older PCs are told to upgrade instead of receiving a text-only task that silently omits files. In-progress downloads block dispatch; failed inputs remain explicit per-file records.
Separate /work/v2/files/list, /download, /upload, /state routes use current device signatures and task binding. Upload metadata is signed and supplied in X-Hainuo-File-Request; bytes have exact length/SHA256 and independent path size limits. No caller URL/recipient or arbitrary source message is accepted. Downloads are verified on PC. Output manifests are restricted to the current task outputs, not arbitrary local paths.
All material/output bytes are AES-GCM encrypted using the existing server keyring with scoped AAD. Server stores ciphertext in DB, so backups, disk capacity and retention matter. File content is cleared after confirmed send, expiry or binding discard. Metadata persists for idempotency. Per-owner/global quotas serialize checks across workers.
Output stages: ready -> uploading -> uploaded -> sending -> sent. Failure after a mutating platform call is unknown and never automatically replayed. Client lost upload receipt first queries original file state before re-upload; ECS received is not platform sent, and sent is not user read. Stale uploading/sending states become unknown. Uploading to Work and replying to the original source_message_id are separate durable steps.
Deployment defaults (configurable, not measured product commitments): 10MiB/file, 20/task, 24h retention, 512MiB/owner and 2GiB/global encrypted payload allowance. Encryption/base64 and DB backups require extra capacity. Verify corporate reverse proxy limits too. Formatted documents are not executed; macro/embedded/external active-content handling remains PC policy.
Platform adapters use GET im/v1/messages/{message_id}/resources/{file_key}?type=file; POST im/v1/files multipart; POST im/v1/messages/{message_id}/reply file content. Scope and private-platform compatibility must be checked against internal docs and real synthetic test files. These are bot message resource APIs, distinct from user-token docx media download.
Tests: synthetic platform events, encrypted routing over loopback WebSocket, signed PC requests to actual FastAPI routes, 3 Word/Excel/PDF materials, real local Office conversion, lost HTTP receipt recovery, one original-message file reply, cross-user/binding checks, conflicting IDs, quotas, body limits and invalid names. Mock platform acceptance does not prove real Work functionality.
Existing ECS parent confirmed by user: /home/cnooc/hainuo-broker-update-20261105-recovery, connector image hainuo-relay:broker-update-30585d6d28a18521, digest sha256:a32e13a92d2191ad0619a8f34e4703fd3e3ce8e44ca784f2b0d092cc49476b02. Before deployment verify router also matches parent, source branch availability and inactive tasks. prepare-update --enable-work-files creates a new plan with the flag; rollback keeps the parent's previous flag and existing key material. Do not run from the initial prepared plan or regenerate keyring. No production deployment performed.
## Live test acceptance, 2026-11-10
Candidate 79c15ee was activated on the test ECS using /home/cnooc/hainuo-broker-update-20261110-files, image hainuo-relay:broker-update-f6b1859625e89b5f. Connector/router reported healthy. This supersedes the historical pre-deployment status above. Future prepare-update operations must use this newly activated plan as parent; retain bindings and key material.
A real private Work task received Word, Excel and PDF materials, verified their hashes on the updated PC, and returned two generated files to the original conversation. Both remote file states were sent; text was replied. User screenshots confirmed both outputs opened in WPS: department totals 120 and 80, formula checks/chart consistent, and the report disclosed missing metadata and the 19.5 difference between source totals 200 and 219.5.
Acceptance is scoped to this task. Full installer release, network/restart faults, partial platform failures, and real multi-user/device acceptance remain pending. No master merge or new client installer is implied.
+6
View File
@@ -82,6 +82,12 @@ services:
HN_FEISHU_OAUTH_SCOPES: ${HN_FEISHU_OAUTH_SCOPES:-}
HN_ENROLLMENT_SIGNER_URL: http://enrollment-signer:8000
HN_REQUEST_TIMEOUT: ${HN_REQUEST_TIMEOUT:-180}
HN_WORK_FILES_ENABLED: ${HN_WORK_FILES_ENABLED:-false}
HN_WORK_FILE_MAX_BYTES: ${HN_WORK_FILE_MAX_BYTES:-10485760}
HN_WORK_FILE_BATCH_LIMIT: ${HN_WORK_FILE_BATCH_LIMIT:-20}
HN_WORK_FILE_RETENTION: ${HN_WORK_FILE_RETENTION:-86400}
HN_WORK_FILE_OWNER_QUOTA_BYTES: ${HN_WORK_FILE_OWNER_QUOTA_BYTES:-536870912}
HN_WORK_FILE_GLOBAL_QUOTA_BYTES: ${HN_WORK_FILE_GLOBAL_QUOTA_BYTES:-2147483648}
HTTP_PROXY: ''
HTTPS_PROXY: ''
ALL_PROXY: ''
+14 -2
View File
@@ -23,19 +23,31 @@ async def rate_limit(redis, bucket: str, limit=60, seconds=60):
class BodyLimitMiddleware:
def __init__(self, app, max_body=262144):
def __init__(self, app, max_body=262144, path_limits=None):
self.app, self.max_body = app, max_body
self.path_limits=path_limits or {}
async def __call__(self, scope, receive, send):
if scope["type"] != "http":
return await self.app(scope, receive, send)
if scope.get('path')in self.path_limits:
# Authenticate upload metadata before buffering its binary body.
class FileBodyTooLarge(Exception):pass
size=0
async def bounded():
nonlocal size
message=await receive();size+=len(message.get('body',b''))
if size>self.path_limits[scope['path']]:raise FileBodyTooLarge()
return message
try:return await self.app(scope,bounded,send)
except FileBodyTooLarge:return await JSONResponse({'detail':'body too large'},status_code=413)(scope,receive,send)
messages, size = [], 0
while True:
message = await receive()
if message["type"] == "http.disconnect":
return
size += len(message.get("body", b""))
if size > self.max_body:
if size > self.path_limits.get(scope.get('path'),self.max_body):
return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send)
messages.append(message)
if not message.get("more_body", False):
+13
View File
@@ -40,6 +40,13 @@ class Settings(BaseSettings):
max_devices: int = 1000
max_connections: int = 1000
max_result: int = 16000
work_files_enabled: bool = False
work_task_cards_enabled: bool = False
work_file_max_bytes: int = 10*1024*1024
work_file_batch_limit: int = 20
work_file_retention: int = 86400
work_file_owner_quota_bytes: int = 512*1024*1024
work_file_global_quota_bytes: int = 2*1024*1024*1024
allow_insecure_local: bool = False
enrollment_signer_url: str = ""
credential_mode: str = "legacy"
@@ -50,6 +57,12 @@ class Settings(BaseSettings):
@model_validator(mode="after")
def validate_runtime(self):
if self.work_task_cards_enabled and self.credential_mode!='broker':raise ValueError('task cards require broker mode')
if not 1024<=self.work_file_max_bytes<=100*1024*1024 or not 1<=self.work_file_batch_limit<=50 or not 300<=self.work_file_retention<=7*86400:
raise ValueError('invalid work file deployment limits')
if self.work_files_enabled and self.credential_mode!='broker':raise ValueError('work files require broker mode')
if not self.work_file_max_bytes<=self.work_file_owner_quota_bytes<=self.work_file_global_quota_bytes:
raise ValueError('invalid work file storage quotas')
if not 30 <= self.task_tracking_timeout <= 3600:
raise ValueError("task tracking timeout must be 30-3600 seconds")
if self.credential_mode not in {"legacy", "broker"}:
+53 -7
View File
@@ -58,6 +58,10 @@ class Pending:
progress: asyncio.Event = field(default_factory=asyncio.Event)
class ClientIdleTimeout(asyncio.TimeoutError):
"""No authenticated task progress arrived within the idle timeout."""
class Broker:
def __init__(self, settings, redis, store):
self.s, self.r, self.store = settings, redis, store
@@ -65,6 +69,8 @@ class Broker:
self.pending: dict[str, Pending] = {}
self.worker_id = uuid.uuid4().hex
self.lifecycle = None
self.files = None
self.task_cards = None
async def replies(self, pub):
async for message in pub.listen():
@@ -112,7 +118,8 @@ class Broker:
if item.future in done:
return item.future.result()
if not done:
raise asyncio.TimeoutError()
if remaining<=self.s.request_timeout:raise asyncio.TimeoutError()
raise ClientIdleTimeout()
finally:
progress.cancel()
with contextlib.suppress(asyncio.CancelledError):
@@ -144,6 +151,11 @@ class Broker:
except Exception:
self.store.audit(owner, "dispatch", host, "identity_rejected")
raise HTTPException(409, "device identity rejected") from None
file_capable=bool(self.files and proof.get('work_files')=='work-files-v1' and chat_id and source_message_id)
if self.files and self.files.pending_count(owner,host,device['binding_generation'],chat_id,True):
return {'status':'materials_receiving','turn_id':turn,'result':'材料仍在接收,尚未执行任务。请等附件接收回执后再发送任务要求。'}
if self.files and self.files.pending_count(owner,host,device['binding_generation'],chat_id) and not file_capable:
return {'status':'unavailable','turn_id':turn,'result':'材料已保留,但当前客户端尚未支持附件处理;请升级配套客户端后再发任务要求。'}
if not self.store.begin_turn(turn, owner, host):
return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"}
task = None
@@ -154,6 +166,12 @@ class Broker:
except ValueError:
self.store.finish(turn,"unbound")
return {"status":"unbound","turn_id":turn,"result":"绑定已变化,任务未下发。"}
if task and file_capable:
try:self.files.claim(task)
except ValueError:
self.store.finish(turn,'not_dispatched')
self.lifecycle.transition(turn,'inflight','completed')
return {'status':'not_dispatched','turn_id':turn,'result':'材料接收或设备绑定状态发生变化,尚未执行任务;请等附件回执后核对。'}
private = x25519.X25519PrivateKey.generate()
future = asyncio.get_running_loop().create_future()
self.pending[turn] = Pending(future, private, public, host, session)
@@ -161,6 +179,8 @@ class Broker:
"text": text, "read_only": True, "channel": "im",
"reply_key": raw_public(private.public_key())}
if task:
if file_capable:
payload['file_transfer']='work-files-v1'
if source_message_id:
payload['result_delivery']='durable-http-v1'
payload["task_context"] = {key:task[key] for key in ("environment","app_id","owner","host_id","binding_generation",
@@ -198,10 +218,12 @@ class Broker:
self.lifecycle.transition(turn,"completed","finished")
self.store.audit(owner, "instruction", host, result.get("status","unknown") if task else "completed")
return result
except asyncio.TimeoutError:
except asyncio.TimeoutError as error:
self.store.finish(turn, "unknown")
if task:
self.lifecycle.transition(turn,"inflight","unknown")
if isinstance(error,ClientIdleTimeout) and self.task_cards:
self.task_cards.mark_connection_lost(turn)
return {"status": "unknown", "turn_id": turn,
"result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"}
finally:
@@ -234,6 +256,9 @@ def create_app(settings=None, redis=None, platform=None):
lifecycle = DeviceLifecycle(store.lifecycle,r,broker)
from .task_delivery import TaskDelivery
deliveries = TaskDelivery(oauth,store.lifecycle,broker,f)
if s.work_files_enabled:
from .work_files import WorkFiles
broker.files=WorkFiles(s,oauth,store.lifecycle,f)
else:
oauth = OAuth(s, r, f)
oauth.store = store
@@ -275,15 +300,21 @@ def create_app(settings=None, redis=None, platform=None):
try:
await lifecycle.maintain()
await deliveries.recover_interrupted_sends()
if broker.files:await broker.files.flush()
await deliveries.flush()
except Exception:
# No credentials or raw exception text in background logs.
pass
lifecycle_task = asyncio.create_task(maintenance()) if lifecycle else None
async def card_maintenance():
while True:
await asyncio.sleep(5)
with contextlib.suppress(Exception):await deliveries.cards.flush()
cards_task=asyncio.create_task(card_maintenance()) if lifecycle and deliveries.cards else None
try:
yield
finally:
work = [reply_task,sweep_task,*background] + ([lifecycle_task] if lifecycle_task else [])
work = [reply_task,sweep_task,*background] + ([lifecycle_task] if lifecycle_task else []) + ([cards_task] if cards_task else [])
for task in work:
task.cancel()
await asyncio.gather(*work, return_exceptions=True)
@@ -297,11 +328,15 @@ def create_app(settings=None, redis=None, platform=None):
await r.aclose()
app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None)
app.add_middleware(BodyLimitMiddleware, max_body=s.max_body)
app.add_middleware(BodyLimitMiddleware, max_body=s.max_body,
path_limits={'/work/v2/files/upload':s.work_file_max_bytes}if broker.files else {})
app.include_router(oauth.router)
if lifecycle:
app.include_router(deliveries.router)
app.state.deliveries=deliveries
if broker.files:
app.include_router(broker.files.router)
app.state.files=broker.files
app.include_router(notification_router(s, r, store, f, oauth))
from .web_session import WebSessions
web_sessions = WebSessions(s, r, store, broker, f)
@@ -528,7 +563,8 @@ def create_app(settings=None, redis=None, platform=None):
message, sender = event.get("message", {}), event.get("sender", {})
owner = sender.get("sender_id", {}).get("open_id")
message_id = message.get("message_id", "")
if (message.get("chat_type") != "p2p" or message.get("message_type") != "text"
kind=message.get('message_type')
if (message.get("chat_type") != "p2p" or kind not in ({'text','file'}if broker.files else {'text'})
or sender.get("sender_type") != "user" or not isinstance(owner,str)
or not 0 < len(owner) <= 128 or any(ord(c) < 32 for c in owner)
or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)):
@@ -539,13 +575,23 @@ def create_app(settings=None, redis=None, platform=None):
or any(ord(c) < 32 for c in message["chat_id"])):
return
try:
text = json.loads(message["content"])["text"].strip()
if not text or len(text) > 8000:
content=json.loads(message['content'])
text = content['text'].strip() if kind=='text' else ''
if kind=='text' and (not text or len(text) > 8000):
return
# Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice.
event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest()
if not store.begin_turn(event_turn, owner, "event"):
return
if kind=='file':
try:
await broker.files.receive(owner,message['chat_id'],message_id,content)
result='材料已接收。可以继续发送其他材料,最后发出本次任务要求;暂未执行模型任务。'
except Exception:
result='这件附件未接收成功,请核对文件格式、大小、资源权限或设备绑定;其他已接收材料仍保留。'
try:await f.reply(message_id,result)
finally:store.finish(event_turn,'attachment_processed')
return
if text == "/绑定":
result = await make_binding_code(owner)
elif text == "/解绑":
+49
View File
@@ -122,6 +122,55 @@ class Feishu:
headers={"Authorization": f"Bearer {token}"},
json={"msg_type": "text", "content": json.dumps({"text": text}, ensure_ascii=False)})
async def reply_task_card(self,message_id,card,delivery_id,*,token):
import re
if not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',message_id):raise PlatformError('invalid reply reference')
data=await self._json('POST',f'/open-apis/im/v1/messages/{message_id}/reply',
headers={'Authorization':'Bearer '+token},json={'msg_type':'interactive',
'content':json.dumps(card,ensure_ascii=False),'uuid':delivery_id})
result=data.get('data',{}).get('message_id')
if not isinstance(result,str)or not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',result):raise PlatformError('card receipt invalid')
return result
async def update_task_card(self,message_id,card,*,token):
import re
if not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',message_id):raise PlatformError('invalid card reference')
await self._json('PATCH',f'/open-apis/im/v1/messages/{message_id}',
headers={'Authorization':'Bearer '+token},json={'content':json.dumps(card,ensure_ascii=False)})
async def download_message_file(self,message_id,file_key,max_bytes):
import re
if not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',message_id)or not re.fullmatch(r'[A-Za-z0-9_-]{1,256}',file_key):raise PlatformError('invalid file reference')
token=await self.tenant_token()
try:
async with self.http.stream('GET',f'/open-apis/im/v1/messages/{message_id}/resources/{file_key}',
params={'type':'file'},headers={'Authorization':'Bearer '+token},timeout=60)as response:
if response.status_code!=200 or response.headers.get('content-type','').lower().startswith(('application/json','text/html')):raise PlatformError('message file download rejected')
raw=bytearray()
async for chunk in response.aiter_bytes():
raw.extend(chunk)
if len(raw)>max_bytes:raise PlatformError('message file size limit')
if not raw:raise PlatformError('empty message file')
return bytes(raw)
except httpx.HTTPError:raise PlatformError('message file download failed')from None
async def upload_file(self,name,content,mime,*,token):
data=await self._json('POST','/open-apis/im/v1/files',headers={'Authorization':'Bearer '+token},
data={'file_type':'stream','file_name':name},files={'file':(name,content,mime)},timeout=60)
key=data.get('data',{}).get('file_key')
import re
if not isinstance(key,str)or not re.fullmatch(r'[A-Za-z0-9_-]{1,256}',key):raise PlatformError('file upload receipt invalid')
return key
async def reply_file(self,message_id,file_key,delivery_id,*,token):
import re
if not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',message_id):raise PlatformError('invalid reply reference')
data=await self._json('POST',f'/open-apis/im/v1/messages/{message_id}/reply',
headers={'Authorization':'Bearer '+token},json={'msg_type':'file','content':json.dumps({'file_key':file_key}), 'uuid':delivery_id})
message=data.get('data',{}).get('message_id')
if not isinstance(message,str)or not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',message):raise PlatformError('file message receipt invalid')
return message
async def send(self, owner, msg_type, content, delivery_id, *, token=None):
token = token or await self.tenant_token()
data = await self._json('POST', '/open-apis/im/v1/messages',
+1 -1
View File
@@ -1,4 +1,4 @@
"""Connector-owned persistent metadata. Never stores message/file/token bodies."""
"""Core routing metadata; adjunct services store payloads only as authenticated ciphertext."""
import sqlite3
import time
import psycopg
+112
View File
@@ -0,0 +1,112 @@
"""Durable private task status cards. No task body, credentials or approval buttons."""
import hashlib
import json
import time
EXECUTION={'queued':'排队中','running':'执行中','waiting_for_approval':'等待本人确认',
'completed':'执行完成','failed':'执行失败','cancelled':'已取消','interrupted':'已中断','unknown':'状态待核对'}
DELIVERY={None:'尚未接收结果','ready':'已接收结果,等待投递','sending':'正在投递结果',
'replied':'结果已发送到当前会话','unknown':'结果投递待核对','discarded':'结果已停止交付'}
def card_content(task_id,execution,delivery,files=None,connection_lost=False):
execution=execution if execution in EXECUTION else 'unknown'
color='green' if execution=='completed' else 'red' if execution in {'failed','interrupted'} else 'orange' if execution in {'waiting_for_approval','unknown'} else 'blue'
label=EXECUTION[execution]
if connection_lost and execution not in {'completed','failed','cancelled','interrupted'}:
color='red';label='客户端失联,任务结果待核对'
elements=[{'tag':'div','text':{'tag':'plain_text','content':'任务状态:'+label}},
{'tag':'div','text':{'tag':'plain_text','content':'文字回复:'+DELIVERY.get(delivery,'状态待核对')}},
{'tag':'note','elements':[{'tag':'plain_text','content':'任务编号:'+task_id}]}]
if execution=='waiting_for_approval':
elements.insert(1,{'tag':'div','text':{'tag':'plain_text','content':'请在客户端查看并确认具体操作。本卡片暂不提供远程批准。'}})
if execution=='completed' and delivery!='replied':
elements.insert(1,{'tag':'div','text':{'tag':'plain_text','content':'任务已执行完成,结果交付请看下方状态。'}})
return {'config':{'wide_screen_mode':True,'enable_forward':False},
'header':{'template':color,'title':{'tag':'plain_text','content':'海诺任务进度'}},'elements':elements}
class TaskCards:
def __init__(self,state,platform,worker_id,clock=time.time,files_enabled=False):
self.state,self.db,self.platform,self.worker_id,self.clock=state,state.db,platform,worker_id,clock
self.files_enabled=files_enabled
with self.db:
self.db.execute('''CREATE TABLE IF NOT EXISTS work_task_connection_v3(
task_id TEXT PRIMARY KEY,reason TEXT NOT NULL)''')
self.db.execute('''CREATE TABLE IF NOT EXISTS work_task_cards_v3(
task_id TEXT PRIMARY KEY,message_id TEXT NOT NULL,digest TEXT NOT NULL,
status TEXT NOT NULL,worker_id TEXT NOT NULL,updated_at DOUBLE PRECISION NOT NULL)''')
def mark_connection_lost(self,task_id):
with self.db:
self.db.execute("INSERT INTO work_task_connection_v3 VALUES(?,'client_idle_timeout') ON CONFLICT DO NOTHING",(task_id,))
def clear_connection_lost(self,task_id):
with self.db:self.db.execute('DELETE FROM work_task_connection_v3 WHERE task_id=?',(task_id,))
def valid(self,task):
device=self.state.store.device(task['host_id']) if task else None
return bool(device and not device['revoked'] and device['owner']==task['owner']
and device['binding_generation']==task['binding_generation'] and task['source_message_id'] and task['chat_id'])
def snapshot(self,task):
row=self.db.execute('SELECT execution_state FROM work_execution_state_v3 WHERE task_id=?',(task['turn_id'],)).fetchone()
delivery=self.db.execute('SELECT status FROM work_result_outbox_v2 WHERE task_id=?',(task['turn_id'],)).fetchone()
execution=row['execution_state'] if row else 'unknown'
if task['status']=='unknown' and execution not in {'completed','failed','cancelled','interrupted'}:execution='unknown'
if task['deadline']<=self.clock() and execution not in {'completed','failed','cancelled','interrupted'}:execution='unknown'
lost=self.db.execute('SELECT reason FROM work_task_connection_v3 WHERE task_id=?',(task['turn_id'],)).fetchone()
return card_content(task['turn_id'],execution,delivery['status'] if delivery else None,connection_lost=bool(lost))
async def flush(self):
# Bound one maintenance pass; business result delivery remains independent.
rows=self.db.execute('''SELECT * FROM work_tasks_v2 WHERE environment=? AND app_id=?
AND source_message_id<>'' AND chat_id<>'' AND updated_at>? ORDER BY updated_at DESC LIMIT 100''',
(self.state.environment,self.state.app_id,self.clock()-86400)).fetchall()
sent=0
for raw in rows:
if sent>=10:break
task=dict(raw)
if not self.valid(task):continue
card=self.snapshot(task)
digest=hashlib.sha256(json.dumps(card,sort_keys=True,ensure_ascii=True).encode()).hexdigest()
old=self.db.execute('SELECT * FROM work_task_cards_v3 WHERE task_id=?',(task['turn_id'],)).fetchone()
if old and (old['status']=='unknown' or old['digest']==digest or self.clock()-old['updated_at']<5):continue
if not old and task['deadline']<=self.clock():continue
try:token=await self.platform.tenant_token()
except Exception:continue
with self.db:
self.state._owner_lock(task['owner'])
task=self.state.task(task['turn_id'])
if not self.valid(task):continue
old=self.db.execute('SELECT * FROM work_task_cards_v3 WHERE task_id=?',(task['turn_id'],)).fetchone()
if old and old['status']=='sending':
# A lost creation receipt cannot safely become another card.
if self.clock()-old['updated_at']>=60:
self.db.execute("UPDATE work_task_cards_v3 SET status=? WHERE task_id=?",
('ready' if old['message_id'] else 'unknown',task['turn_id']))
continue
if old and (old['status']=='unknown' or old['digest']==digest or self.clock()-old['updated_at']<5):continue
# Recompute after token/lock awaits so an older observation cannot overwrite new status.
card=self.snapshot(task)
digest=hashlib.sha256(json.dumps(card,sort_keys=True,ensure_ascii=True).encode()).hexdigest()
message_id=old['message_id'] if old else ''
if not old:
self.db.execute('INSERT INTO work_task_cards_v3 VALUES(?,?,?,?,?,?)',
(task['turn_id'],'','','sending',self.worker_id,self.clock()))
else:
self.db.execute("UPDATE work_task_cards_v3 SET status='sending',worker_id=?,updated_at=? WHERE task_id=?",
(self.worker_id,self.clock(),task['turn_id']))
try:
sent+=1
if message_id:await self.platform.update_task_card(message_id,card,token=token)
else:
delivery_id=hashlib.sha256(('task-card-v3\0'+self.state.environment+'\0'+self.state.app_id+'\0'+task['turn_id']).encode()).hexdigest()[:32]
message_id=await self.platform.reply_task_card(task['source_message_id'],card,delivery_id,token=token)
status='ready';saved_digest=digest
except Exception:
status='ready' if message_id else 'unknown';saved_digest=old['digest'] if old else ''
with self.db:
self.db.execute("UPDATE work_task_cards_v3 SET message_id=?,digest=?,status=?,updated_at=? WHERE task_id=? AND status='sending' AND worker_id=?",
(message_id,saved_digest,status,self.clock(),task['turn_id'],self.worker_id))
+43
View File
@@ -17,12 +17,20 @@ class ResultRequest(TaskRequest):
result: str = Field(max_length=16000, repr=False)
class StateRequest(TaskRequest):
execution_state: str = Field(pattern=r'^(queued|running|waiting_for_approval|completed|failed|cancelled|interrupted|unknown)$')
status_sequence: int = Field(ge=1, le=2147483647, strict=True)
class TaskDelivery:
def __init__(self, auth, state, broker, platform):
self.auth,self.state,self.broker,self.platform=auth,state,broker,platform
self.db=state.db
self.keyring=auth.broker.store.keyring
with self.db:
self.db.execute('''CREATE TABLE IF NOT EXISTS work_execution_state_v3(
task_id TEXT PRIMARY KEY,execution_state TEXT NOT NULL,
status_sequence BIGINT NOT NULL,updated_at DOUBLE PRECISION NOT NULL)''')
self.db.execute("""CREATE TABLE IF NOT EXISTS work_result_outbox_v2(
task_id TEXT PRIMARY KEY,environment TEXT NOT NULL,app_id TEXT NOT NULL,
owner TEXT NOT NULL,host_id TEXT NOT NULL,binding_generation BIGINT NOT NULL,
@@ -30,7 +38,13 @@ class TaskDelivery:
status TEXT NOT NULL,updated_at DOUBLE PRECISION NOT NULL)""")
self.router=APIRouter()
self.router.add_api_route('/work/v2/tasks/state',self.status,methods=['POST'])
self.router.add_api_route('/work/v2/tasks/progress',self.progress,methods=['POST'])
self.router.add_api_route('/work/v2/tasks/result',self.submit,methods=['POST'])
self.cards=None
if auth.s.work_task_cards_enabled:
from .task_cards import TaskCards
self.cards=TaskCards(state,platform,broker.worker_id,files_enabled=auth.s.work_files_enabled)
broker.task_cards=self.cards
def current(self, body):
self.auth.require_binding(body.host_id,body.binding_owner,body.binding_generation)
@@ -49,7 +63,11 @@ class TaskDelivery:
def receipt(self,task):
row=self.db.execute('SELECT status,digest FROM work_result_outbox_v2 WHERE task_id=?',
(task['turn_id'],)).fetchone()
execution=self.db.execute('SELECT execution_state,status_sequence FROM work_execution_state_v3 WHERE task_id=?',(task['turn_id'],)).fetchone()
return {'task_id':task['turn_id'],'status':row['status'] if row else task['status'],
'progress_protocol':'work-progress-v1',
'execution_state':execution['execution_state'] if execution else 'unknown',
'status_sequence':execution['status_sequence'] if execution else 0,
'result_received':bool(row),'result_digest':row['digest'] if row else None,
'deadline':task['deadline']}
@@ -57,6 +75,31 @@ class TaskDelivery:
await self.auth.authenticate(body,request)
task=self.current(body)
pending=self.broker.pending.get(body.task_id)
if pending and pending.host==body.host_id:pending.progress.set()
return no_cache(JSONResponse(self.receipt(task)))
async def progress(self,body:StateRequest,request:Request):
await self.auth.authenticate(body,request)
with self.db:
self.state._owner_lock(body.binding_owner)
task=self.current(body)
if body.execution_state is not None:
old=self.db.execute('SELECT * FROM work_execution_state_v3 WHERE task_id=?',(body.task_id,)).fetchone()
terminal={'completed','failed','cancelled','interrupted'}
if old and body.status_sequence==old['status_sequence'] and body.execution_state!=old['execution_state']:
raise HTTPException(409,'execution_state_conflict')
if not old:
self.db.execute('INSERT INTO work_execution_state_v3 VALUES(?,?,?,?)',
(body.task_id,body.execution_state,body.status_sequence,time.time()))
elif body.status_sequence>old['status_sequence']:
if old['execution_state'] in terminal and body.execution_state!=old['execution_state']:
raise HTTPException(409,'execution_already_terminal')
self.db.execute('UPDATE work_execution_state_v3 SET execution_state=?,status_sequence=?,updated_at=? WHERE task_id=?',
(body.execution_state,body.status_sequence,time.time(),body.task_id))
pending=self.broker.pending.get(body.task_id)
if self.cards:
old=self.db.execute('SELECT status_sequence FROM work_execution_state_v3 WHERE task_id=?',(body.task_id,)).fetchone()
if old and body.status_sequence>=old['status_sequence']:self.cards.clear_connection_lost(body.task_id)
if pending and pending.host==body.host_id:
pending.progress.set()
return no_cache(JSONResponse(self.receipt(task)))
+229
View File
@@ -0,0 +1,229 @@
"""Opt-in, device/task-bound encrypted materials and artifact outbox."""
import base64
import hashlib
import json
import re
import time
from pathlib import PurePosixPath
from fastapi import APIRouter,HTTPException,Request
from fastapi.responses import JSONResponse,Response
from pydantic import Field
from .common import no_cache
from .crypto import canonical
from .task_delivery import TaskRequest
FORMATS={'.docx':'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'.xlsx':'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'.pptx':'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'.pdf':'application/pdf','.txt':'text/plain','.csv':'text/csv','.md':'text/markdown'}
def file_name(value):
if (not isinstance(value,str)or not 1<=len(value)<=200 or value!=PurePosixPath(value).name
or any(c in value for c in '\\/:\r\n\x00')or any(ord(c)<32 for c in value)
or value.endswith((' ','.'))or PurePosixPath(value).suffix.lower()not in FORMATS):
raise ValueError('unsupported_file_name_or_format')
if value.split('.')[0].upper()in {'CON','PRN','AUX','NUL',*('COM'+str(i)for i in range(1,10)),*('LPT'+str(i)for i in range(1,10))}:raise ValueError('unsupported_file_name_or_format')
return value
class FileRequest(TaskRequest):
file_id:str=Field(pattern=r'^[A-Za-z0-9_-]{8,100}$')
class UploadRequest(TaskRequest):
artifact_id:str=Field(pattern=r'^[A-Za-z0-9_-]{8,100}$')
name:str=Field(min_length=1,max_length=200)
sha256:str=Field(pattern=r'^[a-f0-9]{64}$')
size:int=Field(strict=True,ge=1)
class WorkFiles:
def __init__(self,settings,auth,state,platform):
self.s,self.auth,self.state,self.platform=settings,auth,state,platform
self.db=state.db;self.keyring=auth.broker.store.keyring
self.environment,self.app_id=state.environment,state.app_id
with self.db:
self.db.execute('CREATE TABLE IF NOT EXISTS work_file_quota_v1(environment TEXT NOT NULL,app_id TEXT NOT NULL,epoch BIGINT NOT NULL,PRIMARY KEY(environment,app_id))')
self.db.execute('''CREATE TABLE IF NOT EXISTS work_files_v1(
file_id TEXT PRIMARY KEY,environment TEXT NOT NULL,app_id TEXT NOT NULL,
owner TEXT NOT NULL,host_id TEXT NOT NULL,binding_generation BIGINT NOT NULL,
chat_id TEXT NOT NULL,task_id TEXT NOT NULL,source_message_id TEXT NOT NULL,
direction TEXT NOT NULL,name TEXT NOT NULL,size BIGINT NOT NULL,sha256 TEXT NOT NULL,
key_id TEXT NOT NULL,ciphertext TEXT NOT NULL,status TEXT NOT NULL,
platform_key TEXT NOT NULL,platform_message TEXT NOT NULL,
updated DOUBLE PRECISION NOT NULL,expires DOUBLE PRECISION NOT NULL)''')
self.router=APIRouter()
self.router.add_api_route('/work/v2/files/list',self.list_files,methods=['POST'])
self.router.add_api_route('/work/v2/files/download',self.download,methods=['POST'])
self.router.add_api_route('/work/v2/files/upload',self.upload,methods=['POST'])
self.router.add_api_route('/work/v2/files/state',self.file_state,methods=['POST'])
def _aad(self,row):
return canonical(['work-files-v1',*[row[k]for k in ('environment','app_id','owner','host_id','binding_generation',
'chat_id','source_message_id','direction','file_id','sha256','size','name')]])
def _id(self,kind,*parts):return kind+'_'+hashlib.sha256(canonical([self.environment,self.app_id,*parts])).hexdigest()
def reserve(self,owner,size):
# Called inside the owner's transaction. Serialize global quota checks
# across PostgreSQL workers as well as the SQLite test implementation.
scope=(self.environment,self.app_id)
self.db.execute('INSERT INTO work_file_quota_v1 VALUES(?,?,0) ON CONFLICT DO NOTHING',scope)
self.db.execute('UPDATE work_file_quota_v1 SET epoch=epoch+0 WHERE environment=? AND app_id=?',scope)
total=self.db.execute("SELECT COALESCE(SUM(size),0) AS n FROM work_files_v1 WHERE environment=? AND app_id=? AND ciphertext!='' AND expires>?",(*scope,time.time())).fetchone()['n']
own=self.db.execute("SELECT COALESCE(SUM(size),0) AS n FROM work_files_v1 WHERE environment=? AND app_id=? AND owner=? AND ciphertext!='' AND expires>?",(*scope,owner,time.time())).fetchone()['n']
if total+size>self.s.work_file_global_quota_bytes or own+size>self.s.work_file_owner_quota_bytes:raise HTTPException(413,'file_storage_quota')
def _insert(self,*,file_id,owner,host,generation,chat,task,message,direction,name,raw,status,reserved_size=None):
row=dict(file_id=file_id,environment=self.environment,app_id=self.app_id,owner=owner,
host_id=host,binding_generation=generation,chat_id=chat,task_id=task,source_message_id=message,
direction=direction,name=name,size=len(raw)if reserved_size is None else reserved_size,sha256=hashlib.sha256(raw).hexdigest())
key,cipher=self.keyring.encrypt(raw,self._aad(row))
self.db.execute('INSERT INTO work_files_v1 VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING',
(*[row[k]for k in ('file_id','environment','app_id','owner','host_id','binding_generation','chat_id','task_id',
'source_message_id','direction','name','size','sha256')],key,cipher,status,'','',time.time(),time.time()+self.s.work_file_retention))
return dict(self.db.execute('SELECT * FROM work_files_v1 WHERE file_id=?',(file_id,)).fetchone())
async def receive(self,owner,chat,message,content):
"""Only called after platform-event validation and persistent event receipt."""
device=self.state.store.by_owner(owner)
if not device:raise ValueError('bound_device_required')
name=file_name(content.get('file_name'));key=content.get('file_key')
if not isinstance(key,str)or not re.fullmatch(r'[A-Za-z0-9_-]{1,256}',key):raise ValueError('invalid_platform_file_reference')
identity=(owner,device['host_id'],device['binding_generation'])
fid=self._id('in',*identity,message)
with self.db:
self.state._owner_lock(owner)
self.auth.require_binding(device['host_id'],owner,device['binding_generation'])
if self.pending_count(owner,device['host_id'],device['binding_generation'],chat)>=self.s.work_file_batch_limit:raise ValueError('pending_material_count_limit')
old=self.db.execute('SELECT * FROM work_files_v1 WHERE file_id=?',(fid,)).fetchone()
if old:return self.metadata(dict(old))
self.reserve(owner,self.s.work_file_max_bytes)
self._insert(file_id=fid,owner=owner,host=device['host_id'],generation=device['binding_generation'],
chat=chat,task='',message=message,direction='input',name=name,raw=b'',status='receiving',reserved_size=self.s.work_file_max_bytes)
try:
raw=await self.platform.download_message_file(message,key,self.s.work_file_max_bytes)
if not raw or len(raw)>self.s.work_file_max_bytes:raise ValueError('file_size_limit')
with self.db:
self.state._owner_lock(owner)
self.auth.require_binding(device['host_id'],owner,device['binding_generation'])
row=dict(self.db.execute('SELECT * FROM work_files_v1 WHERE file_id=?',(fid,)).fetchone())
if row['status']!='receiving':raise ValueError('material_state_changed')
row.update(size=len(raw),sha256=hashlib.sha256(raw).hexdigest())
key_id,cipher=self.keyring.encrypt(raw,self._aad(row))
self.db.execute("UPDATE work_files_v1 SET size=?,sha256=?,key_id=?,ciphertext=?,status='staged',updated=? WHERE file_id=? AND status='receiving'",(len(raw),row['sha256'],key_id,cipher,time.time(),fid))
row['status']='staged'
return self.metadata(row)
except Exception:
with self.db:self.db.execute("UPDATE work_files_v1 SET status='failed',size=0,ciphertext='',updated=? WHERE file_id=? AND status='receiving'",(time.time(),fid))
raise
def pending_count(self,owner,host,generation,chat,receiving_only=False):
statuses=('receiving',)if receiving_only else('staged','receiving','failed')
placeholders=','.join('?'for _ in statuses)
return self.db.execute("SELECT COUNT(*) AS n FROM work_files_v1 WHERE environment=? AND app_id=? AND owner=? AND host_id=? AND binding_generation=? AND chat_id=? AND status IN ("+placeholders+") AND expires>?",(self.environment,self.app_id,owner,host,generation,chat,*statuses,time.time())).fetchone()['n']
def claim(self,task):
with self.db:
self.state._owner_lock(task['owner'])
if not self.state.task_current(task):raise ValueError('stale_task')
if self.pending_count(task['owner'],task['host_id'],task['binding_generation'],task['chat_id'],True):raise ValueError('materials_still_receiving')
self.db.execute("UPDATE work_files_v1 SET task_id=?,status=CASE WHEN status='failed' THEN 'failed_assigned' ELSE 'assigned' END,updated=? WHERE environment=? AND app_id=? AND owner=? AND host_id=? AND binding_generation=? AND chat_id=? AND direction='input' AND status IN ('staged','failed') AND expires>?",(task['turn_id'],time.time(),self.environment,self.app_id,task['owner'],task['host_id'],task['binding_generation'],task['chat_id'],time.time()))
def current(self,body):
self.auth.require_binding(body.host_id,body.binding_owner,body.binding_generation)
task=self.state.task(body.task_id)
if (not self.state.task_current(task)or not task['source_message_id']or
(task['owner'],task['host_id'],task['binding_generation'])!=(body.binding_owner,body.host_id,body.binding_generation)
or task['status']in {'unbound','revoked','result_discarded','offline'}):raise HTTPException(403,'task_not_recoverable')
return task
def metadata(self,row):
return {k:row[k]for k in ('file_id','direction','name','size','sha256','status')}
async def list_files(self,body:TaskRequest,request:Request):
await self.auth.authenticate(body,request);self.current(body)
rows=self.db.execute("SELECT * FROM work_files_v1 WHERE environment=? AND app_id=? AND task_id=? AND expires>? ORDER BY file_id",(self.environment,self.app_id,body.task_id,time.time())).fetchall()
return no_cache(JSONResponse({'protocol':'work-files-v1','files':[self.metadata(dict(r))for r in rows],
'max_bytes':self.s.work_file_max_bytes,'batch_limit':self.s.work_file_batch_limit}))
def selected(self,body):
task=self.current(body)
row=self.db.execute('SELECT * FROM work_files_v1 WHERE file_id=? AND task_id=? AND environment=? AND app_id=?',
(body.file_id,body.task_id,self.environment,self.app_id)).fetchone()
if not row or row['expires']<=time.time():raise HTTPException(404,'file_not_available')
if (row['owner'],row['host_id'],row['binding_generation'])!=(task['owner'],task['host_id'],task['binding_generation']):raise HTTPException(403,'file_not_available')
return dict(row)
async def download(self,body:FileRequest,request:Request):
await self.auth.authenticate(body,request);row=self.selected(body)
if row['direction']!='input' or row['status']!='assigned':raise HTTPException(403,'input_file_required')
raw=self.keyring.decrypt(row['key_id'],row['ciphertext'],self._aad(row))
if len(raw)!=row['size']or hashlib.sha256(raw).hexdigest()!=row['sha256']:raise HTTPException(409,'file_integrity_failed')
self.current(body)
return no_cache(Response(raw,media_type='application/octet-stream',headers={'X-Hainuo-File-SHA256':row['sha256']}))
async def upload(self,request:Request):
encoded=request.headers.get('X-Hainuo-File-Request','')
if not encoded or len(encoded)>24000:raise HTTPException(422,'file_metadata_required')
try:body=UploadRequest.model_validate(json.loads(base64.b64decode(encoded,altchars=b'-_',validate=True)))
except Exception:raise HTTPException(422,'invalid_file_metadata')from None
await self.auth.authenticate(body,request);task=self.current(body)
try:name=file_name(body.name)
except ValueError:raise HTTPException(422,'unsupported_file_name_or_format')from None
if body.size>self.s.work_file_max_bytes:raise HTTPException(413,'file_size_limit')
raw=await request.body()
if len(raw)!=body.size or hashlib.sha256(raw).hexdigest()!=body.sha256:raise HTTPException(409,'file_integrity_failed')
fid=self._id('out',task['turn_id'],body.artifact_id)
with self.db:
self.state._owner_lock(task['owner']);self.current(body)
old=self.db.execute('SELECT * FROM work_files_v1 WHERE file_id=?',(fid,)).fetchone()
if old and (old['expires']<=time.time()or old['status']in {'expired','discarded'}):raise HTTPException(410,'file_expired')
if not old:
count=self.db.execute("SELECT COUNT(*) AS n FROM work_files_v1 WHERE task_id=? AND direction='output'",(body.task_id,)).fetchone()['n']
if count>=self.s.work_file_batch_limit:raise HTTPException(413,'artifact_count_limit')
self.reserve(task['owner'],len(raw))
row=self._insert(file_id=fid,owner=task['owner'],host=task['host_id'],generation=task['binding_generation'],
chat=task['chat_id'],task=task['turn_id'],message=task['source_message_id'],direction='output',name=name,raw=raw,status='ready')
if (row['sha256'],row['size'],row['name'])!=(body.sha256,body.size,name):raise HTTPException(409,'artifact_conflict')
return no_cache(JSONResponse({'received':True,**self.metadata(row)}))
async def file_state(self,body:FileRequest,request:Request):
await self.auth.authenticate(body,request)
return no_cache(JSONResponse(self.metadata(self.selected(body))))
async def flush(self):
now=time.time()
with self.db:
self.db.execute("UPDATE work_files_v1 SET status='expired',ciphertext='',platform_key='' WHERE environment=? AND app_id=? AND expires<? AND ciphertext!=''",(self.environment,self.app_id,now))
self.db.execute("UPDATE work_files_v1 SET status='failed',size=0,ciphertext='',updated=? WHERE environment=? AND app_id=? AND status='receiving' AND updated<?",(now,self.environment,self.app_id,now-120))
# Interrupted mutating platform calls remain ambiguous, never replayed.
self.db.execute("UPDATE work_files_v1 SET status='unknown',updated=? WHERE environment=? AND app_id=? AND status IN ('uploading','sending') AND updated<?",(now,self.environment,self.app_id,now-120))
rows=self.db.execute("SELECT * FROM work_files_v1 WHERE environment=? AND app_id=? AND direction='output' AND status IN ('ready','uploaded') AND expires>? ORDER BY updated LIMIT 10",(self.environment,self.app_id,now)).fetchall()
for raw_row in rows:
row=dict(raw_row);task=self.state.task(row['task_id'])
if not self.state.task_current(task):
with self.db:self.db.execute("UPDATE work_files_v1 SET status='discarded',ciphertext='',platform_key='' WHERE file_id=?",(row['file_id'],))
continue
try:token=await self.platform.tenant_token()
except Exception:continue
with self.db:
self.state._owner_lock(row['owner'])
if not self.state.task_current(self.state.task(row['task_id'])):continue
working='uploading'if row['status']=='ready'else'sending'
changed=self.db.execute('UPDATE work_files_v1 SET status=?,updated=? WHERE file_id=? AND status=?',(working,time.time(),row['file_id'],row['status'])).rowcount
if changed!=1:continue
try:
if working=='uploading':
content=self.keyring.decrypt(row['key_id'],row['ciphertext'],self._aad(row))
if hashlib.sha256(content).hexdigest()!=row['sha256']:raise ValueError()
key=await self.platform.upload_file(row['name'],content,FORMATS[PurePosixPath(row['name']).suffix.lower()],token=token)
with self.db:self.db.execute("UPDATE work_files_v1 SET status='uploaded',platform_key=?,updated=? WHERE file_id=? AND status='uploading'",(key,time.time(),row['file_id']))
else:
response=await self.platform.reply_file(task['source_message_id'],row['platform_key'],hashlib.sha256(row['file_id'].encode()).hexdigest()[:32],token=token)
with self.db:self.db.execute("UPDATE work_files_v1 SET status='sent',platform_message=?,ciphertext='',updated=? WHERE file_id=? AND status='sending'",(response,time.time(),row['file_id']))
except Exception:
with self.db:self.db.execute("UPDATE work_files_v1 SET status='unknown',updated=? WHERE file_id=? AND status=?",(time.time(),row['file_id'],working))
+14 -2
View File
@@ -196,7 +196,7 @@ def validate_overlay(base, merged, candidate, router_key_required=True):
env = new[name]["environment"]
for key, value in old[name].get("environment", {}).items():
if key not in {"HN_CREDENTIAL_MODE","HN_ROUTER_ADMISSION_SECRET","HN_CREDENTIAL_KEYRING_FILE",
"HN_CREDENTIAL_PERMISSION_PROFILES","HN_CONNECTOR_INTERNAL_URL","HN_FEISHU_APP_ID","HN_OAUTH_ENABLED"}:
"HN_CREDENTIAL_PERMISSION_PROFILES","HN_CONNECTOR_INTERNAL_URL","HN_FEISHU_APP_ID","HN_OAUTH_ENABLED","HN_WORK_FILES_ENABLED","HN_WORK_TASK_CARDS_ENABLED"}:
if env.get(key) != value:
raise DeployError("existing environment changed: "+key)
old_volumes = {v["target"]:v for v in old[name].get("volumes", [])}
@@ -373,6 +373,10 @@ def prepare_update(args):
raise DeployError('update output must be a new absolute directory outside the live deployment')
if not (source/'relay/connector.py').is_file():
raise DeployError('missing update source')
if getattr(args,'enable_work_files',False)and not (source/'relay/work_files.py').is_file():
raise DeployError('work files implementation missing from update source')
if getattr(args,'enable_task_cards',False)and not (source/'relay/task_cards.py').is_file():
raise DeployError('task cards implementation missing from update source')
output=output.resolve()
output.mkdir(mode=0o700)
os.chmod(output,0o700)
@@ -389,8 +393,14 @@ def prepare_update(args):
for mount in overlay['services']['connector']['volumes']:
if mount['target']=='/run/hainuo-credentials/keyring.json':
mount['source']=str(output/'credential-keyring.json')
write_json(output/'compose.broker.json',overlay)
rollback=json.loads(json.dumps(overlay))
if getattr(args,'enable_work_files',False):
overlay['services']['connector']['environment']['HN_WORK_FILES_ENABLED']='true'
plan['work_files_enabled']=True
if getattr(args,'enable_task_cards',False):
overlay['services']['connector']['environment']['HN_WORK_TASK_CARDS_ENABLED']='true'
plan['work_task_cards_enabled']=True
write_json(output/'compose.broker.json',overlay)
for name in ('connector','router'):
rollback['services'][name]['image']=roles[name]['image']
# An update rollback must retain broker env/key mounts, never silently
@@ -708,6 +718,8 @@ def main():
update.add_argument("--plan",required=True)
update.add_argument("--source-root",default=str(Path(__file__).resolve().parents[1]))
update.add_argument("--output",required=True)
update.add_argument('--enable-work-files',action='store_true',help='Opt in after paired-client preparation; rollback retains the previous flag.')
update.add_argument('--enable-task-cards',action='store_true',help='Opt in to paired-client status cards; rollback retains the previous flag.')
permissions=sub.add_parser('update-permissions')
permissions.add_argument('--plan',required=True)
permissions.add_argument('--profiles',required=True)
+29
View File
@@ -643,3 +643,32 @@ def test_private_approved_profile_excludes_pending_review_scopes():
approved=set(json.loads((ROOT/'examples/work-product-profiles.approved.json').read_text())['current-config-v1'])
assert all_scopes-approved=={'contact:department.base:readonly','contact:department.organize:readonly','task:tasklist:write'}
assert len(approved)==20 and {'drive:drive','wiki:wiki','bitable:app','task:task:write'}<=approved
def test_prepare_update_file_feature_is_explicit_and_rollback_restores_parent_flag(scenario):
parent=prepared(scenario);root=scenario['output'];image='sha256:'+'a'*64
for name in ('connector','router'):scenario['roles'][name]['image']=image
deploy.write_json(root/'candidate.json',{'image':image,'tag':parent['candidate_image']})
output=root.parent/'update-files'
args=argparse.Namespace(plan=str(root),source_root=str(ROOT),output=str(output),enable_work_files=True)
deploy.prepare_update(args)
current=json.loads((output/'compose.broker.json').read_text())
rollback=json.loads((output/'compose.rollback.json').read_text())
old=json.loads((root/'compose.broker.json').read_text())
assert current['services']['connector']['environment']['HN_WORK_FILES_ENABLED']=='true'
assert rollback['services']['connector']['environment']==old['services']['connector']['environment']
assert (output/'credential-keyring.json').read_bytes()==(root/'credential-keyring.json').read_bytes()
def test_prepare_update_cards_are_opt_in_and_rollback_keeps_parent_environment(scenario):
parent=prepared(scenario);root=scenario['output'];image='sha256:'+'a'*64
for name in ('connector','router'):scenario['roles'][name]['image']=image
deploy.write_json(root/'candidate.json',{'image':image,'tag':parent['candidate_image']})
output=root.parent/'update-cards'
deploy.prepare_update(argparse.Namespace(plan=str(root),source_root=str(ROOT),output=str(output),enable_task_cards=True))
current=json.loads((output/'compose.broker.json').read_text())
rollback=json.loads((output/'compose.rollback.json').read_text())
old=json.loads((root/'compose.broker.json').read_text())
assert current['services']['connector']['environment']['HN_WORK_TASK_CARDS_ENABLED']=='true'
assert rollback['services']['connector']['environment']==old['services']['connector']['environment']
assert (output/'credential-keyring.json').read_bytes()==(root/'credential-keyring.json').read_bytes()
+2 -2
View File
@@ -74,7 +74,7 @@ class Platform:
@pytest_asyncio.fixture
async def env(tmp_path):
async def env(tmp_path,request):
ca_key, ca = issue_ca()
ca_path = tmp_path / "ca.pem"
ca_path.write_bytes(ca.public_bytes(serialization.Encoding.PEM))
@@ -89,7 +89,7 @@ async def env(tmp_path):
feishu_app_id="cli_test", feishu_app_secret="fake-app-secret", credential_mode="broker",
credential_keyring_file=keyring, credential_permission_profiles={"product-v1": ["read"], "product-v2": ["read", "write"]},
public_url="https://relay.example.test", feishu_api_base="https://work.example.test",
feishu_authorize_url="https://work.example.test/open-apis/authen/v1/index")
feishu_authorize_url="https://work.example.test/open-apis/authen/v1/index",work_files_enabled=bool(getattr(request,'param',False)))
redis = fakeredis.aioredis.FakeRedis(server=fakeredis.FakeServer(), decode_responses=True)
platform = Platform()
app = create_app(settings, redis, platform)
+2 -1
View File
@@ -52,7 +52,7 @@ async def routed(env, tmp_path):
await asyncio.wait_for(server_task,5)
async def device(env, host="a"*32, *, generation=None, legacy=False):
async def device(env, host="a"*32, *, generation=None, legacy=False, work_files=False):
ws = await connect(env["url"],proxy=None)
env["sessions"].append(ws)
challenge = json.loads(await ws.recv())["challenge"]
@@ -65,6 +65,7 @@ async def device(env, host="a"*32, *, generation=None, legacy=False):
plain.update(protocol_version=2,environment=env["settings"].environment,
app_id=env["settings"].feishu_app_id,binding_owner=row["owner"] or "ou_a",
binding_generation=row["binding_generation"] if generation is None else generation)
if work_files:plain['work_files']='work-files-v1'
await ws.send(json.dumps({"host_id":host,"cert":row["cert"],"proof":sign(plain,env["keys"][host])}))
accepted = json.loads(await asyncio.wait_for(ws.recv(),3))
assert accepted["type"] == "connected"
@@ -0,0 +1,51 @@
"""Joint source test: encrypted PC journal -> signed ECS progress -> card projection."""
import importlib
from pathlib import Path
import sys
import types
import httpx
import pytest
from test_credential_api import env,wire
from test_task_delivery import task
from test_task_cards import setup_cards
async def test_native_states_reach_signed_server_and_existing_card(env,tmp_path,monkeypatch):
client=Path(__file__).resolve().parents[3]/'hainuo-build-remote-C-20261112/integrations/work-relay/service/relay'
if not client.is_dir():pytest.skip('paired client source not present')
package=types.ModuleType('joint_pc_relay');package.__path__=[str(client)]
monkeypatch.setitem(sys.modules,'joint_pc_relay',package)
journal_module=importlib.import_module('joint_pc_relay.task_journal')
native=importlib.import_module('joint_pc_relay.hainuo1065')
monkeypatch.setattr(journal_module,'protect',lambda data:b'test:'+data)
monkeypatch.setattr(journal_module,'unprotect',lambda data:data[5:])
clock=[0.0]
# Patch only the native module clock, not shared time.monotonic used by event loop.
monkeypatch.setattr(native,'time',types.SimpleNamespace(monotonic=lambda:clock[0],time=__import__('time').time))
async def no_wait(_):clock[0]+=1
monkeypatch.setattr(native,'asyncio',types.SimpleNamespace(sleep=no_wait))
context={'task_id':'task-durable','conversation_id':'synthetic-conversation'}
ledger=journal_module.TaskJournal(tmp_path);ledger.begin(context,'synthetic')
task(env);cards,calls,card_clock=setup_cards(env,monkeypatch)
states=iter(['running','waiting_for_approval','running','completed']);reported=[];posts=[]
run_id='run_'+'a'*32
def handler(request):
if request.method=='POST':posts.append(request);return httpx.Response(202,json={'run_id':run_id})
return httpx.Response(200,json={'run_id':run_id,'status':next(states),'output':'synthetic result'})
config=tmp_path/'native.json';config.write_text(__import__('json').dumps({'url':'http://127.0.0.1:8643','api_key':'fake-'*10,'mode':'text-only'}))
adapter=native.Hainuo1065Agent(config,transport=httpx.MockTransport(handler));adapter.model='synthetic'
async def progress():
saved=ledger.get(context);path='/work/v2/tasks/progress'
response=await env['client'].post(path,json=wire(env,path,task_id=context['task_id'],
execution_state=saved['execution_state'],status_sequence=saved['status_sequence']))
assert response.status_code==200,response.text
reported.append(response.json()['execution_state'])
card_clock[0]+=6;await cards.flush()
try:
assert await adapter._execute_run('synthetic',{'X-Hermes-Session-Id':'synthetic-session'},progress,journal=ledger,context=context)=='synthetic result'
assert reported==['running','waiting_for_approval','running','completed']
assert ledger.get(context)['status_sequence']==4
assert len(posts)==1
assert [call[0] for call in calls]==['create','update','update','update']
assert len(env['platform'].replies)==0 # Execution completion is not result delivery.
finally:await adapter.close()
+207
View File
@@ -0,0 +1,207 @@
import asyncio
import json
import time
import httpx
import pytest
from test_credential_api import env,wire
from test_task_delivery import task,upload
from relay.task_cards import TaskCards,card_content
from relay.platform import Feishu,PlatformError
def setup_cards(env,monkeypatch):
calls=[];clock=[time.time()]
async def create(message,card,delivery_id,*,token):
calls.append(('create',message,card,delivery_id));return 'om_card'
async def update(message,card,*,token):calls.append(('update',message,card))
monkeypatch.setattr(env['platform'],'reply_task_card',create,raising=False)
monkeypatch.setattr(env['platform'],'update_task_card',update,raising=False)
return TaskCards(env['store'].lifecycle,env['platform'],'worker-test',clock=lambda:clock[0]),calls,clock
async def report(env,state,sequence):
path='/work/v2/tasks/progress'
response=await env['client'].post(path,json=wire(env,path,task_id='task-durable',execution_state=state,status_sequence=sequence))
assert response.status_code==200,response.text
async def test_card_created_once_updates_in_order_and_delivery_separate(env,monkeypatch):
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
await report(env,'running',1);await cards.flush();await cards.flush()
assert len(calls)==1 and calls[0][0]=='create' and calls[0][1]=='om-synthetic'
await report(env,'waiting_for_approval',2)
await cards.flush();assert len(calls)==1 # Update throttle.
clock[0]+=6;await cards.flush()
assert calls[-1][0]=='update' and '等待本人确认' in json.dumps(calls[-1],ensure_ascii=False)
assert 'button' not in json.dumps(calls[-1])
await report(env,'completed',3);clock[0]+=6;await cards.flush()
text=json.dumps(calls[-1],ensure_ascii=False)
assert '执行完成' in text and '尚未接收结果' in text and '结果已发送' not in text
await upload(env);await env['app'].state.deliveries.flush()
clock[0]+=6;await cards.flush()
assert '结果已发送到当前会话' in json.dumps(calls[-1],ensure_ascii=False)
assert len([c for c in calls if c[0]=='create'])==1
async def test_unknown_create_receipt_is_durable_and_never_recreated(env,monkeypatch):
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
async def lost(*args,**kwargs):calls.append(('lost',));raise TimeoutError()
monkeypatch.setattr(env['platform'],'reply_task_card',lost)
await report(env,'running',1);await cards.flush()
clock[0]+=65
reopened=TaskCards(env['store'].lifecycle,env['platform'],'new-worker',clock=lambda:clock[0])
await report(env,'completed',2);await reopened.flush()
assert len(calls)==1
assert env['store'].db.execute('SELECT status FROM work_task_cards_v3').fetchone()['status']=='unknown'
await upload(env);await env['app'].state.deliveries.flush()
assert len(env['platform'].replies)==1 # Card failure never suppresses result text.
async def test_failed_update_retries_same_card_with_newest_state(env,monkeypatch):
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
await report(env,'running',1);await cards.flush()
original=env['platform'].update_task_card
async def lost(*args,**kwargs):calls.append(('lost-update',args[0]));raise TimeoutError()
monkeypatch.setattr(env['platform'],'update_task_card',lost)
await report(env,'waiting_for_approval',2);clock[0]+=6;await cards.flush()
monkeypatch.setattr(env['platform'],'update_task_card',original)
await report(env,'failed',3);clock[0]+=6;await cards.flush()
assert calls[-1][0:2]==('update','om_card')
assert '执行失败' in json.dumps(calls[-1],ensure_ascii=False)
assert len([c for c in calls if c[0]=='create'])==1
async def test_unbind_during_token_preflight_prevents_card_send(env,monkeypatch):
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
async def token():env['store'].unbind('ou_a');return 'fake'
monkeypatch.setattr(env['platform'],'tenant_token',token)
await cards.flush();assert not calls
async def test_two_workers_and_stale_snapshot_cannot_send_old_state(env,monkeypatch):
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
await report(env,'running',1)
paused=asyncio.Event();resume=asyncio.Event()
original=env['platform'].reply_task_card
async def create(*args,**kwargs):paused.set();await resume.wait();return await original(*args,**kwargs)
monkeypatch.setattr(env['platform'],'reply_task_card',create)
first=asyncio.create_task(cards.flush());await paused.wait()
other=TaskCards(env['store'].lifecycle,env['platform'],'worker-other',clock=lambda:clock[0])
await other.flush();assert not calls
resume.set();await first
assert len(calls)==1
await report(env,'waiting_for_approval',2)
async def token():
# A new authenticated status arrives while card worker awaits preflight.
await report(env,'completed',3);return 'fake'
monkeypatch.setattr(env['platform'],'tenant_token',token)
clock[0]+=6;await other.flush()
assert '执行完成' in json.dumps(calls[-1],ensure_ascii=False)
async def test_stale_creation_claim_is_unknown_but_stale_update_can_recover(env,monkeypatch):
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
db=env['store'].db
with db:db.execute('INSERT INTO work_task_cards_v3 VALUES(?,?,?,?,?,?)',('task-durable','','','sending','dead',clock[0]-61))
await cards.flush();await cards.flush();assert not calls
assert db.execute('SELECT status FROM work_task_cards_v3').fetchone()['status']=='unknown'
with db:db.execute("UPDATE work_task_cards_v3 SET message_id='om_card',status='sending',updated_at=?",(clock[0]-61,))
await cards.flush();await cards.flush()
assert calls[0][0:2]==('update','om_card')
def test_card_projection_never_exposes_task_body_or_markdown_controls():
data=card_content('synthetic-task','waiting_for_approval',None)
text=json.dumps(data,ensure_ascii=False)
assert 'button' not in text and 'markdown' not in text
assert data['config']['enable_forward'] is False
def test_text_delivery_never_claims_attachments_delivered():
text=json.dumps(card_content('synthetic','completed','replied',[]),ensure_ascii=False)
assert '文字回复:' in text and '附件交付' not in text and '未收到登记' not in text
mixed=json.dumps(card_content('synthetic','completed','replied',['sent','ready','unknown']),ensure_ascii=False)
assert '附件交付' not in mixed and '待投递1件' not in mixed
@pytest.mark.parametrize('state,color',[
('running','blue'),('waiting_for_approval','orange'),('unknown','orange'),
('completed','green'),('failed','red'),('interrupted','red')])
def test_execution_color_is_independent_of_text_delivery(state,color):
card=card_content('synthetic',state,'replied',[])
assert card['header']['template']==color
if state=='interrupted':
assert '已中断' in json.dumps(card,ensure_ascii=False)
async def test_card_uses_task_bound_source_and_skips_foreign_and_expired(env,monkeypatch):
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
with env['store'].db:
env['store'].db.execute('UPDATE work_tasks_v2 SET deadline=?',(clock[0]-1,))
await cards.flush();assert not calls
with env['store'].db:
env['store'].db.execute("UPDATE work_tasks_v2 SET deadline=?,binding_generation=999",(clock[0]+300,))
await cards.flush();assert not calls
async def test_timeout_projects_unknown_without_claiming_failure(env,monkeypatch):
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
await report(env,'running',1);await cards.flush()
with env['store'].db:env['store'].db.execute("UPDATE work_tasks_v2 SET status='unknown'")
clock[0]+=6;await cards.flush()
text=json.dumps(calls[-1],ensure_ascii=False)
assert '状态待核对' in text and '执行失败' not in text
async def test_lost_client_turns_red_persists_and_recovers_on_authenticated_progress(env,monkeypatch):
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
api=env['app'].state.deliveries;api.cards=cards
await report(env,'running',1);await cards.flush()
cards.mark_connection_lost('task-durable')
with env['store'].db:env['store'].db.execute("UPDATE work_tasks_v2 SET status='unknown'")
clock[0]+=6;await cards.flush()
card=calls[-1][2]
assert card['header']['template']=='red'
text=json.dumps(card,ensure_ascii=False)
assert '客户端失联,任务结果待核对' in text and '执行失败' not in text
reopened=TaskCards(env['store'].lifecycle,env['platform'],'worker-new',clock=lambda:clock[0])
assert reopened.snapshot(env['store'].lifecycle.task('task-durable'))['header']['template']=='red'
await report(env,'completed',2);clock[0]+=6;await cards.flush()
assert calls[-1][2]['header']['template']=='green'
assert not env['store'].db.execute('SELECT * FROM work_task_connection_v3').fetchall()
async def test_idle_timeout_is_distinct_from_hard_tracking_limit():
from types import SimpleNamespace
from relay.connector import Broker,Pending,ClientIdleTimeout
broker=object.__new__(Broker)
broker.s=SimpleNamespace(request_timeout=.01,task_tracking_timeout=1)
item=Pending(asyncio.get_running_loop().create_future(),None,None,'synthetic','synthetic')
with pytest.raises(ClientIdleTimeout):await broker.wait_tracked(item)
broker.s.task_tracking_timeout=0
with pytest.raises(asyncio.TimeoutError)as error:await broker.wait_tracked(item)
assert not isinstance(error.value,ClientIdleTimeout)
@pytest.mark.parametrize('state',['completed','failed','cancelled','interrupted'])
def test_lost_connection_never_overwrites_known_terminal_state(state):
card=card_content('synthetic',state,None,connection_lost=True)
assert '客户端失联' not in json.dumps(card,ensure_ascii=False)
async def test_platform_card_wire_format_and_receipt_validation(env):
seen=[]
def handler(request):
seen.append(request)
return httpx.Response(200,json={'code':0,'data':{'message_id':'om_receipt'}})
platform=Feishu(env['settings'],transport=httpx.MockTransport(handler))
card=card_content('synthetic','running',None)
try:
assert await platform.reply_task_card('om_source',card,'a'*32,token='fake')=='om_receipt'
await platform.update_task_card('om_receipt',card,token='fake')
body=json.loads(seen[0].content)
assert body['msg_type']=='interactive' and json.loads(body['content'])==card
assert seen[1].method=='PATCH' and seen[1].url.path.endswith('/om_receipt')
with pytest.raises(PlatformError):await platform.reply_task_card('../other',card,'a'*32,token='fake')
assert len(seen)==2
finally:await platform.close()
+34
View File
@@ -16,6 +16,40 @@ async def upload(env,turn='task-durable',text='synthetic-private-result'):
return await env['client'].post(path,json=wire(env,path,task_id=turn,result=text))
async def test_execution_status_order_replay_terminal_and_delivery_are_separate(env):
task(env)
path='/work/v2/tasks/progress'
async def report(state,sequence,turn='task-durable'):
return await env['client'].post(path,json=wire(env,path,task_id=turn,
execution_state=state,status_sequence=sequence))
assert (await report('running',1)).json()['execution_state']=='running'
waiting=await report('waiting_for_approval',2)
assert waiting.json()['status_sequence']==2
assert (await report('running',1)).json()['execution_state']=='waiting_for_approval'
assert (await report('waiting_for_approval',2)).status_code==200
assert (await report('running',2)).status_code==409
completed=await report('completed',3)
assert completed.json()['execution_state']=='completed'
assert not completed.json()['result_received']
assert (await report('running',4)).status_code==409
await upload(env)
assert (await report('completed',3)).json()['result_received']
async def test_execution_state_auth_binding_and_parameter_validation(env):
task(env)
path='/work/v2/tasks/progress'
missing=await env['client'].post(path,json=wire(env,path,task_id='task-durable',execution_state='running'))
assert missing.status_code==422
task(env,'foreign-task','ou_b','b'*32)
foreign=await env['client'].post(path,json=wire(env,path,task_id='foreign-task',execution_state='running',status_sequence=1))
assert foreign.status_code==403
env['store'].unbind('ou_a')
revoked=await env['client'].post(path,json=wire(env,path,task_id='task-durable',execution_state='running',status_sequence=1))
assert revoked.status_code==403
assert not env['store'].db.execute('SELECT * FROM work_execution_state_v3').fetchall()
async def test_result_is_encrypted_durable_and_delivered_once_after_worker_loss(env):
task(env)
api=env['app'].state.deliveries
+154
View File
@@ -0,0 +1,154 @@
import asyncio
import base64
import hashlib
import json
import time
from pathlib import Path
import pytest
from test_credential_api import env,wire
from relay.work_files import WorkFiles
pytestmark=pytest.mark.parametrize('env',[True],indirect=True)
def create_task(env,name='task-files',owner='ou_a',host='a'*32,chat='chat-files'):
generation=env['store'].device(host)['binding_generation']
return env['store'].lifecycle.create_task(name,owner,host,generation,'session','worker',time.time()+600,chat,'om-original')
def fake_platform(env,monkeypatch):
files={};calls=[]
async def download(message,key,limit):calls.append(('download',message,key));return b'PRIVATE_MATERIAL_'+key.encode()
async def upload(name,raw,mime,*,token):
calls.append(('upload',name));key='file_'+hashlib.sha256(raw).hexdigest();files[key]=raw;return key
async def reply(message,key,delivery,*,token):calls.append(('reply_file',message,key,delivery));return 'om-file-reply'
monkeypatch.setattr(env['platform'],'download_message_file',download,raising=False)
monkeypatch.setattr(env['platform'],'upload_file',upload,raising=False)
monkeypatch.setattr(env['platform'],'reply_file',reply,raising=False)
return files,calls
async def upload(env,task='task-files',raw=b'PRIVATE_GENERATED_RESULT',artifact='artifact-01',**overrides):
path='/work/v2/files/upload'
data=dict(task_id=task,artifact_id=artifact,name='result.txt',sha256=hashlib.sha256(raw).hexdigest(),size=len(raw));data.update(overrides)
body=wire(env,path,**data)
return await env['client'].post(path,content=raw,headers={'X-Hainuo-File-Request':base64.urlsafe_b64encode(json.dumps(body).encode()).decode()})
async def test_material_event_is_staged_without_model_and_duplicate_does_not_redownload(env,monkeypatch):
_,calls=fake_platform(env,monkeypatch)
event={'header':{'event_type':'im.message.receive_v1','app_id':'cli_test'},'event':{
'sender':{'sender_type':'user','sender_id':{'open_id':'ou_a'}},
'message':{'chat_type':'p2p','chat_id':'chat-files','message_type':'file','message_id':'om-input',
'content':json.dumps({'file_name':'source.txt','file_key':'file-source'})}}}
for _ in range(2):
assert (await env['client'].post('/internal/events',json=event,headers={'X-Internal-Secret':env['settings'].internal_secret})).status_code==200
await asyncio.sleep(.02)
assert len([x for x in calls if x[0]=='download'])==1
row=env['store'].db.execute('SELECT * FROM work_files_v1').fetchone()
assert row['status']=='staged' and row['task_id']==''
assert 'PRIVATE_MATERIAL'not in str(dict(row))
assert env['store'].db.execute('SELECT COUNT(*) AS n FROM work_tasks_v2').fetchone()['n']==0
assert len(env['platform'].replies)==1
async def test_claim_download_and_cross_owner_rejection(env,monkeypatch):
fake_platform(env,monkeypatch);api=env['app'].state.files
incoming=await api.receive('ou_a','chat-files','om-input',{'file_name':'source.txt','file_key':'file-source'})
task=create_task(env);api.claim(task)
path='/work/v2/files/list';listed=await env['client'].post(path,json=wire(env,path,task_id='task-files'))
assert listed.json()['files'][0]['file_id']==incoming['file_id']
path='/work/v2/files/download';body=wire(env,path,task_id='task-files',file_id=incoming['file_id'])
received=await env['client'].post(path,json=body)
assert received.content==b'PRIVATE_MATERIAL_file-source'
assert received.headers['X-Hainuo-File-SHA256']==hashlib.sha256(received.content).hexdigest()
bad=await env['client'].post(path,json=wire(env,path,host='b'*32,task_id='task-files',file_id=incoming['file_id']))
assert bad.status_code==403
assert (await env['client'].post(path,json=body)).status_code==409
async def test_artifact_handoff_encrypted_idempotent_and_replies_to_original_only(env,monkeypatch):
files,calls=fake_platform(env,monkeypatch);create_task(env)
first=await upload(env);assert first.status_code==200 and first.json()['received']
assert (await upload(env)).json()['file_id']==first.json()['file_id']
row=env['store'].db.execute("SELECT * FROM work_files_v1 WHERE direction='output'").fetchone()
assert 'PRIVATE_GENERATED_RESULT'not in str(dict(row))
assert (await upload(env,raw=b'changed')).status_code==409
api=WorkFiles(env['settings'],env['api'],env['store'].lifecycle,env['platform'])
await api.flush();await api.flush();await api.flush()
assert len([x for x in calls if x[0]=='upload'])==1
replies=[x for x in calls if x[0]=='reply_file'];assert len(replies)==1 and replies[0][1]=='om-original'
assert files[replies[0][2]]==b'PRIVATE_GENERATED_RESULT'
assert env['store'].db.execute("SELECT status,ciphertext FROM work_files_v1").fetchone()['status']=='sent'
async def test_bad_digest_filename_size_and_stale_binding_are_rejected(env,monkeypatch):
fake_platform(env,monkeypatch);create_task(env)
assert (await upload(env,sha256='0'*64)).status_code==409
assert (await upload(env,name='../secret.txt')).status_code==422
assert (await upload(env,size=env['settings'].work_file_max_bytes+1)).status_code==413
env['store'].unbind('ou_a')
assert (await upload(env)).status_code==403
assert env['store'].db.execute('SELECT COUNT(*) AS n FROM work_files_v1').fetchone()['n']==0
async def test_ambiguous_platform_upload_or_send_never_repeats(env,monkeypatch):
_,calls=fake_platform(env,monkeypatch);create_task(env);assert (await upload(env)).status_code==200
async def uncertain(*a,**kw):calls.append(('uncertain',));raise ConnectionError('PRIVATE_PLATFORM_ERROR')
monkeypatch.setattr(env['platform'],'upload_file',uncertain)
api=env['app'].state.files;await api.flush();await api.flush()
assert len([x for x in calls if x[0]=='uncertain'])==1
assert env['store'].db.execute('SELECT status FROM work_files_v1').fetchone()['status']=='unknown'
async def test_unbind_before_delivery_discards_without_platform_upload(env,monkeypatch):
_,calls=fake_platform(env,monkeypatch);create_task(env);await upload(env)
env['store'].unbind('ou_a');await env['app'].state.files.flush()
assert not calls
row=env['store'].db.execute('SELECT status,ciphertext FROM work_files_v1').fetchone()
assert row['status']=='discarded'and row['ciphertext']==''
async def test_file_body_limit_is_separate_from_json_limit(env,monkeypatch):
fake_platform(env,monkeypatch);create_task(env)
raw=b'x'*(env['settings'].max_body+1)
assert (await upload(env,raw=raw)).status_code==200
response=await env['client'].post('/work/v2/files/list',content=b'x'*(env['settings'].max_body+1))
assert response.status_code==413
async def test_receiving_material_blocks_dispatch_and_failed_input_is_explicit(env,monkeypatch):
fake_platform(env,monkeypatch);api=env['app'].state.files;entered=asyncio.Event();resume=asyncio.Event()
async def slow(*args):entered.set();await resume.wait();raise ConnectionError('synthetic download failed')
monkeypatch.setattr(env['platform'],'download_message_file',slow)
receiving=asyncio.create_task(api.receive('ou_a','chat-files','om-slow',{'file_name':'source.txt','file_key':'file-slow'}))
await entered.wait()
assert api.pending_count('ou_a','a'*32,env['store'].device('a'*32)['binding_generation'],'chat-files',True)==1
task=create_task(env)
with pytest.raises(ValueError,match='receiving'):api.claim(task)
resume.set()
with pytest.raises(ConnectionError):await receiving
api.claim(task)
row=env['store'].db.execute('SELECT status,task_id,ciphertext FROM work_files_v1').fetchone()
assert row['status']=='failed_assigned'and row['task_id']=='task-files'and row['ciphertext']==''
async def test_owner_quota_blocks_storage_without_upload_or_download(env,monkeypatch):
_,calls=fake_platform(env,monkeypatch);api=env['app'].state.files;create_task(env)
monkeypatch.setattr(env['settings'],'work_file_owner_quota_bytes',10)
response=await upload(env)
assert response.status_code==413 and not calls
from fastapi import HTTPException
with pytest.raises(HTTPException)as error:await api.receive('ou_a','chat-files','om-quota',{'file_name':'source.txt','file_key':'file-source'})
assert error.value.status_code==413 and not calls
async def test_lost_platform_message_receipt_is_unknown_without_repeat(env,monkeypatch):
_,calls=fake_platform(env,monkeypatch);create_task(env);await upload(env)
api=env['app'].state.files;await api.flush()
async def unknown(*args,**kw):calls.append(('reply_unknown',));raise ConnectionError('lost message receipt')
monkeypatch.setattr(env['platform'],'reply_file',unknown)
await api.flush();await api.flush()
assert len([x for x in calls if x[0]=='reply_unknown'])==1
assert env['store'].db.execute('SELECT status FROM work_files_v1').fetchone()['status']=='unknown'
@@ -0,0 +1,92 @@
"""Optional cross-checkout test using actual PC transport/signatures, no real Work."""
import asyncio
import importlib.util
import importlib
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import time
from types import SimpleNamespace
import httpx
import pytest
from test_credential_api import env
from test_work_files import create_task,fake_platform
from relay.crypto import pem_private
pytestmark=pytest.mark.parametrize('env',[True],indirect=True)
async def test_three_materials_native_output_lost_receipt_and_original_file_reply(env,tmp_path,monkeypatch):
source=os.environ.get('HN_PC_FILES_SOURCE')
if not source:pytest.skip('explicit PC source checkout required')
source=Path(source);package=source/'integrations/work-relay/service/relay'
spec=importlib.util.spec_from_file_location('_pc_work_file_chain',package/'__init__.py',submodule_search_locations=[str(package)])
module=importlib.util.module_from_spec(spec);sys.modules[spec.name]=module;spec.loader.exec_module(module)
files_module=importlib.import_module(spec.name+'.work_files')
credentials=importlib.import_module(spec.name+'.credential_provider')
office_spec=importlib.util.spec_from_file_location('_office_chain',source/'capability-slim/runtime/office_workflows.py')
office=importlib.util.module_from_spec(office_spec);office_spec.loader.exec_module(office)
remote_files,platform_calls=fake_platform(env,monkeypatch)
samples=os.environ.get('HN_PC_SAMPLE_DIR');native_python=os.environ.get('HN_OFFICE_PYTHON')
names=['材料.docx','表格.xlsx','参考.pdf']if samples else['one.txt','two.csv','three.md']
if samples:
paths=[Path(samples)/n for n in ['报告模板.docx','材料一.xlsx','文本材料.pdf']]
raw_materials=[p.read_bytes()for p in paths]
else:raw_materials=[b'synthetic material 1',b'column,value\nx,1',b'# source']
mapping={f'file-input-{i}':content for i,content in enumerate(raw_materials)}
async def download(message,key,limit):return mapping[key]
monkeypatch.setattr(env['platform'],'download_message_file',download)
api=env['app'].state.files
for i,name in enumerate(names):await api.receive('ou_a','chat-files',f'om-input-{i}',{'file_name':name,'file_key':f'file-input-{i}'})
task=create_task(env);api.claim(task)
context={k:task[k]for k in ['environment','app_id','owner','host_id','binding_generation','chat_id','source_message_id','conversation_id']}
context.update(task_id='task-files',channel='work-dm')
device=tmp_path/'pc-device';device.mkdir();(device/'device.json').write_text(json.dumps({'host_id':'a'*32,'owner':'ou_a','binding_generation':task['binding_generation']}))
(device/'device.key').write_bytes(pem_private(env['keys']['a'*32]))
loop=asyncio.get_running_loop();requests=[];lost=[False]
def forward(request):
requests.append(request.url.path)
future=asyncio.run_coroutine_threadsafe(env['client'].request(request.method,request.url.path,
content=request.read(),headers=dict(request.headers)),loop)
response=future.result(20)
if request.url.path.endswith('/upload')and not lost[0]:
lost[0]=True;raise httpx.ReadError('synthetic lost upload receipt')
return httpx.Response(response.status_code,headers=dict(response.headers),content=response.content)
transport=httpx.Client(base_url='https://relay.example.test',transport=httpx.MockTransport(forward))
provider=object.__new__(credentials.CredentialProvider)
provider.directory=device;provider.host='a'*32;provider.owner='ou_a';provider.wall=time.time;provider.http=transport
provider.release={'environment':'test','app_id':'cli_test'};provider.cache_key=('synthetic',)
client=SimpleNamespace(provider=provider,http=transport)
home=tmp_path/'pc-home'
try:
staged=await asyncio.to_thread(files_module.stage_materials,client,context,home)
assert len(staged['materials'])==3 and all(m['status']=='ready'for m in staged['materials'])
assert sorted(Path(m['path']).read_bytes()for m in staged['materials'])==sorted(raw_materials)
root=files_module.task_workspace(context,home)
if native_python and samples:
doc=next(m['path']for m in staged['materials']if m['name'].endswith('.docx'))
output=root/'outputs/converted.pdf'
request={'action':'export_pdf','arguments':{'source':doc,'output':str(output)}}
completed=await asyncio.to_thread(subprocess.run,[native_python,'-X','utf8',str(source/'capability-slim/runtime/office_workflows.py')],
input=json.dumps(request).encode(),capture_output=True,timeout=120)
assert completed.returncode==0,completed.stderr
assert json.loads(completed.stdout)['status']=='completed',completed.stdout
assert output.read_bytes().startswith(b'%PDF')
else:
output=root/'outputs/result.txt';output.write_text('synthetic final output')
office.register_artifacts(root,[output])
first=await asyncio.to_thread(files_module.deliver_artifacts,client,context,home)
assert first['unknown']==1
second=await asyncio.to_thread(files_module.deliver_artifacts,client,context,home)
assert second['received']==1 and second['unknown']==0
assert requests.count('/work/v2/files/upload')==1 # Read state instead of resending bytes.
await api.flush();await api.flush();await api.flush()
sent=[x for x in platform_calls if x[0]=='reply_file']
assert len(sent)==1 and sent[0][1]=='om-original'
assert remote_files[sent[0][2]]==output.read_bytes()
assert len([x for x in platform_calls if x[0]=='upload'])==1
finally:transport.close()
@@ -0,0 +1,49 @@
import asyncio
import hashlib
import json
import pytest
from test_credential_api import env
from test_device_lifecycle import routed,device,instruction,reply,send_event,event,wait_for_reply
from test_work_files import fake_platform
pytestmark=pytest.mark.parametrize('env',[True],indirect=True)
def file_event(env,message_id='om-file',chat='chat-a',owner='ou_a'):
value=event(env,message_id,'',chat=chat,owner=owner)
value['event']['message'].update(message_type='file',content=json.dumps({'file_name':'source.txt','file_key':'file-source'}))
return value
async def test_file_then_instruction_uses_encrypted_routing_and_capability_gate(routed,monkeypatch):
env=routed;_,calls=fake_platform(env,monkeypatch)
connection=await device(env,work_files=True)
await send_event(env,file_event(env));await wait_for_reply(env['platform'],1)
await send_event(env,event(env,'om-instruction','请处理刚才的材料'))
payload=await instruction(env,connection)
assert payload['file_transfer']=='work-files-v1'
task_id='msg_'+hashlib.sha256(b'om-instruction').hexdigest()
assert payload['task_context']['task_id']==task_id
rows=env['store'].db.execute('SELECT task_id,status FROM work_files_v1').fetchall()
assert len(rows)==1 and rows[0]['task_id']==task_id and rows[0]['status']=='assigned'
await reply(env,connection,payload,'synthetic result')
await wait_for_reply(env['platform'],2)
assert len([x for x in calls if x[0]=='download'])==1
async def test_old_client_does_not_receive_a_task_that_silently_omits_materials(routed,monkeypatch):
env=routed;fake_platform(env,monkeypatch);connection=await device(env)
await send_event(env,file_event(env));await wait_for_reply(env['platform'],1)
await send_event(env,event(env,'om-instruction','请处理材料'));await wait_for_reply(env['platform'],2)
assert '升级' in env['platform'].replies[-1][1]
assert env['store'].db.execute('SELECT status FROM work_files_v1').fetchone()['status']=='staged'
assert env['store'].db.execute('SELECT COUNT(*) AS n FROM work_tasks_v2').fetchone()['n']==0
async def test_invalid_upload_metadata_is_rejected_before_binary_body_is_read(env):
consumed=[]
async def content():
consumed.append(True);yield b'not read'
response=await env['client'].post('/work/v2/files/upload',content=content())
assert response.status_code==422 and not consumed