Skip to content

Move the mobile outbox out of the App Group container and keep live phone links - #391

Open
oliverames wants to merge 3 commits into
mattt:mattt/ios-appfrom
oliverames:pr/ios-outbox-liveness
Open

oliverames wants to merge 3 commits into
mattt:mattt/ios-appfrom
oliverames:pr/ios-outbox-liveness

Conversation

@oliverames

@oliverames oliverames commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Split out from #381 per #364.

  • Moves the iOS outbox SQLite database from the App Group container to Application Support (with migration of any existing queue and anchors), so iOS doesn't terminate the app with 0xdead10cc on suspend.
  • Adds a liveness check to MobileService.attach(_:) (MobilePeerLink.answers(within:)), so a second connection from the same device doesn't tear down an active link that is still responding.

Review fixes

  • migrateFromAppGroup copies each file and removes the old ones only when every copy matches its source. A failed checkpoint or copy leaves the old outbox in place, and shared() opens the old outbox until the migration is complete, so a partial migration can't strand queued records and anchors.
  • answers(within:) no longer waits for the status call after the timer returns. The first of the call or the timer wins; a silent socket can't hold the result open.
  • attach remembers whether the device was paired before the liveness check and refuses to install a link for a device unpaired during the wait.
  • accept checks the pairing again right before it attaches, so an unpair that lands while the welcome, the MCP handshake and the peer connection are still being set up can't leave the forgotten device with a live link.
  • An old outbox that can't be opened is renamed in place with a .unreadable suffix instead of being opened on every launch, so shared() moves on to the new location and nothing stays open in the App Group. A failed checkpoint still leaves a readable old outbox in place.

Testing

  • swift format lint --strict is clean on the changed files.
  • imcp-serverTests: 254 tests, 1 skipped (pre-existing), 0 failures. New tests cover the restartable migration, quarantining an unopenable database and opening the new path after it, the silent socket, the unpair-during-check race, and refusing a device that's no longer paired at attach.
  • accept takes an NWConnection, so the unpair-during-setup race itself isn't driven end to end; the test covers the check it relies on.
  • xcodebuild -scheme "iMCP iOS" -destination generic/platform=iOS CODE_SIGNING_ALLOWED=NO CODE_SIGNING_REQUIRED=NO build succeeds.
  • CI: this PR targets mattt/ios-app, and the repository's CI workflow runs on pull requests to main, so there are no checks on this PR.

…hone links

Moving the SQLite outbox into Application Support avoids 0xdead10cc kills when iOS suspends the app, and existing outboxes migrate automatically. On the Mac side, a duplicate connection from the same device no longer replaces a link that still answers a status check within the liveness timeout.
@mattt

mattt commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Hi @oliverames. Thanks for sending this. Two things before I merge it into mattt/ios-app:

  • migrateFromAppGroup ignores a failed checkpoint and a failed move, and it moves the database, -wal, and -shm files one at a time. If a move fails or the app is ended part way, shared() still opens the new path, and the fileExists guard skips the migration on every later launch, so queued records and anchors can be left behind. Could you make it restartable, with the old file as the source of truth until the new one is complete?
  • answers(within:) can outlast its timeout. The task group still waits for the status call after the timer returns, and cancelling call also cancels the timer that would disconnect it, so it looks like a silent socket could hold off a working connection indefinitely. An unpair during the wait also lets attach install the link of the device that was just forgotten.

Mattt's mattt#391 review:

- migrateFromAppGroup copies each file and removes the old ones only when
  every copy matches its source. A failed checkpoint or copy leaves the old
  outbox in place, and shared() opens the old outbox until the migration is
  complete, so queued records and anchors are never left behind.
- answers(within:) no longer waits for the status call after the timer
  returns: the first of the call or the timer wins, and a silent socket
  can't hold the result open.
- attach remembers whether the device was paired before the liveness check
  and refuses to install a link for a device unpaired during the wait.

Tests cover the restartable migration, an unopenable database, the silent
socket, and the unpair-during-check race.
oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 20, 2026
@oliverames

oliverames commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @mattt. Both are fixed.

migrateFromAppGroup copies each file and only removes the old ones once every copy matches its source. A failed checkpoint or copy leaves the old outbox in place, and shared() opens the old outbox until the migration is complete, so a partial migration can't strand queued records or anchors.

answers(within:) doesn't wait for the status call after the timer wins anymore. The first of the call or the timer returns, so a silent socket can't hold the result open. attach remembers whether the device was paired before the liveness check and refuses to install a link for a device that got unpaired during the wait.

There are tests for the restartable migration, an unopenable database, the silent socket and the unpair-during-check race. imcp-serverTests is at 252 tests with 1 skipped and no failures, and format is clean.

oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 21, 2026
Port of mattt#391 review fixes (f4ae46a): copy-then-verify outbox
migration, shared() opens the old outbox until migration completes,
answers(within:) returns on the first of call or timer, attach refuses a
device unpaired during the liveness wait.
…adable outbox

accept() now attaches through attachIfPaired, which closes the link when the
device was unpaired while the connection was being set up. An old outbox that
cannot be opened is renamed to .unreadable in place, so shared() falls through
to the new path and nothing in the App Group is opened.
@oliverames

Copy link
Copy Markdown
Contributor Author

@mattt I found two gaps left in my last round, both fixed.

The unpair check only covered the liveness wait. accept authenticates the device and then waits on the welcome, the MCP handshake and the peer connection before it attaches, and an unpair in that window wasn't caught. accept now checks the pairing again right before it attaches.

An old outbox that couldn't be opened made shared() try it again on every launch, so sync stayed off for good, and a file that opened but failed to copy kept a lock in the App Group. An unopenable old outbox is now renamed in place with a .unreadable suffix, so the app moves on to the new location. imcp-serverTests is at 254 with 1 skipped and no failures, and the iOS scheme builds.

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.

2 participants