@joe-broadhead/opencode-mem 0.8.1 → 0.8.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/src/errors.js CHANGED
@@ -66,6 +66,9 @@ export function credentialFailure(code) {
66
66
  return { code: "MEMORY_ACCESS_DENIED", next: "ask the Jelly account's owner to restore the account" }
67
67
  case "invalid_target":
68
68
  return { code: "MEMORY_NOT_OFFERED", next: `ask the Jelly account's owner to offer Memory to this agent, or ${KEY_STEP}` }
69
+ case "not_signed_in":
70
+ // A saved key is the person's who pasted it, never the speaker's: no key step.
71
+ return { code: "MEMORY_NOT_LINKED", next: "link your Jelly account on this host in Jelly, then try again" }
69
72
  default:
70
73
  return { code: "MEMORY_SIGN_IN_FAILED", next: KEY_STEP }
71
74
  }
package/src/events.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { buildSessionPayload, extractCompactionSummary, newTurn } from "./capture.js"
2
+ import { UNLINKED_SENTENCE, asSession } from "./credentials.js"
2
3
  import { CAPTURE_MESSAGE_CHARS, MAX_MESSAGES, splitContent } from "./limits.js"
3
4
  import { parentIDOf, readSession } from "./sessions.js"
4
5
  import { errorMessage, redactSecrets, rememberBounded, shortHash } from "./util.js"
