AI: address issue #483

このコミットが含まれているのは:
Codex Night Worker
2026-11-17 04:09:38 +09:00
コミット 9c71a9fc02
7個のファイルの変更、358行の追加、46行の削除
+1 -2
ファイルの表示
@@ -18,8 +18,7 @@ class StaticMirrorsController < ApplicationController
locale = resolve_locale!
session[:static_mirror_owner] ||= SecureRandom.hex(32)
job = StaticMirrorJob.start(owner: session[:static_mirror_owner], locale: locale.code,
origin: request.base_url)
job = StaticMirrorJob.start(owner: session[:static_mirror_owner], locale: locale.code)
render json: { job: }, status: :accepted
rescue StaticMirrorJob::Busy
response.headers['Retry-After'] = StaticMirrorJob::COOLDOWN.to_s
+19 -11
ファイルの表示
@@ -8,6 +8,7 @@ class StaticMirrorJob < ApplicationJob
EXPIRY = 10.minutes
COOLDOWN = 60
MAX_SECONDS = 60
ACTIVE = ['queued', 'running'].freeze
class Busy < StandardError; end
def self.directory = Rails.root.join('tmp/static_mirror')
@@ -36,9 +37,19 @@ class StaticMirrorJob < ApplicationJob
File.unlink(path) if path&.exist?
end
def self.start(owner:, locale:, origin:)
def self.expire_stalled(value)
return unless value && ACTIVE.include?(value['status'])
return unless Time.current.to_f - value.fetch('created_at') > MAX_SECONDS + 15
value.merge!('status' => 'failed', 'error' => 'worker_stopped')
write(value)
end
def self.start(owner:, locale:)
id = synchronise do
previous = state
expire_stalled(previous)
raise Busy if previous && ACTIVE.include?(previous['status'])
if previous && Time.current.to_f - previous.fetch('created_at') < COOLDOWN
raise Busy
end
@@ -48,7 +59,7 @@ class StaticMirrorJob < ApplicationJob
FileUtils.rm_f(directory.join('archive.zip'))
FileUtils.rm_f(directory.join('archive.part'))
identifier = SecureRandom.hex(24)
write({ 'id' => identifier, 'owner' => owner, 'locale' => locale, 'origin' => origin,
write({ 'id' => identifier, 'owner' => owner, 'locale' => locale,
'created_at' => Time.current.to_f, 'expiry_at' => (Time.current + EXPIRY).to_f,
'status' => 'queued', 'processed' => 0, 'total' => nil, 'data_at' => nil })
identifier
@@ -66,13 +77,9 @@ class StaticMirrorJob < ApplicationJob
return unless owner.present? && value && value['id'] == id && value['owner'] == owner
return unless value['expiry_at'] > Time.current.to_f
if (['queued', 'running'].include?(value['status']) &&
Time.current.to_f - value['created_at'] > MAX_SECONDS + 15)
value.merge!('status' => 'failed', 'error' => 'worker_stopped')
write(value)
end
expire_stalled(value)
if value['status'] == 'ready'
permitted = HistoryAccess.set(user: nil) { HistoryAccess.authorised_artifact?(value) }
permitted = StaticMirror.authorised_artifact?(value)
return unless permitted && directory.join('archive.zip').file?
end
value
@@ -91,7 +98,7 @@ class StaticMirrorJob < ApplicationJob
bytes = directory.join('archive.zip').binread
# Revalidate after reading; send_data avoids delayed sendfile reuse of a stale path.
permitted = HistoryAccess.set(user: nil) { HistoryAccess.authorised_artifact?(value) }
permitted = StaticMirror.authorised_artifact?(value)
permitted ? bytes : nil
end
end
@@ -109,7 +116,8 @@ class StaticMirrorJob < ApplicationJob
def perform(id)
File.open(self.class.directory.join('.generation.lock'), File::RDWR) do |lock|
return unless lock.flock(File::LOCK_EX | File::LOCK_NB)
# Intake briefly owns this lock. A queued worker must wait, never disappear.
lock.flock(File::LOCK_EX)
value = self.class.state
return unless value && value['id'] == id && value['status'] == 'queued'
@@ -127,7 +135,7 @@ class StaticMirrorJob < ApplicationJob
Timeout.timeout(remaining) do
locale = Locale.find_by!(code: value.fetch('locale'), deprecated_at: nil)
mirror = StaticMirror.new(locale:, origin: value.fetch('origin'))
mirror = StaticMirror.new(locale:)
bytes = mirror.export do |processed, total|
self.class.update(id) do |metadata|
metadata.merge!('processed' => processed, 'total' => total, 'data_at' => mirror.data_at)
+77 -23
ファイルの表示
@@ -6,6 +6,9 @@ require 'uri'
# A deliberately small current-state projection, always under public authority.
class StaticMirror
ORIGIN = 'https://hub.nizika.monster'.freeze
PUBLICATION_POLICY = 'static-mirror-public-projection-1'.freeze
DATA_AT_BASIS = 'capture_started_at'.freeze
RESOURCE_LIMIT = 100
NAME_LIMIT = 2_000
MAX_BYTES = 8.megabytes
@@ -18,7 +21,7 @@ class StaticMirror
attr_reader :data_at, :authority, :counts
def initialize(locale:, origin:)
def initialize(locale:, origin: ORIGIN)
@locale = locale
@origin = validated_url(origin).sub(%r{/+\z}, '')
end
@@ -30,11 +33,12 @@ class StaticMirror
capture
progress&.call(0, @posts.length + @tags.length)
generate_pages(&progress)
raise HistoryAccess::Forbidden unless HistoryAccess.authorised_artifact?(authority)
raise HistoryAccess::Forbidden unless self.class.authorised_artifact?(authority)
generated_at = Time.current.utc
manifest = { format_version: 1, generated_at: generated_at.iso8601(6),
data_at:, locale: @locale.code, resources: counts, exclusions: EXCLUSIONS,
data_at:, data_at_basis: DATA_AT_BASIS,
locale: @locale.code, resources: counts, exclusions: EXCLUSIONS,
fields: { posts: ['id', 'title', 'url', 'tags'], tags: ['id', 'category', 'names'] },
limits: { posts: RESOURCE_LIMIT, tags: RESOURCE_LIMIT, names: NAME_LIMIT,
bytes: MAX_BYTES },
@@ -51,33 +55,80 @@ class StaticMirror
end
end
# Static distribution has no admin mode or Counts aggregate exception.
# Recheck the exact public projection, including edits without a new discard.
def self.authorised_artifact?(metadata)
return false unless metadata['publication_policy'] == PUBLICATION_POLICY
ids = metadata.values_at('post_ids', 'tag_ids')
unless ids.all? { |values|
values.is_a?(Array) && values.length <= RESOURCE_LIMIT &&
values.all? { _1.is_a?(Integer) && _1.positive? }
}
return false
end
HistoryAccess.set(user: nil) do
ApplicationRecord.uncached do
CountsExporter.with_read_snapshot do
Locale.where(code: metadata['locale'], deprecated_at: nil).exists? &&
metadata['history_revision'] == HistoryAccess.revision &&
metadata['content_digest'] == projection_digest(
read_projection(post_ids: ids[0], tag_ids: ids[1]))
end
end
end
rescue TooLarge
false
end
def self.read_projection(post_ids: nil, tag_ids: nil)
posts = confirmed(Post, PostVersion, :post_versions)
tags = confirmed(Tag, TagVersion, :tag_versions).where(deprecated_at: nil)
posts = posts.where(id: post_ids) unless post_ids.nil?
tags = tags.where(id: tag_ids) unless tag_ids.nil?
posts = posts.order(:id).limit(RESOURCE_LIMIT).select(:id, :version_no, :title, :url).to_a
tags = tags.order(:id).limit(RESOURCE_LIMIT).select(:id, :version_no, :category).to_a
names = TagName.where(tag_id: tags.map(&:id)).order(:tag_id, :language_code, :id)
.limit(NAME_LIMIT + 1).pluck(:tag_id, :language_code, :script_code, :name, :primary_flg)
raise TooLarge if names.length > NAME_LIMIT
relations = PostTag.where(post_id: posts.map(&:id), tag_id: tags.map(&:id))
.order(:post_id, :tag_id).pluck(:post_id, :tag_id)
[posts, tags, names, relations]
end
def self.confirmed(model, version_model, association)
# Requiring a matching immutable revision closes the legacy unversioned gap.
HistoryAccess.current(model.all, version_model).joins(association)
.where("#{ version_model.table_name }.version_no = #{ model.table_name }.version_no")
end
def self.projection_digest(projection)
posts, tags, names, relations = projection
Digest::SHA256.hexdigest(JSON.generate(
[posts.map(&:attributes), tags.map(&:attributes), names, relations]))
end
private_class_method :confirmed
private
def capture
# Server clock at capture entry; MySQL establishes the read view later.
@data_at = Time.current.utc.iso8601(6)
CountsExporter.with_read_snapshot do
@data_at = Time.current.utc.iso8601(6)
@authority = HistoryAccess.authority
# Requiring a matching immutable revision closes the legacy unversioned gap.
@posts = confirmed(Post, PostVersion, :post_versions).order(:id)
.limit(RESOURCE_LIMIT).select(:id, :title, :url).to_a
@tags = confirmed(Tag, TagVersion, :tag_versions).where(deprecated_at: nil)
.order(:id).limit(RESOURCE_LIMIT).select(:id, :category).to_a
@names = TagName.where(tag_id: @tags.map(&:id)).order(:tag_id, :language_code, :id)
.limit(NAME_LIMIT + 1).pluck(:tag_id, :language_code, :script_code, :name, :primary_flg)
raise TooLarge if @names.length > NAME_LIMIT
@names = @names.group_by(&:first)
@relations = PostTag.where(post_id: @posts.map(&:id), tag_id: @tags.map(&:id))
.order(:post_id, :tag_id).pluck(:post_id, :tag_id)
projection = self.class.read_projection
@posts, @tags, names, @relations = projection
@authority = { 'publication_policy' => PUBLICATION_POLICY,
'locale' => @locale.code,
'history_revision' => HistoryAccess.revision,
'post_ids' => @posts.map(&:id), 'tag_ids' => @tags.map(&:id),
'content_digest' => self.class.projection_digest(projection) }
@names = names.group_by(&:first)
@counts = { posts: @posts.length, tags: @tags.length }
end
end
def confirmed(model, version_model, association)
HistoryAccess.current(model.all, version_model).joins(association)
.where("#{ version_model.table_name }.version_no = #{ model.table_name }.version_no")
end
def generate_pages
assets = Rails.root.join('app/views/static_mirror')
['archive.css', 'archive.js'].each do |name|
@@ -108,7 +159,10 @@ class StaticMirror
yield(processed, @posts.length + @tags.length) if block_given?
end
index = @posts.map { |post|
{ path: resource_path('posts', post.id), title: post.title.presence || post.id.to_s }
tag_ids = @relations.filter_map { |post_id, tag_id| tag_id if post_id == post.id }
names = tag_ids.flat_map { |id| @names.fetch(id, []).map { _1[3] } }.uniq
{ path: resource_path('posts', post.id), title: post.title.presence || post.id.to_s,
names: }
} + @tags.map { |tag|
{ path: resource_path('tags', tag.id), title: tag_name(tag),
names: @names.fetch(tag.id, []).map { _1[3] } }
+91 -3
ファイルの表示
@@ -38,7 +38,8 @@ RSpec.describe 'Static Mirror API', type: :request do
get "/static_mirror/jobs/#{ id }"
expect(json).to include('status' => 'ready', 'processed' => 1, 'total' => 1)
expect(json.fetch('data_at')).to be_present
expect(json.keys).not_to include('owner', 'origin', 'actor_id', 'policy_revision')
expect(json.keys).not_to include('owner', 'origin', 'actor_id', 'policy_revision',
'publication_policy', 'history_revision', 'post_ids', 'tag_ids', 'content_digest')
get "/static_mirror/files/#{ id }"
expect(response).to have_http_status(:ok)
expect(response.media_type).to eq('application/zip')
@@ -47,6 +48,21 @@ RSpec.describe 'Static Mirror API', type: :request do
expect(response.headers['Cache-Control']).to include('no-store')
end
it 'bakes the canonical source origin regardless of request and forwarded hosts' do
host! 'mirror-intake.test'
post '/static_mirror/jobs', params: { locale: 'ja' }, as: :json,
headers: { 'X-Forwarded-Host' => 'untrusted-source.test',
'X-Forwarded-Proto' => 'https' }
expect(response).to have_http_status(:accepted)
id = json.fetch('job')
expect(StaticMirrorJob.state).not_to have_key('origin')
StaticMirrorJob.perform_now(id)
get "/static_mirror/files/#{ id }"
expect(response).to have_http_status(:ok)
expect(response.body).to include('https://hub.nizika.monster/ja')
expect(response.body).not_to include('mirror-intake.test', 'untrusted-source.test')
end
it 'rejects historical and unknown input instead of pretending to restore it' do
['at', 'data_at', 'admin', 'limit'].each do |key|
post '/static_mirror/jobs', params: { locale: 'ja', key => '2020-01-01' }, as: :json
@@ -87,6 +103,21 @@ RSpec.describe 'Static Mirror API', type: :request do
expect(response).to have_http_status(:not_found)
end
it 'rechecks ready ZIPs after a non-discard content change' do
record = create(:post, title: 'mirror-withdrawn-title')
PostVersion.create!(post: record, version_no: 1, event_type: 'create',
title: record.title, url: record.url, tags: '', tags_json: [], parent_post_ids: '',
created_at: Time.current)
id = create_job
StaticMirrorJob.perform_now(id)
record.update!(title: 'mirror-corrected-title')
get "/static_mirror/jobs/#{ id }"
expect(response).to have_http_status(:not_found)
get "/static_mirror/files/#{ id }"
expect(response).to have_http_status(:not_found)
expect(response.body).not_to include('mirror-withdrawn-title')
end
it 'fails lost jobs and rejects expired downloads' do
id = create_job
travel 80.seconds do
@@ -110,11 +141,68 @@ RSpec.describe 'Static Mirror API', type: :request do
end
end
it 'keeps simultaneous generation bounded after the cooldown' do
it 'preserves the queued reservation after the cooldown' do
id = create_job
travel 61.seconds do
post '/static_mirror/jobs', params: { locale: 'ja' }, as: :json
expect(response).to have_http_status(:too_many_requests)
expect(StaticMirrorJob.state).to include('id' => id, 'status' => 'queued')
expect(StaticMirrorJob).to have_received(:perform_later).once
StaticMirrorJob.perform_now(id)
get "/static_mirror/jobs/#{ id }"
expect(json).to include('status' => 'failed', 'error' => 'generation_timeout')
end
end
it 'reclaims a stalled queued reservation without waiting for a status request' do
id = create_job
travel 80.seconds do
second_id = create_job
expect(second_id).not_to eq(id)
StaticMirrorJob.perform_now(id)
expect(StaticMirrorJob.state).to include('id' => second_id, 'status' => 'queued')
StaticMirrorJob.perform_now(second_id)
get "/static_mirror/jobs/#{ second_id }"
expect(json).to include('status' => 'ready')
end
end
it 'waits for brief intake lock contention instead of losing the queued worker' do
id = create_job
path = StaticMirrorJob.directory.join('.generation.lock')
lock = File.open(path, File::RDWR)
lock.flock(File::LOCK_EX)
attempts = Queue.new
worker = StaticMirrorJob.new
allow(worker).to receive(:generate)
allow(File).to receive(:open).and_call_original
allow(File).to receive(:open).with(path, File::RDWR).and_wrap_original do |original, *args, &block|
original.call(*args) do |file|
allow(file).to receive(:flock).and_wrap_original do |flock, mode|
attempts << mode
flock.call(mode)
end
block.call(file)
end
end
thread = Thread.new { worker.perform(id) }
Timeout.timeout(5) do
expect(attempts.pop).to eq(File::LOCK_EX)
lock.close
thread.join
end
expect(worker).to have_received(:generate).with(id, hash_including('status' => 'queued'))
ensure
lock&.close unless lock&.closed?
thread&.kill
thread&.join
end
it 'keeps the generation lock reserved even when stale metadata expires' do
create_job
lock = File.open(StaticMirrorJob.directory.join('.generation.lock'), File::RDWR)
lock.flock(File::LOCK_EX)
travel 61.seconds do
travel 80.seconds do
post '/static_mirror/jobs', params: { locale: 'ja' }, as: :json
expect(response).to have_http_status(:too_many_requests)
end
+93 -2
ファイルの表示
@@ -2,6 +2,7 @@ require 'rails_helper'
RSpec.describe StaticMirror do
include_context 'Ainu locale'
include ActiveSupport::Testing::TimeHelpers
let(:tag) { create(:tag, primary_name: 'mirror-public-tag') }
let(:post) { create(:post, title: '<img src=x onerror=alert(1)>') }
@@ -72,7 +73,8 @@ RSpec.describe StaticMirror do
.to include("../../tags/#{ tag.id }/index.html")
manifest = JSON.parse(files.fetch('manifest.json'))
expect(manifest).to include('format_version' => 1, 'locale' => 'ja',
'data_at' => mirror.data_at, 'resources' => { 'posts' => 1, 'tags' => 1 })
'data_at' => mirror.data_at, 'data_at_basis' => 'capture_started_at',
'resources' => { 'posts' => 1, 'tags' => 1 })
expect(Time.iso8601(manifest.fetch('generated_at'))).to be >= Time.iso8601(mirror.data_at)
expect(manifest.fetch('files').map { _1.fetch('path') })
.to match_array(files.keys - ['manifest.json'])
@@ -101,7 +103,9 @@ RSpec.describe StaticMirror do
expect(files.values.join).not_to include('mirror-secret', legacy.title, private_post.url)
expect(files.keys).not_to include("ja/tags/#{ private_tag.id }/index.html",
"ja/posts/#{ private_post.id }/index.html", "ja/posts/#{ referencing.id }/index.html")
expect(mirror.authority.fetch('admin')).to be(false)
expect(mirror.authority.fetch('publication_policy'))
.to eq('static-mirror-public-projection-1')
expect(mirror.authority.keys).not_to include('admin', 'actor_id', 'artifact_policy')
end
it 'applies later discard boundaries after a restoration' do
@@ -132,6 +136,74 @@ RSpec.describe StaticMirror do
end.to raise_error(HistoryAccess::Forbidden)
end
it 'owns the static publication contract independently of Counts' do
allow(HistoryAccess).to receive(:authority).and_raise('Counts metadata is not applicable')
allow(HistoryAccess).to receive(:authorised_artifact?).and_return(false)
archive_files(mirror.export)
expect(described_class.authorised_artifact?(mirror.authority)).to be(true)
metadata = mirror.authority.except('publication_policy')
.merge('artifact_policy' => HistoryAccess::ARTIFACT_POLICY)
expect(described_class.authorised_artifact?(metadata)).to be(false)
stub_const('HistoryAccess::ARTIFACT_POLICY', 'another-counts-policy')
expect(described_class.authorised_artifact?(mirror.authority)).to be(true)
end
it 'rejects stale projections after edits without discard, including for admins' do
create(:tag_name, :alias, tag:, name: 'mirror-old-alias')
archive_files(mirror.export)
HistoryAccess.set(user: create(:user, :admin)) do
expect(described_class.authorised_artifact?(mirror.authority)).to be(true)
TagName.find_by!(tag:, name: 'mirror-old-alias').update!(name: 'mirror-new-alias')
expect(described_class.authorised_artifact?(mirror.authority)).to be(false)
end
end
it 'rejects a deprecated tag and its names without requiring a discard' do
archive_files(mirror.export)
tag.update!(deprecated_at: Time.current)
expect(described_class.authorised_artifact?(mirror.authority)).to be(false)
files = archive_files(mirror.export)
expect(files.keys).not_to include("ja/tags/#{ tag.id }/index.html")
expect(files.values.join).not_to include('mirror-public-tag')
end
it 'rejects stale post content and relations without a discard' do
archive_files(mirror.export)
post.update!(title: 'mirror-corrected-title')
expect(described_class.authorised_artifact?(mirror.authority)).to be(false)
archive_files(mirror.export)
PostTag.where(post:, tag:).delete_all
expect(described_class.authorised_artifact?(mirror.authority)).to be(false)
end
it 'rejects an artifact after its locale is deprecated' do
archive_files(mirror.export)
Locale.nipponese.update!(deprecated_at: Time.current)
expect(described_class.authorised_artifact?(mirror.authority)).to be(false)
end
it 'fails if a tag is deprecated during generation' do
expect do
mirror.export do |processed, _total|
tag.update!(deprecated_at: Time.current) if processed.zero?
end
end.to raise_error(HistoryAccess::Forbidden)
end
it 'records capture entry rather than claiming the exact DB read-view time' do
freeze_time do
capture_at = Time.current.utc.iso8601(6)
allow(CountsExporter).to receive(:with_read_snapshot).and_wrap_original do |original, &block|
travel 5.seconds
original.call(&block)
end
files = archive_files(mirror.export)
manifest = JSON.parse(files.fetch('manifest.json'))
expect(manifest).to include('data_at' => capture_at, 'data_at_basis' => 'capture_started_at')
expect(Time.iso8601(manifest.fetch('generated_at'))).to be > Time.iso8601(capture_at)
end
end
it 'bounds output and never reports hidden population counts' do
stub_const('StaticMirror::RESOURCE_LIMIT', 1)
second = create(:post, title: 'mirror-beyond-limit')
@@ -148,6 +220,25 @@ RSpec.describe StaticMirror do
files = archive_files(described_class.new(locale:, origin: 'https://source.test').export)
detail = files.fetch("ain-Kana/tags/#{ tag.id }/index.html")
expect(detail).to include('mirror-public-tag', 'mirror-alias', 'lang="ain"', 'lang="ja"')
index = JSON.parse(files.fetch('search-index.json'))
entry = index.find { _1.fetch('path') == "ain-Kana/posts/#{ post.id }/index.html" }
expect(entry.fetch('names')).to include('mirror-public-tag', 'mirror-alias')
end
it 'indexes only names of public linked tags within the output limit' do
other = create(:tag, primary_name: 'mirror-unlinked-tag')
snapshot_tag(other)
private_tag = create(:tag, primary_name: 'mirror-private-tag')
snapshot_tag(private_tag, event: 'discard')
PostTag.create!(post:, tag: private_tag)
files = archive_files(mirror.export)
index = JSON.parse(files.fetch('search-index.json'))
entry = index.find { _1.fetch('path') == "ja/posts/#{ post.id }/index.html" }
expect(entry.fetch('names')).to include('mirror-public-tag')
expect(entry.fetch('names')).not_to include('mirror-private-tag', 'mirror-unlinked-tag')
stub_const('StaticMirror::RESOURCE_LIMIT', 1)
expect(archive_files(mirror.export).fetch('search-index.json'))
.not_to include('mirror-unlinked-tag', 'mirror-private-tag')
end
it 'fails rather than truncating registered aliases at the name limit' do