Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 11 min read

The kill switch passed every test I wrote. The real agent took 2,046 ms to stop.

At hour 65 of 72, Windows CI showed me an audit record I had not thought was possible: an OUTCOME status=ok with a higher sequence number than the KILL record. A tool call had succeeded after the operator pressed stop.

At hour 65 of 72, Windows CI showed me an audit record I had not thought was possible: an OUTCOME status=ok with a higher sequence number than the KILL record. A tool call had succeeded after the operator pressed stop.

It was the third kill-path race I found in five hours, and the first one had been hiding behind a test agent.

Marginalia reads a student's course files, pulls out deadlines with a quarantined model call, and stages a plan and a calendar file. It only stages files; it writes to no external system. Around that sits the safety machinery the brief asks for: a kill switch, a hash-chained audit trail, and a gateway that checks every tool call.

Four things I know about it now that I did not know at the freeze:

  • My kill-switch torture test said the agent stopped in 160 to 270 ms. The real agent took 2,046 ms.
  • My first live run on the Trap Pack produced zero plan items. The reader found three deadlines and rejected all three.
  • A perfectly ordinary email with Windows line endings is quarantined as hostile, because my sanitizer treats a carriage return as a control character.
  • The commit I submitted has a red CI cross. The failing job is the kill-point sweep, one of my main pieces of safety evidence.

It is the same mistake in three forms. Each green check measured a stand-in: a test agent instead of the real one, a fake model instead of a live one, my laptop instead of anyone else's. This post is what each stand-in hid, what I measured afterwards, and what I still do not know.

I built with AI coding agents. I accepted those code and runs, so the mistakes below are mine.

Code: https://github.com/Dev-Am12/Marginalia Β· Demo: Youtube

1. The test agent (the guardrail I added at hour 61, and the two races that followed)

The kill switch has two paths. On the cooperative path, a watchdog thread writes a KILL record to the audit chain, cancels in-flight work and writes HALTED. On the hard path, if the agent wedges, a supervisor kills the whole process tree and writes FORCED_EXIT. I wrote a randomised torture harness that fires kills at random moments and checks the audit chain afterwards. It ran against purpose-built torture agents.

Around hour 61 I asked the question I should have asked at hour 12: how long does the real agent take to stop? The drill's own output already said kill_latency_ms: 2237. Nothing asserted on it.

The cause was a circular wait. The real agent runs its whole pipeline in its main task, and the gateway registered that task with the kill switch. On a kill, the watchdog cancelled the tracked tasks and waited up to 2.0 s for them to finish. But the cancelled main task's cleanup waited for the HALTED record, and only the watchdog writes that, after its wait. Each waited for the other until the 2.0 s timeout broke the circle. My torture agents ran their calls in child tasks, so the circle never formed.

kill requested
  watchdog    cancels the tracked tasks, then waits up to 2.0 s for them to finish
  main task   is cancelled, but its cleanup waits for the HALTED record
  HALTED      is written only by the watchdog, after its wait ends

A red test (31fb3c7) measured 2,046 ms. Giving every trigger its own task brought it to 15 ms in process and 134 ms in the full drill. The spec I had given for that step said the drill should pass. It set no latency bound on the real agent. That was the actual bug.

Two more races followed.

Race 2 (hour 64, test 74692ec). My spec for the fix said to terminate worker subprocesses before scheduling task cancellation. Killing the worker first lets the in-flight call return normally, so the call completed before the cancellation reached the event loop. No CANCELLED record, and the agent could carry on past the kill. The brief’s definition of a kill switch is that pending tool calls are cancelled, not finished; this was a pending call finishing. It never showed on my machine, where the full suite passed three times. In CI a different torture trial failed on each run, which is what a timing race looks like. The fix was to schedule cancellation first, then terminate.

Race 3 (hour 65, test 596418c). Windows CI, torture trial 16: an OUTCOME status=ok with a higher sequence number than KILL. Stage-write tools checked for a kill before closing their intent. Read tools closed theirs with a plain append. If KILL won the audit lock in the gap between a tool returning and its outcome being written, an ok landed after the KILL. I reproduced it deterministically on Linux with a read tool that records KILL as it returns, then made the outcome write refuse after a kill.

