Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
c833750
Add the sync op log schema, model, stores and service
aharonamir Nov 29, 2026
5dd7814
Serve the sync push and pull routes
aharonamir Nov 29, 2026
24b6e67
Put a minted device on the household's existing owner member
aharonamir Nov 29, 2026
5ad80a3
Document the sync v0 routes in the backend OpenAPI
aharonamir Nov 29, 2026
12ce232
Fan out sync.changed to the sender's other devices of the member
aharonamir Nov 29, 2026
8d5222e
Add the sync.changed gateway protocol surface
aharonamir Nov 29, 2026
c2c9ea6
Route sync.changed to onSyncChanged on the client
aharonamir Nov 29, 2026
04dd8e7
Add the sync wire types and OkHttp transport
aharonamir Nov 29, 2026
bc6dd4b
Seal and open sync payloads with SyncCodec
aharonamir Nov 29, 2026
1ef1468
Push, pull and apply the sync op log with SyncEngine
aharonamir Nov 29, 2026
77cfe77
Bound sync pushes by body size and report skipped ops
aharonamir Nov 29, 2026
197e019
Store the sync cursor as plain settings
aharonamir Nov 29, 2026
898fa90
Treat a rename as a newer thread revision
aharonamir Nov 29, 2026
fc52e83
Add the backend sync pass and its ownership rule
aharonamir Nov 29, 2026
23336f1
Route chat sync through the paired backend
aharonamir Nov 29, 2026
60dd0eb
Bound the sync backlog and key titles by content
aharonamir Nov 29, 2026
b2d3aeb
Close and guard chat actions the backend cannot sync
aharonamir Nov 29, 2026
1a168d0
Record each thread's last synced title in settings
aharonamir Nov 29, 2026
c0bb638
Push only titles that differ from the last synced record
aharonamir Nov 29, 2026
c372135
Merge remote chat ops into the Windows store
aharonamir Nov 30, 2026
e6c4d08
Add the chat.changed frame to the host protocol
aharonamir Nov 30, 2026
251f732
Run a sync pass from the Windows sidecar
aharonamir Nov 30, 2026
65db6b3
Reload the open Windows chat when a sync pass changes it
aharonamir Nov 30, 2026
b6920cd
Read one session snapshot per sync pass
aharonamir Nov 30, 2026
2492bd6
Start a sync pass on every reconnect
aharonamir Nov 30, 2026
003f25d
Never cancel a running sync pass for a trigger
aharonamir Nov 30, 2026
1bc7a67
Report the ops a pass applied even when its push fails
aharonamir Nov 30, 2026
eaa1be4
Hold the sync state when a merge did not reach disk
aharonamir Nov 30, 2026
a8c974d
Reset the sync state when the household changes
aharonamir Nov 30, 2026
fc1a2f8
Add a sealed task draft to the shared client
aharonamir Nov 30, 2026
8d7be1f
List the household's devices from the shared client
aharonamir Nov 30, 2026
3d590bd
Send and open sealed tasks on the phone
aharonamir Nov 30, 2026
367ab94
Send a task to the phone from the Windows side
aharonamir Nov 30, 2026
fc2780c
Join a title op id onto its signature line
aharonamir Nov 30, 2026
84b2889
Describe the task composer frames in the host protocol
aharonamir Nov 30, 2026
d7d670f
Wait for the server to accept a task sent from the phone
aharonamir Nov 30, 2026
e62b53a
Correlate the PC's task sends and approvals by trace
aharonamir Nov 30, 2026
10a1d2b
Measure the sync exit check against the real routes and schema
aharonamir Nov 30, 2026
693bba3
Echo the traceparent on a rate-limited task
aharonamir Nov 30, 2026
6f8a32f
Document the composer frames and drop the unused devices.error family
aharonamir Nov 30, 2026
f514bf0
fix(sync): decide a retried op id by the op it names, not its body
aharonamir Nov 30, 2026
4c3e499
Key the Android sync state to the household that wrote it
aharonamir Nov 30, 2026
0b6251d
Describe the shipped sync payloads and duplicate rule
aharonamir Nov 30, 2026
d3da8cd
Catch a torn credential tuple and stop reporting own ops as skips
aharonamir Nov 30, 2026
a717ae7
State the credential-tear residual instead of promising it cannot happen
aharonamir Nov 30, 2026
9c86ac4
Quote the suite total from the final run
aharonamir Nov 30, 2026
83fb687
Record the physical exit run for the sync and task clauses
aharonamir Nov 30, 2026
24bddbf
Record the sealed task that ran each way after the revoke was undone
aharonamir Nov 30, 2026
34ac659
Merge origin/main into the sync branch
aharonamir Nov 30, 2026
ea96cb4
Record the PC-to-phone task refused after screen access was turned on
aharonamir Nov 30, 2026
b6047b0
Show the send status instead of leaving it collapsed
aharonamir Nov 30, 2026
7b08499
Keep the header buttons clickable inside the drag caption
aharonamir Nov 30, 2026
8cbb21e
Tick the turn and task clauses on the measured run
aharonamir Nov 30, 2026
858ce4a
Keep the board handler the gateway attaches over
aharonamir Nov 30, 2026
0d81721
State the title rule the sync pass actually follows
aharonamir Nov 30, 2026
b3dd51a
Transform settings inside the edit so two writers cannot revert each …
aharonamir Nov 30, 2026
62ef11d
Drop a chat reload that a newer open or a turn superseded
aharonamir Nov 30, 2026
d902425
Split the new Android files to one behaviour per file
aharonamir Nov 30, 2026
e0c8b4b
Wait for the opened link to start before asserting it
aharonamir Nov 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Report the ops a pass applied even when its push fails
syncNow returned 0 on any exception, so a pull that applied ops and a push that then failed looked like nothing happened, contradicting the documented return. A push failure is now handled beside the push and returns the pull's applied count; a failed pull still returns 0 because the engine never reported one. Every failure path logs one line with the exception class name only (pull, push, notify, missing key), never a message that could quote plaintext. Tests cover the push and pull paths and their log lines.

