yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
proposal_registry.h
Go to the documentation of this file.
1#ifndef YAZE_SRC_CLI_SERVICE_PROPOSAL_REGISTRY_H_
2#define YAZE_SRC_CLI_SERVICE_PROPOSAL_REGISTRY_H_
3
4#include <filesystem>
5#include <memory>
6#include <mutex>
7#include <optional>
8#include <string>
9#include <unordered_map>
10#include <vector>
11
12#include "absl/status/status.h"
13#include "absl/status/statusor.h"
14#include "absl/strings/string_view.h"
15#include "absl/time/time.h"
16
17namespace yaze {
18namespace cli {
19
20// ProposalRegistry tracks agent-generated ROM modification proposals,
21// storing metadata, diffs, execution logs, and optional screenshots for
22// human review. Each proposal is associated with a sandbox ROM copy.
23//
24// Proposals follow a lifecycle:
25// 1. Created - agent generates a proposal during `agent run`
26// 2. Reviewed - user inspects via `agent diff` or TUI
27// 3. Accepted - changes committed to main ROM via `agent commit`
28// 4. Rejected - sandbox cleaned up via `agent revert`
30 public:
31 enum class ProposalStatus {
32 kPending, // Created but not yet reviewed
33 kAccepted, // User accepted the changes
34 kRejected, // User rejected the changes
35 };
36
38 std::string id;
39 std::string sandbox_id;
40 std::filesystem::path sandbox_directory;
41 std::filesystem::path sandbox_rom_path;
42 std::string description;
43 std::string prompt; // Original agent prompt that created this proposal
45 absl::Time created_at;
46 std::optional<absl::Time> reviewed_at;
47
48 // File paths relative to proposal directory
49 std::filesystem::path diff_path;
50 std::filesystem::path log_path;
51 std::vector<std::filesystem::path> screenshots;
52
53 // Statistics
56 };
57
58 static ProposalRegistry& Instance();
59
60 // Set the root directory for storing proposal data. If not set, uses
61 // YAZE_PROPOSAL_ROOT env var or defaults to system temp directory.
62 void SetRootDirectory(const std::filesystem::path& root);
63
64 const std::filesystem::path& RootDirectory() const;
65
66 // Creates a new proposal linked to the given sandbox. The proposal directory
67 // is created under the root, and metadata is initialized.
68 absl::StatusOr<ProposalMetadata> CreateProposal(
69 absl::string_view sandbox_id,
70 const std::filesystem::path& sandbox_rom_path,
71 absl::string_view prompt,
72 absl::string_view description);
73
74 // Records a diff between original and modified ROM for the proposal.
75 // The diff content is written to a file within the proposal directory.
76 absl::Status RecordDiff(const std::string& proposal_id,
77 absl::string_view diff_content);
78
79 // Appends log output from command execution to the proposal's log file.
80 absl::Status AppendLog(const std::string& proposal_id,
81 absl::string_view log_entry);
82
83 // Adds a screenshot path to the proposal metadata. Screenshots should
84 // be copied into the proposal directory before calling this.
85 absl::Status AddScreenshot(const std::string& proposal_id,
86 const std::filesystem::path& screenshot_path);
87
88 // Updates the number of commands executed for a proposal. Used to track
89 // how many CLI commands ran when generating the proposal.
90 absl::Status UpdateCommandStats(const std::string& proposal_id,
91 int commands_executed);
92
93 // Updates the proposal status (pending -> accepted/rejected) and sets
94 // the review timestamp.
95 absl::Status UpdateStatus(const std::string& proposal_id,
96 ProposalStatus status);
97
98 // Returns the metadata for a specific proposal.
99 absl::StatusOr<ProposalMetadata> GetProposal(
100 const std::string& proposal_id) const;
101
102 // Lists all proposals, optionally filtered by status.
103 std::vector<ProposalMetadata> ListProposals(
104 std::optional<ProposalStatus> filter_status = std::nullopt) const;
105
106 // Returns the most recently created proposal that is still pending.
107 absl::StatusOr<ProposalMetadata> GetLatestPendingProposal() const;
108
109 // Removes a proposal and its associated files from disk and memory.
110 absl::Status RemoveProposal(const std::string& proposal_id);
111
112 // Deletes all proposals older than the specified age.
113 absl::StatusOr<int> CleanupOlderThan(absl::Duration max_age);
114
115 private:
117
118 absl::Status EnsureRootExistsLocked();
119 absl::Status LoadProposalsFromDiskLocked();
120 std::string GenerateProposalIdLocked();
121 std::filesystem::path ProposalDirectory(absl::string_view proposal_id) const;
122 absl::Status WriteMetadataLocked(const ProposalMetadata& metadata) const;
123
124 std::filesystem::path root_directory_;
125 mutable std::mutex mutex_;
126 std::unordered_map<std::string, ProposalMetadata> proposals_;
127 int sequence_ = 0;
128};
129
130} // namespace cli
131} // namespace yaze
132
133#endif // YAZE_SRC_CLI_SERVICE_PROPOSAL_REGISTRY_H_
static ProposalRegistry & Instance()
absl::Status AppendLog(const std::string &proposal_id, absl::string_view log_entry)
absl::StatusOr< ProposalMetadata > GetLatestPendingProposal() const
std::filesystem::path root_directory_
absl::Status UpdateStatus(const std::string &proposal_id, ProposalStatus status)
absl::Status WriteMetadataLocked(const ProposalMetadata &metadata) const
std::unordered_map< std::string, ProposalMetadata > proposals_
absl::Status AddScreenshot(const std::string &proposal_id, const std::filesystem::path &screenshot_path)
std::filesystem::path ProposalDirectory(absl::string_view proposal_id) const
absl::Status RemoveProposal(const std::string &proposal_id)
absl::Status RecordDiff(const std::string &proposal_id, absl::string_view diff_content)
void SetRootDirectory(const std::filesystem::path &root)
const std::filesystem::path & RootDirectory() const
std::vector< ProposalMetadata > ListProposals(std::optional< ProposalStatus > filter_status=std::nullopt) const
absl::StatusOr< ProposalMetadata > CreateProposal(absl::string_view sandbox_id, const std::filesystem::path &sandbox_rom_path, absl::string_view prompt, absl::string_view description)
absl::StatusOr< ProposalMetadata > GetProposal(const std::string &proposal_id) const
absl::StatusOr< int > CleanupOlderThan(absl::Duration max_age)
absl::Status UpdateCommandStats(const std::string &proposal_id, int commands_executed)
Main namespace for the application.
std::vector< std::filesystem::path > screenshots