Commit Graph
35 Commits
Author SHA1 Message Date
AFFAANhandClaude Opus 5 1ea73c1621 perf(crm): track a lead cursor instead of re-sending the whole window
Every lead from the last seven days was re-sent on every cycle. It was
correct — the CRM deduplicates on Meta's lead id — but at a five-minute
interval a campaign with fifty leads a week meant fourteen thousand CRM
requests a day to re-send leads the CRM already had.

Each campaign now records how far its leads have been sent, and the next
run reads from there minus an hour. The overlap matters: Meta filters on
creation time, so without it a lead created moments before a run started
falls between two runs and is never sent.

The cursor advances only after every lead in the batch has been accepted,
so a run that fails half-way is retried in full by the next one — the
self-healing the naive version had, without the traffic. Nothing is
written when there were no leads, since that would mean an event row per
campaign every five minutes.

Also batches insight upserts into one executemany. A single campaign sync
writes a month of days times six breakdowns, which was a few hundred
round trips per campaign per cycle.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-04 11:11:11 +05:30
AFFAANhandClaude Opus 5 b63b852cbe feat(crm): let an agent add CRM fields from chat, over MCP
Two tools on the maskan_crm server: one to list the fields that exist, one
to define a new one. Asking in MaskanX chat for a field now creates it in
the CRM, where it is usable on the next record and visible in the CRM's
own screens.

The list tool exists mainly so the create tool has something to check
against — without it an agent invents a near-duplicate of a field that is
already there under a slightly different name, and its description says
so.

Nulls are stripped from the definition before it is sent, so the CRM's
defaults apply rather than being overwritten with None. A rejected
definition comes back as the CRM's own message, which is what lets an
agent correct itself and retry rather than reporting a constraint
violation to the user.

_request now accepts a list response behind an explicit flag. Every
endpoint returns an object except the collection reads, and an unexpected
array is more likely a proxy's error page than data.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-04 11:07:35 +05:30
AFFAANhandClaude Opus 5 b4007ce825 feat(campaigns): push leads and campaign figures into Maskan CRM
Server to server over the CRM's integration API, deliberately not through
MCP. MCP is for an agent deciding to do something; leads have to reach the
CRM on a schedule whether or not anyone is talking to the agent, and a
lead that arrives only when someone asks for it arrives too late.

Leads are re-sent for a trailing window on every run rather than tracked
as new-since-last-time. Meta's lead id is the external id, so the CRM
ignores one it already has — which makes a half-failed run heal itself on
the next cycle with no bookkeeping.

Meta returns form answers as a list under names the form's author chose,
so mapping is best-effort against aliases: full_name or first/last, phone
or phone_number or mobile. Unrecognised answers are kept in metadata
rather than dropped, and a lead with a phone but no name still gets
through — the CRM requires a first name, and losing a real enquiry to
satisfy a validator would be the wrong trade.

Campaign figures come from stored insights, not Meta: this runs every few
minutes and re-reading Meta would spend the quota on numbers that change
hourly. They go through an upsert rather than the idempotent create used
for leads, because a campaign's figures change every time they are read.

One care point, learnt the hard way and now commented and tested: stored
metrics are already in minor units, so summing them with the raw-row
normaliser reports spend a hundredfold.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-04 10:55:34 +05:30
AFFAANhandClaude Opus 5 53e4a64b22 docs(campaigns): document insights storage and analytics
Covers the three things most likely to be broken by a well-meant change:
insights are re-fetched and upserted rather than appended, breakdown rows
duplicate the spend they break down and must be filtered out of
aggregates, and both dashboard windows end yesterday because today is
partial.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 18:17:25 +05:30
AFFAANhandClaude Opus 5 e0cec074ff fix(campaigns): persist the Meta linkage on insert
create_campaign never wrote meta_campaign_id, sync_status, sync_error or
approved_by. An adopted campaign arrives already linked to a Meta object,
and the link was dropped on the way into the database.

The consequences compounded. Every lookup keyed on meta_campaign_id
missed the row, so adopting a campaign twice created a second copy
instead of returning the first, and the reconciler — which imports any
Meta campaign it cannot find locally — imported the same campaign again
on every cycle. At the default 120s interval that is a new row every two
minutes, indefinitely.

The unit tests could not have caught this: their fake repository stores
the spec object itself, so no field can be lost between write and read.
It took a real database. The two regression tests added here run against
one.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 18:14:24 +05:30
AFFAANhandClaude Opus 5 6c39d42874 feat(campaigns): enforce budget guardrails automatically
Adds the insights reader, a pure guardrail evaluator, and the sweep that
applies them to live campaigns.

