# Google Drive ZIP unpack + local document briefing workflow

Use when the user uploads a ZIP to a private Google Drive folder and asks to unpack it in-place, download/analyze the contents, and return a short briefing.

## Proven workflow

1. **Locate the folder and ZIP.**
   - Start from the named folder if known, then list likely child folders.
   - If the file may still be processing/uploading, retry `drive ls` on the expected parent and use raw Drive queries like `name contains 'OneDrive_1'` or ZIP MIME filters.
   - Confirm the ZIP's `id`, `parents`, `name`, `size`, and `webViewLink` before mutating Drive.

2. **Download to task-local storage.**
   ```bash
   WORK=/home/agent/tmp/<task_slug>
   mkdir -p "$WORK/download" "$WORK/extracted"
   gog -a <account> drive download <zip_file_id> --out "$WORK/download/<name>.zip" --no-input
   ```

3. **Validate and extract locally before upload.**
   ```bash
   python3 - <<'PY'
   import zipfile, pathlib
   work=pathlib.Path('/home/agent/tmp/<task_slug>')
   z=work/'download/<name>.zip'
   out=work/'extracted'
   with zipfile.ZipFile(z) as f:
       bad=f.testzip()
       if bad: raise SystemExit(f'Bad zip member: {bad}')
       f.extractall(out)
   for p in sorted(out.rglob('*')):
       if p.is_file(): print(p.relative_to(out), p.stat().st_size)
   PY
   ```

4. **Upload extracted files to the ZIP's parent folder and verify.**
   ```bash
   cd "$WORK/extracted"
   for f in *; do
     [ -f "$f" ] || continue
     gog -a <account> drive upload "$f" --parent <parent_folder_id> --json --results-only --no-input
   done
   gog -a <account> drive ls --parent <parent_folder_id> --json --no-input
   ```
   - Use `--replace <file_id>` only when the user wants to update an existing file/link; for unpacking a new ZIP, normal create uploads are expected.

5. **Extract text for analysis.**
   - Use `pdftotext -layout` first for born-digital PDFs.
   - If the main report produces very little text, use `ocrmypdf` with a sidecar text file. Prefer `--skip-text` first; if important pages are skipped because of embedded/garbled text, `--force-ocr` can recover summary/conclusion pages, but avoid uploading the huge OCR PDF unless requested.
   ```bash
   pdftotext -layout input.pdf output.txt
   ocrmypdf -l deu+eng --sidecar output_ocr.txt --skip-text --output-type pdf input.pdf /tmp/ocr.pdf
   ocrmypdf -l deu+eng --force-ocr --sidecar output_forceocr.txt --output-type pdf input.pdf /tmp/ocr_force.pdf
   ```

6. **Briefing format for political/meeting documents.**
   - Confirm Drive side effects first: ZIP found, extracted files uploaded, report uploaded if applicable.
   - Keep the user-facing briefing concise and decision-oriented:
     - Worum es geht
     - Kernaussagen / Zahlen
     - Hauptdefizite
     - politische/strategische Fragen
     - empfohlene Sitzungsposition
     - konkrete Fragen für die Sitzung
   - For Swiss/German local politics, avoid pretending certainty where the report says assumptions/prognoses; label forecast sensitivity explicitly.

## Pitfalls

- Google Drive search can lag immediately after upload; listing the expected parent folder is often more reliable than full-text search.
- Some PDFs are tagged/born-digital but yield almost no useful `pdftotext` output; OCR sidecars are useful for reports with scanned pages or rotated tables.
- OCR logs can be noisy and long. Summarize page/char counts and key extracted findings rather than pasting raw OCR.
- Do not print OAuth/keyring secrets; source the configured env file as described in the private Drive retrieval reference.
