{"record":{"id":"d432920345cfbc04","repo":"vxcontrol/pentagi","slug":"failed-to-put-terminal-log-stdout-w","errorCode":null,"errorMessage":"failed to put terminal log (stdout): %w","messagePattern":"failed to put terminal log \\(stdout\\): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/tools/terminal.go","lineNumber":319,"sourceCode":"\t\t\t\t\"For long batch commands, wrap with shell timeout utility: 'timeout %d <command>' to ensure clean completion\",\n\t\t\tctx.Err(),\n\t\t\ttruncateString(dst.String(), 500),\n\t\t\tsuggestedTimeout,\n\t\t)\n\t}\n\n\t// wait for the exec process to finish\n\t_, err = t.dockerClient.ContainerExecInspect(ctx, id)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to inspect exec process: %w\", err)\n\t}\n\n\tresults := dst.String()\n\t// Style system output with color coding\n\tstyledOutput := fmt.Sprintf(\"%s%s%s%s\", ansiColorSystemMsg, results, ansiColorReset, ansiLineTerminator)\n\t_, err = t.tlp.PutMsg(ctx, database.TermlogTypeStdout, styledOutput, t.containerID, t.taskID, t.subtaskID)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to put terminal log (stdout): %w\", err)\n\t}\n\n\tif results == \"\" {\n\t\tresults = \"Command completed successfully with exit code 0. No output produced (silent success)\"\n\t}\n\n\treturn results, nil\n}\n\nfunc (t *terminal) ReadFile(ctx context.Context, flowID int64, path string) (string, error) {\n\tif path == \"\" {\n\t\treturn \"\", fmt.Errorf(\"path is required and cannot be empty\")\n\t}\n\n\tcwd := docker.WorkFolderPathInContainer\n\tescapedPath := strings.ReplaceAll(path, \"'\", \"'\\\"'\\\"'\")\n\tcatCommand := fmt.Sprintf(\"cat '%s'\", escapedPath)\n\t// Format read file command with styling","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/tools/terminal.go#L301-L337","documentation":"After a successful exec, getExecResult logs the styled output to the terminal log store via t.tlp.PutMsg with TermlogTypeStdout. This error wraps a failure of that persistence call, meaning the command ran fine but its transcript could not be recorded, so the function aborts to keep the terminal log consistent.","triggerScenarios":"PutMsg(ctx, database.TermlogTypeStdout, styledOutput, ...) returns an error — typically the database write fails: DB connection lost, transaction/lock issues, or the storage layer rejects the (possibly large or invalid-UTF8) output payload.","commonSituations":"PostgreSQL outage or connection-pool exhaustion during heavy pentest activity; output exceeding a size limit in the termlog table; encoding issues in binary command output.","solutions":["Check database connectivity and logs (Postgres health, connection pool settings)","Verify the termlog table constraints/column sizes vs the output size (truncate large outputs before PutMsg)","Sanitize non-UTF8 output before logging","Retry the operation once the DB recovers — the command result itself was valid"],"exampleFix":"// before\n_, err = t.tlp.PutMsg(ctx, database.TermlogTypeStdout, styledOutput, ...)\nif err != nil { return \"\", fmt.Errorf(\"failed to put terminal log (stdout): %w\", err) }\n// after\nstyledOutput = truncateString(strings.ToValidUTF8(styledOutput, string(utf8.RuneError)), 10000)\n_, err = t.tlp.PutMsg(ctx, database.TermlogTypeStdout, styledOutput, ...)","handlingStrategy":"fallback","validationCode":"// pre-flight DB check in deployment health routine\nif err := db.PingContext(ctx); err != nil {\n    log.Warn(\"database unreachable; termlog writes will fail\")\n}","typeGuard":null,"tryCatchPattern":"out, err := term.ExecCommand(ctx, flowID, cmd, false)\nif err != nil && strings.Contains(err.Error(), \"failed to put terminal log\") {\n    // command succeeded; only logging failed — treat output as usable with degraded audit\n    log.Warn(\"termlog stdout write failed\", \"err\", err)\n}","preventionTips":["Monitor PostgreSQL connectivity and pool saturation","Cap/truncate command output before it reaches the termlog store","Sanitize binary/non-UTF8 output","Alert on termlog write failures to keep audit logs trustworthy"],"tags":["database","logging","persistence","terminal"],"backgroundTag":"database-write-failed","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}