Repository navigation
feat(processing): classify Topic Memory processing failures with safe diagnostics (#2126) #2396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
96df98a
7e6f680
6e2c51b
76d1f30
d9a23fb
d90cd5c
ec56a47
aa8818d
6fbc38b
e105e04
9862ef4
568a763
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,12 @@ | |
| TOPIC_MEMORY_WORK_BLOCKS_TABLE, | ||
| TOPIC_MEMORY_WORK_BUDGETS_TABLE, | ||
| ) | ||
| from powercontext.builtin.processing_diagnostics import ( | ||
| ProcessingDiagnostics, | ||
| ProcessingFailureReason, | ||
| ProcessingStage, | ||
| attach_diagnostics, | ||
| ) | ||
|
|
||
| # Server-owned ceilings, shared by ALL attempts and stages at the same Cursor. | ||
| MAX_TOPIC_MEMORY_WORK_ATTEMPTS = 3 | ||
|
|
@@ -91,13 +97,45 @@ async def load_topic_memory_work_budget( | |
| return (await connection.execute(statement)).mappings().one_or_none() | ||
|
|
||
|
|
||
| def _topic_memory_budget_error( | ||
| reason: str, *, tokens: int | None = None, token_ceiling: bool = True | ||
| ) -> TopicMemoryGenerationError: | ||
| error = TopicMemoryGenerationError(reason) | ||
| if reason == "window_attempt_limit": | ||
| # The attempt ceiling ended the window; charged tokens are not the limiting budget. | ||
| attach_diagnostics( | ||
| error, | ||
| ProcessingDiagnostics( | ||
| stage=ProcessingStage.GENERATION, | ||
| reason=ProcessingFailureReason.USAGE_LIMIT, | ||
| failure_location=reason, | ||
| ), | ||
| ) | ||
| elif reason == "window_provider_budget_exceeded": | ||
| attach_diagnostics( | ||
| error, | ||
| ProcessingDiagnostics( | ||
| stage=ProcessingStage.GENERATION, | ||
| reason=ProcessingFailureReason.USAGE_LIMIT, | ||
| estimated_input_tokens=tokens if token_ceiling else None, | ||
| effective_budget=MAX_TOPIC_MEMORY_WORK_TOKENS if token_ceiling else None, | ||
| failure_location=reason, | ||
| ), | ||
| ) | ||
| return error | ||
|
|
||
|
|
||
| async def require_topic_memory_work_available( | ||
| connection: AsyncConnection, scope_id: str, binding_name: str, source_after: int | ||
| ) -> None: | ||
| """Worker-side guard before Source materialization and projection.""" | ||
| row = await load_topic_memory_work_budget(connection, scope_id, binding_name, source_after) | ||
| if row is not None and (reason := exhausted_reason(row)): | ||
| raise TopicMemoryGenerationError(reason) | ||
| raise _topic_memory_budget_error( | ||
| reason, | ||
| tokens=int(row["tokens"]), | ||
| token_ceiling=int(row["requests"]) < MAX_TOPIC_MEMORY_WORK_REQUESTS, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Preserve the actual ceiling when reconstructing a budget failure A rejected reservation does not persist its proposed charges, so the stored request count cannot identify which ceiling was crossed. With 510 requests and 1,000 tokens already charged, reserving 3 requests and 10 tokens correctly fails on the 512-request ceiling, but a subsequent |
||
| ) | ||
|
|
||
|
|
||
| async def record_topic_memory_work_failure( | ||
|
|
@@ -242,7 +280,11 @@ async def begin(self) -> None: | |
| ) | ||
| ) | ||
| if reason: | ||
| raise TopicMemoryGenerationError(reason) | ||
| raise _topic_memory_budget_error( | ||
| reason, | ||
| tokens=None if row is None else int(row["tokens"]), | ||
| token_ceiling=row is None or int(row["requests"]) < MAX_TOPIC_MEMORY_WORK_REQUESTS, | ||
| ) | ||
|
|
||
| async def _current(self, connection: AsyncConnection): | ||
| table = TOPIC_MEMORY_WORK_BUDGETS_TABLE | ||
|
|
@@ -260,13 +302,14 @@ async def reserve(self, *, requests: int, tokens: int) -> None: | |
| await self._lock(connection) | ||
| row = await self._current(connection) | ||
| reason = str(row["failure_code"]) | ||
| requests_after = row["requests"] + requests | ||
| tokens_after = row["tokens"] + tokens | ||
| if reason in RECOVERABLE_TOPIC_MEMORY_REJECTIONS: | ||
| # Diagnosis only: this attempt is still allowed to run and to charge | ||
| # the reservation, or the frontier could never be retried. | ||
| reason = "" | ||
| if not reason and ( | ||
| row["requests"] + requests > MAX_TOPIC_MEMORY_WORK_REQUESTS | ||
| or row["tokens"] + tokens > MAX_TOPIC_MEMORY_WORK_TOKENS | ||
| requests_after > MAX_TOPIC_MEMORY_WORK_REQUESTS or tokens_after > MAX_TOPIC_MEMORY_WORK_TOKENS | ||
| ): | ||
| reason = "window_provider_budget_exceeded" | ||
| values = ( | ||
|
|
@@ -277,7 +320,11 @@ async def reserve(self, *, requests: int, tokens: int) -> None: | |
| await connection.execute(update(table).where(*self._key()).values(**values)) | ||
| # Raise AFTER committing the terminal marker, never roll it back. | ||
| if reason: | ||
| raise TopicMemoryGenerationError(reason) | ||
| raise _topic_memory_budget_error( | ||
| reason, | ||
| tokens=tokens_after, | ||
| token_ceiling=requests_after <= MAX_TOPIC_MEMORY_WORK_REQUESTS, | ||
| ) | ||
|
|
||
| async def fail(self, reason: str) -> None: | ||
| """Record a rejection cause on the frontier without storing source text. | ||
|
|
@@ -312,5 +359,9 @@ async def complete(self, connection: AsyncConnection) -> None: | |
| row = await self._current(connection) | ||
| code = str(row["failure_code"]) | ||
| if code and code not in RECOVERABLE_TOPIC_MEMORY_REJECTIONS: | ||
| raise TopicMemoryGenerationError(code) | ||
| raise _topic_memory_budget_error( | ||
| code, | ||
| tokens=int(row["tokens"]), | ||
| token_ceiling=int(row["requests"]) < MAX_TOPIC_MEMORY_WORK_REQUESTS, | ||
| ) | ||
| await connection.execute(delete(TOPIC_MEMORY_WORK_BUDGETS_TABLE).where(*self._key())) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] 改了
builtin/persistence/但没有同步release/upgrade-support.json,make check在当前 head 直接判红,全套 CI 因此被跳过本文件位于
scripts/upgrade_release_gate.py::MANAGED_INPUTS的src/powercontext/builtin/persistence之下,managed_digest()会把该目录所有.py/.sql/.json的内容哈希进schema_review.persistence_sha256;本 PR 修改了本文件,摘要必然变化,但release/upgrade-support.json未随之更新。证据(head
7e6f6803,Main run 41430808818,CI preflightjob 125228607409):make check的Run checks步骤失败,日志原文:Upgrade release gate failed: Persistence changed (file set or content; CRLF/LF normalized): update the compatibility reviewmake: *** [Makefile:257: upgrade-policy-check] Error 1(job 退出码 2)。CI preflight红,master.yml中依赖needs.tests.result/needs.quality.outputs.suites的 suite job 全部 skipped:本 head 的 check run 里只有license/cla通过,tests (*)与windows-unit-portability一个都没执行。本 PR 新增的 5 个用例(含tests/builtin/runtime/test_processing_scheduler.py::test_worker_blocked_log_carries_diagnostics、tests/e2e/test_worker_logging.py的 worker→supervisor 字段传播断言)目前没有任何 CI 执行证据。影响:这是仓库强制门禁(
make check内含upgrade-policy-check)的红,PR 无法合入;同时本 PR 的核心声明——诊断字段能从子 Worker 传到 supervisor 日志——完全缺少 CI 侧证据。建议:
release/upgrade-support.json的schema_review:persistence_sha256取scripts/upgrade_release_gate.py::managed_digest(Path("."))的值(不要沿用 master 上的404bcc68…,本 head 的 base 已前进)。rationale增补一句说明本次仅新增诊断字段与日志投影,未改表结构、迁移资源、revision 标识与行编码;这样复核人能从 rationale 直接判断为何是 review trigger 而不是布局变更。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
补充一条排除「base 摘要滞后」的对照证据,避免把基线问题算到本 PR 头上(R-PC51 的反向场景):
compare/1c52626d...7e6f6803的 15 个文件里,落在MANAGED_INPUTS(src/powercontext/builtin/persistence、builtin/code、server/identity.py、server/authz、limits.py)内的只有src/powercontext/builtin/persistence/topic_memory_budget.py一个,所以摘要变化确实只由本 PR 引起。1c52626d的 fix(dsh): exclude current runtime context from recall queries #2386(headafc66e09,3 个文件均在 integrations 下、未碰MANAGED_INPUTS)其CI preflight在 2026-11-02T08:27:34Z 为success⇒ base 树上的managed_digest与release/upgrade-support.json的声明值一致,base 不滞后。因此这里需要的是在本 head 上复算并写回
schema_review.persistence_sha256,而不是 rebase。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
补充本 head 的修复核对:摘要已同步,上一轮的 P1 前提不再成立。
在 exact head
ec56a47f的隔离副本(sparse clone,工作区干净)上,按scripts/upgrade_release_gate.py::managed_digest的算法独立复算(MANAGED_INPUTS五个路径下 237 个.py/.sql/.json,逐个sha256后→归一化,再对json.dumps(files, sort_keys=True)求摘要):所以
make upgrade-policy-check在本 head 上不再因本文件变红,本轮不重复该 P1。同 head 的另外两点(仅供对账,不另开线程):
MANAGED_INPUTS的改动确实只有builtin/persistence/topic_memory_budget.py与新增的builtin/processing_diagnostics.py(后者不在MANAGED_INPUTS内),摘要变化全部由本 PR 引起,不是 base 滞后。tests/builtin/test_processing_diagnostics.py -k "classify_by_code or unknown_topic_memory or durable_blocks or storage_invariant or capability_errors":77 passed(Python 3.12.13)。该文件里其余用例在本机 150s 内未跑完,同一文件在 master 工作区同样如此,属环境/基线耗时,不归责本 PR。CI 侧仍有与代码无关的缺口,需注意不要把它读成「已跑过 CI」:本 head 的
CI preflight红在Check License Header(缺 header 的是 master 上的gateway/crates/pc-gateway-core/src/lifecycle.rs),quality失败导致全部 suite job skip,新增用例目前零 CI 证据。