Three things carry the weight here:

Units. Meta reports spend in major units ("12.34") while budgets and
guardrails are in minor units (1234). normalise_row converts, rounding
half up rather than using round(), which rounds halves to even and can
record an exact half-unit of spend as nothing.

Leads. Meta has no leads field; leads live in the actions array, under
several action types depending on whether the lead came from a Facebook
form or a pixel. Cost per lead is computed from spend and leads over the
same window rather than read from cost_per_action_type, so the two can
never disagree.

Order. The campaign is paused on Meta before the local record changes. A
campaign recorded as paused but still delivering is the outcome this
exists to prevent. One campaign's failure never aborts the sweep, so a
rate limit on the third does not leave the fourth unguarded.

Cost rules are skipped until the first lead or click: no leads yet is not
an infinite cost per lead, and pausing for that would kill every campaign
in its first hour.

Deliberately a deterministic loop rather than a maskanx_cron_jobs entry.
That scheduler runs prompts through an agent, and asking a language model
whether a budget has been exceeded would make an arithmetic guarantee
probabilistic. Follows reconcile.py's lifespan-task pattern instead, and
warns every cycle if Meta is unconfigured — a safety system that cannot
run should be loud.

require_approval_for_budget_increase is enforced at the API layer, where
the budget is actually edited: raising it while a campaign awaits approval
returns 409, since it would change what the approver is reviewing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 18:04:25 +05:30
AFFAANh e8abe33d18 Merge Phase 2: Meta sync, launch control, adopt and reconciliation
Campaigns now reach Meta. Sync builds the campaign/ad set/creative/ad
chain with every object PAUSED; launch is the only action that starts
spend and is gated on an identified operator and a funding source.
Campaigns authored in Ads Manager can be discovered and adopted, and a
background loop keeps local status in step with Meta.

Review of the phase found three defects, fixed here: launch activated
only the campaign so nothing delivered, POST payloads went in the query
string so image uploads could not work, and an ad set with no targeting
reached Meta and failed opaquely.
2026-08-03 17:51:01 +05:30
AFFAANhandClaude Opus 5 584e300cd1 fix(meta): send POST payloads in the body, not the query string
Every write went out as a query string. Graph accepts that for small
payloads, but upload_ad_image base64-encodes an entire image file, and no
real image fits in a URL — image uploads could not have worked.

POSTs now use a form body and get a longer timeout, since an upload is the
one Meta call that carries real weight. Reads and deletes keep their
parameters in the query string, where Graph expects them.

Adds the first tests over the default transport: every other Meta test
injects a fake one, so nothing covered this path.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 17:50:43 +05:30
AFFAANhandClaude Opus 5 4022c8a1f6 fix(meta): refuse an ad set with no audience instead of letting Meta reject it
Graph requires targeting on every ad set. An empty targeting dict reached
Meta and failed opaquely part-way into building the object chain, leaving
a campaign and nothing else. This names the missing field before any
network call, matching how the module already handles a missing objective
and a missing daily budget.

The wizard defaults age and countries, so this only fires on campaigns
built through the API without targeting.

Also fixes the live smoke test, which used Graph's nested geo_locations
shape rather than the flat spec.targeting["countries"] the mapper reads.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 17:48:34 +05:30
AFFAANhandClaude Opus 5 9dba7b81d4 fix(campaigns): launch the whole object chain, not just the campaign
Launch set only the campaign to ACTIVE. Sync creates the ad set and ad
PAUSED, and Meta delivers only when the ad, its ad set and its campaign
are all active — so Launch reported "live" while nothing ran. This was
the central promise of Phase 2 and it did not work.

The chain is now activated children-first, campaign last. Nothing under a
paused campaign delivers, so a failure part-way leaves the campaign unable
to spend. That ordering is also why pause and stop only flip the campaign.

An imported campaign has no stored ad set or ad ids, so launching it still
touches only the campaign and its children keep the statuses set in Ads
Manager.

Deleting an imported campaign is now refused rather than silently
pointless: deleting it on Meta would destroy work MaskanX did not author,
and deleting only the local row achieved nothing because the reconciler
re-imported it on the next cycle. Deleting it in Ads Manager is what
sticks, after which the reconciler archives the record.

Also corrects the README's reconciler interval: it is 120s, not 300s.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 17:45:07 +05:30
AFFAANhandClaude Opus 5 2fc5f8a837 docs(campaigns): document Phase 2, and add the live smoke test
The README still described Phase 1's "creates nothing in Meta" contract,
which is no longer true. It now covers the lifecycle, that sync creates
only paused objects and why that is enforced in three places, what launch
requires, adopt/delete semantics for imported campaigns, and the
reconciler interval.

