yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
project_plan_agent.h
Go to the documentation of this file.
1#ifndef YAZE_CORE_PROJECT_PLAN_AGENT_H_
2#define YAZE_CORE_PROJECT_PLAN_AGENT_H_
3
4#include <string>
5#include <string_view>
6#include <vector>
7
8#include "absl/status/status.h"
9#include "absl/status/statusor.h"
10#include "core/project_plan.h"
11
12namespace yaze::core {
13
14// Agent observations live in one top-level plan section that people do not
15// edit. Every other plan field (notes, answers, titles, route order, markers,
16// unknown sections) belongs to the person editing the plan, and an agent update
17// cannot reach it. Observations are keyed by existing plan targets:
18// "step:<id>", "item:<id>", "decision:<id>" or "milestone:<id>".
19//
20// "agent_status": {
21// "schema": "yaze.plan.agent_status/1",
22// "updated_utc": "...", "agent": "...", "sources": {...},
23// "observations": {"step:D4": {"state": "...", "next": "...", ...}}
24// }
25inline constexpr std::string_view kPlanAgentStatusKey = "agent_status";
26inline constexpr std::string_view kPlanAgentStatusSchema =
27 "yaze.plan.agent_status/1";
28inline constexpr std::string_view kPlanAgentUpdateSchema =
29 "yaze.plan.agent_update/1";
30
31// One agent's proposed observations, read from a "yaze.plan.agent_update/1"
32// document. `base_sha256` names the plan bytes the agent read; a file whose
33// bytes have changed since then is refused without being written.
35 std::string base_sha256;
36 std::string agent;
37 std::string observed_utc;
38 PlanJson sources = PlanJson::object();
39 // "<kind>:<id>" -> observation object. Each replaces the stored observation.
40 PlanJson observations = PlanJson::object();
41 // Observation keys to delete, including keys whose entry no longer exists.
42 std::vector<std::string> remove;
43
44 static absl::StatusOr<PlanAgentUpdate> Parse(std::string_view text);
45};
46
48
53
55 std::vector<PlanAgentChange> changes;
56 bool sources_changed = false;
57
58 bool HasChanges() const;
59};
60
62
63// Validates the update against `plan` and reports what applying it changes.
64// Comparison ignores the "agent" and "observed_utc" stamps, so resending the
65// same facts later is reported as unchanged.
66absl::StatusOr<PlanAgentDiff> DiffPlanAgentUpdate(
67 const ProjectPlan& plan, const PlanAgentUpdate& update);
68
69// Validates, then writes only `plan->extra["agent_status"]`. A rejected update
70// leaves `plan` untouched; an unchanged update leaves it byte-identical.
71absl::StatusOr<PlanAgentDiff> ApplyPlanAgentUpdate(
72 ProjectPlan* plan, const PlanAgentUpdate& update);
73
75 std::string before_sha256;
76 std::string after_sha256;
78 bool written = false;
79};
80
81// Reads the plan at `path`, refuses a stale `base_sha256`, applies the update,
82// and publishes through the shared source-CAS publisher. A dry run, a refused
83// update and an unchanged update never write the file. `base_sha256` may be
84// empty only for a dry run.
85absl::StatusOr<PlanAgentFileResult> ApplyPlanAgentUpdateToFile(
86 const std::string& path, const PlanAgentUpdate& update, bool dry_run);
87
88// The "agent_status" section, or nullptr when the plan has none.
89const PlanJson* FindPlanAgentStatus(const ProjectPlan& plan);
90// The stored observation for `key` ("step:D4"), or nullptr.
92 std::string_view key);
93// Observation keys whose step, item, decision or milestone no longer exists.
94std::vector<std::string> OrphanedPlanAgentObservations(const ProjectPlan& plan);
95// Moves an observation when its entry's ID is renamed.
96void RenamePlanAgentObservation(ProjectPlan* plan, std::string_view before,
97 std::string_view after);
98
99// A draft loaded from `loaded_source` can be saved over `disk_source` only
100// when the two differ in nothing but agent status. Then the disk's agent
101// status is copied into `draft` and this returns true; otherwise, including
102// when either source cannot be parsed, `draft` is untouched and this returns
103// false.
105 std::string_view loaded_source,
106 std::string_view disk_source);
107
108} // namespace yaze::core
109
110#endif // YAZE_CORE_PROJECT_PLAN_AGENT_H_
void RenamePlanAgentObservation(ProjectPlan *plan, std::string_view before, std::string_view after)
const PlanJson * FindPlanAgentStatus(const ProjectPlan &plan)
const PlanJson * FindPlanAgentObservation(const ProjectPlan &plan, std::string_view key)
constexpr std::string_view kPlanAgentUpdateSchema
absl::StatusOr< PlanAgentDiff > ApplyPlanAgentUpdate(ProjectPlan *plan, const PlanAgentUpdate &update)
absl::StatusOr< PlanAgentDiff > DiffPlanAgentUpdate(const ProjectPlan &plan, const PlanAgentUpdate &update)
constexpr std::string_view kPlanAgentStatusSchema
bool AdoptPlanAgentOnlyChanges(ProjectPlan *draft, std::string_view loaded_source, std::string_view disk_source)
const char * PlanAgentChangeKindName(PlanAgentChangeKind kind)
nlohmann::ordered_json PlanJson
absl::StatusOr< PlanAgentFileResult > ApplyPlanAgentUpdateToFile(const std::string &path, const PlanAgentUpdate &update, bool dry_run)
std::vector< std::string > OrphanedPlanAgentObservations(const ProjectPlan &plan)
constexpr std::string_view kPlanAgentStatusKey
std::vector< PlanAgentChange > changes
std::vector< std::string > remove
static absl::StatusOr< PlanAgentUpdate > Parse(std::string_view text)