feat(seo): atomic directive fencing and transactional blog publication
Deploy Canina / deploy (push) Successful in 4m45s

This commit is contained in:
Canina Automation committed 2026-10-28 08:02:30 +00:00
1 parent 0a156fd7b4
commit fb1deadbbc
10 files changed
+353 -187

No files matched your search

@@ -46,6 +46,12 @@ export class ActionFindingDto {
@IsOptional() @IsString() @MaxLength(128) actionId?: string;
@IsOptional() @IsEnum(ResearchFindingStatus) status?: ResearchFindingStatus;
}
export class PublishFindingDto {
@IsInt() @Min(0) expectedVersion: number;
@IsInt() @Min(0) feedbackVersion: number;
@IsString() @MinLength(1) @MaxLength(128) actionId: string;
@IsObject() blog: Record<string, any>;
}
export class QueryFindingsDto {
@IsOptional() @Type(() => Number) @IsInt() @Min(1) @Max(100000) page?: number;
@IsOptional() @Type(() => Number) @IsInt() @Min(1) @Max(100) limit?: number;
@@ -0,0 +1,15 @@
import { Prisma } from '@prisma/client';
import { BlogsService } from '../admin/blogs.service';
import { PrismaService } from '../prisma/prisma.service';
import { RevalidationService } from '../common/revalidation/revalidation.service';
/** Reuse the admin create rules with the transaction client. Never run network
* revalidation (or the scheduler lifecycle) before the outer commit. */
export async function createPublicationBlog(
tx: Prisma.TransactionClient, data: Record<string, any>, userId: string,
) {
const deferred = { revalidateBlog: async () => false } as unknown as RevalidationService;
const service = new BlogsService(tx as PrismaService, deferred);
const { cacheRevalidation: _deferred, ...blog } = await service.createBlog(data, userId);
return blog;
}
@@ -0,0 +1,106 @@
import { Test } from '@nestjs/testing';
import { PassportModule, PassportStrategy } from '@nestjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { JwtService } from '@nestjs/jwt';
import { spawn } from 'child_process';
import { resolve } from 'path';
import { SeoTrackingModule } from './seo-tracking.module';
import { PrismaService } from '../prisma/prisma.service';
import { RevalidationService } from '../common/revalidation/revalidation.service';
import { PrismaClient } from '@prisma/client';
import { SeoTrackingService } from './seo-tracking.service';
class PublicationStrategy extends PassportStrategy(Strategy) {
constructor() { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: 'publication-disposable-only' }); }
validate(payload: any) { return payload; }
}
const url = process.env.PUBLICATION_TEST_DATABASE_URL;
if (url && !url.startsWith('postgresql://publication_test:disposable-only@127.0.0.1:55447/'))
throw new Error('Disposable publication database only');
const deferred = () => { let resolve!: () => void; const promise = new Promise<void>(r => resolve = r); return { promise, resolve }; };
(url ? describe : describe.skip)('atomic publication: real PostgreSQL row ordering', () => {
const db = new PrismaClient({ datasources: { db: { url } } });
let user: any;
const cache = { revalidateBlog: jest.fn(async () => false) };
const service = new SeoTrackingService(db as any, cache as any);
const ids: string[] = [];
it('dormant Python entrypoint uses authenticated real HTTP publication endpoint', async () => {
const module = await Test.createTestingModule({ imports: [PassportModule, SeoTrackingModule], providers: [PublicationStrategy] })
.overrideProvider(PrismaService).useValue(db).compile();
const app = module.createNestApplication({ logger: false }); app.setGlobalPrefix('api');
try {
await app.listen(0, '127.0.0.1');
const jwt = new JwtService({ secret: 'publication-disposable-only' });
const token = jwt.sign({ id: user.id, role: 'admin' }, { expiresIn: '5m' });
const origin = `http://127.0.0.1:${app.getHttpServer().address().port}`;
const child = spawn('python3', [resolve(__dirname, '../../../scripts/content-research/verify_real_http.py')], { env: { ...process.env, PUBLICATION_LOCAL_ORIGIN: origin, PUBLICATION_LOCAL_TOKEN: token } });
let output = ''; child.stdout.on('data', d => output += d); child.stderr.on('data', d => output += d);
const code = await new Promise(resolve => child.on('close', resolve));
expect({ code, output }).toEqual({ code: 0, output: 'PUBLICATION_REAL_HTTP_PASSED\n' });
const fixtures = await db.contentResearchFinding.findMany({ where: { title: 'publication-http-fixture' } });
ids.push(...fixtures.map(f => f.id));
} finally { await app.close(); }
}, 30000);
beforeAll(async () => { user = await db.user.create({ data: { firstName: 'Test', lastName: 'Fixture', mobile: String(Date.now()), role: 'admin' } }); });
afterAll(async () => {
await db.contentResearchFinding.deleteMany({ where: { id: { in: ids } } });
await db.blog.deleteMany({ where: { slug: { startsWith: 'publication-fixture-' } } });
await db.user.delete({ where: { id: user.id } }); await db.$disconnect();
});
async function finding() {
const f = await service.createFinding({ title: 'fixture', hypothesis: 'fixture', targetKeywords: ['test'], primaryKeyword: 'test', expectedOutcome: 'fixture' });
ids.push(f.id); return f;
}
const payload = () => ({ title: 'fixture', slug: 'publication-fixture-' + Math.random(), content: '<p>reviewed fixture</p>', tagNames: ['publication-fixture-tag'] });
function barrierClient(entered: ReturnType<typeof deferred>, release: ReturnType<typeof deferred>, pids: number[]) {
return new Proxy(db, { get(target, prop) {
if (prop === '$transaction') return (fn: any) => db.$transaction(async tx => {
pids.push((await tx.$queryRaw<any[]>`SELECT pg_backend_pid() AS pid`)[0].pid);
return fn(new Proxy(tx, { get(t, key) {
if (key === 'blog') return new Proxy(t.blog, { get(model, method) {
if (method === 'create') return async (args: any) => { entered.resolve(); await release.promise; return model.create(args); };
return (model as any)[method];
} });
return (t as any)[key];
} }));
}, { timeout: 10000 });
return (target as any)[prop];
} });
}
it('feedback committed during draft/preflight rejects publication and creates no Blog', async () => {
const f = await finding(), blog = payload();
await service.updateCeoFeedback(f.id, { expectedVersion: f.version, feedback: 'changed during preflight' });
await expect(service.publishFinding(f.id, { expectedVersion: f.version, feedbackVersion: f.feedbackVersion, actionId: 'stale', blog }, user.id)).rejects.toMatchObject({ status: 409 });
expect(await db.blog.count({ where: { slug: blog.slug } })).toBe(0);
});
it('publication lock blocks concurrent feedback; stale writer conflicts after commit; fresh feedback applies next', async () => {
const f = await finding(), entered = deferred(), release = deferred(), pids: number[] = [];
const publishing = new SeoTrackingService(barrierClient(entered, release, pids) as any, cache as any);
const publish = publishing.publishFinding(f.id, { expectedVersion: 0, feedbackVersion: 0, actionId: 'winner', blog: payload() }, user.id);
await entered.promise;
let settled = false;
// Separate Prisma connection/transaction; actual row lock wait proved with pg_stat_activity.
const feedback = service.updateCeoFeedback(f.id, { expectedVersion: 0, feedback: 'concurrent' }).then(v => { settled = true; return v; }, e => { settled = true; return e; });
let waiting = false;
for (let i = 0; i < 100; i++) {
const rows = await db.$queryRaw<any[]>`SELECT pid FROM pg_stat_activity WHERE wait_event_type = 'Lock' AND pid <> ${pids[0]}`;
if (rows.length) { waiting = true; expect(rows[0].pid).not.toBe(pids[0]); break; }
await new Promise(r => setTimeout(r, 10));
}
expect(waiting).toBe(true); expect(settled).toBe(false); release.resolve();
const result = await publish; expect((await feedback).status).toBe(409);
expect(result.finding.blogId).toBe(result.blog.id); expect(result.consumedFeedbackVersion).toBe(0);
const next = await service.updateCeoFeedback(f.id, { expectedVersion: result.finding.version, feedback: 'next action' });
expect(next.feedbackVersion).toBe(1); expect(next.blogId).toBe(result.blog.id);
});
it('invalid author/relation rolls back both claim and Blog; duplicate request cannot create twice', async () => {
const f = await finding(), blog = payload();
await expect(service.publishFinding(f.id, { expectedVersion: 0, feedbackVersion: 0, actionId: 'rollback', blog }, '00000000-0000-4000-8000-000000000000')).rejects.toBeTruthy();
expect((await service.findOne(f.id)).version).toBe(0);
expect(await db.blog.count({ where: { slug: blog.slug } })).toBe(0);
const r = await service.publishFinding(f.id, { expectedVersion: 0, feedbackVersion: 0, actionId: 'once', blog }, user.id);
await expect(service.publishFinding(f.id, { expectedVersion: 0, feedbackVersion: 0, actionId: 'once', blog }, user.id)).rejects.toMatchObject({ status: 409 });
expect(await db.blog.count({ where: { slug: blog.slug } })).toBe(1);
expect(r.cacheRevalidation).toBe('failed');
});
});
@@ -1,5 +1,6 @@
import {
Controller,
Request,
Get,
Post,
Patch,
@@ -14,6 +15,7 @@ import {
import { SeoTrackingService } from './seo-tracking.service';
import {
ActionFindingDto,
PublishFindingDto,
CreateFindingDto,
QueryFindingsDto,
UpdateCeoFeedbackDto,
@@ -67,6 +69,15 @@ export class SeoTrackingController {
return this.seoTrackingService.actionFinding(id, dto);
}
@Post(':id/publication')
async publishFinding(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() dto: PublishFindingDto,
@Request() req: { user: { id: string } },
) {
return this.seoTrackingService.publishFinding(id, dto, req.user.id);
}
@Post(':id/snapshots')
async addSnapshot(
@Param('id', new ParseUUIDPipe()) id: string,
@@ -1,9 +1,12 @@
import {
Injectable,
Optional,
NotFoundException,
BadRequestException,
ConflictException,
} from '@nestjs/common';
import { createPublicationBlog } from './publication-blog.adapter';
import { RevalidationService } from '../common/revalidation/revalidation.service';
import { PrismaService } from '../prisma/prisma.service';
import {
CreateFindingDto,
@@ -11,6 +14,7 @@ import {
UpdateCeoFeedbackDto,
CreateSnapshotDto,
ActionFindingDto,
PublishFindingDto,
} from './dto/seo-tracking.dto';
import {
createPaginationMeta,
@@ -37,7 +41,10 @@ function checked<T extends object>(type: new () => T, value: T): T {
}
@Injectable()
export class SeoTrackingService {
constructor(private prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
@Optional() private revalidation?: RevalidationService,
) {}
async createFinding(input: CreateFindingDto) {
const dto = checked(CreateFindingDto, input);
if (
@@ -143,6 +150,38 @@ export class SeoTrackingService {
return tx.contentResearchFinding.findUniqueOrThrow({ where: { id } });
});
}
async publishFinding(id: string, input: PublishFindingDto, userId: string) {
const dto = checked(PublishFindingDto, input);
if (!userId || !dto.actionId.trim())
throw new BadRequestException('Authenticated author and action required');
// updateMany is a row-locking CAS. Feedback writes use the same row and
// version: whichever writer wins forces the other stale request to 409.
const result = await this.prisma.$transaction(async (tx) => {
const claim = await tx.contentResearchFinding.updateMany({
where: { id, version: dto.expectedVersion,
feedbackVersion: dto.feedbackVersion, blogId: null },
data: { actionId: dto.actionId, version: { increment: 1 } },
});
if (!claim.count) {
if (!(await tx.contentResearchFinding.findUnique({ where: { id } })))
throw new NotFoundException('Research finding not found');
throw new ConflictException('Finding/directive changed or already published');
}
const blog = await createPublicationBlog(tx, dto.blog, userId);
const finding = await tx.contentResearchFinding.update({
where: { id }, data: { blogId: blog.id },
});
return { blog, finding, consumedFeedbackVersion: dto.feedbackVersion };
});
// Network/cache failures cannot roll back or turn a committed Blog into an
// ambiguous error. No external content generation runs inside the lock.
let cacheRevalidation: 'success' | 'failed' = 'failed';
try {
if (await (this.revalidation ?? new RevalidationService()).revalidateBlog(result.blog.slug))
cacheRevalidation = 'success';
} catch { /* return committed identity even on cache failure */ }
return { ...result, cacheRevalidation };
}
async addSnapshot(findingId: string, input: CreateSnapshotDto) {
const dto = checked(CreateSnapshotDto, input);
const periodStart = new Date(dto.periodStart),
+34 -18
View File
@@ -1,26 +1,42 @@
# Dormant daily publisher research/directive consumer
# Dormant research/directive publication boundary
No cron or active runtime is changed. The actual publisher is agent-driven by `/opt/data/publishers/e4c9b52eb040/cron-prompt.txt` (research line 11, draft/reviews line 13, guarded Admin creation line 17). Its directory and parent batch engine are outside Git. This repository previously contained no matching daily-publisher module. `daily_publisher.run_new` is the versioned integration boundary, not a claim of live enforcement.
No runtime publisher, cron, Gate1/2, deployment, or live API is changed. `daily_publisher.run_new` remains a dormant integration boundary, not current cron enforcement.
## Authorized future caller wiring
## Caller contract (breaking change to dormant callback)
Provision these modules through a reviewed Git/Stage/runtime process, then have the existing owner call `run_new(Consumer(journal, AdminTransport(verified_admin_origin, approved_bearer)), stable_run_id, finding_payload, Content_callback, independent_review_callback, existing_guarded_NEW_create_callback)`.
`run_new(consumer, stable_run_id, research, draft_content, independent_review, publication_preflight)` saves research before drafting. Content and the independent reviewer receive the complete fetched finding and exact CEO free text/version. The draft must now be the actual Admin Blog payload, not a wrapper containing HTML. A nonempty directive requires reviewer identity, immutable evidence location and exact directive/draft SHA256 bindings. Hash checks establish binding, not reviewer authenticity or arbitrary prose compliance.
- finding_payload uses the backend CreateFindingDto contract (title, hypothesis, targetKeywords, primaryKeyword, expectedOutcome, optional metric goal). No article is created by the adapter itself.
- Content callback receives the entire saved/fetched finding, including exact CEO free-text feedback/version. Save immutable draft artifacts; preserve the existing medical/SEO/QA chain.
- Independent reviewer callback receives finding, draft and both SHA256 digests. A nonempty directive requires reviewer identity, evidence artifact URI/path, exact directiveHash and draftHash. Review must quote the directive and map every request to draft passages or explicit unresolved findings. The adapter verifies binding, not arbitrary semantic obedience or reviewer authenticity; trusted review orchestration remains essential. Empty feedback needs no CEO approval. No Gate1/2 is introduced.
- Existing create callback must retain publisher_state ownership, fresh inventory/relations/quota checks, nonempty backup, independent exact-hash QA and write-ahead intent. Return confirmed blog UUID only. Never wrap an unguarded HTTP create callback in production.
- Persist journal outside temporary storage, shared by all invocations, with stable run ID. It records consumed finding and feedback versions, review, draft digest and created identity. A concurrent invocation cannot enter create after a claim transition. Network ambiguity blocks retries. Existing daily quota guard still protects different run IDs and different journal files.
- `reconcile_link(run_id)` only links the saved confirmed blog, never creates another article. If feedback changed after create, retain the blog and route independent repair. No automatic resave/update.
`publication_preflight(draft)` MUST run the existing publisher ownership, fresh inventory/relations/quota, backup, independent QA and write-ahead intent guards, and return the **unchanged reviewed payload without creating a Blog**. The consumer alone sends the atomic publication POST. Do not pass the previous Blog-creating callback: a returned string is rejected, but a malicious/incorrect callback could already have written before returning; trusted orchestration and reviewed wiring remain essential. No active wiring is included here.
## Atomicity limit (not concealed)
The SQLite journal persists intent before network writes. Failed/ambiguous POSTs are never automatically retried. `reconcile_link(run)` is now read-only: it confirms a matching committed actionId/blogId, including after later feedback, or blocks for independent repair. It never creates or relinks an article. Other run IDs still require the existing shared publisher quota/ownership guard.
Backend PATCH action atomically checks `expectedVersion` AND `feedbackVersion`; conflict prevents the callback. It does **not** participate in the later Blog create transaction. CEO edits after claim and before actual creation can therefore race. The adapter detects changed feedback on post-create link, retains the article identity and blocks linking/recreation, but cannot roll back the article. Full no-stale-publication requires an additional backend transaction/lease contract on the actual Blog creation boundary. Do not claim that consumption acknowledgement guarantees free-text implementation or that this dormant adapter protects current cron publication.
## Backend endpoint and serialization boundary
## Executed evidence
`POST /api/admin/seo-tracking/:id/publication`
- RED before implementation: unittest import failed because consumer module did not exist.
- `python -B -m unittest discover -s scripts/content-research -v`: **11 passed**, covering real integration entrypoint with isolated transport/callback fixtures, feedback before/after claim, optional feedback, exact receipt hashes, contention, idempotency, research timeout and publication ambiguity, link-only repair.
- Existing Git editorial suites: **2 passed, 3 skipped** (root-only ownership fixtures unavailable).
- Existing untouched runtime `python -B -m unittest discover -s /opt/data/publishers/e4c9b52eb040 -p test_publisher_state.py -v`: **3 passed, 2 errors**. Both existing tests call intent without the now-required independent QA fixture and fail `QA_REQUIRED: current-day independent exact-hash approval`. Runtime/test files were not edited; this is a pre-existing fixture drift, not a green regression claim.
- No live API request, publication, deployment, push, cron edit or activation. No backend/UI change. Tests establish local callback/API contract only, not live database/network acceptance.
Authenticated JWT admin/superadmin only (same controller guards as existing SEO tracking). Request:
```json
{"expectedVersion":0,"feedbackVersion":0,"actionId":"stable-run-id","blog":{"title":"...","slug":"...","content":"..."}}
```
`blog` uses existing Admin BlogsService create rules, including current-user author fallback, relations/tags, dates, schedule/status defaults and reading-time conversion. The endpoint deliberately does not invent another Blog field schema. It does not accept an arbitrary Prisma create input or bypass Admin create policy. Missing/invalid author or relation fails the entire transaction.
One transaction performs row-locking compare-and-swap on finding id/version/feedbackVersion with `blogId = null`, creates Blog via the existing Admin service using the same transaction client, then stores blogId/actionId. Response contains `blog`, `finding`, `consumedFeedbackVersion`, `cacheRevalidation`. Missing finding is 404; stale version/directive or already-linked finding is 409. Repeat requests are not create-idempotent responses: they conflict; the consumer reconciles saved action identity read-only. Cache revalidation happens only after commit; failure returns `failed` plus the committed identity, not an error encouraging recreation. External generation, review and publisher preflight happen before the transaction, not while holding the row lock.
Feedback PATCH uses the same row and finding version. If feedback commits first, publication's old predicate fails and no Blog exists. If publication locks first, feedback waits; after commit its old expectedVersion fails with 409. A fresh feedback request after committed publication applies to the **next action**, not retroactively to the already committed article. This is a linearization boundary, not a guarantee that all directives arriving after the boundary rewrite published content.
The legacy generic Blog endpoint and PATCH action remain for existing callers: this endpoint does not magically protect callers bypassing it. PATCH action is an acknowledgement/link operation, not publication authorization. No Gate1/2 or semantic-compliance claim is added.
## Many-actions and goals: explicitly not implemented
Prisma currently has one nullable unique blogId, one scalar nonunique external actionId, and one independently nullable goalMetric/goalTarget pair per finding. PATCH replaces those slots; it does not append immutable action history. A finding has many measurement snapshots, but snapshots target the finding only and cannot attribute evidence to a particular action or goal. UI shows one article/expectedOutcome and omits typed action/metric/target fields; the measurement worker resolves one article URL and does not evaluate structured goals.
A future additive migration should introduce Finding 1:N ResearchAction (immutable identity/idempotency key, lifecycle, consumed feedback version, external reference), Finding 1:N ResearchGoal (metric, comparator, unit, target/baseline, window, source), ActionGoal joins, and explicit action article targets. Goal evaluations should reference measured evidence, not imply causal attribution from aggregate page totals. Backfill at most one marked legacy action; never invent historical acknowledgements or complete goals from partial scalar data. Specify legacy projection/conflict rules before dual writes and update backend/UI/worker/tests together. This patch intentionally preserves the initial model and rejects a second publication on a linked finding.
## Local evidence
- Disposable PostgreSQL 16.14, schema pushed only to loopback port 55447; `publication.postgres.spec.ts` exercises actual row-lock wait with distinct backend PIDs via pg_stat_activity, stale-feedback-first rejection, publication-first feedback conflict, full rollback on invalid author, duplicate rejection and cache-failure identity.
- Same suite runs Python `run_new` over actual loopback TCP through Nest JWT/role guards and Prisma, proving successful atomic publication, changed feedback during preflight -> HTTP 409/no Blog, and unauthenticated publication -> HTTP 401. HTTPS production transport is not weakened: the test uses an explicit disposable-only loopback adapter.
- Harness and machine-readable results are local evidence under `/opt/data/cache/scratch/run-publication-pg.mjs`, `publication-pg.log`, `publication-jest.json`. Server shuts down in finally. The existing PG16 npm tooling lives in the untracked evidence directory; it is not a release dependency.
- No live writes, deploy, push, cron activation or runtime publisher edits.
+23 -25
View File
@@ -89,7 +89,7 @@ class Consumer:
if packet.get('ceoFeedback') is not None and not isinstance(packet['ceoFeedback'], str):
raise Blocked('Invalid directive')
def publish(self, run, packet, draft, review, publisher_create):
def publish(self, run, packet, draft, review, publication_preflight):
"""Caller must preserve existing ownership/QA/backup/quota/intent guards.
packet is the complete research/directive supplied to Content before drafting.
@@ -111,39 +111,37 @@ class Consumer:
('claiming', digest(draft), json.dumps(review), run, 'prepared')).rowcount
if changed != 1:
raise Blocked('Another consumer owns action')
# Atomic Backend predicate rejects edits between fetch and claim.
claim = self.api('PATCH', BASE + '/' + packet['id'] + '/action',
dict(expectedVersion=packet['version'], feedbackVersion=packet['feedbackVersion'], actionId=run))
self.check_packet(claim)
if claim['feedbackVersion'] != packet['feedbackVersion']:
raise Blocked('Unexpected claim response')
# Preflight MUST preserve existing ownership/QA/backup/quota/intent
# guards, but must NOT create a Blog. Only this atomic endpoint writes.
reviewed_hash = digest(draft)
payload = publication_preflight(draft)
if not isinstance(payload, dict) or digest(payload) != reviewed_hash:
raise Blocked('Preflight must return the exact reviewed Blog payload')
with self.db() as db:
db.execute('UPDATE runs SET state=? WHERE run=?', ('publication_intent', run))
blog = publisher_create(draft) # No retry on exception or uncertain result.
if not isinstance(blog, str) or not blog:
raise Blocked('Missing blog identity; reconcile publication intent')
result = self.api('POST', BASE + '/' + packet['id'] + '/publication',
dict(expectedVersion=packet['version'],
feedbackVersion=packet['feedbackVersion'], actionId=run, blog=payload))
blog = result.get('blog', {}).get('id')
if (not isinstance(blog, str) or not blog or
result.get('consumedFeedbackVersion') != packet['feedbackVersion']):
raise Blocked('Missing committed publication identity; reconcile, never retry')
with self.db() as db:
db.execute('UPDATE runs SET state=?,blog=? WHERE run=?', ('created', blog, run))
self.reconcile_link(run)
db.execute('UPDATE runs SET state=?,blog=? WHERE run=?', ('linked', blog, run))
return blog
def reconcile_link(self, run):
"""Read-only repair of an ambiguous response; never creates or relinks."""
row = self.row(run)
if row['state'] == 'linked':
return row['blog']
if row['state'] != 'created' or not row['blog']:
raise Blocked('No confirmed created article; authenticated reconciliation required')
packet = json.loads(row['packet'])
if row['state'] != 'publication_intent':
raise Blocked('No publication intent; independent reconciliation required')
current = self.api('GET', BASE + '/' + row['finding'])
self.check_packet(current)
if current['feedbackVersion'] != packet['feedbackVersion']:
raise Blocked('Directive changed after create: retain blog, independent repair required')
if current.get('blogId') not in (None, row['blog']):
raise Blocked('Finding already linked to another article')
if current.get('blogId') != row['blog']:
self.api('PATCH', BASE + '/' + row['finding'] + '/action',
dict(expectedVersion=current['version'], feedbackVersion=current['feedbackVersion'],
actionId=run, blogId=row['blog']))
if current.get('actionId') != run or not current.get('blogId'):
raise Blocked('No matching committed action; never retry automatically')
with self.db() as db:
db.execute('UPDATE runs SET state=? WHERE run=?', ('linked', run))
return row['blog']
db.execute('UPDATE runs SET state=?,blog=? WHERE run=?',
('linked', current['blogId'], run))
return current['blogId']
+5 -4
View File
@@ -1,16 +1,17 @@
"""Versioned integration entrypoint; not installed in the active cron runtime.
The supplied create_new callback MUST be the existing guarded NEW publisher,
The supplied publication_preflight callback MUST run the existing guarded NEW preflight,
including publisher_state ownership, quota, immutable QA, backup and intent.
No article update or automatic retry path is exposed here.
It must return the exact reviewed Blog payload without writing a Blog.
Only the consumer publication endpoint writes; no update or automatic retry path exists.
"""
from consumer import digest
def run_new(consumer, run_id, research, draft_content, independent_review, create_new):
def run_new(consumer, run_id, research, draft_content, independent_review, publication_preflight):
packet = consumer.prepare(run_id, research) # Persist research BEFORE drafting.
draft = draft_content(packet) # Complete fetched CEO directive, not just a keyword.
# Reviewer receives exact immutable content/directive plus digests. A valid receipt
# establishes traceability, not a machine guarantee of free-text obedience.
receipt = independent_review(packet, draft, digest(packet), digest(draft))
return consumer.publish(run_id, packet, draft, receipt, create_new)
return consumer.publish(run_id, packet, draft, receipt, publication_preflight)
+62 -139
View File
@@ -2,170 +2,93 @@ import tempfile
import unittest
from pathlib import Path
from consumer import Consumer, Blocked, digest
from daily_publisher import run_new
class API:
def __init__(self):
self.row = dict(id='finding', version=0, feedbackVersion=0, ceoFeedback=None)
self.events = []
self.stale = False
self.fail_link = False
self.created = []
self.ambiguous = False
def __call__(self, method, path, body=None):
self.events.append((method, path, body))
if method == 'POST':
if not path.endswith('/publication'):
return self.row.copy()
if method == 'GET':
return self.row.copy()
if self.stale or (body.get('blogId') and self.fail_link):
if body['expectedVersion'] != self.row['version'] or body['feedbackVersion'] != self.row['feedbackVersion'] or self.row.get('blogId'):
raise Blocked('409 stale')
assert body['expectedVersion'] == self.row['version']
assert body['feedbackVersion'] == self.row['feedbackVersion']
self.row.update({k: v for k, v in body.items() if k not in ('expectedVersion', 'feedbackVersion')})
self.row['version'] += 1
return self.row.copy()
self.created.append(body['blog'])
self.row.update(version=self.row['version'] + 1, blogId='blog-id', actionId=body['actionId'])
if self.ambiguous:
raise TimeoutError('committed response lost')
return dict(blog={'id': 'blog-id'}, finding=self.row.copy(), consumedFeedbackVersion=body['feedbackVersion'])
class ConsumerTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.api = API()
self.c = Consumer(Path(self.tmp.name) / 'journal.db', self.api)
self.created = []
def tearDown(self):
self.tmp.cleanup()
def publish(self, draft):
self.created.append(draft)
return 'blog-id'
def prepare(self):
return self.c.prepare('day-run', {'title': 'research'})
def review(self, packet, draft):
return dict(reviewer='independent-seo', directiveHash=digest(packet), draftHash=digest(draft), evidence='immutable://review-receipt')
def test_research_before_draft_then_claim_create_link_idempotent(self):
packet = self.prepare()
draft = {'html': 'reviewed'}
result = self.c.publish('day-run', packet, draft, None, self.publish)
self.assertEqual(result, 'blog-id')
self.assertEqual(self.c.publish('day-run', packet, draft, None, self.publish), result)
self.assertEqual(len(self.created), 1)
events = self.api.events
self.assertEqual(events[0][0], 'POST')
self.assertEqual(events[-1][2]['blogId'], 'blog-id')
self.assertEqual([e for e in events if e[0] == 'PATCH'][0][2]['feedbackVersion'], 0)
def test_directive_passed_and_requires_exact_review(self):
self.api.row.update(version=3, feedbackVersion=2, ceoFeedback='Focus on cats')
packet = self.prepare()
self.assertEqual(packet['ceoFeedback'], 'Focus on cats')
draft = {'html': 'cats'}
def receipt(self, p, d, *args):
return dict(reviewer='independent', evidence='immutable://receipt', directiveHash=digest(p), draftHash=digest(d))
def test_entrypoint_receives_directive_then_atomic_publication_idempotent(self):
self.api.row.update(version=1, feedbackVersion=1, ceoFeedback='cats')
def draft(p):
self.assertEqual(p['ceoFeedback'], 'cats')
return {'content': 'cats'}
self.assertEqual(run_new(self.c, 'run', {}, draft, self.receipt, lambda d: d), 'blog-id')
import json
p = json.loads(self.c.row('run')['packet'])
self.assertEqual(self.c.publish('run', p, {}, None, lambda d: d), 'blog-id')
self.assertEqual(len(self.api.created), 1)
self.assertFalse(any(e[0] == 'PATCH' for e in self.api.events))
def test_feedback_between_preflight_and_create_blocks_blog(self):
def preflight(d):
self.api.row.update(version=1, feedbackVersion=1, ceoFeedback='late')
return d
with self.assertRaises(Blocked):
self.c.publish('day-run', packet, draft, None, self.publish)
receipt = self.review(packet, draft)
receipt['draftHash'] = 'wrong'
run_new(self.c, 'run', {}, lambda p: {}, self.receipt, preflight)
self.assertEqual(self.api.created, [])
def test_non_payload_legacy_callback_fails_closed(self):
with self.assertRaises(Blocked):
self.c.publish('day-run', packet, draft, receipt, self.publish)
self.c.publish('day-run', packet, draft, self.review(packet, draft), self.publish)
self.assertEqual(len(self.created), 1)
def test_stale_claim_never_calls_publisher(self):
packet = self.prepare()
self.api.stale = True
run_new(self.c, 'run', {}, lambda p: {}, self.receipt, lambda d: 'blog-id')
self.assertEqual(self.api.created, [])
def test_preflight_cannot_mutate_reviewed_payload(self):
def mutate(d):
d['content'] = 'unreviewed'
return d
with self.assertRaises(Blocked):
self.c.publish('day-run', packet, {}, None, self.publish)
self.assertEqual(self.created, [])
def test_ambiguous_publish_blocks_restart(self):
packet = self.prepare()
def ambiguous(draft):
self.created.append(draft)
raise TimeoutError('unknown write outcome')
run_new(self.c, 'run', {}, lambda p: {'content': 'reviewed'}, self.receipt, mutate)
self.assertEqual(self.api.created, [])
def test_review_exact_hash_required(self):
self.api.row.update(ceoFeedback='cats')
p = self.c.prepare('run', {})
with self.assertRaises(Blocked):
self.c.publish('run', p, {}, None, lambda d: d)
r = self.receipt(p, {})
r['draftHash'] = 'bad'
with self.assertRaises(Blocked):
self.c.publish('run', p, {}, r, lambda d: d)
def test_ambiguous_response_reconciles_read_only_even_after_new_feedback(self):
self.api.ambiguous = True
with self.assertRaises(TimeoutError):
self.c.publish('day-run', packet, {}, None, ambiguous)
restarted = Consumer(self.c.path, self.api)
run_new(self.c, 'run', {}, lambda p: {}, self.receipt, lambda d: d)
self.api.row.update(version=2, feedbackVersion=1, ceoFeedback='next action')
with self.assertRaises(Blocked):
restarted.publish('day-run', packet, {}, None, self.publish)
self.assertEqual(len(self.created), 1)
def test_link_failure_can_retry_link_only(self):
packet = self.prepare()
self.api.fail_link = True
self.c.publish('run', self.c.prepare('run', {}), {}, None, lambda d: d)
self.assertEqual(self.c.reconcile_link('run'), 'blog-id')
self.assertEqual(len(self.api.created), 1)
def test_packet_tampering_and_parallel_consumers_blocked(self):
p = self.c.prepare('run', {})
with self.assertRaises(Blocked):
self.c.publish('day-run', packet, {}, None, self.publish)
self.api.fail_link = False
self.c.reconcile_link('day-run')
self.assertEqual(len(self.created), 1)
def test_research_post_ambiguity_never_reposted(self):
def fail(*args):
raise TimeoutError('unknown')
c = Consumer(self.c.path, fail)
with self.assertRaises(TimeoutError):
c.prepare('run', {})
with self.assertRaises(Blocked):
self.c.prepare('run', {})
self.assertEqual(self.api.events, [])
def test_packet_tampering_rejected(self):
packet = self.prepare()
packet['ceoFeedback'] = ''
with self.assertRaises(Blocked):
self.c.publish('day-run', packet, {}, None, self.publish)
def test_actual_entrypoint_delivers_directive_before_create(self):
from daily_publisher import run_new
self.api.row.update(version=1, feedbackVersion=1, ceoFeedback='cats only')
order = []
def draft(packet):
self.assertEqual(self.api.events[0][0], 'POST')
self.assertEqual(packet['ceoFeedback'], 'cats only')
order.append('draft')
return {'html': 'cats'}
def review(packet, article, directive_hash, draft_hash):
order.append('review')
return self.review(packet, article)
def create(article):
order.append('create')
return self.publish(article)
self.assertEqual(run_new(self.c, 'entry', {}, draft, review, create), 'blog-id')
self.assertEqual(order, ['draft', 'review', 'create'])
def test_feedback_edit_during_draft_rejects_action_and_create(self):
from daily_publisher import run_new
def draft(packet):
self.api.row.update(version=1, feedbackVersion=1, ceoFeedback='new instruction')
self.api.stale = True
return {}
with self.assertRaises(Blocked):
run_new(self.c, 'entry', {}, draft, lambda *args: None, self.publish)
self.assertEqual(self.created, [])
def test_feedback_after_claim_retains_created_identity_no_second_create(self):
packet = self.prepare()
def create(draft):
self.api.row.update(version=2, feedbackVersion=1, ceoFeedback='late')
return self.publish(draft)
with self.assertRaises(Blocked):
self.c.publish('day-run', packet, {}, None, create)
self.assertEqual(self.c.row('day-run')['blog'], 'blog-id')
with self.assertRaises(Blocked):
self.c.publish('day-run', packet, {}, None, self.publish)
self.assertEqual(len(self.created), 1)
def test_overlapping_consumers_only_one_publish(self):
packet = self.prepare()
self.c.publish('run', dict(p, ceoFeedback='tampered'), {}, None, lambda d: d)
other = Consumer(self.c.path, self.api)
def nested(draft):
def preflight(d):
with self.assertRaises(Blocked):
other.publish('day-run', packet, draft, None, self.publish)
return self.publish(draft)
self.c.publish('day-run', packet, {}, None, nested)
self.assertEqual(len(self.created), 1)
other.publish('run', p, d, None, lambda d: d)
return d
self.c.publish('run', p, {}, None, preflight)
self.assertEqual(len(self.api.created), 1)
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,51 @@
"""Disposable-loopback acceptance only; deliberately not AdminTransport HTTPS bypass."""
import json
import os
import tempfile
import urllib.request
import urllib.error
from consumer import Consumer, Blocked
from daily_publisher import run_new
origin = os.environ['PUBLICATION_LOCAL_ORIGIN']
if not origin.startswith('http://127.0.0.1:'):
raise RuntimeError('Disposable loopback only')
token = os.environ['PUBLICATION_LOCAL_TOKEN']
def api(method, path, body=None, bearer=token):
req = urllib.request.Request(origin + path, method=method,
data=None if body is None else json.dumps(body).encode(),
headers={'Authorization': 'Bearer ' + bearer, 'Content-Type': 'application/json'})
try:
with urllib.request.urlopen(req, timeout=10) as r:
return json.load(r)
except urllib.error.HTTPError as e:
raise Blocked('HTTP ' + str(e.code)) from None
research = dict(title='publication-http-fixture', hypothesis='disposable',
primaryKeyword='fixture', targetKeywords=['fixture'], expectedOutcome='fixture')
with tempfile.TemporaryDirectory() as tmp:
c = Consumer(tmp + '/journal.db', api)
result = run_new(c, 'http-success', research,
lambda p: dict(title='fixture', slug='publication-fixture-http', content='<p>reviewed</p>'),
lambda *args: None, lambda d: d)
assert result and c.reconcile_link('http-success') == result
stale = Consumer(tmp + '/stale.db', api)
def preflight(d):
p = json.loads(stale.row('http-stale')['packet'])
api('PATCH', '/api/admin/seo-tracking/' + p['id'] + '/feedback',
dict(expectedVersion=p['version'], feedback='changed after draft'))
return d
try:
run_new(stale, 'http-stale', research,
lambda p: dict(title='stale', slug='publication-fixture-http-stale', content='stale'),
lambda *args: None, preflight)
raise AssertionError('stale publication accepted')
except Blocked as e:
assert str(e) == 'HTTP 409'
p = json.loads(stale.row('http-stale')['packet'])
assert api('GET', '/api/admin/seo-tracking/' + p['id'])['blogId'] is None
try:
api('POST', '/api/admin/seo-tracking/' + p['id'] + '/publication', {}, bearer='invalid')
raise AssertionError('unauthenticated publication accepted')
except Blocked as e:
assert str(e) == 'HTTP 401'
print('PUBLICATION_REAL_HTTP_PASSED')