Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,41 @@ Still open, and why:
staged in #12561 and needs two protected environments with their own
tokens before it can run.
- The Android project is still on androidbrowserhelper 2.5.0.

## 2026-11-23T01:25Z Update: the build never holds up a report

The last three commits of the Android-build change (#12723) merged
before any reviewer read them, so they were read again afterwards. Two
real faults came out of that: one in how the build travels, one in how
the edge keeps it.

A report that carries the build is megabytes; every other report is
kilobytes. The laptop gives a report twenty seconds. On a connection
too slow to send the build in that time, the report failed, and the
next was built the same way and failed too, for as long as the edge
still lacked the build. In that time no report arrived at all, so the
edge took the laptop for gone while it was there. Nothing of the kind
could happen before the build was put into reports.

- A report that carried the build and did not get through, whether cut
off or turned away, is followed at once by one without the build.
The build is tried again ten minutes later. At worst one report in
ten minutes is lost to it.
- A build file that cannot be read is left out the same way, where it
used to stop the report.
- Whether the build and the changed sections fit one report is now
measured on the report as sent, in bytes. It was estimated in
characters, and text outside ASCII is up to three bytes a character,
so the estimate could pass a report the edge then refused as too
large.
- When the build goes alone, the sections it displaced follow at once
instead of at the next ordinary report.

- The edge wrote a new build over the pieces of the old one and only
then recorded the new build. A write that stopped half-way left
pieces of two builds under the old record, and with the laptop away
those were handed out as the old build. The old record is now removed
before its pieces are touched, so a half-written copy is no copy: the
edge says it has none, hands nothing out, and asks for the build
again. Pieces left by a larger build are removed whether or not its
record survived.
10 changes: 10 additions & 0 deletions pm-stack/console/edge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ runs the fleet's tools can be reached.
| Direct actions (reply to or close a prompt, comment, rank an issue, move a task) | edge, once the laptop has been quiet for a minute | run at the edge through the laptop's own classes, and never sent to the laptop, so they are not done twice; a new prompt is filed at the edge and only its queue task is held for the laptop |
| Actions not safe to do late (merge, units, unblock, …) | laptop, relayed at once | refused, saying why |
| The dashboard's own pages, logs, one pull request's page | laptop | a plain notice with a link to the app |
| The signed Android build (`/console/pm-console.apk`) | laptop, and a copy at the edge | the copy is served, and the app says so when the copy is older than the laptop's newest |

The list of actions that may be done late is `HOLDABLE` in
`../src/edge-proto.ts`.
Expand Down Expand Up @@ -50,6 +51,15 @@ laptop: pm-console (../src/server.ts), reporting through ../src/sync.ts
state that changed, how current each unchanged one is, and what became
of anything held for it. The answer says whether anyone is looking, and
carries the actions the owner took while it was away.
- **The signed Android build travels in a report**, once per build, when
the edge says it lacks it, and is kept only if it matches its
fingerprint. It never stands in the way of the reports themselves,
which are how the edge knows the laptop is there. Beside sections that
would take the report past its room (counted in bytes as sent) it goes
alone and they follow at once. If a report carrying it does not get
through (a slow connection runs out of time, or it is turned away), or
the file cannot be read, the reports after that go without it, and it
is tried again ten minutes later.
- **An action for the laptop is relayed at once** when the laptop is in
reach; its answer carries what it read again, so the edge's copy shows
the change before the next report.
Expand Down
16 changes: 14 additions & 2 deletions pm-stack/console/edge/src/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { DIRECT_ACTIONS, DIRECT_READ_SECTIONS, directClasses, directRead } from
import { Devices, KeyedLimiter, Limiter } from "../../src/devices.ts";
import { type Act, DOC_NAMES, type DocName, Docs, Stamped } from "../../src/docs.ts";
import {
APK_LIMIT,
type ApkReport,
apkReport,
type EdgeConfig,
Expand Down Expand Up @@ -90,6 +91,8 @@ const SIGNATURE = /^[A-Za-z0-9_-]{86}$/;
const SYNC_REFUSALS = 20;
/** The Android build is kept in pieces of this many bytes. */
const APK_CHUNK = 512 * 1024;
/** The most pieces a build that may be carried can take. */
const APK_PIECES = Math.ceil(APK_LIMIT / APK_CHUNK);
/** A build that could not be kept is asked for again no sooner than this. */
const APK_RETRY_MS = 10 * 60_000;
/** With no report for this long, the laptop is not there to ask. */
Expand Down Expand Up @@ -270,10 +273,19 @@ export class Edge {
const sha256 = [...digest].map((b) => b.toString(16).padStart(2, "0")).join("");
if (bytes.byteLength !== build.size || sha256 !== build.sha256)
throw new Error("the build sent is not the build described");
const before = await this.apkKept();
// The record of the copy kept so far goes first. Its pieces are about
// to be written over, and a write that stops half-way must not leave
// pieces of two builds under a record that describes one of them.
// With no record there is no copy: nothing is handed out, and the
// build is asked for again.
if (await this.apkKept()) {
this.keptApk = null;
await this.kv.delete("apk");
}
const chunks = Math.ceil(bytes.byteLength / APK_CHUNK);
for (let i = 0; i < chunks; i++) await this.kv.put(`apk:${i}`, bytes.slice(i * APK_CHUNK, (i + 1) * APK_CHUNK));
for (let i = chunks; i < (before?.chunks ?? 0); i++) await this.kv.delete(`apk:${i}`);
// Pieces left by a larger build, whether or not its record survived.
for (let i = chunks; i < APK_PIECES; i++) await this.kv.delete(`apk:${i}`);
this.keptApk = { sha256, size: build.size, at: build.at, chunks };
await this.kv.put("apk", this.keptApk);
}
Expand Down
92 changes: 69 additions & 23 deletions pm-stack/console/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const FAST_MS = 5_000;
const IDLE_MS = 30_000;
const WATCHED_EVERY_MS = 60_000;
const SYNC_TIMEOUT_MS = 20_000;
/** After a report that carried the Android build did not get through,
* the build is left out of the reports that follow for this long. */
const APK_AGAIN_MS = 10 * 60_000;
const DONE_KEPT = 300;

type Fate = "started" | { ok: boolean; error?: string };
Expand Down Expand Up @@ -72,6 +75,12 @@ export class EdgeLink {
private watchedAt = 0;
private timer: ReturnType<typeof setTimeout> | null = null;
private busy = false;
/** The Android build is not put in a report before this time. */
private apkNotBefore = 0;
/** The last report left something out that the next should carry at
* once: the sections, when the build went alone, or everything but
* the build, when a report carrying it did not get through. */
private more = false;
/** Actions being done right now, by the name the edge gave them. */
private running = new Set<string>();
private now: () => number;
Expand Down Expand Up @@ -180,44 +189,78 @@ export class EdgeLink {
this.acks.push({ id: held.id, ...fate });
}

/** The build could not be sent. It is left out of the reports that
* follow for a while: nothing about the build may stand in the way of
* an ordinary report, which is how the edge knows the host is there. */
private holdBuild(why: string): void {
this.apkNotBefore = this.now() + APK_AGAIN_MS;
this.log(`${why}; reporting without it for ten minutes`);
}

/** One report to the edge. Returns its answer, or null if it could not
* be reached or refused the report; nothing is lost either way. */
async once(): Promise<SyncResponse | null> {
if (this.busy) return this.last;
this.busy = true;
try {
const pair = await this.keys();
this.more = false;
const snapshot = this.o.docs.snapshot();
let sections = snapshot.filter((s) => this.sent.get(s.name) !== s.hash || this.want.has(s.name));
const acks = [...this.acks];
const at = Math.max(this.now(), this.lastAt + 1);
// The Android build: always what it is, and the build itself only
// when the edge has said it lacks this one.
// when the edge has said it lacks this one (and not for a while
// after it could not be sent).
const build = (await this.o.apk?.().catch(() => null)) ?? null;
const apk: ApkReport | null =
build && build.size <= APK_LIMIT ? { sha256: build.sha256, size: build.size, at: build.at } : null;
if (apk && build && this.want.has("apk")) {
apk.data = Buffer.from(await build.read()).toString("base64");
// The build and large sections together could pass what the edge
// takes in one report, and a report refused as too large would be
// built the same way again and again. So the build goes alone
// then; the sections are not marked sent and follow next time.
const others = sections.reduce((sum, s) => sum + JSON.stringify(s.data).length, 0);
if (apk.data.length + others > REPORT_ROOM) sections = [];
if (apk && build && this.want.has("apk") && this.now() >= this.apkNotBefore) {
try {
apk.data = Buffer.from(await build.read()).toString("base64");
} catch (error) {
this.holdBuild(`the Android build could not be read (${(error as Error).message})`);
}
}
const config = await this.o.config();
const have = Object.fromEntries(snapshot.map((s) => [s.name, { hash: s.hash, at: s.at, error: s.error }]));
const phones = await this.o.phones.links();
const acts = this.o.docs.recentActs();
const encoded = (carried: typeof sections) => {
const message: SyncRequest & { acts: unknown } = {
v: 1,
at,
...(this.o.version ? { version: this.o.version } : {}),
...(apk ? { apk } : {}),
config,
sections: carried,
have,
acks,
phones,
acts,
};
return new TextEncoder().encode(JSON.stringify(message));
};
let body = encoded(sections);
// The build and large sections together could pass what the edge
// takes in one report. Measured on the report as it would be sent,
// in bytes: when it is past the room there is, the build goes
// alone; the sections are not marked sent and follow at once.
if (apk?.data && sections.length > 0 && body.byteLength > REPORT_ROOM) {
sections = [];
body = encoded(sections);
this.more = true;
}
const message: SyncRequest & { acts: unknown } = {
v: 1,
at,
...(this.o.version ? { version: this.o.version } : {}),
...(apk ? { apk } : {}),
config: await this.o.config(),
sections,
have: Object.fromEntries(snapshot.map((s) => [s.name, { hash: s.hash, at: s.at, error: s.error }])),
acks,
phones: await this.o.phones.links(),
acts: this.o.docs.recentActs(),
// A report that carries the build is large and slow to send. If it
// does not get through, the same report again would not either, and
// while that went on the edge would hear nothing at all from this
// host. So the reports after it go without the build, the first of
// them at once.
const lost = () => {
if (!apk?.data) return;
this.holdBuild("a report carrying the Android build did not get through");
this.more = true;
};
const body = new TextEncoder().encode(JSON.stringify(message));
let res: Response;
try {
res = await this.fetchImpl(`${this.o.url}/console/edge/sync`, {
Expand All @@ -233,6 +276,7 @@ export class EdgeLink {
});
} catch (error) {
if (this.last) this.log(`cannot reach the edge: ${(error as Error).message}`);
lost();
this.last = null;
return null;
}
Expand All @@ -243,6 +287,7 @@ export class EdgeLink {
// gives (it ran backwards): step past it.
if (res.status === 409 && typeof answer?.last === "number") this.lastAt = Math.max(this.lastAt, answer.last);
if (this.last) this.log(`the edge refused the report: http ${res.status} ${answer?.error ?? ""}`);
lost();
this.last = null;
return null;
}
Expand Down Expand Up @@ -271,10 +316,11 @@ export class EdgeLink {
void this.o.docs.current(WATCHED, 0);
}
const answer = await this.once();
// Something was done or is wanted: say so at once.
// Something was done, is wanted, or was left out of that report:
// say so at once.
// (The Android build is not such a thing: it waits for the next
// ordinary report, and half a megabyte is never sent in a hurry.)
if (this.acks.length > 0 || [...this.want].some((name) => name !== "apk")) return 500;
if (this.acks.length > 0 || this.more || [...this.want].some((name) => name !== "apk")) return 500;
return answer?.watching || this.o.docs.watched() ? FAST_MS : IDLE_MS;
}

Expand Down
Loading