Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

fix: review round 1 of #78, whole minutes and unexpected failures

Guido Boyke7a4c067e2cc7
diff --git a/app/controllers/api/v1/time_sessions_controller.rb b/app/controllers/api/v1/time_sessions_controller.rb
index fd54b24..04dbcb5 100644
--- a/app/controllers/api/v1/time_sessions_controller.rb
+++ b/app/controllers/api/v1/time_sessions_controller.rb
@@ -74,7 +74,7 @@ module Api
if result.success?
render json: { data: TimeSessions::Serializer.row(result.resource) }, status: :created
else
- render_error(error: result.code, errors: result.errors, status: :unprocessable_entity)
+ render_booking_failure(result)
end
end
end
@@ -86,6 +86,17 @@ module Api
status: :unprocessable_entity)
end
+ # A failure the actor did not expect (BaseActor answers :internal_error
+ # where it does not re-raise) is no refusal of the input: a 500 without the
+ # exception's text, which may name the database.
+ def render_booking_failure(result)
+ if result.code == :internal_error
+ render_error(error: 'internal_error', status: :internal_server_error)
+ else
+ render_error(error: result.code, errors: result.errors, status: :unprocessable_entity)
+ end
+ end
+
def session_params
params.require(:time_session).permit(
:occurred_on, :duration_minutes, :duration_string,
diff --git a/app/services/lalabase/tools/create_time_session.rb b/app/services/lalabase/tools/create_time_session.rb
index 6538403..cd002aa 100644
--- a/app/services/lalabase/tools/create_time_session.rb
+++ b/app/services/lalabase/tools/create_time_session.rb
@@ -64,6 +64,7 @@ module Lalabase
REFUSAL_TEXT = {
duration_missing: 'duration or duration_minutes is required',
both_durations: 'give either duration or duration_minutes, not both',
+ duration_minutes_invalid: 'duration_minutes must be a whole number of minutes',
occurred_on_invalid: 'invalid occurred_on (expected an ISO 8601 date or timestamp)'
}.freeze
diff --git a/app/services/time_sessions/booking.rb b/app/services/time_sessions/booking.rb
index 68e08b3..c1f4cd5 100644
--- a/app/services/time_sessions/booking.rb
+++ b/app/services/time_sessions/booking.rb
@@ -8,7 +8,8 @@
# Entry point: parse
#
# Three steps, because the callers authorize in between: `parse` refuses
-# unusable input before any ticket is touched, `build` makes the unsaved
+# unusable input before the caller builds or authorizes the booking (MCP
+# before its ticket lookup, REST inside its read gate), `build` makes the unsaved
# booking on a ticket the caller has found readable, and `save` writes it
# through Actors::SaveTimeSession (billed guards, one transaction) once the
# caller has authorized the booking itself (:create?, the project's time
@@ -24,16 +25,17 @@
module TimeSessions
class Booking
# Why `parse` turns an input away. Each caller words it for its surface.
- REFUSALS = %i[duration_missing both_durations occurred_on_invalid].freeze
+ REFUSALS = %i[duration_missing both_durations duration_minutes_invalid occurred_on_invalid].freeze
PLAIN_DATE = /\A\d{4}-\d{2}-\d{2}\z/
+ WHOLE_MINUTES = /\A\s*\d+\s*\z/
attr_reader :refusal
def self.parse(actor:, duration_string: nil, duration_minutes: nil, occurred_on: nil,
description: nil, customer_description: nil)
new(actor: actor, duration_string: duration_string.to_s.strip.presence,
- duration_minutes: duration_minutes.presence, occurred_on: occurred_on,
+ duration_minutes: absent?(duration_minutes) ? nil : duration_minutes, occurred_on: occurred_on,
description: description, customer_description: customer_description)
end
@@ -46,6 +48,12 @@ module TimeSessions
@refusal = refusal_for(occurred_on)
end
+ # Absent is nil or blank text; false, 0 and other values are given.
+ def self.absent?(value)
+ value.nil? || (value.is_a?(String) && value.strip.empty?)
+ end
+ private_class_method :absent?
+
def refused?
end
@@ -81,12 +89,21 @@ module TimeSessions
# The duration first, then the date, the order the MCP tool always answered in.
def refusal_for(occurred_on)
return :duration_missing if @duration_string.nil? && @duration_minutes.nil?
- return :both_durations if @duration_string && @duration_minutes
+ return :both_durations if @duration_string && !@duration_minutes.nil?
+ return :duration_minutes_invalid unless @duration_minutes.nil? || whole_minutes?(@duration_minutes)
@day = day_for(occurred_on)
:occurred_on_invalid if @day.nil?
end
+ # A whole number of minutes, as a number or as digits. A fraction would be
+ # cut to the minute below ("1.5" booked as 1, while duration_string "1.5"
+ # is 90 minutes); anything else is no number. Zero and negative numbers pass
+ # here and are refused by the model (duration > 0).
+ def whole_minutes?(value)
+ value.is_a?(Integer) || (value.is_a?(String) && value.match?(WHOLE_MINUTES))
+ end
+
# Midnight (app zone) of the booking's day, or nil for input that is no date.
def day_for(raw)
raw = raw.to_s.strip
diff --git a/config/locales/services.de.yml b/config/locales/services.de.yml
index 47ef8fb..f48be25 100644
--- a/config/locales/services.de.yml
+++ b/config/locales/services.de.yml
@@ -101,6 +101,7 @@ de:
time_session_booking:
duration_missing: "Bitte geben Sie eine Dauer an (duration_string oder duration_minutes)."
both_durations: "Bitte geben Sie die Dauer entweder als duration_string oder als duration_minutes an, nicht beides."
+ duration_minutes_invalid: "Bitte geben Sie duration_minutes als ganze Zahl von Minuten an."
occurred_on_invalid: "Das Datum (occurred_on) ist kein ISO-8601-Datum und kein ISO-8601-Zeitstempel."
bulk_customer_description_generator:
empty_result: "Zeit-Session %{id}: Die KI hat keine Beschreibung geliefert."
diff --git a/config/locales/services.en.yml b/config/locales/services.en.yml
index 4f96729..53a938f 100644
--- a/config/locales/services.en.yml
+++ b/config/locales/services.en.yml
@@ -101,6 +101,7 @@ en:
time_session_booking:
duration_missing: "Give a duration (duration_string or duration_minutes)."
both_durations: "Give the duration either as duration_string or as duration_minutes, not both."
+ duration_minutes_invalid: "Give duration_minutes as a whole number of minutes."
occurred_on_invalid: "The date (occurred_on) is no ISO 8601 date or timestamp."
bulk_customer_description_generator:
empty_result: "Time session %{id}: The AI returned no description."
diff --git a/spec/requests/api/v1/time_session_booking_spec.rb b/spec/requests/api/v1/time_session_booking_spec.rb
index c93bd26..afb8093 100644
--- a/spec/requests/api/v1/time_session_booking_spec.rb
+++ b/spec/requests/api/v1/time_session_booking_spec.rb
@@ -85,6 +85,21 @@ RSpec.describe 'API V1 time booking', :ci_guard, type: :request do
end
end
+ it 'refuses minutes that are no whole number instead of cutting them, writing nothing' do
+ expect { book(duration_minutes: '1.5') }.not_to(change { bookings })
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(body).to eq('error' => 'validation_failed', 'code' => 'invalid_input',
+ 'errors' => [I18n.t('services.time_session_booking.duration_minutes_invalid', locale: :en)])
+ end
+
+ it 'refuses minutes that are no number at all as input, not as a crash' do
+ post "/api/v1/tickets/#{ticket.id}/time_sessions",
+ params: { time_session: { duration_minutes: true } }.to_json,
+ headers: { 'Authorization' => "Bearer #{pat.token}", 'Content-Type' => 'application/json' }
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(body).to include('error' => 'validation_failed', 'code' => 'invalid_input')
+ end
+
it 'answers the refusal in the token owner language' do
user.update!(language_preference: 'de')
book(duration_string: '1:30', duration_minutes: 45)
@@ -114,6 +129,12 @@ RSpec.describe 'API V1 time booking', :ci_guard, type: :request do
expect(booked.occurred_on).to eq(Time.zone.parse('2026-08-20 00:00'))
end
+ it 'takes a plain date as that day in a zone west of UTC too' do
+ user.update!(timezone: 'America/New_York')
+ book(duration_minutes: 30, occurred_on: '2026-08-20')
+ expect(booked.occurred_on).to eq(Time.zone.parse('2026-08-20 00:00'))
+ end
+
%w[gestern 05.01.2026 2026-02-30].push('2026-10-29 10:00').each do |raw|
it "refuses the unreadable date #{raw.inspect} instead of booking now, writing nothing" do
expect { book(duration_minutes: 30, occurred_on: raw) }.not_to(change { bookings })
@@ -138,6 +159,19 @@ RSpec.describe 'API V1 time booking', :ci_guard, type: :request do
expect(body).to eq('error' => 'group_billed', 'code' => 'refused', 'reason' => 'group_billed', 'errors' => ['nope'])
end
+ # A failure the server did not expect (BaseActor's :internal_error in
+ # production, where it does not re-raise) is no refusal of the input: a 500
+ # without the exception's text, so a client may retry and learns nothing of
+ # the database.
+ it 'answers an unexpected actor failure as a server error without its text' do
+ allow(Actors::SaveTimeSession).to receive(:call)
+ .and_return(Actors::BaseActor::Result.new(success: false, errors: ['PG::Error: secret'], code: :internal_error))
+ book(duration_minutes: 30)
+ expect(response).to have_http_status(:internal_server_error)
+ expect(response.body).not_to include('secret')
+ expect(body).to include('error' => 'internal_error')
+ end
+
it 'books the same row the MCP tool books for the same input' do
input = { duration_string: '1:30', occurred_on: '2026-08-20T00:30:00+02:00', description: 'Fixed the login' }
book(**input)
diff --git a/spec/services/lalabase/tools/create_time_session_spec.rb b/spec/services/lalabase/tools/create_time_session_spec.rb
index 32a69f6..10c21d8 100644
--- a/spec/services/lalabase/tools/create_time_session_spec.rb
+++ b/spec/services/lalabase/tools/create_time_session_spec.rb
@@ -103,6 +103,14 @@ RSpec.describe Lalabase::Tools::CreateTimeSession do
expect(payload['occurred_on']).to eq(Time.zone.parse('2026-09-01 00:00:00').iso8601)
end
+ # A plain date names the day itself; read as UTC midnight and moved into a
+ # zone west of UTC it would land on the day before.
+ it 'takes a plain date as that date in a zone west of UTC too' do
+ actor.update!(timezone: 'America/New_York')
+ payload = JSON.parse(run(duration: '30', occurred_on: '2026-08-20').content)
+ expect(payload['occurred_on']).to eq(Time.zone.parse('2026-08-20 00:00:00').iso8601)
+ end
+
it 'refuses an offset no clock has instead of reading the time as UTC, writing nothing' do
expect { expect(run(duration: '30', occurred_on: '2026-08-20T10:00+99:99').error).to match(/occurred_on/) }
.not_to change(TimeSession, :count)
@@ -146,6 +154,13 @@ RSpec.describe Lalabase::Tools::CreateTimeSession do
expect(result.error).to match(/not both/)
end
+ it 'refuses minutes that are no whole number instead of cutting them, writing nothing' do
+ result = nil
+ expect { result = run(duration_minutes: 1.5) }.not_to change(TimeSession, :count)
+ expect(result.error).to eq('duration_minutes must be a whole number of minutes')
+ expect(run(duration_minutes: true).error).to eq('duration_minutes must be a whole number of minutes')
+ end
+
it 'refuses a call without any duration, writing nothing' do
result = nil
expect { result = run({}) }.not_to change(TimeSession, :count)
diff --git a/spec/services/time_sessions/booking_spec.rb b/spec/services/time_sessions/booking_spec.rb
index 783b77e..2c09989 100644
--- a/spec/services/time_sessions/booking_spec.rb
+++ b/spec/services/time_sessions/booking_spec.rb
@@ -36,6 +36,21 @@ RSpec.describe TimeSessions::Booking do
expect(parse(duration_string: '1:30', duration_minutes: '45').refusal).to eq(:both_durations)
end
+ # Minutes are a whole number: a fraction would be cut to the minute below
+ # ("1.5" booked as 1, while duration_string "1.5" is 90), and a value
+ # that is no number at all must not reach the model as a crash.
+ it 'refuses minutes that are no whole number' do
+ [1.5, '1.5', true, false, '1e2', [30], { a: 1 }].each do |minutes|
+ expect(parse(duration_minutes: minutes).refusal).to eq(:duration_minutes_invalid), minutes.inspect
+ end
+ end
+
+ it 'takes whole minutes as a number or as digits' do
+ [45, '45', ' 45 ', 0, -5].each do |minutes|
+ expect(parse(duration_minutes: minutes).refused?).to be(false), minutes.inspect
+ end
+ end
+
it 'refuses an occurred_on it cannot read' do
%w[gestern 05.01.2026 2026-02-30 10000-01-01].push('2026-10-29 10:00').each do |raw|
expect(parse(duration_string: '30', occurred_on: raw).refusal).to eq(:occurred_on_invalid), raw