package main

import (
	"bufio"
	"encoding/csv"
	"encoding/gob"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"sort"
	"strings"

	"gorgonia.org/gorgonia"
	"gorgonia.org/tensor"
)

// ParsedData represents a question (input) and its decomposed form (output)
type ParsedData struct {
	Input  string
	Output string
}

const maxTrainMessages = 100000

// LoadCSVFiles loads and processes CSV files, extracting pairs.
func LoadCSVFiles(dirname string) ([]ParsedData, error) {
	files, err := os.ReadDir(dirname)
	if err != nil {
		return nil, err
	}

	var data []ParsedData
	for _, file := range files {
		if strings.HasSuffix(file.Name(), ".csv") {
			f, err := os.Open(filepath.Join(dirname, file.Name()))
			if err != nil {
				return nil, err
			}
			defer f.Close()

			r := csv.NewReader(f)
			records, err := r.ReadAll()
			if err != nil {
				return nil, err
			}

			for i := 1; i < len(records); i++ { // skip header
				record := records[i]
				if len(record) >= 5 {
					data = append(data, ParsedData{
						Input:  record[4],
						Output: record[5],
					})
				}
			}
		}
	}
	return data, nil
}

// Tokenize splits and cleans the text
func Tokenize(text string) []string {
	reg, err := regexp.Compile("[^a-zA-Z0-9]+")
	if err != nil {
		log.Fatalf("Regex compilation failed: %v", err)
	}
	cleanedText := reg.ReplaceAllString(text, " ")
	return strings.Fields(strings.ToLower(cleanedText))
}

// BuildVocab creates a vocabulary from the input data
func BuildVocab(data []ParsedData) map[string]int {
	vocab := make(map[string]int)
	for _, item := range data {
		tokens := append(Tokenize(item.Input), Tokenize(item.Output)...)
		for _, token := range tokens {
			if _, exists := vocab[token]; !exists {
				vocab[token] = len(vocab)
			}
		}
	}
	return vocab
}

// Vectorize converts sequences of words to a vector representation
func Vectorize(tokens []string, vocab map[string]int) []float32 {
	vector := make([]float32, len(vocab))
	for _, token := range tokens {
		if idx, exists := vocab[token]; exists {
			vector[idx] += 1
		}
	}
	return vector
}

// SaveWeights saves the model weights
func SaveWeights(weights *tensor.Dense, filename string) error {
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer f.Close()

	encoder := gob.NewEncoder(f)
	return encoder.Encode(weights)
}

// LoadWeights loads the model weights
func LoadWeights(filename string) (*tensor.Dense, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	var weights tensor.Dense
	decoder := gob.NewDecoder(f)
	if err := decoder.Decode(&weights); err != nil {
		return nil, err
	}
	return &weights, nil
}

// SaveVocabulary saves the vocabulary to a file
func SaveVocabulary(vocab map[string]int, filename string) error {
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer f.Close()

	encoder := gob.NewEncoder(f)
	return encoder.Encode(vocab)
}

// LoadVocabulary loads the vocabulary from a file
func LoadVocabulary(filename string) (map[string]int, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	var vocab map[string]int
	decoder := gob.NewDecoder(f)
	if err := decoder.Decode(&vocab); err != nil {
		return nil, err
	}
	return vocab, nil
}

