@cohortapp/agent-sdk 2.5.0 → 2.5.1
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/lib/org/inbound/hydrate.mjs +107 -15
- package/lib/org/inbound/hydrate.test.mjs +127 -0
- package/package.json +1 -1
|
@@ -51,16 +51,81 @@ export function clip(text, max = 4000) {
|
|
|
51
51
|
return `${sp > max * 0.8 ? cut.slice(0, sp) : cut}…`;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* How many of the most recent turns must survive a binding character budget.
|
|
56
|
+
* Without a per-turn cap one long message spends the whole allowance and the
|
|
57
|
+
* turns around it vanish.
|
|
58
|
+
*/
|
|
59
|
+
const MIN_TURNS_VISIBLE = 4;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Who said it. hq denormalises `authorName` onto history rows, and when it is
|
|
63
|
+
* there it wins. When it is NOT — an older server, or any surface that never
|
|
64
|
+
* denormalised one — the fallback is a bare cuid, and a transcript of
|
|
65
|
+
* "cmqh0t9…: fix this / cmqh0tc…: which one?" loses the single distinction that
|
|
66
|
+
* matters most: which turns are MINE. An agent that cannot tell its own words
|
|
67
|
+
* from the sender's re-answers itself and misattributes its own statements back
|
|
68
|
+
* to them. `facts.me` is always known, so that distinction never has to be lost.
|
|
69
|
+
*/
|
|
70
|
+
export function speakerFor(m, facts) {
|
|
71
|
+
const name = s(m && m.authorName);
|
|
72
|
+
if (name) return name;
|
|
73
|
+
const id = s(m && (m.authorId || m.author));
|
|
74
|
+
if (facts && facts.me && id === s(facts.me)) {
|
|
75
|
+
return (Array.isArray(facts.myNames) && facts.myNames[0]) || "me";
|
|
76
|
+
}
|
|
77
|
+
return id || "someone";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Render a comment list into a compact prior-thread context block, oldest first.
|
|
82
|
+
*
|
|
83
|
+
* ORDER IS NOT A DETAIL HERE. The sources disagree: every comment read
|
|
84
|
+
* (`board.taskComments`, `file.listComments`, `files.comments`,
|
|
85
|
+
* `decision.listComments`) returns ASCENDING, but `messaging.history` returns
|
|
86
|
+
* DESCENDING — hq orders `createdAt desc` unless the caller pages forward with
|
|
87
|
+
* an `after` cursor, and the inbound pull never does. So the bare `slice(-limit)`
|
|
88
|
+
* this used to do took the OLDEST rows off a message page and then printed them
|
|
89
|
+
* backwards: the wrong window, in the wrong direction, silently.
|
|
90
|
+
*
|
|
91
|
+
* Sorting on the timestamp makes the tail mean "the most recent turns" for every
|
|
92
|
+
* source. The sort is stable and the comparator declines to order rows with no
|
|
93
|
+
* parseable timestamp, so a source that carries none keeps its arrival order.
|
|
94
|
+
*/
|
|
55
95
|
export function renderThread(comments, { limit = 8, max = 2000 } = {}) {
|
|
56
|
-
const
|
|
96
|
+
const all = (Array.isArray(comments) ? comments : []).slice();
|
|
97
|
+
all.sort((a, b) => {
|
|
98
|
+
const ta = Date.parse(s(a && (a.createdAt || a.at)));
|
|
99
|
+
const tb = Date.parse(s(b && (b.createdAt || b.at)));
|
|
100
|
+
if (!Number.isFinite(ta) || !Number.isFinite(tb)) return 0;
|
|
101
|
+
return ta - tb;
|
|
102
|
+
});
|
|
103
|
+
const rows = all.slice(-limit);
|
|
57
104
|
if (rows.length === 0) return null;
|
|
105
|
+
|
|
106
|
+
// Spend the budget from the NEWEST turn backwards. Clipping the joined string
|
|
107
|
+
// — which is what this did — keeps the oldest turns and drops the newest, the
|
|
108
|
+
// exact inverse of what context is for: the turns nearest the message being
|
|
109
|
+
// answered are the ones that explain it. Caught on the live CEO DM, where one
|
|
110
|
+
// long reply of mine filled the 2000 chars and displaced the four newer turns,
|
|
111
|
+
// including the question under discussion. The per-turn cap is the other half:
|
|
112
|
+
// without it a single long message starves every turn around it.
|
|
113
|
+
const perTurn = Math.max(200, Math.floor(max / Math.min(rows.length, MIN_TURNS_VISIBLE)));
|
|
58
114
|
const lines = rows.map((c) => {
|
|
59
115
|
const who = s(c.authorName || c.authorId || c.author || "someone");
|
|
60
|
-
const body = s(c.body || c.text).replace(/\s+/g, " ").trim();
|
|
116
|
+
const body = clip(s(c.body || c.text).replace(/\s+/g, " ").trim(), perTurn);
|
|
61
117
|
return `${who}: ${body}`;
|
|
62
118
|
});
|
|
63
|
-
|
|
119
|
+
|
|
120
|
+
const kept = [];
|
|
121
|
+
let used = 0;
|
|
122
|
+
for (let i = lines.length - 1; i >= 0; i -= 1) {
|
|
123
|
+
const cost = lines[i].length + (kept.length ? 1 : 0); // +1 for the joining newline
|
|
124
|
+
if (kept.length > 0 && used + cost > max) break;
|
|
125
|
+
kept.unshift(lines[i]);
|
|
126
|
+
used += cost;
|
|
127
|
+
}
|
|
128
|
+
return kept.join("\n") || null;
|
|
64
129
|
}
|
|
65
130
|
|
|
66
131
|
/**
|
|
@@ -146,17 +211,24 @@ function hydrateMessage(c, facts) {
|
|
|
146
211
|
|
|
147
212
|
const chan = facts.channels instanceof Map ? facts.channels.get(s(c.ids.channelId)) : null;
|
|
148
213
|
const root = s(msg.threadRootId || msg.thread_root_id);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
)
|
|
159
|
-
|
|
214
|
+
|
|
215
|
+
// THE CONVERSATION SO FAR. For a thread reply that is the thread; for a
|
|
216
|
+
// TOP-LEVEL message it is the channel itself — which is the whole of a DM.
|
|
217
|
+
// This used to be gated on `root`, so a DM (no thread root, by definition)
|
|
218
|
+
// arrived with no context at all and the agent answered "fix this" without
|
|
219
|
+
// ever seeing what "this" referred to. The page is already in hand from the
|
|
220
|
+
// ACL'd `messaging.history` read `pageChannels` made, so re-joining it here
|
|
221
|
+
// costs no RPC and widens nothing: it is the same read, same aperture.
|
|
222
|
+
const scope = root
|
|
223
|
+
? page.messages.filter((m) => s(m.threadRootId || m.thread_root_id) === root || s(m.id) === root)
|
|
224
|
+
: page.messages;
|
|
225
|
+
const threadContext = renderThread(
|
|
226
|
+
priorTo(scope, msg, c.ids.messageId).map((m) => ({
|
|
227
|
+
authorName: speakerFor(m, facts),
|
|
228
|
+
body: m.body ?? m.text,
|
|
229
|
+
createdAt: m.createdAt,
|
|
230
|
+
})),
|
|
231
|
+
);
|
|
160
232
|
|
|
161
233
|
return {
|
|
162
234
|
ok: true,
|
|
@@ -467,6 +539,26 @@ async function hydrateEmail(c, facts, io, cache) {
|
|
|
467
539
|
// Small helpers
|
|
468
540
|
// ---------------------------------------------------------------------------
|
|
469
541
|
|
|
542
|
+
/**
|
|
543
|
+
* The messages that came BEFORE the trigger — never the ones after it.
|
|
544
|
+
*
|
|
545
|
+
* A history page is a window, not a prefix: when the pull is running behind, or
|
|
546
|
+
* replaying after a crash, the page can hold turns that were said AFTER the
|
|
547
|
+
* message being hydrated. Handing those to the agent as "prior context" would
|
|
548
|
+
* have it answer a question using an answer it has not given yet. When a row
|
|
549
|
+
* carries no parseable timestamp we keep it rather than guess — dropping real
|
|
550
|
+
* conversation is the failure mode being fixed here.
|
|
551
|
+
*/
|
|
552
|
+
function priorTo(messages, trigger, triggerId) {
|
|
553
|
+
const at = Date.parse(s(trigger && trigger.createdAt));
|
|
554
|
+
return (Array.isArray(messages) ? messages : []).filter((m) => {
|
|
555
|
+
if (s(m.id) === s(triggerId)) return false;
|
|
556
|
+
if (!Number.isFinite(at)) return true;
|
|
557
|
+
const t = Date.parse(s(m.createdAt));
|
|
558
|
+
return !Number.isFinite(t) || t <= at;
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
|
|
470
562
|
/** Memoise an async read for the lifetime of one pull. */
|
|
471
563
|
async function cached(cache, key, fn) {
|
|
472
564
|
if (cache.has(key)) return cache.get(key);
|
|
@@ -118,6 +118,64 @@ test("a threaded message carries prior-thread context", async () => {
|
|
|
118
118
|
assert.doesNotMatch(h.threadContext, /one more thing/, "the trigger itself is not its own context");
|
|
119
119
|
});
|
|
120
120
|
|
|
121
|
+
// A DM has no thread root — the CHANNEL is the conversation. Before this, the
|
|
122
|
+
// `if (root)` guard meant a top-level message carried no context at all, so the
|
|
123
|
+
// agent answered "fix this" with no idea what "this" referred to. The page is
|
|
124
|
+
// already in hand from the ACL'd `messaging.history` read, so this costs no RPC.
|
|
125
|
+
test("a top-level DM carries the preceding conversation as context", async () => {
|
|
126
|
+
// Newest-first, exactly as hq's messaging.history returns it.
|
|
127
|
+
const msgs = [
|
|
128
|
+
{ id: "m3", authorId: THEM, authorName: "Casey", body: "fix this then", createdAt: "2026-08-11T15:33:54.000Z" },
|
|
129
|
+
{ id: "m2", authorId: ME, authorName: "Alex", body: "16 min pickup on the morning batch", createdAt: "2026-08-11T15:32:00.000Z" },
|
|
130
|
+
{ id: "m1", authorId: THEM, authorName: "Casey", body: "what is the reply latency here", createdAt: "2026-08-11T15:31:00.000Z" },
|
|
131
|
+
];
|
|
132
|
+
const f = facts();
|
|
133
|
+
f.channels.set("C-dm", { id: "C-dm", kind: "DM", name: "dm-h001-a016" });
|
|
134
|
+
f.channelPages.set("C-dm", { byId: new Map(msgs.map((m) => [m.id, m])), messages: msgs });
|
|
135
|
+
const c = classifyEvent(ev({ family: "messaging", kind: "send", entity_id: "m3", payload: { actor: THEM, channelId: "C-dm", channelKind: "DM" } }));
|
|
136
|
+
const io = fakeIo({});
|
|
137
|
+
const h = await hydrate({ candidate: c, verdict: { surface: "dm", reason: "dm" }, facts: f, io });
|
|
138
|
+
|
|
139
|
+
assert.equal(h.ok, true);
|
|
140
|
+
assert.equal(io.calls.length, 0, "the page was already in hand — context must cost no extra RPC");
|
|
141
|
+
assert.ok(h.threadContext, "a DM with prior turns must carry them");
|
|
142
|
+
assert.match(h.threadContext, /Casey: what is the reply latency here/);
|
|
143
|
+
assert.match(h.threadContext, /Alex: 16 min pickup/);
|
|
144
|
+
assert.doesNotMatch(h.threadContext, /fix this then/, "the trigger itself is not its own context");
|
|
145
|
+
assert.ok(
|
|
146
|
+
h.threadContext.indexOf("what is the reply latency") < h.threadContext.indexOf("16 min pickup"),
|
|
147
|
+
"oldest first — the conversation must read forwards",
|
|
148
|
+
);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("a first-ever message in a channel has no context, not an empty block", async () => {
|
|
152
|
+
const msgs = [{ id: "m1", authorId: THEM, authorName: "Casey", body: "hello", createdAt: "2026-08-11T15:31:00.000Z" }];
|
|
153
|
+
const f = facts();
|
|
154
|
+
f.channels.set("C-dm", { id: "C-dm", kind: "DM" });
|
|
155
|
+
f.channelPages.set("C-dm", { byId: new Map(msgs.map((m) => [m.id, m])), messages: msgs });
|
|
156
|
+
const c = classifyEvent(ev({ family: "messaging", kind: "send", entity_id: "m1", payload: { actor: THEM, channelId: "C-dm", channelKind: "DM" } }));
|
|
157
|
+
const h = await hydrate({ candidate: c, verdict: { surface: "dm", reason: "dm" }, facts: f, io: fakeIo({}) });
|
|
158
|
+
|
|
159
|
+
assert.equal(h.ok, true);
|
|
160
|
+
assert.equal(h.threadContext, null);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("messages AFTER the trigger are never passed off as prior context", async () => {
|
|
164
|
+
const msgs = [
|
|
165
|
+
{ id: "m3", authorId: THEM, authorName: "Casey", body: "later message", createdAt: "2026-08-11T16:00:00.000Z" },
|
|
166
|
+
{ id: "m2", authorId: THEM, authorName: "Casey", body: "the trigger", createdAt: "2026-08-11T15:00:00.000Z" },
|
|
167
|
+
{ id: "m1", authorId: ME, authorName: "Alex", body: "earlier message", createdAt: "2026-08-11T14:00:00.000Z" },
|
|
168
|
+
];
|
|
169
|
+
const f = facts();
|
|
170
|
+
f.channels.set("C-dm", { id: "C-dm", kind: "DM" });
|
|
171
|
+
f.channelPages.set("C-dm", { byId: new Map(msgs.map((m) => [m.id, m])), messages: msgs });
|
|
172
|
+
const c = classifyEvent(ev({ family: "messaging", kind: "send", entity_id: "m2", payload: { actor: THEM, channelId: "C-dm", channelKind: "DM" } }));
|
|
173
|
+
const h = await hydrate({ candidate: c, verdict: { surface: "dm", reason: "dm" }, facts: f, io: fakeIo({}) });
|
|
174
|
+
|
|
175
|
+
assert.match(h.threadContext, /earlier message/);
|
|
176
|
+
assert.doesNotMatch(h.threadContext, /later message/);
|
|
177
|
+
});
|
|
178
|
+
|
|
121
179
|
// ---------------------------------------------------------------------------
|
|
122
180
|
// board
|
|
123
181
|
// ---------------------------------------------------------------------------
|
|
@@ -318,6 +376,75 @@ test("clip and renderThread are bounded", () => {
|
|
|
318
376
|
assert.match(renderThread([{ authorName: "A", body: "hi" }]), /A: hi/);
|
|
319
377
|
});
|
|
320
378
|
|
|
379
|
+
// `messaging.history` returns DESCENDING; every comment source returns ASCENDING.
|
|
380
|
+
// A bare slice(-limit) therefore took the OLDEST rows off a message page and
|
|
381
|
+
// printed them backwards — the wrong window, in the wrong order.
|
|
382
|
+
test("renderThread reads forwards and keeps the most RECENT turns", () => {
|
|
383
|
+
const rows = [];
|
|
384
|
+
for (let i = 12; i >= 1; i -= 1) {
|
|
385
|
+
rows.push({ authorName: "A", body: `turn ${i}`, createdAt: `2026-08-11T00:${String(i).padStart(2, "0")}:00.000Z` });
|
|
386
|
+
}
|
|
387
|
+
const out = renderThread(rows); // limit 8
|
|
388
|
+
|
|
389
|
+
assert.match(out, /turn 12/, "the newest turn is the one that matters most");
|
|
390
|
+
assert.doesNotMatch(out, /turn 1\b/, "the oldest turns fall off the window, not the newest");
|
|
391
|
+
assert.ok(out.indexOf("turn 5") < out.indexOf("turn 12"), "oldest first");
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
test("renderThread leaves undated rows in the order they arrived", () => {
|
|
395
|
+
const out = renderThread([
|
|
396
|
+
{ authorName: "A", body: "first" },
|
|
397
|
+
{ authorName: "B", body: "second" },
|
|
398
|
+
]);
|
|
399
|
+
assert.ok(out.indexOf("first") < out.indexOf("second"));
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
// The `limit` window and the `max` CHARACTER budget are two different windows,
|
|
403
|
+
// and only the first was covered: twelve short turns never reach 2000 chars, so
|
|
404
|
+
// the clip never fired and the direction it clips in was never asserted. On the
|
|
405
|
+
// real DM it fired immediately — one long reply filled the budget and the four
|
|
406
|
+
// newest turns, including the question being answered, were the ones discarded.
|
|
407
|
+
test("renderThread keeps the NEWEST turns when the CHARACTER budget binds", () => {
|
|
408
|
+
const out = renderThread(
|
|
409
|
+
[
|
|
410
|
+
{ authorName: "A", body: "x".repeat(1500), createdAt: "2026-08-11T00:01:00.000Z" },
|
|
411
|
+
{ authorName: "B", body: "the thing I actually asked", createdAt: "2026-08-11T00:02:00.000Z" },
|
|
412
|
+
],
|
|
413
|
+
{ max: 300 },
|
|
414
|
+
);
|
|
415
|
+
assert.match(out, /the thing I actually asked/, "the latest turn must survive the budget");
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
test("renderThread does not let one huge turn starve the turns around it", () => {
|
|
419
|
+
const out = renderThread(
|
|
420
|
+
[
|
|
421
|
+
{ authorName: "A", body: "y".repeat(5000), createdAt: "2026-08-11T00:01:00.000Z" },
|
|
422
|
+
{ authorName: "B", body: "second", createdAt: "2026-08-11T00:02:00.000Z" },
|
|
423
|
+
{ authorName: "C", body: "third", createdAt: "2026-08-11T00:03:00.000Z" },
|
|
424
|
+
],
|
|
425
|
+
{ max: 2000 },
|
|
426
|
+
);
|
|
427
|
+
assert.match(out, /B: second/);
|
|
428
|
+
assert.match(out, /C: third/);
|
|
429
|
+
assert.ok(out.length <= 2100, `bounded, got ${out.length}`);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
// Until hq's `authorName` denormalisation is deployed every row carries a bare
|
|
433
|
+
// cuid, and the agent must still be able to see which turns are its own.
|
|
434
|
+
test("a transcript labels my own turns even when the server sends no names", async () => {
|
|
435
|
+
const msgs = [
|
|
436
|
+
{ id: "m1", authorId: ME, body: "already answered that", createdAt: "2026-08-11T14:00:00.000Z" },
|
|
437
|
+
{ id: "m2", authorId: THEM, body: "and the other thing?", createdAt: "2026-08-11T15:00:00.000Z" },
|
|
438
|
+
];
|
|
439
|
+
const f = facts({ myNames: ["Isla"] });
|
|
440
|
+
f.channels.set("C-dm", { id: "C-dm", kind: "DM" });
|
|
441
|
+
f.channelPages.set("C-dm", { byId: new Map(msgs.map((m) => [m.id, m])), messages: msgs });
|
|
442
|
+
const c = classifyEvent(ev({ family: "messaging", kind: "send", entity_id: "m2", payload: { actor: THEM, channelId: "C-dm", channelKind: "DM" } }));
|
|
443
|
+
const h = await hydrate({ candidate: c, verdict: { surface: "dm", reason: "dm" }, facts: f, io: fakeIo({}) });
|
|
444
|
+
|
|
445
|
+
assert.match(h.threadContext, /Isla: already answered that/, "my own turn is mine, not a cuid");
|
|
446
|
+
});
|
|
447
|
+
|
|
321
448
|
test("event-kind phrasing is stable and total", () => {
|
|
322
449
|
assert.match(describeBoardKind("item.assigned"), /assigned to you/);
|
|
323
450
|
assert.match(describeBoardKind("who.knows"), /Update on your board item/);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cohortapp/agent-sdk",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "Cohort Agent SDK \u2014 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": {
|