@terminal3/t3n-sdk 0.8.0 → 0.11.0

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/README.md CHANGED
@@ -586,7 +586,7 @@ To use the real WASM component:
586
586
 
587
587
  ```bash
588
588
  cd node
589
- cargo build -p session --target wasm32-wasip2 --release
589
+ cargo build -p session-client --target wasm32-wasip2 --release
590
590
  ```
591
591
 
592
592
  2. **Copy the WASM file** to the SDK directory:
package/dist/index.d.ts CHANGED
@@ -26,7 +26,9 @@ interface ClientHandshake {
26
26
  * Attempt to finalize handshake
27
27
  * @param state - Current handshake state
28
28
  * @returns Promise with session bytes if successful
29
- * @throws Error if handshake not ready to finalize
29
+ * @throws Error containing "not yet finalized" if the state machine
30
+ * has not reached its terminal phase. The SDK runtime treats
31
+ * this as the loop's "keep going" signal, not a real error.
30
32
  */
31
33
  finish(state: Uint8Array): Promise<Uint8Array>;
32
34
  }
@@ -45,7 +47,9 @@ interface ClientAuth {
45
47
  * Attempt to finalize authentication
46
48
  * @param state - Current auth state
47
49
  * @returns Promise with DID bytes if successful
48
- * @throws Error if authentication not ready to finalize
50
+ * @throws Error containing "not yet finalized" if the state machine
51
+ * has not reached its terminal phase. The SDK runtime treats
52
+ * this as the loop's "keep going" signal, not a real error.
49
53
  */
50
54
  finish(state: Uint8Array): Promise<Uint8Array>;
51
55
  }
@@ -64,7 +68,9 @@ interface ClientExecute {
64
68
  * Attempt to finalize authentication
65
69
  * @param state - Current auth state
66
70
  * @returns Promise with DID bytes if successful
67
- * @throws Error if authentication not ready to finalize
71
+ * @throws Error containing "not yet finalized" if the state machine
72
+ * has not reached its terminal phase. The SDK runtime treats
73
+ * this as the loop's "keep going" signal, not a real error.
68
74
  */
69
75
  finish(state: Uint8Array): Promise<Uint8Array>;
70
76
  }
@@ -94,7 +100,7 @@ interface SessionCrypto {
94
100
  * authentication flows, and cryptographic operations are handled in WASM.
95
101
  */
96
102
  interface WasmComponent {
97
- /** Client handshake operations */
103
+ /** Client-side state machines exported by the WASM component. */
98
104
  flow: {
99
105
  handshake: ClientHandshake;
100
106
  auth: ClientAuth;
@@ -518,7 +524,28 @@ declare class T3nClient {
518
524
  private readonly logger;
519
525
  private readonly encryption;
520
526
  private status;
527
+ /**
528
+ * In-flight WASM state-machine bytes. Holds the opaque state
529
+ * returned by `flow[method].next()` between iterations of
530
+ * `runFlow`. Always cleared at the top of `runFlow` and again
531
+ * once `tryFinalize` has extracted the terminal payload — so
532
+ * outside of an active loop these slots are always `null`.
533
+ */
521
534
  private wasmState;
535
+ /**
536
+ * Terminal payloads produced by `flow[method].finish()`:
537
+ * - `handshake` → serialized session blob, used by
538
+ * `getSessionState()` for subsequent `session.encrypt` calls.
539
+ * - `auth` → serialized DID; the public `authenticate()` decodes
540
+ * it into `this.did` and the slot is otherwise unused.
541
+ * - `execute` → unused (executes return immediately to the caller).
542
+ *
543
+ * Stored in a dedicated field instead of reusing `wasmState`
544
+ * because the two meanings — "in-flight state machine" vs
545
+ * "finalized payload" — are semantically different and merging
546
+ * them invites the bug-class Devin flagged in PR #1140.
547
+ */
548
+ private finalizedPayload;
522
549
  private did;
523
550
  private handshakeResult;
524
551
  constructor(config: T3nClientConfig);
@@ -559,11 +586,38 @@ declare class T3nClient {
559
586
  getLastResponseHeaders(): Record<string, string>;
560
587
  isAuthenticated(): boolean;
561
588
  /**
562
- * Run a WASM state machine flow to completion
589
+ * Run a WASM state machine flow to completion.
590
+ *
591
+ * Clears both `wasmState[method]` and `finalizedPayload[method]`
592
+ * at entry so a flow that previously threw partway (e.g. an RPC
593
+ * error) starts from a clean slate on retry. Without the reset,
594
+ * stale state from the failed attempt leaks into the new flow
595
+ * and `tryFinalize` may either spuriously succeed or run `next()`
596
+ * against a state that no longer matches the action we're sending.
597
+ *
598
+ * The `tryFinalize`-then-`next` order is load-bearing: the loop's
599
+ * exit condition fires *after* the previous iteration's
600
+ * `handleWasmRequest` has flushed the outbound peer reply, so
601
+ * every state-machine emission reaches the wire before we extract
602
+ * the final payload.
563
603
  */
564
604
  private runFlow;
565
605
  /**
566
- * Try to finalize the current flow
606
+ * Try to finalize the current flow. Returns the finish() payload
607
+ * (a serialized Session for handshake, a serialized DID for auth)
608
+ * or `null` if the state machine has not reached its terminal phase
609
+ * yet.
610
+ *
611
+ * The "not yet finalized" case is the loop's signal to keep
612
+ * iterating, not a real error. Any *other* failure must propagate
613
+ * so callers see real WASM errors instead of silent retries that
614
+ * spin forever.
615
+ *
616
+ * The terminal payload is stored in `finalizedPayload[method]`
617
+ * (a separate field from `wasmState[method]`) so the in-flight
618
+ * state-machine bytes and the finalized session/DID bytes never
619
+ * occupy the same slot. `getSessionState()` reads from
620
+ * `finalizedPayload.handshake`.
567
621
  */
568
622
  private tryFinalize;
569
623
  /**
@@ -584,7 +638,9 @@ declare class T3nClient {
584
638
  */
585
639
  private sendRpcRequest;
586
640
  /**
587
- * Get the current session state for encryption
641
+ * Get the finalized session blob (for `session.encrypt` calls).
642
+ * Populated by `tryFinalize` once the handshake state machine
643
+ * reaches its terminal phase.
588
644
  */
589
645
  private getSessionState;
590
646
  }