At hour 63 all four CI jobs were red. Three kill-path races in about five hours. The first was found by reading the code, the second and third by CI. Marginalia only stages files, so none of this could hurt anyone here. Give the same agent a send or pay tool, and a pending call that finishes after the stop is an email sent, or a payment made, after the operator pressed stop.

What I still cannot explain

The 200-trial run I committed: 150 cooperative kills, median 140 ms, 95th percentile 238 ms, and two at exactly 2,139 ms. The README quotes that 2.1 s worst case. I did not know why, and I still do not.

After the freeze I reran the test in a scratch copy, without touching the submission.

Run Cooperative kill (median / max) Forced kill (median) Result
Windows, committed run (200 trials) 140 ms / 2,139 ms (two trials) 45 ms passed
Windows, rerun after the freeze first four trials 172 to 312 ms 46 ms (first forced trial) failed at trial 6: forced exit 131 after 437 ms, test expects cooperative 130
Linux, rerun after the freeze (200 trials) 82 ms / 191 ms 1,515 ms (max 2,002 ms) passed, no cancellation wait timed out
GitHub Actions, Windows (Python 3.11), submitted commit β€” β€” kill sweep: 29 of 31 clean halts; rows 16 and 18 exit 134 with no HALTED record

The README says 31/31 clean halts. That is true of my machine.

I do not know whether this is one race, several, or a busy laptop. What I can say: in the Windows rerun the forced backstop stopped the agent in 437 ms with no leftover processes, and in the CI rows the process still exited and recovery still passed. What I cannot say is what the README says: a clean cooperative halt at every checkpoint, every time, on every platform.

2. The fake model (the rule that was safe, and useless)

Every test that touched the reader used a fake or recorded model. They all returned dates the way I would have written them: 2026-10-16. The parser accepts a date or a timezone-aware datetime and rejects a datetime with no timezone. That was deliberate, because I did not want the agent inventing a timezone. A unit test pinned it: "due at noon" as a naive datetime is deferred as bad_date.

At hour 65 the agent met a live model end to end for the first time, on my own synthetic semester. At hour 67 I pointed it at the four research-study files from the Trap Pack. Four model calls, 3,134 tokens, exit 0, audit chain verified, no canary string in any staged file. And three deferrals, all bad_date, all from one seminar transcript:

  • "due Friday the fourth of December, by noon"
  • "the portal actually closes Monday the seventh of December"
  • "Essay deadline: Wednesday 9 December 2026, 17:00"

After the freeze I sent that transcript to the model three times with the project's own prompt and schema. For the 9 December line it returned 2026-12-09T17:00:00 every time. That has a clock time and no timezone, so my parser rejected it, as designed. The prompt asks for a timezone-aware datetime, and the transcript never states a timezone. The agent could not satisfy its own rule on that input. (My probe returned one candidate where the live run returned three, so I cannot say the other two failed the same way.)

The rule that rejects it is two lines in the parser:

# src/marginalia/readers/deadlines.py
if parsed.tzinfo is None or parsed.utcoffset() is None:
    return None

And this is what the probe printed, three runs in a row:

BAD due_iso='2026-12-09T17:00:00'  due_text='Wednesday 9 December 2026, 17:00'

The unit test that pins the rule uses a fixture with "due_iso": "2026-11-01T12:00:00" for "due at noon" and expects exactly this deferral. The test and the parser agreed with each other. Only a real model's output disagreed.

Two things bother me.

First, the failure was safe. The quotes landed in the needs-review file and nothing was planned from them. But safe is not useful. An agent that defers every deadline with a clock time has a perfect safety record and no product.

Second, the audit record stored the reason ("not a valid ISO date or timezone-aware datetime") but not the value the model sent. I needed a separate probe to learn what it had returned. The trace was my receipt, and it left out the one field that explains the failure.

This is the record, trimmed (the real one also carries the blob id and the hash-chain fields):

{"seq": 25, "kind": "NOTE",
 "data": {"kind": "READER_DEFER", "code": "bad_date", "chunk_index": 0,
          "reason": "due_iso is not a valid ISO date or timezone-aware datetime"}}

That is all it says. The value the model sent is not in it.

