Skip to content

Add Foundation Models 27 dynamic profile composition APIs - #314

Open
qoli wants to merge 29 commits into
huggingface:mainfrom
qoli:codex/upstream-dynamic-profiles
Open

qoli wants to merge 29 commits into
huggingface:mainfrom
qoli:codex/upstream-dynamic-profiles

Conversation

@qoli

@qoli qoli commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Scope after review

Adds profile composition only: DynamicProfile, Profile, erasure, conditional builders, modifiers, and request-time resolution of those values. No profile session initializer, lifecycle execution, native projection, or provider Tool-loop changes.

Dependencies: #313 and upstream #352 (which replaced/closed #311). Shared compatibility runtime is separate in #315; provider integrations remain individual PRs.

API compatibility

  • Composition follows the installed Foundation Models 27 SDK, including callback actor-context/isolation annotations.
  • historyTransform accepts an ordinary escaping closure, not @sendable, matching Apple. A non-Sendable function/capture now compiles with both modules.
  • Conditional branch storage/initialization remains internal.
  • AnyLanguageModel-only includeSchemaInPrompt is Compatibility SPI.
  • This PR does not decide the public session-construction contract; Add shared profile execution behind Compatibility SPI #315 currently retains its SPI initializer pending the outstanding construction decision.

Fresh validation (2026-10-21)

The main-based diff includes unmerged #313; review the profile-specific files for this layer. Published history is preserved; no force-push or new checkout.

The updated #313 dependency preserves concrete Tool identity in public RequestContext and binds session properties only for execution. historyTransform remains an ordinary, non-@sendable closure; no construction API or provider runtime moved into this PR.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Request-scoped profile state can leak across overlapping requests, and history handling can violate its instruction-free contract.

5 open findings
What changed in this PR

Adds Foundation Models 27-compatible dynamic profiles, session properties, request options, and per-request lifecycle handling.

Changes:

  • Adds dynamic profile composition, modifiers, callbacks, and request projections.
  • Adds observable session properties and instruction-free history access.
  • Adds Foundation Models 27 request-option bridging and compatibility tests.
File Description
Sources/​AnyLanguageModel/​ContextOptions.swift Defines request-context options.
Sources/​AnyLanguageModel/​DynamicInstructions.swift Exposes instruction resolution internally.
Sources/​AnyLanguageModel/​DynamicProfile.swift Implements profiles, modifiers, and lifecycle handling.
Sources/​AnyLanguageModel/​GenerationOptions.swift Adds sampling aliases and tool-calling policy.
Sources/​AnyLanguageModel/​LanguageModelSession.swift Integrates profile resolution and callbacks.
Sources/​AnyLanguageModel/​Models/​SystemLanguageModel.swift Bridges tool-calling mode to Foundation Models.
Sources/​AnyLanguageModel/​SessionProperties.swift Implements session-scoped property storage.
Sources/​AnyLanguageModelMacros/​Plugin.swift Registers the session-property macro.
Sources/​AnyLanguageModelMacros/​SessionPropertyEntryMacro.swift Generates session-property accessors and keys.
Tests/​AnyLanguageModelTests/​APICompatibilityAnyLanguageModelTests.swift Checks AnyLanguageModel syntax compatibility.
Tests/​AnyLanguageModelTests/​APICompatibilityFoundationModelsTests.swift Checks native Foundation Models compatibility.
Tests/​AnyLanguageModelTests/​DynamicProfileTests.swift Exercises profiles and lifecycle behavior.
Tests/​AnyLanguageModelTests/​FoundationModelsGenerationOptionsTests.swift Tests native option bridging.
Tests/​AnyLanguageModelTests/​RequestOptionsTests.swift Tests new request-option values.
Tests/​AnyLanguageModelTests/​SessionPropertyTests.swift Tests property storage and history behavior.
README.md Documents Foundation Models 27 request options.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread Sources/AnyLanguageModel/LanguageModelSession.swift Outdated
Comment thread Sources/AnyLanguageModel/LanguageModelSession.swift Outdated
Comment thread Sources/AnyLanguageModel/DynamicProfile.swift Outdated
Comment thread Sources/AnyLanguageModel/LanguageModelSession.swift Outdated
Comment thread Sources/AnyLanguageModel/SessionProperties.swift

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread Sources/AnyLanguageModel/LanguageModelSession.swift Outdated
Comment thread Sources/AnyLanguageModel/DynamicProfile.swift
Comment thread Sources/AnyLanguageModel/SessionProperties.swift
qoli added 3 commits October 15, 2026 09:03
…dynamic-profiles

# Conflicts:
#	Sources/AnyLanguageModel/LanguageModelSession.swift
#	Sources/AnyLanguageModel/SessionProperties.swift
#	Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift
#	Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift
#	Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift
#	Tests/AnyLanguageModelTests/SessionPropertyTests.swift

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved history, session-isolation, callback-dispatch, and API compatibility issues affect correctness.

5 open findings
3 resolved since last review
Previously missed (2)

In code that hasn't changed since last review

Medium severity Preserve and deduplicate each request's prompt dispatch

Sources/​AnyLanguageModel/​DynamicProfile.swift:531

Two streams created back-to-back can append both prompts before their asynchronous profile resolutions run. Both snapshots then end with the second prompt, so this lookup dispatches onPrompt for that prompt and skips the first request's prompt. Pass the actual request prompt into lifecycle preparation instead of inferring it from shared history, and deduplicate prompt dispatch per request across continuations.

Medium severity Build native history from the resolved transcript

Sources/​AnyLanguageModel/​LanguageModelSession.swift:121

Profile sessions now enter SystemLanguageModel's dynamic-instructions branch, but makeFoundationModelsSession builds its history from session.transcript (SystemLanguageModel.swift:404–412), not the resolved request projection. Thus a profile's historyTransform is ignored even on the initial system-model request, resending entries the transform removed. Build native history from the resolved RequestContext.transcript, filtering its instructions and duplicate current prompt without changing durable history.

🧠 Review effort: Balanced

Comment thread Sources/AnyLanguageModel/DynamicProfile.swift
Comment thread Sources/AnyLanguageModel/GenerationOptions.swift Outdated
Comment thread Sources/AnyLanguageModel/LanguageModelSession.swift Outdated
Comment thread Sources/AnyLanguageModel/SessionProperties.swift Outdated
Comment thread Sources/AnyLanguageModel/LanguageModelSession.swift Outdated
@qoli

qoli commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

The additional prompt-dispatch and native-history findings are also addressed in 9b76d74. Lifecycle preparation receives the actual request prompt and shares a per-request dispatch state across continuations, including concurrent streams. Native Foundation Models session construction now uses the resolved request projection. Regression coverage includes history mutation removing the current prompt, concurrent prompt callbacks, and native projected history without changing durable history. The 34 focused profile/property/options tests pass.

Copilot AI left a comment

Copy link
Copy Markdown

Comment thread Sources/AnyLanguageModel/DynamicProfile.swift Outdated
Comment thread Sources/AnyLanguageModel/DynamicProfile.swift Outdated
Comment thread Sources/AnyLanguageModel/LanguageModelSession.swift Outdated
Comment thread Sources/AnyLanguageModel/LanguageModelSession.swift Outdated
Comment thread Sources/AnyLanguageModel/Models/SystemLanguageModel.swift Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants