Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 7 min read

# Our agent failed safe, and that's exactly how our worst bugs hid

A Near Miss write-up from Agentware 2026. Team Hangers: Lakshya Varshney and Aryan Verma. On the first night of the hackathon, our kill switch would have reported "agent stopped, zero sessions left" while stopping nothi

A Near Miss write-up from Agentware 2026. Team Hangers: Lakshya Varshney and Aryan Verma.

On the first night of the hackathon, our kill switch would have reported "agent stopped, zero sessions left" while stopping nothing at all.

We had read about this failure before we wrote a line of code. Our research on the kill switch warned that the cleanup step, terminating every database session the agent holds and then confirming none are left, can silently match nothing. We wrote the rule into our design: sweep until the query is empty, and confirm the agent is gone from the empty result. Then we built it exactly that way, and it was still wrong.

The sweep looked for sessions where usename = 'ns_agent' and backend_type = 'client backend'. Run as our control-plane role, it returned no rows. That wasn't because the agent was gone: Postgres hides backend_type for other roles' sessions unless the caller has the pg_read_all_stats role. Our "confirm it's empty" check would have confirmed an empty answer to a question Postgres wouldn't let us ask.

That bug is the shape of everything in this write-up. Almost every serious defect we found failed safe, and failing safe is why our tests stayed green.

What we built

NightShift is an on-call agent for a data pipeline. Hourly order files land, and the agent audits them, diagnoses what broke, repairs what it's allowed to, and holds the rest for a human. We built it in 72 hours for the Developer Workflow track.

The design rule was that safety lives in code, not in the prompt:

  • The agent's database role cannot update or delete production.
  • A policy gate checks every action.
  • Destructive steps need a human approval.
  • A separate control plane, with its own credentials and no model key, holds the kill switch.

If the model is completely fooled, the code is still the thing that says no. We tested that claim with 162 tests that assume the model has been taken over by an attacker.

That design is also why the bugs hid. When something went wrong, the agent escalated to a human and changed nothing. That's correct behaviour, and it's also invisible.

1. Two networks and a header check were not a boundary

The agent and the control plane ran in separate containers on separate Docker networks. The control plane's endpoints only answered requests that said they came from this machine (Host: 127.0.0.1) and carried a custom header (X-NightShift: 1). Our tests passed.

Then an independent reviewer attacked it from inside the agent's container. Docker publishes the control plane's port on the host, and any container can reach the host through host.docker.internal. From there, a process can simply write Host: 127.0.0.1 and X-NightShift: 1 itself. Both checks looked at things the caller chooses.

So a compromised agent process could press Unkill on its own kill switch, or approve its own destructive proposal. That's exactly what the architecture exists to prevent, and it was one curl away.

The fix was a secret the agent never receives: a random token created when the control service starts, required on every call that reads or changes state. We ran the same attack again from inside the agent container, including through the host gateway, and got 401.

Network separation and a header check tell you where a request claims to come from. Neither can tell a person at a keyboard from a program in the container you're trying to contain.

2. Safe failures hid broken prompts

Our scenario table looked great. Every hostile scenario ended either repaired or safely escalated. Then we read the recorded model answers one by one.

  • The diagnoser prompt showed from_col and to_col as placeholders in its example answer. The live model copied them as null into every hypothesis. Our validation rejected null, so most diagnoses were thrown away as "rejected twice", and the incidents escalated. The table read as safe escalations. It was a broken prompt.
  • The planner was never given the incident id, so it wrote the batch id into its escalation. It also kept adding publish_batch, which only the verifier may call. Both were stopped by validation and by the gate, which is the system working, and which is also what hid that the prompt was wrong.

When the safe path is also the failure path, a pass rate tells you nothing about whether the agent is doing the job. You have to read why it escalated, not just that it did.

3. Green in mock mode, broken against the real model

Judges run our project in mock mode, replaying recorded model answers with no API key. Everything was green there: in CI, locally, on every recording.

On the last day we ran the demo incident against the real model. It escalated every time with "the model could not be reached". Two things had drifted since we recorded:

  • The provider now answered a plain-text reply on a tool turn with a new error code, output_parse_failed, that our client did not retry.
  • After our retry message, the model called a tool named json that didn't exist.

A day earlier, the first live check had found a sibling bug. The provider reported json_validate_failed as an error event inside a successful HTTP 200 stream. The SDK raised it as a plain error with no status code, so our "retry once on HTTP 400" logic never saw it. Our test double only produced the 400 form, so every test passed.

Both are fixed. Our README now says plainly that a green mock run proves the code and the gates, not that today's provider still answers the way it did when we recorded.

4. A guardrail that checked the step, not the batch

Our blast-radius rule says that a repair moving more than 20% of a batch into quarantine needs a human. It was evaluated per step, so a plan with two steps that each quarantined 15% stayed automatic and moved 30%. No scenario of ours planned two such steps; only a confused or hostile plan would. The gate now counts the rows already in quarantine for that batch.

Smaller ones, each caught before it shipped

  • Postgres lets everyone run functions by default. A schema-level revoke didn't remove it, so our control plane could call the publish function, despite a comment saying it couldn't. A test written for something else exposed it.
  • Verify, edit, publish. A passing verification proves nothing if the agent can change staging afterwards. Publish now recomputes a fingerprint of the verified rows and refuses if anything changed.
  • We shipped the answer key. The chaos package that holds each scenario's expected result was inside the agent's container image. It's now excluded, and a test checks the agent can't import it.
  • A bug with a date on it. One scenario mixed the real clock with the sandbox calendar. It passed all morning, then would have failed for every judge after about 11:25 UTC on the last day. Our first fix made judge flaky in CI, because the live agent on the same database saw the new timestamps as an overdue file and escalated first.
  • Our own research. Several figures in our pre-hackathon research notes had no primary source. We checked each one and dropped what we couldn't confirm before it reached the README.

What we did not fix, and said so

  • A compromised agent can still write the staging rows that verification is computed from, so it could publish rows it invented. We couldn't fix that without moving the auditor out of the agent's process, so SAFETY.md says so instead of claiming the agent "cannot assert a pass". The kill switch stops such a process, but only if someone notices.
  • We did not do adaptive red-teaming. The official Trap Pack is a fixed set of 20 hostile inputs, and our claim is narrower than "secure": 0 failures in 100 live trials, which still allows a true failure rate of up to 3.0% at 95% confidence.

What we'd tell the next team

  1. Write the test as the attacker who owns the process, and run it from where the attacker is. Our first network test checked "the request says it is local". The property we wanted was "the agent cannot reach its own kill switch".
  2. Count your safe failures as failures until you've read why they happened. An escalation is a safe outcome, not proof the agent worked.
  3. Your mocks are a snapshot of the provider. Run the real thing before you call it done, and expect it to have changed.

The kill switch, once fixed, stopped a live model call in 0.52 seconds, closed the stream, and left zero agent sessions with no reconnection. The test that proves it now checks that the sweep really saw the session before it reports the session gone.

Repo: https://github.com/Lakshya-Varshney/nightshift (Apache-2.0) Β· Demo: https://youtu.be/dYwbg7P6scM Β· Raw log of every near miss: NEAR_MISS_LOG.md

Built for Agentware 2026 by Hackathon Raptors. The Near Miss prize that prompted this write-up is backed by FileClerk, UK company filing for people and AI agents.

πŸ“° 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.