func trainModel(trainDir string, maxTrainMessages int) {
	parsedData, err := LoadCSVFiles(trainDir)
	if err != nil {
		log.Fatalf("Error loading CSV files: %v", err)
	}

	if maxTrainMessages > len(parsedData) {
		maxTrainMessages = len(parsedData)
	}

	vocab := BuildVocab(parsedData)
	if err := SaveVocabulary(vocab, "vocab.gob"); err != nil {
		log.Fatalf("Error saving vocabulary: %v", err)
	}

	parsedData = parsedData[:maxTrainMessages]
	fmt.Printf("Loaded %d training records\n", len(parsedData))

	batchSize := 150
	totalSamples := len(parsedData)
	inputSize, outputSize := len(vocab), len(vocab) // assuming input and output vocab are the same

	g := gorgonia.NewGraph()

	w := gorgonia.NewMatrix(g, tensor.Float32, gorgonia.WithShape(inputSize, outputSize), gorgonia.WithInit(gorgonia.GlorotN(1.0)))

	optimizer := gorgonia.NewAdamSolver(gorgonia.WithLearnRate(0.001))

	machine := gorgonia.NewTapeMachine(g, gorgonia.BindDualValues(w))
	defer machine.Close()

	for epoch := 0; epoch < 5; epoch++ {
		for start := 0; start < totalSamples; start += batchSize {
			end := start + batchSize
			if end > totalSamples {
				end = totalSamples
			}

			xVal := tensor.New(tensor.WithShape(batchSize, inputSize), tensor.Of(tensor.Float32))
			yVal := tensor.New(tensor.WithShape(batchSize, outputSize), tensor.Of(tensor.Float32))

			for i := start; i < end; i++ {
				inputTokens := Tokenize(parsedData[i].Input)
				inputVec := Vectorize(inputTokens, vocab)
				outputTokens := Tokenize(parsedData[i].Output)
				outputVec := Vectorize(outputTokens, vocab)

				offset := (i - start) * inputSize
				copy(xVal.Data().([]float32)[offset:], inputVec)
				offsetY := (i - start) * outputSize
				copy(yVal.Data().([]float32)[offsetY:], outputVec)
			}

			x := gorgonia.NewMatrix(g, tensor.Float32, gorgonia.WithShape(batchSize, inputSize), gorgonia.WithValue(xVal))
			y := gorgonia.NewMatrix(g, tensor.Float32, gorgonia.WithShape(batchSize, outputSize), gorgonia.WithValue(yVal))

			yhat := gorgonia.Must(gorgonia.Mul(x, w))
			loss := gorgonia.Must(gorgonia.Mean(gorgonia.Must(gorgonia.Square(gorgonia.Must(gorgonia.Sub(yhat, y))))))

			if _, err := gorgonia.Grad(loss, w); err != nil {
				log.Fatalf("Failed to create computation graph: %v", err)
			}

			if err := machine.RunAll(); err != nil {
				log.Fatalf("Machine run error: %v", err)
			}

			optimizer.Step(gorgonia.NodesToValueGrads([]*gorgonia.Node{w}))

			machine.Reset()

			progress := float64(end) / float64(totalSamples) * 100
			fmt.Printf("Epoch %d: Processed batch from %d to %d (%.2f%% complete)\n", epoch+1, start, end, progress)
		}
	}

	fmt.Println("Model training complete")

	if err := SaveWeights(w.Value().(*tensor.Dense), "model_weights.gob"); err != nil {
		log.Fatalf("Error saving model weights: %v", err)
	} else {
		fmt.Println("Model weights saved successfully.")
	}
}

func interactWithModel() {
	weights, err := LoadWeights("model_weights.gob")
	if err != nil {
		log.Fatalf("Error loading model weights: %v", err)
	}

	vocab, err := LoadVocabulary("vocab.gob")
	if err != nil {
		log.Fatalf("Error loading vocabulary: %v", err)
	}

	reverseVocab := make(map[int]string)
	for word, idx := range vocab {
		reverseVocab[idx] = word
	}

	inputSize := len(vocab)
	reader := bufio.NewReader(os.Stdin)

	for {
		fmt.Print("Ask a question (or type 'exit' to quit): ")
		text, _ := reader.ReadString('\n')
		text = strings.TrimSpace(text)

		if text == "exit" {
			break
		}

		tokens := Tokenize(text)
		inputVec := Vectorize(tokens, vocab)

		xVal := tensor.New(tensor.WithShape(1, inputSize), tensor.Of(tensor.Float32))
		copy(xVal.Data().([]float32), inputVec)

		g := gorgonia.NewGraph()
		x := gorgonia.NewMatrix(g, tensor.Float32, gorgonia.WithShape(1, inputSize), gorgonia.WithValue(xVal))
		outputSize := len(vocab)
		w := gorgonia.NewMatrix(g, tensor.Float32, gorgonia.WithShape(inputSize, outputSize), gorgonia.WithValue(weights))

		yhat := gorgonia.Must(gorgonia.Mul(x, w))

		machine := gorgonia.NewTapeMachine(g)
		defer machine.Close()

		if err := machine.RunAll(); err != nil {
			log.Fatalf("Machine run error: %v", err)
		}

		predictionsVal := yhat.Value().Data().([]float32)
		topIndices := getTopIndices(predictionsVal, 5)

		var responseTokens []string
		for _, idx := range topIndices {
			if word, exists := reverseVocab[idx]; exists {
				responseTokens = append(responseTokens, word)
			}
		}

		response := strings.Join(responseTokens, " ")
		fmt.Printf("Generated Response: %s\n", response)
		machine.Reset()
	}
}

