Recording disclosures¶
To record AI involvement from code, call ai_disclosure.recorder, which
implements DisclosureRecorderInterface. A calling module needs to know only
that interface.
suggest()¶
suggest() leaves a non-binding suggestion for the editor to Accept or
Dismiss on the entity form. It never changes the field.
\Drupal::service('ai_disclosure.recorder')->suggest(
$entity,
'ai_partly_assisted',
'my_module',
'Three paragraphs were rewritten by the assistant.',
);
The entity must be saved already. A second call from the same source on the same entity and language replaces the first one. If the editor has already accepted or dismissed that source's suggestion and the new call passes the same grade and the same note, the decision is kept. A different grade or a different note reopens the suggestion as pending, so a source that reads a new file should describe that file in the note.
A suggestion whose grade is less severe than the disclosure the entity has now
is never offered to the editor: it is stored with the status covered, with
its source and note, so the provenance stays on record. The field is not
touched. The source's pending suggestion for that entity and language is
replaced by that row, and a repeated call with the same grade and note adds
nothing. A decided suggestion stays. A covered row goes back to pending when
the same suggestion is no longer weaker than the disclosure.
apply() and applyProfile() refuse a downgrade in the same way.
The note is shown to the editor as text. Markup in it is escaped, not rendered, so a link or an emphasis written there reaches the form as the characters that spell it.
suggestProfile()¶
suggest() proposes a grade. To propose a whole profile, such as one that
also carries a human review claim and a description, call suggestProfile().
It lives on its own interface, DisclosureProfileSuggesterInterface, so
DisclosureRecorderInterface is unchanged. The same ai_disclosure.recorder
service implements both. Check before you call:
$recorder = \Drupal::service('ai_disclosure.recorder');
if ($recorder instanceof DisclosureProfileSuggesterInterface) {
$recorder->suggestProfile($entity, 'machine_translation', 'my_module');
}
else {
$recorder->suggest($entity, 'ai_translated', 'my_module');
}
It follows the rules of suggest(): the entity must be saved, the profile's
grade is compared with the disclosure in force and a lower one is not stored,
a second call from the same source replaces the first, and an unknown profile
logs a warning and records nothing. The row keeps the profile and its grade.
The editor sees the profile's label in the callout, and Accept applies the
profile to the field, not the bare grade. Dismiss changes nothing.
Accept compares the grade the profile has when the editor presses it, not the
one it had when the suggestion was stored. If the profile was deleted since,
Accept applies the grade stored with the suggestion. Until drush updb has
run, the suggestion table has no profile column: suggestProfile() then stores
the grade only and logs a warning once.
SuggestionStatusChangeEvent::getProfile() returns the profile of a suggestion
that is accepted or dismissed, or NULL for a grade.
Naming the fields¶
A module that knows which fields it wrote can say so, and the card under the
content then lists them, see
A page with several AI elements.
The methods are on DisclosureFieldRecorderInterface, implemented by the same
service, so DisclosureRecorderInterface is unchanged:
$recorder = \Drupal::service('ai_disclosure.recorder');
if ($recorder instanceof DisclosureFieldRecorderInterface) {
$recorder->suggestWithFields($entity, 'ai_generated_autonomous', 'my_module', NULL, ['body']);
}
suggestWithFields()behaves likesuggest().applyWithFields()behaves likeapply(), except that a write the current disclosure already covers is kept as a "covered" row with its fields, whereapply()records nothing.coverWithFields()keeps a source and its fields as a "covered" row, for a write that a stronger one of the same save outweighs.
The row stores a hash of each field's value as it is when you call. A source
keeps one row per entity and language, and its fields add up from call to call;
a field whose value changed since is dropped when the next call is made, and
left out of the card at once. The write tracker calls these methods for every
source plugin, so a plugin needs nothing more. Until drush updb has run, the
suggestion table has no field_hashes column and no field is listed.
apply() and applyProfile()¶
These two methods write the disclosure directly. They suit headless pipelines and cron jobs. Both refuse to downgrade: the write happens only when the requested grade's severity is strictly higher than the entity's currently effective grade. Both set the human review flag to FALSE, because an automated caller cannot attest that a person reviewed anything.
$recorder = \Drupal::service('ai_disclosure.recorder');
$recorder->applyProfile(
$entity,
'machine_translation',
'my_module',
);
Inside hook_entity_presave(), pass $save = FALSE. The recorder then
changes the field values on the entity object and leaves persistence to the
save cycle already running. Calling $entity->save() there would recurse:
$recorder->apply(
$entity,
'ai_translated',
'my_module',
NULL,
FALSE,
);
What a binding write leaves behind¶
A write that happens is recorded in the same
ai_disclosure_suggestion table suggest() writes to, as a row with status
accepted carrying the $source and the $note. It is provenance for a
decision already made, not a proposal: the editor's pending callout and the
report's pending-suggestion count both look at pending rows only, and the
note is never shown to a reader. applyProfile() records the grade the
profile resolves to. Which profile it was is on the field itself.
Nothing is recorded when the downgrade rule refuses an apply() or applyProfile() write, and nothing is
recorded when the write happens during an entity's first insert, because the
row is keyed by an entity ID that does not exist yet.
When suggest() meets a stricter disclosure, it replaces that source's pending
row for the entity and language with a covered row. An accepted or dismissed
row stays. A covered row is not a proposal: the callout and the
pending-suggestion count do not see it.
A suggestion left by suggest() only turns accepted when the entity form
that shows it is saved with that suggestion's grade still selected.
Clicking Accept in the editor's callout stages the intent on the form, it
does not write the row. ai_disclosure_settle_accepted_suggestions() checks,
after the save, that the field still carries the accepted grade before
flipping the status. A save that changes the grade again, or that never
happens, leaves the suggestion pending.
accepted means one thing: a write happened, through that form settlement or
through apply() or applyProfile().
Errors never break the caller's save¶
An unknown grade, an unknown profile or a bundle without the field never
breaks the caller's save: they log a warning on the ai_disclosure channel
and return. The one exception is suggest() on an unsaved entity, which
throws \InvalidArgumentException, because there is no ID to key the
suggestion row on.
The $source argument¶
The $source value is the calling module's machine name, for example ai_automators
or ai_translate. Every recorder call stores it, so the
suggestion table is where a site answers "which module said
this, and why". The compliance report does not expose
that column because it reports on the field, not on this table. The
suggestions listing does show it, so a source that
invents its own naming spoils the record for whoever reads that page or
queries the table directly.
What Views can build from suggestions now¶
The report's "Pending suggestions" column is a count: one number per
entity, pending rows only, computed against whichever field the row is
about. It answers "how many suggestions is this entity still waiting on",
and nothing about grade, source or note.
The suggestion table itself is a Views base table in its own
right, behind the access ai disclosure suggestions permission. That is a
separate, restricted permission, because a raw row exposes a note a
third-party module wrote, which the report never shows.
A view on it returns rows: every suggestion, in every status (pending, accepted, dismissed or covered), with its grade,
source and note, plus the user recorded against it, and a relationship toward
whichever entity type it names. That is where a site sees accepted and
dismissed rows at all, and where "which module suggested what, and why"
in $source and in the note gets read back. The shipped
suggestions listing is one such view. A site can
build its own on the same table.
A suggestion row's langcode follows the translation it was recorded
against: deleting that translation deletes the row with it, in both kinds
of view.
Reacting when a suggestion is decided¶
Accepting or dismissing a suggestion fires an event, so a module can follow
those decisions without polling the table. The name is
AiDisclosureEvents::SUGGESTION_STATUS_CHANGE. The object is a
SuggestionStatusChangeEvent. It carries the old status and the new one, with
the row's identity. That means its ID and the entity type, entity and langcode it is
about, the grade, the source, the note and the uid stored on the row.
The uid is the one the suggestion was recorded under, not the person
clicking Accept. For who is acting right now, read current_user inside the
subscriber.
Two silences are deliberate. An ID matching no row fires nothing, and
neither does a write that sets the status a row already had. The update
still refreshes the row's changed timestamp, but nothing decided, so
nothing is announced. The event is dispatched after the write has
succeeded. A subscriber reacts to a change and cannot veto it.
A subscriber that throws cannot break the change either. The exception is caught where the event is dispatched and written to the log, naming the suggestion, the two statuses and the subscriber that failed, and the call returns as it would have.
The audit trail submodule is the first consumer: it turns each of these into a row that says who decided what, and when.
Calling it from an agent¶
An AI agent reaches this service through the ai_disclosure_tool submodule,
which wraps it in one plugin for the contributed Tool API module. The plugin
adds no rule of its own: the invariants above are what an agent gets. What it
does add is the access check the service leaves to its caller, and a site
setting that keeps binding writes off until someone turns them on. See
The Tool plugin.