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.

Give the meeting lock one owner and guard it

Guido Boykec8a44b2b4955
diff --git a/app/controllers/organisations/meeting_ticket_links_controller.rb b/app/controllers/organisations/meeting_ticket_links_controller.rb
index 0217a61..8267220 100644
--- a/app/controllers/organisations/meeting_ticket_links_controller.rb
+++ b/app/controllers/organisations/meeting_ticket_links_controller.rb
@@ -34,13 +34,11 @@ module Organisations
# covers the normal "clicked twice" case; the rescue is the race backstop.
return respond_link_result(:alert, 'flash.meeting_ticket_links.already_linked') if @meeting.outgoing_references.exists?(target: ticket)
- # The meeting's row lock, the one Actors::MoveMeeting holds: a move that
- # committed while this waited has taken the meeting into another project,
- # and the ticket of this one may not follow it there (404, as for a
- # foreign meeting).
+ # A move that committed while this waited has taken the meeting into
+ # another project, and the ticket of this one may not follow it there
+ # (404, as for a foreign meeting).
Meeting.transaction do
- @meeting.lock!
- raise ActiveRecord::RecordNotFound unless @meeting.project_id == @project.id
+ raise ActiveRecord::RecordNotFound unless @meeting.lock_in_project!(@project.id)
@meeting.outgoing_references.create!(
organisation: @current_organisation,
diff --git a/app/controllers/organisations/meetings_controller.rb b/app/controllers/organisations/meetings_controller.rb
index c22f5bb..093141c 100644
--- a/app/controllers/organisations/meetings_controller.rb
+++ b/app/controllers/organisations/meetings_controller.rb
@@ -474,13 +474,11 @@ module Organisations
organisation_project_meetings_path(@current_organisation, target, meeting_id: @meeting.id)
end
- # The delete was authorized against this project. Actors::MoveMeeting holds
- # the meeting's row lock while it moves it: under the same lock a meeting
- # that moved meanwhile is gone from here, as a deleted one would be.
+ # The delete was authorized against this project: a meeting that moved
+ # meanwhile is gone from here, as a deleted one would be.
def destroy_in_project
Meeting.transaction do
- @meeting.lock!
- raise ActiveRecord::RecordNotFound unless @meeting.project_id == @project.id
+ raise ActiveRecord::RecordNotFound unless @meeting.lock_in_project!(@project.id)
@meeting.destroy
end
diff --git a/app/models/meeting.rb b/app/models/meeting.rb
index bf00128..596c490 100644
--- a/app/models/meeting.rb
+++ b/app/models/meeting.rb
@@ -211,12 +211,17 @@ class Meeting < ApplicationRecord
text.sub(FRONTMATTER_BLOCK, '')
end
- # The single source rule for the AI summary (plan decision 11): transcript
- # preferred, notes fallback. Both are frontmatter-stripped BEFORE the presence
- # check, so a transcript that is nothing but a metadata block still falls through
- # to the notes, and the LLM never sees the id:/parent: noise. Controller guard,
- # view gate and Meetings::SummaryService all derive from THIS method so the rule
- # cannot drift apart (review #235).
+ # THE lock for a write that was authorized against a project: takes the
+ # meeting's row lock and answers whether the meeting is still in that
+ # project. Actors::MoveMeeting holds the same lock while it moves the
+ # meeting, so a write that answers true cannot land after a move, and one
+ # that answers false treats the meeting as gone from that project. Call it
+ # inside the write's transaction. Guard: spec/architecture/meeting_lock_guard_spec.rb.
+ def lock_in_project!(authorized_project_id)
+ lock!
+ project_id == authorized_project_id
+ end
+
# The live summary stream names the project: after a move (Actors::MoveMeeting)
# a page still open in the old project hears nothing of what is written in
# the new one. The view subscribes through the same method.
@@ -224,6 +229,12 @@ class Meeting < ApplicationRecord
"#{dom_id(self)}_#{for_project_id}_summary"
end
+ # The single source rule for the AI summary (plan decision 11): transcript
+ # preferred, notes fallback. Both are frontmatter-stripped BEFORE the presence
+ # check, so a transcript that is nothing but a metadata block still falls through
+ # to the notes, and the LLM never sees the id:/parent: noise. Controller guard,
+ # view gate and Meetings::SummaryService all derive from THIS method so the rule
+ # cannot drift apart (review #235).
def summary_source
self.class.strip_frontmatter(transcript).presence || self.class.strip_frontmatter(notes).presence
end
diff --git a/app/services/actors/move_meeting.rb b/app/services/actors/move_meeting.rb
index 07d6a1d..c378f9b 100644
--- a/app/services/actors/move_meeting.rb
+++ b/app/services/actors/move_meeting.rb
@@ -40,10 +40,12 @@ module Actors
def perform
guard_target!
- # Row lock first. Everything that ties the meeting to its project takes the
- # same lock and looks again (AcceptTicketSuggestion, a manual ticket link,
- # SaveTimeSession), so none of them lands in the old project after a move.
- @meeting.lock!
+ # Row lock first (Meeting#lock_in_project!). Every write that ties the
+ # meeting to its project takes the same lock and looks again, so none of
+ # them lands in the old project after a move. A move that committed while
+ # this one waited has taken the meeting out of the project this move was
+ # checked against.
+ refuse!(:moved_meanwhile) unless @meeting.lock_in_project!(@source_project.id)
guard_movable!
@meeting.update_columns(project_id: @target_project.id, updated_at: Time.current)
@@ -74,10 +76,7 @@ module Actors
refuse!(:forbidden_target)
end
- # lock! reloads the row: a move that committed while this one waited has
- # taken the meeting out of the project this move was checked against.
def guard_movable!
- refuse!(:moved_meanwhile) if @meeting.project_id != @source_project.id
refuse!(:has_ticket_links) if ticket_links.exists?
refuse!(:published_analyses_bound) if bound_analyses.any?
refuse!(:suggestion_run_active) if suggestion_run_active?
diff --git a/app/services/actors/save_meeting.rb b/app/services/actors/save_meeting.rb
index 994b3d3..7e7669c 100644
--- a/app/services/actors/save_meeting.rb
+++ b/app/services/actors/save_meeting.rb
@@ -72,13 +72,11 @@ module Actors
# Tenant-scoped lookup. A given id that matches no meeting in this tenant
# (including a foreign-tenant id) yields nil, which #perform turns into a
# "Meeting not found" failure. A blank id builds a new meeting in the project.
- # The caller authorized the edit against @project. Actors::MoveMeeting holds
- # the meeting's row lock while it moves it; a move that committed meanwhile
- # has taken the meeting where this edit was never authorized. Under the lock
+ # The caller authorized the edit against @project. A move that committed
+ # meanwhile has taken the meeting where this edit was never authorized:
# the meeting is gone from this project, as a deleted one would be.
def guard_still_in_project!
- @meeting.lock!
- return if @meeting.project_id == @project.id
+ return if @meeting.lock_in_project!(@project.id)
@meeting = nil
raise ActorError, 'Meeting not found'
diff --git a/app/services/actors/save_meeting_analysis.rb b/app/services/actors/save_meeting_analysis.rb
index d68636a..829030c 100644
--- a/app/services/actors/save_meeting_analysis.rb
+++ b/app/services/actors/save_meeting_analysis.rb
@@ -199,13 +199,11 @@ module Actors
# the UPDATE below, and this run then writes its search release onto an analysis that
# has meanwhile become private. `perform` already runs inside BaseActor's transaction,
# so the lock lives exactly as long as it must.
- # Actors::MoveMeeting holds the meeting's row lock while it moves it and
- # checks the published analyses. Under the same lock a meeting that moved
- # meanwhile is gone from the project this write was authorized against,
- # so nothing is created, rerun or published there.
+ # Actors::MoveMeeting checks the published analyses under the meeting's
+ # lock. A meeting that moved meanwhile is gone from the project this write
+ # was authorized against, so nothing is created, rerun or published there.
def guard_meeting_in_project!
- @meeting.lock!
- return if @meeting.project_id == @authorized_project_id
+ return if @meeting.lock_in_project!(@authorized_project_id)
raise ActorError, I18n.t('services.meetings.analysis.not_found')
end
diff --git a/app/services/actors/save_time_session.rb b/app/services/actors/save_time_session.rb
index eb80f08..2789d71 100644
--- a/app/services/actors/save_time_session.rb
+++ b/app/services/actors/save_time_session.rb
@@ -78,15 +78,14 @@ module Actors
# A booking on a meeting takes the meeting's row lock, the one
# Actors::MoveMeeting holds while it moves the meeting and its bookings.
def lock_meeting
- @time_session.meeting&.lock!
+ meeting = @time_session.meeting
+ @meeting_moved = meeting.present? && !meeting.lock_in_project!(@authorized_project_id)
end
# A move that committed meanwhile has taken the meeting into another
# project; the booking would land in the old one, so it is refused.
def guard_meeting_project!
- meeting = @time_session.meeting
- return unless meeting
- return if meeting.project_id == @time_session.project_id
+ return unless @meeting_moved
raise ActorError.new(I18n.t("#{ERROR_SCOPE}.meeting_moved"), code: :meeting_moved)
end
diff --git a/spec/architecture/meeting_lock_guard_spec.rb b/spec/architecture/meeting_lock_guard_spec.rb
new file mode 100644
index 0000000..79d2b29
--- /dev/null
+++ b/spec/architecture/meeting_lock_guard_spec.rb
@@ -0,0 +1,103 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# What: a write that was authorized against a project takes the meeting's row
+# lock through Meeting#lock_in_project!, which answers whether the meeting is
+# still in that project. A bare lock on a meeting is refused outside the model.
+# Why: Actors::MoveMeeting holds that lock while it moves a meeting. Three review
+# rounds of the move (lalabase/lalabase#45) found six writes that had been
+# authorized against the old project and still landed after a move: an edit,
+# a delete, a ticket link, a booking, an analysis publication, a second move.
+# Each had been fixed by hand with "lock, then compare the project". A rule
+# that has to be remembered at every write is not a rule.
+# Not seen: a write that takes no lock at all. WRITERS pins the ones that exist;
+# a new write on a meeting is for the person who reads the diff.
+module MeetingLockGuard
+ ROOTS = %w[app lib].freeze
+ MODEL = 'app/models/meeting.rb'
+
+ # A lock taken directly on a meeting: @meeting.lock!, meeting&.lock!,
+ # Meeting.lock, project.meetings.lock. Not lock_in_project!, not the lock of
+ # a meeting's analyses (meeting_analyses.lock).
+ BARE_LOCK = /(?:@meeting|\bmeetings?|\bMeeting)\b&?\.lock(?:!|\b)(?!_)/
+
+ # They lock and then authorize again, under the lock, against the project the
+ # meeting is in NOW: safe after a move too, and their meeting may come from a
+ # chat rather than from a project.
+ ALLOWED = {
+ 'app/services/actors/accept_ticket_suggestion.rb' =>
+ 'asks MeetingPolicy#update? and tickets write in the current project under the lock',
+ 'app/services/actors/start_ticket_suggestion_run.rb' =>
+ 'asks MeetingPolicy#update? under the lock; the meeting may come from a chat reference'
+ }.freeze
+
+ WRITERS = %w[
+ app/services/actors/move_meeting.rb
+ app/services/actors/save_meeting.rb
+ app/services/actors/save_meeting_analysis.rb
+ app/services/actors/save_time_session.rb
+ app/controllers/organisations/meeting_ticket_links_controller.rb
+ app/controllers/organisations/meetings_controller.rb
+ ].freeze
+
+ def self.files
+ ROOTS.flat_map { |root| Dir[Rails.root.join(root, '**', '*.{rb,erb}')] }
+ .map { |path| Pathname(path).relative_path_from(Rails.root).to_s }
+ .reject { |path| path == MODEL }
+ end
+
+ def self.bare_locks(source)
+ source.each_line.with_index(1).filter_map do |line, number|
+ next if line.strip.start_with?('#')
+
+ [number, line.strip] if line.match?(BARE_LOCK)
+ end
+ end
+end
+
+RSpec.describe 'Meeting locks go through Meeting#lock_in_project!', type: :architecture do
+ it 'finds no bare lock on a meeting beyond the allow list' do
+ found = MeetingLockGuard.files.filter_map do |file|
+ next if MeetingLockGuard::ALLOWED.key?(file)
+
+ hits = MeetingLockGuard.bare_locks(File.read(Rails.root.join(file)))
+ "#{file}:\n#{hits.map { |n, text| " #{n}: #{text}" }.join("\n")}" if hits.any?
+ end
+
+ expect(found).to be_empty,
+ "These lock a meeting directly:\n#{found.join("\n")}\n\n" \
+ 'Use meeting.lock_in_project!(authorized_project_id) and treat false as ' \
+ '"the meeting is gone from this project".'
+ end
+
+ it 'keeps the allow list to files that still lock and authorize again' do
+ MeetingLockGuard::ALLOWED.each_key do |file|
+ hits = MeetingLockGuard.bare_locks(File.read(Rails.root.join(file)))
+
+ expect(hits).to be_present, "#{file} no longer locks directly: remove it from ALLOWED"
+ end
+ end
+
+ it 'has every known writer call lock_in_project!' do
+ missing = MeetingLockGuard::WRITERS.reject { |file| File.read(Rails.root.join(file)).include?('lock_in_project!(') }
+
+ expect(missing).to be_empty, "These writers no longer take the lock: #{missing.join(', ')}"
+ end
+
+ describe 'the matcher' do
+ it 'sees the forms a direct lock takes' do
+ ['@meeting.lock!', '@time_session.meeting&.lock!', 'meeting.lock!',
+ 'Meeting.lock.find(id)', 'project.meetings.lock.find(id)'].each do |code|
+ expect(code).to match(MeetingLockGuard::BARE_LOCK), code
+ end
+ end
+
+ it 'leaves the helper and the lock of an analysis alone' do
+ ['@meeting.lock_in_project!(@project.id)', '@meeting.meeting_analyses.lock.find_by(id: @id)',
+ 'meeting_lock = true'].each do |code|
+ expect(code).not_to match(MeetingLockGuard::BARE_LOCK), code
+ end
+ end
+ end
+end
diff --git a/spec/models/meeting_spec.rb b/spec/models/meeting_spec.rb
index 52529d8..dd2c68d 100644
--- a/spec/models/meeting_spec.rb
+++ b/spec/models/meeting_spec.rb
@@ -376,6 +376,23 @@ RSpec.describe Meeting, type: :model do
# a summary-column change pushes the section to the per-meeting stream with the
# exact target/partial the view subscribes to (drift here would silently break
# live updates), and an unrelated edit must NOT broadcast (the guard).
+ describe '#lock_in_project!' do
+ let(:meeting) { create(:meeting, organisation: organisation, project: project, creator: creator) }
+
+ it 'answers true while the meeting is in the authorized project' do
+ expect(Meeting.transaction { meeting.lock_in_project!(project.id) }).to be(true)
+ end
+
+ # The row is read again under the lock: a move that committed after the
+ # instance was loaded counts, not the project the instance still names.
+ it 'answers false once the row names another project' do
+ elsewhere = create(:project, organisation: organisation)
+ Meeting.where(id: meeting.id).update_all(project_id: elsewhere.id)
+
+ expect(Meeting.transaction { meeting.lock_in_project!(project.id) }).to be(false)
+ end
+ end
+
describe 'summary broadcast (after_update_commit)' do
let(:meeting) { create(:meeting, organisation: organisation, project: project, creator: creator, notes: 'x') }
let(:stream) { "#{ActionView::RecordIdentifier.dom_id(meeting)}_#{project.id}_summary" }