using Invercargill; using Invercargill.DataStructures; using Invercargill.Expressions; using InvercargillJson; using Spry; using Spry.Authentication; using Statum; using ClankerSaddle.Data; using ClankerSaddle.Services; namespace ClankerSaddle { /** A project link row on the goal page. */ public class GoalLinkView : Object { public int64 id { get; set; } public int64 project_id { get; set; } public string name { get; set; default = ""; } public string relationship { get; set; default = ""; } public Statum.Model.ActionDto? remove { get; set; } } /** * One goal block (question/RFC/plan revision/completion) as the * page renders it — plan 15 C1: the view over the goal-run * task's own `task_blocks` row (there is no separate goal-side * row). */ public class GoalBlockView : Object { public int64 id { get; set; } public string question { get; set; default = ""; } public string block_type { get; set; default = BLOCK_QUESTION; } public string[] options { get; set; default = {}; } public string body { get; set; default = ""; } public bool answered { get; set; default = false; } public string answer { get; set; default = ""; } public string answered_at_display { get; set; default = ""; } public string answered_by { get; set; default = ""; } public string created_display { get; set; default = ""; } public Statum.Model.ActionDto? answer_action { get; set; } /** True while this is THE pending plan review (the plan section renders its body). */ public bool is_plan_review { get; set; default = false; } /** True while this is THE pending completion review. */ public bool is_completion_review { get; set; default = false; } /** Whether the block body renders as its own preview (not duplicated by a section). */ public bool show_body { get; set; default = true; } /** Plan revisions: the answer was the stable approval verdict. */ public bool approved { get; set; default = false; } } /** * One comment on the goal thread (plan 15 C2: the run task's own * `task_comments` row). {@link is_system} marks the * harness-authored bookkeeping rows — the distinct muted system * styling, never an agent row. */ public class GoalCommentView : Object { public string author { get; set; default = ""; } public string body { get; set; default = ""; } public string quote { get; set; default = ""; } public string created_display { get; set; default = ""; } public bool from_agent { get; set; default = false; } public bool is_system { get; set; default = false; } } /** One progress line from the goal agent's runs (`saddle progress-update`). */ public class GoalProgressView : Object { public int64 id { get; set; } public string summary { get; set; default = ""; } public string when_display { get; set; default = ""; } } /** * One supervision-narrative row (plan 15 C3: the DERIVED read-time * view over the goal's lifecycle stamps, the run task's blocks * and comments, and the goal's tasks' transitions — no * goal_events rows exist). */ public class GoalEventView : Object { public string kind { get; set; default = ""; } public string text { get; set; default = ""; } public int64 task_id { get; set; } public string at_display { get; set; default = ""; } } /** One task row under the goal (the status strip's list). */ public class GoalTaskView : Object { public int64 id { get; set; } public string title { get; set; default = ""; } public string status { get; set; default = "draft"; } public string display { get; set; default = "draft"; } public string mode { get; set; default = "implement"; } public string creator { get; set; default = "human"; } public bool yeehaw { get; set; default = false; } public string updated_display { get; set; default = ""; } /** The task's required capability tags (plan 14 B5 row badges). */ public string[] capabilities { get; set; default = {}; } } /** * One effective rule of the goal (plan 14 B5): the rules that * govern the goal agent and every worker under it — the run * task's own set, the primary project's, and the co-developed * links' in link order. */ public class GoalRuleView : Object { public int64 id { get; set; } public string name { get; set; default = ""; } public string body { get; set; default = ""; } } /** One goal-agent run (a session of the goal-run task). */ public class GoalRunView : Object { public int64 id { get; set; } public string started_display { get; set; default = ""; } public string ended_display { get; set; default = ""; } public bool open { get; set; default = false; } public string outcome { get; set; default = ""; } public string terminal_url { get; set; default = ""; } /** The saddle that hosted the run ("" when unknown). */ public string saddle_name { get; set; default = ""; } } /** One branch of the daemon-reported goal push state. */ public class GoalPushBranchView : Object { public string branch { get; set; default = ""; } public bool pushed { get; set; default = false; } } /** A project option chip (the goal link form's select). */ public class GoalProjectOptionView : Object { public int64 id { get; set; } public string name { get; set; default = ""; } } /** Public root of the `/goals` slot. */ public class GoalsListPublic : Object { public Statum.Model.ActionDto create { get; set; } } /** * The scalar half of the goal page's public state (the lists ride * alongside as separate properties; see {@link GoalState.build}). */ public class GoalPublic : Object { public bool exists { get; set; } public int64 id { get; set; } public int64 project_id { get; set; } public string project_name { get; set; default = ""; } public string title { get; set; default = ""; } public string brief { get; set; default = ""; } public string branch_name { get; set; default = ""; } public string base_branch { get; set; default = ""; } public string status { get; set; default = "draft"; } public string display { get; set; default = "draft"; } public int64 priority { get; set; default = 3; } public string plan { get; set; default = ""; } public string completion_report { get; set; default = ""; } public string compare_url { get; set; default = ""; } public string created_display { get; set; default = ""; } public string started_display { get; set; default = ""; } public string completed_display { get; set; default = ""; } public bool terminal { get; set; } public int64 draft_tasks { get; set; } public int64 queued_tasks { get; set; } public int64 running_tasks { get; set; } public int64 done_tasks { get; set; } public int64 failed_tasks { get; set; } public int64 run_task_id { get; set; } /** True while a completion report awaits review. */ public bool completion_pending { get; set; } /** The saddle currently hosting the goal agent ("" while unassigned). */ public string agent_saddle { get; set; default = ""; } /** Health line for the agent's saddle ("" when none). */ public string saddle_health { get; set; default = ""; } /** The agent's newest recorded activity line ("" when none). */ public string current_command { get; set; default = ""; } public string current_command_display { get; set; default = ""; } /** The current activity's entry has not completed yet. */ public bool current_command_running { get; set; } /** * Plan 15 C4: the ONE muted activity line folded into the * status head (the Current-activity card is gone) — * "running: `cmd`" for the open round, "parked awaiting " * for the parked ones ("" when there is nothing to say). */ public string current_activity { get; set; default = ""; } public Statum.Model.ActionDto? start { get; set; } public Statum.Model.ActionDto? pause { get; set; } public Statum.Model.ActionDto? resume { get; set; } public Statum.Model.ActionDto? cancel { get; set; } /** * Plan 14 B1 parity: re-offer a failed goal's supervision (the * task page's Retry, worded for the run). */ public Statum.Model.ActionDto? retry_run { get; set; } /** * Plan 14 B1 parity: the run is stale/parked on a dead saddle * — requeue it; the next dispatch restores the workspace. */ public Statum.Model.ActionDto? requeue_run { get; set; } public Statum.Model.ActionDto? comment { get; set; } public Statum.Model.ActionDto? add_link { get; set; } /** The sealed GET refresh action (live pulse rehydration). */ public Statum.Model.ActionDto? refresh { get; set; } } /** * State builder shared by the goals list row and the goal page: * fills a {@link GoalPublic} plus the section lists from a goal * row. `full` builds the narrative/runs/sections the list skips. */ public class GoalState : Object { /** * Builds the goal page's whole slot from `goal_id` — the * shape the entrypoint sets and the sealed refresh action * re-hydrates. `exists` is false when the goal is missing. */ public static async Statum.Slot build(int64 goal_id, GoalService goals, TaskService tasks, ProjectService projects, InvercargillSql.Orm.OrmSession db, ActionRegistry? actions, StateService state_service, SaddleService? saddles = null, SaddlePollState? polls = null, RuleService? rules = null) throws GLib.Error { var pub = new GoalPublic(); var links = new Vector(); var blocks = new Vector(); var blocks_open = new Vector(); var blocks_answered = new Vector(); var goal_tasks = new Vector(); var events = new Vector(); var comments = new Vector(); var runs = new Vector(); var push_branches = new Vector(); var progress = new Vector(); var effective_rules = new Vector(); GoalBlockView? plan_pending = null; GoalBlockView? completion_pending = null; var plan_revisions = new Vector(); var project_options = new Vector(); var goal = yield goals.get_goal(goal_id); if (goal != null) { yield populate(((!)goal), goals, tasks, db, actions, true, pub, links, blocks, goal_tasks, events, comments, runs, push_branches, progress, saddles, polls, blocks_open, blocks_answered); foreach (var block in blocks) { if (block.is_plan_review && plan_pending == null) { plan_pending = block; } if (block.is_completion_review && completion_pending == null) { completion_pending = block; } if (block.block_type == BLOCK_PLAN) { plan_revisions.add(block); } } foreach (var project in yield projects.list_projects()) { if (project.id != ((!)goal).project_id) { project_options.add(new GoalProjectOptionView() { id = project.id, name = project.name }); } } // Plan 14 B5: the goal's effective rules — the set a // dispatch ships for the run (and every worker): // run-task rules, the primary project's, then the // co-developed goal links' in link order. if (rules != null) { var scope_task = new TaskEntity() { id = pub.run_task_id, project_id = ((!)goal).project_id }; foreach (var rule in yield ((!)rules).effective_rules_for( scope_task, projects, ((!)goal).id)) { effective_rules.add(new GoalRuleView() { id = rule.id, name = rule.name, body = rule.body }); } } } if (actions != null && goal != null) { pub.refresh = ((!)actions) .author_private( new GoalActionPrivate() { goal_id = goal_id }, "GET"); } var pub_root = (PropertyDictionary) GObjectMapping.to_properties(pub); pub_root["links"] = StateJson.list(links.to_immutable_buffer()); pub_root["blocks"] = StateJson.list_rich(blocks.to_immutable_buffer(), (item, dict) => { dict["options"] = StateJson.strings( ((GoalBlockView) item).options ?? new string[0]); }); pub_root["open_blocks"] = StateJson.list_rich( blocks_open.to_immutable_buffer(), (item, dict) => { dict["options"] = StateJson.strings( ((GoalBlockView) item).options ?? new string[0]); }); pub_root["answered_blocks"] = StateJson.list_rich( blocks_answered.to_immutable_buffer(), (item, dict) => { dict["options"] = StateJson.strings( ((GoalBlockView) item).options ?? new string[0]); }); pub_root["goal_tasks"] = StateJson.list_rich( goal_tasks.to_immutable_buffer(), (item, dict) => { dict["capabilities"] = StateJson.strings( ((GoalTaskView) item).capabilities ?? new string[0]); }); pub_root["events"] = StateJson.list(events.to_immutable_buffer()); pub_root["comments"] = StateJson.list(comments.to_immutable_buffer()); pub_root["runs"] = StateJson.list(runs.to_immutable_buffer()); pub_root["progress"] = StateJson.list( progress.to_immutable_buffer()); pub_root["push_branches"] = StateJson.list( push_branches.to_immutable_buffer()); pub_root["plan_revisions"] = StateJson.list( plan_revisions.to_immutable_buffer()); pub_root["rules"] = StateJson.list( effective_rules.to_immutable_buffer()); pub_root["project_options"] = StateJson.list( project_options.to_immutable_buffer()); pub_root["plan_pending_block"] = plan_pending != null ? new JsonElement.from_properties( (PropertyDictionary) GObjectMapping.to_properties((!)plan_pending)) : new JsonElement.from_element(new NativeElement(false)); pub_root["completion_pending_block"] = completion_pending != null ? new JsonElement.from_properties( (PropertyDictionary) GObjectMapping.to_properties((!)completion_pending)) : new JsonElement.from_element(new NativeElement(false)); return state_service.new_slot(Scope.PAGE, new Statum.State() { type_name = "goal", public_data = pub_root, private_data = new PropertyDictionary() }); } public static async void populate(Goal goal, GoalService goals, TaskService tasks, InvercargillSql.Orm.OrmSession db, ActionRegistry? actions, bool full, GoalPublic pub, Vector links, Vector blocks, Vector goal_tasks, Vector events, Vector comments, Vector runs, Vector push_branches, Vector? progress = null, SaddleService? saddles = null, SaddlePollState? polls = null, Vector? blocks_open = null, Vector? blocks_answered = null) throws GLib.Error { pub.exists = true; pub.id = goal.id; pub.project_id = goal.project_id; pub.title = goal.title; pub.brief = goal.brief; pub.branch_name = goal.branch_name; pub.base_branch = goal.base_branch ?? ""; pub.status = goal.status; // Plan 16 S5 (U3): a goal awaiting its completion review // reads "awaiting your review" — the run-task status card // agrees (no bare "blocked" for review parks). pub.display = goal.status == GOAL_COMPLETION_REVIEW ? "awaiting your review" : goal.status.replace("_", " "); pub.priority = goal.priority; pub.plan = goal.plan ?? ""; pub.completion_report = goal.completion_report ?? ""; pub.created_display = format_stamp(goal.created_at); pub.started_display = format_stamp(goal.started_at); pub.completed_display = format_stamp(goal.completed_at); pub.terminal = goal.status == GOAL_COMPLETE || goal.status == GOAL_FAILED || goal.status == GOAL_CANCELLED; var project = yield db.query() .where(expr("id == $0", elem(goal.project_id))) .first_async(); if (project != null) { pub.project_name = ((!)project).name; string base_name = goal.base_branch != null && ((!)goal.base_branch).length > 0 ? ((!)goal.base_branch) : ((!)project).default_branch; pub.compare_url = @"$(TaskService.https_git_url(((!)project).git_url))/compare/$(base_name)...$(goal.branch_name)"; } if (actions != null) { var sealed = new GoalActionPrivate() { goal_id = goal.id }; pub.comment = ((!)actions).author_private(sealed); pub.add_link = ((!)actions).author_private(sealed); if (goal.status == GOAL_DRAFT) { pub.start = ((!)actions).author_private(sealed); } if (goal.status == GOAL_ACTIVE) { pub.pause = ((!)actions).author_private(sealed); } if (goal.status == GOAL_PAUSED) { pub.resume = ((!)actions).author_private(sealed); } if (!pub.terminal) { pub.cancel = ((!)actions).author_private(sealed); } } foreach (var link in yield goals.list_links(goal.id)) { var view = new GoalLinkView() { id = link.id, project_id = link.project_id, relationship = link.relationship, remove = ((!)actions).author_private( new GoalActionPrivate() { goal_id = goal.id }) }; var linked = yield db.query() .where(expr("id == $0", elem(link.project_id))) .first_async(); if (linked != null) { view.name = ((!)linked).name; } links.add(view); } bool completion_pending = false; bool plan_pending_open = false; // Plan 15 C1: the goal's blocks ARE the goal-run task's // blocks — one row per question, answered in place. foreach (var block in yield goals.list_blocks(goal.id)) { bool pending_plan = block.block_type == BLOCK_PLAN && block.answer == null; // The completion review may ride either kind: the web's // completion raise or the daemon's done-park review block // (adopted — one row, whichever landed first). bool pending_completion = block.answer == null && (block.block_type == BLOCK_COMPLETION || block.block_type == BLOCK_REVIEW); var view = new GoalBlockView() { id = block.id, question = block.question, block_type = block.block_type, options = EntryJson.parse_string_array(block.options), body = block.body ?? "", answered = block.answer != null, answer = block.answer ?? "", answered_at_display = format_stamp(block.answered_at), answered_by = block.answered_by ?? "", created_display = format_stamp(block.created_at), is_plan_review = pending_plan, is_completion_review = pending_completion, // A pending plan hides its body preview (the plan // section renders it); the pending completion review // SHOWS its body — the block IS the review surface. show_body = !pending_plan, approved = block.answer != null && block.answer == NOTICE_PLAN_APPROVED }; if (block.answer == null && actions != null) { view.answer_action = ((!)actions) .author_private( new GoalBlockAnswerPrivate() { goal_id = goal.id, block_id = block.id }); } if (pending_completion) { completion_pending = true; } if (pending_plan) { plan_pending_open = true; } blocks.add(view); } pub.completion_pending = completion_pending; // Plan 15 C4: the current (unanswered) question/RFC blocks // render in full; the historic answered ones fold behind // the answered-blocks expander (the task page's pattern). var open_sink = blocks_open ?? new Vector(); var answered_sink = blocks_answered ?? new Vector(); var block_array = blocks.to_array(); GoalBlockView? pending_review = null; for (var index = block_array.length - 1; index >= 0; index--) { var view = (GoalBlockView) block_array[index]; if (view.answered) { answered_sink.add(view); } else if (view.is_plan_review) { // rendered by the plan section } else if (view.is_completion_review) { // AT MOST ONE pending completion-family block renders // (the rich review form): pre-dedupe twins may exist in // stale data — the first pending row owns the review, // any others stay hidden until the verdict answers // them all. if (pending_review == null) { pending_review = view; open_sink.add(view); } } else { open_sink.add(view); } } var display_map = yield task_display_map(tasks, goal.id); foreach (var task in yield tasks.list_tasks_for_goal(goal.id)) { if (task.kind == TASK_KIND_GOAL_RUN) { pub.run_task_id = task.id; continue; } var view = new GoalTaskView() { id = task.id, title = task.title, status = task.status, mode = task.mode, creator = task.creator, updated_display = format_stamp(task.completed_at ?? task.created_at) }; // Plan 14 B5: the row's required-capability badges — // read straight off the task_capabilities links (no // editing here). var task_caps = new Vector(); foreach (var link in yield db.query() .where(expr("task_id == $0", elem(task.id))) .order_by(expr("id")) .materialise_async()) { var capability = yield db.query() .where(expr("id == $0", elem(link.capability_id))) .first_async(); if (capability != null) { task_caps.add(((!)capability).name); } } view.capabilities = task_caps.to_array(); var display = display_map.get_or_default(task.id.to_string()); view.display = display != null ? ((!)display).display : task.status; view.yeehaw = display != null && ((!)display).yeehaw; goal_tasks.add(view); switch (task.status) { case STATUS_DRAFT: pub.draft_tasks++; break; case STATUS_QUEUED: case STATUS_ASSIGNED: pub.queued_tasks++; break; case STATUS_WORKING: case STATUS_BLOCKED: case STATUS_REVIEW_REQUESTED: case STATUS_SUSPENDED: pub.running_tasks++; break; case STATUS_DONE: pub.done_tasks++; break; case STATUS_FAILED: pub.failed_tasks++; break; default: break; } } if (!full) { return; } // Plan 14 B1: run-control parity on the status head — // "Retry supervision" for a failed goal, requeue-to-resume // for a run stale/parked on a dead saddle (NO notify-me // toggle: goals notify unconditionally by design). if (actions != null && pub.run_task_id > 0) { var run_row = yield db.query() .where(expr("id == $0", elem(pub.run_task_id))) .first_async(); if (run_row != null) { string run_status = ((!)run_row).status; if (goal.status == GOAL_FAILED && run_status == STATUS_FAILED) { pub.retry_run = ((!)actions) .author_private( new GoalActionPrivate() { goal_id = goal.id }); } bool stale_or_parked = run_status == STATUS_WORKING || run_status == STATUS_BLOCKED || run_status == STATUS_REVIEW_REQUESTED || run_status == STATUS_SUSPENDED; if (!pub.terminal && stale_or_parked) { var open_run = yield tasks.open_session_for_task(pub.run_task_id); if (open_run != null) { var host = yield db.query() .where(expr("id == $0", elem(((!)open_run).saddle_id))) .first_async(); if (host != null && ((!)host).status == "offline") { pub.requeue_run = ((!)actions) .author_private( new GoalActionPrivate() { goal_id = goal.id }); } } } } } // Plan 15 C3: the narrative derives at read time (the // service returns newest-first rows). foreach (var row in yield goals.narrative(goal.id)) { events.add(new GoalEventView() { kind = row.kind, text = row.text, task_id = row.task_id, at_display = format_stamp(row.at) }); } // Plan 15 C2: the goal's comment thread IS the run // task's task_comments (injection rides the existing // delivery paths) — the agent's pushes and the human's // composer land on the same rows. foreach (var row in yield goals.list_comments(goal.id)) { comments.add(new GoalCommentView() { author = row.author, body = row.body, quote = row.quote ?? "", created_display = format_stamp(row.created_at), from_agent = row.author == "agent", is_system = row.author == COMMENT_AUTHOR_SYSTEM }); } int64 open_session_saddle = 0; if (pub.run_task_id > 0) { // The goal agent's live progress feed (green, task-page // pattern) — the run task's own progress lines. if (progress != null) { foreach (var row in yield tasks.list_progress(pub.run_task_id)) { ((!)progress).add(new GoalProgressView() { id = row.id, summary = row.summary, when_display = format_stamp(row.created_at) }); } } var session_rows = yield db.query() .where(expr("task_id == $0", elem(pub.run_task_id))) .order_by_desc(expr("id")) .materialise_async(); foreach (var session in session_rows) { string run_saddle = ""; if (saddles != null) { var saddle_row = yield ((!)saddles).get_saddle(session.saddle_id); run_saddle = saddle_row != null ? ((!)saddle_row).name : @"saddle #$(session.saddle_id)"; } runs.add(new GoalRunView() { id = session.id, started_display = format_stamp(session.started_at), ended_display = format_stamp(session.ended_at), open = session.ended_at == null, outcome = session.outcome ?? "", saddle_name = run_saddle, // Deep-links this run's terminal view: the // picker's client-side ?session_id= // selection lands on exactly this session. terminal_url = @"/tasks/$(pub.run_task_id)/terminal?session_id=$(session.id)" }); } // Current activity (task-page parity): the open round's // hosting saddle, its health and the newest recorded // activity line. var open = yield tasks.open_session_for_task(pub.run_task_id); if (open != null) { open_session_saddle = ((!)open).saddle_id; if (saddles != null) { var saddle_row = yield ((!)saddles).get_saddle(((!)open).saddle_id); if (saddle_row != null) { pub.agent_saddle = ((!)saddle_row).name; if (polls != null) { string daemon_state = ""; int64 uptime = 0; int64 activity = 0; int64 contact_ms = 0; ((!)polls).health_for(((!)saddle_row).id, out daemon_state, out uptime, out activity, out contact_ms); if (daemon_state.length > 0) { pub.saddle_health = @"saddle '$(((!)saddle_row).name)' — daemon $(daemon_state)"; } } } } SessionCommand? newest = null; foreach (var command in yield db.query() .where(expr("session_id == $0", elem(((!)open).id))) .order_by(expr("id")) .materialise_async()) { newest = command; } if (newest != null) { // Plan 13 B4: body-first for conversation-level // entries — the output (the comment body, the // failure explanation) leads; only a bare // command renders its command line, so the // literal `saddle comment` echo never leaves // the terminal. string line = ((!)newest).command_text; if (((!)newest).entry_type != "command" && (((!)newest).output_text ?? "").length > 0) { line = (!)newest.output_text; } if (line.length > 0) { pub.current_command = clip_line(line, 200); pub.current_command_display = format_stamp(((!)newest).started_at); pub.current_command_running = ((!)newest).completed_at == null; } } } } // The one muted activity line (plan 15 C4): what the run is // doing right now — running the open round, or parked // awaiting a review / a question / its tasks. var activity_run = pub.run_task_id > 0 ? yield db.query() .where(expr("id == $0", elem(pub.run_task_id))) .first_async() : null; if (activity_run != null) { if (((!)activity_run).status == STATUS_WORKING || ((!)activity_run).status == STATUS_ASSIGNED) { if (pub.current_command.length > 0) { pub.current_activity = pub.current_command_running ? @"running: `$(pub.current_command)`" : @"last: `$(pub.current_command)`"; } } else if (((!)activity_run).status == STATUS_BLOCKED || ((!)activity_run).status == STATUS_REVIEW_REQUESTED) { if (completion_pending) { pub.current_activity = "parked awaiting the completion review"; } else if (plan_pending_open) { pub.current_activity = "parked awaiting the plan review"; } else if (open_sink.length > 0) { pub.current_activity = "parked awaiting a question"; } else { pub.current_activity = "parked"; } } else if (((!)activity_run).status == STATUS_SUSPENDED) { pub.current_activity = "parked awaiting its tasks' terminal transitions"; } } // Plan 15 C4: the push chips derive from the LIVE // daemon-reported per-repo flags of the run's hosting // saddle (the last /status probe) — never a stored // snapshot. A restart with no probe yet renders none. if (open_session_saddle > 0 && polls != null) { string flags = ((!)polls).goal_branches_for(open_session_saddle); if (flags.length > 0) { // The SAME parser the completion-accept gate reads // (GoalService.parse_goal_branches) feeds the chips — // gate and display cannot diverge on one payload. foreach (var row in GoalService.parse_goal_branches(flags)) { push_branches.add(new GoalPushBranchView() { branch = row.label(), pushed = row.pushed }); } } } } /** * The goal tasks' display states + yeehaw badges in one map — * the task page's computed display (`display_of_detail`) with * the queue batch lookups. */ private static async Dictionary task_display_map( TaskService tasks, int64 goal_id) throws GLib.Error { var map = new Dictionary(); var listed = new Vector(); foreach (var task in yield tasks.list_tasks_for_goal(goal_id)) { if (task.kind != TASK_KIND_GOAL_RUN) { listed.add(task); } } var display_map = yield tasks.display_map_for(listed.to_immutable_buffer()); var yeehaw_ids = yield tasks.yeehaw_task_ids(); foreach (var task in listed) { var display = display_map.get_or_default(task.id.to_string()); map.set(task.id.to_string(), new GoalTaskDisplay() { display = display != null ? ((!)display).status : task.status, yeehaw = yeehaw_ids.has(task.id.to_string()) }); } return map; } private static string format_stamp(DateTime? stamp) { return stamp != null ? ((!)stamp).format("%Y-%m-%d %H:%M") : ""; } /** A flattened, clipped activity line (the current-activity display). */ private static string clip_line(string text, int limit) { string flat = text.replace("\n", " ⏎ ").strip(); return flat.length > limit ? flat.substring(0, limit) + "…" : flat; } } /** Display + badge pair for a goal task row. */ public class GoalTaskDisplay : Object { public string display { get; set; default = ""; } public bool yeehaw { get; set; } } /** * Entrypoint for /goals: the goals dashboard — every goal (newest * first) with lifecycle badges, branch, priority and task counts, * plus the create form. The page subscribes to the global pulse * and re-renders whenever goal or task state changes. */ public class GoalsEntrypoint : StatumEntrypoint { private GoalService goal_service = Inversion.inject(); private TaskService task_service = Inversion.inject(); private ProjectService project_service = Inversion.inject(); private InvercargillSql.Orm.OrmSession db = Inversion.inject(); private UserService users = Inversion.inject(); public override async DirectiveBuilder handle() throws GLib.Error { var guard = yield SpryAuth.require_login(directives(), request, users); if (guard != null) { return (!)guard; } var rows = new JsonArray(); foreach (var goal in yield goal_service.list_goals()) { var pub = new GoalPublic(); var links = new Vector(); var blocks = new Vector(); var goal_tasks = new Vector(); var events = new Vector(); var comments = new Vector(); var runs = new Vector(); var push_branches = new Vector(); yield GoalState.populate(goal, goal_service, task_service, db, action_registry, false, pub, links, blocks, goal_tasks, events, comments, runs, push_branches); var row = (PropertyDictionary) GObjectMapping.to_properties(pub); row["plan_pending"] = new JsonElement.from_element( new NativeElement(goal.status == GOAL_PLAN_REVIEW)); rows.add(new JsonElement.from_properties(row)); } var project_options = new Vector(); foreach (var project in yield project_service.list_projects()) { project_options.add(new GoalProjectOptionView() { id = project.id, name = project.name }); } var pub_root = (PropertyDictionary) GObjectMapping.to_properties( new GoalsListPublic() { create = action_registry.author() }); pub_root["goals"] = new JsonElement.from_elements(rows); pub_root["projects"] = StateJson.list( project_options.to_immutable_buffer()); return directives().set(state_service.new_slot(Scope.PAGE, new Statum.State() { type_name = "goals", public_data = pub_root, private_data = new PropertyDictionary() })).subscribe_global(StatePulse.SLOT_TYPE); } } /** * Entrypoint for /goals/{goal_id}: the goal page — status head * with the lifecycle actions, the locked brief, the approved plan * (rejected revisions under the historical-blocks expander), the * task strip + list, the supervision narrative, the comment * thread, the completion review (report + push state + compare * link) and the goal agent's runs with their terminal views. * Subscribes to the global pulse for live re-renders. */ public class GoalDetailEntrypoint : StatumEntrypoint { private GoalService goal_service = Inversion.inject(); private TaskService task_service = Inversion.inject(); private ProjectService project_service = Inversion.inject(); private SaddleService saddle_service = Inversion.inject(); private SaddlePollState poll_state = Inversion.inject(); private RuleService rule_service = Inversion.inject(); private InvercargillSql.Orm.OrmSession db = Inversion.inject(); private UserService users = Inversion.inject(); public override async DirectiveBuilder handle() throws GLib.Error { var guard = yield SpryAuth.require_login(directives(), request, users); if (guard != null) { return (!)guard; } int64 goal_id = int64.parse(request.route("goal_id") ?? "0"); var state = yield GoalState.build(goal_id, goal_service, task_service, project_service, db, action_registry, state_service, saddle_service, poll_state, rule_service); return directives().set(state).subscribe_global(StatePulse.SLOT_TYPE); } } }