Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/APP_PERFORMANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
2 changes: 1 addition & 1 deletion src/decompnet/app/deep_permuter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/decompnet/app/store_result_repairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
31 changes: 22 additions & 9 deletions src/decompnet/app/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import re
import threading
import time
import uuid
from contextlib import contextmanager, nullcontext
Expand Down Expand Up @@ -618,20 +619,32 @@ 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.
allowed = name in {'compile_and_diff','compile_patch','compile_variants','add_note','get_assembly'} or (
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():
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)


def _invoke(net: RomNetwork, agent: Agent, slug: str, name: str, args: dict) -> str:
Expand All @@ -642,7 +655,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:
Expand Down Expand Up @@ -752,7 +765,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
Expand Down
4 changes: 3 additions & 1 deletion src/decompnet/candidate_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_compact_candidate_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
66 changes: 66 additions & 0 deletions tests/test_release_lock_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""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)


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)
5 changes: 5 additions & 0 deletions tests/test_store_result_repairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')==[]
Loading