package turn // A Session's transcript lives in the operator's Store between turns, // as a tar.gz of the Claude Code project directory where its turns run. // The working directory is the same path in every Job, so the project // directory's name is too, and `claude --resume` finds the transcript // where the archive puts it. import ( "archive/tar" "compress/gzip" "context" "errors" "fmt" "io" "io/fs" "os" "path/filepath" "tangled.org/guid.foo/simmer/dataplane" ) // downloadTranscript unpacks the Session's transcript into projects, // and answers whether it holds the conversation uid in project, so the // turn can resume it. It answers false when the Store has none, or when // the archive lacks the conversation, as after a turn whose claude // failed before it wrote one. The turn then starts the conversation // again under the same id, where --resume would fail on every turn. func downloadTranscript(ctx context.Context, client *dataplane.Client, projects, project, uid string) (bool, error) { archive, err := client.Transcript(ctx) if errors.Is(err, dataplane.ErrNoTranscript) { return false, nil } if err != nil { return false, fmt.Errorf("downloading the transcript: %w", err) } defer archive.Close() if err := unpack(archive, projects); err != nil { return false, fmt.Errorf("unpacking the transcript: %w", err) } _, err = os.Stat(filepath.Join(projects, project, uid+".jsonl")) if errors.Is(err, os.ErrNotExist) { return false, nil } return err == nil, err } // uploadTranscript packs projects/ and uploads it. A turn // whose claude never started has no project directory, and nothing to // upload. func uploadTranscript(ctx context.Context, client *dataplane.Client, projects, project string) error { if _, err := os.Stat(filepath.Join(projects, project)); errors.Is(err, os.ErrNotExist) { return nil } archive, err := os.CreateTemp("", "simmer-transcript-*.tar.gz") if err != nil { return err } defer os.Remove(archive.Name()) defer archive.Close() if err := pack(archive, projects, project); err != nil { return fmt.Errorf("packing the transcript: %w", err) } if _, err := archive.Seek(0, io.SeekStart); err != nil { return err } if err := client.PutTranscript(ctx, archive); err != nil { return fmt.Errorf("uploading the transcript: %w", err) } return nil } // pack writes root/project as a tar.gz whose names start with project. func pack(w io.Writer, root, project string) error { gz := gzip.NewWriter(w) entries := tar.NewWriter(gz) err := filepath.WalkDir(filepath.Join(root, project), func(path string, entry fs.DirEntry, err error) error { if err != nil || !(entry.IsDir() || entry.Type().IsRegular()) { return err } info, err := entry.Info() if err != nil { return err } header, err := tar.FileInfoHeader(info, "") if err != nil { return err } if header.Name, err = filepath.Rel(root, path); err != nil { return err } if err := entries.WriteHeader(header); err != nil || entry.IsDir() { return err } file, err := os.Open(path) if err != nil { return err } defer file.Close() _, err = io.Copy(entries, file) return err }) if err != nil { return err } if err := entries.Close(); err != nil { return err } return gz.Close() } // unpack writes a tar.gz's directories and regular files under root. A // name that would land outside root is refused, so an archive cannot // write anywhere else in the Job. func unpack(r io.Reader, root string) error { gz, err := gzip.NewReader(r) if err != nil { return err } entries := tar.NewReader(gz) for { header, err := entries.Next() if errors.Is(err, io.EOF) { return nil } if err != nil { return err } if !filepath.IsLocal(header.Name) { return fmt.Errorf("the archive names %q, outside its directory", header.Name) } path := filepath.Join(root, header.Name) switch header.Typeflag { case tar.TypeDir: err = os.MkdirAll(path, 0o700) case tar.TypeReg: err = writeFile(path, entries) } if err != nil { return err } } } func writeFile(path string, content io.Reader) error { if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { return err } file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600) if err != nil { return err } _, err = io.Copy(file, content) if closeErr := file.Close(); err == nil { err = closeErr } return err }