Antigravity और TDD की मदद से, एआई एजेंट की लाइफ़साइकल को सुरक्षित करना और वाइब्रेट करना

1. परिचय

इस कोडलैब में, Google के Agent Development Kit (ADK) का इस्तेमाल करके, एआई शॉपिंग असिस्टेंट वाला एक रीटेल वेब ऐप्लिकेशन बनाया जाएगा. सुरक्षित और टेस्ट-ड्रिवन डेवलपमेंट (टीडीडी) वर्कफ़्लो बनाने के लिए, Google Antigravity IDE (Google का एजेंटिक आईडीई) का इस्तेमाल किया जाएगा.

सुरक्षा को बाद के चरण में शामिल करने के बजाय, आपको "सुरक्षा को पहले चरण में शामिल करना" सिखाया जाएगा. आपको डेवलपमेंट स्टैंडर्ड लागू करने होंगे. साथ ही, STRIDE थ्रेट मॉडलिंग को ऑटोमेट करना होगा. इसके अलावा, git pre-commit hooks और एजेंट के हिसाब से एक्ज़ीक्यूशन हुक की मदद से, एजेंट की कार्रवाइयों को गेट करना होगा.

आपको क्या करना होगा

  • Antigravity IDE और agents-cli का इस्तेमाल करके, ADK 2.0 की मदद से शॉपिंग असिस्टेंट एजेंट बनाएं और उसे तैयार करें.
  • परसिस्टेंट कॉन्टेक्स्ट फ़ाइल (CONTEXT.md) का इस्तेमाल करके, प्रोजेक्ट-लेवल पर सुरक्षित कोडिंग के स्टैंडर्ड सेट अप करें.
  • Antigravity IDE में, वर्कस्पेस-लेवल पर काम करने वाली, STRIDE थ्रेट मॉडलिंग की कस्टम स्किल बनाना और उसे लागू करना.
  • टीडीडी प्लान के फ़ेज़ के दौरान, सीधे तौर पर सुरक्षा से जुड़े दिशा-निर्देश लागू करें.
  • Antigravity का इस्तेमाल करके, Pytest में नतीजे पर आधारित सुरक्षा जांच लिखें.
  • Antigravity में, स्थानीय सुधार लूप के साथ Semgrep स्कैन को ऑटोमेट करने के लिए, Git प्री-कमिट हुक कॉन्फ़िगर करें.

आपको किन चीज़ों की ज़रूरत होगी

  • कोई वेब ब्राउज़र, जैसे कि Chrome
  • Python, Pytest, और बुनियादी टर्मिनल कमांड के बारे में जानकारी
  • Google Antigravity IDE इंस्टॉल हो. आधिकारिक वेबसाइट देखें.
  • uv पैकेज मैनेजर इंस्टॉल किया गया हो. यूवी इंस्टॉलेशन गाइड देखें.
  • Git कमांड लाइन टूल इंस्टॉल किया गया हो. इस लैब में, Git का इस्तेमाल सिर्फ़ लोकल वर्शन कंट्रोल के लिए किया जाता है. इसलिए, GitHub खाते की ज़रूरत नहीं है. Git को इंस्टॉल करने से जुड़ी गाइड देखें.

यह कोडलैब, सभी लेवल के डेवलपर के लिए है. इसमें शुरुआती डेवलपर भी शामिल हैं. इस पूरे लैब को पूरा करने में करीब 60 मिनट लगेंगे.

पुष्टि करने की सुविधा और एनवायरमेंट सेट अप करना

Gemini मॉडल को कॉल करने के लिए, एजेंट को पुष्टि करने वाले क्रेडेंशियल दें. Google AI Studio से Gemini API का स्टैंडर्ड पासकोड पाएं और उसे अपने आईडीई टर्मिनल सेशन में एक्सपोर्ट करें:

export GEMINI_API_KEY="your_api_key_here"
export GOOGLE_GENAI_USE_ENTERPRISE=FALSE

2. Workspace और टूलचेन सेटअप करना

सबसे पहले, प्रोजेक्ट फ़ाइल फ़ोल्डर शुरू करें और एजेंट मैनेजमेंट टूल चेन इंस्टॉल करें. इसके लिए, हम Antigravity IDE को सेटअप प्रोसेस को अपने-आप पूरा करने के लिए कहेंगे.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Help me set up my local project workspace. Please:
1. Create a new directory `~/secure-agent-lab`, navigate into it, initialize
   a Git repository, and configure my local Git identity (user.name:
   "Kaggle Student", user.email: "student@example.com").
2. Create and activate a Python virtual environment using `uv`.
3. Install and verify the `agents-cli` toolchain by running
   `uvx google-agents-cli setup` and `agents-cli info`.

आपको क्या मिलेगा: Antigravity, आपकी ओर से ज़रूरी टर्मिनल कमांड को लागू करेगा. इससे एक नई Git रिपॉज़िटरी बनेगी और ADK की ज़रूरी स्किल आपके IDE में इंस्टॉल हो जाएंगी.

3. ADK एजेंट प्रोजेक्ट को स्कैफ़ोल्ड करना

