@volter/supercode-teams 0.2.170 → 0.2.171

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/supercode-teams",
3
- "version": "0.2.170",
3
+ "version": "0.2.171",
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",
@@ -136,8 +136,22 @@ const managerGuard=(auth,sessionId,now,{governance=false}={})=>({
136
136
  async function resolve(api,auth,ref){const head='SELECT s.*,v.descriptor_json,v.sort_at FROM sessions s JOIN session_versions v ON v.team_id=s.team_id AND v.session_id=s.id AND v.revision=s.revision WHERE s.team_id=? AND ',native=/^([a-z][a-z0-9-]*):(.+)$/.exec(ref);let rows;if(native)rows=await api.store.query(head+'s.id IN (SELECT session_id FROM origins WHERE team_id=? AND harness=? AND native_id=? AND deleted_at IS NULL)',[auth.team_id,auth.team_id,native[1],native[2]]);else if(ref.length>=36)rows=await api.store.query(head+'s.id=?',[auth.team_id,ref]);else if(ref.length>=8)rows=await api.store.query(head+'s.id LIKE ?',[auth.team_id,`${ref}%`]);else rows=[];const visible=[];for(let r of rows){r=await canonicalSession(api,auth.team_id,r);r=await api.store.one('SELECT s.*,v.descriptor_json,v.sort_at FROM sessions s JOIN session_versions v ON v.team_id=s.team_id AND v.session_id=s.id AND v.revision=s.revision WHERE s.team_id=? AND s.id=?',[auth.team_id,r.id]);if(r&&await canReadMerged(api,auth,r)&&!visible.some(x=>x.id===r.id))visible.push(r);}if(visible.length>1)fail(400,'ambiguous_reference','ambiguous session reference');if(!visible.length)fail(404,'not_found','not found');return visible[0];}
137
137
  function qobj(url){return Object.fromEntries([...url.searchParams].map(([k,v])=>[k,['limit','since','until','tail','revision'].includes(k)?Number(v):k==='include_children'?v==='true':v]));}
138
138
  function projection(row){return JSON.parse(row.descriptor_json);}
139
- async function readerProjection(api,auth,row){
139
+ /**
140
+ * The native bindings sessions have now (contracts: Descriptor.bindings), by session id: each one a machine reported in
141
+ * its latest heartbeat, from the incarnation it runs as, on a machine still enrolled and seen within the window attach
142
+ * uses (effects.mjs), so a read offers only bindings attach would accept. Only the contract's fields are served. The
143
+ * captured descriptor never holds them; a heartbeat replaces them, so every read takes them fresh.
144
+ */
145
+ const BINDING_FIELDS=['id','machine_id','incarnation_id','native_session_id','native_runtime_id','harness','state','observed_at','native_capabilities'];
146
+ async function liveBindings(api,teamId,sessionId=null){
147
+ const rows=await api.store.query(`SELECT b.session_id,b.descriptor_json FROM bindings b JOIN machines m ON m.team_id=b.team_id AND m.id=b.machine_id AND m.incarnation_id=b.incarnation_id AND m.revoked_at IS NULL AND m.last_seen_at>? WHERE b.team_id=?${sessionId?' AND b.session_id=?':''} ORDER BY b.id`,[api.clock.nowMs()-45000,teamId,...(sessionId?[sessionId]:[])]);
148
+ const bySession=new Map();
149
+ for(const row of rows){const reported=JSON.parse(row.descriptor_json),binding=Object.fromEntries(BINDING_FIELDS.filter(field=>field in reported).map(field=>[field,reported[field]]));const list=bySession.get(row.session_id)??[];list.push(binding);bySession.set(row.session_id,list);}
150
+ return bySession;
151
+ }
152
+ async function readerProjection(api,auth,row,bindings=null){
140
153
  const view=projection(row);
154
+ view.bindings=(bindings??await liveBindings(api,auth.team_id,row.id)).get(row.id)??[];
141
155
  for(const field of ['parent_id','continued_from']){
142
156
  const reference=view[field];
143
157
  if(!reference)continue;
@@ -177,11 +191,11 @@ async function listSessions(api,res,auth,url,requestId,search){
177
191
  }else tuples=rows.filter(row=>row.revision===row.version_revision).map(row=>[row.id,row.version_revision]);
178
192
  const rowByVersion=new Map(rows.map(row=>[`${row.id}:${row.version_revision}`,row])),grantsBySession=new Map();
179
193
  for(const grant of allGrants){const list=grantsBySession.get(grant.session_id)??[];list.push(grant);grantsBySession.set(grant.session_id,list);}
180
- const hits=[],hitTuples=[];
194
+ const hits=[],hitTuples=[],bindingsBySession=await liveBindings(api,auth.team_id);
181
195
  for(const [id,revision]of tuples){
182
196
  const row=rowByVersion.get(`${id}:${revision}`),current=currentById.get(id);
183
197
  if(!row||!current||current.canonical_id||!await canReadMerged(api,{...auth,now:boundaryNow},current))continue;
184
- const view=await readerProjection(api,auth,row);
198
+ const view=await readerProjection(api,auth,row,bindingsBySession);
185
199
  if(query.user&&(query.user==='me'?auth.principal_id:query.user)!==view.user?.principal_id)continue;
186
200
  if(query.repo&&query.repo!==view.repository?.key)continue;
187
201
  if(query.harness&&query.harness!==view.harness)continue;
@@ -226,7 +240,7 @@ async function activityOf(api,teamId,sessionId){
226
240
  async function sessionDescriptor(api,res,auth,id,requestId){
227
241
  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});
228
242
  const activity=await activityOf(api,auth.team_id,row.id);
229
- send(res,200,result(api,auth,{...JSON.parse(row.descriptor_json),turnActivity:activity,working:activity.state==='working'?{since:activity.since}:null}),requestId,{ETag:`"${row.revision}"`});
243
+ send(res,200,result(api,auth,{...JSON.parse(row.descriptor_json),bindings:(await liveBindings(api,auth.team_id,row.id)).get(row.id)??[],turnActivity:activity,working:activity.state==='working'?{since:activity.since}:null}),requestId,{ETag:`"${row.revision}"`});
230
244
  }
231
245
  async function correlateRoute(api,req,res,auth,path,requestId){
232
246
  if(!['user','service'].includes(auth.kind)||auth.role==='viewer')fail(403,'forbidden','credential cannot correlate sessions');
@@ -252,6 +266,7 @@ async function readSession(api,res,auth,id,url,requestId){
252
266
  const {rows,beforeCursor,revision}=snapshot;
253
267
  const messages=rows.map(row=>({id:row.id,source_id:row.source_id,native_key:row.native_key,recorded_at:row.recorded_at,value:JSON.parse(row.value_json)}));
254
268
  const descriptor=JSON.parse(snapshot.descriptor_json);
269
+ descriptor.bindings=(await liveBindings(api,auth.team_id,descriptor.id)).get(descriptor.id)??[];
255
270
  const activity=await activityOf(api,auth.team_id,descriptor.id);
256
271
  send(res,200,{version:1,context:{server_id:api.serverId,team_id:auth.team_id,principal_id:auth.principal_id},descriptor,turnActivity:activity,working:activity.state==='working'?{since:activity.since}:null,names:await sessionNames(api,auth.team_id,descriptor),messages,before_cursor:beforeCursor,event_cursor:snapshot.eventCursor,revision,display:{returned:messages.length,total:snapshot.total,truncated_fields:0}},requestId);
257
272
  }