feat(formats): implement author attribution service and propagate spec tests

Book metadata extraction previously handled author parsing naively by taking only the first raw dc:creator string, ignoring MARC relator roles, multi-creator ordering, deduplication, and structured author fallbacks.

Define the formal author attribution model in specs/book-author-parsing.allium and propagate the specification into a comprehensive test suite (AuthorAttributionTest) covering all 23 obligations across value equality, lifecycle state transitions, contract invariants, and surface exposure.

Implement AuthorAttribution, AuthorAttributionService, and BookMetadataSurface to fulfill:
- Dublin Core precedence over structured author elements
- MARC role code resolution (aut -> Author, edt -> Editor, trl -> Translator, ill -> Illustrator, custom capitalized)
- Grouping creators by resolved role in order of appearance with deduplication
- Fallback formatting for structured author fields (first, middle, last, nickname)
- Complete status lifecycle enforcement (unparsed to parsed)

Register AuthorAttributionTest in CMakeLists.txt and update AGENTS.md documentation.
This commit is contained in:
Agel committed 2026-10-29 15:21:06 +01:00
1 parent 306d4cac5c
commit bde4e310b5
6 files changed
+1005

No files matched your search

+151
View File
@@ -0,0 +1,151 @@
-- allium: 3
-- book-author-parsing.allium
-- Scope: Construction of book author attribution string from EPUB and FB2 metadata in Asteria
-- Includes:
-- - Extraction of Dublin Core dc:creator elements with role attributes
-- - Fallback to structured author elements (first-name, middle-name, last-name, nickname)
-- - MARC role mapping (aut -> Author, edt -> Editor, trl -> Translator, ill -> Illustrator, custom capitalized)
-- - Role grouping in document order
-- - Deduplication of creator names within each role group
-- - Formatted output string representation ("Author: Bob, Charlie, Editor: Alice")
-- Excludes:
-- - Low-level XML streaming and archive decompression
-- - UI rendering and line-breaking
------------------------------------------------------------
-- Value Types
------------------------------------------------------------
value StructuredAuthor {
first_name: String?
middle_name: String?
last_name: String?
nickname: String?
}
value CreatorItem {
role_code: String?
name: String
}
------------------------------------------------------------
-- Contracts
------------------------------------------------------------
contract AuthorAttributionService {
resolve_attribution: (file_path: String) -> String
@invariant Precedence
-- If Dublin Core creators are present, they take precedence;
-- structured author elements are used only as a fallback when DC creators are absent.
@invariant RoleMapping
-- Roles "aut", empty, or unspecified map to "Author".
-- Standard MARC codes map to human-readable names:
-- "edt" -> "Editor", "trl" -> "Translator", "ill" -> "Illustrator".
-- Unrecognized or custom roles are capitalized as-is.
@invariant GroupingAndDeduplication
-- Creators are grouped by resolved role in order of first appearance.
-- Identical names within the same role are deduplicated, preserving first appearance.
-- Each role group is formatted as "<Role>: <Name1>, <Name2>".
-- Role groups are joined with ", ".
@invariant EmptyFallback
-- If no valid creator or structured author elements exist, the attribution is empty string.
}
------------------------------------------------------------
-- Entities and Variants
------------------------------------------------------------
entity Book {
file_path: String
is_active: Boolean
}
entity BookMetadata {
book: Book
status: unparsed | parsed
transitions status {
unparsed -> parsed
terminal: parsed
}
raw_dc_creators: List<CreatorItem>
raw_structured_authors: List<StructuredAuthor>
author_attribution: String when status = parsed
}
------------------------------------------------------------
-- Rules
------------------------------------------------------------
rule LoadMetadata {
when: LoadMetadataTrigger(book, creators, authors)
ensures: BookMetadata.created(
book: book,
status: unparsed,
raw_dc_creators: creators,
raw_structured_authors: authors
)
}
rule ParseMetadataWithDcCreators {
when: ParseMetadata(metadata)
requires: metadata.status = unparsed
requires: metadata.raw_dc_creators.count > 0
ensures:
metadata.status = parsed
metadata.author_attribution = format_creators(metadata.raw_dc_creators)
}
rule ParseMetadataFallbackToStructuredAuthors {
when: ParseMetadata(metadata)
requires: metadata.status = unparsed
requires: metadata.raw_dc_creators.count = 0
ensures:
metadata.status = parsed
metadata.author_attribution = format_structured_authors(metadata.raw_structured_authors)
}
------------------------------------------------------------
-- Invariants
------------------------------------------------------------
invariant FormattedAttributionRequiresParsedState {
for metadata in BookMetadatas where status = parsed:
metadata.author_attribution != null
}
------------------------------------------------------------
-- Actor Declarations
------------------------------------------------------------
actor ReaderEngine {
identified_by: Book where is_active = true
}
------------------------------------------------------------
-- Surfaces
------------------------------------------------------------
surface BookMetadataSurface {
facing engine: ReaderEngine
context metadata: BookMetadata
contracts:
fulfils AuthorAttributionService
exposes:
metadata.book.file_path
metadata.author_attribution when metadata.status = parsed
provides:
LoadMetadataTrigger(metadata.book, metadata.raw_dc_creators, metadata.raw_structured_authors)
when engine.is_active
ParseMetadata(metadata)
when metadata.status = unparsed and engine.is_active
}