Abstract
This paper presents a serverless architecture that overcomes the stateless nature and 6-minute execution limit of Google Apps Script (GAS). By configuring a 1-second immediate timeout in UrlFetchApp loopback calls, an orchestrator dispatches background tasks and terminates immediately. This design frees up the caller's execution quota while the target Web App runs to completion in an isolated container. Combined with a transactional Google Sheets state machine, this design supports self-perpetuating parallel MapReduce runs and multi-turn, state-hydrated generative AI agent networks without external compute infrastructure.
Introduction
Google Apps Script (GAS) is the premier low-code platform for integrating, automating, and extending Google Workspace Ref. While universally accessible and free, GAS is constrained by strict runtime limits—most notably a hard 6-minute execution cap per script instance and a synchronous, stateless container lifecycle Ref.
Although the compilation shift to the V8 engine significantly reduced processing costs, handling large-scale ETL pipelines, parallel computations, or multi-agent generative AI simulations remains highly challenging. Traditional workarounds, such as time-driven triggers, suffer from high initialization latency, trigger omissions, and strict platform quotas.
Earlier asynchronous attempts, such as the fetchAll-based RunAll framework, still remained bound by the parent container's 6-minute ceiling. However, the addition of the timeoutSeconds parameter to the UrlFetchApp utility Ref enables a new approach. Integrating aggressive timeouts, self-referential Web App loopbacks Ref, and a shared spreadsheet state ledger makes it possible to execute highly parallel, self-perpetuating, and infinite MapReduce operations that easily bypass the 6-minute wall.
Repository
The complete, refactored production source code, containing full implementations of the asynchronous engine, concurrency controllers, and state preservation layers, is hosted on GitHub:
https://github.com/tanaikech/gas-asynchronous-mapreduce-broker
Theoretical Foundations: Bypassing the 6-Minute Wall
To model this bypass mathematically, let N represent the total number of long-running computational or inferential tasks, and T_task represent the processing duration of a single task. In a traditional, single-threaded synchronous execution model, the total execution time T_sync is represented by:
T_sync = T_task,1 + T_task,2 + ... + T_task,N + T_overhead_sync
Under Google's runtime policies Ref, execution is forcefully terminated if:
T_sync >= 360 seconds
Even with synchronous parallelization utilizing UrlFetchApp.fetchAll Ref, the system remains bound by the slowest worker in the batch:
T_para = max(T_task,1, T_task,2, ..., T_task,N) + T_overhead_para
If the slowest task exceeds 360 seconds, or if the parent thread's aggregation overhead exceeds the limit, the parent container crashes, causing data loss.
A. The 1-Second Immediate Timeout Hack
Our paradigm bypasses this constraint by introducing a non-blocking asynchronous dispatch model. We configure UrlFetchApp.fetch with a parameter configuration of timeoutSeconds: 1 and muteHttpExceptions: true. When pointed at the script's own Web App URL Ref, the following sequence occurs:
- The orchestrator dispatches an HTTP POST request to the Web App container.
- The HTTP request is established on Google's internal infrastructure, triggering the initialization of a separate, isolated target Web App container (Worker).
- At exactly 1.0 seconds, the orchestrator's UrlFetchApp client raises a local timeout exception. This exception is caught and suppressed.
- Crucially, the target Web App container does not terminate. Because the HTTP handshake was completed and the payload was accepted, Google's infrastructure executes the target doPost function to completion, granting it an isolated 6-minute execution window.
Let T_hop represent the combined latency of the Web App invocation, authorization handshake, and spreadsheet state-saving. Because the orchestrator dispatches tasks asynchronously with a 1-second timeout, the orchestrator's active running time T_orch scales at O(1) relative to task execution:
T_orch = sum(T_timeoutSeconds for j from 1 to M) + T_overhead_orch ≈ M \* 1s + T_overhead_orch
where M is the number of active dispatches in the current batch. The actual heavy computational tasks run entirely in parallel background containers:
T_background = max(T_task,1, T_task,2, ..., T_task,C) + T_hop
where C is the concurrency limit. Once a worker finishes, it writes its state to the Shared Sheet and executes an autonomous loopback HTTP call to wake up the Dispatcher. The orchestrator container is thus recycled continuously, dodging the 360-second limit entirely.
B. Decoupling Time-Driven Triggers
Consequently, when this loopback mechanism is initiated via a Time-driven Trigger, the trigger execution context terminates cleanly within 1.0 seconds (consuming virtually zero quota), while the background Web App container obtains a full, independent execution window that bypasses the trigger's inherent time limits. This represents an important optimization for building highly resilient, long-running systems within Google Workspace.
Pioneering Architectural Innovations
This parallel execution framework relies on six core engineering innovations to manage state, concurrency, and error handling.
1. Self-Perpetuating Token Bucket Queue
To prevent API rate limits (such as Google’s HTTP 429 exceptions or lock contention), tasks are not dispatched all at once. Instead, an independent background daemon (the Dispatcher) acts as a load balancer, continuously auditing the active queue and enforcing a strict concurrency cap (e.g., maximum of 5 concurrent workers). When a worker completes its task, it executes an autonomous loopback HTTP call to wake up the Dispatcher, which then launches the next pending task in the queue.
2. Persistent Memory State Machine
Since GAS Web App containers are stateless and initialize a clean memory space on every doPost invocation, in-memory state tracking is impossible. Our design repurposes Google Sheets as a persistent, transactional memory engine. Every task state (PENDING, RUNNING, COMPLETED, ERROR, CANCELLED) and historical conversation log is written directly to the sheet, providing a durable ledger that survives container restarts.
3. Fan-In Race Condition Immunity
In the Reducer phase, when multiple workers finish almost simultaneously, they all attempt to wake up the Dispatcher to trigger the final consolidation step. This introduces a race condition where multiple threads might try to run the reduction logic concurrently. Our architecture resolves this by using the GAS sheet creation API as a mutex. The code attempts to programmatically insert a uniquely named results sheet (insertSheet). If a duplicate sheet creation is attempted, the underlying engine throws a catchable exception, which immediate aborts duplicate threads and ensures only one thread executes the final reduction.
4. Lock-Free Parallel LLM Inference
In multi-agent environments, blocking execution while waiting for external API calls dramatically reduces throughput. Our framework completely bypasses global script locks during long-lived LLM network communications. Instead, workers execute their API queries in parallel, holding a localized DocumentLock only during the sub-second write-back operations to the shared spreadsheet, maximizing concurrency.
5. Dynamic LLM-Driven Self-Correction
If a worker encounters an error (such as an API timeout, JSON parsing fault, or safety block), it writes the failure trace to the queue log. Rather than crashing the entire pipeline, the Dispatcher intercepts the failure and launches a specialized error-correction LLM loop. This sub-agent analyzes the failed prompt and the resulting error message, dynamically rewrites the prompt to bypass the issue, and queues the task for an automated retry.
6. Immortal Log Rotation
Continuous logging can quickly exceed the physical limit of Google Sheets (10 million cells), degrading performance. To prevent this, the logging engine monitors raw log row counts in real-time. If the log sheet exceeds a safe operating threshold (e.g., 1000 rows), the engine automatically truncates and rotates older entries, maintaining fast, lightweight write operations indefinitely.
System Topologies & Workflows
Our research validates this architecture across two distinct design patterns.
Topology 1: Asynchronous MapReduce Broker
This topology establishes a decentralized broker that processes heavy synthetic tasks concurrently, using a Shared Google Sheet as an atomic transactional queue ledger Ref.
Topology 2: Stateful Multi-Turn Agent Network
This configuration manages conversational states across multi-step execution. Using the GASADK library Ref, it orchestrates multiple specialized AI agents (Lead Engineer, Resource Director, Colony Governor) through progressive conversational turns, maintaining context and execution histories on a shared ledger.
Conversational State Transitions
The diagram below shows how conversational state ($\mathcal{H}$) accumulates across progressive execution turns, demonstrating how history is preserved and serialized across stateless containers.
Practical Deployment & Verification Guide
Follow these steps to deploy and verify this architecture within a Google Workspace environment.
A. Step-by-Step Configuration and Deployment
1. Host Ledger Setup
- Create a new Google Spreadsheet to serve as the shared, persistent state ledger.
- Open Extensions > Apps Script, and clear all boilerplate code from the editor.
2. External Library Integration (Required for Topology 2)
- In the left-hand sidebar of the GAS editor, click the Add a library (+) icon.
- In the lookup field, enter the official
GASADKProject Key Ref:1w2mwhWQd4_6rom-UBRPD8gayBoqGH_87awSBVqGI8DdaQI_pOeSuGYDu - Select the latest release version, set the Identifier to
GASADK, and click Add.
3. Credential Storage
- Navigate to Project Settings (the gear icon on the left panel).
- Add a script property named
GEMINI_API_KEYand populate it with a valid API key obtained from Google AI Studio.
4. Code Provisioning
- Copy the relevant script files (obtained from the GitHub repository Ref) into the editor.
5. Web App Publishing
- Click Deploy > New Deployment in the upper right.
- Click the gear icon and choose Web App.
- Configure the deployment parameters:
- Execute as: Me (the author account, ensuring access to local sheets and property services)
-
Who has access: Anyone (allowing the loopback HTTP calls to communicate with the
/execendpoint anonymously)
- Click Deploy, authorize the requested Drive, Spreadsheet, and External URL scopes, and copy the generated Web App URL.
6. Loopback Target Mapping
- Return to the script editor and paste the copied Web App URL into the global
WEB_APP_URLvariable. - Critical Step: Because Web App versions are frozen on deployment, any modification to code requires creating a new version. Click Manage Deployments, select the active deployment, click the pencil icon, set the version to New version, and redeploy.
B. Verification Testing
-
Execution Verification: In the script editor, select
startTest4_1(for Topology 1) orstartUnifiedStatefulMapReduce(for Topology 2) and click Run. Navigate back to the spreadsheet. You will see the real-time creation of the log sheets and the concurrent execution of tasks as they transition fromPENDINGtoRUNNINGandCOMPLETED. -
Emergency Halt Verification: While background tasks are active, run the
emergencyHaltNetworkfunction. Verify that all remainingPENDINGqueue entries immediately transition toCANCELLEDand execution halts gracefully within one cycle.
C. Modular Task Customization (Decoupled Mission Profiling)
To modify the task definitions or agent roles, you do not need to rewrite the underlying execution engine. The engine is completely decoupled from the execution logic by parsing a declarative MISSION_BLUEPRINT configuration.
For instance, to re-engineer the system from the Martian colonization task to an AI Venture Strategy Startup Roadmap, you can update the MISSION_BLUEPRINT object as follows:
/**
* MISSION_BLUEPRINT
* Declaratively defines the agent structure, step counts, prompts, and reduction rules.
*/
const MISSION_BLUEPRINT = {
MAX_TURNS: 3,
MAX_RETRIES: 2,
AGENTS: [
{
id: "CHAIN-STARTUP-CEO",
initialPrompt:
"You are the CEO of a medical AI startup. Detail the core business model, value proposition, and customer acquisition strategy.",
},
{
id: "CHAIN-STARTUP-CTO",
initialPrompt:
"You are the CTO of a medical AI startup. Detail the software architecture, data privacy measures, and chosen model pipeline.",
},
{
id: "CHAIN-STARTUP-CMO",
initialPrompt:
"You are the CMO of a medical AI startup. Detail the product launch marketing strategy, branding, and conversion funnel.",
},
],
TURN_PROMPTS: {
2: "COMPETITION UPDATE: A tech giant has announced a similar free medical AI feature. Based on your previous plans, how do we pivot and build a defensive moat?",
3: "PITCH STAGE: The pivot is finalized. Write your department's definitive operational plan, synthesizing the baseline startup idea with the new defensive moat strategies.",
},
REDUCER: {
id: "CHAIN-VC-LEAD-INVESTOR",
prompt:
"You are the Lead VC Investor analyzing this startup. Synthesize the pitch reports from the CEO, CTO, and CMO into a single 'Start-up Investment Prospectus & 12-Month Execution Roadmap'. Highlight risk mitigations.\n\n",
},
};
Saving this configuration and running the initializer dynamically launches the CEO, CTO, and CMO agents in parallel, injects the competitive pivot threat on Turn 2, and aggregates the results into a cohesive VC prospectus in FinalResult_Plan3.
Experimental Results & Forensic Analysis
We evaluate both topologies using empirical runtime execution data.
A. Quantitative Analysis: Topology 1
We configured Topology 1 with 20 heavy synthetic tasks, each simulating a 120-second workload. Using our token-bucket design, the concurrency limit C was capped at 5.
Analyzing the execution ledger QueueLog_4_1, we observe highly coordinated state progression:
-
Batch 1 (Tasks 1–5): Dispatched at
06:06:45.401Zthrough06:06:45.434Z. Each of these five worker threads executed in parallel. -
Batch 2 (Tasks 6–10): Registered completed states exactly 120 seconds later, with the next batch dispatched at
06:08:49.712Z. -
Batch 3 (Tasks 11–15): Dispatched at
06:10:54.481Z. -
Batch 4 (Tasks 16–20): Dispatched at
06:12:58.694Zand completed at06:12:58.739Z. -
Reducer Aggregation: The final merge step was executed at
06:15:05.096Z.
Total elapsed wall time was 500 seconds (8 minutes and 20 seconds). In a standard synchronous thread, this execution would have suffered a hard crash at exactly 360 seconds. Our asynchronous architecture sustained operations past the execution wall, completing all 20 tasks, sorting the resulting cryptographic tokens sequentially, and outputting a structured manifest in FinalResult_4_1.
B. Quantitative Analysis: Topology 2
We evaluated the multi-agent coordination of Topology 2 under dynamic environmental shifts using execution logs from Plan 3.3.
1. Latency Analysis
The entire state-space transitioned from initial manual trigger to finalized manifesto synthesis in 53.39 seconds (from the start log at 02:47:46.791Z to the system complete log at 02:48:40.181Z). Under the legacy synchronous paradigm (Plan 3.0), the identical workflow required 71.34 seconds, demonstrating an absolute speedup of 25.16%. By reducing the idle worker loopback delay (WORKER_START_DELAY_MS) from 3000ms to 1000ms in Plan 3.3, we eliminated 8.0 seconds of unnecessary container wait states, driving the system close to its mathematical minimum latency bound.
2. Verification of Parallelism
To confirm actual multi-threaded execution within Google’s infrastructure, we audited the start/end timestamps of Turn 1 (Step 1/3) agents:
-
CHAIN-MARS-ENGINEER: Executed from02:47:52.709Zto02:47:55.350Z(duration = 2.641 seconds) -
CHAIN-MARS-GOVERNOR: Executed from02:47:52.789Zto02:47:56.323Z(duration = 3.534 seconds) -
CHAIN-MARS-RESOURCES: Executed from02:47:54.663Zto02:47:57.695Z(duration = 3.032 seconds)
Because all three workers started execution within an 1.8-second window and processed their tasks concurrently, the total step duration was bounded by the slowest task:
T_Step1 = max(2.641, 3.534, 3.032) = 3.534 seconds
In a sequential, single-threaded engine, this step would require the sum of all tasks:
T_Sequential = 2.641 + 3.534 + 3.032 = 9.207 seconds
Our parallel dispatcher achieved a 61.6% latency reduction on Turn 1 alone. This efficiency gain compounded across subsequent turns, demonstrating the scalability of our approach.
3. Output Synthesis & Semantic Integrity
The synthesis step was evaluated using the final strategic manifesto written to FinalResult_Plan3. The Reducer successfully hydrated and reconciled the outputs of the three agents:
- The Lead Engineer's recommendation of Decentralized Grid Isolation.
- The Resource Director's proposal of migrating farming into Subterranean Lava Tubes.
- The Colony Governor's implementation of Habitat Consolidation.
The resulting "Mars Master Manifesto" synthesized these domain-specific strategic recommendations into a unified, chronological action plan (Phases I–III). This successful aggregation confirms the semantic consistency and preservation of conversational history throughout the parallel, stateless execution cycles of the network.
Practical Considerations, Security, and Trade-offs
Evaluating this framework for production workloads requires analyzing its performance characteristics and operational trade-offs compared to traditional cloud environments.
A. The "Last Resort" Theorem
Executing complex applications entirely within Google Apps Script introduces significant operational overhead compared to dedicated cloud compute engines such as AWS Lambda, GCP Cloud Run, or Cloud Functions. Spawning GAS Web Apps, validating OAuth tokens, initiating Spreadsheet or Property service I/O, calling LLM endpoints, and writing state back to the shared ledger introduces a latency penalty of approximately 3 to 5 seconds per execution hop. This latency is caused by container cold starts, authorization handshakes, and API serialization.
Consequently, this architecture is computationally inefficient and should not be used when native external cloud options are viable. Instead, it serves as "The Ultimate Last Resort" for compliance-restricted enterprise scenarios. When strict organizational regulations prohibit data egress to external, non-Workspace cloud environments, this framework allows developers to build secure, parallel, and self-healing systems directly within the compliant Workspace boundary.
B. System Quotas and Concurrency Limits
Architectural planning must account for the platform's execution and storage limits:
- Web App Concurrency Throttling: Google limits concurrent connections to deployed Web Apps to 30. Our implementation caps active workers at 10 (Exp 1) or 3 (Exp 2) to maintain a safe operating margin and prevent connection dropouts.
-
Document Lock Scaling:
LockServicetimeouts are set to 30 seconds. While sufficient for low-concurrency runs, lock contention scales non-linearly with task volume. We mitigate this by holding document-level locks only during sub-second write-back operations, bypassing script-level locks entirely during long API calls. -
API Allotments: Operational life depends on the host's daily
UrlFetchAppquotas (20,000 requests for consumer accounts; 100,000 requests for Google Workspace accounts). - Persistent Ledger Limits: Writing comprehensive execution logs can lead to spreadsheet bloating, eventually hitting Google Sheets' limit of 10 million cells. Our architecture resolves this by monitoring log row counts and dynamically rotating older entries to maintain optimal I/O speeds.
Conclusion & Future Outlook
We have presented an asynchronous, self-healing, and self-perpetuating MapReduce framework designed to run natively within Google Apps Script. By utilizing a 1-second timeout loopback protocol, our design bypasses the legacy 6-minute wall-time limit, allowing workflows to scale indefinitely.
This design opens up new development possibilities for secure, in-suite architectures. Future work will investigate integrating this loopback model with the Model Context Protocol (MCP) and Agent-to-Agent (A2A) networks. This will lay the groundwork for secure, federated agent networks capable of autonomous negotiation and execution entirely within enterprise office suites.
Summary
This paper presents an asynchronous serverless framework that overcomes the stateless nature and 6-minute execution limit of Google Apps Script (GAS) Ref. By configuring a 1-second timeout parameter in UrlFetchApp loopback calls Ref, the orchestrator can dispatch background tasks and terminate immediately. This frees up the caller's execution quota while the target Web App runs to completion in an isolated container. Combined with a transactional Google Sheets state machine Ref, this design supports self-perpetuating parallel MapReduce runs and multi-turn, state-hydrated generative AI agent networks without external compute infrastructure.
Key Takeaways
- Temporal Bypass: A 1-second immediate timeout loopback call allows the orchestrator to dispatch background worker threads and terminate immediately, dodging the 6-minute execution limit.
- Shared Memory Ledger: Google Sheets serves as a persistent, transactional memory engine, enabling state-machine tracking and conversation history hydration across isolated execution contexts.
- Dynamic Self-Healing: Integrated LLM exception handlers analyze execution errors (such as formatting faults or API limits) and dynamically rewrite prompt strategies to auto-retry and heal failing threads.
- Decoupled Workload Architecture: The orchestration engine is fully decoupled from task specifications, allowing developers to redefine entire multi-agent behaviors by updating a declarative configuration.
- Secure Enterprise Compliance: Despite a 3-to-5 second latency overhead per execution hop, this architecture provides a secure, compliant serverless platform for organizations that restrict data egress to external cloud systems.






Top comments (0)