@sjawhar/pi-envoy 17.30.1 → 17.30.3

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/README.md CHANGED
@@ -138,7 +138,7 @@ threads, comments on its documents and routed issues, mentions, and messages, ne
138
138
  word for word. A subagent reads its parent's list. The list stays under 8,000 characters (at most 7,999): the oldest
139
139
  items that do not fit are left out, and its last line says how many and that `dispatch answers`
140
140
  prints them all. It is read on every request and waits at most 800 ms for Dispatch. Past that,
141
- or when the read fails, the request carries the last list read, with the time it was read and why
141
+ or when the read fails, the read gives the last list read, with the time it was read and why
142
142
  this read failed, or, when no list read has an item from the last 72 hours, one line saying the
143
143
  list could not be read and why. A Dispatch build older than the answers route answers it with its
144
144
  own `404 NOT_FOUND` "no route for …". Against that Dispatch a request carries no such line, since
@@ -148,12 +148,31 @@ route; and that Dispatch is not asked again for 5 minutes. Any other answer, a p
148
148
  a 200 that is no answers list included, is a failed read.
149
149
 
150
150
  The list goes after the request's last message only on the request where it first shows an item,
151
- or new words in one. On every request after that it stays right after the message it followed
152
- there, so the request ends with its own last message and the model does not take a list with
153
- nothing new in it for a new turn; it keeps that place when it only loses items or its header
154
- changes. After a compaction summarizes that message, or in a new process, the list goes after the
155
- last message once more. It is never at the head, so a change in it costs only the bytes after it,
156
- and a list that keeps its place is inside the cached prefix of the next request.
151
+ new words in one, or an item taken back. Items the 8,000-character limit leaves out count too: a
152
+ new message longer than the limit, or a comment an edit made too long to fit, moves the list,
153
+ whose last line then says how many items were left out. An answer a person retracted leaves the
154
+ list while its time is still inside the 72 hours, and that moves the list too; an item that only
155
+ aged out of the 72 hours does not. On every request after that the list stays right after the
156
+ message it followed there, with the bytes it had then, so the request ends with its own last
157
+ message and the model does not take a list with nothing new in it for a new turn. It keeps that
158
+ place and those bytes when it only loses items to the 72 hours or names another read, so a kept
159
+ list can name an older read than the request's own and still show an item that has since left
160
+ the 72 hours. A list an older copy of the extension added keeps its place only when nothing but
161
+ its read time and reason changed. A read with no list to give, the line saying the list could not
162
+ be read, is never hidden behind a kept list: that line goes after the last message, and the list
163
+ goes there again once a read gives it. A read that fails but still gives the last list read, with
164
+ the same items, keeps the kept list's place and bytes, so that request does not say that its read
165
+ failed, unless the kept bytes are that read's own text, which say it; otherwise the log says it
166
+ instead, with the reason, on the first such request and not again until a read has answered, so a
167
+ failure that lasts is one line. A read that finds nothing for the session takes a kept list out of
168
+ the request when an item it shows was taken back inside the 72 hours, as when a person retracts
169
+ the only answer, and leaves it where it is when its items only aged out; it takes a kept failure
170
+ line out. A Dispatch with no answers route, when no list read before from it has an item from the
171
+ last 72 hours (none was read, or every item of it has aged out), or no Dispatch at all, leaves a
172
+ kept list where it is. Moving the list empties its old place.
173
+ After a compaction summarizes the message it followed, or in a new process, the list goes after
174
+ the last message once more. It is never at the head, so a change in it costs only the bytes from
175
+ its place on, and a list that keeps its place is inside the cached prefix of the next request.
157
176
 
158
177
  Oh My Pi also builds requests that are not turns of the conversation from it; those carry the
