@cohortapp/agent-sdk 2.18.30 → 2.18.31

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cohortapp/agent-sdk",
3
- "version": "2.18.30",
3
+ "version": "2.18.31",
4
4
  "description": "Cohort Agent SDK — autonomous AI colleague runtime. Deploy senior AI colleagues on dedicated Mac minis, wired to the Cohort operating surface.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -170,11 +170,60 @@ function isEmergencyStop(paths) {
170
170
  return existsSync(paths.emergencyStop);
171
171
  }
172
172
 
173
+ /**
174
+ * Normalise a log call into the one shape every writer downstream expects.
175
+ *
176
+ * PURE, and exported, because this is the whole of the defect and a defect that
177
+ * cost 28,924 lost lines on one seat deserves a test that does not have to
178
+ * start a consumer to reach it.
179
+ *
180
+ * Two conventions were live against a one-parameter `log(entry)`: the
181
+ * consumer's own `log({level, stage, …})`, and the handlers' `log("warn", msg)`
182
+ * — their documented contract, used at 14 sites. Spreading a STRING yields its
183
+ * characters, so the written line became `{"0":"w","1":"a","2":"r","3":"n"}` and
184
+ * the message was dropped by the missing second parameter.
185
+ *
186
+ * Both are accepted rather than one being declared correct. Rejecting either
187
+ * would turn a silent loss into a throw inside a logger, which is worse than
188
+ * the bug.
189
+ *
190
+ * @param {unknown} entry an object, or a level string
191
+ * @param {unknown} [msg] the message, when `entry` is a level string
192
+ * @returns {object} always a plain object, never a spread string
193
+ */
194
+ export function normaliseLogEntry(entry, msg) {
195
+ if (typeof entry === "string") {
196
+ return msg === undefined ? { level: entry } : { level: entry, msg: String(msg) };
197
+ }
198
+ if (entry && typeof entry === "object" && !Array.isArray(entry)) return entry;
199
+ // Not a string and not a plain object. Spreading it is what shredded the
200
+ // lines, so wrap it and make it loud instead.
201
+ return { level: "error", stage: "log_bad_entry", value: String(entry) };
202
+ }
203
+
173
204
  function defaultLogger(entry) {
174
205
  // The default logger ALSO writes to logs/cadence-bus/<date>.jsonl via
175
206
  // logBusEvent, but mirrors important events to stderr so launchd's
176
207
  // StandardErrorPath captures them.
177
- if (entry.level === "error" || entry.level === "warn") {
208
+ //
209
+ // A MISSING LEVEL IS ITSELF AN ALARM. This used to test `entry.level` and
210
+ // nothing else, which made the mirror blind to exactly the failure that
211
+ // destroys the level: a shredded entry has no `.level`, so 28,924 lost lines
212
+ // on one seat produced not one stderr line, and the alarm that would have
213
+ // reported the loss was disabled by the loss. That is the general shape worth
214
+ // naming — a monitor whose firing condition reads a field the failure can
215
+ // damage — and it is the same defect as a pin asserting /restarted/i, which
216
+ // both the truth and the false claim satisfied.
217
+ //
218
+ // So: mirror an unknown level too. The cost of a spurious stderr line is
219
+ // nothing; the cost of a silent one is eleven days.
220
+ const level = entry && typeof entry === "object" ? entry.level : undefined;
221
+ if (level === undefined || level === null || level === "") {
222
+ try { process.stderr.write(`[cadence-consumer] LOG ENTRY WITH NO LEVEL — ${JSON.stringify(entry)}\n`); }
223
+ catch { /* ignore */ }
224
+ return;
225
+ }
226
+ if (level === "error" || level === "warn") {
178
227
  try { process.stderr.write(`[cadence-consumer] ${JSON.stringify(entry)}\n`); }
179
228
  catch { /* ignore */ }
180
229
  }
@@ -1224,8 +1273,37 @@ export function startConsumer(opts = {}) {
1224
1273
  return { allowed: true };
1225
1274
  }
1226
1275
 
1227
- function log(entry) {
1228
- const enriched = { ts: new Date().toISOString(), ...entry };
1276
+ /**
1277
+ * TWO CONVENTIONS, ONE FUNCTION, AND THE LOSS WAS SILENT.
1278
+ *
1279
+ * This took a single object. The handlers it is handed to call it as
1280
+ * `log("warn", msg)` — their own contract, documented at the head of
1281
+ * cadence-handlers.mjs ("Handlers / guards receive ({ event, agentRoot, log })")
1282
+ * and used at 14 sites. Spreading a STRING yields its characters, so the
1283
+ * written line became `{"0":"w","1":"a","2":"r","3":"n","ts":…}` and the
1284
+ * message — the second argument — was discarded entirely by a one-parameter
1285
+ * signature.
1286
+ *
1287
+ * Measured: 28,924 shredded lines in eleven days on one seat (reconstructed as
1288
+ * 23,738 error, 3,287 warn, 1,899 info) against 323 findable well-formed error
1289
+ * lines; and 17,057 on a second seat over thirteen days with a different mix
1290
+ * (12,093 info, 3,919 warn, 19 error), which is the same defect meeting a
1291
+ * different handler population. On the charitable test — is the lost line
1292
+ * merely a duplicate of a good one? — 3 of 23,739 on the first seat and 0 of
1293
+ * 295 on the second had a well-formed error or warn line in the same second.
1294
+ * Nothing was redundant.
1295
+ *
1296
+ * NO RECOVERY IS POSSIBLE for what has already been written. Only the
1297
+ * timestamp and the level's first letters survive; the message is gone. The
1298
+ * affected windows are unrecoverable and should be recorded as such rather
1299
+ * than reconstructed.
1300
+ */
1301
+ function log(entry, msg) {
1302
+ // Accept both conventions rather than picking a winner: 14 live call sites
1303
+ // use (level, msg) and the consumer's own calls use ({level, stage, …}).
1304
+ // Rejecting either would convert a silent loss into a crash in a logger,
1305
+ // which is worse.
1306
+ const enriched = { ts: new Date().toISOString(), ...normaliseLogEntry(entry, msg) };
1229
1307
  logBusEvent(agentRoot, enriched);
1230
1308
  if (userLogger) {
1231
1309
  try { userLogger(enriched); } catch { /* never crash on logging */ }