@cohortapp/agent-sdk 2.18.29 → 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.29",
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": {
@@ -54,7 +54,7 @@
54
54
  import { appendFileSync, mkdirSync, readdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "fs";
55
55
  import { spawn } from "child_process";
56
56
  import { join } from "path";
57
- import { deliver, deliverWithRetry, replyTargetOf, canDeliverTo, canReactTo, deliverReaction } from "./deliver.mjs";
57
+ import { deliver, deliverWithRetry, replyTargetOf, canDeliverTo, canReactTo, deliverReaction, cohortReplyRoute } from "./deliver.mjs";
58
58
  import { spokeFor, spokeSince, noSendFor, NON_ANSWER_KINDS } from "../../lib/comms/receipts.mjs";
59
59
  import { resultTextFromStdout } from "./session-outcomes.mjs";
60
60
  // The ack generator funnels through `claude --print` like every other daemon
@@ -1388,6 +1388,56 @@ export function composeRefusedSend(rec, o = {}) {
1388
1388
  * @param {object} rec the obligation record; its `item.direct_reason` is read
1389
1389
  * @returns {boolean}
1390
1390
  */
1391
+ /**
1392
+ * Is this obligation's reply surface a DOCUMENT rather than a conversation?
1393
+ *
1394
+ * A doc or file comment is not a request for work. Nobody asked the seat for
1395
+ * anything by commenting on a document it can see, so there is no one waiting
1396
+ * for a notice about work that did not finish — and posting one is actively
1397
+ * harmful, because a comment fans out as an inbound to every seat the document
1398
+ * is shared with.
1399
+ *
1400
+ * MEASURED, 2026-10-01 on one shared alerting doc: 11 of its 14 comments were
1401
+ * assurance notices, alternating between two seats every ~10 minutes from
1402
+ * 02:53Z to 04:57Z, burying the three substantive comments a colleague had
1403
+ * written. The mechanism is a closed loop: seat A's notice lands as a comment,
1404
+ * the comment arrives as an inbound at seat B, B's session fails on it, B posts
1405
+ * its own notice, which arrives as an inbound at A. A human recipient of the
1406
+ * document saw every one.
1407
+ *
1408
+ * WHY THIS IS A SURFACE TEST AND NOT A REASON TEST. `isAmbientSurface` already
1409
+ * withholds the silent-success narration on a membership-proved surface, and a
1410
+ * shared document is exactly that — but only until the first notice. The
1411
+ * directedness reasons include `commenter` ("I SPOKE there"), which is NOT a
1412
+ * membership reason, so a seat that posts one notice becomes a commenter and the
1413
+ * surface flips to identity-shaped for every notice after it. The notice
1414
+ * manufactures its own audience, which is why the loop sustains for hours
1415
+ * instead of stopping. A test on the SURFACE cannot be flipped by anything the
1416
+ * seat itself does.
1417
+ *
1418
+ * Scoped to documents on purpose. `task_comment` and `decision` are NOT included:
1419
+ * a board comment can carry an assignment and a decision thread can carry a
1420
+ * request, so a person there may genuinely be waiting. Those keep the existing
1421
+ * reason-based treatment.
1422
+ *
1423
+ * @param {object} rec the obligation record
1424
+ * @returns {boolean}
1425
+ */
1426
+ const DOCUMENT_SURFACES = Object.freeze(["doc_comment", "file_comment"]);
1427
+ function isDocumentSurface(rec) {
1428
+ const item = rec && rec.item;
1429
+ if (!item) return false;
1430
+ try {
1431
+ const route = cohortReplyRoute(item);
1432
+ return DOCUMENT_SURFACES.includes(String(route && route.surface));
1433
+ } catch {
1434
+ // Unresolvable route → fall back to the reason-based treatment rather than
1435
+ // inventing silence. Failing open here costs a notice; failing closed could
1436
+ // swallow one a person is waiting on.
1437
+ return false;
1438
+ }
1439
+ }
1440
+
1391
1441
  function isAmbientSurface(rec) {
1392
1442
  const reason = String((rec && rec.item && rec.item.direct_reason) || "");
1393
1443
  if (!reason) return false; // unknown provenance → treat as a DM, narrate
@@ -2101,7 +2151,10 @@ export async function settleSession(a = {}) {
2101
2151
  // close silently to avoid. The debt is still discharged (silent is not
2102
2152
  // un-closed); only the narration is withheld. `direct_reason` reaches here
2103
2153
  // through `itemSnapshot`, and the guard fails open to narrating.
2104
- if (isAmbientSurface(rec)) {
2154
+ // `isDocumentSurface` as well as the reason test: a document stops being
2155
+ // membership-proved the moment this seat comments on it (reason `commenter`),
2156
+ // so the reason alone cannot hold the line across more than one notice.
2157
+ if (isAmbientSurface(rec) || isDocumentSurface(rec)) {
2105
2158
  closeObligation(a.key, {
2106
2159
  outcome: "answered",
2107
2160
  now,
@@ -2167,6 +2220,20 @@ export async function settleSession(a = {}) {
2167
2220
  rec.lastFailureAt = now;
2168
2221
  writeRecord(rec);
2169
2222
 
2223
+ // A DOCUMENT IS NOT A REQUEST FOR WORK, SO IT GETS NO FAILURE NOTICE.
2224
+ //
2225
+ // The silent-success branch above already withholds narration on an ambient
2226
+ // surface; this branch never asked the same question, and that asymmetry is
2227
+ // what produced the measured loop (see isDocumentSurface). The debt is closed
2228
+ // and escalated exactly as an undeliverable one is, so an operator still has
2229
+ // the trace — what is withheld is only the comment.
2230
+ if (isDocumentSurface(rec)) {
2231
+ escalate(rec, { failure, now, told: false });
2232
+ closeObligation(a.key, { outcome: "failed", now, note: `${failure.label}; document surface, notice withheld` });
2233
+ counters.bump("assurance.failure_notice_withheld_document", { service: rec.service });
2234
+ return { spoke: false, text: null, verdict: `failed:${failure.label}`, willRetry: false };
2235
+ }
2236
+
2170
2237
  // GENERATED, not stamped. `generateFailureNotice` always returns text — the
2171
2238
  // degraded path is a plainer sentence, never silence — so the guarantee the
2172
2239
  // deterministic composer bought is intact while the words are this ask's own.
@@ -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 */ }