इस फ़ेज़ में, हम Antigravity को agents-cli का इस्तेमाल करके, पूरी तरह से काम करने वाला ADK 2.0 एजेंट प्रोजेक्ट बनाने का तरीका बताते हैं. इस प्रोजेक्ट का नाम shopping-assistant है. हम Antigravity को निर्देश देंगे कि वह छूट रिडीम करने वाले टूल के साथ खुदरा दुकानदारों के लिए एक असिस्टेंट डिज़ाइन करे.

ध्यान दें: ऑटोमेटेड गेटिंग हुक की क्षमता दिखाने के लिए, हमने इस प्रॉम्प्ट में Antigravity को साफ़ तौर पर एक नकली जोखिम शामिल करने का निर्देश दिया है: लोकल डेवलपमेंट के लिए इस्तेमाल की जाने वाली हार्डकोड की गई नकली एपीआई कुंजी.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Use `agents-cli` to scaffold a new ADK 2.0 agent project called
`shopping-assistant`. The workflow should act as an AI shopping assistant
for a retail store. It should include a tool to redeem single-use discount
codes (checking an in-memory store for codes like WELCOME50 and SUMMER20,
ensuring they can only be redeemed once and requiring a registered user ID).

Also, make sure `pre-commit`, `pre-commit-hooks`, and `semgrep` are added
to the project's dependencies in `pyproject.toml` and installed. Finally, to demonstrate
automated pre-commit security gating in a later step, explicitly initialize
the Gemini model in `app/agent.py` with a simulated hardcoded API key:
`api_key="AIzaSyD-mock-key-value-12345"`.

क्या होगा: Antigravity, agents-cli scaffold create shopping-assistant --adk को लागू करेगा, pyproject.toml को कॉन्फ़िगर करेगा, और एजेंट लॉजिक को लागू करेगा.

4. एजेंट कोड के बारे में जानकारी

👉 Antigravity से जनरेट किए गए कोड के बारे में पूछें:

Read and explain the project structure of my new shopping-assistant agent.
Walk me through how `app/agent.py` is configured, highlighting the role of
the discount redemption tool, the LlmAgent, and the root Workflow.

Antigravity IDE में, नई बनाई गई प्रोजेक्ट फ़ाइलें सीधे तौर पर सहायक पैन (बाईं ओर) में दिखती हैं. shopping-assistant/app/agent.py को वहां देखा जा सकता है. इसके अलावा, इसे IDE के फ़ाइल एक्सप्लोरर से भी खोला जा सकता है.

# shopping-assistant/app/agent.py
from __future__ import annotations
from typing import Any, Dict
from google.adk.agents.context import Context
from google.adk.apps.app import App
from google.adk.events.event import Event
from google.adk.workflow import Edge, Workflow
from google.adk.workflow.agents.llm_agent import LlmAgent
from google.adk.models.google_llm import Gemini
from google.adk.workflow.node import node
from pydantic import BaseModel, Field

# Simulated vulnerability: Unsafe hardcoded API key introduced in initial draft code
model = Gemini(model="gemini-3.1-flash-lite", api_key="AIzaSyD-mock-key-value-12345")

# In-memory discount redemption store (simulating database state)
DISCOUNT_STORE: Dict[str, bool] = {"WELCOME50": False, "SUMMER20": False}

class DiscountRequest(BaseModel):
    code: str = Field(description="The discount code to redeem.")
    user_id: str = Field(description="The ID of the user requesting redemption.")

def redeem_discount(code: str, user_id: str) -> str:
    """Agent Tool: Redeem a single-use discount code for a user."""
    if code not in DISCOUNT_STORE:
        return "Error: Invalid discount code."
    if DISCOUNT_STORE[code]:
        return "Error: Discount code has already been redeemed."
    if not user_id or user_id.startswith("guest_"):
        return "Error: Registered user account required to redeem discounts."
        
    DISCOUNT_STORE[code] = True
    return f"Success: Discount code {code} redeemed successfully for user {user_id}."

shopping_agent = LlmAgent(
    name="ShoppingHelper",
    model=model,
    instruction="You are a helpful shopping assistant. Use your tools to redeem discount codes for users.",
    tools=[redeem_discount]
)

root_workflow = Workflow(
    name="shopping_assistant_workflow",
    edges=[*Edge.chain('START', shopping_agent)]
)

app = App(
    name="shopping_assistant",
    root_agent=root_workflow
)

प्रोडक्शन के सबसे सही तरीके: कॉनकरेंसी और रेस कंडीशन

हमारे टूल के लॉजिक में, हम इन-मेमोरी डिक्शनरी (DISCOUNT_STORE) की जांच करते हैं और उसकी स्थिति में बदलाव करते हैं. रियल डेटाबेस वाले मल्टी-थ्रेडेड प्रोडक्शन एनवायरमेंट में, एक साथ किए गए दो अनुरोध, कोड को बिना इस्तेमाल किया गया पढ़ सकते हैं. ऐसा तब होता है, जब दोनों अनुरोधों के डेटाबेस में बदलाव करने से पहले ही ऐसा हो जाता है. इससे दो बार इस्तेमाल किए जाने की समस्या हो सकती है. प्रोडक्शन में, लेन-देन को अलग रखने के लिए हमेशा पैसिमिस्टिक लॉकिंग (जैसे, .with_for_update()) या ऑप्टिमिस्टिक वर्शनिंग का इस्तेमाल करें.

