feat(cybersecurity): auto-commit con 48 cambios

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-04 23:44:39 +02:00
parent efc9911925
commit 729921e16e
48 changed files with 3765 additions and 8 deletions
+38
View File
@@ -0,0 +1,38 @@
package core
import (
"regexp"
"strings"
)
// ansiCSI matches CSI sequences: ESC [ ... <final byte>
// Covers colors (SGR), cursor movement, erase, etc.
var ansiCSI = regexp.MustCompile(`\x1b\[[0-9;?]*[ -/]*[@-~]`)
// ansiOSC matches OSC sequences: ESC ] ... <BEL or ST>
// Used for window titles, hyperlinks, etc.
var ansiOSC = regexp.MustCompile(`\x1b\][^\x07\x1b]*(\x07|\x1b\\)`)
// ansiEsc matches other two-character escape sequences: ESC <char>
// Covers ESC c (reset), ESC ( B, ESC ) 0, etc.
var ansiEsc = regexp.MustCompile(`\x1b[@-Z\\-_]|\x1b[()][0-9A-Za-z]`)
// StripANSI removes ANSI/VT100 terminal escape sequences from s and filters
// non-printable control characters, preserving newlines (\n), tabs (\t) and
// carriage returns (\r).
func StripANSI(s string) string {
s = ansiCSI.ReplaceAllString(s, "")
s = ansiOSC.ReplaceAllString(s, "")
s = ansiEsc.ReplaceAllString(s, "")
return strings.Map(func(r rune) rune {
// Preserve printable characters, \n (0x0A), \t (0x09), \r (0x0D).
if r == '\n' || r == '\t' || r == '\r' {
return r
}
// Drop C0 control characters (0x00-0x1F) and DEL (0x7F).
if r < 0x20 || r == 0x7F {
return -1
}
return r
}, s)
}
+55
View File
@@ -0,0 +1,55 @@
---
name: strip_ansi
kind: function
lang: go
domain: core
version: "1.0.0"
purity: pure
signature: "func StripANSI(s string) string"
description: "Elimina secuencias de escape ANSI/VT100 de un string y filtra caracteres de control no imprimibles, preservando \\n, \\t y \\r."
tags: ["terminal", "ansi", "string", "sanitize", "terminal-capture"]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: ""
imports: ["regexp", "strings"]
params:
- name: s
desc: "String que puede contener secuencias de escape de terminal (CSI, OSC, escapes simples) y/o caracteres de control."
output: "String limpio: sin secuencias ANSI ni caracteres de control, preservando saltos de línea (\\n), tabulaciones (\\t) y retornos de carro (\\r)."
tested: true
tests:
- "golden: color SGR codes"
- "edge OSC titulo de ventana"
- "edge movimientos de cursor"
- "edge string sin escapes preserva saltos de linea"
- "edge string vacio"
- "edge preserva tabs"
test_file_path: "functions/core/strip_ansi_test.go"
file_path: "functions/core/strip_ansi.go"
---
## Ejemplo
```go
// Limpiar output de terminal con color rojo
raw := "\x1b[31mError:\x1b[0m archivo no encontrado"
clean := core.StripANSI(raw)
// clean == "Error: archivo no encontrado"
// Limpiar título de ventana OSC
raw2 := "\x1b]0;mi titulo\x07contenido real"
clean2 := core.StripANSI(raw2)
// clean2 == "contenido real"
```
## Cuando usarla
Cuando captures output de un PTY/TUI/subprocess y necesites texto plano: antes de indexar logs con ANSI en un buscador, antes de difar output de terminal, o cuando muestres salida de comando en un contexto sin soporte de escape (UI web, archivo, base de datos).
## Gotchas
- Preserva `\n`, `\t` y `\r` a propósito: el output de terminales suele tener CRLF y tabulaciones con semántica propia.
- Cubre CSI, OSC y escapes simples de dos caracteres. Secuencias DCS o PM (rarísimas) no se eliminan; si las necesitas, añade una regex adicional antes de llamar a esta función.
- Las regexes están precompiladas a nivel de paquete: no hay coste de compilación por llamada.
+53
View File
@@ -0,0 +1,53 @@
package core
import "testing"
func TestStripANSI(t *testing.T) {
t.Run("golden: color SGR codes", func(t *testing.T) {
got := StripANSI("\x1b[31mhola\x1b[0m mundo")
want := "hola mundo"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("edge OSC titulo de ventana", func(t *testing.T) {
got := StripANSI("\x1b]0;mi titulo\x07texto")
want := "texto"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("edge movimientos de cursor", func(t *testing.T) {
got := StripANSI("linea1\x1b[2K\x1b[1Glinea2")
want := "linea1linea2"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("edge string sin escapes preserva saltos de linea", func(t *testing.T) {
got := StripANSI("plano\ncon\nlineas")
want := "plano\ncon\nlineas"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("edge string vacio", func(t *testing.T) {
got := StripANSI("")
want := ""
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("edge preserva tabs", func(t *testing.T) {
got := StripANSI("a\tb")
want := "a\tb"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
}
+23
View File
@@ -0,0 +1,23 @@
package core
// PrefixDelta returns the portion of curr that follows the longest common
// prefix (LCP) shared with prev, comparing rune-by-rune to avoid splitting
// multi-byte characters.
//
// In the monotone streaming case (curr = prev + new), this returns exactly
// the new suffix. When the text diverges mid-way (reflow), it returns
// everything from the point of divergence to the end of curr.
func PrefixDelta(prev, curr string) string {
prevRunes := []rune(prev)
currRunes := []rune(curr)
common := 0
for common < len(prevRunes) && common < len(currRunes) {
if prevRunes[common] != currRunes[common] {
break
}
common++
}
return string(currRunes[common:])
}
+63
View File
@@ -0,0 +1,63 @@
---
name: text_prefix_delta
kind: function
lang: go
domain: core
version: "1.0.0"
purity: pure
signature: "func PrefixDelta(prev, curr string) string"
description: "Calcula el delta de streaming entre dos versiones de un texto: devuelve la porción de curr que sigue al prefijo común más largo con prev, comparando runa a runa para no partir caracteres multibyte."
tags: [string, diff, streaming, delta, terminal-capture]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: ""
imports: []
params:
- name: prev
desc: "Versión anterior del texto acumulativo (snapshot anterior del stream)."
- name: curr
desc: "Versión actual del texto acumulativo (snapshot actual, normalmente extiende a prev)."
output: "La porción de curr que sigue al prefijo común con prev (el 'delta' de streaming). Devuelve cadena vacía si curr no añade nada nuevo tras el prefijo común."
tested: true
tests:
- "monotono append normal"
- "prev vacio devuelve curr completo"
- "sin cambios devuelve vacio"
- "divergencia en medio devuelve desde divergencia"
- "curr mas corto que prev devuelve vacio"
- "multibyte cafe streaming"
- "multibyte prefijo parcial antes de acento"
- "ambos vacios devuelve vacio"
- "prev no vacio curr vacio devuelve vacio"
- "determinismo misma entrada misma salida"
test_file_path: "functions/core/text_prefix_delta_test.go"
file_path: "functions/core/text_prefix_delta.go"
---
## Ejemplo
```go
// Bucle de streaming por snapshots acumulativos:
prev := ""
snapshots := []string{"Hola", "Hola, mun", "Hola, mundo!"}
for _, curr := range snapshots {
delta := PrefixDelta(prev, curr)
if delta != "" {
fmt.Print(delta) // emite solo la parte nueva
}
prev = curr
}
// Output: Hola, mundo!
```
## Cuando usarla
Cuando hagas streaming por snapshots acumulativos y necesites emitir solo la parte nueva de cada snapshot. Caso típico: consumir `pty_capture_stream_go_infra` donde cada captura de la TUI es un snapshot que extiende al anterior, y quieres emitir eventos `text_delta` estilo SSE/streaming sin reenviar texto ya enviado.
## Gotchas
- Compara por prefijo común, no por diff completo. Si el texto cambia en medio (reflow, borrado, sobreescritura de terminal), el delta incluye todo desde el punto de divergencia hasta el final de curr — puede re-emitir texto ya visto. Adecuado para append monótono; en streaming de TUI con reflow es heurístico, no exacto.
- Trabaja sobre runas (no bytes) para no partir caracteres UTF-8 multibyte como 'é', '中', '→'. El offset de corte siempre cae en un límite de runa válido.
+87
View File
@@ -0,0 +1,87 @@
package core
import "testing"
func TestPrefixDelta(t *testing.T) {
tests := []struct {
name string
prev string
curr string
want string
}{
{
name: "monotono append normal",
prev: "PON",
curr: "PONG",
want: "G",
},
{
name: "prev vacio devuelve curr completo",
prev: "",
curr: "abc",
want: "abc",
},
{
name: "sin cambios devuelve vacio",
prev: "abc",
curr: "abc",
want: "",
},
{
name: "divergencia en medio devuelve desde divergencia",
prev: "abc",
curr: "abXY",
want: "XY",
},
{
name: "curr mas corto que prev devuelve vacio",
prev: "abcdef",
curr: "abc",
want: "",
},
{
name: "multibyte cafe streaming",
prev: "café",
curr: "café con leche",
want: " con leche",
},
{
name: "multibyte prefijo parcial antes de acento",
prev: "ca",
curr: "café",
want: "fé",
},
{
name: "ambos vacios devuelve vacio",
prev: "",
curr: "",
want: "",
},
{
name: "prev no vacio curr vacio devuelve vacio",
prev: "hola",
curr: "",
want: "",
},
{
name: "determinismo misma entrada misma salida",
prev: "hello world",
curr: "hello world!",
want: "!",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := PrefixDelta(tc.prev, tc.curr)
if got != tc.want {
t.Errorf("PrefixDelta(%q, %q) = %q, want %q", tc.prev, tc.curr, got, tc.want)
}
// Verificar determinismo: segunda llamada produce el mismo resultado.
got2 := PrefixDelta(tc.prev, tc.curr)
if got != got2 {
t.Errorf("no determinista: primera=%q segunda=%q", got, got2)
}
})
}
}
@@ -0,0 +1,304 @@
package cybersecurity
import (
"bytes"
"testing"
)
// --- GenerateIdentity ---
func TestGenerateIdentity(t *testing.T) {
t.Run("genera keypairs con longitudes correctas", func(t *testing.T) {
id, err := GenerateIdentity()
if err != nil {
t.Fatalf("GenerateIdentity() error = %v", err)
}
if len(id.SignPub) != 32 {
t.Errorf("SignPub len = %d, want 32", len(id.SignPub))
}
if len(id.SignPriv) != 64 {
t.Errorf("SignPriv len = %d, want 64", len(id.SignPriv))
}
if len(id.KexPub) != 32 {
t.Errorf("KexPub len = %d, want 32", len(id.KexPub))
}
if len(id.KexPriv) != 32 {
t.Errorf("KexPriv len = %d, want 32", len(id.KexPriv))
}
})
t.Run("dos llamadas producen identidades distintas", func(t *testing.T) {
id1, err1 := GenerateIdentity()
id2, err2 := GenerateIdentity()
if err1 != nil || err2 != nil {
t.Fatal("GenerateIdentity() error inesperado")
}
if bytes.Equal(id1.SignPub, id2.SignPub) {
t.Error("SignPub idénticos en dos identidades distintas")
}
if bytes.Equal(id1.KexPub, id2.KexPub) {
t.Error("KexPub idénticos en dos identidades distintas")
}
})
}
// --- SealAEAD / OpenAEAD ---
func TestSealOpenAEAD(t *testing.T) {
key := make([]byte, 32)
for i := range key {
key[i] = byte(i + 1)
}
plaintext := []byte("mensaje secreto del bus de mensajería")
aad := []byte("room:sala-general")
t.Run("round-trip con aad", func(t *testing.T) {
nonce, ct, err := SealAEAD(key, plaintext, aad)
if err != nil {
t.Fatalf("SealAEAD error = %v", err)
}
got, err := OpenAEAD(key, nonce, ct, aad)
if err != nil {
t.Fatalf("OpenAEAD error = %v", err)
}
if !bytes.Equal(got, plaintext) {
t.Errorf("got %q, want %q", got, plaintext)
}
})
t.Run("round-trip sin aad (nil)", func(t *testing.T) {
nonce, ct, err := SealAEAD(key, plaintext, nil)
if err != nil {
t.Fatalf("SealAEAD error = %v", err)
}
got, err := OpenAEAD(key, nonce, ct, nil)
if err != nil {
t.Fatalf("OpenAEAD error = %v", err)
}
if !bytes.Equal(got, plaintext) {
t.Errorf("got %q, want %q", got, plaintext)
}
})
t.Run("error con clave de longitud incorrecta", func(t *testing.T) {
_, _, err := SealAEAD(key[:16], plaintext, nil)
if err == nil {
t.Error("esperaba error con clave de 16 bytes, got nil")
}
})
t.Run("error de autenticacion con ciphertext modificado", func(t *testing.T) {
nonce, ct, err := SealAEAD(key, plaintext, aad)
if err != nil {
t.Fatalf("SealAEAD error = %v", err)
}
ct[0] ^= 0xFF // corromper el primer byte
_, err = OpenAEAD(key, nonce, ct, aad)
if err == nil {
t.Error("esperaba error de autenticación con ciphertext corrupto, got nil")
}
})
t.Run("error de autenticacion con aad distinto", func(t *testing.T) {
nonce, ct, err := SealAEAD(key, plaintext, aad)
if err != nil {
t.Fatalf("SealAEAD error = %v", err)
}
_, err = OpenAEAD(key, nonce, ct, []byte("room:otra-sala"))
if err == nil {
t.Error("esperaba error de autenticación con aad distinto, got nil")
}
})
t.Run("nonces distintos en llamadas sucesivas", func(t *testing.T) {
n1, _, err1 := SealAEAD(key, plaintext, nil)
n2, _, err2 := SealAEAD(key, plaintext, nil)
if err1 != nil || err2 != nil {
t.Fatal("SealAEAD error inesperado")
}
if bytes.Equal(n1, n2) {
t.Error("nonces iguales en dos llamadas sucesivas (no aleatorios)")
}
})
}
// --- SealKeyBox / OpenKeyBox ---
func TestSealOpenKeyBox(t *testing.T) {
t.Run("round-trip con identidad generada", func(t *testing.T) {
id, err := GenerateIdentity()
if err != nil {
t.Fatalf("GenerateIdentity error = %v", err)
}
roomKey := make([]byte, 32)
for i := range roomKey {
roomKey[i] = byte(i + 42)
}
sealed, err := SealKeyBox(id.KexPub, roomKey)
if err != nil {
t.Fatalf("SealKeyBox error = %v", err)
}
opened, err := OpenKeyBox(id.KexPub, id.KexPriv, sealed)
if err != nil {
t.Fatalf("OpenKeyBox error = %v", err)
}
if !bytes.Equal(opened, roomKey) {
t.Errorf("got %x, want %x", opened, roomKey)
}
})
t.Run("error con recipientKexPub de longitud incorrecta", func(t *testing.T) {
_, err := SealKeyBox(make([]byte, 16), []byte("secret"))
if err == nil {
t.Error("esperaba error con kexPub de 16 bytes, got nil")
}
})
t.Run("error al abrir con clave equivocada", func(t *testing.T) {
id, _ := GenerateIdentity()
other, _ := GenerateIdentity()
sealed, err := SealKeyBox(id.KexPub, []byte("roomkey"))
if err != nil {
t.Fatalf("SealKeyBox error = %v", err)
}
_, err = OpenKeyBox(other.KexPub, other.KexPriv, sealed)
if err == nil {
t.Error("esperaba error al abrir con keypair distinto, got nil")
}
})
t.Run("error con mensaje truncado", func(t *testing.T) {
id, _ := GenerateIdentity()
_, err := OpenKeyBox(id.KexPub, id.KexPriv, []byte("corto"))
if err == nil {
t.Error("esperaba error con sealedMsg truncado, got nil")
}
})
}
// --- SignEd25519 / VerifyEd25519 ---
func TestSignVerifyEd25519(t *testing.T) {
t.Run("firma y verificacion exitosa", func(t *testing.T) {
id, err := GenerateIdentity()
if err != nil {
t.Fatalf("GenerateIdentity error = %v", err)
}
msg := []byte("evento:room_key_rotation:v2")
sig := SignEd25519(id.SignPriv, msg)
if len(sig) != 64 {
t.Errorf("sig len = %d, want 64", len(sig))
}
if !VerifyEd25519(id.SignPub, msg, sig) {
t.Error("VerifyEd25519 devolvió false para firma válida")
}
})
t.Run("firma es determinista (misma entrada, misma firma)", func(t *testing.T) {
id, _ := GenerateIdentity()
msg := []byte("determinismo criptografico")
sig1 := SignEd25519(id.SignPriv, msg)
sig2 := SignEd25519(id.SignPriv, msg)
if !bytes.Equal(sig1, sig2) {
t.Error("Ed25519 debe ser determinista: mismas entradas deben producir misma firma")
}
})
t.Run("falla con mensaje modificado", func(t *testing.T) {
id, _ := GenerateIdentity()
msg := []byte("mensaje original")
sig := SignEd25519(id.SignPriv, msg)
modified := []byte("mensaje modificado")
if VerifyEd25519(id.SignPub, modified, sig) {
t.Error("VerifyEd25519 devolvió true para mensaje modificado")
}
})
t.Run("falla con clave publica incorrecta", func(t *testing.T) {
id1, _ := GenerateIdentity()
id2, _ := GenerateIdentity()
msg := []byte("autenticidad del remitente")
sig := SignEd25519(id1.SignPriv, msg)
if VerifyEd25519(id2.SignPub, msg, sig) {
t.Error("VerifyEd25519 devolvió true con clave pública de otra identidad")
}
})
t.Run("falla con firma corrupta", func(t *testing.T) {
id, _ := GenerateIdentity()
msg := []byte("integridad")
sig := SignEd25519(id.SignPriv, msg)
sig[0] ^= 0xFF
if VerifyEd25519(id.SignPub, msg, sig) {
t.Error("VerifyEd25519 devolvió true con firma corrupta")
}
})
}
// --- Integración: flujo completo megolm-reducido ---
func TestE2EMessagingFlow(t *testing.T) {
t.Run("flujo completo: generar identidad, distribuir clave de sala, cifrar y firmar mensaje", func(t *testing.T) {
// Servidor genera identidad
server, err := GenerateIdentity()
if err != nil {
t.Fatalf("GenerateIdentity server: %v", err)
}
// Usuario genera identidad
user, err := GenerateIdentity()
if err != nil {
t.Fatalf("GenerateIdentity user: %v", err)
}
// Servidor genera clave de sala y la distribuye al usuario cifrada con su KexPub
roomKey := make([]byte, 32)
for i := range roomKey {
roomKey[i] = byte(i)
}
sealedKey, err := SealKeyBox(user.KexPub, roomKey)
if err != nil {
t.Fatalf("SealKeyBox: %v", err)
}
// Usuario desella la clave de sala
gotRoomKey, err := OpenKeyBox(user.KexPub, user.KexPriv, sealedKey)
if err != nil {
t.Fatalf("OpenKeyBox: %v", err)
}
if !bytes.Equal(gotRoomKey, roomKey) {
t.Fatal("clave de sala distribuida no coincide")
}
// Usuario cifra un mensaje con la clave de sala
plainMsg := []byte("hola sala, este es mi primer mensaje cifrado e2e")
aad := []byte("room:sala-secreta:seq:1")
nonce, ct, err := SealAEAD(gotRoomKey, plainMsg, aad)
if err != nil {
t.Fatalf("SealAEAD: %v", err)
}
// Usuario firma el ciphertext para autenticación del remitente
sig := SignEd25519(user.SignPriv, ct)
// Receptor verifica firma del remitente
if !VerifyEd25519(user.SignPub, ct, sig) {
t.Fatal("verificación de firma del remitente falló")
}
// Receptor descifra el mensaje
decrypted, err := OpenAEAD(gotRoomKey, nonce, ct, aad)
if err != nil {
t.Fatalf("OpenAEAD: %v", err)
}
if !bytes.Equal(decrypted, plainMsg) {
t.Errorf("mensaje descifrado %q != original %q", decrypted, plainMsg)
}
// Servidor tiene distinta identidad que el usuario (las claves no se confunden)
if bytes.Equal(server.SignPub, user.SignPub) {
t.Error("server y user tienen la misma clave pública de firma")
}
})
}
@@ -0,0 +1,40 @@
package cybersecurity
import (
"crypto/ed25519"
"crypto/rand"
"golang.org/x/crypto/nacl/box"
)
// Identity holds a dual keypair for a messaging participant:
// an Ed25519 keypair for signing and a X25519 keypair for key exchange.
type Identity struct {
SignPub []byte // Ed25519 public key (32 bytes)
SignPriv []byte // Ed25519 private key (64 bytes)
KexPub []byte // X25519 public key (32 bytes)
KexPriv []byte // X25519 private key (32 bytes)
}
// GenerateIdentity creates a new Identity with freshly generated Ed25519 and X25519 keypairs.
// Ed25519 keys are used for signing; X25519 keys for key exchange (sealed box).
func GenerateIdentity() (Identity, error) {
// Ed25519 keypair for message signing
signPub, signPriv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return Identity{}, err
}
// X25519 keypair for key exchange (nacl/box uses Curve25519 internally)
kexPub, kexPriv, err := box.GenerateKey(rand.Reader)
if err != nil {
return Identity{}, err
}
return Identity{
SignPub: []byte(signPub),
SignPriv: []byte(signPriv),
KexPub: kexPub[:],
KexPriv: kexPriv[:],
}, nil
}
@@ -0,0 +1,53 @@
---
name: generate_identity
kind: function
lang: go
domain: cybersecurity
version: "1.0.0"
purity: impure
signature: "func GenerateIdentity() (Identity, error)"
description: "Genera una identidad criptográfica dual con un par Ed25519 (firma) y un par X25519 (intercambio de claves). Punto de entrada obligatorio para cualquier participante en el bus de mensajería cifrado."
tags: [messaging, e2e-crypto, crypto, identity, ed25519, x25519, keygen, e2e-messaging]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports:
- crypto/ed25519
- crypto/rand
- golang.org/x/crypto/nacl/box
params:
- name: "(ninguno)"
desc: "Sin parámetros. Usa crypto/rand como fuente de entropía del sistema."
output: "Identity{SignPub []byte, SignPriv []byte, KexPub []byte, KexPriv []byte} o error si falla el RNG del sistema."
tested: true
tests:
- "genera keypairs con longitudes correctas"
- "dos llamadas producen identidades distintas"
test_file_path: "functions/cybersecurity/e2e_messaging_crypto_test.go"
file_path: "functions/cybersecurity/generate_identity.go"
---
## Ejemplo
```go
id, err := cybersecurity.GenerateIdentity()
if err != nil {
log.Fatal(err)
}
// id.SignPub / id.SignPriv — par Ed25519 para firmar mensajes
// id.KexPub / id.KexPriv — par X25519 para recibir claves de sala cifradas
fmt.Printf("identity pub(sign)=%x pub(kex)=%x\n", id.SignPub, id.KexPub)
```
## Cuando usarla
Al registrar un nuevo participante en el bus de mensajería: llama GenerateIdentity una sola vez por dispositivo/sesión, persiste los bytes de las cuatro claves de forma segura, y publica `SignPub` + `KexPub` en el directorio de participantes.
## Gotchas
- La función depende de `crypto/rand`; en entornos con entropía insuficiente (contenedores recién arrancados) puede bloquearse brevemente.
- `SignPriv` tiene 64 bytes (no 32): Ed25519 concatena seed (32) + clave pública (32) internamente. No truncar.
- `KexPub`/`KexPriv` son exactamente 32 bytes (Curve25519). Pasar exactamente esos slices a `SealKeyBox`/`OpenKeyBox`.
- Nunca reutilizar una identidad entre dispositivos distintos del mismo usuario sin un protocolo de clonado seguro.
+29
View File
@@ -0,0 +1,29 @@
package cybersecurity
import (
"fmt"
"golang.org/x/crypto/chacha20poly1305"
)
// OpenAEAD decrypts a ciphertext produced by SealAEAD using ChaCha20-Poly1305.
// key must be exactly 32 bytes. nonce must match the one returned by SealAEAD.
// aad must match what was passed to SealAEAD (can be nil).
// Returns an error if authentication fails (tampered ciphertext, wrong key, or wrong aad).
func OpenAEAD(key, nonce, ciphertext, aad []byte) ([]byte, error) {
if len(key) != chacha20poly1305.KeySize {
return nil, fmt.Errorf("open_aead: key must be %d bytes, got %d", chacha20poly1305.KeySize, len(key))
}
aead, err := chacha20poly1305.New(key)
if err != nil {
return nil, fmt.Errorf("open_aead: create cipher: %w", err)
}
plaintext, err := aead.Open(nil, nonce, ciphertext, aad)
if err != nil {
return nil, fmt.Errorf("open_aead: authentication failed: %w", err)
}
return plaintext, nil
}
+62
View File
@@ -0,0 +1,62 @@
---
name: open_aead
kind: function
lang: go
domain: cybersecurity
version: "1.0.0"
purity: impure
signature: "func OpenAEAD(key, nonce, ciphertext, aad []byte) ([]byte, error)"
description: "Descifra y autentica un ciphertext producido por SealAEAD usando ChaCha20-Poly1305. Devuelve error explícito si la autenticación falla (ciphertext alterado, clave incorrecta o AAD distinto)."
tags: [messaging, e2e-crypto, crypto, aead, chacha20poly1305, symmetric, e2e-messaging]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports:
- golang.org/x/crypto/chacha20poly1305
params:
- name: key
desc: "Clave simétrica de exactamente 32 bytes. Debe ser la misma usada en SealAEAD."
- name: nonce
desc: "Nonce de 12 bytes devuelto por SealAEAD. Debe transmitirse junto al ciphertext."
- name: ciphertext
desc: "Ciphertext producido por SealAEAD (incluye los 16 bytes del tag Poly1305)."
- name: aad
desc: "Datos autenticados adicionales. Debe ser idéntico al aad usado en SealAEAD, o nil si se pasó nil."
output: "Plaintext descifrado, o error si la autenticación falla o la clave tiene longitud incorrecta."
tested: true
tests:
- "round-trip con aad"
- "round-trip sin aad (nil)"
- "error con clave de longitud incorrecta"
- "error de autenticacion con ciphertext modificado"
- "error de autenticacion con aad distinto"
- "nonces distintos en llamadas sucesivas"
test_file_path: "functions/cybersecurity/e2e_messaging_crypto_test.go"
file_path: "functions/cybersecurity/open_aead.go"
---
## Ejemplo
```go
// nonce y ct vienen de SealAEAD; aad debe reconstruirse igual
aad := []byte("room:sala-general:seq:42")
plaintext, err := cybersecurity.OpenAEAD(key, nonce, ct, aad)
if err != nil {
// mensaje alterado, clave incorrecta o aad distinto — descartar
log.Printf("autenticación fallida: %v", err)
return
}
fmt.Printf("mensaje: %s\n", plaintext)
```
## Cuando usarla
Al recibir un mensaje del bus: después de resolver la room key con OpenKeyBox, llama OpenAEAD para descifrar y verificar integridad. Si devuelve error, el mensaje llegó corrupto o fue alterado en tránsito — descartar siempre, nunca procesar plaintext parcial.
## Gotchas
- El error no distingue entre "clave incorrecta", "nonce incorrecto" y "ciphertext alterado": todos devuelven el mismo error de autenticación por diseño (evita oráculos de padding).
- Si el ciphertext tiene menos de 16 bytes, la función devuelve error antes de intentar descifrar.
- El aad debe ser reconstructible por el receptor de forma independiente (no viaja en el mensaje cifrado).
+32
View File
@@ -0,0 +1,32 @@
package cybersecurity
import (
"fmt"
"golang.org/x/crypto/nacl/box"
)
// OpenKeyBox decrypts a sealed box produced by SealKeyBox using the recipient's X25519 keypair.
// kexPub and kexPriv must each be exactly 32 bytes and correspond to the public key
// passed to SealKeyBox as recipientKexPub.
// Returns an error if decryption or authentication fails.
func OpenKeyBox(kexPub, kexPriv, sealedMsg []byte) ([]byte, error) {
if len(kexPub) != 32 {
return nil, fmt.Errorf("open_key_box: kexPub must be 32 bytes, got %d", len(kexPub))
}
if len(kexPriv) != 32 {
return nil, fmt.Errorf("open_key_box: kexPriv must be 32 bytes, got %d", len(kexPriv))
}
var pub [32]byte
var priv [32]byte
copy(pub[:], kexPub)
copy(priv[:], kexPriv)
plaintext, ok := box.OpenAnonymous(nil, sealedMsg, &pub, &priv)
if !ok {
return nil, fmt.Errorf("open_key_box: decryption failed (authentication error or corrupted message)")
}
return plaintext, nil
}
+58
View File
@@ -0,0 +1,58 @@
---
name: open_key_box
kind: function
lang: go
domain: cybersecurity
version: "1.0.0"
purity: impure
signature: "func OpenKeyBox(kexPub, kexPriv, sealedMsg []byte) ([]byte, error)"
description: "Descifra un sealed box anónimo producido por SealKeyBox usando el par X25519 del destinatario. Devuelve error si la autenticación falla o el mensaje está corrupto."
tags: [messaging, e2e-crypto, crypto, nacl, x25519, sealed-box, key-distribution, e2e-messaging]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports:
- golang.org/x/crypto/nacl/box
params:
- name: kexPub
desc: "Clave pública X25519 del destinatario (exactamente 32 bytes). Debe coincidir con la usada en SealKeyBox."
- name: kexPriv
desc: "Clave privada X25519 del destinatario (exactamente 32 bytes). Viene del campo KexPriv de su Identity."
- name: sealedMsg
desc: "Sealed box producido por SealKeyBox. Mínimo 48 bytes (32 overhead ephemeral + 16 tag)."
output: "Secreto descifrado (ej. room key de 32 bytes), o error si la autenticación falla, el par de claves no coincide, o el mensaje está truncado."
tested: true
tests:
- "round-trip con identidad generada"
- "error con recipientKexPub de longitud incorrecta"
- "error al abrir con clave equivocada"
- "error con mensaje truncado"
test_file_path: "functions/cybersecurity/e2e_messaging_crypto_test.go"
file_path: "functions/cybersecurity/open_key_box.go"
---
## Ejemplo
```go
// Receptor obtiene su Identity del almacén seguro
id, _ := loadIdentityFromSecureStorage()
roomKey, err := cybersecurity.OpenKeyBox(id.KexPub, id.KexPriv, sealedMsgFromServer)
if err != nil {
log.Printf("no se pudo abrir la room key: %v", err)
return
}
// roomKey lista para usar en SealAEAD / OpenAEAD
```
## Cuando usarla
Al recibir una distribución de clave de sala del servidor: llama OpenKeyBox con el par X25519 propio para recuperar la room key simétrica. Después de obtenerla, úsala en OpenAEAD para descifrar los mensajes de esa sala.
## Gotchas
- El error no distingue entre "clave incorrecta" y "mensaje corrupto" por diseño de seguridad.
- Si `sealedMsg` tiene menos de 48 bytes (overhead mínimo del sealed box), la función devuelve error sin intentar descifrar.
- `kexPub` y `kexPriv` deben ser el par correspondiente: pasar la pubkey de otro usuario con la privkey propia siempre falla.
- La room key recuperada es sensible: no logearla ni incluirla en mensajes de error.
+31
View File
@@ -0,0 +1,31 @@
package cybersecurity
import (
"crypto/rand"
"fmt"
"io"
"golang.org/x/crypto/chacha20poly1305"
)
// SealAEAD encrypts plaintext with ChaCha20-Poly1305, returning a random nonce and ciphertext.
// key must be exactly 32 bytes. aad (additional authenticated data) may be nil.
// The returned nonce must be stored alongside the ciphertext and passed to OpenAEAD.
func SealAEAD(key, plaintext, aad []byte) (nonce, ciphertext []byte, err error) {
if len(key) != chacha20poly1305.KeySize {
return nil, nil, fmt.Errorf("seal_aead: key must be %d bytes, got %d", chacha20poly1305.KeySize, len(key))
}
aead, err := chacha20poly1305.New(key)
if err != nil {
return nil, nil, fmt.Errorf("seal_aead: create cipher: %w", err)
}
nonce = make([]byte, aead.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, nil, fmt.Errorf("seal_aead: generate nonce: %w", err)
}
ciphertext = aead.Seal(nil, nonce, plaintext, aad)
return nonce, ciphertext, nil
}
+60
View File
@@ -0,0 +1,60 @@
---
name: seal_aead
kind: function
lang: go
domain: cybersecurity
version: "1.0.0"
purity: impure
signature: "func SealAEAD(key, plaintext, aad []byte) (nonce, ciphertext []byte, err error)"
description: "Cifra plaintext con ChaCha20-Poly1305 usando una clave simétrica de 32 bytes. Genera un nonce aleatorio por llamada. Admite datos autenticados adicionales (AAD) para vincular contexto al cifrado sin cifrarlo."
tags: [messaging, e2e-crypto, crypto, aead, chacha20poly1305, symmetric, e2e-messaging]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports:
- crypto/rand
- golang.org/x/crypto/chacha20poly1305
params:
- name: key
desc: "Clave simétrica de exactamente 32 bytes (256 bits). Típicamente la room key distribuida con SealKeyBox."
- name: plaintext
desc: "Bytes a cifrar. Puede ser vacío."
- name: aad
desc: "Datos autenticados adicionales (AAD): se autentican pero no se cifran. Útil para room ID, número de secuencia, etc. Puede ser nil."
output: "nonce (12 bytes aleatorios), ciphertext (plaintext cifrado + 16 bytes de tag Poly1305), o error si la clave tiene longitud incorrecta o falla el RNG."
tested: true
tests:
- "round-trip con aad"
- "round-trip sin aad (nil)"
- "error con clave de longitud incorrecta"
- "error de autenticacion con ciphertext modificado"
- "error de autenticacion con aad distinto"
- "nonces distintos en llamadas sucesivas"
test_file_path: "functions/cybersecurity/e2e_messaging_crypto_test.go"
file_path: "functions/cybersecurity/seal_aead.go"
---
## Ejemplo
```go
key := make([]byte, 32) // en producción: room key distribuida con SealKeyBox
aad := []byte("room:sala-general:seq:42")
nonce, ct, err := cybersecurity.SealAEAD(key, []byte("hola sala"), aad)
if err != nil {
log.Fatal(err)
}
// Almacenar nonce junto al ciphertext para descifrar después
```
## Cuando usarla
Al cifrar cada mensaje en una sala del bus: usa la room key de la sala como `key`, incluye el ID de sala y número de secuencia en `aad` para prevenir replay attacks entre salas, y transmite `nonce + ciphertext` juntos al destinatario.
## Gotchas
- El nonce es aleatorio (12 bytes): con una misma key, la probabilidad de colisión de nonces es despreciable para <2^32 mensajes, pero en escenarios de alto volumen considera rotar la room key periódicamente.
- El ciphertext es 16 bytes más largo que el plaintext (tag Poly1305).
- `aad` no viaja cifrado: el destinatario debe reconstruirlo independientemente para verificar. Si aad difiere aunque sea 1 bit, OpenAEAD falla con error de autenticación.
- Nunca reutilizar `(key, nonce)` para dos plaintexts distintos: rompe la confidencialidad de ChaCha20.
+28
View File
@@ -0,0 +1,28 @@
package cybersecurity
import (
"crypto/rand"
"fmt"
"golang.org/x/crypto/nacl/box"
)
// SealKeyBox encrypts secret for a recipient identified by their X25519 public key,
// using an anonymous sealed box (ephemeral sender keypair, no sender authentication).
// Intended for distributing a symmetric room key to a participant.
// recipientKexPub must be exactly 32 bytes.
func SealKeyBox(recipientKexPub, secret []byte) ([]byte, error) {
if len(recipientKexPub) != 32 {
return nil, fmt.Errorf("seal_key_box: recipientKexPub must be 32 bytes, got %d", len(recipientKexPub))
}
var recipientKey [32]byte
copy(recipientKey[:], recipientKexPub)
sealed, err := box.SealAnonymous(nil, secret, &recipientKey, rand.Reader)
if err != nil {
return nil, fmt.Errorf("seal_key_box: %w", err)
}
return sealed, nil
}
+56
View File
@@ -0,0 +1,56 @@
---
name: seal_key_box
kind: function
lang: go
domain: cybersecurity
version: "1.0.0"
purity: impure
signature: "func SealKeyBox(recipientKexPub, secret []byte) ([]byte, error)"
description: "Cifra un secreto (típicamente una room key simétrica) para un destinatario identificado por su clave pública X25519, usando un sealed box anónimo (nacl/box). El emisor no se autentica; usar SignEd25519 por separado si se necesita autenticación del remitente."
tags: [messaging, e2e-crypto, crypto, nacl, x25519, sealed-box, key-distribution, e2e-messaging]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports:
- crypto/rand
- golang.org/x/crypto/nacl/box
params:
- name: recipientKexPub
desc: "Clave pública X25519 del destinatario (exactamente 32 bytes). Viene del campo KexPub de su Identity."
- name: secret
desc: "Bytes a cifrar. Típicamente una room key de 32 bytes, pero puede ser cualquier secreto."
output: "Sealed box cifrado (overhead: 32 bytes de ephemeral pubkey + 16 bytes de tag Poly1305), o error si recipientKexPub no tiene 32 bytes o falla el RNG."
tested: true
tests:
- "round-trip con identidad generada"
- "error con recipientKexPub de longitud incorrecta"
- "error al abrir con clave equivocada"
- "error con mensaje truncado"
test_file_path: "functions/cybersecurity/e2e_messaging_crypto_test.go"
file_path: "functions/cybersecurity/seal_key_box.go"
---
## Ejemplo
```go
// Distribuir room key al usuario al unirse a la sala
roomKey := make([]byte, 32) // generada por el servidor de sala
sealed, err := cybersecurity.SealKeyBox(user.KexPub, roomKey)
if err != nil {
log.Fatal(err)
}
// Enviar sealed al usuario; solo él puede abrirlo con OpenKeyBox
```
## Cuando usarla
Al distribuir una clave simétrica de sala a un nuevo participante: cifra la room key con la KexPub del destinatario antes de transmitirla. El destinatario usa OpenKeyBox para recuperarla. Combinar con SignEd25519 sobre el sealed box si se necesita autenticar que el servidor distribuyó la clave.
## Gotchas
- El sealed box es anónimo: el receptor no puede verificar quién lo generó. Firmar el sealed box con SignEd25519 si la autenticación del emisor importa.
- Overhead fijo: 48 bytes adicionales sobre el secreto (32 ephemeral pubkey + 16 tag).
- El sealed box no puede abrirse sin la clave privada X25519 correspondiente: si el usuario pierde KexPriv, la room key es irrecuperable.
- `recipientKexPub` debe tener exactamente 32 bytes; la función valida y devuelve error claro si no.
+10
View File
@@ -0,0 +1,10 @@
package cybersecurity
import "crypto/ed25519"
// SignEd25519 signs msg with an Ed25519 private key and returns the 64-byte signature.
// priv must be a valid Ed25519 private key (64 bytes as returned by GenerateIdentity or ed25519.GenerateKey).
// This function is pure: same inputs always produce the same output (ed25519 is deterministic).
func SignEd25519(priv, msg []byte) []byte {
return ed25519.Sign(ed25519.PrivateKey(priv), msg)
}
+45
View File
@@ -0,0 +1,45 @@
---
name: sign_ed25519
kind: function
lang: go
domain: cybersecurity
version: "1.0.0"
purity: pure
signature: "func SignEd25519(priv, msg []byte) []byte"
description: "Firma un mensaje con una clave privada Ed25519 y devuelve la firma de 64 bytes. Determinista: mismas entradas producen siempre la misma firma. Sin efectos secundarios ni I/O."
tags: [messaging, e2e-crypto, crypto, ed25519, signing, pure, e2e-messaging]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: ""
imports:
- crypto/ed25519
params:
- name: priv
desc: "Clave privada Ed25519 de 64 bytes. Viene del campo SignPriv de Identity."
- name: msg
desc: "Bytes a firmar. Puede ser cualquier dato: ciphertext, evento, room key distribuida, etc."
output: "Firma Ed25519 de exactamente 64 bytes. Siempre determinista para la misma (priv, msg)."
tested: true
tests:
- "firma y verificacion exitosa"
- "firma es determinista (misma entrada, misma firma)"
- "falla con mensaje modificado"
- "falla con clave publica incorrecta"
- "falla con firma corrupta"
test_file_path: "functions/cybersecurity/e2e_messaging_crypto_test.go"
file_path: "functions/cybersecurity/sign_ed25519.go"
---
## Ejemplo
```go
// Firmar el ciphertext de un mensaje antes de transmitirlo
sig := cybersecurity.SignEd25519(id.SignPriv, ciphertext)
// Transmitir ciphertext + sig; el receptor verifica con VerifyEd25519
```
## Cuando usarla
Después de cifrar un mensaje con SealAEAD: firma el ciphertext (no el plaintext) con tu SignPriv para que el receptor pueda verificar la autoría con VerifyEd25519. También útil para firmar eventos de control del bus (rotación de clave, join/leave de sala).
+10
View File
@@ -0,0 +1,10 @@
package cybersecurity
import "crypto/ed25519"
// VerifyEd25519 reports whether sig is a valid Ed25519 signature of msg under pub.
// pub must be a valid Ed25519 public key (32 bytes as returned by GenerateIdentity).
// Returns true only if the signature is authentic; false on any mismatch or invalid input.
func VerifyEd25519(pub, msg, sig []byte) bool {
return ed25519.Verify(ed25519.PublicKey(pub), msg, sig)
}
+51
View File
@@ -0,0 +1,51 @@
---
name: verify_ed25519
kind: function
lang: go
domain: cybersecurity
version: "1.0.0"
purity: pure
signature: "func VerifyEd25519(pub, msg, sig []byte) bool"
description: "Verifica una firma Ed25519 sobre un mensaje usando la clave pública del firmante. Devuelve true solo si la firma es auténtica. Sin efectos secundarios ni I/O."
tags: [messaging, e2e-crypto, crypto, ed25519, signing, pure, e2e-messaging]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: ""
imports:
- crypto/ed25519
params:
- name: pub
desc: "Clave pública Ed25519 de 32 bytes del firmante. Viene del campo SignPub de su Identity."
- name: msg
desc: "Mensaje original que fue firmado. Debe ser idéntico al pasado a SignEd25519."
- name: sig
desc: "Firma de 64 bytes producida por SignEd25519."
output: "true si la firma es válida para (pub, msg). false en cualquier otro caso: firma incorrecta, pub equivocada, msg alterado, o sig corrupta."
tested: true
tests:
- "firma y verificacion exitosa"
- "firma es determinista (misma entrada, misma firma)"
- "falla con mensaje modificado"
- "falla con clave publica incorrecta"
- "falla con firma corrupta"
test_file_path: "functions/cybersecurity/e2e_messaging_crypto_test.go"
file_path: "functions/cybersecurity/verify_ed25519.go"
---
## Ejemplo
```go
// Receptor verifica autoría antes de descifrar
if !cybersecurity.VerifyEd25519(sender.SignPub, ciphertext, sig) {
log.Println("firma inválida: mensaje descartado")
return
}
// Solo si la firma es válida, descifrar con OpenAEAD
plaintext, err := cybersecurity.OpenAEAD(roomKey, nonce, ciphertext, aad)
```
## Cuando usarla
Al recibir un mensaje del bus: verifica la firma del remitente sobre el ciphertext antes de intentar descifrar. Devuelve false para cualquier fallo de autenticación — nunca procesar un mensaje con firma inválida.
+149
View File
@@ -0,0 +1,149 @@
package infra
import (
"bytes"
"context"
"fmt"
"os/exec"
"sync"
"syscall"
"time"
"github.com/creack/pty"
)
// PTYCaptureIdle launches a command inside a pseudo-terminal (PTY) and captures
// all output until the terminal has been idle for at least idle duration, or
// maxDur has elapsed. Before sending inputs it waits warmup to let the process
// initialize. Between each input step it waits stepDelay.
//
// The returned string is the raw PTY output, ANSI escape sequences included.
// To turn it into plain text: use vt_render_go_tui to reconstruct the 2D screen
// layout for TUIs with absolute cursor positioning (claude, htop), or
// strip_ansi_go_core for sequential, log-like output.
func PTYCaptureIdle(
ctx context.Context,
name string,
args []string,
warmup time.Duration,
inputs []string,
stepDelay time.Duration,
idle time.Duration,
maxDur time.Duration,
) (string, error) {
cmd := exec.CommandContext(ctx, name, args...)
ptmx, err := pty.Start(cmd)
if err != nil {
return "", fmt.Errorf("pty_capture_idle: pty.Start: %w", err)
}
// Set a reasonable terminal size so TUIs render without truncating.
if szErr := pty.Setsize(ptmx, &pty.Winsize{Rows: 40, Cols: 120}); szErr != nil {
// Non-fatal: continue even if resize fails.
_ = szErr
}
var (
mu sync.Mutex
buf bytes.Buffer
lastByte = time.Now()
)
// Reader goroutine: copy PTY output into buf and track last-byte time.
readDone := make(chan struct{})
go func() {
defer close(readDone)
tmp := make([]byte, 4096)
for {
n, rerr := ptmx.Read(tmp)
if n > 0 {
mu.Lock()
buf.Write(tmp[:n])
lastByte = time.Now()
mu.Unlock()
}
if rerr != nil {
// EIO/EOF is normal on Linux when the PTY master is closed
// after the child exits. Not a real error.
return
}
}
}()
start := time.Now()
// Wait for warmup so the TUI/CLI has time to initialize.
select {
case <-time.After(warmup):
case <-ctx.Done():
_ = ptmx.Close()
<-readDone
mu.Lock()
out := buf.String()
mu.Unlock()
return out, fmt.Errorf("pty_capture_idle: context cancelled during warmup: %w", ctx.Err())
}
// Send inputs one by one with stepDelay between them.
for _, in := range inputs {
if _, werr := ptmx.Write([]byte(in)); werr != nil {
// PTY may have closed already; stop sending.
break
}
select {
case <-time.After(stepDelay):
case <-ctx.Done():
goto done
}
}
done:
// Poll until idle or maxDur.
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
mu.Lock()
sinceLastByte := time.Since(lastByte)
mu.Unlock()
elapsed := time.Since(start)
if sinceLastByte >= idle || elapsed >= maxDur {
goto shutdown
}
case <-ctx.Done():
goto shutdown
}
}
shutdown:
// Close the PTY master. This signals EOF to the reader goroutine.
_ = ptmx.Close()
// Graceful shutdown: SIGTERM first, then SIGKILL after 2s.
if cmd.Process != nil {
_ = cmd.Process.Signal(syscall.SIGTERM)
killTimer := time.NewTimer(2 * time.Second)
waitCh := make(chan error, 1)
go func() { waitCh <- cmd.Wait() }()
select {
case <-waitCh:
// Process exited cleanly.
case <-killTimer.C:
_ = cmd.Process.Kill()
<-waitCh
}
killTimer.Stop()
}
<-readDone
mu.Lock()
out := buf.String()
mu.Unlock()
return out, nil
}
+83
View File
@@ -0,0 +1,83 @@
---
name: pty_capture_idle
kind: function
lang: go
domain: infra
version: "1.0.0"
purity: impure
signature: "func PTYCaptureIdle(ctx context.Context, name string, args []string, warmup time.Duration, inputs []string, stepDelay, idle, maxDur time.Duration) (string, error)"
description: "Lanza un comando dentro de un pseudo-terminal (PTY) en memoria y captura todo su output hasta que el terminal permanece idle durante al menos `idle`, o se alcanza `maxDur`. Soporta envío de inputs interactivos tras el warmup inicial. Devuelve el output RAW con secuencias ANSI intactas."
tags: ["terminal", "pty", "tui", "capture", "automation", "terminal-capture"]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports:
- "context"
- "time"
- "github.com/creack/pty"
tested: true
tests:
- "captura output de echo hola"
- "input interactivo con cat"
- "timeout duro con sleep 10"
test_file_path: "functions/infra/pty_capture_idle_test.go"
file_path: "functions/infra/pty_capture_idle.go"
params:
- name: ctx
desc: "Contexto de cancelación. Si se cancela, la función aborta la captura y retorna el output acumulado hasta ese momento."
- name: name
desc: "Nombre o path del ejecutable a lanzar (debe existir en PATH o ser un path absoluto)."
- name: args
desc: "Argumentos posicionales para el ejecutable. Puede ser nil o vacío."
- name: warmup
desc: "Tiempo que la función espera después de arrancar el proceso antes de enviar inputs. Permite que la TUI inicialice su render. Típico: 500ms2s para CLIs lentas."
- name: inputs
desc: "Lista de strings a escribir al PTY en secuencia, uno por vez. Incluir '\\r' al final de cada string para simular Enter. Puede ser nil si solo se quiere observar la salida sin interactuar."
- name: stepDelay
desc: "Espera entre cada input enviado. Permite que la TUI procese y renderice la respuesta de cada paso antes de enviar el siguiente."
- name: idle
desc: "Tiempo sin nuevos bytes en el PTY que se considera 'respuesta terminada'. Cuando el terminal lleva idle sin actividad, la función retorna. Típico: 500ms2s."
- name: maxDur
desc: "Timeout duro desde el inicio de la función. Garantiza que la función retorna aunque la TUI siga emitiendo output indefinidamente (spinners, relojes). Típico: 30s120s."
output: "String con el output completo del terminal desde el arranque hasta la captura, incluyendo secuencias de escape ANSI. Vacío string si el proceso no produjo nada. Error si el PTY no pudo arrancar o el contexto fue cancelado durante warmup."
---
## Ejemplo
```go
// Capturar una sesión de claude con un prompt automático
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
raw, err := PTYCaptureIdle(
ctx,
"claude", nil,
2*time.Second, // warmup: esperar que claude cargue
[]string{"hola, responde PONG\r"}, // inputs: enviar mensaje + Enter
300*time.Millisecond, // stepDelay entre inputs
2*time.Second, // idle: cortar cuando lleve 2s sin output
120*time.Second, // maxDur: timeout duro
)
if err != nil {
log.Fatal(err)
}
// raw contiene el render completo con ANSI; limpiar antes de procesar texto:
// clean := StripANSI(raw) // strip_ansi_go_tui
fmt.Println(raw)
```
## Cuando usarla
Cuando necesites automatizar o scriptear una CLI interactiva que solo entra en modo interactivo si detecta un TTY real (como `claude`, `vim`, `fzf`, `htop`, `python` REPL, `psql`). El PTY hace creer al proceso que habla con un terminal real, sin abrir ninguna ventana gráfica.
## Gotchas
- **Linux/Unix only.** Usa PTY POSIX (`creack/pty`). No funciona en Windows.
- **Output RAW con ANSI.** El string devuelto contiene secuencias de escape (`\x1b[...m`, cursor moves, etc.). Para convertirlo a texto plano: usa `vt_render_go_tui` (reconstruye el layout 2D — correcto para TUIs con posicionamiento absoluto como `claude` o `htop`) o `strip_ansi_go_core` (para output secuencial tipo log). `strip_ansi` sobre una TUI con layout absoluto deja las palabras pegadas porque los espacios entre columnas eran movimientos de cursor.
- **Idle es heurístico.** Si la TUI hace render periódico (spinners, relojes en pantalla, progress bars continuas), el idle nunca se dispara y la función esperará hasta `maxDur`. Aumentar `maxDur` o matar el spinner antes de capturar.
- **El binario debe existir en PATH** (o usar path absoluto en `name`). La función devuelve error si `pty.Start` falla.
- **EIO/EOF al cerrar PTY es normal en Linux.** El goroutine lector lo absorbe silenciosamente; no se propaga como error.
- **SIGTERM → SIGKILL.** Al terminar la captura, la función envía SIGTERM al proceso y espera 2s antes de SIGKILL. Procesos que ignoran SIGTERM (como `sleep`) se matan limpiamente.
- **Tamaño de terminal fijado a 40×120.** Suficiente para la mayoría de TUIs. Si el render se ve truncado, el llamador puede hacer `pty.Setsize` adicional después de obtener el ptmx (no expuesto por esta función; para casos avanzados, reimplementar con acceso directo al ptmx).
+74
View File
@@ -0,0 +1,74 @@
package infra
import (
"context"
"strings"
"testing"
"time"
)
func TestPTYCaptureIdle(t *testing.T) {
t.Run("captura output de echo hola", func(t *testing.T) {
if testing.Short() {
t.Skip("skip en modo corto")
}
ctx := context.Background()
out, err := PTYCaptureIdle(ctx, "echo", []string{"hola"}, 100*time.Millisecond, nil, 0, 300*time.Millisecond, 5*time.Second)
if err != nil {
t.Fatalf("error inesperado: %v", err)
}
if !strings.Contains(out, "hola") {
t.Errorf("se esperaba 'hola' en el output, got: %q", out)
}
})
t.Run("input interactivo con cat", func(t *testing.T) {
if testing.Short() {
t.Skip("skip en modo corto: timing sensible en CI")
}
ctx := context.Background()
// cat repite stdin a stdout via PTY; el PTY hace echo del input ademas.
// "ping\r" simula Enter; la palabra "ping" debe aparecer en el output.
out, err := PTYCaptureIdle(
ctx,
"cat", nil,
200*time.Millisecond,
[]string{"ping\r"},
100*time.Millisecond,
500*time.Millisecond,
5*time.Second,
)
if err != nil {
t.Fatalf("error inesperado: %v", err)
}
if !strings.Contains(out, "ping") {
t.Errorf("se esperaba 'ping' en el output, got: %q", out)
}
})
t.Run("timeout duro con sleep 10", func(t *testing.T) {
if testing.Short() {
t.Skip("skip en modo corto: espera ~1s de timeout")
}
ctx := context.Background()
start := time.Now()
_, err := PTYCaptureIdle(
ctx,
"sleep", []string{"10"},
50*time.Millisecond,
nil,
0,
600*time.Millisecond,
1*time.Second,
)
elapsed := time.Since(start)
if err != nil {
// Un error de señal/exit es esperado; no falla el test.
t.Logf("error (esperado al matar sleep): %v", err)
}
// La función debe retornar en menos de 3s, no esperar los 10s del sleep.
if elapsed >= 3*time.Second {
t.Errorf("la función tardó %v, se esperaba < 3s", elapsed)
}
})
}
+176
View File
@@ -0,0 +1,176 @@
package infra
import (
"bytes"
"context"
"fmt"
"os/exec"
"sync"
"syscall"
"time"
"github.com/creack/pty"
)
// PTYCaptureStream launches a command inside a pseudo-terminal (PTY) and
// streams periodic snapshots of the accumulated output through a channel.
// Unlike PTYCaptureIdle, which returns the full output at the end,
// PTYCaptureStream emits the ENTIRE buffer accumulated so far on every
// snapshotInterval tick — allowing callers to observe the terminal render
// while the process is still running.
//
// The returned channel is closed when capture ends (idle/maxDur/ctx cancel).
// The last value sent before closing is always a final snapshot of the
// complete buffer, regardless of tick alignment.
//
// Callers MUST drain the channel or cancel ctx to avoid blocking the
// internal goroutine. Error is returned only if pty.Start fails.
func PTYCaptureStream(
ctx context.Context,
name string,
args []string,
warmup time.Duration,
inputs []string,
stepDelay time.Duration,
snapshotInterval time.Duration,
idle time.Duration,
maxDur time.Duration,
) (<-chan string, error) {
cmd := exec.CommandContext(ctx, name, args...)
ptmx, err := pty.Start(cmd)
if err != nil {
return nil, fmt.Errorf("pty_capture_stream: pty.Start: %w", err)
}
// Set a reasonable terminal size so TUIs render without truncating.
if szErr := pty.Setsize(ptmx, &pty.Winsize{Rows: 40, Cols: 120}); szErr != nil {
// Non-fatal: continue even if resize fails.
_ = szErr
}
var (
mu sync.Mutex
buf bytes.Buffer
lastByte = time.Now()
)
// Reader goroutine: copy PTY output into buf and track last-byte time.
readDone := make(chan struct{})
go func() {
defer close(readDone)
tmp := make([]byte, 4096)
for {
n, rerr := ptmx.Read(tmp)
if n > 0 {
mu.Lock()
buf.Write(tmp[:n])
lastByte = time.Now()
mu.Unlock()
}
if rerr != nil {
// EIO/EOF is normal on Linux when the PTY master is closed
// after the child exits. Not a real error.
return
}
}
}()
ch := make(chan string, 16)
// snapshot returns a copy of the current buffer contents.
snapshot := func() string {
mu.Lock()
s := buf.String()
mu.Unlock()
return s
}
// send emits a snapshot to ch, respecting ctx cancellation.
send := func(s string) bool {
select {
case ch <- s:
return true
case <-ctx.Done():
return false
}
}
// Conducting goroutine: handles warmup, inputs, periodic snapshots,
// idle/maxDur detection, and shutdown.
go func() {
defer func() {
// Shutdown: close PTY master, SIGTERM → SIGKILL, wait reader.
_ = ptmx.Close()
if cmd.Process != nil {
_ = cmd.Process.Signal(syscall.SIGTERM)
killTimer := time.NewTimer(2 * time.Second)
waitCh := make(chan error, 1)
go func() { waitCh <- cmd.Wait() }()
select {
case <-waitCh:
// Process exited cleanly.
case <-killTimer.C:
_ = cmd.Process.Kill()
<-waitCh
}
killTimer.Stop()
}
<-readDone
// Final snapshot — always emitted so consumers get the complete state.
send(snapshot())
close(ch)
}()
start := time.Now()
// Wait for warmup so the TUI/CLI has time to initialize.
select {
case <-time.After(warmup):
case <-ctx.Done():
return
}
// Send inputs one by one with stepDelay between them.
for _, in := range inputs {
if _, werr := ptmx.Write([]byte(in)); werr != nil {
// PTY may have closed already; stop sending.
break
}
select {
case <-time.After(stepDelay):
case <-ctx.Done():
return
}
}
// Main loop: emit snapshots on ticker, cut on idle or maxDur.
ticker := time.NewTicker(snapshotInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
// Emit current accumulated snapshot.
if !send(snapshot()) {
return
}
// Check termination conditions.
mu.Lock()
sinceLastByte := time.Since(lastByte)
mu.Unlock()
elapsed := time.Since(start)
if sinceLastByte >= idle || elapsed >= maxDur {
return
}
case <-ctx.Done():
return
}
}
}()
return ch, nil
}
+100
View File
@@ -0,0 +1,100 @@
---
name: pty_capture_stream
kind: function
lang: go
domain: infra
version: "1.0.0"
purity: impure
signature: "func PTYCaptureStream(ctx context.Context, name string, args []string, warmup time.Duration, inputs []string, stepDelay, snapshotInterval, idle, maxDur time.Duration) (<-chan string, error)"
description: "Lanza un comando dentro de un pseudo-terminal (PTY) y emite snapshots acumulativos del buffer de output a través de un canal, en intervalos regulares. Cada snapshot es el contenido RAW completo del PTY hasta ese instante (ANSI incluido). Permite hacer streaming del render de una TUI mientras sigue generando, sin esperar al final."
tags: ["terminal", "pty", "tui", "capture", "automation", "streaming", "terminal-capture"]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports:
- "context"
- "time"
- "github.com/creack/pty"
tested: true
tests:
- "snapshots crecientes con pausas"
- "snapshot final siempre presente"
- "timeout duro con sleep 10"
test_file_path: "functions/infra/pty_capture_stream_test.go"
file_path: "functions/infra/pty_capture_stream.go"
params:
- name: ctx
desc: "Contexto de cancelación. Si se cancela, la goroutine interna aborta, emite el snapshot final y cierra el canal."
- name: name
desc: "Nombre o path del ejecutable a lanzar (debe existir en PATH o ser un path absoluto)."
- name: args
desc: "Argumentos posicionales para el ejecutable. Puede ser nil o vacío."
- name: warmup
desc: "Tiempo que se espera después de arrancar el proceso antes de enviar inputs. Permite que la TUI inicialice su render. Típico: 500ms4s para CLIs lentas como claude."
- name: inputs
desc: "Lista de strings a escribir al PTY en secuencia. Incluir '\\r' al final para simular Enter. Puede ser nil si solo se quiere observar sin interactuar."
- name: stepDelay
desc: "Espera entre cada input enviado. Permite que la TUI procese y renderice la respuesta de cada paso antes de enviar el siguiente."
- name: snapshotInterval
desc: "Cada cuánto tiempo se emite un snapshot del buffer acumulado al canal. Controla la frecuencia de actualización del streaming. Valores recomendados: 100ms300ms. Por debajo de 50ms genera mucho ruido y CPU innecesario."
- name: idle
desc: "Tiempo sin nuevos bytes en el PTY que se considera 'respuesta terminada'. Cuando el terminal lleva este tiempo sin actividad, la captura finaliza. Típico: 2s4s para claude, 500ms para CLIs rápidas."
- name: maxDur
desc: "Timeout duro desde el inicio de la función. Garantiza que el canal se cierra aunque la TUI siga emitiendo (spinners, relojes, progress bars). Típico: 60s120s para prompts de claude."
output: "Canal de strings (<-chan string). Cada string es el output RAW acumulado del terminal desde el arranque hasta ese instante, con secuencias ANSI intactas (no deltas). El canal se cierra cuando termina la captura; el último valor enviado antes del cierre es siempre el snapshot final completo. Error si pty.Start falla al arrancar el proceso."
---
## Ejemplo
```go
// Streaming de una sesión claude: ver la respuesta formarse en tiempo real.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
ch, err := PTYCaptureStream(
ctx,
"claude", nil,
4*time.Second, // warmup: esperar que claude cargue
[]string{"hola, responde PONG\r"}, // inputs: enviar mensaje + Enter
300*time.Millisecond, // stepDelay entre inputs
150*time.Millisecond, // snapshotInterval: snapshot cada 150ms
4*time.Second, // idle: cortar cuando lleve 4s sin output
120*time.Second, // maxDur: timeout duro
)
if err != nil {
log.Fatal(err)
}
var lastRender string
for raw := range ch {
// Aplicar VT render para reconstruir la pantalla 2D desde ANSI.
screen := VTRender(raw) // vt_render_go_tui
// Parsear el estado actual de la respuesta de claude.
resp := ParseClaudeTUI(screen) // parse_claude_tui_go_tui
if resp.Response != lastRender {
fmt.Printf("\r[streaming] %s", resp.Response)
lastRender = resp.Response
}
}
// Al salir del for, el canal está cerrado: captura terminada.
fmt.Println("\n[done]", lastRender)
```
## Cuando usarla
Cuando quieras observar el render de una TUI **mientras sigue generando** — por ejemplo, ver la respuesta de `claude` formarse en tiempo real en lugar de esperar al final. Cada snapshot del canal es el estado completo de la pantalla en ese instante; aplica `vt_render_go_tui` + `parse_claude_tui_go_tui` para extraer texto interpretado de cada frame.
Para captura one-shot (solo quieres el output final), usa `pty_capture_idle_go_infra` — más simple, sin goroutina consumidora.
## Gotchas
- **Linux/Unix only.** Usa PTY POSIX (`creack/pty`). No funciona en Windows.
- **Snapshots ACUMULATIVOS, no deltas.** Cada string del canal es el buffer completo desde el inicio, no solo los bytes nuevos. Para calcular lo nuevo en cada tick: `delta := snapshot[len(prevSnapshot):]` — o usa `text_prefix_delta_go_core` si existe. El consumidor decide si quiere el frame completo o el incremento.
- **El consumidor DEBE drenar el canal o cancelar ctx.** Si el canal (capacidad 16) se llena y el consumidor deja de leer, la goroutina interna se bloquea. Patrón seguro: `for range ch {}` en goroutina separada si no se necesita el contenido.
- **La TUI re-renderiza el frame entero.** El buffer crudo crece monotónicamente en bytes, pero el render VT interpretado puede no ser monótono (la TUI puede limpiar la pantalla y re-dibujar). Comparar `VTRender(snapshot)` frame a frame para detectar cambios reales.
- **snapshotInterval < 50ms genera ruido.** El output ANSI de una TUI activa puede cambiar miles de veces por segundo; muestrear muy rápido satura el canal con frames casi idénticos y consume CPU innecesariamente.
- **Idle es heurístico.** Si la TUI tiene spinners o progress bars que emiten bytes continuamente, `idle` nunca se dispara y la función espera hasta `maxDur`. Subir `maxDur` o detener el spinner antes de capturar.
- **EIO/EOF al cerrar PTY es normal en Linux.** El goroutine lector lo absorbe silenciosamente.
- **SIGTERM → SIGKILL.** Al terminar, se envía SIGTERM y se espera 2s antes de SIGKILL.
+120
View File
@@ -0,0 +1,120 @@
package infra
import (
"context"
"strings"
"testing"
"time"
)
func TestPTYCaptureStream(t *testing.T) {
t.Run("snapshots crecientes con pausas", func(t *testing.T) {
if testing.Short() {
t.Skip("skip en modo corto: timing sensible")
}
ctx := context.Background()
// bash -lc imprime A, pausa 0.3s, B, pausa 0.3s, C, pausa 0.3s.
// Con snapshotInterval 100ms e idle 400ms debería recibir varios snapshots
// y el último debe contener A, B y C.
ch, err := PTYCaptureStream(
ctx,
"bash", []string{"-lc", "printf A; sleep 0.3; printf B; sleep 0.3; printf C; sleep 0.3"},
50*time.Millisecond, // warmup
nil, // inputs
0, // stepDelay
100*time.Millisecond, // snapshotInterval
400*time.Millisecond, // idle
5*time.Second, // maxDur
)
if err != nil {
t.Fatalf("error inesperado al arrancar: %v", err)
}
var snapshots []string
for s := range ch {
snapshots = append(snapshots, s)
}
if len(snapshots) < 2 {
t.Errorf("se esperaban >=2 snapshots, got %d", len(snapshots))
}
// Snapshots deben ser acumulativos (monótonos en longitud).
for i := 1; i < len(snapshots); i++ {
if len(snapshots[i]) < len(snapshots[i-1]) {
t.Errorf("snapshot[%d] len=%d < snapshot[%d] len=%d — no acumulativo",
i, len(snapshots[i]), i-1, len(snapshots[i-1]))
}
}
// El último snapshot debe contener A, B y C.
last := snapshots[len(snapshots)-1]
for _, want := range []string{"A", "B", "C"} {
if !strings.Contains(last, want) {
t.Errorf("último snapshot no contiene %q: %q", want, last)
}
}
})
t.Run("snapshot final siempre presente", func(t *testing.T) {
if testing.Short() {
t.Skip("skip en modo corto")
}
ctx := context.Background()
// Output instantáneo; con idle 300ms el canal cierra rápido.
ch, err := PTYCaptureStream(
ctx,
"bash", []string{"-lc", "printf HOLA"},
50*time.Millisecond,
nil,
0,
150*time.Millisecond, // snapshotInterval
300*time.Millisecond, // idle
5*time.Second,
)
if err != nil {
t.Fatalf("error inesperado: %v", err)
}
var last string
for s := range ch {
last = s
}
if !strings.Contains(last, "HOLA") {
t.Errorf("último snapshot no contiene 'HOLA': %q", last)
}
})
t.Run("timeout duro con sleep 10", func(t *testing.T) {
if testing.Short() {
t.Skip("skip en modo corto: espera ~1s de timeout")
}
ctx := context.Background()
start := time.Now()
ch, err := PTYCaptureStream(
ctx,
"sleep", []string{"10"},
50*time.Millisecond,
nil,
0,
200*time.Millisecond, // snapshotInterval
600*time.Millisecond, // idle
1*time.Second, // maxDur duro en 1s
)
if err != nil {
t.Fatalf("error inesperado al arrancar: %v", err)
}
// Drenar completamente el canal.
for range ch {
}
elapsed := time.Since(start)
// La función debe retornar en menos de 3s, no esperar los 10s del sleep.
if elapsed >= 3*time.Second {
t.Errorf("la función tardó %v, se esperaba < 3s", elapsed)
}
})
}
+390
View File
@@ -0,0 +1,390 @@
package tui
import (
"regexp"
"strings"
"unicode"
)
// Unicode markers used by the Claude Code TUI.
const (
markerUser = '' // U+276F — user prompt
markerAssistant = '●' // U+25CF — assistant response / tool call
markerToolResult = '⎿' // U+23BF — tool result
markerProgress = '✻' // U+273B — progress indicator (ignore)
markerBoxTL = '╭' // U+256D — top-left box corner (banner start)
markerBoxBL = '╰' // U+2570 — bottom-left box corner (banner end)
markerBoxBR = '╯' // U+256F — bottom-right box corner (banner end)
markerHRule = '─' // U+2500 — horizontal rule
)
// reToolUse matches "Identifier(anything)" — a tool_use line.
var reToolUse = regexp.MustCompile(`^([A-Za-z_][A-Za-z0-9_]*)\((.*)\)\s*$`)
// reProgress matches Claude's generation status/spinner line by its stable
// signature: "(Ns … tokens" or "esc to interrupt". Used when the line still
// carries that suffix, e.g. "✽ Whatchamacalliting… (2s · ↓ 1 tokens · esc to interrupt)".
var reProgress = regexp.MustCompile(`\(\d+s\b[^)]*tokens?\b|esc to interrupt`)
// reSpinner matches the spinner line by STRUCTURE rather than by its (infinite,
// ever-changing) gerund word: a non-alphanumeric glyph (✻ ✽ ✢ ✶ ✺ …) followed by
// a single word and a horizontal ellipsis, e.g. "✽ Forging…" or "✶ Puzzling…".
// This catches early frames that don't yet show the "(Ns · tokens)" suffix. The
// caller guards known turn markers (●//⎿) so a legitimate answer ending in "…"
// is not misclassified.
var reSpinner = regexp.MustCompile(`^\s*[^\p{L}\p{N}\s]\s+\p{L}[\p{L}'\-]*…`)
// ClaudeTurnRole classifies each turn/block extracted from the screen.
type ClaudeTurnRole string
const (
// ClaudeTurnUser is a message typed by the user (line starting with " ").
ClaudeTurnUser ClaudeTurnRole = "user"
// ClaudeTurnAssistant is a response block from the assistant.
ClaudeTurnAssistant ClaudeTurnRole = "assistant"
// ClaudeTurnToolUse is a tool invocation "● ToolName(args)".
ClaudeTurnToolUse ClaudeTurnRole = "tool_use"
// ClaudeTurnToolResult is a result line "⎿ ..." following a tool_use.
ClaudeTurnToolResult ClaudeTurnRole = "tool_result"
)
// ClaudeTurn is a single conversation block extracted from the rendered screen.
type ClaudeTurn struct {
Role ClaudeTurnRole `json:"role"`
Text string `json:"text"` // textual content (multiline joined with \n)
ToolName string `json:"tool_name,omitempty"` // only for tool_use
}
// ClaudeTUIParse is the result of parsing one captured Claude TUI screen.
type ClaudeTUIParse struct {
Turns []ClaudeTurn `json:"turns"` // all visible turns in order
Answer string `json:"answer"` // assistant reply to the last user turn (like `claude -p`)
}
// ParseClaudeTUI parses the rendered text of a Claude Code TUI screen and
// extracts the conversation turns and the final assistant answer.
//
// The screen is expected to be the output of VTRender applied to a PTY
// capture of the claude CLI. Heuristics handle the welcome banner, status
// bar, progress lines and multi-line continuations.
func ParseClaudeTUI(screen string) ClaudeTUIParse {
lines := strings.Split(screen, "\n")
// --- Step 1: strip the welcome banner (box drawn with ╭...╰/╯) ---
lines = stripBanner(lines)
// --- Step 2: strip the status bar at the bottom ---
lines = stripStatusBar(lines)
// --- Step 3: collect turns from the remaining lines ---
turns := extractTurns(lines)
// --- Step 4: compute Answer from turns ---
answer := computeAnswer(turns)
return ClaudeTUIParse{Turns: turns, Answer: answer}
}
// stripBanner removes the welcome banner block from the top of the lines
// slice. The banner is a Unicode box that starts with a line containing ╭
// and ends with a line containing ╰ or ╯.
func stripBanner(lines []string) []string {
// Find a banner start (╭) in the first ~15 lines.
startIdx := -1
for i := 0; i < len(lines) && i < 15; i++ {
if strings.ContainsRune(lines[i], markerBoxTL) {
startIdx = i
break
}
}
if startIdx < 0 {
return lines
}
// Find the matching close (╰ or ╯) after the start.
for i := startIdx; i < len(lines); i++ {
if strings.ContainsRune(lines[i], markerBoxBL) || strings.ContainsRune(lines[i], markerBoxBR) {
return lines[i+1:]
}
}
return lines
}
// isHRule returns true when the line consists mostly of ─ (U+2500) characters
// — at least 40 of them and the line has no other significant content.
func isHRule(line string) bool {
count := 0
for _, r := range line {
if r == markerHRule {
count++
}
}
return count >= 40
}
// isStatusBarLine returns true for lines that belong to the Claude status bar
// (CTX:, IN:, OUT:, Total:, Limits:, $, "← for agents", etc.).
func isStatusBarLine(line string) bool {
trimmed := strings.TrimSpace(line)
if trimmed == "" {
return false
}
prefixes := []string{
"CTX:", "IN:", "OUT:", "Total:", "Limits:", "$", "← for agents",
}
for _, p := range prefixes {
if strings.Contains(trimmed, p) {
return true
}
}
return false
}
// stripStatusBar removes the status bar at the bottom of the lines slice.
// Strategy: scan from the bottom upward. The footer looks like:
//
// <hrule>
// (empty prompt)
// <hrule>
// <status lines with CTX: / $0.xxx / ← for agents ...>
//
// We look for the LAST hrule that is followed by an empty-prompt line and
// another hrule, and discard everything from that hrule onward.
// Additionally, any trailing status-bar-flavored lines are dropped first.
func stripStatusBar(lines []string) []string {
if len(lines) == 0 {
return lines
}
// Trim trailing blank lines first.
end := len(lines)
for end > 0 && strings.TrimSpace(lines[end-1]) == "" {
end--
}
lines = lines[:end]
// Remove explicit status-bar lines from the bottom.
for len(lines) > 0 && isStatusBarLine(lines[len(lines)-1]) {
lines = lines[:len(lines)-1]
}
// Now find the pattern: hrule / empty- / hrule and cut there.
// Scan from the bottom upward.
for i := len(lines) - 1; i >= 2; i-- {
if !isHRule(lines[i]) {
continue
}
// Check that lines[i-1] is the empty prompt "" (optional surrounding spaces).
mid := strings.TrimSpace(lines[i-1])
if mid != string([]rune{markerUser}) && mid != string([]rune{markerUser, ' '}) {
// Also allow a completely empty line (prompt area can be blank).
if mid != "" {
continue
}
}
// Check lines[i-2] is also an hrule.
if isHRule(lines[i-2]) {
// Cut from lines[i-2] onward.
lines = lines[:i-2]
break
}
}
// Trim trailing blank lines again after stripping.
for len(lines) > 0 && strings.TrimSpace(lines[len(lines)-1]) == "" {
lines = lines[:len(lines)-1]
}
return lines
}
// firstRune returns the first non-space rune in s, or 0 if s is blank.
func firstRune(s string) rune {
for _, r := range s {
if !unicode.IsSpace(r) {
return r
}
}
return 0
}
// isMarkerLine returns true when the line starts with one of the recognised
// turn markers (, ●, ⎿, ✻).
func isMarkerLine(line string) bool {
r := firstRune(line)
return r == markerUser || r == markerAssistant || r == markerToolResult || r == markerProgress
}
// isProgressLine reports whether the line is a Claude generation status/spinner
// line (the animated "✻/✽ Word… (Ns · ↓ N tokens · esc to interrupt)" indicator).
// The glyph and the gerund word change on every frame, so it is detected by
// structure/signature, never by the specific word. These lines are noise and must
// never be folded into an assistant answer — critical when capturing frames
// mid-generation (streaming), where a different "loading" word appears each tick.
func isProgressLine(line string) bool {
r := firstRune(line)
if r == markerProgress {
return true
}
// Known turn markers are never progress, even if they end in "…".
if r == markerUser || r == markerAssistant || r == markerToolResult {
return false
}
return reProgress.MatchString(line) || reSpinner.MatchString(line)
}
// isBreakLine reports whether the line should end an assistant/user/tool
// continuation: either a turn marker or a progress/spinner line.
func isBreakLine(line string) bool {
return isMarkerLine(line) || isProgressLine(line)
}
// textAfterMarker returns the text that follows the first occurrence of
// marker in line, trimmed of leading spaces.
func textAfterMarker(line string, marker rune) string {
idx := strings.IndexRune(line, marker)
if idx < 0 {
return ""
}
rest := line[idx+len(string(marker)):]
return strings.TrimLeft(rest, " ")
}
// extractTurns scans lines and groups them into ClaudeTurn slices.
func extractTurns(lines []string) []ClaudeTurn {
var turns []ClaudeTurn
i := 0
for i < len(lines) {
line := lines[i]
// Progress/spinner lines are noise in any position — skip early so they
// are never folded into an assistant continuation (matters for streaming).
if isProgressLine(line) {
i++
continue
}
r := firstRune(line)
switch r {
case markerProgress:
// ✻ lines are noise — skip (also covered by isProgressLine above).
i++
case markerUser:
text := textAfterMarker(line, markerUser)
if text == "" {
// Empty prompt — skip.
i++
continue
}
// Collect continuation lines (indented, non-marker, non-empty).
i++
for i < len(lines) {
cont := lines[i]
if isBreakLine(cont) || strings.TrimSpace(cont) == "" {
break
}
text += "\n" + strings.TrimRight(cont, " ")
i++
}
turns = append(turns, ClaudeTurn{Role: ClaudeTurnUser, Text: strings.TrimRight(text, " ")})
case markerAssistant:
body := textAfterMarker(line, markerAssistant)
i++
// Determine if this is a tool_use or assistant text.
if m := reToolUse.FindStringSubmatch(body); m != nil {
// tool_use — do NOT collect continuation lines.
turns = append(turns, ClaudeTurn{
Role: ClaudeTurnToolUse,
Text: body,
ToolName: m[1],
})
} else {
// assistant text — collect continuation lines.
for i < len(lines) {
cont := lines[i]
if isBreakLine(cont) {
break
}
trimmed := strings.TrimSpace(cont)
if trimmed == "" {
// A single blank line may separate paragraphs; peek ahead.
// If the next non-blank line is also a continuation, keep it.
j := i + 1
for j < len(lines) && strings.TrimSpace(lines[j]) == "" {
j++
}
if j < len(lines) && !isBreakLine(lines[j]) {
// Include the blank line(s) as paragraph separator.
body += "\n"
i = j
continue
}
break
}
body += "\n" + strings.TrimRight(cont, " ")
i++
}
turns = append(turns, ClaudeTurn{
Role: ClaudeTurnAssistant,
Text: strings.TrimRight(body, " \n"),
})
}
case markerToolResult:
text := textAfterMarker(line, markerToolResult)
// Also accept └ as alias (some terminals substitute).
if text == "" {
text = textAfterMarker(line, '└')
}
i++
// Collect continuation lines for the tool result.
for i < len(lines) {
cont := lines[i]
if isBreakLine(cont) || strings.TrimSpace(cont) == "" {
break
}
text += "\n" + strings.TrimRight(cont, " ")
i++
}
turns = append(turns, ClaudeTurn{
Role: ClaudeTurnToolResult,
Text: strings.TrimRight(text, " "),
})
default:
// Blank or unrecognised line — skip.
i++
}
}
return turns
}
// computeAnswer finds the last user turn and concatenates all assistant
// (non-tool_use, non-tool_result) turns that follow it.
// If there is no user turn, concatenates all assistant turns.
func computeAnswer(turns []ClaudeTurn) string {
lastUserIdx := -1
for i, t := range turns {
if t.Role == ClaudeTurnUser {
lastUserIdx = i
}
}
var parts []string
start := 0
if lastUserIdx >= 0 {
start = lastUserIdx + 1
}
for _, t := range turns[start:] {
if t.Role == ClaudeTurnAssistant {
parts = append(parts, t.Text)
}
}
return strings.TrimSpace(strings.Join(parts, "\n"))
}
+67
View File
@@ -0,0 +1,67 @@
---
name: parse_claude_tui
kind: function
lang: go
domain: tui
version: "1.0.0"
purity: pure
signature: "func ParseClaudeTUI(screen string) ClaudeTUIParse"
description: "Parsea el texto renderizado de la pantalla de la TUI de Claude Code y extrae los turnos de la conversación (user, assistant, tool_use, tool_result) y la respuesta final del asistente. Equivalente a lo que devolvería `claude -p` pero operando sobre el render visual."
tags: [terminal-capture, claude, tui, parse, conversation]
uses_functions:
- vt_render_go_tui
uses_types:
- claude_turn_go_tui
- claude_tui_parse_go_tui
returns:
- claude_tui_parse_go_tui
returns_optional: false
error_type: ""
imports: []
params:
- name: screen
desc: "Texto renderizado de la pantalla de la TUI de Claude Code, producido por VTRender(raw, rows, cols). Debe incluir el contenido visible completo: banner opcional, conversación y status bar opcional."
output: "ClaudeTUIParse con los turnos visibles en orden (Role, Text, ToolName) y Answer — la concatenación de los bloques assistant que siguen al último turno user, equivalente al output de `claude -p`."
tested: true
tests:
- "golden screen — banner + status bar + single Q&A"
- "multiline assistant response"
- "tool_use + tool_result + final assistant text"
- "multi-turn — answer from last user only"
- "no banner no status bar — minimal screen"
- "determinism — same input produces same output"
test_file_path: "functions/tui/parse_claude_tui_test.go"
file_path: "functions/tui/parse_claude_tui.go"
---
## Ejemplo
```go
// Pipeline completo: PTY capture → VTRender → ParseClaudeTUI → usar .Answer
import (
"fmt"
"fn-registry/functions/infra"
"fn-registry/functions/tui"
)
raw, _ := infra.PtyCaptureIdle("claude", []string{}, 40, 220, 8000)
screen := tui.VTRender(raw, 40, 220)
result := tui.ParseClaudeTUI(screen)
fmt.Println(result.Answer) // imprime la respuesta final del asistente
for _, turn := range result.Turns {
fmt.Printf("[%s] %s\n", turn.Role, turn.Text)
}
```
## Cuando usarla
Cuando captures la TUI de `claude` con `pty_capture_idle_go_infra` + `vt_render_go_tui` y necesites extraer la respuesta como dato estructurado (equivalente a `claude -p`) en vez de procesar el render visual crudo. Úsala para agentes que lanzan `claude` como subproceso TUI y quieren leer la respuesta sin requerir modo headless.
## Gotchas
- **Heurístico y dependiente del layout de la TUI de Claude Code**: si Claude cambia los marcadores (``, `●`, `⎿`, `✻`) o el formato del banner/status-bar, el parser puede dejar de funcionar sin aviso.
- **Solo ve lo visible en el grid**: `VTRender` reconstruye únicamente lo que cabe en el terminal emulado (rows × cols). Respuestas largas que hacen scroll hacia arriba se truncan por arriba — no hay scrollback. Para respuestas largas, aumentar `rows` en `VTRender` o usar `claude -p` directamente.
- **tool_use/tool_result best-effort**: la TUI colapsa algunos bloques de herramientas. Los `ToolName` y textos de `tool_result` pueden quedar incompletos si la TUI los trunca con `…`.
- **Answer asume captura post-respuesta**: `PtyCaptureIdle` debe haberse disparado DESPUÉS de que la respuesta terminó de renderizarse (el spinner `✻` desapareció). Si se captura durante el streaming, `Answer` puede estar incompleto.
+214
View File
@@ -0,0 +1,214 @@
package tui
import (
"testing"
)
// goldenScreen is the exact sample screen from the spec.
const goldenScreen = `╭─── Claude Code v2.1.161 ─────────────────────────────────────────────────────────────────────────────────────────────╮
│ │ Tips for getting started │
│ Welcome back Enmanuel! │ Run /init to create a CLAUDE.md file with instructions for Cla… │
│ │ ─────────────────────────────────────────────────────────────── │
│ ▐▛███▜▌ │ What's new │
│ ▝▜█████▛▘ │ ` + "`OTEL_RESOURCE_ATTRIBUTES`" + ` values are now included as labels o… │
│ ▘▘ ▝▝ │ ` + "`claude agents`" + ` rows now show ` + "`done/total`" + ` before the detail w… │
│ Opus 4.8 (1M context) with xh… · Claude Max · │ ` + "`/mcp`" + ` now collapses claude.ai connectors you've never signed … │
│ gutierenmanuel15@gmail.com's Organization │ /release-notes for more │
│ ~/fn_registry │ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
responde unicamente con la palabra PONG, sin explicaciones
● PONG
✻ Crunched for 2s
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Opus 4.8 (1M context) │ CTX: █░░░░░░░░░ 11% (107k/1.0M) │ IN:6k OUT:5 (cache:17k) │ ⎇ master [~4 ?28 ↑1] │ 22:26
$0.565 │ +0/-0 │ Total: ↓107k/↑5 │ Limits: 5h:6% →02:40 │ 7d:11% →Sun 17:00 │ ⏱ 7s │ ~/fn_registry
← for agents`
func TestParseClaudeTUI(t *testing.T) {
t.Run("golden screen — banner + status bar + single Q&A", func(t *testing.T) {
got := ParseClaudeTUI(goldenScreen)
if got.Answer != "PONG" {
t.Errorf("Answer = %q, want %q", got.Answer, "PONG")
}
if len(got.Turns) != 2 {
t.Errorf("len(Turns) = %d, want 2", len(got.Turns))
for i, turn := range got.Turns {
t.Logf(" Turns[%d]: role=%s text=%q", i, turn.Role, turn.Text)
}
return
}
if got.Turns[0].Role != ClaudeTurnUser {
t.Errorf("Turns[0].Role = %q, want %q", got.Turns[0].Role, ClaudeTurnUser)
}
wantUserText := "responde unicamente con la palabra PONG, sin explicaciones"
if got.Turns[0].Text != wantUserText {
t.Errorf("Turns[0].Text = %q, want %q", got.Turns[0].Text, wantUserText)
}
if got.Turns[1].Role != ClaudeTurnAssistant {
t.Errorf("Turns[1].Role = %q, want %q", got.Turns[1].Role, ClaudeTurnAssistant)
}
if got.Turns[1].Text != "PONG" {
t.Errorf("Turns[1].Text = %q, want %q", got.Turns[1].Text, "PONG")
}
})
t.Run("multiline assistant response", func(t *testing.T) {
screen := ` explica brevemente
● linea uno
linea dos`
got := ParseClaudeTUI(screen)
if len(got.Turns) != 2 {
t.Fatalf("len(Turns) = %d, want 2; turns: %+v", len(got.Turns), got.Turns)
}
wantText := "linea uno\nlinea dos"
if got.Turns[1].Text != wantText {
t.Errorf("Turns[1].Text = %q, want %q", got.Turns[1].Text, wantText)
}
if !contains(got.Answer, "linea uno") || !contains(got.Answer, "linea dos") {
t.Errorf("Answer %q should contain both continuation lines", got.Answer)
}
})
t.Run("tool_use + tool_result + final assistant text", func(t *testing.T) {
screen := ` pregunta
● Read(main.go)
⎿ Read 50 lines
● aqui esta el resumen`
got := ParseClaudeTUI(screen)
if len(got.Turns) != 4 {
t.Fatalf("len(Turns) = %d, want 4; turns: %+v", len(got.Turns), got.Turns)
}
if got.Turns[0].Role != ClaudeTurnUser {
t.Errorf("Turns[0].Role = %q", got.Turns[0].Role)
}
if got.Turns[1].Role != ClaudeTurnToolUse {
t.Errorf("Turns[1].Role = %q, want tool_use", got.Turns[1].Role)
}
if got.Turns[1].ToolName != "Read" {
t.Errorf("Turns[1].ToolName = %q, want Read", got.Turns[1].ToolName)
}
if got.Turns[2].Role != ClaudeTurnToolResult {
t.Errorf("Turns[2].Role = %q, want tool_result", got.Turns[2].Role)
}
if got.Turns[3].Role != ClaudeTurnAssistant {
t.Errorf("Turns[3].Role = %q, want assistant", got.Turns[3].Role)
}
// Answer must be ONLY the assistant text, not the tool_use.
if got.Answer != "aqui esta el resumen" {
t.Errorf("Answer = %q, want %q", got.Answer, "aqui esta el resumen")
}
})
t.Run("multi-turn — answer from last user only", func(t *testing.T) {
screen := ` primera pregunta
● primera respuesta
segunda pregunta
● segunda respuesta`
got := ParseClaudeTUI(screen)
if len(got.Turns) != 4 {
t.Fatalf("len(Turns) = %d, want 4; turns: %+v", len(got.Turns), got.Turns)
}
if got.Answer != "segunda respuesta" {
t.Errorf("Answer = %q, want %q", got.Answer, "segunda respuesta")
}
})
t.Run("no banner no status bar — minimal screen", func(t *testing.T) {
screen := " hola\n\n● mundo"
got := ParseClaudeTUI(screen)
if len(got.Turns) != 2 {
t.Fatalf("len(Turns) = %d, want 2; turns: %+v", len(got.Turns), got.Turns)
}
if got.Answer != "mundo" {
t.Errorf("Answer = %q, want %q", got.Answer, "mundo")
}
})
t.Run("determinism — same input produces same output", func(t *testing.T) {
first := ParseClaudeTUI(goldenScreen)
second := ParseClaudeTUI(goldenScreen)
if first.Answer != second.Answer {
t.Errorf("non-deterministic: %q != %q", first.Answer, second.Answer)
}
if len(first.Turns) != len(second.Turns) {
t.Errorf("non-deterministic turns count: %d != %d", len(first.Turns), len(second.Turns))
}
})
}
// TestParseClaudeTUI_Spinner verifies that the generation spinner — which shows a
// DIFFERENT random gerund word on every frame ("Whatchamacalliting", "Forging",
// "Puzzling", "Crunched"...) — is never folded into the answer, regardless of the
// word, the glyph, or whether the "(Ns · tokens)" suffix is present yet.
func TestParseClaudeTUI_Spinner(t *testing.T) {
cases := []struct {
name string
screen string
want string
}{
{
name: "spinner with tokens suffix glued after answer",
screen: " di PONG\n\n● PONG\n\n✽ Whatchamacalliting… (2s · ↓ 1 tokens · esc to interrupt)\n",
want: "PONG",
},
{
name: "spinner early frame, no suffix yet, different word",
screen: " di HOLA\n\n● HOLA\n\n✶ Puzzling…\n",
want: "HOLA",
},
{
name: "classic crunched line",
screen: " x\n\n● respuesta\n\n✻ Crunched for 4s\n",
want: "respuesta",
},
{
name: "spinner BEFORE the answer block (mid-generation snapshot)",
screen: " pregunta\n\n✽ Forging… (1s · ↑ 3 tokens · esc to interrupt)\n\n● respuesta parcial\n",
want: "respuesta parcial",
},
{
name: "assistant line ending in ellipsis is NOT treated as spinner",
screen: " x\n\n● la historia continua…\n",
want: "la historia continua…",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := ParseClaudeTUI(tc.screen)
if got.Answer != tc.want {
t.Errorf("Answer = %q, want %q", got.Answer, tc.want)
}
})
}
}
func contains(s, sub string) bool {
return len(sub) == 0 || (len(s) >= len(sub) && (s == sub ||
len(s) > 0 && containsStr(s, sub)))
}
func containsStr(s, sub string) bool {
for i := 0; i <= len(s)-len(sub); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
}
+54
View File
@@ -0,0 +1,54 @@
package tui
import (
"strings"
"github.com/hinshun/vt10x"
)
// VTRender emulates a terminal of size cols×rows, feeds raw into it,
// and returns the resulting screen as plain text preserving the visual layout.
//
// Unlike strip_ansi which removes escape sequences from sequential output,
// VTRender correctly handles TUIs that use absolute cursor positioning
// (ESC[row;colH, ESC[colG, etc.) by maintaining a 2D grid and reconstructing
// real spaces between columns.
//
// Defaults: rows<=0 → 40, cols<=0 → 120.
// Trailing spaces on each line are trimmed. Trailing empty lines are removed.
func VTRender(raw string, rows, cols int) string {
if rows <= 0 {
rows = 40
}
if cols <= 0 {
cols = 120
}
// Create a fresh terminal emulator for each call — no shared state.
term := vt10x.New(vt10x.WithSize(cols, rows))
term.Write([]byte(raw)) //nolint:errcheck // Write on vt10x never returns a meaningful error
// String() returns all rows joined by '\n', one row per terminal line.
// Each row is exactly `cols` runes wide (padded with NUL/space for empty cells).
raw = term.String()
lines := strings.Split(raw, "\n")
// Trim trailing spaces from every line (cells that were never written
// contain NUL '\x00' in some versions, so we trim both NUL and space).
for i, line := range lines {
// Replace NUL characters (unwritten cells) with spaces first.
line = strings.ReplaceAll(line, "\x00", " ")
lines[i] = strings.TrimRight(line, " ")
}
// Remove trailing empty lines — the TUI probably only used the top portion
// of the grid. Keep intermediate empty lines (real visual separators).
last := len(lines) - 1
for last >= 0 && lines[last] == "" {
last--
}
lines = lines[:last+1]
return strings.Join(lines, "\n")
}
+64
View File
@@ -0,0 +1,64 @@
---
name: vt_render
kind: function
lang: go
domain: tui
version: "1.0.0"
purity: pure
signature: "func VTRender(raw string, rows, cols int) string"
description: "Emula un terminal virtual de tamaño cols×rows, alimenta raw (stream con secuencias ANSI/VT100 incluyendo posicionamiento absoluto de cursor) y devuelve el estado final de la pantalla como texto plano que preserva el layout visual. A diferencia de strip_ansi, reconstruye espacios reales entre columnas posicionadas con movimientos de cursor absolutos."
tags: ["terminal", "vt100", "tui", "render", "ansi", "screen", "terminal-capture"]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: ""
imports:
- "github.com/hinshun/vt10x"
- "strings"
tested: true
tests:
- "layout absoluto basico A y B separados por movimiento de cursor"
- "dos palabras separadas por movimiento de columna no aparecen pegadas"
- "texto multilinea simple con CRLF"
- "trim de filas vacias al final de grid grande"
- "determinismo misma entrada misma salida"
- "defaults rows y cols al pasar cero"
test_file_path: "functions/tui/vt_render_test.go"
file_path: "functions/tui/vt_render.go"
params:
- name: raw
desc: "Stream crudo de bytes de terminal, con secuencias de escape ANSI/VT100 intactas (colores, cursor moves, borrados de línea, scroll). Típicamente la salida de pty_capture_idle_go_infra."
- name: rows
desc: "Número de filas del terminal virtual. Debe coincidir con el tamaño de PTY usado al capturar. Si <=0 usa 40 como default."
- name: cols
desc: "Número de columnas del terminal virtual. Debe coincidir con el ancho de PTY usado al capturar. Si <=0 usa 120 como default."
output: "Texto plano multilínea con el layout visual de la pantalla: espacios reales entre columnas, sin trailing spaces por línea, sin filas vacías finales. Las líneas vacías intermedias se conservan (son separación visual real)."
---
## Ejemplo
```go
// Capturar output crudo de una TUI (ej. claude CLI) con el PTY del mismo tamaño.
raw, _ := pty_capture_idle("claude", []string{"--help"}, 40, 120, 2*time.Second, 10*time.Second)
// Renderizar el grid final como texto plano.
screen := tui.VTRender(raw, 40, 120)
fmt.Println(screen)
// Salida: texto con columnas alineadas, igual a lo que se vería en pantalla.
// Ejemplo real: "foo bar" si foo y bar estaban separados por ESC[10G.
```
## Cuando usarla
Úsala cuando captures el output crudo de una TUI con layout absoluto (claude CLI, htop, dialog, ncurses) y `strip_ansi_go_core` te deje las palabras pegadas (ej. "2newMCPservers"). Contrasta con `strip_ansi_go_core` y `strip_ansi_go_tui`, que sirven para output secuencial tipo logs donde no hay movimientos de cursor absolutos. Si el stream tiene `ESC[row;colH` o `ESC[colG`, este es el correcto.
Librería emuladora usada: `github.com/hinshun/vt10x` (vt10x v0.0.0-20220301184237-5011da428d02). Implementa VT10x completo sin CGO. API: `vt10x.New(vt10x.WithSize(cols, rows))` + `Write([]byte)` + `String()`.
## Gotchas
- **Tamaño debe coincidir**: rows×cols deben ser iguales a los que se usaron al capturar (pty_capture_idle usa 40×120 por defecto). Si no coinciden, el wrapping del texto no cuadra y las columnas se descuadran.
- **Solo texto, sin color**: la función vuelca únicamente los caracteres (rune de cada celda). Los atributos de color se pierden — es texto plano.
- **Solo estado final del grid**: si la TUI hizo scroll durante su ejecución, solo se ve el estado final de las 40 filas visibles. El historial de scroll no está disponible.
- **Emojis y caracteres de doble ancho**: algunos caracteres Unicode (emojis, CJK) ocupan 2 columnas visualmente pero solo 1 celda en el grid de vt10x, lo que puede descuadrar columnas en TUIs que los usan.
- **NUL en celdas vacías**: las celdas no escritas contienen `\x00` en algunas versiones del emulador. La función los reemplaza por espacio antes del trim, pero si el raw contiene NUL intencional, se trataría como espacio.
+114
View File
@@ -0,0 +1,114 @@
package tui
import (
"strings"
"testing"
)
func TestVTRender(t *testing.T) {
t.Run("layout absoluto basico A y B separados por movimiento de cursor", func(t *testing.T) {
// ESC[1;5H mueve el cursor a fila 1 columna 5 (1-indexed).
// Resultado esperado: 'A' en col 1, espacios, 'B' en col 5.
out := VTRender("A\x1b[1;5HB", 2, 10)
lines := strings.Split(out, "\n")
if len(lines) == 0 {
t.Fatalf("resultado vacio")
}
first := lines[0]
if len(first) < 5 {
t.Fatalf("linea demasiado corta: %q", first)
}
if first[0] != 'A' {
t.Errorf("esperaba 'A' en columna 0, got %q en linea %q", string(first[0]), first)
}
if first[4] != 'B' {
t.Errorf("esperaba 'B' en columna 4 (0-indexed), got %q en linea %q", string(first[4]), first)
}
// Verificar que hay espacios entre A y B (no están pegadas).
if strings.Contains(first, "AB") {
t.Errorf("A y B estan pegadas en %q, deberían estar separadas", first)
}
})
t.Run("dos palabras separadas por movimiento de columna no aparecen pegadas", func(t *testing.T) {
// ESC[10G mueve el cursor a la columna 10 (1-indexed) de la línea actual.
out := VTRender("foo\x1b[10Gbar", 2, 20)
lines := strings.Split(out, "\n")
if len(lines) == 0 {
t.Fatalf("resultado vacio")
}
first := lines[0]
if strings.Contains(first, "foobar") {
t.Errorf("foo y bar estan pegadas: %q — esperaba espacios entre ellas", first)
}
if !strings.Contains(first, "foo") {
t.Errorf("no encontre 'foo' en %q", first)
}
if !strings.Contains(first, "bar") {
t.Errorf("no encontre 'bar' en %q", first)
}
// foo en col 0-2, bar en col 9-11 (columna 10 es 0-indexed 9).
if len(first) < 12 {
t.Fatalf("linea demasiado corta para verificar: %q", first)
}
// Debe haber al menos un espacio entre foo y bar.
fooEnd := strings.Index(first, "foo") + 3
barStart := strings.Index(first, "bar")
if barStart <= fooEnd {
t.Errorf("bar empieza en %d pero foo termina en %d — sin separacion en %q", barStart, fooEnd, first)
}
})
t.Run("texto multilinea simple con CRLF", func(t *testing.T) {
out := VTRender("linea1\r\nlinea2", 5, 40)
if !strings.Contains(out, "linea1") {
t.Errorf("no encontre 'linea1' en %q", out)
}
if !strings.Contains(out, "linea2") {
t.Errorf("no encontre 'linea2' en %q", out)
}
lines := strings.Split(out, "\n")
// linea1 y linea2 deben estar en líneas distintas.
found1, found2 := -1, -1
for i, l := range lines {
if strings.Contains(l, "linea1") {
found1 = i
}
if strings.Contains(l, "linea2") {
found2 = i
}
}
if found1 == found2 {
t.Errorf("linea1 y linea2 estan en la misma linea (%d) de la salida: %q", found1, out)
}
})
t.Run("trim de filas vacias al final de grid grande", func(t *testing.T) {
// Input corto en un grid de 40 filas — no debe producir 40 lineas.
out := VTRender("hola", 40, 120)
count := strings.Count(out, "\n")
if count >= 3 {
t.Errorf("demasiadas lineas (%d) para 'hola' en grid de 40 filas: %q", count, out)
}
if !strings.Contains(out, "hola") {
t.Errorf("no encontre 'hola' en %q", out)
}
})
t.Run("determinismo misma entrada misma salida", func(t *testing.T) {
input := "foo\x1b[10Gbar\r\n\x1b[2;1Hbaz"
out1 := VTRender(input, 10, 40)
out2 := VTRender(input, 10, 40)
if out1 != out2 {
t.Errorf("resultados distintos:\nout1=%q\nout2=%q", out1, out2)
}
})
t.Run("defaults rows y cols al pasar cero", func(t *testing.T) {
// Verificar que no entra en pánico con valores <= 0.
out := VTRender("test", 0, 0)
if !strings.Contains(out, "test") {
t.Errorf("no encontre 'test' con defaults (rows=0,cols=0): %q", out)
}
})
}