@@ -43,13 +44,21 @@ export function summaryMessages(text) {
43
44
  * releases: the shipped 2.0.x stream uses `session.inbox.*` and
44
45
  * `session.execution.*`, while the documented V2 API uses `session.next.*`.
45
46
  *
47
+ * Each turn is captured as its own session (`asSession`): inside Jelly it is
48
+ * sent with that session's token, which names the person who spoke there. A
49
+ * session whose speaker has no Jelly account linked on the host is skipped:
50
+ * nothing is sent or spooled for it.
51
+ *
52
+ * With `capture: false` the stream is still read, for `session.deleted` only:
53
+ * what is kept for a deleted session (its token among them) is dropped.
54
+ *
46
55
  * @param {any} ctx
47
56
  * @param {ReturnType<import("./runtime.js").createRuntime>} runtime
48
57
  */
49
58
  export function attachCapture(ctx, runtime) {
50
- if (!runtime.config.capture) return () => {}
59
+ const capturing = Boolean(runtime.config.capture)
51
60
  if (!ctx?.event || typeof ctx.event.subscribe !== "function") {
52
- runtime.log.warn("host has no event.subscribe; capture disabled")
61
+ if (capturing) runtime.log.warn("host has no event.subscribe; capture disabled")
53
62
  return () => {}
54
63
  }
55
64
 
@@ -60,14 +69,25 @@ export function attachCapture(ctx, runtime) {
60
69
  const seenEvents = new Set()
61
70
  const compacted = new Set()
62
71
  let unavailableLogged = false
72
+ /** Sessions already logged as having an unlinked speaker; bounded like the other maps. */
73
+ const unlinkedLogged = new Set()
63
74
 
64
75
  /**
65
- * Memory is unavailable (a gateway or Jelly install with no credential):
66
- * nothing is captured, and nothing is spooled for later. Logged once.
76
+ * Memory is unavailable (a gateway or Jelly install with no credential), or
77
+ * the person speaking in this session has no Jelly account linked on the
78
+ * host: nothing is captured, and nothing is spooled for later. Logged once,
79
+ * and once per such session. Runs inside the session's `asSession`.
67
80
  */
68
- function skipUnavailable(what) {
69
- const reason = runtime.unavailable?.() ?? ""
81
+ async function skipUnavailable(what, sessionID) {
82
+ const reason = runtime.availability ? await runtime.availability() : (runtime.unavailable?.() ?? "")
70
83
  if (!reason) return false
84
+ if (reason === UNLINKED_SENTENCE) {
85
+ if (!unlinkedLogged.has(sessionID)) {
86
+ rememberBounded(unlinkedLogged, sessionID, undefined, MODELS_MAX)
87
+ runtime.log.info(`${what} not captured: the person speaking hasn't linked a Jelly account on this host`, { sessionID })
88
+ }
89
+ return true
90
+ }
71
91
  if (!unavailableLogged) {
72
92
  unavailableLogged = true
73
93
  runtime.log.info(`${what} not captured: memory is unavailable`, { reason })
@@ -111,11 +131,20 @@ export function attachCapture(ctx, runtime) {
111
131
  }
112
132
  }
113
133
 
114
- async function flush(sessionID, reason = "idle") {
134
+ /**
135
+ * Capture one session's open turn, as that session and that turn: a newer
136
+ * user message may have arrived (a superseded turn), and this turn's words
137
+ * go with this turn's token, never the newer speaker's.
138
+ */
139
+ function flush(sessionID, reason = "idle") {
140
+ return asSession(sessionID, () => flushTurn(sessionID, reason), turns.get(sessionID)?.messageID)
141
+ }
142
+
143
+ async function flushTurn(sessionID, reason) {
115
144
  const turn = turns.get(sessionID)
116
145
  if (!turn) return
117
146
  turns.delete(sessionID)
118
- if (skipUnavailable("turn")) return
147
+ if (await skipUnavailable("turn", sessionID)) return
119
148
  if (skipSubagent(sessionID, "turn")) return
120
149
  if (turn.model === "unknown" && models.has(sessionID)) turn.model = models.get(sessionID)
121
150
  const facts = await sessionFacts(sessionID, turn)
@@ -169,16 +198,22 @@ export function attachCapture(ctx, runtime) {
169
198
  runtime.drainSpool?.()
170
199
  } catch (error) {
171
200
  // A gateway or control failure leaves the turn in the spool with its
172
- // visibility; the replay asks the gateway to place it again.
201
+ // visibility; the replay asks the gateway to place it again. A speaker
202
+ // Jelly says has no linked account (learnt during this send) is not
203
+ // kept: the spool deletes the entry.
173
204
  const outcome = runtime.releaseSpool?.(staged, error)
174
205
  runtime.log.warn("turn capture failed", { outcome: outcome ?? "kept", error: errorMessage(error) })
175
206
  }
176
207
  }
177
208
 
178
- async function captureCompaction(sessionID) {
209
+ function captureCompaction(sessionID) {
210
+ return asSession(sessionID, () => captureCompactionSummary(sessionID))
211
+ }
212
+
213
+ async function captureCompactionSummary(sessionID) {
179
214
  if (!runtime.config.captureScenarios) return
180
215
  try {
181
- if (skipUnavailable("compaction summary")) return
216
+ if (await skipUnavailable("compaction summary", sessionID)) return
182
217
  if (skipSubagent(sessionID, "compaction")) return
183
218
  const facts = await sessionFacts(sessionID, {})
184
219
  if (skipSubagent(sessionID, "compaction")) return
@@ -263,6 +298,22 @@ export function attachCapture(ctx, runtime) {
263
298
  }
264
299
 
265
300
  const data = payloadOf(event)
301
+ if (event.type === "session.deleted") {
302
+ const sessionID = data.sessionID ?? data.info?.id
303
+ if (sessionID) {
304
+ turns.delete(sessionID)
305
+ models.delete(sessionID)
306
+ unlinkedLogged.delete(sessionID)
307
+ if (runtime.forgetSession) runtime.forgetSession(sessionID)
308
+ else runtime.invalidate?.(sessionID)
309
+ }
310
+ return
311
+ }
312
+ // A new user message: the person speaking may have changed, so the
313
+ // session's Jelly token is asked for again; also with capture off.
314
+ if (event.type === "session.inbox.enqueued" && data.item?.type === "user") runtime.userTurn?.(data.sessionID, data.inboxID)
315
+ if (event.type === "session.next.prompt.admitted") runtime.userTurn?.(data.sessionID, data.messageID)
316
+ if (!capturing) return
266
317
  if (!seenTypes.has(event.type)) {
267
318
  seenTypes.add(event.type)
268
319
  runtime.log.debug("event type observed", event.type)
@@ -354,15 +405,7 @@ export function attachCapture(ctx, runtime) {
354
405
  return
355
406
  }
356
407
 
357
- // --- lifecycle ---------------------------------------------------------
358
- case "session.deleted": {
359
- if (data.sessionID) {
360
- turns.delete(data.sessionID)
361
- models.delete(data.sessionID)
362
- runtime.invalidate?.(data.sessionID)
363
- }
364
- return
365
- }
408
+ // `session.deleted` is handled before the switch, also with capture off.
366
409
  default:
367
410
  return
368
411
  }
package/src/hooks.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { buildCompactionInstruction } from "./compaction.js"
2
+ import { asSession } from "./credentials.js"
2
3
  import { hasProtocol, protocolBlock } from "./protocol.js"
3
4
  import { isChildSession } from "./sessions.js"
4
5
  import { errorMessage } from "./util.js"
@@ -25,6 +26,10 @@ function positive(value, fallback) {
25
26
  * Every hook is best effort. A memory, control-plane, or backend failure must
26
27
  * never block a model call or a compaction.
27
28
  *
29
+ * Each hook runs as its event's session (`asSession`): inside Jelly the pinned
30
+ * block and the checkpoint use that session's token, which names the person
31
+ * speaking there.
32
+ *
28
33
  * @param {any} ctx
29
34
  * @param {any} runtime
30
35
  * @param {{ deadlineMs?: number, compactionTimeoutMs?: number }} [options]
@@ -67,23 +72,26 @@ export async function attachMemoryHooks(ctx, runtime, options = {}) {
67
72
  }
68
73
  }
69
74
 
70
- if (config.bootstrap) {
71
- const promptRegistration = await ctx.session.hook("prompt", (event) => {
72
- try {
73
- runtime.noteAgent?.(event?.sessionID, event?.agent)
74
- if (runtime.unavailable?.()) return
75
- if (runtime.skipsSubagent?.(event.sessionID)) return
76
- runtime.prefetchBootstrap?.(event.sessionID, event.agent)
77
- } catch (error) {
78
- log.warn("bootstrap prefetch failed", errorMessage(error))
79
- }
80
- })
81
- disposers.push(promptRegistration)
82
- }
75
+ // `prompt` runs once per user message, before the turn's model calls, tool
76
+ // calls and capture: the person speaking may have changed, so the session's
77
+ // Jelly token is asked for again (`runtime.userTurn`, src/credentials.js).
78
+ const promptRegistration = await ctx.session.hook("prompt", (event) => asSession(event?.sessionID, () => {
79
+ try {
80
+ runtime.userTurn?.(event?.sessionID, event?.messageID)
81
+ runtime.noteAgent?.(event?.sessionID, event?.agent)
82
+ if (!config.bootstrap) return
83
+ if (runtime.unavailable?.()) return
84
+ if (runtime.skipsSubagent?.(event.sessionID)) return
85
+ runtime.prefetchBootstrap?.(event.sessionID, event.agent)
86
+ } catch (error) {
87
+ log.warn("bootstrap prefetch failed", errorMessage(error))
88
+ }
89
+ }))
90
+ disposers.push(promptRegistration)
83
91
 
84
92
  // The protocol is independent of the bootstrap setting: an agent with no
85
93
  // global instruction file still needs the memory rules.
86
- const contextRegistration = await ctx.session.hook("context", async (event) => {
94
+ const contextRegistration = await ctx.session.hook("context", (event) => asSession(event?.sessionID, async () => {
87
95
  try {
88
96
  // The agent of every model call, for turn capture (see runtime.noteAgent).
89
97
  runtime.noteAgent?.(event?.sessionID, event?.agent)
@@ -113,6 +121,10 @@ export async function attachMemoryHooks(ctx, runtime, options = {}) {
113
121
  }
114
122
  }
115
123
 
124
+ // The bootstrap may have learnt that the person speaking has no Jelly
125
+ // account linked on this host: then no protocol either.
126
+ if (runtime.unavailable?.()) return
127
+
116
128
  if (!alreadyTaught) {
117
129
  const block = protocolBlock()
118
130
  event.system.push({ type: "text", text: block })
@@ -121,10 +133,10 @@ export async function attachMemoryHooks(ctx, runtime, options = {}) {
121
133
  } catch (error) {
122
134
  log.warn("context hook failed", errorMessage(error))
123
135
  }
124
- })
136
+ }))
125
137
  disposers.push(contextRegistration)
126
138
 
127
- const compactionRegistration = await ctx.session.hook("compaction", async (event) => {
139
+ const compactionRegistration = await ctx.session.hook("compaction", (event) => asSession(event?.sessionID, async () => {
128
140
  try {
129
141
  const alreadyInstructed = event?.system?.some?.((part) => String(part?.text ?? "").includes("<mem_compaction"))
130
142
  if (Array.isArray(event?.system) && !alreadyInstructed && !runtime.unavailable?.()) {
@@ -165,7 +177,7 @@ export async function attachMemoryHooks(ctx, runtime, options = {}) {
165
177
  log.warn("bootstrap invalidation failed", errorMessage(error))
166
178
  }
167
179
  }
168
- })
180
+ }))
169
181
  disposers.push(compactionRegistration)
170
182
 
171
183
  return {
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { configWarnings, resolveConfig } from "./config.js"
2
- import { createCredentials, followOrigin } from "./credentials.js"
2
+ import { asSession, createCredentials, followOrigin } from "./credentials.js"
3
3
  import { createLogger } from "./log.js"
4
4
  import { createBackend } from "./backend/v3.js"
5
5
  import { createRuntime } from "./runtime.js"
@@ -85,7 +85,10 @@ export default {
85
85
  skillRefreshQueued = options
86
86
  return skillRefreshPromise
87
87
  }
88
- skillRefreshPromise = (async () => {
88
+ // The mirror serves every session of the engine: work with no session,
89
+ // never one speaker's (src/credentials.js), also when a session's
90
+ // /mem-sync or bind asked for it.
91
+ skillRefreshPromise = asSession("", async () => {
89
92
  try {
90
93
  // Nothing to mirror from while memory is unavailable.
91
94
  if (runtime.unavailable()) return skillState
@@ -118,7 +121,7 @@ export default {
118
121
  skillRefreshQueued = null
119
122
  if (queued) void runtime.refreshSkills(queued)
120
123
  }
121
- })()
124
+ })
122
125
  return skillRefreshPromise
123
126
  }
124
127
 
package/src/runtime.js CHANGED
@@ -4,7 +4,7 @@ import { createClock } from "./config.js"
4
4
  import { dayIn, dueSection } from "./records.js"
5
5
  import { createRecordsStore } from "./records-store.js"
6
6
  import { buildCheckpoint, checkpointFingerprint } from "./compaction.js"
7
- import { credentialLine, unavailableReason } from "./credentials.js"
7
+ import { UNLINKED_SENTENCE, asSession, credentialLine, unavailableReason } from "./credentials.js"
8
8
  import { createControlClient } from "./spaces.js"
9
9
  import { createSkillContexts } from "./skill-contexts.js"
10
10
  import { createSpool } from "./spool.js"
@@ -100,14 +100,34 @@ export function createRuntime(input) {
100
100
  * One sentence saying why memory is unavailable, or "" when it is. Only a
101
101
  * gateway deployment needs a credential and an address; local direct mode
102
102
  * never reports itself unavailable here.
103
+ *
104
+ * It answers for the session the current work runs in (`asSession`): a
105
+ * session whose speaker Jelly said has no Jelly account linked on this host
106
+ * gets `UNLINKED_SENTENCE` while the refusal is held. Synchronous, so it
107
+ * knows only what Jelly already answered; `availability` asks first.
103
108
  */
104
109
  function unavailable() {
105
110
  if (!control || !credentials) return ""
106
- return unavailableReason({
111
+ const reason = unavailableReason({
107
112
  source: credentials.source(),
108
113
  controlURL: config.controlURL,
109
114
  inJelly: credentials.inJelly(),
110
115
  })
116
+ if (reason) return reason
117
+ return credentials.unlinked?.() ? UNLINKED_SENTENCE : ""
118
+ }
119
+
120
+ /**
121
+ * `unavailable()` for the current session after asking Jelly for its token
122
+ * when nothing is cached or held for it: the tools, the commands and the
123
+ * capture call this before they act, so an unlinked speaker's session is
124
+ * known before anything is sent or spooled.
125
+ */
126
+ async function availability() {
127
+ const reason = unavailable()
128
+ if (reason || !control || !credentials?.check) return reason
129
+ await credentials.check()
130
+ return unavailable()
111
131
  }
112
132
 
113
133
  /**
@@ -478,7 +498,9 @@ export function createRuntime(input) {
478
498
  if (attempted && attempted.signature === signature && Date.now() - attempted.at < SKILL_BIND_RETRY_MS) return
479
499
  if (skillMirror.inflight) return
480
500
  skillMirror.attempts.set(spaceID, { signature, at: Date.now() })
481
- queueMicrotask(() => {
501
+ // The mirror serves every session of the engine: it is work with no
502
+ // session, never one speaker's (src/credentials.js).
503
+ queueMicrotask(() => asSession("", () => {
482
504
  if (skillMirror.inflight) return
483
505
  skillMirror.inflight = Promise.resolve()
484
506
  .then(() => api.refreshSkills({ reason: "session-bind", context, timeoutMs: SKILL_BIND_TIMEOUT_MS }))
@@ -486,7 +508,7 @@ export function createRuntime(input) {
486
508
  .finally(() => {
487
509
  skillMirror.inflight = null
488
510
  })
489
- })
511
+ }))
490
512
  }
491
513
 
492
514
  /**
@@ -689,13 +711,21 @@ export function createRuntime(input) {
689
711
  * `reason` says why nothing was written: no-session, disabled, unavailable, subagent,
690
712
  * context-unresolved, no-context, empty, duplicate, or write-failed.
691
713
  */
692
- async function writeCompactionCheckpoint(input = {}) {
714
+ function writeCompactionCheckpoint(input = {}) {
715
+ // As the session compacting: its token, or nothing when its speaker has
716
+ // no Jelly account linked on this host.
717
+ return asSession(input?.sessionID, () => writeCheckpoint(input))
718
+ }
719
+
720
+ async function writeCheckpoint(input) {
693
721
  if (!input || typeof input !== "object") input = {}
694
722
  const sessionID = input.sessionID ? String(input.sessionID) : ""
695
723
  const skipped = (reason) => ({ ok: false, chars: 0, sessionID, reason })
696
724
  if (!sessionID) return skipped("no-session")
697
725
  if (config.compactionCheckpoint === false) return skipped("disabled")
698
- if (unavailable()) return skipped("unavailable")
726
+ // As the session compacting: its token, or nothing when its speaker has
727
+ // no Jelly account linked on this host.
728
+ if (await availability()) return skipped("unavailable")
699
729
  if (skipsSubagent(sessionID)) return skipped("subagent")
700
730
 
701
731
  const timeoutMs =
@@ -789,7 +819,33 @@ export function createRuntime(input) {
789
819
  }
790
820
  }
791
821
 
792
- const spool = createSpool({ config, log, backend, captureTurn: gatewayCapture ? captureTurn : null, now })
822
+ const spool = createSpool({
823
+ config,
824
+ log,
825
+ backend,
826
+ captureTurn: gatewayCapture ? captureTurn : null,
827
+ now,
828
+ // The account the current session's token names, recorded with an entry,
829
+ // and checked again before its replay (src/spool.js).
830
+ subject: () => credentials?.subject?.() ?? "",
831
+ speaker: async () => {
832
+ if (!credentials?.check) return ""
833
+ await credentials.check()
834
+ return credentials.subject()
835
+ },
836
+ })
837
+
838
+ /** A new user message in a session: its Jelly token is asked for again (src/credentials.js `userTurn`). */
839
+ function userTurn(sessionID, messageID) {
840
+ credentials?.userTurn?.(sessionID, messageID)
841
+ }
842
+
843
+ /** Drop what is kept for a session OpenCode deleted: its credential, pinned block and context. */
844
+ function forgetSession(sessionID) {
845
+ if (!sessionID) return
846
+ credentials?.forget?.(sessionID)
847
+ invalidate(sessionID)
848
+ }
793
849
  let lastDrain = 0
794
850
 
795
851
  /**
@@ -860,6 +916,9 @@ export function createRuntime(input) {
860
916
  today,
861
917
  credentials,
862
918
  unavailable,
919
+ availability,
920
+ userTurn,
921
+ forgetSession,
863
922
  scope,
864
923
  scopeFor,
865
924
  sharedScope,
package/src/spool.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import fs from "node:fs"
2
2
  import path from "node:path"
3
+ import { asSession, currentSession, isUnlinked } from "./credentials.js"
3
4
  import { errorMessage, oneLine, redactSecrets, shortHash } from "./util.js"
4
5
 
5
6
  /** At most this many sends per flush; entries that are not due do not count. */
@@ -27,7 +28,7 @@ const CLAIM_SUFFIX = /\.(\d+)-(\d+)\.inflight$/
27
28
  * quarantined with the reason.
28
29
  * - `unreachable`: no answer from the backend (transport error, timeout, 502,
29
30
  * 503, 504). The rest of the queue would fail the same way.
30
- * - `transient`: anything else, including 401 (a credential problem, fixed by
31
+ * - `transient`: an entry whose session has another speaker now, and anything else, including 401 (a credential problem, fixed by
31
32
  * the operator, not by the entry), 404 (a route the edge does not forward),
32
33
  * 408, 425, 429, other 5xx, and every control-plane failure: placement is
33
34
  * resolved again on the next attempt.
@@ -36,6 +37,7 @@ const CLAIM_SUFFIX = /\.(\d+)-(\d+)\.inflight$/
36
37
  * @returns {"permanent" | "unreachable" | "transient"}
37
38
  */
38
39
  export function classifyFailure(error) {
40
+ if (error?.speakerChanged) return "transient"
39
41
  const status = Number(error?.status)
40
42
  if (!Number.isFinite(status) || status === 0 || status === 408) return "unreachable"
41
43
  if (status === 502 || status === 503 || status === 504) return "unreachable"
@@ -75,12 +77,30 @@ function backoffMs(attempts) {
75
77
  * version is ignored on replay. In local direct mode the entry carries the
76
78
  * fixed local scope and is written to `conversation/add`.
77
79
  *
80
+ * **Whose credential sends an entry.** Each entry records `credentialSession`:
81
+ * the OpenCode session it was captured or written in ("" for work with no
82
+ * session). A replay runs as that session (`asSession`), so inside Jelly it is
83
+ * sent with that session's token, the token that names the person who spoke
84
+ * there, never with the token of the session whose turn happened to start the
85
+ * flush. An entry written before 0.8.2 has no such field: a turn replays as its
86
+ * own `sessionID`, anything else as work with no session. When Jelly says the
87
+ * person speaking in an entry's session has not linked a Jelly account on this
88
+ * host, the entry is deleted and the log says so, without its content: no
89
+ * other credential may send it. An entry also records `credentialSubject`, the
90
+ * account its token named; when its session's token names someone else at
91
+ * replay (another person spoke there since), it is kept for later, never sent
92
+ * with that token.
93
+ *
78
94
  * @param {{ config: any, log: any, backend: any, captureTurn?: Function | null, now?: () => number }} input
79
95
  */
80
96
  export function createSpool(input) {
81
97
  const { config, log, backend } = input
82
98
  const captureTurn = typeof input.captureTurn === "function" ? input.captureTurn : null
83
99
  const now = typeof input.now === "function" ? input.now : Date.now
100
+ /** The account the current session's token names, or "": recorded with an entry. */
101
+ const subject = typeof input.subject === "function" ? input.subject : () => ""
102
+ /** The account the current session's token names now, asking Jelly when needed: checked before a replay. */
103
+ const speaker = typeof input.speaker === "function" ? input.speaker : async () => ""
84
104
  const spoolDir = path.join(config.stateDir, "spool")
85
105
  const claimsDir = path.join(config.stateDir, "claims")
86
106
  let sequence = 0
@@ -133,7 +153,7 @@ export function createSpool(input) {
133
153
  try {
134
154
  ensureDir()
135
155
  const file = path.join(spoolDir, newName(data))
136
- writePrivate(file, { kind, data, scope, ...meta, queuedAt: new Date(now()).toISOString() }, "wx")
156
+ writePrivate(file, entryOf(kind, data, scope, meta), "wx")
137
157
  return file
138
158
  } catch (error) {
139
159
  log.error("spool write failed", errorMessage(error))
@@ -141,6 +161,20 @@ export function createSpool(input) {
141
161
  }
142
162
  }
143
163
 
164
+ /** The record written for a capture: the session whose credential will send it comes with it. */
165
+ function entryOf(kind, data, scope, meta) {
166
+ const credentialSubject = subject()
167
+ return {
168
+ kind,
169
+ data,
170
+ scope,
171
+ credentialSession: currentSession(),
172
+ ...(credentialSubject ? { credentialSubject } : {}),
173
+ ...meta,
174
+ queuedAt: new Date(now()).toISOString(),
175
+ }
176
+ }
177
+
144
178
  /**
145
179
  * Persist a capture that is about to be sent by this process. It is written
146
180
  * already claimed, so a concurrent flush cannot send it too. On success the
@@ -153,7 +187,7 @@ export function createSpool(input) {
153
187
  try {
154
188
  ensureDir()
155
189
  const file = claimName(path.join(spoolDir, newName(data)))
156
- writePrivate(file, { kind, data, scope, ...meta, queuedAt: new Date(now()).toISOString() }, "wx")
190
+ writePrivate(file, entryOf(kind, data, scope, meta), "wx")
157
191
  return file
158
192
  } catch (error) {
159
193
  log.error("spool write failed", errorMessage(error))
@@ -240,9 +274,35 @@ export function createSpool(input) {
240
274
  return "kept"
241
275
  }
242
276
 
277
+ /**
278
+ * Delete an entry whose session's speaker has no Jelly account linked on
279
+ * this host. The log names the file and kind, never the content.
280
+ */
281
+ function dropUnlinked(file, entry) {
282
+ try {
283
+ fs.unlinkSync(file)
284
+ } catch {
285
+ /* already gone */
286
+ }
287
+ log.warn("spooled entry dropped: the person speaking in its session hasn't linked a Jelly account on this host, and no other credential may send it", {
288
+ file: path.basename(pendingOf(file)),
289
+ kind: entry?.kind ?? null,
290
+ })
291
+ }
292
+
243
293
  /** Return a staged capture after a failed live send. */
244
294
  function releaseSpool(claimed, error) {
245
295
  if (!claimed) return null
296
+ if (isUnlinked(error)) {
297
+ let entry = null
298
+ try {
299
+ entry = JSON.parse(fs.readFileSync(claimed, "utf8"))
300
+ } catch {
301
+ /* the kind is only for the log */
302
+ }
303
+ dropUnlinked(claimed, entry)
304
+ return "dropped"
305
+ }
246
306
  try {
247
307
  const entry = JSON.parse(fs.readFileSync(claimed, "utf8"))
248
308
  return recordFailure(claimed, entry, error)
@@ -275,6 +335,15 @@ export function createSpool(input) {
275
335
  return entry.sessionID ?? entry.scope?.session_id ?? null
276
336
  }
277
337
 
338
+ /**
339
+ * The session whose credential sends an entry: the one it recorded, else
340
+ * (an entry from before 0.8.2) a turn's own session, else none.
341
+ */
342
+ function credentialSessionOf(entry) {
343
+ if (typeof entry.credentialSession === "string") return entry.credentialSession
344
+ return entry.kind === "session" ? (turnSessionOf(entry) ?? "") : ""
345
+ }
346
+
278
347
  /**
279
348
  * Cross-process, cross-instance claim on a capture. Multiple plugin instances
280
349
  * can observe the same global event stream, so turn identity has to be
@@ -369,6 +438,18 @@ export function createSpool(input) {
369
438
  }
370
439
 
371
440
  async function send(entry) {
441
+ // Someone else may speak in the session now. An entry captured under one
442
+ // person's token is never sent with another's: it waits, with backoff,
443
+ // until its session's token names that person again, or the caps
444
+ // quarantine it.
445
+ if (typeof entry.credentialSubject === "string" && entry.credentialSubject) {
446
+ const named = await speaker()
447
+ if (named && named !== entry.credentialSubject) {
448
+ throw Object.assign(new Error("the person speaking in this entry's session has changed since it was captured; it waits for them"), {
449
+ speakerChanged: true,
450
+ })
451
+ }
452
+ }
372
453
  if (entry.kind === "session") {
373
454
  if (captureTurn) {
374
455
  // Control mode: the gateway places the turn from the visibility it
@@ -460,11 +541,17 @@ export function createSpool(input) {
460
541
 
461
542
  sends += 1
462
543
  try {
463
- await send(entry)
544
+ // As the session the entry was captured in: its token, no other.
545
+ await asSession(credentialSessionOf(entry), () => send(entry))
464
546
  fs.unlinkSync(claimed)
465
547
  result.flushed += 1
466
548
  unreachable = 0
467
549
  } catch (error) {
550
+ if (isUnlinked(error)) {
551
+ dropUnlinked(claimed, entry)
552
+ result.dropped = (result.dropped ?? 0) + 1
553
+ continue
554
+ }
468
555
  result.failed += 1
469
556
  if (recordFailure(claimed, entry, error) === "quarantined") result.quarantined += 1
470
557
  if (classifyFailure(error) === "unreachable") {
@@ -1,4 +1,5 @@
1
1
  import { MEM_NAMESPACE_DESCRIPTION, PINNED_TOOLS } from "../call-shapes.js"
2
+ import { asSession } from "../credentials.js"
2
3
  import { createToolkit } from "./shared.js"
3
4
  import { registerRecall } from "./recall.js"
4
5
  import { registerWiki } from "./wiki.js"
@@ -16,6 +17,9 @@ import { registerSpaces } from "./spaces.js"
16
17
  * The tools are registered even when memory is unavailable (a gateway or
17
18
  * Jelly install with no credential): each then answers the one sentence
18
19
  * `runtime.unavailable()` gives, `mem_status` included, and makes no request.
20
+ * Each call runs as its session (`context.sessionID`, `asSession`): inside
21
+ * Jelly it uses that session's token, and in a session whose speaker has no
22
+ * Jelly account linked on the host it answers one sentence saying so.
19
23
  *
20
24
  * @param {any} ctx
21
25
  * @param {any} runtime
@@ -47,11 +51,12 @@ function gateEditor(editor, runtime) {
47
51
  target.add({
48
52
  ...definition,
49
53
  options: { ...definition.options, ...(PINNED_TOOLS.includes(definition.name) ? { pinned: true } : {}) },
50
- execute: async (input, context) => {
51
- const reason = runtime.unavailable?.() ?? ""
52
- if (reason) return { content: reason }
53
- return definition.execute(input, context)
54
- },
54
+ execute: (input, context) =>
55
+ asSession(context?.sessionID, async () => {
56
+ const reason = (runtime.availability ? await runtime.availability() : runtime.unavailable?.()) ?? ""
57
+ if (reason) return { content: reason }
58
+ return definition.execute(input, context)
59
+ }),
55
60
  })
56
61
  },
57
62
  })