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 4 of #81, answer a comment written before a failure after the commit as written

Guido Boykea6010305e8bb
diff --git a/CLAUDE.md b/CLAUDE.md
index 3eb9811..1f76ba3 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -880,9 +880,15 @@ part can name a charset of its own (`text/plain; charset=...`): `html` hands it
it came, as before (a Latin-1 comment keeps its letters), `plain` transcodes it to UTF-8, and a
`plain` part declared binary must hold UTF-8. An unexpected actor failure (`internal_error`) answers 500 without the exception's
text, every other failure 422 `validation_failed` as before; MCP answers `comment failed:
-unexpected error`. Capability `posts.plain`. Guards:
+unexpected error`. ⚠️ A failure after the commit (an after_commit callback or a job enqueue
+that raised) leaves the comment written: `Posts::Comment#save` counts it as saved, so REST
+answers 201 and keeps its Idempotency-Key, and MCP returns the comment. Answered as an error,
+a retry with the same key wrote it twice, because the key keeps only successes. Capability
+`posts.plain`. Guards:
`spec/requests/api/v1/post_comment_format_spec.rb`, `spec/services/posts/comment_spec.rb`,
-`spec/requests/api/mcp_plain_comment_spec.rb`.
+`spec/requests/api/mcp_plain_comment_spec.rb`, `spec/locales/token_rule_refusal_texts_spec.rb`
+(the refusals of `Posts::Comment` and `TimeSessions::Booking`, the MCP texts and the de and en
+keys are three lists kept by hand; the guard holds them together).
## Admin: Betreiber-Teile kommen nur über Schlitze
diff --git a/app/controllers/api/v1/posts_controller.rb b/app/controllers/api/v1/posts_controller.rb
index 73d6d89..cea8499 100644
--- a/app/controllers/api/v1/posts_controller.rb
+++ b/app/controllers/api/v1/posts_controller.rb
@@ -66,8 +66,9 @@ module Api
# 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. Every other failure
- # answers as it always did.
+ # exception's text, which may name the database. Only a failure before the
+ # commit gets here: a comment already written counts as saved
+ # (Posts::Comment#save). Every other failure answers as it always did.
def render_comment_failure(result)
return render_error(error: 'internal_error', status: :internal_server_error) if result.code == :internal_error
diff --git a/app/services/CLAUDE.md b/app/services/CLAUDE.md
index faffb08..3339160 100644
--- a/app/services/CLAUDE.md
+++ b/app/services/CLAUDE.md
@@ -202,7 +202,11 @@ Verzeichnis-Übersicht (Auswahl):
a token: the MCP tool `post_comment` and `POST /api/v1/tickets/:ticket_id/posts` call it.
`parse` (format `html`/`plain`, valid UTF-8, no NUL in plain, `plain_text_body.rb`, empty
after conversion), `build` (the unsaved post through `Actors::SavePost`, for the caller's
- `:create?`), `save`; rights and wording stay with the callers. ⚠️ An empty html body is
+ `:create?`), `save` (a comment written before a failure after the commit counts as saved,
+ else a retry with the same Idempotency-Key writes it twice); rights and wording stay with
+ the callers. The refusals (`REFUSALS`), the tool's `REFUSAL_TEXT` and the
+ `services.post_comment` keys in de and en are held together by
+ `spec/locales/token_rule_refusal_texts_spec.rb`. ⚠️ An empty html body is
answered differently on purpose: MCP refuses it before the ticket lookup (`body is
required`, as always), REST leaves it to the model after the write check (its answer
without `format` stays byte for byte). Not a second rule: do not align them in one hull.
diff --git a/app/services/lalabase/CLAUDE.md b/app/services/lalabase/CLAUDE.md
index aa02da1..21ddfb1 100644
--- a/app/services/lalabase/CLAUDE.md
+++ b/app/services/lalabase/CLAUDE.md
@@ -74,7 +74,8 @@ Codebase-Verständnis.
is decided on the converted text (a body of Unicode whitespace only is `body is
required`). The tool keeps its own texts (`REFUSAL_TEXT`) and checks before the ticket
lookup; model save failures stay `refused`, an unexpected one (`internal_error`) is
- `comment failed: unexpected error`, never the exception's text.
+ `comment failed: unexpected error`, never the exception's text, unless it came after
+ the commit: then the comment is written and the tool returns it.
Zwei Formen: `record`-Werkzeuge liefern
Zeilen und zitieren jede (Ticket-Werkzeuge), `report`-Werkzeuge liefern nur Zahlen und
zitieren die Auswertung (`summarize_time_sessions`, Zitationstyp `time_report`). Ein
diff --git a/app/services/lalabase/tools/post_comment.rb b/app/services/lalabase/tools/post_comment.rb
index 9bb9338..4065220 100644
--- a/app/services/lalabase/tools/post_comment.rb
+++ b/app/services/lalabase/tools/post_comment.rb
@@ -103,6 +103,8 @@ module Lalabase
# wrong, and create_ticket and create_time_session answer a refused save
# the same way. An unexpected failure (BaseActor's :internal_error where it
# does not re-raise) gets a fixed text: its message may name the database.
+ # Only a failure before the commit gets here: a comment already written
+ # counts as saved (Posts::Comment#save).
def self.comment_failure(outcome)
return 'comment failed: unexpected error' if outcome.code == :internal_error
diff --git a/app/services/posts/comment.rb b/app/services/posts/comment.rb
index 8d8524c..1e5cff9 100644
--- a/app/services/posts/comment.rb
+++ b/app/services/posts/comment.rb
@@ -67,10 +67,21 @@ module Posts
# => Actors::BaseActor::Result. A model refusal (an empty content, an
# addressee outside the organisation) comes back as :validation_failed,
# nothing written.
+ #
+ # A failure after the commit (an after_commit callback or a job enqueue
+ # that raised) leaves the comment written, yet BaseActor answers it as
+ # :internal_error where it does not re-raise, having logged it. It counts
+ # as saved here: answered as a failure, it would be retried, and a retry
+ # with the same Idempotency-Key, whose store keeps only successes, would
+ # write the comment a second time. A rollback marks the post new again, so
+ # a persisted post was committed.
def save
raise ArgumentError, 'build the comment before saving it' if @actor.nil?
- @actor.call
+ result = @actor.call
+ return result if result.success? || [email protected]?
+
+ Actors::BaseActor::Result.new(success: true, resource: @actor.record)
end
private
diff --git a/spec/requests/api/v1/post_comment_format_spec.rb b/spec/requests/api/v1/post_comment_format_spec.rb
index c20300e..a323863 100644
--- a/spec/requests/api/v1/post_comment_format_spec.rb
+++ b/spec/requests/api/v1/post_comment_format_spec.rb
@@ -546,6 +546,29 @@ RSpec.describe 'API V1 comment format', :ci_guard, type: :request do
expect(body).to eq('error' => 'validation_failed', 'code' => 'invalid_input',
'errors' => ['Visible to user must be a member of the organisation'])
end
+
+ def without_reraise
+ original = Rails.application.config.x.actors.reraise_unexpected
+ Rails.application.config.x.actors.reraise_unexpected = false
+ yield
+ ensure
+ Rails.application.config.x.actors.reraise_unexpected = original
+ end
+
+ # The author's subscription runs after the commit (Post after_create_commit).
+ # Answered as a 500, the comment stood all the same, and a retry with the
+ # same key wrote it a second time: the key keeps only successes.
+ it 'answers a comment written before a failure after the commit as written, so a keyed retry replays it' do
+ ticket
+ allow(NotificationSubscription).to receive(:system_subscribe).and_raise(ActiveRecord::ConnectionNotEstablished, 'gone')
+
+ expect { without_reraise { comment(key: 'r3a-after-commit', format: 'plain', body: 'x') } }.to change { posts_count }.by(1)
+ expect(response).to have_http_status(:created)
+ first = response.body
+ expect { without_reraise { comment(key: 'r3a-after-commit', format: 'plain', body: 'x') } }.not_to(change { posts_count })
+ expect(response).to have_http_status(:created)
+ expect(response.body).to eq(first)
+ end
end
describe 'Idempotency-Key' do
diff --git a/spec/services/lalabase/tools/post_comment_failure_spec.rb b/spec/services/lalabase/tools/post_comment_failure_spec.rb
index e2b1b54..97dc144 100644
--- a/spec/services/lalabase/tools/post_comment_failure_spec.rb
+++ b/spec/services/lalabase/tools/post_comment_failure_spec.rb
@@ -100,6 +100,23 @@ RSpec.describe Lalabase::Tools::PostComment, :ci_guard do
expect(result.error_code).to eq(:refused)
end
+ # The author's subscription runs after the commit (Post after_create_commit).
+ # The comment stands, so the tool answers it: an error would invite a retry
+ # that writes it a second time.
+ it 'answers a comment written before a failure after the commit with the comment' do
+ ticket
+ allow(NotificationSubscription).to receive(:system_subscribe).and_raise(ActiveRecord::ConnectionNotEstablished, 'gone')
+ original = Rails.application.config.x.actors.reraise_unexpected
+ Rails.application.config.x.actors.reraise_unexpected = false
+
+ result = nil
+ expect { result = execute }.to change(Post, :count).by(1)
+ expect(result).to be_success
+ expect(Post.find(JSON.parse(result.content).fetch('id'))).to be_persisted
+ ensure
+ Rails.application.config.x.actors.reraise_unexpected = original
+ end
+
it 'still names the reason of any other failure, as before' do
failing_save(:validation_failed, ['Content darf nicht leer sein'])
result = execute
diff --git a/spec/services/posts/comment_spec.rb b/spec/services/posts/comment_spec.rb
index 8c37f63..68028d2 100644
--- a/spec/services/posts/comment_spec.rb
+++ b/spec/services/posts/comment_spec.rb
@@ -228,5 +228,41 @@ RSpec.describe Posts::Comment, :ci_guard do
it 'refuses to save a comment that was not built' do
expect { described_class.parse(body: 'x').save }.to raise_error(ArgumentError)
end
+
+ # BaseActor answers an unexpected exception as :internal_error only where it
+ # does not re-raise (production); development and test re-raise.
+ def without_reraise
+ original = Rails.application.config.x.actors.reraise_unexpected
+ Rails.application.config.x.actors.reraise_unexpected = false
+ yield
+ ensure
+ Rails.application.config.x.actors.reraise_unexpected = original
+ end
+
+ # The author's subscription runs after the commit (Post after_create_commit).
+ it 'counts a comment written before a failure after the commit as saved' do
+ comment = described_class.parse(body: 'x', format: 'plain')
+ post = comment.build(ticket: ticket, current_user: user)
+ allow(NotificationSubscription).to receive(:system_subscribe).and_raise(ActiveRecord::ConnectionNotEstablished, 'gone')
+
+ result = nil
+ expect { without_reraise { result = comment.save } }.to change(Post, :count).by(1)
+ expect(result).to be_success
+ expect(result.resource).to eq(post)
+ end
+
+ # The post_added activity is written inside the transaction: a failure there
+ # rolls the post back, and it must not count as saved.
+ it 'answers a failure before the commit as a failure, writing nothing' do
+ comment = described_class.parse(body: 'x', format: 'plain')
+ post = comment.build(ticket: ticket, current_user: user)
+ allow_any_instance_of(Ticket).to receive(:track_post_added).and_raise(ActiveRecord::ConnectionNotEstablished, 'gone')
+
+ result = nil
+ expect { without_reraise { result = comment.save } }.not_to change(Post, :count)
+ expect(result).not_to be_success
+ expect(result.code).to eq(:internal_error)
+ expect(post).not_to be_persisted
+ end
end
end