The fix would be to apply the timezone from the mandate and flag the assumption. That is a product decision, and I did not make it before the freeze.

3. One laptop (the Trap Pack email quarantined for the wrong reason)

I ran the 20 hostile Trap Pack inputs through my deterministic runner, plus the live reader on four of them, and compared the outcomes with the organisers' answer key afterwards: 24 judgments, 10 match and 14 differ. The strangest rows were the four emails. Three were quarantined as CONTROL_REMOVED with no hostile span isolated, and one of those three was the benign newsletter (file 04).

After the freeze I reran the exact commit that produced those committed results, on Linux. Five of the 20 rows changed: the four emails and the truncated JSON file. The totals moved from 6 clean and 14 quarantined to 8 and 12.

My guess was line endings, so I tested it. In a scratch copy I converted only the four .eml files to Windows (CRLF) line endings and ran the same commit again:

import pathlib
for p in pathlib.Path("fixtures/official_trap_pack").rglob("*.eml"):
    p.write_bytes(p.read_bytes().replace(b"\n", b"\r\n"))

The totals went back to 6 clean and 14 quarantined, and three of the five changed rows matched again. Two still do not (file 01's span count, and the extra findings on file 10), and I do not know why.

The mechanism is plain. The sanitizer counts a carriage return as a control character, so a file with CRLF endings gets a CONTROL_REMOVED finding and is quarantined. The repo's copies of the inputs are LF, because git normalises them, so CI and any reviewer on Linux never saw it. My Windows laptop did. At the commit I submitted, the benign newsletter with CRLF endings is still quarantined, and at an earlier commit, converting all 20 inputs to CRLF quarantined all 20. An agent that reads a student's files will meet Windows line endings constantly. It reads like a quirk of the Trap Pack. It is a bug in my agent: ordinary files with Windows line endings are treated as hostile.

A smaller one. The 200-trial torture output I committed was UTF-16 with a byte-order mark, because PowerShell's Tee-Object wrote it. Git stored it as binary and GitHub would not display it. I re-encoded it before the freeze. The evidence existed, but a reader could not read it.

What I would check instead

  1. Assert latency on the real agent. A kill-path step is not done until a number is printed and asserted against the real thing, not a test agent that happens to have a different task shape.
  2. Run live on day one. Put the real model on real input in the first twelve hours, not at hour 65. A fake model can only return what you imagined, so put the awkward shapes in your fixtures, such as a clock time with no timezone.
  3. Log the value you reject, not only the reason. My trace said a date was invalid and never said which one. I needed a separate probe to find out.
  4. Commit safety evidence from two operating systems, with the files as users have them. If the README says 31/31, it should say on which machine and commit, and CI is the arbiter, not me.

Reproduce it

Run these in a scratch copy, because the Trap Pack runner rewrites its results file:

python -m pytest -q tests/test_kill_sweep.py
TORTURE_TRIALS=200 python -m pytest -q -s tests/test_kill_torture.py -k test_kill_torture_randomised_trials
python -m marginalia trap --official fixtures/official_trap_pack

The due-date probe I used is a 90-line script outside the repo: https://gist.github.com/Dev-Am12/87b9a4ddc48e040c62d54aa61beb5231

The evidence files are in the repo: tests/kill-torture/RUN_OUTPUT.txt, tests/kill-sweep/RESULTS.md, tests/trap-pack/RESULTS.md and VERDICTS.md, and traces/trap-research-reader/.

What I did not fix

Marginalia only stages files. It was tested on synthetic semesters, with a handful of live runs and no long soak. The numbers above come from one Windows laptop, a Linux sandbox and GitHub's runners, and where they disagree I have said so rather than picked one.

Still open at the freeze:

  • Timezones. Naive datetimes still defer. Whether to assume the mandate's timezone and flag it is a product decision I left open.
  • Line endings. CRLF files are still quarantined as CONTROL_REMOVED.
  • Kill sweep. The two Windows (Python 3.11) failures and the 2,139 ms outliers have no root cause yet.
  • Forced-kill latency. 45 ms on my laptop and 1,515 ms on Linux, unexplained.

This is my Near Miss entry for Agentware 2026. Thanks to Hackathon Raptors for running it, and to FileClerk for backing the write-up awards.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.