File size: 14,212 Bytes
bc5aa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e16649
bc5aa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e16649
 
 
 
 
 
 
 
 
 
 
 
 
 
bc5aa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e16649
bc5aa4d
 
 
7e16649
bc5aa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e16649
 
bc5aa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e16649
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc5aa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e16649
 
bc5aa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e16649
bc5aa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e16649
bc5aa4d
 
 
 
 
7e16649
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import json
import random
import uuid
from dataclasses import asdict
from pathlib import Path
from typing import Dict, List

from .grader import calculate_reward
from .models import (
    CheckResults,
    GeoAuditAction,
    GeoAuditObservation,
    GeoAuditState,
    PageData,
)


class GeoAuditEnvironment:
    """
    A small RL-style environment for GEO page auditing.

    reset() starts a new page audit.
    step() applies one action such as checking metadata or flagging an issue.
    """

    AVAILABLE_ACTIONS = [
        "check_title_tag",
        "check_meta_description",
        "check_headers",
        "check_schema",
        "check_direct_answer",
        "check_content_structure",
        "check_word_count",
        "check_trust_signals",
        "check_sources",
        "flag_issue",
        "mark_positive",
        "submit_report",
    ]

    ISSUE_TYPES = [
        "missing_title_tag",
        "weak_title_tag",
        "missing_meta_description",
        "weak_meta_description",
        "no_direct_answer",
        "buried_answer",
        "missing_faq_schema",
        "missing_howto_schema",
        "missing_article_schema",
        "no_headers",
        "poor_header_structure",
        "thin_content",
        "no_author",
        "no_date",
        "no_sources",
    ]

    POSITIVE_TYPES = [
        "clear_direct_answer",
        "strong_title_tag",
        "strong_meta_description",
        "good_heading_structure",
        "has_faq_schema",
        "has_howto_schema",
        "has_article_schema",
        "has_author",
        "has_date",
        "has_sources",
        "comprehensive_content",
    ]

    def __init__(self) -> None:
        self._state = GeoAuditState()
        self._data = self._load_data()

    def _load_data(self) -> Dict[str, List[Dict]]:
        data_dir = Path(__file__).parent.parent / "data"
        return {
            "easy": self._load_json(data_dir / "task1_easy.json"),
            "medium": self._load_json(data_dir / "task2_medium.json"),
            "hard": self._load_json(data_dir / "task3_hard.json"),
        }

    def _load_json(self, path: Path) -> List[Dict]:
        if not path.exists():
            return []
        with path.open("r", encoding="utf-8") as handle:
            return json.load(handle)

    def reset(self, task_difficulty: str = "easy") -> GeoAuditObservation:
        pages = self._data.get(task_difficulty, [])
        page = random.choice(pages) if pages else self._get_dummy_page()

        self._state = GeoAuditState(
            episode_id=str(uuid.uuid4()),
            task_id=page.get("page_id", "unknown"),
            task_difficulty=task_difficulty,
            step_count=0,
            max_steps=10,
            ground_truth_issues=page.get("issues", []),
            ground_truth_positives=page.get("positives", []),
            current_page=page,
            checks_performed=[],
            flagged_issues=[],
            marked_positives=[],
        )

        return self._build_observation(
            message=(
                "Audit started. Inspect the page, flag issues, then submit the report. "
                f"Target query: '{page.get('target_query', '')}'"
            )
        )

    def step(self, action: GeoAuditAction) -> GeoAuditObservation:
        self._state.step_count += 1

        if self._state.step_count > self._state.max_steps:
            return self._finalize("Max steps reached. Auto-submitting report.")

        if action.action_type.startswith("check_"):
            return self._handle_check(action)
        if action.action_type == "flag_issue":
            return self._handle_flag(action)
        if action.action_type == "mark_positive":
            return self._handle_positive(action)
        if action.action_type == "submit_report":
            return self._finalize("Report submitted.")

        return self._build_observation(message=f"Unknown action: {action.action_type}")

    def _handle_check(self, action: GeoAuditAction) -> GeoAuditObservation:
        if action.action_type in self._state.checks_performed:
            return self._build_observation(
                message=f"Already checked: {action.action_type}"
            )

        self._state.checks_performed.append(action.action_type)
        result = self._analyze(action.action_type.replace("check_", ""))
        return self._build_observation(message=result)

    def _handle_flag(self, action: GeoAuditAction) -> GeoAuditObservation:
        if not action.issue_type:
            return self._build_observation(
                message="flag_issue requires an issue_type."
            )

        if action.issue_type not in self.ISSUE_TYPES:
            return self._build_observation(
                message=f"Unknown issue type: {action.issue_type}"
            )

        issue = {
            "type": action.issue_type,
            "severity": action.severity or "medium",
            "details": action.details or "",
        }

        existing_types = {item["type"] for item in self._state.flagged_issues}
        if issue["type"] not in existing_types:
            self._state.flagged_issues.append(issue)

        return self._build_observation(
            message=f"Flagged issue: {issue['type']} ({issue['severity']})"
        )

    def _handle_positive(self, action: GeoAuditAction) -> GeoAuditObservation:
        if not action.positive_type:
            return self._build_observation(
                message="mark_positive requires a positive_type."
            )

        if action.positive_type not in self.POSITIVE_TYPES:
            return self._build_observation(
                message=f"Unknown positive type: {action.positive_type}"
            )

        positive = {
            "type": action.positive_type,
            "details": action.details or "",
        }

        existing_types = {item["type"] for item in self._state.marked_positives}
        if positive["type"] not in existing_types:
            self._state.marked_positives.append(positive)

        return self._build_observation(
            message=f"Marked positive: {positive['type']}"
        )

    def _analyze(self, check_type: str) -> str:
        page = self._state.current_page

        if check_type == "title_tag":
            title = page.get("title_tag", "")
            if not title:
                return "Title check: no title tag found."
            return f"Title check: '{title}' ({len(title)} chars)."

        if check_type == "meta_description":
            meta = page.get("meta_description", "")
            if not meta:
                return "Meta description check: missing."
            return f"Meta description check: '{meta[:100]}' ({len(meta)} chars)."

        if check_type == "headers":
            headers = page.get("headers", [])
            h1 = page.get("h1", "")
            return f"Header check: H1='{h1}', section headers={headers}."

        if check_type == "schema":
            schemas = page.get("schema_types", [])
            if not schemas:
                return "Schema check: no schema markup found."
            return f"Schema check: found {schemas}."

        if check_type == "direct_answer":
            paragraph = page.get("first_paragraph", "")
            query = page.get("target_query", "")
            return (
                "Direct answer check: "
                f"query='{query}', opening paragraph='{paragraph[:140]}'."
            )

        if check_type == "content_structure":
            return (
                "Content structure check: "
                f"{len(page.get('headers', []))} headers, "
                f"{page.get('word_count', 0)} words."
            )

        if check_type == "word_count":
            count = page.get("word_count", 0)
            if count < 300:
                return f"Word count check: thin content ({count} words)."
            if count < 500:
                return f"Word count check: light content ({count} words)."
            return f"Word count check: healthy length ({count} words)."

        if check_type == "trust_signals":
            return (
                "Trust signal check: "
                f"author={page.get('has_author', False)}, "
                f"date={page.get('has_date', False)}."
            )

        if check_type == "sources":
            return (
                "Source check: "
                f"has_sources={page.get('has_sources', False)}, "
                f"source_count={page.get('source_count', 0)}."
            )

        return f"Unknown check type: {check_type}"

    def _finalize(self, message: str) -> GeoAuditObservation:
        reward = calculate_reward(
            flagged=self._state.flagged_issues,
            ground_truth=self._state.ground_truth_issues,
            marked_positives=self._state.marked_positives,
            ground_truth_positives=self._state.ground_truth_positives,
        )
        return self._build_observation(message=message, done=True, reward=reward)

    def _build_observation(
        self,
        message: str = "",
        done: bool = False,
        reward: float = 0.0,
    ) -> GeoAuditObservation:
        page = self._state.current_page
        return GeoAuditObservation(
            page=PageData(
                url=page.get("url", ""),
                target_query=page.get("target_query", ""),
                title_tag=page.get("title_tag", ""),
                meta_description=page.get("meta_description", ""),
                h1=page.get("h1", ""),
                first_paragraph=page.get("first_paragraph", ""),
                word_count=page.get("word_count", 0),
                headers=page.get("headers", []),
                schema_types=page.get("schema_types", []),
                has_author=page.get("has_author", False),
                has_date=page.get("has_date", False),
                has_sources=page.get("has_sources", False),
                source_count=page.get("source_count", 0),
            ),
            checked=self._build_check_results(),
            flagged_issues=self._state.flagged_issues,
            marked_positives=self._state.marked_positives,
            step_count=self._state.step_count,
            max_steps=self._state.max_steps,
            available_actions=self.AVAILABLE_ACTIONS,
            message=message,
            done=done,
            reward=reward,
        )

    def observation_dict(self, observation: GeoAuditObservation) -> Dict:
        """Convert nested dataclasses into plain JSON-friendly structures."""
        return asdict(observation)

    def _build_check_results(self) -> CheckResults:
        page = self._state.current_page
        results = CheckResults()

        for check in self._state.checks_performed:
            check_type = check.replace("check_", "")

            if check_type == "title_tag":
                results.title_tag = {
                    "present": bool(page.get("title_tag")),
                    "value": page.get("title_tag", ""),
                    "length": len(page.get("title_tag", "")),
                }
            elif check_type == "meta_description":
                results.meta_description = {
                    "present": bool(page.get("meta_description")),
                    "value": page.get("meta_description", ""),
                    "length": len(page.get("meta_description", "")),
                }
            elif check_type == "headers":
                results.headers = {
                    "h1": page.get("h1", ""),
                    "headers": page.get("headers", []),
                }
            elif check_type == "schema":
                results.schema = {"schema_types": page.get("schema_types", [])}
            elif check_type == "direct_answer":
                results.direct_answer = {
                    "first_paragraph": page.get("first_paragraph", "")
                }
            elif check_type == "content_structure":
                results.content_structure = {
                    "header_count": len(page.get("headers", [])),
                    "word_count": page.get("word_count", 0),
                }
            elif check_type == "word_count":
                results.word_count = {"word_count": page.get("word_count", 0)}
            elif check_type == "trust_signals":
                results.trust_signals = {
                    "has_author": page.get("has_author", False),
                    "has_date": page.get("has_date", False),
                }
            elif check_type == "sources":
                results.sources = {
                    "has_sources": page.get("has_sources", False),
                    "source_count": page.get("source_count", 0),
                }

        return results

    def _get_dummy_page(self) -> Dict:
        return {
            "page_id": "dummy_001",
            "url": "https://example.com/how-to-stake-ethereum",
            "target_query": "how to stake ethereum",
            "title_tag": "Ethereum Staking Basics",
            "meta_description": "",
            "h1": "Ethereum Staking Guide",
            "first_paragraph": (
                "Welcome to our blockchain blog. We love crypto and share updates "
                "for curious readers exploring new technologies."
            ),
            "word_count": 280,
            "headers": ["H2: About", "H2: Contact"],
            "schema_types": [],
            "has_author": False,
            "has_date": False,
            "has_sources": False,
            "source_count": 0,
            "issues": [
                {"type": "missing_meta_description", "severity": "critical"},
                {"type": "no_direct_answer", "severity": "critical"},
                {"type": "thin_content", "severity": "medium"},
            ],
            "positives": [],
        }

    @property
    def state(self) -> GeoAuditState:
        return self._state

    def current_observation(self) -> GeoAuditObservation:
        if not self._state.current_page:
            return self._build_observation(
                message="No active episode. Call reset() to start an audit."
            )
        return self._build_observation(message="Current episode state.")