- TokenCtx + Classify + formatTokenCount (R2.2, R3.1-3.5, R4.4) - ReadTokenCtx orchestrator: env > PID walk > transcript parse (R1.1-1.6, R2.1-2.6) - parseLastUsage: 10MB scanner, JSONL last-assistant-wins, zero-usage qualifies (R2.1-2.3) - parsePIDFile: strict two-line layout; rejects partial/corrupted writes (R6.2-6.3) - resolveViaPIDWalk: 8-hop ancestor walk, liveness + start-stamp check (R1.2, R6.1, R6.3) - proc_darwin.go (x/sys/unix kinfo_proc) + proc_linux.go (/proc/stat field 22) - parseStatStartTime: platform-agnostic, handles comm with parens/spaces Parent 1.0 from dev/plans/26174-planctl-context-tokens/tasks.md
54 lines
1.8 KiB
Go
54 lines
1.8 KiB
Go
package main
|
|
|
|
import "testing"
|
|
|
|
// spec:26174-planctl-context-tokens/R6.3+D§3.3
|
|
// Synthetic stat line: benign comm, starttime=12345.
|
|
func TestParseStatStartTime_simple(t *testing.T) {
|
|
// 22 fields: pid (comm) state ppid pgrp session tty_nr tpgid flags minflt cminflt majflt cmajflt utime stime cutime cstime priority nice num_threads itrealvalue starttime
|
|
line := "1234 (bash) S 1 1 1 0 -1 4194304 100 0 0 0 1 2 0 0 20 0 1 0 12345 ...\n"
|
|
n, err := parseStatStartTime(line)
|
|
if err != nil {
|
|
t.Fatalf("parseStatStartTime: %v", err)
|
|
}
|
|
if n != 12345 {
|
|
t.Errorf("got %d, want 12345", n)
|
|
}
|
|
}
|
|
|
|
// spec:26174-planctl-context-tokens/R6.3+D§3.3
|
|
// comm containing spaces and parens: the "LAST ')'" parse strategy must still
|
|
// find the field-22 starttime correctly.
|
|
func TestParseStatStartTime_commWithParensAndSpaces(t *testing.T) {
|
|
line := "1234 (my (weird) program) S 1 1 1 0 -1 4194304 100 0 0 0 1 2 0 0 20 0 1 0 99999 ...\n"
|
|
n, err := parseStatStartTime(line)
|
|
if err != nil {
|
|
t.Fatalf("parseStatStartTime: %v", err)
|
|
}
|
|
if n != 99999 {
|
|
t.Errorf("got %d, want 99999", n)
|
|
}
|
|
}
|
|
|
|
// spec:26174-planctl-context-tokens/R6.3+D§3.3
|
|
func TestParseStatStartTime_noParenError(t *testing.T) {
|
|
if _, err := parseStatStartTime("not a stat line"); err == nil {
|
|
t.Fatal("expected error for missing ')'")
|
|
}
|
|
}
|
|
|
|
// spec:26174-planctl-context-tokens/R6.3+D§3.3
|
|
func TestParseStatStartTime_tooFewFields(t *testing.T) {
|
|
if _, err := parseStatStartTime("1 (x) S 1 1"); err == nil {
|
|
t.Fatal("expected error for too few fields")
|
|
}
|
|
}
|
|
|
|
// spec:26174-planctl-context-tokens/R6.3+D§3.3
|
|
func TestParseStatStartTime_nonNumeric(t *testing.T) {
|
|
// 20 tail fields with a non-numeric in starttime slot.
|
|
tail := "S 1 1 1 0 -1 4194304 100 0 0 0 1 2 0 0 20 0 1 0 NOPE"
|
|
if _, err := parseStatStartTime("1 (x) " + tail); err == nil {
|
|
t.Fatal("expected error for non-numeric starttime")
|
|
}
|
|
}
|