Skip to content

feat(agent): take the phone's chats into the chat records - #271

Open
geoolekom wants to merge 4 commits into
mainfrom
phone-chats-into-records
Open

geoolekom wants to merge 4 commits into
mainfrom
phone-chats-into-records

Conversation

@geoolekom

@geoolekom geoolekom commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What changes

A paired phone can now send the computer the chats it started and the turns it wrote in any chat. The agent stores them in its own chat records (source chat) as turns written elsewhere, with the times and the conversation id the phone gave them. So the computer's chat-list and chat-get show them, facts are learned from them, and their time is counted, as for the computer's own chats.

  • New sync command sync-push-chat. One push is {id, seen, turns}: the phone's conversation id, the time of the last item the phone knows the computer has, and up to 200 turns (role, time, text). The answer is {"ok":true,"format":2,"id","forked","added"}. The command, its limits and its errors are written in agent/src/phone/push.rs:
    • a push is one line of at most 16 KiB (phone_pairing::MAX_LINE; a longer one closes the channel, as for any line the agent does not expect). The phone measures what it writes and splits a long run into pushes that fit, each with the seen of the one before. A turn that is too long for a line of its own is split by the phone into turns of the same role, in order, each a millisecond after the one before, so nothing is cut or left out and the computer's chat holds the parts one after the other;
    • a malformed push, two equal turns in one push, or a time that is not spelled ...T...Z (UTC) is {"error":"invalid sync request"};
    • a turn more than a day after the computer's clock is {"error":"time too far ahead"}, so the phone can tell a wrong clock from a wrong shape. The computer stores its own later turns of a chat after the latest turn the chat holds, so a time days ahead would carry them with it; a time within the day is kept, and does that for up to a day. A phone that is behind is not refused;
    • an id that is not a lower case UUID is invalid chat, a seen that no item of the chat has is unknown seen (the phone then sends the chat again from its start), and a push during the erasure is erasing.
  • The phone's turns are stored under the phone's id, through the same file store the chat answers with. The times of the phone and the computer are never used to order turns (the one guard above only refuses a clock that is far ahead): records stay in the order they were stored, and the point the phone last saw is found by the time it carries.
  • A resend adds nothing. A push whose turns are all stored already, in order, right after seen, answers added: 0, whatever was stored after them since. The check does not rely on the store dropping an equal turn.
  • Both sides adding turns makes a fork. When the computer's chat has records after seen, it is left as it is. The agent stores the earlier part plus the phone's turns as a new chat and answers with its id. The id is made from the push, so a resend gives the same fork and not a second one.
  • The write is one step under the chat's intake and the store's lock. A message of the person, or the marker a Claude Code answer writes at its end, is stored either before the push is checked or after it is written, never between.
  • An erasure cannot be undone by a push. The check that may ask the host whether everything is being erased runs once, as the command starts. While the records are held nothing may wait for the host, so the manager counts the erasures that reached the deleting step (Manager::erasures, no I/O) and the push compares that count as it starts with the count as it writes. An erasure that began, and even ended, in between makes the push answer erasing and store nothing.
  • A phone turn in a computer chat makes the next turn on Claude Code start a new session from the records instead of resuming the old one, as any turn written elsewhere does. This holds also for a turn that lands while a Claude Code answer is being written: the answer gets no marker if any turn of any role, other than the ones it stored itself, came after the turn it answers, and the marker is written under the intake, so a phone turn cannot land between that look and the marker.
  • sync-chats says where each chat was started (origin: computer, phone or continued). It is kept in a hidden chat_origin call in the records, which models and the earlier-part builder skip. The turns copied into a fork are stored without telling the fact learner or the activity recorder again, so nothing is counted twice.
  • sync-chats format is 2 because the chat items have a new field. The phone code reads the items by key and does not check format, so an old phone still reads the answer.

Known limits

  • "Continued on the phone" is shown as the continued origin in sync-chats. The titles in chat-list are unchanged.