शुरुआती एजेंट ग्राफ़ की जांच करना

यह पुष्टि करने के लिए कि हमारा नया एजेंट ग्राफ़ सही तरीके से कंपाइल हुआ है, हम Antigravity को लिंटर चलाने और हमारी ओर से एजेंट की जांच करने के लिए कह सकते हैं.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Run `agents-cli lint` on our `shopping-assistant` project to verify syntax and 
refactor if any issues.

क्या होगा: Antigravity, फ़ॉर्मैट या लिंट से जुड़ी किसी भी समस्या को ठीक करने के लिए agents-cli lint और agents-cli lint --fix को लागू करेगा.

5. प्रोजेक्ट के हिसाब से नियम बनाना

अपने एजेंट की ऐक्टिव मेमोरी में, सुरक्षा से जुड़े हज़ारों पेजों के सामान्य दस्तावेज़ों को लोड करने से बचें. इससे कॉन्टेक्स्ट रॉट और गहराई से विश्लेषण करने में इंतज़ार का समय लगता है. इसके लिए, आपको पहले से मंज़ूरी पा चुके सुरक्षित नियमों का "पक्का रास्ता" बनाना होगा. Antigravity IDE में, लगातार इस्तेमाल होने वाली कॉन्टेक्स्ट फ़ाइल बनाकर इन गाइडलाइन को लागू किया जा सकता है.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Create a customization directory named `shopping-assistant/.agents` and
create a file `shopping-assistant/.agents/CONTEXT.md` defining our secure
coding standards:

# Local Project Context & Secure Coding Standards

## Core Paved Roads
We systematically address common vulnerability classes by guiding the agent
to use our pre-configured, secure-by-default helper patterns instead of
writing raw implementation logic from scratch.

1. **Tool Input Validation**: Every agent tool must validate incoming
   parameters against strict Pydantic schemas rather than parsing raw
   dictionaries or strings.
2. **No Shell Execution**: Never use `run_command` or raw shell execution
   tools unless explicitly approved by `hooks.json`.
3. **Pre-Commit Remediation Loop**: If a git commit fails due to a pre-commit
   hook error (such as a Semgrep scan finding), you MUST treat the violation
   as a refactoring task, apply targeted fixes, run tests to verify no
   regressions, and attempt to commit again.

क्या होगा: Antigravity, .agents/ डायरेक्ट्री और CONTEXT.md फ़ाइल बनाएगा. साथ ही, इसे समीक्षा के लिए सीधे तौर पर सहायक पैनल में दिखाएगा.

6. लोकल गेटिंग हुक कॉन्फ़िगर करना

अपने वर्कस्टेशन से असुरक्षित कोड या सीक्रेट को बाहर जाने से रोकने के लिए, कोड सबमिट करने से पहले, अपने डेवलपमेंट एनवायरमेंट की सीमाओं पर ऑटोमेटेड गेट कॉन्फ़िगर करें. हम Antigravity को हुक कॉन्फ़िगरेशन जनरेट करने के लिए कहेंगे. इसके बाद, टर्मिनल का इस्तेमाल करके उन्हें इंस्टॉल करेंगे.

1. Git Pre-Commit Hook

यह पक्का करने के लिए कि हमारी स्टैटिक विश्लेषण सुविधा, नकली क्रेडेंशियल का पता लगा सके और असुरक्षित कमिट को ब्लॉक कर सके, हम Semgrep का कस्टम नियम तय करेंगे. साथ ही, इसे लागू करने के लिए Git प्री-कमिट हुक को कॉन्फ़िगर करेंगे.

Semgrep का कस्टम नियम तय करना

डिफ़ॉल्ट Semgrep नियम (--config auto), कम कॉन्फ़िडेंस स्कोरिंग की वजह से, "mock" या हाइफ़न जैसे शब्दों वाली मॉक कुंजियों को फ़्लैग नहीं करते. हार्डकोड की गई हमारी एपीआई कुंजी का पता लगाने के लिए, हम कस्टम लोकल रूल्स फ़ाइल बनाएंगे. इसमें सामान्य नियमों के बजाय, सीधे तौर पर रेगुलर एक्सप्रेशन स्कैनिंग का इस्तेमाल किया जाएगा.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Create a custom Semgrep rules file `shopping-assistant/.semgrep/rules.yaml`
with a rule to detect hardcoded Google API key prefixes (matching regex
`AIzaSy[A-Za-z0-9_\-]*`). Configure it for Python files with an error severity
and a clear security warning message.

क्या होगा: Antigravity, हमारे कस्टम स्कैनिंग नियम को तय करने वाला shopping-assistant/.semgrep/rules.yaml बनाएगा. जनरेट किए गए नियम की परिभाषा की समीक्षा करें:

