Skip to content

feat(task-service): support sharded policies and policy model tokens - #561

Merged
Pigbibi merged 1 commit into
mainfrom
feat/policy-models-and-sharded-policies
Nov 29, 2026
Merged

Pigbibi merged 1 commit into
mainfrom
feat/policy-models-and-sharded-policies

Conversation

@Pigbibi

@Pigbibi Pigbibi commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

AI assistance

Antigravity AI assistant was used to implement, test, and verify this change.

What changed

  • Added cloudflare/task-service/src/policies.mjs (loadProjectPolicies) supporting optional PROJECT_POLICY_DEFAULTS, sharded PROJECT_POLICIES_<N> JSON bindings (decoupling Cloudflare's 5.1 kB per-binding limit), fail-closed duplicate-key checks, and models[provider]: "policy" expansion from bundled config/api-tier-models.json.
  • Wired loadProjectPolicies into state.mjs, projects.mjs, and completion-webhooks.mjs.
  • Added "policy" and "policy:<simple|standard|complex>" model token support in direct mode: "api" submissions (task-admission.mjs + api.mjs), resolving the newest live provider model via selectAPIModel and storing the resolved concrete model and complexity.

How verified

  • node --test test/routing.mjs: 32/32 passed (including unit tests for loadProjectPolicies defaults/sharding/policy expansion and resolveTaskSubmission live policy:standard resolution).
  • PYTHONPATH=src /usr/local/bin/python3.13 -m unittest discover -s tests/task_service -q: 322/322 passed.
  • Remote CI (QuantAIService V2 CI, CodeQL, Engineering governance, Documentation check, GitGuardian) and Codex review (chatgpt-codex-connector) passed on head commit f94944f14a.

Why

Cloudflare Workers text/JSON env bindings have a 5.1 kB limit that made adding new projects to PROJECT_POLICIES fragile, and direct API callers (such as Drift Check) had to hardcode model IDs that break when providers retire models.

Risk

R2 — worker admission and policy loading only. Fully backward-compatible with existing unsharded PROJECT_POLICIES and pinned request.model callers. Rollback is a single-commit revert.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T11:44:46.780074Z f94944f Manual request
🔒 Security Review ✅ Completed 2026-11-29T11:46:36.701948Z f94944f Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Pigbibi

Pigbibi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@Pigbibi Pigbibi added the agent-approved An agent cleared this high-risk change on the current head; not an owner confirmation label Nov 29, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: f94944f14a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Pigbibi
Pigbibi merged commit a39d32f into main Nov 29, 2026
17 of 19 checks passed
@Pigbibi
Pigbibi deleted the feat/policy-models-and-sharded-policies branch November 29, 2026 11:46
@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: f94944f14a

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@qsl-gemini-reviewer

Copy link
Copy Markdown

AI Code Review (gemini / gemini-4-neon)

The sharded policy loader and direct API policy-model resolution are clean overall, with two medium-severity issues to fix before merge: (1) stripping policy:* tokens inside apiModelCandidates allows mode: 'auto' (usesRouting) requests with model: 'policy:' to silently ignore the requested tier instead of rejecting or honoring it, and (2) mergeProjectPolicy uses truthiness checks for providers/models so falsy non-object values like null/false bypass validation and fail open to defaults.

Reviewed commit: f94944f14a · Coverage: complete · Unresolved findings: true

Findings

  • [MEDIUM] cloudflare/task-service/src/api.mjs: apiModelCandidates sets pinnedModel to null whenever parsePolicyModelRequest(request.model) !== null, but resolveTaskSubmission in cloudflare/task-service/src/task-admission.mjs only parses policy tokens in the !usesRouting branch. When usesRouting is true (e.g. mode: 'auto'), a request with model: 'policy:complex' or 'policy:simple' bypasses parsePolicyModelRequest in task-admission.mjs yet has its model token stripped by apiModelCandidates, silently resolving against decision.tier instead of honoring the requested tier or rejecting policy:* under routing. Reject or resolve policy:* tokens before branching on usesRouting (or omit request.model in the synthetic request passed from task-admission.mjs instead of stripping it globally in apiModelCandidates).
  • [MEDIUM] cloudflare/task-service/src/policies.mjs: mergeProjectPolicy validates defaults.providers/entry.providers and defaults.models/entry.models with truthiness checks (e.g. (entry.providers && !object(entry.providers))). If a policy entry or defaults sets providers or models to a falsy non-object value (null, false, '', 0), validation is skipped: when defaults exist, the invalid entry value silently falls back to inheriting defaults (failing open); when defaults do not exist, the non-object value is preserved on the merged policy. Check property presence / !== undefined before verifying object(...).
  • [LOW] cloudflare/task-service/src/api.mjs: policyModelsForProvider treats a provider without tier_models (!Object.hasOwn(item, 'tier_models')) as 'policy' and expands bundled tierPolicy models, whereas tierCandidates returns [item.model] and apiModelCandidates returns [] when decision.model is null. For providers without tier_models, models: {[provider]: 'policy'} therefore admits bundled policy models while model: 'policy:standard' fails in apiModelCandidates. Also guard env.API_PROVIDERS[provider] with Object.hasOwn(env.API_PROVIDERS, provider).
  • [LOW] cloudflare/task-service/test/routing.mjs: Add unit tests covering the newly wired sharded policy lookups in CompletionWebhooks and workerContext (projects.mjs), falsy non-object providers/models rejection in loadProjectPolicies, and negative/routing cases in resolveTaskSubmission (e.g. policy:standard with mode: 'agent', invalid policy:unknown, and policy:complex with usesRouting: true).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-approved An agent cleared this high-risk change on the current head; not an owner confirmation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant