@bli-cockpit/cli 0.2.276 → 0.2.277

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,10 +12,19 @@
12
12
  * Step 2 goes to the storage host with the signed URL ONLY: the device token is
13
13
  * sent to Tower's own routes in steps 1 and 3 and never to anyone else.
14
14
  *
15
+ * A laptop recording (`recording`, from `cockpit notes record stop`, BLI-23470)
16
+ * also names its session to step 3, so Tower answers a repeat with the note it
17
+ * already made, and keeps the object key Tower issued in the session's
18
+ * `upload.json` before the PUT. The next try after a failed step 3 (a timeout
19
+ * after the server saved, a 5xx) asks step 3 again with that key and sends no
20
+ * byte; only when Tower says the object is not there (`upload_not_found`) does
21
+ * it upload again.
22
+ *
15
23
  * Nothing here throws; every failure comes back as a closed reason label and
16
24
  * the sentence Tower (or this file) wrote for it.
17
25
  */
18
26
  import { createReadStream } from "node:fs";
27
+ import { readFile, writeFile } from "node:fs/promises";
19
28
  import { Readable } from "node:stream";
20
29
  import { writeLine } from "./cli-io.js";
21
30
  import { ask, TAG } from "./notes-door.js";
@@ -23,8 +32,61 @@ import { noteAudioMime } from "./notes-file.js";
23
32
  /** A signed upload URL lives two hours; the PUT must finish inside that. */
24
33
  const PUT_DEADLINE_MS = 2 * 60 * 60 * 1000;
25
34
  const SMALL_DEADLINE_MS = 60_000;
35
+ /** The object key a previous try of this session was issued, or null (no file, unreadable, another session's). */
36
+ async function keptObjectKey(recording) {
37
+ try {
38
+ const kept = JSON.parse(await readFile(recording.stateFile, "utf8"));
39
+ return kept.session === recording.session && typeof kept.objectKey === "string" && kept.objectKey ? kept.objectKey : null;
40
+ }
41
+ catch {
42
+ return null;
43
+ }
44
+ }
45
+ async function keepObjectKey(door, recording, objectKey, size) {
46
+ try {
47
+ await writeFile(recording.stateFile, `${JSON.stringify({ session: recording.session, objectKey, bytes: size, issuedAt: new Date().toISOString() })}\n`, { mode: 0o600 });
48
+ }
49
+ catch (error) {
50
+ // Not fatal: without it a retry sends the file again, and Tower's session check still makes one note.
51
+ writeLine(door.io.stderr, `${TAG} upload key not kept ${JSON.stringify({ session: recording.session, reason: error.code ?? "write_failed" })}`);
52
+ }
53
+ }
54
+ async function confirmSaved(door, input, objectKey) {
55
+ const saved = await ask(door, {
56
+ path: "/api/notes/audio/saved", method: "POST", label: "notes audio saved", timeoutMs: SMALL_DEADLINE_MS,
57
+ body: {
58
+ object_key: objectKey, file_name: input.fileName,
59
+ ...(input.exclude ? { exclusions: input.exclude } : {}),
60
+ ...(input.folder ? { folder_path: input.folder } : {}),
61
+ ...(input.recording ? { recording_session: input.recording.session } : {}),
62
+ },
63
+ });
64
+ if (!saved.ok)
65
+ return { ok: false, reason: saved.reason, detail: saved.detail };
66
+ return { ok: true, body: saved.body };
67
+ }
68
+ /**
69
+ * A recording whose file already reached storage on an earlier try: ask `/saved` again with the kept key. Null means
70
+ * "upload it" (nothing kept, or Tower says the object is not there); any other answer is final for this try.
71
+ */
72
+ async function resumeKept(door, input, recording) {
73
+ const kept = await keptObjectKey(recording);
74
+ if (!kept)
75
+ return null;
76
+ writeLine(door.io.stderr, `${TAG} upload resumed ${JSON.stringify({ session: recording.session, bytes: input.size, reason: "object_key_kept" })}`);
77
+ const saved = await confirmSaved(door, input, kept);
78
+ if (saved.ok || saved.reason !== "upload_not_found")
79
+ return saved;
80
+ writeLine(door.io.stderr, `${TAG} upload resumed ${JSON.stringify({ session: recording.session, reason: "kept_object_not_in_storage" })}`);
81
+ return null;
82
+ }
26
83
  export async function uploadAudioDirect(door, input) {
27
84
  const started = Date.now();
85
+ if (input.recording) {
86
+ const resumed = await resumeKept(door, input, input.recording);
87
+ if (resumed)
88
+ return resumed;
89
+ }
28
90
  const mediaType = noteAudioMime(input.fileName) ?? "";
29
91
  const target = await ask(door, {
30
92
  path: "/api/notes/audio/target", method: "POST", label: "notes audio target", timeoutMs: SMALL_DEADLINE_MS,
@@ -37,6 +99,9 @@ export async function uploadAudioDirect(door, input) {
37
99
  if (!issued.objectKey || !url) {
38
100
  return { ok: false, reason: "target_unreadable", detail: "Tower answered without an upload link." };
39
101
  }
102
+ // Kept BEFORE the PUT: a crash after the bytes landed still lets the next try ask `/saved` instead of re-sending.
103
+ if (input.recording)
104
+ await keepObjectKey(door, input.recording, issued.objectKey, input.size);
40
105
  writeLine(door.io.stderr, `Uploading ${input.fileName} (${(input.size / 1_000_000).toFixed(1)} MB) straight to storage...`);
41
106
  let put;
42
107
  try {
@@ -63,15 +128,5 @@ export async function uploadAudioDirect(door, input) {
63
128
  : `Storage refused the upload (HTTP ${put.status}). Nothing was saved; try again.` };
64
129
  }
65
130
  writeLine(door.io.stderr, `${TAG} storage put ok ${JSON.stringify({ bytes: input.size, elapsed_ms: Date.now() - started })}`);
66
- const saved = await ask(door, {
67
- path: "/api/notes/audio/saved", method: "POST", label: "notes audio saved", timeoutMs: SMALL_DEADLINE_MS,
68
- body: {
69
- object_key: issued.objectKey, file_name: input.fileName,
70
- ...(input.exclude ? { exclusions: input.exclude } : {}),
71
- ...(input.folder ? { folder_path: input.folder } : {}),
72
- },
73
- });
74
- if (!saved.ok)
75
- return { ok: false, reason: saved.reason, detail: saved.detail };
76
- return { ok: true, body: saved.body };
131
+ return await confirmSaved(door, input, issued.objectKey);
77
132
  }
@@ -22,7 +22,9 @@
22
22
  * `MAX_RECORDING_SECONDS` of audio (8 hours, about 922 MB of 16 kHz mono
23
23
  * 16-bit, under the 1,000,000,000-byte storage limit), writing `ended.txt`
24
24
  * (`max_duration_reached`) and `done`. `status` then shows the session as
25
- * stopped and waiting for upload.
25
+ * stopped and waiting for upload. BLI-23470: the same stop fires after 8 hours
26
+ * on the wall clock, or once the file nears the storage limit
27
+ * (`max_size_reached`), whichever comes first (`RECORDER_STOP_RULE`).
26
28
  *
27
29
  * Everything in a session lives in one directory under the collector's state
28
30
  * dir. Nothing here touches the network.
@@ -40,8 +42,35 @@ export const MAX_RECORDING_SECONDS = 28_800;
40
42
  export const MAX_RECORDING_BYTES = MAX_RECORDING_SECONDS * RECORDING_SAMPLE_RATE * 2;
41
43
  /** The marker a recorder writes when it ended by itself, holding one closed reason label. */
42
44
  export const ENDED_MARKER = "ended.txt";
43
- /** The only reason a recorder ends by itself today. */
45
+ /** Why a recorder ends by itself: the 8-hour cap (by audio or by the clock). */
44
46
  export const MAX_DURATION_REASON = "max_duration_reached";
47
+ /** BLI-23470: or its file came near the storage limit first (only if the audio is not the format asked for). */
48
+ export const MAX_SIZE_REASON = "max_size_reached";
49
+ /** The storage bucket's per-object limit (`AUDIO_DIRECT_MAX_BYTES` on the server). */
50
+ export const AUDIO_UPLOAD_LIMIT_BYTES = 1_000_000_000;
51
+ /**
52
+ * BLI-23470: the macOS recorder stops once `audio.wav` reaches this, 1 MB under the limit: room for the WAV header
53
+ * and the chunks AVAudioRecorder adds, and for what it writes in one 0.2 s poll even at a far higher rate.
54
+ */
55
+ export const RECORDER_STOP_BYTES = AUDIO_UPLOAD_LIMIT_BYTES - 1_000_000;
56
+ /** BLI-23470: a recorder whose `audio.wav` has not changed for this long is not recording (both platforms write it continuously). */
57
+ export const RECORDER_SILENT_AFTER_MS = 60_000;
58
+ /** Asked again after this pause before calling it silent: a laptop just back from sleep writes again within a second. */
59
+ export const RECORDER_SILENT_RECHECK_MS = 3_000;
60
+ /** A session older than the 8-hour cap plus this has no recorder of ours behind it, whatever its pid says. */
61
+ export const RECORDER_PAST_CAP_MARGIN_SECONDS = 15 * 60;
62
+ /** BLI-23470: the session file keeping the object key Tower issued for its upload (see `notes-audio-direct.ts`). */
63
+ export const UPLOAD_STATE_FILE = "upload.json";
64
+ /**
65
+ * BLI-23470: when the macOS recorder stops by itself, as JavaScript it runs (and the tests run, with a fake clock).
66
+ * Whichever comes first: 8 hours of audio in the file, 8 hours on the wall clock since it started (the old tick
67
+ * count drifted), or the file nearing the storage limit. Returns the closed reason label, or null to keep recording.
68
+ */
69
+ export const RECORDER_STOP_RULE = `function capReached(startedMs, nowMs, audioSeconds, fileBytes) {
70
+ if (audioSeconds >= ${MAX_RECORDING_SECONDS} || nowMs - startedMs >= ${MAX_RECORDING_SECONDS * 1000}) return '${MAX_DURATION_REASON}';
71
+ if (fileBytes >= ${RECORDER_STOP_BYTES}) return '${MAX_SIZE_REASON}';
72
+ return null;
73
+ }`;
45
74
  /** The JXA recorder. `argv[0]` is the session directory. Reasons it writes to `failed.txt`: mic_denied, recorder_failed. */
46
75
  export const RECORDER_SCRIPT = `ObjC.import('AVFoundation');
47
76
  ObjC.import('Foundation');
@@ -51,8 +80,19 @@ function writeText(file, text) {
51
80
  }
52
81
  function exists(file) { return $.NSFileManager.defaultManager.fileExistsAtPath(file); }
53
82
  function tick(seconds) { $.NSRunLoop.currentRunLoop.runUntilDate($.NSDate.dateWithTimeIntervalSinceNow(seconds)); }
83
+ function fileBytes(file) {
84
+ // Never throws: a size it cannot read is 0, and the audio and clock checks still stop the recorder.
85
+ try {
86
+ var attrs = $.NSFileManager.defaultManager.attributesOfItemAtPathError(file, null);
87
+ if (!attrs || attrs.isNil()) return 0;
88
+ var size = ObjC.unwrap(attrs.objectForKey('NSFileSize'));
89
+ return typeof size === 'number' ? size : 0;
90
+ } catch (e) { return 0; }
91
+ }
92
+ ${RECORDER_STOP_RULE}
54
93
  function run(argv) {
55
94
  var dir = argv[0];
95
+ var startedMs = Date.now();
56
96
  var auth = $.AVCaptureDevice.authorizationStatusForMediaType('soun');
57
97
  if (auth === 1 || auth === 2) { writeText(dir + '/failed.txt', 'mic_denied'); $.exit(3); }
58
98
  var settings = $.NSDictionary.dictionaryWithObjectsForKeys(
@@ -69,10 +109,11 @@ function run(argv) {
69
109
  if (now === 1 || now === 2) { recorder.stop; writeText(dir + '/failed.txt', 'mic_denied'); $.exit(3); }
70
110
  if (!announced && now === 3) { announced = true; writeText(dir + '/ready', 'ok'); }
71
111
  if (!announced && waited > 120) { recorder.stop; writeText(dir + '/failed.txt', 'mic_permission_unanswered'); $.exit(5); }
72
- // currentTime is the audio in the file, so it bounds the file size; the tick count is a backstop if it ever reads wrong.
73
- if (recorder.currentTime >= ${MAX_RECORDING_SECONDS} || waited >= ${MAX_RECORDING_SECONDS + 600}) {
112
+ // currentTime is the audio in the file; the wall clock and the file size are the backstops (BLI-23470).
113
+ var capped = capReached(startedMs, Date.now(), recorder.currentTime, fileBytes(dir + '/audio.wav'));
114
+ if (capped) {
74
115
  recorder.stop;
75
- writeText(dir + '/${ENDED_MARKER}', '${MAX_DURATION_REASON}');
116
+ writeText(dir + '/${ENDED_MARKER}', capped);
76
117
  writeText(dir + '/done', 'ok');
77
118
  $.exit(0);
78
119
  }
@@ -130,6 +171,15 @@ export async function fileSize(file) {
130
171
  return null;
131
172
  }
132
173
  }
174
+ /** How long ago `file` last changed, by the real clock, or null when it does not exist (BLI-23470). */
175
+ export async function fileQuietMs(file) {
176
+ try {
177
+ return Math.max(0, Date.now() - (await stat(file)).mtimeMs);
178
+ }
179
+ catch {
180
+ return null;
181
+ }
182
+ }
133
183
  export async function fileExists(file) {
134
184
  return (await fileSize(file)) !== null;
135
185
  }
@@ -22,6 +22,14 @@
22
22
  * refuses by name. Both platforms share one session directory, marker files,
23
23
  * JSON answers and WAV format, so the upload and recovery below are one code path.
24
24
  *
25
+ * BLI-23470: a session whose pid answers but whose recorder is plainly gone
26
+ * (silent file, past the cap, never opened the microphone) is crashed, so it
27
+ * uploads and `start` works again; an upload names its session and keeps the
28
+ * object key Tower issued (`upload.json`), so a retry makes no second note and
29
+ * sends no byte twice; failed uploads are counted on the session and `status`
30
+ * reports them (`upload_failures`, `last_attempt_age_seconds`) for the app's
31
+ * backoff.
32
+ *
25
33
  * Capture is `notes-record-capture.ts` (macOS) and
26
34
  * `notes-record-capture-win32.ts` (Windows); the header repair is `notes-record-wav.ts`.
27
35
  */
@@ -29,7 +37,7 @@ import { link, rm } from "node:fs/promises";
29
37
  import path from "node:path";
30
38
  import { setTimeout as delay } from "node:timers/promises";
31
39
  import { writeLine } from "./cli-io.js";
32
- import { audioPath, fileExists, fileSize, launchRecorder, listSessions, newSessionId, prepareSessionDir, processAlive, readEndedReason, readReason, recordingsRoot, requestStop, sessionDir, uploadPath, writeSession, MAX_RECORDING_SECONDS, RECORDING_SAMPLE_RATE, } from "./notes-record-capture.js";
40
+ import { audioPath, fileExists, fileQuietMs, fileSize, launchRecorder, listSessions, newSessionId, prepareSessionDir, processAlive, readEndedReason, readReason, recordingsRoot, requestStop, sessionDir, uploadPath, writeSession, MAX_RECORDING_SECONDS, RECORDER_PAST_CAP_MARGIN_SECONDS, RECORDER_SILENT_AFTER_MS, RECORDER_SILENT_RECHECK_MS, RECORDING_SAMPLE_RATE, UPLOAD_STATE_FILE, } from "./notes-record-capture.js";
33
41
  import { repairWavHeader } from "./notes-record-wav.js";
34
42
  const TAG = "[notes record]";
35
43
  const READY_DEADLINE_MS = 150_000;
@@ -47,14 +55,19 @@ export function defaultRecordDeps(io, upload) {
47
55
  }
48
56
  catch { /* already gone */ } },
49
57
  upload,
58
+ audioQuietMs: fileQuietMs,
50
59
  };
51
60
  }
52
61
  /**
53
62
  * Where a session stands. Order matters: an end the CLI wrote, then an end the recorder wrote (`ended.txt`
54
63
  * at the 8-hour cap, or `done`), then a live process. A marker outranks the pid, so a pid the system reused
55
64
  * never pins a finished recorder as "recording". `endedReason` is the recorder's closed label, if it wrote one.
65
+ *
66
+ * BLI-23470: a live pid is not proof either. With no marker, a pid the system reused (or one owned by another
67
+ * user, EPERM) would pin a crashed session as "recording" forever: never uploaded, and `start` refused. So a
68
+ * process that looks alive still counts as crashed when its recorder is plainly not recording (`recorderGone`).
56
69
  */
57
- async function viewOf(root, session, deps) {
70
+ async function viewOf(root, session, deps, io) {
58
71
  if (session.endedAt)
59
72
  return { state: session.endedBy === "cancel" ? "cancelled" : "stopped", endedReason: null };
60
73
  const ended = await readEndedReason(root, session.id);
@@ -62,11 +75,48 @@ async function viewOf(root, session, deps) {
62
75
  return { state: "stopped", endedReason: ended };
63
76
  if (await fileExists(path.join(sessionDir(root, session.id), "done")))
64
77
  return { state: "stopped", endedReason: null };
65
- return { state: deps.alive(session.pid) ? "recording" : "crashed", endedReason: null };
78
+ if (!deps.alive(session.pid))
79
+ return { state: "crashed", endedReason: null };
80
+ const gone = await recorderGone(root, session, deps, io);
81
+ return gone ? { state: "crashed", endedReason: gone } : { state: "recording", endedReason: null };
82
+ }
83
+ /**
84
+ * Why a session whose pid answers is not really recording, or null when it is. Judged on the session's own files:
85
+ * - `recorder_past_cap`: started longer ago than the 8-hour cap plus a margin; no recorder of ours runs that long.
86
+ * - `recorder_never_ready`: the microphone never opened (no `ready`) and `start` gave up waiting long ago.
87
+ * - `recorder_silent`: the microphone opened, but `audio.wav` has not changed for `RECORDER_SILENT_AFTER_MS`.
88
+ * Asked twice, `RECORDER_SILENT_RECHECK_MS` apart: right after a laptop wakes from sleep the file is old
89
+ * but the recorder writes again within a second, and that recording must not be cut off.
90
+ */
91
+ async function recorderGone(root, session, deps, io) {
92
+ const ageMs = deps.now().getTime() - Date.parse(session.startedAt);
93
+ let reason = null;
94
+ if (ageMs > (MAX_RECORDING_SECONDS + RECORDER_PAST_CAP_MARGIN_SECONDS) * 1000)
95
+ reason = "recorder_past_cap";
96
+ else if (!(await fileExists(path.join(sessionDir(root, session.id), "ready")))) {
97
+ if (ageMs > READY_DEADLINE_MS + RECORDER_SILENT_AFTER_MS)
98
+ reason = "recorder_never_ready";
99
+ }
100
+ else {
101
+ const audio = audioPath(root, session.id);
102
+ const quiet = await deps.audioQuietMs(audio);
103
+ if (quiet !== null && quiet > RECORDER_SILENT_AFTER_MS) {
104
+ const before = await fileSize(audio);
105
+ await deps.sleep(RECORDER_SILENT_RECHECK_MS);
106
+ const again = await deps.audioQuietMs(audio);
107
+ // The size too: a file system that is slow to move the modified time still shows the bytes growing.
108
+ if (again !== null && again > RECORDER_SILENT_AFTER_MS && (await fileSize(audio)) === before)
109
+ reason = "recorder_silent";
110
+ }
111
+ }
112
+ if (reason) {
113
+ writeLine(io.stderr, `${TAG} recorder gone ${JSON.stringify({ session: session.id, pid: session.pid, reason, age_s: Math.round(ageMs / 1000) })}`);
114
+ }
115
+ return reason;
66
116
  }
67
- async function findRecording(root, sessions, deps) {
117
+ async function findRecording(root, sessions, deps, io) {
68
118
  for (const session of sessions) {
69
- if ((await viewOf(root, session, deps)).state === "recording")
119
+ if ((await viewOf(root, session, deps, io)).state === "recording")
70
120
  return session;
71
121
  }
72
122
  return undefined;
@@ -120,7 +170,7 @@ export async function runNotesRecord(command, io, deps) {
120
170
  }
121
171
  async function startRecording(command, io, deps, root) {
122
172
  const sessions = await listSessions(root);
123
- const running = await findRecording(root, sessions, deps);
173
+ const running = await findRecording(root, sessions, deps, io);
124
174
  if (running) {
125
175
  return refuse(io, command, "already_recording", `A recording is already running (started ${running.startedAt}). Run \`cockpit notes record stop\` to finish it or \`cockpit notes record cancel\` to keep the file and stop.`);
126
176
  }
@@ -162,7 +212,7 @@ async function closeRecorder(root, session, deps) {
162
212
  deps.kill(session.pid);
163
213
  }
164
214
  async function cancelRecording(command, io, deps, root) {
165
- const running = await findRecording(root, await listSessions(root), deps);
215
+ const running = await findRecording(root, await listSessions(root), deps, io);
166
216
  if (!running)
167
217
  return refuse(io, command, "not_recording", "No recording is running.");
168
218
  await closeRecorder(root, running, deps);
@@ -213,7 +263,7 @@ async function stopRecording(command, io, deps, root) {
213
263
  const sessions = await listSessions(root);
214
264
  const target = command.sessionId
215
265
  ? sessions.find((s) => s.id === command.sessionId)
216
- : await findRecording(root, sessions, deps);
266
+ : await findRecording(root, sessions, deps, io);
217
267
  if (!target) {
218
268
  const waiting = sessions.filter((s) => !s.submittedAt && !s.nothingToUpload);
219
269
  const hint = waiting.length ? ` Waiting for upload: ${waiting.map((s) => s.id).join(", ")} (\`cockpit notes record stop <id>\`).` : "";
@@ -223,12 +273,21 @@ async function stopRecording(command, io, deps, root) {
223
273
  return refuse(io, command, "already_submitted", `Recording ${target.id} was already submitted at ${target.submittedAt}.`);
224
274
  }
225
275
  let session = target;
226
- const view = await viewOf(root, target, deps);
276
+ const view = await viewOf(root, target, deps, io);
227
277
  if (view.state === "recording") {
228
278
  await closeRecorder(root, target, deps);
229
279
  session = { ...target, endedAt: deps.now().toISOString(), endedBy: "stop" };
230
280
  await writeSession(root, session);
231
281
  }
282
+ else if (view.endedReason?.startsWith("recorder_")) {
283
+ // BLI-23470: its pid answers but the recorder is gone (`recorderGone`). Ask a recorder that may still be there to
284
+ // close its file, but never kill: that pid may belong to somebody else's process now. Pinned as stopped.
285
+ await requestStop(root, target.id);
286
+ for (let waited = 0; waited < STOP_DEADLINE_MS && deps.alive(target.pid); waited += POLL_MS)
287
+ await deps.sleep(POLL_MS);
288
+ session = { ...target, endedAt: deps.now().toISOString(), endedBy: "stop" };
289
+ await writeSession(root, session);
290
+ }
232
291
  const audio = audioPath(root, session.id);
233
292
  const reason = await unsendable(root, session, deps);
234
293
  if (reason) {
@@ -240,9 +299,15 @@ async function stopRecording(command, io, deps, root) {
240
299
  writeLine(io.stderr, `${TAG} uploading original ${JSON.stringify({ session: session.id, bytes, seconds: Math.round(audioSeconds(bytes)), ended_reason: view.endedReason })}`);
241
300
  if (!command.json)
242
301
  writeLine(io.stderr, `Uploading the whole recording (${clock(audioSeconds(bytes))}, ${megabytes(bytes)})...`);
243
- const code = await deps.upload({ ...command, action: "upload", paths: [sent] }, sent);
302
+ // BLI-23470: the upload names this session, and keeps the object key Tower issued beside the audio, so a retry after
303
+ // `/saved` failed asks Tower again instead of sending the whole file twice (and Tower answers with the same note).
304
+ const recordingUpload = { session: session.id, stateFile: path.join(sessionDir(root, session.id), UPLOAD_STATE_FILE) };
305
+ const code = await deps.upload({ ...command, action: "upload", paths: [sent], recordingUpload }, sent);
244
306
  if (code !== 0) {
245
- writeLine(io.stderr, `${TAG} upload failed ${JSON.stringify({ session: session.id, exit: code })}`);
307
+ // Counted on the session, so the app's retries back off across restarts (5 min doubling to 1 h, `backoff.rs`).
308
+ const failures = (session.uploadFailures ?? 0) + 1;
309
+ await writeSession(root, { ...session, uploadFailures: failures, lastUploadAttemptAt: deps.now().toISOString() });
310
+ writeLine(io.stderr, `${TAG} upload failed ${JSON.stringify({ session: session.id, exit: code, upload_failures: failures })}`);
246
311
  writeLine(io.stderr, `The upload did not finish. The recording is kept; the BLI Notetaker app tries again by itself, or run \`cockpit notes record stop ${session.id}\`.`);
247
312
  return code;
248
313
  }
@@ -261,14 +326,19 @@ async function recordingStatus(command, io, deps, root) {
261
326
  const sessions = (await listSessions(root)).filter((s) => !s.submittedAt && !s.nothingToUpload);
262
327
  const rows = [];
263
328
  for (const session of sessions) {
264
- const { state, endedReason } = await viewOf(root, session, deps);
329
+ const { state, endedReason } = await viewOf(root, session, deps, io);
265
330
  const bytes = await fileSize(audioPath(root, session.id));
266
331
  if (state !== "recording" && bytes === null)
267
332
  continue;
268
- const elapsed = state === "recording" ? (deps.now().getTime() - Date.parse(session.startedAt)) / 1000 : audioSeconds(bytes);
333
+ const now = deps.now().getTime();
334
+ const elapsed = state === "recording" ? (now - Date.parse(session.startedAt)) / 1000 : audioSeconds(bytes);
335
+ const lastAttempt = session.lastUploadAttemptAt ? Date.parse(session.lastUploadAttemptAt) : Number.NaN;
269
336
  rows.push({
270
337
  session: session.id, state, needs_upload: state !== "recording", ended_reason: endedReason,
271
338
  started_at: session.startedAt, elapsed_seconds: Math.round(elapsed), bytes, file: audioPath(root, session.id),
339
+ // BLI-23470: what the app's backoff reads. Seconds since the last failed upload, so no clock is compared across processes.
340
+ upload_failures: session.uploadFailures ?? 0,
341
+ last_attempt_age_seconds: Number.isFinite(lastAttempt) ? Math.max(0, Math.round((now - lastAttempt) / 1000)) : null,
272
342
  });
273
343
  }
274
344
  writeLine(io.stderr, `${TAG} status ${JSON.stringify({ sessions: rows.length, needs_upload: rows.filter((row) => row.needs_upload).length })}`);
@@ -80,9 +80,12 @@ async function uploadOneNote(command, door, filePath) {
80
80
  * upload route.
81
81
  */
82
82
  async function sendNoteFile(command, door, filePath, read) {
83
- if (read.direct) {
83
+ // BLI-23470: a laptop recording always goes direct, whatever its size, so it names its session to `/saved` and a
84
+ // retry never makes a second note (the small-file route has no session check).
85
+ if (read.direct || (command.recordingUpload && noteAudioMime(read.fileName))) {
84
86
  const direct = await uploadAudioDirect(door, {
85
87
  filePath, fileName: read.fileName, size: read.size, exclude: command.exclude, folder: command.folder,
88
+ ...(command.recordingUpload ? { recording: command.recordingUpload } : {}),
86
89
  });
87
90
  return direct.ok ? { ok: true, body: direct.body } : { ok: false, reason: direct.reason, detail: direct.detail };
88
91
  }
@@ -21,7 +21,7 @@ export async function runCockpitCli(argv, io) {
21
21
  }
22
22
 
23
23
  if (command === "--version" || command === "-V" || command === "version") {
24
- writeLine(io?.stdout ?? process.stdout, "0.2.276");
24
+ writeLine(io?.stdout ?? process.stdout, "0.2.277");
25
25
  return 0;
26
26
  }
27
27
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bli-cockpit/cli",
3
- "version": "0.2.276",
3
+ "version": "0.2.277",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "bin": {