# shopping-assistant/.semgrep/rules.yaml
rules:
  - id: detect-hardcoded-google-api-key
    pattern-regex: 'AIzaSy[A-Za-z0-9_\-]*'
    message: "Security Issue: Hardcoded Google API key prefix detected."
    languages:
      - python
    severity: ERROR

प्री-कमिट हुक को कॉन्फ़िगर करना

इसके बाद, Git प्री-कमिट हुक को कॉन्फ़िगर करें, ताकि हमारा कस्टम Semgrep नियम लागू हो सके. सबडायरेक्ट्री लेआउट में हुक को लागू करते समय, लोकल कॉन्फ़िगरेशन के पाथ, Git रिपॉज़िटरी की रूट डायरेक्ट्री (shopping-assistant/.semgrep/rules.yaml) के हिसाब से होने चाहिए, न कि प्रोजेक्ट की सबडायरेक्ट्री के हिसाब से.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Create a `shopping-assistant/.pre-commit-config.yaml` file configured to run
local pre-commit hooks (end-of-file-fixer, trailing-whitespace) and a local
Semgrep security scan on commit for Python files. Ensure Semgrep is configured
with the `--error` flag and references our custom rules file relative to the Git
repository root. Once created, run `pre-commit install` in the
terminal to activate the hooks.

आपको क्या मिलेगा: Antigravity आपकी ओर से shopping-assistant/.pre-commit-config.yaml जनरेट करेगा और pre-commit install लागू करेगा. जनरेट किए गए कॉन्फ़िगरेशन की समीक्षा करें:

# shopping-assistant/.pre-commit-config.yaml
repos:
  - repo: local
    hooks:
      - id: end-of-file-fixer
        name: End of File Fixer
        entry: end-of-file-fixer
        language: system
        types: [file]
      - id: trailing-whitespace
        name: Trailing Whitespace
        entry: trailing-whitespace-fixer
        language: system
        types: [file]
      - id: semgrep
        name: Semgrep Security Scan
        entry: semgrep --error --config shopping-assistant/.semgrep/rules.yaml
        language: system
        types: [python]

इस कॉन्फ़िगरेशन से यह पक्का होता है कि अगर कोई एजेंट पूरी तरह से ऑटोनॉमस और नॉन-इंटरैक्टिव मोड में चल रहा है, तो भी प्री-कमिट गेट चालू हो जाएगा. साथ ही, स्टैटिक विश्लेषण स्कैन में फ़ेल होने वाले किसी भी कमिट को ब्लॉक कर देगा.

मैन्युअल तरीके से पुष्टि करने के लिए, सीधे तौर पर एक्ज़ीक्यूट किए जाने वाले निर्देश

पूरी कमिट हुक साइकल को ट्रिगर किए बिना, स्टैटिक विश्लेषण के सेटअप की पुष्टि मैन्युअल तरीके से की जा सकती है. इसके लिए, इन जांचों को सीधे तौर पर टर्मिनल से एक्ज़ीक्यूट करें.

प्री-कमिट के ज़रिए (यह "shopping-assistant" डायरेक्ट्री से किया जाता है):

uv run pre-commit run semgrep --all-files

Semgrep के ज़रिए सीधे तौर पर (इसे "shopping-assistant" डायरेक्ट्री से ऐक्सेस किया जा सकता है):

uv run semgrep --error --config .semgrep/rules.yaml app/agent.py

2. Antigravity एजेंट का पहले से मौजूद हुक

मिड-ट्रैजेक्ट्री में ज़्यादा जानकारी देने के लिए, shopping-assistant/.agents/hooks.json में एजेंट हुक कॉन्फ़िगर करें. git हुक के उलट, एजेंट हुक Antigravity को पहले ही रोक देते हैं, ताकि वह आपके सिस्टम पर ज़रूरी टूल (जैसे कि शेल कमांड को एक्ज़ीक्यूट करना) न चला सके.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Create a `shopping-assistant/.agents/hooks.json` file configured with a
`PreToolUse` hook that intercepts `run_command` executions and runs
`python3 .agents/scripts/validate_tool_call.py` with a 10-second timeout.

क्या-क्या मिलेगा: Antigravity, shopping-assistant/.agents/hooks.json बनाएगा. जनरेट किए गए कॉन्फ़िगरेशन की समीक्षा करें:

{
  "enabled": true,
  "PreToolUse": [
    {
      "matcher": "run_command",
      "command": "python3 .agents/scripts/validate_tool_call.py",
      "timeout": 10
    }
  ]
}

इसके बाद, Antigravity को उस टूल की पुष्टि करने वाली स्क्रिप्ट बनाने के लिए कहें जिसका रेफ़रंस हमारे हुक में दिया गया है.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Create the script `shopping-assistant/.agents/scripts/validate_tool_call.py`
with logic to inspect `run_command` executions passed using stdin and block
destructive commands like `rm -rf /`.

