From d1fd0c21abc86d3088ef325d237c9ecbac5cdbff Mon Sep 17 00:00:00 2001 From: RetroGameDeveloper <40120498+RetroGameDeveloper@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:15:45 +0000 Subject: [PATCH 1/2] fix: prevent release lock cycles and agent tool crashes --- docs/APP_PERFORMANCE.md | 8 ++++ src/decompnet/app/deep_permuter.py | 2 +- src/decompnet/app/store_result_repairs.py | 2 + src/decompnet/app/tools.py | 29 +++++++++---- src/decompnet/candidate_quality.py | 4 +- tests/test_compact_candidate_quality.py | 12 ++++++ tests/test_release_lock_order.py | 52 +++++++++++++++++++++++ tests/test_store_result_repairs.py | 5 +++ 8 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 tests/test_release_lock_order.py diff --git a/docs/APP_PERFORMANCE.md b/docs/APP_PERFORMANCE.md index 068bc0b1c..70ba72f4e 100644 --- a/docs/APP_PERFORMANCE.md +++ b/docs/APP_PERFORMANCE.md @@ -396,3 +396,11 @@ reuse/archive follow-ups run separately in bounded batches and index the changed functions rather than rescanning the project. Advisory draft reports share bounded trial bookkeeping; completed resume receipts flush immediately. Stopping a lane flushes advisory reports and queues follow-ups for its already committed results. + +### Releases queued behind a compile + +A plain agent release takes the per-agent/function candidate lock before retaining the project +publication lock. Compiles use that same order. This lets an in-flight compile publish while its +release waits, preventing a cycle where the release owns the project lock and waits for the +compile's candidate lock. Release receipt checks and note-publication markers remain inside their +existing guards. Candidate-lock waits at tool entry are included in `candidate_lock_wait_ms`. diff --git a/src/decompnet/app/deep_permuter.py b/src/decompnet/app/deep_permuter.py index 289fe9222..4f121507e 100644 --- a/src/decompnet/app/deep_permuter.py +++ b/src/decompnet/app/deep_permuter.py @@ -91,7 +91,7 @@ class Search(Permuter): def __init__(self, net, minutes, max_variants, log, fn=None): super().__init__(net) self.room_holds = {} - self.parallel_claims = True + self.parallel_claims = getattr(log, 'parallel_claims', True) self.started_at = time.monotonic() self.seconds = min(minutes*60,getattr(log,'maximum_seconds',minutes*60)) self.deadline = self.started_at+self.seconds diff --git a/src/decompnet/app/store_result_repairs.py b/src/decompnet/app/store_result_repairs.py index ac5ecc7cb..1045594a6 100644 --- a/src/decompnet/app/store_result_repairs.py +++ b/src/decompnet/app/store_result_repairs.py @@ -29,6 +29,8 @@ def signed_word(typ, context): def initialized_before(flow): """Must-initialization on reachable edges, including declaration initializers.""" + if flow.entry is None: + return {} # An empty body has no reachable stores or initialization evidence. reachable, pending = set(), [flow.entry] while pending: index = pending.pop() diff --git a/src/decompnet/app/tools.py b/src/decompnet/app/tools.py index 82a98364d..a348696f8 100644 --- a/src/decompnet/app/tools.py +++ b/src/decompnet/app/tools.py @@ -4,6 +4,7 @@ from __future__ import annotations import re +import threading import time import uuid from contextlib import contextmanager, nullcontext @@ -618,7 +619,7 @@ def _named(net: RomNetwork, agent: Agent, slug: str, args: dict, tool="") -> dic @contextmanager -def _tool_foreground(net, slug, name, args): +def _tool_foreground(net, agent, slug, name, args): # Ordinary judgements, current candidate/target reads and notes can use context # already installed by a saved recheck. Settings/layout/prerequisite changes # still serialize behind the complete import. @@ -626,12 +627,22 @@ def _tool_foreground(net, slug, name, args): name == 'get_candidate' and args.get('base','current') == 'current') or ( name == 'release_function' and not args.get('needs','').strip() and not args.get('not_c',False)) notes = allowed and name in {'add_note','release_function'} - # Keep note writes and their publication marker together: the saved worker - # must not finish and acknowledge dirty state between those two steps. - with (net.progress.foreground() if allowed else nullcontext(), - net.progress._project_lock(slug) if notes else nullcontext()): - yield - if notes:net.progress.foreground_changed(slug) + with net.progress.foreground() if allowed else nullcontext(): + attempt = nullcontext() + if notes and name == 'release_function': + # Compiles own the candidate lock before publishing under the project + # lock. A release must use that same order, including its receipt guard. + fn = net._function(args['function'],slug) + store = net.project_store(fn['project_id']) + with store.lock: + lock = net._attempt_locks.setdefault((agent.id,fn['id']),threading.RLock()) + from ..request_timing import timed_lock + attempt = timed_lock(lock,'candidate_lock_wait') + # Keep note writes and their publication marker together so a finishing + # saved worker cannot acknowledge them before they are published. + with attempt, net.progress._project_lock(slug) if notes else nullcontext(): + yield + if notes:net.progress.foreground_changed(slug) def _invoke(net: RomNetwork, agent: Agent, slug: str, name: str, args: dict) -> str: @@ -642,7 +653,7 @@ def _invoke(net: RomNetwork, agent: Agent, slug: str, name: str, args: dict) -> and (agent.id, slug) in getattr(net, '_pending_tool_handshakes', set())): return _dispatch(net, agent, slug, name, args) # Enter before this wrapper's sync, not only inside the engine's attempt. - with _tool_foreground(net, slug, name, args): + with _tool_foreground(net, agent, slug, name, args): net.progress.sync(slug, force=True) result = _dispatch(net,agent,slug,name,args) if name not in READ_ONLY: @@ -752,7 +763,7 @@ def execute(): else: # Name resolution also reads the project before the engine # judge enters its own foreground scope. - with _tool_foreground(net, slug, name, args): + with _tool_foreground(net, agent, slug, name, args): result=_dispatch(net,agent,slug,name,args,count_usage=True) except Exception as exc: error=exc diff --git a/src/decompnet/candidate_quality.py b/src/decompnet/candidate_quality.py index 6da25d12b..01cf50567 100644 --- a/src/decompnet/candidate_quality.py +++ b/src/decompnet/candidate_quality.py @@ -119,7 +119,9 @@ def view(net,fn): out += ['Reported issue: '+report['issue']+'; source revision '+report['source_hash'][:20]+'.'] for ident in report['alternatives']: row=store.one('SELECT source,score FROM attempts WHERE id=? AND function_id=?',ident,fn['id']) - if row: + if row and row['source'] is None: + out += [f'Alternative attempt {ident}: recorded source unavailable; no review or fresh verification inferred.'] + elif row: out += [f'Compiled alternative attempt {ident}, historical score {row["score"]} (quality unverified):', '```c\n'+row['source'][:6000]+'\n```'] if sum(map(len,out))>18000: diff --git a/tests/test_compact_candidate_quality.py b/tests/test_compact_candidate_quality.py index c983ea1f3..ce8fbf341 100644 --- a/tests/test_compact_candidate_quality.py +++ b/tests/test_compact_candidate_quality.py @@ -88,3 +88,15 @@ def test_candidate_read_does_not_attach_saved_best_report_to_agent_trial(tmp_pat monkeypatch.setattr(agent_evidence, 'baseline', lambda *a: None) text = agent_tokens.get_candidate(net, agent, 'game', 'f') assert 'return 2' in text and 'Candidate quality (agent report' not in text + + +def test_quality_view_reports_missing_alternative_source_without_changing_best(tmp_path): + net,_=_game(tmp_path,[('f',10,'open')]) + fn=net._function('f','game');source='int f(){return 1;}' + report(net,fn,source) + with net.store.tx() as db: + db.execute('INSERT INTO attempts(id,function_id,agent_id,source,score,compiled,created_at) VALUES(999,?,1,NULL,4,1,0)',(fn['id'],)) + text=candidate_quality.view(net,fn) + assert 'recorded source unavailable' in text and 'no review or fresh verification inferred' in text + assert '```c' not in text + assert dict(net._function('f','game'))==dict(fn) diff --git a/tests/test_release_lock_order.py b/tests/test_release_lock_order.py new file mode 100644 index 000000000..4db0f4773 --- /dev/null +++ b/tests/test_release_lock_order.py @@ -0,0 +1,52 @@ +"""A queued release cannot hold the project lock needed by its in-flight compile.""" +import threading +from concurrent.futures import ThreadPoolExecutor + +import pytest + +from decompnet.app import tools +from tests.test_agent_workflow_efficiency import game + + +@pytest.mark.parametrize('transport',['api','mcp']) +def test_release_waits_for_candidate_before_holding_project_lock(tmp_path,monkeypatch,transport): + net,agent,fn=game(tmp_path) + net.claim_row(agent,'game','f') + queued=threading.Event();compiling=threading.Event();compile_thread=[] + class CandidateLock: + def __init__(self):self.lock=threading.RLock() + def acquire(self,*args,**kwargs): + if compiling.is_set() and threading.get_ident()!=compile_thread[0]:queued.set() + return self.lock.acquire(*args,**kwargs) + def release(self):self.lock.release() + def __enter__(self):self.acquire();return self + def __exit__(self,*args):self.release() + candidate=CandidateLock() + net._attempt_locks[(agent.id,fn['id'])]=candidate + monkeypatch.setattr(net,'release_function',lambda *args,**kwargs:'Released; findings saved.') + def compile_result(): + compile_thread.append(threading.get_ident()) + with candidate: + compiling.set() + assert queued.wait(5) + project=net.progress._project_lock('game') + acquired=project.acquire(timeout=.5) + if acquired:project.release() + return acquired + args={'function':'f','notes':'Tried the ordinary judge; retain these findings.','move_on':True} + def call():return tools.call(net,agent,'game','release_function',args) + if transport=='mcp': + import anyio + from mcp.server.mcpserver import Context + monkeypatch.setattr(Context,'headers',property(lambda self:{})) + server=tools.build_mcp(net,lambda ctx:(agent,'game')) + async def request():return await server.call_tool('release_function',args) + def call():return anyio.run(request) + with ThreadPoolExecutor(2) as pool: + compile_future=pool.submit(compile_result) + assert compiling.wait(5) + release_future=pool.submit(call) + acquired=compile_future.result(timeout=6) + assert 'findings saved' in str(release_future.result(timeout=6)) + assert acquired, 'Release held the project lock while waiting for this same compile.' + assert not getattr(net.progress.local,'foreground',False) diff --git a/tests/test_store_result_repairs.py b/tests/test_store_result_repairs.py index f30ffc6a6..ab874fa6b 100644 --- a/tests/test_store_result_repairs.py +++ b/tests/test_store_result_repairs.py @@ -116,3 +116,8 @@ def test_shared_draft_pipeline_reaches_supported_architectures(arch): def test_function_local_alias_cannot_override_header_width(): text=source('typedef short s32; s32 height=127; s32 y=1; p->value=y+height;') assert not variants(text, 'demo', 'typedef int s32;') + + +@pytest.mark.parametrize('body',['','/* no statements */',';']) +def test_empty_control_flow_has_no_store_reuse_proposal(body): + assert variants('void demo(void) {'+body+'}', 'demo')==[] From 6cc181a46bfc86634a619aafead3a9e1f36e7975 Mon Sep 17 00:00:00 2001 From: RetroGameDeveloper <40120498+RetroGameDeveloper@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:21:46 +0000 Subject: [PATCH 2/2] fix: reject function replacement while a release waits --- src/decompnet/app/tools.py | 2 ++ tests/test_release_lock_order.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/decompnet/app/tools.py b/src/decompnet/app/tools.py index a348696f8..6794a3371 100644 --- a/src/decompnet/app/tools.py +++ b/src/decompnet/app/tools.py @@ -641,6 +641,8 @@ def _tool_foreground(net, agent, slug, name, args): # Keep note writes and their publication marker together so a finishing # saved worker cannot acknowledge them before they are published. with attempt, net.progress._project_lock(slug) if notes else nullcontext(): + if notes and name == 'release_function' and net._function(args['function'],slug)['id'] != fn['id']: + raise NetworkError('The function changed while this release waited. Read its current claim before releasing.') yield if notes:net.progress.foreground_changed(slug) diff --git a/tests/test_release_lock_order.py b/tests/test_release_lock_order.py index 4db0f4773..50c5ef790 100644 --- a/tests/test_release_lock_order.py +++ b/tests/test_release_lock_order.py @@ -50,3 +50,17 @@ def call():return anyio.run(request) assert 'findings saved' in str(release_future.result(timeout=6)) assert acquired, 'Release held the project lock while waiting for this same compile.' assert not getattr(net.progress.local,'foreground',False) + + +def test_release_rejects_a_replaced_function_before_entering_its_handler(tmp_path,monkeypatch): + from decompnet.network import NetworkError + net,agent,fn=game(tmp_path) + resolve=net._function;calls=[] + def changed(*args): + calls.append(1) + return resolve(*args) if len(calls)==1 else {**dict(fn),'id':fn['id']+1} + monkeypatch.setattr(net,'_function',changed) + with pytest.raises(NetworkError,match='function changed'): + with tools._tool_foreground(net,agent,'game','release_function',{'function':'f'}): + raise AssertionError('Must not acquire a different candidate lock under the project lock') + assert not getattr(net.progress.local,'foreground',False)