From 19bfda15c820bd40056dbdd9a0ecc005e18b2dee Mon Sep 17 00:00:00 2001 From: Armand <174164929+NanoMars@users.noreply.github.com> Date: Thu, 5 Nov 2026 15:55:40 -0500 Subject: [PATCH] Private ship windows for D-155 --- app/mcp/admin_mcp.rb | 6 +-- app/mcp/admin_mcp/tools/projects.rb | 44 ++++++++++++++++++ app/models/project.rb | 15 +++++-- app/models/ship.rb | 12 +++-- app/views/projects/show.html.erb | 2 +- ...261105210000_add_ship_until_to_projects.rb | 6 +++ db/schema.rb | 5 ++- test/controllers/admin/mcp_controller_test.rb | 23 +++++++++- test/models/project_test.rb | 45 +++++++++++++++++++ 9 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20261105210000_add_ship_until_to_projects.rb diff --git a/app/mcp/admin_mcp.rb b/app/mcp/admin_mcp.rb index b5e8aee..006c84c 100644 --- a/app/mcp/admin_mcp.rb +++ b/app/mcp/admin_mcp.rb @@ -6,7 +6,7 @@ # |---|---|---| # | Read | admin_summary, find_people, get_person, find_projects, get_project, find_ships, get_ship, list_orders, list_extensions, metrics, nudges, public_stats | Tools::Overview, People, Projects, Ships, Orders, Extensions, Numbers | # | Analytics | describe_schema, query_sql, funnel, audit_log | Tools::Analytics, Tools::Audit | -# | Write | save_review_draft, set_lapse_coding, set_reviewer_ai, set_checklist_item, set_hours_start, edit_snapshot, edit_review_text, send_verdict_dm, mark_order_ordered, decide_extension | The same model methods the admin pages call (Nudge::ChangesNeeded and Nudge::Rejected for send_verdict_dm, Order#ordered!, Extension#decide!) | +# | Write | save_review_draft, set_lapse_coding, set_reviewer_ai, set_checklist_item, set_hours_start, edit_snapshot, edit_review_text, send_verdict_dm, mark_order_ordered, decide_extension, set_ship_until | The same model methods the admin pages call (Nudge::ChangesNeeded and Nudge::Rejected for send_verdict_dm, Order#ordered!, Extension#decide!) | # # Before a tool runs, AdminMcp::Tool writes an AdminAuditEvent. A tool whose audit row fails does not run. module AdminMcp @@ -14,7 +14,7 @@ module AdminMcp Save Point admin tools. Most people in this data are teenagers: share only what the task needs. Start with describe_schema, or a typed finder (find_people, find_projects, find_ships) before query_sql. Every call is written to the admin log, with the tool and the IDs it names. - Write tools (save_review_draft, set_lapse_coding, set_reviewer_ai, set_checklist_item, set_hours_start, edit_snapshot, edit_review_text, send_verdict_dm, mark_order_ordered, decide_extension) + Write tools (save_review_draft, set_lapse_coding, set_reviewer_ai, set_checklist_item, set_hours_start, edit_snapshot, edit_review_text, send_verdict_dm, mark_order_ordered, decide_extension, set_ship_until) change real data, and ordering, deciding an extension or send_verdict_dm sends the participant a Slack DM: confirm each with the human first. Everything on the ship review page saves through a tool except the verdict and the screenshots: set_lapse_coding and set_reviewer_ai change the limits at once. No tool gives a verdict (D-136): the reviewer presses it on the page. @@ -29,7 +29,7 @@ class Refused < StandardError; end # Every tool, for the server to offer. def self.tools [ Tools::Overview::AdminSummary, Tools::People::FindPeople, Tools::People::GetPerson, - Tools::Projects::FindProjects, Tools::Projects::GetProject, + Tools::Projects::FindProjects, Tools::Projects::GetProject, Tools::Projects::SetShipUntil, Tools::Ships::FindShips, Tools::Ships::GetShip, Tools::Ships::SaveReviewDraft, Tools::Ships::SetLapseCoding, Tools::Ships::SetReviewerAi, Tools::Ships::SetChecklistItem, Tools::Ships::SetHoursStart, Tools::Ships::EditSnapshot, Tools::Ships::EditReviewText, Tools::Ships::SendVerdictDm, diff --git a/app/mcp/admin_mcp/tools/projects.rb b/app/mcp/admin_mcp/tools/projects.rb index c314b74..ea6a818 100644 --- a/app/mcp/admin_mcp/tools/projects.rb +++ b/app/mcp/admin_mcp/tools/projects.rb @@ -47,4 +47,48 @@ def self.run(args, _caller) ) end end + + # D-155: a private ship window for one project. No DM, no post: the admin tells the person. + class SetShipUntil < AdminMcp::Tool + writes "set_ship_until", "Give one project a private ship window", + "Gives one project a private window to ship after the public end (D-155): the project may ship until ship_until, and only Hackatime time from before the public end (Program::ENDS_AT) counts on that ship. " \ + "Send user_id or project_id, and ship_until as a UTC date and time like 2026-11-06T21:00, or null to clear it. It never shortens the public end or a Changes needed window, and an unset project is unchanged. " \ + "Sends no DM and posts nothing: tell the person yourself. Answers { project_id, user_id, ship_until, ship_closes_at }.", + { user_id: { type: "integer", description: "The participant's user ID. Use this or project_id." }, + project_id: { type: "integer", description: "The project ID. Use this or user_id." }, + ship_until: { type: %w[string null], description: "A UTC date and time, or null to clear the window." } }, + required: %w[ship_until] + + def self.audit_extra(args) + project = find_project(args) + project ? { "project_id" => project.id } : {} + rescue StandardError + {} + end + + def self.run(args, _caller) + raise AdminMcp::Refused, "Send ship_until: a UTC date and time, or null to clear it." unless args.key?(:ship_until) + + project = find_project(args) || raise(AdminMcp::Refused, "Send user_id or project_id of a project that exists.") + value = args[:ship_until].nil? ? nil : parse_time(args[:ship_until]) + raise AdminMcp::Refused, "Send ship_until as a UTC date and time like 2026-11-06T21:00, or null." if value == :invalid + + project.update!(ship_until: value) + { project_id: project.id, user_id: project.user_id, ship_until: project.ship_until, ship_closes_at: project.ship_closes_at } + end + + def self.find_project(args) + if args[:project_id] then Project.find_by(id: args[:project_id]) + elsif args[:user_id] then Project.find_by(user_id: args[:user_id]) + end + end + + def self.parse_time(text) + return :invalid if text.blank? + + Time.find_zone!("UTC").parse(text.to_s) || :invalid + rescue ArgumentError + :invalid + end + end end diff --git a/app/models/project.rb b/app/models/project.rb index 196a517..ef71baa 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -586,10 +586,18 @@ def hours_logged(refresh: false) @hours_logged = nil if refresh @hours_logged ||= begin refresh_hours if refresh - user.hours_in(Program::FIRST_STREAK_DATE..) + user.hours_in(Program::FIRST_STREAK_DATE..hours_last_day) end end + # D-155: a project with an admin-set ship window (ship_until) counts only time from before the public end once that + # end has passed: the streak day the end falls on, in the person's zone, is the last one. nil (no limit) for everyone else. + def hours_last_day + return unless ship_until && Time.current > Program::ENDS_AT + + StreakActivity.streak_date_for(Program::ENDS_AT, user.timezone) + end + # D-065: Hackatime time counts from the later of Save Point's start and your sign-up, unless a reviewer set a start on # this project (hours_start_override). D-066: a jame gam submission of this game moves it later still, whatever its review # status. Nothing counts earlier, so the streak sync clips every span here and the ship's hours, the Airtable dates and @@ -740,9 +748,10 @@ def changes_needed_until [ latest.reviewed_at + CHANGES_NEEDED_WINDOW, Program::ENDS_AT ].max end - # The last moment this person may ship: the cutoff (Program::ENDS_AT), or their Changes needed deadline (D-140) when it runs later. + # The last moment this person may ship: the cutoff (Program::ENDS_AT), or their Changes needed deadline (D-140) or the + # private window an admin set (ship_until, D-155) when it runs later. def ship_closes_at - [ ship_cutoff, changes_needed_until ].compact.max + [ ship_cutoff, changes_needed_until, ship_until ].compact.max end # Until then, unless an admin banned the account or rejected the newest ship for good (D-063). diff --git a/app/models/ship.rb b/app/models/ship.rb index d706b19..7719821 100644 --- a/app/models/ship.rb +++ b/app/models/ship.rb @@ -171,7 +171,13 @@ def by?(user) = project.user_id == user.id def counted_hours return 0 unless project&.user - project.user.hours_in(Program::FIRST_STREAK_DATE..(created_at || Time.current).to_date).to_d.clamp(0, hours) + last_day = [ (created_at || Time.current).to_date, project.hours_last_day ].compact.min + project.user.hours_in(Program::FIRST_STREAK_DATE..last_day).to_d.clamp(0, hours) + end + + # D-155: where this ship's Hackatime time stops: now, or the public end for a project with a private ship window (ship_until). + def hours_end + project.ship_until && Time.current > Program::ENDS_AT ? Program::ENDS_AT : Time.current end # D-063: the seconds on each linked Hackatime project from the hours start (D-065) to now, kept as hackatime_seconds when the @@ -180,7 +186,7 @@ def capture_hackatime_seconds user = project&.user return unless user&.hackatime_linked? && hackatime_projects.any? - self.hackatime_seconds = Hackatime.project_seconds(user, hackatime_projects, project.hours_start..Time.current) + self.hackatime_seconds = Hackatime.project_seconds(user, hackatime_projects, project.hours_start..hours_end) rescue Hackatime::NotLinked, Hackatime::Expired, Hackatime::Unavailable nil end @@ -441,7 +447,7 @@ def fetch_hackatime_categories! user = project&.user return unless user&.hackatime_linked? - window = { from: project.hours_start, to: created_at.utc } + window = { from: project.hours_start, to: [ created_at.utc, hours_end ].min } ai = Hackatime.category_seconds(user, hackatime_projects, AI_CODING_CATEGORY, **window) art_parts = ART_CATEGORIES.map { |category| Hackatime.category_seconds(user, hackatime_projects, category, **window) } art = art_parts.sum unless art_parts.any?(&:nil?) # one unknown part leaves the art time unknown diff --git a/app/views/projects/show.html.erb b/app/views/projects/show.html.erb index fa90f5a..773ad05 100644 --- a/app/views/projects/show.html.erb +++ b/app/views/projects/show.html.erb @@ -117,7 +117,7 @@ <% else %>