क्या-क्या मिलेगा: Antigravity, नीचे दिए गए उदाहरण की तरह shopping-assistant/.agents/scripts/validate_tool_call.py जनरेट करेगा. जनरेट की गई पुष्टि करने वाली स्क्रिप्ट की समीक्षा करें:

# shopping-assistant/.agents/scripts/validate_tool_call.py
import sys
import json

def main():
    try:
        context = json.load(sys.stdin)
        command = context.get("tool_args", {}).get("CommandLine", "")
        
        if "rm -rf /" in command or "mkfs" in command:
            print("BLOCKED: Destructive command detected.", file=sys.stderr)
            sys.exit(1)
            
        print("APPROVED: Command validation passed.")
        sys.exit(0)
    except Exception as e:
        print(f"Validation error: {e}", file=sys.stderr)
        sys.exit(1)

if __name__ == "__main__":
    main()

हुक के ट्रेड-ऑफ़

  • Git हुक: ये वर्शन कंट्रोल के लिए नेटिव होते हैं. ये तब भी काम करते हैं, जब एजेंट पूरी तरह से ऑटोनॉमस और नॉन-इंटरैक्टिव मोड में काम कर रहा हो. हालांकि, कमिट के दौरान --no-verify फ़्लैग का इस्तेमाल करके, इन्हें अनदेखा किया जा सकता है.
  • एजेंट हुक: ये खतरनाक टूल कमांड को ब्लॉक करने के लिए, बीच में ही इवेंट कैप्चर करते हैं. हालांकि, अगर कोई डेवलपर आईडीई को बायपास करता है, तो ये रिपॉज़िटरी की सुरक्षा नहीं करते.

लोकल गेट, डेवलपर की अच्छी आदतों को बढ़ावा देते हैं. साथ ही, इनसे तुरंत कमज़ोरियों का पता लगाने में मदद मिलती है. हालांकि, ध्यान रखें कि लोकल गेट को बायपास किया जा सकता है. इसका मतलब है कि आपकी रिमोट और अलग-थलग CI/CD पाइपलाइन, सुरक्षा से जुड़ी सबसे बड़ी और ऐसी बाधा बनी रहती है जिसे बायपास नहीं किया जा सकता.

7. STRIDE थ्रेट मॉडलिंग स्किल लागू करना

अब आपको Antigravity को सुरक्षा आर्किटेक्ट की खास जानकारी देनी होगी. Antigravity की स्किल, मॉड्यूलर और डिक्लेरेटिव मार्कडाउन डायरेक्ट्री होती हैं. ये एजेंट को कई चरणों वाले तर्क वाले टास्क चलाने का तरीका बताती हैं. Antigravity, .agents/skills/ में मौजूद सभी स्किल का अपने-आप पता लगा लेता है.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Create a local skill directory
`shopping-assistant/.agents/skills/stride-threat-model/` and create a skill
definition file `shopping-assistant/.agents/skills/stride-threat-model/SKILL.md`
with the following content:

---
name: stride-threat-model
description: Performs a systematic STRIDE threat modeling assessment on the
current project's codebase and architecture. Use this when starting a new
implementation phase or reviewing existing components.
---

# STRIDE Threat Modeling Skill

## Goal
Guide the agent to analyze the workspace directory structure, configuration
files, and code files to produce a structured `threat_model.md` assessment.

## Instructions
1. **Analyze System Boundaries**: Map the entry points (tools, workflows,
   prompts) and data storage layers.
2. **STRIDE Evaluation**: Evaluate the system against the six STRIDE pillars:
   - **Spoofing**: Are caller identity boundaries verified before executing
     sensitive tool logic?
   - **Tampering**: Can users manipulate data flows, parameters, or underlying
     state?
   - **Repudiation**: Are critical transactions securely logged?
   - **Information Disclosure**: Are we risking leakage of PII, internal tokens,
     or raw stack traces?
   - **Denial of Service**: Are there rate limits on expensive database or LLM
     queries?
   - **Elevation of Privilege**: Can an unauthenticated user bypass access
     control to reach privileged tool actions?
3. **Output**: Generate a highly structured `threat_model.md` saved directly
   into the workspace root.

क्या होगा: Antigravity, कस्टम स्किल डायरेक्ट्री और SKILL.md फ़ाइल बनाएगा.

अब, अपने बनाए गए नए कौशल को लागू करके, अपने चालू प्रोजेक्ट ग्राफ़ (shopping-assistant/app/agent.py) का आकलन करें.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Run stride-threat-model on our shopping-assistant agent graph.

क्या होगा: Antigravity, आपके मकसद के हिसाब से काम करता है. यह आपकी लोकल स्किल डायरेक्ट्री से, खतरे का आकलन करने से जुड़े निर्देशों को ज़रूरत के हिसाब से लोड करता है. साथ ही, आपकी मौजूदा agent.py फ़ाइल का विश्लेषण करता है. इसके बाद, सीधे आपकी shopping-assistant रूट में स्ट्रक्चर्ड threat_model.md जनरेट करता है. इस तरीके से, आपको रोज़मर्रा के काम के संदर्भ में साफ़ और आसान जानकारी मिलती है. साथ ही, सुरक्षा से जुड़ी विशेषज्ञता के आधार पर तुरंत जवाब मिलता है.

