| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- using Inversion;
- using Spry;
- using Spry.Authentication;
- using Statum;
- using ClankerSaddle.Services;
- namespace ClankerSaddle {
- /** Sealed task id carried by the task row actions. */
- public class TaskActionPrivate : Object {
- public int64 task_id { get; set; }
- }
- /** Sealed block identity carried by {@link AnswerBlockAction}. */
- public class BlockAnswerPrivate : Object {
- public int64 task_id { get; set; }
- public int64 block_id { get; set; }
- }
- /**
- * Common base for actions sealed against a task id: materialises the
- * typed private blob and exposes the resolved id (falling back to the
- * hidden `task_id` form field).
- */
- public abstract class SealedTaskAction : TypedStatumAction<TaskActionPrivate> {
- protected override Type private_type { get { return typeof(TaskActionPrivate); } }
- /** The sealed task id, or 0 when neither blob nor form field carried one. */
- protected int64 sealed_task_id() {
- var ctx = request_private;
- return ctx != null ? ((!)ctx).task_id : Forms.integer(request, "task_id", 0);
- }
- }
- /**
- * Creates a task from the queue form (project and title required).
- * Priority clamps to 1–9; the design-task checkbox and the optional
- * parent id support design tasks and manual subtasks. Branch fields
- * are optional — `branch_from` prefills from the project's default
- * branch and `task_branch` is inherited from the parent task when
- * left blank.
- */
- public class CreateTaskAction : StatumAction {
- private TaskService tasks = inject<TaskService>();
- private CapabilityService capabilities = inject<CapabilityService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 project_id = Forms.integer(request, "project_id", 0);
- string title = Forms.field(request, "title");
- if (project_id <= 0 || title.length == 0) {
- return directives().notify("error", "Project and title are required").navigate("/tasks");
- }
- int64? parent_id = null;
- int64 parsed_parent = Forms.integer(request, "parent_task_id", 0);
- if (parsed_parent > 0) {
- parent_id = parsed_parent;
- }
- // Required capabilities: the create form's multi-select posts
- // several capability_id fields; every id must resolve before
- // the task is created. (A plain array — the Invercargill
- // collections mishandle boxed value types in foreach.)
- int64[] required = {};
- if (request.form != null) {
- foreach (var value in request.form.get_field_all("capability_id")) {
- int64 parsed = int64.parse(value.strip());
- if (parsed <= 0) {
- continue;
- }
- var capability = yield capabilities.get_capability(parsed);
- if (capability == null) {
- return directives().notify("error",
- @"No capability exists with id $(parsed)").navigate("/tasks");
- }
- required += parsed;
- }
- }
- try {
- var task = yield tasks.create_task(project_id, title,
- Forms.field_raw(request, "description"),
- Forms.integer(request, "priority", 5),
- Forms.field(request, "is_design_task") == "on", parent_id,
- Forms.optional(request, "task_branch"),
- Forms.optional(request, "branch_from"));
- if (Forms.field(request, "plan").length > 0) {
- yield tasks.update_plan(task.id, Forms.field_raw(request, "plan"));
- }
- foreach (int64 capability_id in required) {
- yield capabilities.attach_to_task(capability_id, task.id);
- }
- return directives().notify("success", @"Task '$(task.title)' queued")
- .navigate(@"/tasks/$(task.id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not create the task: $(e.message)").navigate("/tasks");
- }
- }
- }
- /**
- * Saves the editable fields of a task; the id comes from the sealed
- * {@link TaskActionPrivate} blob (the hidden form field is a
- * fallback).
- */
- public class UpdateTaskAction : SealedTaskAction {
- private TaskService tasks = inject<TaskService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- if (task_id <= 0) {
- return directives().navigate("/tasks");
- }
- try {
- int64? priority = null;
- if (Forms.field(request, "priority").length > 0) {
- priority = Forms.integer(request, "priority", 5);
- }
- yield tasks.update_task(task_id,
- Forms.optional(request, "title"),
- request.form != null ? Forms.field(request, "description") : null,
- priority,
- Forms.optional(request, "status"));
- yield tasks.set_branch_fields(task_id,
- Forms.optional(request, "task_branch"),
- Forms.optional(request, "branch_from"));
- return directives().notify("success", "Task saved").navigate(@"/tasks/$(task_id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not save the task: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /**
- * Appends a comment to a task as the signed-in user. An optional
- * `quote` field carries text the user highlighted in the terminal log
- * or an RFC body; the quote is stored with the comment and delivered
- * to the saddle as a quote block.
- */
- public class AddCommentAction : SealedTaskAction {
- private TaskService tasks = inject<TaskService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- string body = Forms.field_raw(request, "body");
- string quote = Forms.field_raw(request, "quote");
- if (task_id <= 0 || body.length == 0) {
- return directives().notify("error", "A comment body is required").navigate(@"/tasks/$(task_id)");
- }
- var me = SpryAuth.current(request);
- try {
- yield tasks.add_comment(task_id, @"user:$(me.username)", body, quote);
- yield tasks.log_session_entry(task_id, "user-comment",
- @"comment by user:$(me.username)", quote.length > 0 ? "> " + quote.replace("\n", "\n> ") + "\n\n" + body : body);
- // Terminals post with a return_to so the live view survives
- // the comment; every other caller lands on the task page.
- string return_to = Forms.field(request, "return_to");
- string destination = return_to.length > 0 && return_to.has_prefix("/")
- ? return_to : @"/tasks/$(task_id)";
- return directives().notify("success", "Comment added").navigate(destination);
- } catch (Error e) {
- return directives().notify("error", @"Could not add the comment: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /**
- * Saves the task's markdown plan from the web UI's plan editor (the
- * human counterpart of the agent's `saddle plan`).
- */
- public class UpdatePlanAction : SealedTaskAction {
- private TaskService tasks = inject<TaskService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- if (task_id <= 0) {
- return directives().navigate("/tasks");
- }
- try {
- yield tasks.update_plan(task_id, Forms.field_raw(request, "plan"));
- return directives().notify("success", "Plan saved").navigate(@"/tasks/$(task_id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not save the plan: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /**
- * Records the human's answer to a block; the task and block ids come
- * from the sealed {@link BlockAnswerPrivate} blob. Question blocks
- * submit either a chosen `option` or the free-text `other` field.
- * Answers are stored verbatim — the saddle daemon applies its own
- * "Human selected: …" formatting, so a stored prefix would wrap the
- * answer a second time.
- */
- public class AnswerBlockAction : TypedStatumAction<BlockAnswerPrivate> {
- private TaskService tasks = inject<TaskService>();
- private UserService users = inject<UserService>();
- protected override Type private_type { get { return typeof(BlockAnswerPrivate); } }
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- var ctx = request_private;
- if (ctx == null) {
- return directives().navigate("/tasks");
- }
- int64 task_id = ((!)ctx).task_id;
- string option = Forms.field(request, "option");
- string other = Forms.field_raw(request, "other");
- string answer = Forms.field_raw(request, "answer");
- if (option == "__other__") {
- answer = other.strip().length > 0 ? other.strip() : answer;
- } else if (option.length > 0) {
- answer = option;
- }
- if (answer.length == 0) {
- return directives().notify("error", "An answer is required").navigate(@"/tasks/$(task_id)");
- }
- try {
- yield tasks.answer_block(((!)ctx).block_id, answer);
- return directives().notify("success", "Answer recorded; the saddle will collect it")
- .navigate(@"/tasks/$(task_id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not answer the block: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /** Creates a child task under the sealed parent task id. */
- public class CreateSubtaskAction : SealedTaskAction {
- private TaskService tasks = inject<TaskService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- string title = Forms.field(request, "title");
- if (task_id <= 0 || title.length == 0) {
- return directives().notify("error", "A subtask title is required").navigate(@"/tasks/$(task_id)");
- }
- try {
- yield tasks.create_subtask(task_id, title, Forms.field_raw(request, "description"));
- return directives().notify("success", "Subtask created").navigate(@"/tasks/$(task_id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not create the subtask: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /** Adds a dependency edge from the sealed task to the form's target. */
- public class AddDependencyAction : SealedTaskAction {
- private TaskService tasks = inject<TaskService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- int64 depends_on = Forms.integer(request, "depends_on_task_id", 0);
- if (task_id <= 0 || depends_on <= 0) {
- return directives().notify("error", "Pick a task to depend on").navigate(@"/tasks/$(task_id)");
- }
- try {
- yield tasks.add_dependency(task_id, depends_on);
- return directives().notify("success", "Dependency added").navigate(@"/tasks/$(task_id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not add the dependency: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /**
- * Completes a task (summary and working branch from the form); closes
- * the open session and frees the saddle running it.
- */
- public class CompleteTaskAction : SealedTaskAction {
- private TaskService tasks = inject<TaskService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- if (task_id <= 0) {
- return directives().navigate("/tasks");
- }
- try {
- yield tasks.complete_task(task_id, Forms.optional(request, "summary"),
- Forms.optional(request, "working_branch"), "done");
- return directives().notify("success", "Task completed").navigate(@"/tasks/$(task_id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not complete the task: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /**
- * Cancels a task (status 'failed'); the sealed id comes from the task
- * page's cancel button.
- */
- public class CancelTaskAction : SealedTaskAction {
- private TaskService tasks = inject<TaskService>();
- private SaddleService saddles = inject<SaddleService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- if (task_id <= 0) {
- return directives().navigate("/tasks");
- }
- try {
- yield saddles.abort_live_session(tasks, task_id, "cancelled");
- yield tasks.cancel_task(task_id);
- return directives().notify("success", "Task cancelled").navigate(@"/tasks/$(task_id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not cancel the task: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /**
- * Retries a failed task: the requeue path with the failure summary
- * moved into a comment (and cleared), all history kept. The next
- * dispatch resumes the daemon-side workspace when it still exists.
- */
- public class RetryTaskAction : SealedTaskAction {
- private TaskService tasks = inject<TaskService>();
- private SaddleService saddles = inject<SaddleService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- if (task_id <= 0) {
- return directives().navigate("/tasks");
- }
- var detail = yield tasks.get_task(task_id);
- if (detail == null) {
- return directives().navigate("/tasks");
- }
- if (((!)detail).task.status != STATUS_FAILED) {
- return directives().notify("error", "Only failed tasks can be retried")
- .navigate(@"/tasks/$(task_id)");
- }
- try {
- yield saddles.abort_live_session(tasks, task_id, "retried");
- yield tasks.retry_task(task_id);
- return directives().notify("success", "Task requeued for another attempt")
- .navigate(@"/tasks/$(task_id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not retry the task: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /**
- * Manually requeues a task stuck working/blocked on a saddle that
- * went offline (the automatic three-strike path may never fire when
- * the saddle row was refreshed to offline first). The same path as
- * the dispatcher's abandon: requeue + bot comment + saddle offline.
- */
- public class ResumeTaskAction : SealedTaskAction {
- private TaskService task_service = inject<TaskService>();
- private SaddleService saddle_service = inject<SaddleService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- if (task_id <= 0) {
- return directives().navigate("/tasks");
- }
- try {
- var session = yield task_service.open_session_for_task(task_id);
- if (session != null) {
- yield saddle_service.abort_live_session(task_service, task_id, "stopped");
- yield saddle_service.mark_saddle_unreachable(((!)session).saddle_id, task_service);
- } else {
- yield task_service.requeue_task(task_id);
- }
- return directives().notify("success",
- "Task requeued; the daemon restores its context on the next dispatch")
- .navigate(@"/tasks/$(task_id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not requeue the task: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /**
- * Flips a task's subtask auto-approve toggle: when on, review blocks
- * raised by this task's descendants are answered automatically —
- * the task's own review stays a human gate.
- */
- public class ToggleAutoApproveAction : SealedTaskAction {
- private TaskService task_service = inject<TaskService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- if (task_id <= 0) {
- return directives().navigate("/tasks");
- }
- try {
- var task = yield task_service.update_task(task_id, null, null, null, null);
- if (task == null) {
- return directives().navigate("/tasks");
- }
- ((!)task).auto_approve = ((!)task).auto_approve != 0 ? 0 : 1;
- yield task_service.save_auto_approve(((!)task).id, ((!)task).auto_approve != 0);
- string state = ((!)task).auto_approve != 0 ? "on" : "off";
- return directives().notify("success",
- @"Subtask auto-approve is now $(state)")
- .navigate(@"/tasks/$(task_id)");
- } catch (Error e) {
- return directives().notify("error", @"Could not toggle auto-approve: $(e.message)")
- .navigate(@"/tasks/$(task_id)");
- }
- }
- }
- /**
- * The task detail page's polling action: rebuilds the `task` slot from
- * current data so the three-second client poll shows live comments,
- * blocks, output-driven status changes and completions. Navigates
- * nowhere; a failed reload leaves the client's state untouched.
- */
- public class RefreshTaskDataAction : SealedTaskAction {
- private TaskService task_service = inject<TaskService>();
- private ProjectService project_service = inject<ProjectService>();
- private SaddleService saddle_service = inject<SaddleService>();
- private RuleService rule_service = inject<RuleService>();
- private CapabilityService capability_service = inject<CapabilityService>();
- private UserService users = inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- int64 task_id = sealed_task_id();
- if (task_id <= 0) {
- return directives();
- }
- var state = yield TaskDetailState.build(state_service, task_service,
- project_service, saddle_service, rule_service, capability_service,
- action_registry, task_id);
- return directives().set(state);
- }
- }
- }
|