Files
craft-ai-integrations/tools/mutation/mutation_contract.py
Jeremy d37d4528c5 fix(ai-native): make private files owner-only on Windows (#184)
On Unix, craft-ai-native creates its private files with mode 0600. On Windows they took their folder's access list, which is not private in a folder that CRAFT_AI_DIR names outside %LOCALAPPDATA%.

- Private files and the folders this crate creates get an owner-only access list (the user's SID and SYSTEM) in the call that creates them, so no other account can open them in between.
- Creation does not follow a link planted at the name, and handles paths past 260 characters.
- A leftover temp file is removed and created again, never reused. This also fixes the Unix case.
- The Win32 calls live in crates/ai-native/src/owner_only/win_acl.rs, the workspace's one named unsafe exception. The workspace lint moves from forbid to deny, and tests/unsafe_exception.rs fails if unsafe is allowed anywhere else.
- Volumes without access lists (FAT, exFAT) still save, without one, as documented.
2026-11-11 21:47:02 +00:00

334 lines
20 KiB
Python

"""Regression guards for the contract fixes already merged in craft-libs.
Do not import the runner here: it executes immediately. Cases use its public tuple format and
name a focused runtime test so a compile error can never count as evidence.
"""
CORE = 'crates/ai-core/src/'
DRAFT = CORE + 'draft.rs'
ROLE = CORE + 'role.rs'
CLIENT = 'crates/ai-client/src/'
RUN = CLIENT + 'client_run.rs'
SIZE = CLIENT + 'disclosed_size.rs'
ASSIGN = CLIENT + 'assignments.rs'
REFRESH = CLIENT + 'assignments/refresh.rs'
MANAGE = CLIENT + 'manage.rs'
M = 'craft-ai-core'
CL = 'craft-ai-client'
CA = [
('sample-approval-ignores-changed-destination', 'crates/ai-client/src/tools_sample.rs',
'if reviewed != Some(plan.disclosure()) {', 'if reviewed.is_none() {',
CL, 'sample_consent', 'changed_destination_asks_again_before_saving_or_building'),
('sample-approval-compares-only-destination', 'crates/ai-client/src/tools_sample.rs',
'if reviewed != Some(plan.disclosure()) {',
'if reviewed.is_none_or(|shown| shown.destination != plan.disclosure().destination) {',
CL, 'sample_consent', 'changed_payload_asks_again_before_saving_or_building'),
('disclosed-file-size-not-measured', RUN,
'crate::disclosed_size::measure_local_files(&mut disclosure, &request);', '',
CL, 'disclosed_size', 'a_local_files_size_is_measured_not_taken_from_the_app'),
('unreadable-file-keeps-claimed-size', SIZE,
'item.bytes = local_file_len(&url),', 'item.bytes = local_file_len(&url).or(item.bytes),',
CL, 'disclosed_size', 'a_local_file_that_cannot_be_read_shows_no_size'),
('unparsed-address-keeps-claimed-size', SIZE,
'Err(_) => item.bytes = None,', 'Err(_) => {}',
CL, 'disclosed_size', 'addresses_that_do_not_parse_show_no_size'),
('folder-size-disclosed', SIZE,
'meta.is_file().then_some(meta.len())', 'Some(meta.len())',
CL, 'disclosed_size', 'a_local_file_that_cannot_be_read_shows_no_size'),
('web-handle-size-dropped', SIZE,
'Ok(_) => {}', 'Ok(_) => item.bytes = None,',
CL, 'disclosed_size', 'other_payloads_keep_their_size'),
('late-model-answer-taken', DRAFT,
'''pub fn set_models(&mut self, address: &str, models: Vec<ModelInfo>) -> bool {
if address.trim() != self.endpoint.trim() {''',
'''pub fn set_models(&mut self, address: &str, models: Vec<ModelInfo>) -> bool {
if false {''',
M, 'draft_provenance', 'a_test_that_answers_after_the_address_moved_on'),
('probe-address-forgotten', DRAFT,
'''self.models = models;
self.models_for = Some(self.endpoint.trim().to_owned());''',
'''self.models = models;
self.models_for = None;''',
M, 'draft_provenance', 'new_draft_discards_models'),
('model-address-not-checked', DRAFT,
'.is_none_or(|origin| origin == address)', '.is_none_or(|_| true)',
M, 'draft_provenance', 'returning_to_original_address'),
('preset-address-forgotten', DRAFT,
'models_for: (!models.is_empty()).then(|| preset.endpoint.trim().to_owned()),',
'models_for: None,', M, 'draft_provenance', 'preset_models_belong'),
('status-ignores-a-refused-route', 'crates/ai-client/src/client.rs',
'return unavailable(reason);', 'let _ = reason;',
CL, 'route_support', 'a_saved_omni_model'),
('plan-ignores-a-refused-route', 'crates/ai-client/src/client_run.rs',
'self.refuse_unsupported_route(resolved.connection, &resolved.model.id, &request)?;', '',
CL, 'route_support', 'it_is_refused_at_plan'),
('start-ignores-a-refused-route', 'crates/ai-client/src/client_run.rs',
'self.refuse_unsupported_route(&plan.connection, &plan.model, &plan.request)?;', '',
CL, 'route_support_limits', 'start_checks_the_route_again'),
('route-ignores-options', 'crates/ai-client/src/route_support.rs',
'if accepted && complete && outputs && stream && !query.has_options {',
'if accepted && complete && outputs && stream {',
CL, 'route_support_limits', 'options_outputs_and_streaming'),
('route-ignores-outputs', 'crates/ai-client/src/route_support.rs',
'let outputs = (1..=spec.max_outputs).contains(&query.outputs);', 'let outputs = true;',
CL, 'route_support_limits', 'options_outputs_and_streaming'),
('route-ignores-streaming', 'crates/ai-client/src/route_support.rs',
'let stream = !query.stream || spec.features.streaming;', 'let stream = true;',
CL, 'route_support_limits', 'options_outputs_and_streaming'),
('plan-hides-the-options', 'crates/ai-client/src/client_run.rs',
'has_options: !request.params.is_empty(),', 'has_options: false,',
CL, 'route_support_limits', 'options_outputs_and_streaming'),
('vertex-ignores-options', 'crates/ai-client/src/route_support.rs',
'&& !query.has_options;', ';',
CL, 'route_support_limits', 'vertex_refuses_options'),
('offered-without-an-adapter', 'crates/ai-client/src/client.rs',
'!self.factory.knows(&connection.provider)\n ||', 'false\n ||',
CL, 'route_support_limits', 'a_provider_with_no_adapter'),
('bound-without-an-adapter', 'crates/ai-client/src/client.rs',
'if !self.factory.knows(&resolved.connection.provider) {\n return Err(AiError::unsupported(NO_ADAPTER));',
'if false {\n return Err(AiError::unsupported(NO_ADAPTER));',
CL, 'route_support_limits', 'a_provider_with_no_adapter'),
('input-count-not-checked', 'crates/ai-client/src/route_support.rs',
'.all(|(role, n)| spec.accepts.get(*role).is_some_and(|max| n <= max));',
'.all(|(role, _)| spec.accepts.contains_key(*role));',
CL, 'route_support_limits', 'more_of_an_input'),
('built-in-address-not-checked', 'crates/ai-client/src/route_support.rs',
'if query.connection.endpoint.as_deref().is_some_and(allowed) {', 'if true {',
CL, 'route_support_limits', 'a_built_in_adapter_refuses_an_address'),
('last-success-ignores-address', 'crates/ai-client/src/client.rs',
'.filter(|e| e.start.destination == destination)', '',
CL, 'route_support_limits', 'the_last_success'),
('last-success-counts-a-failure', 'crates/ai-client/src/client.rs',
'.filter(|end| end.outcome == Outcome::Done)', '',
CL, 'route_support_limits', 'the_last_success'),
('refused-choice-rewrites-store', 'crates/ai-client/src/tools.rs',
'let written = self.shared.try_update(|store| {', 'let written = self.shared.update(|store| {',
CL, 'route_support_limits', 'a_refused_choice_does_not_rewrite'),
('picks-offers-a-refused-route', 'crates/ai-client/src/client.rs',
'eligible.retain(|r| !self.known_unsupported(role, r.connection, &r.model.id));', '',
CL, 'route_support', 'a_model_known_to_be_refused'),
('replaced-adapter-keeps-the-old-limits', 'crates/ai-client/src/factory.rs',
'''self.accelerators.remove(&name);
self.support.remove(&name);
self.standard_senders.remove(&name);
self.builders.insert''',
'''self.accelerators.remove(&name);
self.standard_senders.remove(&name);
self.builders.insert''',
CL, 'route_support', 'a_custom_adapter_replacing'),
('no-answer-counts-as-supported', 'crates/ai-client/src/factory.rs',
'.map_or(RouteSupport::Untested, |check| check(query))',
'.map_or(RouteSupport::Supported, |check| check(query))',
CL, 'route_support', 'an_adapter_that_does_not_say'),
('picker-offers-a-refused-route', 'crates/ai-client/src/client.rs',
'.filter(|r| !self.known_unsupported(role, r.connection, &r.model.id))', '',
CL, 'route_support', 'a_model_known_to_be_refused'),
('inputs-not-checked', 'crates/ai-client/src/route_support.rs',
'if accepted && complete && outputs && stream && !query.has_options {',
'if complete && outputs && stream && !query.has_options {',
CL, 'route_support', 'inputs_a_listed_model'),
('assign-skips-the-final-check', ASSIGN,
'''let resolved = resolve(role, &[std::slice::from_ref(&binding)], &store.connections)
.map_err(|e| AiError::invalid(e.to_string()))?;
refuse_unsupported(self, role, resolved.connection, model)?;
let layer = layer_mut(store, scope);''',
'let layer = layer_mut(store, scope);',
CL, 'model_assignment', 'a_model_that_cannot_do_the_job'),
('declared-ability-not-marked', ASSIGN,
'info.tags.push(declared_tag(&role.capability));', '',
CL, 'model_assignment', 'a_person_can_declare_an_unlisted_model'),
('withdraw-removes-a-provider-ability', ASSIGN,
'if !declared {', 'if false {',
CL, 'model_assignment', 'withdrawing_never_removes'),
('stale-withdraw-accepted', ASSIGN,
'if what == Withdraw::Choice && selected != Some(true) {', 'if false {',
CL, 'model_assignment', 'withdrawing_a_choice_that_has_moved_on'),
('withdraw-leaves-a-binding-stale', ASSIGN,
'if let Some((user, scope)) = binding_needing(store, role, connection, model) {',
'if let Some((user, scope)) = None::<(RoleId, ChoiceScope)> {',
CL, 'model_assignment', 'withdrawing_the_ability_is_refused_while_another_tool'),
('withdraw-ignores-app-overrides', ASSIGN,
'.map(|b| (b.role.clone(), ChoiceScope::App(app.clone())))',
'.filter(|_| false).map(|b| (b.role.clone(), ChoiceScope::App(app.clone())))',
CL, 'model_assignment', 'withdrawing_the_ability_is_refused_while_an_app_override'),
('refresh-drops-declared-abilities', MANAGE,
'saved.models = refreshed_models(&saved.models, models);', 'saved.models = models;',
CL, 'model_refresh', 'a_refresh_keeps_declared'),
('assign-ignores-a-refused-route', ASSIGN,
'refuse_unsupported(self, role, resolved.connection, model)?;', 'let _ = resolved;',
CL, 'assignment_guards', 'assigning_a_route_the_adapter_refuses'),
('state-offers-a-refused-route', ASSIGN,
'refuse_unsupported(self, role, resolved.connection, model).map_err(|e| e.message)',
'''let _ = resolved;
Ok(())''',
CL, 'assignment_guards', 'assigning_a_route_the_adapter_refuses'),
('refused-route-blocks-an-adapterless-default', ASSIGN,
'RouteSupport::Supported | RouteSupport::Untested => Ok(()),',
'RouteSupport::Supported => Ok(()),\n RouteSupport::Untested => Err(AiError::unsupported("untested")),',
CL, 'assignment_guards', 'a_provider_with_no_adapter'),
('withdraw-trusts-a-replaced-tool', ASSIGN,
'''// caller holding the old one must not take back the new choice or its ability.
store
.check_tool(role)
.map_err(|e| AiError::invalid(e.to_string()))?;''',
'// caller holding the old one must not take back the new choice or its ability.',
CL, 'assignment_guards', 'withdrawing_with_a_tool_that_was_replaced'),
('refresh-drops-a-declared-input', REFRESH,
'Some(reported) => add_input(reported, &mut now.tags, &input, most),',
'Some(_) => {}',
CL, 'model_refresh', 'a_refresh_keeps_an_input'),
('refresh-forgets-an-input-while-the-ability-is-gone', REFRESH,
'None if !now.tags.contains(tag) => now.tags.push(tag.clone()),',
'None if false => now.tags.push(tag.clone()),',
CL, 'model_refresh', 'a_declared_input_survives_two_refreshes'),
('store-writes-in-place', 'crates/ai-native/src/fsutil.rs',
'let result = write_tmp(&tmp, bytes).and_then(|()| fs::rename(&tmp, path));',
'let result = fs::write(path, bytes);',
CL, 'assignment_guards', 'a_failed_write_leaves_no_part_of_an_assignment'),
('private-write-reuses-a-leftover-temp-file', 'crates/ai-native/src/owner_only.rs',
'options.write(true).create_new(true);',
'options.write(true).create(true).truncate(true);',
'craft-ai-native', 'lib', 'a_leftover_temp_file_does_not_loosen_the_saved_file'),
('save-drops-a-model-listed-later', MANAGE,
'if !draft.opened_with().contains(&later.id)',
'if false && !draft.opened_with().contains(&later.id)',
CL, 'assignment_guards', 'saving_a_form_opened_before_a_model_was_listed'),
('declared-input-not-marked', ASSIGN,
'info.tags.push(declared_input_tag(&role.capability, input));', '',
CL, 'model_refresh', 'a_refresh_keeps_an_input'),
('save-drops-declarations', MANAGE,
'let mut models = refreshed_models(&slot.models, listed);',
'let mut models = listed;',
CL, 'assignment_guards', 'saving_a_'),
('stale-form-restores-a-withdrawal', MANAGE,
'let listed = without_withdrawn(listed, &slot.models);', '',
CL, 'withdrawal_scope', 'a_form_opened_before_a_withdrawal_does_not_bring_it_back'),
('stale-form-restores-a-withdrawn-input', REFRESH,
'spec.accepts.remove(input);', '',
CL, 'withdrawal_scope', 'a_form_opened_before_a_withdrawal_does_not_bring_back_an_input'),
('declared-input-blocked-by-any-tool', ASSIGN,
'&& (whole', '&& (true',
CL, 'withdrawal_scope', 'only_a_choice_that_takes_the_declared_input'),
('refusal-hides-the-scope', ASSIGN,
"ChoiceScope::App(app) => format!(\"{app}'s own choice\"),",
'ChoiceScope::App(_) => "a choice".to_owned(),',
CL, 'withdrawal_scope', 'a_refusal_names_the_scope'),
('set-enabled-unknown-writes', MANAGE,
''' self.shared.try_update(|saved| {
saved
''',
''' self.shared.update(|saved| {
saved
''',
CL, 'withdrawal_scope', 'changes_with_nothing_to_change_write_nothing'),
('delete-unknown-writes', MANAGE,
'let (gone, bindings_removed, key_still_used) = self.shared.try_update(|saved| {',
'let (gone, bindings_removed, key_still_used) = self.shared.update(|saved| {',
CL, 'withdrawal_scope', 'changes_with_nothing_to_change_write_nothing'),
('clear-nothing-writes', 'crates/ai-client/src/client.rs',
'let _ = self.shared.try_update(|file| {', 'let _ = self.shared.update(|file| {',
CL, 'withdrawal_scope', 'changes_with_nothing_to_change_write_nothing'),
('probe-answer-wipes-declared-models', 'crates/ai-egui/src/panel.rs',
'Some(_) => self.form.draft.confirm_models_for(tested),',
'Some(_) => self.form.draft.set_models(tested, Vec::new()),',
'craft-ai-egui', 'lib', 'a_custom_connection_test_keeps_the_declared_models'),
('custom-test-leaves-models-on-the-old-address', DRAFT,
''' return false;
}
self.models_for = Some(self.endpoint.trim().to_owned());
true
}
/// Known provenance''',
''' return false;
}
true
}
/// Known provenance''',
'craft-ai-egui', 'lib', 'a_passed_custom_test_at_a_new_address'),
('custom-test-confirms-an-old-address', DRAFT,
''' pub fn confirm_models_for(&mut self, address: &str) -> bool {
if address.trim() != self.endpoint.trim() {''',
''' pub fn confirm_models_for(&mut self, address: &str) -> bool {
if false && address.trim() != self.endpoint.trim() {''',
M, 'draft_provenance', 'confirming_models_moves_them_only'),
('refresh-hides-a-lost-choice', MANAGE,
'Ok(unavailable_choices(file, id))', 'Ok(Vec::new())',
CL, 'model_refresh', 'a_refresh_reports'),
('refresh-ignores-a-lost-ability', REFRESH,
'known_tool(store, &features, &b.role).is_some_and(|tool| {',
'known_tool(store, &features, &b.role).is_some_and(|tool| false && {',
CL, 'model_refresh', 'a_refresh_reports_a_choice_whose_model_stays_listed'),
('refused-bind-rewrites-the-file', 'crates/ai-client/src/client.rs',
'self.shared.try_update(|store| {', 'self.shared.update(|store| {',
CL, 'model_assignment', 'every_refused_store_change'),
('refused-choice-rewrites-the-file', 'crates/ai-client/src/tools.rs',
'let written = self.shared.try_update(|store| {', 'let written = self.shared.update(|store| {',
CL, 'model_assignment', 'every_refused_store_change'),
('abort-probe-uses-unconditional-update', MANAGE,
'self.shared.try_update(|file| {', 'self.shared.update(|file| {',
CL, 'saved_probe_abort', ''),
('nested-unknown-fields-dropped', 'crates/ai-native/src/shared/keep_unknown.rs',
'(Some(in_seen), Some(in_written)) => carry(in_file, in_seen, in_written),',
'(Some(_), Some(_)) => {}',
'craft-ai-native', 'shared_nested_fields', 'a_write_keeps_unknown_fields_at_every_depth'),
('list-unknown-fields-dropped', 'crates/ai-native/src/shared/keep_unknown.rs',
'carry_items(file, seen, written);', 'let _ = (file, seen, written);',
'craft-ai-native', 'shared_nested_fields', 'a_write_keeps_unknown_fields_at_every_depth'),
('removed-field-comes-back', 'crates/ai-native/src/shared/keep_unknown.rs',
'(None, Some(_)) | (Some(_), None) => {}',
'(None, Some(_)) => {} (Some(_), None) => { written.insert(name.clone(), in_file.clone()); }',
'craft-ai-native', 'shared_nested_fields', 'a_field_this_build_knows_can_still_be_cleared'),
('duplicate-identity-guessed', 'crates/ai-native/src/shared/keep_unknown.rs',
'.and_modify(|slot| *slot = None)', '.and_modify(|_| {})',
'craft-ai-native', 'shared_nested_fields', 'an_identity_the_file_holds_twice'),
('written-duplicate-identity-guessed', 'crates/ai-native/src/shared/keep_unknown.rs',
'if written_times.get(&key) != Some(&1) {', 'if written_times.is_empty() {',
'craft-ai-native', 'shared_nested_fields', 'an_identity_written_twice_is_not_guessed_at'),
('file-item-indexed-on-first-field-only', 'crates/ai-native/src/shared/keep_unknown.rs',
'for key in identities(item) {', 'for key in identities(item).take(1) {',
'craft-ai-native', 'shared_nested_fields', 'an_item_with_an_unknown_id_is_matched'),
('replace-forgets-the-old-file', 'crates/ai-native/src/shared.rs',
'self.write(store, before.as_deref())', 'self.write(store, None)',
'craft-ai-native', 'shared_nested_fields', 'replace_keeps_unknown_fields'),
('abort-callback-still-rewrites-existing-file', 'crates/ai-native/src/shared.rs',
'Err(error) => return Ok(Err(error)),',
'Err(error) => { self.write(&store, before.as_deref())?; return Ok(Err(error)); },',
'craft-ai-native', 'shared_try_update', 'callback_error_preserves_raw_nested_settings_and_discards_mutations'),
('abort-callback-still-creates-missing-file', 'crates/ai-native/src/shared.rs',
'Err(error) => return Ok(Err(error)),',
'Err(error) => { self.write(&store, before.as_deref())?; return Ok(Err(error)); },',
'craft-ai-native', 'shared_try_update', 'callback_error_does_not_create_a_missing_store'),
('loaded-model-address-forgotten', DRAFT,
'models_for: c.endpoint.as_deref().map(|e| e.trim().to_owned()),', 'models_for: None,',
M, 'draft_provenance', 'changing_model_contents'),
('loaded-address-not-trimmed', DRAFT,
'models_for: c.endpoint.as_deref().map(|e| e.trim().to_owned()),',
'models_for: c.endpoint.clone(),', M, 'draft_provenance', 'whitespace_in_a_saved_address'),
('saved-probe-ignores-changed-settings', MANAGE,
'if *saved != connection {', 'if false {',
CL, 'saved_probe_writeback', 'changed_endpoint_keeps_new_models_and_returns_a_stale_error'),
('saved-probe-compares-only-endpoint', MANAGE,
'if *saved != connection {', 'if saved.endpoint != connection.endpoint {',
CL, 'saved_probe_writeback', 'every_changed_connection_field_prevents_writeback'),
('picker-ignores-required-input', ROLE,
'.all(|i| role.inputs.contains(i))', '.all(|_| true)',
M, 'required_inputs', 'picker_does_not_offer'),
('binding-ignores-required-input', ROLE,
'spec.requires.iter().find(|i| !role.inputs.contains(i))', 'spec.requires.iter().find(|_| false)',
M, 'required_inputs', 'binding_refuses'),
('app-picker-ignores-required-input', ROLE,
'.all(|i| role.inputs.contains(i))', '.all(|_| true)',
CL, 'required_inputs', 'picker_omits'),
('app-status-and-plan-ignore-required-input', ROLE,
'spec.requires.iter().find(|i| !role.inputs.contains(i))', 'spec.requires.iter().find(|_| false)',
CL, 'required_inputs', 'status_and_plan'),
('start-accepts-another-apps-plan', RUN,
'if plan.disclosure.app != self.app {', 'if false {',
CL, 'plan_app_scope', 'another_apps_plan_is_refused_before_key_provider_and_history'),
('start-accepts-another-apps-local-plan', RUN,
'if plan.disclosure.app != self.app {',
'if plan.disclosure.locality == craft_ai_core::Locality::Remote && plan.disclosure.app != self.app {',
CL, 'plan_app_scope', 'another_apps_plan_is_refused_before_key_provider_and_history'),
]