Skip to content

session: idle sessions gain time_updated after a service restart and jump to the top of the list #63920

Open

Description

@ValDagon

Description

Idle sessions get a new time_updated without any new user-visible activity, so they jump to the top of the session list.

I have four sessions that had been idle for 1–2 hours. Around two background-service restarts (serve --service at 11:41:10 and 11:43:26 local time) each of them received a new time_updated, with no corresponding new row in session_message:

last session_message new time_updated time_idle
09:57:04 11:43:30 09:57:04
10:06:42 11:42:15 10:06:42
10:16:02 11:41:22 10:16:02
11:42:58 11:43:29 11:42:58

time_viewed also stayed at the old value, time_suspended is NULL, resume_attempts is 0. For the three older sessions no session_message.time_updated changed either, so the session row itself was written, not a message.

Because GET /api/session sorts by time_updated DESC, the list reorders itself: old conversations reappear at the top after a restart, and with two clients attached (desktop + phone) the order changes while it is being read.

A control request does not reproduce it:

GET /api/session/<id>
time_updated before: 1794615764251
time_updated after:  1794615764251

This only rules out the single-session GET; it does not rule out the session-list endpoint or client event subscriptions.

Steps to reproduce

  1. Leave a few sessions idle for an hour, no suspended/background work.
  2. Record time_updated, time_idle, time_suspended and the max seq in session_message.
  3. Restart the background service (and/or attach a second client).
  4. Re-read the row: time_updated moved forward while no message was added.

Not yet isolated to a single trigger — cold start vs. client event subscription vs. two clients is still open. The plan is to copy the DB (OPENCODE_DB), stop all clients, do one isolated serve --service with debug logging, and diff session_v2 / session_message / session_inbox; then repeat with one list request, one client, two clients.

Expected Behavior

Recency of an idle session should only change when the session actually receives user-visible activity or an explicit metadata edit. Internal recovery/bookkeeping (inbox, stale execution settling) should not make an old session look recently active.

Actual Behavior

time_updated is rewritten by something other than conversation activity, and the global list reorders.

Additional Context

Relevant code in the 2.3.0 bundle:

  • The session projector has a helper equivalent to updateSession(db, sessionID, {time_updated: event.created}), used for session.inbox.enqueued, session.step.started and session.step.failed without usage data, plus move/agent/model/rename/metadata/archive events.
  • SessionRunner.drain runs settleStaleCompactions / settleStaleToolCalls, but only after its admission checks; their events update message state, not session_v2.time_updated.
  • resumeSuspendedSessions normally selects sessions with time_suspended IS NOT NULL; claim/release/countResume preserve time_updated.
  • Execution start/terminal events preserve time_updated and change time_idle instead.

So an inbox/step/recovery event is the most plausible writer, but the originating event was not persisted in the event table, so the exact path is unverified.

Related UX note: the session list is global across projects and ordered by time_updated, so any internal timestamp write in one project changes the order seen in every project. Scoping the default list to the current project_id (with an explicit "all projects" mode) would contain the damage.