Verification

  • cargo fmt --check, cargo clippy -p construct-agent -p chat-core --all-targets --locked -- -D warnings, pre-commit run --all-files: clean. cargo test -p construct-agent -p chat-core -p phone-pairing --locked --no-fail-fast: everything passes except timing tests that are not part of this change. a_backoff_is_not_cut_short_by_a_new_prompt in agent/tests/manage.rs fails on a clean main build too (6 of 16 runs under load). Two chat_claude tests (a_silent_claude_is_given_up, a_turn_that_is_dropped_leaves_nothing_running) failed once while the machine was at a load of 6 to 9, and pass alone. All of them pass in CI.
  • With a test phone (agent/tests/phone_chats.rs): a chat the phone started is in chat-list and chat-get with the phone's id and times; the same turns sent twice add nothing; a turn of the same text at another time is a new turn; a malformed push or a bad id is refused and stores nothing; both sides adding turns gives a fork with the computer's chat unchanged, the same fork on a resend, and nothing hidden in it but where it started.
  • A turn too long for one line, sent as same-role parts a millisecond apart, is stored whole and in order, and a resend of it adds nothing (agent/tests/phone_chats.rs).
  • A fact is learned from a phone turn once, and the copied part of a fork is not sent again; a phone turn from before the first start is history and is not sent (agent/tests/chat_facts.rs).
  • A phone turn in a computer chat makes the next Claude Code turn start from the records, with the phone turn in the prompt, no --resume and a new session id; the turn after it resumes that session; a resend changes nothing (agent/tests/chat_claude.rs).
  • A push over the real paired channel is stored and answered, and refused while an erasure runs (agent/tests/phone.rs).
  • The phone's clock behind or ahead of the computer's changes no order; a turn more than a day ahead is refused as time too far ahead and one before the computer's clock is not; a resend that overlaps the stored turns adds only the new ones; an unknown seen is refused and a resend from the start is stored; two turns of one time do not make the next push a fork; a fork keeps the calls of tools with their results; the model never sees where a chat was started (agent/tests/phone_chats.rs).
  • Nothing is stored while everything is erased: at the command's start, and when an erasure begins after it (the count of erasures changes between the start and the write), and the host is asked once (agent/tests/phone_chats.rs). The manager counts an erasure that got as far as deleting and not one that found the host already erasing (agent/tests/manage_erase.rs). An own store that reads the count from inside its erase finds it already changed, so the count may not move after the stores are erased (agent/tests/manage_erase.rs).
  • The marker's count of the answer's own turns: an answer that tells text, calls a tool, gets its result and tells more, with nothing from outside, gets exactly one marker (chat-core/tests/intake.rs). Without the count of the text told before a call, that test fails.
  • The intake and the marker (chat-core/tests/intake.rs): a message is stored only after the work under the intake is done, shown by the first record it stores and not by a pause; an answer that ends by itself gets its marker; a turn of either role stored from outside during an answer leaves it without a marker; and a turn stored right after the look the marker goes by, from a thread that works under the intake, never comes before the marker, which fails if the marker does not hold the intake.

Signed-off-by: Yehor Komarov <10210242+geoolekom@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3172d535-b1cd-407e-90f1-8c16dee96f9c
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Review fixes for the push of a phone's chats.

- A push is written as one step under the chat's intake and the store's
  lock, so a message of the person or the marker of a harness session is
  stored either before the push is checked or after it is written. The
  erasure check is asked again once the store is held.
- The marker of a harness session is looked up and written under the
  intake too.
- A `seen` that no item of the chat has is refused (`unknown seen`), and
  the item is found by its time from the end, so two items of one time do
  not make a false fork.
- A push is refused when it carries two equal turns, a turn more than a
  day after the computer's clock, or a time that is not spelled
  `...T...Z`. The times of the phone and the computer are still never
  used to order turns.
- The fork id is made with the shared length-prefixed digest.
- `valid_id` of the chat is public and used by the push and the handoff.
- The hidden calls of the chat are named for what they are
  (`is_own_call`), and the origin note is documented as no session
  marker.
- The push command moves to its own module, `phone::push`, which holds
  the wire contract (request, line limit, answer, errors). The rule for
  a fork is told once, in `chat::from_phone`.
- Tests: the order of a chat is the order its turns arrived, whatever
  the phone's clock says; a turn far ahead is refused; a resend that
  overlaps stored turns adds the new ones only; an unknown `seen`;
  two turns of one time; the calls of tools kept in a fork; the model
  never sees where a chat was started; nothing is stored during an
  erasure; a message waits for work under the intake.

Signed-off-by: Yehor Komarov <10210242+geoolekom@users.noreply.github.com>
…rasure without I/O

A phone turn too long for one line is now split by the phone into turns of the
same role, a millisecond apart, so nothing is left out of the computer's chat.

A turn more than a day after the computer's clock is refused as
`time too far ahead`, not as a malformed push. Times still order nothing.

The write-time erasure check no longer reads the host while the intake and the
records are held: the manager counts the erasures that reached the deleting
step, and a push compares that count as it starts and as it writes. The check
that may wait for the host runs once, as the command starts.

The look before a Claude Code marker counts a turn of either role that the
answer did not store itself, so a phone turn of any kind during an answer
withholds the marker. Tests: the marker waits for the intake and a turn that
lands between the look and the marker never precedes it; the intake test uses
a signal instead of a sleep; the manager's erasure count; split turns.

The module docs tell each fact in one place: the wire contract in `push.rs`,
what is stored and when a chat forks in `from_phone.rs`, the line limit next to
`MAX_LINE`.

Signed-off-by: Yehor Komarov <10210242+geoolekom@users.noreply.github.com>
… owner of the erasure rule

An answer that tells text, calls a tool, gets its result and tells more text,
with nothing from outside, gets exactly one marker. This is the only test that
fails when the count of the answer's own turns misses the text told before a
call.

The manager's count of erasures is read by an own store from inside its
`erase`, and is already 1 there: a push that read the count before the deleting
began and writes after it must see the change, so the count may not move after
the stores are erased.

The rule that keeps a push from writing into an erasure is told in one place,
`Manager::erasures`; the other places that touch it point there.

Signed-off-by: Yehor Komarov <10210242+geoolekom@users.noreply.github.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant