I Built Session Discovery to Find Lookalikes, Not Our Own Sessions
I made a mistake in our session discovery code, and it was there long before anyone noticed. This post covers what I got wrong, why it looked right, and what I’m changing.
What happened
Our app listed a running Claude Code session on a machine where nobody had a session open. Nothing was open in our app, in an editor, or in a remote shell. The only matching process was Claude Code’s background-session supervisor, a daemon that manages other sessions and runs no conversation itself.
My discovery code still listed that daemon as a session.
What I wrote
The discovery function built the session list from two sources:
- The agent’s own table of sessions it started. This was the correct source.
- Every process on the machine whose executable name matched a supported harness. If no session in the table owned one of these processes, I reported it as a separate session and gave it an ID made from the process’s details.
The second source only checked the executable name. It never looked at the arguments, at who started the process, or at whether the process was a conversation at all. So it picked up the supervisor daemon. It would also pick up an instance started from an editor, one started by hand in a shell, and anything else that happened to have the same name.
Why it was wrong
It did more than it should, and that made it look correct. Every session our app started did show up in the list, so the feature seemed to work. It also showed everything that looked like a session.
I answered the wrong question. Our app starts its sessions itself, so it already knows exactly which ones belong to it. Scanning the process table answers “what on this machine looks like a harness?” That’s not the same question as “which sessions are mine?” I wrote code for the first question and treated the answer as the second.
The design had already ruled this out. Our design says sessions are created from the interface. It also says that a harness the user starts from a workspace shell is just a program running in that shell, not a session. My code had drifted away from that, and I didn’t check it against the spec.
The bad data spread. A session found this way didn’t stay a harmless extra row. It got assigned to a workspace, linked to a subscription, given a controller record, and counted in listings and usage figures. All of that was for something our app never started.
Why the tests didn’t catch it
My tests checked that the sessions our app started were found. They passed, because the oversized list did contain them. No test asked whether anything else was in the list. Nothing failed until an unrelated process with the right name happened to be running.
That’s the real trap with this approach. When a check can only add extra results, a test that only looks for the expected results will always pass.
What I should have done
The agent’s own record of the sessions it started should have been the only source of truth. If a process isn’t in that record, it isn’t our session. It doesn’t matter what its executable is called.
If we ever need to look at the environment (for example, to recover after a crash), it should only be used to check or reconnect sessions we already have a record of. It should never create new ones.
What I’m changing
- Discovery will use only the agent’s own table. I’m removing the step that creates sessions from processes it finds on the machine.
- Tests will check for things that should not appear. For each harness, the tests will start a lookalike process (a daemon, a hand-started instance, an unrelated binary with the same name) and assert that none of them show up.
- I’ll compare features with the design before calling them done. In this case the spec already had the answer. A quick look at it during review would have caught the drift.
- I’ll question name-based matching. If code decides what something is by its name or pattern alone, I’ll ask whether we already have a more reliable record.
The lesson
When your system creates the things it tracks, its own record of what it created is the source of truth. Finding them by pattern-matching the environment can only return too much, because there’s no way to tell your own items from lookalikes. Tests that only check whether the right answer is in the results won’t fail when the results also include things that shouldn’t be there.
I’m sorry for the confusion the phantom sessions caused, and for the wrong usage and attribution data they created. The fix is in progress, and the new tests are written so this kind of over-reporting fails a test instead of reaching users.