Also documents the separate campaign_test database. Without it the
repository integration tests skip and the suite still passes, so the skip
count matters as much as the exit code.

The live smoke test is the only thing that proves Meta accepts what the
client sends: every other test runs against a fake transport. It syncs a
real campaign, reads back from Graph that all three objects are PAUSED,
and deletes them in a finally block. Opt-in via MASKANX_LIVE_TESTS=1.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 17:39:05 +05:30
AFFAANhandClaude Opus 5 9ecb08bb80 feat(campaigns): delete on Meta when deleting a MaskanX campaign
Deleting a campaign only removed the local row. A live campaign deleted
in MaskanX kept spending on Meta with nothing left here recording that it
existed. Meta is now deleted first, and a failure there keeps the local
row and answers 502, since forgetting it here while it still spends there
is the worse of the two outcomes.

An imported campaign is never deleted on Meta. MaskanX did not author it,
and forgetting the import must not destroy work done in Ads Manager.

Deleting a campaign cascades to its ad sets and ads, so only the campaign
id is sent. The creative is left behind deliberately: it is an
account-level asset that other ads may reference.

Meta reports a delete of an absent object as code 100, which delete_object
swallows, so retrying a partially-failed cleanup is safe.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 17:33:32 +05:30
AFFAANhandClaude Opus 5 2522701ec4 feat(campaigns): reconcile local records with Meta on a background loop
Meta publishes no webhooks for campaign create or delete, so this polls every
120s by default (MASKANX_CAMPAIGN_RECONCILE_SECONDS; 0 disables).

Meta is authoritative for delivery state; MaskanX keeps its own metadata.
Guardrails, approvals and audit history are never touched by reconciliation,
and campaigns still in a local-only status are skipped entirely so a draft
that has never reached Meta cannot be overwritten.

A campaign that disappears from Meta is archived, not deleted: its spend
history has to stay reportable.

The loop swallows and logs a failed cycle rather than dying, and is cancelled
on shutdown alongside the watchdog. It only starts when META_ADS_ACCOUNT_ID
is set.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 16:50:16 +05:30
AFFAANhandClaude Opus 5 09aee8b621 feat(campaigns): discover and adopt campaigns created in Meta
Campaigns can be authored on either side. Anything MaskanX did not create is
origin="imported": MaskanX reports on it and can pause or stop it, but it was
built elsewhere.

Adoption is keyed on meta_campaign_id, which carries a unique index, so
adopting the same campaign twice returns the existing record instead of
violating the constraint. Discover excludes anything already adopted.

The literal /discover and /adopt routes are declared before /{campaign_id} so
they cannot be captured as campaign ids; a test pins that ordering.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 16:34:32 +05:30
AFFAANhandClaude Opus 5 8153edfce9 feat(campaigns): add launch, pause and stop
Launch is the only action that starts real spend, so it refuses unless every
precondition holds: the campaign is synced, its status allows launching, an
identified operator approved it, and the ad account has a payment method.
Each refusal names what to do rather than letting Meta fail opaquely later.

An approval recorded as "unauthenticated" does not authorise spend. With
MASKANX_OPERATOR_TOKENS unset every approval is unattributable, so launch is
blocked until operator auth is configured.

Stop and pause set the Meta object PAUSED before recording the local change,
so a Meta failure cannot leave a campaign that is stopped in MaskanX but
still delivering.

Replaces the Phase 1 test asserting /launch did not exist with one asserting
it is unreachable from draft; the guarded property is unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 11:37:35 +05:30
AFFAANhandClaude Opus 5 48a3110beb feat(campaigns): sync approved campaigns to Meta as paused objects
Creates the campaign, ad set, creative and ad chain, every object PAUSED.
Nothing here starts delivery; Launch is a separate explicit action.

Each Meta id is persisted as soon as it is obtained, so a failure part-way
through is resumable: a retry reuses the stored ids and creates only what is
still missing. Without that, retrying a partial failure would create a second
set of ad objects in a real advertising account.

Page id and ad account are resolved before any network call, so missing
configuration cannot leave a half-built chain in the account. A Meta failure
records sync_status=failed with Meta's code and subcode, leaves the status at
approved, and keeps the ids already obtained.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 10:45:17 +05:30
AFFAANh 3a99f3e001 fix(meta): close review findings on write client and budget validation
Task 4 review: Approved with no Critical findings, but three Important
and two hardening items. All five addressed:

- objects.build_campaign_payload: normalise a bare string
  special_ad_categories value to a single-element list instead of
  exploding it into characters via list() - the field controls
  regulated-advertising compliance and advanced is unvalidated input.
