@volter/teams 0.1.60 → 0.1.62

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": "@volter/teams",
3
- "version": "0.1.60",
3
+ "version": "0.1.62",
4
4
  "type": "module",
5
5
  "description": "Volter Teams, the neutral team plane: machine identity and daemon core, the local door and server link, admission from machine grants, product doors, and the Teams server's neutral routes (company RFC 0008).",
6
6
  "license": "MIT",
package/server/http.mjs CHANGED
@@ -243,10 +243,31 @@ async function groupRoute(api,req,res,auth,groupId,principalId,url,requestId){
243
243
  statements.push({sql:'UPDATE teams SET auth_epoch=auth_epoch+1 WHERE id=?',params:[auth.team_id]});await guardedMutation(api,res,auth,guards,statements,{status,body:response,targetId:target});api.events.publish(auth.team_id,{type:'authorization_changed',group_id:groupId});if(req.method==='DELETE')closeChannelsFor(api,auth.team_id,{groupId,principalId:principalId??null,reason:'group_changed'});send(res,status,response,requestId);
244
244
  }
245
245
  async function enroll(api,req,res,auth,requestId){if(auth.role==='viewer')fail(403,'forbidden','viewers cannot enroll machines');const b=await parseJson(req);exact(b,['name','enrollment_key']);if(!b.name||!b.enrollment_key)fail(400,'invalid_request','name and enrollment_key required');const byKey=()=>api.store.one('SELECT m.*,s.id source_id,s.name source_name,s.kind source_kind,s.state source_state,s.visibility,s.revision source_revision,s.last_received_at,s.decoder,s.counters_json,s.last_error FROM machines m JOIN sources s ON s.team_id=m.team_id AND s.machine_id=m.id WHERE m.team_id=? AND m.custodian_id=? AND m.enrollment_key=?',[auth.team_id,auth.principal_id,b.enrollment_key]);let machine=await byKey();
246
- // The same person enrolling a machine of the same name again (a reinstall, a lost home) takes that machine back; the
247
- // caller then rotates its credential, as for a machine it already knew.
248
- if(!machine){const own=await api.store.one('SELECT id FROM machines WHERE team_id=? AND custodian_id=? AND name=? AND revoked_at IS NULL',[auth.team_id,auth.principal_id,b.name]);if(own){await api.store.transaction([{sql:'UPDATE machines SET enrollment_key=? WHERE team_id=? AND id=?',params:[b.enrollment_key,auth.team_id,own.id]}]);machine=await byKey();}}
249
- if(machine){return send(res,200,result(api,auth,{machine:machineJson(machine,api.clock.nowMs(),api),source:sourceJson({id:machine.source_id,name:machine.source_name,kind:machine.source_kind,state:machine.source_state,machine_id:machine.id,custodian_id:machine.custodian_id,visibility:machine.visibility,revision:machine.source_revision,last_received_at:machine.last_received_at,decoder:machine.decoder,counters_json:machine.counters_json,last_error:machine.last_error}),credential:null}),requestId);}const mid=api.ids.next('machine'),sid=api.ids.next('source'),cid=api.ids.next('credential'),token=makeToken(cid),now=api.clock.nowMs(),data={machine:{id:mid,name:b.name,custodian_id:auth.principal_id,enrollment:'active',connection:'offline',last_seen_at:null,incarnation_id:null,revision:1},source:{id:sid,name:`${b.name} connector`,kind:'connector',state:'active',machine_id:mid,custodian_id:auth.principal_id,default_visibility:'restricted',revision:1,last_received_at:null,decoder:null},credential:{token,kind:'machine',expires_at:now+2592000000}},response=result(api,auth,data);await guardedMutation(api,res,auth,[...authGuards(auth,now),{sql:'SELECT 1 ok WHERE NOT EXISTS (SELECT 1 FROM sources WHERE team_id=? AND name=?)',params:[auth.team_id,`${b.name} connector`],code:'conflict',status:409,message:`a machine named ${b.name} is already enrolled in this team; enroll this one under another --name`}],[{sql:'INSERT INTO machines(team_id,id,name,custodian_id,enrollment_key) VALUES (?,?,?,?,?)',params:[auth.team_id,mid,b.name,auth.principal_id,b.enrollment_key]},{sql:"INSERT INTO sources(team_id,id,name,kind,machine_id,custodian_id,visibility,state) VALUES (?,?,?,?,?,?,?,'active')",params:[auth.team_id,sid,`${b.name} connector`,'connector',mid,auth.principal_id,'restricted']},{sql:'INSERT INTO credentials(id,token_hash,principal_id,kind,team_id,source_id,machine_id,expires_at,created_at) VALUES (?,?,?,?,?,?,?,?,?)',params:[cid,hashToken(token),auth.principal_id,'machine',auth.team_id,sid,mid,now+2592000000,now]}],{status:201,body:response,targetId:mid});send(res,201,response,requestId);}
246
+ // Enrollment is a fresh, authenticated custodial act. A disconnect revoked the
247
+ // old credentials, but must not permanently reserve this custodian's name.
248
+ if(!machine) machine=await api.store.one('SELECT m.*,s.id source_id,s.name source_name,s.kind source_kind,s.state source_state,s.visibility,s.revision source_revision,s.last_received_at,s.decoder,s.counters_json,s.last_error FROM machines m JOIN sources s ON s.team_id=m.team_id AND s.machine_id=m.id WHERE m.team_id=? AND m.custodian_id=? AND m.name=?',[auth.team_id,auth.principal_id,b.name]);
249
+ if(machine){
250
+ const now=api.clock.nowMs(),reviving=machine.revoked_at!=null;
251
+ const changed=reviving||machine.enrollment_key!==b.enrollment_key;
252
+ const cid=reviving?api.ids.next('credential'):null,token=reviving?makeToken(cid):null;
253
+ const updated=changed?{...machine,enrollment_key:b.enrollment_key,revision:machine.revision+1,...(reviving?{revoked_at:null,last_seen_at:null,incarnation_id:null,heartbeat_sequence:0}: {})}:machine;
254
+ const source=sourceJson({id:machine.source_id,name:machine.source_name,kind:machine.source_kind,state:reviving?'active':machine.source_state,machine_id:machine.id,custodian_id:machine.custodian_id,visibility:machine.visibility,revision:machine.source_revision+(reviving?1:0),last_received_at:machine.last_received_at,decoder:machine.decoder,counters_json:machine.counters_json,last_error:machine.last_error});
255
+ const response=result(api,auth,{machine:machineJson(updated,now,api),source,credential:reviving?{token,kind:'machine',expires_at:now+2592000000}:null});
256
+ if(changed){
257
+ const guards=[...authGuards(auth,now),{sql:'SELECT 1 ok FROM machines m JOIN sources s ON s.team_id=m.team_id AND s.machine_id=m.id WHERE m.team_id=? AND m.id=? AND m.custodian_id=? AND m.revision=? AND s.id=? AND s.revision=?',params:[auth.team_id,machine.id,auth.principal_id,machine.revision,machine.source_id,machine.source_revision],code:'revision_mismatch',status:409,message:'machine enrollment changed; retry connect'}];
258
+ const statements=[{sql:'UPDATE machines SET enrollment_key=?,revision=revision+1 WHERE team_id=? AND id=?',params:[b.enrollment_key,auth.team_id,machine.id]}];
259
+ if(reviving)statements.push(
260
+ {sql:'UPDATE machines SET revoked_at=NULL,last_seen_at=NULL,incarnation_id=NULL,heartbeat_sequence=0 WHERE team_id=? AND id=?',params:[auth.team_id,machine.id]},
261
+ {sql:"UPDATE sources SET state='active',revision=revision+1 WHERE team_id=? AND id=?",params:[auth.team_id,machine.source_id]},
262
+ {sql:'UPDATE credentials SET revoked_at=? WHERE team_id=? AND machine_id=? AND revoked_at IS NULL',params:[now,auth.team_id,machine.id]},
263
+ {sql:'INSERT INTO credentials(id,token_hash,principal_id,kind,team_id,source_id,machine_id,expires_at,created_at) VALUES (?,?,?,?,?,?,?,?,?)',params:[cid,hashToken(token),auth.principal_id,'machine',auth.team_id,machine.source_id,machine.id,now+2592000000,now]},
264
+ {sql:'UPDATE teams SET auth_epoch=auth_epoch+1 WHERE id=?',params:[auth.team_id]});
265
+ await guardedMutation(api,res,auth,guards,statements,{status:200,body:response,targetId:machine.id});
266
+ if(reviving)api.events.publish(auth.team_id,{type:'authorization_changed',machine_id:machine.id});
267
+ }
268
+ return send(res,200,response,requestId);
269
+ }
270
+ const mid=api.ids.next('machine'),sid=api.ids.next('source'),cid=api.ids.next('credential'),token=makeToken(cid),now=api.clock.nowMs(),data={machine:{id:mid,name:b.name,custodian_id:auth.principal_id,enrollment:'active',connection:'offline',last_seen_at:null,incarnation_id:null,revision:1},source:{id:sid,name:`${b.name} connector`,kind:'connector',state:'active',machine_id:mid,custodian_id:auth.principal_id,default_visibility:'restricted',revision:1,last_received_at:null,decoder:null},credential:{token,kind:'machine',expires_at:now+2592000000}},response=result(api,auth,data);await guardedMutation(api,res,auth,[...authGuards(auth,now),{sql:'SELECT 1 ok WHERE NOT EXISTS (SELECT 1 FROM sources WHERE team_id=? AND name=?)',params:[auth.team_id,`${b.name} connector`],code:'conflict',status:409,message:`a machine named ${b.name} is already enrolled in this team; enroll this one under another --name`}],[{sql:'INSERT INTO machines(team_id,id,name,custodian_id,enrollment_key) VALUES (?,?,?,?,?)',params:[auth.team_id,mid,b.name,auth.principal_id,b.enrollment_key]},{sql:"INSERT INTO sources(team_id,id,name,kind,machine_id,custodian_id,visibility,state) VALUES (?,?,?,?,?,?,?,'active')",params:[auth.team_id,sid,`${b.name} connector`,'connector',mid,auth.principal_id,'restricted']},{sql:'INSERT INTO credentials(id,token_hash,principal_id,kind,team_id,source_id,machine_id,expires_at,created_at) VALUES (?,?,?,?,?,?,?,?,?)',params:[cid,hashToken(token),auth.principal_id,'machine',auth.team_id,sid,mid,now+2592000000,now]}],{status:201,body:response,targetId:mid});send(res,201,response,requestId);}
250
271
  // `online` is a machine that heartbeats and holds its link, so callers can reach it; `unlinked` heartbeats without one.
