{"record":{"id":"9a66bf2abcb3115e","repo":"vxcontrol/pentagi","slug":"target-container-is-not-operational","errorCode":null,"errorMessage":"target container is not operational","messagePattern":"target container is not operational","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/tools/terminal.go","lineNumber":458,"sourceCode":"\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to put terminal log (write file cmd): %w\", err)\n\t}\n\n\treturn fmt.Sprintf(\"Successfully wrote %d bytes to %s\", len(content), path), nil\n}\n\n// writeFileToContainer copies content into the flow's container at path,\n// overwriting it. It performs no terminal-log writes; WriteFile and EditFile\n// each log their own, differently-worded, success message.\nfunc (t *terminal) writeFileToContainer(ctx context.Context, flowID int64, path, content string) error {\n\tcontainerName := PrimaryTerminalName(t.tenantPrefix, flowID)\n\n\tisRunning, err := t.dockerClient.IsContainerRunning(ctx, t.containerLID)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"container runtime check failed: %w\", err)\n\t}\n\tif !isRunning {\n\t\treturn fmt.Errorf(\"target container is not operational\")\n\t}\n\n\t// Docker SDK requires TAR format for file transfer\n\ttarBuffer := &bytes.Buffer{}\n\tarchiveWriter := tar.NewWriter(tarBuffer)\n\tdefer archiveWriter.Close()\n\n\tfilename := filepath.Base(path)\n\tfileDescriptor := &tar.Header{\n\t\tName: filename,\n\t\tMode: 0600,\n\t\tSize: int64(len(content)),\n\t}\n\terr = archiveWriter.WriteHeader(fileDescriptor)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"tar archive header generation failed: %w\", err)\n\t}\n","sourceCodeStart":440,"sourceCodeEnd":476,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/tools/terminal.go#L440-L476","documentation":"The write-path sibling of 'container runtime is not operational': after a successful IsContainerRunning query, writeFileToContainer refuses to write if the flow's terminal container reports a non-running state. Unlike 958, Docker answered fine — the container is simply exited/stopped, so the tar-based PutToArchive would fail.","triggerScenarios":"WriteFile or EditFile invoked while the flow's terminal container has exited: sandbox container finished/crashed, `docker stop` issued, host rebooted without restart policy, or the flow was cleaned up while the tool call was still in flight.","commonSituations":"Agent attempts to persist results after its sandbox was torn down at flow end; container OOM-killed or entrypoint exited so Docker set state=exited; restart policy set to `no` so it never comes back after a daemon restart.","solutions":["Restart the container: `docker start <container>` or let the flow lifecycle re-create the primary terminal container, then retry the write","Check the exit reason: `docker inspect -f '{{.State.Status}} {{.State.ExitCode}} {{.State.OOMKilled}}' <container>` and `docker logs`","Set a restart policy (e.g. unless-stopped) or ensure the platform's container manager keeps the terminal container alive for the flow duration","In the caller, check IsContainerRunning before writing and either restart the container or fail fast with a user-actionable message"],"exampleFix":"// before\n_, err := tool.WriteFile(ctx, flowID, content, \"/tmp/results.json\")\n// after\nif running, _ := dockerClient.IsContainerRunning(ctx, containerLID); !running {\n    if err := dockerClient.StartContainer(ctx, containerLID); err != nil {\n        return fmt.Errorf(\"terminal container not running and restart failed: %w\", err)\n    }\n}\n_, err := tool.WriteFile(ctx, flowID, content, \"/tmp/results.json\")","handlingStrategy":"fallback","validationCode":"running, err := dockerClient.IsContainerRunning(ctx, containerLID)\nif err != nil {\n    return fmt.Errorf(\"cannot verify container: %w\", err)\n}\nif !running {\n    if err := dockerClient.StartContainer(ctx, containerLID); err != nil {\n        return fmt.Errorf(\"container stopped and could not be started: %w\", err)\n    }\n}","typeGuard":"func containerOperational(inspect types.ContainerJSON) bool {\n    return inspect.State != nil && inspect.State.Running && !inspect.State.Dead\n}","tryCatchPattern":"_, err := tool.WriteFile(ctx, flowID, content, path)\nif err != nil && strings.Contains(err.Error(), \"target container is not operational\") {\n    // restart or recreate the terminal container, then retry once\n    _, err = tool.WriteFile(ctx, flowID, content, path)\n}","preventionTips":["Set restart=unless-stopped on sandbox containers","Check OOMKilled/ExitCode via docker inspect to fix recurring crashes","Keep terminal containers alive until all agent tool calls complete","Verify container state before every write, not only at flow start"],"tags":["docker","container-lifecycle","file-write"],"backgroundTag":"container-not-running","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}