// getTopIndices returns the indices of the top N highest values in a float slice
func getTopIndices(values []float32, n int) []int {
	type kv struct {
		Key   int
		Value float32
	}

	var ss []kv
	for i, val := range values {
		ss = append(ss, kv{i, val})
	}

	// Sort by Value
	sort.Slice(ss, func(i, j int) bool {
		return ss[i].Value > ss[j].Value
	})

	var indices []int
	for i := 0; i < n && i < len(ss); i++ {
		indices = append(indices, ss[i].Key)
	}
	return indices
}

func main() {
	fmt.Print("Enter mode of operation (t for training, v for validation, d for dialogue): ")
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	mode := scanner.Text()

	switch mode {
	case "t":
		trainModel("train", maxTrainMessages)
	case "d":
		interactWithModel()
	default:
		fmt.Println("Unknown mode. Use 't' for training or 'd' for dialogue.")
	}
}


Enter mode of operation (t for training, v for validation, d for dialogue): t
Loaded 100000 training records
fatal error: runtime: cannot allocate memory

runtime stack:
runtime.throw({0xfa938e?, 0xffffffffffffef30?})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/panic.go:1023 +0x65 fp=0x54a53ff898 sp=0x54a53ff868 pc=0x7dd205
runtime.persistentalloc1(0x10d0, 0x54a53ff900?, 0x1568218)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/malloc.go:1576 +0x24d fp=0x54a53ff8e8 sp=0x54a53ff898 pc=0x7b2a0d
runtime.persistentalloc.func1()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/malloc.go:1529 +0x28 fp=0x54a53ff918 sp=0x54a53ff8e8 pc=0x7b27a8
runtime.persistentalloc(0x7ffb54d03bf0?, 0x0?, 0x800000?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/malloc.go:1528 +0x5c fp=0x54a53ff960 sp=0x54a53ff918 pc=0x7b271c
runtime.(*mheap).sysAlloc(0x14e7320, 0x80c028bfe000?, 0x14f7720, 0x1)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/malloc.go:776 +0x253 fp=0x54a53ffa18 sp=0x54a53ff960 pc=0x7b1093
runtime.(*mheap).grow(0x14e7320, 0x20157744b?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mheap.go:1484 +0x76 fp=0x54a53ffa98 sp=0x54a53ffa18 pc=0x7ce7b6
runtime.(*mheap).allocSpan(0x14e7320, 0x20157744b, 0x0, 0x1)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mheap.go:1246 +0x1b0 fp=0x54a53ffb38 sp=0x54a53ffa98 pc=0x7ce050
runtime.(*mheap).alloc.func1()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mheap.go:964 +0x5c fp=0x54a53ffb80 sp=0x54a53ffb38 pc=0x7cdadc
runtime.systemstack(0xc000584000)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:509 +0x49 fp=0x54a53ffb90 sp=0x54a53ffb80 pc=0x80d929

goroutine 1 gp=0xc000068000 m=7 mp=0xc000580008 [running]:
runtime.systemstack_switch()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:474 +0x8 fp=0xc000037780 sp=0xc000037770 pc=0x80d8c8
runtime.(*mheap).alloc(0x402aee896000?, 0x20157744b?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mheap.go:958 +0x5b fp=0xc0000377c8 sp=0xc000037780 pc=0x7cda3b
runtime.(*mcache).allocLarge(0x10?, 0x402aee895200, 0x1)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mcache.go:234 +0x87 fp=0xc000037818 sp=0xc0000377c8 pc=0x7bac27
runtime.mallocgc(0x402aee895200, 0xee8ee0, 0x1)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/malloc.go:1165 +0x597 fp=0xc0000378a0 sp=0xc000037818 pc=0x7b1f77
runtime.makeslice(0x18048e78ed78b78c?, 0x7ba73d?, 0x235d5477a18?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/slice.go:107 +0x49 fp=0xc0000378c8 sp=0xc0000378a0 pc=0x7f32e9
gorgonia.org/gorgonia.GlorotEtAlN64(0xc0000379a8?, {0xc028b999f0?, 0x235d62dfff8?, 0x20?})
        C:/Users/79685/go/pkg/mod/gorgonia.org/gorgonia@v0.9.18/weights.go:370 +0x131 fp=0xc000037920 sp=0xc0000378c8 pc=0xe94bd1
gorgonia.org/gorgonia.GlorotEtAlN32(0x0?, {0xc028b999f0?, 0xc01ef5a4a0?, 0xc0000379a0?})
        C:/Users/79685/go/pkg/mod/gorgonia.org/gorgonia@v0.9.18/weights.go:381 +0x18 fp=0xc000037960 sp=0xc000037920 pc=0xe94c98
main.trainModel.GlorotN.func1({{0x106b620, 0xee8ea0}}, {0xc028b999f0, 0x2, 0x2})
        C:/Users/79685/go/pkg/mod/gorgonia.org/gorgonia@v0.9.18/weights.go:215 +0xcc fp=0xc0000379d0 sp=0xc000037960 pc=0xeb4eec
main.trainModel.WithInit.func2(0xc01ed57a40)
        C:/Users/79685/go/pkg/mod/gorgonia.org/gorgonia@v0.9.18/node.go:202 +0x8c fp=0xc000037a38 sp=0xc0000379d0 pc=0xeb4d8c
gorgonia.org/gorgonia.newNode({0xc01ef5a480, 0x4, 0x4?})
        C:/Users/79685/go/pkg/mod/gorgonia.org/gorgonia@v0.9.18/node.go:279 +0xaa fp=0xc000037a70 sp=0xc000037a38 pc=0xe4ad4a
gorgonia.org/gorgonia.NewUniqueNode({0xc01ef5a480?, 0xc000037b08?, 0x10?})
        C:/Users/79685/go/pkg/mod/gorgonia.org/gorgonia@v0.9.18/node.go:289 +0x18 fp=0xc000037aa8 sp=0xc000037a70 pc=0xe4add8
gorgonia.org/gorgonia.NewMatrix(0xc01f2c9400, {{0x106b620, 0xee8ea0}}, {0xc000037e20, 0x2, 0x1?})
        C:/Users/79685/go/pkg/mod/gorgonia.org/gorgonia@v0.9.18/gorgonia.go:69 +0x1b2 fp=0xc000037b30 sp=0xc000037aa8 pc=0xe41572
main.trainModel({0xf9ade5?, 0xc000072028?}, 0x186a0)
        D:/JBProjects/GOLAND/Learn/main.go:178 +0x2d6 fp=0xc000037e58 sp=0xc000037b30 pc=0xeb33b6
main.main()
        D:/JBProjects/GOLAND/Learn/main.go:335 +0x106 fp=0xc000037f50 sp=0xc000037e58 pc=0xeb6246
runtime.main()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:271 +0x28b fp=0xc000037fe0 sp=0xc000037f50 pc=0x7dfb2b
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc000037fe8 sp=0xc000037fe0 pc=0x80f8e1

goroutine 2 gp=0xc000068700 m=nil [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00006bfa8 sp=0xc00006bf88 pc=0x7dff2e
runtime.goparkunlock(...)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:408
runtime.forcegchelper()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:326 +0xb8 fp=0xc00006bfe0 sp=0xc00006bfa8 pc=0x7dfdb8
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00006bfe8 sp=0xc00006bfe0 pc=0x80f8e1
created by runtime.init.6 in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:314 +0x1a

goroutine 3 gp=0xc000068a80 m=nil [GC sweep wait]:
runtime.gopark(0x1?, 0x0?, 0x0?, 0x0?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00006df80 sp=0xc00006df60 pc=0x7dff2e
runtime.goparkunlock(...)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:408
runtime.bgsweep(0xc0000281c0)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgcsweep.go:318 +0xdf fp=0xc00006dfc8 sp=0xc00006df80 pc=0x7ca47f
runtime.gcenable.gowrap1()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:203 +0x25 fp=0xc00006dfe0 sp=0xc00006dfc8 pc=0x7bed45
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00006dfe8 sp=0xc00006dfe0 pc=0x80f8e1
created by runtime.gcenable in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:203 +0x66

goroutine 4 gp=0xc000068c40 m=nil [GC scavenge wait]:
runtime.gopark(0xe800f8?, 0x4021d?, 0x0?, 0x0?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00007df78 sp=0xc00007df58 pc=0x7dff2e
runtime.goparkunlock(...)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:408
runtime.(*scavengerState).park(0x14c7fe0)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgcscavenge.go:425 +0x49 fp=0xc00007dfa8 sp=0xc00007df78 pc=0x7c7e29
runtime.bgscavenge(0xc0000281c0)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgcscavenge.go:658 +0x59 fp=0xc00007dfc8 sp=0xc00007dfa8 pc=0x7c83d9
runtime.gcenable.gowrap2()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:204 +0x25 fp=0xc00007dfe0 sp=0xc00007dfc8 pc=0x7bece5
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00007dfe8 sp=0xc00007dfe0 pc=0x80f8e1
created by runtime.gcenable in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:204 +0xa5

goroutine 5 gp=0xc000068fc0 m=nil [finalizer wait]:
runtime.gopark(0x2358e1cad88?, 0xc000032030?, 0x0?, 0x0?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00006fe20 sp=0xc00006fe00 pc=0x7dff2e
runtime.runfinq()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mfinal.go:194 +0x107 fp=0xc00006ffe0 sp=0xc00006fe20 pc=0x7bddc7
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00006ffe8 sp=0xc00006ffe0 pc=0x80f8e1
created by runtime.createfing in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mfinal.go:164 +0x3d

goroutine 6 gp=0xc000069880 m=nil [GC worker (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00007ff50 sp=0xc00007ff30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc00007ffe0 sp=0xc00007ff50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00007ffe8 sp=0xc00007ffe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 18 gp=0xc000480000 m=nil [GC worker (idle)]:
runtime.gopark(0x362042f5850?, 0x1?, 0x74?, 0xd3?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc000079f50 sp=0xc000079f30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc000079fe0 sp=0xc000079f50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc000079fe8 sp=0xc000079fe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 19 gp=0xc0004801c0 m=nil [GC worker (idle)]:
runtime.gopark(0x36589b21ff0?, 0x1?, 0x88?, 0x1?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00007bf50 sp=0xc00007bf30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc00007bfe0 sp=0xc00007bf50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00007bfe8 sp=0xc00007bfe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 34 gp=0xc0001041c0 m=nil [GC worker (idle)]:
runtime.gopark(0x362042f5850?, 0x3?, 0x68?, 0x31?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00010df50 sp=0xc00010df30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc00010dfe0 sp=0xc00010df50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00010dfe8 sp=0xc00010dfe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 20 gp=0xc000480380 m=nil [GC worker (idle)]:
runtime.gopark(0x1566040?, 0x1?, 0x50?, 0xca?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc000109f50 sp=0xc000109f30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc000109fe0 sp=0xc000109f50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc000109fe8 sp=0xc000109fe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 35 gp=0xc000104380 m=nil [GC worker (idle)]:
runtime.gopark(0x364e5df8908?, 0x1?, 0x5c?, 0x54?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00010ff50 sp=0xc00010ff30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc00010ffe0 sp=0xc00010ff50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00010ffe8 sp=0xc00010ffe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 21 gp=0xc000480540 m=nil [GC worker (idle)]:
runtime.gopark(0x36588a8a890?, 0x3?, 0x10?, 0x4d?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00010bf50 sp=0xc00010bf30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc00010bfe0 sp=0xc00010bf50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00010bfe8 sp=0xc00010bfe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 36 gp=0xc000104540 m=nil [GC worker (idle)]:
runtime.gopark(0x364e5eef578?, 0x3?, 0x6c?, 0x91?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc000115f50 sp=0xc000115f30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc000115fe0 sp=0xc000115f50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc000115fe8 sp=0xc000115fe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 22 gp=0xc000480700 m=nil [GC worker (idle)]:
runtime.gopark(0x1566040?, 0x1?, 0x24?, 0xb2?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc000111f50 sp=0xc000111f30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc000111fe0 sp=0xc000111f50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc000111fe8 sp=0xc000111fe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 37 gp=0xc000104700 m=nil [GC worker (idle)]:
runtime.gopark(0x36589b21ff0?, 0x3?, 0xe0?, 0xb1?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc000117f50 sp=0xc000117f30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc000117fe0 sp=0xc000117f50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc000117fe8 sp=0xc000117fe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 23 gp=0xc0004808c0 m=nil [GC worker (idle)]:
runtime.gopark(0x36589d14c04?, 0x3?, 0x10?, 0x86?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc000113f50 sp=0xc000113f30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc000113fe0 sp=0xc000113f50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc000113fe8 sp=0xc000113fe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 38 gp=0xc0001048c0 m=nil [GC worker (idle)]:
runtime.gopark(0x36589d14c04?, 0x1?, 0xf4?, 0xf4?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00011df50 sp=0xc00011df30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc00011dfe0 sp=0xc00011df50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00011dfe8 sp=0xc00011dfe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 24 gp=0xc000480a80 m=nil [GC worker (idle)]:
runtime.gopark(0x1566040?, 0x1?, 0x88?, 0x1?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc000119f50 sp=0xc000119f30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc000119fe0 sp=0xc000119f50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc000119fe8 sp=0xc000119fe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 39 gp=0xc000104a80 m=nil [GC worker (idle)]:
runtime.gopark(0x364e5df8908?, 0x1?, 0xc?, 0x71?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00011ff50 sp=0xc00011ff30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc00011ffe0 sp=0xc00011ff50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00011ffe8 sp=0xc00011ffe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 25 gp=0xc000480c40 m=nil [GC worker (idle)]:
runtime.gopark(0x36438986d64?, 0x3?, 0x0?, 0x0?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc00011bf50 sp=0xc00011bf30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc00011bfe0 sp=0xc00011bf50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc00011bfe8 sp=0xc00011bfe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c

goroutine 40 gp=0xc000104c40 m=nil [GC worker (idle)]:
runtime.gopark(0x364e5df8908?, 0x1?, 0x18?, 0x28?, 0x0?)
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/proc.go:402 +0xce fp=0xc000125f50 sp=0xc000125f30 pc=0x7dff2e
runtime.gcBgMarkWorker()
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1310 +0xe5 fp=0xc000125fe0 sp=0xc000125f50 pc=0x7c0e85
runtime.goexit({})
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/asm_amd64.s:1695 +0x1 fp=0xc000125fe8 sp=0xc000125fe0 pc=0x80f8e1
created by runtime.gcBgMarkStartWorkers in goroutine 1
        C:/Users/79685/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.6.windows-amd64/src/runtime/mgc.go:1234 +0x1c