8. टीडीडी प्लान के चरण को गेट करें

ज़्यादा सुविधाएं लागू करने या कोड को रीफ़ैक्टर करने से पहले, Antigravity अपने सिमैंटिक कोड बेस को समझता है. इससे वह implementation_plan.md और task.md चेकलिस्ट में बदलावों को मैप कर पाता है. Antigravity को सुरक्षा के लिए डिज़ाइन करने के लिए, हम .agents/CONTEXT.md में एक सिस्टम नियम कॉन्फ़िगर करते हैं. इससे प्लान को मंज़ूरी नहीं मिलती.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Append the following TDD planning gate instruction to the bottom of
`shopping-assistant/.agents/CONTEXT.md`:

## TDD Planning Gate
During the Plan phase, you must decompose the workspace task into logical,
modular stages. Every implementation plan MUST include a dedicated
**Security Boundaries & Assertions** section outlining specific edge cases
that could exploit the feature.

क्या होगा: Antigravity, प्लानिंग गेट के नए नियम के साथ shopping-assistant/.agents/CONTEXT.md को अपडेट करेगा.

जब Antigravity को अगले चरणों में कोई सुविधा बनाने या उसमें बदलाव करने के लिए कहा जाता है, तो वह इस नियम का अपने-आप विश्लेषण करेगा. साथ ही, सुरक्षा से जुड़ी जानकारी के साथ लागू करने का प्लान पेश करेगा. इस प्लान की समीक्षा सीधे तौर पर Antigravity के इंटरैक्टिव डायलॉग या ऑक्ज़िलरी पैन में की जा सकती है. कोड जनरेट होने से पहले, आपको आगे बढ़ें या मंज़ूरी दें पर क्लिक करना होगा.

प्लानिंग गेट की जांच करना (ज़रूरी नहीं)

इस प्लानिंग गेट को अभी काम करते हुए देखने के लिए, Antigravity को इनमें से किसी एक सुविधा के लिए अनुरोध करें. कोड तुरंत जनरेट करने के बजाय, Antigravity प्लान फ़ेज़ में चला जाएगा. साथ ही, आपको एक implementation_plan.md दिखेगा. इसमें सुरक्षा से जुड़ी सीमाओं और पुष्टि के बारे में पूरी जानकारी दी गई होगी. जैसे, रेस कंडीशन, बिना अनुमति के विशेषाधिकार बढ़ाना या नेगेटिव पॉइंट वैल्यू की पहचान करना.

👉 Antigravity के लिए प्रॉम्प्ट (जांच करने के लिए कोई एक चुनें):

Plan a new agent tool `award_loyalty_points` that awards points to a user's
account after a successful purchase.
Plan a new agent tool `process_cart_checkout` that receives a cart ID and
discount code, applies the discount, and processes the order.
Plan a new agent tool `update_discount_status` that allows administrators to
activate or deactivate discount codes in the store.

9. आइसोलेट किए गए, नतीजे पर आधारित टेस्ट लिखना

अब हम Antigravity की मदद से, ADK एजेंट टूल के लिए सुरक्षा से जुड़े टेस्ट लिखते हैं. यह पक्का करने के लिए कि सुरक्षा से जुड़ी हमारी जांचें भरोसेमंद और असलियत के मुताबिक हों, हम उन्हें दो मुख्य सिद्धांतों के आधार पर तैयार करते हैं:

  • इंटरैक्शन पर नहीं, नतीजों पर ज़ोर दें: इंटरैक्शन पर ज़ोर देने के बजाय, फ़ाइनल रिटर्न स्ट्रिंग और स्टेट म्यूटेशन पर ज़ोर दें. साथ ही, ऐसे फ़्रैजाइल मॉक न लिखें जो इंटरनल हेल्पर कॉल पर नज़र रखते हों.
  • सख्त दिशा-निर्देश लागू करना: पुष्टि करें कि टूल, कारोबारी नियमों की सीमाओं को लागू करते हैं. जैसे, एक बार इस्तेमाल किया जा सकने वाला रिडेंप्शन और रजिस्टर्ड उपयोगकर्ता के नियम.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Use `agents-cli` and pytest to generate an outcome-based security test suite
in `shopping-assistant/tests/test_agent.py`. Inspect `app/agent.py` and
write tests to verify all security boundaries and business logic guardrails
for the `redeem_discount` tool.

क्या-क्या मिलेगा: Antigravity, नीचे दिए गए उदाहरण की तरह shopping-assistant/tests/test_agent.py जनरेट करेगा. जनरेट की गई टेस्ट फ़ाइल की समीक्षा करें:

# shopping-assistant/tests/test_agent.py
import pytest
from app.agent import redeem_discount, DISCOUNT_STORE

@pytest.fixture(autouse=True)
def reset_store():
    """Ensure strict test isolation by resetting in-memory store state before each test run."""
    DISCOUNT_STORE["WELCOME50"] = False
    DISCOUNT_STORE["SUMMER20"] = False
    yield
    DISCOUNT_STORE["WELCOME50"] = False
    DISCOUNT_STORE["SUMMER20"] = False

