@volter/supercode-teams 0.2.160 → 0.2.162

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/mail-history.mjs CHANGED
@@ -22,10 +22,11 @@ function sessionDirectory(binary, env, signal) {
22
22
 
23
23
  /** Resolve from supercode's existing session directory and explicit voice
24
24
  * backing, never by interpreting an operator mailbox's generated name. */
25
- function mailActors(rows, sessions, self) {
25
+ function mailActors(rows, sessions, self, answered = []) {
26
26
  const directory = new Map(sessions.map(session => [session.address, session]));
27
- const voices = new Map(rows.filter(row => row.envelope.voice_for).map(row => [row.envelope.from, row.envelope.voice_for]));
28
- const names = new Map(rows.map(row => [row.envelope.from, row.envelope.from_name]));
27
+ const identities = [...answered, ...rows];
28
+ const voices = new Map(identities.filter(row => row.envelope.voice_for).map(row => [row.envelope.from, row.envelope.voice_for]));
29
+ const names = new Map(identities.map(row => [row.envelope.from, row.envelope.from_name]));
29
30
  const actor = (address) => {
30
31
  const backing = voices.get(address) || address;
31
32
  const native = /^sc:([^:]+):([^:]+):(.+)$/.exec(backing);
@@ -45,6 +46,29 @@ function mailActors(rows, sessions, self) {
45
46
  }));
46
47
  }
47
48
 
49
+ async function answeredMail(rows, binary, env, signal) {
50
+ const known = new Set(rows.filter(row => row.envelope.voice_for).map(row => row.envelope.from));
51
+ const replies = rows.filter(row => row.direction === 'out' && row.envelope.in_reply_to && !known.has(row.session) && /^sc:[^:]+:operator:/.test(row.session));
52
+ const ids = [...new Set(replies.map(row => row.envelope.in_reply_to))];
53
+ if (!ids.length) return [];
54
+ return new Promise((resolve, reject) => {
55
+ const child = spawn(binary, ['message', 'show', ...ids, '--json'], {
56
+ env, stdio: ['ignore', 'pipe', 'pipe'], signal: AbortSignal.any([...(signal ? [signal] : []), AbortSignal.timeout(20_000)]),
57
+ });
58
+ let output = '', detail = '';
59
+ child.stdout.on('data', chunk => { output += chunk; if (output.length > 8_000_000) child.kill(); });
60
+ child.stderr.on('data', chunk => { detail = (detail + chunk).slice(-1000); });
61
+ child.on('error', reject);
62
+ child.on('close', code => {
63
+ try {
64
+ if (code !== 0 && code !== 3) throw new Error(detail || 'Answered messages could not be read');
65
+ const parents = JSON.parse(output).filter(row => row.found === true).map(row => row.message);
66
+ resolve(parents.filter(parent => parent.envelope?.voice_for && replies.some(reply => reply.envelope.in_reply_to === parent.envelope.id && reply.session === parent.envelope.from && reply.envelope.from === parent.session)));
67
+ } catch (error) { reject(error); }
68
+ });
69
+ });
70
+ }
71
+
48
72
  // Match the CLI's local_machine_name: the native mailbox belongs to its
49
73
  // enrolled context, which can differ from a viewer connector's --home.
50
74
  function machineName(env) {
@@ -81,7 +105,8 @@ export function readMailHistory({ binary, env, harness, session, signal }) {
81
105
  if (code !== 0) return reject(new Error(error || 'Mailbox could not be read'));
82
106
  try {
83
107
  const rows = JSON.parse(out).filter(row => ['peer', 'channel'].includes(row.envelope?.kind));
84
- resolve(mailActors(rows, await sessionDirectory(binary, env, signal), address));
108
+ const [sessions, answered] = await Promise.all([sessionDirectory(binary, env, signal), answeredMail(rows, binary, env, signal)]);
109
+ resolve(mailActors(rows, sessions, address, answered));
85
110
  } catch (error) { reject(error); }
86
111
  });
87
112
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/supercode-teams",
3
- "version": "0.2.160",
3
+ "version": "0.2.162",
4
4
  "type": "module",
5
5
  "description": "A machine daemon serving this machine's doors, and a local or remote Teams server for shared session discovery, OpenTelemetry ingestion and access to enrolled machines.",
6
6
  "license": "MIT",
@@ -203,7 +203,10 @@ const ACTIVITY_FRESH_MS=60000;
203
203
  /** The connector's latest fresh native turn report; stale or unrecognized reports remain unknown. */
204
204
  async function activityOf(api,teamId,sessionId){
205
205
  const row=await api.store.one('SELECT a.turn,a.since,a.observed_at FROM session_activity a JOIN origins o ON o.team_id=a.team_id AND o.source_id=a.source_id AND o.harness=a.harness AND o.native_id=a.native_id WHERE a.team_id=? AND o.deleted_at IS NULL AND o.session_id IN (SELECT id FROM sessions WHERE team_id=? AND (id=? OR canonical_id=?)) ORDER BY a.observed_at DESC LIMIT 1',[teamId,teamId,sessionId,sessionId]);
206
- return row&&['working','idle','needs_input'].includes(row.turn)&&api.clock.nowMs()-row.observed_at<ACTIVITY_FRESH_MS?{state:row.turn,since:row.since??row.observed_at,observedAt:row.observed_at}:{state:'unknown'};
206
+ // Unknown is a fresh server determination too: give it an ordering time so
207
+ // an older known report cannot restore Working while the source is stale.
208
+ const now=api.clock.nowMs();
209
+ return row&&['working','idle','needs_input'].includes(row.turn)&&now-row.observed_at<ACTIVITY_FRESH_MS?{state:row.turn,since:row.since??row.observed_at,observedAt:row.observed_at}:{state:'unknown',observedAt:now};
207
210
  }
208
211
  async function sessionDescriptor(api,res,auth,id,requestId){
209
212
  const row=await api.store.authorizedSession({teamId:auth.team_id,principalId:auth.principal_id,credentialId:auth.credential_id,ref:id,now:api.clock.nowMs(),bound:auth.bound});