251
272
  const machineJson=(m,now,api)=>({id:m.id,name:m.name,custodian_id:m.custodian_id,enrollment:m.revoked_at==null?'active':'revoked',connection:m.last_seen_at!=null&&m.last_seen_at+45000>now?(!api||api.machineLinks?.has(`${m.team_id}\0${m.id}`)?'online':'unlinked'):'offline',last_seen_at:m.last_seen_at,incarnation_id:m.incarnation_id,host_key_id:m.host_key_id??null,revision:m.revision});
252
273
  async function machineAdminRoute(api,req,res,auth,machineId,roots,requestId){
@@ -45,7 +45,7 @@ export function agentImage(seed: string): string;
45
45
  * Who wrote each entry of a projected transcript (supercode-ui's `TranscriptEntryModel`s), from the messages' own
46
46
  * annotations. Each actor carries `id` (a principal id, a session address or seed) for a host that draws its own faces.
47
47
  */
48
- export function withPeople<Entry extends { role: string; text: string; ts: number | null; id: string }>(
48
+ export function withPeople<Entry extends { role: string; text: string; ts: number | null; id: string; messageIndex?: number }>(
49
49
  transcript: Entry[],
50
50
  messages: Array<{ role?: string; metadata?: Record<string, unknown> }>,
51
51
  options?: { agentName?: string | null; sessionId?: string; personImage?(seed: string): string; agentImage?(seed: string): string; locale?: string | string[] },
@@ -104,24 +104,32 @@ export function withPeople(transcript, messages, { agentName = null, sessionId =
104
104
  for (const message of messages) {
105
105
  if (message?.role !== 'user') continue;
106
106
  const at = Date.parse(message.metadata?.timestamp ?? '');
107
- if (Number.isFinite(at)) byTime.set(at, message.metadata);
107
+ // A timestamp alone is not an identity: a native batch may record several
108
+ // turns at the same instant. Keep this fallback only when it is unambiguous.
109
+ if (Number.isFinite(at)) byTime.set(at, byTime.has(at) ? null : message.metadata);
108
110
  }
109
111
  const people = transcript.map((entry) => {
110
112
  if (entry.role === 'assistant') return { ...entry, actor: { id: sessionId, ...(agentName ? { name: agentName } : {}), image: agent(sessionId), kind: 'agent' } };
111
113
  if (entry.role !== 'user') return entry;
114
+ // The projection carries its one-based position in this exact capture.
115
+ // Resolve it before looking at the words: a person's pasted envelope or
116
+ // wake notice remains that person's message when the native source says so.
117
+ const indexed = Number.isSafeInteger(entry.messageIndex) && entry.messageIndex > 0;
118
+ const native = indexed ? messages[entry.messageIndex - 1] : null;
119
+ const metadata = indexed ? native?.role === 'user' ? native.metadata ?? {} : {} : byTime.get(entry.ts) ?? {};
120
+ const attributed = Boolean(metadata.source_principal_id || metadata.source_name || metadata.source_via);
112
121
  const automated = /^\s*<(hook_prompt|supercode-notice)\b[^>]*>([\s\S]*)<\/\1>\s*$/.exec(entry.text);
113
122
  // This exact legacy wake string is emitted by mail_route::CODEX_WAKE. Do
114
123
  // not infer automation from general wording in a person's own message.
115
124
  const legacyWake = entry.text.trim() === 'You have unread supercode messages. Read them with: supercode message inbox';
116
- if (automated || legacyWake) return { ...entry, role: 'notice', code: 'automation', text: `${automated?.[1] === 'hook_prompt' ? 'Hook' : 'Mailbox'} notification: ${automated ? automated[2].trim() : entry.text.trim()}` };
125
+ if (!attributed && (automated || legacyWake)) return { ...entry, role: 'notice', code: 'automation', text: `${automated?.[1] === 'hook_prompt' ? 'Hook' : 'Mailbox'} notification: ${automated ? automated[2].trim() : entry.text.trim()}` };
117
126
  // A harness's delivery notice about a message is a compact system line, not a person's turn.
118
- if (/^\s*\[Cross-session [a-z ]*notice\]/i.test(entry.text)) return { ...entry, role: 'notice', text: entry.text.trim().split('\n')[0] };
119
- const peer = peerMessage(entry.text);
127
+ if (!attributed && /^\s*\[Cross-session [a-z ]*notice\]/i.test(entry.text)) return { ...entry, role: 'notice', text: entry.text.trim().split('\n')[0] };
128
+ const peer = attributed ? null : peerMessage(entry.text);
120
129
  if (peer) {
121
130
  const seed = peer.address.startsWith('sc:') ? peer.address : peer.name;
122
131
  return { ...entry, text: peer.body, mail: { id: peer.id || entry.id, from: peer.name, to: agentName || 'This session', direction: 'in' }, actor: { id: seed, name: peer.name, image: agent(seed), kind: 'agent' }, context: [{ id: `${entry.id}:envelope`, kind: 'envelope', label: 'Delivered by supercode', detail: entry.text }] };
123
132
  }
124
- const metadata = byTime.get(entry.ts) ?? {};
125
133
  const name = metadata.source_name || null;
126
134
  const seed = name ? (metadata.source_principal_id || name) : '';
127
135
  const via = typeof metadata.source_via === 'string' ? metadata.source_via.trim() : '';