<%= project_schedule_note(@project) %>

- <% if @project.latest_standing_ship && @project.may_ship? && (Time.current < Program::ENDS_AT || @project.changes_needed_until) %><%= render "projects/ship_counter", project: @project %><% end %> + <% if @project.latest_standing_ship && @project.may_ship? && (Time.current < Program::ENDS_AT || @project.changes_needed_until || @project.ship_until) %><%= render "projects/ship_counter", project: @project %><% end %> <% end %> <%# D-112: what the reviewer wrote on your latest approved ship, whatever the next step is. %> <% if (reviewer_note = project_reviewer_note(@project)) %><%= render "projects/reviewer_note", note: reviewer_note %><% end %> diff --git a/db/migrate/20261105210000_add_ship_until_to_projects.rb b/db/migrate/20261105210000_add_ship_until_to_projects.rb new file mode 100644 index 0000000..50c1fed --- /dev/null +++ b/db/migrate/20261105210000_add_ship_until_to_projects.rb @@ -0,0 +1,6 @@ +# D-155: a deadline an admin sets for one project, so a person gets a private window to ship after the public end. +class AddShipUntilToProjects < ActiveRecord::Migration[8.1] + def change + add_column :projects, :ship_until, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 35c95cb..d6d44be 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_11_04_140000) do +ActiveRecord::Schema[8.1].define(version: 2026_11_05_210000) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -251,6 +251,7 @@ t.integer "jame_gam_match_count", default: 0, null: false t.integer "jame_gam_flagged_match_count", default: 0, null: false t.datetime "jame_gam_checked_at" + t.datetime "ship_until" t.index ["buddy_code"], name: "index_projects_on_buddy_code", unique: true t.index ["user_id"], name: "index_projects_on_user_id", unique: true end @@ -341,7 +342,7 @@ t.index ["snapshot_edited_by_id"], name: "index_ships_on_snapshot_edited_by_id" t.index ["status"], name: "index_ships_on_status" t.index ["superseded_by_id"], name: "index_ships_on_superseded_by_id" - t.check_constraint "(status::text <> ALL (ARRAY['awaiting_fraud'::character varying, 'approved'::character varying])) OR claimed_hours IS NULL OR review_hours IS NULL OR review_hours >= claimed_hours OR deflation_reason IS NOT NULL AND deflation_reason ~ '\\S'::text", name: "ships_deflation_has_reason" + t.check_constraint "(status::text <> ALL (ARRAY['awaiting_fraud'::character varying, 'approved'::character varying]::text[])) OR claimed_hours IS NULL OR review_hours IS NULL OR review_hours >= claimed_hours OR deflation_reason IS NOT NULL AND deflation_reason ~ '\\S'::text", name: "ships_deflation_has_reason" t.check_constraint "fraud_deduction_hours <= 0::numeric OR fraud_notes IS NOT NULL AND fraud_notes ~ '\\S'::text", name: "ships_deduction_has_notes" t.check_constraint "status::text <> 'approved'::text OR approved_hours IS NOT NULL AND review_hours IS NOT NULL AND approved_hours = (review_hours - fraud_deduction_hours)", name: "ships_approved_hours_add_up" end diff --git a/test/controllers/admin/mcp_controller_test.rb b/test/controllers/admin/mcp_controller_test.rb index 53c78d7..f566f12 100644 --- a/test/controllers/admin/mcp_controller_test.rb +++ b/test/controllers/admin/mcp_controller_test.rb @@ -63,8 +63,8 @@ class Admin::McpControllerTest < ActionDispatch::IntegrationTest tools = response.parsed_body.dig("result", "tools").index_by { it["name"] } assert_equal %w[admin_summary audit_log decide_extension describe_schema edit_review_text edit_snapshot find_people find_projects find_ships funnel get_person get_project get_ship - list_extensions list_orders mark_order_ordered metrics nudges public_stats query_sql save_review_draft send_verdict_dm set_checklist_item set_hours_start set_lapse_coding set_reviewer_ai], tools.keys.sort - writes = %w[save_review_draft set_lapse_coding set_reviewer_ai set_checklist_item set_hours_start edit_snapshot edit_review_text send_verdict_dm mark_order_ordered decide_extension] + list_extensions list_orders mark_order_ordered metrics nudges public_stats query_sql save_review_draft send_verdict_dm set_checklist_item set_hours_start set_lapse_coding set_reviewer_ai set_ship_until], tools.keys.sort + writes = %w[save_review_draft set_lapse_coding set_reviewer_ai set_checklist_item set_hours_start edit_snapshot edit_review_text send_verdict_dm mark_order_ordered decide_extension set_ship_until] writes.each do |name| assert tools[name]["description"].start_with?("Changes real data: confirm with the human first."), name assert_equal false, tools[name].dig("annotations", "readOnlyHint") @@ -461,6 +461,25 @@ class Admin::McpControllerTest < ActionDispatch::IntegrationTest assert_includes AdminAuditEvent.where(tool: "set_hours_start").map(&:ids), { "ship_id" => ship.id, "project_id" => project.id } end + test "set_ship_until sets and clears a private ship window, logs the project, and sends nothing (D-155)" do + project = projects(:ana) + window = "2026-11-06T04:00" + assert_no_enqueued_jobs only: SlackMessageJob do + done = mcp_tool(@token, "set_ship_until", { user_id: project.user_id, ship_until: window }) + assert_equal project.id, done["project_id"] + assert_equal Time.utc(2026, 11, 6, 4), project.reload.ship_until + assert_equal Time.utc(2026, 11, 6, 4), project.ship_closes_at + + mcp_tool(@token, "set_ship_until", { project_id: project.id, ship_until: "nonsense" }) + assert mcp_error? + assert_equal Time.utc(2026, 11, 6, 4), project.reload.ship_until + + mcp_tool(@token, "set_ship_until", { project_id: project.id, ship_until: nil }) + assert_nil project.reload.ship_until + end + assert_includes AdminAuditEvent.where(tool: "set_ship_until").map(&:ids), { "user_id" => project.user_id, "project_id" => project.id } + end + test "send_verdict_dm sends the verdict DM once, returns its text, and can leave the note out (D-139)" do sent = [] dm = SlackBot.method(:dm) diff --git a/test/models/project_test.rb b/test/models/project_test.rb index bdd73ea..a054054 100644 --- a/test/models/project_test.rb +++ b/test/models/project_test.rb @@ -344,6 +344,51 @@ class ProjectDeadlineTest < ActiveSupport::TestCase assert_not project.may_ship?(now: Program::SHOP_CLOSES_AT - 1.minute), "D-138: the open shop does not reopen shipping" end + test "a private ship window (D-155) lets one project ship after the end and leaves everyone else alone" do + project = projects(:orpheus) + window = Program::ENDS_AT + 24.hours + assert_nil project.ship_until + assert_equal Program::ENDS_AT, project.ship_closes_at + assert_not project.may_ship?(now: Program::ENDS_AT + 1.hour), "no window, no shipping after the end" + + project.update!(ship_until: window) + assert_equal window, project.ship_closes_at + assert project.may_ship?(now: Program::ENDS_AT + 1.hour) + assert project.may_ship?(now: window) + assert_not project.may_ship?(now: window + 1.second) + + project.update!(ship_until: Program::ENDS_AT - 1.day) + assert_equal Program::ENDS_AT, project.ship_closes_at, "a window never shortens the public end" + project.update!(ship_until: nil) + assert_equal Program::ENDS_AT, project.ship_closes_at + end + + test "with a ship window only time before the end counts, and without one nothing changes (D-155)" do + project = projects(:ana) + user = project.user + user.update!(timezone: "America/New_York") + Hackatime.stubbed_users = { "token-ana" => 99 } + link_hackatime(user) + project.update!(hackatime_projects: [ "pong" ]) + last_day = StreakActivity.streak_date_for(Program::ENDS_AT, user.timezone) + user.streak_activities.create!(activity_date: last_day - 1, coded_seconds: 3 * 3600) + user.streak_activities.create!(activity_date: last_day + 1, coded_seconds: 2 * 3600) # logged after the end + + travel_to(Program::ENDS_AT + 2.hours) do + assert_nil project.hours_last_day + assert_equal 5.0, project.hours_logged, "no window: the open-ended count is kept" + assert_equal 5.0, Project.find(project.id).hours_logged + + project.update!(ship_until: Program::ENDS_AT + 24.hours) + assert_equal last_day, project.hours_last_day + assert_equal 3.0, Project.find(project.id).hours_logged, "the window counts only time from before the end" + end + + travel_to(Program::ENDS_AT - 2.hours) do + assert_nil project.hours_last_day, "before the end nothing is capped" + end + end + test "nobody can ask for more time any more" do project = projects(:orpheus) link_hackatime(users(:orpheus))