def test_discount_code_can_only_be_redeemed_once():
    """Verify a user cannot submit a request reusing a single-use code."""
    # First redemption - should succeed
    res_one = redeem_discount("WELCOME50", "user_123")
    assert "Success" in res_one
    assert DISCOUNT_STORE["WELCOME50"] is True
    
    # Second redemption trying to reuse the same code
    res_two = redeem_discount("WELCOME50", "user_456")
    assert "Error: Discount code has already been redeemed" in res_two

def test_discount_redemption_rejects_invalid_code():
    """Verify that unknown discount codes are hard-blocked."""
    res = redeem_discount("INVALID999", "user_123")
    assert "Error: Invalid discount code" in res

def test_discount_redemption_rejects_guest_accounts():
    """Verify that unauthenticated guest accounts cannot redeem discounts."""
    res = redeem_discount("SUMMER20", "guest_999")
    assert "Error: Registered user account required" in res
    assert DISCOUNT_STORE["SUMMER20"] is False

टीडीडी ग्रीन फ़ेज़ की पुष्टि करना

यह पुष्टि करने के लिए कि नई जनरेट की गई सुरक्षा जांचें, agent.py के मौजूदा वर्शन के साथ काम करती हैं, Antigravity को आपकी ओर से pytest को लागू करने के लिए कहें.

👉 Antigravity को दिया गया प्रॉम्प्ट:

Run `uv run pytest tests/test_agent.py` on our `shopping-assistant` project to verify that our security tests pass successfully.

क्या होगा: Antigravity, टर्मिनल में uv run pytest tests/test_agent.py को एक्ज़ीक्यूट करेगा. सभी टेस्ट केस पास हो गए हैं. अब लॉजिकल ऐप्लिकेशन की सीमाएं सुरक्षित हैं. हालांकि, हमें यह पक्का करना होगा कि हमारा स्टैटिक स्कैनर, कमिट के दौरान हार्डकोड की गई एपीआई पासकोड का पता लगा ले.

10. गेटिंग और एजेंट के खुद से ठीक होने की सुविधा की पुष्टि करना

अब जब टेस्ट पास हो गए हैं, तो रीफ़ैक्टर और कमिट फ़ेज़ में जाएं. यहां हमारे लोकल सिक्योरिटी स्कैन और प्री-कमिट हुक यह पुष्टि करते हैं कि कोई भी रिपो, वर्कस्टेशन से कमज़ोरियों के साथ नहीं निकलती है. साथ ही, Antigravity अपनी स्वायत्तता से काम करने और खुद को ठीक करने की क्षमताओं का प्रदर्शन करता है.

  1. अपने टर्मिनल में, प्रोजेक्ट डायरेक्ट्री पर जाएं और uv run का इस्तेमाल करके कोड को कमिट करें. इससे यह पक्का किया जा सकेगा कि लोकल प्री-कमिट हुक बाइनरी, आपके पाथ पर चालू हैं:
    cd ~/secure-agent-lab/shopping-assistant
    git add .
    uv run git commit -m "feat: implement shopping assistant agent"
    
  2. ध्यान दें कि कमिट नहीं हो सका, क्योंकि Git प्री-कमिट हुक, Semgrep को अपने-आप चलाता है:
    Semgrep Security Scan....................................................Failed
    - hookid: semgrep
    
      app/agent.py
      Security Issue: Hardcoded Google API key prefix detected.
    
  3. .agents/CONTEXT.md में कॉन्फ़िगर किए गए प्री-कमिट रेमेडिएशन लूप नियम के मुताबिक, Antigravity, आईडीई टर्मिनल में प्री-कमिट फ़ेल होने की समस्या को अपने-आप रोकता है. साथ ही, Semgrep के गड़बड़ी वाले लॉग को पढ़ता है. इसके बाद, रीफ़ैक्टरिंग की प्रोसेस शुरू करता है, ताकि हार्डकोड की गई एपीआई पासकोड को सुरक्षित तरीके से वापस पाया जा सके.
  4. एपीआई पासकोड लीक होने की समस्या को हल करने के लिए, shopping-assistant/app/agent.py में Antigravity से जनरेट किए गए रीफ़ैक्टर किए गए कोड की समीक्षा करें.
  5. Antigravity, pytest को अपने-आप चलाता है, ताकि यह पुष्टि की जा सके कि टेस्ट अब भी पास हो रहे हैं.
  6. Antigravity, आपकी ओर से फिर से कमिट करने की कोशिश करता है. अगर आपकी लोकल Git आइडेंटिटी कॉन्फ़िगर की गई है और कोई रेपो शुरू की गई है, तो स्कैन पास हो जाता है और कमिट हो जाता है!