Activity

  1. added
    area/sessions-storage
    type/bug
    version/v2
    on Nov 14, 2026
  2. opencodejr commented on Nov 14, 2026

    @opencodejr
    Contributor

    Thanks for the detailed data. I tried to reproduce this on v2.3.0 and couldn't.

    What I tried:

    1. Ran turns in a few sessions with a simulated model. One was a plain reply. One used a read tool call and a background explore subagent, so it had both a parent and a child session.
    2. Waited until each session had settled (time_idle set, time_suspended NULL) and recorded session_v2.time_updated and the session_message counts.
    3. Restarted the background service, first with a client attached during the restart and then with a second client attached after it.
    4. Re-read the rows.

    In every case time_updated stayed exactly the same, and no rows were added to session_message.

    The only time I saw time_updated move after a restart was when the service was stopped while a turn was still suspended. The turn resumed after the restart and wrote new messages. That might explain your fourth session, whose last message was only about 30 seconds before its new time_updated. It doesn't explain the three that had been idle for hours.

    The isolation run you described would help a lot. If you can, please share:

    • The output of opencode --version and your OS.
    • For one affected session, the event rows and the session_inbox rows around the restart, plus the debug log lines from the serve --service start (--log-level debug --print-logs).
    • Anything special about those sessions: subagents or background jobs, scheduled tasks, queued or deferred inbox items, sessions created by opencode run, imported or moved sessions, plugins or MCP servers.
    • Which clients were attached (TUI, desktop, web or phone via opencode pair) and whether any of them sends messages or changes models or agents when it connects.

    You can reply here with the details, or edit the issue. If you edit it, please also leave a comment saying you've updated it so we know you're done.

    Related: #62655 (asks for a last-message timestamp in the session list, separate from time.updated).

  3. ValDagon commented on Nov 14, 2026

    @ValDagon
    Author

    Isolated: the write lands milliseconds after the client's event stream tears down, not after the restart

    I ran the investigation on a copy of the DB and then matched the production log against the row values. The write is not caused by the service restart, not by GET /api/session, and not by client attach/detach on its own. It lands 0–5 ms after the client's GET /api/event subscription ends, and in 17 of 18 cases 0–4 ms after the aborted POST /api/rpc/experimental.browser/attach that follows that stream close — i.e. in the browse-MCP attach handshake between the desktop client and the server.

    Method

    • sqlite3 "file:$HOME/.local/share/opencode/opencode.db?mode=ro" ".backup /tmp/oc63920/oc.db" — point-in-time copy, source opened read-only (499 sessions, 23 482 messages). Used .backup instead of copying db + -wal + -shm by hand so the copy is consistent while the live service keeps writing.
    • Isolated instance: opencode serve --service --port 49387 with OPENCODE_DB pointing at the copy and its own HOME / XDG_*, so it has its own service.json, logs and lock files. The running desktop service (port 49374) was left alone.
    • After every step: read-only dump of session_v2 (id, time_updated, time_idle, time_viewed, time_suspended, resume_attempts, time_archived), session_message (id, session, type, seq, time_created, time_updated) and session_inbox, then a diff that reports "time_updated changed without new messages: YES/NO".

    Factors, one at a time (each is a clean diff, no writes)

    Step Result
    cold serve --service boot, zero clients no rows written at all
    GET /api/session none
    GET /api/session/{id} none
    GET /api/session/{id}/message?limit=5&order=desc none
    GET /api/session/active none
    "desktop": SSE /api/event + poll loop (session list, session, message), 15 s, then disconnect none
    "phone": two concurrent SSE /api/event + polls, both disconnect none
    POST /api/session/{id}/view with a valid body ({"idle": …}, HTTP 204) none — time_viewed does not move either
    POST /api/rpc/experimental.browser/attach without a registered client 400 rpc.unavailable in 37 ms — nothing to abort, no write
    SIGTERM shutdown with an SSE client attached none, even though the log shows the same GET /api/event → 200 line the real restarts show

    So: restart alone, the list endpoint, the detail endpoint, the polling shape, one client, two clients, and client detach on its own are all excluded.

    What does write

    All 20 sessions in the copy with time_updated more than 60 s past their last session_message and no time_archived were checked against the log. 18 fall inside the retained log window, and:

    • 18 of 18 land within 10 ms of the /api/event stream closing (GET /api/event logged with status 200 as the stream ends);
    • 17 of 18 land 0–4 ms after a 499 on /api/rpc/experimental.browser/attach — the aborted attach that follows that stream close. The 18th (ses_e2abd702…, 2026-11-13T17:43:22Z) is 1 ms after an SSE close but its own 499 comes 16.7 min later, so the stream close is the common ancestor and the aborted attach is the usual companion.

    No bump without a stream close, no exceptions.

    Your four rows, mapped to concrete sessions (report times are local, log is UTC):

    session last session_message new time_updated offset to aborted RPC new messages at that ms
    ses_e26af054bffeWiLkByAF6HPIl7 10:16:02.027Z 10:41:22.039Z +1 ms 0
    ses_e26bc4b84ffenQ5Ee8pgBl5LK2 10:06:42.309Z 10:42:15.685Z +2 ms 0
    ses_e265a31c6ffeiN11BRMGEO45Ju 10:42:58.480Z 10:43:29.727Z +1 ms 0
    ses_e28908eb1ffexunofM4se9m3L3 09:57:04.445Z 10:43:30.375Z +1 ms 0

    time_idle, time_viewed, time_suspended and resume_attempts are untouched in every case, and the write is exactly one column: session_v2.time_updated.

    That also explains the shape you saw: the two restarts at 10:41:10Z / 10:43:26Z are the client reconnects, and each reconnect tears down the previous browser attach for the sessions that are open in the UI. Two open sessions produce two bumps a second apart (10:43:29.727 and 10:43:30.375); a reconnect a minute later produces a third one (10:42:15.685) with no restart involved.

    session_message is not involved in any of them, and neither is boot recovery — but note that boot recovery does write when a session was genuinely suspended at shutdown: in the first isolated boot the copy still contained one in-flight session, which the new process resumed and wrote three messages into (synthetic "The server restarted while you were working", a system notice, and a failed idle), clearing time_suspended. time_updated was not touched there either.

    Two corrections to the report

    1. The event table is empty in this build — select count(*) from event is 0 on the live DB with 499 sessions. The originating event was not "not persisted for this event"; nothing is ever persisted there, so its absence says nothing about the path.
    2. There is a second, independent way to get old sessions on top: the archive sweep. 176 sessions have time_updated == time_archived (e.g. a bulk PATCH /api/session/{id} {"archived":true} loop at 11:01:29–11:01:33Z), and GET /api/session returns archived sessions in the same recency-sorted list — in my probe they occupied slots 4–12, ahead of everything else. Even with the silent write fixed, any archive sweep will push those sessions to the top of the global list.

    Repro note for whoever fixes it

    The write needs a real desktop client, because only it registers experimental.browser; without a registered RPC the endpoint fails immediately with rpc.unavailable and nothing is aborted, which is why a headless server cannot reproduce it. Attach a desktop client (browse MCP configured) to an isolated serve on a copied DB, open two sessions, restart the service, and diff session_v2 — the two open sessions get time_updated stamped at the aborted attach, with no new messages.

    Suggested direction is the one in the report: the attach handshake must not stamp session_v2.time_updated, and the default list should be scoped to project_id and/or ordered by the last session_message rather than by a session column that internal bookkeeping can rewrite.

  4. opencodejr commented on Nov 14, 2026

    @opencodejr
    Contributor

    Seen with a simulated model or a mock server, not a real provider; a maintainer should confirm.

    Thanks for isolating this. Your finding about the browser attach was the key. I could reproduce it on v2.3.0, and it still happens on the latest dev (c9957fd).

    There was no desktop app here, so the script below uses curl to make the same experimental.browser/attach call the desktop's browser pane makes, against a real opencode serve. It then drops the connection. In the test:

    • The session sat idle with no messages, and its time_updated didn't change.
    • The attach moved time_updated forward, and the drop moved it forward again.
    • No message was added either time.
    • Each time, session_v2.permission changed as well: the browser allow rules were added on attach and removed on drop. That's why it looked like only time_updated was written; the permission column wasn't in your dump.
    • GET /api/session then reported the session's updated as the moment of the drop.

    Where it comes from in the code: the browser plugin grants and revokes browser access by updating the session's permissions (syncPermissions in packages/plugin-browser/src/connection.ts). The session projector then stores a permissions change together with time_updated: event.created (packages/core/src/session/projector.ts). So every desktop reconnect that detaches a browser pane makes its session look recently active.

    The archive point is separate: archiving a session sets time_updated on purpose, so ordering or filtering archived sessions in the list would be better as its own feature request.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduction for #63920: the desktop browser pane's attach/detach rewrites session_v2.time_updated
    # of an idle session (via a session.permissions update), with no new message.
    # Needs bun and curl. Usage: bash repro.sh [opencode command...]   (default: opencode from PATH)
    set -u
    if [ $# -gt 0 ]; then OC=("$@"); else OC=(opencode); fi
    PORT=${PORT:-4199}
    W=$(mktemp -d /tmp/oc63920.XXXX)
    export HOME=$W/home XDG_DATA_HOME=$W/home/data XDG_CONFIG_HOME=$W/home/config XDG_STATE_HOME=$W/home/state XDG_CACHE_HOME=$W/home/cache
    export OPENCODE_SERVER_PASSWORD=secret
    mkdir -p "$W/proj" && cd "$W/proj" && git init -q
    "${OC[@]}" --version
    "${OC[@]}" serve --port $PORT > "$W/serve.log" 2>&1 &
    SERVER=$!
    trap 'kill $SERVER 2>/dev/null' EXIT
    for i in $(seq 1 120); do curl -s -o /dev/null -u opencode:secret localhost:$PORT/api/session && break; sleep 1; done
    H=(-s -u opencode:secret -H 'content-type: application/json')
    LOC="location%5Bdirectory%5D=$W/proj"
    SID=$(curl "${H[@]}" -H "x-opencode-directory: $W/proj" -X POST localhost:$PORT/api/session -d '{"title":"idle session"}' | sed -E 's/.*"id":"(ses_[^"]+)".*/\1/')
    echo "session: $SID"
    # Wait until the location's built-in plugins (incl. opencode.browser) have registered their RPCs.
    for i in $(seq 1 120); do
      r=$(curl "${H[@]}" -X POST "localhost:$PORT/api/rpc/experimental.browser/state?$LOC" -d '{"input":{}}')
      case "$r" in *rpc.unavailable*) sleep 1 ;; *) break ;; esac
    done
    DB=$(find "$W/home" -name '*.db' | head -1)
    q() {
      bun -e 'import {Database} from "bun:sqlite"; const d=new Database(process.argv[1],{readonly:true}); console.log(JSON.stringify(d.query("select time_updated, permission, (select count(*) from session_message where session_id=id) as messages from session_v2 where id=?").get(process.argv[2])))' "$DB" "$SID"
    }
    echo "idle      : $(q)   now=$(date +%s%3N)"
    sleep 3
    echo "idle +3s  : $(q)   now=$(date +%s%3N)"
    # What the desktop browser pane does: a long-lived attach call, then its connection drops (499).
    curl "${H[@]}" -X POST "localhost:$PORT/api/rpc/experimental.browser/attach?$LOC" \
      -d "{\"input\":{\"sessionID\":\"$SID\",\"connectionID\":\"c1\",\"version\":4}}" > "$W/attach.out" 2>&1 &
    CURL=$!
    sleep 2
    echo "attached  : $(q)   now=$(date +%s%3N)"
    kill $CURL
    sleep 2
    echo "detached  : $(q)   now=$(date +%s%3N)"
    echo "list order: $(curl "${H[@]}" "localhost:$PORT/api/session" | grep -o '"updated":[0-9]*' | head -3 | tr '\n' ' ')"

    Run it: bash repro.sh

  5. added
    status/repro-simulated
    on Nov 14, 2026
  6. ValDagon commented on Nov 14, 2026

    @ValDagon
    Author

    Confirmed on a real desktop client (v2.3.0, macOS), plus one correction to what I posted above

    Everything above was measured with a simulated client: a headless serve plus SSE
    and a curl attach, because only a registered experimental.browser RPC makes the
    endpoint hang long enough to abort. I have now run the same check on the shipping
    desktop app.

    Conditions: two sessions open in the app, opencode service restart, app reopened,
    read-only snapshots of session_v2 and session_message before and after
    (503 sessions in each snapshot).

    session messages max seq time_updated verdict
    ses_e2656f84effeeGyMBufV26qakL 191 → 191 1820 → 1820 14:19:12.634Z → 14:21:35.255Z bumped, no new message
    ses_e25a7d448ffeIuJbRuPa9S3yR1 47 → 57 474 → 535 14:20:26.194Z → 14:24:32.108Z real activity (control)

    The other 501 sessions did not change. Server log around the write:

    2026-11-14T14:21:35.243Z GET  /api/event 200
    2026-11-14T14:21:35.251Z GET  /api/event 200
    2026-11-14T14:21:35.252Z POST /api/rpc/experimental.browser/attach 499
    2026-11-14T14:21:35.253Z GET  /api/event 200
    2026-11-14T14:21:35.253Z POST /api/rpc/experimental.browser/attach 499
    2026-11-14T14:21:35.370Z GET  /api/event 200
    

    Two aborted attaches, matching the two open sessions, and the row is stamped 2 ms
    after the second 499. Same shape as the isolated run, this time with the real
    client and no mock.

    Correction to my own report: the write is not one column.

    I wrote "the write is exactly one column: session_v2.time_updated". That was an
    artifact of my dump, which did not include permission — the same reason you gave
    for why the original report looked like a single-column write.

    For this session:

    • in a copy taken at 11:55Z: permission = [{"action":"browser","resource":"*","effect":"allow"},{"action":"skill","resource":"browser","effect":"allow"}]
    • after the 14:21:35Z teardown: permission = []

    No session_message row was added and max(seq) did not move. time_viewed and
    time_idle were stamped at 14:19:12.637Z, three milliseconds after the pre-restart
    time_updated, when the session was opened in the UI — that part is ordinary viewing,
    not the teardown. My snapshot only carries five columns, so I make no claim about the
    remaining time_* columns here.

    So syncPermissions holds on a live client too: grant on attach, revoke on drop, and
    the projector stamps time_updated for the permission change.

    If a real client counts as confirmation rather than simulation, status/repro-simulated
    is probably too weak a label now — I can't change labels from my side.

    On the archive half: agreed that it is a separate request, and it now has its own
    report — #63951.

  7. r266-tech commented on Nov 14, 2026

    @r266-tech
    Contributor

    The projector fix is available in this patch: permission-only updates keep the current time_updated. It uses an SQL self-assignment; leaving the field out still triggers Drizzle's $onUpdate.

    The new regression drives the real browser plugin through attach/detach and checks permissions, unchanged timestamps/list order, and no new messages. It fails on unmodified dev d759c548aa and with the field omitted. All 56 focused tests and bun run check pass locally on macOS/Bun 1.4.4; no model call or desktop UI is involved.

    I have 3 open PRs and the submission page enforces a 3-PR limit, so I’m sharing the patch here for review. AI-assisted implementation and validation.

  8. Albert-Deng commented on Nov 19, 2026

    @Albert-Deng

    Same root cause reproduced from normal desktop use, with no restart and no curl, on Windows (Desktop 2.3.2).

    Steps: open a session that has been idle for several days, change nothing.
    Result in session_v2:

    • time_updated moves to "now"
    • session_message gains no rows (same max time_created)
    • time_viewed / time_idle / time_busy unchanged
    • permission gains the browser allow rules, so this is the attach path

    Two points that may help prioritize this:

    1. It is not rare. This machine's server log recorded 146 browser/attach responses in a few days (107 with status 400, 39 with 499). Each successful attach and its later abort writes session.permission, so a long-lived tab is bumped at least twice, and often much later when a pending attach finally aborts (observed spans up to about 83 minutes). The write therefore lands while the user is doing nothing.

    2. It is user-visible in the new Recent sidebar: sessions sort by time_updated, so old, untouched conversations keep jumping to the top without any new message.

    The linked patch is still only a compare link because of the 3-PR limit. Would a maintainer take it, or is someone else allowed to open the PR with attribution? Happy to test a build on Windows.

  9. r266-tech commented on Nov 19, 2026

    @r266-tech
    Contributor

    Thanks for the Windows confirmation. Maintainers are welcome to cherry-pick fa3486f058. It applies cleanly to dev at d33b578333. On macOS the recency regression fails on that base, and all 49 browser and session projector tests pass with the patch.

    If a build includes the fix, the Windows check is whether an old session stays in place in Recent when you open and close it without sending a message. AI-assisted validation.

  10. MordorianGuy commented on Nov 27, 2026

    @MordorianGuy

    Confirmed on Windows desktop 2.4.2 — startup tab restore bumps every restored session sequentially

    Same root cause, with a user-visible repro path: after an app update + service restart, the desktop client restores previously opened session tabs. Each restored session's browser attach aborts (499) and bumps its time_updated. 18 sessions idle since Oct 24–Nov 23 (no new messages in any of them) were all bumped within ~50 seconds, so the whole "Recent" list filled up with long-abandoned conversations.

    Server log correlated 1:1 with time_updated values from GET /api/session (local = UTC+3, log timestamps are UTC):

    22:16:02.394Z POST /api/rpc/experimental.browser/attach 499  ->  time_updated 01:16:02
    22:16:04.357Z POST /api/rpc/experimental.browser/attach 499  ->  time_updated 01:16:04
    22:16:06.318Z POST /api/rpc/experimental.browser/attach 499  ->  time_updated 01:16:06
    22:16:07.023Z POST /api/rpc/experimental.browser/attach 499  ->  time_updated 01:16:07
    22:16:09.924Z POST /api/rpc/experimental.browser/attach 499  ->  time_updated 01:16:09
    22:16:11.084Z POST /api/rpc/experimental.browser/attach 499  ->  time_updated 01:16:11
    22:16:12.210Z POST /api/rpc/experimental.browser/attach 499  ->  time_updated 01:16:12
    

    (18 attach/499 + bump pairs in total, one per restored session, spaced ~0.5–2 s apart.)

    One detail that may matter for the patch: none of the 18 bumped sessions have browser allow rules in their permissions afterwards — so in this build the bump happens on a row write where the permission column does not visibly change (consistent with Drizzle $onUpdate firing on any update to the row, as described above).

    Environment: opencode desktop 2.4.2 on Windows 10 x64 (the CLI on PATH reports v2.0.32), SQLite storage, single client. The unmerged patch referenced in this thread covers this write path, so I'm adding this datapoint here instead of opening a duplicate issue.

  11. r266-tech commented on Nov 27, 2026

    @r266-tech
    Contributor

    The patch is now in #65009. Seeing no browser rules after the 499 is consistent with attach granting them and detach revoking them. The PR regression checks the timestamp after each write, including the revoke that restores the original permissions.

    If you get a Windows desktop build whose service includes 3d172e3db7, could you repeat the startup restore without sending a message? Comparing one idle tab’s time_updated before shutdown and after restore, along with its position in Recent, would tell us whether the fix covers the desktop path you observed. The 2.4.2 trace gives us the before-patch result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/sessions-storagestatus/repro-simulatedtype/bugversion/v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions