"""Scratch checkouts only: replace executor outcome, never worker timeout/retry policy."""
import sys, subprocess, pathlib
root = pathlib.Path(sys.argv[1]).resolve()
row = sys.argv[2]
assert str(root) in ('/home/ubuntu/wt/pr-25675-before', '/home/ubuntu/wt/pr-25675-after')
p = root / 'apps/server/src/orchestration-v2/EffectWorker.ts'
original = subprocess.check_output(['git', 'show', 'HEAD:apps/server/src/orchestration-v2/EffectWorker.ts'], cwd=root, text=True)
text = original
marker = 'case "delegated-tasks.stop":\n            return threads'
assert original.count(marker) == 1
if row in ('R1', 'R2'):
    inject = '''case "delegated-tasks.stop":
            return Effect.logInfo("EVIDENCE stop executor entered", { effectId: effect.id, attempt: effect.attemptCount }).pipe(
              Effect.andThen(Effect.never),
              Effect.onInterrupt(() => Effect.logInfo("EVIDENCE stop executor interrupted", { effectId: effect.id, attempt: effect.attemptCount })),
            );
            return threads'''
    text = original.replace(marker, inject)
elif row == 'R4':
    inject = '''case "delegated-tasks.stop":
            if (effect.attemptCount === 1) return Effect.logInfo("EVIDENCE R4 attempt 1 hangs", { effectId: effect.id }).pipe(Effect.andThen(Effect.never));
            return Effect.logInfo("EVIDENCE R4 fresh attempt waits 25s then calls genuine service", { effectId: effect.id, attempt: effect.attemptCount }).pipe(
              Effect.andThen(Effect.sleep("25 seconds")),
              Effect.andThen(threads.stopDelegatedTasks({ threadId: effect.threadId, commandId: effect.commandId, reason: effect.request.reason })),
              Effect.mapError((cause) => new OrchestrationEffectExecutionError({ effectId: effect.id, effectType: effect.request.type, cause })),
            );
            return threads'''
    text = original.replace(marker, inject)
elif row == 'R5':
    inject = '''case "delegated-tasks.stop":
            if (effect.attemptCount === 1) return Effect.sleep("5 seconds").pipe(Effect.andThen(Effect.fail(new OrchestrationEffectExecutionError({
              effectId: effect.id, effectType: effect.request.type, cause: "EVIDENCE_R5_ORIGINAL_EXECUTOR_FAILURE",
            }))));
            return threads'''
    text = original.replace(marker, inject)
elif row not in ('R3', 'NONE'):
    raise ValueError(row)
p.write_text(text)
print(f'{row}: executor injection installed in {root}; all worker policy unchanged')
