fix(submit): use file path over stdin; fix CF CodeMirror textarea (#305)

## Problem

After the initial submit hardening, two issues remained: source code was
read in Lua and piped as stdin to the scraper (unnecessary roundtrip
since
the file exists on disk), and CF's `page.fill()` timed out on the hidden
`textarea[name="source"]` because CodeMirror owns the editor state.

## Solution

Pass the source file path as a CLI arg instead — AtCoder calls
`page.set_input_files(file_path)` directly, CF reads it with
`Path(file_path).read_text()`. Fix CF source injection via
`page.evaluate()`
into the CodeMirror instance. Extract `BROWSER_SUBMIT_NAV_TIMEOUT` as a
per-platform `defaultdict` (CF defaults to 2× nav timeout). Save the
buffer
with `vim.cmd.update()` before submitting.
This commit is contained in:
Barrett Ruth 2026-03-05 14:34:14 -05:00 committed by GitHub
parent 127089c57f
commit a202725cc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 269 additions and 168 deletions

View file

@ -33,6 +33,7 @@ from .timeouts import (
BROWSER_NAV_TIMEOUT,
BROWSER_SESSION_TIMEOUT,
BROWSER_SETTLE_DELAY,
BROWSER_SUBMIT_NAV_TIMEOUT,
BROWSER_TURNSTILE_POLL,
HTTP_TIMEOUT,
)
@ -297,7 +298,7 @@ def _ensure_browser() -> None:
def _submit_headless(
contest_id: str,
problem_id: str,
source_code: str,
file_path: str,
language_id: str,
credentials: dict[str, str],
_retried: bool = False,
@ -362,19 +363,12 @@ def _submit_headless(
f'select[name="data.LanguageId"] option[value="{language_id}"]'
).wait_for(state="attached", timeout=BROWSER_ELEMENT_WAIT)
page.select_option('select[name="data.LanguageId"]', language_id)
ext = _LANGUAGE_ID_EXTENSION.get(language_id, "txt")
page.set_input_files(
"#input-open-file",
{
"name": f"solution.{ext}",
"mimeType": "text/plain",
"buffer": source_code.encode(),
},
)
page.set_input_files("#input-open-file", file_path)
page.wait_for_timeout(BROWSER_SETTLE_DELAY)
page.locator('button[type="submit"]').click()
page.wait_for_url(
lambda url: "/submissions/me" in url, timeout=BROWSER_NAV_TIMEOUT
lambda url: "/submissions/me" in url,
timeout=BROWSER_SUBMIT_NAV_TIMEOUT["atcoder"],
)
except Exception as e:
submit_error = str(e)
@ -423,7 +417,7 @@ def _submit_headless(
return _submit_headless(
contest_id,
problem_id,
source_code,
file_path,
language_id,
credentials,
_retried=True,
@ -581,7 +575,7 @@ class AtcoderScraper(BaseScraper):
self,
contest_id: str,
problem_id: str,
source_code: str,
file_path: str,
language_id: str,
credentials: dict[str, str],
) -> SubmitResult:
@ -589,7 +583,7 @@ class AtcoderScraper(BaseScraper):
_submit_headless,
contest_id,
problem_id,
source_code,
file_path,
language_id,
credentials,
)
@ -651,15 +645,14 @@ async def main_async() -> int:
return 0 if contest_result.success else 1
if mode == "submit":
if len(sys.argv) != 5:
if len(sys.argv) != 6:
print(
SubmitResult(
success=False,
error="Usage: atcoder.py submit <contest_id> <problem_id> <language>",
error="Usage: atcoder.py submit <contest_id> <problem_id> <language> <file_path>",
).model_dump_json()
)
return 1
source_code = sys.stdin.read()
creds_raw = os.environ.get("CP_CREDENTIALS", "{}")
try:
credentials = json.loads(creds_raw)
@ -667,7 +660,7 @@ async def main_async() -> int:
credentials = {}
language_id = get_language_id("atcoder", sys.argv[4]) or sys.argv[4]
submit_result = await scraper.submit(
sys.argv[2], sys.argv[3], source_code, language_id, credentials
sys.argv[2], sys.argv[3], sys.argv[5], language_id, credentials
)
print(submit_result.model_dump_json())
return 0 if submit_result.success else 1