- objects.build_ad_set_payload: validate optimization_goal and
  billing_event against new allowlists before forwarding them, since
  billing_event determines how the ad account is charged and both come
  from client-controlled advanced.
- validation.validate_budget: reject a lifetime-only budget (Meta's
  create_ad_set only accepts daily_budget) so it fails at validation
  time instead of passing approval and only failing at sync.
- client.py: create_campaign/create_ad_set/create_ad now send the
  CREATE_STATUS constant rather than the caller's status object, so a
  str subclass with a lying __ne__ can no longer slip "ACTIVE" past the
  guard.
- client._post: refuse any POST to a campaign/adset/ad creation path
  with a non-PAUSED status before touching the transport, as defence in
  depth if a future caller bypasses the typed create_* guards.

10 new tests (8 in test_meta_writes.py, 2 in test_campaign_validation.py).
2026-08-03 01:17:18 +05:30
AFFAANh a5e58f2eb3 feat(meta): add write helpers that always create paused objects
Adds the write half of the Meta Graph API client: create_campaign,
create_ad_set, upload_ad_image, create_ad_creative, create_ad,
update_object_status, and list_campaigns/list_ad_sets/list_ads. Every
create_* helper hard-codes status="PAUSED" and raises ValueError with
zero network calls if asked for anything else; update_object_status is
the only method allowed to send ACTIVE. Dict params Graph expects as
JSON strings (targeting, object_story_spec, special_ad_categories) are
json.dumps-encoded before being sent.

Also adds src/adclaw/meta/objects.py with pure functions mapping a
CampaignSpec's budget/targeting/advanced fields onto Graph parameter
names, keeping that mapping unit-testable without a transport.

26 new tests in tests/test_meta_writes.py, all against a FakeTransport
(no live network calls, no real Meta objects created).
2026-08-03 01:03:39 +05:30
AFFAANhandClaude Opus 5 04ad516fe2 test(campaigns): exercise repository SQL against PostgreSQL
Phase 1 covered only the pure row mapper, so every SQL path in
CampaignRepository was untested — parameter ordering, RETURNING clauses and
transaction behaviour had never been executed. Phase 2 creates real Meta
objects on top of this layer, so the gap is closed first.

Seven tests cover the CRUD round trip, LookupError on unknown ids, event
recording, and that update_campaign_with_event writes both rows or neither.
They skip rather than fail when PostgreSQL is unreachable, and delete every
row they create in a finally block.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 00:51:49 +05:30
AFFAANh 50345a1c89 feat(campaigns): require an operator identity to approve
approved_by was client-supplied, making approval forgeable ahead of
Phase 2 pushing real spend to Meta. Add a proportionate operator-token
check (no login system) via a new require_operator FastAPI dependency:
MASKANX_OPERATOR_TOKENS maps name:token pairs, and approve_campaign now
derives its actor solely from the resolved operator, never from the
client-supplied ActorPayload.actor. Unset env resolves to
"unauthenticated" so local dev and existing tests are not blocked;
set-but-unrecognised tokens 401. submit and reject remain
unauthenticated since neither authorises spend.

Updates test_submit_then_approve_moves_through_states to assert
approved_by == "unauthenticated" (env var unset in tests) instead of
the previously-trusted client actor, since a forged actor must now be
ignored.
2026-08-03 00:17:49 +05:30
AFFAANh 2e46f8485d feat(campaigns): write status change and audit event atomically
_transition previously called update_campaign then add_event as two
separate commits, so a failed add_event left a status change with no
audit row. Phase 2 launches campaigns that authorise spend, so a
compliance hole like that has to close first.

Add CampaignRepository.update_campaign_with_event, which runs the
UPDATE and the event INSERT on one connection inside one transaction
and commits once. Route campaign transitions (submit/approve/reject)
through it instead of the update_campaign + add_event pair; update_campaign
and add_event themselves are unchanged for other callers.

Also update test_campaign_api.py's FakeRepo/monkeypatch to implement the
new repository method, since production code now calls it in the
submit/approve/reject path.
2026-08-03 00:10:40 +05:30
AFFAANhandClaude Opus 5 62c8281ed0 Merge Phase 1 campaign management
Campaign records, approval workflow, budget guardrails against the ad
account minimum, and Meta ad preview. Creates nothing in Meta.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-02 23:35:04 +05:30
AFFAANhandClaude Opus 5 4d205c1086 docs(campaigns): document phase 1 campaign management
Records what Phase 1 does and deliberately does not do: no Meta writes, no
launch or sync endpoint, previews rendered without creating objects, budget
minimums enforced locally, and the fact that a payment method can only be
attached in Meta Business Manager.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-02 19:06:14 +05:30
AFFAANhandClaude Opus 5 3859f937a2 fix(campaigns): close PUT budget-minimum bypass and data-destruction bug
PUT /campaigns/{id} validated the raw request payload instead of the
merged campaign state, so omitting ad_account_id skipped the minimum
check and a full model_dump() overwrote every unset field (targeting,
guardrails, objective, etc.) with its default, silently destroying
data. Validate against the effective post-merge budget/guardrails/
account and only apply fields the client actually sent
(exclude_unset=True).

