package server import ( "encoding/json" "fmt" "maps" "strings" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" ) type memoryValue struct { output stage.Values sequence int } // Serialize/decode to sever mutable aliases between stage work and retained state. func copyValues(values stage.Values) (stage.Values, error) { data, err := json.Marshal(values) if err != nil { return nil, err } if len(data) > 1<<20 { return nil, fmt.Errorf("shared memory value exceeds 1 MiB") } var result stage.Values decoder := json.NewDecoder(strings.NewReader(string(data))) decoder.UseNumber() if err := decoder.Decode(&result); err != nil { return nil, err } return result, nil } func memoryInput(previous stage.Values, bindings map[string]string, memory map[string]memoryValue) (stage.Values, map[string]protocol.MemoryRead, error) { input := maps.Clone(previous) if len(bindings) > 0 { input = stage.Values{} } if input == nil { input = stage.Values{} } reads := map[string]protocol.MemoryRead{} for alias, ref := range bindings { optional := strings.HasSuffix(ref, "?") owner, field, _ := strings.Cut(strings.TrimSuffix(ref, "?"), ".") entry, ok := memory[owner] if !ok && optional { continue } if !ok { return nil, nil, fmt.Errorf("shared memory source %s has not completed in this run", owner) } value, ok := entry.output[field] if !ok && optional { continue } if !ok { return nil, nil, fmt.Errorf("shared memory field %s is missing", ref) } input[alias] = value reads[alias] = protocol.MemoryRead{Source: ref, Sequence: entry.sequence} } cloned, err := copyValues(input) return cloned, reads, err }