forked from luopingyi/xc_agent_platform_demo
Compare commits
4 Commits
v0.2.4-liv
...
v0.2.8-liv
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78cfea508d | ||
|
|
2361e7e84f | ||
|
|
82f3af1cc2 | ||
|
|
79555c7db6 |
13
README.md
13
README.md
@@ -2,7 +2,7 @@
|
||||
|
||||
ModelHub XC(信创模盒)**适配智能体** Phase1 本地骨架。
|
||||
|
||||
> **不代注册;Victor 已批准队列闸门 live 代提。** 发布智能体本身 **不得分**。积分来自成功适配,数字一律 **待平台核实**。
|
||||
> **不代注册;Victor 已批准队列闸门 live 代提。** 发布智能体本身 **不得分**。积分来自验证通过的适配(`verify_pass` / `verifyResult==1`),数字一律 **待平台核实**。
|
||||
> 冲突裁决:**适配 W1 优先**;本仓默认 live 开启,仅在实时验证队列低于阈值时提交。
|
||||
|
||||
## 契约要点
|
||||
@@ -20,12 +20,11 @@ Tag `v0.2.3-live-token` 内置个人凭证,风险与停交说明见 [token bak
|
||||
Victor 的确认已内置,`LIVE_CONFIRM` 仍接受但不再必需。每次 task/add 前,
|
||||
通过 Xc-Token 全量分页读取 onlyMine task/page;非终态计入队列,读取失败停交。
|
||||
`LIVE_QUEUE_THRESHOLD=95`(严格小于),`LIVE_MAX_SUBMITS_PER_CYCLE=3`,
|
||||
`LIVE_CYCLE_SECONDS=600`(最小 60 秒)。环境队列提示仅可额外阻塞。
|
||||
`LIVE_CYCLE_SECONDS=3600`(最小 60 秒)。环境队列提示仅可额外阻塞。
|
||||
候选来自 `PENDING_SUBMIT_MD` / `PENDING_SUBMIT_TXT`,否则使用
|
||||
`src/data/candidates.json`(随镜像内置);A verified_reuse → 其他 A → B。
|
||||
禁提、重复、GPU 未映射、占位符候选跳过。按 try_next_gpus 顺序选择首个已映射且未提交的模型/GPU 对。`LIVE_STATE_FILE` 默认
|
||||
`/tmp/live-submitted.json`,格式为 `model|gpuEnum` 字符串列表,在请求前持久化;失败请求也不自动重试。旧版仅模型名状态文件须先离线迁移,否则 fail closed。
|
||||
去重合并远端全部状态的 `modelId` + `gpuType`、内置 `src/data/submitted-ledger.json` 和持久化状态。A 允许同模型换 GPU;do-not-resubmit 模型跨 GPU 全禁。服务启动重建一次并只记录三类 pair 数;每次提交前刷新远端,失败则 hold `ledger_rebuild_failed`。task/page total 接受非负整数或 ASCII 数字字符串。
|
||||
禁提、重复、GPU 未映射、占位符候选跳过。按 try_next_gpus 顺序选择首个已映射且未提交的模型/GPU 对。去重合并远端全部状态的 model/GPU、内置 `src/data/submitted-ledger.json` 和进程内提交记录;永久拒绝对内置于 `src/data/reject-marks.json`,不依赖容器文件。A 允许同模型换 GPU;do-not-resubmit 模型跨 GPU 全禁。服务启动记录 remote/baked/marks 数量;每次提交前刷新远端,失败则 hold `ledger_rebuild_failed`。
|
||||
只读平台预检默认开启(`LIVE_PRECHECK=0` 关闭),每周期最多 `LIVE_MAX_PRECHECKS_PER_CYCLE=30` 次;预检失败仍由 task/add 判定。用 `python3 scripts/sync_reject_marks.py <log.txt>` 合并永久拒绝日志到 baked marks,重建镜像后保留;支持 `--dry-run`、`--marks` 和 `--date`。
|
||||
凭证优先级:`EXTERNAL_SERVICE_TOKEN` → `XC_TOKEN_FILE` → `XC_TOKEN` → `MODELHUB_XC_TOKEN`;跳过空值和占位符。每周期只记录 source 环境变量名,使用 `Xc-Token` 请求头,不记录凭证值。
|
||||
离线再生成:`python3 scripts/bake_candidates.py`、`python3 scripts/bake_ledger.py`,读取 `/workspace/modelhub-adapt` 本地文件;候选 342(A 46、B 296),ledger 206 对及 28 个模型禁提项。
|
||||
配置优先 build-config,失败使用 sample 并记录来源及原因。token 缺失只 hold。
|
||||
@@ -84,3 +83,7 @@ tests/ # stdlib unittest
|
||||
|
||||
本仓库用于 Victor 本地/Gitea 交付。Victor 已批准上述队列闸门自动提交;不自动注册。
|
||||
`make dry-run-pending` 仍仅生成草稿;测试全部 mock 平台接口。
|
||||
|
||||
成功指标统一为 `verify_pass`(`verifyResult==1`,验证通过);任务 status 为 `success` 但 `verifyResult==-1` 属于验证失败,积分为 0。
|
||||
|
||||
每轮 live 在凭证解析后记录一次 `[live] stats total=N queue=N verify_pass=N verify_fail=N failed=N unknown=N points=X`;读取失败则记录 `[live] stats unavailable reason=<安全原因>`,提交仍由原队列闸门决定。`GET /status` 保留凭证存在布尔值,新增 `success_metric: "verify_pass"` 和 `stats`:最近成功读取的上述数字及 `updated_at`(epoch 秒),尚未读取时为 null。queue 按未终结状态计数;积分仅累计 verify_pass 的有效数字。
|
||||
|
||||
@@ -50,3 +50,5 @@
|
||||
- API gpuType 仅映射壁砺/壁砺166M/Biren_166m 与曦望/曦望 S2/Sunrise_pt-200-x1;未知卡不请求 API,报告 GAP: gpu enum unmapped。
|
||||
- 选策优化后仍为 experience_order,perf_order=unset;order_source=experience;无正式性能表。Section A 仅按 foothold 文本将 verified_reuse 提前,其余 A 保持文件序,再接 B。API foothold evidence: GAP (not wired)。
|
||||
- **live 前必须有 token + API 调用实证**;sample 单独不是 production-ready。队列 ≥95 继续禁 live,本轮无 live。
|
||||
|
||||
成功量 / 成功样本的指标口径:成功指标统一为 `verify_pass`(`verifyResult==1`,验证通过);任务 status 为 `success` 但 `verifyResult==-1` 属于验证失败,积分为 0。
|
||||
|
||||
@@ -23,14 +23,14 @@
|
||||
| Dockerfile | **仓库根目录**;base `modelhubxc-4pd.tencentcloudcr.com/xc_agent_platform/python:3.11-slim` | A |
|
||||
| 端口 | **8080**,`EXPOSE 8080` | A |
|
||||
| 健康检查 | `GET /health` → 200 `{"status":"ok"}` | A |
|
||||
| 可选状态 | `GET /status` → 布尔 `strategy_id_present` / `external_service_token_present`(**不回传值**) | 本仓 Phase1 |
|
||||
| 可选状态 | `GET /status` → 布尔 `strategy_id_present` / `external_service_token_present`(**不回传值**);另含 `success_metric: "verify_pass"` 与 `stats`(最近统计及 epoch 秒 `updated_at`,未读取时 null) | 本仓 Phase1 |
|
||||
| 优雅退出 | `SIGTERM`,约 **≤30s** 平台窗口;本实现目标 ~1s 退出主循环 | B |
|
||||
| 平台注入 | `STRATEGY_ID`;`EXTERNAL_SERVICE_TOKEN`(及社区别名) | A/B |
|
||||
| **STRATEGY_ID 缺失** | **Phase1:进程启动失败(exit ≠0)** | 产品规划 Phase1 + PRD |
|
||||
| 资源 | 约 100m–1 CPU / 256–512Mi | B |
|
||||
| **发布智能体得分** | **0 分**(发布 ≠ 得分) | B |
|
||||
| Live 默认 | **开**(Victor 已批准);`ALLOW_LIVE_SUBMIT=0` 停交,`LIVE_CONFIRM` 可选 | 本仓 |
|
||||
| 积分 | 来自**成功适配**;智能体代提成功计同一成功分;公式/周报 **待平台核实** | B / D |
|
||||
| 积分 | 来自**验证通过**(`verify_pass`,`verifyResult==1`)的适配;status=`success` 且 verifyResult=-1 为验证失败、0 分;公式/周报 **待平台核实** | B / D |
|
||||
|
||||
## 鉴权双轨(文档索引,Phase1 不代调)
|
||||
|
||||
@@ -61,7 +61,10 @@ Victor 确认已内置,`LIVE_CONFIRM` 仍接受但不必需。
|
||||
缺凭证、HTTP/平台错误、JSON 错误、分页不完整或无效配置均停交。
|
||||
ASYNC_QUEUE_DEPTH / QUEUE_SNAPSHOT_JSON 仅为额外阻塞提示。
|
||||
每周期最多 `LIVE_MAX_SUBMITS_PER_CYCLE`(默认 3)次;周期
|
||||
`LIVE_CYCLE_SECONDS` 默认 600、最小 60 秒,通过 shutdown Event 等待。
|
||||
候选 A verified_reuse 优先,禁提过滤、GPU enum、占位符检查及请求前持久化去重。
|
||||
`LIVE_STATE_FILE` 默认 /tmp/live-submitted.json;保留该文件可跨周期/重启去重。
|
||||
`LIVE_CYCLE_SECONDS` 默认 3600、最小 60 秒,通过 shutdown Event 等待。
|
||||
候选 A verified_reuse 优先,禁提过滤、GPU enum、占位符检查及远端、内置 ledger 和进程内去重。
|
||||
永久拒绝对保存在 `src/data/reject-marks.json`,随镜像重建保留;不写容器状态文件。
|
||||
`LIVE_PRECHECK` 默认 1,只读预检已验证/验证中 GPU;`LIVE_MAX_PRECHECKS_PER_CYCLE` 默认 30。
|
||||
build-config 失败回退 sample 并记录原因;日志不记录响应正文或凭证。
|
||||
|
||||
统计成功口径:成功指标统一为 `verify_pass`(`verifyResult==1`,验证通过);任务 status 为 `success` 但 `verifyResult==-1` 属于验证失败,积分为 0。 每轮日志格式为 `[live] stats total=N queue=N verify_pass=N verify_fail=N failed=N unknown=N points=X`;失败日志为 `[live] stats unavailable reason=<安全原因>`。
|
||||
|
||||
58
scripts/sync_reject_marks.py
Normal file
58
scripts/sync_reject_marks.py
Normal file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Merge permanent rejects from a local live log into baked marks."""
|
||||
import argparse
|
||||
import datetime
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from src.config_params import GPU_ENUMS
|
||||
from src.live_queue import model_key
|
||||
from src.marks import PERMANENT_PAIR_CODES, REJECT_CODES, load_marks
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('log')
|
||||
parser.add_argument('--marks', default='src/data/reject-marks.json')
|
||||
parser.add_argument('--date', default=datetime.date.today().isoformat())
|
||||
parser.add_argument('--dry-run', action='store_true')
|
||||
args = parser.parse_args()
|
||||
path = Path(args.marks)
|
||||
if path.exists():
|
||||
load_marks(path)
|
||||
rows = json.loads(path.read_text())['marks']
|
||||
else:
|
||||
rows = []
|
||||
merged = {}
|
||||
for row in rows:
|
||||
merged.setdefault((model_key(row['model']), row['gpu']), row)
|
||||
before = len(merged)
|
||||
for line in Path(args.log).read_text().splitlines():
|
||||
match = re.search(r'\[live\] (rejected|skip) (.*)', line)
|
||||
if not match:
|
||||
continue
|
||||
fields = dict(token.split('=', 1) for token in match[2].split() if '=' in token)
|
||||
model, gpu = fields.get('model', ''), fields.get('gpu', '')
|
||||
raw = fields.get('code', '') if match[1] == 'rejected' else fields.get('reason', '').removeprefix('marked:precheck_')
|
||||
if match[1] == 'skip' and not fields.get('reason', '').startswith('marked:precheck_'):
|
||||
continue
|
||||
try:
|
||||
code = int(raw)
|
||||
except ValueError:
|
||||
continue
|
||||
if (code not in PERMANENT_PAIR_CODES or gpu not in GPU_ENUMS.values()
|
||||
or not re.fullmatch(r'[\w.-]+/[\w.\-]+', model)):
|
||||
continue
|
||||
merged.setdefault((model_key(model), gpu), dict(model=model, gpu=gpu, code=code,
|
||||
reason=REJECT_CODES[code], date=args.date))
|
||||
rows = sorted(merged.values(), key=lambda r: (r['model'].lower(), r['gpu']))
|
||||
if not args.dry_run:
|
||||
path.write_text(json.dumps(dict(version=1, marks=rows), indent=2) + '\n')
|
||||
print(f'added {len(rows) - before} total {len(rows)}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
61
src/data/reject-marks.json
Normal file
61
src/data/reject-marks.json
Normal file
@@ -0,0 +1,61 @@
|
||||
{
|
||||
"version": 1,
|
||||
"marks": [
|
||||
{
|
||||
"model": "abaryan/CyberXP_Agent_Llama_3.2_1B",
|
||||
"gpu": "Iluvatar_bi-150",
|
||||
"code": 60027,
|
||||
"reason": "CREATE_CONTEST_TASK_MODEL_ALREADY_VERIFIED",
|
||||
"date": "2026-10-17"
|
||||
},
|
||||
{
|
||||
"model": "armandosj85/Qwen2.5-1.5B-Instruct-DPO",
|
||||
"gpu": "Iluvatar_bi-150",
|
||||
"code": 60028,
|
||||
"reason": "CREATE_CONTEST_TASK_MODEL_VERIFY_RUNNING",
|
||||
"date": "2026-10-17"
|
||||
},
|
||||
{
|
||||
"model": "dphn/dolphin-2.9.3-qwen2-1.5b",
|
||||
"gpu": "Biren_166m",
|
||||
"code": 60027,
|
||||
"reason": "CREATE_CONTEST_TASK_MODEL_ALREADY_VERIFIED",
|
||||
"date": "2026-10-17"
|
||||
},
|
||||
{
|
||||
"model": "KingNish/Qwen2.5-0.5b-Test-ft",
|
||||
"gpu": "Biren_166m",
|
||||
"code": 60028,
|
||||
"reason": "CREATE_CONTEST_TASK_MODEL_VERIFY_RUNNING",
|
||||
"date": "2026-10-17"
|
||||
},
|
||||
{
|
||||
"model": "MadeAgents/Hammer2.1-0.5b",
|
||||
"gpu": "Biren_166m",
|
||||
"code": 60027,
|
||||
"reason": "CREATE_CONTEST_TASK_MODEL_ALREADY_VERIFIED",
|
||||
"date": "2026-10-17"
|
||||
},
|
||||
{
|
||||
"model": "rnnandi/bangla-smollm-135m",
|
||||
"gpu": "Iluvatar_bi-150",
|
||||
"code": 60028,
|
||||
"reason": "CREATE_CONTEST_TASK_MODEL_VERIFY_RUNNING",
|
||||
"date": "2026-10-17"
|
||||
},
|
||||
{
|
||||
"model": "SakanaAI/TAID-LLM-1.5B",
|
||||
"gpu": "Biren_166m",
|
||||
"code": 60027,
|
||||
"reason": "CREATE_CONTEST_TASK_MODEL_ALREADY_VERIFIED",
|
||||
"date": "2026-10-17"
|
||||
},
|
||||
{
|
||||
"model": "SultanR/SmolTulu-1.7b-Instruct",
|
||||
"gpu": "Iluvatar_bi-150",
|
||||
"code": 60028,
|
||||
"reason": "CREATE_CONTEST_TASK_MODEL_VERIFY_RUNNING",
|
||||
"date": "2026-10-17"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import json
|
||||
from typing import Any, Dict, Tuple
|
||||
|
||||
from .live_queue import last_stats
|
||||
from .strategy import external_service_token_present, strategy_id_present
|
||||
|
||||
|
||||
@@ -13,8 +14,10 @@ def health_payload() -> Dict[str, str]:
|
||||
|
||||
|
||||
def status_payload() -> Dict[str, Any]:
|
||||
"""Booleans only — never include STRATEGY_ID or token values."""
|
||||
"""Safe presence flags and last-known verification statistics."""
|
||||
return {
|
||||
"stats": last_stats(),
|
||||
"success_metric": "verify_pass",
|
||||
"strategy_id_present": strategy_id_present(),
|
||||
"external_service_token_present": external_service_token_present(),
|
||||
}
|
||||
|
||||
@@ -12,24 +12,11 @@ def load_baked(path=DATA):
|
||||
{model_key(m) for m in data['blocked_models']})
|
||||
|
||||
|
||||
def load_persisted(path):
|
||||
rows = json.loads(path.read_text()) if path.exists() else []
|
||||
if not isinstance(rows, list) or any(not isinstance(r, str) or r.count('|') != 1 for r in rows):
|
||||
raise ValueError('Invalid pair ledger')
|
||||
return {(model_key(m), g) for m, g in (r.split('|') for r in rows) if m and g}
|
||||
|
||||
|
||||
def rebuild(state, reader=None):
|
||||
def rebuild(reader=None):
|
||||
from .marks import load_marks
|
||||
count, reason, remote = (reader or read_queue)()
|
||||
if type(count) is not int or count < 0:
|
||||
raise ValueError('ledger_rebuild_failed')
|
||||
baked, blocked = load_baked()
|
||||
persisted = load_persisted(state)
|
||||
return count, remote | baked | persisted, blocked, (len(remote), len(baked), len(persisted))
|
||||
|
||||
|
||||
def persist(path, pairs):
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
temporary = path.with_name(path.name + '.tmp')
|
||||
temporary.write_text(json.dumps(sorted(m + '|' + g for m, g in pairs)))
|
||||
temporary.replace(path)
|
||||
marks = load_marks()
|
||||
return count, remote | baked, blocked, (len(remote), len(baked), len(marks))
|
||||
|
||||
@@ -7,21 +7,24 @@ from pathlib import Path
|
||||
from .banned import banned_rule
|
||||
from .config_params import GPU_ENUMS, build_config_params_with_meta
|
||||
from .gate import evaluate_gate, queue_threshold
|
||||
from .live_queue import read_queue, model_key
|
||||
from .live_queue import read_queue, read_queue_stats, model_key
|
||||
from .pending_pool import build_ordered_candidates
|
||||
from .task_add_client import TaskAddClient, build_payload, resolve_token_with_source, token_probe_status
|
||||
from .ledger import load_baked, load_persisted, persist, rebuild
|
||||
from .ledger import load_baked
|
||||
from .marks import load_marks, REJECT_CODES, PERMANENT_PAIR_CODES
|
||||
from .precheck import precheck_model, SAFE_STATUSES
|
||||
|
||||
_submitted = set()
|
||||
_marked = {}
|
||||
_cycle_lock = threading.Lock()
|
||||
|
||||
|
||||
def run_cycle(candidates=None, client=None, queue_reader=None, now=None):
|
||||
def run_cycle(candidates=None, client=None, queue_reader=None, now=None, stats_reader=None, precheck=None):
|
||||
with _cycle_lock:
|
||||
return _run_cycle(candidates, client, queue_reader, now)
|
||||
return _run_cycle(candidates, client, queue_reader, now, stats_reader, precheck)
|
||||
|
||||
|
||||
def _run_cycle(candidates, client, queue_reader, now):
|
||||
def _run_cycle(candidates, client, queue_reader, now, stats_reader, precheck):
|
||||
decisions = []
|
||||
|
||||
def emit(action, reason, **fields):
|
||||
@@ -43,6 +46,28 @@ def _run_cycle(candidates, client, queue_reader, now):
|
||||
emit('hold', 'missing/invalid_token')
|
||||
return decisions
|
||||
print('[live] token source=' + source, file=sys.stderr)
|
||||
# An injected queue reader keeps existing offline callers offline.
|
||||
reason, stats = 'not_read', None
|
||||
if stats_reader is not None or queue_reader is None:
|
||||
try:
|
||||
_, reason, _, stats = (stats_reader or read_queue_stats)()
|
||||
except Exception:
|
||||
reason, stats = 'read_failed', None
|
||||
if stats is not None:
|
||||
try:
|
||||
line = ' '.join(f'{key}={stats[key]:g}' if key == 'points'
|
||||
else f'{key}={int(stats[key])}' for key in
|
||||
('total', 'queue', 'verify_pass', 'verify_fail', 'failed', 'unknown', 'points'))
|
||||
except (KeyError, TypeError, ValueError, OverflowError):
|
||||
stats, reason = None, 'invalid_stats'
|
||||
if stats is None:
|
||||
# Only known reasons may enter logs; injected errors may contain secrets.
|
||||
safe_reasons = {'not_read', 'read_failed', 'invalid_stats', 'missing/invalid_token',
|
||||
'http_error', 'malformed_json', 'transport/http_error', 'platform_error',
|
||||
'malformed_data', 'malformed_records', 'pagination_total_changed',
|
||||
'pagination_inconsistent', 'incomplete_pagination'}
|
||||
line = 'unavailable reason=' + (reason if isinstance(reason, str) and reason in safe_reasons else 'read_failed')
|
||||
print('[live] stats ' + line, file=sys.stderr)
|
||||
try:
|
||||
cap = int(os.environ.get('LIVE_MAX_SUBMITS_PER_CYCLE', '3'))
|
||||
if cap < 1:
|
||||
@@ -50,6 +75,12 @@ def _run_cycle(candidates, client, queue_reader, now):
|
||||
except ValueError:
|
||||
emit('hold', 'invalid_cycle_cap')
|
||||
return decisions
|
||||
try:
|
||||
attempt_cap = int(os.environ.get('LIVE_MAX_ATTEMPTS_PER_CYCLE', '15'))
|
||||
if attempt_cap < 1:
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
attempt_cap = 15
|
||||
if candidates is None:
|
||||
try:
|
||||
if os.environ.get('PENDING_SUBMIT_MD') or os.environ.get('PENDING_SUBMIT_TXT'):
|
||||
@@ -63,17 +94,55 @@ def _run_cycle(candidates, client, queue_reader, now):
|
||||
if not candidates:
|
||||
emit('hold', 'no_candidates')
|
||||
return decisions
|
||||
state = Path(os.environ.get('LIVE_STATE_FILE', '/tmp/live-submitted.json'))
|
||||
try:
|
||||
persisted = load_persisted(state) | _submitted
|
||||
marks = load_marks()
|
||||
except ValueError:
|
||||
emit('hold', 'marks_read_failed')
|
||||
return decisions
|
||||
try:
|
||||
baked, blocked = load_baked()
|
||||
known = persisted | baked
|
||||
known = baked | _submitted
|
||||
except (OSError, ValueError, KeyError, TypeError):
|
||||
emit('hold', 'state_read_failed')
|
||||
return decisions
|
||||
try:
|
||||
precheck_cap = int(os.environ.get('LIVE_MAX_PRECHECKS_PER_CYCLE', '30'))
|
||||
if precheck_cap < 1:
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
precheck_cap = 30
|
||||
prechecks = 0
|
||||
emitted_marks = set()
|
||||
|
||||
def choose(choices, key, model, mapping):
|
||||
selected = None
|
||||
for choice in choices:
|
||||
if choice not in GPU_ENUMS:
|
||||
continue
|
||||
enum = GPU_ENUMS[choice]
|
||||
pair = (key, enum)
|
||||
if pair in known:
|
||||
continue
|
||||
code = marks.get(pair, _marked.get(pair))
|
||||
reason = 'marked:' + str(code) if code is not None else None
|
||||
if enum in mapping:
|
||||
code = mapping[enum]
|
||||
_marked[pair] = code
|
||||
if pair not in marks:
|
||||
reason = 'marked:precheck_' + str(code)
|
||||
if reason:
|
||||
if pair not in emitted_marks:
|
||||
emit('skip', reason, model=model, gpu=enum)
|
||||
emitted_marks.add(pair)
|
||||
continue
|
||||
if selected is None:
|
||||
selected = choice
|
||||
return selected
|
||||
|
||||
reader = queue_reader or read_queue
|
||||
client = client if client is not None else TaskAddClient()
|
||||
attempts = 0
|
||||
counted = 0
|
||||
for candidate in candidates:
|
||||
model = candidate.get('model_address') or candidate.get('modelAddress') or ''
|
||||
rule = banned_rule(model + ' ' + str(candidate.get('model_name', candidate.get('modelName', ''))))
|
||||
@@ -90,14 +159,38 @@ def _run_cycle(candidates, client, queue_reader, now):
|
||||
if candidate.get('skip_reason'):
|
||||
emit('skip', 'pending_filter', model=model)
|
||||
continue
|
||||
if attempts >= cap:
|
||||
if counted >= cap:
|
||||
emit('hold', 'cycle_cap')
|
||||
break
|
||||
mapping = {}
|
||||
choices = candidate.get('try_next_gpus') or [candidate.get('targetGpu')]
|
||||
gpu = next((g for g in choices if g in GPU_ENUMS and (key, GPU_ENUMS[g]) not in known), None)
|
||||
gpu = choose(choices, key, model, mapping)
|
||||
if gpu is None:
|
||||
emit('skip', 'all_gpus_done_or_unmapped', model=model)
|
||||
continue
|
||||
if os.environ.get('LIVE_PRECHECK', '1') != '0':
|
||||
if prechecks >= precheck_cap:
|
||||
emit('hold', 'precheck_cap')
|
||||
break
|
||||
prechecks += 1
|
||||
try:
|
||||
status, mapping = (precheck or precheck_model)(model)
|
||||
if status not in SAFE_STATUSES:
|
||||
status = 'malformed_data'
|
||||
if status == 'ok' and not isinstance(mapping, dict):
|
||||
status = 'malformed_data'
|
||||
if status != 'ok':
|
||||
mapping = {}
|
||||
mapping = {g: c for g, c in mapping.items()
|
||||
if g in GPU_ENUMS.values() and type(c) is int and c in (60027, 60028)}
|
||||
except Exception:
|
||||
status, mapping = 'transport_error', {}
|
||||
if status != 'ok':
|
||||
emit('precheck', status, model=model)
|
||||
gpu = choose(choices, key, model, mapping)
|
||||
if gpu is None:
|
||||
emit('skip', 'all_gpus_done_or_unmapped', model=model)
|
||||
continue
|
||||
# Rebuild remote identities before config calls and every submission.
|
||||
try:
|
||||
count, why, remote = reader()
|
||||
@@ -107,7 +200,7 @@ def _run_cycle(candidates, client, queue_reader, now):
|
||||
except Exception:
|
||||
emit('hold', 'ledger_rebuild_failed')
|
||||
break
|
||||
gpu = next((g for g in choices if g in GPU_ENUMS and (key, GPU_ENUMS[g]) not in known), None)
|
||||
gpu = choose(choices, key, model, mapping)
|
||||
if gpu is None:
|
||||
emit('skip', 'all_gpus_done_or_unmapped', model=model)
|
||||
continue
|
||||
@@ -126,13 +219,7 @@ def _run_cycle(candidates, client, queue_reader, now):
|
||||
continue
|
||||
pair = (key, GPU_ENUMS[gpu])
|
||||
known.add(pair)
|
||||
persisted.add(pair)
|
||||
_submitted.add(pair)
|
||||
try:
|
||||
persist(state, persisted)
|
||||
except OSError:
|
||||
emit('hold', 'state_write_failed')
|
||||
break
|
||||
attempts += 1
|
||||
try:
|
||||
result = client.submit(payload)
|
||||
@@ -142,6 +229,24 @@ def _run_cycle(candidates, client, queue_reader, now):
|
||||
reason = 'task_add_response' if status is not None else 'transport_error'
|
||||
except Exception:
|
||||
status, code, reason = None, None, 'submit_error'
|
||||
if code is not None and code != 0:
|
||||
emit('rejected', REJECT_CODES.get(code, 'unknown'), model=model,
|
||||
gpu=GPU_ENUMS[gpu], code=code, http=status, queue_before=count)
|
||||
_submitted.discard(pair)
|
||||
known.discard(pair)
|
||||
if code in PERMANENT_PAIR_CODES or code != 60007:
|
||||
_marked[pair] = code
|
||||
if code == 60007:
|
||||
emit('hold', 'task_num_limit')
|
||||
break
|
||||
if attempts >= attempt_cap:
|
||||
emit('hold', 'attempt_cap')
|
||||
break
|
||||
continue
|
||||
counted += 1
|
||||
emit('submitted', reason, model=model, gpu=GPU_ENUMS[gpu], queue_before=count,
|
||||
http=status, code=code, **meta)
|
||||
if attempts >= attempt_cap:
|
||||
emit('hold', 'attempt_cap')
|
||||
break
|
||||
return decisions
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
"""Fresh authenticated queue snapshots. Never expose response bodies or credentials."""
|
||||
import json
|
||||
import math
|
||||
import time
|
||||
import os
|
||||
import urllib.request
|
||||
from .task_add_client import resolve_token
|
||||
@@ -7,6 +9,52 @@ from .task_add_client import resolve_token
|
||||
TERMINAL = {'success', 'failed', 'fail', 'error', 'cancelled', 'canceled'}
|
||||
|
||||
|
||||
_last_stats = None
|
||||
|
||||
|
||||
def last_stats():
|
||||
"""Return an isolated numeric snapshot, without retaining response fields."""
|
||||
return dict(_last_stats) if _last_stats is not None else None
|
||||
|
||||
|
||||
def compute_stats(records):
|
||||
"""Partition records using queue status first, then verification outcome."""
|
||||
stats = dict(total=0, queue=0, verify_pass=0, verify_fail=0,
|
||||
failed=0, unknown=0, points=0.0)
|
||||
for record in records:
|
||||
stats['total'] += 1
|
||||
status = str(record.get('status', '')).strip().lower()
|
||||
result = record.get('verifyResult')
|
||||
if type(result) is str:
|
||||
try:
|
||||
result = float(result.strip())
|
||||
except ValueError:
|
||||
result = None
|
||||
if type(result) not in (int, float):
|
||||
result = None
|
||||
if status not in TERMINAL:
|
||||
bucket = 'queue'
|
||||
elif result == 1:
|
||||
bucket = 'verify_pass'
|
||||
elif result == -1:
|
||||
bucket = 'verify_fail'
|
||||
elif status in TERMINAL - {'success'}:
|
||||
bucket = 'failed'
|
||||
else:
|
||||
bucket = 'unknown'
|
||||
stats[bucket] += 1
|
||||
if bucket == 'verify_pass':
|
||||
credit = record.get('stageAndNoviceTaskCredit')
|
||||
if type(credit) in (int, float, str):
|
||||
try:
|
||||
credit = float(credit)
|
||||
if math.isfinite(credit):
|
||||
stats['points'] += credit
|
||||
except (ValueError, OverflowError):
|
||||
pass
|
||||
return stats
|
||||
|
||||
|
||||
def model_key(value):
|
||||
value = str(value or '').strip().lower().rstrip('/')
|
||||
for prefix in ('https://huggingface.co/', 'http://huggingface.co/', 'https://modelscope.cn/models/'):
|
||||
@@ -19,13 +67,16 @@ def valid_total(value):
|
||||
return (type(value) is int and value >= 0) or (isinstance(value, str) and value.isascii() and value.isdigit())
|
||||
|
||||
|
||||
def read_queue():
|
||||
"""Return (count, reason, model identities); count None means fail closed."""
|
||||
def read_queue_stats():
|
||||
"""Return (count, reason, models, stats); count None means fail closed."""
|
||||
global _last_stats
|
||||
|
||||
try:
|
||||
token = resolve_token()
|
||||
except (ValueError, OSError, UnicodeError):
|
||||
return None, 'missing/invalid_token', set()
|
||||
return None, 'missing/invalid_token', set(), None
|
||||
count, models, seen, total = 0, set(), 0, None
|
||||
stats = compute_stats([])
|
||||
for page in range(1, 21):
|
||||
try:
|
||||
request = urllib.request.Request(
|
||||
@@ -34,27 +85,30 @@ def read_queue():
|
||||
headers={'Xc-Token': token, 'Accept': 'application/json'}, method='GET')
|
||||
with urllib.request.urlopen(request, timeout=30) as response:
|
||||
if not 200 <= response.status < 300:
|
||||
return None, 'http_error', set()
|
||||
return None, 'http_error', set(), None
|
||||
body = json.loads(response.read())
|
||||
except (ValueError, UnicodeError):
|
||||
return None, 'malformed_json', set()
|
||||
return None, 'malformed_json', set(), None
|
||||
except OSError:
|
||||
return None, 'transport/http_error', set()
|
||||
return None, 'transport/http_error', set(), None
|
||||
if not isinstance(body, dict) or type(body.get('code')) not in (int, str) or str(body['code']) != '0':
|
||||
return None, 'platform_error', set()
|
||||
return None, 'platform_error', set(), None
|
||||
data = body.get('data')
|
||||
if not isinstance(data, dict) or not valid_total(data.get('total')):
|
||||
return None, 'malformed_data', set()
|
||||
return None, 'malformed_data', set(), None
|
||||
data['total'] = int(data['total'])
|
||||
records = data.get('records')
|
||||
if not isinstance(records, list) or any(not isinstance(r, dict) for r in records):
|
||||
return None, 'malformed_records', set()
|
||||
return None, 'malformed_records', set(), None
|
||||
if total is not None and total != data['total']:
|
||||
return None, 'pagination_total_changed', set()
|
||||
return None, 'pagination_total_changed', set(), None
|
||||
total = data['total']
|
||||
seen += len(records)
|
||||
if seen > total:
|
||||
return None, 'pagination_inconsistent', set()
|
||||
return None, 'pagination_inconsistent', set(), None
|
||||
page_stats = compute_stats(records)
|
||||
for key in stats:
|
||||
stats[key] += page_stats[key]
|
||||
for record in records:
|
||||
count += str(record.get('status', '')).strip().lower() not in TERMINAL
|
||||
model, gpu = model_key(record.get('modelId')), record.get('gpuType')
|
||||
@@ -62,7 +116,13 @@ def read_queue():
|
||||
models.add((model, gpu))
|
||||
if seen == total:
|
||||
# Do not retain arbitrary response fields, which may echo credentials.
|
||||
return count, 'ok', {m for m in models if all(token.lower() not in part.lower() for part in m)}
|
||||
_last_stats = dict(stats, updated_at=time.time())
|
||||
return count, 'ok', {m for m in models if all(token.lower() not in part.lower() for part in m)}, stats
|
||||
if not records:
|
||||
break
|
||||
return None, 'incomplete_pagination', set()
|
||||
return None, 'incomplete_pagination', set(), None
|
||||
|
||||
|
||||
def read_queue():
|
||||
"""Return (count, reason, models), preserving the queue gating API."""
|
||||
return read_queue_stats()[:3]
|
||||
|
||||
53
src/marks.py
Normal file
53
src/marks.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""Baked, validated permanent model/GPU reject marks."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
from .config_params import GPU_ENUMS
|
||||
from .live_queue import model_key
|
||||
|
||||
DATA = Path(__file__).parent / 'data/reject-marks.json'
|
||||
PERMANENT_PAIR_CODES = {60012, *range(60019, 60030)}
|
||||
|
||||
REJECT_CODES = {
|
||||
60006: 'CANNOT_CREATE_CONTEST_TASK_BECAUSE_NOT_IN_WHITE_LIST',
|
||||
60007: 'MODEL_VERIFY_TASK_NUM_LIMIT',
|
||||
60008: 'CREATE_CONTEST_TASK_REQUEST_EMPTY',
|
||||
60009: 'CREATE_CONTEST_TASK_CONTRIBUTORS_EMPTY',
|
||||
60010: 'CREATE_CONTEST_TASK_SUBMISSION_CONFIG_EMPTY',
|
||||
60011: 'CREATE_CONTEST_TASK_GPU_TYPE_EMPTY',
|
||||
60012: 'CREATE_CONTEST_TASK_MULTI_GPU_UNSUPPORTED',
|
||||
60013: 'CREATE_CONTEST_TASK_MODEL_ID_EMPTY',
|
||||
60014: 'CREATE_CONTEST_TASK_SCRIPT_SUBMIT_DISABLED',
|
||||
60015: 'CREATE_CONTEST_TASK_TASK_TYPE_EMPTY',
|
||||
60016: 'CREATE_CONTEST_TASK_FRAMEWORK_EMPTY',
|
||||
60017: 'CREATE_CONTEST_TASK_DRIVER_IMAGE_NOT_FOUND',
|
||||
60018: 'CREATE_CONTEST_TASK_CONTRIBUTOR_NOT_FOUND',
|
||||
60019: 'CREATE_CONTEST_TASK_SUBMISSION_GPU_TYPE_MISMATCH',
|
||||
60020: 'CREATE_CONTEST_TASK_MODEL_NOT_DOWNLOADED',
|
||||
60021: 'CREATE_CONTEST_TASK_FRAMEWORK_UNRESOLVED',
|
||||
60022: 'CREATE_CONTEST_TASK_GPU_CONFIG_UNSUPPORTED',
|
||||
60023: 'CREATE_CONTEST_TASK_YAML_CONFIG_EMPTY',
|
||||
60024: 'CREATE_CONTEST_TASK_YAML_CONFIG_INVALID',
|
||||
60025: 'CREATE_CONTEST_TASK_GPU_TYPE_UNSUPPORTED',
|
||||
60026: 'CREATE_CONTEST_TASK_MODEL_NOT_FOUND',
|
||||
60027: 'CREATE_CONTEST_TASK_MODEL_ALREADY_VERIFIED',
|
||||
60028: 'CREATE_CONTEST_TASK_MODEL_VERIFY_RUNNING',
|
||||
60029: 'CREATE_CONTEST_TASK_TRANSFORMERS_REQUIRES_FAILED_BASELINE',
|
||||
60030: 'CREATE_CONTEST_TASK_API_VERSION_UNSUPPORTED',
|
||||
}
|
||||
|
||||
|
||||
def load_marks(path=DATA):
|
||||
try:
|
||||
data = json.loads(Path(path).read_text())
|
||||
if not isinstance(data, dict) or type(data.get('version')) is not int or data['version'] != 1 or not isinstance(data.get('marks'), list):
|
||||
raise ValueError
|
||||
marks = {}
|
||||
for row in data['marks']:
|
||||
if (not isinstance(row, dict) or not isinstance(row.get('model'), str)
|
||||
or not row['model'].strip() or row.get('gpu') not in set(GPU_ENUMS.values())
|
||||
or type(row.get('code')) is not int):
|
||||
raise ValueError
|
||||
marks[model_key(row['model']), row['gpu']] = row['code']
|
||||
return marks
|
||||
except (OSError, ValueError, TypeError, KeyError) as error:
|
||||
raise ValueError('Invalid reject marks') from error
|
||||
55
src/precheck.py
Normal file
55
src/precheck.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""Read-only platform pair status; return only fixed statuses and GPU codes."""
|
||||
import json
|
||||
import os
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from .config_params import GPU_ENUMS
|
||||
from .task_add_client import resolve_token
|
||||
|
||||
SAFE_STATUSES = {'ok', 'transport_error', 'http_error', 'malformed_json',
|
||||
'platform_error', 'malformed_data', 'missing/invalid_token'}
|
||||
|
||||
|
||||
def precheck_model(model, token=None, opener=urllib.request.urlopen):
|
||||
try:
|
||||
token = resolve_token() if token is None else token
|
||||
if not isinstance(token, str) or not token.strip() or '<' in token or '>' in token:
|
||||
return 'missing/invalid_token', {}
|
||||
request = urllib.request.Request(
|
||||
os.environ.get('MODELHUB_API_BASE', 'https://modelhub.org.cn').rstrip('/')
|
||||
+ '/api/computility/models/search-by-model-id?modelId='
|
||||
+ urllib.parse.quote(model, safe=''),
|
||||
headers={'Xc-Token': token, 'Accept': 'application/json'}, method='GET')
|
||||
except Exception:
|
||||
return 'missing/invalid_token', {}
|
||||
try:
|
||||
with opener(request, timeout=30) as response:
|
||||
if response.status != 200:
|
||||
return 'http_error', {}
|
||||
raw = response.read()
|
||||
except urllib.error.HTTPError:
|
||||
return 'http_error', {}
|
||||
except Exception:
|
||||
return 'transport_error', {}
|
||||
try:
|
||||
body = json.loads(raw)
|
||||
except (ValueError, TypeError, UnicodeError):
|
||||
return 'malformed_json', {}
|
||||
if not isinstance(body, dict) or 'code' not in body:
|
||||
return 'malformed_data', {}
|
||||
if type(body['code']) not in (str, int) or str(body['code']) != '0':
|
||||
return 'platform_error', {}
|
||||
data = body.get('data')
|
||||
if not isinstance(data, dict) or not isinstance(data.get('verifyResult'), dict):
|
||||
return 'malformed_data', {}
|
||||
result = {}
|
||||
for gpu, row in data['verifyResult'].items():
|
||||
if gpu not in GPU_ENUMS.values():
|
||||
continue
|
||||
if not isinstance(row, dict) or not isinstance(row.get('result'), str):
|
||||
return 'malformed_data', {}
|
||||
code = {'已验证': 60027, '验证中': 60028}.get(row['result'])
|
||||
if code is not None:
|
||||
result[gpu] = code
|
||||
return 'ok', result
|
||||
@@ -59,15 +59,14 @@ def _on_sigterm(signum: int, frame) -> None: # noqa: ARG001
|
||||
|
||||
|
||||
def _live_worker():
|
||||
from pathlib import Path
|
||||
from .ledger import rebuild
|
||||
try:
|
||||
_, _, _, sizes = rebuild(Path(os.environ.get('LIVE_STATE_FILE', '/tmp/live-submitted.json')))
|
||||
sys.stderr.write('[live] ledger rebuilt remote=%d baked=%d persisted=%d\n' % sizes)
|
||||
_, _, _, sizes = rebuild()
|
||||
sys.stderr.write('[live] ledger rebuilt remote=%d baked=%d marks=%d\n' % sizes)
|
||||
except Exception:
|
||||
sys.stderr.write('[live] hold reason=ledger_rebuild_failed\n')
|
||||
try:
|
||||
interval = max(60, int(os.environ.get('LIVE_CYCLE_SECONDS', '600')))
|
||||
interval = max(60, int(os.environ.get('LIVE_CYCLE_SECONDS', '3600')))
|
||||
except ValueError:
|
||||
interval = 600
|
||||
while not _shutdown.is_set():
|
||||
|
||||
@@ -58,3 +58,18 @@ class TestHealth(unittest.TestCase):
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
||||
class TestStatsStatus(unittest.TestCase):
|
||||
def test_status_stats_and_metric(self):
|
||||
from src.live_queue import compute_stats
|
||||
stats = dict(compute_stats([]), updated_at=123.0)
|
||||
for snapshot in (None, stats):
|
||||
with mock.patch('src.health.last_stats', return_value=snapshot):
|
||||
code, _, body = handle_get('/status')
|
||||
self.assertEqual(code, 200)
|
||||
payload = json.loads(body)
|
||||
self.assertEqual(payload['stats'], snapshot)
|
||||
self.assertEqual(payload['success_metric'], 'verify_pass')
|
||||
self.assertIn('strategy_id_present', payload)
|
||||
self.assertIn('external_service_token_present', payload)
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import io
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
from src.live_cycle import run_cycle, _submitted
|
||||
from src.live_cycle import run_cycle, _submitted, _marked
|
||||
|
||||
|
||||
def candidate(name, section='B', foothold='unknown'):
|
||||
@@ -12,12 +11,11 @@ def candidate(name, section='B', foothold='unknown'):
|
||||
|
||||
class TestLiveCycle(unittest.TestCase):
|
||||
def setUp(self):
|
||||
directory = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(directory.cleanup)
|
||||
self.env = patch.dict(os.environ, {'STRATEGY_ID': 'sid', 'LIVE_STATE_FILE': directory.name + '/state.json', 'CONFIG_PARAMS_SOURCE': 'sample', 'XC_TOKEN': 'synthetic-secret'}, clear=True)
|
||||
self.env = patch.dict(os.environ, {'LIVE_PRECHECK': '0', 'STRATEGY_ID': 'sid', 'CONFIG_PARAMS_SOURCE': 'sample', 'XC_TOKEN': 'synthetic-secret'}, clear=True)
|
||||
self.env.start()
|
||||
self.addCleanup(self.env.stop)
|
||||
_submitted.clear()
|
||||
_marked.clear()
|
||||
self.client = Mock()
|
||||
self.client.submit.return_value = dict(http_status=200, platform_code=0)
|
||||
network = patch('urllib.request.urlopen', side_effect=AssertionError('network forbidden'))
|
||||
@@ -31,6 +29,79 @@ class TestLiveCycle(unittest.TestCase):
|
||||
reader = Mock(side_effect=[(n, 'platform_error' if n is None else 'ok', set()) for n in counts])
|
||||
return run_cycle(rows or [candidate('a')], self.client, reader)
|
||||
|
||||
def submitted_pairs(self):
|
||||
return sorted(m + '|' + g for m, g in _submitted)
|
||||
|
||||
def test_done_rejection_marks_pair_and_continues(self):
|
||||
for code in (60027, 60028):
|
||||
with self.subTest(code=code):
|
||||
self.client.submit.side_effect = [
|
||||
dict(http_status=200, platform_code=code),
|
||||
dict(http_status=200, platform_code=0)]
|
||||
with patch.dict(os.environ, {'LIVE_MAX_SUBMITS_PER_CYCLE': '1'}):
|
||||
decisions = self.cycle([candidate(str(code)), candidate('ok' + str(code))], (1, 1))
|
||||
self.assertEqual([d['action'] for d in decisions], ['rejected', 'submitted'])
|
||||
self.assertEqual(_marked[('org/' + str(code), 'Biren_166m')], code)
|
||||
self.assertIn('rejected', self.logs.getvalue())
|
||||
self.assertIn('CREATE_CONTEST_TASK_MODEL_VERIFY_RUNNING', self.logs.getvalue())
|
||||
|
||||
def test_rejection_marked_and_not_retried_in_process(self):
|
||||
self.client.submit.return_value = dict(http_status=200, platform_code=60020)
|
||||
self.cycle()
|
||||
self.assertEqual(self.submitted_pairs(), [])
|
||||
self.cycle(counts=())
|
||||
self.client.submit.assert_called_once()
|
||||
|
||||
def test_unknown_rejection(self):
|
||||
self.client.submit.return_value = dict(http_status=200, platform_code=99999)
|
||||
decisions = self.cycle()
|
||||
self.assertEqual(decisions[0]['action'], 'rejected')
|
||||
self.assertEqual(decisions[0]['reason'], 'unknown')
|
||||
self.assertEqual(self.submitted_pairs(), [])
|
||||
|
||||
def test_attempt_cap_and_invalid_fallback(self):
|
||||
for value, expected in ((None, 15), ('bad', 15), ('0', 15), ('-1', 15), ('2', 2)):
|
||||
with self.subTest(value=value):
|
||||
_marked.clear()
|
||||
self.client.reset_mock()
|
||||
self.client.submit.return_value = dict(http_status=200, platform_code=60020)
|
||||
with patch.dict(os.environ, {} if value is None else {'LIVE_MAX_ATTEMPTS_PER_CYCLE': value}):
|
||||
decisions = self.cycle([candidate(str(i)) for i in range(expected)], (1,) * expected)
|
||||
self.assertEqual(self.client.submit.call_count, expected)
|
||||
self.assertEqual(decisions[-1]['reason'], 'attempt_cap')
|
||||
|
||||
def test_submit_cap_counts_successes_after_rejections(self):
|
||||
self.client.submit.side_effect = [dict(http_status=200, platform_code=c) for c in (60020, 0, 60028, 0, 0)]
|
||||
decisions = self.cycle([candidate(str(i)) for i in range(7)], (1,) * 5)
|
||||
self.assertEqual(self.client.submit.call_count, 5)
|
||||
self.assertEqual(sum(d['action'] == 'submitted' for d in decisions), 3)
|
||||
self.assertEqual(decisions[-1]['reason'], 'cycle_cap')
|
||||
|
||||
def test_task_limit_stops_cycle(self):
|
||||
self.client.submit.return_value = dict(http_status=200, platform_code=60007)
|
||||
decisions = self.cycle([candidate('a'), candidate('b')])
|
||||
self.assertEqual([d['action'] for d in decisions], ['rejected', 'hold'])
|
||||
self.assertEqual(decisions[-1]['reason'], 'task_num_limit')
|
||||
self.assertEqual(self.submitted_pairs(), [])
|
||||
self.client.submit.assert_called_once()
|
||||
|
||||
def test_unknown_outcomes_count_and_reserve_before_submit(self):
|
||||
for result in (dict(http_status=200, platform_code='0'), dict(http_status=None, platform_code=None), OSError()):
|
||||
with self.subTest(result=result):
|
||||
_submitted.clear()
|
||||
self.client.reset_mock()
|
||||
def submit(payload):
|
||||
self.assertIn('org/a|Biren_166m', self.submitted_pairs())
|
||||
if isinstance(result, Exception):
|
||||
raise result
|
||||
return result
|
||||
self.client.submit.side_effect = submit
|
||||
with patch.dict(os.environ, {'LIVE_MAX_SUBMITS_PER_CYCLE': '1'}):
|
||||
decisions = self.cycle([candidate('a'), candidate('b')])
|
||||
self.assertEqual(decisions[0]['action'], 'submitted')
|
||||
self.assertEqual(decisions[-1]['reason'], 'cycle_cap')
|
||||
self.client.submit.assert_called_once()
|
||||
|
||||
def test_high_and_failed_queue(self):
|
||||
for n in (95, None):
|
||||
decisions = self.cycle(counts=(n,))
|
||||
@@ -57,10 +128,9 @@ class TestLiveCycle(unittest.TestCase):
|
||||
self.cycle(counts=(99,))
|
||||
self.client.submit.assert_called_once()
|
||||
|
||||
def test_banned_duplicate_persistence(self):
|
||||
def test_banned_and_duplicate_memory(self):
|
||||
rows = [candidate('GPT-2'), candidate('ok'), candidate('ok')]
|
||||
self.cycle(rows)
|
||||
_submitted.clear()
|
||||
self.cycle([candidate('ok')], ())
|
||||
self.client.submit.assert_called_once()
|
||||
self.assertIn('banned:', self.logs.getvalue())
|
||||
@@ -83,7 +153,7 @@ class TestLiveCycle(unittest.TestCase):
|
||||
self.assertIn('[live] token_probe EXTERNAL_SERVICE_TOKEN=absent XC_TOKEN_FILE=absent XC_TOKEN=absent MODELHUB_XC_TOKEN=absent', self.logs.getvalue())
|
||||
self.assertNotIn('[live] token source=', self.logs.getvalue())
|
||||
|
||||
def test_rejected_token_probe_does_not_leak(self):
|
||||
def test_marked_token_probe_does_not_leak(self):
|
||||
with patch.dict(os.environ, {'XC_TOKEN': '<private-secret-fragment>'}):
|
||||
self.cycle()
|
||||
self.client.submit.assert_not_called()
|
||||
@@ -107,7 +177,7 @@ class TestLiveCycle(unittest.TestCase):
|
||||
response.__exit__ = Mock(return_value=False)
|
||||
response.status = 200
|
||||
response.read.return_value = json.dumps({'code': 40100}).encode()
|
||||
with patch.dict(os.environ, {'XC_TOKEN': 'synthetic-secret'}), patch('urllib.request.urlopen', return_value=response):
|
||||
with patch.dict(os.environ, {'XC_TOKEN': 'synthetic-secret', 'LIVE_PRECHECK': '0'}), patch('urllib.request.urlopen', return_value=response):
|
||||
run_cycle([candidate('a')], self.client)
|
||||
self.client.submit.assert_not_called()
|
||||
|
||||
@@ -117,3 +187,31 @@ class TestLiveCycle(unittest.TestCase):
|
||||
self.client.submit.assert_not_called()
|
||||
self.assertIn('all_gpus_done_or_unmapped', self.logs.getvalue())
|
||||
self.assertIn('placeholder', self.logs.getvalue())
|
||||
|
||||
def test_stats_logged_once_and_submission_continues(self):
|
||||
from src.live_queue import compute_stats
|
||||
stats = compute_stats([dict(status='success', verifyResult=1, stageAndNoviceTaskCredit='5.5')])
|
||||
stats_reader = Mock(return_value=(0, 'ok', set(), stats))
|
||||
run_cycle([candidate('stats')], self.client, lambda: (0, 'ok', set()), stats_reader=stats_reader)
|
||||
stats_reader.assert_called_once_with()
|
||||
lines = [s for s in self.logs.getvalue().splitlines() if s.startswith('[live] stats ')]
|
||||
self.assertEqual(lines, ['[live] stats total=1 queue=0 verify_pass=1 verify_fail=0 failed=0 unknown=0 points=5.5'])
|
||||
self.assertNotIn('synthetic-secret', self.logs.getvalue())
|
||||
self.client.submit.assert_called_once()
|
||||
|
||||
def test_stats_failure_is_safe_and_does_not_gate(self):
|
||||
for index, reader in enumerate((Mock(return_value=(None, 'platform_error', set(), None)),
|
||||
Mock(side_effect=OSError('synthetic-secret')),
|
||||
Mock(return_value=(None, 'synthetic-secret', set(), None)))):
|
||||
self.logs.truncate(0)
|
||||
self.logs.seek(0)
|
||||
run_cycle([candidate('stats-failure-' + str(index))], self.client, lambda: (0, 'ok', set()), stats_reader=reader)
|
||||
lines = [s for s in self.logs.getvalue().splitlines() if s.startswith('[live] stats ')]
|
||||
self.assertEqual(len(lines), 1)
|
||||
self.assertTrue(lines[0].startswith('[live] stats unavailable reason='))
|
||||
self.assertNotIn('synthetic-secret', self.logs.getvalue())
|
||||
self.assertEqual(self.client.submit.call_count, 3)
|
||||
|
||||
def test_injected_queue_skips_stats_network(self):
|
||||
self.cycle()
|
||||
self.assertEqual(self.logs.getvalue().count('[live] stats unavailable reason=not_read'), 1)
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock, patch
|
||||
from src.live_cycle import run_cycle, _submitted
|
||||
from src.live_cycle import run_cycle, _submitted, _marked
|
||||
from src.ledger import load_baked, rebuild
|
||||
from src.banned import banned_rule
|
||||
from src.task_add_client import resolve_token_with_source
|
||||
@@ -12,13 +11,11 @@ from src.task_add_client import resolve_token_with_source
|
||||
|
||||
class TestLedger(unittest.TestCase):
|
||||
def setUp(self):
|
||||
temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(temp.cleanup)
|
||||
self.state = Path(temp.name) / 'state.json'
|
||||
env = patch.dict(os.environ, dict(STRATEGY_ID='sid', XC_TOKEN='synthetic-credential', CONFIG_PARAMS_SOURCE='sample', LIVE_STATE_FILE=str(self.state)), clear=True)
|
||||
env = patch.dict(os.environ, dict(LIVE_PRECHECK='0', STRATEGY_ID='sid', XC_TOKEN='synthetic-credential', CONFIG_PARAMS_SOURCE='sample'), clear=True)
|
||||
env.start()
|
||||
self.addCleanup(env.stop)
|
||||
_submitted.clear()
|
||||
_marked.clear()
|
||||
self.addCleanup(_submitted.clear)
|
||||
net = patch('urllib.request.urlopen', side_effect=AssertionError('network forbidden'))
|
||||
net.start()
|
||||
@@ -29,11 +26,10 @@ class TestLedger(unittest.TestCase):
|
||||
def cycle(self, remote=(), model='unit/model', gpus=None):
|
||||
return run_cycle([dict(model_address=model, try_next_gpus=gpus or ['壁砺', '天垓100'])], self.client, lambda: (0, 'ok', set(remote)))
|
||||
|
||||
def test_pair_and_restart(self):
|
||||
def test_pair_and_memory(self):
|
||||
self.cycle([('unit/model', 'Biren_166m')])
|
||||
self.assertEqual(self.client.submit.call_args.args[0]['targetGpu'], 'Iluvatar_bi-100')
|
||||
self.assertEqual(json.loads(self.state.read_text()), ['unit/model|Iluvatar_bi-100'])
|
||||
_submitted.clear()
|
||||
self.assertIn(('unit/model', 'Iluvatar_bi-100'), _submitted)
|
||||
self.cycle([('unit/model', 'Biren_166m')])
|
||||
self.client.submit.assert_called_once()
|
||||
|
||||
@@ -67,6 +63,6 @@ class TestLedger(unittest.TestCase):
|
||||
self.assertGreater(len(rows), 0)
|
||||
self.assertEqual([r['section'] for r in rows], sorted(r['section'] for r in rows))
|
||||
self.assertFalse(any(banned_rule(r['model_address']) for r in rows))
|
||||
count, pairs, blocked, sizes = rebuild(self.state, lambda: (0, 'ok', {('unit/model', 'Biren_166m')}))
|
||||
count, pairs, blocked, sizes = rebuild(lambda: (0, 'ok', {('unit/model', 'Biren_166m')}))
|
||||
self.assertIn(('unit/model', 'Biren_166m'), pairs)
|
||||
self.assertEqual(sizes[0], 1)
|
||||
|
||||
@@ -53,3 +53,52 @@ class TestLiveQueue(unittest.TestCase):
|
||||
req = call.call_args.args[0]
|
||||
self.assertEqual(req.get_header('Xc-token'), 'synthetic-secret')
|
||||
self.assertEqual(req.method, 'GET')
|
||||
|
||||
|
||||
class TestVerifyStats(unittest.TestCase):
|
||||
response = TestLiveQueue.response
|
||||
|
||||
def test_compute_stats_precedence_and_types(self):
|
||||
from src.live_queue import compute_stats
|
||||
rows = [
|
||||
dict(status='waiting', verifyResult=1, stageAndNoviceTaskCredit=100),
|
||||
dict(status='等待中', verifyResult=-1),
|
||||
dict(status='success', verifyResult=-1, stageAndNoviceTaskCredit=100),
|
||||
dict(status='failed', verifyResult=' -1 '),
|
||||
dict(status='success'), dict(status='success', verifyResult=True),
|
||||
dict(status='success', verifyResult=False),
|
||||
dict(status='success', verifyResult='garbage'),
|
||||
dict(status='error', verifyResult=None),
|
||||
dict(status=' CANCELED ', verifyResult=0),
|
||||
]
|
||||
for result, credit in [(1, '5'), ('1', 5.0), (1.0, None), (1, 'abc'), (1, True), (1, 'NaN')]:
|
||||
rows.append(dict(status='success', verifyResult=result, stageAndNoviceTaskCredit=credit))
|
||||
stats = compute_stats(rows)
|
||||
self.assertEqual(stats, dict(total=16, queue=2, verify_pass=6, verify_fail=2,
|
||||
failed=2, unknown=4, points=10.0))
|
||||
self.assertEqual(sum(stats[k] for k in ('queue', 'verify_pass', 'verify_fail', 'failed', 'unknown')), stats['total'])
|
||||
self.assertIs(type(stats['points']), float)
|
||||
self.assertTrue(all(type(v) is int for k, v in stats.items() if k != 'points'))
|
||||
|
||||
def test_paginated_stats_and_snapshot(self):
|
||||
from src.live_queue import read_queue_stats, last_stats
|
||||
pages = [self.response({'code': 0, 'data': {'total': 2, 'records': [row]}})
|
||||
for row in [dict(status='success', verifyResult=1, stageAndNoviceTaskCredit='5', body='synthetic-secret'),
|
||||
dict(status='success', verifyResult=-1)]]
|
||||
with patch.dict(os.environ, {'XC_TOKEN': 'synthetic-secret'}, clear=True), patch('urllib.request.urlopen', side_effect=pages):
|
||||
count, reason, models, stats = read_queue_stats()
|
||||
self.assertEqual((count, reason, models), (0, 'ok', set()))
|
||||
self.assertEqual(stats['verify_pass'], 1)
|
||||
self.assertEqual(stats['verify_fail'], 1)
|
||||
self.assertEqual(stats['points'], 5.0)
|
||||
snapshot = last_stats()
|
||||
self.assertIn('updated_at', snapshot)
|
||||
self.assertNotIn('synthetic-secret', json.dumps(snapshot))
|
||||
snapshot['total'] = 999
|
||||
stats['total'] = 888
|
||||
self.assertEqual(last_stats()['total'], 2)
|
||||
with patch.dict(os.environ, {}, clear=True):
|
||||
self.assertEqual(read_queue_stats(), (None, 'missing/invalid_token', set(), None))
|
||||
self.assertEqual(last_stats()['total'], 2)
|
||||
with patch('src.live_queue.read_queue_stats', return_value=(3, 'ok', set(), {})):
|
||||
self.assertEqual(read_queue(), (3, 'ok', set()))
|
||||
|
||||
258
tests/test_reject_marks.py
Normal file
258
tests/test_reject_marks.py
Normal file
@@ -0,0 +1,258 @@
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import urllib.error
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from src.config_params import GPU_ENUMS
|
||||
from src.live_cycle import run_cycle, _submitted, _marked
|
||||
from src.marks import load_marks, PERMANENT_PAIR_CODES
|
||||
from src.precheck import precheck_model
|
||||
|
||||
|
||||
class TestMarks(unittest.TestCase):
|
||||
def test_baked_marks(self):
|
||||
marks = load_marks()
|
||||
self.assertEqual(len(marks), 8)
|
||||
self.assertTrue(all(g in GPU_ENUMS.values() for m, g in marks))
|
||||
self.assertEqual(marks['madeagents/hammer2.1-0.5b', 'Biren_166m'], 60027)
|
||||
self.assertEqual(PERMANENT_PAIR_CODES, {60012, *range(60019, 60030)})
|
||||
|
||||
def test_invalid_marks(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory) / 'marks.json'
|
||||
with self.assertRaises(ValueError):
|
||||
load_marks(path)
|
||||
valid = dict(model='org/model', gpu='Biren_166m', code=60027)
|
||||
for data in ('bad json', [], {}, {'version': 1, 'marks': {}},
|
||||
{'version': 1, 'marks': [None]},
|
||||
*({'version': 1, 'marks': [dict(valid, **update)]} for update in
|
||||
({'model': ''}, {'model': 4}, {'gpu': 'unknown'}, {'gpu': []}, {'code': True}, {'code': '60027'}))):
|
||||
path.write_text(data if isinstance(data, str) else json.dumps(data))
|
||||
with self.subTest(data=data), self.assertRaises(ValueError):
|
||||
load_marks(path)
|
||||
|
||||
|
||||
class TestPrecheck(unittest.TestCase):
|
||||
def check(self, body, raw=False):
|
||||
response = Mock(status=200)
|
||||
response.read.return_value = body if raw else json.dumps(body).encode()
|
||||
response.__enter__ = Mock(return_value=response)
|
||||
response.__exit__ = Mock(return_value=False)
|
||||
opener = Mock(return_value=response)
|
||||
result = precheck_model('org/model name', token='synthetic-secret', opener=opener)
|
||||
request = opener.call_args.args[0]
|
||||
self.assertEqual(request.full_url, 'https://modelhub.org.cn/api/computility/models/search-by-model-id?modelId=org%2Fmodel%20name')
|
||||
self.assertEqual(request.get_header('Xc-token'), 'synthetic-secret')
|
||||
self.assertEqual(request.get_header('Accept'), 'application/json')
|
||||
self.assertEqual(request.method, 'GET')
|
||||
self.assertEqual(opener.call_args.kwargs, {'timeout': 30})
|
||||
self.assertNotIn('synthetic-secret', repr(result))
|
||||
self.assertNotIn('authorName', repr(result))
|
||||
return result
|
||||
|
||||
def test_mapping_and_redaction(self):
|
||||
with patch.dict(os.environ, {}, clear=True):
|
||||
self.assertEqual(self.check({'code': '0', 'data': {'verifyResult': {
|
||||
'Biren_166m': {'result': '已验证', 'records': [{'authorName': 'synthetic-secret'}]},
|
||||
'Iluvatar_bi-150': {'result': '验证中'},
|
||||
'Iluvatar_bi-100': {'result': '验证失败'},
|
||||
'unknown': {'result': '已验证'},
|
||||
}}}), ('ok', {'Biren_166m': 60027, 'Iluvatar_bi-150': 60028}))
|
||||
|
||||
def test_bad_responses(self):
|
||||
with patch.dict(os.environ, {}, clear=True):
|
||||
for body, expected, raw in ((b'{bad', 'malformed_json', True),
|
||||
({'code': 3}, 'platform_error', False),
|
||||
({'code': 0, 'data': []}, 'malformed_data', False),
|
||||
([], 'malformed_data', False),
|
||||
({'code': 0, 'data': {'verifyResult': []}}, 'malformed_data', False),
|
||||
({'code': 0, 'data': {'verifyResult': {'Biren_166m': None}}}, 'malformed_data', False)):
|
||||
with self.subTest(body=body):
|
||||
self.assertEqual(self.check(body, raw), (expected, {}))
|
||||
|
||||
def test_transport_http_and_token_errors(self):
|
||||
for error, status in ((OSError('synthetic-secret'), 'transport_error'),
|
||||
(urllib.error.HTTPError('url', 403, 'secret', {}, None), 'http_error')):
|
||||
self.assertEqual(precheck_model('o/m', 'synthetic-secret', Mock(side_effect=error)), (status, {}))
|
||||
with patch.dict(os.environ, {}, clear=True):
|
||||
opener = Mock()
|
||||
self.assertEqual(precheck_model('o/m', opener=opener), ('missing/invalid_token', {}))
|
||||
opener.assert_not_called()
|
||||
response = Mock(status=500)
|
||||
response.__enter__ = Mock(return_value=response)
|
||||
response.__exit__ = Mock(return_value=False)
|
||||
self.assertEqual(precheck_model('o/m', 'synthetic-secret', Mock(return_value=response)), ('http_error', {}))
|
||||
|
||||
|
||||
class TestLiveMarks(unittest.TestCase):
|
||||
def setUp(self):
|
||||
_submitted.clear()
|
||||
_marked.clear()
|
||||
self.env = patch.dict(os.environ, dict(STRATEGY_ID='sid', XC_TOKEN='synthetic-secret', CONFIG_PARAMS_SOURCE='sample'), clear=True)
|
||||
self.env.start()
|
||||
self.addCleanup(self.env.stop)
|
||||
self.addCleanup(_submitted.clear)
|
||||
self.addCleanup(_marked.clear)
|
||||
self.client = Mock()
|
||||
self.client.submit.return_value = dict(http_status=200, platform_code=0)
|
||||
self.precheck = Mock(return_value=('ok', {}))
|
||||
self.reader = Mock(return_value=(0, 'ok', set()))
|
||||
self.logs = io.StringIO()
|
||||
logpatch = patch('sys.stderr', self.logs)
|
||||
logpatch.start()
|
||||
self.addCleanup(logpatch.stop)
|
||||
# All external collaborators must be injected in this suite.
|
||||
netpatch = patch('urllib.request.urlopen', side_effect=AssertionError('network forbidden'))
|
||||
netpatch.start()
|
||||
self.addCleanup(netpatch.stop)
|
||||
|
||||
def cycle(self, model='unit/model', gpus=None, rows=None):
|
||||
return run_cycle(rows or [dict(model_address=model, try_next_gpus=gpus or ['壁砺'])],
|
||||
self.client, self.reader, precheck=self.precheck)
|
||||
|
||||
def test_marks_read_failure(self):
|
||||
with patch('src.live_cycle.load_marks', side_effect=ValueError()):
|
||||
self.assertEqual(self.cycle(), [dict(action='hold', reason='marks_read_failed')])
|
||||
self.client.submit.assert_not_called()
|
||||
self.precheck.assert_not_called()
|
||||
|
||||
def test_baked_mark_uses_next_choice(self):
|
||||
result = self.cycle('MadeAgents/Hammer2.1-0.5b', ['壁砺', '天垓100'])
|
||||
self.assertEqual(result[0], dict(action='skip', reason='marked:60027', model='MadeAgents/Hammer2.1-0.5b', gpu='Biren_166m'))
|
||||
self.assertEqual(self.client.submit.call_args.args[0]['targetGpu'], 'Iluvatar_bi-100')
|
||||
self.assertEqual(self.logs.getvalue().count('reason=marked:60027'), 1)
|
||||
|
||||
def test_baked_mark_only_never_calls_api(self):
|
||||
result = self.cycle('MadeAgents/Hammer2.1-0.5b')
|
||||
self.assertEqual(result[-1]['reason'], 'all_gpus_done_or_unmapped')
|
||||
self.client.submit.assert_not_called()
|
||||
self.precheck.assert_not_called()
|
||||
self.reader.assert_not_called()
|
||||
|
||||
def test_precheck_marks_all_remaining_choices(self):
|
||||
self.precheck.return_value = ('ok', {'Biren_166m': 60027, 'Iluvatar_bi-100': 60028})
|
||||
result = self.cycle(gpus=['壁砺', '天垓100'])
|
||||
self.assertEqual([d['reason'] for d in result], ['marked:precheck_60027', 'marked:precheck_60028', 'all_gpus_done_or_unmapped'])
|
||||
self.assertEqual(_marked, {('unit/model', 'Biren_166m'): 60027, ('unit/model', 'Iluvatar_bi-100'): 60028})
|
||||
self.cycle(gpus=['壁砺', '天垓100'])
|
||||
self.precheck.assert_called_once()
|
||||
self.client.submit.assert_not_called()
|
||||
|
||||
def test_missing_mapping_submits(self):
|
||||
self.cycle()
|
||||
self.client.submit.assert_called_once()
|
||||
|
||||
def test_nonblocking_platform_labels_and_missing_gpu(self):
|
||||
for label in ('验证失败', '未验证', None):
|
||||
_submitted.clear()
|
||||
response = Mock(status=200)
|
||||
mapping = {} if label is None else {'Biren_166m': {'result': label}}
|
||||
response.read.return_value = json.dumps(dict(code=0, data=dict(verifyResult=mapping))).encode()
|
||||
response.__enter__ = Mock(return_value=response)
|
||||
response.__exit__ = Mock(return_value=False)
|
||||
self.precheck.side_effect = lambda model: precheck_model(model, 'synthetic-secret', Mock(return_value=response))
|
||||
self.cycle()
|
||||
self.assertEqual(self.client.submit.call_count, 3)
|
||||
|
||||
def test_precheck_uses_next_choice(self):
|
||||
self.precheck.return_value = 'ok', {'Biren_166m': 60027}
|
||||
result = self.cycle(gpus=['壁砺', '天垓100'])
|
||||
self.assertEqual(result[0]['reason'], 'marked:precheck_60027')
|
||||
self.assertEqual(self.client.submit.call_args.args[0]['targetGpu'], 'Iluvatar_bi-100')
|
||||
self.precheck.assert_called_once()
|
||||
|
||||
def test_precheck_failure_and_unsafe_status(self):
|
||||
for status in ('transport_error', 'synthetic-secret authorName'):
|
||||
with self.subTest(status=status):
|
||||
_submitted.clear()
|
||||
self.precheck.return_value = status, {}
|
||||
result = self.cycle()
|
||||
self.assertEqual(result[0]['action'], 'precheck')
|
||||
self.assertEqual(result[-1]['action'], 'submitted')
|
||||
self.assertNotIn('synthetic-secret', self.logs.getvalue())
|
||||
self.assertNotIn('authorName', self.logs.getvalue())
|
||||
|
||||
def test_disabled(self):
|
||||
with patch.dict(os.environ, {'LIVE_PRECHECK': '0'}):
|
||||
self.cycle()
|
||||
self.precheck.assert_not_called()
|
||||
self.client.submit.assert_called_once()
|
||||
|
||||
def test_precheck_cap(self):
|
||||
self.precheck.return_value = 'ok', {'Biren_166m': 60027}
|
||||
rows = [dict(model_address=f'unit/m{i}', targetGpu='壁砺') for i in range(32)]
|
||||
for value, count in (('2', 2), ('bad', 30), ('0', 30)):
|
||||
_marked.clear()
|
||||
self.precheck.reset_mock()
|
||||
with patch.dict(os.environ, {'LIVE_MAX_PRECHECKS_PER_CYCLE': value, 'LIVE_MAX_ATTEMPTS_PER_CYCLE': '1'}):
|
||||
result = self.cycle(rows=rows)
|
||||
self.assertEqual(self.precheck.call_count, count)
|
||||
self.assertEqual(result[-1]['reason'], 'precheck_cap')
|
||||
self.assertEqual(sum(d['reason'] == 'precheck_cap' for d in result), 1)
|
||||
self.client.submit.assert_not_called()
|
||||
|
||||
def test_refresh_reuses_precheck_mapping(self):
|
||||
self.precheck.return_value = 'ok', {'Iluvatar_bi-100': 60028}
|
||||
self.reader.return_value = 0, 'ok', {('unit/model', 'Biren_166m')}
|
||||
result = self.cycle(gpus=['壁砺', '天垓100'])
|
||||
self.precheck.assert_called_once()
|
||||
self.client.submit.assert_not_called()
|
||||
self.assertEqual(result[-1]['reason'], 'all_gpus_done_or_unmapped')
|
||||
self.assertEqual(sum(d['reason'] == 'marked:precheck_60028' for d in result), 1)
|
||||
|
||||
def test_rejection_memory_and_exact_log(self):
|
||||
self.client.submit.return_value = dict(http_status=200, platform_code=60022)
|
||||
self.cycle()
|
||||
result = self.cycle()
|
||||
self.client.submit.assert_called_once()
|
||||
self.assertEqual(result[0]['reason'], 'marked:60022')
|
||||
self.assertIn('[live] rejected model=unit/model gpu=Biren_166m code=60022 http=200 queue_before=0 reason=CREATE_CONTEST_TASK_GPU_CONFIG_UNSUPPORTED\n', self.logs.getvalue())
|
||||
|
||||
def test_no_state_file_writes(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory) / 'state.json'
|
||||
with patch.dict(os.environ, {'LIVE_STATE_FILE': str(path)}), patch.object(Path, 'write_text', side_effect=AssertionError('file write forbidden')):
|
||||
self.cycle()
|
||||
self.assertFalse(path.exists())
|
||||
|
||||
|
||||
class TestSyncMarks(unittest.TestCase):
|
||||
def test_sync_and_dry_run(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
marks = Path(directory) / 'marks.json'
|
||||
log = Path(directory) / 'log.txt'
|
||||
existing = dict(model='Org/Existing', gpu='Biren_166m', code=60027, reason='first reason', date='2020-01-01')
|
||||
marks.write_text(json.dumps(dict(version=1, marks=[existing])))
|
||||
log.write_text('''timestamp console [live] rejected model=org/existing gpu=Biren_166m code=60028 http=200
|
||||
prefix [live] rejected model=Org/New gpu=Iluvatar_bi-150 code=60022 http=200
|
||||
[live] rejected model=org/new gpu=Iluvatar_bi-150 code=60027
|
||||
[live] skip model=Org/Precheck gpu=Biren_166m reason=marked:precheck_60028
|
||||
[live] rejected model=Org/Limit gpu=Biren_166m code=60007
|
||||
[live] rejected model=Org/Disabled gpu=Biren_166m code=60014
|
||||
[live] rejected model=Org/Success gpu=Biren_166m code=0
|
||||
[live] rejected model=invalid gpu=Biren_166m code=60022
|
||||
[live] rejected model=Org/Invalid gpu=unknown code=60022
|
||||
''')
|
||||
command = [sys.executable, 'scripts/sync_reject_marks.py', str(log), '--marks', str(marks), '--date', '2026-10-17']
|
||||
before = marks.read_bytes()
|
||||
result = subprocess.run(command + ['--dry-run'], capture_output=True, text=True, check=True)
|
||||
self.assertEqual(result.stdout, 'added 2 total 3\n')
|
||||
self.assertEqual(result.stderr, '')
|
||||
self.assertEqual(marks.read_bytes(), before)
|
||||
result = subprocess.run(command, capture_output=True, text=True, check=True)
|
||||
self.assertEqual(result.stdout, 'added 2 total 3\n')
|
||||
rows = json.loads(marks.read_text())['marks']
|
||||
self.assertEqual(rows[0], existing)
|
||||
self.assertEqual(rows[1]['model'], 'Org/New')
|
||||
self.assertEqual(rows[1]['code'], 60022)
|
||||
self.assertEqual(rows[2]['code'], 60028)
|
||||
self.assertTrue(marks.read_bytes().endswith(b'\n'))
|
||||
self.assertEqual(len(load_marks(marks)), 3)
|
||||
result = subprocess.run(command, capture_output=True, text=True, check=True)
|
||||
self.assertEqual(result.stdout, 'added 0 total 3\n')
|
||||
Reference in New Issue
Block a user