159
178
  skill but no list, and wait on no read: a side turn (a Dispatch BTW, the stop-time self-check,
package/dist/envoy.js CHANGED
@@ -45400,6 +45400,12 @@ function withinWindow(items, now) {
45400
45400
  const start = now - SESSION_ANSWERS_WINDOW_MS;
45401
45401
  return items.filter((item) => Date.parse(item.at) >= start);
45402
45402
  }
45403
+ function windowOf(items, now) {
45404
+ return {
45405
+ words: sessionAnswersWords(withinWindow(items, now)),
45406
+ since: new Date(now - SESSION_ANSWERS_WINDOW_MS).toISOString()
45407
+ };
45408
+ }
45403
45409
  function sessionAnswersWords(items) {
45404
45410
  return items.map((item) => JSON.stringify([
45405
45411
  item.kind,
@@ -45411,12 +45417,33 @@ function sessionAnswersWords(items) {
45411
45417
  item.text
45412
45418
  ]));
45413
45419
  }
45420
+ function wordTime(word) {
45421
+ try {
45422
+ const fields = JSON.parse(word);
45423
+ return Array.isArray(fields) && typeof fields[3] === "string" ? Date.parse(fields[3]) : Number.NaN;
45424
+ } catch {
45425
+ return Number.NaN;
45426
+ }
45427
+ }
45428
+ function sessionAnswersChanged(shown, next, since) {
45429
+ const before = new Set(shown);
45430
+ if (next.some((word) => !before.has(word)))
45431
+ return true;
45432
+ const after = new Set(next);
45433
+ const start = since === undefined ? Number.NaN : Date.parse(since);
45434
+ return shown.some((word) => {
45435
+ if (after.has(word))
45436
+ return false;
45437
+ const at = wordTime(word);
45438
+ return Number.isNaN(at) || Number.isNaN(start) || at >= start;
45439
+ });
45440
+ }
45414
45441
  var MAX_CHARS_TEXT = SESSION_ANSWERS_MAX_CHARS.toLocaleString("en-US");
45415
45442
  function leftOutLine(count, namesCommand) {
45416
45443
  const counted = `${count} older ${count === 1 ? "item" : "items"} left out to stay under ${MAX_CHARS_TEXT} characters`;
45417
45444
  return namesCommand ? `${counted}: run \`dispatch answers\` to read every one in full.` : `${counted}.`;
45418
45445
  }
45419
- function renderShown(items, options) {
45446
+ function renderSessionAnswers(items, options) {
45420
45447
  const inWindow = withinWindow(items, Number(options.now));
45421
45448
  const unsent = inWindow.length === items.length ? Math.max(0, (options.total ?? items.length) - items.length) : 0;
45422
45449
  const count = inWindow.length + unsent;
@@ -45444,7 +45471,7 @@ As read at ${options.cached.readAt}; this read failed: ${failureSentence(options
45444
45471
  ].join(`
45445
45472
 
45446
45473
  `);
45447
- return { text, shown: inWindow.slice(0, blocks.length) };
45474
+ return text;
45448
45475
  }
45449
45476
  function renderSessionAnswersList(items, options) {
45450
45477
  const shown = withinWindow(items, Number(options.now));
@@ -45517,24 +45544,19 @@ async function readSessionAnswers(options) {
45517
45544
  const entries = state.sessions.get(session) ?? new Map;
45518
45545
  state.sessions.set(session, entries);
45519
45546
  const carry = (cache, reason, noRoute = false) => {
45520
- const rendered = cache === undefined ? undefined : renderShown(cache.items, {
45521
- now: now(),
45547
+ const at = now();
45548
+ const text = cache === undefined ? undefined : renderSessionAnswers(cache.items, {
45549
+ now: at,
45522
45550
  total: cache.total,
45523
45551
  cached: { readAt: cache.read_at, reason },
45524
45552
  withoutCommand: noRoute
45525
45553
  });
45526
- if (cache === undefined || rendered === undefined) {
45554
+ if (cache === undefined || text === undefined) {
45527
45555
  if (noRoute)
45528
45556
  return { text: undefined, source: "unavailable", reason };
45529
45557
  return { text: sessionAnswersFailureLine(reason), source: "failure", reason };
45530
45558
  }
45531
- return {
45532
- text: rendered.text,
45533
- source: "cache",
45534
- readAt: cache.read_at,
45535
- words: sessionAnswersWords(rendered.shown),
45536
- reason
45537
- };
45559
+ return { text, source: "cache", readAt: cache.read_at, ...windowOf(cache.items, at), reason };
45538
45560
  };
45539
45561
  if (!dispatch.enabled) {
45540
45562
  const url = options.dispatchUrl;
@@ -45575,6 +45597,7 @@ async function readSessionAnswers(options) {
45575
45597
  retryNoRoute: false
45576
45598
  });
45577
45599
  const started = client.sessionAnswers(session, {
45600
+ since: new Date(now() - SESSION_ANSWERS_WINDOW_MS).toISOString(),
45578
45601
  maxChars: SESSION_ANSWERS_MAX_CHARS,
45579
45602
  signal: AbortSignal.timeout(READ_TIMEOUT_MS)
45580
45603
  }).then((response) => {
@@ -45626,12 +45649,12 @@ async function readSessionAnswers(options) {
45626
45649
  return noRoute();
45627
45650
  if ("failure" in outcome)
45628
45651
  return carry(current.cache, outcome.failure);
45629
- const rendered = renderShown(outcome.cache.items, { now: now(), total: outcome.cache.total });
45652
+ const at = now();
45630
45653
  return {
45631
- text: rendered?.text,
45654
+ text: renderSessionAnswers(outcome.cache.items, { now: at, total: outcome.cache.total }),
45632
45655
  source: "fresh",
45633
45656
  readAt: outcome.cache.read_at,
45634
- words: sessionAnswersWords(rendered?.shown ?? [])
45657
+ ...windowOf(outcome.cache.items, at)
45635
45658
  };
45636
45659
  }
45637
45660
 
@@ -47671,7 +47694,7 @@ var import_nats2 = __toESM(require_mod4(), 1);
47671
47694
 
47672
47695
  // package.json
47673
47696
  var name = "@sjawhar/pi-envoy";
47674
- var version2 = "17.30.1";
47697
+ var version2 = "17.30.3";
47675
47698
  var package_default = {
47676
47699
  name,
47677
47700
  version: version2,
@@ -49013,32 +49036,96 @@ var CONVERSIONS_KEY = Symbol.for("legion.pi-envoy.session-answers-conversions.v1
49013
49036
  var conversionsGlobal = globalThis;
49014
49037
  var conversions = conversionsGlobal[CONVERSIONS_KEY] ?? new WeakMap;
49015
49038
  conversionsGlobal[CONVERSIONS_KEY] = conversions;
49039
+ function carriedAnswers(read) {
49040
+ if (read.text === undefined && read.words === undefined)
49041
+ return;
49042
+ const reason = read.source === "cache" ? read.reason : undefined;
49043
+ const answered = read.source === "fresh";
49044
+ return { text: read.text, words: read.words, since: read.since, reason, answered };
49045
+ }
49016
49046
  var PLACEMENTS_KEY = Symbol.for("legion.pi-envoy.session-answers-placements.v1");
49017
49047
  var placementsGlobal = globalThis;
49018
49048
  var placements = placementsGlobal[PLACEMENTS_KEY] ?? new WeakMap;
49019
49049
  placementsGlobal[PLACEMENTS_KEY] = placements;
49050
+ var UNSHOWN_KEY = Symbol.for("legion.pi-envoy.session-answers-unshown.v1");
49051
+ var unshownGlobal = globalThis;
49052
+ var unshown = unshownGlobal[UNSHOWN_KEY] ?? new WeakMap;
49053
+ unshownGlobal[UNSHOWN_KEY] = unshown;
49054
+ var READ_LINE_PREFIX = "As read at ";
49055
+ function withoutReadLine(text) {
49056
+ const lines = text.split(`
49057
+ `);
49058
+ return lines[1]?.startsWith(READ_LINE_PREFIX) === true ? [lines[0], ...lines.slice(2)].join(`
49059
+ `) : text;
49060
+ }
49061
+ function failedRead(text) {
49062
+ const line = text.split(`
49063
+ `)[1];
49064
+ if (line?.startsWith(READ_LINE_PREFIX) !== true)
49065
+ return;
49066
+ const failed = "; this read failed: ";
49067
+ const at = line.indexOf(failed);
49068
+ return at < 0 ? undefined : line.slice(at + failed.length);
49069
+ }
49070
+ function isList(carried) {
49071
+ return carried.text.startsWith(SESSION_ANSWERS_MARKER);
49072
+ }
49020
49073
  function showsNew(shown, next) {
49021
- if (shown.words === undefined || next.words === undefined)
49022
- return shown.text !== next.text;
49023
- const before = new Set(shown.words);
49024
- return next.words.some((word) => !before.has(word));
49074
+ if (isList(shown) !== isList(next))
49075
+ return true;
49076
+ if (!isList(next))
49077
+ return false;
49078
+ if (shown.words === undefined || next.words === undefined) {
49079
+ return withoutReadLine(shown.text) !== withoutReadLine(next.text);
49080
+ }
49081
+ return sessionAnswersChanged(shown.words, next.words, next.since);
49082
+ }
49083
+ function keepPlaced(messages, placed) {
49084
+ const index = messages.findLastIndex((message) => messageKey(message) === placed.after);
49085
+ if (index < 0)
49086
+ return;
49087
+ return [
49088
+ ...messages.slice(0, index + 1),
49089
+ userTextMessage(placed.carried.text),
49090
+ ...messages.slice(index + 1)
49091
+ ];
49092
+ }
49093
+ function keptWithoutText(messages, carried, request, compacting) {
49094
+ const sessions = placements.get(request.session);
49095
+ const placed = sessions?.get(request.id);
49096
+ if (sessions === undefined || placed === undefined)
49097
+ return;
49098
+ const shown = placed.carried.words;
49099
+ const next = carried?.words;
49100
+ const withdrawn = next !== undefined && (shown === undefined || sessionAnswersChanged(shown, next, carried?.since));
49101
+ if (!isList(placed.carried) || withdrawn) {
49102
+ sessions.delete(request.id);
49103
+ return;
49104
+ }
49105
+ return compacting ? undefined : keepPlaced(messages, placed);
49025
49106
  }
49026
49107
  function placeList(messages, carried, request, compacting) {
49027
- const list = userTextMessage(carried.text);
49028
49108
  if (compacting)
49029
- return [...messages, list];
49109
+ return [...messages, userTextMessage(carried.text)];
49030
49110
  const sessions = placements.get(request.session) ?? new Map;
49031
49111
  placements.set(request.session, sessions);
49032
49112
  const placed = sessions.get(request.id);
49033
49113
  if (placed !== undefined && !showsNew(placed.carried, carried)) {
49034
- const index = messages.findLastIndex((message) => messageKey(message) === placed.after);
49035
- if (index >= 0) {
49036
- sessions.set(request.id, { after: placed.after, carried });
49037
- return [...messages.slice(0, index + 1), list, ...messages.slice(index + 1)];
49114
+ const kept = keepPlaced(messages, placed);
49115
+ if (kept !== undefined) {
49116
+ if (carried.reason !== undefined && carried.text !== placed.carried.text) {
49117
+ const told = unshown.get(request.session) ?? new Set;
49118
+ unshown.set(request.session, told);
49119
+ if (!told.has(request.id)) {
49120
+ told.add(request.id);
49121
+ request.unshownFailure(carried.reason);
49122
+ }
49123
+ }
49124
+ return kept;
49038
49125
  }
49039
49126
  }
49040
49127
  sessions.set(request.id, { after: messageKey(messages.at(-1)), carried });
49041
- return [...messages, list];
49128
+ return [...messages, userTextMessage(carried.text)];
49042
49129
  }
49043
49130
  async function withSessionAnswers(messages, request) {
49044
49131
  if (decided.has(messages))
@@ -49062,10 +49149,13 @@ async function withSessionAnswers(messages, request) {
49062
49149
  return;
49063
49150
  }
49064
49151
  const carried = await request.read();
49065
- if (carried === undefined)
49066
- return;
49067
- const placed = placeList(messages, carried, request, previous === "cut" || request.compacting());
49068
- decided.add(placed);
49152
+ if (carried?.answered === true)
49153
+ unshown.get(request.session)?.delete(request.id);
49154
+ const compacting = previous === "cut" || request.compacting();
49155
+ const text = carried?.text;
49156
+ const placed = carried === undefined || text === undefined ? keptWithoutText(messages, carried, request, compacting) : placeList(messages, { ...carried, text }, request, compacting);
49157
+ if (placed !== undefined)
49158
+ decided.add(placed);
49069
49159
  return placed;
49070
49160
  }
49071
49161
  function placedLegacy(messages, request) {
@@ -49076,7 +49166,11 @@ function placedLegacy(messages, request) {
49076
49166
  return;
49077
49167
  const recorded = conversions.get(request.session);
49078
49168
  const compacting = typeof recorded === "object" && recorded.afterCut || request.compacting();
49079
- const placed = placeList(messages.slice(0, -1), { text }, request, compacting);
49169
+ const reason = failedRead(text);
49170
+ if (reason === undefined && text.startsWith(SESSION_ANSWERS_MARKER)) {
49171
+ unshown.get(request.session)?.delete(request.id);
49172
+ }
49173
+ const placed = placeList(messages.slice(0, -1), { text, reason }, request, compacting);
49080
49174
  decided.add(placed);
49081
49175
  return placed;
49082
49176
  }
@@ -55263,10 +55357,10 @@ ${LATE_BTW_NOTE}`, "inert", identity, claim);
55263
55357
  });
55264
55358
  if (read.notice !== undefined)
55265
55359
  logger3.warn(`envoy: ${read.notice}`);
55266
- return read.text === undefined ? undefined : { text: read.text, words: read.words ?? [] };
55360
+ return carriedAnswers(read);
55267
55361
  } catch (error) {
55268
55362
  logger3.warn(`envoy: people's answers could not be read: ${messageFor(error)}`);
55269
- return { text: sessionAnswersFailureLine(messageFor(error)), words: [] };
55363
+ return { text: sessionAnswersFailureLine(messageFor(error)) };
55270
55364
  }
55271
55365
  };
55272
55366
  if (atStart.dispatch.enabled || inbound) {
@@ -55283,7 +55377,8 @@ ${LATE_BTW_NOTE}`, "inert", identity, claim);
55283
55377
  const live = liveAgentSession(context.sessionManager.getSessionId());
55284
55378
  return live !== undefined && (("isCompacting" in live) && live.isCompacting === true || ("compactionSpeculation" in live) && live.compactionSpeculation === "running");
55285
55379
  },
55286
- read: () => sessionAnswersText(context)
55380
+ read: () => sessionAnswersText(context),
55381
+ unshownFailure: (reason) => logger3.warn(`envoy: people's answers could not be read (${reason}); the request keeps the list placed before, which does not name this read's failure`)
55287
55382
  });
55288
55383
  const result = messages ?? withSkill ?? aged;
55289
55384
  return result === undefined ? undefined : { messages: result };
@@ -211,10 +211,11 @@ prompt, a skill, or the issue you work on. There is no list to browse and nothin
211
211
  secret exists is recorded against your session with the reason you signed, and it reaches a
212
212
  person's Inbox unless the model judge decides it first.
213
213
  - `UNKNOWN_SECRET` and a denial are final. Do not ask again, and do not try a name of your own.
214
- - A secret that a service owns, such as `LEGION_SLACK_BOT_TOKEN`, goes to that service's own pods
215
- and VMs and to nothing else: `Evaluate` in `packages/envoy/internal/broker/policy/policy.go`
216
- grants a service-owned secret at once when the session's own service is the secret's owner, and
217
- denies every other session, with no person asked.
214
+ - A secret that a service owns goes to that service's own pods and VMs and to nothing else:
215
+ `Evaluate` in `packages/envoy/internal/broker/policy/policy.go` grants a service-owned secret at
216
+ once when the session's own service is the secret's owner, and denies every other session, with
217
+ no person asked. A shared agent secret, such as `LEGION_SLACK_BOT_TOKEN`, goes at once to every
218
+ session, a pod's and a person's alike.
218
219
  - The `agent-secrets secret` forms (`list`, `show`, `create`, `set`, `retag`, `delete`, `restore`)
219
220
  call the broker's store under whatever cloud credential the environment gives them, and the
220
221
  store's IAM decides what each may read and write.
@@ -271,9 +272,8 @@ messages of the thread. A `ts` that names no message answers
271
272
  `{"ok":false,"error":"thread_not_found"}`, also at exit 0, which is why the body decides and not the
272
273
  status. A thread read needs the app's `channels:history` scope in a public channel and
273
274
  `groups:history` in a private one; without them Slack answers `missing_scope`, which only a person
274
- in the Slack workspace can fix. A session with a broker identity that is not one of the
275
- `legion-daemon` service's own is denied this token, and the example exits 77; a session with no
276
- broker identity exits 2.
275
+ in the Slack workspace can fix. `LEGION_SLACK_BOT_TOKEN` is a shared agent secret, so every session
276
+ with a broker identity is granted it; a session with no broker identity exits 2.
277
277
 
278
278
  Paging: Slack recommends a `limit` of at most 200. While the answer's `has_more` is true, pass its
279
279
  `response_metadata.next_cursor` as `cursor` to the next call. Each page repeats the thread's root
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjawhar/pi-envoy",
3
- "version": "17.30.1",
3
+ "version": "17.30.3",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "omp": {