Also: persist company_id on UPDATE (was accepted but dropped), log
a warning when Meta account lookups fail so the budget-minimum
fail-open is observable, reword the non-editable-status 409 message
to not prescribe an impossible reject-to-draft action, and add
coverage for Meta error surfacing, LookupError-to-404 on PUT/submit,
reject's pending_approval->draft transition, and GET-unknown-id 404.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-01 14:00:10 +05:30
AFFAANh d0c8ed43aa feat(campaigns): add campaign API with approval workflow and preview 2026-08-01 13:43:57 +05:30
AFFAANh 4098af5a55 fix(campaigns): use RETURNING in create/update_campaign to avoid extra round-trip and TOCTOU race
create_campaign/update_campaign previously committed then opened a
second connection via get_campaign() just to re-read the row they had
just written, adding an extra round-trip and a race window where a
concurrent delete between commit and re-fetch was misreported as
"disappeared immediately after insert/update". Both now RETURNING the
row from the same statement/cursor that wrote it, using a shared
_RETURNING_CLAUSE derived from _SELECT_COLUMNS so the two column lists
cannot drift apart. update_campaign now raises LookupError for an
unknown id (0 rows affected) instead of misdiagnosing it as a race.

Also: delete_campaign uses the (cur.rowcount or 0) > 0 idiom already
established in postgres_repo.py, the module docstring documents that
this repository's SQL paths rely on integration tests rather than
unit tests, and a new DB-free test asserts the RETURNING and SELECT
column lists stay identical.
2026-08-01 13:36:36 +05:30
AFFAANh 1df49aee37 feat(campaigns): add PostgreSQL campaign repository
Adds CampaignRepository with list/get/create/update/delete for
campaigns plus add_event/list_events for the audit trail, following
the existing postgres_repo.py connection pattern. Replaces the
brief's two bare `assert created/updated is not None` checks with
explicit RuntimeError raises so the guard survives python -O.
2026-08-01 13:27:08 +05:30
AFFAANh 985527a451 fix(meta): cover access-token env handling, JSON-encoded creative, and previews fail-fast contract
Address code review findings on the read-only Meta Graph client: add
monkeypatch-based tests for access_token_from_env/MetaNotConfiguredError,
assert generate_previews JSON-encodes the creative param, pin the
existing fail-fast-on-partial-error contract with a dedicated test, and
declare httpx as an explicit dependency since it was only resolving
transitively.
2026-08-01 13:21:57 +05:30
AFFAANh fd421d2f53 feat(meta): add read-only Graph client for accounts and previews 2026-08-01 13:10:36 +05:30
AFFAANhandClaude Opus 5 4a306c2e18 fix(campaigns): use explicit None check for auto-pause daily budget comparison
validate_guardrails used a truthy check on daily_budget, which silently
skipped the auto-pause-above-daily-budget check when daily_budget was 0 -
exactly the case where a positive auto-pause threshold can never trigger.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-01 13:06:38 +05:30
AFFAANhandClaude Opus 5 6bb8b1338a feat(campaigns): validate budgets against account minimum
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-01 12:59:55 +05:30
AFFAANhandClaude Opus 5 74eeb60191 fix(campaigns): use (str, Enum) for 3.10 compat, add state-machine test coverage
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-01 12:56:09 +05:30
AFFAANhandClaude Opus 5 d22e414869 feat(campaigns): add domain models and status state machine
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-01 12:30:10 +05:30
AFFAANh 38b6118fc3 feat(campaigns): add campaign management schema migration 2026-08-01 12:23:23 +05:30
AFFAANhandClaude Opus 5 19e1e84fb7 Initial commit: MaskanX backend
Independent FastAPI backend for the MaskanX agentic growth platform.

Includes the agent runtime, MCP client integrations (Meta Ads, LinkedIn,
HubSpot, Tavily, Exa, xAI, Citedy, image generation), PostgreSQL storage
for chats and cron jobs, provider and secret management, and the CLI.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-01 10:28:22 +05:30