Part of #32.
  • Loading branch information
aharonamir committed Nov 30, 2026
commit 1bc7a67de1e2cdf3d5bbb7283722c19f9fd4f4e0
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ import kotlinx.serialization.json.put
* under a new op id.
* - An op this PC wrote is inert when it comes back on a later pull, so our own
* older title can never undo a rename that has not been pushed yet.
*
* A failure logs one line with the exception's class name only, never a message,
* a title or a turn: those could carry plaintext.
*/
class BackendSyncHost(
private val sessions: () -> BackendSession?,
private val chats: ChatStore,
private val cursorFile: Path,
private val notify: (String) -> Unit,
private val log: (String) -> Unit = {},
private val http: SyncHttp = OkHttpSyncHttp(),
private val scope: CoroutineScope,
) {
Expand All @@ -69,16 +73,21 @@ class BackendSyncHost(

private val titlesFile: Path = cursorFile.resolveSibling(TITLES_FILE)

/** One pull-then-push pass; waits for a pass already in flight. */
/**
* One pull-then-push pass; waits for a pass already in flight.
*
* @return the ops this pass applied. A failed push does not hide them; a
* pull that failed returns 0, because the engine never reported a count.
*/
suspend fun syncNow(): Int = withContext(Dispatchers.IO) {
try {
pass.withLock { runPass() }
} catch (cancelled: CancellationException) {
throw cancelled
} catch (_: Exception) {
// A store read, a bad tag or a transport reset: the cursor and the
// applied rows are the only durable result. Nothing is logged,
// because a failure can quote plaintext.
} catch (failure: Exception) {
// A store read, a bad tag or a transport reset. Nothing is logged
// with the message, because it could quote plaintext.
log("sync pass failed (${failure.javaClass.simpleName})")
0
}
}
Expand Down Expand Up @@ -158,7 +167,11 @@ class BackendSyncHost(
// One snapshot: the token and the key must describe the same household.
val session = sessions() ?: return 0
if (session.deviceId.isBlank() || session.accessToken.isBlank() || session.gatewayUrl.isBlank()) return 0
val key = backendContentKey(session) ?: return 0
val key = backendContentKey(session)
if (key == null) {
log("sync pass skipped: the stored session has no usable content key")
return 0
}

val cursorStore = SyncCursorFile(cursorFile)
val start = cursorStore.read()
Expand All @@ -181,7 +194,14 @@ class BackendSyncHost(
baseUrl = { BackendUrls.httpBase(session.gatewayUrl) },
)

val report = engine.pull()
val report = try {
engine.pull()
} catch (cancelled: CancellationException) {
throw cancelled
} catch (failure: Exception) {
log("sync pull failed (${failure.javaClass.simpleName})")
return 0
}

// The pull is done: persist its cursor and announce what it applied even
// if the push below fails, or the next pass would see duplicates and
Expand All @@ -193,14 +213,25 @@ class BackendSyncHost(
titlesStore.write(recordedTitles)
}
for (id in target.changed) {
runCatching { notify(id) }
runCatching { notify(id) }.onFailure { failed ->
log("sync notify failed (${failed.javaClass.simpleName})")
}
}

val backlog = backlog(session.deviceId, recordedTitles)
if (backlog.drafts.isEmpty()) {
return report.applied
}
val answer = engine.push(backlog.drafts)
val answer = try {
engine.push(backlog.drafts)
} catch (cancelled: CancellationException) {
throw cancelled
} catch (failure: Exception) {
// What the pull applied still stands (and its cursor is persisted),
// so the caller sees that count instead of a misleading zero.
log("sync push failed (${failure.javaClass.simpleName})")
return report.applied
}
// Only an acknowledged op proves the server holds the title. A duplicate
// ack means it already did.
var recorded = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ internal fun runProtocol(options: CliOptions) {
chats = store,
cursorFile = dataDir.resolve("sync-cursor.json"),
notify = { conversationId -> io.send(Protocol.chatChanged(conversationId)) },
log = io::log,
scope = backendScope,
)
val runner = EngineTaskRunner(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,34 @@ class BackendSyncHostTest {
}
}

@Test
fun `a failed push still reports the ops the pull applied`() = runBlocking {
Harness().use { harness ->
harness.server.seed(
SyncKind.TURN_APPEND,
"c-1",
"m-1",
turnPayload("user", "hello", 10),
opId = "m-1",
clientAt = 10,
)
harness.server.pushFailure = IllegalStateException("offline")

assertEquals(1, harness.host().syncNow())
assertTrue(harness.logs.any { it.contains("sync push failed (IllegalStateException)") })
}
}

@Test
fun `a failed pull logs its class and applies nothing`() = runBlocking {
Harness().use { harness ->
harness.server.pullFailure = IllegalArgumentException("bad tag")

assertEquals(0, harness.host().syncNow())
assertTrue(harness.logs.any { it.contains("sync pull failed (IllegalArgumentException)") })
}
}

@Test
fun `triggers during a slow pass add one follow-up pass and cancel nothing`() = runBlocking {
Harness().use { harness ->
Expand Down Expand Up @@ -275,6 +303,7 @@ class BackendSyncHostTest {
val chats = ChatStore(dataDir.resolve("conversations.json"))
val server = FakeSyncServer()
val notified = mutableListOf<String>()
val logs = mutableListOf<String>()
private val sessions = BackendSessionStore(dataDir.resolve("backend-session.json"))
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)

Expand All @@ -292,6 +321,7 @@ class BackendSyncHostTest {
chats = chats,
cursorFile = dataDir.resolve(CURSOR_FILE),
notify = { id -> notified += id },
log = { line -> logs += line },
http = server,
scope = scope,
)
Expand All @@ -314,12 +344,15 @@ class BackendSyncHostTest {
var completedPulls = 0
var cancelledPulls = 0
var pullGate: CompletableDeferred<Unit>? = null
var pushFailure: Exception? = null
var pullFailure: Exception? = null
val firstPullStarted = CompletableDeferred<Unit>()
private var heldOnce = false

private val head: Long get() = log.maxOfOrNull { it.seq } ?: 0L

override suspend fun pull(token: String, baseUrl: String, after: Long, limit: Int): PullAnswer {
pullFailure?.let { throw it }
pulls += 1
pullAfters += after
pullTokens += token
Expand All @@ -344,6 +377,7 @@ class BackendSyncHostTest {
}

override suspend fun push(token: String, baseUrl: String, ops: List<SealedOp>): PushAnswer {
pushFailure?.let { throw it }
pushBatches += ops
return PushAnswer(head = head, accepted = ops.map(::accept))
}
Expand Down