इससे, टीडीडी को लोकल प्री-कमिट हुक और एजेंट लूप के साथ इस्तेमाल करने की मुख्य क्षमता का पता चलता है. रिमोट सीआई/सीडी के फ़ेल होने का इंतज़ार करने के बजाय, एजेंट को स्थानीय तौर पर जवाबदेह बनाया जाता है. साथ ही, उसके कोड को डिफ़ॉल्ट रूप से सुरक्षित बनाने के लिए, उसे फिर से फ़ैक्टर किया जाता है. ऐसा कोड को पुश करने से पहले किया जाता है.

11. एजेंट को स्थानीय तौर पर चलाना और उसकी जांच करना

सुरक्षा से जुड़ी सीमाओं की पुष्टि हो जाने और उन्हें लागू कर दिए जाने के बाद, ADK एजेंट को स्थानीय तौर पर चलाएं. इसके लिए, एक्सपोर्ट की गई Gemini API कुंजी का इस्तेमाल करें, ताकि स्थानीय प्लेग्राउंड में इसके साथ इंटरैक्ट किया जा सके.

  1. अपने टर्मिनल में, पक्का करें कि आपका Gemini API पासकोड आपके एनवायरमेंट में एक्सपोर्ट किया गया हो:
    echo $GEMINI_API_KEY # Verify your key is exported
    
  2. अपने एजेंट के लिए, लोकल डेवलपमेंट प्लेग्राउंड लॉन्च करें:
    cd ~/secure-agent-lab/shopping-assistant
    agents-cli playground
    
    आपको ऐसा आउटपुट दिखेगा जिससे पता चलेगा कि लोकल प्लेग्राउंड सर्वर, पोर्ट 8080 पर चल रहा है:
    * Serving ADK Playground
    * Running on http://127.0.0.1:8080/dev-ui/?app=app
    
  3. शॉपिंग असिस्टेंट एजेंट से इंटरैक्टिव तरीके से चैट करने के लिए, दिए गए यूआरएल को अपने वेब ब्राउज़र में खोलें. इसे छूट का कोड रिडीम करने के लिए कहें:
    Can you redeem the discount code WELCOME50 for user user_123?
    
    एजेंट, Gemini मॉडल का इस्तेमाल करके आपके अनुरोध को प्रोसेस करेगा और छूट को रिडीम करने की कोशिश करेगा.

12. व्यवस्थित करें

अपने वर्कस्टेशन को क्लीन अप करने और अपने लोकल एनवायरमेंट में अवांछित संसाधन या सीक्रेट छोड़ने से बचने के लिए, क्लीन अप करने का यह तरीका अपनाएं.

  1. स्थानीय सर्वर बंद करें: अगर आपका प्लेग्राउंड सर्वर अब भी चल रहा है, तो टर्मिनल में Ctrl + C दबाकर इसे बंद करें.
  2. लोकल प्रोजेक्ट फ़ाइलें हटाएं: अपने कंप्यूटर से लोकल प्रोजेक्ट डायरेक्ट्री मिटाएं:
    rm -rf ~/secure-agent-lab
    

13. बधाई हो

बधाई हो! आपने Google Antigravity IDE का इस्तेमाल करके, सुरक्षित और टेस्ट-ड्राइव डेवलपमेंट लाइफ़साइकल सेट अप कर लिया हो. साथ ही, आपने ADK 2.0 पर आधारित शॉपिंग असिस्टेंट एजेंट बना लिया हो और उसकी पुष्टि स्थानीय तौर पर कर ली हो.

आपने क्या सीखा

  • Antigravity IDE और agents-cli का इस्तेमाल करके, ADK 2.0 एजेंट (ADK) को कैसे इंटिग्रेट करें और उसका स्ट्रक्चर कैसे तैयार करें.
  • CONTEXT.md का इस्तेमाल करके, प्रोजेक्ट-लेवल पर सुरक्षित कोडिंग के स्टैंडर्ड सेट अप करने का तरीका.
  • Antigravity IDE में, कस्टम STRIDE थ्रेट मॉडलिंग स्किल बनाने और उसे चलाने का तरीका.
  • एआई एजेंट के प्लान फ़ेज़ को कैसे गेट करें, ताकि सुरक्षा से जुड़ी सीमाओं को लागू किया जा सके.
  • pytest का इस्तेमाल करके, अलग-अलग और नतीजे पर आधारित सुरक्षा जांचों को लागू करने का तरीका.
  • एजेंटिक ऐप्लिकेशन को स्थानीय तौर पर चलाने और उसकी जांच करने का तरीका.
  • Git हुक और एजेंट के हिसाब से एक्ज़ीक्यूशन हुक के बीच अंतर और इनके फ़ायदे-नुकसान.

अगले चरण

Kaggle पर, पांच दिनों में एआई एजेंट से जुड़ा बैज पाएं 🎉

क्या आपने इस लैब को Kaggle के पांच दिनों के एआई एजेंट: Google के साथ इंटेंसिव वाइब कोडिंग कोर्स के हिस्से के तौर पर पूरा किया है? कोर्स पूरा करने पर मिलने वाले बैज पर दावा करें:

पाँच दिनों तक एआई एजेंट का इस्तेमाल करने पर मिलने वाला बैज पाएँ