2020117-agent 0.1.13 → 0.1.14

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.
Files changed (2) hide show
  1. package/dist/agent.js +43 -7
  2. package/package.json +1 -1
package/dist/agent.js CHANGED
@@ -685,6 +685,37 @@ async function startSwarmListener(label) {
685
685
  handleStop(job, msg.id, label);
686
686
  }
687
687
  });
688
+ // Handle customer disconnect — claim any earned tokens immediately
689
+ node.on('peer-leave', (peerId) => {
690
+ const tag = peerId.slice(0, 8);
691
+ // Find and end all sessions for this peer
692
+ for (const [sessionId, session] of activeSessions) {
693
+ if (session.peerId === peerId) {
694
+ console.log(`[${label}] Peer ${tag} disconnected — ending session ${sessionId} (${session.totalEarned} sats earned)`);
695
+ endSession(node, session, label);
696
+ }
697
+ }
698
+ // Clean up any P2P streaming jobs for this peer
699
+ for (const [jobId, job] of p2pJobs) {
700
+ if (job.socket?.remotePublicKey?.toString('hex') === peerId) {
701
+ console.log(`[${label}] Peer ${tag} disconnected — claiming P2P job ${jobId} tokens`);
702
+ job.stopped = true;
703
+ batchClaim(job.tokens, jobId, label);
704
+ p2pJobs.delete(jobId);
705
+ releaseSlot();
706
+ }
707
+ }
708
+ // Clean up backend WebSockets for this peer
709
+ for (const [wsId, entry] of backendWebSockets) {
710
+ if (entry.peerId === peerId) {
711
+ try {
712
+ entry.ws.close(1001, 'Peer disconnected');
713
+ }
714
+ catch { }
715
+ backendWebSockets.delete(wsId);
716
+ }
717
+ }
718
+ });
688
719
  }
689
720
  async function runP2PGeneration(node, job, msg, label) {
690
721
  const source = SUB_KIND
@@ -728,13 +759,18 @@ function endSession(node, session, label) {
728
759
  backendWebSockets.delete(wsId);
729
760
  }
730
761
  }
731
- node.send(session.socket, {
732
- type: 'session_end',
733
- id: session.sessionId,
734
- session_id: session.sessionId,
735
- total_sats: session.totalEarned,
736
- duration_s: durationS,
737
- });
762
+ try {
763
+ node.send(session.socket, {
764
+ type: 'session_end',
765
+ id: session.sessionId,
766
+ session_id: session.sessionId,
767
+ total_sats: session.totalEarned,
768
+ duration_s: durationS,
769
+ });
770
+ }
771
+ catch {
772
+ // Socket may already be closed (peer disconnect)
773
+ }
738
774
  console.log(`[${label}] Session ${session.sessionId} ended: ${session.totalEarned} sats, ${durationS}s`);
739
775
  // Batch claim tokens
740
776
  batchClaim(session.tokens, session.sessionId, label);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "2020117-agent",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "2020117 agent runtime — API polling + Hyperswarm P2P + Cashu streaming payments",
5
5
  "type": "module",
6
6
  "bin": {